From 484b28469d0a341fc1eff52f43225ed70aec2652 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Tue, 15 Mar 2016 20:38:22 +1000 Subject: [PATCH] * fix test for filter/preg + add test for filter/pcre --- t/CMakeLists.txt | 9 +++++++-- t/t_filters.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ t/t_rx_posix.c | 22 --------------------- 3 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 t/t_filters.c delete mode 100644 t/t_rx_posix.c diff --git a/t/CMakeLists.txt b/t/CMakeLists.txt index 146fc99..3010542 100644 --- a/t/CMakeLists.txt +++ b/t/CMakeLists.txt @@ -5,11 +5,16 @@ set(SRC_DIR "../src") add_executable("t_logfile" "t_logfile.c" "${SRC_DIR}/logfile.c") add_executable("t_matches" "t_matches.c" "${SRC_DIR}/matches.c") add_executable("t_ipaddr" "t_ipaddr.c" "${SRC_DIR}/ipaddr.c" "${SRC_DIR}/matches.c") -#add_executable("t_rx_posix" "t_rx_posix.c" "${SRC_DIR}/regexps_posix.c" "${SRC_DIR}/log.c") add_executable("t_config_param" "t_config_param.c" "${SRC_DIR}/config.c" "${SRC_DIR}/log.c") +add_executable("t_filter_preg" "t_filters.c" "${SRC_DIR}/filters/preg.c") +add_executable("t_filter_pcre" "t_filters.c" "${SRC_DIR}/filters/pcre.c") +target_link_libraries("t_filter_pcre" "pcre") + add_test("tests/f2b_logfile_*" "t_logfile") add_test("tests/f2b_matches_*" "t_matches") add_test("tests/f2b_ipaddr_*" "t_ipaddr") -#add_test("tests/f2b_regex_*" "t_rx_posix") add_test("tests/f2b_config_param*" "t_config_param") + +add_test("tests/filter/preg" "t_filter_preg") +add_test("tests/filter/pcre" "t_filter_pcre") diff --git a/t/t_filters.c b/t/t_filters.c new file mode 100644 index 0000000..485c0d6 --- /dev/null +++ b/t/t_filters.c @@ -0,0 +1,50 @@ +#include "../src/common.h" +#include "../src/log.h" +#include "../src/ipaddr.h" +#include "../src/filters/filter.h" + +int main() { + cfg_t *filter = NULL; + char matchbuf[IPADDR_MAX] = ""; + bool result = false; + + filter = create("test"); + assert(filter != NULL); + + result = config(filter, "nonexistent", "yes"); + assert(result == false); + + result = config(filter, "icase", "yes"); + assert(result == true); + + result = config(filter, "icase", "no"); + assert(result == true); + + result = ready(filter); + assert(result == false); + + result = append(filter, "host without marker"); + assert(result == false); + + result = append(filter, "host with marker "); + assert(result == true); + + result = ready(filter); + assert(result == true); + + result = match(filter, "host", matchbuf, sizeof(matchbuf)); + assert(result == false); + + result = match(filter, "host with marker ", matchbuf, sizeof(matchbuf)); + assert(result == false); + + result = match(filter, "host with marker 1.2.3.4", matchbuf, sizeof(matchbuf)); + assert(result == true); + + result = match(filter, "host with marker example.com", matchbuf, sizeof(matchbuf)); + assert(result == false); + + destroy(filter); + + return 0; +} diff --git a/t/t_rx_posix.c b/t/t_rx_posix.c deleted file mode 100644 index fb7d636..0000000 --- a/t/t_rx_posix.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "../src/common.h" -#include "../src/regexps.h" - -int main() { - f2b_regex_t *list = NULL; - f2b_regex_t *regex = NULL; - char matchbuf[32] = ""; - - assert((regex = f2b_regex_create("line without HOST", true)) == NULL); - assert((regex = f2b_regex_create("line with ", true)) != NULL); - - assert(f2b_regexlist_match(list, "line with 192.168.0.1", matchbuf, sizeof(matchbuf)) == false); - - assert((list = f2b_regexlist_append(list, regex)) != NULL); - - assert(f2b_regexlist_match(list, "line with 192.168.0.1", matchbuf, sizeof(matchbuf)) == true); - assert(strncmp(matchbuf, "192.168.0.1", sizeof(matchbuf)) == 0); - - assert((list = f2b_regexlist_destroy(list)) == NULL); - - return 0; -}