tests for clear_file_from_sess
This commit is contained in:
@@ -285,6 +285,7 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
|
|||||||
|
|
||||||
void clear_file_from_sess(session *s) {
|
void clear_file_from_sess(session *s) {
|
||||||
s_file_t *sf = s->file;
|
s_file_t *sf = s->file;
|
||||||
|
if (sf != NULL) {
|
||||||
if (sf->name != NULL) {
|
if (sf->name != NULL) {
|
||||||
free(sf->name);
|
free(sf->name);
|
||||||
sf->name = NULL;
|
sf->name = NULL;
|
||||||
@@ -304,6 +305,7 @@ void clear_file_from_sess(session *s) {
|
|||||||
free(sf);
|
free(sf);
|
||||||
s->file = NULL;
|
s->file = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* download to the client */
|
/* download to the client */
|
||||||
void file_download(session *sess) {
|
void file_download(session *sess) {
|
||||||
|
|||||||
@@ -67,18 +67,18 @@ add_test(NAME file_p_file_send_prepare_test_1 COMMAND file_send_prepare)
|
|||||||
|
|
||||||
# ---/ file_receive_prepare /-------------------------------------------
|
# ---/ file_receive_prepare /-------------------------------------------
|
||||||
|
|
||||||
add_executable(file_receive_prepare
|
add_executable(clear_file_from_sess
|
||||||
file_receive_prepare.c
|
clear_file_from_sess.c
|
||||||
utils.c
|
utils.c
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(file_receive_prepare
|
target_link_libraries(clear_file_from_sess
|
||||||
cmocka
|
cmocka
|
||||||
file_p
|
file_p
|
||||||
main
|
main
|
||||||
utils
|
utils
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_options(file_receive_prepare PRIVATE -Wl,--wrap=stat,--wrap=statvfs,--wrap=print_log,--wrap=open,--wrap=chdir,--wrap=write,--wrap=mkdir)
|
target_link_options(clear_file_from_sess PRIVATE -Wl,--wrap=free)
|
||||||
|
|
||||||
add_test(NAME file_p_file_receive_prepare_test_1 COMMAND file_receive_prepare)
|
add_test(NAME file_p_clear_file_from_sess_test_1 COMMAND clear_file_from_sess)
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
#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>
|
||||||
|
|
||||||
|
int free_call_times = 0;
|
||||||
|
|
||||||
|
void __real_free(void *ptr);
|
||||||
|
|
||||||
|
void __wrap_free(void *ptr) {
|
||||||
|
free_call_times++;
|
||||||
|
__real_free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_file_from_sess__normal(void **state) {
|
||||||
|
session sess = {};
|
||||||
|
free_call_times = 0;
|
||||||
|
s_file_t *f = malloc(sizeof(s_file_t));
|
||||||
|
f->description = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
f->name = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
f->path = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
|
||||||
|
sess.file = f;
|
||||||
|
|
||||||
|
clear_file_from_sess(&sess);
|
||||||
|
|
||||||
|
assert_ptr_equal(sess.file, NULL);
|
||||||
|
assert_int_equal(free_call_times, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_file_from_sess__no_description(void **state) {
|
||||||
|
session sess = {};
|
||||||
|
free_call_times = 0;
|
||||||
|
s_file_t *f = malloc(sizeof(s_file_t));
|
||||||
|
f->description = NULL;
|
||||||
|
f->name = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
f->path = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
|
||||||
|
sess.file = f;
|
||||||
|
|
||||||
|
clear_file_from_sess(&sess);
|
||||||
|
|
||||||
|
assert_ptr_equal(sess.file, NULL);
|
||||||
|
assert_int_equal(free_call_times, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_file_from_sess__no_name(void **state) {
|
||||||
|
session sess = {};
|
||||||
|
free_call_times = 0;
|
||||||
|
s_file_t *f = malloc(sizeof(s_file_t));
|
||||||
|
f->description = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
f->name = NULL;
|
||||||
|
f->path = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
|
||||||
|
sess.file = f;
|
||||||
|
|
||||||
|
clear_file_from_sess(&sess);
|
||||||
|
|
||||||
|
assert_ptr_equal(sess.file, NULL);
|
||||||
|
assert_int_equal(free_call_times, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_file_from_sess__no_path(void **state) {
|
||||||
|
session sess = {};
|
||||||
|
free_call_times = 0;
|
||||||
|
s_file_t *f = malloc(sizeof(s_file_t));
|
||||||
|
f->description = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
f->name = malloc(sizeof (char) * INBUFSIZE);
|
||||||
|
f->path = NULL;
|
||||||
|
|
||||||
|
sess.file = f;
|
||||||
|
|
||||||
|
clear_file_from_sess(&sess);
|
||||||
|
|
||||||
|
assert_ptr_equal(sess.file, NULL);
|
||||||
|
assert_int_equal(free_call_times, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test__clear_file_from_sess__no_file(void **state) {
|
||||||
|
session sess = {.file = NULL};
|
||||||
|
free_call_times = 0;
|
||||||
|
|
||||||
|
clear_file_from_sess(&sess);
|
||||||
|
|
||||||
|
assert_ptr_equal(sess.file, NULL);
|
||||||
|
assert_int_equal(free_call_times, 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__clear_file_from_sess__normal),
|
||||||
|
cmocka_unit_test(test__clear_file_from_sess__no_description),
|
||||||
|
cmocka_unit_test(test__clear_file_from_sess__no_name),
|
||||||
|
cmocka_unit_test(test__clear_file_from_sess__no_path),
|
||||||
|
cmocka_unit_test(test__clear_file_from_sess__no_file),
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user