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

Side by Side Diff: test/matcher.js

Issue 29354864: Issue 4223 - Migrate some more of adblockplustests (Closed)
Patch Set: Addressed final nit Created Oct. 4, 2016, 12: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.js ('k') | test/regexpFilters_matching.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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 let {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 = new Matcher();
44 let result = [];
45 for (let dummy of expected)
46 {
47 let keyword = matcher.findKeyword(filter);
48 result.push(keyword);
49 if (keyword)
50 {
51 let dummyFilter = Filter.fromText('^' + keyword + '^');
52 dummyFilter.filterCount = Infinity;
53 matcher.add(dummyFilter);
54 }
55 }
56
57 test.equal(result.join(", "), expected.join(", "), "Keyword candidates for " + filter.text);
58 }
59 }
60
61 function checkMatch(test, filters, location, contentType, docDomain, thirdParty, sitekey, specificOnly, expected)
62 {
63 let matcher = new Matcher();
64 for (let filter of filters)
65 matcher.add(Filter.fromText(filter));
66
67 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d ocDomain, thirdParty, sitekey, specificOnly);
68 if (result)
69 result = result.text;
70
71 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"));
72
73 let combinedMatcher = new CombinedMatcher();
74 for (let i = 0; i < 2; i++)
75 {
76 for (let filter of filters)
77 combinedMatcher.add(Filter.fromText(filter));
78
79 let result = combinedMatcher.matchesAny(location, RegExpFilter.typeMap[conte ntType], docDomain, thirdParty, sitekey, specificOnly);
80 if (result)
81 result = result.text;
82
83 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"));
84
85 // Generic whitelisting rules can match for specificOnly searches, so we
86 // can't easily know which rule will match for these whitelisting tests
87 if (specificOnly)
88 continue;
89
90 // For next run: add whitelisting filters for filters that aren't already
91 filters = filters.map((text) => text.substr(0, 2) == "@@" ? text : "@@" + te xt);
92 if (expected && expected.substr(0, 2) != "@@")
93 expected = "@@" + expected;
94 }
95 }
96
97 function cacheCheck(test, matcher, location, contentType, docDomain, thirdParty, expected)
98 {
99 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d ocDomain, thirdParty);
100 if (result)
101 result = result.text;
102
103 test.equal(result, expected, "match(" + location + ", " + contentType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with stati c filters");
104 }
105
106 exports.testMatcherClassDefinitions = function(test)
107 {
108 test.equal(typeof Matcher, "function", "typeof Matcher");
109 test.equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher");
110 test.equal(typeof defaultMatcher, "object", "typeof defaultMatcher");
111 test.ok(defaultMatcher instanceof CombinedMatcher, "defaultMatcher is a Combin edMatcher instance");
112
113 test.done();
114 };
115
116 exports.testKeywordExtraction = function(test)
117 {
118 compareKeywords(test, "*", []);
119 compareKeywords(test, "asdf", []);
120 compareKeywords(test, "/asdf/", []);
121 compareKeywords(test, "/asdf1234", []);
122 compareKeywords(test, "/asdf/1234", ["asdf"]);
123 compareKeywords(test, "/asdf/1234^", ["asdf", "1234"]);
124 compareKeywords(test, "/asdf/123456^", ["123456", "asdf"]);
125 compareKeywords(test, "^asdf^1234^56as^", ["asdf", "1234", "56as"]);
126 compareKeywords(test, "*asdf/1234^", ["1234"]);
127 compareKeywords(test, "|asdf,1234*", ["asdf"]);
128 compareKeywords(test, "||domain.example^", ["example", "domain"]);
129 compareKeywords(test, "&asdf=1234|", ["asdf", "1234"]);
130 compareKeywords(test, "^foo%2Ebar^", ["foo%2ebar"]);
131 compareKeywords(test, "^aSdF^1234", ["asdf"]);
132 compareKeywords(test, "_asdf_1234_", ["asdf", "1234"]);
133 compareKeywords(test, "+asdf-1234=", ["asdf", "1234"]);
134 compareKeywords(test, "/123^ad2&ad&", ["123", "ad2"]);
135 compareKeywords(test, "/123^ad2&ad$script,domain=example.com", ["123", "ad2"]) ;
136
137 test.done();
138 };
139
140 exports.testFilterMatching = function(test)
141 {
142 checkMatch(test, [], "http://abc/def", "IMAGE", null, false, null, false, null );
143 checkMatch(test, ["abc"], "http://abc/def", "IMAGE", null, false, null, false, "abc");
144 checkMatch(test, ["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null, false, "abc");
145 checkMatch(test, ["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null, false, "abc");
146 checkMatch(test, ["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null, false, null);
147 checkMatch(test, ["abc", "://abc/d"], "http://abc/def", "IMAGE", null, false, null, false, "://abc/d");
148 checkMatch(test, ["://abc/d", "abc"], "http://abc/def", "IMAGE", null, false, null, false, "://abc/d");
149 checkMatch(test, ["|http://"], "http://abc/def", "IMAGE", null, false, null, f alse, "|http://");
150 checkMatch(test, ["|http://abc"], "http://abc/def", "IMAGE", null, false, null , false, "|http://abc");
151 checkMatch(test, ["|abc"], "http://abc/def", "IMAGE", null, false, null, false , null);
152 checkMatch(test, ["|/abc/def"], "http://abc/def", "IMAGE", null, false, null, false, null);
153 checkMatch(test, ["/def|"], "http://abc/def", "IMAGE", null, false, null, fals e, "/def|");
154 checkMatch(test, ["/abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, "/abc/def|");
155 checkMatch(test, ["/abc/|"], "http://abc/def", "IMAGE", null, false, null, fal se, null);
156 checkMatch(test, ["http://abc/|"], "http://abc/def", "IMAGE", null, false, nul l, false, null);
157 checkMatch(test, ["|http://abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, "|http://abc/def|");
158 checkMatch(test, ["|/abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, null);
159 checkMatch(test, ["|http://abc/|"], "http://abc/def", "IMAGE", null, false, nu ll, false, null);
160 checkMatch(test, ["|/abc/|"], "http://abc/def", "IMAGE", null, false, null, fa lse, null);
161 checkMatch(test, ["||example.com/abc"], "http://example.com/abc/def", "IMAGE", null, false, null, false, "||example.com/abc");
162 checkMatch(test, ["||com/abc/def"], "http://example.com/abc/def", "IMAGE", nul l, false, null, false, "||com/abc/def");
163 checkMatch(test, ["||com/abc"], "http://example.com/abc/def", "IMAGE", null, f alse, null, false, "||com/abc");
164 checkMatch(test, ["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, null);
165 checkMatch(test, ["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, null);
166 checkMatch(test, ["||http://example.com/"], "http://example.com/abc/def", "IMA GE", null, false, null, false, null);
167 checkMatch(test, ["||example.com/abc/def|"], "http://example.com/abc/def", "IM AGE", null, false, null, false, "||example.com/abc/def|");
168 checkMatch(test, ["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, "||com/abc/def|");
169 checkMatch(test, ["||example.com/abc|"], "http://example.com/abc/def", "IMAGE" , null, false, null, false, null);
170 checkMatch(test, ["abc", "://abc/d", "asdf1234"], "http://abc/def", "IMAGE", n ull, false, null, false, "://abc/d");
171 checkMatch(test, ["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "h ttp://abc/def", "IMAGE", null, false, null, false, "://abc/de");
172 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d ef", "IMAGE", null, false, null, false, "abc$~third-party");
173 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d ef", "IMAGE", null, true, null, false, "abc$third-party");
174 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");
175 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" );
176 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def"], "http:/ /abc/def", "IMAGE", null, true, null, false, "//abc/def");
177 checkMatch(test, ["//abc/def", "abc$third-party", "abc$~third-party"], "http:/ /abc/def", "IMAGE", null, true, null, false, "//abc/def");
178 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" );
179 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part y"], "http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party");
180 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$~third-par ty"], "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party");
181 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "IMAGE", null, false, null, false, "abc$image");
182 checkMatch(test, ["abc$image", "abc$script", "abc$~script"], "http://abc/def", "SCRIPT", null, false, null, false, "abc$script");
183 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "OTHER", null, false, null, false, "abc$~image");
184 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$image");
185 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~script"], "http://abc/def", "SCRIPT", null, false, null, false, "//abc/def$script");
186 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "OTHER", null, false, null, false, "//abc/def$~image");
187 checkMatch(test, ["abc$image", "abc$~image", "//abc/def"], "http://abc/def", " IMAGE", null, false, null, false, "//abc/def");
188 checkMatch(test, ["//abc/def", "abc$image", "abc$~image"], "http://abc/def", " IMAGE", null, false, null, false, "//abc/def");
189 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$image"], "http://abc/d ef", "IMAGE", null, false, null, false, "//abc/def$image");
190 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$script"], "http://abc/ def", "IMAGE", null, false, null, false, "abc$image");
191 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");
192 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo .com|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, "abc$ domain=bar.com");
193 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");
194 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");
195 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) ;
196 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) ;
197 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");
198 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey =foo-publickey");
199 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey =bar-publickey");
200 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null);
201 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "baz.com", false, null, false, null);
202 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");
203 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);
204 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);
205 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");
206 checkMatch(test, ["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE ", "foo.com", false, null, false, "@@foo.com$generichide");
207 checkMatch(test, ["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLO CK", "foo.com", false, null, false, "@@foo.com$genericblock");
208 checkMatch(test, ["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE ", "foo.com", false, null, false, null);
209 checkMatch(test, ["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLO CK", "foo.com", false, null, false, null);
210 checkMatch(test, ["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, nu ll, true, null);
211 checkMatch(test, ["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo. com", false, null, true, "/bar$domain=foo.com");
212
213 test.done();
214 };
215
216 exports.testResultCacheChecks = function(test)
217 {
218 let matcher = new CombinedMatcher();
219 matcher.add(Filter.fromText("abc$image"));
220 matcher.add(Filter.fromText("abc$script"));
221 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping"));
222 matcher.add(Filter.fromText("cba$third-party"));
223 matcher.add(Filter.fromText("cba$~third-party,~script"));
224 matcher.add(Filter.fromText("http://def$image"));
225 matcher.add(Filter.fromText("http://def$script"));
226 matcher.add(Filter.fromText("http://def$~image,~script,~media,~ping"));
227 matcher.add(Filter.fromText("http://fed$third-party"));
228 matcher.add(Filter.fromText("http://fed$~third-party,~script"));
229
230 cacheCheck(test, matcher, "http://abc", "IMAGE", null, false, "abc$image");
231 cacheCheck(test, matcher, "http://abc", "SCRIPT", null, false, "abc$script");
232 cacheCheck(test, matcher, "http://abc", "OTHER", null, false, "abc$~image,~scr ipt,~media,~ping");
233 cacheCheck(test, matcher, "http://cba", "IMAGE", null, false, "cba$~third-part y,~script");
234 cacheCheck(test, matcher, "http://cba", "IMAGE", null, true, "cba$third-party" );
235 cacheCheck(test, matcher, "http://def", "IMAGE", null, false, "http://def$imag e");
236 cacheCheck(test, matcher, "http://def", "SCRIPT", null, false, "http://def$scr ipt");
237 cacheCheck(test, matcher, "http://def", "OTHER", null, false, "http://def$~ima ge,~script,~media,~ping");
238 cacheCheck(test, matcher, "http://fed", "IMAGE", null, false, "http://fed$~thi rd-party,~script");
239 cacheCheck(test, matcher, "http://fed", "IMAGE", null, true, "http://fed$third -party");
240 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, false, "cba$~third- party,~script");
241 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, true, "cba$third-pa rty");
242 cacheCheck(test, matcher, "http://abc_cba", "SCRIPT", null, false, "abc$script ");
243 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, false, "http ://fed$~third-party,~script");
244 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, true, "http: //fed$third-party");
245 cacheCheck(test, matcher, "http://def?http://fed", "SCRIPT", null, false, "htt p://def$script");
246
247 test.done();
248 };
OLDNEW
« no previous file with comments | « test/filterStorage.js ('k') | test/regexpFilters_matching.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld