mirror of
https://github.com/nmap/nmap.git
synced 2025-12-09 14:11:29 +00:00
Added a nsock test module for loglevels.
This commit is contained in:
@@ -13,7 +13,8 @@ LDFLAGS = -lssl -lpcap -lcrypto
|
||||
|
||||
SRC = tests_main.c \
|
||||
basic.c \
|
||||
timer.c
|
||||
timer.c \
|
||||
logs.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
|
||||
178
nsock/tests/logs.c
Normal file
178
nsock/tests/logs.c
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Nsock regression test suite
|
||||
* Same license as nmap -- see http://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 void log_handler(nsock_pool nsp, const struct nsock_log_rec *rec) {
|
||||
struct log_test_data *ltd;
|
||||
|
||||
ltd = (struct log_test_data *)nsp_getud(nsp);
|
||||
ltd->total++;
|
||||
|
||||
switch(rec->level) {
|
||||
case NSOCK_LOG_DBG_ALL:
|
||||
ltd->got_dbgfull = 1;
|
||||
break;
|
||||
|
||||
case NSOCK_LOG_DBG:
|
||||
ltd->got_dbg = 1;
|
||||
break;
|
||||
|
||||
case NSOCK_LOG_INFO:
|
||||
ltd->got_info = 1;
|
||||
break;
|
||||
|
||||
case NSOCK_LOG_ERROR:
|
||||
ltd->got_error = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "UNEXPECTED LOG LEVEL (%d)!\n", (int)rec->level);
|
||||
ltd->errcode = -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
static void nop_handler(nsock_pool nsp, nsock_event nse, void *udata) {
|
||||
return;
|
||||
}
|
||||
|
||||
static int check_loglevel(struct log_test_data *ltd, nsock_loglevel_t level) {
|
||||
int rc = 0;
|
||||
nsock_event_id id;
|
||||
|
||||
nsock_set_loglevel(ltd->nsp, 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(ltd->nsp, 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 = nsp_new(ltd);
|
||||
AssertNonNull(ltd->nsp);
|
||||
|
||||
*tdata = ltd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int log_teardown(void *tdata) {
|
||||
struct log_test_data *ltd = (struct log_test_data *)tdata;
|
||||
|
||||
if (tdata) {
|
||||
nsp_delete(ltd->nsp);
|
||||
free(tdata);
|
||||
}
|
||||
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(ltd->nsp, 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(ltd->nsp, 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
|
||||
};
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
extern const struct test_case TestPoolUserData;
|
||||
extern const struct test_case TestTimer;
|
||||
extern const struct test_case TestLogLevels;
|
||||
extern const struct test_case TestErrLevels;
|
||||
|
||||
|
||||
static const struct test_case *TestCases[] = {
|
||||
@@ -24,6 +26,9 @@ static const struct test_case *TestCases[] = {
|
||||
&TestPoolUserData,
|
||||
/* ---- timer.c */
|
||||
&TestTimer,
|
||||
/* ---- logs.c */
|
||||
&TestLogLevels,
|
||||
&TestErrLevels,
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user