Skip to content

Commit

Permalink
add(test): add test for get_valid_confg_file_path, use gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Aug 24, 2023
1 parent de90ce8 commit 0017376
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 3 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/gtest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: GOOGLE_TEST
on: [push]
jobs:
run-google-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: build
run: |
cmake -S . -B build
cmake --build build
- name: run-all-test
run: |
cd build
# ctest
./unit_test

- uses: sarisia/actions-status-discord@v1
if: always()
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ job.status }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
!Makefile
!CPPLINT.cfg
!webserv.drawio
!CMakeLists.txt

.idea
.vscode
.DS_Store
CMakeLists.txt
CMakeCache.txt
54 changes: 54 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.23)
project(webserv)

set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


# google test ------------------------------------------------------------------
include(FetchContent)
include(GoogleTest)

FetchContent_Declare(
googletest
DOWNLOAD_EXTRACT_TIMESTAMP true
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()


# when you .cpp or .hpp is added, add the following
# include, webserv srcs, unit_test_srcs
# includes ---------------------------------------------------------------------
include_directories(
includes
)

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

#add_executable(webserv
# srcs/main.cpp
# ${webserv_srcs}
#)

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

add_executable(unit_test
${webserv_srcs}
${unit_test_srcs}
)

# test -------------------------------------------------------------------------
target_link_libraries(
unit_test
GTest::gtest_main
)

gtest_discover_tests(unit_test)
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ re : fclean all
lint :
cpplint --recursive srcs

#.PHONY : unit
#unit :
# ./test/unit_test/run_unit_test.sh

.PHONY : unit
unit :
./test/unit_test/run_unit_test.sh

rm -rf build
cmake -S . -B build
cmake --build build
#cd build && ctest
cd build && ./unit_test

-include $(DEPS)
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "gtest/gtest.h"
#include "webserv.hpp"

// TEST(Test, Test) {
// EXPECT_EQ("OK", "OK");
// EXPECT_EQ("NG", "OK");
// EXPECT_EQ(1, func(1));
// EXPECT_EQ(2, func(2));
// EXPECT_EQ(3, func(3));
// }

TEST(TEST_get_valid_config_file_path, simple_test) {
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/file.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file.conf"));

EXPECT_EQ("../test/unit_test/is_valid_file_path/files/file.CONF", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file.CONF"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/file.Conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file.Conf"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/file.conF", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file.conF"));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/nothing.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/nothing"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/conf"));

EXPECT_EQ("../test/unit_test/is_valid_file_path/files/a.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/a.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/ a.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/a.conf "));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/ a.conf "));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/X.conf"));

EXPECT_EQ("../test/unit_test/is_valid_file_path/files/a.b.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/a.b.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/a.conf.b"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/conf.a"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/conf.a."));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)""));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"."));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)".."));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir.conf"));
}

TEST(TEST_get_valid_config_file_path, test_dir_rwx) {
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/dir/file_rrr.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rrr.conf"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/dir/file_rr-.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rr-.conf"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/dir/file_r-r.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_r-r.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_-rr")); // git add permission denied
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/dir/file_r--.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_r--.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_-r-")); // git add permission denied
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_--r")); // git add permission denied
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_---")); // git add permission denied
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/this_is_dir.conf"));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rrr"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rrr"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rr-"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_-rr")); // git add permission denied
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_r-r"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_-r-")); // git add permission denied
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_--r")); // git add permission denied
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_---")); // git add permission denied
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_r--"));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/this_is_dir"));

}

TEST(TEST_get_valid_config_file_path, test_dir_rw) {
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_rrr.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_rr-.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_r-r.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_-rr.conf")); // git add permission denied
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_r--.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_-r-.conf")); // git add permission denied
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_--r.conf")); // git add permission denied
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_---.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/thid_is_dir.conf"));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_rrr"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_rr-"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_r-r"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_-rr")); // git add permission denied
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_r--"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_-r-")); // git add permission denied
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_--r")); // git add permission denied
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_---")); // git add permission denied
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/thid_is_dir"));
}

0 comments on commit 0017376

Please sign in to comment.