12c593315Sopenharmony_ci/* 22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library 32c593315Sopenharmony_ci * 42c593315Sopenharmony_ci * Copyright (c) 2016 Tatsuhiro Tsujikawa 52c593315Sopenharmony_ci * 62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the 82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 122c593315Sopenharmony_ci * the following conditions: 132c593315Sopenharmony_ci * 142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be 152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software. 162c593315Sopenharmony_ci * 172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 242c593315Sopenharmony_ci */ 252c593315Sopenharmony_ci#include "shrpx_http_test.h" 262c593315Sopenharmony_ci 272c593315Sopenharmony_ci#ifdef HAVE_UNISTD_H 282c593315Sopenharmony_ci# include <unistd.h> 292c593315Sopenharmony_ci#endif // HAVE_UNISTD_H 302c593315Sopenharmony_ci 312c593315Sopenharmony_ci#include <cstdlib> 322c593315Sopenharmony_ci 332c593315Sopenharmony_ci#include <CUnit/CUnit.h> 342c593315Sopenharmony_ci 352c593315Sopenharmony_ci#include "shrpx_http.h" 362c593315Sopenharmony_ci#include "shrpx_config.h" 372c593315Sopenharmony_ci#include "shrpx_log.h" 382c593315Sopenharmony_ci 392c593315Sopenharmony_cinamespace shrpx { 402c593315Sopenharmony_ci 412c593315Sopenharmony_civoid test_shrpx_http_create_forwarded(void) { 422c593315Sopenharmony_ci BlockAllocator balloc(1024, 1024); 432c593315Sopenharmony_ci 442c593315Sopenharmony_ci CU_ASSERT("by=\"example.com:3000\";for=\"[::1]\";host=\"www.example.com\";" 452c593315Sopenharmony_ci "proto=https" == 462c593315Sopenharmony_ci http::create_forwarded(balloc, 472c593315Sopenharmony_ci FORWARDED_BY | FORWARDED_FOR | 482c593315Sopenharmony_ci FORWARDED_HOST | FORWARDED_PROTO, 492c593315Sopenharmony_ci StringRef::from_lit("example.com:3000"), 502c593315Sopenharmony_ci StringRef::from_lit("[::1]"), 512c593315Sopenharmony_ci StringRef::from_lit("www.example.com"), 522c593315Sopenharmony_ci StringRef::from_lit("https"))); 532c593315Sopenharmony_ci 542c593315Sopenharmony_ci CU_ASSERT("for=192.168.0.1" == 552c593315Sopenharmony_ci http::create_forwarded( 562c593315Sopenharmony_ci balloc, FORWARDED_FOR, StringRef::from_lit("alpha"), 572c593315Sopenharmony_ci StringRef::from_lit("192.168.0.1"), 582c593315Sopenharmony_ci StringRef::from_lit("bravo"), StringRef::from_lit("charlie"))); 592c593315Sopenharmony_ci 602c593315Sopenharmony_ci CU_ASSERT("by=_hidden;for=\"[::1]\"" == 612c593315Sopenharmony_ci http::create_forwarded( 622c593315Sopenharmony_ci balloc, FORWARDED_BY | FORWARDED_FOR, 632c593315Sopenharmony_ci StringRef::from_lit("_hidden"), StringRef::from_lit("[::1]"), 642c593315Sopenharmony_ci StringRef::from_lit(""), StringRef::from_lit(""))); 652c593315Sopenharmony_ci 662c593315Sopenharmony_ci CU_ASSERT("by=\"[::1]\";for=_hidden" == 672c593315Sopenharmony_ci http::create_forwarded( 682c593315Sopenharmony_ci balloc, FORWARDED_BY | FORWARDED_FOR, 692c593315Sopenharmony_ci StringRef::from_lit("[::1]"), StringRef::from_lit("_hidden"), 702c593315Sopenharmony_ci StringRef::from_lit(""), StringRef::from_lit(""))); 712c593315Sopenharmony_ci 722c593315Sopenharmony_ci CU_ASSERT("" == 732c593315Sopenharmony_ci http::create_forwarded( 742c593315Sopenharmony_ci balloc, 752c593315Sopenharmony_ci FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO, 762c593315Sopenharmony_ci StringRef::from_lit(""), StringRef::from_lit(""), 772c593315Sopenharmony_ci StringRef::from_lit(""), StringRef::from_lit(""))); 782c593315Sopenharmony_ci} 792c593315Sopenharmony_ci 802c593315Sopenharmony_civoid test_shrpx_http_create_via_header_value(void) { 812c593315Sopenharmony_ci std::array<char, 16> buf; 822c593315Sopenharmony_ci 832c593315Sopenharmony_ci auto end = http::create_via_header_value(std::begin(buf), 1, 1); 842c593315Sopenharmony_ci 852c593315Sopenharmony_ci CU_ASSERT(("1.1 nghttpx" == StringRef{std::begin(buf), end})); 862c593315Sopenharmony_ci 872c593315Sopenharmony_ci std::fill(std::begin(buf), std::end(buf), '\0'); 882c593315Sopenharmony_ci 892c593315Sopenharmony_ci end = http::create_via_header_value(std::begin(buf), 2, 0); 902c593315Sopenharmony_ci 912c593315Sopenharmony_ci CU_ASSERT(("2 nghttpx" == StringRef{std::begin(buf), end})); 922c593315Sopenharmony_ci} 932c593315Sopenharmony_ci 942c593315Sopenharmony_civoid test_shrpx_http_create_affinity_cookie(void) { 952c593315Sopenharmony_ci BlockAllocator balloc(1024, 1024); 962c593315Sopenharmony_ci StringRef c; 972c593315Sopenharmony_ci 982c593315Sopenharmony_ci c = http::create_affinity_cookie(balloc, StringRef::from_lit("cookie-val"), 992c593315Sopenharmony_ci 0xf1e2d3c4u, StringRef{}, false); 1002c593315Sopenharmony_ci 1012c593315Sopenharmony_ci CU_ASSERT("cookie-val=f1e2d3c4" == c); 1022c593315Sopenharmony_ci 1032c593315Sopenharmony_ci c = http::create_affinity_cookie(balloc, StringRef::from_lit("alpha"), 1042c593315Sopenharmony_ci 0x00000000u, StringRef{}, true); 1052c593315Sopenharmony_ci 1062c593315Sopenharmony_ci CU_ASSERT("alpha=00000000; Secure" == c); 1072c593315Sopenharmony_ci 1082c593315Sopenharmony_ci c = http::create_affinity_cookie(balloc, StringRef::from_lit("bravo"), 1092c593315Sopenharmony_ci 0x01111111u, StringRef::from_lit("bar"), 1102c593315Sopenharmony_ci false); 1112c593315Sopenharmony_ci 1122c593315Sopenharmony_ci CU_ASSERT("bravo=01111111; Path=bar" == c); 1132c593315Sopenharmony_ci 1142c593315Sopenharmony_ci c = http::create_affinity_cookie(balloc, StringRef::from_lit("charlie"), 1152c593315Sopenharmony_ci 0x01111111u, StringRef::from_lit("bar"), 1162c593315Sopenharmony_ci true); 1172c593315Sopenharmony_ci 1182c593315Sopenharmony_ci CU_ASSERT("charlie=01111111; Path=bar; Secure" == c); 1192c593315Sopenharmony_ci} 1202c593315Sopenharmony_ci 1212c593315Sopenharmony_civoid test_shrpx_http_create_altsvc_header_value(void) { 1222c593315Sopenharmony_ci { 1232c593315Sopenharmony_ci BlockAllocator balloc(1024, 1024); 1242c593315Sopenharmony_ci std::vector<AltSvc> altsvcs{ 1252c593315Sopenharmony_ci AltSvc{ 1262c593315Sopenharmony_ci .protocol_id = StringRef::from_lit("h3"), 1272c593315Sopenharmony_ci .host = StringRef::from_lit("127.0.0.1"), 1282c593315Sopenharmony_ci .service = StringRef::from_lit("443"), 1292c593315Sopenharmony_ci .params = StringRef::from_lit("ma=3600"), 1302c593315Sopenharmony_ci }, 1312c593315Sopenharmony_ci }; 1322c593315Sopenharmony_ci 1332c593315Sopenharmony_ci CU_ASSERT(R"(h3="127.0.0.1:443"; ma=3600)" == 1342c593315Sopenharmony_ci http::create_altsvc_header_value(balloc, altsvcs)); 1352c593315Sopenharmony_ci } 1362c593315Sopenharmony_ci 1372c593315Sopenharmony_ci { 1382c593315Sopenharmony_ci BlockAllocator balloc(1024, 1024); 1392c593315Sopenharmony_ci std::vector<AltSvc> altsvcs{ 1402c593315Sopenharmony_ci AltSvc{ 1412c593315Sopenharmony_ci .protocol_id = StringRef::from_lit("h3"), 1422c593315Sopenharmony_ci .service = StringRef::from_lit("443"), 1432c593315Sopenharmony_ci .params = StringRef::from_lit("ma=3600"), 1442c593315Sopenharmony_ci }, 1452c593315Sopenharmony_ci AltSvc{ 1462c593315Sopenharmony_ci .protocol_id = StringRef::from_lit("h3%"), 1472c593315Sopenharmony_ci .host = StringRef::from_lit("\"foo\""), 1482c593315Sopenharmony_ci .service = StringRef::from_lit("4433"), 1492c593315Sopenharmony_ci }, 1502c593315Sopenharmony_ci }; 1512c593315Sopenharmony_ci 1522c593315Sopenharmony_ci CU_ASSERT(R"(h3=":443"; ma=3600, h3%25="\"foo\":4433")" == 1532c593315Sopenharmony_ci http::create_altsvc_header_value(balloc, altsvcs)); 1542c593315Sopenharmony_ci } 1552c593315Sopenharmony_ci} 1562c593315Sopenharmony_ci 1572c593315Sopenharmony_civoid test_shrpx_http_check_http_scheme(void) { 1582c593315Sopenharmony_ci CU_ASSERT(http::check_http_scheme(StringRef::from_lit("https"), true)); 1592c593315Sopenharmony_ci CU_ASSERT(!http::check_http_scheme(StringRef::from_lit("https"), false)); 1602c593315Sopenharmony_ci CU_ASSERT(!http::check_http_scheme(StringRef::from_lit("http"), true)); 1612c593315Sopenharmony_ci CU_ASSERT(http::check_http_scheme(StringRef::from_lit("http"), false)); 1622c593315Sopenharmony_ci CU_ASSERT(!http::check_http_scheme(StringRef::from_lit("foo"), true)); 1632c593315Sopenharmony_ci CU_ASSERT(!http::check_http_scheme(StringRef::from_lit("foo"), false)); 1642c593315Sopenharmony_ci CU_ASSERT(!http::check_http_scheme(StringRef{}, true)); 1652c593315Sopenharmony_ci CU_ASSERT(!http::check_http_scheme(StringRef{}, false)); 1662c593315Sopenharmony_ci} 1672c593315Sopenharmony_ci 1682c593315Sopenharmony_ci} // namespace shrpx 169