879a051a59182af872e17eee4db5070b9879a423
[openssl.git] / test / test_test.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * Copyright (c) 2017 Oracle and/or its affiliates.  All rights reserved.
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16
17 #include <openssl/opensslconf.h>
18 #include <openssl/err.h>
19 #include <openssl/crypto.h>
20
21 #include "e_os.h"
22 #include "testutil.h"
23
24 #define C(l, b, t)                                      \
25     if ((t) != b) {                                     \
26         fprintf(stderr, "FATAL : %s != %d\n", #t, b);   \
27         goto l;                                         \
28     }
29
30 static int test_int(void)
31 {
32     C(err, 1, TEST_int_eq(1, 1));
33     C(err, 0, TEST_int_eq(1, -1));
34     C(err, 1, TEST_int_ne(1, 2));
35     C(err, 0, TEST_int_ne(3, 3));
36     C(err, 1, TEST_int_lt(4, 9));
37     C(err, 0, TEST_int_lt(9, 4));
38     C(err, 1, TEST_int_le(4, 9));
39     C(err, 1, TEST_int_le(5, 5));
40     C(err, 0, TEST_int_le(9, 4));
41     C(err, 1, TEST_int_gt(8, 5));
42     C(err, 0, TEST_int_gt(5, 8));
43     C(err, 1, TEST_int_ge(8, 5));
44     C(err, 1, TEST_int_ge(6, 6));
45     C(err, 0, TEST_int_ge(5, 8));
46     return 1;
47
48 err:
49     return 0;
50 }
51
52 static int test_uint(void)
53 {
54     C(err, 1, TEST_uint_eq(3u, 3u));
55     C(err, 0, TEST_uint_eq(3u, 5u));
56     C(err, 1, TEST_uint_ne(4u, 2u));
57     C(err, 0, TEST_uint_ne(6u, 6u));
58     C(err, 1, TEST_uint_lt(5u, 9u));
59     C(err, 0, TEST_uint_lt(9u, 5u));
60     C(err, 1, TEST_uint_le(5u, 9u));
61     C(err, 1, TEST_uint_le(7u, 7u));
62     C(err, 0, TEST_uint_le(9u, 5u));
63     C(err, 1, TEST_uint_gt(11u, 1u));
64     C(err, 0, TEST_uint_gt(1u, 11u));
65     C(err, 1, TEST_uint_ge(11u, 1u));
66     C(err, 1, TEST_uint_ge(6u, 6u));
67     C(err, 0, TEST_uint_ge(1u, 11u));
68     return 1;
69
70 err:
71     return 0;
72 }
73
74 static int test_char(void)
75 {
76     C(err, 1, TEST_char_eq('a', 'a'));
77     C(err, 0, TEST_char_eq('a', 'A'));
78     C(err, 1, TEST_char_ne('a', 'c'));
79     C(err, 0, TEST_char_ne('e', 'e'));
80     C(err, 1, TEST_char_lt('i', 'x'));
81     C(err, 0, TEST_char_lt('x', 'i'));
82     C(err, 1, TEST_char_le('i', 'x'));
83     C(err, 1, TEST_char_le('n', 'n'));
84     C(err, 0, TEST_char_le('x', 'i'));
85     C(err, 1, TEST_char_gt('w', 'n'));
86     C(err, 0, TEST_char_gt('n', 'w'));
87     C(err, 1, TEST_char_ge('w', 'n'));
88     C(err, 1, TEST_char_ge('p', 'p'));
89     C(err, 0, TEST_char_ge('n', 'w'));
90     return 1;
91
92 err:
93     return 0;
94 }
95
96 static int test_uchar(void)
97 {
98     C(err, 1, TEST_uchar_eq(49, 49));
99     C(err, 0, TEST_uchar_eq(49, 60));
100     C(err, 1, TEST_uchar_ne(50, 2));
101     C(err, 0, TEST_uchar_ne(66, 66));
102     C(err, 1, TEST_uchar_lt(60, 80));
103     C(err, 0, TEST_uchar_lt(80, 60));
104     C(err, 1, TEST_uchar_le(60, 80));
105     C(err, 1, TEST_uchar_le(78, 78));
106     C(err, 0, TEST_uchar_le(80, 60));
107     C(err, 1, TEST_uchar_gt(88, 37));
108     C(err, 0, TEST_uchar_gt(37, 88));
109     C(err, 1, TEST_uchar_ge(88, 37));
110     C(err, 1, TEST_uchar_ge(66, 66));
111     C(err, 0, TEST_uchar_ge(37, 88));
112     return 1;
113
114 err:
115     return 0;
116 }
117
118 static int test_long(void)
119 {
120     C(err, 1, TEST_long_eq(123l, 123l));
121     C(err, 0, TEST_long_eq(123l, -123l));
122     C(err, 1, TEST_long_ne(123l, 500l));
123     C(err, 0, TEST_long_ne(1000l, 1000l));
124     C(err, 1, TEST_long_lt(-8923l, 102934563l));
125     C(err, 0, TEST_long_lt(102934563l, -8923l));
126     C(err, 1, TEST_long_le(-8923l, 102934563l));
127     C(err, 1, TEST_long_le(12345l, 12345l));
128     C(err, 0, TEST_long_le(102934563l, -8923l));
129     C(err, 1, TEST_long_gt(84325677l, 12345l));
130     C(err, 0, TEST_long_gt(12345l, 84325677l));
131     C(err, 1, TEST_long_ge(84325677l, 12345l));
132     C(err, 1, TEST_long_ge(465869l, 465869l));
133     C(err, 0, TEST_long_ge(12345l, 84325677l));
134     return 1;
135
136 err:
137     return 0;
138 }
139
140 static int test_ulong(void)
141 {
142     C(err, 1, TEST_ulong_eq(919ul, 919ul));
143     C(err, 0, TEST_ulong_eq(919ul, 10234ul));
144     C(err, 1, TEST_ulong_ne(8190ul, 66ul));
145     C(err, 0, TEST_ulong_ne(10555ul, 10555ul));
146     C(err, 1, TEST_ulong_lt(10234ul, 1000000ul));
147     C(err, 0, TEST_ulong_lt(1000000ul, 10234ul));
148     C(err, 1, TEST_ulong_le(10234ul, 1000000ul));
149     C(err, 1, TEST_ulong_le(100000ul, 100000ul));
150     C(err, 0, TEST_ulong_le(1000000ul, 10234ul));
151     C(err, 1, TEST_ulong_gt(100000000ul, 22ul));
152     C(err, 0, TEST_ulong_gt(22ul, 100000000ul));
153     C(err, 1, TEST_ulong_ge(100000000ul, 22ul));
154     C(err, 1, TEST_ulong_ge(10555ul, 10555ul));
155     C(err, 0, TEST_ulong_ge(22ul, 100000000ul));
156     return 1;
157
158 err:
159     return 0;
160 }
161
162 static int test_size_t(void)
163 {
164     C(err, 1, TEST_int_eq((size_t)10, (size_t)10));
165     C(err, 0, TEST_int_eq((size_t)10, (size_t)12));
166     C(err, 1, TEST_int_ne((size_t)10, (size_t)12));
167     C(err, 0, TEST_int_ne((size_t)24, (size_t)24));
168     C(err, 1, TEST_int_lt((size_t)30, (size_t)88));
169     C(err, 0, TEST_int_lt((size_t)88, (size_t)30));
170     C(err, 1, TEST_int_le((size_t)30, (size_t)88));
171     C(err, 1, TEST_int_le((size_t)33, (size_t)33));
172     C(err, 0, TEST_int_le((size_t)88, (size_t)30));
173     C(err, 1, TEST_int_gt((size_t)52, (size_t)33));
174     C(err, 0, TEST_int_gt((size_t)33, (size_t)52));
175     C(err, 1, TEST_int_ge((size_t)52, (size_t)33));
176     C(err, 1, TEST_int_ge((size_t)38, (size_t)38));
177     C(err, 0, TEST_int_ge((size_t)33, (size_t)52));
178     return 1;
179
180 err:
181     return 0;
182 }
183
184 static int test_pointer(void)
185 {
186     int x = 0;
187     char y = 1;
188
189     C(err, 1, TEST_ptr(&y));
190     C(err, 0, TEST_ptr(NULL));
191     C(err, 0, TEST_ptr_null(&y));
192     C(err, 1, TEST_ptr_null(NULL));
193     C(err, 1, TEST_ptr_eq(NULL, NULL));
194     C(err, 0, TEST_ptr_eq(NULL, &y));
195     C(err, 0, TEST_ptr_eq(&y, NULL));
196     C(err, 0, TEST_ptr_eq(&y, &x));
197     C(err, 1, TEST_ptr_eq(&x, &x));
198     C(err, 0, TEST_ptr_ne(NULL, NULL));
199     C(err, 1, TEST_ptr_ne(NULL, &y));
200     C(err, 1, TEST_ptr_ne(&y, NULL));
201     C(err, 1, TEST_ptr_ne(&y, &x));
202     C(err, 0, TEST_ptr_ne(&x, &x));
203     return 1;
204
205 err:
206     return 0;
207 }
208
209 static int test_bool(void)
210 {
211     C(err, 0, TEST_true(0));
212     C(err, 1, TEST_true(1));
213     C(err, 1, TEST_false(0));
214     C(err, 0, TEST_false(1));
215     return 1;
216
217 err:
218     return 0;
219 }
220
221 static int test_string(void)
222 {
223     static char buf[] = "abc";
224     C(err, 1, TEST_str_eq(NULL, NULL));
225     C(err, 1, TEST_str_eq("abc", buf));
226     C(err, 0, TEST_str_eq("abc", NULL));
227     C(err, 0, TEST_str_eq(NULL, buf));
228     C(err, 0, TEST_str_ne(NULL, NULL));
229     C(err, 0, TEST_str_ne("abc", buf));
230     C(err, 1, TEST_str_ne("abc", NULL));
231     C(err, 1, TEST_str_ne(NULL, buf));
232     return 1;
233
234 err:
235     return 0;
236 }
237
238 static int test_memory(void)
239 {
240     static char buf[] = "xyz";
241     C(err, 1, TEST_mem_eq(NULL, 0, NULL, 0));
242     C(err, 1, TEST_mem_eq(NULL, 1, NULL, 2));
243     C(err, 0, TEST_mem_eq(NULL, 0, "xyz", 3));
244     C(err, 0, TEST_mem_eq(NULL, 0, "", 0));
245     C(err, 0, TEST_mem_eq("xyz", 3, NULL, 0));
246     C(err, 0, TEST_mem_eq("xyz", 3, buf, sizeof(buf)));
247     C(err, 1, TEST_mem_eq("xyz", 4, buf, sizeof(buf)));
248     return 1;
249
250 err:
251     return 0;
252 }
253
254 static int test_messages(void)
255 {
256     TEST_info("This is an %s message.", "info");
257     TEST_error("This is an %s message.", "error");
258     return 1;
259 }
260
261 static int test_single_eval(void)
262 {
263     int i = 4;
264     long l = -9000;
265     char c = 'd';
266     unsigned char uc = 22;
267     unsigned long ul = 500;
268     size_t st = 1234;
269     char buf[4] = { 0 }, *p = buf;
270
271            /* int */
272     return TEST_int_eq(i++, 4)
273            && TEST_int_eq(i, 5)
274            && TEST_int_gt(++i, 5)
275            && TEST_int_le(5, i++)
276            && TEST_int_ne(--i, 5)
277            && TEST_int_eq(12, i *= 2)
278            /* Long */
279            && TEST_long_eq(l--, -9000L)
280            && TEST_long_eq(++l, -9000L)
281            && TEST_long_ne(-9000L, l /= 2)
282            && TEST_long_lt(--l, -4500L)
283            /* char */
284            && TEST_char_eq(++c, 'e')
285            && TEST_char_eq('e', c--)
286            && TEST_char_ne('d', --c)
287            && TEST_char_le('b', --c)
288            && TEST_char_lt(c++, 'c')
289            /* unsigned char */
290            && TEST_uchar_eq(22, uc++)
291            && TEST_uchar_eq(uc /= 2, 11)
292            && TEST_ulong_eq(ul ^= 1, 501)
293            && TEST_ulong_eq(502, ul ^= 3)
294            && TEST_ulong_eq(ul = ul * 3 - 6, 1500)
295            /* size_t */
296            && TEST_size_t_eq((--i, st++), 1234)
297            && TEST_size_t_eq(st, 1235)
298            && TEST_int_eq(11, i)
299            /* pointers */
300            && TEST_ptr_eq(p++, buf)
301            && TEST_ptr_eq(buf + 2, ++p)
302            && TEST_ptr_eq(buf, p -= 2)
303            && TEST_ptr(++p)
304            && TEST_ptr_eq(p, buf + 1)
305            && TEST_ptr_null(p = NULL)
306            /* strings */
307            && TEST_str_eq(p = "123456" + 1, "23456")
308            && TEST_str_eq("3456", ++p)
309            && TEST_str_ne(p++, "456")
310            /* memory */
311            && TEST_mem_eq(--p, sizeof("3456"), "3456", sizeof("3456"))
312            && TEST_mem_ne(p++, sizeof("456"), "456", sizeof("456"))
313            && TEST_mem_eq(p--, sizeof("456"), "456", sizeof("456"));
314 }
315
316 void register_tests(void)
317 {
318     ADD_TEST(test_int);
319     ADD_TEST(test_uint);
320     ADD_TEST(test_char);
321     ADD_TEST(test_uchar);
322     ADD_TEST(test_long);
323     ADD_TEST(test_ulong);
324     ADD_TEST(test_size_t);
325     ADD_TEST(test_pointer);
326     ADD_TEST(test_bool);
327     ADD_TEST(test_string);
328     ADD_TEST(test_memory);
329     ADD_TEST(test_messages);
330     ADD_TEST(test_single_eval);
331 }