tests for clear_file_from_sess

This commit is contained in:
2026-06-25 14:31:21 +03:00
parent fdea1a03c9
commit e770362817
3 changed files with 135 additions and 22 deletions
+19 -17
View File
@@ -285,24 +285,26 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
void clear_file_from_sess(session *s) {
s_file_t *sf = s->file;
if (sf->name != NULL) {
free(sf->name);
sf->name = NULL;
if (sf != NULL) {
if (sf->name != NULL) {
free(sf->name);
sf->name = NULL;
}
if (sf->path != NULL) {
free(sf->path);
sf->path = NULL;
}
if (sf->description != NULL) {
free(sf->description);
sf->description = NULL;
}
if (s->fd > -1) {
close(s->fd);
s->fd = -1;
}
free(sf);
s->file = NULL;
}
if (sf->path != NULL) {
free(sf->path);
sf->path = NULL;
}
if (sf->description != NULL) {
free(sf->description);
sf->description = NULL;
}
if (s->fd > -1) {
close(s->fd);
s->fd = -1;
}
free(sf);
s->file = NULL;
}
/* download to the client */
+5 -5
View File
@@ -67,18 +67,18 @@ add_test(NAME file_p_file_send_prepare_test_1 COMMAND file_send_prepare)
# ---/ file_receive_prepare /-------------------------------------------
add_executable(file_receive_prepare
file_receive_prepare.c
add_executable(clear_file_from_sess
clear_file_from_sess.c
utils.c
)
target_link_libraries(file_receive_prepare
target_link_libraries(clear_file_from_sess
cmocka
file_p
main
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)
+111
View File
@@ -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);
}