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

Side by Side Diff: test/matcher.js

Issue 30025555: Issue 6820 - Move tests to mocha (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebased. Created April 5, 2019, 4:16 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 | « test/filterStorage_readwrite.js ('k') | test/notification.js » ('j') | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const assert = require("assert");
20 const {createSandbox} = require("./_common"); 21 const {createSandbox} = require("./_common");
21 22
22 let Filter = null; 23 let Filter = null;
23 let RegExpFilter = null; 24 let RegExpFilter = null;
24 let CombinedMatcher = null; 25 let CombinedMatcher = null;
25 let defaultMatcher = null; 26 let defaultMatcher = null;
26 let Matcher = null; 27 let Matcher = null;
27 let parseURL = null; 28 let parseURL = null;
28 29
29 exports.setUp = function(callback) 30 describe("Matcher", () =>
30 { 31 {
31 let sandboxedRequire = createSandbox(); 32 beforeEach(() =>
32 ( 33 {
33 {Filter, RegExpFilter} = sandboxedRequire("../lib/filterClasses"), 34 let sandboxedRequire = createSandbox();
34 {CombinedMatcher, defaultMatcher, Matcher} = sandboxedRequire("../lib/matche r"), 35 (
35 {parseURL} = sandboxedRequire("../lib/url") 36 {Filter, RegExpFilter} = sandboxedRequire("../lib/filterClasses"),
36 ); 37 {CombinedMatcher, defaultMatcher, Matcher} = sandboxedRequire("../lib/matc her"),
37 38 {parseURL} = sandboxedRequire("../lib/url")
38 callback(); 39 );
39 }; 40 });
40 41
41 function compareKeywords(test, text, expected) 42 function compareKeywords(text, expected)
42 { 43 {
43 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)]) 44 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)])
44 { 45 {
46 let matcher = new Matcher();
47 let result = [];
48 for (let i = 0; i < expected.length; i++)
49 {
50 let keyword = matcher.findKeyword(filter);
51 result.push(keyword);
52 if (keyword)
53 {
54 let dummyFilter = Filter.fromText("^" + keyword + "^");
55 dummyFilter.filterCount = Infinity;
56 matcher.add(dummyFilter);
57 }
58 }
59
60 assert.equal(result.join(", "), expected.join(", "), "Keyword candidates f or " + filter.text);
61 }
62 }
63
64 function checkMatch(filters, location, contentType, docDomain, sitekey, specif icOnly, expected, expectedFirstMatch = expected)
65 {
66 let url = parseURL(location);
67
45 let matcher = new Matcher(); 68 let matcher = new Matcher();
46 let result = [];
47 for (let i = 0; i < expected.length; i++)
48 {
49 let keyword = matcher.findKeyword(filter);
50 result.push(keyword);
51 if (keyword)
52 {
53 let dummyFilter = Filter.fromText("^" + keyword + "^");
54 dummyFilter.filterCount = Infinity;
55 matcher.add(dummyFilter);
56 }
57 }
58
59 test.equal(result.join(", "), expected.join(", "), "Keyword candidates for " + filter.text);
60 }
61 }
62
63 function checkMatch(test, filters, location, contentType, docDomain, sitekey, sp ecificOnly, expected, expectedFirstMatch = expected)
64 {
65 let url = parseURL(location);
66
67 let matcher = new Matcher();
68 for (let filter of filters)
69 matcher.add(Filter.fromText(filter));
70
71 let result = matcher.matchesAny(url, RegExpFilter.typeMap[contentType], docDom ain, sitekey, specificOnly);
72 if (result)
73 result = result.text;
74
75 test.equal(result, expectedFirstMatch, "match(" + location + ", " + contentTyp e + ", " + docDomain + ", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specificOnly") + ") with:\n" + filters.join("\n"));
76
77 let combinedMatcher = new CombinedMatcher();
78 for (let i = 0; i < 2; i++)
79 {
80 for (let filter of filters) 69 for (let filter of filters)
81 combinedMatcher.add(Filter.fromText(filter)); 70 matcher.add(Filter.fromText(filter));
82 71
83 result = combinedMatcher.matchesAny(url, RegExpFilter.typeMap[contentType], docDomain, sitekey, specificOnly); 72 let result = matcher.matchesAny(url, RegExpFilter.typeMap[contentType], docD omain, sitekey, specificOnly);
84 if (result) 73 if (result)
85 result = result.text; 74 result = result.text;
86 75
87 test.equal(result, expected, "combinedMatch(" + location + ", " + contentTyp e + ", " + docDomain + ", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specificOnly") + ") with:\n" + filters.join("\n")); 76 assert.equal(result, expectedFirstMatch, "match(" + location + ", " + conten tType + ", " + docDomain + ", " + (sitekey || "no-sitekey") + ", " + (specificOn ly ? "specificOnly" : "not-specificOnly") + ") with:\n" + filters.join("\n"));
88 77
89 // Generic whitelisting rules can match for specificOnly searches, so we 78 let combinedMatcher = new CombinedMatcher();
90 // can't easily know which rule will match for these whitelisting tests 79 for (let i = 0; i < 2; i++)
91 if (specificOnly) 80 {
92 continue; 81 for (let filter of filters)
93 82 combinedMatcher.add(Filter.fromText(filter));
94 // For next run: add whitelisting filters for filters that aren't already 83
95 filters = filters.map(text => text.substring(0, 2) == "@@" ? text : "@@" + t ext); 84 result = combinedMatcher.matchesAny(url, RegExpFilter.typeMap[contentType] , docDomain, sitekey, specificOnly);
96 if (expected && expected.substring(0, 2) != "@@") 85 if (result)
97 expected = "@@" + expected; 86 result = result.text;
87
88 assert.equal(result, expected, "combinedMatch(" + location + ", " + conten tType + ", " + docDomain + ", " + (sitekey || "no-sitekey") + ", " + (specificOn ly ? "specificOnly" : "not-specificOnly") + ") with:\n" + filters.join("\n"));
89
90 // Generic whitelisting rules can match for specificOnly searches, so we
91 // can't easily know which rule will match for these whitelisting tests
92 if (specificOnly)
93 continue;
94
95 // For next run: add whitelisting filters for filters that aren't already
96 filters = filters.map(text => text.substring(0, 2) == "@@" ? text : "@@" + text);
97 if (expected && expected.substring(0, 2) != "@@")
98 expected = "@@" + expected;
99 }
98 } 100 }
99 } 101
100 102 function checkSearch(filters, location, contentType, docDomain,
101 function checkSearch(test, filters, location, contentType, docDomain, 103 sitekey, specificOnly, filterType,
102 sitekey, specificOnly, filterType, expected) 104 expected)
103 { 105 {
104 let url = parseURL(location); 106 let url = parseURL(location);
105 107
106 let matcher = new CombinedMatcher(); 108 let matcher = new CombinedMatcher();
107 for (let filter of filters) 109 for (let filter of filters)
108 matcher.add(Filter.fromText(filter)); 110 matcher.add(Filter.fromText(filter));
109 111
110 let result = matcher.search(url, RegExpFilter.typeMap[contentType], 112 let result = matcher.search(url, RegExpFilter.typeMap[contentType],
111 docDomain, sitekey, specificOnly, filterType); 113 docDomain, sitekey, specificOnly,
112 for (let key in result) 114 filterType);
113 result[key] = result[key].map(filter => filter.text); 115 for (let key in result)
114 116 result[key] = result[key].map(filter => filter.text);
115 test.deepEqual(result, expected, "search(" + location + ", " + 117
116 contentType + ", " + docDomain + ", " + 118 assert.deepEqual(result, expected, "search(" + location + ", " +
117 (sitekey || "no-sitekey") + ", " + 119 contentType + ", " + docDomain + ", " +
118 (specificOnly ? "specificOnly" : "not-specificOnly") + ", " + 120 (sitekey || "no-sitekey") + ", " +
119 filterType + ") with:\n" + filters.join("\n")); 121 (specificOnly ? "specificOnly" : "not-specificOnly") + ", " +
120 } 122 filterType + ") with:\n" + filters.join("\n"));
121
122 function cacheCheck(test, matcher, location, contentType, docDomain, expected)
123 {
124 let url = parseURL(location);
125
126 let result = matcher.matchesAny(url, RegExpFilter.typeMap[contentType], docDom ain);
127 if (result)
128 result = result.text;
129
130 test.equal(result, expected, "match(" + location + ", " + contentType + ", " + docDomain + ") with static filters");
131 }
132
133 exports.testMatcherClassDefinitions = function(test)
134 {
135 test.equal(typeof Matcher, "function", "typeof Matcher");
136 test.equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher");
137 test.equal(typeof defaultMatcher, "object", "typeof defaultMatcher");
138 test.ok(defaultMatcher instanceof CombinedMatcher, "defaultMatcher is a Combin edMatcher instance");
139
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, null, false, null);
170 checkMatch(test, ["abc"], "http://abc/def", "IMAGE", null, null, false, "abc") ;
171 checkMatch(test, ["abc", "ddd"], "http://abc/def", "IMAGE", null, null, false, "abc");
172 checkMatch(test, ["ddd", "abc"], "http://abc/def", "IMAGE", null, null, false, "abc");
173 checkMatch(test, ["ddd", "abd"], "http://abc/def", "IMAGE", null, null, false, null);
174 checkMatch(test, ["abc", "://abc/d"], "http://abc/def", "IMAGE", null, null, f alse, "://abc/d");
175 checkMatch(test, ["://abc/d", "abc"], "http://abc/def", "IMAGE", null, null, f alse, "://abc/d");
176 checkMatch(test, ["|http://"], "http://abc/def", "IMAGE", null, null, false, " |http://");
177 checkMatch(test, ["|http://abc"], "http://abc/def", "IMAGE", null, null, false , "|http://abc");
178 checkMatch(test, ["|abc"], "http://abc/def", "IMAGE", null, null, false, null) ;
179 checkMatch(test, ["|/abc/def"], "http://abc/def", "IMAGE", null, null, false, null);
180 checkMatch(test, ["/def|"], "http://abc/def", "IMAGE", null, null, false, "/de f|");
181 checkMatch(test, ["/abc/def|"], "http://abc/def", "IMAGE", null, null, false, "/abc/def|");
182 checkMatch(test, ["/abc/|"], "http://abc/def", "IMAGE", null, null, false, nul l);
183 checkMatch(test, ["http://abc/|"], "http://abc/def", "IMAGE", null, null, fals e, null);
184 checkMatch(test, ["|http://abc/def|"], "http://abc/def", "IMAGE", null, null, false, "|http://abc/def|");
185 checkMatch(test, ["|/abc/def|"], "http://abc/def", "IMAGE", null, null, false, null);
186 checkMatch(test, ["|http://abc/|"], "http://abc/def", "IMAGE", null, null, fal se, null);
187 checkMatch(test, ["|/abc/|"], "http://abc/def", "IMAGE", null, null, false, nu ll);
188 checkMatch(test, ["||example.com/abc"], "http://example.com/abc/def", "IMAGE", null, null, false, "||example.com/abc");
189 checkMatch(test, ["||com/abc/def"], "http://example.com/abc/def", "IMAGE", nul l, null, false, "||com/abc/def");
190 checkMatch(test, ["||com/abc"], "http://example.com/abc/def", "IMAGE", null, n ull, false, "||com/abc");
191 checkMatch(test, ["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", nu ll, null, false, null);
192 checkMatch(test, ["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", nu ll, null, false, null);
193 checkMatch(test, ["||http://example.com/"], "http://example.com/abc/def", "IMA GE", null, null, false, null);
194 checkMatch(test, ["||example.com/abc/def|"], "http://example.com/abc/def", "IM AGE", null, null, false, "||example.com/abc/def|");
195 checkMatch(test, ["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", nu ll, null, false, "||com/abc/def|");
196 checkMatch(test, ["||example.com/abc|"], "http://example.com/abc/def", "IMAGE" , null, null, false, null);
197 checkMatch(test, ["abc", "://abc/d", "asdf1234"], "http://abc/def", "IMAGE", n ull, null, false, "://abc/d");
198 checkMatch(test, ["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "h ttp://abc/def", "IMAGE", null, null, false, "://abc/de");
199 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d ef", "IMAGE", null, null, false, "abc$~third-party");
200 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d ef", "IMAGE", "other-domain", null, false, "abc$third-party");
201 checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_de f"], "http://abc/def", "IMAGE", null, null, false, "//abc/def$~third-party");
202 checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_de f"], "http://abc/def", "IMAGE", "other-domain", null, false, "//abc/def$third-pa rty");
203 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def"], "http:/ /abc/def", "IMAGE", "other-domain", null, false, "//abc/def");
204 checkMatch(test, ["//abc/def", "abc$third-party", "abc$~third-party"], "http:/ /abc/def", "IMAGE", "other-domain", null, false, "//abc/def");
205 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part y"], "http://abc/def", "IMAGE", "other-domain", null, false, "//abc/def$third-pa rty");
206 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part y"], "http://abc/def", "IMAGE", null, null, false, "abc$~third-party");
207 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$~third-par ty"], "http://abc/def", "IMAGE", "other-domain", null, false, "abc$third-party") ;
208 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "IMAGE", null, null, false, "abc$image");
209 checkMatch(test, ["abc$image", "abc$script", "abc$~script"], "http://abc/def", "SCRIPT", null, null, false, "abc$script");
210 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "OTHER", null, null, false, "abc$~image");
211 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "IMAGE", null, null, false, "//abc/def$image");
212 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~script"], "http://abc/def", "SCRIPT", null, null, false, "//abc/def$script");
213 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "OTHER", null, null, false, "//abc/def$~image");
214 checkMatch(test, ["abc$image", "abc$~image", "//abc/def"], "http://abc/def", " IMAGE", null, null, false, "//abc/def");
215 checkMatch(test, ["//abc/def", "abc$image", "abc$~image"], "http://abc/def", " IMAGE", null, null, false, "//abc/def");
216 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$image"], "http://abc/d ef", "IMAGE", null, null, false, "//abc/def$image");
217 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$script"], "http://abc/ def", "IMAGE", null, null, false, "abc$image");
218 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo .com|~bar.com"], "http://foo.com/abc/def", "IMAGE", "foo.com", null, false, "abc $domain=foo.com");
219 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo .com|~bar.com"], "http://bar.com/abc/def", "IMAGE", "bar.com", null, false, "abc $domain=bar.com");
220 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo .com|~bar.com"], "http://baz.com/abc/def", "IMAGE", "baz.com", 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://foo.com/abc/def", "IMAGE", "foo.com", null, false, "abc $domain=foo.com");
222 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo .com|~bar.com"], "http://bar.com/abc/def", "IMAGE", "bar.com", null, false, null );
223 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo .com|~bar.com"], "http://baz.com/abc/def", "IMAGE", "baz.com", null, false, null );
224 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo .com|~bar.com"], "http://baz.com/ccc/def", "IMAGE", "baz.com", null, false, "ccc $domain=~foo.com|~bar.com");
225 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], " http://foo.com/abc/def", "IMAGE", "foo.com", "foo-publickey", false, "abc$siteke y=foo-publickey");
226 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], " http://bar.com/abc/def", "IMAGE", "bar.com", "bar-publickey", false, "abc$siteke y=bar-publickey");
227 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], " http://bar.com/abc/def", "IMAGE", "bar.com", "bar-publickey", false, null);
228 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], " http://baz.com/abc/def", "IMAGE", "baz.com", null, false, null);
229 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar -publickey,domain=bar.com"], "http://foo.com/abc/def", "IMAGE", "foo.com", "foo- publickey", 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://foo.com/abc/def", "IMAGE", "foo.com", "bar- publickey", false, null);
231 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar -publickey,domain=bar.com"], "http://bar.com/abc/def", "IMAGE", "bar.com", "foo- publickey", false, null);
232 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar -publickey,domain=bar.com"], "http://bar.com/abc/def", "IMAGE", "bar.com", "bar- publickey", false, "abc$sitekey=bar-publickey,domain=bar.com");
233 checkMatch(test, ["@@foo.com$document"], "http://foo.com/bar", "DOCUMENT", "fo o.com", null, false, "@@foo.com$document");
234 checkMatch(test, ["@@foo.com$elemhide"], "http://foo.com/bar", "ELEMHIDE", "fo o.com", null, false, "@@foo.com$elemhide");
235 checkMatch(test, ["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE ", "foo.com", null, false, "@@foo.com$generichide");
236 checkMatch(test, ["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLO CK", "foo.com", null, false, "@@foo.com$genericblock");
237 checkMatch(test, ["@@bar.com$document"], "http://foo.com/bar", "DOCUMENT", "fo o.com", null, false, null);
238 checkMatch(test, ["@@bar.com$elemhide"], "http://foo.com/bar", "ELEMHIDE", "fo o.com", null, false, null);
239 checkMatch(test, ["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE ", "foo.com", null, false, null);
240 checkMatch(test, ["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLO CK", "foo.com", null, false, null);
241 checkMatch(test, ["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", null, tru e, null);
242 checkMatch(test, ["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo. com", null, true, "/bar$domain=foo.com");
243 checkMatch(test, ["@@||foo.com^"], "http://foo.com/bar", "IMAGE", "foo.com", n ull, false, null, "@@||foo.com^");
244 checkMatch(test, ["/bar", "@@||foo.com^"], "http://foo.com/bar", "IMAGE", "foo .com", null, false, "@@||foo.com^");
245 checkMatch(test, ["/bar", "@@||foo.com^"], "http://foo.com/foo", "IMAGE", "foo .com", null, false, null, "@@||foo.com^");
246 checkMatch(test, ["||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com ", null, false, "||foo.com^$popup");
247 checkMatch(test, ["@@||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.c om", null, false, null, "@@||foo.com^$popup");
248 checkMatch(test, ["||foo.com^$popup", "@@||foo.com^$popup"], "http://foo.com/b ar", "POPUP", "foo.com", null, false, "@@||foo.com^$popup", "||foo.com^$popup");
249 checkMatch(test, ["||foo.com^$csp=script-src 'none'"], "http://foo.com/bar", " CSP", "foo.com", null, false, "||foo.com^$csp=script-src 'none'");
250 checkMatch(test, ["@@||foo.com^$csp"], "http://foo.com/bar", "CSP", "foo.com", null, false, null, "@@||foo.com^$csp");
251 checkMatch(test, ["||foo.com^$csp=script-src 'none'", "@@||foo.com^$csp"], "ht tp://foo.com/bar", "CSP", "foo.com", null, false, "@@||foo.com^$csp", "||foo.com ^$csp=script-src 'none'");
252
253 // See #7312.
254 checkMatch(test, ["^foo/bar/$script"], "http://foo/bar/", "SCRIPT", "example.c om", null, true, null);
255 checkMatch(test, ["^foo/bar/$script"], "http://foo/bar/", "SCRIPT", "example.c om", null, false, "^foo/bar/$script");
256 checkMatch(test, ["^foo/bar/$script,domain=example.com", "@@^foo/bar/$script"] , "http://foo/bar/", "SCRIPT", "example.com", null, true, "@@^foo/bar/$script", "^foo/bar/$script,domain=example.com");
257 checkMatch(test, ["@@^foo/bar/$script", "^foo/bar/$script,domain=example.com"] , "http://foo/bar/", "SCRIPT", "example.com", null, true, "@@^foo/bar/$script", "^foo/bar/$script,domain=example.com");
258 checkMatch(test, ["@@^foo/bar/$script", "^foo/bar/$script,domain=example.com"] , "http://foo/bar/", "SCRIPT", "example.com", null, false, "@@^foo/bar/$script") ;
259
260 test.done();
261 };
262
263 exports.testFilterSearch = function(test)
264 {
265 // Start with three filters: foo, bar, and @@foo
266 let filters = ["foo", "bar", "@@foo"];
267
268 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com",
269 null, false, "all",
270 {blocking: ["foo"], whitelist: ["@@foo"]});
271
272 // Blocking only.
273 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com",
274 null, false, "blocking", {blocking: ["foo"]});
275
276 // Whitelist only.
277 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com",
278 null, false, "whitelist", {whitelist: ["@@foo"]});
279
280 // Different URLs.
281 checkSearch(test, filters, "http://example.com/bar", "IMAGE", "example.com",
282 null, false, "all", {blocking: ["bar"], whitelist: []});
283 checkSearch(test, filters, "http://example.com/foo/bar", "IMAGE",
284 "example.com", null, false, "all",
285 {blocking: ["foo", "bar"], whitelist: ["@@foo"]});
286
287 // Non-matching content type.
288 checkSearch(test, filters, "http://example.com/foo", "CSP", "example.com",
289 null, false, "all", {blocking: [], whitelist: []});
290
291 // Non-matching specificity.
292 checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com",
293 null, true, "all", {blocking: [], whitelist: ["@@foo"]});
294
295 test.done();
296 };
297
298 exports.testResultCacheChecks = function(test)
299 {
300 let matcher = new CombinedMatcher();
301 matcher.add(Filter.fromText("abc$image"));
302 matcher.add(Filter.fromText("abc$script"));
303 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping"));
304 matcher.add(Filter.fromText("cba$third-party"));
305 matcher.add(Filter.fromText("cba$~third-party,~script"));
306 matcher.add(Filter.fromText("http://def$image"));
307 matcher.add(Filter.fromText("http://def$script"));
308 matcher.add(Filter.fromText("http://def$~image,~script,~media,~ping"));
309 matcher.add(Filter.fromText("http://fed$third-party"));
310 matcher.add(Filter.fromText("http://fed$~third-party,~script"));
311
312 cacheCheck(test, matcher, "http://abc", "IMAGE", null, "abc$image");
313 cacheCheck(test, matcher, "http://abc", "SCRIPT", null, "abc$script");
314 cacheCheck(test, matcher, "http://abc", "OTHER", null, "abc$~image,~script,~me dia,~ping");
315 cacheCheck(test, matcher, "http://cba", "IMAGE", null, "cba$~third-party,~scri pt");
316 cacheCheck(test, matcher, "http://cba", "IMAGE", "other-domain", "cba$third-pa rty");
317 cacheCheck(test, matcher, "http://def", "IMAGE", null, "http://def$image");
318 cacheCheck(test, matcher, "http://def", "SCRIPT", null, "http://def$script");
319 cacheCheck(test, matcher, "http://def", "OTHER", null, "http://def$~image,~scr ipt,~media,~ping");
320 cacheCheck(test, matcher, "http://fed", "IMAGE", null, "http://fed$~third-part y,~script");
321 cacheCheck(test, matcher, "http://fed", "IMAGE", "other-domain", "http://fed$t hird-party");
322 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, "cba$~third-party,~ script");
323 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", "other-domain", "cba$thir d-party");
324 cacheCheck(test, matcher, "http://abc_cba", "SCRIPT", null, "abc$script");
325 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, "http://fed$ ~third-party,~script");
326 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", "other-domain", "h ttp://fed$third-party");
327 cacheCheck(test, matcher, "http://def?http://fed", "SCRIPT", null, "http://def $script");
328
329 test.done();
330 };
331
332 exports.testWhitelisted = function(test)
333 {
334 let matcher = new CombinedMatcher();
335
336 test.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
337 RegExpFilter.typeMap.IMAGE));
338 test.ok(!matcher.isWhitelisted(parseURL("https://example.com/bar"),
339 RegExpFilter.typeMap.IMAGE));
340 test.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
341 RegExpFilter.typeMap.SUBDOCUMENT));
342
343 matcher.add(Filter.fromText("@@/foo^$image"));
344
345 test.ok(matcher.isWhitelisted(parseURL("https://example.com/foo"),
346 RegExpFilter.typeMap.IMAGE));
347 test.ok(!matcher.isWhitelisted(parseURL("https://example.com/bar"),
348 RegExpFilter.typeMap.IMAGE));
349 test.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
350 RegExpFilter.typeMap.SUBDOCUMENT));
351
352 matcher.remove(Filter.fromText("@@/foo^$image"));
353
354 test.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
355 RegExpFilter.typeMap.IMAGE));
356 test.ok(!matcher.isWhitelisted(parseURL("https://example.com/bar"),
357 RegExpFilter.typeMap.IMAGE));
358 test.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
359 RegExpFilter.typeMap.SUBDOCUMENT));
360
361 test.done();
362 };
363
364 exports.testAddRemoveByKeyword = function(test)
365 {
366 let matcher = new CombinedMatcher();
367
368 matcher.add(Filter.fromText("||example.com/foo/bar/ad.jpg^"));
369
370 // Add the same filter a second time to make sure it doesn't get added again
371 // by a different keyword.
372 matcher.add(Filter.fromText("||example.com/foo/bar/ad.jpg^"));
373
374 test.ok(!!matcher.matchesAny(parseURL("https://example.com/foo/bar/ad.jpg"),
375 RegExpFilter.typeMap.IMAGE));
376
377 matcher.remove(Filter.fromText("||example.com/foo/bar/ad.jpg^"));
378
379 // Make sure the filter got removed so there is no match.
380 test.ok(!matcher.matchesAny(parseURL("https://example.com/foo/bar/ad.jpg"),
381 RegExpFilter.typeMap.IMAGE));
382
383
384 // Map { "example" => { text: "||example.com^$~third-party" } }
385 matcher.add(Filter.fromText("||example.com^$~third-party"));
386
387 test.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1);
388
389 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword)
390 {
391 test.equal(key, "example");
392 test.deepEqual(value, Filter.fromText("||example.com^$~third-party"));
393 break;
394 } 123 }
395 124
396 test.ok(!!matcher.matchesAny(parseURL("https://example.com/example/ad.jpg"), 125 function cacheCheck(matcher, location, contentType, docDomain, expected)
397 RegExpFilter.typeMap.IMAGE, "example.com")); 126 {
398 127 let url = parseURL(location);
399 // Map { 128
400 // "example" => Map { 129 let result = matcher.matchesAny(url, RegExpFilter.typeMap[contentType], docD omain);
401 // "" => Map { 130 if (result)
402 // { text: "||example.com^$~third-party" } => true, 131 result = result.text;
403 // { text: "/example/*$~third-party" } => true 132
404 // } 133 assert.equal(result, expected, "match(" + location + ", " + contentType + ", " + docDomain + ") with static filters");
405 // }
406 // }
407 matcher.add(Filter.fromText("/example/*$~third-party"));
408
409 test.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1);
410
411 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword)
412 {
413 test.equal(key, "example");
414 test.equal(value.size, 1);
415
416 let map = value.get("");
417 test.equal(map.size, 2);
418 test.equal(map.get(Filter.fromText("||example.com^$~third-party")), true);
419 test.equal(map.get(Filter.fromText("/example/*$~third-party")), true);
420
421 break;
422 } 134 }
423 135
424 test.ok(!!matcher.matchesAny(parseURL("https://example.com/example/ad.jpg"), 136 it("Class Definitions", () =>
425 RegExpFilter.typeMap.IMAGE, "example.com")); 137 {
426 138 assert.equal(typeof Matcher, "function", "typeof Matcher");
427 // Map { "example" => { text: "/example/*$~third-party" } } 139 assert.equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher");
428 matcher.remove(Filter.fromText("||example.com^$~third-party")); 140 assert.equal(typeof defaultMatcher, "object", "typeof defaultMatcher");
429 141 assert.ok(defaultMatcher instanceof CombinedMatcher, "defaultMatcher is a Co mbinedMatcher instance");
430 test.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1); 142 });
431 143
432 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword) 144 it("Keyword Extraction", () =>
433 { 145 {
434 test.equal(key, "example"); 146 compareKeywords("*", []);
435 test.deepEqual(value, Filter.fromText("/example/*$~third-party")); 147 compareKeywords("asdf", []);
436 break; 148 compareKeywords("/asdf/", []);
437 } 149 compareKeywords("/asdf1234", []);
438 150 compareKeywords("/asdf/1234", ["asdf"]);
439 test.ok(!!matcher.matchesAny(parseURL("https://example.com/example/ad.jpg"), 151 compareKeywords("/asdf/1234^", ["asdf", "1234"]);
440 RegExpFilter.typeMap.IMAGE, "example.com")); 152 compareKeywords("/asdf/123456^", ["123456", "asdf"]);
441 153 compareKeywords("^asdf^1234^56as^", ["asdf", "1234", "56as"]);
442 // Map {} 154 compareKeywords("*asdf/1234^", ["1234"]);
443 matcher.remove(Filter.fromText("/example/*$~third-party")); 155 compareKeywords("|asdf,1234*", ["asdf"]);
444 156 compareKeywords("||domain.example^", ["example", "domain"]);
445 test.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 0); 157 compareKeywords("&asdf=1234|", ["asdf", "1234"]);
446 158 compareKeywords("^foo%2Ebar^", ["foo%2ebar"]);
447 test.ok(!matcher.matchesAny(parseURL("https://example.com/example/ad.jpg"), 159 compareKeywords("^aSdF^1234", ["asdf"]);
448 RegExpFilter.typeMap.IMAGE, "example.com")); 160 compareKeywords("_asdf_1234_", ["asdf", "1234"]);
449 161 compareKeywords("+asdf-1234=", ["asdf", "1234"]);
450 test.done(); 162 compareKeywords("/123^ad2&ad&", ["123", "ad2"]);
451 }; 163 compareKeywords("/123^ad2&ad$script,domain=example.com", ["123", "ad2"]);
164 });
165
166 it("Filter Matching", () =>
167 {
168 checkMatch([], "http://abc/def", "IMAGE", null, null, false, null);
169 checkMatch(["abc"], "http://abc/def", "IMAGE", null, null, false, "abc");
170 checkMatch(["abc", "ddd"], "http://abc/def", "IMAGE", null, null, false, "ab c");
171 checkMatch(["ddd", "abc"], "http://abc/def", "IMAGE", null, null, false, "ab c");
172 checkMatch(["ddd", "abd"], "http://abc/def", "IMAGE", null, null, false, nul l);
173 checkMatch(["abc", "://abc/d"], "http://abc/def", "IMAGE", null, null, false , "://abc/d");
174 checkMatch(["://abc/d", "abc"], "http://abc/def", "IMAGE", null, null, false , "://abc/d");
175 checkMatch(["|http://"], "http://abc/def", "IMAGE", null, null, false, "|htt p://");
176 checkMatch(["|http://abc"], "http://abc/def", "IMAGE", null, null, false, "| http://abc");
177 checkMatch(["|abc"], "http://abc/def", "IMAGE", null, null, false, null);
178 checkMatch(["|/abc/def"], "http://abc/def", "IMAGE", null, null, false, null );
179 checkMatch(["/def|"], "http://abc/def", "IMAGE", null, null, false, "/def|") ;
180 checkMatch(["/abc/def|"], "http://abc/def", "IMAGE", null, null, false, "/ab c/def|");
181 checkMatch(["/abc/|"], "http://abc/def", "IMAGE", null, null, false, null);
182 checkMatch(["http://abc/|"], "http://abc/def", "IMAGE", null, null, false, n ull);
183 checkMatch(["|http://abc/def|"], "http://abc/def", "IMAGE", null, null, fals e, "|http://abc/def|");
184 checkMatch(["|/abc/def|"], "http://abc/def", "IMAGE", null, null, false, nul l);
185 checkMatch(["|http://abc/|"], "http://abc/def", "IMAGE", null, null, false, null);
186 checkMatch(["|/abc/|"], "http://abc/def", "IMAGE", null, null, false, null);
187 checkMatch(["||example.com/abc"], "http://example.com/abc/def", "IMAGE", nul l, null, false, "||example.com/abc");
188 checkMatch(["||com/abc/def"], "http://example.com/abc/def", "IMAGE", null, n ull, false, "||com/abc/def");
189 checkMatch(["||com/abc"], "http://example.com/abc/def", "IMAGE", null, null, false, "||com/abc");
190 checkMatch(["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", null, null, false, null);
191 checkMatch(["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", null, null, false, null);
192 checkMatch(["||http://example.com/"], "http://example.com/abc/def", "IMAGE", null, null, false, null);
193 checkMatch(["||example.com/abc/def|"], "http://example.com/abc/def", "IMAGE" , null, null, false, "||example.com/abc/def|");
194 checkMatch(["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", null, null, false, "||com/abc/def|");
195 checkMatch(["||example.com/abc|"], "http://example.com/abc/def", "IMAGE", nu ll, null, false, null);
196 checkMatch(["abc", "://abc/d", "asdf1234"], "http://abc/def", "IMAGE", null, null, false, "://abc/d");
197 checkMatch(["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "http: //abc/def", "IMAGE", null, null, false, "://abc/de");
198 checkMatch(["abc$third-party", "abc$~third-party", "ddd"], "http://abc/def", "IMAGE", null, null, false, "abc$~third-party");
199 checkMatch(["abc$third-party", "abc$~third-party", "ddd"], "http://abc/def", "IMAGE", "other-domain", null, false, "abc$third-party");
200 checkMatch(["//abc/def$third-party", "//abc/def$~third-party", "//abc_def"], "http://abc/def", "IMAGE", null, null, false, "//abc/def$~third-party");
201 checkMatch(["//abc/def$third-party", "//abc/def$~third-party", "//abc_def"], "http://abc/def", "IMAGE", "other-domain", null, false, "//abc/def$third-party" );
202 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def"], "http://abc /def", "IMAGE", null, null, false, "//abc/def");
203 checkMatch(["//abc/def", "abc$third-party", "abc$~third-party"], "http://abc /def", "IMAGE", null, null, false, "//abc/def");
204 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$third-party"], "http://abc/def", "IMAGE", "other-domain", null, false, "//abc/def$third-party" );
205 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$third-party"], "http://abc/def", "IMAGE", null, null, false, "abc$~third-party");
206 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$~third-party"] , "http://abc/def", "IMAGE", "other-domain", null, false, "abc$third-party");
207 checkMatch(["abc$image", "abc$script", "abc$~image"], "http://abc/def", "IMA GE", null, null, false, "abc$image");
208 checkMatch(["abc$image", "abc$script", "abc$~script"], "http://abc/def", "SC RIPT", null, null, false, "abc$script");
209 checkMatch(["abc$image", "abc$script", "abc$~image"], "http://abc/def", "OTH ER", null, null, false, "abc$~image");
210 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "htt p://abc/def", "IMAGE", null, null, false, "//abc/def$image");
211 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~script"], "ht tp://abc/def", "SCRIPT", null, null, false, "//abc/def$script");
212 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "htt p://abc/def", "OTHER", null, null, false, "//abc/def$~image");
213 checkMatch(["abc$image", "abc$~image", "//abc/def"], "http://abc/def", "IMAG E", null, null, false, "//abc/def");
214 checkMatch(["//abc/def", "abc$image", "abc$~image"], "http://abc/def", "IMAG E", null, null, false, "//abc/def");
215 checkMatch(["abc$image", "abc$~image", "//abc/def$image"], "http://abc/def", "IMAGE", null, null, false, "//abc/def$image");
216 checkMatch(["abc$image", "abc$~image", "//abc/def$script"], "http://abc/def" , "IMAGE", null, null, false, "abc$image");
217 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "foo.com", null, false, "abc$domain=foo. com");
218 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "bar.com", null, false, "abc$domain=bar. com");
219 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "baz.com", null, false, "abc$domain=~foo .com|~bar.com");
220 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "foo.com", null, false, "abc$domain=foo. com");
221 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "bar.com", null, false, null);
222 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "baz.com", null, false, null);
223 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://ccc/def", "IMAGE", "baz.com", null, false, "ccc$domain=~foo .com|~bar.com");
224 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "foo.com", "foo-publickey", false, "abc$sitekey=foo-public key");
225 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "bar.com", "bar-publickey", false, "abc$sitekey=bar-public key");
226 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "bar.com", "bar-publickey", false, null);
227 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "baz.com", null, false, null);
228 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", "foo-publickey", false, "abc$sitekey=foo-publickey,domain=foo.com");
229 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", "bar-publickey", false, null);
230 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", "foo-publickey", false, null);
231 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", "bar-publickey", false, "abc$sitekey=bar-publickey,domain=bar.com");
232 checkMatch(["@@foo.com$document"], "http://foo.com/bar", "DOCUMENT", "foo.co m", null, false, "@@foo.com$document");
233 checkMatch(["@@foo.com$elemhide"], "http://foo.com/bar", "ELEMHIDE", "foo.co m", null, false, "@@foo.com$elemhide");
234 checkMatch(["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE", " foo.com", null, false, "@@foo.com$generichide");
235 checkMatch(["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK", "foo.com", null, false, "@@foo.com$genericblock");
236 checkMatch(["@@bar.com$document"], "http://foo.com/bar", "DOCUMENT", "foo.co m", null, false, null);
237 checkMatch(["@@bar.com$elemhide"], "http://foo.com/bar", "ELEMHIDE", "foo.co m", null, false, null);
238 checkMatch(["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE", " foo.com", null, false, null);
239 checkMatch(["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK", "foo.com", null, false, null);
240 checkMatch(["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", null, true, n ull);
241 checkMatch(["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo.com" , null, true, "/bar$domain=foo.com");
242 checkMatch(["@@||foo.com^"], "http://foo.com/bar", "IMAGE", "foo.com", null, false, null, "@@||foo.com^");
243 checkMatch(["/bar", "@@||foo.com^"], "http://foo.com/bar", "IMAGE", "foo.com ", null, false, "@@||foo.com^");
244 checkMatch(["/bar", "@@||foo.com^"], "http://foo.com/foo", "IMAGE", "foo.com ", null, false, null, "@@||foo.com^");
245 checkMatch(["||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com", n ull, false, "||foo.com^$popup");
246 checkMatch(["@@||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com", null, false, null, "@@||foo.com^$popup");
247 checkMatch(["||foo.com^$popup", "@@||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com", null, false, "@@||foo.com^$popup", "||foo.com^$popup");
248 checkMatch(["||foo.com^$csp=script-src 'none'"], "http://foo.com/bar", "CSP" , "foo.com", null, false, "||foo.com^$csp=script-src 'none'");
249 checkMatch(["@@||foo.com^$csp"], "http://foo.com/bar", "CSP", "foo.com", nul l, false, null, "@@||foo.com^$csp");
250 checkMatch(["||foo.com^$csp=script-src 'none'", "@@||foo.com^$csp"], "http:/ /foo.com/bar", "CSP", "foo.com", null, false, "@@||foo.com^$csp", "||foo.com^$cs p=script-src 'none'");
251
252 // See #7312.
253 checkMatch(["^foo/bar/$script"], "http://foo/bar/", "SCRIPT", "example.com", null, true, null);
254 checkMatch(["^foo/bar/$script"], "http://foo/bar/", "SCRIPT", "example.com", null, false, "^foo/bar/$script");
255 checkMatch(["^foo/bar/$script,domain=example.com", "@@^foo/bar/$script"], "h ttp://foo/bar/", "SCRIPT", "example.com", null, true, "@@^foo/bar/$script", "^fo o/bar/$script,domain=example.com");
256 checkMatch(["@@^foo/bar/$script", "^foo/bar/$script,domain=example.com"], "h ttp://foo/bar/", "SCRIPT", "example.com", null, true, "@@^foo/bar/$script", "^fo o/bar/$script,domain=example.com");
257 checkMatch(["@@^foo/bar/$script", "^foo/bar/$script,domain=example.com"], "h ttp://foo/bar/", "SCRIPT", "example.com", null, false, "@@^foo/bar/$script");
258 });
259
260 it("Filter Search", () =>
261 {
262 // Start with three filters: foo, bar, and @@foo
263 let filters = ["foo", "bar", "@@foo"];
264
265 checkSearch(filters, "http://example.com/foo", "IMAGE", "example.com",
266 null, false, "all",
267 {blocking: ["foo"], whitelist: ["@@foo"]});
268
269 // Blocking only.
270 checkSearch(filters, "http://example.com/foo", "IMAGE", "example.com",
271 null, false, "blocking", {blocking: ["foo"]});
272
273 // Whitelist only.
274 checkSearch(filters, "http://example.com/foo", "IMAGE", "example.com",
275 null, false, "whitelist", {whitelist: ["@@foo"]});
276
277 // Different URLs.
278 checkSearch(filters, "http://example.com/bar", "IMAGE", "example.com",
279 null, false, "all", {blocking: ["bar"], whitelist: []});
280 checkSearch(filters, "http://example.com/foo/bar", "IMAGE",
281 "example.com", null, false, "all",
282 {blocking: ["foo", "bar"], whitelist: ["@@foo"]});
283
284 // Non-matching content type.
285 checkSearch(filters, "http://example.com/foo", "CSP", "example.com",
286 null, false, "all", {blocking: [], whitelist: []});
287
288 // Non-matching specificity.
289 checkSearch(filters, "http://example.com/foo", "IMAGE", "example.com",
290 null, true, "all", {blocking: [], whitelist: ["@@foo"]});
291 });
292
293 it("Result cache checks", () =>
294 {
295 let matcher = new CombinedMatcher();
296 matcher.add(Filter.fromText("abc$image"));
297 matcher.add(Filter.fromText("abc$script"));
298 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping"));
299 matcher.add(Filter.fromText("cba$third-party"));
300 matcher.add(Filter.fromText("cba$~third-party,~script"));
301 matcher.add(Filter.fromText("http://def$image"));
302 matcher.add(Filter.fromText("http://def$script"));
303 matcher.add(Filter.fromText("http://def$~image,~script,~media,~ping"));
304 matcher.add(Filter.fromText("http://fed$third-party"));
305 matcher.add(Filter.fromText("http://fed$~third-party,~script"));
306
307 cacheCheck(matcher, "http://abc", "IMAGE", null, "abc$image");
308 cacheCheck(matcher, "http://abc", "SCRIPT", null, "abc$script");
309 cacheCheck(matcher, "http://abc", "OTHER", null, "abc$~image,~script,~media, ~ping");
310 cacheCheck(matcher, "http://cba", "IMAGE", null, "cba$~third-party,~script") ;
311 cacheCheck(matcher, "http://cba", "IMAGE", "other-domain", "cba$third-party" );
312 cacheCheck(matcher, "http://def", "IMAGE", null, "http://def$image");
313 cacheCheck(matcher, "http://def", "SCRIPT", null, "http://def$script");
314 cacheCheck(matcher, "http://def", "OTHER", null, "http://def$~image,~script, ~media,~ping");
315 cacheCheck(matcher, "http://fed", "IMAGE", null, "http://fed$~third-party,~s cript");
316 cacheCheck(matcher, "http://fed", "IMAGE", "other-domain", "http://fed$third -party");
317 cacheCheck(matcher, "http://abc_cba", "MEDIA", null, "cba$~third-party,~scri pt");
318 cacheCheck(matcher, "http://abc_cba", "MEDIA", "other-domain", "cba$third-pa rty");
319 cacheCheck(matcher, "http://abc_cba", "SCRIPT", null, "abc$script");
320 cacheCheck(matcher, "http://def?http://fed", "MEDIA", null, "http://fed$~thi rd-party,~script");
321 cacheCheck(matcher, "http://def?http://fed", "MEDIA", "other-domain", "http: //fed$third-party");
322 cacheCheck(matcher, "http://def?http://fed", "SCRIPT", null, "http://def$scr ipt");
323 });
324
325 it("Whitelisted", () =>
326 {
327 let matcher = new CombinedMatcher();
328
329 assert.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
330 RegExpFilter.typeMap.IMAGE));
331 assert.ok(!matcher.isWhitelisted(parseURL("https://example.com/bar"),
332 RegExpFilter.typeMap.IMAGE));
333 assert.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
334 RegExpFilter.typeMap.SUBDOCUMENT));
335
336 matcher.add(Filter.fromText("@@/foo^$image"));
337
338 assert.ok(matcher.isWhitelisted(parseURL("https://example.com/foo"),
339 RegExpFilter.typeMap.IMAGE));
340 assert.ok(!matcher.isWhitelisted(parseURL("https://example.com/bar"),
341 RegExpFilter.typeMap.IMAGE));
342 assert.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
343 RegExpFilter.typeMap.SUBDOCUMENT));
344
345 matcher.remove(Filter.fromText("@@/foo^$image"));
346
347 assert.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
348 RegExpFilter.typeMap.IMAGE));
349 assert.ok(!matcher.isWhitelisted(parseURL("https://example.com/bar"),
350 RegExpFilter.typeMap.IMAGE));
351 assert.ok(!matcher.isWhitelisted(parseURL("https://example.com/foo"),
352 RegExpFilter.typeMap.SUBDOCUMENT));
353 });
354
355 it("Add remove by keyword", () =>
356 {
357 let matcher = new CombinedMatcher();
358
359 matcher.add(Filter.fromText("||example.com/foo/bar/ad.jpg^"));
360
361 // Add the same filter a second time to make sure it doesn't get added again
362 // by a different keyword.
363 matcher.add(Filter.fromText("||example.com/foo/bar/ad.jpg^"));
364
365 assert.ok(!!matcher.matchesAny(parseURL("https://example.com/foo/bar/ad.jpg" ),
366 RegExpFilter.typeMap.IMAGE));
367
368 matcher.remove(Filter.fromText("||example.com/foo/bar/ad.jpg^"));
369
370 // Make sure the filter got removed so there is no match.
371 assert.ok(!matcher.matchesAny(parseURL("https://example.com/foo/bar/ad.jpg") ,
372 RegExpFilter.typeMap.IMAGE));
373
374
375 // Map { "example" => { text: "||example.com^$~third-party" } }
376 matcher.add(Filter.fromText("||example.com^$~third-party"));
377
378 assert.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1);
379
380 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword)
381 {
382 assert.equal(key, "example");
383 assert.deepEqual(value, Filter.fromText("||example.com^$~third-party"));
384 break;
385 }
386
387 assert.ok(!!matcher.matchesAny(parseURL("https://example.com/example/ad.jpg" ),
388 RegExpFilter.typeMap.IMAGE, "example.com"));
389
390 // Map {
391 // "example" => Map {
392 // "" => Map {
393 // { text: "||example.com^$~third-party" } => true,
394 // { text: "/example/*$~third-party" } => true
395 // }
396 // }
397 // }
398 matcher.add(Filter.fromText("/example/*$~third-party"));
399
400 assert.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1);
401
402 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword)
403 {
404 assert.equal(key, "example");
405 assert.equal(value.size, 1);
406
407 let map = value.get("");
408 assert.equal(map.size, 2);
409 assert.equal(map.get(Filter.fromText("||example.com^$~third-party")), true );
410 assert.equal(map.get(Filter.fromText("/example/*$~third-party")), true);
411
412 break;
413 }
414
415 assert.ok(!!matcher.matchesAny(parseURL("https://example.com/example/ad.jpg" ),
416 RegExpFilter.typeMap.IMAGE, "example.com"));
417
418 // Map { "example" => { text: "/example/*$~third-party" } }
419 matcher.remove(Filter.fromText("||example.com^$~third-party"));
420
421 assert.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 1);
422
423 for (let [key, value] of matcher._blacklist._filterDomainMapsByKeyword)
424 {
425 assert.equal(key, "example");
426 assert.deepEqual(value, Filter.fromText("/example/*$~third-party"));
427 break;
428 }
429
430 assert.ok(!!matcher.matchesAny(parseURL("https://example.com/example/ad.jpg" ),
431 RegExpFilter.typeMap.IMAGE, "example.com"));
432
433 // Map {}
434 matcher.remove(Filter.fromText("/example/*$~third-party"));
435
436 assert.equal(matcher._blacklist._filterDomainMapsByKeyword.size, 0);
437
438 assert.ok(!matcher.matchesAny(parseURL("https://example.com/example/ad.jpg") ,
439 RegExpFilter.typeMap.IMAGE, "example.com"));
440 });
441 });
OLDNEW
« no previous file with comments | « test/filterStorage_readwrite.js ('k') | test/notification.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld