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

Side by Side Diff: test/matcher.js

Issue 29990555: Issue 7179 - Add function to return all matching filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add JSDoc for collection parameter Created Jan. 28, 2019, 5:34 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/matcher.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 if (specificOnly) 87 if (specificOnly)
88 continue; 88 continue;
89 89
90 // For next run: add whitelisting filters for filters that aren't already 90 // For next run: add whitelisting filters for filters that aren't already
91 filters = filters.map(text => text.substr(0, 2) == "@@" ? text : "@@" + text ); 91 filters = filters.map(text => text.substr(0, 2) == "@@" ? text : "@@" + text );
92 if (expected && expected.substr(0, 2) != "@@") 92 if (expected && expected.substr(0, 2) != "@@")
93 expected = "@@" + expected; 93 expected = "@@" + expected;
94 } 94 }
95 } 95 }
96 96
97 function checkSearch(test, filters, location, contentType, docDomain,
98 thirdParty, sitekey, specificOnly, filterType,
99 expected)
100 {
101 let matcher = new CombinedMatcher();
102 for (let filter of filters)
103 matcher.add(Filter.fromText(filter));
104
105 let result = matcher.search(location, RegExpFilter.typeMap[contentType],
106 docDomain, thirdParty, sitekey, specificOnly,
107 filterType);
108 for (let key in result)
109 result[key] = result[key].map(filter => filter.text);
110
111 test.deepEqual(result, expected, "search(" + location + ", " +
112 contentType + ", " + docDomain + ", " +
113 (thirdParty ? "third-party" : "first-party") + ", " +
114 (sitekey || "no-sitekey") + ", " +
115 (specificOnly ? "specificOnly" : "not-specificOnly") + ", " +
116 filterType + ") with:\n" + filters.join("\n"));
117 }
118
97 function cacheCheck(test, matcher, location, contentType, docDomain, thirdParty, expected) 119 function cacheCheck(test, matcher, location, contentType, docDomain, thirdParty, expected)
98 { 120 {
99 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d ocDomain, thirdParty); 121 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d ocDomain, thirdParty);
100 if (result) 122 if (result)
101 result = result.text; 123 result = result.text;
102 124
103 test.equal(result, expected, "match(" + location + ", " + contentType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with stati c filters"); 125 test.equal(result, expected, "match(" + location + ", " + contentType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with stati c filters");
104 } 126 }
105 127
106 exports.testMatcherClassDefinitions = function(test) 128 exports.testMatcherClassDefinitions = function(test)
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 checkMatch(test, ["||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com ", false, null, false, "||foo.com^$popup"); 241 checkMatch(test, ["||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com ", false, null, false, "||foo.com^$popup");
220 checkMatch(test, ["@@||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.c om", false, null, false, null, "@@||foo.com^$popup"); 242 checkMatch(test, ["@@||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.c om", false, null, false, null, "@@||foo.com^$popup");
221 checkMatch(test, ["||foo.com^$popup", "@@||foo.com^$popup"], "http://foo.com/b ar", "POPUP", "foo.com", false, null, false, "@@||foo.com^$popup", "||foo.com^$p opup"); 243 checkMatch(test, ["||foo.com^$popup", "@@||foo.com^$popup"], "http://foo.com/b ar", "POPUP", "foo.com", false, null, false, "@@||foo.com^$popup", "||foo.com^$p opup");
222 checkMatch(test, ["||foo.com^$csp=script-src 'none'"], "http://foo.com/bar", " CSP", "foo.com", false, null, false, "||foo.com^$csp=script-src 'none'"); 244 checkMatch(test, ["||foo.com^$csp=script-src 'none'"], "http://foo.com/bar", " CSP", "foo.com", false, null, false, "||foo.com^$csp=script-src 'none'");
223 checkMatch(test, ["@@||foo.com^$csp"], "http://foo.com/bar", "CSP", "foo.com", false, null, false, null, "@@||foo.com^$csp"); 245 checkMatch(test, ["@@||foo.com^$csp"], "http://foo.com/bar", "CSP", "foo.com", false, null, false, null, "@@||foo.com^$csp");
224 checkMatch(test, ["||foo.com^$csp=script-src 'none'", "@@||foo.com^$csp"], "ht tp://foo.com/bar", "CSP", "foo.com", false, null, false, "@@||foo.com^$csp", "|| foo.com^$csp=script-src 'none'"); 246 checkMatch(test, ["||foo.com^$csp=script-src 'none'", "@@||foo.com^$csp"], "ht tp://foo.com/bar", "CSP", "foo.com", false, null, false, "@@||foo.com^$csp", "|| foo.com^$csp=script-src 'none'");
225 247
226 test.done(); 248 test.done();
227 }; 249 };
228 250
251 exports.testFilterSearch = function(test)
252 {
253 // Start with three filters: foo, bar, and @@foo
254 let filters = ["foo", "bar", "@@foo"];
255
256 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com",
257 false, null, false, "all",
258 {blocking: ["foo"], whitelist: ["@@foo"]});
259
260 // Blocking only.
261 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com",
262 false, null, false, "blocking", {blocking: ["foo"]});
263
264 // Whitelist only.
265 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com",
266 false, null, false, "whitelist", {whitelist: ["@@foo"]});
267
268 // Different URLs.
269 checkSearch(test, filters, "http://example.com/bar", "IMAGE", "example.com",
270 false, null, false, "all", {blocking: ["bar"], whitelist: []});
271 checkSearch(test, filters, "http://example.com/foo/bar", "IMAGE",
272 "example.com", false, null, false, "all",
273 {blocking: ["foo", "bar"], whitelist: ["@@foo"]});
274
275 // Non-matching content type.
276 checkSearch(test, filters, "http://example.com/foo", "CSP", "example.com",
277 false, null, false, "all", {blocking: [], whitelist: []});
278
279 // Non-matching specificity.
280 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com",
281 false, null, true, "all", {blocking: [], whitelist: ["@@foo"]});
282
283 test.done();
284 };
285
229 exports.testResultCacheChecks = function(test) 286 exports.testResultCacheChecks = function(test)
230 { 287 {
231 let matcher = new CombinedMatcher(); 288 let matcher = new CombinedMatcher();
232 matcher.add(Filter.fromText("abc$image")); 289 matcher.add(Filter.fromText("abc$image"));
233 matcher.add(Filter.fromText("abc$script")); 290 matcher.add(Filter.fromText("abc$script"));
234 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping")); 291 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping"));
235 matcher.add(Filter.fromText("cba$third-party")); 292 matcher.add(Filter.fromText("cba$third-party"));
236 matcher.add(Filter.fromText("cba$~third-party,~script")); 293 matcher.add(Filter.fromText("cba$~third-party,~script"));
237 matcher.add(Filter.fromText("http://def$image")); 294 matcher.add(Filter.fromText("http://def$image"));
238 matcher.add(Filter.fromText("http://def$script")); 295 matcher.add(Filter.fromText("http://def$script"));
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 RegExpFilter.typeMap.IMAGE)); 363 RegExpFilter.typeMap.IMAGE));
307 364
308 matcher.remove(Filter.fromText("||example.com/foo/bar/image.jpg")); 365 matcher.remove(Filter.fromText("||example.com/foo/bar/image.jpg"));
309 366
310 // Make sure the filter got removed so there is no match. 367 // Make sure the filter got removed so there is no match.
311 test.ok(!matcher.matchesAny("https://example.com/foo/bar/image.jpg", 368 test.ok(!matcher.matchesAny("https://example.com/foo/bar/image.jpg",
312 RegExpFilter.typeMap.IMAGE)); 369 RegExpFilter.typeMap.IMAGE));
313 370
314 test.done(); 371 test.done();
315 }; 372 };
OLDNEW
« no previous file with comments | « lib/matcher.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld