Skip to content

Commit 9c8a77c

Browse files
committed
1. Add a separate API test.
2. Eliminate the unit.h file, included into api.c.
1 parent 24b19da commit 9c8a77c

4 files changed

Lines changed: 105 additions & 133 deletions

File tree

tests/api.c

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,120 @@
1919
*/
2020

2121

22+
#include <stdio.h>
2223
#include <wolfssh/ssh.h>
23-
#include <tests/unit.h>
2424

2525

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)
2832

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)
3334

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"))
3438

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)
36102
{
37-
int result;
103+
WOLFSSH_CTX* ctx;
104+
WOLFSSH* ssh;
38105

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));
42108

43-
return result;
109+
wolfSSH_free(ssh);
110+
wolfSSH_CTX_free(ctx);
44111
}
45112

46113

47-
static int test_wolfSSH_Cleanup(void)
114+
static void test_client_wolfSSH_new(void)
48115
{
49-
int result;
116+
WOLFSSH_CTX* ctx;
117+
WOLFSSH* ssh;
50118

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));
54121

55-
return result;
122+
wolfSSH_free(ssh);
123+
wolfSSH_CTX_free(ctx);
56124
}
57125

58126

59-
int ApiTest(void)
127+
int main(void)
60128
{
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+
65137
return 0;
66138
}

tests/include.am

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
# included from Top Level Makefile.am
33
# All paths should be given relative to the root
44

5-
check_PROGRAMS += tests/unit.test
6-
noinst_PROGRAMS += tests/unit.test
7-
8-
tests_unit_test_SOURCES = tests/unit.c tests/api.c
5+
check_PROGRAMS += tests/unit.test tests/api.test
6+
noinst_PROGRAMS += tests/unit.test tests/api.test
97

8+
tests_unit_test_SOURCES = tests/unit.c
109
tests_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS)
1110
tests_unit_test_LDADD = src/libwolfssh.la $(LIB_STATIC_ADD)
1211
tests_unit_test_DEPENDENCIES = src/libwolfssh.la
1312

14-
DISTCLEANFILES+= tests/.libs/unit.test
13+
tests_api_test_SOURCES = tests/api.c
14+
tests_api_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS)
15+
tests_api_test_LDADD = src/libwolfssh.la $(LIB_STATIC_ADD)
16+
tests_api_test_DEPENDENCIES = src/libwolfssh.la
17+
18+
DISTCLEANFILES+= tests/.libs/unit.test tests/.libs/api.test

tests/unit.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <wolfssh/ssh.h>
2424
#include <wolfssh/keygen.h>
2525
#include <wolfssh/internal.h>
26-
#include <tests/unit.h>
2726

2827

2928
/* Utility functions */
@@ -394,10 +393,6 @@ int main(void)
394393
testResult = testResult || unitResult;
395394
#endif
396395

397-
printf("Running the API tests\n");
398-
unitResult = ApiTest();
399-
testResult = testResult || unitResult;
400-
401396
return (testResult ? 1 : 0);
402397
}
403398

tests/unit.h

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)