blob: c81fc757d25c05f57f4452a18a26f4155f0c65d9 [file]
/** @file
Unit tests for ControlMatcher.
@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 "proxy/CacheControl.h"
#include "proxy/ControlMatcher.h"
#include "tscore/MatcherUtils.h"
#include "tscore/ink_memory.h"
#include <catch2/catch_test_macros.hpp>
#include <string_view>
namespace
{
class TestRequestData : public HttpRequestData
{
public:
explicit TestRequestData(std::string_view url) : _url(url) {}
char *
get_string() override
{
return ats_strndup(_url.data(), _url.size());
}
private:
std::string_view _url;
};
void
parse_line(char *text, matcher_line &line, int line_number)
{
REQUIRE(parseConfigLine(text, &line, &http_dest_tags) == nullptr);
line.line_num = line_number;
}
} // namespace
TEST_CASE("UrlMatcher inserts and matches exact URLs", "[ControlMatcher]")
{
UrlMatcher<CacheControlRecord, CacheControlResult> matcher{"CacheControl", "cache.config"};
char config[] = "url=http://example.com/exact action=never-cache";
matcher_line line;
matcher.AllocateSpace(1);
parse_line(config, line, 1);
Result result = matcher.NewEntry(&line);
REQUIRE_FALSE(result.failed());
REQUIRE(matcher.num_el == 1);
TestRequestData exact_request{"http://example.com/exact"};
CacheControlResult exact_result;
matcher.Match(&exact_request, &exact_result);
CHECK(exact_result.never_cache);
TestRequestData other_request{"http://example.com/other"};
CacheControlResult other_result;
matcher.Match(&other_request, &other_result);
CHECK_FALSE(other_result.never_cache);
}
TEST_CASE("UrlMatcher does not insert invalid records", "[ControlMatcher]")
{
UrlMatcher<CacheControlRecord, CacheControlResult> matcher{"CacheControl", "cache.config"};
char config[] = "url=http://example.com/exact action=invalid";
matcher_line line;
matcher.AllocateSpace(1);
parse_line(config, line, 1);
Result result = matcher.NewEntry(&line);
CHECK(result.failed());
CHECK(matcher.num_el == 0);
}
TEST_CASE("UrlMatcher rejects duplicate URLs", "[ControlMatcher]")
{
UrlMatcher<CacheControlRecord, CacheControlResult> matcher{"CacheControl", "cache.config"};
char first_config[] = "url=http://example.com/exact action=never-cache";
char second_config[] = "url=http://example.com/exact action=standard-cache";
matcher_line first_line;
matcher_line second_line;
matcher.AllocateSpace(2);
parse_line(first_config, first_line, 1);
parse_line(second_config, second_line, 2);
Result first_result = matcher.NewEntry(&first_line);
Result second_result = matcher.NewEntry(&second_line);
CHECK_FALSE(first_result.failed());
CHECK(second_result.failed());
CHECK(matcher.num_el == 1);
}
TEST_CASE("RegexMatcher rejects a bad pattern at a reused slot", "[ControlMatcher]")
{
RegexMatcher<CacheControlRecord, CacheControlResult> matcher{"CacheControl", "cache.config"};
// The first line compiles its regex and then fails to initialize its record, which
// leaves num_el where it was, so the next line lands on the same slot with that
// pattern still in it.
char first_config[] = "url_regex=^http://example.com/good action=invalid";
char second_config[] = "url_regex=^http://example.com/(unterminated action=never-cache";
matcher_line first_line;
matcher_line second_line;
matcher.AllocateSpace(2);
parse_line(first_config, first_line, 1);
parse_line(second_config, second_line, 2);
CHECK(matcher.NewEntry(&first_line).failed());
// The second line's own pattern does not compile, so it must be rejected rather than
// inherit the pattern left in the slot.
CHECK(matcher.NewEntry(&second_line).failed());
CHECK(matcher.num_el == 0);
// And the inherited pattern must not match anything.
TestRequestData request{"http://example.com/good"};
CacheControlResult result;
matcher.Match(&request, &result);
CHECK_FALSE(result.never_cache);
}