| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "src/core/lib/matchers/matchers.h" |
| 16 |
|
| 17 |
#include <gtest/gtest.h> |
| 18 |
|
| 19 |
namespace grpc_core { |
| 20 |
|
| 21 |
TEST(StringMatcherTest, ExactMatchCaseSensitive) { |
| 22 |
auto string_matcher = |
| 23 |
StringMatcher::Create(StringMatcher::Type::kExact, |
| 24 |
/*matcher=*/"exact", /*case_sensitive=*/true); |
| 25 |
ASSERT_TRUE(string_matcher.ok()); |
| 26 |
EXPECT_TRUE(string_matcher->Match("exact")); |
| 27 |
EXPECT_FALSE(string_matcher->Match("Exact")); |
| 28 |
EXPECT_FALSE(string_matcher->Match("exacz")); |
| 29 |
} |
| 30 |
|
| 31 |
TEST(StringMatcherTest, ExactMatchCaseInsensitive) { |
| 32 |
auto string_matcher = |
| 33 |
StringMatcher::Create(StringMatcher::Type::kExact, |
| 34 |
/*matcher=*/"exact", /*case_sensitive=*/false); |
| 35 |
ASSERT_TRUE(string_matcher.ok()); |
| 36 |
EXPECT_TRUE(string_matcher->Match("Exact")); |
| 37 |
EXPECT_FALSE(string_matcher->Match("Exacz")); |
| 38 |
} |
| 39 |
|
| 40 |
TEST(StringMatcherTest, PrefixMatchCaseSensitive) { |
| 41 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kPrefix, |
| 42 |
/*matcher=*/"prefix", |
| 43 |
/*case_sensitive=*/true); |
| 44 |
ASSERT_TRUE(string_matcher.ok()); |
| 45 |
EXPECT_TRUE(string_matcher->Match("prefix-test")); |
| 46 |
EXPECT_FALSE(string_matcher->Match("xx-prefix-test")); |
| 47 |
EXPECT_FALSE(string_matcher->Match("Prefix-test")); |
| 48 |
EXPECT_FALSE(string_matcher->Match("pre-test")); |
| 49 |
} |
| 50 |
|
| 51 |
TEST(StringMatcherTest, PrefixMatchCaseInsensitive) { |
| 52 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kPrefix, |
| 53 |
/*matcher=*/"prefix", |
| 54 |
/*case_sensitive=*/false); |
| 55 |
ASSERT_TRUE(string_matcher.ok()); |
| 56 |
EXPECT_TRUE(string_matcher->Match("PREfix-test")); |
| 57 |
EXPECT_FALSE(string_matcher->Match("xx-PREfix-test")); |
| 58 |
EXPECT_FALSE(string_matcher->Match("PRE-test")); |
| 59 |
} |
| 60 |
|
| 61 |
TEST(StringMatcherTest, SuffixMatchCaseSensitive) { |
| 62 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSuffix, |
| 63 |
/*matcher=*/"suffix", |
| 64 |
/*case_sensitive=*/true); |
| 65 |
ASSERT_TRUE(string_matcher.ok()); |
| 66 |
EXPECT_TRUE(string_matcher->Match("test-suffix")); |
| 67 |
EXPECT_FALSE(string_matcher->Match("test-Suffix")); |
| 68 |
EXPECT_FALSE(string_matcher->Match("test-suffix-xx")); |
| 69 |
EXPECT_FALSE(string_matcher->Match("test-suffiz")); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(StringMatcherTest, SuffixMatchCaseInSensitive) { |
| 73 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSuffix, |
| 74 |
/*matcher=*/"suffix", |
| 75 |
/*case_sensitive=*/false); |
| 76 |
ASSERT_TRUE(string_matcher.ok()); |
| 77 |
EXPECT_TRUE(string_matcher->Match("Test-SUFFIX")); |
| 78 |
EXPECT_FALSE(string_matcher->Match("Test-SUFFIX-xx")); |
| 79 |
EXPECT_FALSE(string_matcher->Match("Test-SUFFIZ")); |
| 80 |
} |
| 81 |
|
| 82 |
TEST(StringMatcherTest, InvalidRegex) { |
| 83 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSafeRegex, |
| 84 |
/*matcher=*/"a[b-a]", |
| 85 |
/*case_sensitive=*/true); |
| 86 |
EXPECT_FALSE(string_matcher.ok()); |
| 87 |
EXPECT_EQ(string_matcher.status().code(), absl::StatusCode::kInvalidArgument); |
| 88 |
EXPECT_EQ(string_matcher.status().message(), |
| 89 |
"Invalid regex string specified in matcher."); |
| 90 |
} |
| 91 |
|
| 92 |
TEST(StringMatcherTest, SafeRegexMatchCaseSensitive) { |
| 93 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSafeRegex, |
| 94 |
/*matcher=*/"regex.*"); |
| 95 |
ASSERT_TRUE(string_matcher.ok()); |
| 96 |
EXPECT_TRUE(string_matcher->Match("regex-test")); |
| 97 |
EXPECT_FALSE(string_matcher->Match("xx-regex-test")); |
| 98 |
EXPECT_FALSE(string_matcher->Match("Regex-test")); |
| 99 |
EXPECT_FALSE(string_matcher->Match("test-regex")); |
| 100 |
} |
| 101 |
|
| 102 |
TEST(StringMatcherTest, PresenceMatchUsingSafeRegex) { |
| 103 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kSafeRegex, |
| 104 |
/*matcher=*/".+"); |
| 105 |
ASSERT_TRUE(string_matcher.ok()); |
| 106 |
EXPECT_TRUE(string_matcher->Match("any-value")); |
| 107 |
EXPECT_FALSE(string_matcher->Match("")); |
| 108 |
} |
| 109 |
|
| 110 |
TEST(StringMatcherTest, ContainsMatchCaseSensitive) { |
| 111 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kContains, |
| 112 |
/*matcher=*/"contains", |
| 113 |
/*case_sensitive=*/true); |
| 114 |
ASSERT_TRUE(string_matcher.ok()); |
| 115 |
EXPECT_TRUE(string_matcher->Match("test-contains")); |
| 116 |
EXPECT_TRUE(string_matcher->Match("test-contains-test")); |
| 117 |
EXPECT_FALSE(string_matcher->Match("test-Contains")); |
| 118 |
EXPECT_FALSE(string_matcher->Match("test-containz")); |
| 119 |
} |
| 120 |
|
| 121 |
TEST(StringMatcherTest, ContainsMatchCaseInSensitive) { |
| 122 |
auto string_matcher = StringMatcher::Create(StringMatcher::Type::kContains, |
| 123 |
/*matcher=*/"contains", |
| 124 |
/*case_sensitive=*/false); |
| 125 |
ASSERT_TRUE(string_matcher.ok()); |
| 126 |
EXPECT_TRUE(string_matcher->Match("Test-Contains")); |
| 127 |
EXPECT_TRUE(string_matcher->Match("Test-Contains-Test")); |
| 128 |
EXPECT_FALSE(string_matcher->Match("Test-Containz")); |
| 129 |
} |
| 130 |
|
| 131 |
TEST(HeaderMatcherTest, StringMatcher) { |
| 132 |
auto header_matcher = |
| 133 |
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kExact, |
| 134 |
/*matcher=*/"exact"); |
| 135 |
ASSERT_TRUE(header_matcher.ok()); |
| 136 |
EXPECT_TRUE(header_matcher->Match("exact")); |
| 137 |
EXPECT_FALSE(header_matcher->Match("Exact")); |
| 138 |
EXPECT_FALSE(header_matcher->Match("exacz")); |
| 139 |
EXPECT_FALSE(header_matcher->Match(absl::nullopt)); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(HeaderMatcherTest, StringMatcherWithInvertMatch) { |
| 143 |
auto header_matcher = |
| 144 |
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kExact, |
| 145 |
/*matcher=*/"exact", |
| 146 |
/*range_start=*/0, /*range_end=*/0, |
| 147 |
/*present_match=*/false, /*invert_match=*/true); |
| 148 |
ASSERT_TRUE(header_matcher.ok()); |
| 149 |
EXPECT_FALSE(header_matcher->Match("exact")); |
| 150 |
EXPECT_TRUE(header_matcher->Match("Exact")); |
| 151 |
EXPECT_TRUE(header_matcher->Match("exacz")); |
| 152 |
EXPECT_FALSE(header_matcher->Match(absl::nullopt)); |
| 153 |
} |
| 154 |
|
| 155 |
TEST(HeaderMatcherTest, InvalidRegex) { |
| 156 |
auto header_matcher = |
| 157 |
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kSafeRegex, |
| 158 |
/*matcher=*/"a[b-a]", |
| 159 |
/*range_start=*/0, /*range_end=*/0, |
| 160 |
/*present_match=*/false, /*invert_match=*/true); |
| 161 |
EXPECT_FALSE(header_matcher.ok()); |
| 162 |
EXPECT_EQ(header_matcher.status().code(), absl::StatusCode::kInvalidArgument); |
| 163 |
EXPECT_EQ(header_matcher.status().message(), |
| 164 |
"Invalid regex string specified in matcher."); |
| 165 |
} |
| 166 |
|
| 167 |
TEST(HeaderMatcherTest, RangeMatcherValidRange) { |
| 168 |
auto header_matcher = |
| 169 |
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kRange, |
| 170 |
/*matcher=*/"", /*range_start=*/10, |
| 171 |
/*range_end*/ 20); |
| 172 |
ASSERT_TRUE(header_matcher.ok()); |
| 173 |
EXPECT_TRUE(header_matcher->Match("16")); |
| 174 |
EXPECT_TRUE(header_matcher->Match("10")); |
| 175 |
EXPECT_FALSE(header_matcher->Match("3")); |
| 176 |
EXPECT_FALSE(header_matcher->Match("20")); |
| 177 |
EXPECT_FALSE(header_matcher->Match(absl::nullopt)); |
| 178 |
} |
| 179 |
|
| 180 |
TEST(HeaderMatcherTest, RangeMatcherValidRangeWithInvertMatch) { |
| 181 |
auto header_matcher = HeaderMatcher::Create( |
| 182 |
/*name=*/"key", HeaderMatcher::Type::kRange, |
| 183 |
/*matcher=*/"", /*range_start=*/10, |
| 184 |
/*range_end=*/20, /*present_match=*/false, /*invert_match=*/true); |
| 185 |
ASSERT_TRUE(header_matcher.ok()); |
| 186 |
EXPECT_FALSE(header_matcher->Match("16")); |
| 187 |
EXPECT_FALSE(header_matcher->Match("10")); |
| 188 |
EXPECT_TRUE(header_matcher->Match("3")); |
| 189 |
EXPECT_TRUE(header_matcher->Match("20")); |
| 190 |
EXPECT_FALSE(header_matcher->Match(absl::nullopt)); |
| 191 |
} |
| 192 |
|
| 193 |
TEST(HeaderMatcherTest, RangeMatcherInvalidRange) { |
| 194 |
auto header_matcher = |
| 195 |
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kRange, |
| 196 |
/*matcher=*/"", /*range_start=*/20, |
| 197 |
/*range_end=*/10); |
| 198 |
EXPECT_FALSE(header_matcher.ok()); |
| 199 |
EXPECT_EQ(header_matcher.status().code(), absl::StatusCode::kInvalidArgument); |
| 200 |
EXPECT_EQ( |
| 201 |
header_matcher.status().message(), |
| 202 |
"Invalid range specifier specified: end cannot be smaller than start."); |
| 203 |
} |
| 204 |
|
| 205 |
TEST(HeaderMatcherTest, PresentMatcherTrue) { |
| 206 |
auto header_matcher = |
| 207 |
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kPresent, |
| 208 |
/*matcher=*/"", /*range_start=*/0, |
| 209 |
/*range_end=*/0, /*present_match=*/true); |
| 210 |
ASSERT_TRUE(header_matcher.ok()); |
| 211 |
EXPECT_TRUE(header_matcher->Match("any_value")); |
| 212 |
EXPECT_FALSE(header_matcher->Match(absl::nullopt)); |
| 213 |
} |
| 214 |
|
| 215 |
TEST(HeaderMatcherTest, PresentMatcherTrueWithInvertMatch) { |
| 216 |
auto header_matcher = HeaderMatcher::Create( |
| 217 |
/*name=*/"key", HeaderMatcher::Type::kPresent, |
| 218 |
/*matcher=*/"", /*range_start=*/0, |
| 219 |
/*range_end=*/0, /*present_match=*/true, /*invert_match=*/true); |
| 220 |
ASSERT_TRUE(header_matcher.ok()); |
| 221 |
EXPECT_FALSE(header_matcher->Match("any_value")); |
| 222 |
EXPECT_TRUE(header_matcher->Match(absl::nullopt)); |
| 223 |
} |
| 224 |
|
| 225 |
TEST(HeaderMatcherTest, PresentMatcherFalse) { |
| 226 |
auto header_matcher = |
| 227 |
HeaderMatcher::Create(/*name=*/"key", HeaderMatcher::Type::kPresent, |
| 228 |
/*matcher=*/"", /*range_start=*/0, |
| 229 |
/*range_end=*/0, /*present_match=*/false); |
| 230 |
ASSERT_TRUE(header_matcher.ok()); |
| 231 |
EXPECT_FALSE(header_matcher->Match("any_value")); |
| 232 |
EXPECT_TRUE(header_matcher->Match(absl::nullopt)); |
| 233 |
} |
| 234 |
|
| 235 |
TEST(HeaderMatcherTest, PresentMatcherFalseWithInvertMatch) { |
| 236 |
auto header_matcher = HeaderMatcher::Create( |
| 237 |
/*name=*/"key", HeaderMatcher::Type::kPresent, |
| 238 |
/*matcher=*/"", /*range_start=*/0, |
| 239 |
/*range_end=*/0, /*present_match=*/false, /*invert_match=*/true); |
| 240 |
ASSERT_TRUE(header_matcher.ok()); |
| 241 |
EXPECT_TRUE(header_matcher->Match("any_value")); |
| 242 |
EXPECT_FALSE(header_matcher->Match(absl::nullopt)); |
| 243 |
} |
| 244 |
|
| 245 |
} // namespace grpc_core |
| 246 |
|
| 247 |
int main(int argc, char** argv) { |
| 248 |
::testing::InitGoogleTest(&argc, argv); |
| 249 |
return RUN_ALL_TESTS(); |
| 250 |
} |