Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/matcher.js

Issue 29556737: Issue 5141 - Convert filter match to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Sept. 26, 2017, 9:34 p.m.
Right Patch Set: Fixed many issues. One test left out. Created Oct. 6, 2017, 1:45 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« compiled/filter/Matcher.cpp ('K') | « lib/notification.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 "use strict";
19
20 const {createSandbox} = require("./_common");
21
22 let Filter = null;
23 let RegExpFilter = null;
24 let CombinedMatcher = null;
25 let defaultMatcher = null;
26 let Matcher = null;
27
28 exports.setUp = function(callback)
29 {
30 let sandboxedRequire = createSandbox();
31 (
32 {Filter, RegExpFilter} = sandboxedRequire("../lib/filterClasses"),
33 {CombinedMatcher, defaultMatcher, Matcher} = sandboxedRequire("../lib/matche r")
34 );
35
36 callback();
37 };
38
39 function compareKeywords(test, text, expected)
40 {
41 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)])
42 {
43 let matcher = Matcher.create();
44 let result = [];
45 for (let i = 0; i < expected.length; i++)
46 {
47 let keyword = matcher.findKeyword(filter);
48 result.push(keyword);
49 if (keyword)
50 {
51 let dummyFilter = Filter.fromText("^" + keyword + "^");
52 let keyword2 = matcher.findKeyword(dummyFilter);
53 test.equal(keyword2, keyword);
54 dummyFilter.filterCount = Infinity;
55 matcher.add(dummyFilter);
56 }
57 }
58 test.equal(result.join(", "), expected.join(", "), "Keyword candidates for " + filter.text);
59 }
60 }
61
62 function checkMatch(test, filters, location, contentType, docDomain, thirdParty, sitekey, specificOnly, expected)
63 {
64 let matcher = Matcher.create();
65 for (let filter of filters)
66 matcher.add(Filter.fromText(filter));
67
68 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d ocDomain, thirdParty, sitekey, specificOnly);
69 if (result)
70 result = result.text;
71
72 test.equal(result, expected, "match(" + location + ", " + contentType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " + (sitek ey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specificOnly" ) + ") with:\n" + filters.join("\n"));
73
74 let combinedMatcher = CombinedMatcher.create();
75 for (let i = 0; i < 2; i++)
76 {
77 for (let filter of filters)
78 combinedMatcher.add(Filter.fromText(filter));
79
80 result = combinedMatcher.matchesAny(location, RegExpFilter.typeMap[contentTy pe], docDomain, thirdParty, sitekey, specificOnly);
81 if (result)
82 result = result.text;
83
84 test.equal(result, expected, "combinedMatch(" + location + ", " + contentTyp e + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-spe cificOnly") + ") with:\n" + filters.join("\n"));
85
86 // Generic whitelisting rules can match for specificOnly searches, so we
87 // can't easily know which rule will match for these whitelisting tests
88 if (specificOnly)
89 continue;
90
91 // For next run: add whitelisting filters for filters that aren't already
92 filters = filters.map(text => text.substr(0, 2) == "@@" ? text : "@@" + text );
93 if (expected && expected.substr(0, 2) != "@@")
94 expected = "@@" + expected;
95 }
96 }
97
98 function cacheCheck(test, matcher, location, contentType, docDomain, thirdParty, expected)
99 {
100 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d ocDomain, thirdParty);
101 if (result)
102 result = result.text;
103
104 test.equal(result, expected, "match(" + location + ", " + contentType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with stati c filters");
105 }
106
107 exports.testMatcherClassDefinitions = function(test)
108 {
109 test.equal(typeof Matcher, "function", "typeof Matcher");
110 test.equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher");
111 test.equal(typeof defaultMatcher, "object", "typeof defaultMatcher");
112 test.ok(defaultMatcher instanceof CombinedMatcher, "defaultMatcher is a Combin edMatcher instance");
113
114 test.done();
115 };
116
117 exports.testMatcherContainer = function(test)
118 {
119 let matcher = Matcher.create();
120 let filter = Filter.fromText("/asdf/123456^");
121 test.ok(!matcher.hasFilter(filter));
122 matcher.add(filter);
123 test.ok(matcher.hasFilter(filter));
124 let keyword = matcher.getKeywordForFilter(filter);
125 test.equal(keyword, "123456");
126
127 let filter2 = Filter.fromText("/123456^");
128 matcher.add(filter2);
129 let keyword2 = matcher.getKeywordForFilter(filter2);
130 test.equal(keyword2, "123456");
131
132 let filter3 = Filter.fromText("@@/asdf/123456^");
133 matcher.add(filter3);
134 test.ok(matcher.hasFilter(filter3));
135 let keyword3 = matcher.getKeywordForFilter(filter3);
136 test.equal(keyword3, "asdf");
137
138 matcher.remove(filter);
139 test.ok(!matcher.hasFilter(filter));
140 test.done();
141 };
142
143 exports.testKeywordExtraction = function(test)
144 {
145 compareKeywords(test, "*", []);
146 compareKeywords(test, "asdf", []);
147 compareKeywords(test, "/asdf/", []);
148 compareKeywords(test, "/asdf1234", []);
149 compareKeywords(test, "/asdf/1234", ["asdf"]);
150 compareKeywords(test, "/asdf/1234^", ["asdf", "1234"]);
151 compareKeywords(test, "/asdf/123456^", ["123456", "asdf"]);
152 compareKeywords(test, "^asdf^1234^56as^", ["asdf", "1234", "56as"]);
153 compareKeywords(test, "*asdf/1234^", ["1234"]);
154 compareKeywords(test, "|asdf,1234*", ["asdf"]);
155 compareKeywords(test, "||domain.example^", ["example", "domain"]);
156 compareKeywords(test, "&asdf=1234|", ["asdf", "1234"]);
157 compareKeywords(test, "^foo%2Ebar^", ["foo%2ebar"]);
158 compareKeywords(test, "^aSdF^1234", ["asdf"]);
159 compareKeywords(test, "_asdf_1234_", ["asdf", "1234"]);
160 compareKeywords(test, "+asdf-1234=", ["asdf", "1234"]);
161 compareKeywords(test, "/123^ad2&ad&", ["123", "ad2"]);
162 compareKeywords(test, "/123^ad2&ad$script,domain=example.com", ["123", "ad2"]) ;
163
164 test.done();
165 };
166
167 exports.testFilterMatching = function(test)
168 {
169 checkMatch(test, [], "http://abc/def", "IMAGE", null, false, null, false, null );
170 checkMatch(test, ["abc"], "http://abc/def", "IMAGE", null, false, null, false, "abc");
171 checkMatch(test, ["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null, false, "abc");
172 checkMatch(test, ["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null, false, "abc");
173 checkMatch(test, ["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null, false, null);
174 checkMatch(test, ["abc", "://abc/d"], "http://abc/def", "IMAGE", null, false, null, false, "://abc/d");
175 checkMatch(test, ["://abc/d", "abc"], "http://abc/def", "IMAGE", null, false, null, false, "://abc/d");
176 checkMatch(test, ["|http://"], "http://abc/def", "IMAGE", null, false, null, f alse, "|http://");
177 checkMatch(test, ["|http://abc"], "http://abc/def", "IMAGE", null, false, null , false, "|http://abc");
178 checkMatch(test, ["|abc"], "http://abc/def", "IMAGE", null, false, null, false , null);
179 checkMatch(test, ["|/abc/def"], "http://abc/def", "IMAGE", null, false, null, false, null);
180 checkMatch(test, ["/def|"], "http://abc/def", "IMAGE", null, false, null, fals e, "/def|");
181 checkMatch(test, ["/abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, "/abc/def|");
182 checkMatch(test, ["/abc/|"], "http://abc/def", "IMAGE", null, false, null, fal se, null);
183 checkMatch(test, ["http://abc/|"], "http://abc/def", "IMAGE", null, false, nul l, false, null);
184 checkMatch(test, ["|http://abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, "|http://abc/def|");
185 checkMatch(test, ["|/abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, null);
186 checkMatch(test, ["|http://abc/|"], "http://abc/def", "IMAGE", null, false, nu ll, false, null);
187 checkMatch(test, ["|/abc/|"], "http://abc/def", "IMAGE", null, false, null, fa lse, null);
188 checkMatch(test, ["||example.com/abc"], "http://example.com/abc/def", "IMAGE", null, false, null, false, "||example.com/abc");
189 checkMatch(test, ["||com/abc/def"], "http://example.com/abc/def", "IMAGE", nul l, false, null, false, "||com/abc/def");
190 checkMatch(test, ["||com/abc"], "http://example.com/abc/def", "IMAGE", null, f alse, null, false, "||com/abc");
191 checkMatch(test, ["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, null);
192 checkMatch(test, ["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, null);
193 checkMatch(test, ["||http://example.com/"], "http://example.com/abc/def", "IMA GE", null, false, null, false, null);
194 checkMatch(test, ["||example.com/abc/def|"], "http://example.com/abc/def", "IM AGE", null, false, null, false, "||example.com/abc/def|");
195 checkMatch(test, ["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, "||com/abc/def|");
196 checkMatch(test, ["||example.com/abc|"], "http://example.com/abc/def", "IMAGE" , null, false, null, false, null);
197 checkMatch(test, ["abc", "://abc/d", "asdf1234"], "http://abc/def", "IMAGE", n ull, false, null, false, "://abc/d");
198 checkMatch(test, ["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "h ttp://abc/def", "IMAGE", null, false, null, false, "://abc/de");
199 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d ef", "IMAGE", null, false, null, false, "abc$~third-party");
200 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d ef", "IMAGE", null, true, null, false, "abc$third-party");
201 checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_de f"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$~third-part y");
202 checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_de f"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party" );
203 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def"], "http:/ /abc/def", "IMAGE", null, true, null, false, "//abc/def");
204 checkMatch(test, ["//abc/def", "abc$third-party", "abc$~third-party"], "http:/ /abc/def", "IMAGE", null, true, null, false, "//abc/def");
205 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part y"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party" );
206 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part y"], "http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party");
207 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$~third-par ty"], "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party");
208 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "IMAGE", null, false, null, false, "abc$image");
209 checkMatch(test, ["abc$image", "abc$script", "abc$~script"], "http://abc/def", "SCRIPT", null, false, null, false, "abc$script");
210 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "OTHER", null, false, null, false, "abc$~image");
211 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$image");
212 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~script"], "http://abc/def", "SCRIPT", null, false, null, false, "//abc/def$script");
213 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "OTHER", null, false, null, false, "//abc/def$~image");
214 checkMatch(test, ["abc$image", "abc$~image", "//abc/def"], "http://abc/def", " IMAGE", null, false, null, false, "//abc/def");
215 checkMatch(test, ["//abc/def", "abc$image", "abc$~image"], "http://abc/def", " IMAGE", null, false, null, false, "//abc/def");
216 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$image"], "http://abc/d ef", "IMAGE", null, false, null, false, "//abc/def$image");
217 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$script"], "http://abc/ def", "IMAGE", null, false, null, false, "abc$image");
218 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo .com|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$ domain=foo.com");
219 // checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~f oo.com|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, "ab c$domain=bar.com");
220 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo .com|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, "abc$ domain=~foo.com|~bar.com");
221 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo .com|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$ domain=foo.com");
222 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo .com|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, null) ;
223 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo .com|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, null) ;
224 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo .com|~bar.com"], "http://ccc/def", "IMAGE", "baz.com", false, null, false, "ccc$ domain=~foo.com|~bar.com");
225 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey =foo-publickey");
226 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey =bar-publickey");
227 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null);
228 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "baz.com", false, null, false, null);
229 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar -publickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "foo-p ublickey", false, "abc$sitekey=foo-publickey,domain=foo.com");
230 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar -publickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "bar-p ublickey", false, null);
231 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar -publickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "foo-p ublickey", false, null);
232 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar -publickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "bar-p ublickey", false, "abc$sitekey=bar-publickey,domain=bar.com");
233 checkMatch(test, ["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE ", "foo.com", false, null, false, "@@foo.com$generichide");
234 checkMatch(test, ["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLO CK", "foo.com", false, null, false, "@@foo.com$genericblock");
235 checkMatch(test, ["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE ", "foo.com", false, null, false, null);
236 checkMatch(test, ["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLO CK", "foo.com", false, null, false, null);
237 checkMatch(test, ["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, nu ll, true, null);
238 checkMatch(test, ["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo. com", false, null, true, "/bar$domain=foo.com");
239
240 test.done();
241 };
242
243 exports.testResultCacheChecks = function(test)
244 {
245 let matcher = new CombinedMatcher.create();
246 matcher.add(Filter.fromText("abc$image"));
247 matcher.add(Filter.fromText("abc$script"));
248 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping"));
249 matcher.add(Filter.fromText("cba$third-party"));
250 matcher.add(Filter.fromText("cba$~third-party,~script"));
251 matcher.add(Filter.fromText("http://def$image"));
252 matcher.add(Filter.fromText("http://def$script"));
253 matcher.add(Filter.fromText("http://def$~image,~script,~media,~ping"));
254 matcher.add(Filter.fromText("http://fed$third-party"));
255 matcher.add(Filter.fromText("http://fed$~third-party,~script"));
256
257 cacheCheck(test, matcher, "http://abc", "IMAGE", null, false, "abc$image");
258 cacheCheck(test, matcher, "http://abc", "SCRIPT", null, false, "abc$script");
259 cacheCheck(test, matcher, "http://abc", "OTHER", null, false, "abc$~image,~scr ipt,~media,~ping");
260 cacheCheck(test, matcher, "http://cba", "IMAGE", null, false, "cba$~third-part y,~script");
261 cacheCheck(test, matcher, "http://cba", "IMAGE", null, true, "cba$third-party" );
262 cacheCheck(test, matcher, "http://def", "IMAGE", null, false, "http://def$imag e");
263 cacheCheck(test, matcher, "http://def", "SCRIPT", null, false, "http://def$scr ipt");
264 cacheCheck(test, matcher, "http://def", "OTHER", null, false, "http://def$~ima ge,~script,~media,~ping");
265 cacheCheck(test, matcher, "http://fed", "IMAGE", null, false, "http://fed$~thi rd-party,~script");
266 cacheCheck(test, matcher, "http://fed", "IMAGE", null, true, "http://fed$third -party");
267 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, false, "cba$~third- party,~script");
268 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, true, "cba$third-pa rty");
269 cacheCheck(test, matcher, "http://abc_cba", "SCRIPT", null, false, "abc$script ");
270 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, false, "http ://fed$~third-party,~script");
271 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, true, "http: //fed$third-party");
272 cacheCheck(test, matcher, "http://def?http://fed", "SCRIPT", null, false, "htt p://def$script");
273
274 test.done();
275 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld