summaryrefslogtreecommitdiffstats
path: root/nsock/tests/test-common.h
blob: d74646004625650d178561ad31cdaa8d2417a034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Nsock regression test suite
 * Same license as nmap -- see https://nmap.org/book/man-legal.html
 */


#ifndef __TEST_COMMON_H
#define __TEST_COMMON_H

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <nsock.h>


#define PORT_UDP    55234
#define PORT_TCP    55235
#define PORT_TCPSSL 55236


#define __ASSERT_BASE(stmt)    do { \
        if (!(stmt)) { \
            fprintf(stderr, "(%s:%d) Assertion failed: " #stmt "\n", \
                    __FILE__, __LINE__); \
            return -EINVAL; \
        } \
    } while (0)


#define AssertNonNull(a)        __ASSERT_BASE((a) != NULL);
#define AssertEqual(a, b)       __ASSERT_BASE((a) == (b));
#define AssertStrEqual(a, b)    __ASSERT_BASE(strcmp((a), (b)) == 0);


struct test_case {
  const char *t_name;
  int (*t_setup)(void **tdata);
  int (*t_run)(void *tdata);
  int (*t_teardown)(void *tdata);
};


static inline const char *get_test_name(const struct test_case *test) {
  return test->t_name;
}

static inline int test_setup(const struct test_case *test, void **tdata) {
  int rc;

  assert(test);

  if (test->t_setup)
    rc = test->t_setup(tdata);
  else
    rc = 0;

  return rc;
}

static inline int test_run(const struct test_case *test, void *tdata) {
  int rc;

  assert(test);

  if (test->t_run)
    rc = test->t_run(tdata);
  else
    rc = 0;

  return rc;
}

static inline int test_teardown(const struct test_case *test, void *tdata) {
  int rc;

  assert(test);

  if (test->t_teardown)
    rc = test->t_teardown(tdata);
  else
    rc = 0;

  return rc;
}

#endif /* ^__TEST_COMMON_H */