unit.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2021 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. printf("\n expected: "); printf description; \
  34. printf("\n result: "); printf result; printf("\n\n"); \
  35. XABORT(); \
  36. } while(0)
  37. #else
  38. #define Fail(description, result) do { \
  39. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  40. printf("\n expected: ");printf description; \
  41. printf("\n result: "); printf result; printf("\n\n"); \
  42. } while (0)
  43. #endif
  44. #define Assert(test, description, result) if (!(test)) Fail(description, result)
  45. #define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE"))
  46. #define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE"))
  47. #define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL"))
  48. #define AssertNull(x) do { \
  49. PEDANTIC_EXTENSION void* _x = (void *) (x); \
  50. \
  51. Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \
  52. } while(0)
  53. #define AssertInt(x, y, op, er) do { \
  54. int _x = (int)x; \
  55. int _y = (int)y; \
  56. \
  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 = x; \
  67. const char* _y = y; \
  68. int _z = (_x && _y) ? strcmp(_x, _y) : -1; \
  69. \
  70. Assert(_z op 0, ("%s " #op " %s", #x, #y), \
  71. ("\"%s\" " #er " \"%s\"", _x, _y));\
  72. } while(0)
  73. #define AssertStrEQ(x, y) AssertStr(x, y, ==, !=)
  74. #define AssertStrNE(x, y) AssertStr(x, y, !=, ==)
  75. #define AssertStrGT(x, y) AssertStr(x, y, >, <=)
  76. #define AssertStrLT(x, y) AssertStr(x, y, <, >=)
  77. #define AssertStrGE(x, y) AssertStr(x, y, >=, <)
  78. #define AssertStrLE(x, y) AssertStr(x, y, <=, >)
  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. #define AssertPtrEq(x, y) AssertPtr(x, y, ==, !=)
  85. #define AssertPtrNE(x, y) AssertPtr(x, y, !=, ==)
  86. #define AssertPtrGT(x, y) AssertPtr(x, y, >, <=)
  87. #define AssertPtrLT(x, y) AssertPtr(x, y, <, >=)
  88. #define AssertPtrGE(x, y) AssertPtr(x, y, >=, <)
  89. #define AssertPtrLE(x, y) AssertPtr(x, y, <=, >)
  90. void ApiTest(void);
  91. int SuiteTest(int argc, char** argv);
  92. int HashTest(void);
  93. void SrpTest(void);
  94. #endif /* CyaSSL_UNIT_H */