OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "components/subresource_filter/core/common/url_pattern_matching.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "components/subresource_filter/core/common/url_pattern.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "url/gurl.h" | |
12 | |
13 namespace subresource_filter { | |
14 | |
15 namespace { | |
16 | |
17 constexpr proto::AnchorType kAnchorNone = proto::ANCHOR_TYPE_NONE; | |
18 constexpr proto::AnchorType kBoundary = proto::ANCHOR_TYPE_BOUNDARY; | |
19 constexpr proto::AnchorType kSubdomain = proto::ANCHOR_TYPE_SUBDOMAIN; | |
20 | |
21 } // namespace | |
22 | |
23 TEST(UrlPatternMatchingTest, BuildFailureFunctionForUrlPattern) { | |
24 const struct { | |
25 UrlPattern url_pattern; | |
26 std::vector<size_t> expected_failure_function; | |
27 } kTestCases[] = { | |
28 {{"abcd", proto::URL_PATTERN_TYPE_SUBSTRING}, {0, 0, 0, 0}}, | |
29 {{"&a?a/"}, {0, 0, 0, 0, 0}}, | |
30 {{"^a?a/"}, {1, 0, 0, 1, 2, 3}}, | |
31 | |
32 {{"abc*abc", kBoundary, kAnchorNone}, {0, 0, 0}}, | |
33 {{"abc*aaa", kBoundary, kAnchorNone}, {0, 1, 2}}, | |
34 {{"aaa*abc", kBoundary, kAnchorNone}, {0, 0, 0}}, | |
35 | |
36 {{"abc*abc", kAnchorNone, kBoundary}, {0, 0, 0}}, | |
37 {{"abc*aaa", kAnchorNone, kBoundary}, {0, 0, 0}}, | |
38 {{"aaa*abc", kAnchorNone, kBoundary}, {0, 1, 2}}, | |
39 | |
40 {{"abc*cca", kSubdomain, kAnchorNone}, {0, 0, 0, 0, 1, 0}}, | |
41 {{"abc*cca", kBoundary, kAnchorNone}, {0, 1, 0}}, | |
42 {{"abc*cca"}, {0, 0, 0, 0, 1, 0}}, | |
43 | |
44 {{"abc*abacaba*cab"}, {0, 0, 0, 0, 0, 1, 0, 1, 2, 3, 0, 0, 0}}, | |
45 {{"aaa*a^d*^^b"}, {0, 1, 2, 1, 0, 0, 0, 1, 0, 1, 0}}, | |
46 {{"aaa*a^d*^^b", kAnchorNone, kBoundary}, {0, 1, 2, 1, 0, 0, 0}}, | |
47 {{"^^a*a^d*^^b", kBoundary, kAnchorNone}, {1, 0, 0, 0, 1, 0, 1, 0}}, | |
48 }; | |
49 | |
50 for (const auto& test_case : kTestCases) { | |
51 SCOPED_TRACE(testing::Message() | |
52 << "Pattern: " << test_case.url_pattern.url_pattern | |
53 << "; Anchors: " | |
54 << static_cast<int>(test_case.url_pattern.anchor_left) << ", " | |
55 << static_cast<int>(test_case.url_pattern.anchor_right)); | |
56 | |
57 std::vector<size_t> failure; | |
58 BuildFailureFunction(test_case.url_pattern, &failure); | |
59 EXPECT_EQ(test_case.expected_failure_function, failure); | |
60 } | |
61 } | |
62 | |
63 TEST(UrlPatternMatchingTest, IsUrlPatternMatch) { | |
64 const struct { | |
65 UrlPattern url_pattern; | |
66 const char* url; | |
67 bool expect_match; | |
68 } kTestCases[] = { | |
69 {{"xampl", proto::URL_PATTERN_TYPE_SUBSTRING}, | |
70 "http://example.com", | |
71 true}, | |
72 {{"example", proto::URL_PATTERN_TYPE_SUBSTRING}, | |
73 "http://example.com", | |
74 true}, | |
75 {{"/a?a"}, "http://ex.com/a?a", true}, | |
76 {{"^abc"}, "http://ex.com/abc?a", true}, | |
77 {{"^abc"}, "http://ex.com/a?abc", true}, | |
78 {{"^abc"}, "http://ex.com/abc?abc", true}, | |
79 | |
80 {{"http://ex", kBoundary, kAnchorNone}, "http://example.com", true}, | |
81 {{"mple.com/", kAnchorNone, kBoundary}, "http://example.com", true}, | |
82 | |
83 // Note: "example.com" will be normalized into "example.com/". | |
84 {{"http://*mpl", kBoundary, kAnchorNone}, "http://example.com", true}, | |
85 {{"mpl*com/", kAnchorNone, kBoundary}, "http://example.com", true}, | |
86 {{"example^com"}, "http://example.com", false}, | |
87 {{"example^com"}, "http://example/com", true}, | |
88 {{"example.com^"}, "http://example.com:8080", true}, | |
89 {{"http*.com/", kBoundary, kBoundary}, "http://example.com", true}, | |
90 {{"http*.org/", kBoundary, kBoundary}, "http://example.com", false}, | |
91 | |
92 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path/bbb?k=v&p1=0&p2=1", false}, | |
93 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&p1=0&p2=1", true}, | |
94 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&k=v&p1=0&p2=1", true}, | |
95 {{"/path?*&p1=*&p2="}, | |
96 "http://ex.com/aaa/path?k=v&p1=0&p3=10&p2=1", | |
97 true}, | |
98 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path&p1=0&p2=1", false}, | |
99 {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&p2=0&p1=1", false}, | |
100 | |
101 {{"abc*def*ghijk*xyz"}, | |
102 "http://example.com/abcdeffffghijkmmmxyzzz", | |
103 true}, | |
104 {{"abc*cdef"}, "http://example.com/abcdef", false}, | |
105 | |
106 {{"^^a^^"}, "http://ex.com/?a=/", true}, | |
107 {{"^^a^^"}, "http://ex.com/?a=/&b=0", true}, | |
108 {{"^^a^^"}, "http://ex.com/?a=", false}, | |
109 | |
110 {{"ex.com^path^*k=v^"}, "http://ex.com/path/?k1=v1&ak=v&kk=vv", true}, | |
111 {{"ex.com^path^*k=v^"}, "http://ex.com/p/path/?k1=v1&ak=v&kk=vv", false}, | |
112 {{"a^a&a^a&"}, "http://ex.com/a/a/a/a/?a&a&a&a&a", true}, | |
113 | |
114 {{"abc*def^"}, "http://ex.com/abc/a/ddef/", true}, | |
115 | |
116 {{"https://example.com/"}, "http://example.com/", false}, | |
117 {{"example.com/", kSubdomain, kAnchorNone}, "http://example.com/", true}, | |
118 {{"examp", kSubdomain, kAnchorNone}, "http://example.com/", true}, | |
119 {{"xamp", kSubdomain, kAnchorNone}, "http://example.com/", false}, | |
120 {{"examp", kSubdomain, kAnchorNone}, "http://test.example.com/", true}, | |
121 {{"t.examp", kSubdomain, kAnchorNone}, "http://test.example.com/", false}, | |
122 {{"com^", kSubdomain, kAnchorNone}, "http://test.example.com/", true}, | |
123 {{"x.com", kSubdomain, kAnchorNone}, "http://ex.com/?url=x.com", false}, | |
124 {{"ex.com/", kSubdomain, kBoundary}, "http://ex.com/", true}, | |
125 {{"ex.com^", kSubdomain, kBoundary}, "http://ex.com/", true}, | |
126 {{"ex.co", kSubdomain, kBoundary}, "http://ex.com/", false}, | |
127 {{"ex.com", kSubdomain, kBoundary}, "http://rex.com.ex.com/", false}, | |
128 {{"ex.com/", kSubdomain, kBoundary}, "http://rex.com.ex.com/", true}, | |
129 {{"http", kSubdomain, kBoundary}, "http://http.com/", false}, | |
130 {{"http", kSubdomain, kAnchorNone}, "http://http.com/", true}, | |
131 {{"/example.com", kSubdomain, kBoundary}, "http://example.com/", false}, | |
132 {{"/example.com/", kSubdomain, kBoundary}, "http://example.com/", false}, | |
133 }; | |
134 | |
135 for (const auto& test_case : kTestCases) { | |
136 SCOPED_TRACE(testing::Message() | |
137 << "Rule: " << test_case.url_pattern.url_pattern | |
138 << "; Anchors: " | |
139 << static_cast<int>(test_case.url_pattern.anchor_left) << ", " | |
140 << static_cast<int>(test_case.url_pattern.anchor_right) | |
141 << "; URL: " << GURL(test_case.url)); | |
142 | |
143 std::vector<size_t> failure; | |
144 BuildFailureFunction(test_case.url_pattern, &failure); | |
145 const bool is_match = | |
146 IsUrlPatternMatch(GURL(test_case.url), test_case.url_pattern, | |
147 failure.begin(), failure.end()); | |
148 EXPECT_EQ(test_case.expect_match, is_match); | |
149 } | |
150 } | |
151 | |
152 } // namespace subresource_filter | |
OLD | NEW |