1 /*
2 * Nsock regression test suite
3 * Same license as nmap -- see https://nmap.org/book/man-legal.html
4 */
5
6
7 #ifndef __TEST_COMMON_H
8 #define __TEST_COMMON_H
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <assert.h>
16 #include <nsock.h>
17
18
19 #define PORT_UDP 55234
20 #define PORT_TCP 55235
21 #define PORT_TCPSSL 55236
22
23
24 #define __ASSERT_BASE(stmt) do { \
25 if (!(stmt)) { \
26 fprintf(stderr, "(%s:%d) Assertion failed: " #stmt "\n", \
27 __FILE__, __LINE__); \
28 return -EINVAL; \
29 } \
30 } while (0)
31
32
33 #define AssertNonNull(a) __ASSERT_BASE((a) != NULL);
34 #define AssertEqual(a, b) __ASSERT_BASE((a) == (b));
35 #define AssertStrEqual(a, b) __ASSERT_BASE(strcmp((a), (b)) == 0);
36
37
38 struct test_case {
39 const char *t_name;
40 int (*t_setup)(void **tdata);
41 int (*t_run)(void *tdata);
42 int (*t_teardown)(void *tdata);
43 };
44
45
get_test_name(const struct test_case * test)46 static inline const char *get_test_name(const struct test_case *test) {
47 return test->t_name;
48 }
49
test_setup(const struct test_case * test,void ** tdata)50 static inline int test_setup(const struct test_case *test, void **tdata) {
51 int rc;
52
53 assert(test);
54
55 if (test->t_setup)
56 rc = test->t_setup(tdata);
57 else
58 rc = 0;
59
60 return rc;
61 }
62
test_run(const struct test_case * test,void * tdata)63 static inline int test_run(const struct test_case *test, void *tdata) {
64 int rc;
65
66 assert(test);
67
68 if (test->t_run)
69 rc = test->t_run(tdata);
70 else
71 rc = 0;
72
73 return rc;
74 }
75
test_teardown(const struct test_case * test,void * tdata)76 static inline int test_teardown(const struct test_case *test, void *tdata) {
77 int rc;
78
79 assert(test);
80
81 if (test->t_teardown)
82 rc = test->t_teardown(tdata);
83 else
84 rc = 0;
85
86 return rc;
87 }
88
89 #endif /* ^__TEST_COMMON_H */
90