testing: file_receive_prepare / file_send_prepare more reliable line processing

This commit is contained in:
2026-06-30 17:09:12 +03:00
parent fd2b54c3c5
commit 5564c0ac69
5 changed files with 142 additions and 22 deletions
+19 -1
View File
@@ -61,12 +61,30 @@ target_link_libraries(file_send_prepare
test_utils
)
target_link_options(file_send_prepare PRIVATE -Wl,--wrap=write,--wrap=print_log,--wrap=lseek,--wrap=open,--wrap=db_get_file)
target_link_options(file_send_prepare PRIVATE -Wl,--wrap=write,--wrap=print_log,--wrap=lseek,--wrap=open,--wrap=db_get_file,--wrap=session_send_string)
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
)
target_link_libraries(file_receive_prepare
cmocka
file_p
main
utils
test_utils
)
target_link_options(file_receive_prepare PRIVATE -Wl,--wrap=statvfs,--wrap=stat,--wrap=mkdir,--wrap=chdir,--wrap=write,--wrap=open,--wrap=session_send_string)
add_test(NAME file_p_file_receive_prepare_test_1 COMMAND file_receive_prepare)
# ---/ clear_file_from_sess /-------------------------------------------
add_executable(clear_file_from_sess
clear_file_from_sess.c
)
+46 -9
View File
@@ -10,14 +10,13 @@
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <file_p.h>
#include <main.h>
#include <string.h>
#include <sys/cdefs.h>
#include <test_utils.h>
#include <unistd.h>
#include <utils.h>
#include <test_utils.h>
#define FSIZE 10233
#define FSIZE_S "10233"
@@ -44,6 +43,14 @@ dbuf_t *dbuf;
size_t fs_available = 10;
size_t fs_namemax = 255;
int _errno = 0;
char *str2snd = NULL;
void __wrap_session_send_string(struct session *sess, const char *fmt, ...) {
if (str2snd != NULL) {
assert_string_equal(fmt, str2snd);
str2snd = NULL;
}
}
int __wrap_statvfs(const char *__restrict __file,
struct statvfs *__restrict __buf) {
@@ -72,7 +79,7 @@ int __wrap_open(const char *__file, int __oflag, ...) {
}
/* CASE: Success */
void test__file_receive_prepare(void **state) {
void test__file_receive_prepare__regular(void **state) {
dbuf = dbuf_init(INBUFSIZE);
char line[] = "\"" FNAME "\" " FSIZE_S " 1";
session sess = {
@@ -90,6 +97,34 @@ void test__file_receive_prepare(void **state) {
dbuf_destroy(&dbuf);
}
/* CASE: Name not correct (start) */
void test__file_receive_prepare_name_not_correct_start(void **state) {
char line[] = FNAME "\" " FSIZE_S " 1";
str2snd = "Error: can't find ending character \" for the file name";
session sess = {
.uname = "user1234",
.sd = 0,
.file = NULL,
};
int res = 0;
res = file_receive_prepare(&sess, line, NULL);
assert_int_equal(res, -6);
}
/* CASE: Name not correct (end) */
void test__file_receive_prepare_name_not_correct_end(void **state) {
char line[] = "\"FNAME " FSIZE_S " 1";
str2snd = "Error: can't find ending character \" for the file name";
session sess = {
.uname = "user1234",
.sd = 0,
.file = NULL,
};
int res = 0;
res = file_receive_prepare(&sess, line, NULL);
assert_int_equal(res, -6);
}
/* CASE: File name is too long */
void test__file_receive_prepare__name_too_long(void **state) {
dbuf = dbuf_init(INBUFSIZE);
@@ -100,10 +135,10 @@ void test__file_receive_prepare__name_too_long(void **state) {
.file = NULL,
};
int res = 0;
str2snd = "file name is too long\n";
res = file_receive_prepare(&sess, line, NULL);
assert_int_equal(res, -1);
assert_string_equal("file name is too long\n", dbuf->ptr);
assert_ptr_equal(sess.file, NULL);
dbuf_destroy(&dbuf);
}
@@ -120,10 +155,10 @@ void test__file_receive_prepare__no_space(void **state) {
.file = NULL,
};
int res = 0;
str2snd = "There is no space for such size! (%ld)\n";
res = file_receive_prepare(&sess, line, NULL);
assert_int_equal(res, -2);
assert_string_equal("There is no space for such size! (20000)\n", dbuf->ptr);
assert_ptr_equal(sess.file, NULL);
dbuf_destroy(&dbuf);
}
@@ -138,12 +173,12 @@ void test__file_receive_prepare__err_dir(void **state) {
.file = NULL,
};
int res = 0;
str2snd = ERR_FSV;
will_return(__wrap_mkdir, 1);
will_return(__wrap_stat, 1);
res = file_receive_prepare(&sess, line, NULL);
assert_int_equal(res, -4);
assert_ptr_equal(sess.file, NULL);
assert_string_equal(ERR_FSV, dbuf->ptr);
dbuf_destroy(&dbuf);
}
@@ -157,6 +192,7 @@ void test__file_receive_prepare__name_err(void **state) {
.file = NULL,
};
int res = 0;
str2snd = ERR_FSV;
will_return(__wrap_stat, 1);
will_return(__wrap_mkdir, 0);
will_return(__wrap_open, -1);
@@ -164,7 +200,6 @@ void test__file_receive_prepare__name_err(void **state) {
res = file_receive_prepare(&sess, line, NULL);
assert_int_equal(res, -3);
assert_ptr_equal(sess.file, NULL);
assert_string_equal(ERR_FSV, dbuf->ptr);
dbuf_destroy(&dbuf);
}
@@ -188,11 +223,11 @@ void test__file_receive_prepare__exists(void **state) {
will_return(__wrap_stat, 1);
will_return(__wrap_mkdir, 0);
will_return(__wrap_open, 5);
str2snd = "accept";
res = file_receive_prepare(&sess, line, NULL);
assert_int_equal(res, 0);
assert_ptr_not_equal(sess.file, NULL);
assert_string_equal("accept", dbuf->ptr);
dbuf_destroy(&dbuf);
}
@@ -201,7 +236,9 @@ int tear_down(void **state) { return 0; }
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test__file_receive_prepare),
cmocka_unit_test(test__file_receive_prepare__regular),
cmocka_unit_test(test__file_receive_prepare_name_not_correct_start),
cmocka_unit_test(test__file_receive_prepare_name_not_correct_end),
cmocka_unit_test(test__file_receive_prepare__name_too_long),
cmocka_unit_test(test__file_receive_prepare__no_space),
cmocka_unit_test(test__file_receive_prepare__err_dir),
+40 -2
View File
@@ -15,6 +15,7 @@
#include <unistd.h>
#include <utils.h>
#include <test_utils.h>
char *str2snd = NULL;
dbuf_t *dbuf;
@@ -24,6 +25,11 @@ dbuf_t *dbuf;
#define FID 19
#define FID_S "19"
void __wrap_session_send_string(struct session *sess, const char *fmt, ...) {
assert_string_equal(fmt, str2snd);
str2snd = NULL;
}
ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur {
dbuf_write(__buf, __n, &dbuf);
return __n;
@@ -40,7 +46,7 @@ s_file_t *__wrap_db_get_file(i_get_file_db *arg) {
int __wrap_open(const char *__file, int __oflag, ...) { return mock_type(int); }
void test__file_receive_prepare(void **state) {
void test__file_receive_prepare__regular(void **state) {
s_file_t file = {.name = FNAME, .size = FSIZE, .hash = FHASH, .id = FID};
char line[] = "[" FNAME "]";
session sess = {
@@ -61,6 +67,36 @@ void test__file_receive_prepare(void **state) {
assert_string_equal(file.path, "./storage/00/03390c");
}
void test__file_receive_prepare__name_not_correct_start(void **state) {
s_file_t file = {.name = FNAME, .size = FSIZE, .hash = FHASH, .id = FID};
char line[] = FNAME;
str2snd = "Error: can't find starting character \"[\" for the file name";
session sess = {
.uname = "user1234",
.sd = 0,
.file = NULL,
};
int res = 0;
res = file_send_prepare(&sess, line, NULL);
assert_int_equal(res, -4);
assert_ptr_equal(sess.file, NULL);
}
void test__file_receive_prepare__name_not_correct_end(void **state) {
s_file_t file = {.name = FNAME, .size = FSIZE, .hash = FHASH, .id = FID};
char line[] = "[" FNAME;
str2snd = "Error: can't find ending character \"]\" for the file name";
session sess = {
.uname = "user1234",
.sd = 0,
.file = NULL,
};
int res = 0;
res = file_send_prepare(&sess, line, NULL);
assert_int_equal(res, -5);
assert_ptr_equal(sess.file, NULL);
}
void test__file_send_prepare__db_no_file(void **state) {
char line[] = "[" FNAME "]";
session sess = {
@@ -124,7 +160,9 @@ int tear_down(void **state) { return 0; }
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test__file_receive_prepare),
cmocka_unit_test(test__file_receive_prepare__regular),
cmocka_unit_test(test__file_receive_prepare__name_not_correct_start),
cmocka_unit_test(test__file_receive_prepare__name_not_correct_end),
cmocka_unit_test(test__file_send_prepare__no_open),
cmocka_unit_test(test__file_send_prepare__db_no_file),
cmocka_unit_test(test__file_send_prepare__size_not_correct),