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 60564b1
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 27 deletions.
27 changes: 23 additions & 4 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:
Linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -11,11 +11,30 @@ jobs:
cmake -S . -B build
cmake --build build
- name: run-all-test
- name: run all tests on Linux
run: ./build/unit_test

- uses: sarisia/actions-status-discord@v1
if: always()
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ job.status }}
status: ${{ job.status }}

macOS:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3

- name: build
run: |
cmake -S . -B build
cmake --build build
- name: run all tests on macOS
run: ./build/unit_test

- uses: sarisia/actions-status-discord@v1
if: always()
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ job.status }}
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/TestSocket.cpp
)

add_executable(unit_test
${webserv_srcs}
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ lint :

.PHONY : unit
unit :
rm -rf build
#rm -rf build
cmake -S . -B build
cmake --build build
./build/unit_test 2>/dev/null
#./build/unit_test
#cd build && ctest
./build/unit_test

-include $(DEPS)
45 changes: 44 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 All @@ -26,20 +28,59 @@ Socket::Socket() : _status(ERROR),
this->_status = OK;
}

Socket::Socket(const char *server_ip, const char *server_port) : _status(ERROR),
_socket_fd(ERROR),
_addr_info(NULL),
_server_ip(server_ip),
_server_port(server_port) {
if (create_socket() == ERROR) {
return;
}
if (bind_socket() == ERROR) {
return;
}
if (listen_socket() == ERROR) {
return;
}
if (set_fd_to_nonblock() == ERROR) {
return;
}
this->_status = OK;
}

// Socket::Socket(const Socket &copy) {
// *this = copy;
// }
//
// Socket &Socket::operator=(const Socket &rhs) {
// if (this != &rhs) {
// _status = rhs._status;
// _socket_fd = rhs._socket_fd;
// _server_ip = rhs._server_ip;
// _server_port = rhs._server_port;
// // _addr_info = rhs._addr_info;
// set_addr_info(this->_server_ip, this->_server_port, &this->_addr_info);
// }
// return *this;
// }

Socket::~Socket() {
if (this->_addr_info != NULL) {
freeaddrinfo(this->_addr_info);
this->_addr_info = NULL;
}
if (this->_socket_fd != ERROR) {
close_socket_fd(this->_socket_fd);
this->_socket_fd = ERROR;
}
// std::cout << "destructor" << std::endl;
}

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 Expand Up @@ -134,3 +175,5 @@ void Socket::close_socket_fd(int socket_fd) {

int Socket::get_socket_fd() const { return this->_socket_fd; }
int Socket::get_status() const { return this->_status; }
// std::string Socket::get_server_port() const { return this->_server_port; }
// std::string Socket::get_server_ip() const { return this->_server_ip; }
5 changes: 5 additions & 0 deletions srcs/Socket/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
class Socket {
public:
Socket();
Socket(const char *server_ip, const char *server_port);
~Socket();
// Socket(const Socket &copy); // for debug
// Socket &operator=(const Socket &rhs); // for debug

int get_socket_fd() const;
int get_status() const;
// std::string get_server_ip() const; // for debug
// std::string get_server_port() const; // for debug

private:
int _status;
Expand Down
Loading

0 comments on commit 60564b1

Please sign in to comment.