diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 343647bd..c8893bb4 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -12,7 +12,7 @@ jobs: cmake --build build - name: run all tests on Linux - run: ./build/unit_test + run: ./build/unit_test 2>/dev/null - uses: sarisia/actions-status-discord@v1 if: always() @@ -32,7 +32,7 @@ jobs: cmake --build build - name: run all tests on macOS - run: ./build/unit_test + run: ./build/unit_test 2>/dev/null - uses: sarisia/actions-status-discord@v1 if: always() diff --git a/Makefile b/Makefile index 75c910f8..63177355 100644 --- a/Makefile +++ b/Makefile @@ -71,8 +71,8 @@ unit : #rm -rf build cmake -S . -B build cmake --build build - #./build/unit_test 2>/dev/null - ./build/unit_test + ./build/unit_test 2>/dev/null + #./build/unit_test #cd build && ctest -include $(DEPS) \ No newline at end of file diff --git a/srcs/Socket/Socket.cpp b/srcs/Socket/Socket.cpp index b0458302..8ed21e11 100644 --- a/srcs/Socket/Socket.cpp +++ b/srcs/Socket/Socket.cpp @@ -22,9 +22,10 @@ Socket::Socket() : _status(ERROR), if (listen_socket() == ERROR) { return; } - if (set_fd_to_nonblock() == ERROR) { - return; - } + // todo: accept error if nonblock + // if (set_fd_to_nonblock() == ERROR) { + // return; + // } this->_status = OK; } @@ -33,6 +34,7 @@ Socket::Socket(const char *server_ip, const char *server_port) : _status(ERROR), _addr_info(NULL), _server_ip(server_ip), _server_port(server_port) { + // std::cout << "ip:" << server_ip << ", port:" << server_port << std::endl; if (create_socket() == ERROR) { return; } @@ -42,9 +44,10 @@ Socket::Socket(const char *server_ip, const char *server_port) : _status(ERROR), if (listen_socket() == ERROR) { return; } - if (set_fd_to_nonblock() == ERROR) { - return; - } + // todo: accept error if nonblock + // if (set_fd_to_nonblock() == ERROR) { + // return; + // } this->_status = OK; } diff --git a/test/unit_test/TestServer.cpp b/test/unit_test/TestServer.cpp new file mode 100644 index 00000000..396d473c --- /dev/null +++ b/test/unit_test/TestServer.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +#include +#include +#include +#include "gtest/gtest.h" +#include "webserv.hpp" +#include "Server.hpp" +#include "Client.hpp" +#include "Color.hpp" + +struct s_server { + const char *server_ip; + const char *server_port; + std::string recv_msg; +}; + +struct s_client { + const char *server_ip; + const char *server_port; + std::string send_msg; + std::string recv_msg; +}; + +static void run_server_and_client(const char *server_ip, + const char *server_port, + const std::string &client_send_msg, + std::string &server_recv_msg, + std::string &client_recv_msg); + +// int port = 49152; + +TEST(ServerUnitTest, Constructor) { + EXPECT_NO_THROW(Server()); + EXPECT_NO_THROW(Server(SERVER_IP, SERVER_PORT)); + + EXPECT_ANY_THROW(Server("hoge", SERVER_PORT)); + EXPECT_ANY_THROW(Server("42", SERVER_PORT)); + EXPECT_ANY_THROW(Server("127.0.0.256", SERVER_PORT)); + EXPECT_ANY_THROW(Server(SERVER_IP, "a")); + EXPECT_ANY_THROW(Server(SERVER_IP, "-1")); + EXPECT_ANY_THROW(Server("huga", "-1")); +} + +TEST(ServerUnitTest, ConnectClient1) { + std::string msg = "test request"; + std::string server_recv_msg; + std::string client_recv_msg; + + try { + run_server_and_client(SERVER_IP, SERVER_PORT, msg, server_recv_msg, client_recv_msg); + EXPECT_EQ(msg, server_recv_msg); + EXPECT_EQ("test response", client_recv_msg); + } + catch (std::exception const &e) { + std::cerr << e.what() << std::endl; + } +} + +TEST(ServerUnitTest, ConnectClient2) { + std::string msg = ""; + std::string server_recv_msg; + std::string client_recv_msg; + + try { + run_server_and_client(SERVER_IP, SERVER_PORT, msg, server_recv_msg, client_recv_msg); + EXPECT_EQ(msg, server_recv_msg); + EXPECT_EQ("test response", client_recv_msg); + } + catch (std::exception const &e) { + std::cerr << e.what() << std::endl; + } +} + +TEST(ServerUnitTest, ConnectClient3) { + std::string msg = "a\n\n\nb\n\n\n\n\r\nc\td"; + std::string server_recv_msg; + std::string client_recv_msg; + + try { + run_server_and_client(SERVER_IP, SERVER_PORT, msg, server_recv_msg, client_recv_msg); + EXPECT_EQ(msg, server_recv_msg); + EXPECT_EQ("test response", client_recv_msg); + } + catch (std::exception const &e) { + std::cerr << e.what() << std::endl; + } +} + +TEST(ServerUnitTest, ConnectClient4) { + std::string msg = "GET /home.html HTTP/1.1\r\n" + "Host: developer.mozilla.org\r\n" + "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0\r\n" + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" + "Accept-Language: en-US,en;q=0.5\r\n" + "Accept-Encoding: gzip, deflate, br\r\n" + "Referer: https://developer.mozilla.org/testpage.html\r\n" + "Connection: keep-alive\r\n" + "Upgrade-Insecure-Requests: 1\r\n" + "If-Modified-Since: Mon, 18 Jul 2016 02:36:04 GMT\r\n" + "If-None-Match: \"c561c68d0ba92bbeb8b0fff2a9199f722e3a621a\"\r\n" + "Cache-Control: max-age=0\r\n"; + std::string server_recv_msg; + std::string client_recv_msg; + + try { + run_server_and_client(SERVER_IP, SERVER_PORT, msg, server_recv_msg, client_recv_msg); + EXPECT_EQ(msg, server_recv_msg); + EXPECT_EQ("test response", client_recv_msg); + } + catch (std::exception const &e) { + std::cerr << e.what() << std::endl; + } +} + +/* helper */ +static void *run_server(void *server_info) { + s_server *s = (s_server *)server_info; + Server server = Server(s->server_ip, s->server_port); + + server.process_client_connection(); + s->recv_msg = server.get_recv_message(); + return NULL; +} + +static void *run_client(void *client_info) { + s_client *c = (s_client *)client_info; + Client client = Client(c->server_ip, c->server_port); + + sleep(1); + client.process_server_connect(c->send_msg); + c->recv_msg = client.get_recv_message(); + return NULL; +} + +static void run_server_and_client(const char *server_ip, + const char *server_port, + const std::string &client_send_msg, + std::string &server_recv_msg, + std::string &client_recv_msg) { + s_server server_info = {server_ip, server_port, ""}; + s_client client_info = {server_ip, server_port, client_send_msg, ""}; + + pthread_t server_thread, client_thread; + int ret1, ret2; + + ret1 = pthread_create(&server_thread, NULL, run_server, (void *)&server_info); + ret2 = pthread_create(&client_thread, NULL, run_client, (void *)&client_info); + if (ret1 != OK || ret2 != OK) { + throw std::runtime_error("pthread_create error"); + } + + ret1 = pthread_join(server_thread, NULL); + ret2 = pthread_join(client_thread, NULL); + if (ret1 != OK || ret2 != OK) { + throw std::runtime_error("pthread_join error"); + } + + // std::cout << "server_recv_msg:" << server_info.recv_msg << std::endl; + // std::cout << "client_recv_msg:" << client_info.recv_msg << std::endl; + server_recv_msg = server_info.recv_msg; + client_recv_msg = client_info.recv_msg; +}