| OLD | NEW |
| (Empty) |
| 1 (function() | |
| 2 { | |
| 3 module("Filter matcher", {setup: prepareFilterComponents, teardown: restoreFil
terComponents}); | |
| 4 | |
| 5 function compareKeywords(text, expected) | |
| 6 { | |
| 7 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)]) | |
| 8 { | |
| 9 let matcher = new Matcher(); | |
| 10 let result = []; | |
| 11 for (let dummy of expected) | |
| 12 { | |
| 13 let keyword = matcher.findKeyword(filter); | |
| 14 result.push(keyword); | |
| 15 if (keyword) | |
| 16 { | |
| 17 let dummyFilter = Filter.fromText('^' + keyword + '^'); | |
| 18 dummyFilter.filterCount = Infinity; | |
| 19 matcher.add(dummyFilter); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 equal(result.join(", "), expected.join(", "), "Keyword candidates for " +
filter.text); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 function checkMatch(filters, location, contentType, docDomain, thirdParty, sit
ekey, specificOnly, expected) | |
| 28 { | |
| 29 let matcher = new Matcher(); | |
| 30 for (let filter of filters) | |
| 31 matcher.add(Filter.fromText(filter)); | |
| 32 | |
| 33 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType],
docDomain, thirdParty, sitekey, specificOnly); | |
| 34 if (result) | |
| 35 result = result.text; | |
| 36 | |
| 37 equal(result, expected, "match(" + location + ", " + contentType + ", " + do
cDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " + (sitekey
|| "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specificOnly") +
") with:\n" + filters.join("\n")); | |
| 38 | |
| 39 let combinedMatcher = new CombinedMatcher(); | |
| 40 for (let i = 0; i < 2; i++) | |
| 41 { | |
| 42 for (let filter of filters) | |
| 43 combinedMatcher.add(Filter.fromText(filter)); | |
| 44 | |
| 45 let result = combinedMatcher.matchesAny(location, RegExpFilter.typeMap[con
tentType], docDomain, thirdParty, sitekey, specificOnly); | |
| 46 if (result) | |
| 47 result = result.text; | |
| 48 | |
| 49 equal(result, expected, "combinedMatch(" + location + ", " + contentType +
", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " +
(sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specif
icOnly") + ") with:\n" + filters.join("\n")); | |
| 50 | |
| 51 // Generic whitelisting rules can match for specificOnly searches, so we | |
| 52 // can't easily know which rule will match for these whitelisting tests | |
| 53 if (specificOnly) | |
| 54 continue; | |
| 55 | |
| 56 // For next run: add whitelisting filters for filters that aren't already | |
| 57 filters = filters.map((text) => text.substr(0, 2) == "@@" ? text : "@@" +
text); | |
| 58 if (expected && expected.substr(0, 2) != "@@") | |
| 59 expected = "@@" + expected; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 function cacheCheck(matcher, location, contentType, docDomain, thirdParty, exp
ected) | |
| 64 { | |
| 65 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType],
docDomain, thirdParty); | |
| 66 if (result) | |
| 67 result = result.text; | |
| 68 | |
| 69 equal(result, expected, "match(" + location + ", " + contentType + ", " + do
cDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with static f
ilters"); | |
| 70 } | |
| 71 | |
| 72 test("Matcher class definitions", function() | |
| 73 { | |
| 74 equal(typeof Matcher, "function", "typeof Matcher"); | |
| 75 equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher"); | |
| 76 equal(typeof defaultMatcher, "object", "typeof defaultMatcher"); | |
| 77 ok(defaultMatcher instanceof CombinedMatcher, "defaultMatcher is a CombinedM
atcher instance"); | |
| 78 }); | |
| 79 | |
| 80 test("Keyword extraction", function() | |
| 81 { | |
| 82 compareKeywords("*", []); | |
| 83 compareKeywords("asdf", []); | |
| 84 compareKeywords("/asdf/", []); | |
| 85 compareKeywords("/asdf1234", []); | |
| 86 compareKeywords("/asdf/1234", ["asdf"]); | |
| 87 compareKeywords("/asdf/1234^", ["asdf", "1234"]); | |
| 88 compareKeywords("/asdf/123456^", ["123456", "asdf"]); | |
| 89 compareKeywords("^asdf^1234^56as^", ["asdf", "1234", "56as"]); | |
| 90 compareKeywords("*asdf/1234^", ["1234"]); | |
| 91 compareKeywords("|asdf,1234*", ["asdf"]); | |
| 92 compareKeywords("||domain.example^", ["example", "domain"]); | |
| 93 compareKeywords("&asdf=1234|", ["asdf", "1234"]); | |
| 94 compareKeywords("^foo%2Ebar^", ["foo%2ebar"]); | |
| 95 compareKeywords("^aSdF^1234", ["asdf"]); | |
| 96 compareKeywords("_asdf_1234_", ["asdf", "1234"]); | |
| 97 compareKeywords("+asdf-1234=", ["asdf", "1234"]); | |
| 98 compareKeywords("/123^ad2&ad&", ["123", "ad2"]); | |
| 99 compareKeywords("/123^ad2&ad$script,domain=example.com", ["123", "ad2"]); | |
| 100 }); | |
| 101 | |
| 102 test("Filter matching", function() | |
| 103 { | |
| 104 checkMatch([], "http://abc/def", "IMAGE", null, false, null, false, null); | |
| 105 checkMatch(["abc"], "http://abc/def", "IMAGE", null, false, null, false, "ab
c"); | |
| 106 checkMatch(["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null, fal
se, "abc"); | |
| 107 checkMatch(["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null, fal
se, "abc"); | |
| 108 checkMatch(["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null, fal
se, null); | |
| 109 checkMatch(["abc", "://abc/d"], "http://abc/def", "IMAGE", null, false, null
, false, "://abc/d"); | |
| 110 checkMatch(["://abc/d", "abc"], "http://abc/def", "IMAGE", null, false, null
, false, "://abc/d"); | |
| 111 checkMatch(["|http://"], "http://abc/def", "IMAGE", null, false, null, false
, "|http://"); | |
| 112 checkMatch(["|http://abc"], "http://abc/def", "IMAGE", null, false, null, fa
lse, "|http://abc"); | |
| 113 checkMatch(["|abc"], "http://abc/def", "IMAGE", null, false, null, false, nu
ll); | |
| 114 checkMatch(["|/abc/def"], "http://abc/def", "IMAGE", null, false, null, fals
e, null); | |
| 115 checkMatch(["/def|"], "http://abc/def", "IMAGE", null, false, null, false, "
/def|"); | |
| 116 checkMatch(["/abc/def|"], "http://abc/def", "IMAGE", null, false, null, fals
e, "/abc/def|"); | |
| 117 checkMatch(["/abc/|"], "http://abc/def", "IMAGE", null, false, null, false,
null); | |
| 118 checkMatch(["http://abc/|"], "http://abc/def", "IMAGE", null, false, null, f
alse, null); | |
| 119 checkMatch(["|http://abc/def|"], "http://abc/def", "IMAGE", null, false, nul
l, false, "|http://abc/def|"); | |
| 120 checkMatch(["|/abc/def|"], "http://abc/def", "IMAGE", null, false, null, fal
se, null); | |
| 121 checkMatch(["|http://abc/|"], "http://abc/def", "IMAGE", null, false, null,
false, null); | |
| 122 checkMatch(["|/abc/|"], "http://abc/def", "IMAGE", null, false, null, false,
null); | |
| 123 checkMatch(["||example.com/abc"], "http://example.com/abc/def", "IMAGE", nul
l, false, null, false, "||example.com/abc"); | |
| 124 checkMatch(["||com/abc/def"], "http://example.com/abc/def", "IMAGE", null, f
alse, null, false, "||com/abc/def"); | |
| 125 checkMatch(["||com/abc"], "http://example.com/abc/def", "IMAGE", null, false
, null, false, "||com/abc"); | |
| 126 checkMatch(["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", null,
false, null, false, null); | |
| 127 checkMatch(["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", null,
false, null, false, null); | |
| 128 checkMatch(["||http://example.com/"], "http://example.com/abc/def", "IMAGE",
null, false, null, false, null); | |
| 129 checkMatch(["||example.com/abc/def|"], "http://example.com/abc/def", "IMAGE"
, null, false, null, false, "||example.com/abc/def|"); | |
| 130 checkMatch(["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", null,
false, null, false, "||com/abc/def|"); | |
| 131 checkMatch(["||example.com/abc|"], "http://example.com/abc/def", "IMAGE", nu
ll, false, null, false, null); | |
| 132 checkMatch(["abc", "://abc/d", "asdf1234"], "http://abc/def", "IMAGE", null,
false, null, false, "://abc/d"); | |
| 133 checkMatch(["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "http:
//abc/def", "IMAGE", null, false, null, false, "://abc/de"); | |
| 134 checkMatch(["abc$third-party", "abc$~third-party", "ddd"], "http://abc/def",
"IMAGE", null, false, null, false, "abc$~third-party"); | |
| 135 checkMatch(["abc$third-party", "abc$~third-party", "ddd"], "http://abc/def",
"IMAGE", null, true, null, false, "abc$third-party"); | |
| 136 checkMatch(["//abc/def$third-party", "//abc/def$~third-party", "//abc_def"],
"http://abc/def", "IMAGE", null, false, null, false, "//abc/def$~third-party"); | |
| 137 checkMatch(["//abc/def$third-party", "//abc/def$~third-party", "//abc_def"],
"http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"); | |
| 138 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def"], "http://abc
/def", "IMAGE", null, true, null, false, "//abc/def"); | |
| 139 checkMatch(["//abc/def", "abc$third-party", "abc$~third-party"], "http://abc
/def", "IMAGE", null, true, null, false, "//abc/def"); | |
| 140 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$third-party"],
"http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"); | |
| 141 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$third-party"],
"http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party"); | |
| 142 checkMatch(["abc$third-party", "abc$~third-party", "//abc/def$~third-party"]
, "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party"); | |
| 143 checkMatch(["abc$image", "abc$script", "abc$~image"], "http://abc/def", "IMA
GE", null, false, null, false, "abc$image"); | |
| 144 checkMatch(["abc$image", "abc$script", "abc$~script"], "http://abc/def", "SC
RIPT", null, false, null, false, "abc$script"); | |
| 145 checkMatch(["abc$image", "abc$script", "abc$~image"], "http://abc/def", "OTH
ER", null, false, null, false, "abc$~image"); | |
| 146 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "htt
p://abc/def", "IMAGE", null, false, null, false, "//abc/def$image"); | |
| 147 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~script"], "ht
tp://abc/def", "SCRIPT", null, false, null, false, "//abc/def$script"); | |
| 148 checkMatch(["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "htt
p://abc/def", "OTHER", null, false, null, false, "//abc/def$~image"); | |
| 149 checkMatch(["abc$image", "abc$~image", "//abc/def"], "http://abc/def", "IMAG
E", null, false, null, false, "//abc/def"); | |
| 150 checkMatch(["//abc/def", "abc$image", "abc$~image"], "http://abc/def", "IMAG
E", null, false, null, false, "//abc/def"); | |
| 151 checkMatch(["abc$image", "abc$~image", "//abc/def$image"], "http://abc/def",
"IMAGE", null, false, null, false, "//abc/def$image"); | |
| 152 checkMatch(["abc$image", "abc$~image", "//abc/def$script"], "http://abc/def"
, "IMAGE", null, false, null, false, "abc$image"); | |
| 153 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$doma
in=foo.com"); | |
| 154 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, "abc$doma
in=bar.com"); | |
| 155 checkMatch(["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, "abc$doma
in=~foo.com|~bar.com"); | |
| 156 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$doma
in=foo.com"); | |
| 157 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, null); | |
| 158 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com
|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, null); | |
| 159 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com
|~bar.com"], "http://ccc/def", "IMAGE", "baz.com", false, null, false, "ccc$doma
in=~foo.com|~bar.com"); | |
| 160 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http
://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey=foo
-publickey"); | |
| 161 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http
://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey=bar
-publickey"); | |
| 162 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http
://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null); | |
| 163 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http
://abc/def", "IMAGE", "baz.com", false, null, false, null); | |
| 164 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub
lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "foo-publi
ckey", false, "abc$sitekey=foo-publickey,domain=foo.com"); | |
| 165 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub
lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "bar-publi
ckey", false, null); | |
| 166 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub
lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "foo-publi
ckey", false, null); | |
| 167 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub
lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "bar-publi
ckey", false, "abc$sitekey=bar-publickey,domain=bar.com"); | |
| 168 checkMatch(["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE", "
foo.com", false, null, false, "@@foo.com$generichide"); | |
| 169 checkMatch(["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK",
"foo.com", false, null, false, "@@foo.com$genericblock"); | |
| 170 checkMatch(["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE", "
foo.com", false, null, false, null); | |
| 171 checkMatch(["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK",
"foo.com", false, null, false, null); | |
| 172 checkMatch(["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, null,
true, null); | |
| 173 checkMatch(["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo.com"
, false, null, true, "/bar$domain=foo.com"); | |
| 174 }); | |
| 175 | |
| 176 test("Result cache checks", function() | |
| 177 { | |
| 178 let matcher = new CombinedMatcher(); | |
| 179 matcher.add(Filter.fromText("abc$image")); | |
| 180 matcher.add(Filter.fromText("abc$script")); | |
| 181 matcher.add(Filter.fromText("abc$~image,~script,~object,~ping")); | |
| 182 matcher.add(Filter.fromText("cba$third-party")); | |
| 183 matcher.add(Filter.fromText("cba$~third-party,~script")); | |
| 184 matcher.add(Filter.fromText("http://def$image")); | |
| 185 matcher.add(Filter.fromText("http://def$script")); | |
| 186 matcher.add(Filter.fromText("http://def$~image,~script,~object,~ping")); | |
| 187 matcher.add(Filter.fromText("http://fed$third-party")); | |
| 188 matcher.add(Filter.fromText("http://fed$~third-party,~script")); | |
| 189 | |
| 190 cacheCheck(matcher, "http://abc", "IMAGE", null, false, "abc$image"); | |
| 191 cacheCheck(matcher, "http://abc", "SCRIPT", null, false, "abc$script"); | |
| 192 cacheCheck(matcher, "http://abc", "OTHER", null, false, "abc$~image,~script,
~object,~ping"); | |
| 193 cacheCheck(matcher, "http://cba", "IMAGE", null, false, "cba$~third-party,~s
cript"); | |
| 194 cacheCheck(matcher, "http://cba", "IMAGE", null, true, "cba$third-party"); | |
| 195 cacheCheck(matcher, "http://def", "IMAGE", null, false, "http://def$image"); | |
| 196 cacheCheck(matcher, "http://def", "SCRIPT", null, false, "http://def$script"
); | |
| 197 cacheCheck(matcher, "http://def", "OTHER", null, false, "http://def$~image,~
script,~object,~ping"); | |
| 198 cacheCheck(matcher, "http://fed", "IMAGE", null, false, "http://fed$~third-p
arty,~script"); | |
| 199 cacheCheck(matcher, "http://fed", "IMAGE", null, true, "http://fed$third-par
ty"); | |
| 200 cacheCheck(matcher, "http://abc_cba", "OBJECT", null, false, "cba$~third-par
ty,~script"); | |
| 201 cacheCheck(matcher, "http://abc_cba", "OBJECT", null, true, "cba$third-party
"); | |
| 202 cacheCheck(matcher, "http://abc_cba", "SCRIPT", null, false, "abc$script"); | |
| 203 cacheCheck(matcher, "http://def?http://fed", "OBJECT", null, false, "http://
fed$~third-party,~script"); | |
| 204 cacheCheck(matcher, "http://def?http://fed", "OBJECT", null, true, "http://f
ed$third-party"); | |
| 205 cacheCheck(matcher, "http://def?http://fed", "SCRIPT", null, false, "http://
def$script"); | |
| 206 }); | |
| 207 })(); | |
| OLD | NEW |