From 124d4c9fad736dbb16acb594b2988c21ba7f668b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 28 Feb 2016 23:47:23 +0900 Subject: [PATCH] src: Fix tests --- src/Makefile.am | 1 + src/shrpx-unittest.cc | 5 +- src/shrpx_config_test.cc | 116 ----------------------- src/shrpx_downstream_test.cc | 4 +- src/shrpx_http_test.cc | 25 ++--- src/shrpx_worker_test.cc | 179 +++++++++++++++++++++++++++++++++++ src/shrpx_worker_test.h | 38 ++++++++ src/util_test.cc | 15 ++- 8 files changed, 247 insertions(+), 136 deletions(-) create mode 100644 src/shrpx_worker_test.cc create mode 100644 src/shrpx_worker_test.h diff --git a/src/Makefile.am b/src/Makefile.am index 2c8738931f..cd75fdf419 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -169,6 +169,7 @@ nghttpx_unittest_SOURCES = shrpx-unittest.cc \ shrpx_ssl_test.cc shrpx_ssl_test.h \ shrpx_downstream_test.cc shrpx_downstream_test.h \ shrpx_config_test.cc shrpx_config_test.h \ + shrpx_worker_test.cc shrpx_worker_test.h \ shrpx_http_test.cc shrpx_http_test.h \ http2_test.cc http2_test.h \ util_test.cc util_test.h \ diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index 8eba2c65eb..bebc0f2885 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -33,6 +33,7 @@ #include "shrpx_ssl_test.h" #include "shrpx_downstream_test.h" #include "shrpx_config_test.h" +#include "shrpx_worker_test.h" #include "http2_test.h" #include "util_test.h" #include "nghttp2_gzip_test.h" @@ -118,8 +119,8 @@ int main(int argc, char *argv[]) { shrpx::test_shrpx_config_read_tls_ticket_key_file) || !CU_add_test(pSuite, "config_read_tls_ticket_key_file_aes_256", shrpx::test_shrpx_config_read_tls_ticket_key_file_aes_256) || - !CU_add_test(pSuite, "config_match_downstream_addr_group", - shrpx::test_shrpx_config_match_downstream_addr_group) || + !CU_add_test(pSuite, "worker_match_downstream_addr_group", + shrpx::test_shrpx_worker_match_downstream_addr_group) || !CU_add_test(pSuite, "http_create_forwarded", shrpx::test_shrpx_http_create_forwarded) || !CU_add_test(pSuite, "util_streq", shrpx::test_util_streq) || diff --git a/src/shrpx_config_test.cc b/src/shrpx_config_test.cc index 53b1bc303c..635c23d957 100644 --- a/src/shrpx_config_test.cc +++ b/src/shrpx_config_test.cc @@ -238,120 +238,4 @@ void test_shrpx_config_read_tls_ticket_key_file_aes_256(void) { "a..............................b")); } -void test_shrpx_config_match_downstream_addr_group(void) { - auto groups = std::vector{ - {"nghttp2.org/"}, - {"nghttp2.org/alpha/bravo/"}, - {"nghttp2.org/alpha/charlie"}, - {"nghttp2.org/delta%3A"}, - {"www.nghttp2.org/"}, - {"[::1]/"}, - {"nghttp2.org/alpha/bravo/delta"}, - // Check that match is done in the single node - {"example.com/alpha/bravo"}, - {"192.168.0.1/alpha/"}, - }; - - Router router; - - for (size_t i = 0; i < groups.size(); ++i) { - auto &g = groups[i]; - router.add_route(StringRef{g.pattern}, i); - } - - CU_ASSERT(0 == match_downstream_addr_group(router, "nghttp2.org", "/", groups, - 255)); - - // port is removed - CU_ASSERT(0 == match_downstream_addr_group(router, "nghttp2.org:8080", "/", - groups, 255)); - - // host is case-insensitive - CU_ASSERT(4 == match_downstream_addr_group(router, "WWW.nghttp2.org", - "/alpha", groups, 255)); - - CU_ASSERT(1 == match_downstream_addr_group(router, "nghttp2.org", - "/alpha/bravo/", groups, 255)); - - // /alpha/bravo also matches /alpha/bravo/ - CU_ASSERT(1 == match_downstream_addr_group(router, "nghttp2.org", - "/alpha/bravo", groups, 255)); - - // path part is case-sensitive - CU_ASSERT(0 == match_downstream_addr_group(router, "nghttp2.org", - "/Alpha/bravo", groups, 255)); - - CU_ASSERT(1 == match_downstream_addr_group(router, "nghttp2.org", - "/alpha/bravo/charlie", groups, - 255)); - - CU_ASSERT(2 == match_downstream_addr_group(router, "nghttp2.org", - "/alpha/charlie", groups, 255)); - - // pattern which does not end with '/' must match its entirely. So - // this matches to group 0, not group 2. - CU_ASSERT(0 == match_downstream_addr_group(router, "nghttp2.org", - "/alpha/charlie/", groups, 255)); - - CU_ASSERT(255 == match_downstream_addr_group(router, "example.org", "/", - groups, 255)); - - CU_ASSERT(255 == match_downstream_addr_group(router, "", "/", groups, 255)); - - CU_ASSERT(255 == - match_downstream_addr_group(router, "", "alpha", groups, 255)); - - CU_ASSERT(255 == - match_downstream_addr_group(router, "foo/bar", "/", groups, 255)); - - // If path is "*", only match with host + "/". - CU_ASSERT(0 == match_downstream_addr_group(router, "nghttp2.org", "*", groups, - 255)); - - CU_ASSERT(5 == - match_downstream_addr_group(router, "[::1]", "/", groups, 255)); - CU_ASSERT( - 5 == match_downstream_addr_group(router, "[::1]:8080", "/", groups, 255)); - CU_ASSERT(255 == - match_downstream_addr_group(router, "[::1", "/", groups, 255)); - CU_ASSERT(255 == - match_downstream_addr_group(router, "[::1]8000", "/", groups, 255)); - - // Check the case where adding route extends tree - CU_ASSERT(6 == match_downstream_addr_group( - router, "nghttp2.org", "/alpha/bravo/delta", groups, 255)); - - CU_ASSERT(1 == match_downstream_addr_group(router, "nghttp2.org", - "/alpha/bravo/delta/", groups, - 255)); - - // Check the case where query is done in a single node - CU_ASSERT(7 == match_downstream_addr_group(router, "example.com", - "/alpha/bravo", groups, 255)); - - CU_ASSERT(255 == match_downstream_addr_group(router, "example.com", - "/alpha/bravo/", groups, 255)); - - CU_ASSERT(255 == match_downstream_addr_group(router, "example.com", "/alpha", - groups, 255)); - - // Check the case where quey is done in a single node - CU_ASSERT(8 == match_downstream_addr_group(router, "192.168.0.1", "/alpha", - groups, 255)); - - CU_ASSERT(8 == match_downstream_addr_group(router, "192.168.0.1", "/alpha/", - groups, 255)); - - CU_ASSERT(8 == match_downstream_addr_group(router, "192.168.0.1", - "/alpha/bravo", groups, 255)); - - CU_ASSERT(255 == match_downstream_addr_group(router, "192.168.0.1", "/alph", - groups, 255)); - - CU_ASSERT(255 == match_downstream_addr_group(router, "192.168.0.1", "/", - groups, 255)); - - router.dump(); -} - } // namespace shrpx diff --git a/src/shrpx_downstream_test.cc b/src/shrpx_downstream_test.cc index 5b496f9b1b..d9989a7206 100644 --- a/src/shrpx_downstream_test.cc +++ b/src/shrpx_downstream_test.cc @@ -77,8 +77,8 @@ void test_downstream_field_store_header(void) { CU_ASSERT(nullptr == fs.header(http2::HD__METHOD)); // By name - CU_ASSERT(Header("alpha", "0") == *fs.header("alpha")); - CU_ASSERT(nullptr == fs.header("bravo")); + CU_ASSERT(Header("alpha", "0") == *fs.header(StringRef::from_lit("alpha"))); + CU_ASSERT(nullptr == fs.header(StringRef::from_lit("bravo"))); } void test_downstream_crumble_request_cookie(void) { diff --git a/src/shrpx_http_test.cc b/src/shrpx_http_test.cc index 721b53f529..0351055039 100644 --- a/src/shrpx_http_test.cc +++ b/src/shrpx_http_test.cc @@ -40,25 +40,28 @@ namespace shrpx { void test_shrpx_http_create_forwarded(void) { CU_ASSERT("by=\"example.com:3000\";for=\"[::1]\";host=\"www.example.com\";" "proto=https" == - http::create_forwarded( - FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO, - "example.com:3000", "[::1]", "www.example.com", "https")); + http::create_forwarded(FORWARDED_BY | FORWARDED_FOR | + FORWARDED_HOST | FORWARDED_PROTO, + StringRef::from_lit("example.com:3000"), + "[::1]", "www.example.com", "https")); - CU_ASSERT("for=192.168.0.1" == http::create_forwarded(FORWARDED_FOR, "alpha", - "192.168.0.1", "bravo", - "charlie")); + CU_ASSERT("for=192.168.0.1" == + http::create_forwarded(FORWARDED_FOR, StringRef::from_lit("alpha"), + "192.168.0.1", "bravo", "charlie")); CU_ASSERT("by=_hidden;for=\"[::1]\"" == - http::create_forwarded(FORWARDED_BY | FORWARDED_FOR, "_hidden", - "[::1]", "", "")); + http::create_forwarded(FORWARDED_BY | FORWARDED_FOR, + StringRef::from_lit("_hidden"), "[::1]", "", + "")); CU_ASSERT("by=\"[::1]\";for=_hidden" == - http::create_forwarded(FORWARDED_BY | FORWARDED_FOR, "[::1]", - "_hidden", "", "")); + http::create_forwarded(FORWARDED_BY | FORWARDED_FOR, + StringRef::from_lit("[::1]"), "_hidden", "", + "")); CU_ASSERT("" == http::create_forwarded(FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO, - "", "", "", "")); + StringRef::from_lit(""), "", "", "")); } } // namespace shrpx diff --git a/src/shrpx_worker_test.cc b/src/shrpx_worker_test.cc new file mode 100644 index 0000000000..11a15e0102 --- /dev/null +++ b/src/shrpx_worker_test.cc @@ -0,0 +1,179 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2016 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "shrpx_worker_test.h" + +#ifdef HAVE_UNISTD_H +#include +#endif // HAVE_UNISTD_H + +#include + +#include + +#include "shrpx_worker.h" +#include "shrpx_connect_blocker.h" + +namespace shrpx { + +void test_shrpx_worker_match_downstream_addr_group(void) { + auto groups = std::vector(); + for (auto &s : {"nghttp2.org/", "nghttp2.org/alpha/bravo/", + "nghttp2.org/alpha/charlie", "nghttp2.org/delta%3A", + "www.nghttp2.org/", "[::1]/", "nghttp2.org/alpha/bravo/delta", + // Check that match is done in the single node + "example.com/alpha/bravo", "192.168.0.1/alpha/"}) { + groups.push_back(DownstreamAddrGroup{ImmutableString(s)}); + } + + Router router; + + for (size_t i = 0; i < groups.size(); ++i) { + auto &g = groups[i]; + router.add_route(StringRef{g.pattern}, i); + } + + CU_ASSERT(0 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/"), groups, 255)); + + // port is removed + CU_ASSERT(0 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org:8080"), + StringRef::from_lit("/"), groups, 255)); + + // host is case-insensitive + CU_ASSERT(4 == match_downstream_addr_group( + router, StringRef::from_lit("WWW.nghttp2.org"), + StringRef::from_lit("/alpha"), groups, 255)); + + CU_ASSERT(1 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/alpha/bravo/"), groups, 255)); + + // /alpha/bravo also matches /alpha/bravo/ + CU_ASSERT(1 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/alpha/bravo"), groups, 255)); + + // path part is case-sensitive + CU_ASSERT(0 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/Alpha/bravo"), groups, 255)); + + CU_ASSERT(1 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/alpha/bravo/charlie"), groups, 255)); + + CU_ASSERT(2 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/alpha/charlie"), groups, 255)); + + // pattern which does not end with '/' must match its entirely. So + // this matches to group 0, not group 2. + CU_ASSERT(0 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/alpha/charlie/"), groups, 255)); + + CU_ASSERT(255 == match_downstream_addr_group( + router, StringRef::from_lit("example.org"), + StringRef::from_lit("/"), groups, 255)); + + CU_ASSERT(255 == match_downstream_addr_group(router, StringRef::from_lit(""), + StringRef::from_lit("/"), groups, + 255)); + + CU_ASSERT(255 == match_downstream_addr_group(router, StringRef::from_lit(""), + StringRef::from_lit("alpha"), + groups, 255)); + + CU_ASSERT(255 == + match_downstream_addr_group(router, StringRef::from_lit("foo/bar"), + StringRef::from_lit("/"), groups, 255)); + + // If path is StringRef::from_lit("*", only match with host + "/"). + CU_ASSERT(0 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("*"), groups, 255)); + + CU_ASSERT(5 == + match_downstream_addr_group(router, StringRef::from_lit("[::1]"), + StringRef::from_lit("/"), groups, 255)); + CU_ASSERT(5 == match_downstream_addr_group( + router, StringRef::from_lit("[::1]:8080"), + StringRef::from_lit("/"), groups, 255)); + CU_ASSERT(255 == + match_downstream_addr_group(router, StringRef::from_lit("[::1"), + StringRef::from_lit("/"), groups, 255)); + CU_ASSERT(255 == match_downstream_addr_group( + router, StringRef::from_lit("[::1]8000"), + StringRef::from_lit("/"), groups, 255)); + + // Check the case where adding route extends tree + CU_ASSERT(6 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/alpha/bravo/delta"), groups, 255)); + + CU_ASSERT(1 == match_downstream_addr_group( + router, StringRef::from_lit("nghttp2.org"), + StringRef::from_lit("/alpha/bravo/delta/"), groups, 255)); + + // Check the case where query is done in a single node + CU_ASSERT(7 == match_downstream_addr_group( + router, StringRef::from_lit("example.com"), + StringRef::from_lit("/alpha/bravo"), groups, 255)); + + CU_ASSERT(255 == match_downstream_addr_group( + router, StringRef::from_lit("example.com"), + StringRef::from_lit("/alpha/bravo/"), groups, 255)); + + CU_ASSERT(255 == match_downstream_addr_group( + router, StringRef::from_lit("example.com"), + StringRef::from_lit("/alpha"), groups, 255)); + + // Check the case where quey is done in a single node + CU_ASSERT(8 == match_downstream_addr_group( + router, StringRef::from_lit("192.168.0.1"), + StringRef::from_lit("/alpha"), groups, 255)); + + CU_ASSERT(8 == match_downstream_addr_group( + router, StringRef::from_lit("192.168.0.1"), + StringRef::from_lit("/alpha/"), groups, 255)); + + CU_ASSERT(8 == match_downstream_addr_group( + router, StringRef::from_lit("192.168.0.1"), + StringRef::from_lit("/alpha/bravo"), groups, 255)); + + CU_ASSERT(255 == match_downstream_addr_group( + router, StringRef::from_lit("192.168.0.1"), + StringRef::from_lit("/alph"), groups, 255)); + + CU_ASSERT(255 == match_downstream_addr_group( + router, StringRef::from_lit("192.168.0.1"), + StringRef::from_lit("/"), groups, 255)); + + router.dump(); +} + +} // namespace shrpx diff --git a/src/shrpx_worker_test.h b/src/shrpx_worker_test.h new file mode 100644 index 0000000000..c9bdfe0d9e --- /dev/null +++ b/src/shrpx_worker_test.h @@ -0,0 +1,38 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2016 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef SHRPX_WORKER_TEST_H +#define SHRPX_WORKER_TEST_H + +#ifdef HAVE_CONFIG_H +#include +#endif // HAVE_CONFIG_H + +namespace shrpx { + +void test_shrpx_worker_match_downstream_addr_group(void); + +} // namespace shrpx + +#endif // SHRPX_WORKER_TEST_H diff --git a/src/util_test.cc b/src/util_test.cc index 6f605c5b66..cd8e2b0280 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -456,14 +456,19 @@ void test_util_parse_config_str_list(void) { } void test_util_make_http_hostport(void) { - CU_ASSERT("localhost" == util::make_http_hostport("localhost", 80)); - CU_ASSERT("[::1]" == util::make_http_hostport("::1", 443)); - CU_ASSERT("localhost:3000" == util::make_http_hostport("localhost", 3000)); + CU_ASSERT("localhost" == + util::make_http_hostport(StringRef::from_lit("localhost"), 80)); + CU_ASSERT("[::1]" == + util::make_http_hostport(StringRef::from_lit("::1"), 443)); + CU_ASSERT("localhost:3000" == + util::make_http_hostport(StringRef::from_lit("localhost"), 3000)); } void test_util_make_hostport(void) { - CU_ASSERT("localhost:80" == util::make_hostport("localhost", 80)); - CU_ASSERT("[::1]:443" == util::make_hostport("::1", 443)); + CU_ASSERT("localhost:80" == + util::make_hostport(StringRef::from_lit("localhost"), 80)); + CU_ASSERT("[::1]:443" == + util::make_hostport(StringRef::from_lit("::1"), 443)); } } // namespace shrpx