Browse Source

* add tests for f2b_statefile_t

master
Alex 'AdUser' Z 8 years ago
parent
commit
4aaf5f5e73
  1. 2
      t/CMakeLists.txt
  2. 58
      t/t_statefile.c

2
t/CMakeLists.txt

@ -6,6 +6,7 @@ add_executable("t_cmd" "t_cmd.c" "${SRC_DIR}/strlcpy.c" "${SRC_DIR}
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_statefile" "t_statefile.c" "${SRC_DIR}/strlcpy.c" "${SRC_DIR}/matches.c" "${SRC_DIR}/ipaddr.c" "${SRC_DIR}/statefile.c" "${SRC_DIR}/log.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")
@ -13,6 +14,7 @@ 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")
add_test("tests/f2b_statefile_*" "t_statefile")
add_test("tests/f2b_config_param*" "t_config_param")
add_test("tests/backend/usage_*" "t_backend_usage")

58
t/t_statefile.c

@ -0,0 +1,58 @@
#include "../src/common.h"
#include "../src/ipaddr.h"
#include "../src/statefile.h"
int main() {
const char matches = 4;
bool res = false;
f2b_ipaddr_t *list = NULL;
f2b_statefile_t *sf = NULL;
char jailname[64] = "";
struct stat st;
time_t banned_at = 0, release_at = 0;
UNUSED(list);
snprintf(jailname, sizeof(jailname), "%lu", time(NULL)); /* gen unique jailname */
sf = f2b_statefile_create("/tmp", jailname);
assert(sf != NULL);
list = f2b_statefile_load(sf, matches);
assert(list == NULL);
banned_at = time(NULL) - 60;
release_at = banned_at + 60 * 2;
list = f2b_ipaddr_create("1.2.3.4", matches);
list->banned = true;
list->banned_at = banned_at;
list->release_at = release_at;
/* empty file should be created after f2b_statefile_create() call */
stat(sf->path, &st);
assert(st.st_size == 0);
res = f2b_statefile_save(sf, list);
assert(res == true);
stat(sf->path, &st);
assert(st.st_size > 0);
f2b_ipaddr_destroy(list);
list = NULL;
list = f2b_statefile_load(sf, matches);
assert(list != NULL);
assert(list->banned == true);
assert(list->banned_at == banned_at);
assert(list->release_at == release_at);
f2b_ipaddr_destroy(list);
unlink(sf->path);
f2b_statefile_destroy(sf);
return 0;
}
Loading…
Cancel
Save