tests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright 2017 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. void test_openssl_errors(void)
  142. {
  143. ERR_print_errors_cb(openssl_error_cb, NULL);
  144. ERR_clear_error();
  145. }
  146. /*
  147. * Define some comparisons between pairs of various types.
  148. * These functions return 1 if the test is true.
  149. * Otherwise, they return 0 and pretty-print diagnostics.
  150. *
  151. * In each case the functions produced are:
  152. * int test_name_eq(const type t1, const type t2, const char *desc, ...);
  153. * int test_name_ne(const type t1, const type t2, const char *desc, ...);
  154. * int test_name_lt(const type t1, const type t2, const char *desc, ...);
  155. * int test_name_le(const type t1, const type t2, const char *desc, ...);
  156. * int test_name_gt(const type t1, const type t2, const char *desc, ...);
  157. * int test_name_ge(const type t1, const type t2, const char *desc, ...);
  158. *
  159. * The t1 and t2 arguments are to be compared for equality, inequality,
  160. * less than, less than or equal to, greater than and greater than or
  161. * equal to respectively. If the specified condition holds, the functions
  162. * return 1. If the condition does not hold, the functions print a diagnostic
  163. * message and return 0.
  164. *
  165. * The desc argument is a printf format string followed by its arguments and
  166. * this is included in the output if the condition being tested for is false.
  167. */
  168. #define DEFINE_COMPARISON(type, name, opname, op, fmt) \
  169. int test_ ## name ## _ ## opname(const char *file, int line, \
  170. const char *s1, const char *s2, \
  171. const type t1, const type t2) \
  172. { \
  173. if (t1 op t2) \
  174. return 1; \
  175. test_fail_message(NULL, file, line, #type, s1, s2, #op, \
  176. "[" fmt "] compared to [" fmt "]", \
  177. t1, t2); \
  178. return 0; \
  179. }
  180. #define DEFINE_COMPARISONS(type, name, fmt) \
  181. DEFINE_COMPARISON(type, name, eq, ==, fmt) \
  182. DEFINE_COMPARISON(type, name, ne, !=, fmt) \
  183. DEFINE_COMPARISON(type, name, lt, <, fmt) \
  184. DEFINE_COMPARISON(type, name, le, <=, fmt) \
  185. DEFINE_COMPARISON(type, name, gt, >, fmt) \
  186. DEFINE_COMPARISON(type, name, ge, >=, fmt)
  187. DEFINE_COMPARISONS(int, int, "%d")
  188. DEFINE_COMPARISONS(unsigned int, uint, "%u")
  189. DEFINE_COMPARISONS(char, char, "%c")
  190. DEFINE_COMPARISONS(unsigned char, uchar, "%u")
  191. DEFINE_COMPARISONS(long, long, "%ld")
  192. DEFINE_COMPARISONS(unsigned long, ulong, "%lu")
  193. DEFINE_COMPARISONS(size_t, size_t, "%zu")
  194. DEFINE_COMPARISON(void *, ptr, eq, ==, "%p")
  195. DEFINE_COMPARISON(void *, ptr, ne, !=, "%p")
  196. int test_ptr_null(const char *file, int line, const char *s, const void *p)
  197. {
  198. if (p == NULL)
  199. return 1;
  200. test_fail_message(NULL, file, line, "ptr", s, "NULL", "==", "%p", p);
  201. return 0;
  202. }
  203. int test_ptr(const char *file, int line, const char *s, const void *p)
  204. {
  205. if (p != NULL)
  206. return 1;
  207. test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);
  208. return 0;
  209. }
  210. int test_true(const char *file, int line, const char *s, int b)
  211. {
  212. if (b)
  213. return 1;
  214. test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");
  215. return 0;
  216. }
  217. int test_false(const char *file, int line, const char *s, int b)
  218. {
  219. if (!b)
  220. return 1;
  221. test_fail_message(NULL, file, line, "bool", s, "false", "==", "true");
  222. return 0;
  223. }
  224. int test_str_eq(const char *file, int line, const char *st1, const char *st2,
  225. const char *s1, const char *s2)
  226. {
  227. if (s1 == NULL && s2 == NULL)
  228. return 1;
  229. if (s1 == NULL || s2 == NULL || strcmp(s1, s2) != 0) {
  230. test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
  231. s1, s1 == NULL ? 0 : strlen(s1),
  232. s2, s2 == NULL ? 0 : strlen(s2));
  233. return 0;
  234. }
  235. return 1;
  236. }
  237. int test_str_ne(const char *file, int line, const char *st1, const char *st2,
  238. const char *s1, const char *s2)
  239. {
  240. if ((s1 == NULL) ^ (s2 == NULL))
  241. return 1;
  242. if (s1 == NULL || strcmp(s1, s2) == 0) {
  243. test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
  244. s1, s1 == NULL ? 0 : strlen(s1),
  245. s2, s2 == NULL ? 0 : strlen(s2));
  246. return 0;
  247. }
  248. return 1;
  249. }
  250. int test_strn_eq(const char *file, int line, const char *st1, const char *st2,
  251. const char *s1, const char *s2, size_t len)
  252. {
  253. if (s1 == NULL && s2 == NULL)
  254. return 1;
  255. if (s1 == NULL || s2 == NULL || strncmp(s1, s2, len) != 0) {
  256. test_fail_string_message(NULL, file, line, "string", st1, st2, "==",
  257. s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
  258. s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
  259. return 0;
  260. }
  261. return 1;
  262. }
  263. int test_strn_ne(const char *file, int line, const char *st1, const char *st2,
  264. const char *s1, const char *s2, size_t len)
  265. {
  266. if ((s1 == NULL) ^ (s2 == NULL))
  267. return 1;
  268. if (s1 == NULL || strncmp(s1, s2, len) == 0) {
  269. test_fail_string_message(NULL, file, line, "string", st1, st2, "!=",
  270. s1, s1 == NULL ? 0 : OPENSSL_strnlen(s1, len),
  271. s2, s2 == NULL ? 0 : OPENSSL_strnlen(s2, len));
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. int test_mem_eq(const char *file, int line, const char *st1, const char *st2,
  277. const void *s1, size_t n1, const void *s2, size_t n2)
  278. {
  279. if (s1 == NULL && s2 == NULL)
  280. return 1;
  281. if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {
  282. test_fail_memory_message(NULL, file, line, "memory", st1, st2, "==",
  283. s1, n1, s2, n2);
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
  289. const void *s1, size_t n1, const void *s2, size_t n2)
  290. {
  291. if ((s1 == NULL) ^ (s2 == NULL))
  292. return 1;
  293. if (n1 != n2)
  294. return 1;
  295. if (s1 == NULL || memcmp(s1, s2, n1) == 0) {
  296. test_fail_memory_message(NULL, file, line, "memory", st1, st2, "!=",
  297. s1, n1, s2, n2);
  298. return 0;
  299. }
  300. return 1;
  301. }
  302. #define DEFINE_BN_COMPARISONS(opname, op, zero_cond) \
  303. int test_BN_ ## opname(const char *file, int line, \
  304. const char *s1, const char *s2, \
  305. const BIGNUM *t1, const BIGNUM *t2) \
  306. { \
  307. if (BN_cmp(t1, t2) op 0) \
  308. return 1; \
  309. test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2, \
  310. #op, t1, t2); \
  311. return 0; \
  312. } \
  313. int test_BN_ ## opname ## _zero(const char *file, int line, \
  314. const char *s, const BIGNUM *a) \
  315. { \
  316. if (a != NULL &&(zero_cond)) \
  317. return 1; \
  318. test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", \
  319. s, "0", #op, a); \
  320. return 0; \
  321. }
  322. DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
  323. DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
  324. DEFINE_BN_COMPARISONS(gt, >, !BN_is_negative(a) && !BN_is_zero(a))
  325. DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
  326. DEFINE_BN_COMPARISONS(lt, <, BN_is_negative(a) && !BN_is_zero(a))
  327. DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
  328. int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
  329. {
  330. if (a != NULL && BN_is_one(a))
  331. return 1;
  332. test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
  333. return 0;
  334. }
  335. int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
  336. {
  337. if (a != NULL && BN_is_odd(a))
  338. return 1;
  339. test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s, a);
  340. return 0;
  341. }
  342. int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
  343. {
  344. if (a != NULL && !BN_is_odd(a))
  345. return 1;
  346. test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
  347. a);
  348. return 0;
  349. }
  350. int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
  351. const BIGNUM *a, BN_ULONG w)
  352. {
  353. BIGNUM *bw;
  354. if (a != NULL && BN_is_word(a, w))
  355. return 1;
  356. bw = BN_new();
  357. BN_set_word(bw, w);
  358. test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
  359. BN_free(bw);
  360. return 0;
  361. }
  362. int test_BN_abs_eq_word(const char *file, int line, const char *bns,
  363. const char *ws, const BIGNUM *a, BN_ULONG w)
  364. {
  365. BIGNUM *bw, *aa;
  366. if (a != NULL && BN_abs_is_word(a, w))
  367. return 1;
  368. bw = BN_new();
  369. aa = BN_dup(a);
  370. BN_set_negative(aa, 0);
  371. BN_set_word(bw, w);
  372. test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
  373. aa, bw);
  374. BN_free(bw);
  375. BN_free(aa);
  376. return 0;
  377. }
  378. static const char *print_time(const ASN1_TIME *t)
  379. {
  380. return t == NULL ? "<null>" : (const char *)ASN1_STRING_get0_data(t);
  381. }
  382. #define DEFINE_TIME_T_COMPARISON(opname, op) \
  383. int test_time_t_ ## opname(const char *file, int line, \
  384. const char *s1, const char *s2, \
  385. const time_t t1, const time_t t2) \
  386. { \
  387. ASN1_TIME *at1 = ASN1_TIME_set(NULL, t1); \
  388. ASN1_TIME *at2 = ASN1_TIME_set(NULL, t2); \
  389. int r = at1 != NULL && at2 != NULL \
  390. && ASN1_TIME_compare(at1, at2) op 0; \
  391. if (!r) \
  392. test_fail_message(NULL, file, line, "time_t", s1, s2, #op, \
  393. "[%s] compared to [%s]", \
  394. print_time(at1), print_time(at2)); \
  395. ASN1_STRING_free(at1); \
  396. ASN1_STRING_free(at2); \
  397. return r; \
  398. }
  399. DEFINE_TIME_T_COMPARISON(eq, ==)
  400. DEFINE_TIME_T_COMPARISON(ne, !=)
  401. DEFINE_TIME_T_COMPARISON(gt, >)
  402. DEFINE_TIME_T_COMPARISON(ge, >=)
  403. DEFINE_TIME_T_COMPARISON(lt, <)
  404. DEFINE_TIME_T_COMPARISON(le, <=)