From 49579ae0af60ad848b52a36f76e2fc5845144d95 Mon Sep 17 00:00:00 2001 From: helintongh Date: Fri, 15 Sep 2023 17:36:30 +0800 Subject: [PATCH] feat: add unittest for http parser --- example/CMakeLists.txt | 1 + include/cinatra/picohttpparser.h | 2 ++ press_tool/CMakeLists.txt | 1 + tests/CMakeLists.txt | 37 ++++++++++++++++++++- tests/test_http_parse.cpp | 57 ++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/test_http_parse.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 6d4edde3..75a60689 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -24,6 +24,7 @@ if (ENABLE_SIMD STREQUAL "AARCH64") if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64") add_library(neon INTERFACE IMPORTED) target_compile_options(neon INTERFACE -march=armv8-a+fp+simd) + target_link_libraries(${project_name} neon) endif () elseif (ENABLE_SIMD STREQUAL "SSE42") if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64") diff --git a/include/cinatra/picohttpparser.h b/include/cinatra/picohttpparser.h index ca2ade5b..b7a8a7c3 100644 --- a/include/cinatra/picohttpparser.h +++ b/include/cinatra/picohttpparser.h @@ -51,6 +51,8 @@ #ifdef _MSC_VER #define ssize_t intptr_t +#else +#include #endif namespace cinatra { diff --git a/press_tool/CMakeLists.txt b/press_tool/CMakeLists.txt index 5b2d8a51..a96e319e 100644 --- a/press_tool/CMakeLists.txt +++ b/press_tool/CMakeLists.txt @@ -27,6 +27,7 @@ if (ENABLE_SIMD STREQUAL "AARCH64") if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64") add_library(neon INTERFACE IMPORTED) target_compile_options(neon INTERFACE -march=armv8-a+fp+simd) + target_link_libraries(${project_name} neon) endif () elseif (ENABLE_SIMD STREQUAL "SSE42") if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ee04bf8f..87e3eaf2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -59,4 +59,39 @@ if (CINATRA_ENABLE_SSL) add_definitions(-DCINATRA_ENABLE_SSL) target_link_libraries(test_cinatra OpenSSL::SSL OpenSSL::Crypto) target_link_libraries(test_corofile PRIVATE OpenSSL::SSL OpenSSL::Crypto) -endif () \ No newline at end of file +endif () + +add_executable(test_http_parse + test_http_parse.cpp + ) + +if (ENABLE_SIMD STREQUAL "AARCH64") + if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64") + add_library(neon INTERFACE IMPORTED) + target_compile_options(neon INTERFACE -march=armv8-a+fp+simd) + target_link_libraries(test_http_parse neon) + endif () +elseif (ENABLE_SIMD STREQUAL "SSE42") + if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64") + add_library(sse4_2 INTERFACE IMPORTED) + if(MSVC) + target_compile_options(sse4_2 INTERFACE /arch:SSE4.2) + else() + target_compile_options(sse4_2 INTERFACE -msse4.2) + endif() + target_link_libraries(test_http_parse sse4_2) + endif () +elseif (ENABLE_SIMD STREQUAL "AVX2") + if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64") + add_library(avx2 INTERFACE IMPORTED) + if(MSVC) + target_compile_options(avx2 INTERFACE /arch:AVX2) + else() + target_compile_options(avx2 INTERFACE -mavx2) + endif() + target_link_libraries(test_http_parse avx2) + set(CMAKE_CXX_FLAGS "-fpermissive") + endif () +endif () + +add_test(NAME test_http_parse COMMAND test_http_parse) \ No newline at end of file diff --git a/tests/test_http_parse.cpp b/tests/test_http_parse.cpp new file mode 100644 index 00000000..04ea5d40 --- /dev/null +++ b/tests/test_http_parse.cpp @@ -0,0 +1,57 @@ +#define DOCTEST_CONFIG_IMPLEMENT + +#include "cinatra/picohttpparser.h" +#include "doctest/doctest.h" + +#include + +using namespace cinatra; + +#define REQ \ + "GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg " \ + "HTTP/1.1\r\n" \ + "Host: www.kittyhell.com\r\n" \ + "User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; " \ + "rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 " \ + "Pathtraq/0.9\r\n" \ + "Accept: " \ + "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" \ + "Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n" \ + "Accept-Encoding: gzip,deflate\r\n" \ + "Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n" \ + "Keep-Alive: 115\r\n" \ + "Connection: keep-alive\r\n" \ + "Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; " \ + "__utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; " \ + "__utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor." \ + "com|utmcct=/reader/|utmcmd=referral\r\n" \ + "\r\n" + +TEST_CASE("http parser test") +{ + const char *method; + size_t method_len; + const char *path; + size_t path_len; + int minor_version; + cinatra::http_header headers[64]; + size_t num_headers; + int i, ret; + + num_headers = sizeof(headers) / sizeof(headers[0]); + ret = cinatra::detail::phr_parse_request( + REQ, sizeof(REQ) - 1, &method, &method_len, &path, &path_len, + &minor_version, headers, &num_headers, 0); + CHECK(ret == 703); + CHECK(strncmp(method, "GET", method_len) == 0); + CHECK(minor_version == 1); + std::string name(headers[0].name); + std::string value(headers[0].value); + CHECK(name == "Host"); + CHECK(value == "www.kittyhell.com"); + +} + +DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007) +int main(int argc, char **argv) { return doctest::Context(argc, argv).run(); } +DOCTEST_MSVC_SUPPRESS_WARNING_POP \ No newline at end of file