From be5c39a1cf6489ab0d58187ca59ef22d2204c97e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 27 Oct 2017 23:16:01 +0900 Subject: [PATCH] src: Add tests --- src/shrpx-unittest.cc | 4 ++++ src/shrpx_downstream_test.cc | 34 ++++++++++++++++++++++++++++++++++ src/shrpx_downstream_test.h | 1 + src/shrpx_http_test.cc | 27 +++++++++++++++++++++++++++ src/shrpx_http_test.h | 1 + 5 files changed, 67 insertions(+) diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index 88c1dc40..7f9bd806 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -117,6 +117,8 @@ int main(int argc, char *argv[]) { shrpx::test_downstream_rewrite_location_response_header) || !CU_add_test(pSuite, "downstream_supports_non_final_response", shrpx::test_downstream_supports_non_final_response) || + !CU_add_test(pSuite, "downstream_find_affinity_cookie", + shrpx::test_downstream_find_affinity_cookie) || !CU_add_test(pSuite, "config_parse_header", shrpx::test_shrpx_config_parse_header) || !CU_add_test(pSuite, "config_parse_log_format", @@ -131,6 +133,8 @@ int main(int argc, char *argv[]) { shrpx::test_shrpx_http_create_forwarded) || !CU_add_test(pSuite, "http_create_via_header_value", shrpx::test_shrpx_http_create_via_header_value) || + !CU_add_test(pSuite, "http_create_affinity_cookie", + shrpx::test_shrpx_http_create_affinity_cookie) || !CU_add_test(pSuite, "router_match", shrpx::test_shrpx_router_match) || !CU_add_test(pSuite, "router_match_wildcard", shrpx::test_shrpx_router_match_wildcard) || diff --git a/src/shrpx_downstream_test.cc b/src/shrpx_downstream_test.cc index fc8c54a8..98514437 100644 --- a/src/shrpx_downstream_test.cc +++ b/src/shrpx_downstream_test.cc @@ -189,4 +189,38 @@ void test_downstream_supports_non_final_response(void) { CU_ASSERT(!d.supports_non_final_response()); } +void test_downstream_find_affinity_cookie(void) { + Downstream d(nullptr, nullptr, 0); + + auto &req = d.request(); + req.fs.add_header_token(StringRef::from_lit("cookie"), StringRef{}, false, + http2::HD_COOKIE); + req.fs.add_header_token(StringRef::from_lit("cookie"), + StringRef::from_lit("a=b;;c=d"), false, + http2::HD_COOKIE); + req.fs.add_header_token(StringRef::from_lit("content-length"), + StringRef::from_lit("599"), false, + http2::HD_CONTENT_LENGTH); + req.fs.add_header_token(StringRef::from_lit("cookie"), + StringRef::from_lit("lb=deadbeef;LB=f1f2f3f4"), false, + http2::HD_COOKIE); + req.fs.add_header_token(StringRef::from_lit("cookie"), + StringRef::from_lit("short=e1e2e3e"), false, + http2::HD_COOKIE); + + uint32_t aff; + + aff = d.find_affinity_cookie(StringRef::from_lit("lb")); + + CU_ASSERT(0xdeadbeef == aff); + + aff = d.find_affinity_cookie(StringRef::from_lit("LB")); + + CU_ASSERT(0xf1f2f3f4 == aff); + + aff = d.find_affinity_cookie(StringRef::from_lit("short")); + + CU_ASSERT(0 == aff); +} + } // namespace shrpx diff --git a/src/shrpx_downstream_test.h b/src/shrpx_downstream_test.h index c042de17..d9153ee5 100644 --- a/src/shrpx_downstream_test.h +++ b/src/shrpx_downstream_test.h @@ -37,6 +37,7 @@ void test_downstream_crumble_request_cookie(void); void test_downstream_assemble_request_cookie(void); void test_downstream_rewrite_location_response_header(void); void test_downstream_supports_non_final_response(void); +void test_downstream_find_affinity_cookie(void); } // namespace shrpx diff --git a/src/shrpx_http_test.cc b/src/shrpx_http_test.cc index eaf67adc..0fdcc93b 100644 --- a/src/shrpx_http_test.cc +++ b/src/shrpx_http_test.cc @@ -91,4 +91,31 @@ void test_shrpx_http_create_via_header_value(void) { CU_ASSERT(("2 nghttpx" == StringRef{std::begin(buf), end})); } +void test_shrpx_http_create_affinity_cookie(void) { + BlockAllocator balloc(1024, 1024); + StringRef c; + + c = http::create_affinity_cookie(balloc, StringRef::from_lit("cookie-val"), + 0xf1e2d3c4u, StringRef{}, false); + + CU_ASSERT("cookie-val=f1e2d3c4" == c); + + c = http::create_affinity_cookie(balloc, StringRef::from_lit("alpha"), + 0x00000000u, StringRef{}, true); + + CU_ASSERT("alpha=00000000; Secure" == c); + + c = http::create_affinity_cookie(balloc, StringRef::from_lit("bravo"), + 0x01111111u, StringRef::from_lit("bar"), + false); + + CU_ASSERT("bravo=01111111; Path=bar" == c); + + c = http::create_affinity_cookie(balloc, StringRef::from_lit("charlie"), + 0x01111111u, StringRef::from_lit("bar"), + true); + + CU_ASSERT("charlie=01111111; Path=bar; Secure" == c); +} + } // namespace shrpx diff --git a/src/shrpx_http_test.h b/src/shrpx_http_test.h index 7692c95a..85d2947d 100644 --- a/src/shrpx_http_test.h +++ b/src/shrpx_http_test.h @@ -33,6 +33,7 @@ namespace shrpx { void test_shrpx_http_create_forwarded(void); void test_shrpx_http_create_via_header_value(void); +void test_shrpx_http_create_affinity_cookie(void); } // namespace shrpx