-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |