Fix some issues raise by coverity in the tests.
[openssl.git] / test / test_test.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2017, Oracle and/or its affiliates.  All rights reserved.
4  *
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
9  */
10
11 #include <stdio.h>
12 #include <string.h>
13
14 #include <openssl/opensslconf.h>
15 #include <openssl/err.h>
16 #include <openssl/crypto.h>
17 #include <openssl/bn.h>
18
19 #include "e_os.h"
20 #include "testutil.h"
21
22 #define TEST(expected, test) test_case((expected), #test, (test))
23
24 static int test_case(int expected, const char *test, int result)
25 {
26     if (result != expected) {
27         fprintf(stderr, "# FATAL: %s != %d\n", test, expected);
28         return 0;
29     }
30     return 1;
31 }
32
33 static int test_int(void)
34 {
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)))
49         goto err;
50     return 1;
51
52 err:
53     return 0;
54 }
55
56 static int test_uint(void)
57 {
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)))
72         goto err;
73     return 1;
74
75 err:
76     return 0;
77 }
78
79 static int test_char(void)
80 {
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')))
95         goto err;
96     return 1;
97
98 err:
99     return 0;
100 }
101
102 static int test_uchar(void)
103 {
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)))
118         goto err;
119     return 1;
120
121 err:
122     return 0;
123 }
124
125 static int test_long(void)
126 {
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)))
141         goto err;
142     return 1;
143
144 err:
145     return 0;
146 }
147
148 static int test_ulong(void)
149 {
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)))
164         goto err;
165     return 1;
166
167 err:
168     return 0;
169 }
170
171 static int test_size_t(void)
172 {
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)))
187         goto err;
188     return 1;
189
190 err:
191     return 0;
192 }
193
194 static int test_pointer(void)
195 {
196     int x = 0;
197     char y = 1;
198
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)))
213         goto err;
214     return 1;
215
216 err:
217     return 0;
218 }
219
220 static int test_bool(void)
221 {
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)))
226         goto err;
227     return 1;
228
229 err:
230     return 0;
231 }
232
233 static int test_string(void)
234 {
235     static char buf[] = "abc";
236
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")))
251         goto err;
252     return 1;
253
254 err:
255     return 0;
256 }
257
258 static int test_memory(void)
259 {
260     static char buf[] = "xyz";
261
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))))
273         goto err;
274     return 1;
275
276 err:
277     return 0;
278 }
279
280 static int test_memory_overflow(void)
281 {
282     /* Verify that the memory printing overflows without walking the stack */
283     const char *p = "1234567890123456789012345678901234567890123456789012";
284     const char *q = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
285
286     return TEST(0, TEST_mem_eq(p, strlen(p), q, strlen(q)));
287 }
288
289 static int test_bignum(void)
290 {
291     BIGNUM *a = NULL, *b = NULL, *c = NULL;
292     int r = 0;
293
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)))
350         goto err;
351
352     r = 1;
353 err:
354     BN_free(a);
355     BN_free(b);
356     BN_free(c);
357     return r;
358 }
359
360 static int test_long_output(void)
361 {
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";
371
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)));
376 }
377
378 static int test_long_bignum(void)
379 {
380     int r;
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"
388                       "FFFFFF";
389     const char bs[] = "1234567890123456789012345678901234567890123456789012"
390                       "1234567890123456789012345678901234567890123456789013"
391                       "987657";
392     const char cs[] = "-"        /* 64 characters plus sign */
393                       "123456789012345678901234567890"
394                       "123456789012345678901234567890"
395                       "ABCD";
396     const char ds[] = "-"        /* 63 characters plus sign */
397                       "23456789A123456789B123456789C"
398                       "123456789D123456789E123456789F"
399                       "ABCD";
400
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)));
411     BN_free(a);
412     BN_free(b);
413     BN_free(c);
414     BN_free(d);
415     return r;
416 }
417
418 static int test_messages(void)
419 {
420     TEST_info("This is an %s message.", "info");
421     TEST_error("This is an %s message.", "error");
422     return 1;
423 }
424
425 static int test_single_eval(void)
426 {
427     int i = 4;
428     long l = -9000;
429     char c = 'd';
430     unsigned char uc = 22;
431     unsigned long ul = 500;
432     size_t st = 1234;
433     char buf[4] = { 0 }, *p = buf;
434
435            /* int */
436     return TEST_int_eq(i++, 4)
437            && TEST_int_eq(i, 5)
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)
442            /* Long */
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)
447            /* char */
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')
453            /* unsigned char */
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)
459            /* size_t */
460            && TEST_size_t_eq((--i, st++), 1234)
461            && TEST_size_t_eq(st, 1235)
462            && TEST_int_eq(11, i)
463            /* pointers */
464            && TEST_ptr_eq(p++, buf)
465            && TEST_ptr_eq(buf + 2, ++p)
466            && TEST_ptr_eq(buf, p -= 2)
467            && TEST_ptr(++p)
468            && TEST_ptr_eq(p, buf + 1)
469            && TEST_ptr_null(p = NULL)
470            /* strings */
471            && TEST_str_eq(p = "123456" + 1, "23456")
472            && TEST_str_eq("3456", ++p)
473            && TEST_str_ne(p++, "456")
474            /* memory */
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"));
478 }
479
480 static int test_output(void)
481 {
482     const char s[] = "1234567890123456789012345678901234567890123456789012"
483                      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
484
485     test_output_string("test", s, sizeof(s) - 1);
486     test_output_memory("test", (const unsigned char *)s, sizeof(s));
487     return 1;
488 }
489
490 static const char *bn_output_tests[] = {
491     NULL,
492     "0",
493     "-12345678",
494     "1234567890123456789012345678901234567890123456789012"
495     "1234567890123456789012345678901234567890123456789013"
496     "987657"
497 };
498
499 static int test_bn_output(int n)
500 {
501     BIGNUM *b = NULL;
502
503     if (bn_output_tests[n] != NULL
504             && !TEST_true(BN_hex2bn(&b, bn_output_tests[n])))
505         return 0;
506     test_output_bignum(bn_output_tests[n], b);
507     BN_free(b);
508     return 1;
509 }
510
511
512 void register_tests(void)
513 {
514     ADD_TEST(test_int);
515     ADD_TEST(test_uint);
516     ADD_TEST(test_char);
517     ADD_TEST(test_uchar);
518     ADD_TEST(test_long);
519     ADD_TEST(test_ulong);
520     ADD_TEST(test_size_t);
521     ADD_TEST(test_pointer);
522     ADD_TEST(test_bool);
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));
533 }