#include "shrpx_worker_test.h"
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <cstdlib>
#include "munitxx.h"
#include "shrpx_worker.h"
#include "shrpx_connect_blocker.h"
#include "shrpx_log.h"
namespace shrpx {
namespace {
const MunitTest tests[]{
munit_void_test(test_shrpx_worker_match_downstream_addr_group),
munit_test_end(),
};
}
const MunitSuite worker_suite{
"/worker", tests, nullptr, 1, MUNIT_SUITE_OPTION_NONE,
};
void test_shrpx_worker_match_downstream_addr_group(void) {
auto groups = std::vector<std::shared_ptr<DownstreamAddrGroup>>();
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",
"example.com/alpha/bravo", "192.168.0.1/alpha/", "/golf/"}) {
auto g = std::make_shared<DownstreamAddrGroup>();
g->pattern = ImmutableString(s);
groups.push_back(std::move(g));
}
BlockAllocator balloc(1024, 1024);
RouterConfig routerconf;
auto &router = routerconf.router;
auto &wcrouter = routerconf.rev_wildcard_router;
auto &wp = routerconf.wildcard_patterns;
for (size_t i = 0; i < groups.size(); ++i) {
auto &g = groups[i];
router.add_route(std::string_view{std::ranges::begin(g->pattern),
std::ranges::end(g->pattern)},
i);
}
assert_size(0, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv, "/"sv,
groups, 255, balloc));
assert_size(0, ==,
match_downstream_addr_group(routerconf, "nghttp2.org:8080"sv,
"/"sv, groups, 255, balloc));
assert_size(4, ==,
match_downstream_addr_group(routerconf, "WWW.nghttp2.org"sv,
"/alpha"sv, groups, 255, balloc));
assert_size(1, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/alpha/bravo/"sv, groups, 255,
balloc));
assert_size(1, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/alpha/bravo"sv, groups, 255,
balloc));
assert_size(0, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/Alpha/bravo"sv, groups, 255,
balloc));
assert_size(1, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/alpha/bravo/charlie"sv, groups, 255,
balloc));
assert_size(2, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/alpha/charlie"sv, groups, 255,
balloc));
assert_size(0, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/alpha/charlie/"sv, groups, 255,
balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, "example.org"sv, "/"sv,
groups, 255, balloc));
assert_size(
255, ==,
match_downstream_addr_group(routerconf, ""sv, "/"sv, groups, 255, balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, ""sv, "alpha"sv, groups,
255, balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, "foo/bar"sv, "/"sv,
groups, 255, balloc));
assert_size(0, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv, "*"sv,
groups, 255, balloc));
assert_size(5, ==,
match_downstream_addr_group(routerconf, "[::1]"sv, "/"sv, groups,
255, balloc));
assert_size(5, ==,
match_downstream_addr_group(routerconf, "[::1]:8080"sv, "/"sv,
groups, 255, balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, "[::1"sv, "/"sv, groups,
255, balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, "[::1]8000"sv, "/"sv,
groups, 255, balloc));
assert_size(6, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/alpha/bravo/delta"sv, groups, 255,
balloc));
assert_size(1, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/alpha/bravo/delta/"sv, groups, 255,
balloc));
assert_size(7, ==,
match_downstream_addr_group(routerconf, "example.com"sv,
"/alpha/bravo"sv, groups, 255,
balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, "example.com"sv,
"/alpha/bravo/"sv, groups, 255,
balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, "example.com"sv,
"/alpha"sv, groups, 255, balloc));
assert_size(8, ==,
match_downstream_addr_group(routerconf, "192.168.0.1"sv,
"/alpha"sv, groups, 255, balloc));
assert_size(8, ==,
match_downstream_addr_group(routerconf, "192.168.0.1"sv,
"/alpha/"sv, groups, 255, balloc));
assert_size(8, ==,
match_downstream_addr_group(routerconf, "192.168.0.1"sv,
"/alpha/bravo"sv, groups, 255,
balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, "192.168.0.1"sv,
"/alph"sv, groups, 255, balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, "192.168.0.1"sv, "/"sv,
groups, 255, balloc));
auto g1 = std::make_shared<DownstreamAddrGroup>();
g1->pattern = "git.nghttp2.org"_is;
groups.push_back(std::move(g1));
auto g2 = std::make_shared<DownstreamAddrGroup>();
g2->pattern = ".nghttp2.org"_is;
groups.push_back(std::move(g2));
auto g3 = std::make_shared<DownstreamAddrGroup>();
g3->pattern = ".local"_is;
groups.push_back(std::move(g3));
wp.emplace_back("git.nghttp2.org"sv);
wcrouter.add_route("gro.2ptthgn.tig"sv, 0);
wp.back().router.add_route("/echo/"sv, 10);
wp.emplace_back(".nghttp2.org"sv);
wcrouter.add_route("gro.2ptthgn."sv, 1);
wp.back().router.add_route("/echo/"sv, 11);
wp.back().router.add_route("/echo/foxtrot"sv, 12);
wp.emplace_back(".local"sv);
wcrouter.add_route("lacol."sv, 2);
wp.back().router.add_route("/"sv, 13);
assert_size(11, ==,
match_downstream_addr_group(routerconf, "git.nghttp2.org"sv,
"/echo"sv, groups, 255, balloc));
assert_size(10, ==,
match_downstream_addr_group(routerconf, "0git.nghttp2.org"sv,
"/echo"sv, groups, 255, balloc));
assert_size(11, ==,
match_downstream_addr_group(routerconf, "it.nghttp2.org"sv,
"/echo"sv, groups, 255, balloc));
assert_size(255, ==,
match_downstream_addr_group(routerconf, ".nghttp2.org"sv,
"/echo/foxtrot"sv, groups, 255,
balloc));
assert_size(9, ==,
match_downstream_addr_group(routerconf, "alpha.nghttp2.org"sv,
"/golf"sv, groups, 255, balloc));
assert_size(0, ==,
match_downstream_addr_group(routerconf, "nghttp2.org"sv,
"/echo"sv, groups, 255, balloc));
assert_size(13, ==,
match_downstream_addr_group(routerconf, "test.local"sv, ""sv,
groups, 255, balloc));
}
}