From 923c950a88fff2b459d6f766bea27ee32a57117a Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Wed, 21 Sep 2016 13:52:12 +1000 Subject: [PATCH] + t/t_cmd.c --- t/CMakeLists.txt | 2 ++ t/t_cmd.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 t/t_cmd.c diff --git a/t/CMakeLists.txt b/t/CMakeLists.txt index fc3f7bf..45b3cf2 100644 --- a/t/CMakeLists.txt +++ b/t/CMakeLists.txt @@ -2,12 +2,14 @@ enable_testing() set(SRC_DIR "../src") +add_executable("t_cmd" "t_cmd.c" "${SRC_DIR}/strlcpy.c" "${SRC_DIR}/commands.c") add_executable("t_cmsg" "t_cmsg.c" "${SRC_DIR}/strlcpy.c" "${SRC_DIR}/cmsg.c") add_executable("t_matches" "t_matches.c" "${SRC_DIR}/strlcpy.c" "${SRC_DIR}/matches.c") add_executable("t_ipaddr" "t_ipaddr.c" "${SRC_DIR}/strlcpy.c" "${SRC_DIR}/matches.c" "${SRC_DIR}/ipaddr.c") add_executable("t_config_param" "t_config_param.c" "${SRC_DIR}/strlcpy.c" "${SRC_DIR}/config.c" "${SRC_DIR}/log.c") add_executable("t_backend_usage" "t_backend_usage.c" "${SRC_DIR}/strlcpy.c") +add_test("tests/f2b_cmd_*" "t_cmd") add_test("tests/f2b_cmsg_*" "t_cmsg") add_test("tests/f2b_matches_*" "t_matches") add_test("tests/f2b_ipaddr_*" "t_ipaddr") diff --git a/t/t_cmd.c b/t/t_cmd.c new file mode 100644 index 0000000..573a34b --- /dev/null +++ b/t/t_cmd.c @@ -0,0 +1,32 @@ +#include "../src/common.h" +#include "../src/commands.h" + +int main() { + char buf[1024]; + const char *line; + + buf[0] = '\0'; + f2b_cmd_append_arg(buf, sizeof(buf), "42"); + assert(strcmp(buf, "42\n") == 0); + + line = "status"; + assert(f2b_cmd_parse(buf, sizeof(buf), line) == CMD_STATUS); + + line = "statu"; /* no such command */ + assert(f2b_cmd_parse(buf, sizeof(buf), line) == CMD_NONE); + + line = "jail test"; /* incomplete command */ + assert(f2b_cmd_parse(buf, sizeof(buf), line) == CMD_NONE); + + buf[0] = '\0'; + line = "jail test status"; + assert(f2b_cmd_parse(buf, sizeof(buf), line) == CMD_JAIL_STATUS); + assert(strcmp(buf, "test\n") == 0); + + buf[0] = '\0'; + line = "jail test set bantime 7200"; + assert(f2b_cmd_parse(buf, sizeof(buf), line) == CMD_JAIL_SET); + assert(strcmp(buf, "test\nbantime\n7200\n") == 0); + + return EXIT_SUCCESS; +}