2
0

unit.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef TESTS_UNIT_H
  22. #define TESTS_UNIT_H
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #ifndef WOLFSSL_USER_SETTINGS
  27. #include <wolfssl/options.h>
  28. #endif
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #undef TEST_OPENSSL_COEXIST /* can't use this option with unit tests */
  31. #undef OPENSSL_COEXIST /* can't use this option with unit tests */
  32. #include <wolfssl/ssl.h>
  33. #include <wolfssl/test.h> /* thread and tcp stuff */
  34. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  35. #define XABORT() WC_DO_NOTHING
  36. #else
  37. #define XABORT() abort()
  38. #endif
  39. #ifndef WOLFSSL_PASSTHRU_ERR
  40. #define Fail(description, result) do { \
  41. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  42. fputs("\n expected: ", stdout); printf description; \
  43. fputs("\n result: ", stdout); printf result; fputs("\n\n", stdout); \
  44. fflush(stdout); \
  45. XABORT(); \
  46. } while(0)
  47. #else
  48. #define Fail(description, result) do { \
  49. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  50. fputs("\n expected: ", stdout); printf description; \
  51. fputs("\n result: ", stdout); printf result; fputs("\n\n", stdout); \
  52. fflush(stdout); \
  53. } while (0)
  54. #endif
  55. #define Assert(test, description, result) if (!(test)) Fail(description, result)
  56. #define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE"))
  57. #define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE"))
  58. #define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL"))
  59. #define AssertNull(x) do { \
  60. PEDANTIC_EXTENSION void* _x = (void*)(x); \
  61. Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  62. } while(0)
  63. #define AssertInt(x, y, op, er) do { \
  64. int _x = (int)(x); \
  65. int _y = (int)(y); \
  66. Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \
  67. } while(0)
  68. #define AssertIntEQ(x, y) AssertInt(x, y, ==, !=)
  69. #define AssertIntNE(x, y) AssertInt(x, y, !=, ==)
  70. #define AssertIntGT(x, y) AssertInt(x, y, >, <=)
  71. #define AssertIntLT(x, y) AssertInt(x, y, <, >=)
  72. #define AssertIntGE(x, y) AssertInt(x, y, >=, <)
  73. #define AssertIntLE(x, y) AssertInt(x, y, <=, >)
  74. #define AssertStr(x, y, op, er) do { \
  75. const char* _x = (const char*)(x); \
  76. const char* _y = (const char*)(y); \
  77. int _z = (_x && _y) ? strcmp(_x, _y) : -1; \
  78. Assert(_z op 0, ("%s " #op " %s", #x, #y), \
  79. ("\"%s\" " #er " \"%s\"", _x, _y));\
  80. } while(0)
  81. #define AssertStrEQ(x, y) AssertStr(x, y, ==, !=)
  82. #define AssertStrNE(x, y) AssertStr(x, y, !=, ==)
  83. #define AssertStrGT(x, y) AssertStr(x, y, >, <=)
  84. #define AssertStrLT(x, y) AssertStr(x, y, <, >=)
  85. #define AssertStrGE(x, y) AssertStr(x, y, >=, <)
  86. #define AssertStrLE(x, y) AssertStr(x, y, <=, >)
  87. #ifdef WOLF_C89
  88. #define AssertPtr(x, y, op, er) do { \
  89. void* _x = (void*)(x); \
  90. void* _y = (void*)(y); \
  91. Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \
  92. } while(0)
  93. #else
  94. #define AssertPtr(x, y, op, er) do { \
  95. PRAGMA_GCC_DIAG_PUSH \
  96. /* remarkably, without this inhibition, */ \
  97. /* the _Pragma()s make the declarations warn. */ \
  98. PRAGMA_GCC("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
  99. /* inhibit "ISO C forbids conversion of function pointer */ \
  100. /* to object pointer type [-Werror=pedantic]" */ \
  101. PRAGMA_GCC("GCC diagnostic ignored \"-Wpedantic\"") \
  102. void* _x = (void*)(x); \
  103. void* _y = (void*)(y); \
  104. Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \
  105. PRAGMA_GCC_DIAG_POP \
  106. } while(0)
  107. #endif
  108. #define AssertPtrEq(x, y) AssertPtr(x, y, ==, !=)
  109. #define AssertPtrNE(x, y) AssertPtr(x, y, !=, ==)
  110. #define AssertPtrGT(x, y) AssertPtr(x, y, >, <=)
  111. #define AssertPtrLT(x, y) AssertPtr(x, y, <, >=)
  112. #define AssertPtrGE(x, y) AssertPtr(x, y, >=, <)
  113. #define AssertPtrLE(x, y) AssertPtr(x, y, <=, >)
  114. #define TEST_FAIL 0
  115. #define TEST_SUCCESS 1
  116. #define TEST_SUCCESS_NO_MSGS 2
  117. #define TEST_SKIPPED 3 /* Test skipped - not run. */
  118. #define TEST_SKIPPED_NO_MSGS 4 /* Test skipped - not run. */
  119. #define EXPECT_DECLS \
  120. int _ret = TEST_SKIPPED, _fail_codepoint_id = TEST_FAIL
  121. #define EXPECT_DECLS_NO_MSGS(fail_codepoint_offset) \
  122. int _ret = TEST_SKIPPED_NO_MSGS, \
  123. _fail_codepoint_id = (fail_codepoint_offset)
  124. #define EXPECT_FAILURE_CODEPOINT_ID _fail_codepoint_id
  125. #define EXPECT_RESULT() \
  126. ((void)_fail_codepoint_id, \
  127. _ret == TEST_SUCCESS_NO_MSGS ? TEST_SUCCESS : \
  128. _ret == TEST_SKIPPED_NO_MSGS ? TEST_SKIPPED : \
  129. _ret)
  130. #define EXPECT_SUCCESS() \
  131. ((_ret == TEST_SUCCESS) || \
  132. (_ret == TEST_SKIPPED) || \
  133. (_ret == TEST_SUCCESS_NO_MSGS) || \
  134. (_ret == TEST_SKIPPED_NO_MSGS))
  135. #define EXPECT_FAIL() \
  136. (! EXPECT_SUCCESS())
  137. #define EXPECT_TEST(ret) do { \
  138. if (EXPECT_SUCCESS()) { \
  139. _ret = (ret); \
  140. } \
  141. } while (0)
  142. #define ExpFail(description, result) do { \
  143. if ((_ret == TEST_SUCCESS_NO_MSGS) || (_ret == TEST_SKIPPED_NO_MSGS)) \
  144. _ret = _fail_codepoint_id; \
  145. else { \
  146. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  147. fputs("\n expected: ", stdout); printf description; \
  148. fputs("\n result: ", stdout); printf result; \
  149. fputs("\n\n", stdout); \
  150. fflush(stdout); \
  151. _ret = TEST_FAIL; \
  152. } \
  153. } while (0)
  154. #define Expect(test, description, result) do { \
  155. if (EXPECT_SUCCESS()) { \
  156. if (!(test)) \
  157. ExpFail(description, result); \
  158. else if (_ret == TEST_SKIPPED_NO_MSGS) \
  159. _ret = TEST_SUCCESS_NO_MSGS; \
  160. else \
  161. _ret = TEST_SUCCESS; \
  162. } \
  163. if (_ret == TEST_SUCCESS_NO_MSGS) \
  164. --_fail_codepoint_id; \
  165. } while (0)
  166. #define ExpectTrue(x) Expect( (x), ("%s is true", #x), (#x " => FALSE"))
  167. #define ExpectFalse(x) Expect(!(x), ("%s is false", #x), (#x " => TRUE"))
  168. #define ExpectNotNull(x) Expect( (x), ("%s is not null", #x), (#x " => NULL"))
  169. #define ExpectNull(x) do { \
  170. if (EXPECT_SUCCESS()) { \
  171. PEDANTIC_EXTENSION void* _x = (void*)(x); \
  172. Expect(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  173. } \
  174. } while(0)
  175. #define ExpectInt(x, y, op, er) do { \
  176. if (EXPECT_SUCCESS()) { \
  177. int _x = (int)(x); \
  178. int _y = (int)(y); \
  179. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y));\
  180. } \
  181. } while(0)
  182. #define ExpectIntEQ(x, y) ExpectInt(x, y, ==, !=)
  183. #define ExpectIntNE(x, y) ExpectInt(x, y, !=, ==)
  184. #define ExpectIntGT(x, y) ExpectInt(x, y, >, <=)
  185. #define ExpectIntLT(x, y) ExpectInt(x, y, <, >=)
  186. #define ExpectIntGE(x, y) ExpectInt(x, y, >=, <)
  187. #define ExpectIntLE(x, y) ExpectInt(x, y, <=, >)
  188. #define ExpectStr(x, y, op, er) do { \
  189. if (EXPECT_SUCCESS()) { \
  190. const char* _x = (const char*)(x); \
  191. const char* _y = (const char*)(y); \
  192. int _z = (_x && _y) ? XSTRCMP(_x, _y) : -1; \
  193. Expect(_z op 0, ("%s " #op " %s", #x, #y), \
  194. ("\"%s\" " #er " \"%s\"", _x, _y));\
  195. } \
  196. } while(0)
  197. #define ExpectStrEQ(x, y) ExpectStr(x, y, ==, !=)
  198. #define ExpectStrNE(x, y) ExpectStr(x, y, !=, ==)
  199. #define ExpectStrGT(x, y) ExpectStr(x, y, >, <=)
  200. #define ExpectStrLT(x, y) ExpectStr(x, y, <, >=)
  201. #define ExpectStrGE(x, y) ExpectStr(x, y, >=, <)
  202. #define ExpectStrLE(x, y) ExpectStr(x, y, <=, >)
  203. #define ExpectPtr(x, y, op, er) do { \
  204. if (EXPECT_SUCCESS()) { \
  205. PRAGMA_DIAG_PUSH \
  206. /* remarkably, without this inhibition, */ \
  207. /* the _Pragma()s make the declarations warn. */ \
  208. PRAGMA("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
  209. /* inhibit "ISO C forbids conversion of function pointer */ \
  210. /* to object pointer type [-Werror=pedantic]" */ \
  211. PRAGMA("GCC diagnostic ignored \"-Wpedantic\"") \
  212. void* _x = (void*)(x); \
  213. void* _y = (void*)(y); \
  214. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y));\
  215. PRAGMA_DIAG_POP \
  216. } \
  217. } while(0)
  218. #define ExpectPtrEq(x, y) ExpectPtr(x, y, ==, !=)
  219. #define ExpectPtrNE(x, y) ExpectPtr(x, y, !=, ==)
  220. #define ExpectPtrGT(x, y) ExpectPtr(x, y, >, <=)
  221. #define ExpectPtrLT(x, y) ExpectPtr(x, y, <, >=)
  222. #define ExpectPtrGE(x, y) ExpectPtr(x, y, >=, <)
  223. #define ExpectPtrLE(x, y) ExpectPtr(x, y, <=, >)
  224. #define ExpectBuf(x, y, z, op, er) do { \
  225. if (EXPECT_SUCCESS()) { \
  226. const byte* _x = (const byte*)(x); \
  227. const byte* _y = (const byte*)(y); \
  228. int _z = (int)(z); \
  229. int _w = ((_x) && (_y)) ? XMEMCMP(_x, _y, (unsigned long)_z) : -1; \
  230. Expect(_w op 0, ("%s " #op " %s for %s", #x, #y, #z), \
  231. ("\"%p\" " #er " \"%p\" for \"%d\"", \
  232. (const void *)_x, (const void *)_y, _z)); \
  233. } \
  234. } while(0)
  235. #define ExpectBufEQ(x, y, z) ExpectBuf(x, y, z, ==, !=)
  236. #define ExpectBufNE(x, y, z) ExpectBuf(x, y, z, !=, ==)
  237. #define ExpectFail() ExpectTrue(0)
  238. #define DoExpectNull(x) do { \
  239. PEDANTIC_EXTENSION void* _x = (void*)(x); \
  240. Expect(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  241. } while(0)
  242. #define DoExpectInt(x, y, op, er) do { \
  243. int _x = (int)(x); \
  244. int _y = (int)(y); \
  245. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \
  246. } while(0)
  247. #define DoExpectIntEQ(x, y) DoExpectInt(x, y, ==, !=)
  248. #define DoExpectIntNE(x, y) DoExpectInt(x, y, !=, ==)
  249. #define DoExpectIntGT(x, y) DoExpectInt(x, y, >, <=)
  250. #define DoExpectIntLT(x, y) DoExpectInt(x, y, <, >=)
  251. #define DoExpectIntGE(x, y) DoExpectInt(x, y, >=, <)
  252. #define DoExpectIntLE(x, y) DoExpectInt(x, y, <=, >)
  253. #define DoExpectStr(x, y, op, er) do { \
  254. const char* _x = (const char*)(x); \
  255. const char* _y = (const char*)(y); \
  256. int _z = (_x && _y) ? strcmp(_x, _y) : -1; \
  257. Expect(_z op 0, ("%s " #op " %s", #x, #y), \
  258. ("\"%s\" " #er " \"%s\"", _x, _y));\
  259. } while(0)
  260. #define DoExpectStrEQ(x, y) DoExpectStr(x, y, ==, !=)
  261. #define DoExpectStrNE(x, y) DoExpectStr(x, y, !=, ==)
  262. #define DoExpectStrGT(x, y) DoExpectStr(x, y, >, <=)
  263. #define DoExpectStrLT(x, y) DoExpectStr(x, y, <, >=)
  264. #define DoExpectStrGE(x, y) DoExpectStr(x, y, >=, <)
  265. #define DoExpectStrLE(x, y) DoExpectStr(x, y, <=, >)
  266. #define DoExpectPtr(x, y, op, er) do { \
  267. PRAGMA_DIAG_PUSH \
  268. /* remarkably, without this inhibition, */ \
  269. /* the _Pragma()s make the declarations warn. */ \
  270. PRAGMA("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
  271. /* inhibit "ISO C forbids conversion of function pointer */ \
  272. /* to object pointer type [-Werror=pedantic]" */ \
  273. PRAGMA("GCC diagnostic ignored \"-Wpedantic\"") \
  274. void* _x = (void*)(x); \
  275. void* _y = (void*)(y); \
  276. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \
  277. PRAGMA_DIAG_POP \
  278. } while(0)
  279. #define DoExpectPtrEq(x, y) DoExpectPtr(x, y, ==, !=)
  280. #define DoExpectPtrNE(x, y) DoExpectPtr(x, y, !=, ==)
  281. #define DoExpectPtrGT(x, y) DoExpectPtr(x, y, >, <=)
  282. #define DoExpectPtrLT(x, y) DoExpectPtr(x, y, <, >=)
  283. #define DoExpectPtrGE(x, y) DoExpectPtr(x, y, >=, <)
  284. #define DoExpectPtrLE(x, y) DoExpectPtr(x, y, <=, >)
  285. #define DoExpectBuf(x, y, z, op, er) do { \
  286. const byte* _x = (const byte*)(x); \
  287. const byte* _y = (const byte*)(y); \
  288. int _z = (int)(z); \
  289. int _w = ((_x) && (_y)) ? XMEMCMP(_x, _y, _z) : -1; \
  290. Expect(_w op 0, ("%s " #op " %s for %s", #x, #y, #z), \
  291. ("\"%p\" " #er " \"%p\" for \"%d\"", _x, _y, _z));\
  292. } while(0)
  293. #define DoExpectBufEQ(x, y, z) DoExpectBuf(x, y, z, ==, !=)
  294. #define DoExpectBufNE(x, y, z) DoExpectBuf(x, y, z, !=, ==)
  295. void ApiTest_PrintTestCases(void);
  296. int ApiTest_RunIdx(int idx);
  297. int ApiTest_RunName(char* name);
  298. int ApiTest(void);
  299. int SuiteTest(int argc, char** argv);
  300. int HashTest(void);
  301. void SrpTest(void);
  302. int w64wrapper_test(void);
  303. int QuicTest(void);
  304. #endif /* TESTS_UNIT_H */