VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.5/test/testutil/tests.c@ 104605

最後變更 在這個檔案從104605是 104078,由 vboxsync 提交於 11 月 前

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

檔案大小: 16.5 KB
 
1/*
2 * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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#include "../testutil.h"
11#include "output.h"
12#include "tu_local.h"
13
14#include <errno.h>
15#include <string.h>
16#include <ctype.h>
17#include <openssl/asn1.h>
18
19/*
20 * Output a failed test first line.
21 * All items are optional are generally not preinted if passed as NULL.
22 * The special cases are for prefix where "ERROR" is assumed and for left
23 * and right where a non-failure message is produced if either is NULL.
24 */
25void test_fail_message_prefix(const char *prefix, const char *file,
26 int line, const char *type,
27 const char *left, const char *right,
28 const char *op)
29{
30 test_printf_stderr("%s: ", prefix != NULL ? prefix : "ERROR");
31 if (type)
32 test_printf_stderr("(%s) ", type);
33 if (op != NULL) {
34 if (left != NULL && right != NULL)
35 test_printf_stderr("'%s %s %s' failed", left, op, right);
36 else
37 test_printf_stderr("'%s'", op);
38 }
39 if (file != NULL) {
40 test_printf_stderr(" @ %s:%d", file, line);
41 }
42 test_printf_stderr("\n");
43}
44
45/*
46 * A common routine to output test failure messages. Generally this should not
47 * be called directly, rather it should be called by the following functions.
48 *
49 * |desc| is a printf formatted description with arguments |args| that is
50 * supplied by the user and |desc| can be NULL. |type| is the data type
51 * that was tested (int, char, ptr, ...). |fmt| is a system provided
52 * printf format with following arguments that spell out the failure
53 * details i.e. the actual values compared and the operator used.
54 *
55 * The typical use for this is from an utility test function:
56 *
57 * int test6(const char *file, int line, int n) {
58 * if (n != 6) {
59 * test_fail_message(1, file, line, "int", "value %d is not %d", n, 6);
60 * return 0;
61 * }
62 * return 1;
63 * }
64 *
65 * calling test6(3, "oops") will return 0 and produce out along the lines of:
66 * FAIL oops: (int) value 3 is not 6\n
67 */
68static void test_fail_message(const char *prefix, const char *file, int line,
69 const char *type, const char *left,
70 const char *right, const char *op,
71 const char *fmt, ...)
72 PRINTF_FORMAT(8, 9);
73
74static void test_fail_message_va(const char *prefix, const char *file,
75 int line, const char *type,
76 const char *left, const char *right,
77 const char *op, const char *fmt, va_list ap)
78{
79 test_fail_message_prefix(prefix, file, line, type, left, right, op);
80 if (fmt != NULL) {
81 test_vprintf_stderr(fmt, ap);
82 test_printf_stderr("\n");
83 }
84 test_flush_stderr();
85}
86
87static void test_fail_message(const char *prefix, const char *file,
88 int line, const char *type,
89 const char *left, const char *right,
90 const char *op, const char *fmt, ...)
91{
92 va_list ap;
93
94 va_start(ap, fmt);
95 test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap);
96 va_end(ap);
97}
98
99void test_info_c90(const char *desc, ...)
100{
101 va_list ap;
102
103 va_start(ap, desc);
104 test_fail_message_va("INFO", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
105 va_end(ap);
106}
107
108void test_info(const char *file, int line, const char *desc, ...)
109{
110 va_list ap;
111
112 va_start(ap, desc);
113 test_fail_message_va("INFO", file, line, NULL, NULL, NULL, NULL, desc, ap);
114 va_end(ap);
115}
116
117void test_error_c90(const char *desc, ...)
118{
119 va_list ap;
120
121 va_start(ap, desc);
122 test_fail_message_va(NULL, NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
123 va_end(ap);
124 test_printf_stderr("\n");
125}
126
127void test_error(const char *file, int line, const char *desc, ...)
128{
129 va_list ap;
130
131 va_start(ap, desc);
132 test_fail_message_va(NULL, file, line, NULL, NULL, NULL, NULL, desc, ap);
133 va_end(ap);
134 test_printf_stderr("\n");
135}
136
137void test_perror(const char *s)
138{
139 /*
140 * Using openssl_strerror_r causes linking issues since it isn't
141 * exported from libcrypto.so
142 */
143 TEST_error("%s: %s", s, strerror(errno));
144}
145
146void test_note(const char *fmt, ...)
147{
148 if (fmt != NULL) {
149 va_list ap;
150
151 va_start(ap, fmt);
152 test_vprintf_stderr(fmt, ap);
153 va_end(ap);
154 test_printf_stderr("\n");
155 }
156 test_flush_stderr();
157}
158
159
160int test_skip(const char *file, int line, const char *desc, ...)
161{
162 va_list ap;
163
164 va_start(ap, desc);
165 test_fail_message_va("SKIP", file, line, NULL, NULL, NULL, NULL, desc, ap);
166 va_end(ap);
167 return TEST_SKIP_CODE;
168}
169
170int test_skip_c90(const char *desc, ...)
171{
172 va_list ap;
173
174 va_start(ap, desc);
175 test_fail_message_va("SKIP", NULL, -1, NULL, NULL, NULL, NULL, desc, ap);
176 va_end(ap);
177 test_printf_stderr("\n");
178 return TEST_SKIP_CODE;
179}
180
181
182void test_openssl_errors(void)
183{
184 ERR_print_errors_cb(openssl_error_cb, NULL);
185 ERR_clear_error();
186}
187
188/*
189 * Define some comparisons between pairs of various types.
190 * These functions return 1 if the test is true.
191 * Otherwise, they return 0 and pretty-print diagnostics.
192 *
193 * In each case the functions produced are:
194 * int test_name_eq(const type t1, const type t2, const char *desc, ...);
195 * int test_name_ne(const type t1, const type t2, const char *desc, ...);
196 * int test_name_lt(const type t1, const type t2, const char *desc, ...);
197 * int test_name_le(const type t1, const type t2, const char *desc, ...);
198 * int test_name_gt(const type t1, const type t2, const char *desc, ...);
199 * int test_name_ge(const type t1, const type t2, const char *desc, ...);
200 *
201 * The t1 and t2 arguments are to be compared for equality, inequality,
202 * less than, less than or equal to, greater than and greater than or
203 * equal to respectively. If the specified condition holds, the functions
204 * return 1. If the condition does not hold, the functions print a diagnostic
205 * message and return 0.
206 *
207 * The desc argument is a printf format string followed by its arguments and
208 * this is included in the output if the condition being tested for is false.
209 */
210#define DEFINE_COMPARISON(type, name, opname, op, fmt) \
211 int test_ ## name ## _ ## opname(const char *file, int line, \
212 const char *s1, const char *s2, \
213 const type t1, const type t2) \
214 { \
215 if (t1 op t2) \
216 return 1; \
217 test_fail_message(NULL, file, line, #type, s1, s2, #op, \
218 "[" fmt "] compared to [" fmt "]", \
219 t1, t2); \
220 return 0; \
221 }
222
223#define DEFINE_COMPARISONS(type, name, fmt) \
224 DEFINE_COMPARISON(type, name, eq, ==, fmt) \
225 DEFINE_COMPARISON(type, name, ne, !=, fmt) \
226 DEFINE_COMPARISON(type, name, lt, <, fmt) \
227 DEFINE_COMPARISON(type, name, le, <=, fmt) \
228 DEFINE_COMPARISON(type, name, gt, >, fmt) \
229 DEFINE_COMPARISON(type, name, ge, >=, fmt)
230
231DEFINE_COMPARISONS(int, int, "%d")
232DEFINE_COMPARISONS(unsigned int, uint, "%u")
233DEFINE_COMPARISONS(char, char, "%c")
234DEFINE_COMPARISONS(unsigned char, uchar, "%u")
235DEFINE_COMPARISONS(long, long, "%ld")
236DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
237DEFINE_COMPARISONS(size_t, size_t, "%zu")
238DEFINE_COMPARISONS(double, double, "%g")
239
240DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
241DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
242
243int test_ptr_null(const char *file, int line, const char *s, const void *p)
244{
245 if (p == NULL)
246 return 1;
247 test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p);
248 return 0;
249}
250
251int test_ptr(const char *file, int line, const char *s, const void *p)
252{
253 if (p != NULL)
254 return 1;
255 test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);
256 return 0;
257}
258
259int test_true(const char *file, int line, const char *s, int b)
260{
261 if (b)
262 return 1;
263 test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");
264 return 0;
265}
266
267int test_false(const char *file, int line, const char *s, int b)
268{
269 if (!b)
270 return 1;
271 test_fail_message(NULL, file, line, "bool", s, "false", "==", "true");
272 return 0;
273}
274
275int test_str_eq(const char *file, int line, const char *st1, const char *st2,
276 const char *s1, const char *s2)
277{
278 if (s1 == NULL && s2 == NULL)
279 return 1;
280 if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
281 test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
282 s1, s1 == NULL ? 0 : strlen(s1),
283 s2, s2 == NULL ? 0 : strlen(s2));
284 return 0;
285 }
286 return 1;
287}
288
289int test_str_ne(const char *file, int line, const char *st1, const char *st2,
290 const char *s1, const char *s2)
291{
292 if ((s1 == NULL) ^ (s2 == NULL))
293 return 1;
294 if (s1 == NULL || strcmp(s1, s2) == 0) {
295 test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
296 s1, s1 == NULL ? 0 : strlen(s1),
297 s2, s2 == NULL ? 0 : strlen(s2));
298 return 0;
299 }
300 return 1;
301}
302
303int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
304 const char *s1, size_t n1, const char *s2, size_t n2)
305{
306 if (s1 == NULL && s2 == NULL)
307 return 1;
308 if (n1 != n2 || s1 == NULL || s2 == NULL || strncmp(s1, s2, n1) != 0) {
309 test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
310 s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1),
311 s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2));
312 return 0;
313 }
314 return 1;
315}
316
317int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
318 const char *s1, size_t n1, const char *s2, size_t n2)
319{
320 if ((s1 == NULL) ^ (s2 == NULL))
321 return 1;
322 if (n1 != n2 || s1 == NULL || strncmp(s1, s2, n1) == 0) {
323 test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
324 s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1),
325 s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2));
326 return 0;
327 }
328 return 1;
329}
330
331int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
332 const void *s1, size_t n1, const void *s2, size_t n2)
333{
334 if (s1 == NULL && s2 == NULL)
335 return 1;
336 if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
337 test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==",
338 s1, n1, s2, n2);
339 return 0;
340 }
341 return 1;
342}
343
344int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
345 const void *s1, size_t n1, const void *s2, size_t n2)
346{
347 if ((s1 == NULL) ^ (s2 == NULL))
348 return 1;
349 if (n1 != n2)
350 return 1;
351 if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
352 test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=",
353 s1, n1, s2, n2);
354 return 0;
355 }
356 return 1;
357}
358
359#define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
360 int test_BN_ ## opname(const char *file, int line, \
361 const char *s1, const char *s2, \
362 const BIGNUM *t1, const BIGNUM *t2) \
363 { \
364 if (BN_cmp(t1, t2) op 0) \
365 return 1; \
366 test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
367 #op, t1, t2); \
368 return 0; \
369 } \
370 int test_BN_ ## opname ## _zero(const char *file, int line, \
371 const char *s, const BIGNUM *a) \
372 { \
373 if (a != NULL &&(zero_cond)) \
374 return 1; \
375 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
376 s, "0", #op, a); \
377 return 0; \
378 }
379
380DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
381DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
382DEFINE_BN_COMPARISONS(gt, >, !BN_is_negative(a) && !BN_is_zero(a))
383DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
384DEFINE_BN_COMPARISONS(lt, <, BN_is_negative(a) && !BN_is_zero(a))
385DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
386
387int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
388{
389 if (a != NULL && BN_is_one(a))
390 return 1;
391 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
392 return 0;
393}
394
395int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
396{
397 if (a != NULL && BN_is_odd(a))
398 return 1;
399 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a);
400 return 0;
401}
402
403int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
404{
405 if (a != NULL && !BN_is_odd(a))
406 return 1;
407 test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
408 a);
409 return 0;
410}
411
412int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
413 const BIGNUM *a, BN_ULONG w)
414{
415 BIGNUM *bw;
416
417 if (a != NULL && BN_is_word(a, w))
418 return 1;
419 if ((bw = BN_new()) != NULL)
420 BN_set_word(bw, w);
421 test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
422 BN_free(bw);
423 return 0;
424}
425
426int test_BN_abs_eq_word(const char *file, int line, const char *bns,
427 const char *ws, const BIGNUM *a, BN_ULONG w)
428{
429 BIGNUM *bw, *aa;
430
431 if (a != NULL && BN_abs_is_word(a, w))
432 return 1;
433 if ((aa = BN_dup(a)) != NULL)
434 BN_set_negative(aa, 0);
435 if ((bw = BN_new()) != NULL)
436 BN_set_word(bw, w);
437 test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
438 aa, bw);
439 BN_free(bw);
440 BN_free(aa);
441 return 0;
442}
443
444static const char *print_time(const ASN1_TIME *t)
445{
446 return t == NULL ? "<null>" : (const char *)ASN1_STRING_get0_data(t);
447}
448
449#define DEFINE_TIME_T_COMPARISON(opname, op) \
450 int test_time_t_ ## opname(const char *file, int line, \
451 const char *s1, const char *s2, \
452 const time_t t1, const time_t t2) \
453 { \
454 ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1); \
455 ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2); \
456 int r = at1 != NULL && at2 != NULL \
457 && ASN1_TIME_compare(at1, at2) op 0; \
458 if (!r) \
459 test_fail_message(NULL, file, line, "time_t", s1, s2, #op, \
460 "[%s] compared to [%s]", \
461 print_time(at1), print_time(at2)); \
462 ASN1_STRING_free(at1); \
463 ASN1_STRING_free(at2); \
464 return r; \
465 }
466DEFINE_TIME_T_COMPARISON(eq, ==)
467DEFINE_TIME_T_COMPARISON(ne, !=)
468DEFINE_TIME_T_COMPARISON(gt, >)
469DEFINE_TIME_T_COMPARISON(ge, >=)
470DEFINE_TIME_T_COMPARISON(lt, <)
471DEFINE_TIME_T_COMPARISON(le, <=)
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette