-
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
7 changed files
with
112 additions
and
25 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
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: | ||
|
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 was deleted.
Oops, something went wrong.
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,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; | ||
} |