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
+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),