diff --git a/src/components/file_p/file_p.c b/src/components/file_p/file_p.c index 298b885..b46128a 100644 --- a/src/components/file_p/file_p.c +++ b/src/components/file_p/file_p.c @@ -315,7 +315,14 @@ void file_download(session *sess) { int read_len; if (sess->file->package_rest < INBUFSIZE) { + if (sess->file->package_rest == 0) { + sess->state = OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE; + return; + } read_len = sess->file->package_rest; + } else if (sess->file->rest < INBUFSIZE) { + /* Do not trust read */ + read_len = sess->file->rest; } else { read_len = INBUFSIZE; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 61d9e03..62c96e6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(session) add_subdirectory(user) -add_subdirectory(file_p) \ No newline at end of file +add_subdirectory(file_p) +add_subdirectory(test_utils) \ No newline at end of file diff --git a/test/file_p/CMakeLists.txt b/test/file_p/CMakeLists.txt index 6f3d71b..a13c7de 100644 --- a/test/file_p/CMakeLists.txt +++ b/test/file_p/CMakeLists.txt @@ -2,13 +2,13 @@ add_executable(clear_list clear_list.c - utils.c ) target_link_libraries(clear_list cmocka file_p main + test_utils ) target_link_options(clear_list PRIVATE -Wl,--wrap=free) @@ -19,13 +19,13 @@ add_test(NAME file_p_clear_list_test_1 COMMAND clear_list) add_executable(file_list file_list.c - utils.c ) target_link_libraries(file_list cmocka file_p main + test_utils ) target_link_options(file_list PRIVATE -Wl,--wrap=db_get_files_data,--wrap=write) @@ -36,13 +36,13 @@ add_test(NAME file_p_file_list_test_1 COMMAND file_list) add_executable(extract_names_from_hash extract_names_from_hash.c - utils.c ) target_link_libraries(extract_names_from_hash cmocka file_p main + test_utils ) add_test(NAME file_p_extract_names_from_hash_test_1 COMMAND extract_names_from_hash) @@ -51,7 +51,6 @@ add_test(NAME file_p_extract_names_from_hash_test_1 COMMAND extract_names_from_h add_executable(file_send_prepare file_send_prepare.c - utils.c ) target_link_libraries(file_send_prepare @@ -59,6 +58,7 @@ target_link_libraries(file_send_prepare file_p main utils + test_utils ) target_link_options(file_send_prepare PRIVATE -Wl,--wrap=write,--wrap=print_log,--wrap=lseek,--wrap=open,--wrap=db_get_file) @@ -69,7 +69,6 @@ add_test(NAME file_p_file_send_prepare_test_1 COMMAND file_send_prepare) add_executable(clear_file_from_sess clear_file_from_sess.c - utils.c ) target_link_libraries(clear_file_from_sess @@ -77,6 +76,7 @@ target_link_libraries(clear_file_from_sess file_p main utils + test_utils ) target_link_options(clear_file_from_sess PRIVATE -Wl,--wrap=free) @@ -87,7 +87,6 @@ add_test(NAME file_p_clear_file_from_sess_test_1 COMMAND clear_file_from_sess) add_executable(file_download file_download.c - utils.c ) target_link_libraries(file_download @@ -96,6 +95,7 @@ target_link_libraries(file_download main utils client + test_utils ) target_link_options(file_download PRIVATE -Wl,--wrap=print_log,--wrap=read,--wrap=write) diff --git a/test/file_p/clear_list.c b/test/file_p/clear_list.c index deb7ad5..3dd7a55 100644 --- a/test/file_p/clear_list.c +++ b/test/file_p/clear_list.c @@ -10,7 +10,7 @@ #include #include #include -#include "utils.h" +#include fl_t fl_sample = { .name = "test_file", .description = "Hello test file", .owner = "user1234"}; diff --git a/test/file_p/file_download.c b/test/file_p/file_download.c index d8e40f0..80c338f 100644 --- a/test/file_p/file_download.c +++ b/test/file_p/file_download.c @@ -9,6 +9,7 @@ #include #include #include +#include dbuf_t *dbuf = NULL; #define FNAME "Test file" @@ -16,29 +17,8 @@ dbuf_t *dbuf = NULL; #define FSIZE PACKAGE_SIZE *ITERATIONS int read_ret_zero = 0; int write_ret_m_1 = 0; -char *print_str = NULL; -void *print_str_arr = NULL; - -int __wrap_print_log(FILE *output, enum pl_type type, const char *fmt, ...) { - if (print_str != NULL) { - assert_string_equal(print_str, fmt); - } else if (print_str_arr != NULL) { - char **ptrs = print_str_arr; - va_list args; - assert_string_equal(*ptrs, fmt); - ptrs++; - va_start(args, fmt); - while (*ptrs != NULL) { - char *out_str = va_arg(args, char *); - assert_string_equal(out_str, *ptrs); - ptrs++; - } - - va_end(args); - } - - return 0; -}; +extern char *print_str; +extern void *print_str_arr; ssize_t __wrap_read(int __fd, void *__buf, size_t __nbytes) { if (read_ret_zero) @@ -66,20 +46,52 @@ void test__file_download__normal(void **state) { sess.file->path = malloc(sizeof(char) * INBUFSIZE); sess.file->description = malloc(sizeof(char) * INBUFSIZE); strcpy(sess.file->name, FNAME); + char cont_mes[64]; + + sprintf(cont_mes, "continue %d\n", PACKAGE_SIZE); const char *fmt_args[] = {"File %s is downloaded from the server\n", FNAME, NULL}; print_str_arr = fmt_args; - while (sess.state != OP_WAIT) { + for (int i = 0; (sess.state != OP_WAIT) && (i < 2000000000); i++) { file_download(&sess); if (sess.state == OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE) { - download_confirm("continue", &sess, NULL); + download_confirm(cont_mes, &sess, NULL); + } + } + assert_int_equal(sess.state, OP_WAIT); + assert_ptr_equal(sess.file, NULL); + clear_file_from_sess(&sess); +} + +void test__file_download__normal_alt_buf_size(void **state) { + dbuf = dbuf_init(INBUFSIZE); + + session sess = {.state = OP_DOWNLOAD}; + sess.file = malloc(sizeof(s_file_t)); + sess.file->name = malloc(sizeof FNAME); + sess.file->size = sess.file->rest = FSIZE; + sess.file->package_rest = PACKAGE_SIZE; + sess.file->path = malloc(sizeof(char) * INBUFSIZE); + sess.file->description = malloc(sizeof(char) * INBUFSIZE); + strcpy(sess.file->name, FNAME); + char cont_mes[64]; + + sprintf(cont_mes, "continue %d\n", PACKAGE_SIZE / 3); + + const char *fmt_args[] = {"File %s is downloaded from the server\n", FNAME, + NULL}; + print_str_arr = fmt_args; + + for (int i = 0; (sess.state != OP_WAIT) && (i < 2000000000); i++) { + file_download(&sess); + if (sess.state == OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE) { + download_confirm(cont_mes, &sess, NULL); } } assert_int_equal(sess.state, OP_WAIT); assert_ptr_equal(sess.file, NULL); - print_str_arr = NULL; clear_file_from_sess(&sess); } @@ -108,7 +120,6 @@ void test__file_download__cancel(void **state) { } assert_int_equal(sess.state, OP_WAIT); assert_ptr_equal(sess.file, NULL); - print_str_arr = NULL; } void test__file_download__read_zero(void **state) { @@ -129,7 +140,6 @@ void test__file_download__read_zero(void **state) { assert_int_equal(sess.state, OP_WAIT); assert_ptr_equal(sess.file, NULL); read_ret_zero = false; - print_str_arr = NULL; } void test__file_download__read_write_m_1(void **state) { @@ -154,7 +164,6 @@ void test__file_download__read_write_m_1(void **state) { assert_int_equal(sess.state, ERR); assert_ptr_equal(sess.file, NULL); write_ret_m_1 = false; - print_str_arr = NULL; } int setup(void **state) { return 0; } @@ -163,6 +172,7 @@ int tear_down(void **state) { return 0; } int main(int argc, char **argv) { const struct CMUnitTest tests[] = { cmocka_unit_test(test__file_download__normal), + cmocka_unit_test(test__file_download__normal_alt_buf_size), cmocka_unit_test(test__file_download__cancel), cmocka_unit_test(test__file_download__read_zero), cmocka_unit_test(test__file_download__read_write_m_1), diff --git a/test/file_p/file_list.c b/test/file_p/file_list.c index d9223f3..a746128 100644 --- a/test/file_p/file_list.c +++ b/test/file_p/file_list.c @@ -13,6 +13,7 @@ #include #include #include +#include #define COUNT 20 dbuf_t *dbuf; diff --git a/test/file_p/file_receive_prepare.c b/test/file_p/file_receive_prepare.c index 081a56e..64da95e 100644 --- a/test/file_p/file_receive_prepare.c +++ b/test/file_p/file_receive_prepare.c @@ -17,6 +17,7 @@ #include #include #include +#include #define FSIZE 10233 #define FSIZE_S "10233" @@ -60,11 +61,6 @@ int __wrap_mkdir(const char *__path, __mode_t __mode) { return mock_type(int); } int __wrap_chdir(const char *__path) { return 0; } -int __wrap_print_log(FILE *output, enum pl_type type, - const char *format_message, ...) { - return 0; -}; - ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur { dbuf_write(__buf, __n, &dbuf); return __n; diff --git a/test/file_p/file_send_prepare.c b/test/file_p/file_send_prepare.c index b84f223..79f2171 100644 --- a/test/file_p/file_send_prepare.c +++ b/test/file_p/file_send_prepare.c @@ -14,6 +14,7 @@ #include #include #include +#include dbuf_t *dbuf; @@ -23,11 +24,6 @@ dbuf_t *dbuf; #define FID 19 #define FID_S "19" -int __wrap_print_log(FILE *output, enum pl_type type, - const char *format_message, ...) { - return 0; -}; - ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur { dbuf_write(__buf, __n, &dbuf); return __n; diff --git a/test/test_utils/CMakeLists.txt b/test/test_utils/CMakeLists.txt new file mode 100644 index 0000000..2517f2b --- /dev/null +++ b/test/test_utils/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(test_utils STATIC) +target_link_libraries(test_utils cmocka main utils) +target_sources(test_utils + PRIVATE + test_utils.c + PUBLIC + FILE_SET HEADERS + FILES + test_utils.h + ) \ No newline at end of file diff --git a/test/file_p/utils.c b/test/test_utils/test_utils.c similarity index 76% rename from test/file_p/utils.c rename to test/test_utils/test_utils.c index 1d72507..5559563 100644 --- a/test/file_p/utils.c +++ b/test/test_utils/test_utils.c @@ -1,11 +1,13 @@ #include "../../src/main.h" +#include "cmocka.h" #include #include #include #include #include #include -#include "utils.h" +#include "test_utils.h" +#include dbuf_t *dbuf_init(size_t init_sz) { dbuf_t *dbuf = malloc(sizeof(dbuf_t)); @@ -78,4 +80,32 @@ void fill_list_with_samples(fl_t *fl_sample, fl_t **fl_st, fl_t **fl_cur, if (fl_cur != NULL) { *fl_cur = _fl_cur; } -} \ No newline at end of file +} + +char *print_str = NULL; +void *print_str_arr = NULL; + +int __wrap_print_log(FILE *output, enum pl_type type, const char *fmt, ...) { + if (print_str != NULL) { + assert_string_equal(print_str, fmt); + print_str = NULL; + } else if (print_str_arr != NULL) { + char **ptrs = print_str_arr; + va_list args; + assert_string_equal(*ptrs, fmt); + ptrs++; + va_start(args, fmt); + + while (*ptrs != NULL) { + char *out_str = va_arg(args, char *); + assert_string_equal(out_str, *ptrs); + ptrs++; + } + + print_str_arr = NULL; + + va_end(args); + } + + return 0; +}; \ No newline at end of file diff --git a/test/file_p/utils.h b/test/test_utils/test_utils.h similarity index 56% rename from test/file_p/utils.h rename to test/test_utils/test_utils.h index 73c26dd..8bcda29 100644 --- a/test/file_p/utils.h +++ b/test/test_utils/test_utils.h @@ -1,4 +1,9 @@ -#include "../../src/main.h" +#ifndef TEST_UTILS_H +#define TEST_UTILS_H + +#include +#include +#include typedef struct { char *ptr; @@ -10,4 +15,7 @@ void fill_list_with_samples(fl_t *fl_samples, fl_t **fl_st, fl_t **fl_cur, int amount); dbuf_t *dbuf_init(size_t init_sz); int32_t dbuf_write(const char *text, size_t len, dbuf_t **dbuf); -int32_t dbuf_destroy(dbuf_t **dbuf); \ No newline at end of file +int32_t dbuf_destroy(dbuf_t **dbuf); +int __wrap_print_log(FILE *output, enum pl_type type, const char *fmt, ...); + +#endif \ No newline at end of file