diff --git a/TODO.md b/TODO.md index 89f1d83..46fd7bb 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,8 @@ ## Tasks +# Testing +- [ ] file_receive_prepare / file_send_prepare more reliable line processing + # Development plans - [ ] Remove file when error while saving the data in DB diff --git a/src/components/client/client.c b/src/components/client/client.c index dc13cac..9f7af4e 100644 --- a/src/components/client/client.c +++ b/src/components/client/client.c @@ -12,8 +12,8 @@ #include #include -#define DEF_LIMIT 15 -#define DEF_PAGE_NUM 1 +#define DIF_LIMIT 15 +#define DIF_PAGE_NUM 1 void process_client_command(char *line, session *sess, server_data_t *s_d) { char arg_1[32]; @@ -32,9 +32,9 @@ void process_client_command(char *line, session *sess, server_data_t *s_d) { switch (ret) { case EOF: case 0: - fl_args.limit = DEF_LIMIT; + fl_args.limit = DIF_LIMIT; case 1: - fl_args.page = DEF_PAGE_NUM; + fl_args.page = DIF_PAGE_NUM; } file_list(sess, &fl_args); return; @@ -66,9 +66,9 @@ void process_client_command(char *line, session *sess, server_data_t *s_d) { switch (ret) { case 2: - fl_args.limit = DEF_LIMIT; + fl_args.limit = DIF_LIMIT; case 3: - fl_args.page = DEF_PAGE_NUM; + fl_args.page = DIF_PAGE_NUM; } /* by name */ diff --git a/src/components/client/client.h b/src/components/client/client.h index 8a78960..02110ab 100644 --- a/src/components/client/client.h +++ b/src/components/client/client.h @@ -7,4 +7,7 @@ #include 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); + +/* TEST */ +void process_client_command(char *line, session *sess, server_data_t *s_d); #endif \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 62c96e6..46f4484 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(session) add_subdirectory(user) add_subdirectory(file_p) -add_subdirectory(test_utils) \ No newline at end of file +add_subdirectory(test_utils) +add_subdirectory(client) \ No newline at end of file diff --git a/test/client/CMakeLists.txt b/test/client/CMakeLists.txt new file mode 100644 index 0000000..328748b --- /dev/null +++ b/test/client/CMakeLists.txt @@ -0,0 +1,17 @@ +# ---/ process_client_command /-------------------------------------------------- + +add_executable(process_client_command + process_client_command.c +) + +target_link_libraries(process_client_command + cmocka + client + file_p + main + test_utils +) + +target_link_options(process_client_command PRIVATE -Wl,--wrap=file_list,--wrap=file_send_prepare,--wrap=file_receive_prepare) + +add_test(NAME client_process_client_command_test_1 COMMAND process_client_command) \ No newline at end of file diff --git a/test/client/process_client_command.c b/test/client/process_client_command.c new file mode 100644 index 0000000..07b9ee3 --- /dev/null +++ b/test/client/process_client_command.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +int file_list_calls = 0; +int _limit = 15; +int _page = 2; +char _line[128]; +char file_name[] = "hello_file"; +int file_size = 121212; +int file_perm = 1; + +void __wrap_file_list(session *sess, i_file_list_t *f_args) { + assert_int_equal(_limit, f_args->limit); + assert_int_equal(_page, f_args->page); + file_list_calls++; +} + +int32_t __wrap_file_send_prepare(session *sess, char *line, + server_data_t *s_d) { + assert_string_equal(line, _line); + return mock_type(int); +} + +int __wrap_file_receive_prepare(session *sess, char *line, server_data_t *s_d) { + assert_string_equal(line, _line); + return mock_type(int); +} + +void test__process_client_command__file_list(void **state) { + session sess = {}; + char line[128]; + + sprintf(line, "file list %d %d", _limit, _page); + process_client_command(line, &sess, NULL); + assert_int_equal(file_list_calls, 1); + file_list_calls = 0; +} + +void test__process_client_command__file_upload(void **state) { + session sess = {.state = OP_WAIT}; + + will_return(__wrap_file_receive_prepare, 0); + sprintf(_line, "file upload \"%s\" %d %d", file_name, file_size, file_perm); + + process_client_command(_line, &sess, NULL); + + assert_int_equal(file_list_calls, 0); + assert_int_equal(sess.state, OP_UPLOAD); +} + +void test__process_client_command__file_upload_fail(void **state) { + session sess = {.state = OP_WAIT}; + + will_return(__wrap_file_receive_prepare, 1); + sprintf(_line, "file upload \"%s\" %d %d", file_name, file_size, file_perm); + + process_client_command(_line, &sess, NULL); + + assert_int_equal(file_list_calls, 0); + assert_int_equal(sess.state, OP_WAIT); +} + +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__process_client_command__file_list), + cmocka_unit_test(test__process_client_command__file_upload), + cmocka_unit_test(test__process_client_command__file_upload_fail), + }; + + return cmocka_run_group_tests(tests, setup, tear_down); +}