summaryrefslogtreecommitdiffstats
path: root/nsock/tests/logs.c
blob: d316d34b5f79c44717a3a8e9819e047e13b0a5b6 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Nsock regression test suite
 * Same license as nmap -- see https://nmap.org/book/man-legal.html
 */

#include "test-common.h"


struct log_test_data {
  nsock_pool nsp;
  nsock_loglevel_t current_level;
  unsigned int got_dbgfull: 1;
  unsigned int got_dbg: 1;
  unsigned int got_info: 1;
  unsigned int got_error: 1;
  unsigned int total;
  int errcode;
};

static struct log_test_data *GlobalLTD;

static void log_handler(const struct nsock_log_rec *rec) {
  GlobalLTD->total++;
  switch(rec->level) {
    case NSOCK_LOG_DBG_ALL:
      GlobalLTD->got_dbgfull = 1;
      break;

    case NSOCK_LOG_DBG:
      GlobalLTD->got_dbg = 1;
      break;

    case NSOCK_LOG_INFO:
      GlobalLTD->got_info = 1;
      break;

    case NSOCK_LOG_ERROR:
      GlobalLTD->got_error = 1;
      break;

    default:
      fprintf(stderr, "UNEXPECTED LOG LEVEL (%d)!\n", (int)rec->level);
      GlobalLTD->errcode = -EINVAL;
  }
}

static void nop_handler(nsock_pool nsp, nsock_event nse, void *udata) {
}

static int check_loglevel(struct log_test_data *ltd, nsock_loglevel_t level) {
  int rc = 0;
  nsock_event_id id;

  nsock_set_loglevel(level);

  ltd->current_level = level;

  ltd->got_dbgfull = 0;
  ltd->got_dbg     = 0;
  ltd->got_info    = 0;
  ltd->got_error   = 0;

  ltd->total   = 0;
  ltd->errcode = 0;

  id = nsock_timer_create(ltd->nsp, nop_handler, 200, NULL);
  nsock_event_cancel(ltd->nsp, id, 0);

  if (ltd->errcode)
    return ltd->errcode;

  if (ltd->total < 1)
    return -EINVAL;

  return rc;
}

static int check_errlevel(struct log_test_data *ltd, nsock_loglevel_t level) {
  nsock_event_id id;

  nsock_set_loglevel(level);

  ltd->current_level = level;

  ltd->got_dbgfull = 0;
  ltd->got_dbg     = 0;
  ltd->got_info    = 0;
  ltd->got_error   = 0;

  ltd->total   = 0;
  ltd->errcode = 0;

  id = nsock_timer_create(ltd->nsp, nop_handler, 200, NULL);
  nsock_event_cancel(ltd->nsp, id, 0);

  if (ltd->errcode)
    return ltd->errcode;

  if (ltd->total > 0)
    return -EINVAL;

  return 0;
}

static int log_setup(void **tdata) {
  struct log_test_data *ltd;

  ltd = calloc(1, sizeof(struct log_test_data));
  if (ltd == NULL)
    return -ENOMEM;

  ltd->nsp = nsock_pool_new(ltd);
  AssertNonNull(ltd->nsp);

  *tdata = GlobalLTD = ltd;
  return 0;
}

static int log_teardown(void *tdata) {
  struct log_test_data *ltd = (struct log_test_data *)tdata;

  if (tdata) {
    nsock_pool_delete(ltd->nsp);
    free(tdata);
  }
  nsock_set_log_function(NULL);
  GlobalLTD = NULL;
  return 0;
}

static int log_check_std_levels(void *tdata) {
  struct log_test_data *ltd = (struct log_test_data *)tdata;
  nsock_loglevel_t lvl;
  int rc = 0;

  nsock_set_log_function(log_handler);

  for (lvl = NSOCK_LOG_DBG_ALL; lvl < NSOCK_LOG_ERROR; lvl++) {
    rc = check_loglevel(ltd, lvl);
    if (rc)
      return rc;
  }

  return 0;
}

static int log_check_err_levels(void *tdata) {
  struct log_test_data *ltd = (struct log_test_data *)tdata;
  nsock_loglevel_t lvl;
  int rc = 0;

  nsock_set_log_function(log_handler);

  for (lvl = NSOCK_LOG_ERROR; lvl <= NSOCK_LOG_NONE; lvl++) {
    rc = check_errlevel(ltd, NSOCK_LOG_ERROR);
    if (rc)
      return rc;
  }

  return 0;
}


const struct test_case TestLogLevels = {
  .t_name     = "set standard log levels",
  .t_setup    = log_setup,
  .t_run      = log_check_std_levels,
  .t_teardown = log_teardown
};

const struct test_case TestErrLevels = {
  .t_name     = "check error log levels",
  .t_setup    = log_setup,
  .t_run      = log_check_err_levels,
  .t_teardown = log_teardown
};