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 cb47d80
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/gtest.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: GOOGLE_TEST
name: UNIT_TEST
on: [push]
jobs:
run-google-test:
run-unit-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -11,7 +11,7 @@ jobs:
cmake -S . -B build
cmake --build build
- name: run-all-test
- name: run all tests
run: ./build/unit_test

- uses: sarisia/actions-status-discord@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: UNIT_TEST
name: UNIT_TEST_SH
on: [push]
jobs:
run-unit-test:
Expand Down
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
4 changes: 3 additions & 1 deletion srcs/Socket/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <iostream>
#include "webserv.hpp"
#include "Socket.hpp"
Expand Down Expand Up @@ -39,7 +41,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.

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

static struct sockaddr_in create_addr();
static int create_nonblock_client_fd();

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;
int client_fd;

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

// connect under SOMAXCONN
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 = create_addr();
EXPECT_EQ(connect(client_fd, (struct sockaddr *)&addr, sizeof(addr)), OK);
}
}

// connect over SOMAXCONN -> fd set to nonblock
client_fd = create_nonblock_client_fd();
// 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 = create_addr();
EXPECT_EQ(connect(client_fd, (struct sockaddr *)&addr, sizeof(addr)), ERROR);
}

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

static struct sockaddr_in create_addr() {
struct sockaddr_in addr = {};

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

static int create_nonblock_client_fd() {
int client_fd;
int result_fcntl;

client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd == ERROR) {
return ERROR;
}
result_fcntl = fcntl(client_fd, F_SETFL, O_NONBLOCK);
if (result_fcntl == ERROR) {
close(client_fd);
return ERROR;
}
return client_fd;
}

0 comments on commit cb47d80

Please sign in to comment.