OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/subresource_filter/core/common/url_pattern_matching.h" | 5 #include "components/subresource_filter/core/common/url_pattern.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "components/subresource_filter/core/common/url_pattern.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "url/gurl.h" | 8 #include "url/gurl.h" |
12 | 9 |
13 namespace subresource_filter { | 10 namespace subresource_filter { |
14 | 11 |
15 namespace { | 12 namespace { |
16 | 13 |
17 constexpr proto::AnchorType kAnchorNone = proto::ANCHOR_TYPE_NONE; | 14 constexpr proto::AnchorType kAnchorNone = proto::ANCHOR_TYPE_NONE; |
18 constexpr proto::AnchorType kBoundary = proto::ANCHOR_TYPE_BOUNDARY; | 15 constexpr proto::AnchorType kBoundary = proto::ANCHOR_TYPE_BOUNDARY; |
19 constexpr proto::AnchorType kSubdomain = proto::ANCHOR_TYPE_SUBDOMAIN; | 16 constexpr proto::AnchorType kSubdomain = proto::ANCHOR_TYPE_SUBDOMAIN; |
20 | 17 |
21 } // namespace | 18 } // namespace |
22 | 19 |
23 TEST(UrlPatternMatchingTest, BuildFailureFunctionForUrlPattern) { | 20 TEST(SubresourceFilterUrlPatternTest, MatchesUrl) { |
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 { | 21 const struct { |
65 UrlPattern url_pattern; | 22 UrlPattern url_pattern; |
66 const char* url; | 23 const char* url; |
67 bool expect_match; | 24 bool expect_match; |
68 } kTestCases[] = { | 25 } kTestCases[] = { |
26 {{"", proto::URL_PATTERN_TYPE_SUBSTRING}, "http://ex.com/", true}, | |
27 {{"", proto::URL_PATTERN_TYPE_WILDCARDED}, "http://ex.com/", true}, | |
28 {{"", kBoundary, kAnchorNone}, "http://ex.com/", true}, | |
engedy
2017/04/05 11:40:30
nit: Could you please also add a test with {"", kA
pkalinnikov
2017/04/05 13:29:22
The lines 26-27 cover this case already.
engedy
2017/04/05 13:41:33
Ah. Got it.
| |
29 {{"", kSubdomain, kAnchorNone}, "http://ex.com/", true}, | |
30 {{"", kSubdomain, kAnchorNone}, "http://ex.com/", true}, | |
31 {{"^", kSubdomain, kAnchorNone}, "http://ex.com/", false}, | |
32 {{".", kSubdomain, kAnchorNone}, "http://ex.com/", false}, | |
33 {{"", kAnchorNone, kBoundary}, "http://ex.com/", true}, | |
34 {{"^", kAnchorNone, kBoundary}, "http://ex.com/", true}, | |
35 {{".", kAnchorNone, kBoundary}, "http://ex.com/", false}, | |
36 {{"", kBoundary, kBoundary}, "http://ex.com/", false}, | |
37 {{"", kSubdomain, kBoundary}, "http://ex.com/", false}, | |
38 {{"com/", kSubdomain, kBoundary}, "http://ex.com/", true}, | |
39 | |
69 {{"xampl", proto::URL_PATTERN_TYPE_SUBSTRING}, | 40 {{"xampl", proto::URL_PATTERN_TYPE_SUBSTRING}, |
70 "http://example.com", | 41 "http://example.com", |
71 true}, | 42 true}, |
72 {{"example", proto::URL_PATTERN_TYPE_SUBSTRING}, | 43 {{"example", proto::URL_PATTERN_TYPE_SUBSTRING}, |
73 "http://example.com", | 44 "http://example.com", |
74 true}, | 45 true}, |
75 {{"/a?a"}, "http://ex.com/a?a", true}, | 46 {{"/a?a"}, "http://ex.com/a?a", true}, |
76 {{"^abc"}, "http://ex.com/abc?a", true}, | 47 {{"^abc"}, "http://ex.com/abc?a", true}, |
77 {{"^abc"}, "http://ex.com/a?abc", true}, | 48 {{"^abc"}, "http://ex.com/a?abc", true}, |
78 {{"^abc"}, "http://ex.com/abc?abc", true}, | 49 {{"^abc"}, "http://ex.com/abc?abc", true}, |
50 {{"^abc^abc"}, "http://ex.com/abc?abc", true}, | |
51 {{"^com^abc^abc"}, "http://ex.com/abc?abc", false}, | |
79 | 52 |
80 {{"http://ex", kBoundary, kAnchorNone}, "http://example.com", true}, | 53 {{"http://ex", kBoundary, kAnchorNone}, "http://example.com", true}, |
54 {{"http://ex", kAnchorNone, kAnchorNone}, "http://example.com", true}, | |
81 {{"mple.com/", kAnchorNone, kBoundary}, "http://example.com", true}, | 55 {{"mple.com/", kAnchorNone, kBoundary}, "http://example.com", true}, |
56 {{"mple.com/", kAnchorNone, kAnchorNone}, "http://example.com", true}, | |
57 {{"mple.com/", kSubdomain, kAnchorNone}, "http://example.com", false}, | |
58 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.com", false}, | |
59 {{"ex.com", kSubdomain, kAnchorNone}, "http://ex.com", true}, | |
60 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.ex.com", true}, | |
61 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.hex.com", false}, | |
82 | 62 |
83 // Note: "example.com" will be normalized into "example.com/". | 63 // Note: "example.com" will be normalized into "example.com/". |
84 {{"http://*mpl", kBoundary, kAnchorNone}, "http://example.com", true}, | 64 {{"http://*mpl", kBoundary, kAnchorNone}, "http://example.com", true}, |
85 {{"mpl*com/", kAnchorNone, kBoundary}, "http://example.com", true}, | 65 {{"mpl*com/", kAnchorNone, kBoundary}, "http://example.com", true}, |
86 {{"example^com"}, "http://example.com", false}, | 66 {{"example^com"}, "http://example.com", false}, |
87 {{"example^com"}, "http://example/com", true}, | 67 {{"example^com"}, "http://example/com", true}, |
88 {{"example.com^"}, "http://example.com:8080", true}, | 68 {{"example.com^"}, "http://example.com:8080", true}, |
89 {{"http*.com/", kBoundary, kBoundary}, "http://example.com", true}, | 69 {{"http*.com/", kBoundary, kBoundary}, "http://example.com", true}, |
90 {{"http*.org/", kBoundary, kBoundary}, "http://example.com", false}, | 70 {{"http*.org/", kBoundary, kBoundary}, "http://example.com", false}, |
91 | 71 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 {{"ex.co", kSubdomain, kBoundary}, "http://ex.com/", false}, | 106 {{"ex.co", kSubdomain, kBoundary}, "http://ex.com/", false}, |
127 {{"ex.com", kSubdomain, kBoundary}, "http://rex.com.ex.com/", false}, | 107 {{"ex.com", kSubdomain, kBoundary}, "http://rex.com.ex.com/", false}, |
128 {{"ex.com/", kSubdomain, kBoundary}, "http://rex.com.ex.com/", true}, | 108 {{"ex.com/", kSubdomain, kBoundary}, "http://rex.com.ex.com/", true}, |
129 {{"http", kSubdomain, kBoundary}, "http://http.com/", false}, | 109 {{"http", kSubdomain, kBoundary}, "http://http.com/", false}, |
130 {{"http", kSubdomain, kAnchorNone}, "http://http.com/", true}, | 110 {{"http", kSubdomain, kAnchorNone}, "http://http.com/", true}, |
131 {{"/example.com", kSubdomain, kBoundary}, "http://example.com/", false}, | 111 {{"/example.com", kSubdomain, kBoundary}, "http://example.com/", false}, |
132 {{"/example.com/", kSubdomain, kBoundary}, "http://example.com/", false}, | 112 {{"/example.com/", kSubdomain, kBoundary}, "http://example.com/", false}, |
133 }; | 113 }; |
134 | 114 |
135 for (const auto& test_case : kTestCases) { | 115 for (const auto& test_case : kTestCases) { |
136 SCOPED_TRACE(testing::Message() | 116 SCOPED_TRACE(testing::Message() << "Rule: " << test_case.url_pattern |
137 << "Rule: " << test_case.url_pattern.url_pattern | 117 << "; URL: " << GURL(test_case.url)); |
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 | 118 |
143 std::vector<size_t> failure; | 119 const bool is_match = test_case.url_pattern.MatchesUrl(GURL(test_case.url)); |
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); | 120 EXPECT_EQ(test_case.expect_match, is_match); |
149 } | 121 } |
150 } | 122 } |
151 | 123 |
152 } // namespace subresource_filter | 124 } // namespace subresource_filter |
OLD | NEW |