Skip to content

Commit 24b19da

Browse files
committed
1. Copied over some of the test infrastructure from wolfSSL.
2. Added API test cases for only wolfSSH_Init and wolfSSH_Cleanup.
1 parent c3d0c89 commit 24b19da

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

tests/api.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,48 @@
1919
*/
2020

2121

22+
#include <wolfssh/ssh.h>
2223
#include <tests/unit.h>
2324

2425

26+
#define TEST_SUCCESS (1)
27+
#define TEST_FAIL (0)
28+
29+
#define testingFmt " %s:"
30+
#define resultFmt " %s\n"
31+
static const char* passed = "passed";
32+
static const char* failed = "failed";
33+
34+
35+
static int test_wolfSSH_Init(void)
36+
{
37+
int result;
38+
39+
printf(testingFmt, "wolfSSH_Init()");
40+
result = wolfSSH_Init();
41+
printf(resultFmt, result == WS_SUCCESS ? passed : failed);
42+
43+
return result;
44+
}
45+
46+
47+
static int test_wolfSSH_Cleanup(void)
48+
{
49+
int result;
50+
51+
printf(testingFmt, "wolfSSH_Cleanup()");
52+
result = wolfSSH_Cleanup();
53+
printf(resultFmt, result == WS_SUCCESS ? passed : failed);
54+
55+
return result;
56+
}
57+
58+
2559
int ApiTest(void)
2660
{
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");
2765
return 0;
2866
}

tests/unit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ int main(void)
394394
testResult = testResult || unitResult;
395395
#endif
396396

397+
printf("Running the API tests\n");
397398
unitResult = ApiTest();
398-
printf("API: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
399399
testResult = testResult || unitResult;
400400

401401
return (testResult ? 1 : 0);

tests/unit.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,74 @@
2626

2727
#pragma once
2828

29+
#ifndef WOLFSSH_TESTS_UNIT_H
30+
#define WOLFSSH_TESTS_UNIT_H
31+
32+
33+
#include <stdio.h>
34+
35+
2936
#ifdef __cplusplus
3037
extern "C" {
3138
#endif
3239

40+
41+
#define Fail(description, result) do { \
42+
printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
43+
printf("\n expected: "); printf description; \
44+
printf("\n result: "); printf result; printf("\n\n"); \
45+
abort(); \
46+
} while(0)
47+
48+
#define Assert(test, description, result) if (!(test)) Fail(description, result)
49+
50+
#define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE"))
51+
#define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE"))
52+
#define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL"))
53+
54+
#define AssertNull(x) do { \
55+
void* _x = (void *) (x); \
56+
\
57+
Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \
58+
} while(0)
59+
60+
#define AssertInt(x, y, op, er) do { \
61+
int _x = x; \
62+
int _y = y; \
63+
\
64+
Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \
65+
} while(0)
66+
67+
#define AssertIntEQ(x, y) AssertInt(x, y, ==, !=)
68+
#define AssertIntNE(x, y) AssertInt(x, y, !=, ==)
69+
#define AssertIntGT(x, y) AssertInt(x, y, >, <=)
70+
#define AssertIntLT(x, y) AssertInt(x, y, <, >=)
71+
#define AssertIntGE(x, y) AssertInt(x, y, >=, <)
72+
#define AssertIntLE(x, y) AssertInt(x, y, <=, >)
73+
74+
#define AssertStr(x, y, op, er) do { \
75+
const char* _x = x; \
76+
const char* _y = y; \
77+
int _z = strcmp(_x, _y); \
78+
\
79+
Assert(_z op 0, ("%s " #op " %s", #x, #y), \
80+
("\"%s\" " #er " \"%s\"", _x, _y));\
81+
} while(0)
82+
83+
#define AssertStrEQ(x, y) AssertStr(x, y, ==, !=)
84+
#define AssertStrNE(x, y) AssertStr(x, y, !=, ==)
85+
#define AssertStrGT(x, y) AssertStr(x, y, >, <=)
86+
#define AssertStrLT(x, y) AssertStr(x, y, <, >=)
87+
#define AssertStrGE(x, y) AssertStr(x, y, >=, <)
88+
#define AssertStrLE(x, y) AssertStr(x, y, <=, >)
89+
90+
3391
int ApiTest(void);
3492

93+
3594
#ifdef __cplusplus
3695
}
3796
#endif
97+
98+
99+
#endif

0 commit comments

Comments
 (0)