tests.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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, cast) \
  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. (cast)t1, (cast)t2); \
  195. return 0; \
  196. }
  197. #define DEFINE_COMPARISONS(type, name, fmt, cast) \
  198. DEFINE_COMPARISON(type, name, eq, ==, fmt, cast) \
  199. DEFINE_COMPARISON(type, name, ne, !=, fmt, cast) \
  200. DEFINE_COMPARISON(type, name, lt, <, fmt, cast) \
  201. DEFINE_COMPARISON(type, name, le, <=, fmt, cast) \
  202. DEFINE_COMPARISON(type, name, gt, >, fmt, cast) \
  203. DEFINE_COMPARISON(type, name, ge, >=, fmt, cast)
  204. DEFINE_COMPARISONS(int, int, "%d", int)
  205. DEFINE_COMPARISONS(unsigned int, uint, "%u", unsigned int)
  206. DEFINE_COMPARISONS(char, char, "%c", char)
  207. DEFINE_COMPARISONS(unsigned char, uchar, "%u", unsigned char)
  208. DEFINE_COMPARISONS(long, long, "%ld", long)
  209. DEFINE_COMPARISONS(unsigned long, ulong, "%lu", unsigned long)
  210. DEFINE_COMPARISONS(int64_t, int64_t, "%lld", long long)
  211. DEFINE_COMPARISONS(uint64_t, uint64_t, "%llu", unsigned long long)
  212. DEFINE_COMPARISONS(size_t, size_t, "%zu", size_t)
  213. DEFINE_COMPARISONS(double, double, "%g", double)
  214. DEFINE_COMPARISON(void *, ptr, eq, ==, "%p", void *)
  215. DEFINE_COMPARISON(void *, ptr, ne, !=, "%p", void *)
  216. int test_ptr_null(const char *file, int line, const char *s, const void *p)
  217. {
  218. if (p == NULL)
  219. return 1;
  220. test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p);
  221. return 0;
  222. }
  223. int test_ptr(const char *file, int line, const char *s, const void *p)
  224. {
  225. if (p != NULL)
  226. return 1;
  227. test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);
  228. return 0;
  229. }
  230. int test_true(const char *file, int line, const char *s, int b)
  231. {
  232. if (b)
  233. return 1;
  234. test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");
  235. return 0;
  236. }
  237. int test_false(const char *file, int line, const char *s, int b)
  238. {
  239. if (!b)
  240. return 1;
  241. test_fail_message(NULL, file, line, "bool", s, "false", "==", "true");
  242. return 0;
  243. }
  244. int test_str_eq(const char *file, int line, const char *st1, const char *st2,
  245. const char *s1, const char *s2)
  246. {
  247. if (s1 == NULL && s2 == NULL)
  248. return 1;
  249. if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
  250. test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
  251. s1, s1 == NULL ? 0 : strlen(s1),
  252. s2, s2 == NULL ? 0 : strlen(s2));
  253. return 0;
  254. }
  255. return 1;
  256. }
  257. int test_str_ne(const char *file, int line, const char *st1, const char *st2,
  258. const char *s1, const char *s2)
  259. {
  260. if ((s1 == NULL) ^ (s2 == NULL))
  261. return 1;
  262. if (s1 == NULL || strcmp(s1, s2) == 0) {
  263. test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
  264. s1, s1 == NULL ? 0 : strlen(s1),
  265. s2, s2 == NULL ? 0 : strlen(s2));
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
  271. const char *s1, size_t n1, const char *s2, size_t n2)
  272. {
  273. if (s1 == NULL && s2 == NULL)
  274. return 1;
  275. if (n1 != n2 || s1 == NULL || s2 == NULL || strncmp(s1, s2, n1) != 0) {
  276. test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
  277. s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1),
  278. s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2));
  279. return 0;
  280. }
  281. return 1;
  282. }
  283. int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
  284. const char *s1, size_t n1, const char *s2, size_t n2)
  285. {
  286. if ((s1 == NULL) ^ (s2 == NULL))
  287. return 1;
  288. if (n1 != n2 || s1 == NULL || strncmp(s1, s2, n1) == 0) {
  289. test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
  290. s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, n1),
  291. s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, n2));
  292. return 0;
  293. }
  294. return 1;
  295. }
  296. int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
  297. const void *s1, size_t n1, const void *s2, size_t n2)
  298. {
  299. if (s1 == NULL && s2 == NULL)
  300. return 1;
  301. if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
  302. test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==",
  303. s1, n1, s2, n2);
  304. return 0;
  305. }
  306. return 1;
  307. }
  308. int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
  309. const void *s1, size_t n1, const void *s2, size_t n2)
  310. {
  311. if ((s1 == NULL) ^ (s2 == NULL))
  312. return 1;
  313. if (n1 != n2)
  314. return 1;
  315. if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
  316. test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=",
  317. s1, n1, s2, n2);
  318. return 0;
  319. }
  320. return 1;
  321. }
  322. #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
  323. int test_BN_ ## opname(const char *file, int line, \
  324. const char *s1, const char *s2, \
  325. const BIGNUM *t1, const BIGNUM *t2) \
  326. { \
  327. if (BN_cmp(t1, t2) op 0) \
  328. return 1; \
  329. test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
  330. #op, t1, t2); \
  331. return 0; \
  332. } \
  333. int test_BN_ ## opname ## _zero(const char *file, int line, \
  334. const char *s, const BIGNUM *a) \
  335. { \
  336. if (a != NULL &&(zero_cond)) \
  337. return 1; \
  338. test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
  339. s, "0", #op, a); \
  340. return 0; \
  341. }
  342. DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
  343. DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
  344. DEFINE_BN_COMPARISONS(gt, >, !BN_is_negative(a) && !BN_is_zero(a))
  345. DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
  346. DEFINE_BN_COMPARISONS(lt, <, BN_is_negative(a) && !BN_is_zero(a))
  347. DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
  348. int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
  349. {
  350. if (a != NULL && BN_is_one(a))
  351. return 1;
  352. test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
  353. return 0;
  354. }
  355. int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
  356. {
  357. if (a != NULL && BN_is_odd(a))
  358. return 1;
  359. test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a);
  360. return 0;
  361. }
  362. int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
  363. {
  364. if (a != NULL && !BN_is_odd(a))
  365. return 1;
  366. test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
  367. a);
  368. return 0;
  369. }
  370. int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
  371. const BIGNUM *a, BN_ULONG w)
  372. {
  373. BIGNUM *bw;
  374. if (a != NULL && BN_is_word(a, w))
  375. return 1;
  376. if ((bw = BN_new()) != NULL)
  377. BN_set_word(bw, w);
  378. test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
  379. BN_free(bw);
  380. return 0;
  381. }
  382. int test_BN_abs_eq_word(const char *file, int line, const char *bns,
  383. const char *ws, const BIGNUM *a, BN_ULONG w)
  384. {
  385. BIGNUM *bw, *aa;
  386. if (a != NULL && BN_abs_is_word(a, w))
  387. return 1;
  388. if ((aa = BN_dup(a)) != NULL)
  389. BN_set_negative(aa, 0);
  390. if ((bw = BN_new()) != NULL)
  391. BN_set_word(bw, w);
  392. test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
  393. aa, bw);
  394. BN_free(bw);
  395. BN_free(aa);
  396. return 0;
  397. }
  398. static const char *print_time(const ASN1_TIME *t)
  399. {
  400. return t == NULL ? "<null>" : (const char *)ASN1_STRING_get0_data(t);
  401. }
  402. #define DEFINE_TIME_T_COMPARISON(opname, op) \
  403. int test_time_t_ ## opname(const char *file, int line, \
  404. const char *s1, const char *s2, \
  405. const time_t t1, const time_t t2) \
  406. { \
  407. ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1); \
  408. ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2); \
  409. int r = at1 != NULL && at2 != NULL \
  410. && ASN1_TIME_compare(at1, at2) op 0; \
  411. if (!r) \
  412. test_fail_message(NULL, file, line, "time_t", s1, s2, #op, \
  413. "[%s] compared to [%s]", \
  414. print_time(at1), print_time(at2)); \
  415. ASN1_STRING_free(at1); \
  416. ASN1_STRING_free(at2); \
  417. return r; \
  418. }
  419. DEFINE_TIME_T_COMPARISON(eq, ==)
  420. DEFINE_TIME_T_COMPARISON(ne, !=)
  421. DEFINE_TIME_T_COMPARISON(gt, >)
  422. DEFINE_TIME_T_COMPARISON(ge, >=)
  423. DEFINE_TIME_T_COMPARISON(lt, <)
  424. DEFINE_TIME_T_COMPARISON(le, <=)