From 0040c07f817477b3c7c0480c76cc37bdc4bd02e5 Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Tue, 16 Jun 2026 18:33:20 +0300 Subject: [PATCH] tests: user module covered up --- src/components/user/user.c | 5 +-- src/components/user/user.h | 2 +- test/user/CMakeLists.txt | 44 +++++++++++++++++++-- test/user/login.c | 72 +++++++++++++++++++++++++++++++++++ test/user/process_user_name.c | 69 +++++++++++++++++++++++++++++++++ 5 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 test/user/login.c create mode 100644 test/user/process_user_name.c diff --git a/src/components/user/user.c b/src/components/user/user.c index d286c42..a254b2a 100644 --- a/src/components/user/user.c +++ b/src/components/user/user.c @@ -9,7 +9,7 @@ #include #include -int32_t create_user(session *sess, char *line) { +__attribute__((weak)) int32_t create_user(session *sess, char *line) { i_db_user_create p; int32_t res; if (sscanf(line, "register %s %s %s", p.uname, p.pass, p.email) == 3) { @@ -74,8 +74,7 @@ int login(session *sess, char *pass) { sess->state = OP_WAIT; sess->privileges = (char)atoi(&response.privileges); sess->uid = response.uid; - sprintf(tmp_string, "Welcome, %s\n", sess->uname); - session_send_string(sess, tmp_string); + session_send_string(sess, "Welcome, %s\n", sess->uname); return 0; } else { sprintf(tmp_string, "login_again>\n"); diff --git a/src/components/user/user.h b/src/components/user/user.h index a94d3a3..930b09d 100644 --- a/src/components/user/user.h +++ b/src/components/user/user.h @@ -6,7 +6,7 @@ #include "main.h" int login(session *sess, char *pass); -int process_user_name(char *uname, session *sess); +int process_user_name(char *line, session *sess); /* Testing */ int32_t create_user(session *sess, char *line); diff --git a/test/user/CMakeLists.txt b/test/user/CMakeLists.txt index 8a04e92..3f068a9 100644 --- a/test/user/CMakeLists.txt +++ b/test/user/CMakeLists.txt @@ -1,19 +1,19 @@ # ---/ create_user /------------------------------------------------- -add_executable(create_user +add_executable(test_create_user create_user.c ) -target_link_libraries(create_user +target_link_libraries(test_create_user cmocka user main db ) -target_link_options(create_user PRIVATE -Wl,--wrap=db_user_create,--wrap=session_send_string) +target_link_options(test_create_user PRIVATE -Wl,--wrap=db_user_create,--wrap=session_send_string) -add_test(NAME user_test_1 COMMAND create_user) +add_test(NAME user_test_1 COMMAND test_create_user) # ---/ db_user_create /------------------------------------------------- @@ -31,3 +31,39 @@ target_link_libraries(db_user_create target_link_options(db_user_create PRIVATE -Wl,--wrap=db_user_create,--wrap=session_send_string) add_test(NAME user_test_2 COMMAND db_user_create) + +# ---/ process_user_name /------------------------------------------- + +add_executable(process_user_name + process_user_name.c +) + +target_link_libraries(process_user_name + cmocka + user + main +) + +target_link_options(process_user_name PRIVATE + -Wl,--wrap=session_send_string +) + +add_test(NAME user_test_3 COMMAND process_user_name) + +# ---/ login /------------------------------------------------------- + +add_executable(login + login.c +) + +target_link_libraries(login + cmocka + user + main +) + +target_link_options(login PRIVATE + -Wl,--wrap=session_send_string,--wrap=db_user_auth +) + +add_test(NAME user_test_4 COMMAND login) \ No newline at end of file diff --git a/test/user/login.c b/test/user/login.c new file mode 100644 index 0000000..04df8e2 --- /dev/null +++ b/test/user/login.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define UNAME "user1234" +#define PASS "pass1234" +#define UID 1234 +int id = 0; +char ret_buf[1024]; + +int32_t __wrap_db_user_auth(i_auth_t *c, o_auth_t *r) { + r->uid = id; + return id; +} + +void __wrap_session_send_string(struct session *sess, const char *fmt, ...) { + char buf[INBUFSIZE]; + size_t b_len = 0; + va_list args; + va_start(args, fmt); + b_len = vsprintf(buf, fmt, args); + strcpy(ret_buf, buf); + va_end(args); +} + +void test_create_login_success(void **state) { + session sess = {.uname = UNAME}; + int ret = 0; + id = UID; + ret = login(&sess, PASS); + assert_int_equal(ret, 0); + assert_int_equal(sess.uid, UID); + // assert_int_not_equal(sess.privileges, 0); + assert_int_equal(sess.state, OP_WAIT); + + char welcome_mes[sizeof "Welcome, " + sizeof UNAME + 5]; + sprintf(welcome_mes, "Welcome, %s\n", UNAME); + assert_string_equal(welcome_mes, ret_buf); +} + +void test_create_login_fail(void **state) { + session sess = {.uname = UNAME}; + int ret = 0; + id = 0; + ret = login(&sess, PASS); + assert_int_equal(ret, 1); + assert_int_equal(sess.uid, 0); + // assert_int_not_equal(sess.privileges, 0); + assert_int_equal(sess.state, OP_LOGIN_USR); + assert_string_equal("login_again>\n", ret_buf); +} + +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_create_login_success), + cmocka_unit_test(test_create_login_fail) + }; + + return cmocka_run_group_tests(tests, setup, tear_down); +} diff --git a/test/user/process_user_name.c b/test/user/process_user_name.c new file mode 100644 index 0000000..186032e --- /dev/null +++ b/test/user/process_user_name.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +static char user_name[] = "user1234"; +char *str2snd; + +void __wrap_session_send_string(struct session *sess, const char *fmt, ...) { + assert_string_equal(fmt, str2snd); +} + +int32_t create_user(session *sess, char *line) { + return 321; +} + +void test__process_user_name__user_name(void **state) { + session sess = {.uname = NULL, .state = OP_WAIT}; + str2snd = "password> "; + int ret = process_user_name(user_name, &sess); + assert_int_equal(ret, 0); + assert_string_equal(sess.uname, user_name); + assert_int_equal(sess.state, OP_LOGIN_PSS); +} + +void test__process_user_name__exit(void **state) { + session sess = {.uname = NULL, .state = OP_WAIT}; + str2snd = "Bye!"; + int ret = process_user_name("exit", &sess); + assert_int_equal(ret, 1); + assert_int_equal(sess.state, ERR); + assert_int_equal(sess.reason, EXIT); +} + +void test__process_user_name__register(void **state) { + session sess = {.uname = NULL, .state = OP_WAIT}; + str2snd = "ok\n"; + int ret = process_user_name("register", &sess); + assert_int_equal(ret, 321); +} + +void test__process_user_name__anonymous(void **state) { + session sess = {.uname = NULL, .state = OP_WAIT}; + str2snd = "Welcome, Anonymous!"; + int ret = process_user_name("anonymous", &sess); + assert_int_equal(ret, 3); + assert_string_equal(sess.uname, "anonymous"); + assert_int_equal(sess.state, OP_WAIT); +} + +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__process_user_name__user_name), + cmocka_unit_test(test__process_user_name__exit), + cmocka_unit_test(test__process_user_name__register), + cmocka_unit_test(test__process_user_name__anonymous), + }; + + return cmocka_run_group_tests(tests, setup, tear_down); +}