testing: prepare_start
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <unistd.h>
|
||||
#include <utils.h>
|
||||
#include <test_utils.h>
|
||||
|
||||
char *str2snd = NULL;
|
||||
|
||||
dbuf_t *dbuf;
|
||||
|
||||
@@ -17,4 +17,25 @@ target_link_libraries(start_server
|
||||
|
||||
target_link_options(start_server PRIVATE -Wl,--wrap=socket,--wrap=setsockopt,--wrap=bind,--wrap=listen,--wrap=exit,--wrap=print_log)
|
||||
|
||||
add_test(NAME client_start_server_test_1 COMMAND start_server)
|
||||
add_test(NAME client_start_server_test_1 COMMAND start_server)
|
||||
|
||||
# ---/ prepare_start /--------------------------------------------------
|
||||
|
||||
add_executable(prepare_start
|
||||
prepare_start.c
|
||||
)
|
||||
|
||||
target_link_libraries(prepare_start
|
||||
cmocka
|
||||
client
|
||||
file_p
|
||||
session
|
||||
utils
|
||||
main
|
||||
server
|
||||
test_utils
|
||||
)
|
||||
|
||||
target_link_options(prepare_start PRIVATE -Wl,--wrap=print_log,--wrap=print_log,--wrap=chdir,--wrap=mkdir,--wrap=exit,--wrap=directory_exists)
|
||||
|
||||
add_test(NAME client_prepare_start_test_1 COMMAND prepare_start)
|
||||
@@ -0,0 +1,84 @@
|
||||
#include <main.h>
|
||||
#include <cmocka.h>
|
||||
#include <server.h>
|
||||
|
||||
/* TODO: finish */
|
||||
|
||||
int _errno = 0;
|
||||
int _exit_code = 0;
|
||||
extern void *print_str_arr;
|
||||
|
||||
void __wrap_exit(int __status) { _exit_code = __status; }
|
||||
|
||||
int __wrap_chdir(const char *__path) {
|
||||
return mock_type(int);
|
||||
}
|
||||
|
||||
int __wrap_mkdir(const char *__path, __mode_t __mode) {
|
||||
return mock_type(int);
|
||||
}
|
||||
|
||||
int __wrap_directory_exists(const char *path) {
|
||||
return mock_type(int);
|
||||
}
|
||||
|
||||
void test__prepare_start__success(void **state) {
|
||||
int argc = 2;
|
||||
|
||||
char *argv[] = {
|
||||
"test_prog",
|
||||
"w_dir"
|
||||
};
|
||||
|
||||
will_return(__wrap_chdir, 0);
|
||||
will_return(__wrap_directory_exists, 1);
|
||||
|
||||
prepare_start(argc,argv);
|
||||
assert_int_equal(_exit_code, 0);
|
||||
}
|
||||
|
||||
void test__prepare_start__success_mkdir(void **state) {
|
||||
int argc = 2;
|
||||
|
||||
char *argv[] = {
|
||||
"test_prog",
|
||||
"w_dir"
|
||||
};
|
||||
|
||||
will_return(__wrap_chdir, 0);
|
||||
will_return(__wrap_directory_exists, 0);
|
||||
will_return(__wrap_mkdir, 1);
|
||||
|
||||
prepare_start(argc,argv);
|
||||
assert_int_equal(_exit_code, 0);
|
||||
}
|
||||
|
||||
void test__prepare_start__success_no_arg(void **state) {
|
||||
int argc = 1;
|
||||
|
||||
char *argv[] = {
|
||||
"test_prog"
|
||||
};
|
||||
|
||||
will_return(__wrap_chdir, 0);
|
||||
will_return(__wrap_directory_exists, 0);
|
||||
will_return(__wrap_mkdir, 1);
|
||||
|
||||
prepare_start(argc,argv);
|
||||
assert_int_equal(_exit_code, 1);
|
||||
}
|
||||
|
||||
int setup(void **state) {
|
||||
print_str_arr = NULL;
|
||||
return 0;
|
||||
}
|
||||
int tear_down(void **state) { return 0; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test__prepare_start__success),
|
||||
cmocka_unit_test(test__prepare_start__success_mkdir),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||
}
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
@@ -17,8 +16,6 @@ int _errno = 0;
|
||||
int _exit_code = 0;
|
||||
extern void *print_str_arr;
|
||||
|
||||
/* TODO: finish */
|
||||
|
||||
int __wrap_socket(int __domain, int __type, int __protocol) {
|
||||
errno = _errno;
|
||||
return mock_type(int);
|
||||
@@ -79,6 +76,34 @@ void test__start_server_err_setsockopt(void **state) {
|
||||
assert_int_equal(_exit_code, 4);
|
||||
}
|
||||
|
||||
void test__start_server_err_bind(void **state) {
|
||||
_errno = EBADF;
|
||||
will_return(__wrap_socket, 1);
|
||||
will_return(__wrap_setsockopt,0);
|
||||
will_return(__wrap_bind, -1);
|
||||
will_return(__wrap_listen, 0);
|
||||
|
||||
const char *fmt_args[] = {"starting, socket bind: %s\n", strerror(_errno),
|
||||
NULL};
|
||||
print_str_arr = fmt_args;
|
||||
start_server();
|
||||
assert_int_equal(_exit_code, 5);
|
||||
}
|
||||
|
||||
void test__start_server_err_listen(void **state) {
|
||||
_errno = EBADF;
|
||||
will_return(__wrap_socket, 1);
|
||||
will_return(__wrap_setsockopt,0);
|
||||
will_return(__wrap_bind, 0);
|
||||
will_return(__wrap_listen, -1);
|
||||
|
||||
const char *fmt_args[] = {"starting, socket listen: %s\n", strerror(_errno),
|
||||
NULL};
|
||||
print_str_arr = fmt_args;
|
||||
start_server();
|
||||
assert_int_equal(_exit_code, 6);
|
||||
}
|
||||
|
||||
int setup(void **state) {
|
||||
print_str_arr = NULL;
|
||||
return 0;
|
||||
@@ -89,6 +114,8 @@ int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test__start_server_success),
|
||||
cmocka_unit_test(test__start_server_err_socket),
|
||||
cmocka_unit_test(test__start_server_err_bind),
|
||||
cmocka_unit_test(test__start_server_err_listen),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||
|
||||
Reference in New Issue
Block a user