unit.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2022 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()
  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. #define AssertPtr(x, y, op, er) do { \
  79. PRAGMA_GCC_DIAG_PUSH; \
  80. /* remarkably, without this inhibition, */ \
  81. /* the _Pragma()s make the declarations warn. */ \
  82. PRAGMA_GCC("GCC diagnostic ignored \"-Wdeclaration-after-statement\""); \
  83. /* inhibit "ISO C forbids conversion of function pointer */ \
  84. /* to object pointer type [-Werror=pedantic]" */ \
  85. PRAGMA_GCC("GCC diagnostic ignored \"-Wpedantic\""); \
  86. void* _x = (void*)(x); \
  87. void* _y = (void*)(y); \
  88. Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%p " #er " %p", _x, _y)); \
  89. PRAGMA_GCC_DIAG_POP; \
  90. } while(0)
  91. #define AssertPtrEq(x, y) AssertPtr(x, y, ==, !=)
  92. #define AssertPtrNE(x, y) AssertPtr(x, y, !=, ==)
  93. #define AssertPtrGT(x, y) AssertPtr(x, y, >, <=)
  94. #define AssertPtrLT(x, y) AssertPtr(x, y, <, >=)
  95. #define AssertPtrGE(x, y) AssertPtr(x, y, >=, <)
  96. #define AssertPtrLE(x, y) AssertPtr(x, y, <=, >)
  97. void ApiTest(void);
  98. int SuiteTest(int argc, char** argv);
  99. int HashTest(void);
  100. void SrpTest(void);
  101. int w64wrapper_test(void);
  102. int QuicTest(void);
  103. #endif /* CyaSSL_UNIT_H */