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 22 23 #ifndef CyaSSL_UNIT_H 24 #define CyaSSL_UNIT_H 25 26 #include <wolfssl/ssl.h> 27 #include <wolfssl/test.h> /* thread and tcp stuff */ 28 29 #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST 30 #define XABORT() 31 #else 32 #define XABORT() abort() 33 #endif 34 35 #ifndef WOLFSSL_PASSTHRU_ERR 36 #define Fail(description, result) do { \ 37 printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \ 38 printf("\n expected: "); printf description; \ 39 printf("\n result: "); printf result; printf("\n\n"); \ 40 XABORT(); \ 41 } while(0) 42 #else 43 #define Fail(description, result) do { \ 44 printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \ 45 printf("\n expected: ");printf description; \ 46 printf("\n result: "); printf result; printf("\n\n"); \ 47 } while (0) 48 #endif 49 50 #define Assert(test, description, result) if (!(test)) Fail(description, result) 51 52 #define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE")) 53 #define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE")) 54 #define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL")) 55 56 #define AssertNull(x) do { \ 57 PEDANTIC_EXTENSION void* _x = (void *) (x); \ 58 \ 59 Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \ 60 } while(0) 61 62 #define AssertInt(x, y, op, er) do { \ 63 int _x = (int)x; \ 64 int _y = (int)y; \ 65 \ 66 Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \ 67 } while(0) 68 69 #define AssertIntEQ(x, y) AssertInt(x, y, ==, !=) 70 #define AssertIntNE(x, y) AssertInt(x, y, !=, ==) 71 #define AssertIntGT(x, y) AssertInt(x, y, >, <=) 72 #define AssertIntLT(x, y) AssertInt(x, y, <, >=) 73 #define AssertIntGE(x, y) AssertInt(x, y, >=, <) 74 #define AssertIntLE(x, y) AssertInt(x, y, <=, >) 75 76 #define AssertStr(x, y, op, er) do { \ 77 const char* _x = x; \ 78 const char* _y = y; \ 79 int _z = (_x && _y) ? strcmp(_x, _y) : -1; \ 80 \ 81 Assert(_z op 0, ("%s " #op " %s", #x, #y), \ 82 ("\"%s\" " #er " \"%s\"", _x, _y));\ 83 } while(0) 84 85 #define AssertStrEQ(x, y) AssertStr(x, y, ==, !=) 86 #define AssertStrNE(x, y) AssertStr(x, y, !=, ==) 87 #define AssertStrGT(x, y) AssertStr(x, y, >, <=) 88 #define AssertStrLT(x, y) AssertStr(x, y, <, >=) 89 #define AssertStrGE(x, y) AssertStr(x, y, >=, <) 90 #define AssertStrLE(x, y) AssertStr(x, y, <=, >) 91 92 #define AssertPtr(x, y, op, er) do { \ 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 } while(0) 97 98 #define AssertPtrEq(x, y) AssertPtr(x, y, ==, !=) 99 #define AssertPtrNE(x, y) AssertPtr(x, y, !=, ==) 100 #define AssertPtrGT(x, y) AssertPtr(x, y, >, <=) 101 #define AssertPtrLT(x, y) AssertPtr(x, y, <, >=) 102 #define AssertPtrGE(x, y) AssertPtr(x, y, >=, <) 103 #define AssertPtrLE(x, y) AssertPtr(x, y, <=, >) 104 105 106 void ApiTest(void); 107 int SuiteTest(int argc, char** argv); 108 int HashTest(void); 109 void SrpTest(void); 110 111 112 #endif /* CyaSSL_UNIT_H */ 113