test_test.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/opensslconf.h>
  13. #include <openssl/err.h>
  14. #include <openssl/crypto.h>
  15. #include <openssl/bn.h>
  16. #include "internal/nelem.h"
  17. #include "testutil.h"
  18. #define TEST(expected, test) test_case((expected), #test, (test))
  19. static int test_case(int expected, const char *test, int result)
  20. {
  21. if (result != expected) {
  22. fprintf(stderr, "# FATAL: %s != %d\n", test, expected);
  23. return 0;
  24. }
  25. return 1;
  26. }
  27. static int test_int(void)
  28. {
  29. if (!TEST(1, TEST_int_eq(1, 1))
  30. | !TEST(0, TEST_int_eq(1, -1))
  31. | !TEST(1, TEST_int_ne(1, 2))
  32. | !TEST(0, TEST_int_ne(3, 3))
  33. | !TEST(1, TEST_int_lt(4, 9))
  34. | !TEST(0, TEST_int_lt(9, 4))
  35. | !TEST(1, TEST_int_le(4, 9))
  36. | !TEST(1, TEST_int_le(5, 5))
  37. | !TEST(0, TEST_int_le(9, 4))
  38. | !TEST(1, TEST_int_gt(8, 5))
  39. | !TEST(0, TEST_int_gt(5, 8))
  40. | !TEST(1, TEST_int_ge(8, 5))
  41. | !TEST(1, TEST_int_ge(6, 6))
  42. | !TEST(0, TEST_int_ge(5, 8)))
  43. goto err;
  44. return 1;
  45. err:
  46. return 0;
  47. }
  48. static int test_uint(void)
  49. {
  50. if (!TEST(1, TEST_uint_eq(3u, 3u))
  51. | !TEST(0, TEST_uint_eq(3u, 5u))
  52. | !TEST(1, TEST_uint_ne(4u, 2u))
  53. | !TEST(0, TEST_uint_ne(6u, 6u))
  54. | !TEST(1, TEST_uint_lt(5u, 9u))
  55. | !TEST(0, TEST_uint_lt(9u, 5u))
  56. | !TEST(1, TEST_uint_le(5u, 9u))
  57. | !TEST(1, TEST_uint_le(7u, 7u))
  58. | !TEST(0, TEST_uint_le(9u, 5u))
  59. | !TEST(1, TEST_uint_gt(11u, 1u))
  60. | !TEST(0, TEST_uint_gt(1u, 11u))
  61. | !TEST(1, TEST_uint_ge(11u, 1u))
  62. | !TEST(1, TEST_uint_ge(6u, 6u))
  63. | !TEST(0, TEST_uint_ge(1u, 11u)))
  64. goto err;
  65. return 1;
  66. err:
  67. return 0;
  68. }
  69. static int test_char(void)
  70. {
  71. if (!TEST(1, TEST_char_eq('a', 'a'))
  72. | !TEST(0, TEST_char_eq('a', 'A'))
  73. | !TEST(1, TEST_char_ne('a', 'c'))
  74. | !TEST(0, TEST_char_ne('e', 'e'))
  75. | !TEST(1, TEST_char_lt('i', 'x'))
  76. | !TEST(0, TEST_char_lt('x', 'i'))
  77. | !TEST(1, TEST_char_le('i', 'x'))
  78. | !TEST(1, TEST_char_le('n', 'n'))
  79. | !TEST(0, TEST_char_le('x', 'i'))
  80. | !TEST(1, TEST_char_gt('w', 'n'))
  81. | !TEST(0, TEST_char_gt('n', 'w'))
  82. | !TEST(1, TEST_char_ge('w', 'n'))
  83. | !TEST(1, TEST_char_ge('p', 'p'))
  84. | !TEST(0, TEST_char_ge('n', 'w')))
  85. goto err;
  86. return 1;
  87. err:
  88. return 0;
  89. }
  90. static int test_uchar(void)
  91. {
  92. if (!TEST(1, TEST_uchar_eq(49, 49))
  93. | !TEST(0, TEST_uchar_eq(49, 60))
  94. | !TEST(1, TEST_uchar_ne(50, 2))
  95. | !TEST(0, TEST_uchar_ne(66, 66))
  96. | !TEST(1, TEST_uchar_lt(60, 80))
  97. | !TEST(0, TEST_uchar_lt(80, 60))
  98. | !TEST(1, TEST_uchar_le(60, 80))
  99. | !TEST(1, TEST_uchar_le(78, 78))
  100. | !TEST(0, TEST_uchar_le(80, 60))
  101. | !TEST(1, TEST_uchar_gt(88, 37))
  102. | !TEST(0, TEST_uchar_gt(37, 88))
  103. | !TEST(1, TEST_uchar_ge(88, 37))
  104. | !TEST(1, TEST_uchar_ge(66, 66))
  105. | !TEST(0, TEST_uchar_ge(37, 88)))
  106. goto err;
  107. return 1;
  108. err:
  109. return 0;
  110. }
  111. static int test_long(void)
  112. {
  113. if (!TEST(1, TEST_long_eq(123l, 123l))
  114. | !TEST(0, TEST_long_eq(123l, -123l))
  115. | !TEST(1, TEST_long_ne(123l, 500l))
  116. | !TEST(0, TEST_long_ne(1000l, 1000l))
  117. | !TEST(1, TEST_long_lt(-8923l, 102934563l))
  118. | !TEST(0, TEST_long_lt(102934563l, -8923l))
  119. | !TEST(1, TEST_long_le(-8923l, 102934563l))
  120. | !TEST(1, TEST_long_le(12345l, 12345l))
  121. | !TEST(0, TEST_long_le(102934563l, -8923l))
  122. | !TEST(1, TEST_long_gt(84325677l, 12345l))
  123. | !TEST(0, TEST_long_gt(12345l, 84325677l))
  124. | !TEST(1, TEST_long_ge(84325677l, 12345l))
  125. | !TEST(1, TEST_long_ge(465869l, 465869l))
  126. | !TEST(0, TEST_long_ge(12345l, 84325677l)))
  127. goto err;
  128. return 1;
  129. err:
  130. return 0;
  131. }
  132. static int test_ulong(void)
  133. {
  134. if (!TEST(1, TEST_ulong_eq(919ul, 919ul))
  135. | !TEST(0, TEST_ulong_eq(919ul, 10234ul))
  136. | !TEST(1, TEST_ulong_ne(8190ul, 66ul))
  137. | !TEST(0, TEST_ulong_ne(10555ul, 10555ul))
  138. | !TEST(1, TEST_ulong_lt(10234ul, 1000000ul))
  139. | !TEST(0, TEST_ulong_lt(1000000ul, 10234ul))
  140. | !TEST(1, TEST_ulong_le(10234ul, 1000000ul))
  141. | !TEST(1, TEST_ulong_le(100000ul, 100000ul))
  142. | !TEST(0, TEST_ulong_le(1000000ul, 10234ul))
  143. | !TEST(1, TEST_ulong_gt(100000000ul, 22ul))
  144. | !TEST(0, TEST_ulong_gt(22ul, 100000000ul))
  145. | !TEST(1, TEST_ulong_ge(100000000ul, 22ul))
  146. | !TEST(1, TEST_ulong_ge(10555ul, 10555ul))
  147. | !TEST(0, TEST_ulong_ge(22ul, 100000000ul)))
  148. goto err;
  149. return 1;
  150. err:
  151. return 0;
  152. }
  153. static int test_size_t(void)
  154. {
  155. if (!TEST(1, TEST_size_t_eq((size_t)10, (size_t)10))
  156. | !TEST(0, TEST_size_t_eq((size_t)10, (size_t)12))
  157. | !TEST(1, TEST_size_t_ne((size_t)10, (size_t)12))
  158. | !TEST(0, TEST_size_t_ne((size_t)24, (size_t)24))
  159. | !TEST(1, TEST_size_t_lt((size_t)30, (size_t)88))
  160. | !TEST(0, TEST_size_t_lt((size_t)88, (size_t)30))
  161. | !TEST(1, TEST_size_t_le((size_t)30, (size_t)88))
  162. | !TEST(1, TEST_size_t_le((size_t)33, (size_t)33))
  163. | !TEST(0, TEST_size_t_le((size_t)88, (size_t)30))
  164. | !TEST(1, TEST_size_t_gt((size_t)52, (size_t)33))
  165. | !TEST(0, TEST_size_t_gt((size_t)33, (size_t)52))
  166. | !TEST(1, TEST_size_t_ge((size_t)52, (size_t)33))
  167. | !TEST(1, TEST_size_t_ge((size_t)38, (size_t)38))
  168. | !TEST(0, TEST_size_t_ge((size_t)33, (size_t)52)))
  169. goto err;
  170. return 1;
  171. err:
  172. return 0;
  173. }
  174. static int test_time_t(void)
  175. {
  176. if (!TEST(1, TEST_time_t_eq((time_t)10, (time_t)10))
  177. | !TEST(0, TEST_time_t_eq((time_t)10, (time_t)12))
  178. | !TEST(1, TEST_time_t_ne((time_t)10, (time_t)12))
  179. | !TEST(0, TEST_time_t_ne((time_t)24, (time_t)24))
  180. | !TEST(1, TEST_time_t_lt((time_t)30, (time_t)88))
  181. | !TEST(0, TEST_time_t_lt((time_t)88, (time_t)30))
  182. | !TEST(1, TEST_time_t_le((time_t)30, (time_t)88))
  183. | !TEST(1, TEST_time_t_le((time_t)33, (time_t)33))
  184. | !TEST(0, TEST_time_t_le((time_t)88, (time_t)30))
  185. | !TEST(1, TEST_time_t_gt((time_t)52, (time_t)33))
  186. | !TEST(0, TEST_time_t_gt((time_t)33, (time_t)52))
  187. | !TEST(1, TEST_time_t_ge((time_t)52, (time_t)33))
  188. | !TEST(1, TEST_time_t_ge((time_t)38, (time_t)38))
  189. | !TEST(0, TEST_time_t_ge((time_t)33, (time_t)52)))
  190. goto err;
  191. return 1;
  192. err:
  193. return 0;
  194. }
  195. static int test_pointer(void)
  196. {
  197. int x = 0;
  198. char y = 1;
  199. if (!TEST(1, TEST_ptr(&y))
  200. | !TEST(0, TEST_ptr(NULL))
  201. | !TEST(0, TEST_ptr_null(&y))
  202. | !TEST(1, TEST_ptr_null(NULL))
  203. | !TEST(1, TEST_ptr_eq(NULL, NULL))
  204. | !TEST(0, TEST_ptr_eq(NULL, &y))
  205. | !TEST(0, TEST_ptr_eq(&y, NULL))
  206. | !TEST(0, TEST_ptr_eq(&y, &x))
  207. | !TEST(1, TEST_ptr_eq(&x, &x))
  208. | !TEST(0, TEST_ptr_ne(NULL, NULL))
  209. | !TEST(1, TEST_ptr_ne(NULL, &y))
  210. | !TEST(1, TEST_ptr_ne(&y, NULL))
  211. | !TEST(1, TEST_ptr_ne(&y, &x))
  212. | !TEST(0, TEST_ptr_ne(&x, &x)))
  213. goto err;
  214. return 1;
  215. err:
  216. return 0;
  217. }
  218. static int test_bool(void)
  219. {
  220. if (!TEST(0, TEST_true(0))
  221. | !TEST(1, TEST_true(1))
  222. | !TEST(1, TEST_false(0))
  223. | !TEST(0, TEST_false(1)))
  224. goto err;
  225. return 1;
  226. err:
  227. return 0;
  228. }
  229. static int test_string(void)
  230. {
  231. static char buf[] = "abc";
  232. if (!TEST(1, TEST_str_eq(NULL, NULL))
  233. | !TEST(1, TEST_str_eq("abc", buf))
  234. | !TEST(0, TEST_str_eq("abc", NULL))
  235. | !TEST(0, TEST_str_eq("abc", ""))
  236. | !TEST(0, TEST_str_eq(NULL, buf))
  237. | !TEST(0, TEST_str_ne(NULL, NULL))
  238. | !TEST(0, TEST_str_eq("", NULL))
  239. | !TEST(0, TEST_str_eq(NULL, ""))
  240. | !TEST(0, TEST_str_ne("", ""))
  241. | !TEST(0, TEST_str_eq("\1\2\3\4\5", "\1x\3\6\5"))
  242. | !TEST(0, TEST_str_ne("abc", buf))
  243. | !TEST(1, TEST_str_ne("abc", NULL))
  244. | !TEST(1, TEST_str_ne(NULL, buf))
  245. | !TEST(0, TEST_str_eq("abcdef", "abcdefghijk")))
  246. goto err;
  247. return 1;
  248. err:
  249. return 0;
  250. }
  251. static int test_memory(void)
  252. {
  253. static char buf[] = "xyz";
  254. if (!TEST(1, TEST_mem_eq(NULL, 0, NULL, 0))
  255. | !TEST(1, TEST_mem_eq(NULL, 1, NULL, 2))
  256. | !TEST(0, TEST_mem_eq(NULL, 0, "xyz", 3))
  257. | !TEST(0, TEST_mem_eq(NULL, 7, "abc", 3))
  258. | !TEST(0, TEST_mem_ne(NULL, 0, NULL, 0))
  259. | !TEST(0, TEST_mem_eq(NULL, 0, "", 0))
  260. | !TEST(0, TEST_mem_eq("", 0, NULL, 0))
  261. | !TEST(0, TEST_mem_ne("", 0, "", 0))
  262. | !TEST(0, TEST_mem_eq("xyz", 3, NULL, 0))
  263. | !TEST(0, TEST_mem_eq("xyz", 3, buf, sizeof(buf)))
  264. | !TEST(1, TEST_mem_eq("xyz", 4, buf, sizeof(buf))))
  265. goto err;
  266. return 1;
  267. err:
  268. return 0;
  269. }
  270. static int test_memory_overflow(void)
  271. {
  272. /* Verify that the memory printing overflows without walking the stack */
  273. const char *p = "1234567890123456789012345678901234567890123456789012";
  274. const char *q = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  275. return TEST(0, TEST_mem_eq(p, strlen(p), q, strlen(q)));
  276. }
  277. static int test_bignum(void)
  278. {
  279. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  280. int r = 0;
  281. if (!TEST(1, TEST_int_eq(BN_dec2bn(&a, "0"), 1))
  282. | !TEST(1, TEST_BN_eq_word(a, 0))
  283. | !TEST(0, TEST_BN_eq_word(a, 30))
  284. | !TEST(1, TEST_BN_abs_eq_word(a, 0))
  285. | !TEST(0, TEST_BN_eq_one(a))
  286. | !TEST(1, TEST_BN_eq_zero(a))
  287. | !TEST(0, TEST_BN_ne_zero(a))
  288. | !TEST(1, TEST_BN_le_zero(a))
  289. | !TEST(0, TEST_BN_lt_zero(a))
  290. | !TEST(1, TEST_BN_ge_zero(a))
  291. | !TEST(0, TEST_BN_gt_zero(a))
  292. | !TEST(1, TEST_BN_even(a))
  293. | !TEST(0, TEST_BN_odd(a))
  294. | !TEST(1, TEST_BN_eq(b, c))
  295. | !TEST(0, TEST_BN_eq(a, b))
  296. | !TEST(0, TEST_BN_ne(NULL, c))
  297. | !TEST(1, TEST_int_eq(BN_dec2bn(&b, "1"), 1))
  298. | !TEST(1, TEST_BN_eq_word(b, 1))
  299. | !TEST(1, TEST_BN_eq_one(b))
  300. | !TEST(0, TEST_BN_abs_eq_word(b, 0))
  301. | !TEST(1, TEST_BN_abs_eq_word(b, 1))
  302. | !TEST(0, TEST_BN_eq_zero(b))
  303. | !TEST(1, TEST_BN_ne_zero(b))
  304. | !TEST(0, TEST_BN_le_zero(b))
  305. | !TEST(0, TEST_BN_lt_zero(b))
  306. | !TEST(1, TEST_BN_ge_zero(b))
  307. | !TEST(1, TEST_BN_gt_zero(b))
  308. | !TEST(0, TEST_BN_even(b))
  309. | !TEST(1, TEST_BN_odd(b))
  310. | !TEST(1, TEST_int_eq(BN_dec2bn(&c, "-334739439"), 10))
  311. | !TEST(0, TEST_BN_eq_word(c, 334739439))
  312. | !TEST(1, TEST_BN_abs_eq_word(c, 334739439))
  313. | !TEST(0, TEST_BN_eq_zero(c))
  314. | !TEST(1, TEST_BN_ne_zero(c))
  315. | !TEST(1, TEST_BN_le_zero(c))
  316. | !TEST(1, TEST_BN_lt_zero(c))
  317. | !TEST(0, TEST_BN_ge_zero(c))
  318. | !TEST(0, TEST_BN_gt_zero(c))
  319. | !TEST(0, TEST_BN_even(c))
  320. | !TEST(1, TEST_BN_odd(c))
  321. | !TEST(1, TEST_BN_eq(a, a))
  322. | !TEST(0, TEST_BN_ne(a, a))
  323. | !TEST(0, TEST_BN_eq(a, b))
  324. | !TEST(1, TEST_BN_ne(a, b))
  325. | !TEST(0, TEST_BN_lt(a, c))
  326. | !TEST(1, TEST_BN_lt(c, b))
  327. | !TEST(0, TEST_BN_lt(b, c))
  328. | !TEST(0, TEST_BN_le(a, c))
  329. | !TEST(1, TEST_BN_le(c, b))
  330. | !TEST(0, TEST_BN_le(b, c))
  331. | !TEST(1, TEST_BN_gt(a, c))
  332. | !TEST(0, TEST_BN_gt(c, b))
  333. | !TEST(1, TEST_BN_gt(b, c))
  334. | !TEST(1, TEST_BN_ge(a, c))
  335. | !TEST(0, TEST_BN_ge(c, b))
  336. | !TEST(1, TEST_BN_ge(b, c)))
  337. goto err;
  338. r = 1;
  339. err:
  340. BN_free(a);
  341. BN_free(b);
  342. BN_free(c);
  343. return r;
  344. }
  345. static int test_long_output(void)
  346. {
  347. const char *p = "1234567890123456789012345678901234567890123456789012";
  348. const char *q = "1234567890klmnopqrs01234567890EFGHIJKLM0123456789XYZ";
  349. const char *r = "1234567890123456789012345678901234567890123456789012"
  350. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY+"
  351. "12345678901234567890123ABC78901234567890123456789012";
  352. const char *s = "1234567890123456789012345678901234567890123456789012"
  353. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY-"
  354. "1234567890123456789012345678901234567890123456789012"
  355. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  356. return TEST(0, TEST_str_eq(p, q))
  357. & TEST(0, TEST_str_eq(q, r))
  358. & TEST(0, TEST_str_eq(r, s))
  359. & TEST(0, TEST_mem_eq(r, strlen(r), s, strlen(s)));
  360. }
  361. static int test_long_bignum(void)
  362. {
  363. int r;
  364. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  365. const char as[] = "1234567890123456789012345678901234567890123456789012"
  366. "1234567890123456789012345678901234567890123456789012"
  367. "1234567890123456789012345678901234567890123456789012"
  368. "1234567890123456789012345678901234567890123456789012"
  369. "1234567890123456789012345678901234567890123456789012"
  370. "1234567890123456789012345678901234567890123456789012"
  371. "FFFFFF";
  372. const char bs[] = "1234567890123456789012345678901234567890123456789012"
  373. "1234567890123456789012345678901234567890123456789013"
  374. "987657";
  375. const char cs[] = "-" /* 64 characters plus sign */
  376. "123456789012345678901234567890"
  377. "123456789012345678901234567890"
  378. "ABCD";
  379. const char ds[] = "-" /* 63 characters plus sign */
  380. "23456789A123456789B123456789C"
  381. "123456789D123456789E123456789F"
  382. "ABCD";
  383. r = TEST_true(BN_hex2bn(&a, as))
  384. && TEST_true(BN_hex2bn(&b, bs))
  385. && TEST_true(BN_hex2bn(&c, cs))
  386. && TEST_true(BN_hex2bn(&d, ds))
  387. && (TEST(0, TEST_BN_eq(a, b))
  388. & TEST(0, TEST_BN_eq(b, a))
  389. & TEST(0, TEST_BN_eq(b, NULL))
  390. & TEST(0, TEST_BN_eq(NULL, a))
  391. & TEST(1, TEST_BN_ne(a, NULL))
  392. & TEST(0, TEST_BN_eq(c, d)));
  393. BN_free(a);
  394. BN_free(b);
  395. BN_free(c);
  396. BN_free(d);
  397. return r;
  398. }
  399. static int test_messages(void)
  400. {
  401. TEST_info("This is an %s message.", "info");
  402. TEST_error("This is an %s message.", "error");
  403. return 1;
  404. }
  405. static int test_single_eval(void)
  406. {
  407. int i = 4;
  408. long l = -9000;
  409. char c = 'd';
  410. unsigned char uc = 22;
  411. unsigned long ul = 500;
  412. size_t st = 1234;
  413. char buf[4] = { 0 }, *p = buf;
  414. /* int */
  415. return TEST_int_eq(i++, 4)
  416. && TEST_int_eq(i, 5)
  417. && TEST_int_gt(++i, 5)
  418. && TEST_int_le(5, i++)
  419. && TEST_int_ne(--i, 5)
  420. && TEST_int_eq(12, i *= 2)
  421. /* Long */
  422. && TEST_long_eq(l--, -9000L)
  423. && TEST_long_eq(++l, -9000L)
  424. && TEST_long_ne(-9000L, l /= 2)
  425. && TEST_long_lt(--l, -4500L)
  426. /* char */
  427. && TEST_char_eq(++c, 'e')
  428. && TEST_char_eq('e', c--)
  429. && TEST_char_ne('d', --c)
  430. && TEST_char_le('b', --c)
  431. && TEST_char_lt(c++, 'c')
  432. /* unsigned char */
  433. && TEST_uchar_eq(22, uc++)
  434. && TEST_uchar_eq(uc /= 2, 11)
  435. && TEST_ulong_eq(ul ^= 1, 501)
  436. && TEST_ulong_eq(502, ul ^= 3)
  437. && TEST_ulong_eq(ul = ul * 3 - 6, 1500)
  438. /* size_t */
  439. && TEST_size_t_eq((--i, st++), 1234)
  440. && TEST_size_t_eq(st, 1235)
  441. && TEST_int_eq(11, i)
  442. /* pointers */
  443. && TEST_ptr_eq(p++, buf)
  444. && TEST_ptr_eq(buf + 2, ++p)
  445. && TEST_ptr_eq(buf, p -= 2)
  446. && TEST_ptr(++p)
  447. && TEST_ptr_eq(p, buf + 1)
  448. && TEST_ptr_null(p = NULL)
  449. /* strings */
  450. && TEST_str_eq(p = &("123456"[1]), "23456")
  451. && TEST_str_eq("3456", ++p)
  452. && TEST_str_ne(p++, "456")
  453. /* memory */
  454. && TEST_mem_eq(--p, sizeof("3456"), "3456", sizeof("3456"))
  455. && TEST_mem_ne(p++, sizeof("456"), "456", sizeof("456"))
  456. && TEST_mem_eq(p--, sizeof("456"), "456", sizeof("456"));
  457. }
  458. static int test_output(void)
  459. {
  460. const char s[] = "1234567890123456789012345678901234567890123456789012"
  461. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  462. test_output_string("test", s, sizeof(s) - 1);
  463. test_output_memory("test", (const unsigned char *)s, sizeof(s));
  464. return 1;
  465. }
  466. static const char *bn_output_tests[] = {
  467. NULL,
  468. "0",
  469. "-12345678",
  470. "1234567890123456789012345678901234567890123456789012"
  471. "1234567890123456789012345678901234567890123456789013"
  472. "987657"
  473. };
  474. static int test_bn_output(int n)
  475. {
  476. BIGNUM *b = NULL;
  477. if (bn_output_tests[n] != NULL
  478. && !TEST_true(BN_hex2bn(&b, bn_output_tests[n])))
  479. return 0;
  480. test_output_bignum(bn_output_tests[n], b);
  481. BN_free(b);
  482. return 1;
  483. }
  484. static int test_skip_one(void)
  485. {
  486. return TEST_skip("skip test");
  487. }
  488. static int test_skip_many(int n)
  489. {
  490. return TEST_skip("skip tests: %d", n);
  491. }
  492. static int test_skip_null(void)
  493. {
  494. /*
  495. * This is not a recommended way of skipping a test, a reason or
  496. * description should be included.
  497. */
  498. return TEST_skip(NULL);
  499. }
  500. int setup_tests(void)
  501. {
  502. ADD_TEST(test_int);
  503. ADD_TEST(test_uint);
  504. ADD_TEST(test_char);
  505. ADD_TEST(test_uchar);
  506. ADD_TEST(test_long);
  507. ADD_TEST(test_ulong);
  508. ADD_TEST(test_size_t);
  509. ADD_TEST(test_time_t);
  510. ADD_TEST(test_pointer);
  511. ADD_TEST(test_bool);
  512. ADD_TEST(test_string);
  513. ADD_TEST(test_memory);
  514. ADD_TEST(test_memory_overflow);
  515. ADD_TEST(test_bignum);
  516. ADD_TEST(test_long_bignum);
  517. ADD_TEST(test_long_output);
  518. ADD_TEST(test_messages);
  519. ADD_TEST(test_single_eval);
  520. ADD_TEST(test_output);
  521. ADD_ALL_TESTS(test_bn_output, OSSL_NELEM(bn_output_tests));
  522. ADD_TEST(test_skip_one);
  523. ADD_TEST(test_skip_null);
  524. ADD_ALL_TESTS(test_skip_many, 3);
  525. return 1;
  526. }