diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index cc0f95be..7350773a 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -25,7 +25,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: # We must fetch at least the immediate parents so that if this is # a pull request then we can checkout the head. @@ -47,7 +47,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} @@ -70,4 +70,4 @@ jobs: make ; - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2 diff --git a/.github/workflows/verify-build.yml b/.github/workflows/verify-build.yml index 77530449..8d4afe11 100644 --- a/.github/workflows/verify-build.yml +++ b/.github/workflows/verify-build.yml @@ -58,8 +58,8 @@ jobs: os-type: ubuntu build-type: asan compiler-family: clang - c-compiler: clang-13 - cc-compiler: clang++-13 + c-compiler: clang-14 + cc-compiler: clang++-14 debug: debug coverage: nocoverage # This test gives false positives on newer versions of clang @@ -78,8 +78,8 @@ jobs: os-type: ubuntu build-type: lsan compiler-family: clang - c-compiler: clang-13 - cc-compiler: clang++-13 + c-compiler: clang-14 + cc-compiler: clang++-14 debug: debug coverage: nocoverage - test-group: extra @@ -87,8 +87,8 @@ jobs: os-type: ubuntu build-type: tsan compiler-family: clang - c-compiler: clang-13 - cc-compiler: clang++-13 + c-compiler: clang-14 + cc-compiler: clang++-14 debug: debug coverage: nocoverage - test-group: extra @@ -96,107 +96,17 @@ jobs: os-type: ubuntu build-type: ubsan compiler-family: clang - c-compiler: clang-13 - cc-compiler: clang++-13 + c-compiler: clang-14 + cc-compiler: clang++-14 debug: debug coverage: nocoverage - - test-group: extra - os: ubuntu-20.04 - os-type: ubuntu - build-type: none - compiler-family: gcc - c-compiler: gcc-7 - cc-compiler: g++-7 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-20.04 - os-type: ubuntu - build-type: none - compiler-family: gcc - c-compiler: gcc-8 - cc-compiler: g++-8 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-latest - os-type: ubuntu - build-type: none - compiler-family: gcc - c-compiler: gcc-9 - cc-compiler: g++-9 - debug: nodebug - coverage: nocoverage - test-group: extra os: ubuntu-latest os-type: ubuntu build-type: none compiler-family: gcc - c-compiler: gcc-10 - cc-compiler: g++-10 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-20.04 - os-type: ubuntu - build-type: none - compiler-family: clang - c-compiler: clang-6.0 - cc-compiler: clang++-6.0 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-20.04 - os-type: ubuntu - build-type: none - compiler-family: clang - c-compiler: clang-7 - cc-compiler: clang++-7 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-20.04 - os-type: ubuntu - build-type: none - compiler-family: clang - c-compiler: clang-8 - cc-compiler: clang++-8 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-20.04 - os-type: ubuntu - build-type: none - compiler-family: clang - c-compiler: clang-9 - cc-compiler: clang++-9 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-20.04 - os-type: ubuntu - build-type: none - compiler-family: clang - c-compiler: clang-10 - cc-compiler: clang++-10 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-latest - os-type: ubuntu - build-type: none - compiler-family: clang - c-compiler: clang-11 - cc-compiler: clang++-11 - debug: nodebug - coverage: nocoverage - - test-group: extra - os: ubuntu-latest - os-type: ubuntu - build-type: none - compiler-family: clang - c-compiler: clang-12 - cc-compiler: clang++-12 + c-compiler: gcc-11 + cc-compiler: g++-11 debug: nodebug coverage: nocoverage - test-group: extra @@ -204,8 +114,8 @@ jobs: os-type: ubuntu build-type: none compiler-family: clang - c-compiler: clang-13 - cc-compiler: clang++-13 + c-compiler: clang-14 + cc-compiler: clang++-14 debug: nodebug coverage: nocoverage - test-group: extra @@ -213,17 +123,17 @@ jobs: os-type: ubuntu build-type: valgrind compiler-family: gcc - c-compiler: gcc-10 - cc-compiler: g++-10 + c-compiler: gcc-11 + cc-compiler: g++-11 debug: nodebug coverage: nocoverage - test-group: extra - os: ubuntu-20.04 + os: ubuntu-22.04 os-type: ubuntu build-type: iwyu compiler-family: clang - c-compiler: clang-9 - cc-compiler: clang++-9 + c-compiler: clang-14 + cc-compiler: clang++-14 debug: nodebug coverage: nocoverage - test-group: performance @@ -231,8 +141,8 @@ jobs: os-type: ubuntu build-type: select compiler-family: gcc - c-compiler: gcc-10 - cc-compiler: g++-10 + c-compiler: gcc-11 + cc-compiler: g++-11 debug: nodebug coverage: nocoverage - test-group: performance @@ -240,8 +150,8 @@ jobs: os-type: ubuntu build-type: nodelay compiler-family: gcc - c-compiler: gcc-10 - cc-compiler: g++-10 + c-compiler: gcc-11 + cc-compiler: g++-11 debug: nodebug coverage: nocoverage - test-group: performance @@ -249,8 +159,8 @@ jobs: os-type: ubuntu build-type: threads compiler-family: gcc - c-compiler: gcc-10 - cc-compiler: g++-10 + c-compiler: gcc-11 + cc-compiler: g++-11 debug: nodebug coverage: nocoverage - test-group: extra @@ -258,13 +168,13 @@ jobs: os-type: ubuntu build-type: lint compiler-family: gcc - c-compiler: gcc-10 - cc-compiler: g++-10 + c-compiler: gcc-11 + cc-compiler: g++-11 debug: debug coverage: nocoverage steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: # We must fetch at least the immediate parents so that if this is # a pull request then we can checkout the head. @@ -294,7 +204,7 @@ jobs: if: ${{ matrix.compiler-family == 'gcc' && matrix.os-type == 'ubuntu' }} - name: Install valgrind if needed - run: sudo apt-get install valgrind valgrind-dbg + run: sudo apt-get install valgrind if: ${{ matrix.build-type == 'valgrind' && matrix.os-type == 'ubuntu' }} - name: Install cpplint if needed @@ -312,7 +222,7 @@ jobs: - name: IWYU from cache (for testing) id: cache-IWYU - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: include-what-you-use key: ${{ matrix.os }}-${{ matrix.c-compiler }}-include-what-you-use-pre-built @@ -341,7 +251,7 @@ jobs: - name: CURL from cache (for testing) id: cache-CURL - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: curl-7.75.0 key: ${{ matrix.os }}-CURL-pre-built @@ -386,7 +296,7 @@ jobs: - name: Fetch libmicrohttpd from cache id: cache-libmicrohttpd - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: libmicrohttpd-0.9.64 key: ${{ matrix.os }}-${{ matrix.c-compiler }}-libmicrohttpd-pre-built diff --git a/examples/allowing_disallowing_methods.cpp b/examples/allowing_disallowing_methods.cpp index 73389142..9702f4b1 100644 --- a/examples/allowing_disallowing_methods.cpp +++ b/examples/allowing_disallowing_methods.cpp @@ -19,6 +19,7 @@ */ #include +#include class hello_world_resource : public httpserver::http_resource { public: diff --git a/examples/basic_authentication.cpp b/examples/basic_authentication.cpp index 7fb82340..7456a159 100644 --- a/examples/basic_authentication.cpp +++ b/examples/basic_authentication.cpp @@ -19,6 +19,8 @@ */ #include +#include +#include class user_pass_resource : public httpserver::http_resource { public: diff --git a/examples/custom_access_log.cpp b/examples/custom_access_log.cpp index f1a59d53..8f596c90 100644 --- a/examples/custom_access_log.cpp +++ b/examples/custom_access_log.cpp @@ -19,6 +19,8 @@ */ #include +#include +#include #include diff --git a/examples/custom_error.cpp b/examples/custom_error.cpp index a82d5972..a0c52aa1 100644 --- a/examples/custom_error.cpp +++ b/examples/custom_error.cpp @@ -19,6 +19,7 @@ */ #include +#include std::shared_ptr not_found_custom(const httpserver::http_request&) { return std::shared_ptr(new httpserver::string_response("Not found custom", 404, "text/plain")); diff --git a/examples/deferred_with_accumulator.cpp b/examples/deferred_with_accumulator.cpp index 3d3a4e69..d08ec431 100644 --- a/examples/deferred_with_accumulator.cpp +++ b/examples/deferred_with_accumulator.cpp @@ -20,6 +20,9 @@ #include #include +#include +#include +#include // cpplint errors on chrono and thread because they are replaced (in Chromium) by other google libraries. // This is not an issue here. #include // NOLINT [build/c++11] diff --git a/examples/digest_authentication.cpp b/examples/digest_authentication.cpp index 40767dc2..2cb769a8 100644 --- a/examples/digest_authentication.cpp +++ b/examples/digest_authentication.cpp @@ -19,6 +19,7 @@ */ #include +#include #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" diff --git a/examples/file_upload.cpp b/examples/file_upload.cpp index 7e1afd5a..aac16abb 100644 --- a/examples/file_upload.cpp +++ b/examples/file_upload.cpp @@ -20,6 +20,8 @@ #include #include +#include +#include class file_upload_resource : public httpserver::http_resource { public: diff --git a/examples/handlers.cpp b/examples/handlers.cpp index 10778aaf..bf620361 100644 --- a/examples/handlers.cpp +++ b/examples/handlers.cpp @@ -19,6 +19,7 @@ */ #include +#include class hello_world_resource : public httpserver::http_resource { public: diff --git a/examples/hello_with_get_arg.cpp b/examples/hello_with_get_arg.cpp index 268d00c3..be1f8e6b 100644 --- a/examples/hello_with_get_arg.cpp +++ b/examples/hello_with_get_arg.cpp @@ -19,6 +19,8 @@ */ #include +#include +#include class hello_world_resource : public httpserver::http_resource { public: diff --git a/examples/hello_world.cpp b/examples/hello_world.cpp index 391a600f..9c06f87a 100755 --- a/examples/hello_world.cpp +++ b/examples/hello_world.cpp @@ -19,6 +19,8 @@ */ #include +#include +#include #include diff --git a/examples/minimal_deferred.cpp b/examples/minimal_deferred.cpp index e7c77f50..61119286 100644 --- a/examples/minimal_deferred.cpp +++ b/examples/minimal_deferred.cpp @@ -20,6 +20,9 @@ #include #include +#include +#include +#include static int counter = 0; diff --git a/examples/minimal_file_response.cpp b/examples/minimal_file_response.cpp index a5dc8106..a9204b85 100644 --- a/examples/minimal_file_response.cpp +++ b/examples/minimal_file_response.cpp @@ -19,6 +19,7 @@ */ #include +#include class file_response_resource : public httpserver::http_resource { public: diff --git a/examples/minimal_hello_world.cpp b/examples/minimal_hello_world.cpp index f8fb27f0..41e3cc14 100644 --- a/examples/minimal_hello_world.cpp +++ b/examples/minimal_hello_world.cpp @@ -19,6 +19,7 @@ */ #include +#include class hello_world_resource : public httpserver::http_resource { public: diff --git a/examples/minimal_https.cpp b/examples/minimal_https.cpp index d5b8d443..ed7e069f 100644 --- a/examples/minimal_https.cpp +++ b/examples/minimal_https.cpp @@ -19,6 +19,7 @@ */ #include +#include class hello_world_resource : public httpserver::http_resource { public: diff --git a/examples/minimal_ip_ban.cpp b/examples/minimal_ip_ban.cpp index 7be3cd17..adfb7cd5 100644 --- a/examples/minimal_ip_ban.cpp +++ b/examples/minimal_ip_ban.cpp @@ -19,6 +19,7 @@ */ #include +#include class hello_world_resource : public httpserver::http_resource { public: diff --git a/examples/service.cpp b/examples/service.cpp index 2bc9d4ad..309628bc 100644 --- a/examples/service.cpp +++ b/examples/service.cpp @@ -22,6 +22,7 @@ #include #include +#include #include diff --git a/examples/setting_headers.cpp b/examples/setting_headers.cpp index ea678b92..8d5f06ac 100644 --- a/examples/setting_headers.cpp +++ b/examples/setting_headers.cpp @@ -19,6 +19,7 @@ */ #include +#include class hello_world_resource : public httpserver::http_resource { public: diff --git a/examples/url_registration.cpp b/examples/url_registration.cpp index be6d1ce3..8102075f 100644 --- a/examples/url_registration.cpp +++ b/examples/url_registration.cpp @@ -19,6 +19,8 @@ */ #include +#include +#include class hello_world_resource : public httpserver::http_resource { public: diff --git a/src/file_info.cpp b/src/file_info.cpp index 88a21583..d37e7aeb 100644 --- a/src/file_info.cpp +++ b/src/file_info.cpp @@ -19,6 +19,7 @@ */ #include +#include #include "httpserver/file_info.hpp" namespace httpserver { diff --git a/src/http_request.cpp b/src/http_request.cpp index c49c3b8e..49db297c 100644 --- a/src/http_request.cpp +++ b/src/http_request.cpp @@ -25,6 +25,9 @@ #include #include "httpserver/http_utils.hpp" #include "httpserver/string_utilities.hpp" +#include +#include +#include namespace httpserver { diff --git a/src/http_resource.cpp b/src/http_resource.cpp index 88688484..45d3a25b 100644 --- a/src/http_resource.cpp +++ b/src/http_resource.cpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "httpserver/string_response.hpp" namespace httpserver { class http_response; } diff --git a/src/http_response.cpp b/src/http_response.cpp index cb357b24..b88dd866 100644 --- a/src/http_response.cpp +++ b/src/http_response.cpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "httpserver/http_utils.hpp" namespace httpserver { diff --git a/src/http_utils.cpp b/src/http_utils.cpp index 975f1a8f..459ba6bc 100644 --- a/src/http_utils.cpp +++ b/src/http_utils.cpp @@ -45,6 +45,8 @@ #include #include #include +#include +#include #include "httpserver/string_utilities.hpp" @@ -206,9 +208,9 @@ const char* http_utils::text_plain = "text/plain"; const char* http_utils::upload_filename_template = "libhttpserver.XXXXXX"; #if defined(_WIN32) - const char http_utils::path_separator = '\\'; +const char http_utils::path_separator = '\\'; #else // _WIN32 - const char http_utils::path_separator = '/'; +const char http_utils::path_separator = '/'; #endif // _WIN32 std::vector http_utils::tokenize_url(const std::string& str, const char separator) { diff --git a/src/httpserver/deferred_response.hpp b/src/httpserver/deferred_response.hpp index 85f8791f..8bff629c 100644 --- a/src/httpserver/deferred_response.hpp +++ b/src/httpserver/deferred_response.hpp @@ -38,7 +38,7 @@ struct MHD_Response; namespace httpserver { namespace details { - MHD_Response* get_raw_response_helper(void* cls, ssize_t (*cb)(void*, uint64_t, char*, size_t)); +MHD_Response* get_raw_response_helper(void* cls, ssize_t (*cb)(void*, uint64_t, char*, size_t)); } template diff --git a/src/webserver.cpp b/src/webserver.cpp index 6e0c7ead..8947bcca 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -19,6 +19,9 @@ */ #include "httpserver/webserver.hpp" +#include +#include +#include #if defined(_WIN32) && !defined(__CYGWIN__) #include @@ -290,6 +293,7 @@ bool webserver::start(bool blocking) { start_conf |= MHD_USE_IPv6; } + if (use_dual_stack) { start_conf |= MHD_USE_DUAL_STACK; } @@ -321,6 +325,7 @@ bool webserver::start(bool blocking) { } if (daemon == nullptr) { + perror("MHD_start_daemon"); throw std::invalid_argument("Unable to connect daemon to port: " + std::to_string(port)); } diff --git a/test/integ/authentication.cpp b/test/integ/authentication.cpp index bcc1c55b..2bc37c93 100644 --- a/test/integ/authentication.cpp +++ b/test/integ/authentication.cpp @@ -32,9 +32,11 @@ #include #include +#include #include "./httpserver.hpp" #include "./littletest.hpp" +#include "test_utils.hpp" #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" @@ -97,7 +99,8 @@ LT_BEGIN_SUITE(authentication_suite) LT_END_SUITE(authentication_suite) LT_BEGIN_AUTO_TEST(authentication_suite, base_auth) - webserver ws = create_webserver(PORT); + int test_port = test_utils::get_random_port(); + webserver ws = create_webserver(test_port); user_pass_resource user_pass; LT_ASSERT_EQ(true, ws.register_resource("base", &user_pass)); @@ -109,7 +112,7 @@ LT_BEGIN_AUTO_TEST(authentication_suite, base_auth) CURLcode res; curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypass"); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -122,7 +125,8 @@ LT_BEGIN_AUTO_TEST(authentication_suite, base_auth) LT_END_AUTO_TEST(base_auth) LT_BEGIN_AUTO_TEST(authentication_suite, base_auth_fail) - webserver ws = create_webserver(PORT); + int test_port = test_utils::get_random_port(); + webserver ws = create_webserver(test_port); user_pass_resource user_pass; LT_ASSERT_EQ(true, ws.register_resource("base", &user_pass)); @@ -134,7 +138,7 @@ LT_BEGIN_AUTO_TEST(authentication_suite, base_auth_fail) CURLcode res; curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "wrongpass"); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -152,7 +156,8 @@ LT_END_AUTO_TEST(base_auth_fail) #ifndef _WINDOWS LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth) - webserver ws = create_webserver(PORT) + int test_port = test_utils::get_random_port(); + webserver ws = create_webserver(test_port) .digest_auth_random("myrandom") .nonce_nc_size(300); @@ -175,7 +180,7 @@ LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth) #else curl_easy_setopt(curl, CURLOPT_USERPWD, "myuser:mypass"); #endif - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -192,7 +197,8 @@ LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth) LT_END_AUTO_TEST(digest_auth) LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth_wrong_pass) - webserver ws = create_webserver(PORT) + int test_port = test_utils::get_random_port(); + webserver ws = create_webserver(test_port) .digest_auth_random("myrandom") .nonce_nc_size(300); @@ -215,7 +221,7 @@ LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth_wrong_pass) #else curl_easy_setopt(curl, CURLOPT_USERPWD, "myuser:wrongpass"); #endif - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); diff --git a/test/integ/ban_system.cpp b/test/integ/ban_system.cpp index 7e551003..f51cc4e6 100644 --- a/test/integ/ban_system.cpp +++ b/test/integ/ban_system.cpp @@ -26,6 +26,7 @@ #include "./httpserver.hpp" #include "httpserver/http_utils.hpp" #include "./littletest.hpp" +#include "test_utils.hpp" using std::shared_ptr; @@ -68,7 +69,8 @@ LT_BEGIN_SUITE(ban_system_suite) LT_END_SUITE(ban_system_suite) LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks) - webserver ws = create_webserver(PORT).default_policy(http_utils::ACCEPT); + int test_port = test_utils::get_random_port(); + webserver ws = create_webserver(test_port).default_policy(http_utils::ACCEPT); ws.start(false); ok_resource resource; @@ -80,7 +82,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -95,7 +97,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks) CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); res = curl_easy_perform(curl); LT_ASSERT_NEQ(res, 0); @@ -108,7 +110,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -123,7 +125,8 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks) LT_END_AUTO_TEST(accept_default_ban_blocks) LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes) - webserver ws = create_webserver(PORT).default_policy(http_utils::REJECT); + int test_port = test_utils::get_random_port(); + webserver ws = create_webserver(test_port).default_policy(http_utils::REJECT); ws.start(false); ok_resource resource; @@ -134,7 +137,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes) { CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); res = curl_easy_perform(curl); LT_ASSERT_NEQ(res, 0); @@ -147,7 +150,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -162,7 +165,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes) CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); res = curl_easy_perform(curl); LT_ASSERT_NEQ(res, 0); diff --git a/test/integ/basic.cpp b/test/integ/basic.cpp index 3e680cb6..ef44df83 100644 --- a/test/integ/basic.cpp +++ b/test/integ/basic.cpp @@ -29,6 +29,9 @@ #include "./httpserver.hpp" #include "httpserver/string_utilities.hpp" #include "./littletest.hpp" +#include "test_utils.hpp" + +int test_port; using std::string; using std::map; @@ -340,7 +343,7 @@ class print_response_resource : public http_resource { #ifdef HTTPSERVER_PORT #define PORT HTTPSERVER_PORT #else -#define PORT 8080 +#define PORT test_port #endif // PORT #define STR2(p) #p @@ -351,7 +354,7 @@ LT_BEGIN_SUITE(basic_suite) std::unique_ptr ws; void set_up() { - ws = std::make_unique(create_webserver(PORT)); + ws = std::make_unique(create_webserver(test_port)); ws->start(false); } @@ -375,7 +378,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, two_endpoints) { CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/OK").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -389,7 +392,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, two_endpoints) { CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/NOK"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/NOK").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &t); @@ -434,7 +437,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, family_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/OK").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -448,7 +451,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, family_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/OK/").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -462,7 +465,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, family_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/go"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/OK/go").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -477,7 +480,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, family_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/OK").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -491,7 +494,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, family_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/OK/").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -505,7 +508,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, family_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/OK/go"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/OK/go").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -529,7 +532,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, overlapping_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/bar/"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/foo/bar/").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -547,7 +550,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, overlapping_endpoints) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/bar/"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/foo/bar/").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -565,7 +568,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, read_body) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -582,7 +585,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, read_long_body) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -600,7 +603,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, resource_setting_header) map ss; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -620,7 +623,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, resource_setting_cookie) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); curl_easy_setopt(curl, CURLOPT_COOKIEJAR, ""); @@ -652,7 +655,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, request_with_header) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); struct curl_slist *list = nullptr; @@ -675,7 +678,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, request_with_cookie) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -693,7 +696,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, complete) { CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); CURLcode res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -702,7 +705,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, complete) { CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); CURLcode res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -711,7 +714,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, complete) { CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); CURLcode res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -720,7 +723,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, complete) { CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH"); CURLcode res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -729,7 +732,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, complete) /* { CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "CONNECT"); CURLcode res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -739,7 +742,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, complete) { CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, nullptr); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0); @@ -759,7 +762,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, only_render) s = ""; curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -770,7 +773,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, only_render) s = ""; curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -781,7 +784,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, only_render) s = ""; curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -793,7 +796,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, only_render) /* s = ""; curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "CONNECT"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -805,7 +808,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, only_render) s = ""; curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "NOT_EXISTENT"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -816,7 +819,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, only_render) s = ""; curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, nullptr); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0); @@ -835,7 +838,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, postprocessor) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "arg1=lib&arg2=httpserver"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -874,7 +877,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, postprocessor_large_field_last_field) // Append the suffix cString[offset] = '\0'; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cString); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -920,7 +923,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, postprocessor_large_field_first_field) // Append the suffix std::snprintf(cString + offset, totalLength + 1 - offset, "%s", suffix); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cString); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -942,7 +945,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, same_key_different_value) // The curl default content type triggers the file processing // logic in the webserver. However, since there is no actual // file, the arg handling should be the same. - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "arg=inertia&arg=isaproperty&arg=ofmatter"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -959,7 +962,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, same_key_different_value_plain_content) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base?arg=beep&arg=boop&arg=hello&arg=what"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base?arg=beep&arg=boop&arg=hello&arg=what").c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "arg=beep&arg=boop&arg=hello&arg=what"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -979,7 +982,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, empty_arg) curl_global_init(CURL_GLOBAL_ALL); CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "arg1"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); res = curl_easy_perform(curl); @@ -993,7 +996,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, no_response) curl_global_init(CURL_GLOBAL_ALL); CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); CURLcode res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -1009,7 +1012,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, empty_response) curl_global_init(CURL_GLOBAL_ALL); CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); CURLcode res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -1027,7 +1030,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_matching) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/regex/matching/number/10"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/regex/matching/number/10").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1045,7 +1048,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/whatever/passed/in/input"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/this/captures/whatever/passed/in/input").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1063,7 +1066,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg_with_url_pars) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/whatever/passed/in/input?arg2=second_argument"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/this/captures/whatever/passed/in/input?arg2=second_argument").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1082,7 +1085,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg_custom) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/numeric/11/passed/in/input"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/this/captures/numeric/11/passed/in/input").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1096,7 +1099,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg_custom) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/numeric/text/passed/in/input"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/this/captures/numeric/text/passed/in/input").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1118,7 +1121,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, querystring_processing) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/args/passed/in/the/querystring?arg=first&arg2=second"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/this/captures/args/passed/in/the/querystring?arg=first&arg2=second").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1136,7 +1139,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, full_arguments_processing) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/args/passed/in/the/querystring?arg=argument"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/this/captures/args/passed/in/the/querystring?arg=argument").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1154,7 +1157,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, querystring_query_processing) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/args/passed/in/the/querystring?arg1=value1&arg2=value2&arg3=value3"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/this/captures/args/passed/in/the/querystring?arg1=value1&arg2=value2&arg3=value3").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1173,7 +1176,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, register_unregister) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1188,7 +1191,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, register_unregister) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1209,7 +1212,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, register_unregister) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1229,7 +1232,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, file_serving_resource) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1247,7 +1250,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, file_serving_resource_empty) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1265,7 +1268,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, file_serving_resource_default_content_type) map ss; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headerfunc); curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ss); @@ -1284,7 +1287,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, file_serving_resource_missing) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1308,7 +1311,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, file_serving_resource_dir) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1332,7 +1335,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, exception_forces_500) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1355,7 +1358,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, untyped_error_forces_500) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1379,7 +1382,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, request_is_printable) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1414,7 +1417,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, response_is_printable) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1446,7 +1449,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, long_path_pieces) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/settings/somestringthatisreallylong/with_really_a_lot_of_content/and_underscores_and_looooooooooooooooooong_stuff"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/settings/somestringthatisreallylong/with_really_a_lot_of_content/and_underscores_and_looooooooooooooooooong_stuff").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1464,7 +1467,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, url_with_regex_like_pieces) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/settings/{}"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/settings/{}").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1482,7 +1485,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, non_family_url_with_regex_like_pieces) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/settings/{}"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/settings/{}").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1505,7 +1508,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_url_exact_match) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/a/bar/"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/foo/a/bar/").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1524,7 +1527,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_url_exact_match) string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/foo/{v|[a-z]}/bar/"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/foo/{v|[a-z]}/bar/").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1553,7 +1556,7 @@ LT_BEGIN_AUTO_TEST(basic_suite, method_not_allowed_header) map ss; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -1570,5 +1573,6 @@ LT_BEGIN_AUTO_TEST(basic_suite, method_not_allowed_header) LT_END_AUTO_TEST(method_not_allowed_header) LT_BEGIN_AUTO_TEST_ENV() + test_port = test_utils::get_random_port(); AUTORUN_TESTS() LT_END_AUTO_TEST_ENV() diff --git a/test/integ/deferred.cpp b/test/integ/deferred.cpp index 6bece022..fe644870 100644 --- a/test/integ/deferred.cpp +++ b/test/integ/deferred.cpp @@ -39,6 +39,9 @@ #include "./httpserver.hpp" #include "./littletest.hpp" +#include "test_utils.hpp" + +int test_port; using std::shared_ptr; using std::string; @@ -107,7 +110,7 @@ class deferred_resource_with_data : public http_resource { #ifdef HTTPSERVER_PORT #define PORT HTTPSERVER_PORT #else -#define PORT 8080 +#define PORT test_port #endif // PORT #define STR2(p) #p @@ -118,7 +121,7 @@ LT_BEGIN_SUITE(deferred_suite) std::unique_ptr ws; void set_up() { - ws = std::make_unique(create_webserver(PORT)); + ws = std::make_unique(create_webserver(test_port)); ws->start(false); } @@ -137,7 +140,7 @@ LT_BEGIN_AUTO_TEST(deferred_suite, deferred_response_suite) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -155,7 +158,7 @@ LT_BEGIN_AUTO_TEST(deferred_suite, deferred_response_with_data) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -166,5 +169,6 @@ LT_BEGIN_AUTO_TEST(deferred_suite, deferred_response_with_data) LT_END_AUTO_TEST(deferred_response_with_data) LT_BEGIN_AUTO_TEST_ENV() + test_port = test_utils::get_random_port(); AUTORUN_TESTS() LT_END_AUTO_TEST_ENV() diff --git a/test/integ/file_upload.cpp b/test/integ/file_upload.cpp index 57b6942b..4ede919d 100644 --- a/test/integ/file_upload.cpp +++ b/test/integ/file_upload.cpp @@ -33,6 +33,9 @@ #include "./httpserver.hpp" #include "httpserver/string_utilities.hpp" #include "./littletest.hpp" +#include "test_utils.hpp" + +int test_port; using std::string; using std::string_view; @@ -53,7 +56,7 @@ using httpserver::http::arg_map; #ifdef HTTPSERVER_PORT #define PORT HTTPSERVER_PORT #else -#define PORT 8080 +#define PORT test_port #endif // PORT #define STR2(p) #p @@ -117,7 +120,7 @@ static std::pair send_file_to_webserver(bool add_second_file, } CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/upload"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/upload").c_str()); curl_easy_setopt(curl, CURLOPT_MIMEPOST, form); res = curl_easy_perform(curl); @@ -151,7 +154,7 @@ static std::pair send_large_file(string* content, std::string curl_mime_name(field, LARGE_KEY); curl_mime_filedata(field, LARGE_CONTENT_FILEPATH); - std::string url = "localhost:" PORT_STRING "/upload"; + std::string url = "http://localhost:" + std::to_string(test_port) + "/upload"; if (!args.empty()) { url.append(args); } @@ -192,7 +195,7 @@ static std::tuple send_file_via_put() { return {false, CURLcode{}, 0L}; } - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/upload"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/upload").c_str()); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_READDATA, fd); curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t) file_info.st_size); @@ -277,7 +280,7 @@ LT_END_AUTO_TEST(check_files) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk) string upload_directory = "."; - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_MEMORY_AND_DISK) .file_upload_dir(upload_directory) @@ -325,7 +328,7 @@ LT_END_AUTO_TEST(file_upload_memory_and_disk) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_via_put) string upload_directory = "."; - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_MEMORY_AND_DISK) .file_upload_dir(upload_directory) @@ -356,7 +359,7 @@ LT_END_AUTO_TEST(file_upload_memory_and_disk_via_put) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_additional_params) string upload_directory = "."; - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_MEMORY_AND_DISK) .file_upload_dir(upload_directory) @@ -409,7 +412,7 @@ LT_END_AUTO_TEST(file_upload_memory_and_disk_additional_params) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_and_disk_two_files) string upload_directory = "."; - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_MEMORY_AND_DISK) .file_upload_dir(upload_directory) @@ -477,7 +480,7 @@ LT_END_AUTO_TEST(file_upload_memory_and_disk_two_files) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_disk_only) string upload_directory = "."; - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .no_put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_DISK_ONLY) .file_upload_dir(upload_directory) @@ -519,7 +522,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_disk_only) LT_END_AUTO_TEST(file_upload_disk_only) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_only_incl_content) - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_MEMORY_ONLY)); ws->start(false); @@ -549,7 +552,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_only_incl_content) LT_END_AUTO_TEST(file_upload_memory_only_incl_content) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_large_content) - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_MEMORY_ONLY)); ws->start(false); @@ -586,7 +589,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_large_content) LT_END_AUTO_TEST(file_upload_large_content) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_large_content_with_args) - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_MEMORY_ONLY)); ws->start(false); @@ -629,7 +632,7 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_large_content_with_args) LT_END_AUTO_TEST(file_upload_large_content_with_args) LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_only_excl_content) - auto ws = std::make_unique(create_webserver(PORT) + auto ws = std::make_unique(create_webserver(test_port) .no_put_processed_data_to_content() .file_upload_target(httpserver::FILE_UPLOAD_MEMORY_ONLY)); ws->start(false); @@ -658,5 +661,6 @@ LT_BEGIN_AUTO_TEST(file_upload_suite, file_upload_memory_only_excl_content) LT_END_AUTO_TEST(file_upload_memory_only_excl_content) LT_BEGIN_AUTO_TEST_ENV() + test_port = test_utils::get_random_port(); AUTORUN_TESTS() LT_END_AUTO_TEST_ENV() diff --git a/test/integ/nodelay.cpp b/test/integ/nodelay.cpp index 068ef18a..d6a48505 100644 --- a/test/integ/nodelay.cpp +++ b/test/integ/nodelay.cpp @@ -25,6 +25,9 @@ #include "./httpserver.hpp" #include "./littletest.hpp" +#include "test_utils.hpp" + +int test_port; using std::shared_ptr; @@ -39,7 +42,7 @@ using httpserver::create_webserver; #ifdef HTTPSERVER_PORT #define PORT HTTPSERVER_PORT #else -#define PORT 8080 +#define PORT test_port #endif // PORT #define STR2(p) #p @@ -57,7 +60,7 @@ LT_BEGIN_SUITE(threaded_suite) std::unique_ptr ws; void set_up() { - ws = std::make_unique(create_webserver(PORT)); + ws = std::make_unique(create_webserver(test_port)); ws->start(false); } @@ -75,7 +78,7 @@ LT_BEGIN_AUTO_TEST(threaded_suite, base) CURLcode res; curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -83,5 +86,6 @@ LT_BEGIN_AUTO_TEST(threaded_suite, base) LT_END_AUTO_TEST(base) LT_BEGIN_AUTO_TEST_ENV() + test_port = test_utils::get_random_port(); AUTORUN_TESTS() LT_END_AUTO_TEST_ENV() diff --git a/test/integ/threaded.cpp b/test/integ/threaded.cpp index 849285c5..116e2d49 100644 --- a/test/integ/threaded.cpp +++ b/test/integ/threaded.cpp @@ -29,6 +29,9 @@ #include "./httpserver.hpp" #include "./littletest.hpp" +#include "test_utils.hpp" + +int test_port; using std::shared_ptr; @@ -42,7 +45,7 @@ using httpserver::create_webserver; #ifdef HTTPSERVER_PORT #define PORT HTTPSERVER_PORT #else -#define PORT 8080 +#define PORT test_port #endif // PORT #define STR2(p) #p @@ -64,7 +67,7 @@ LT_BEGIN_SUITE(threaded_suite) void set_up() { #ifndef _WINDOWS - ws = std::make_unique(create_webserver(PORT).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5)); + ws = std::make_unique(create_webserver(test_port).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5)); ws->start(false); #endif } @@ -86,7 +89,7 @@ LT_BEGIN_AUTO_TEST(threaded_suite, base) CURLcode res; curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); res = curl_easy_perform(curl); LT_ASSERT_EQ(res, 0); @@ -95,5 +98,6 @@ LT_BEGIN_AUTO_TEST(threaded_suite, base) LT_END_AUTO_TEST(base) LT_BEGIN_AUTO_TEST_ENV() + test_port = test_utils::get_random_port(); AUTORUN_TESTS() LT_END_AUTO_TEST_ENV() diff --git a/test/integ/ws_start_stop.cpp b/test/integ/ws_start_stop.cpp index cb77dc59..a13f8700 100644 --- a/test/integ/ws_start_stop.cpp +++ b/test/integ/ws_start_stop.cpp @@ -37,13 +37,16 @@ #include "./httpserver.hpp" #include "./littletest.hpp" +#include "test_utils.hpp" + +int test_port; using std::shared_ptr; #ifdef HTTPSERVER_PORT #define PORT HTTPSERVER_PORT #else -#define PORT 8080 +#define PORT test_port #endif // PORT #define STR2(p) #p @@ -88,7 +91,7 @@ LT_END_SUITE(ws_start_stop_suite) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop) { // NOLINT (internal scope opening - not method start) - httpserver::webserver ws = httpserver::create_webserver(PORT); + httpserver::webserver ws = httpserver::create_webserver(test_port); ok_resource ok; LT_ASSERT_EQ(true, ws.register_resource("base", &ok)); ws.start(false); @@ -97,7 +100,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -110,7 +113,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop) } { - httpserver::webserver ws = httpserver::create_webserver(PORT).start_method(httpserver::http::http_utils::INTERNAL_SELECT); + httpserver::webserver ws = httpserver::create_webserver(test_port).start_method(httpserver::http::http_utils::INTERNAL_SELECT); ok_resource ok; LT_ASSERT_EQ(true, ws.register_resource("base", &ok)); ws.start(false); @@ -119,7 +122,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -132,7 +135,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop) } { - httpserver::webserver ws = httpserver::create_webserver(PORT).start_method(httpserver::http::http_utils::THREAD_PER_CONNECTION); + httpserver::webserver ws = httpserver::create_webserver(test_port).start_method(httpserver::http::http_utils::THREAD_PER_CONNECTION); ok_resource ok; LT_ASSERT_EQ(true, ws.register_resource("base", &ok)); ws.start(false); @@ -141,7 +144,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -154,62 +157,8 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop) } LT_END_AUTO_TEST(start_stop) -#if defined(IPV6_TESTS_ENABLED) - -LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ipv6) - { // NOLINT (internal scope opening - not method start) - httpserver::webserver ws = httpserver::create_webserver(PORT).use_ipv6(); - ok_resource ok; - LT_ASSERT_EQ(true, ws.register_resource("base", &ok)); - ws.start(false); - - curl_global_init(CURL_GLOBAL_ALL); - std::string s; - CURL *curl = curl_easy_init(); - CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:" PORT_STRING "/base"); - curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); - curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); - res = curl_easy_perform(curl); - LT_ASSERT_EQ(res, 0); - LT_CHECK_EQ(s, "OK"); - curl_easy_cleanup(curl); - - ws.stop(); - } -LT_END_AUTO_TEST(ipv6) - -LT_BEGIN_AUTO_TEST(ws_start_stop_suite, dual_stack) - { // NOLINT (internal scope opening - not method start) - httpserver::webserver ws = httpserver::create_webserver(PORT).use_dual_stack(); - ok_resource ok; - LT_ASSERT_EQ(true, ws.register_resource("base", &ok)); - ws.start(false); - - curl_global_init(CURL_GLOBAL_ALL); - std::string s; - CURL *curl = curl_easy_init(); - CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:" PORT_STRING "/base"); - curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); - curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); - res = curl_easy_perform(curl); - LT_ASSERT_EQ(res, 0); - LT_CHECK_EQ(s, "OK"); - curl_easy_cleanup(curl); - - ws.stop(); - } -LT_END_AUTO_TEST(dual_stack) - -#endif - LT_BEGIN_AUTO_TEST(ws_start_stop_suite, sweet_kill) - httpserver::webserver ws = httpserver::create_webserver(PORT); + httpserver::webserver ws = httpserver::create_webserver(test_port); ok_resource ok; LT_ASSERT_EQ(true, ws.register_resource("base", &ok)); ws.start(false); @@ -219,7 +168,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, sweet_kill) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -236,7 +185,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, sweet_kill) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -247,7 +196,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, sweet_kill) LT_END_AUTO_TEST(sweet_kill) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, disable_options) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .no_ssl() .no_ipv6() .no_debug() @@ -266,7 +215,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, disable_options) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -279,7 +228,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, disable_options) LT_END_AUTO_TEST(disable_options) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, enable_options) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .debug() .pedantic() .deferred() @@ -294,7 +243,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, enable_options) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -313,7 +262,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_socket) struct sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr("127.0.0.1"); - address.sin_port = htons(PORT); + address.sin_port = htons(test_port); bind(fd, (struct sockaddr*) &address, sizeof(address)); listen(fd, 10000); @@ -326,7 +275,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_socket) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -340,7 +289,7 @@ LT_END_AUTO_TEST(custom_socket) #endif LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource) - httpserver::webserver ws = httpserver::create_webserver(PORT).single_resource(); + httpserver::webserver ws = httpserver::create_webserver(test_port).single_resource(); ok_resource ok; LT_ASSERT_EQ(true, ws.register_resource("/", &ok, true)); ws.start(false); @@ -349,7 +298,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/any/url/works"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/any/url/works").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -362,7 +311,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource) LT_END_AUTO_TEST(single_resource) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource_not_default_resource) - httpserver::webserver ws = httpserver::create_webserver(PORT).single_resource(); + httpserver::webserver ws = httpserver::create_webserver(test_port).single_resource(); ok_resource ok; LT_CHECK_THROW(ws.register_resource("/other", &ok, true)); LT_CHECK_THROW(ws.register_resource("/", &ok, false)); @@ -373,7 +322,7 @@ LT_END_AUTO_TEST(single_resource_not_default_resource) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads) { // NOLINT (internal scope opening - not method start) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .start_method(httpserver::http::http_utils::THREAD_PER_CONNECTION) .max_threads(5); LT_CHECK_THROW(ws.start(false)); @@ -382,7 +331,7 @@ LT_END_AUTO_TEST(thread_per_connection_fails_with_max_threads) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads_stack_size) { // NOLINT (internal scope opening - not method start) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .start_method(httpserver::http::http_utils::THREAD_PER_CONNECTION) .max_thread_stack_size(4*1024*1024); LT_CHECK_THROW(ws.start(false)); @@ -390,7 +339,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_thr LT_END_AUTO_TEST(thread_per_connection_fails_with_max_threads_stack_size) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, tuning_options) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .max_connections(10) .max_threads(10) .memory_limit(10000) @@ -406,7 +355,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, tuning_options) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -419,7 +368,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, tuning_options) LT_END_AUTO_TEST(tuning_options) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_base) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .use_ssl() .https_mem_key(ROOT "/key.pem") .https_mem_cert(ROOT "/cert.pem"); @@ -434,7 +383,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_base) CURLcode res; curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl - curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("https://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -448,7 +397,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_base) LT_END_AUTO_TEST(ssl_base) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_protocol_priorities) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .use_ssl() .https_mem_key(ROOT "/key.pem") .https_mem_cert(ROOT "/cert.pem") @@ -464,7 +413,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_protocol_priorities) CURLcode res; curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl - curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("https://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -477,7 +426,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_protocol_priorities) LT_END_AUTO_TEST(ssl_with_protocol_priorities) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_trust) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .use_ssl() .https_mem_key(ROOT "/key.pem") .https_mem_cert(ROOT "/cert.pem") @@ -493,7 +442,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_trust) CURLcode res; curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl - curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("https://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -515,7 +464,7 @@ void* start_ws_blocking(void* par) { } LT_BEGIN_AUTO_TEST(ws_start_stop_suite, blocking_server) - httpserver::webserver ws = httpserver::create_webserver(PORT); + httpserver::webserver ws = httpserver::create_webserver(test_port); pthread_t tid; pthread_create(&tid, nullptr, start_ws_blocking, reinterpret_cast(&ws)); @@ -526,7 +475,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, blocking_server) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -544,7 +493,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, blocking_server) LT_END_AUTO_TEST(blocking_server) LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_error_resources) - httpserver::webserver ws = httpserver::create_webserver(PORT) + httpserver::webserver ws = httpserver::create_webserver(test_port) .not_found_resource(not_found_custom) .method_not_allowed_resource(not_allowed_custom); @@ -557,7 +506,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_error_resources) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -572,7 +521,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_error_resources) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/not_registered"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/not_registered").c_str()); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -594,7 +543,7 @@ LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_error_resources) std::string s; CURL *curl = curl_easy_init(); CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base"); + curl_easy_setopt(curl, CURLOPT_URL, ("http://localhost:" + std::to_string(test_port) + "/base").c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); @@ -615,5 +564,6 @@ LT_END_AUTO_TEST(custom_error_resources) #endif LT_BEGIN_AUTO_TEST_ENV() + test_port = test_utils::get_random_port(); AUTORUN_TESTS() LT_END_AUTO_TEST_ENV() diff --git a/test/littletest.hpp b/test/littletest.hpp index c26f6125..c8701dff 100644 --- a/test/littletest.hpp +++ b/test/littletest.hpp @@ -70,12 +70,13 @@ #define LT_BEGIN_TEST(__lt_suite_name__, __lt_test_name__) \ struct __lt_test_name__ ## _class: public __lt_suite_name__, littletest::test<__lt_test_name__ ## _class> \ { \ + using littletest::test_base::operator(); \ __lt_test_name__ ## _class() \ { \ __lt_name__ = #__lt_test_name__; \ littletest::auto_test_vector.push_back(this); \ } \ - void operator()(littletest::test_runner* __lt_tr__) \ + virtual void operator()(littletest::test_runner* __lt_tr__) \ { #define LT_END_TEST(__lt_test_name__) \ diff --git a/test/test_utils.hpp b/test/test_utils.hpp new file mode 100644 index 00000000..18cae3ce --- /dev/null +++ b/test/test_utils.hpp @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2024 Jules + * + * This file is part of libhttpserver. + */ +#ifndef TEST_TEST_UTILS_HPP_ +#define TEST_TEST_UTILS_HPP_ + +#include +#include +#include +#include + +namespace test_utils { + +int get_random_port() { + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == -1) { + return -1; + } + + sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = 0; // bind to a random port + + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { + close(sock); + return -1; + } + + socklen_t len = sizeof(addr); + if (getsockname(sock, (struct sockaddr *)&addr, &len) == -1) { + close(sock); + return -1; + } + + close(sock); + return ntohs(addr.sin_port); +} + +} // namespace test_utils + +#endif // TEST_TEST_UTILS_HPP_