]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/test_test.c
2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
14 #include <openssl/opensslconf.h>
15 #include <openssl/err.h>
16 #include <openssl/crypto.h>
17 #include <openssl/bn.h>
22 #define TEST(expected, test) test_case((expected), #test, (test))
24 static int test_case(int expected
, const char *test
, int result
)
26 if (result
!= expected
) {
27 fprintf(stderr
, "# FATAL: %s != %d\n", test
, expected
);
33 static int test_int(void)
35 if (!TEST(1, TEST_int_eq(1, 1))
36 | !TEST(0, TEST_int_eq(1, -1))
37 | !TEST(1, TEST_int_ne(1, 2))
38 | !TEST(0, TEST_int_ne(3, 3))
39 | !TEST(1, TEST_int_lt(4, 9))
40 | !TEST(0, TEST_int_lt(9, 4))
41 | !TEST(1, TEST_int_le(4, 9))
42 | !TEST(1, TEST_int_le(5, 5))
43 | !TEST(0, TEST_int_le(9, 4))
44 | !TEST(1, TEST_int_gt(8, 5))
45 | !TEST(0, TEST_int_gt(5, 8))
46 | !TEST(1, TEST_int_ge(8, 5))
47 | !TEST(1, TEST_int_ge(6, 6))
48 | !TEST(0, TEST_int_ge(5, 8)))
56 static int test_uint(void)
58 if (!TEST(1, TEST_uint_eq(3u, 3u))
59 | !TEST(0, TEST_uint_eq(3u, 5u))
60 | !TEST(1, TEST_uint_ne(4u, 2u))
61 | !TEST(0, TEST_uint_ne(6u, 6u))
62 | !TEST(1, TEST_uint_lt(5u, 9u))
63 | !TEST(0, TEST_uint_lt(9u, 5u))
64 | !TEST(1, TEST_uint_le(5u, 9u))
65 | !TEST(1, TEST_uint_le(7u, 7u))
66 | !TEST(0, TEST_uint_le(9u, 5u))
67 | !TEST(1, TEST_uint_gt(11u, 1u))
68 | !TEST(0, TEST_uint_gt(1u, 11u))
69 | !TEST(1, TEST_uint_ge(11u, 1u))
70 | !TEST(1, TEST_uint_ge(6u, 6u))
71 | !TEST(0, TEST_uint_ge(1u, 11u)))
79 static int test_char(void)
81 if (!TEST(1, TEST_char_eq('a', 'a'))
82 | !TEST(0, TEST_char_eq('a', 'A'))
83 | !TEST(1, TEST_char_ne('a', 'c'))
84 | !TEST(0, TEST_char_ne('e', 'e'))
85 | !TEST(1, TEST_char_lt('i', 'x'))
86 | !TEST(0, TEST_char_lt('x', 'i'))
87 | !TEST(1, TEST_char_le('i', 'x'))
88 | !TEST(1, TEST_char_le('n', 'n'))
89 | !TEST(0, TEST_char_le('x', 'i'))
90 | !TEST(1, TEST_char_gt('w', 'n'))
91 | !TEST(0, TEST_char_gt('n', 'w'))
92 | !TEST(1, TEST_char_ge('w', 'n'))
93 | !TEST(1, TEST_char_ge('p', 'p'))
94 | !TEST(0, TEST_char_ge('n', 'w')))
102 static int test_uchar(void)
104 if (!TEST(1, TEST_uchar_eq(49, 49))
105 | !TEST(0, TEST_uchar_eq(49, 60))
106 | !TEST(1, TEST_uchar_ne(50, 2))
107 | !TEST(0, TEST_uchar_ne(66, 66))
108 | !TEST(1, TEST_uchar_lt(60, 80))
109 | !TEST(0, TEST_uchar_lt(80, 60))
110 | !TEST(1, TEST_uchar_le(60, 80))
111 | !TEST(1, TEST_uchar_le(78, 78))
112 | !TEST(0, TEST_uchar_le(80, 60))
113 | !TEST(1, TEST_uchar_gt(88, 37))
114 | !TEST(0, TEST_uchar_gt(37, 88))
115 | !TEST(1, TEST_uchar_ge(88, 37))
116 | !TEST(1, TEST_uchar_ge(66, 66))
117 | !TEST(0, TEST_uchar_ge(37, 88)))
125 static int test_long(void)
127 if (!TEST(1, TEST_long_eq(123l, 123l))
128 | !TEST(0, TEST_long_eq(123l, -123l))
129 | !TEST(1, TEST_long_ne(123l, 500l))
130 | !TEST(0, TEST_long_ne(1000l, 1000l))
131 | !TEST(1, TEST_long_lt(-8923l, 102934563l))
132 | !TEST(0, TEST_long_lt(102934563l, -8923l))
133 | !TEST(1, TEST_long_le(-8923l, 102934563l))
134 | !TEST(1, TEST_long_le(12345l, 12345l))
135 | !TEST(0, TEST_long_le(102934563l, -8923l))
136 | !TEST(1, TEST_long_gt(84325677l, 12345l))
137 | !TEST(0, TEST_long_gt(12345l, 84325677l))
138 | !TEST(1, TEST_long_ge(84325677l, 12345l))
139 | !TEST(1, TEST_long_ge(465869l, 465869l))
140 | !TEST(0, TEST_long_ge(12345l, 84325677l)))
148 static int test_ulong(void)
150 if (!TEST(1, TEST_ulong_eq(919ul, 919ul))
151 | !TEST(0, TEST_ulong_eq(919ul, 10234ul))
152 | !TEST(1, TEST_ulong_ne(8190ul, 66ul))
153 | !TEST(0, TEST_ulong_ne(10555ul, 10555ul))
154 | !TEST(1, TEST_ulong_lt(10234ul, 1000000ul))
155 | !TEST(0, TEST_ulong_lt(1000000ul, 10234ul))
156 | !TEST(1, TEST_ulong_le(10234ul, 1000000ul))
157 | !TEST(1, TEST_ulong_le(100000ul, 100000ul))
158 | !TEST(0, TEST_ulong_le(1000000ul, 10234ul))
159 | !TEST(1, TEST_ulong_gt(100000000ul, 22ul))
160 | !TEST(0, TEST_ulong_gt(22ul, 100000000ul))
161 | !TEST(1, TEST_ulong_ge(100000000ul, 22ul))
162 | !TEST(1, TEST_ulong_ge(10555ul, 10555ul))
163 | !TEST(0, TEST_ulong_ge(22ul, 100000000ul)))
171 static int test_size_t(void)
173 if (!TEST(1, TEST_int_eq((size_t)10, (size_t)10))
174 | !TEST(0, TEST_int_eq((size_t)10, (size_t)12))
175 | !TEST(1, TEST_int_ne((size_t)10, (size_t)12))
176 | !TEST(0, TEST_int_ne((size_t)24, (size_t)24))
177 | !TEST(1, TEST_int_lt((size_t)30, (size_t)88))
178 | !TEST(0, TEST_int_lt((size_t)88, (size_t)30))
179 | !TEST(1, TEST_int_le((size_t)30, (size_t)88))
180 | !TEST(1, TEST_int_le((size_t)33, (size_t)33))
181 | !TEST(0, TEST_int_le((size_t)88, (size_t)30))
182 | !TEST(1, TEST_int_gt((size_t)52, (size_t)33))
183 | !TEST(0, TEST_int_gt((size_t)33, (size_t)52))
184 | !TEST(1, TEST_int_ge((size_t)52, (size_t)33))
185 | !TEST(1, TEST_int_ge((size_t)38, (size_t)38))
186 | !TEST(0, TEST_int_ge((size_t)33, (size_t)52)))
194 static int test_pointer(void)
199 if (!TEST(1, TEST_ptr(&y
))
200 | !TEST(0, TEST_ptr(NULL
))
201 | !TEST(0, TEST_ptr_null(&y
))
202 | !TEST(1, TEST_ptr_null(NULL
))
203 | !TEST(1, TEST_ptr_eq(NULL
, NULL
))
204 | !TEST(0, TEST_ptr_eq(NULL
, &y
))
205 | !TEST(0, TEST_ptr_eq(&y
, NULL
))
206 | !TEST(0, TEST_ptr_eq(&y
, &x
))
207 | !TEST(1, TEST_ptr_eq(&x
, &x
))
208 | !TEST(0, TEST_ptr_ne(NULL
, NULL
))
209 | !TEST(1, TEST_ptr_ne(NULL
, &y
))
210 | !TEST(1, TEST_ptr_ne(&y
, NULL
))
211 | !TEST(1, TEST_ptr_ne(&y
, &x
))
212 | !TEST(0, TEST_ptr_ne(&x
, &x
)))
220 static int test_bool(void)
222 if (!TEST(0, TEST_true(0))
223 | !TEST(1, TEST_true(1))
224 | !TEST(1, TEST_false(0))
225 | !TEST(0, TEST_false(1)))
233 static int test_string(void)
235 static char buf
[] = "abc";
237 if (!TEST(1, TEST_str_eq(NULL
, NULL
))
238 | !TEST(1, TEST_str_eq("abc", buf
))
239 | !TEST(0, TEST_str_eq("abc", NULL
))
240 | !TEST(0, TEST_str_eq("abc", ""))
241 | !TEST(0, TEST_str_eq(NULL
, buf
))
242 | !TEST(0, TEST_str_ne(NULL
, NULL
))
243 | !TEST(0, TEST_str_eq("", NULL
))
244 | !TEST(0, TEST_str_eq(NULL
, ""))
245 | !TEST(0, TEST_str_ne("", ""))
246 | !TEST(0, TEST_str_eq("\1\2\3\4\5", "\1x\3\6\5"))
247 | !TEST(0, TEST_str_ne("abc", buf
))
248 | !TEST(1, TEST_str_ne("abc", NULL
))
249 | !TEST(1, TEST_str_ne(NULL
, buf
))
250 | !TEST(0, TEST_str_eq("abcdef", "abcdefghijk")))
258 static int test_memory(void)
260 static char buf
[] = "xyz";
262 if (!TEST(1, TEST_mem_eq(NULL
, 0, NULL
, 0))
263 | !TEST(1, TEST_mem_eq(NULL
, 1, NULL
, 2))
264 | !TEST(0, TEST_mem_eq(NULL
, 0, "xyz", 3))
265 | !TEST(0, TEST_mem_eq(NULL
, 7, "abc", 3))
266 | !TEST(0, TEST_mem_ne(NULL
, 0, NULL
, 0))
267 | !TEST(0, TEST_mem_eq(NULL
, 0, "", 0))
268 | !TEST(0, TEST_mem_eq("", 0, NULL
, 0))
269 | !TEST(0, TEST_mem_ne("", 0, "", 0))
270 | !TEST(0, TEST_mem_eq("xyz", 3, NULL
, 0))
271 | !TEST(0, TEST_mem_eq("xyz", 3, buf
, sizeof(buf
)))
272 | !TEST(1, TEST_mem_eq("xyz", 4, buf
, sizeof(buf
))))
280 static int test_memory_overflow(void)
282 /* Verify that the memory printing overflows without walking the stack */
283 const char *p
= "1234567890123456789012345678901234567890123456789012";
284 const char *q
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
286 return TEST(0, TEST_mem_eq(p
, strlen(p
), q
, strlen(q
)));
289 static int test_bignum(void)
291 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
;
294 if (!TEST(1, TEST_int_eq(BN_dec2bn(&a
, "0"), 1))
295 | !TEST(1, TEST_BN_eq_word(a
, 0))
296 | !TEST(0, TEST_BN_eq_word(a
, 30))
297 | !TEST(1, TEST_BN_abs_eq_word(a
, 0))
298 | !TEST(0, TEST_BN_eq_one(a
))
299 | !TEST(1, TEST_BN_eq_zero(a
))
300 | !TEST(0, TEST_BN_ne_zero(a
))
301 | !TEST(1, TEST_BN_le_zero(a
))
302 | !TEST(0, TEST_BN_lt_zero(a
))
303 | !TEST(1, TEST_BN_ge_zero(a
))
304 | !TEST(0, TEST_BN_gt_zero(a
))
305 | !TEST(1, TEST_BN_even(a
))
306 | !TEST(0, TEST_BN_odd(a
))
307 | !TEST(1, TEST_BN_eq(b
, c
))
308 | !TEST(0, TEST_BN_eq(a
, b
))
309 | !TEST(0, TEST_BN_ne(NULL
, c
))
310 | !TEST(1, TEST_int_eq(BN_dec2bn(&b
, "1"), 1))
311 | !TEST(1, TEST_BN_eq_word(b
, 1))
312 | !TEST(1, TEST_BN_eq_one(b
))
313 | !TEST(0, TEST_BN_abs_eq_word(b
, 0))
314 | !TEST(1, TEST_BN_abs_eq_word(b
, 1))
315 | !TEST(0, TEST_BN_eq_zero(b
))
316 | !TEST(1, TEST_BN_ne_zero(b
))
317 | !TEST(0, TEST_BN_le_zero(b
))
318 | !TEST(0, TEST_BN_lt_zero(b
))
319 | !TEST(1, TEST_BN_ge_zero(b
))
320 | !TEST(1, TEST_BN_gt_zero(b
))
321 | !TEST(0, TEST_BN_even(b
))
322 | !TEST(1, TEST_BN_odd(b
))
323 | !TEST(1, TEST_int_eq(BN_dec2bn(&c
, "-334739439"), 10))
324 | !TEST(0, TEST_BN_eq_word(c
, 334739439))
325 | !TEST(1, TEST_BN_abs_eq_word(c
, 334739439))
326 | !TEST(0, TEST_BN_eq_zero(c
))
327 | !TEST(1, TEST_BN_ne_zero(c
))
328 | !TEST(1, TEST_BN_le_zero(c
))
329 | !TEST(1, TEST_BN_lt_zero(c
))
330 | !TEST(0, TEST_BN_ge_zero(c
))
331 | !TEST(0, TEST_BN_gt_zero(c
))
332 | !TEST(0, TEST_BN_even(c
))
333 | !TEST(1, TEST_BN_odd(c
))
334 | !TEST(1, TEST_BN_eq(a
, a
))
335 | !TEST(0, TEST_BN_ne(a
, a
))
336 | !TEST(0, TEST_BN_eq(a
, b
))
337 | !TEST(1, TEST_BN_ne(a
, b
))
338 | !TEST(0, TEST_BN_lt(a
, c
))
339 | !TEST(1, TEST_BN_lt(c
, b
))
340 | !TEST(0, TEST_BN_lt(b
, c
))
341 | !TEST(0, TEST_BN_le(a
, c
))
342 | !TEST(1, TEST_BN_le(c
, b
))
343 | !TEST(0, TEST_BN_le(b
, c
))
344 | !TEST(1, TEST_BN_gt(a
, c
))
345 | !TEST(0, TEST_BN_gt(c
, b
))
346 | !TEST(1, TEST_BN_gt(b
, c
))
347 | !TEST(1, TEST_BN_ge(a
, c
))
348 | !TEST(0, TEST_BN_ge(c
, b
))
349 | !TEST(1, TEST_BN_ge(b
, c
)))
360 static int test_long_output(void)
362 const char *p
= "1234567890123456789012345678901234567890123456789012";
363 const char *q
= "1234567890klmnopqrs01234567890EFGHIJKLM0123456789XYZ";
364 const char *r
= "1234567890123456789012345678901234567890123456789012"
365 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY+"
366 "12345678901234567890123ABC78901234567890123456789012";
367 const char *s
= "1234567890123456789012345678901234567890123456789012"
368 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY-"
369 "1234567890123456789012345678901234567890123456789012"
370 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
372 return TEST(0, TEST_str_eq(p
, q
))
373 & TEST(0, TEST_str_eq(q
, r
))
374 & TEST(0, TEST_str_eq(r
, s
))
375 & TEST(0, TEST_mem_eq(r
, strlen(r
), s
, strlen(s
)));
378 static int test_long_bignum(void)
381 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
, *d
= NULL
;
382 const char as
[] = "1234567890123456789012345678901234567890123456789012"
383 "1234567890123456789012345678901234567890123456789012"
384 "1234567890123456789012345678901234567890123456789012"
385 "1234567890123456789012345678901234567890123456789012"
386 "1234567890123456789012345678901234567890123456789012"
387 "1234567890123456789012345678901234567890123456789012"
389 const char bs
[] = "1234567890123456789012345678901234567890123456789012"
390 "1234567890123456789012345678901234567890123456789013"
392 const char cs
[] = "-" /* 64 characters plus sign */
393 "123456789012345678901234567890"
394 "123456789012345678901234567890"
396 const char ds
[] = "-" /* 63 characters plus sign */
397 "23456789A123456789B123456789C"
398 "123456789D123456789E123456789F"
401 r
= TEST_true(BN_hex2bn(&a
, as
))
402 && TEST_true(BN_hex2bn(&b
, bs
))
403 && TEST_true(BN_hex2bn(&c
, cs
))
404 && TEST_true(BN_hex2bn(&d
, ds
))
405 && (TEST(0, TEST_BN_eq(a
, b
))
406 & TEST(0, TEST_BN_eq(b
, a
))
407 & TEST(0, TEST_BN_eq(b
, NULL
))
408 & TEST(0, TEST_BN_eq(NULL
, a
))
409 & TEST(1, TEST_BN_ne(a
, NULL
))
410 & TEST(0, TEST_BN_eq(c
, d
)));
418 static int test_messages(void)
420 TEST_info("This is an %s message.", "info");
421 TEST_error("This is an %s message.", "error");
425 static int test_single_eval(void)
430 unsigned char uc
= 22;
431 unsigned long ul
= 500;
433 char buf
[4] = { 0 }, *p
= buf
;
436 return TEST_int_eq(i
++, 4)
438 && TEST_int_gt(++i
, 5)
439 && TEST_int_le(5, i
++)
440 && TEST_int_ne(--i
, 5)
441 && TEST_int_eq(12, i
*= 2)
443 && TEST_long_eq(l
--, -9000L)
444 && TEST_long_eq(++l
, -9000L)
445 && TEST_long_ne(-9000L, l
/= 2)
446 && TEST_long_lt(--l
, -4500L)
448 && TEST_char_eq(++c
, 'e')
449 && TEST_char_eq('e', c
--)
450 && TEST_char_ne('d', --c
)
451 && TEST_char_le('b', --c
)
452 && TEST_char_lt(c
++, 'c')
454 && TEST_uchar_eq(22, uc
++)
455 && TEST_uchar_eq(uc
/= 2, 11)
456 && TEST_ulong_eq(ul
^= 1, 501)
457 && TEST_ulong_eq(502, ul
^= 3)
458 && TEST_ulong_eq(ul
= ul
* 3 - 6, 1500)
460 && TEST_size_t_eq((--i
, st
++), 1234)
461 && TEST_size_t_eq(st
, 1235)
462 && TEST_int_eq(11, i
)
464 && TEST_ptr_eq(p
++, buf
)
465 && TEST_ptr_eq(buf
+ 2, ++p
)
466 && TEST_ptr_eq(buf
, p
-= 2)
468 && TEST_ptr_eq(p
, buf
+ 1)
469 && TEST_ptr_null(p
= NULL
)
471 && TEST_str_eq(p
= "123456" + 1, "23456")
472 && TEST_str_eq("3456", ++p
)
473 && TEST_str_ne(p
++, "456")
475 && TEST_mem_eq(--p
, sizeof("3456"), "3456", sizeof("3456"))
476 && TEST_mem_ne(p
++, sizeof("456"), "456", sizeof("456"))
477 && TEST_mem_eq(p
--, sizeof("456"), "456", sizeof("456"));
480 static int test_output(void)
482 const char s
[] = "1234567890123456789012345678901234567890123456789012"
483 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
485 test_output_string("test", s
, sizeof(s
) - 1);
486 test_output_memory("test", (const unsigned char *)s
, sizeof(s
));
490 static const char *bn_output_tests
[] = {
494 "1234567890123456789012345678901234567890123456789012"
495 "1234567890123456789012345678901234567890123456789013"
499 static int test_bn_output(int n
)
503 if (bn_output_tests
[n
] != NULL
504 && !TEST_true(BN_hex2bn(&b
, bn_output_tests
[n
])))
506 test_output_bignum(bn_output_tests
[n
], b
);
512 int setup_tests(void)
517 ADD_TEST(test_uchar
);
519 ADD_TEST(test_ulong
);
520 ADD_TEST(test_size_t
);
521 ADD_TEST(test_pointer
);
523 ADD_TEST(test_string
);
524 ADD_TEST(test_memory
);
525 ADD_TEST(test_memory_overflow
);
526 ADD_TEST(test_bignum
);
527 ADD_TEST(test_long_bignum
);
528 ADD_TEST(test_long_output
);
529 ADD_TEST(test_messages
);
530 ADD_TEST(test_single_eval
);
531 ADD_TEST(test_output
);
532 ADD_ALL_TESTS(test_bn_output
, OSSL_NELEM(bn_output_tests
));