Skip to content

Commit

Permalink
add(test): add test_socket
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Aug 24, 2023
1 parent da8af3d commit 9db8e5d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ enable_testing()
# includes ---------------------------------------------------------------------
include_directories(
includes
srcs/Socket
)

# webserv_srcs -----------------------------------------------------------------
set(webserv_srcs
srcs/get_valid_config_file_path.cpp
srcs/Socket/Socket.cpp
)

add_executable(webserv
Expand All @@ -38,7 +40,9 @@ add_executable(webserv

# unit_test_srcs ---------------------------------------------------------------
set (unit_test_srcs
test/unit_test/is_valid_file_path/test_get_valid_config_file_path.cpp)
test/unit_test/is_valid_file_path/test_get_valid_config_file_path.cpp
test/unit_test/test_socket.cpp
)

add_executable(unit_test
${webserv_srcs}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ lint :

.PHONY : unit
unit :
rm -rf build
#rm -rf build
cmake -S . -B build
cmake --build build
#cd build && ctest
Expand Down
2 changes: 1 addition & 1 deletion srcs/Socket/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int Socket::create_socket() {
int errcode;
int ai_family, ai_socktype, ai_protocol;

errcode = set_addr_info(&this->_addr_info);
errcode = set_addr_info(this->_server_ip, this->_server_port, &this->_addr_info);
if (errcode != OK) {
std::cerr << gai_strerror(errcode) << std::endl;
return ERROR;
Expand Down
18 changes: 0 additions & 18 deletions test/unit_test/socket/test_socket.cpp

This file was deleted.

80 changes: 80 additions & 0 deletions test/unit_test/test_socket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include "gtest/gtest.h"
#include "webserv.hpp"
#include "Socket.hpp"

TEST(SocketUnitTest, Construct) {
ASSERT_NO_THROW(Socket());
}

TEST(SocketUnitTest, Getter) {
Socket socket = Socket();

EXPECT_EQ(OK, socket.get_status());
EXPECT_NE(ERROR, socket.get_socket_fd());
}

TEST(SocketIntegrationTest, ConnectToClient) {
Socket server;

EXPECT_EQ(server.get_status(), OK);
EXPECT_NE(server.get_socket_fd(), ERROR);

int client_fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(std::strtol(SERVER_PORT, NULL, 10));
addr.sin_addr.s_addr = inet_addr(SERVER_IP);

EXPECT_EQ(connect(client_fd, (struct sockaddr *)&addr, sizeof(addr)), OK);
close(client_fd);
}

TEST(SocketIntegrationTest, ConnectTooManyClient) {
Socket server;

EXPECT_EQ(server.get_status(), OK);
EXPECT_NE(server.get_socket_fd(), ERROR);

int client_fd;
std::vector<int> client_fds;
for (int i = 0; i < SOMAXCONN; ++i) {
client_fd = socket(AF_INET, SOCK_STREAM, 0);
// printf("cnt:%d, client_fd:%d\n", i+1, client_fd);
EXPECT_NE(client_fd, ERROR);
if (client_fd != ERROR) {
client_fds.push_back(client_fd);

struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(std::strtol(SERVER_PORT, NULL, 10));
addr.sin_addr.s_addr = inet_addr(SERVER_IP);

EXPECT_EQ(connect(client_fd, (struct sockaddr *)&addr, sizeof(addr)), OK);
}
}

// connect over SOMAXCONN -> fd set to nonblock
client_fd = socket(AF_INET, SOCK_STREAM, 0);
// printf("cnt:%d, client_fd:%d\n", SOMAXCONN, client_fd);
EXPECT_NE(client_fd, ERROR);
if (client_fd != ERROR) {
client_fds.push_back(client_fd);
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(std::strtol(SERVER_PORT, NULL, 10));
addr.sin_addr.s_addr = inet_addr(SERVER_IP);

int result_fd_nonblock = fcntl(client_fd, F_SETFL, O_NONBLOCK | FD_CLOEXEC);
EXPECT_NE(result_fd_nonblock, ERROR);
EXPECT_EQ(connect(client_fd, (struct sockaddr *)&addr, sizeof(addr)), ERROR);
}

for (std::vector<int>::iterator itr = client_fds.begin(); itr != client_fds.end(); ++itr) {
close(*itr);
}
client_fds.clear();
}

0 comments on commit 9db8e5d

Please sign in to comment.