tests.c 17 KB

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