From 2559fe737c0d11ae7a6b7aabddc5771ac91552de Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Thu, 2 Jul 2026 14:37:25 +0300 Subject: [PATCH] tests: download_confirm --- src/components/client/client.c | 4 +- test/client/CMakeLists.txt | 20 +++++++- test/client/download_confirm.c | 85 ++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 test/client/download_confirm.c diff --git a/src/components/client/client.c b/src/components/client/client.c index 9f7bcd0..9ada2ef 100644 --- a/src/components/client/client.c +++ b/src/components/client/client.c @@ -101,7 +101,7 @@ void process_client_command(char *line, session *sess, server_data_t *s_d) { void download_confirm(char *line, session *sess, server_data_t *s_d) { if (!strncmp(line, "continue", sizeof "continue" - 1)) { - int pac_siz = 0; + int pac_siz = PACKAGE_SIZE; sscanf(line, "continue %d\n", &pac_siz); sess->state = OP_DOWNLOAD; sess->file->package_rest = pac_siz; @@ -112,5 +112,7 @@ void download_confirm(char *line, session *sess, server_data_t *s_d) { sess->file->name); clear_file_from_sess(sess); sess->state = OP_WAIT; + return; } + print_log(stdout, pl_info, "The answer is not correct. Send either \"continue \%PACKAGE_SIZE\% or \"cancel\""); } \ No newline at end of file diff --git a/test/client/CMakeLists.txt b/test/client/CMakeLists.txt index b592df1..372e993 100644 --- a/test/client/CMakeLists.txt +++ b/test/client/CMakeLists.txt @@ -14,4 +14,22 @@ target_link_libraries(process_client_command target_link_options(process_client_command PRIVATE -Wl,--wrap=file_list,--wrap=file_send_prepare,--wrap=file_receive_prepare,--wrap=session_send_string) -add_test(NAME client_process_client_command_test_1 COMMAND process_client_command) \ No newline at end of file +add_test(NAME client_process_client_command_test_1 COMMAND process_client_command) + +# ---/ download_confirm /-------------------------------------------------- + +add_executable(download_confirm + download_confirm.c +) + +target_link_libraries(download_confirm + cmocka + client + file_p + main + test_utils +) + +target_link_options(download_confirm PRIVATE -Wl,--wrap=print_log,--wrap=clear_file_from_sess) + +add_test(NAME client_download_confirm_test_1 COMMAND download_confirm) \ No newline at end of file diff --git a/test/client/download_confirm.c b/test/client/download_confirm.c new file mode 100644 index 0000000..da068af --- /dev/null +++ b/test/client/download_confirm.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#define PAC_SIZ 107898 +#define FNAME "test_file_1" + +char *str2snd = NULL; +int clear_file_from_sess_call = 0; + +extern void *print_str_arr; + +void __wrap_clear_file_from_sess(session *s) { + clear_file_from_sess_call++; + s->file = NULL; + return; +} + +void test__download_confirm__continue(void **state) { + s_file_t file = {.package_rest = 0}; + session sess = {.state = OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE, .file = &file}; + char line[128]; + sprintf(line, "continue %d\n", PAC_SIZ); + + download_confirm(line, &sess, NULL); + assert_int_equal(sess.state, OP_DOWNLOAD); + assert_int_equal(sess.file->package_rest, PAC_SIZ); +} + +void test__download_confirm__continue_no_size(void **state) { + s_file_t file = {.package_rest = 0}; + session sess = {.state = OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE, .file = &file}; + char line[128]; + sprintf(line, "continue"); + + download_confirm(line, &sess, NULL); + assert_int_equal(sess.state, OP_DOWNLOAD); + assert_int_equal(sess.file->package_rest, PACKAGE_SIZE); +} + +void test__download_confirm__cancel(void **state) { + s_file_t file = {.package_rest = 0, .name = FNAME}; + session sess = {.state = OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE, .file = &file}; + const char *fmt_args[] = {"Downloading of %s is canceled by the client\n", + FNAME, NULL}; + print_str_arr = fmt_args; + download_confirm("cancel\n", &sess, NULL); + assert_int_equal(sess.state, OP_WAIT); + assert_ptr_equal(sess.file, NULL); +} + +void test__download_confirm__noise(void **state) { + s_file_t file = {.package_rest = 0, .name = FNAME}; + session sess = {.state = OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE, .file = &file}; + const char *fmt_args[] = {"The answer is not correct. Send either \"continue " + "\%PACKAGE_SIZE\% or \"cancel\"", + NULL}; + print_str_arr = fmt_args; + download_confirm("jasdf", &sess, NULL); + assert_int_equal(sess.state, OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE); +} + +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__download_confirm__continue), + cmocka_unit_test(test__download_confirm__continue_no_size), + cmocka_unit_test(test__download_confirm__cancel), + cmocka_unit_test(test__download_confirm__noise), + }; + + return cmocka_run_group_tests(tests, setup, tear_down); +} \ No newline at end of file