|
19 | 19 | */ |
20 | 20 |
|
21 | 21 |
|
| 22 | +#include <stdio.h> |
22 | 23 | #include <wolfssh/ssh.h> |
23 | | -#include <tests/unit.h> |
24 | 24 |
|
25 | 25 |
|
26 | | -#define TEST_SUCCESS (1) |
27 | | -#define TEST_FAIL (0) |
| 26 | +#define Fail(description, result) do { \ |
| 27 | + printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \ |
| 28 | + printf("\n expected: "); printf description; \ |
| 29 | + printf("\n result: "); printf result; printf("\n\n"); \ |
| 30 | + abort(); \ |
| 31 | +} while(0) |
28 | 32 |
|
29 | | -#define testingFmt " %s:" |
30 | | -#define resultFmt " %s\n" |
31 | | -static const char* passed = "passed"; |
32 | | -static const char* failed = "failed"; |
| 33 | +#define Assert(test, description, result) if (!(test)) Fail(description, result) |
33 | 34 |
|
| 35 | +#define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE")) |
| 36 | +#define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE")) |
| 37 | +#define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL")) |
34 | 38 |
|
35 | | -static int test_wolfSSH_Init(void) |
| 39 | +#define AssertNull(x) do { \ |
| 40 | + void* _x = (void *) (x); \ |
| 41 | + \ |
| 42 | + Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \ |
| 43 | +} while(0) |
| 44 | + |
| 45 | +#define AssertInt(x, y, op, er) do { \ |
| 46 | + int _x = x; \ |
| 47 | + int _y = y; \ |
| 48 | + \ |
| 49 | + Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \ |
| 50 | +} while(0) |
| 51 | + |
| 52 | +#define AssertIntEQ(x, y) AssertInt(x, y, ==, !=) |
| 53 | +#define AssertIntNE(x, y) AssertInt(x, y, !=, ==) |
| 54 | +#define AssertIntGT(x, y) AssertInt(x, y, >, <=) |
| 55 | +#define AssertIntLT(x, y) AssertInt(x, y, <, >=) |
| 56 | +#define AssertIntGE(x, y) AssertInt(x, y, >=, <) |
| 57 | +#define AssertIntLE(x, y) AssertInt(x, y, <=, >) |
| 58 | + |
| 59 | +#define AssertStr(x, y, op, er) do { \ |
| 60 | + const char* _x = x; \ |
| 61 | + const char* _y = y; \ |
| 62 | + int _z = strcmp(_x, _y); \ |
| 63 | + \ |
| 64 | + Assert(_z op 0, ("%s " #op " %s", #x, #y), \ |
| 65 | + ("\"%s\" " #er " \"%s\"", _x, _y));\ |
| 66 | +} while(0) |
| 67 | + |
| 68 | +#define AssertStrEQ(x, y) AssertStr(x, y, ==, !=) |
| 69 | +#define AssertStrNE(x, y) AssertStr(x, y, !=, ==) |
| 70 | +#define AssertStrGT(x, y) AssertStr(x, y, >, <=) |
| 71 | +#define AssertStrLT(x, y) AssertStr(x, y, <, >=) |
| 72 | +#define AssertStrGE(x, y) AssertStr(x, y, >=, <) |
| 73 | +#define AssertStrLE(x, y) AssertStr(x, y, <=, >) |
| 74 | + |
| 75 | + |
| 76 | +enum WS_TestEndpointTypes { |
| 77 | + TEST_GOOD_ENDPOINT_SERVER = WOLFSSH_ENDPOINT_SERVER, |
| 78 | + TEST_GOOD_ENDPOINT_CLIENT = WOLFSSH_ENDPOINT_CLIENT, |
| 79 | + TEST_BAD_ENDPOINT_NEXT, |
| 80 | + TEST_BAD_ENDPOINT_LAST = 255 |
| 81 | +}; |
| 82 | + |
| 83 | +static void test_wolfSSH_CTX_new(void) |
| 84 | +{ |
| 85 | + WOLFSSH_CTX* ctx; |
| 86 | + |
| 87 | + AssertNull(ctx = wolfSSH_CTX_new(TEST_BAD_ENDPOINT_NEXT, NULL)); |
| 88 | + wolfSSH_CTX_free(ctx); |
| 89 | + |
| 90 | + AssertNull(ctx = wolfSSH_CTX_new(TEST_BAD_ENDPOINT_LAST, NULL)); |
| 91 | + wolfSSH_CTX_free(ctx); |
| 92 | + |
| 93 | + AssertNotNull(ctx = wolfSSH_CTX_new(TEST_GOOD_ENDPOINT_SERVER, NULL)); |
| 94 | + wolfSSH_CTX_free(ctx); |
| 95 | + |
| 96 | + AssertNotNull(ctx = wolfSSH_CTX_new(TEST_GOOD_ENDPOINT_CLIENT, NULL)); |
| 97 | + wolfSSH_CTX_free(ctx); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +static void test_server_wolfSSH_new(void) |
36 | 102 | { |
37 | | - int result; |
| 103 | + WOLFSSH_CTX* ctx; |
| 104 | + WOLFSSH* ssh; |
38 | 105 |
|
39 | | - printf(testingFmt, "wolfSSH_Init()"); |
40 | | - result = wolfSSH_Init(); |
41 | | - printf(resultFmt, result == WS_SUCCESS ? passed : failed); |
| 106 | + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL)); |
| 107 | + AssertNotNull(ssh = wolfSSH_new(ctx)); |
42 | 108 |
|
43 | | - return result; |
| 109 | + wolfSSH_free(ssh); |
| 110 | + wolfSSH_CTX_free(ctx); |
44 | 111 | } |
45 | 112 |
|
46 | 113 |
|
47 | | -static int test_wolfSSH_Cleanup(void) |
| 114 | +static void test_client_wolfSSH_new(void) |
48 | 115 | { |
49 | | - int result; |
| 116 | + WOLFSSH_CTX* ctx; |
| 117 | + WOLFSSH* ssh; |
50 | 118 |
|
51 | | - printf(testingFmt, "wolfSSH_Cleanup()"); |
52 | | - result = wolfSSH_Cleanup(); |
53 | | - printf(resultFmt, result == WS_SUCCESS ? passed : failed); |
| 119 | + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL)); |
| 120 | + AssertNotNull(ssh = wolfSSH_new(ctx)); |
54 | 121 |
|
55 | | - return result; |
| 122 | + wolfSSH_free(ssh); |
| 123 | + wolfSSH_CTX_free(ctx); |
56 | 124 | } |
57 | 125 |
|
58 | 126 |
|
59 | | -int ApiTest(void) |
| 127 | +int main(void) |
60 | 128 | { |
61 | | - printf(" Begin API Tests\n"); |
62 | | - AssertIntEQ(test_wolfSSH_Init(), WS_SUCCESS); |
63 | | - AssertIntEQ(test_wolfSSH_Cleanup(), WS_SUCCESS); |
64 | | - printf(" End API Tests\n"); |
| 129 | + AssertIntEQ(wolfSSH_Init(), WS_SUCCESS); |
| 130 | + |
| 131 | + test_wolfSSH_CTX_new(); |
| 132 | + test_server_wolfSSH_new(); |
| 133 | + test_client_wolfSSH_new(); |
| 134 | + |
| 135 | + AssertIntEQ(wolfSSH_Cleanup(), WS_SUCCESS); |
| 136 | + |
65 | 137 | return 0; |
66 | 138 | } |
0 commit comments