]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/test_test.c
2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (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>
19 #include "internal/nelem.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_size_t_eq((size_t)10, (size_t)10))
174 | !TEST(0, TEST_size_t_eq((size_t)10, (size_t)12))
175 | !TEST(1, TEST_size_t_ne((size_t)10, (size_t)12))
176 | !TEST(0, TEST_size_t_ne((size_t)24, (size_t)24))
177 | !TEST(1, TEST_size_t_lt((size_t)30, (size_t)88))
178 | !TEST(0, TEST_size_t_lt((size_t)88, (size_t)30))
179 | !TEST(1, TEST_size_t_le((size_t)30, (size_t)88))
180 | !TEST(1, TEST_size_t_le((size_t)33, (size_t)33))
181 | !TEST(0, TEST_size_t_le((size_t)88, (size_t)30))
182 | !TEST(1, TEST_size_t_gt((size_t)52, (size_t)33))
183 | !TEST(0, TEST_size_t_gt((size_t)33, (size_t)52))
184 | !TEST(1, TEST_size_t_ge((size_t)52, (size_t)33))
185 | !TEST(1, TEST_size_t_ge((size_t)38, (size_t)38))
186 | !TEST(0, TEST_size_t_ge((size_t)33, (size_t)52)))
194 static int test_time_t(void)
196 if (!TEST(1, TEST_time_t_eq((time_t)10, (time_t)10))
197 | !TEST(0, TEST_time_t_eq((time_t)10, (time_t)12))
198 | !TEST(1, TEST_time_t_ne((time_t)10, (time_t)12))
199 | !TEST(0, TEST_time_t_ne((time_t)24, (time_t)24))
200 | !TEST(1, TEST_time_t_lt((time_t)30, (time_t)88))
201 | !TEST(0, TEST_time_t_lt((time_t)88, (time_t)30))
202 | !TEST(1, TEST_time_t_le((time_t)30, (time_t)88))
203 | !TEST(1, TEST_time_t_le((time_t)33, (time_t)33))
204 | !TEST(0, TEST_time_t_le((time_t)88, (time_t)30))
205 | !TEST(1, TEST_time_t_gt((time_t)52, (time_t)33))
206 | !TEST(0, TEST_time_t_gt((time_t)33, (time_t)52))
207 | !TEST(1, TEST_time_t_ge((time_t)52, (time_t)33))
208 | !TEST(1, TEST_time_t_ge((time_t)38, (time_t)38))
209 | !TEST(0, TEST_time_t_ge((time_t)33, (time_t)52)))
217 static int test_pointer(void)
222 if (!TEST(1, TEST_ptr(&y
))
223 | !TEST(0, TEST_ptr(NULL
))
224 | !TEST(0, TEST_ptr_null(&y
))
225 | !TEST(1, TEST_ptr_null(NULL
))
226 | !TEST(1, TEST_ptr_eq(NULL
, NULL
))
227 | !TEST(0, TEST_ptr_eq(NULL
, &y
))
228 | !TEST(0, TEST_ptr_eq(&y
, NULL
))
229 | !TEST(0, TEST_ptr_eq(&y
, &x
))
230 | !TEST(1, TEST_ptr_eq(&x
, &x
))
231 | !TEST(0, TEST_ptr_ne(NULL
, NULL
))
232 | !TEST(1, TEST_ptr_ne(NULL
, &y
))
233 | !TEST(1, TEST_ptr_ne(&y
, NULL
))
234 | !TEST(1, TEST_ptr_ne(&y
, &x
))
235 | !TEST(0, TEST_ptr_ne(&x
, &x
)))
243 static int test_bool(void)
245 if (!TEST(0, TEST_true(0))
246 | !TEST(1, TEST_true(1))
247 | !TEST(1, TEST_false(0))
248 | !TEST(0, TEST_false(1)))
256 static int test_string(void)
258 static char buf
[] = "abc";
260 if (!TEST(1, TEST_str_eq(NULL
, NULL
))
261 | !TEST(1, TEST_str_eq("abc", buf
))
262 | !TEST(0, TEST_str_eq("abc", NULL
))
263 | !TEST(0, TEST_str_eq("abc", ""))
264 | !TEST(0, TEST_str_eq(NULL
, buf
))
265 | !TEST(0, TEST_str_ne(NULL
, NULL
))
266 | !TEST(0, TEST_str_eq("", NULL
))
267 | !TEST(0, TEST_str_eq(NULL
, ""))
268 | !TEST(0, TEST_str_ne("", ""))
269 | !TEST(0, TEST_str_eq("\1\2\3\4\5", "\1x\3\6\5"))
270 | !TEST(0, TEST_str_ne("abc", buf
))
271 | !TEST(1, TEST_str_ne("abc", NULL
))
272 | !TEST(1, TEST_str_ne(NULL
, buf
))
273 | !TEST(0, TEST_str_eq("abcdef", "abcdefghijk")))
281 static int test_memory(void)
283 static char buf
[] = "xyz";
285 if (!TEST(1, TEST_mem_eq(NULL
, 0, NULL
, 0))
286 | !TEST(1, TEST_mem_eq(NULL
, 1, NULL
, 2))
287 | !TEST(0, TEST_mem_eq(NULL
, 0, "xyz", 3))
288 | !TEST(0, TEST_mem_eq(NULL
, 7, "abc", 3))
289 | !TEST(0, TEST_mem_ne(NULL
, 0, NULL
, 0))
290 | !TEST(0, TEST_mem_eq(NULL
, 0, "", 0))
291 | !TEST(0, TEST_mem_eq("", 0, NULL
, 0))
292 | !TEST(0, TEST_mem_ne("", 0, "", 0))
293 | !TEST(0, TEST_mem_eq("xyz", 3, NULL
, 0))
294 | !TEST(0, TEST_mem_eq("xyz", 3, buf
, sizeof(buf
)))
295 | !TEST(1, TEST_mem_eq("xyz", 4, buf
, sizeof(buf
))))
303 static int test_memory_overflow(void)
305 /* Verify that the memory printing overflows without walking the stack */
306 const char *p
= "1234567890123456789012345678901234567890123456789012";
307 const char *q
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
309 return TEST(0, TEST_mem_eq(p
, strlen(p
), q
, strlen(q
)));
312 static int test_bignum(void)
314 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
;
317 if (!TEST(1, TEST_int_eq(BN_dec2bn(&a
, "0"), 1))
318 | !TEST(1, TEST_BN_eq_word(a
, 0))
319 | !TEST(0, TEST_BN_eq_word(a
, 30))
320 | !TEST(1, TEST_BN_abs_eq_word(a
, 0))
321 | !TEST(0, TEST_BN_eq_one(a
))
322 | !TEST(1, TEST_BN_eq_zero(a
))
323 | !TEST(0, TEST_BN_ne_zero(a
))
324 | !TEST(1, TEST_BN_le_zero(a
))
325 | !TEST(0, TEST_BN_lt_zero(a
))
326 | !TEST(1, TEST_BN_ge_zero(a
))
327 | !TEST(0, TEST_BN_gt_zero(a
))
328 | !TEST(1, TEST_BN_even(a
))
329 | !TEST(0, TEST_BN_odd(a
))
330 | !TEST(1, TEST_BN_eq(b
, c
))
331 | !TEST(0, TEST_BN_eq(a
, b
))
332 | !TEST(0, TEST_BN_ne(NULL
, c
))
333 | !TEST(1, TEST_int_eq(BN_dec2bn(&b
, "1"), 1))
334 | !TEST(1, TEST_BN_eq_word(b
, 1))
335 | !TEST(1, TEST_BN_eq_one(b
))
336 | !TEST(0, TEST_BN_abs_eq_word(b
, 0))
337 | !TEST(1, TEST_BN_abs_eq_word(b
, 1))
338 | !TEST(0, TEST_BN_eq_zero(b
))
339 | !TEST(1, TEST_BN_ne_zero(b
))
340 | !TEST(0, TEST_BN_le_zero(b
))
341 | !TEST(0, TEST_BN_lt_zero(b
))
342 | !TEST(1, TEST_BN_ge_zero(b
))
343 | !TEST(1, TEST_BN_gt_zero(b
))
344 | !TEST(0, TEST_BN_even(b
))
345 | !TEST(1, TEST_BN_odd(b
))
346 | !TEST(1, TEST_int_eq(BN_dec2bn(&c
, "-334739439"), 10))
347 | !TEST(0, TEST_BN_eq_word(c
, 334739439))
348 | !TEST(1, TEST_BN_abs_eq_word(c
, 334739439))
349 | !TEST(0, TEST_BN_eq_zero(c
))
350 | !TEST(1, TEST_BN_ne_zero(c
))
351 | !TEST(1, TEST_BN_le_zero(c
))
352 | !TEST(1, TEST_BN_lt_zero(c
))
353 | !TEST(0, TEST_BN_ge_zero(c
))
354 | !TEST(0, TEST_BN_gt_zero(c
))
355 | !TEST(0, TEST_BN_even(c
))
356 | !TEST(1, TEST_BN_odd(c
))
357 | !TEST(1, TEST_BN_eq(a
, a
))
358 | !TEST(0, TEST_BN_ne(a
, a
))
359 | !TEST(0, TEST_BN_eq(a
, b
))
360 | !TEST(1, TEST_BN_ne(a
, b
))
361 | !TEST(0, TEST_BN_lt(a
, c
))
362 | !TEST(1, TEST_BN_lt(c
, b
))
363 | !TEST(0, TEST_BN_lt(b
, c
))
364 | !TEST(0, TEST_BN_le(a
, c
))
365 | !TEST(1, TEST_BN_le(c
, b
))
366 | !TEST(0, TEST_BN_le(b
, c
))
367 | !TEST(1, TEST_BN_gt(a
, c
))
368 | !TEST(0, TEST_BN_gt(c
, b
))
369 | !TEST(1, TEST_BN_gt(b
, c
))
370 | !TEST(1, TEST_BN_ge(a
, c
))
371 | !TEST(0, TEST_BN_ge(c
, b
))
372 | !TEST(1, TEST_BN_ge(b
, c
)))
383 static int test_long_output(void)
385 const char *p
= "1234567890123456789012345678901234567890123456789012";
386 const char *q
= "1234567890klmnopqrs01234567890EFGHIJKLM0123456789XYZ";
387 const char *r
= "1234567890123456789012345678901234567890123456789012"
388 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY+"
389 "12345678901234567890123ABC78901234567890123456789012";
390 const char *s
= "1234567890123456789012345678901234567890123456789012"
391 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY-"
392 "1234567890123456789012345678901234567890123456789012"
393 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
395 return TEST(0, TEST_str_eq(p
, q
))
396 & TEST(0, TEST_str_eq(q
, r
))
397 & TEST(0, TEST_str_eq(r
, s
))
398 & TEST(0, TEST_mem_eq(r
, strlen(r
), s
, strlen(s
)));
401 static int test_long_bignum(void)
404 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
, *d
= NULL
;
405 const char as
[] = "1234567890123456789012345678901234567890123456789012"
406 "1234567890123456789012345678901234567890123456789012"
407 "1234567890123456789012345678901234567890123456789012"
408 "1234567890123456789012345678901234567890123456789012"
409 "1234567890123456789012345678901234567890123456789012"
410 "1234567890123456789012345678901234567890123456789012"
412 const char bs
[] = "1234567890123456789012345678901234567890123456789012"
413 "1234567890123456789012345678901234567890123456789013"
415 const char cs
[] = "-" /* 64 characters plus sign */
416 "123456789012345678901234567890"
417 "123456789012345678901234567890"
419 const char ds
[] = "-" /* 63 characters plus sign */
420 "23456789A123456789B123456789C"
421 "123456789D123456789E123456789F"
424 r
= TEST_true(BN_hex2bn(&a
, as
))
425 && TEST_true(BN_hex2bn(&b
, bs
))
426 && TEST_true(BN_hex2bn(&c
, cs
))
427 && TEST_true(BN_hex2bn(&d
, ds
))
428 && (TEST(0, TEST_BN_eq(a
, b
))
429 & TEST(0, TEST_BN_eq(b
, a
))
430 & TEST(0, TEST_BN_eq(b
, NULL
))
431 & TEST(0, TEST_BN_eq(NULL
, a
))
432 & TEST(1, TEST_BN_ne(a
, NULL
))
433 & TEST(0, TEST_BN_eq(c
, d
)));
441 static int test_messages(void)
443 TEST_info("This is an %s message.", "info");
444 TEST_error("This is an %s message.", "error");
448 static int test_single_eval(void)
453 unsigned char uc
= 22;
454 unsigned long ul
= 500;
456 char buf
[4] = { 0 }, *p
= buf
;
459 return TEST_int_eq(i
++, 4)
461 && TEST_int_gt(++i
, 5)
462 && TEST_int_le(5, i
++)
463 && TEST_int_ne(--i
, 5)
464 && TEST_int_eq(12, i
*= 2)
466 && TEST_long_eq(l
--, -9000L)
467 && TEST_long_eq(++l
, -9000L)
468 && TEST_long_ne(-9000L, l
/= 2)
469 && TEST_long_lt(--l
, -4500L)
471 && TEST_char_eq(++c
, 'e')
472 && TEST_char_eq('e', c
--)
473 && TEST_char_ne('d', --c
)
474 && TEST_char_le('b', --c
)
475 && TEST_char_lt(c
++, 'c')
477 && TEST_uchar_eq(22, uc
++)
478 && TEST_uchar_eq(uc
/= 2, 11)
479 && TEST_ulong_eq(ul
^= 1, 501)
480 && TEST_ulong_eq(502, ul
^= 3)
481 && TEST_ulong_eq(ul
= ul
* 3 - 6, 1500)
483 && TEST_size_t_eq((--i
, st
++), 1234)
484 && TEST_size_t_eq(st
, 1235)
485 && TEST_int_eq(11, i
)
487 && TEST_ptr_eq(p
++, buf
)
488 && TEST_ptr_eq(buf
+ 2, ++p
)
489 && TEST_ptr_eq(buf
, p
-= 2)
491 && TEST_ptr_eq(p
, buf
+ 1)
492 && TEST_ptr_null(p
= NULL
)
494 && TEST_str_eq(p
= "123456" + 1, "23456")
495 && TEST_str_eq("3456", ++p
)
496 && TEST_str_ne(p
++, "456")
498 && TEST_mem_eq(--p
, sizeof("3456"), "3456", sizeof("3456"))
499 && TEST_mem_ne(p
++, sizeof("456"), "456", sizeof("456"))
500 && TEST_mem_eq(p
--, sizeof("456"), "456", sizeof("456"));
503 static int test_output(void)
505 const char s
[] = "1234567890123456789012345678901234567890123456789012"
506 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
508 test_output_string("test", s
, sizeof(s
) - 1);
509 test_output_memory("test", (const unsigned char *)s
, sizeof(s
));
513 static const char *bn_output_tests
[] = {
517 "1234567890123456789012345678901234567890123456789012"
518 "1234567890123456789012345678901234567890123456789013"
522 static int test_bn_output(int n
)
526 if (bn_output_tests
[n
] != NULL
527 && !TEST_true(BN_hex2bn(&b
, bn_output_tests
[n
])))
529 test_output_bignum(bn_output_tests
[n
], b
);
534 static int test_skip_one(void)
536 return TEST_skip("skip test");
539 static int test_skip_many(int n
)
541 return TEST_skip("skip tests: %d", n
);
544 static int test_skip_null(void)
547 * This is not a recommended way of skipping a test, a reason or
548 * description should be included.
550 return TEST_skip(NULL
);
553 int setup_tests(void)
558 ADD_TEST(test_uchar
);
560 ADD_TEST(test_ulong
);
561 ADD_TEST(test_size_t
);
562 ADD_TEST(test_time_t
);
563 ADD_TEST(test_pointer
);
565 ADD_TEST(test_string
);
566 ADD_TEST(test_memory
);
567 ADD_TEST(test_memory_overflow
);
568 ADD_TEST(test_bignum
);
569 ADD_TEST(test_long_bignum
);
570 ADD_TEST(test_long_output
);
571 ADD_TEST(test_messages
);
572 ADD_TEST(test_single_eval
);
573 ADD_TEST(test_output
);
574 ADD_ALL_TESTS(test_bn_output
, OSSL_NELEM(bn_output_tests
));
575 ADD_TEST(test_skip_one
);
576 ADD_TEST(test_skip_null
);
577 ADD_ALL_TESTS(test_skip_many
, 3);