From e7703628176a979eed4935784fa69119d0dfa77a Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Thu, 25 Jun 2026 14:31:21 +0300 Subject: [PATCH] tests for clear_file_from_sess --- src/components/file_p/file_p.c | 36 +++++----- test/file_p/CMakeLists.txt | 10 +-- test/file_p/clear_file_from_sess.c | 111 +++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 22 deletions(-) create mode 100644 test/file_p/clear_file_from_sess.c diff --git a/src/components/file_p/file_p.c b/src/components/file_p/file_p.c index ded7114..f4d7181 100644 --- a/src/components/file_p/file_p.c +++ b/src/components/file_p/file_p.c @@ -285,24 +285,26 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) { void clear_file_from_sess(session *s) { s_file_t *sf = s->file; - if (sf->name != NULL) { - free(sf->name); - sf->name = NULL; + if (sf != NULL) { + if (sf->name != NULL) { + free(sf->name); + sf->name = NULL; + } + if (sf->path != NULL) { + free(sf->path); + sf->path = NULL; + } + if (sf->description != NULL) { + free(sf->description); + sf->description = NULL; + } + if (s->fd > -1) { + close(s->fd); + s->fd = -1; + } + free(sf); + s->file = NULL; } - if (sf->path != NULL) { - free(sf->path); - sf->path = NULL; - } - if (sf->description != NULL) { - free(sf->description); - sf->description = NULL; - } - if (s->fd > -1) { - close(s->fd); - s->fd = -1; - } - free(sf); - s->file = NULL; } /* download to the client */ diff --git a/test/file_p/CMakeLists.txt b/test/file_p/CMakeLists.txt index 2e18164..8729423 100644 --- a/test/file_p/CMakeLists.txt +++ b/test/file_p/CMakeLists.txt @@ -67,18 +67,18 @@ add_test(NAME file_p_file_send_prepare_test_1 COMMAND file_send_prepare) # ---/ file_receive_prepare /------------------------------------------- -add_executable(file_receive_prepare - file_receive_prepare.c +add_executable(clear_file_from_sess + clear_file_from_sess.c utils.c ) -target_link_libraries(file_receive_prepare +target_link_libraries(clear_file_from_sess cmocka file_p main utils ) -target_link_options(file_receive_prepare PRIVATE -Wl,--wrap=stat,--wrap=statvfs,--wrap=print_log,--wrap=open,--wrap=chdir,--wrap=write,--wrap=mkdir) +target_link_options(clear_file_from_sess PRIVATE -Wl,--wrap=free) -add_test(NAME file_p_file_receive_prepare_test_1 COMMAND file_receive_prepare) \ No newline at end of file +add_test(NAME file_p_clear_file_from_sess_test_1 COMMAND clear_file_from_sess) \ No newline at end of file diff --git a/test/file_p/clear_file_from_sess.c b/test/file_p/clear_file_from_sess.c new file mode 100644 index 0000000..b50efa7 --- /dev/null +++ b/test/file_p/clear_file_from_sess.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils.h" +#include +#include +#include + +int free_call_times = 0; + +void __real_free(void *ptr); + +void __wrap_free(void *ptr) { + free_call_times++; + __real_free(ptr); +} + +void test__clear_file_from_sess__normal(void **state) { + session sess = {}; + free_call_times = 0; + s_file_t *f = malloc(sizeof(s_file_t)); + f->description = malloc(sizeof (char) * INBUFSIZE); + f->name = malloc(sizeof (char) * INBUFSIZE); + f->path = malloc(sizeof (char) * INBUFSIZE); + + sess.file = f; + + clear_file_from_sess(&sess); + + assert_ptr_equal(sess.file, NULL); + assert_int_equal(free_call_times, 4); +} + +void test__clear_file_from_sess__no_description(void **state) { + session sess = {}; + free_call_times = 0; + s_file_t *f = malloc(sizeof(s_file_t)); + f->description = NULL; + f->name = malloc(sizeof (char) * INBUFSIZE); + f->path = malloc(sizeof (char) * INBUFSIZE); + + sess.file = f; + + clear_file_from_sess(&sess); + + assert_ptr_equal(sess.file, NULL); + assert_int_equal(free_call_times, 3); +} + +void test__clear_file_from_sess__no_name(void **state) { + session sess = {}; + free_call_times = 0; + s_file_t *f = malloc(sizeof(s_file_t)); + f->description = malloc(sizeof (char) * INBUFSIZE); + f->name = NULL; + f->path = malloc(sizeof (char) * INBUFSIZE); + + sess.file = f; + + clear_file_from_sess(&sess); + + assert_ptr_equal(sess.file, NULL); + assert_int_equal(free_call_times, 3); +} + +void test__clear_file_from_sess__no_path(void **state) { + session sess = {}; + free_call_times = 0; + s_file_t *f = malloc(sizeof(s_file_t)); + f->description = malloc(sizeof (char) * INBUFSIZE); + f->name = malloc(sizeof (char) * INBUFSIZE); + f->path = NULL; + + sess.file = f; + + clear_file_from_sess(&sess); + + assert_ptr_equal(sess.file, NULL); + assert_int_equal(free_call_times, 3); +} + +void test__clear_file_from_sess__no_file(void **state) { + session sess = {.file = NULL}; + free_call_times = 0; + + clear_file_from_sess(&sess); + + assert_ptr_equal(sess.file, NULL); + assert_int_equal(free_call_times, 0); +} + +int setup(void **state) { return 0; } +int tear_down(void **state) { return 0; } + +int main(int argc, char **argv) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test__clear_file_from_sess__normal), + cmocka_unit_test(test__clear_file_from_sess__no_description), + cmocka_unit_test(test__clear_file_from_sess__no_name), + cmocka_unit_test(test__clear_file_from_sess__no_path), + cmocka_unit_test(test__clear_file_from_sess__no_file), + }; + + return cmocka_run_group_tests(tests, setup, tear_down); +}