summaryrefslogtreecommitdiffstats
path: root/nsock/tests/tests_main.c
blob: 4ab6d0759ba6fe0f313876dc8f79d7c68f390ede (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Nsock regression test suite
 * Same license as nmap -- see https://nmap.org/book/man-legal.html
 */


#include "test-common.h"
#include "string.h"


#ifndef WIN32
  #define RESET       "\033[0m"
  #define BOLDRED     "\033[1m\033[31m"
  #define BOLDGREEN   "\033[1m\033[32m"
  #define TEST_FAILED "[" BOLDRED "FAILED" RESET "]"
  #define TEST_OK     "[" BOLDGREEN "OK" RESET "]"
#else
  /* WIN32 terminal has no ANSI driver */
  #define TEST_FAILED "[FAILED]"
  #define TEST_OK     "[OK]"
#endif



/* socket_strerror() comes from nbase
 * Declared here to work around a silly inclusion issue until I can fix it. */
char *socket_strerror(int errnum);

extern const struct test_case TestPoolUserData;
extern const struct test_case TestTimer;
extern const struct test_case TestLogLevels;
extern const struct test_case TestErrLevels;
extern const struct test_case TestConnectTCP;
extern const struct test_case TestConnectFailure;
extern const struct test_case TestGHLists;
extern const struct test_case TestGHHeaps;
extern const struct test_case TestHeapOrdering;
extern const struct test_case TestProxyParse;
extern const struct test_case TestCancelTCP;
extern const struct test_case TestCancelUDP;
#ifdef HAVE_OPENSSL
extern const struct test_case TestCancelSSL;
#endif


static const struct test_case *TestCases[] = {
  /* ---- basic.c */
  &TestPoolUserData,
  /* ---- timer.c */
  &TestTimer,
  /* ---- logs.c */
  &TestLogLevels,
  &TestErrLevels,
  /* ---- connect.c */
  &TestConnectTCP,
  &TestConnectFailure,
  /* ---- ghlists.c */
  &TestGHLists,
  /* ---- ghheaps.c */
  &TestGHHeaps,
  &TestHeapOrdering,
  /* ---- proxychain.c */
  &TestProxyParse,
  /* ---- cancel.c */
  &TestCancelTCP,
  &TestCancelUDP,
#ifdef HAVE_OPENSSL
  &TestCancelSSL,
#endif
  NULL
};


static int test_case_run(const struct test_case *test) {
  int rc;
  void *tdata = NULL;

  rc = test_setup(test, &tdata);
  if (rc)
    return rc;

  rc = test_run(test, tdata);
  if (rc)
    return rc;

  return test_teardown(test, tdata);
}

#ifdef WIN32
static int win_init(void) {
  WSADATA data;
  int rc;

  rc = WSAStartup(MAKEWORD(2, 2), &data);
  if (rc)
    fatal("Failed to start winsock: %s\n", socket_strerror(rc));

  return 0;
}
#endif

int main(int ac, char **av) {
  int rc, i;

  /* simple "do we have ssl" check for run_tests.sh */
  if (ac == 2 && !strncmp(av[1], "--ssl", 5)) {
#ifdef HAVE_SSL
    return 0;
#else
    return 1;
#endif
  }

#ifdef WIN32
  win_init();
#endif

  for (i = 0; TestCases[i] != NULL; i++) {
    const struct test_case *current = TestCases[i];
    const char *name = get_test_name(current);

    printf("%-48s", name);
    fflush(stdout);
    rc = test_case_run(current);
    if (rc) {
      printf(TEST_FAILED " (%s)\n", socket_strerror(-rc));
      break;
    }
    printf(TEST_OK "\n");
  }
  return rc;
}