tests.c 17 KB

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