Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
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"; | |
Wladimir Palant
2017/10/17 12:51:22
This test has been taken from the master branch an
| |
19 | |
20 const {createSandbox} = require("./_common"); | |
21 | |
22 let Filter = null; | |
23 let RegExpFilter = null; | |
24 let Matcher = null; | |
25 let defaultMatcher = null; | |
26 | |
27 exports.setUp = function(callback) | |
28 { | |
29 let sandboxedRequire = createSandbox(); | |
30 ( | |
31 {Filter, RegExpFilter} = sandboxedRequire("../lib/filterClasses"), | |
32 {defaultMatcher, Matcher} = sandboxedRequire("../lib/matcher") | |
sergei
2017/10/18 10:58:44
What about deletion of defaultMatcher in tearDown?
| |
33 ); | |
34 | |
35 callback(); | |
36 }; | |
37 | |
38 function checkMatch(test, filters, location, contentType, docDomain, thirdParty, sitekey, specificOnly, expected) | |
39 { | |
40 for (let prefix of ["", "@@"]) | |
41 { | |
42 // Generic whitelisting rules can match for specificOnly searches, so we | |
43 // can't easily know which rule will match for these whitelisting tests | |
44 if (prefix == "@@" && specificOnly) | |
45 continue; | |
46 | |
47 let matcher = Matcher.create(); | |
sergei
2017/10/18 10:58:43
matcher is not deleted.
| |
48 let actualFilters = []; | |
49 for (let text of filters) | |
50 { | |
51 if (prefix && !text.startsWith(prefix)) | |
52 text = prefix + text; | |
53 actualFilters.push(text); | |
54 | |
55 let filter = Filter.fromText(text); | |
56 matcher.add(filter); | |
57 filter.delete(); | |
58 } | |
59 | |
60 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], docDomain, thirdParty, sitekey, specificOnly); | |
61 if (result) | |
62 { | |
63 let tmp = result.text; | |
64 result.delete(); | |
65 result = tmp; | |
66 } | |
67 | |
68 let actualExpected = expected; | |
69 if (actualExpected && prefix && !expected.startsWith(prefix)) | |
70 actualExpected = prefix + actualExpected; | |
71 test.equal(result, actualExpected, | |
72 `match(${location}, ${contentType}, ${docDomain}, | |
73 ${thirdParty ? "third-party" : "first-party"}, | |
74 ${sitekey || "no-sitekey"}, | |
75 ${specificOnly ? "specificOnly" : "not-specificOnly"}) | |
76 with: ${actualFilters.join(" ")}`.replace(/\s+/g, " ")); | |
77 } | |
78 } | |
79 | |
80 exports.testMatcherClassDefinitions = function(test) | |
81 { | |
82 test.equal(typeof Matcher, "function", "typeof Matcher"); | |
83 test.equal(typeof defaultMatcher, "object", "typeof defaultMatcher"); | |
84 test.ok(defaultMatcher instanceof Matcher, "defaultMatcher is a Matcher instan ce"); | |
85 | |
86 test.done(); | |
87 }; | |
88 | |
89 exports.testFilterMatching = function(test) | |
90 { | |
91 checkMatch(test, [], "http://abc/def", "IMAGE", null, false, null, false, null ); | |
92 checkMatch(test, ["abc"], "http://abc/def", "IMAGE", null, false, null, false, "abc"); | |
93 checkMatch(test, ["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null, false, "abc"); | |
94 checkMatch(test, ["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null, false, "abc"); | |
95 checkMatch(test, ["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null, false, null); | |
96 checkMatch(test, ["://abc/d"], "http://abc/def", "IMAGE", null, false, null, f alse, "://abc/d"); | |
97 checkMatch(test, ["|http://"], "http://abc/def", "IMAGE", null, false, null, f alse, "|http://"); | |
98 checkMatch(test, ["|http://abc"], "http://abc/def", "IMAGE", null, false, null , false, "|http://abc"); | |
99 checkMatch(test, ["|abc"], "http://abc/def", "IMAGE", null, false, null, false , null); | |
100 checkMatch(test, ["|/abc/def"], "http://abc/def", "IMAGE", null, false, null, false, null); | |
101 checkMatch(test, ["/def|"], "http://abc/def", "IMAGE", null, false, null, fals e, "/def|"); | |
102 checkMatch(test, ["/abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, "/abc/def|"); | |
103 checkMatch(test, ["/abc/|"], "http://abc/def", "IMAGE", null, false, null, fal se, null); | |
104 checkMatch(test, ["http://abc/|"], "http://abc/def", "IMAGE", null, false, nul l, false, null); | |
105 checkMatch(test, ["|http://abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, "|http://abc/def|"); | |
106 checkMatch(test, ["|/abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, null); | |
107 checkMatch(test, ["|http://abc/|"], "http://abc/def", "IMAGE", null, false, nu ll, false, null); | |
108 checkMatch(test, ["|/abc/|"], "http://abc/def", "IMAGE", null, false, null, fa lse, null); | |
109 checkMatch(test, ["||example.com/abc"], "http://example.com/abc/def", "IMAGE", null, false, null, false, "||example.com/abc"); | |
110 checkMatch(test, ["||com/abc/def"], "http://example.com/abc/def", "IMAGE", nul l, false, null, false, "||com/abc/def"); | |
111 checkMatch(test, ["||com/abc"], "http://example.com/abc/def", "IMAGE", null, f alse, null, false, "||com/abc"); | |
112 checkMatch(test, ["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, null); | |
113 checkMatch(test, ["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, null); | |
114 checkMatch(test, ["||http://example.com/"], "http://example.com/abc/def", "IMA GE", null, false, null, false, null); | |
115 checkMatch(test, ["||example.com/abc/def|"], "http://example.com/abc/def", "IM AGE", null, false, null, false, "||example.com/abc/def|"); | |
116 checkMatch(test, ["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", nu ll, false, null, false, "||com/abc/def|"); | |
117 checkMatch(test, ["||example.com/abc|"], "http://example.com/abc/def", "IMAGE" , null, false, null, false, null); | |
118 checkMatch(test, ["://abc/d", "asdf1234"], "http://abc/def", "IMAGE", null, fa lse, null, false, "://abc/d"); | |
119 checkMatch(test, ["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "h ttp://abc/def", "IMAGE", null, false, null, false, "://abc/de"); | |
120 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d ef", "IMAGE", null, false, null, false, "abc$~third-party"); | |
121 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d ef", "IMAGE", null, true, null, false, "abc$third-party"); | |
122 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"); | |
123 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" ); | |
124 checkMatch(test, ["abc$~third-party", "//abc/def"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def"); | |
125 checkMatch(test, ["abc$~third-party", "//abc/def$third-party"], "http://abc/de f", "IMAGE", null, true, null, false, "//abc/def$third-party"); | |
126 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part y"], "http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party"); | |
127 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$~third-par ty"], "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party"); | |
128 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "IMAGE", null, false, null, false, "abc$image"); | |
129 checkMatch(test, ["abc$image", "abc$script", "abc$~script"], "http://abc/def", "SCRIPT", null, false, null, false, "abc$script"); | |
130 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "OTHER", null, false, null, false, "abc$~image"); | |
131 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$image"); | |
132 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~script"], "http://abc/def", "SCRIPT", null, false, null, false, "//abc/def$script"); | |
133 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "OTHER", null, false, null, false, "//abc/def$~image"); | |
134 checkMatch(test, ["abc$~image", "//abc/def"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def"); | |
135 checkMatch(test, ["abc$~image", "//abc/def$image"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$image"); | |
136 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$script"], "http://abc/ def", "IMAGE", null, false, null, false, "abc$image"); | |
137 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"); | |
Wladimir Palant
2017/10/17 12:51:22
This test and a few similar ones are currently fai
sergei
2017/11/20 16:30:49
I think it is because of mutable docDomain. docDom
| |
138 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"); | |
139 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"); | |
140 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"); | |
141 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) ; | |
142 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) ; | |
143 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"); | |
144 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey =foo-publickey"); | |
145 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey =bar-publickey"); | |
146 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null); | |
147 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], " http://abc/def", "IMAGE", "baz.com", false, null, false, null); | |
148 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"); | |
149 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); | |
150 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); | |
151 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"); | |
152 checkMatch(test, ["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE ", "foo.com", false, null, false, "@@foo.com$generichide"); | |
153 checkMatch(test, ["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLO CK", "foo.com", false, null, false, "@@foo.com$genericblock"); | |
154 checkMatch(test, ["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE ", "foo.com", false, null, false, null); | |
155 checkMatch(test, ["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLO CK", "foo.com", false, null, false, null); | |
156 checkMatch(test, ["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, nu ll, true, null); | |
157 checkMatch(test, ["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo. com", false, null, true, "/bar$domain=foo.com"); | |
158 | |
159 test.done(); | |
160 }; | |
OLD | NEW |