unit.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #include <wolfssl/ssl.h>
  24. #include <wolfssl/test.h> /* thread and tcp stuff */
  25. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  26. #define XABORT() WC_DO_NOTHING
  27. #else
  28. #define XABORT() abort()
  29. #endif
  30. #ifndef WOLFSSL_PASSTHRU_ERR
  31. #define Fail(description, result) do { \
  32. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  33. fputs("\n expected: ", stdout); printf description; \
  34. fputs("\n result: ", stdout); printf result; fputs("\n\n", stdout); \
  35. fflush(stdout); \
  36. XABORT(); \
  37. } while(0)
  38. #else
  39. #define Fail(description, result) do { \
  40. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  41. fputs("\n expected: ", stdout); printf description; \
  42. fputs("\n result: ", stdout); printf result; fputs("\n\n", stdout); \
  43. fflush(stdout); \
  44. } while (0)
  45. #endif
  46. #define Assert(test, description, result) if (!(test)) Fail(description, result)
  47. #define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE"))
  48. #define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE"))
  49. #define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL"))
  50. #define AssertNull(x) do { \
  51. PEDANTIC_EXTENSION void* _x = (void*)(x); \
  52. Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  53. } while(0)
  54. #define AssertInt(x, y, op, er) do { \
  55. int _x = (int)(x); \
  56. int _y = (int)(y); \
  57. Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \
  58. } while(0)
  59. #define AssertIntEQ(x, y) AssertInt(x, y, ==, !=)
  60. #define AssertIntNE(x, y) AssertInt(x, y, !=, ==)
  61. #define AssertIntGT(x, y) AssertInt(x, y, >, <=)
  62. #define AssertIntLT(x, y) AssertInt(x, y, <, >=)
  63. #define AssertIntGE(x, y) AssertInt(x, y, >=, <)
  64. #define AssertIntLE(x, y) AssertInt(x, y, <=, >)
  65. #define AssertStr(x, y, op, er) do { \
  66. const char* _x = (const char*)(x); \
  67. const char* _y = (const char*)(y); \
  68. int _z = (_x && _y) ? strcmp(_x, _y) : -1; \
  69. Assert(_z op 0, ("%s " #op " %s", #x, #y), \
  70. ("\"%s\" " #er " \"%s\"", _x, _y));\
  71. } while(0)
  72. #define AssertStrEQ(x, y) AssertStr(x, y, ==, !=)
  73. #define AssertStrNE(x, y) AssertStr(x, y, !=, ==)
  74. #define AssertStrGT(x, y) AssertStr(x, y, >, <=)
  75. #define AssertStrLT(x, y) AssertStr(x, y, <, >=)
  76. #define AssertStrGE(x, y) AssertStr(x, y, >=, <)
  77. #define AssertStrLE(x, y) AssertStr(x, y, <=, >)
  78. #ifdef WOLF_C89
  79. #define AssertPtr(x, y, op, er) do { \
  80. void* _x = (void*)(x); \
  81. void* _y = (void*)(y); \
  82. Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \
  83. } while(0)
  84. #else
  85. #define AssertPtr(x, y, op, er) do { \
  86. PRAGMA_GCC_DIAG_PUSH \
  87. /* remarkably, without this inhibition, */ \
  88. /* the _Pragma()s make the declarations warn. */ \
  89. PRAGMA_GCC("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
  90. /* inhibit "ISO C forbids conversion of function pointer */ \
  91. /* to object pointer type [-Werror=pedantic]" */ \
  92. PRAGMA_GCC("GCC diagnostic ignored \"-Wpedantic\"") \
  93. void* _x = (void*)(x); \
  94. void* _y = (void*)(y); \
  95. Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \
  96. PRAGMA_GCC_DIAG_POP \
  97. } while(0)
  98. #endif
  99. #define AssertPtrEq(x, y) AssertPtr(x, y, ==, !=)
  100. #define AssertPtrNE(x, y) AssertPtr(x, y, !=, ==)
  101. #define AssertPtrGT(x, y) AssertPtr(x, y, >, <=)
  102. #define AssertPtrLT(x, y) AssertPtr(x, y, <, >=)
  103. #define AssertPtrGE(x, y) AssertPtr(x, y, >=, <)
  104. #define AssertPtrLE(x, y) AssertPtr(x, y, <=, >)
  105. #define TEST_FAIL 0
  106. #define TEST_SUCCESS 1
  107. #define TEST_SUCCESS_NO_MSGS 2
  108. #define TEST_SKIPPED 3 /* Test skipped - not run. */
  109. #define TEST_SKIPPED_NO_MSGS 4 /* Test skipped - not run. */
  110. #define EXPECT_DECLS \
  111. int _ret = TEST_SKIPPED, _fail_codepoint_id = TEST_FAIL
  112. #define EXPECT_DECLS_NO_MSGS(fail_codepoint_offset) \
  113. int _ret = TEST_SKIPPED_NO_MSGS, \
  114. _fail_codepoint_id = (fail_codepoint_offset)
  115. #define EXPECT_FAILURE_CODEPOINT_ID _fail_codepoint_id
  116. #define EXPECT_RESULT() \
  117. ((void)_fail_codepoint_id, \
  118. _ret == TEST_SUCCESS_NO_MSGS ? TEST_SUCCESS : \
  119. _ret == TEST_SKIPPED_NO_MSGS ? TEST_SKIPPED : \
  120. _ret)
  121. #define EXPECT_SUCCESS() \
  122. ((_ret == TEST_SUCCESS) || \
  123. (_ret == TEST_SKIPPED) || \
  124. (_ret == TEST_SUCCESS_NO_MSGS) || \
  125. (_ret == TEST_SKIPPED_NO_MSGS))
  126. #define EXPECT_FAIL() \
  127. (! EXPECT_SUCCESS())
  128. #define ExpFail(description, result) do { \
  129. if ((_ret == TEST_SUCCESS_NO_MSGS) || (_ret == TEST_SKIPPED_NO_MSGS)) \
  130. _ret = _fail_codepoint_id; \
  131. else { \
  132. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  133. fputs("\n expected: ", stdout); printf description; \
  134. fputs("\n result: ", stdout); printf result; \
  135. fputs("\n\n", stdout); \
  136. fflush(stdout); \
  137. _ret = TEST_FAIL; \
  138. } \
  139. } while (0)
  140. #define Expect(test, description, result) do { \
  141. if (EXPECT_SUCCESS()) { \
  142. if (!(test)) \
  143. ExpFail(description, result); \
  144. else if (_ret == TEST_SKIPPED_NO_MSGS) \
  145. _ret = TEST_SUCCESS_NO_MSGS; \
  146. else \
  147. _ret = TEST_SUCCESS; \
  148. } \
  149. if (_ret == TEST_SUCCESS_NO_MSGS) \
  150. --_fail_codepoint_id; \
  151. } while (0)
  152. #define ExpectTrue(x) Expect( (x), ("%s is true", #x), (#x " => FALSE"))
  153. #define ExpectFalse(x) Expect(!(x), ("%s is false", #x), (#x " => TRUE"))
  154. #define ExpectNotNull(x) Expect( (x), ("%s is not null", #x), (#x " => NULL"))
  155. #define ExpectNull(x) do { \
  156. if (EXPECT_SUCCESS()) { \
  157. PEDANTIC_EXTENSION void* _x = (void*)(x); \
  158. Expect(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  159. } \
  160. } while(0)
  161. #define ExpectInt(x, y, op, er) do { \
  162. if (EXPECT_SUCCESS()) { \
  163. int _x = (int)(x); \
  164. int _y = (int)(y); \
  165. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y));\
  166. } \
  167. } while(0)
  168. #define ExpectIntEQ(x, y) ExpectInt(x, y, ==, !=)
  169. #define ExpectIntNE(x, y) ExpectInt(x, y, !=, ==)
  170. #define ExpectIntGT(x, y) ExpectInt(x, y, >, <=)
  171. #define ExpectIntLT(x, y) ExpectInt(x, y, <, >=)
  172. #define ExpectIntGE(x, y) ExpectInt(x, y, >=, <)
  173. #define ExpectIntLE(x, y) ExpectInt(x, y, <=, >)
  174. #define ExpectStr(x, y, op, er) do { \
  175. if (EXPECT_SUCCESS()) { \
  176. const char* _x = (const char*)(x); \
  177. const char* _y = (const char*)(y); \
  178. int _z = (_x && _y) ? XSTRCMP(_x, _y) : -1; \
  179. Expect(_z op 0, ("%s " #op " %s", #x, #y), \
  180. ("\"%s\" " #er " \"%s\"", _x, _y));\
  181. } \
  182. } while(0)
  183. #define ExpectStrEQ(x, y) ExpectStr(x, y, ==, !=)
  184. #define ExpectStrNE(x, y) ExpectStr(x, y, !=, ==)
  185. #define ExpectStrGT(x, y) ExpectStr(x, y, >, <=)
  186. #define ExpectStrLT(x, y) ExpectStr(x, y, <, >=)
  187. #define ExpectStrGE(x, y) ExpectStr(x, y, >=, <)
  188. #define ExpectStrLE(x, y) ExpectStr(x, y, <=, >)
  189. #define ExpectPtr(x, y, op, er) do { \
  190. if (EXPECT_SUCCESS()) { \
  191. PRAGMA_DIAG_PUSH \
  192. /* remarkably, without this inhibition, */ \
  193. /* the _Pragma()s make the declarations warn. */ \
  194. PRAGMA("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
  195. /* inhibit "ISO C forbids conversion of function pointer */ \
  196. /* to object pointer type [-Werror=pedantic]" */ \
  197. PRAGMA("GCC diagnostic ignored \"-Wpedantic\"") \
  198. void* _x = (void*)(x); \
  199. void* _y = (void*)(y); \
  200. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y));\
  201. PRAGMA_DIAG_POP \
  202. } \
  203. } while(0)
  204. #define ExpectPtrEq(x, y) ExpectPtr(x, y, ==, !=)
  205. #define ExpectPtrNE(x, y) ExpectPtr(x, y, !=, ==)
  206. #define ExpectPtrGT(x, y) ExpectPtr(x, y, >, <=)
  207. #define ExpectPtrLT(x, y) ExpectPtr(x, y, <, >=)
  208. #define ExpectPtrGE(x, y) ExpectPtr(x, y, >=, <)
  209. #define ExpectPtrLE(x, y) ExpectPtr(x, y, <=, >)
  210. #define ExpectBuf(x, y, z, op, er) do { \
  211. if (EXPECT_SUCCESS()) { \
  212. const byte* _x = (const byte*)(x); \
  213. const byte* _y = (const byte*)(y); \
  214. int _z = (int)(z); \
  215. int _w = ((_x) && (_y)) ? XMEMCMP(_x, _y, (unsigned long)_z) : -1; \
  216. Expect(_w op 0, ("%s " #op " %s for %s", #x, #y, #z), \
  217. ("\"%p\" " #er " \"%p\" for \"%d\"", \
  218. (const void *)_x, (const void *)_y, _z)); \
  219. } \
  220. } while(0)
  221. #define ExpectBufEQ(x, y, z) ExpectBuf(x, y, z, ==, !=)
  222. #define ExpectBufNE(x, y, z) ExpectBuf(x, y, z, !=, ==)
  223. #define ExpectFail() ExpectTrue(0)
  224. #define DoExpectNull(x) do { \
  225. PEDANTIC_EXTENSION void* _x = (void*)(x); \
  226. Expect(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  227. } while(0)
  228. #define DoExpectInt(x, y, op, er) do { \
  229. int _x = (int)(x); \
  230. int _y = (int)(y); \
  231. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \
  232. } while(0)
  233. #define DoExpectIntEQ(x, y) DoExpectInt(x, y, ==, !=)
  234. #define DoExpectIntNE(x, y) DoExpectInt(x, y, !=, ==)
  235. #define DoExpectIntGT(x, y) DoExpectInt(x, y, >, <=)
  236. #define DoExpectIntLT(x, y) DoExpectInt(x, y, <, >=)
  237. #define DoExpectIntGE(x, y) DoExpectInt(x, y, >=, <)
  238. #define DoExpectIntLE(x, y) DoExpectInt(x, y, <=, >)
  239. #define DoExpectStr(x, y, op, er) do { \
  240. const char* _x = (const char*)(x); \
  241. const char* _y = (const char*)(y); \
  242. int _z = (_x && _y) ? strcmp(_x, _y) : -1; \
  243. Expect(_z op 0, ("%s " #op " %s", #x, #y), \
  244. ("\"%s\" " #er " \"%s\"", _x, _y));\
  245. } while(0)
  246. #define DoExpectStrEQ(x, y) DoExpectStr(x, y, ==, !=)
  247. #define DoExpectStrNE(x, y) DoExpectStr(x, y, !=, ==)
  248. #define DoExpectStrGT(x, y) DoExpectStr(x, y, >, <=)
  249. #define DoExpectStrLT(x, y) DoExpectStr(x, y, <, >=)
  250. #define DoExpectStrGE(x, y) DoExpectStr(x, y, >=, <)
  251. #define DoExpectStrLE(x, y) DoExpectStr(x, y, <=, >)
  252. #define DoExpectPtr(x, y, op, er) do { \
  253. PRAGMA_DIAG_PUSH \
  254. /* remarkably, without this inhibition, */ \
  255. /* the _Pragma()s make the declarations warn. */ \
  256. PRAGMA("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
  257. /* inhibit "ISO C forbids conversion of function pointer */ \
  258. /* to object pointer type [-Werror=pedantic]" */ \
  259. PRAGMA("GCC diagnostic ignored \"-Wpedantic\"") \
  260. void* _x = (void*)(x); \
  261. void* _y = (void*)(y); \
  262. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \
  263. PRAGMA_DIAG_POP \
  264. } while(0)
  265. #define DoExpectPtrEq(x, y) DoExpectPtr(x, y, ==, !=)
  266. #define DoExpectPtrNE(x, y) DoExpectPtr(x, y, !=, ==)
  267. #define DoExpectPtrGT(x, y) DoExpectPtr(x, y, >, <=)
  268. #define DoExpectPtrLT(x, y) DoExpectPtr(x, y, <, >=)
  269. #define DoExpectPtrGE(x, y) DoExpectPtr(x, y, >=, <)
  270. #define DoExpectPtrLE(x, y) DoExpectPtr(x, y, <=, >)
  271. #define DoExpectBuf(x, y, z, op, er) do { \
  272. const byte* _x = (const byte*)(x); \
  273. const byte* _y = (const byte*)(y); \
  274. int _z = (int)(z); \
  275. int _w = ((_x) && (_y)) ? XMEMCMP(_x, _y, _z) : -1; \
  276. Expect(_w op 0, ("%s " #op " %s for %s", #x, #y, #z), \
  277. ("\"%p\" " #er " \"%p\" for \"%d\"", _x, _y, _z));\
  278. } while(0)
  279. #define DoExpectBufEQ(x, y, z) DoExpectBuf(x, y, z, ==, !=)
  280. #define DoExpectBufNE(x, y, z) DoExpectBuf(x, y, z, !=, ==)
  281. void ApiTest_PrintTestCases(void);
  282. int ApiTest_RunIdx(int idx);
  283. int ApiTest_RunName(char* name);
  284. int ApiTest(void);
  285. int SuiteTest(int argc, char** argv);
  286. int HashTest(void);
  287. void SrpTest(void);
  288. int w64wrapper_test(void);
  289. int QuicTest(void);
  290. #endif /* TESTS_UNIT_H */