mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
11 KiB
269 lines
11 KiB
4 years ago
|
// Copyright 2021 gRPC authors.
|
||
|
//
|
||
|
// Licensed 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.
|
||
|
|
||
4 years ago
|
#include "src/core/lib/matchers/matchers.h"
|
||
4 years ago
|
|
||
2 years ago
|
#include "absl/status/status.h"
|
||
|
#include "gtest/gtest.h"
|
||
4 years ago
|
|
||
|
namespace grpc_core {
|
||
|
|
||
|
TEST(StringMatcherTest, ExactMatchCaseSensitive) {
|
||
|
auto string_matcher =
|
||
4 years ago
|
StringMatcher::Create(StringMatcher::Type::kExact,
|
||
4 years ago
|
/*matcher=*/"exact", /*case_sensitive=*/true);
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("exact"));
|
||
|
EXPECT_FALSE(string_matcher->Match("Exact"));
|
||
|
EXPECT_FALSE(string_matcher->Match("exacz"));
|
||
|
}
|
||
|
|
||
|
TEST(StringMatcherTest, ExactMatchCaseInsensitive) {
|
||
|
auto string_matcher =
|
||
4 years ago
|
StringMatcher::Create(StringMatcher::Type::kExact,
|
||
4 years ago
|
/*matcher=*/"exact", /*case_sensitive=*/false);
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("Exact"));
|
||
|
EXPECT_FALSE(string_matcher->Match("Exacz"));
|
||
|
}
|
||
|
|
||
|
TEST(StringMatcherTest, PrefixMatchCaseSensitive) {
|
||
4 years ago
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kPrefix,
|
||
4 years ago
|
/*matcher=*/"prefix",
|
||
|
/*case_sensitive=*/true);
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("prefix-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("xx-prefix-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("Prefix-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("pre-test"));
|
||
|
}
|
||
|
|
||
|
TEST(StringMatcherTest, PrefixMatchCaseInsensitive) {
|
||
4 years ago
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kPrefix,
|
||
4 years ago
|
/*matcher=*/"prefix",
|
||
|
/*case_sensitive=*/false);
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("PREfix-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("xx-PREfix-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("PRE-test"));
|
||
|
}
|
||
|
|
||
|
TEST(StringMatcherTest, SuffixMatchCaseSensitive) {
|
||
4 years ago
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSuffix,
|
||
4 years ago
|
/*matcher=*/"suffix",
|
||
|
/*case_sensitive=*/true);
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("test-suffix"));
|
||
|
EXPECT_FALSE(string_matcher->Match("test-Suffix"));
|
||
|
EXPECT_FALSE(string_matcher->Match("test-suffix-xx"));
|
||
|
EXPECT_FALSE(string_matcher->Match("test-suffiz"));
|
||
|
}
|
||
|
|
||
|
TEST(StringMatcherTest, SuffixMatchCaseInSensitive) {
|
||
4 years ago
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSuffix,
|
||
4 years ago
|
/*matcher=*/"suffix",
|
||
|
/*case_sensitive=*/false);
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("Test-SUFFIX"));
|
||
|
EXPECT_FALSE(string_matcher->Match("Test-SUFFIX-xx"));
|
||
|
EXPECT_FALSE(string_matcher->Match("Test-SUFFIZ"));
|
||
|
}
|
||
|
|
||
|
TEST(StringMatcherTest, InvalidRegex) {
|
||
4 years ago
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSafeRegex,
|
||
4 years ago
|
/*matcher=*/"a[b-a]",
|
||
|
/*case_sensitive=*/true);
|
||
|
EXPECT_FALSE(string_matcher.ok());
|
||
|
EXPECT_EQ(string_matcher.status().code(), absl::StatusCode::kInvalidArgument);
|
||
|
EXPECT_EQ(string_matcher.status().message(),
|
||
2 years ago
|
"Invalid regex string specified in matcher: "
|
||
|
"invalid character class range: b-a")
|
||
|
<< string_matcher.status();
|
||
4 years ago
|
}
|
||
|
|
||
|
TEST(StringMatcherTest, SafeRegexMatchCaseSensitive) {
|
||
4 years ago
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSafeRegex,
|
||
4 years ago
|
/*matcher=*/"regex.*");
|
||
4 years ago
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("regex-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("xx-regex-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("Regex-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("test-regex"));
|
||
|
}
|
||
|
|
||
3 years ago
|
TEST(StringMatcherTest, PresenceMatchUsingSafeRegex) {
|
||
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSafeRegex,
|
||
|
/*matcher=*/".+");
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("any-value"));
|
||
|
EXPECT_FALSE(string_matcher->Match(""));
|
||
|
}
|
||
|
|
||
4 years ago
|
TEST(StringMatcherTest, ContainsMatchCaseSensitive) {
|
||
4 years ago
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kContains,
|
||
4 years ago
|
/*matcher=*/"contains",
|
||
|
/*case_sensitive=*/true);
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("test-contains"));
|
||
|
EXPECT_TRUE(string_matcher->Match("test-contains-test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("test-Contains"));
|
||
|
EXPECT_FALSE(string_matcher->Match("test-containz"));
|
||
|
}
|
||
|
|
||
|
TEST(StringMatcherTest, ContainsMatchCaseInSensitive) {
|
||
4 years ago
|
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kContains,
|
||
4 years ago
|
/*matcher=*/"contains",
|
||
|
/*case_sensitive=*/false);
|
||
|
ASSERT_TRUE(string_matcher.ok());
|
||
|
EXPECT_TRUE(string_matcher->Match("Test-Contains"));
|
||
|
EXPECT_TRUE(string_matcher->Match("Test-Contains-Test"));
|
||
|
EXPECT_FALSE(string_matcher->Match("Test-Containz"));
|
||
|
}
|
||
|
|
||
|
TEST(HeaderMatcherTest, StringMatcher) {
|
||
|
auto header_matcher =
|
||
4 years ago
|
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kExact,
|
||
4 years ago
|
/*matcher=*/"exact");
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_TRUE(header_matcher->Match("exact"));
|
||
|
EXPECT_FALSE(header_matcher->Match("Exact"));
|
||
|
EXPECT_FALSE(header_matcher->Match("exacz"));
|
||
3 years ago
|
EXPECT_FALSE(header_matcher->Match(absl::nullopt));
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
TEST(HeaderMatcherTest, StringMatcherCaseInsensitive) {
|
||
|
auto header_matcher =
|
||
|
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kExact,
|
||
|
/*matcher=*/"exact", /*range_start=*/0,
|
||
|
/*range_end=*/0, /*present_match=*/false,
|
||
|
/*invert_match=*/false, /*case_sensitive=*/false);
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_TRUE(header_matcher->Match("exact"));
|
||
|
EXPECT_TRUE(header_matcher->Match("Exact"));
|
||
|
EXPECT_FALSE(header_matcher->Match("exacz"));
|
||
|
EXPECT_FALSE(header_matcher->Match(absl::nullopt));
|
||
|
}
|
||
|
|
||
4 years ago
|
TEST(HeaderMatcherTest, StringMatcherWithInvertMatch) {
|
||
|
auto header_matcher =
|
||
4 years ago
|
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kExact,
|
||
4 years ago
|
/*matcher=*/"exact",
|
||
|
/*range_start=*/0, /*range_end=*/0,
|
||
|
/*present_match=*/false, /*invert_match=*/true);
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_FALSE(header_matcher->Match("exact"));
|
||
|
EXPECT_TRUE(header_matcher->Match("Exact"));
|
||
|
EXPECT_TRUE(header_matcher->Match("exacz"));
|
||
3 years ago
|
EXPECT_FALSE(header_matcher->Match(absl::nullopt));
|
||
4 years ago
|
}
|
||
|
|
||
|
TEST(HeaderMatcherTest, InvalidRegex) {
|
||
|
auto header_matcher =
|
||
4 years ago
|
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kSafeRegex,
|
||
4 years ago
|
/*matcher=*/"a[b-a]",
|
||
|
/*range_start=*/0, /*range_end=*/0,
|
||
|
/*present_match=*/false, /*invert_match=*/true);
|
||
|
EXPECT_FALSE(header_matcher.ok());
|
||
|
EXPECT_EQ(header_matcher.status().code(), absl::StatusCode::kInvalidArgument);
|
||
|
EXPECT_EQ(header_matcher.status().message(),
|
||
2 years ago
|
"Invalid regex string specified in matcher: "
|
||
|
"invalid character class range: b-a")
|
||
|
<< header_matcher.status();
|
||
4 years ago
|
}
|
||
|
|
||
|
TEST(HeaderMatcherTest, RangeMatcherValidRange) {
|
||
|
auto header_matcher =
|
||
4 years ago
|
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kRange,
|
||
4 years ago
|
/*matcher=*/"", /*range_start=*/10,
|
||
|
/*range_end*/ 20);
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_TRUE(header_matcher->Match("16"));
|
||
|
EXPECT_TRUE(header_matcher->Match("10"));
|
||
|
EXPECT_FALSE(header_matcher->Match("3"));
|
||
|
EXPECT_FALSE(header_matcher->Match("20"));
|
||
3 years ago
|
EXPECT_FALSE(header_matcher->Match(absl::nullopt));
|
||
|
}
|
||
|
|
||
|
TEST(HeaderMatcherTest, RangeMatcherValidRangeWithInvertMatch) {
|
||
|
auto header_matcher = HeaderMatcher::Create(
|
||
|
/*name=*/"key", HeaderMatcher::Type::kRange,
|
||
|
/*matcher=*/"", /*range_start=*/10,
|
||
|
/*range_end=*/20, /*present_match=*/false, /*invert_match=*/true);
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_FALSE(header_matcher->Match("16"));
|
||
|
EXPECT_FALSE(header_matcher->Match("10"));
|
||
|
EXPECT_TRUE(header_matcher->Match("3"));
|
||
|
EXPECT_TRUE(header_matcher->Match("20"));
|
||
|
EXPECT_FALSE(header_matcher->Match(absl::nullopt));
|
||
4 years ago
|
}
|
||
|
|
||
|
TEST(HeaderMatcherTest, RangeMatcherInvalidRange) {
|
||
|
auto header_matcher =
|
||
4 years ago
|
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kRange,
|
||
4 years ago
|
/*matcher=*/"", /*range_start=*/20,
|
||
3 years ago
|
/*range_end=*/10);
|
||
4 years ago
|
EXPECT_FALSE(header_matcher.ok());
|
||
|
EXPECT_EQ(header_matcher.status().code(), absl::StatusCode::kInvalidArgument);
|
||
|
EXPECT_EQ(
|
||
|
header_matcher.status().message(),
|
||
|
"Invalid range specifier specified: end cannot be smaller than start.");
|
||
|
}
|
||
|
|
||
|
TEST(HeaderMatcherTest, PresentMatcherTrue) {
|
||
|
auto header_matcher =
|
||
4 years ago
|
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kPresent,
|
||
4 years ago
|
/*matcher=*/"", /*range_start=*/0,
|
||
|
/*range_end=*/0, /*present_match=*/true);
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_TRUE(header_matcher->Match("any_value"));
|
||
|
EXPECT_FALSE(header_matcher->Match(absl::nullopt));
|
||
|
}
|
||
|
|
||
3 years ago
|
TEST(HeaderMatcherTest, PresentMatcherTrueWithInvertMatch) {
|
||
|
auto header_matcher = HeaderMatcher::Create(
|
||
|
/*name=*/"key", HeaderMatcher::Type::kPresent,
|
||
|
/*matcher=*/"", /*range_start=*/0,
|
||
|
/*range_end=*/0, /*present_match=*/true, /*invert_match=*/true);
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_FALSE(header_matcher->Match("any_value"));
|
||
|
EXPECT_TRUE(header_matcher->Match(absl::nullopt));
|
||
|
}
|
||
|
|
||
4 years ago
|
TEST(HeaderMatcherTest, PresentMatcherFalse) {
|
||
|
auto header_matcher =
|
||
4 years ago
|
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kPresent,
|
||
4 years ago
|
/*matcher=*/"", /*range_start=*/0,
|
||
|
/*range_end=*/0, /*present_match=*/false);
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_FALSE(header_matcher->Match("any_value"));
|
||
|
EXPECT_TRUE(header_matcher->Match(absl::nullopt));
|
||
|
}
|
||
|
|
||
3 years ago
|
TEST(HeaderMatcherTest, PresentMatcherFalseWithInvertMatch) {
|
||
|
auto header_matcher = HeaderMatcher::Create(
|
||
|
/*name=*/"key", HeaderMatcher::Type::kPresent,
|
||
|
/*matcher=*/"", /*range_start=*/0,
|
||
|
/*range_end=*/0, /*present_match=*/false, /*invert_match=*/true);
|
||
|
ASSERT_TRUE(header_matcher.ok());
|
||
|
EXPECT_TRUE(header_matcher->Match("any_value"));
|
||
|
EXPECT_FALSE(header_matcher->Match(absl::nullopt));
|
||
|
}
|
||
|
|
||
4 years ago
|
} // namespace grpc_core
|
||
|
|
||
|
int main(int argc, char** argv) {
|
||
|
::testing::InitGoogleTest(&argc, argv);
|
||
|
return RUN_ALL_TESTS();
|
||
|
}
|