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