tests: extract_names_from_hash, file_send_prepare [not finished]

This commit is contained in:
2026-06-19 16:46:54 +03:00
parent 573907a0c5
commit 2f78bd8373
6 changed files with 175 additions and 10 deletions
+3
View File
@@ -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
+34 -1
View File
@@ -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)
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)
+48
View File
@@ -0,0 +1,48 @@
#include <cmocka.h>
#include <db/db.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#include <file_p.h>
#include <main.h>
#include <string.h>
#include <sys/cdefs.h>
#include <unistd.h>
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);
}
+31 -8
View File
@@ -14,10 +14,14 @@
#include <sys/cdefs.h>
#include <unistd.h>
#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);
+57
View File
@@ -0,0 +1,57 @@
#include <cmocka.h>
#include <db/db.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#include <file_p.h>
#include <main.h>
#include <string.h>
#include <sys/cdefs.h>
#include <unistd.h>
#include <utils.h>
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);
}
+2 -1
View File
@@ -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;