unit.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. \
  53. Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  54. } while(0)
  55. #define AssertInt(x, y, op, er) do { \
  56. int _x = (int)(x); \
  57. int _y = (int)(y); \
  58. \
  59. Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \
  60. } while(0)
  61. #define AssertIntEQ(x, y) AssertInt(x, y, ==, !=)
  62. #define AssertIntNE(x, y) AssertInt(x, y, !=, ==)
  63. #define AssertIntGT(x, y) AssertInt(x, y, >, <=)
  64. #define AssertIntLT(x, y) AssertInt(x, y, <, >=)
  65. #define AssertIntGE(x, y) AssertInt(x, y, >=, <)
  66. #define AssertIntLE(x, y) AssertInt(x, y, <=, >)
  67. #define AssertStr(x, y, op, er) do { \
  68. const char* _x = x; \
  69. const char* _y = y; \
  70. int _z = (_x && _y) ? strcmp(_x, _y) : -1; \
  71. \
  72. Assert(_z op 0, ("%s " #op " %s", #x, #y), \
  73. ("\"%s\" " #er " \"%s\"", _x, _y));\
  74. } while(0)
  75. #define AssertStrEQ(x, y) AssertStr(x, y, ==, !=)
  76. #define AssertStrNE(x, y) AssertStr(x, y, !=, ==)
  77. #define AssertStrGT(x, y) AssertStr(x, y, >, <=)
  78. #define AssertStrLT(x, y) AssertStr(x, y, <, >=)
  79. #define AssertStrGE(x, y) AssertStr(x, y, >=, <)
  80. #define AssertStrLE(x, y) AssertStr(x, y, <=, >)
  81. #define AssertPtr(x, y, op, er) do { \
  82. PRAGMA_GCC_DIAG_PUSH; \
  83. /* remarkably, without this inhibition, */ \
  84. /* the _Pragma()s make the declarations warn. */ \
  85. PRAGMA_GCC("GCC diagnostic ignored \"-Wdeclaration-after-statement\""); \
  86. /* inhibit "ISO C forbids conversion of function pointer */ \
  87. /* to object pointer type [-Werror=pedantic]" */ \
  88. PRAGMA_GCC("GCC diagnostic ignored \"-Wpedantic\""); \
  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. PRAGMA_GCC_DIAG_POP; \
  93. } while(0)
  94. #define AssertPtrEq(x, y) AssertPtr(x, y, ==, !=)
  95. #define AssertPtrNE(x, y) AssertPtr(x, y, !=, ==)
  96. #define AssertPtrGT(x, y) AssertPtr(x, y, >, <=)
  97. #define AssertPtrLT(x, y) AssertPtr(x, y, <, >=)
  98. #define AssertPtrGE(x, y) AssertPtr(x, y, >=, <)
  99. #define AssertPtrLE(x, y) AssertPtr(x, y, <=, >)
  100. void ApiTest(void);
  101. int SuiteTest(int argc, char** argv);
  102. int HashTest(void);
  103. void SrpTest(void);
  104. int w64wrapper_test(void);
  105. int QuicTest(void);
  106. #endif /* CyaSSL_UNIT_H */