tests: start_server [not finished]

This commit is contained in:
2026-07-03 16:45:42 +03:00
parent a0a33f924c
commit 1b34076773
6 changed files with 79 additions and 14 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
- [ ] Tests for the server module - [ ] Tests for the server module
# Development plans # Development plans
- [ ] make print_log to write a file log
- [ ] Remove file when error while saving the data in DB - [ ] Remove file when error while saving the data in DB
- [ ] Colorize server messages - [ ] Colorize server messages
+15 -11
View File
@@ -1,23 +1,24 @@
/* SPDX-License-Identifier: MIT */ /* SPDX-License-Identifier: MIT */
/* Copyright (c) 2026 Oleksandr Zhylin */ /* Copyright (c) 2026 Oleksandr Zhylin */
#include <db.h>
#include <file_p.h>
#include <main.h>
#include <session.h>
#include <utils.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <db.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <file_p.h>
#include <main.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <session.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <utils.h>
volatile sig_atomic_t shutdown_requested = 0; volatile sig_atomic_t shutdown_requested = 0;
volatile sig_atomic_t signal_received = 0; volatile sig_atomic_t signal_received = 0;
@@ -78,7 +79,7 @@ void server_main_loop(server_data_t *s_d) {
} }
if (sr == -1) { if (sr == -1) {
perror("select"); print_log(stderr, pl_error, "select: %s", strerror(errno));
exit(6); exit(6);
} }
@@ -119,7 +120,8 @@ void server_main_loop(server_data_t *s_d) {
char *get_welcome_mes(void) { char *get_welcome_mes(void) {
int fd = open(WELCOME_FILE_NAME, O_RDONLY); int fd = open(WELCOME_FILE_NAME, O_RDONLY);
if (fd == -1) { if (fd == -1) {
perror(WELCOME_FILE_NAME); print_log(stderr, pl_fatal, "starting, welcome file \"%s\": %s\n",
WELCOME_FILE_NAME, strerror(errno));
exit(7); exit(7);
} }
int filesize = lseek(fd, 0, SEEK_END) + 1; int filesize = lseek(fd, 0, SEEK_END) + 1;
@@ -127,7 +129,8 @@ char *get_welcome_mes(void) {
char *welcome_message = char *welcome_message =
mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (welcome_message == MAP_FAILED) { if (welcome_message == MAP_FAILED) {
perror(WELCOME_FILE_NAME); print_log(stderr, pl_fatal, "starting, welcome file \"%s\": %s\n",
WELCOME_FILE_NAME, strerror(errno));
close(fd); close(fd);
exit(5); exit(5);
} }
@@ -150,13 +153,14 @@ int start_server(void) {
res = bind(ls, (struct sockaddr *)&s_addr, sizeof(s_addr)); res = bind(ls, (struct sockaddr *)&s_addr, sizeof(s_addr));
if (res == -1) { if (res == -1) {
perror("bind"); print_log(stderr, pl_fatal, "starting, socket bind: %s\n", strerror(errno));
exit(3); exit(3);
} }
res = listen(ls, LISTEN_QLEN); res = listen(ls, LISTEN_QLEN);
if (res == -1) { if (res == -1) {
perror("listen"); print_log(stderr, pl_fatal, "starting, socket listen: %s\n",
strerror(errno));
exit(4); exit(4);
} }
return ls; return ls;
@@ -169,7 +173,7 @@ void prepare_start(int argc, char *argv[]) {
} }
if (-1 == chdir(argv[1])) { if (-1 == chdir(argv[1])) {
perror(argv[1]); print_log(stderr, pl_fatal, "chdir: \"%s\" %s\n", argv[1], strerror(errno));
exit(2); exit(2);
} }
+1 -1
View File
@@ -32,7 +32,7 @@ int print_log(FILE *output, enum pl_type type, const char *format_message,
char *uf_str = malloc(strlen(date_preffix) + 16 + strlen(format_message) + 4); char *uf_str = malloc(strlen(date_preffix) + 16 + strlen(format_message) + 4);
sprintf(uf_str, uff_str, date_preffix, format_message); sprintf(uf_str, uff_str, date_preffix, format_message);
va_start(args, format_message); va_start(args, format_message);
ret = vfprintf(stdout, uf_str, args); ret = vfprintf(output, uf_str, args);
va_end(args); va_end(args);
return ret; return ret;
} }
+1
View File
@@ -3,3 +3,4 @@ add_subdirectory(user)
add_subdirectory(file_p) add_subdirectory(file_p)
add_subdirectory(test_utils) add_subdirectory(test_utils)
add_subdirectory(client) add_subdirectory(client)
add_subdirectory(server)
+19
View File
@@ -0,0 +1,19 @@
# ---/ start_server /--------------------------------------------------
add_executable(start_server
start_server.c
)
target_link_libraries(start_server
cmocka
client
file_p
session
utils
main
test_utils
)
target_link_options(start_server PRIVATE -Wl,--wrap=socket,--wrap=setsockopt,--wrap=bind,--wrap=listen,--wrap=exit)
add_test(NAME client_start_server_test_1 COMMAND start_server)
+41
View File
@@ -0,0 +1,41 @@
#include <cmocka.h>
#include <db/db.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/socket.h>
#include <client.h>
#include <file_p.h>
#include <main.h>
/* TODO: finish */
int __wrap_socket(int __domain, int __type, int __protocol) { return 0; }
int __wrap_setsockopt(int __fd, int __level, int __optname,
const void *__optval, socklen_t __optlen) {
return 0;
}
int __wrap_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len) {
return 0;
}
int __wrap_listen(int __fd, int __n) { return 0; }
void __wrap_exit(int __status) {}
void test__start_server_success(void **state) {}
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__start_server_success),
};
return cmocka_run_group_tests(tests, setup, tear_down);
}