unit.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2023 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 CyaSSL_UNIT_H
  22. #define CyaSSL_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 EXPECT_DECLS \
  106. int _ret = TEST_SKIPPED
  107. #define EXPECT_RESULT() \
  108. _ret
  109. #define EXPECT_SUCCESS() \
  110. (_ret == TEST_SUCCESS)
  111. #define EXPECT_FAIL() \
  112. (_ret == TEST_FAIL)
  113. #define ExpFail(description, result) do { \
  114. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  115. fputs("\n expected: ", stdout); printf description; \
  116. fputs("\n result: ", stdout); printf result; fputs("\n\n", stdout); \
  117. fflush(stdout); \
  118. _ret = TEST_FAIL; \
  119. } while (0)
  120. #define Expect(test, description, result) do { \
  121. if (_ret != TEST_FAIL) { if (!(test)) ExpFail(description, result); \
  122. else _ret = TEST_SUCCESS; } \
  123. } while (0)
  124. #define ExpectTrue(x) Expect( (x), ("%s is true", #x), (#x " => FALSE"))
  125. #define ExpectFalse(x) Expect(!(x), ("%s is false", #x), (#x " => TRUE"))
  126. #define ExpectNotNull(x) Expect( (x), ("%s is not null", #x), (#x " => NULL"))
  127. #define ExpectNull(x) do { \
  128. if (_ret != TEST_FAIL) { \
  129. PEDANTIC_EXTENSION void* _x = (void*)(x); \
  130. Expect(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  131. } \
  132. } while(0)
  133. #define ExpectInt(x, y, op, er) do { \
  134. if (_ret != TEST_FAIL) { \
  135. int _x = (int)(x); \
  136. int _y = (int)(y); \
  137. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y));\
  138. } \
  139. } while(0)
  140. #define ExpectIntEQ(x, y) ExpectInt(x, y, ==, !=)
  141. #define ExpectIntNE(x, y) ExpectInt(x, y, !=, ==)
  142. #define ExpectIntGT(x, y) ExpectInt(x, y, >, <=)
  143. #define ExpectIntLT(x, y) ExpectInt(x, y, <, >=)
  144. #define ExpectIntGE(x, y) ExpectInt(x, y, >=, <)
  145. #define ExpectIntLE(x, y) ExpectInt(x, y, <=, >)
  146. #define ExpectStr(x, y, op, er) do { \
  147. if (_ret != TEST_FAIL) { \
  148. const char* _x = (const char*)(x); \
  149. const char* _y = (const char*)(y); \
  150. int _z = (_x && _y) ? XSTRCMP(_x, _y) : -1; \
  151. Expect(_z op 0, ("%s " #op " %s", #x, #y), \
  152. ("\"%s\" " #er " \"%s\"", _x, _y));\
  153. } \
  154. } while(0)
  155. #define ExpectStrEQ(x, y) ExpectStr(x, y, ==, !=)
  156. #define ExpectStrNE(x, y) ExpectStr(x, y, !=, ==)
  157. #define ExpectStrGT(x, y) ExpectStr(x, y, >, <=)
  158. #define ExpectStrLT(x, y) ExpectStr(x, y, <, >=)
  159. #define ExpectStrGE(x, y) ExpectStr(x, y, >=, <)
  160. #define ExpectStrLE(x, y) ExpectStr(x, y, <=, >)
  161. #define ExpectPtr(x, y, op, er) do { \
  162. if (_ret != TEST_FAIL) { \
  163. PRAGMA_DIAG_PUSH \
  164. /* remarkably, without this inhibition, */ \
  165. /* the _Pragma()s make the declarations warn. */ \
  166. PRAGMA("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
  167. /* inhibit "ISO C forbids conversion of function pointer */ \
  168. /* to object pointer type [-Werror=pedantic]" */ \
  169. PRAGMA("GCC diagnostic ignored \"-Wpedantic\"") \
  170. void* _x = (void*)(x); \
  171. void* _y = (void*)(y); \
  172. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y));\
  173. PRAGMA_DIAG_POP \
  174. } \
  175. } while(0)
  176. #define ExpectPtrEq(x, y) ExpectPtr(x, y, ==, !=)
  177. #define ExpectPtrNE(x, y) ExpectPtr(x, y, !=, ==)
  178. #define ExpectPtrGT(x, y) ExpectPtr(x, y, >, <=)
  179. #define ExpectPtrLT(x, y) ExpectPtr(x, y, <, >=)
  180. #define ExpectPtrGE(x, y) ExpectPtr(x, y, >=, <)
  181. #define ExpectPtrLE(x, y) ExpectPtr(x, y, <=, >)
  182. #define ExpectBuf(x, y, z, op, er) do { \
  183. if (_ret != TEST_FAIL) { \
  184. const byte* _x = (const byte*)(x); \
  185. const byte* _y = (const byte*)(y); \
  186. int _z = (int)(z); \
  187. int _w = ((_x) && (_y)) ? XMEMCMP(_x, _y, _z) : -1; \
  188. Expect(_w op 0, ("%s " #op " %s for %s", #x, #y, #z), \
  189. ("\"%p\" " #er " \"%p\" for \"%d\"", \
  190. (const void *)_x, (const void *)_y, _z)); \
  191. } \
  192. } while(0)
  193. #define ExpectBufEQ(x, y, z) ExpectBuf(x, y, z, ==, !=)
  194. #define ExpectBufNE(x, y, z) ExpectBuf(x, y, z, !=, ==)
  195. #define ExpectFail() ExpectTrue(0)
  196. #define DoExpectNull(x) do { \
  197. PEDANTIC_EXTENSION void* _x = (void*)(x); \
  198. Expect(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  199. } while(0)
  200. #define DoExpectInt(x, y, op, er) do { \
  201. int _x = (int)(x); \
  202. int _y = (int)(y); \
  203. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \
  204. } while(0)
  205. #define DoExpectIntEQ(x, y) DoExpectInt(x, y, ==, !=)
  206. #define DoExpectIntNE(x, y) DoExpectInt(x, y, !=, ==)
  207. #define DoExpectIntGT(x, y) DoExpectInt(x, y, >, <=)
  208. #define DoExpectIntLT(x, y) DoExpectInt(x, y, <, >=)
  209. #define DoExpectIntGE(x, y) DoExpectInt(x, y, >=, <)
  210. #define DoExpectIntLE(x, y) DoExpectInt(x, y, <=, >)
  211. #define DoExpectStr(x, y, op, er) do { \
  212. const char* _x = (const char*)(x); \
  213. const char* _y = (const char*)(y); \
  214. int _z = (_x && _y) ? strcmp(_x, _y) : -1; \
  215. Expect(_z op 0, ("%s " #op " %s", #x, #y), \
  216. ("\"%s\" " #er " \"%s\"", _x, _y));\
  217. } while(0)
  218. #define DoExpectStrEQ(x, y) DoExpectStr(x, y, ==, !=)
  219. #define DoExpectStrNE(x, y) DoExpectStr(x, y, !=, ==)
  220. #define DoExpectStrGT(x, y) DoExpectStr(x, y, >, <=)
  221. #define DoExpectStrLT(x, y) DoExpectStr(x, y, <, >=)
  222. #define DoExpectStrGE(x, y) DoExpectStr(x, y, >=, <)
  223. #define DoExpectStrLE(x, y) DoExpectStr(x, y, <=, >)
  224. #define DoExpectPtr(x, y, op, er) do { \
  225. PRAGMA_DIAG_PUSH \
  226. /* remarkably, without this inhibition, */ \
  227. /* the _Pragma()s make the declarations warn. */ \
  228. PRAGMA("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
  229. /* inhibit "ISO C forbids conversion of function pointer */ \
  230. /* to object pointer type [-Werror=pedantic]" */ \
  231. PRAGMA("GCC diagnostic ignored \"-Wpedantic\"") \
  232. void* _x = (void*)(x); \
  233. void* _y = (void*)(y); \
  234. Expect(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \
  235. PRAGMA_DIAG_POP \
  236. } while(0)
  237. #define DoExpectPtrEq(x, y) DoExpectPtr(x, y, ==, !=)
  238. #define DoExpectPtrNE(x, y) DoExpectPtr(x, y, !=, ==)
  239. #define DoExpectPtrGT(x, y) DoExpectPtr(x, y, >, <=)
  240. #define DoExpectPtrLT(x, y) DoExpectPtr(x, y, <, >=)
  241. #define DoExpectPtrGE(x, y) DoExpectPtr(x, y, >=, <)
  242. #define DoExpectPtrLE(x, y) DoExpectPtr(x, y, <=, >)
  243. #define DoExpectBuf(x, y, z, op, er) do { \
  244. const byte* _x = (const byte*)(x); \
  245. const byte* _y = (const byte*)(y); \
  246. int _z = (int)(z); \
  247. int _w = ((_x) && (_y)) ? XMEMCMP(_x, _y, _z) : -1; \
  248. Expect(_w op 0, ("%s " #op " %s for %s", #x, #y, #z), \
  249. ("\"%p\" " #er " \"%p\" for \"%d\"", _x, _y, _z));\
  250. } while(0)
  251. #define DoExpectBufEQ(x, y, z) DoExpectBuf(x, y, z, ==, !=)
  252. #define DoExpectBufNE(x, y, z) DoExpectBuf(x, y, z, !=, ==)
  253. void ApiTest_PrintTestCases(void);
  254. int ApiTest_RunIdx(int idx);
  255. int ApiTest_RunName(char* name);
  256. int ApiTest(void);
  257. int SuiteTest(int argc, char** argv);
  258. int HashTest(void);
  259. void SrpTest(void);
  260. int w64wrapper_test(void);
  261. int QuicTest(void);
  262. #endif /* CyaSSL_UNIT_H */