From 3d8498b648de1fb7f7646f982cf93288e67589cc Mon Sep 17 00:00:00 2001 From: Lu Qiuwen Date: Tue, 18 Sep 2018 20:45:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0HTTP=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=B1=82=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + plugin/protocol/http/CMakeLists.txt | 10 +++ plugin/protocol/http/test/test_http_half.cpp | 83 ++++++++++++++++++++ vendor/CMakeLists.txt | 3 +- 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 plugin/protocol/http/test/test_http_half.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a3f6663..0dfc2f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall) +enable_testing() add_subdirectory(vendor) add_subdirectory(common) add_subdirectory(plugin) diff --git a/plugin/protocol/http/CMakeLists.txt b/plugin/protocol/http/CMakeLists.txt index 80b268b..c16e36d 100644 --- a/plugin/protocol/http/CMakeLists.txt +++ b/plugin/protocol/http/CMakeLists.txt @@ -1,3 +1,4 @@ +### PLUGIN add_library(http src/http_entry.cpp src/http_half.cpp) target_include_directories(http PRIVATE include/internal) @@ -6,3 +7,12 @@ target_include_directories(http PUBLIC incluce/external) target_link_libraries(http common) target_link_libraries(http http-parser-static) target_link_libraries(http libevent-static) + + +### UNITTEST CASE +add_executable(test-http-half test/test_http_half.cpp) +target_include_directories(test-http-half PRIVATE include/internal) +target_link_libraries(test-http-half gtest common http) + +include(GoogleTest) +gtest_discover_tests(test-http-half) diff --git a/plugin/protocol/http/test/test_http_half.cpp b/plugin/protocol/http/test/test_http_half.cpp new file mode 100644 index 0000000..a0cb1e7 --- /dev/null +++ b/plugin/protocol/http/test/test_http_half.cpp @@ -0,0 +1,83 @@ + +#include +#include + +static const char * __identify_http_request = + "POST /gen_204 HTTP/1.1\r\n" + "Host: www.google.com\r\n" + "Connection: close\r\n" + "Content-Length: 0\r\n" + "Origin: https://www.google.com\r\n" + "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + "Accept: */*\r\n" + "X-Client-Data: CJG2yQEIorbJAQjEtskBCKmdygEI2J3KAQjZncoBCKijygEY+aXKAQ==\r\n" + "Referer: https://www.google.com/\r\n" + "Accept-Encoding: gzip, deflate\r\n" + "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7\r\n"; + +static const char * __identify_not_http_request = "MZMZ"; + +TEST(HttpHalfConstruct, ProtoIdentifyIsHTTP) +{ + auto * hf_request = hf_private_create(TFE_HTTP_REQUEST, 1, 1); + ASSERT_TRUE(hf_request != NULL); + + int ret = hf_private_parse(hf_request, (const unsigned char *) __identify_http_request, + strlen(__identify_http_request)); + + EXPECT_EQ(ret, 0); +} + +TEST(HttpHalfConstruct, ProtoIdentifyNotHTTP) +{ + auto * hf_request = hf_private_create(TFE_HTTP_REQUEST, 1, 1); + ASSERT_TRUE(hf_request != NULL); + + int ret = hf_private_parse(hf_request, (const unsigned char *) __identify_not_http_request, + strlen(__identify_not_http_request)); + + EXPECT_EQ(ret, -1); +} + +static const char * __get_http_request_01 = + "GET /gfwlist/gfwlist/master/gfwlist.txt HTTP/1.1\r\n" + "Host: raw.githubusercontent.com\r\n" + "Connection: close\r\n" + "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\r\n" + "Accept: */*\r\n" + "Accept-Encoding: gzip, deflate\r\n" + "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7\r\n" + "If-None-Match: \"023aeae5eafc12082067c36031888adb3bafa797\"\r\n" + "\r\n"; + +void __get_http_request_01_verify_helper(struct http_half_private * hf_private) +{ + auto * hf_public = &hf_private->hf_public; + EXPECT_EQ(hf_public->major_version, 1); + EXPECT_EQ(hf_public->minor_version, 1); + + auto * hf_public_request = &hf_public->req_spec; + EXPECT_STREQ(hf_public_request->uri, "/gfwlist/gfwlist/master/gfwlist.txt"); + EXPECT_STREQ(hf_public_request->url, "/gfwlist/gfwlist/master/gfwlist.txt"); + EXPECT_STREQ(hf_public_request->host, "raw.githubusercontent.com"); + EXPECT_EQ(hf_public_request->method, TFE_HTTP_GET); +} + +TEST(HttpHalfConstruct, GetWithNoBody) +{ + auto * hf_request = hf_private_create(TFE_HTTP_REQUEST, 0, 0); + ASSERT_TRUE(hf_request != NULL); + + int ret = + hf_private_parse(hf_request, (const unsigned char *) __get_http_request_01, strlen(__get_http_request_01)); + ASSERT_EQ(ret, 0); + + __get_http_request_01_verify_helper(hf_request); +} + +int main(int argc, char ** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt index 19dce43..edb79d9 100644 --- a/vendor/CMakeLists.txt +++ b/vendor/CMakeLists.txt @@ -112,9 +112,10 @@ file(MAKE_DIRECTORY ${INSTALL_DIR}/include) add_library(gtest STATIC IMPORTED GLOBAL) set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgtest.a) set_property(TARGET gtest PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include) +set_property(TARGET gtest PROPERTY INTERFACE_LINK_LIBRARIES pthread) add_library(gmock STATIC IMPORTED GLOBAL) -set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgtest.a) +set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgmock.a) set_property(TARGET gmock PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)