Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: components/subresource_filter/core/common/url_pattern_unittest.cc

Issue 2793993002: [subresource_filter] Replace KMP by std::search. (Closed)
Patch Set: Fix tests. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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> 7 #include <vector>
pkalinnikov 2017/04/04 10:06:48 nit: Not used.
pkalinnikov 2017/04/04 16:42:59 Done.
8 8
9 #include "components/subresource_filter/core/common/url_pattern.h"
10 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/gurl.h" 10 #include "url/gurl.h"
12 11
13 namespace subresource_filter { 12 namespace subresource_filter {
14 13
15 namespace { 14 namespace {
16 15
17 constexpr proto::AnchorType kAnchorNone = proto::ANCHOR_TYPE_NONE; 16 constexpr proto::AnchorType kAnchorNone = proto::ANCHOR_TYPE_NONE;
18 constexpr proto::AnchorType kBoundary = proto::ANCHOR_TYPE_BOUNDARY; 17 constexpr proto::AnchorType kBoundary = proto::ANCHOR_TYPE_BOUNDARY;
19 constexpr proto::AnchorType kSubdomain = proto::ANCHOR_TYPE_SUBDOMAIN; 18 constexpr proto::AnchorType kSubdomain = proto::ANCHOR_TYPE_SUBDOMAIN;
20 19
21 } // namespace 20 } // namespace
22 21
23 TEST(UrlPatternMatchingTest, BuildFailureFunctionForUrlPattern) { 22 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 { 23 const struct {
65 UrlPattern url_pattern; 24 UrlPattern url_pattern;
66 const char* url; 25 const char* url;
67 bool expect_match; 26 bool expect_match;
68 } kTestCases[] = { 27 } kTestCases[] = {
28 {{"", proto::URL_PATTERN_TYPE_SUBSTRING}, "http://ex.com/", true},
29 {{"", proto::URL_PATTERN_TYPE_WILDCARDED}, "http://ex.com/", true},
30 {{"", kBoundary, kAnchorNone}, "http://ex.com/", true},
31 {{"", kSubdomain, kAnchorNone}, "http://ex.com/", true},
32 {{"", kSubdomain, kAnchorNone}, "http://ex.com/", true},
33 {{"^", kSubdomain, kAnchorNone}, "http://ex.com/", false},
34 {{".", kSubdomain, kAnchorNone}, "http://ex.com/", false},
35 {{"", kAnchorNone, kBoundary}, "http://ex.com/", true},
36 {{"^", kAnchorNone, kBoundary}, "http://ex.com/", true},
37 {{".", kAnchorNone, kBoundary}, "http://ex.com/", false},
38 {{"", kBoundary, kBoundary}, "http://ex.com/", false},
39 {{"", kSubdomain, kBoundary}, "http://ex.com/", false},
40 {{"com/", kSubdomain, kBoundary}, "http://ex.com/", true},
41
69 {{"xampl", proto::URL_PATTERN_TYPE_SUBSTRING}, 42 {{"xampl", proto::URL_PATTERN_TYPE_SUBSTRING},
70 "http://example.com", 43 "http://example.com",
71 true}, 44 true},
72 {{"example", proto::URL_PATTERN_TYPE_SUBSTRING}, 45 {{"example", proto::URL_PATTERN_TYPE_SUBSTRING},
73 "http://example.com", 46 "http://example.com",
74 true}, 47 true},
75 {{"/a?a"}, "http://ex.com/a?a", true}, 48 {{"/a?a"}, "http://ex.com/a?a", true},
76 {{"^abc"}, "http://ex.com/abc?a", true}, 49 {{"^abc"}, "http://ex.com/abc?a", true},
77 {{"^abc"}, "http://ex.com/a?abc", true}, 50 {{"^abc"}, "http://ex.com/a?abc", true},
78 {{"^abc"}, "http://ex.com/abc?abc", true}, 51 {{"^abc"}, "http://ex.com/abc?abc", true},
52 {{"^abc^abc"}, "http://ex.com/abc?abc", true},
53 {{"^com^abc^abc"}, "http://ex.com/abc?abc", false},
79 54
80 {{"http://ex", kBoundary, kAnchorNone}, "http://example.com", true}, 55 {{"http://ex", kBoundary, kAnchorNone}, "http://example.com", true},
56 {{"http://ex", kAnchorNone, kAnchorNone}, "http://example.com", true},
81 {{"mple.com/", kAnchorNone, kBoundary}, "http://example.com", true}, 57 {{"mple.com/", kAnchorNone, kBoundary}, "http://example.com", true},
58 {{"mple.com/", kAnchorNone, kAnchorNone}, "http://example.com", true},
59 {{"mple.com/", kSubdomain, kAnchorNone}, "http://example.com", false},
60 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.com", false},
61 {{"ex.com", kSubdomain, kAnchorNone}, "http://ex.com", true},
62 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.ex.com", true},
63 {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.hex.com", false},
82 64
83 // Note: "example.com" will be normalized into "example.com/". 65 // Note: "example.com" will be normalized into "example.com/".
84 {{"http://*mpl", kBoundary, kAnchorNone}, "http://example.com", true}, 66 {{"http://*mpl", kBoundary, kAnchorNone}, "http://example.com", true},
85 {{"mpl*com/", kAnchorNone, kBoundary}, "http://example.com", true}, 67 {{"mpl*com/", kAnchorNone, kBoundary}, "http://example.com", true},
86 {{"example^com"}, "http://example.com", false}, 68 {{"example^com"}, "http://example.com", false},
87 {{"example^com"}, "http://example/com", true}, 69 {{"example^com"}, "http://example/com", true},
88 {{"example.com^"}, "http://example.com:8080", true}, 70 {{"example.com^"}, "http://example.com:8080", true},
89 {{"http*.com/", kBoundary, kBoundary}, "http://example.com", true}, 71 {{"http*.com/", kBoundary, kBoundary}, "http://example.com", true},
90 {{"http*.org/", kBoundary, kBoundary}, "http://example.com", false}, 72 {{"http*.org/", kBoundary, kBoundary}, "http://example.com", false},
91 73
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 {{"ex.co", kSubdomain, kBoundary}, "http://ex.com/", false}, 108 {{"ex.co", kSubdomain, kBoundary}, "http://ex.com/", false},
127 {{"ex.com", kSubdomain, kBoundary}, "http://rex.com.ex.com/", false}, 109 {{"ex.com", kSubdomain, kBoundary}, "http://rex.com.ex.com/", false},
128 {{"ex.com/", kSubdomain, kBoundary}, "http://rex.com.ex.com/", true}, 110 {{"ex.com/", kSubdomain, kBoundary}, "http://rex.com.ex.com/", true},
129 {{"http", kSubdomain, kBoundary}, "http://http.com/", false}, 111 {{"http", kSubdomain, kBoundary}, "http://http.com/", false},
130 {{"http", kSubdomain, kAnchorNone}, "http://http.com/", true}, 112 {{"http", kSubdomain, kAnchorNone}, "http://http.com/", true},
131 {{"/example.com", kSubdomain, kBoundary}, "http://example.com/", false}, 113 {{"/example.com", kSubdomain, kBoundary}, "http://example.com/", false},
132 {{"/example.com/", kSubdomain, kBoundary}, "http://example.com/", false}, 114 {{"/example.com/", kSubdomain, kBoundary}, "http://example.com/", false},
133 }; 115 };
134 116
135 for (const auto& test_case : kTestCases) { 117 for (const auto& test_case : kTestCases) {
136 SCOPED_TRACE(testing::Message() 118 SCOPED_TRACE(testing::Message() << "Rule: " << test_case.url_pattern
137 << "Rule: " << test_case.url_pattern.url_pattern 119 << "; 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 120
143 std::vector<size_t> failure; 121 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); 122 EXPECT_EQ(test_case.expect_match, is_match);
149 } 123 }
150 } 124 }
151 125
152 } // namespace subresource_filter 126 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698