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 // Stop after a certain recursion depth.
26 EXPECT_FALSE(MatchPattern("123456789012345678", "?????????????????*"));
27
28 // Test UTF8 matching.
29 EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0", "*\xe2\x99\xa0"));
30 EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0.", "heart: ?."));
31 EXPECT_TRUE(MatchPattern("hearts: \xe2\x99\xa0\xe2\x99\xa0", "*"));
32 // Invalid sequences should be handled as a single invalid character.
33 EXPECT_TRUE(MatchPattern("invalid: \xef\xbf\xbe", "invalid: ?"));
34 // If the pattern has invalid characters, it shouldn't match anything.
35 EXPECT_FALSE(MatchPattern("\xf4\x90\x80\x80", "\xf4\x90\x80\x80"));
36
37 // Test UTF16 character matching.
38 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("www.google.com"),
39 UTF8ToUTF16("*.com")));
40 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello*1234"),
41 UTF8ToUTF16("He??o\\*1*")));
42
43 // This test verifies that consecutive wild cards are collapsed into 1
44 // wildcard (when this doesn't occur, MatchPattern reaches it's maximum
45 // recursion depth).
46 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello"),
47 UTF8ToUTF16("He********************************o")));
48 }
49
50 } // namespace base
51