file_p tests
This commit is contained in:
@@ -30,18 +30,24 @@
|
|||||||
char *extract_second_arg(char *);
|
char *extract_second_arg(char *);
|
||||||
size_t get_file_size(char *, char *);
|
size_t get_file_size(char *, char *);
|
||||||
|
|
||||||
void clear_list(fl_t *start) {
|
void clear_list(fl_t **a_start, fl_t **a_current) {
|
||||||
if (start) {
|
if (a_start == NULL) return;
|
||||||
|
fl_t *start = *a_start;
|
||||||
|
if (start != NULL) {
|
||||||
fl_t *curr = start;
|
fl_t *curr = start;
|
||||||
fl_t *next;
|
fl_t *next;
|
||||||
do {
|
do {
|
||||||
next = curr->next;
|
next = curr->next;
|
||||||
free(curr->description);
|
if (curr->description != NULL) free(curr->description);
|
||||||
free(curr->name);
|
if (curr->name != NULL) free(curr->name);
|
||||||
free(curr->owner);
|
if (curr->owner != NULL) free(curr->owner);
|
||||||
free(curr);
|
free(curr);
|
||||||
} while ((curr = next) != NULL);
|
} while ((curr = next) != NULL);
|
||||||
}
|
}
|
||||||
|
*a_start = NULL;
|
||||||
|
if (a_current != NULL) {
|
||||||
|
*a_current = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_list(session *sess, server_data_t *s_d, i_file_list_t *f_args) {
|
void file_list(session *sess, server_data_t *s_d, i_file_list_t *f_args) {
|
||||||
@@ -54,9 +60,7 @@ void file_list(session *sess, server_data_t *s_d, i_file_list_t *f_args) {
|
|||||||
fl_current = NULL;
|
fl_current = NULL;
|
||||||
|
|
||||||
if (sess->fl_start) {
|
if (sess->fl_start) {
|
||||||
clear_list(fl_start);
|
clear_list(&fl_start, &fl_current);
|
||||||
sess->fl_start = NULL;
|
|
||||||
sess->fl_current = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
args.limit = f_args->limit;
|
args.limit = f_args->limit;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ enum f_actions {
|
|||||||
F_DOWNLOAD,
|
F_DOWNLOAD,
|
||||||
};
|
};
|
||||||
|
|
||||||
void clear_list(fl_t *start);
|
void clear_list(fl_t **a_start, fl_t **a_current);
|
||||||
void file_list(session *, server_data_t *, i_file_list_t *l_args);
|
void file_list(session *, server_data_t *, i_file_list_t *l_args);
|
||||||
int file_send_prepare(session *, char *, server_data_t *);
|
int file_send_prepare(session *, char *, server_data_t *);
|
||||||
int file_receive_prepare(session *sess, char *line, server_data_t *s_d);
|
int file_receive_prepare(session *sess, char *line, server_data_t *s_d);
|
||||||
|
|||||||
@@ -232,8 +232,7 @@ void close_session(session *connections[], int sd) {
|
|||||||
connections[sd]->sd = -1;
|
connections[sd]->sd = -1;
|
||||||
free(connections[sd]->uname);
|
free(connections[sd]->uname);
|
||||||
if (connections[sd]->fl_start != NULL) {
|
if (connections[sd]->fl_start != NULL) {
|
||||||
fl_t *fl_start = connections[sd]->fl_start;
|
clear_list(&(connections[sd]->fl_start), NULL);
|
||||||
clear_list(fl_start);
|
|
||||||
}
|
}
|
||||||
free(connections[sd]);
|
free(connections[sd]);
|
||||||
connections[sd] = NULL;
|
connections[sd] = NULL;
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
add_subdirectory(session)
|
add_subdirectory(session)
|
||||||
add_subdirectory(user)
|
add_subdirectory(user)
|
||||||
|
add_subdirectory(file_p)
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# ---/ clear_list /--------------------------------------------------
|
||||||
|
|
||||||
|
add_executable(clear_list
|
||||||
|
clear_list.c
|
||||||
|
utils.c
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(clear_list
|
||||||
|
cmocka
|
||||||
|
file_p
|
||||||
|
main
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_options(clear_list PRIVATE -Wl,--wrap=free)
|
||||||
|
|
||||||
|
add_test(NAME file_p_clear_list_test_1 COMMAND clear_list)
|
||||||
|
|
||||||
|
# ---/ file_list /---------------------------------------------------
|
||||||
|
|
||||||
|
add_executable(file_list
|
||||||
|
file_list.c
|
||||||
|
utils.c
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(file_list
|
||||||
|
cmocka
|
||||||
|
file_p
|
||||||
|
main
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_options(file_list PRIVATE -Wl,--wrap=db_get_files_data)
|
||||||
|
|
||||||
|
add_test(NAME file_p_file_list_test_1 COMMAND file_list)
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
#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 <file_p.h>
|
||||||
|
#include <main.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
fl_t fl_sample = {
|
||||||
|
.name = "test_file", .description = "Hello test file", .owner = "user1234"};
|
||||||
|
|
||||||
|
int free_call_times = 0;
|
||||||
|
|
||||||
|
void __real_free(void *ptr);
|
||||||
|
|
||||||
|
void __wrap_free(void *ptr) {
|
||||||
|
free_call_times++;
|
||||||
|
__real_free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CALL_TIMES 3
|
||||||
|
|
||||||
|
void test__clear_list__normal(void **state) {
|
||||||
|
fl_t *fl_st, *fl_cur;
|
||||||
|
free_call_times = 0;
|
||||||
|
fill_list_with_samples(&fl_sample, &fl_st, &fl_cur, CALL_TIMES);
|
||||||
|
|
||||||
|
clear_list(&fl_st, &fl_cur);
|
||||||
|
assert_ptr_equal(fl_st, NULL);
|
||||||
|
assert_ptr_equal(fl_cur, NULL);
|
||||||
|
assert_int_equal(free_call_times, 4 * CALL_TIMES);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_list__with__part_null(void **state) {
|
||||||
|
fl_t *fl_st, *fl_cur;
|
||||||
|
free_call_times = 0;
|
||||||
|
fill_list_with_samples(&fl_sample, &fl_st, &fl_cur, CALL_TIMES);
|
||||||
|
|
||||||
|
fl_cur->description = NULL;
|
||||||
|
fl_cur->name = NULL;
|
||||||
|
fl_cur->owner = NULL;
|
||||||
|
|
||||||
|
clear_list(&fl_st, &fl_cur);
|
||||||
|
assert_ptr_equal(fl_st, NULL);
|
||||||
|
assert_ptr_equal(fl_cur, NULL);
|
||||||
|
assert_int_equal(free_call_times, 4 * CALL_TIMES - 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_list__with__null_1(void **state) {
|
||||||
|
fl_t *fl_st = NULL, *fl_cur = NULL;
|
||||||
|
free_call_times = 0;
|
||||||
|
|
||||||
|
clear_list(&fl_st, &fl_cur);
|
||||||
|
assert_ptr_equal(fl_st, NULL);
|
||||||
|
assert_ptr_equal(fl_cur, NULL);
|
||||||
|
assert_int_equal(free_call_times, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_list__with__cur_null(void **state) {
|
||||||
|
fl_t *fl_st = NULL, *fl_cur = NULL;
|
||||||
|
fill_list_with_samples(&fl_sample, &fl_st, &fl_cur, CALL_TIMES);
|
||||||
|
|
||||||
|
clear_list(&fl_st, NULL);
|
||||||
|
assert_ptr_equal(fl_st, NULL);
|
||||||
|
assert_ptr_not_equal(fl_cur, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_list__with__large(void **state) {
|
||||||
|
fl_t *fl_st = NULL, *fl_cur = NULL;
|
||||||
|
fill_list_with_samples(&fl_sample, &fl_st, &fl_cur, 1024);
|
||||||
|
free_call_times = 0;
|
||||||
|
|
||||||
|
clear_list(&fl_st, &fl_cur);
|
||||||
|
assert_ptr_equal(fl_st, NULL);
|
||||||
|
assert_ptr_equal(fl_cur, NULL);
|
||||||
|
assert_int_equal(free_call_times, 1024 * 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
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__clear_list__normal),
|
||||||
|
cmocka_unit_test(test__clear_list__with__part_null),
|
||||||
|
cmocka_unit_test(test__clear_list__with__null_1),
|
||||||
|
cmocka_unit_test(test__clear_list__with__cur_null),
|
||||||
|
cmocka_unit_test(test__clear_list__with__large),
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#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>
|
||||||
|
/* TODO: complete */
|
||||||
|
fl_t fl_sample = {
|
||||||
|
.name = "test_file", .description = "Hello test file", .owner = "user1234"};
|
||||||
|
|
||||||
|
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, 123);
|
||||||
|
*full_count = 123;
|
||||||
|
return 123;
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__file_list(void **state) {}
|
||||||
|
|
||||||
|
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_list),
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include "../../src/main.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (fl_st == NULL) return;
|
||||||
|
|
||||||
|
for (int i = 0; i < amount; i++) {
|
||||||
|
if (i == 0) {
|
||||||
|
_fl_st = _fl_cur = malloc(sizeof(fl_t));
|
||||||
|
memcpy(_fl_st, fl_sample, sizeof(fl_t));
|
||||||
|
} else {
|
||||||
|
_fl_cur->next = malloc(sizeof(fl_t));
|
||||||
|
memcpy(_fl_cur->next, fl_sample, sizeof(fl_t));
|
||||||
|
_fl_cur = _fl_cur->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
_fl_cur->name = malloc(sizeof(char) * (strlen(fl_sample->name) + 32));
|
||||||
|
_fl_cur->description =
|
||||||
|
malloc(sizeof(char) * strlen(fl_sample->description) + 1);
|
||||||
|
_fl_cur->owner = malloc(sizeof(char) * strlen(fl_sample->owner) + 1);
|
||||||
|
|
||||||
|
sprintf(_fl_cur->name, "%s_%d", fl_sample->name, i + 1);
|
||||||
|
memcpy(_fl_cur->description, fl_sample->description,
|
||||||
|
sizeof(char) * strlen(fl_sample->description) + 1);
|
||||||
|
memcpy(_fl_cur->owner, fl_sample->owner,
|
||||||
|
sizeof(char) * strlen(fl_sample->owner) + 1);
|
||||||
|
}
|
||||||
|
*fl_st = _fl_st;
|
||||||
|
if (fl_cur != NULL) {
|
||||||
|
*fl_cur = _fl_cur;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
#include "../../src/main.h"
|
||||||
|
|
||||||
|
void fill_list_with_samples(fl_t *fl_samples, fl_t **fl_st,
|
||||||
|
fl_t **fl_cur, int amount);
|
||||||
+23
-2
@@ -21,7 +21,7 @@ void __wrap_session_send_string(struct session *sess, const char *fmt, ...) {}
|
|||||||
|
|
||||||
/* TODO: divide into separate cases and check messages */
|
/* TODO: divide into separate cases and check messages */
|
||||||
|
|
||||||
void test_create_user(void **state) {
|
void test__create_user__normal(void **state) {
|
||||||
session sess = {};
|
session sess = {};
|
||||||
i_db_user_create p;
|
i_db_user_create p;
|
||||||
int32_t ret = 0;
|
int32_t ret = 0;
|
||||||
@@ -45,11 +45,32 @@ void test_create_user(void **state) {
|
|||||||
assert_int_equal(ret, -4);
|
assert_int_equal(ret, -4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test__create_user__partial(void **state) {
|
||||||
|
session sess = {};
|
||||||
|
i_db_user_create p;
|
||||||
|
int32_t ret = 0;
|
||||||
|
char line[1024];
|
||||||
|
|
||||||
|
/* Less args */
|
||||||
|
sprintf(line, "register %s %s\n", UNAME, PASS);
|
||||||
|
ret = create_user(&sess, line);
|
||||||
|
assert_int_equal(ret, 0);
|
||||||
|
|
||||||
|
/* More args */
|
||||||
|
sprintf(line, "register %s %s %s not_used\n", UNAME, PASS, EMAIL);
|
||||||
|
will_return(__wrap_db_user_create, 321);
|
||||||
|
ret = create_user(&sess, line);
|
||||||
|
assert_int_equal(ret, 321);
|
||||||
|
}
|
||||||
|
|
||||||
int setup(void **state) { return 0; }
|
int setup(void **state) { return 0; }
|
||||||
int tear_down(void **state) { return 0; }
|
int tear_down(void **state) { return 0; }
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
const struct CMUnitTest tests[] = {cmocka_unit_test(test_create_user)};
|
const struct CMUnitTest tests[] = {
|
||||||
|
cmocka_unit_test(test__create_user__normal),
|
||||||
|
cmocka_unit_test(test__create_user__partial),
|
||||||
|
};
|
||||||
|
|
||||||
return cmocka_run_group_tests(tests, setup, tear_down);
|
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user