file_send/receive_prepare: empty name case

This commit is contained in:
2026-07-01 15:39:17 +03:00
parent 5564c0ac69
commit fef3b0cde6
3 changed files with 79 additions and 21 deletions
+21 -10
View File
@@ -157,20 +157,26 @@ int32_t file_send_prepare(session *sess, char *line, server_data_t *s_d) {
char *name_begin = strchr(line, '[');
if (name_begin == NULL) {
session_send_string(sess,
"Error: can't find starting character \"[\" for the file name");
session_send_string(
sess, "Error: can't find starting character \"[\" for the file name");
clear_file_from_sess(sess);
return -4;
}
name_begin += 1;
char *name_end = strrchr(line, ']');
if (name_end == NULL) {
session_send_string(sess,
"Error: can't find ending character \"]\" for the file name");
if (name_end == NULL) {
session_send_string(
sess, "Error: can't find ending character \"]\" for the file name");
clear_file_from_sess(sess);
return -5;
}
int name_len = name_end - name_begin;
if (name_len == 0) {
session_send_string(
sess, "File name should not be empty");
clear_file_from_sess(sess);
return -6;
}
strncpy(args.name, name_begin, name_len);
args.name[name_len] = 0;
sess->file = db_get_file(&args);
@@ -217,19 +223,26 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
int perm;
char *name_begin = strchr(line, '"');
if (name_begin == NULL) {
session_send_string(sess,
"Error: can't find starting character \" for the file name");
session_send_string(
sess, "Error: can't find starting character \" for the file name");
clear_file_from_sess(sess);
return -5;
}
name_begin += 1;
char *name_end = strrchr(name_begin, '"');
if (name_end == NULL) {
session_send_string(sess, "Error: can't find ending character \" for the file name");
session_send_string(
sess, "Error: can't find ending character \" for the file name");
clear_file_from_sess(sess);
return -6;
}
int name_len = name_end - name_begin;
if (name_len == 0) {
session_send_string(
sess, "File name should not be empty");
clear_file_from_sess(sess);
return -7;
}
sscanf(name_end, "\" %zd %d", &fsize, &perm);
sess->file = malloc(sizeof(s_file_t));
sess->file->name = malloc(sizeof(char) * (name_len + 1));
@@ -299,8 +312,6 @@ int file_receive_prepare(session *sess, char *line, server_data_t *s_d) {
break;
}
mes_len = sprintf(mes, "accept");
sess->fd = file_d;
sess->file->size = fsize;
+36 -5
View File
@@ -98,7 +98,21 @@ void test__file_receive_prepare__regular(void **state) {
}
/* CASE: Name not correct (start) */
void test__file_receive_prepare_name_not_correct_start(void **state) {
void test__file_receive_prepare_name_not_correct_start_1(void **state) {
char line[] = FNAME " " FSIZE_S " 1";
str2snd = "Error: can't find starting 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, -5);
}
/* CASE: Name not correct (start) */
void test__file_receive_prepare_name_not_correct_start_2(void **state) {
char line[] = FNAME "\" " FSIZE_S " 1";
str2snd = "Error: can't find ending character \" for the file name";
session sess = {
@@ -182,7 +196,7 @@ void test__file_receive_prepare__err_dir(void **state) {
dbuf_destroy(&dbuf);
}
/* CASE: Error in the file's name */
/* CASE: Error in the file name */
void test__file_receive_prepare__name_err(void **state) {
dbuf = dbuf_init(INBUFSIZE);
char line[] = "\"" FNAME "\" " FSIZE_S " 1";
@@ -203,8 +217,23 @@ void test__file_receive_prepare__name_err(void **state) {
dbuf_destroy(&dbuf);
}
/* CASE: Error empty name */
void test__file_receive_prepare__name_empty(void **state) {
char line[] = "\"\" " FSIZE_S " 1";
str2snd = "File name should not be empty";
session sess = {
.uname = "user1234",
.sd = 0,
.file = NULL,
};
int res = 0;
res = file_receive_prepare(&sess, line, NULL);
assert_int_equal(res, -7);
assert_ptr_equal(sess.file, NULL);
}
/* CASE: File with such name already exist */
void test__file_receive_prepare__exists(void **state) {
void test__file_receive_prepare__exist(void **state) {
dbuf = dbuf_init(INBUFSIZE);
char line[] = "\"" FNAME "\" " FSIZE_S " 1";
session sess = {
@@ -237,13 +266,15 @@ int tear_down(void **state) { return 0; }
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
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_start_1),
cmocka_unit_test(test__file_receive_prepare_name_not_correct_start_2),
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),
cmocka_unit_test(test__file_receive_prepare__name_err),
cmocka_unit_test(test__file_receive_prepare__exists),
cmocka_unit_test(test__file_receive_prepare__exist),
cmocka_unit_test(test__file_receive_prepare__name_empty),
};
return cmocka_run_group_tests(tests, setup, tear_down);
+22 -6
View File
@@ -46,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__regular(void **state) {
void test__file_send_prepare__regular(void **state) {
s_file_t file = {.name = FNAME, .size = FSIZE, .hash = FHASH, .id = FID};
char line[] = "[" FNAME "]";
session sess = {
@@ -67,7 +67,7 @@ void test__file_receive_prepare__regular(void **state) {
assert_string_equal(file.path, "./storage/00/03390c");
}
void test__file_receive_prepare__name_not_correct_start(void **state) {
void test__file_send_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";
@@ -82,7 +82,7 @@ void test__file_receive_prepare__name_not_correct_start(void **state) {
assert_ptr_equal(sess.file, NULL);
}
void test__file_receive_prepare__name_not_correct_end(void **state) {
void test__file_send_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";
@@ -155,17 +155,33 @@ void test__file_send_prepare__size_not_correct(void **state) {
dbuf_destroy(&dbuf);
}
void test__file_send_prepare__name_empty(void **state) {
char line[] = "[]" ;
str2snd = "File name should not be empty";
session sess = {
.uname = "user1234",
.sd = 0,
.file = NULL,
};
int res = 0;
res = file_send_prepare(&sess, line, NULL);
assert_int_equal(res, -6);
assert_ptr_equal(sess.file, NULL);
}
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__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__regular),
cmocka_unit_test(test__file_send_prepare__name_not_correct_start),
cmocka_unit_test(test__file_send_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),
cmocka_unit_test(test__file_send_prepare__name_empty),
};
return cmocka_run_group_tests(tests, setup, tear_down);