tests: user module covered up
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int32_t create_user(session *sess, char *line) {
|
__attribute__((weak)) int32_t create_user(session *sess, char *line) {
|
||||||
i_db_user_create p;
|
i_db_user_create p;
|
||||||
int32_t res;
|
int32_t res;
|
||||||
if (sscanf(line, "register %s %s %s", p.uname, p.pass, p.email) == 3) {
|
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->state = OP_WAIT;
|
||||||
sess->privileges = (char)atoi(&response.privileges);
|
sess->privileges = (char)atoi(&response.privileges);
|
||||||
sess->uid = response.uid;
|
sess->uid = response.uid;
|
||||||
sprintf(tmp_string, "Welcome, %s\n", sess->uname);
|
session_send_string(sess, "Welcome, %s\n", sess->uname);
|
||||||
session_send_string(sess, tmp_string);
|
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
sprintf(tmp_string, "login_again>\n");
|
sprintf(tmp_string, "login_again>\n");
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
int login(session *sess, char *pass);
|
int login(session *sess, char *pass);
|
||||||
int process_user_name(char *uname, session *sess);
|
int process_user_name(char *line, session *sess);
|
||||||
|
|
||||||
/* Testing */
|
/* Testing */
|
||||||
int32_t create_user(session *sess, char *line);
|
int32_t create_user(session *sess, char *line);
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
# ---/ create_user /-------------------------------------------------
|
# ---/ create_user /-------------------------------------------------
|
||||||
|
|
||||||
add_executable(create_user
|
add_executable(test_create_user
|
||||||
create_user.c
|
create_user.c
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(create_user
|
target_link_libraries(test_create_user
|
||||||
cmocka
|
cmocka
|
||||||
user
|
user
|
||||||
main
|
main
|
||||||
db
|
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 /-------------------------------------------------
|
# ---/ 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)
|
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)
|
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)
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
#include <cmocka.h>
|
||||||
|
#include <db/db.h>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <main.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <user.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
#include <cmocka.h>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <main.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <user.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user