Skip to content

Commit

Permalink
add(test): server test
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Aug 26, 2023
1 parent 2e72258 commit 7f5d6de
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 9 additions & 6 deletions srcs/Socket/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
164 changes: 164 additions & 0 deletions test/unit_test/TestServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include <netdb.h>
#include <sys/socket.h>
#include <cstdio>
#include <exception>
#include <string>
#include <pthread.h>
#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;
}

0 comments on commit 7f5d6de

Please sign in to comment.