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