tests: process_client_command

This commit is contained in:
2026-07-01 18:42:31 +03:00
parent fef3b0cde6
commit d3d4b4a4ea
5 changed files with 159 additions and 14 deletions
-4
View File
@@ -1,9 +1,5 @@
## Tasks
# Testing
- [x] file_receive_prepare / file_send_prepare more reliable line processing
- [ ] test empty names
# Development plans
- [ ] Remove file when error while saving the data in DB
+19 -7
View File
@@ -2,18 +2,15 @@
/* Copyright (c) 2026 Oleksandr Zhylin */
#include <client.h>
#include <fcntl.h>
#include <file_p.h>
#include <main.h>
#include <session.h>
#include <utils.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define DIF_LIMIT 15
#define DIF_PAGE_NUM 1
#include <utils.h>
void process_client_command(char *line, session *sess, server_data_t *s_d) {
char arg_1[32];
@@ -23,7 +20,15 @@ void process_client_command(char *line, session *sess, server_data_t *s_d) {
fl_args.name = NULL;
int32_t ret = 0;
sscanf(line, "%s %s", arg_1, arg_2);
ret = sscanf(line, "%s %s", arg_1, arg_2);
if (ret == 1) {
if (!strcmp(arg_1, "file")) {
session_send_string(sess,
"Available commands: list, upload, download, search");
return;
}
}
if (!strcmp(arg_1, "file")) {
/* LIST */
@@ -60,11 +65,18 @@ void process_client_command(char *line, session *sess, server_data_t *s_d) {
if (!strcmp(arg_2, "search")) {
char *s_type = arg_1; /* since we don't need the contents of arg_1 and
arg_2 variables */
char *s_val = arg_2;
char *s_val = NULL;
ret = sscanf(line, "%*s %*s %s %s %u %u", s_type, s_val, &(fl_args.limit),
&(fl_args.page));
switch (ret) {
case 0:
case -1:
session_send_string(sess, "The search criteria is not specified!");
return;
case 1:
session_send_string(sess, "The file name is not specified!");
return;
case 2:
fl_args.limit = DIF_LIMIT;
case 3:
+3
View File
@@ -4,6 +4,9 @@
#ifndef CLIENT_H
#define CLIENT_H
#define DIF_LIMIT 15
#define DIF_PAGE_NUM 1
#include <main.h>
void process_client_command(char *line, session *sess, server_data_t *s_d);
void download_confirm(char *line, session *sess, server_data_t *s_d);
+1 -1
View File
@@ -12,6 +12,6 @@ target_link_libraries(process_client_command
test_utils
)
target_link_options(process_client_command PRIVATE -Wl,--wrap=file_list,--wrap=file_send_prepare,--wrap=file_receive_prepare)
target_link_options(process_client_command PRIVATE -Wl,--wrap=file_list,--wrap=file_send_prepare,--wrap=file_receive_prepare,--wrap=session_send_string)
add_test(NAME client_process_client_command_test_1 COMMAND process_client_command)
+136 -2
View File
@@ -13,16 +13,31 @@
#include <string.h>
int file_list_calls = 0;
int _limit = 15;
int _page = 2;
int _limit = DIF_LIMIT;
int _page = DIF_PAGE_NUM;
char *_name = NULL;
char _line[128];
char file_name[] = "hello_file";
int file_size = 121212;
int file_perm = 1;
char *str2snd = NULL;
void __wrap_session_send_string(struct session *sess, const char *fmt, ...) {
if (str2snd != NULL) {
assert_string_equal(fmt, str2snd);
str2snd = NULL;
}
}
void __wrap_file_list(session *sess, i_file_list_t *f_args) {
assert_int_equal(_limit, f_args->limit);
if (_limit != DIF_LIMIT) _limit = DIF_LIMIT;
if (_page != DIF_PAGE_NUM) _page = DIF_PAGE_NUM;
assert_int_equal(_page, f_args->page);
if (_name != NULL) {
assert_string_equal(_name, f_args->name);
_name = NULL;
}
file_list_calls++;
}
@@ -71,6 +86,116 @@ void test__process_client_command__file_upload_fail(void **state) {
assert_int_equal(sess.state, OP_WAIT);
}
void test__process_client_command__file_download(void **state) {
session sess = {.state = OP_WAIT};
will_return(__wrap_file_send_prepare, 0);
sprintf(_line, "file download \"%s\" %d %d", file_name, file_size, file_perm);
process_client_command(_line, &sess, NULL);
assert_int_equal(sess.state, OP_DOWNLOAD);
}
void test__process_client_command__file_download_fail(void **state) {
session sess = {.state = OP_WAIT};
will_return(__wrap_file_send_prepare, 1);
sprintf(_line, "file download \"%s\" %d %d", file_name, file_size, file_perm);
process_client_command(_line, &sess, NULL);
assert_int_equal(file_list_calls, 0);
assert_int_equal(sess.state, OP_WAIT);
}
void test__process_client_command__file_search(void **state) {
session sess = {.state = OP_WAIT};
_name = "name";
_limit = 20;
_page = 2;
sprintf(_line, "file search %s \"%s\" %d %d", _name, file_name, _limit, _page);
process_client_command(_line, &sess, NULL);
assert_int_equal(file_list_calls, 0);
assert_int_equal(sess.state, OP_WAIT);
}
void test__process_client_command__file_search_no_page(void **state) {
session sess = {.state = OP_WAIT};
_name = "name";
_limit = 20;
sprintf(_line, "file search %s \"%s\" %d", _name, file_name, _limit);
process_client_command(_line, &sess, NULL);
assert_int_equal(file_list_calls, 0);
assert_int_equal(sess.state, OP_WAIT);
}
void test__process_client_command__file_search_no_limit(void **state) {
session sess = {.state = OP_WAIT};
_name = "name";
sprintf(_line, "file search %s \"%s\"", _name, file_name);
process_client_command(_line, &sess, NULL);
assert_int_equal(file_list_calls, 0);
assert_int_equal(sess.state, OP_WAIT);
}
void test__process_client_command__file_search_no_name(void **state) {
session sess = {.state = OP_WAIT};
_name = "name";
sprintf(_line, "file search name");
str2snd = "The file name is not specified!";
process_client_command(_line, &sess, NULL);
assert_int_equal(file_list_calls, 0);
assert_int_equal(sess.state, OP_WAIT);
}
void test__process_client_command__file_search_no_type(void **state) {
session sess = {.state = OP_WAIT};
sprintf(_line, "file search");
str2snd = "The search criteria is not specified!";
process_client_command(_line, &sess, NULL);
assert_int_equal(file_list_calls, 0);
assert_int_equal(sess.state, OP_WAIT);
}
void test__process_client_command__file(void **state) {
session sess = {.state = OP_WAIT};
sprintf(_line, "file");
str2snd = "Available commands: list, upload, download, search";
process_client_command(_line, &sess, NULL);
assert_int_equal(file_list_calls, 0);
assert_int_equal(sess.state, OP_WAIT);
}
void test__process_client_command__exit(void **state) {
session sess = {.state = OP_WAIT};
str2snd = "OK. Bye!\n";
process_client_command("exit", &sess, NULL);
assert_int_equal(file_list_calls, 0);
assert_int_equal(sess.state, ERR);
assert_int_equal(sess.reason, EXIT);
}
int setup(void **state) { return 0; }
int tear_down(void **state) { return 0; }
@@ -79,6 +204,15 @@ int main(int argc, char **argv) {
cmocka_unit_test(test__process_client_command__file_list),
cmocka_unit_test(test__process_client_command__file_upload),
cmocka_unit_test(test__process_client_command__file_upload_fail),
cmocka_unit_test(test__process_client_command__file_download),
cmocka_unit_test(test__process_client_command__file_download_fail),
cmocka_unit_test(test__process_client_command__file_search),
cmocka_unit_test(test__process_client_command__file_search_no_page),
cmocka_unit_test(test__process_client_command__file_search_no_limit),
cmocka_unit_test(test__process_client_command__file_search_no_name),
cmocka_unit_test(test__process_client_command__file_search_no_type),
cmocka_unit_test(test__process_client_command__file),
cmocka_unit_test(test__process_client_command__exit),
};
return cmocka_run_group_tests(tests, setup, tear_down);