diff --git a/plugins/header_rewrite/CMakeLists.txt b/plugins/header_rewrite/CMakeLists.txt index e1a8ef7bc68..028b80bb404 100644 --- a/plugins/header_rewrite/CMakeLists.txt +++ b/plugins/header_rewrite/CMakeLists.txt @@ -57,6 +57,12 @@ if(BUILD_TESTING) if(maxminddb_FOUND) target_link_libraries(test_header_rewrite PRIVATE maxminddb::maxminddb) endif() + + add_executable(test_matcher matcher_tests.cc matcher.cc lulu.cc regex_helper.cc resources.cc) + add_test(NAME test_matcher COMMAND $) + + target_link_libraries(test_matcher PRIVATE catch2::catch2 ts::tscore libswoc::libswoc PCRE::PCRE) + endif() verify_global_plugin(header_rewrite) verify_remap_plugin(header_rewrite) diff --git a/plugins/header_rewrite/header_rewrite.cc b/plugins/header_rewrite/header_rewrite.cc index 86f7bdf5291..9f5bdce815d 100644 --- a/plugins/header_rewrite/header_rewrite.cc +++ b/plugins/header_rewrite/header_rewrite.cc @@ -40,12 +40,6 @@ // Debugs namespace header_rewrite_ns { -const char PLUGIN_NAME[] = "header_rewrite"; -const char PLUGIN_NAME_DBG[] = "dbg_header_rewrite"; - -DbgCtl dbg_ctl{PLUGIN_NAME_DBG}; -DbgCtl pi_dbg_ctl{PLUGIN_NAME}; - std::once_flag initHRWLibs; PluginFactory plugin_factory; diff --git a/plugins/header_rewrite/matcher.cc b/plugins/header_rewrite/matcher.cc index 75faa6a2a12..f4aec365e1a 100644 --- a/plugins/header_rewrite/matcher.cc +++ b/plugins/header_rewrite/matcher.cc @@ -21,11 +21,21 @@ limitations under the License. */ +#include "tsutil/DbgCtl.h" #include #include #include "matcher.h" +namespace header_rewrite_ns +{ +const char PLUGIN_NAME[] = "header_rewrite"; +const char PLUGIN_NAME_DBG[] = "dbg_header_rewrite"; + +DbgCtl dbg_ctl{PLUGIN_NAME_DBG}; +DbgCtl pi_dbg_ctl{PLUGIN_NAME}; +} // namespace header_rewrite_ns + static bool match_with_modifiers(std::string_view rhs, std::string_view lhs, CondModifiers mods) { diff --git a/plugins/header_rewrite/matcher.h b/plugins/header_rewrite/matcher.h index 3099b300f87..1a61b3e505d 100644 --- a/plugins/header_rewrite/matcher.h +++ b/plugins/header_rewrite/matcher.h @@ -380,3 +380,11 @@ template class Matchers : public Matcher std::variant, swoc::IPRangeSet, regexHelper> _data; CondModifiers _mods = CondModifiers::NONE; }; + +// forward declare spcializations implemented in matcher.cc + +template <> bool Matchers::test_eq(const std::string &) const; + +template <> bool Matchers::test_set(const std::string &) const; + +template <> bool Matchers::test(const sockaddr *const &, const Resources &) const; diff --git a/plugins/header_rewrite/matcher_tests.cc b/plugins/header_rewrite/matcher_tests.cc new file mode 100644 index 00000000000..eb730bc4fd6 --- /dev/null +++ b/plugins/header_rewrite/matcher_tests.cc @@ -0,0 +1,109 @@ +/** + @file Test for matcher.cc + + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "matcher.h" +#include "ts/apidefs.h" +#define CATCH_CONFIG_MAIN +#include "catch.hpp" + +int +_TSAssert(const char *, const char *, int) +{ + return 0; +} + +void +TSError(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); +} + +TSHttpStatus +TSHttpHdrStatusGet(TSMBuffer, TSMLoc) +{ + return TS_HTTP_STATUS_OK; +} + +TSReturnCode +TSHandleMLocRelease(TSMBuffer, TSMLoc, TSMLoc) +{ + return TS_SUCCESS; +} + +const char * +TSHttpHookNameLookup(TSHttpHookID) +{ + return nullptr; +} + +TSReturnCode +TSHttpTxnClientReqGet(TSHttpTxn, TSMBuffer *, TSMLoc *) +{ + return TS_SUCCESS; +} + +TSReturnCode +TSHttpTxnServerReqGet(TSHttpTxn, TSMBuffer *, TSMLoc *) +{ + return TS_SUCCESS; +} + +TSReturnCode +TSHttpTxnClientRespGet(TSHttpTxn, TSMBuffer *, TSMLoc *) +{ + return TS_SUCCESS; +} + +TSReturnCode +TSHttpTxnServerRespGet(TSHttpTxn, TSMBuffer *, TSMLoc *) +{ + return TS_SUCCESS; +} + +ClassAllocator mutexAllocator("mutexAllocator"); + +TEST_CASE("Matcher", "[plugins][header_rewrite]") +{ + Matchers foo(MATCH_EQUAL); + TSHttpTxn txn = nullptr; + TSCont c = nullptr; + Resources res(txn, c); + + foo.set("FOO", CondModifiers::MOD_NOCASE); + REQUIRE(foo.test("foo", res) == true); +} + +TEST_CASE("MatcherSet", "[plugins][header_rewrite]") +{ + Matchers foo(MATCH_SET); + TSHttpTxn txn = nullptr; + TSCont c = nullptr; + Resources res(txn, c); + + foo.set("foo, bar, baz", CondModifiers::MOD_NOCASE); + REQUIRE(foo.test("FOO", res) == true); +}