tests.c 16 KB

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