• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/strings/pattern.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 
9 namespace base {
10 
TEST(StringUtilTest,MatchPatternTest)11 TEST(StringUtilTest, MatchPatternTest) {
12   EXPECT_TRUE(MatchPattern("www.google.com", "*.com"));
13   EXPECT_TRUE(MatchPattern("www.google.com", "*"));
14   EXPECT_FALSE(MatchPattern("www.google.com", "www*.g*.org"));
15   EXPECT_TRUE(MatchPattern("Hello", "H?l?o"));
16   EXPECT_FALSE(MatchPattern("www.google.com", "http://*)"));
17   EXPECT_FALSE(MatchPattern("www.msn.com", "*.COM"));
18   EXPECT_TRUE(MatchPattern("Hello*1234", "He??o\\*1*"));
19   EXPECT_FALSE(MatchPattern("", "*.*"));
20   EXPECT_TRUE(MatchPattern("", "*"));
21   EXPECT_TRUE(MatchPattern("", "?"));
22   EXPECT_TRUE(MatchPattern("", ""));
23   EXPECT_FALSE(MatchPattern("Hello", ""));
24   EXPECT_TRUE(MatchPattern("Hello*", "Hello*"));
25   EXPECT_TRUE(MatchPattern("abcd", "*???"));
26   EXPECT_FALSE(MatchPattern("abcd", "???"));
27   EXPECT_TRUE(MatchPattern("abcb", "a*b"));
28   EXPECT_FALSE(MatchPattern("abcb", "a?b"));
29 
30   // Test UTF8 matching.
31   EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0", "*\xe2\x99\xa0"));
32   EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0.", "heart: ?."));
33   EXPECT_TRUE(MatchPattern("hearts: \xe2\x99\xa0\xe2\x99\xa0", "*"));
34   // Invalid sequences should be handled as a single invalid character.
35   EXPECT_TRUE(MatchPattern("invalid: \xef\xbf\xbe", "invalid: ?"));
36   // If the pattern has invalid characters, it shouldn't match anything.
37   EXPECT_FALSE(MatchPattern("\xf4\x90\x80\x80", "\xf4\x90\x80\x80"));
38 
39   // Test UTF16 character matching.
40   EXPECT_TRUE(MatchPattern(UTF8ToUTF16("www.google.com"),
41                            UTF8ToUTF16("*.com")));
42   EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello*1234"),
43                            UTF8ToUTF16("He??o\\*1*")));
44 
45   // Some test cases that might cause naive implementations to exhibit
46   // exponential run time or fail.
47   EXPECT_TRUE(MatchPattern("Hello", "He********************************o"));
48   EXPECT_TRUE(MatchPattern("123456789012345678", "?????????????????*"));
49   EXPECT_TRUE(MatchPattern("aaaaaaaaaaab", "a*a*a*a*a*a*a*a*a*a*a*b"));
50 }
51 
52 }  // namespace base
53