1 // Copyright 2024 The 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/util/glob.h"
16
17 #include <grpc/grpc.h>
18
19 #include "absl/strings/match.h"
20 #include "gtest/gtest.h"
21 #include "test/core/test_util/test_config.h"
22
23 namespace grpc_core {
24 namespace testing {
25
TEST(GlobTest,DefaultsToStringMatching)26 TEST(GlobTest, DefaultsToStringMatching) {
27 EXPECT_TRUE(GlobMatch("arst", "arst"));
28 }
29
TEST(GlobTest,AsteriskMatchesMultipleCharacters)30 TEST(GlobTest, AsteriskMatchesMultipleCharacters) {
31 EXPECT_TRUE(GlobMatch("a", "*"));
32 EXPECT_TRUE(GlobMatch("arst", "*"));
33 }
34
TEST(GlobTest,QuestionMarkMatchesSingleCharacter)35 TEST(GlobTest, QuestionMarkMatchesSingleCharacter) {
36 EXPECT_TRUE(GlobMatch("a", "?"));
37 EXPECT_FALSE(GlobMatch("arst", "?"));
38 }
39
TEST(GlobTest,AsteriskMatchesEmpty)40 TEST(GlobTest, AsteriskMatchesEmpty) { EXPECT_TRUE(GlobMatch("", "*")); }
41
TEST(GlobTest,QuestionMarkDoesNotMatcheEmpty)42 TEST(GlobTest, QuestionMarkDoesNotMatcheEmpty) {
43 EXPECT_FALSE(GlobMatch("", "?"));
44 }
45
TEST(GlobTest,EmbeddedAsterisk)46 TEST(GlobTest, EmbeddedAsterisk) {
47 EXPECT_TRUE(GlobMatch("arst", "a*t"));
48 EXPECT_TRUE(GlobMatch("arst", "*rst"));
49 EXPECT_TRUE(GlobMatch("arst", "ar*"));
50 EXPECT_TRUE(GlobMatch("arst", "*r*"));
51 EXPECT_FALSE(GlobMatch("arst", "*q*"));
52 EXPECT_FALSE(GlobMatch("*arst", "**q*"));
53 }
54
TEST(GlobTest,EmbeddedQuestionMark)55 TEST(GlobTest, EmbeddedQuestionMark) {
56 EXPECT_TRUE(GlobMatch("arst", "?rst"));
57 EXPECT_TRUE(GlobMatch("arst", "a?st"));
58 EXPECT_TRUE(GlobMatch("arst", "ar?t"));
59 EXPECT_TRUE(GlobMatch("arst", "ars?"));
60 EXPECT_TRUE(GlobMatch("arst", "??s?"));
61 EXPECT_TRUE(GlobMatch("?arst", "???s?"));
62 EXPECT_FALSE(GlobMatch("?arst", "arst"));
63 }
64
TEST(GlobTest,BothWildcardsWorkTogether)65 TEST(GlobTest, BothWildcardsWorkTogether) {
66 EXPECT_TRUE(GlobMatch("arst", "?r*"));
67 EXPECT_TRUE(GlobMatch("arst", "*s?"));
68 EXPECT_TRUE(GlobMatch("arst", "a?*"));
69 EXPECT_TRUE(GlobMatch("arst", "*?t"));
70 }
71
72 } // namespace testing
73 } // namespace grpc_core
74
main(int argc,char ** argv)75 int main(int argc, char** argv) {
76 grpc::testing::TestEnvironment env(&argc, argv);
77 ::testing::InitGoogleTest(&argc, argv);
78 grpc_init();
79 auto res = RUN_ALL_TESTS();
80 grpc_shutdown();
81 return res;
82 }
83