From 2f78bd83737ff756ea8a7cc398946b40725cedee Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Fri, 19 Jun 2026 16:46:54 +0300 Subject: [PATCH] tests: extract_names_from_hash, file_send_prepare [not finished] --- src/components/file_p/file_p.h | 3 ++ test/file_p/CMakeLists.txt | 35 +++++++++++++++- test/file_p/extract_names_from_hash.c | 48 ++++++++++++++++++++++ test/file_p/file_list.c | 39 ++++++++++++++---- test/file_p/file_send_prepare.c | 57 +++++++++++++++++++++++++++ test/file_p/utils.c | 3 +- 6 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 test/file_p/extract_names_from_hash.c create mode 100644 test/file_p/file_send_prepare.c diff --git a/src/components/file_p/file_p.h b/src/components/file_p/file_p.h index ae3e551..06b0727 100644 --- a/src/components/file_p/file_p.h +++ b/src/components/file_p/file_p.h @@ -27,4 +27,7 @@ void clear_file_from_sess(session *); int directory_exists(const char *path); void file_download(session *sess); void file_upload(session *sess); + +/* TESTING */ +void extract_names_from_hash(int32_t file_hash, char *dirname, char *fname); #endif \ No newline at end of file diff --git a/test/file_p/CMakeLists.txt b/test/file_p/CMakeLists.txt index ea5e097..bf9b4df 100644 --- a/test/file_p/CMakeLists.txt +++ b/test/file_p/CMakeLists.txt @@ -30,4 +30,37 @@ target_link_libraries(file_list target_link_options(file_list PRIVATE -Wl,--wrap=db_get_files_data,--wrap=write) -add_test(NAME file_p_file_list_test_1 COMMAND file_list) \ No newline at end of file +add_test(NAME file_p_file_list_test_1 COMMAND file_list) + +# ---/ extract_names_from_hash /------------------------------------- + +add_executable(extract_names_from_hash + extract_names_from_hash.c + utils.c +) + +target_link_libraries(extract_names_from_hash + cmocka + file_p + main +) + +add_test(NAME file_p_extract_names_from_hash_test_1 COMMAND extract_names_from_hash) + +# ---/ file_send_prepare /------------------------------------------- + +add_executable(file_send_prepare + file_send_prepare.c + utils.c +) + +target_link_libraries(file_send_prepare + cmocka + file_p + main + utils +) + +target_link_options(file_list PRIVATE -Wl,--wrap=db_get_files_data,--wrap=write,--wrap=print_log,--wrap=lseek,--wrap=open) + +add_test(NAME file_p_file_send_prepare_test_1 COMMAND file_send_prepare) \ No newline at end of file diff --git a/test/file_p/extract_names_from_hash.c b/test/file_p/extract_names_from_hash.c new file mode 100644 index 0000000..6cbaaa1 --- /dev/null +++ b/test/file_p/extract_names_from_hash.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils.h" +#include +#include +#include +#include +#include + +typedef struct { + int32_t hash; + char dir[3]; + char name[7]; +} name_example_t; + +name_example_t samples[] = { + {.hash = 2138234, .dir = "00", .name = "20a07a"}, + {.hash = 123456, .dir = "00", .name = "01e240"}, + {.hash = 99399234, .dir = "05", .name = "ecb642"}, +}; + +void test__extract_names_from_hash(void **state) { + for (int i = 0 ; i < 3; i++) { + char dir[3]; + char name[7]; + extract_names_from_hash(samples[i].hash, dir, name); + assert_string_equal(dir, samples[i].dir); + assert_string_equal(name, samples[i].name); + } +} + +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__extract_names_from_hash), + }; + + return cmocka_run_group_tests(tests, setup, tear_down); +} diff --git a/test/file_p/file_list.c b/test/file_p/file_list.c index 6765569..d9223f3 100644 --- a/test/file_p/file_list.c +++ b/test/file_p/file_list.c @@ -14,10 +14,14 @@ #include #include +#define COUNT 20 dbuf_t *dbuf; +int amount_available = 0; -fl_t fl_sample = { - .name = "test_file", .description = "Hello test file", .owner = "user1234", .next = NULL}; +fl_t fl_sample = {.name = "test_file", + .description = "Hello test file", + .owner = "user1234", + .next = NULL}; ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur { dbuf_write(__buf, __n, &dbuf); @@ -26,26 +30,44 @@ ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur { int32_t __wrap_db_get_files_data(i_get_files_db *arg, fl_t **fl_start, int32_t *full_count) { - fill_list_with_samples(&fl_sample, fl_start, NULL, arg->limit); - *full_count = arg->limit; - return arg->limit; + int amount = amount_available < arg->limit ? amount_available : arg->limit; + fill_list_with_samples(&fl_sample, fl_start, NULL, amount); + *full_count = amount; + return amount; } -#define COUNT 20 - void test__file_list(void **state) { session sess = { .fl_start = NULL, .fl_current = NULL, .uname = "user1234", }; + amount_available = 200; i_file_list_t fl_args = {.page = 1, .limit = COUNT, .name = NULL}; dbuf = dbuf_init(INBUFSIZE); file_list(&sess, &fl_args); assert_int_not_equal(dbuf->len, 0); char *end_str = strstr(dbuf->ptr, ":END: PAGE"); char strcmp[128]; - sprintf(strcmp, ":END: PAGE 1/1 COUNT: %d/%d\n", COUNT, COUNT); + sprintf(strcmp, ":END: PAGE 1/1 COUNT: %d/%d\n", COUNT, COUNT); + assert_string_equal(strcmp, end_str); + dbuf_destroy(&dbuf); +} + +void test__file_list__empty(void **state) { + session sess = { + .fl_start = NULL, + .fl_current = NULL, + .uname = "user1234", + }; + amount_available = 0; + i_file_list_t fl_args = {.page = 1, .limit = COUNT, .name = NULL}; + dbuf = dbuf_init(INBUFSIZE); + file_list(&sess, &fl_args); + assert_int_not_equal(dbuf->len, 0); + char *end_str = strstr(dbuf->ptr, ":END: PAGE"); + char strcmp[128]; + sprintf(strcmp, ":END: PAGE 1/1 COUNT: %d/%d\n", 0, 0); assert_string_equal(strcmp, end_str); dbuf_destroy(&dbuf); } @@ -56,6 +78,7 @@ int tear_down(void **state) { return 0; } int main(int argc, char **argv) { const struct CMUnitTest tests[] = { cmocka_unit_test(test__file_list), + cmocka_unit_test(test__file_list__empty), }; return cmocka_run_group_tests(tests, setup, tear_down); diff --git a/test/file_p/file_send_prepare.c b/test/file_p/file_send_prepare.c new file mode 100644 index 0000000..7a8e252 --- /dev/null +++ b/test/file_p/file_send_prepare.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils.h" +#include +#include +#include +#include +#include +#include + +dbuf_t *dbuf; + +/* TODO: finish */ + +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; +} + +__off_t __wrap_lseek (int __fd, __off_t __offset, int __whence) __THROW { + return mock_type(int); +} + +int __wrap_open(const char *__file, int __oflag, ...) { + return mock_type(int); +} + +void test__file_send_prepare(void **state) { + dbuf_t *dbuf = dbuf_init(INBUFSIZE); + session sess = { + .uname = "user1234", + .sd = 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__file_send_prepare), + }; + + return cmocka_run_group_tests(tests, setup, tear_down); +} diff --git a/test/file_p/utils.c b/test/file_p/utils.c index ec61c18..1d72507 100644 --- a/test/file_p/utils.c +++ b/test/file_p/utils.c @@ -32,6 +32,7 @@ int32_t dbuf_write(const char *text, size_t text_len, dbuf_t **__dbuf) { it_times++; int _it_times = it_times; strncpy(dbuf->ptr + dbuf->len, text, text_len); + dbuf->ptr[dbuf->len + text_len] = '\0'; dbuf->len += text_len; return 0; } @@ -45,7 +46,7 @@ int32_t dbuf_destroy(dbuf_t **dbuf) { void fill_list_with_samples(fl_t *fl_sample, fl_t **fl_st, fl_t **fl_cur, int amount) { - fl_t *_fl_st, *_fl_cur; + fl_t *_fl_st = NULL, *_fl_cur = NULL; if (fl_st == NULL) return;