OLD | NEW |
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 /* global assert */ |
| 21 |
20 const library = require("../../lib/content/snippets.js"); | 22 const library = require("../../lib/content/snippets.js"); |
21 const {timeout} = require("./_utils"); | 23 const {timeout} = require("./_utils"); |
22 | 24 |
23 // We need this stub for the injector. | 25 describe("Snippets", () => |
24 window.browser = { | 26 { |
25 runtime: { | 27 before(() => |
26 getURL: () => "" | 28 { |
| 29 // We need this stub for the injector. |
| 30 window.browser = { |
| 31 runtime: { |
| 32 getURL: () => "" |
| 33 } |
| 34 }; |
| 35 }); |
| 36 |
| 37 async function runSnippet(snippetName, ...args) |
| 38 { |
| 39 let snippet = library[snippetName]; |
| 40 |
| 41 assert.ok(snippet); |
| 42 |
| 43 snippet(...args); |
| 44 |
| 45 // For snippets that run in the context of the document via a <script> |
| 46 // element (i.e. snippets that use makeInjector()), we need to wait for |
| 47 // execution to be complete. |
| 48 await timeout(100); |
27 } | 49 } |
28 }; | 50 |
29 | |
30 async function runSnippet(test, snippetName, ...args) | |
31 { | |
32 let snippet = library[snippetName]; | |
33 | |
34 test.ok(snippet); | |
35 | |
36 snippet(...args); | |
37 | |
38 // For snippets that run in the context of the document via a <script> | |
39 // element (i.e. snippets that use makeInjector()), we need to wait for | |
40 // execution to be complete. | |
41 await timeout(100); | |
42 } | |
43 | |
44 exports.testAbortOnPropertyReadSnippet = async function(test) | |
45 { | |
46 function testProperty(property, result = true, errorName = "ReferenceError") | 51 function testProperty(property, result = true, errorName = "ReferenceError") |
47 { | 52 { |
48 let path = property.split("."); | 53 let path = property.split("."); |
49 | 54 |
50 let exceptionCaught = false; | 55 let exceptionCaught = false; |
51 let value = 1; | 56 let value = 1; |
52 | 57 |
53 try | 58 try |
54 { | 59 { |
55 let obj = window; | 60 let obj = window; |
56 while (path.length > 1) | 61 while (path.length > 1) |
57 obj = obj[path.shift()]; | 62 obj = obj[path.shift()]; |
58 value = obj[path.shift()]; | 63 value = obj[path.shift()]; |
59 } | 64 } |
60 catch (e) | 65 catch (e) |
61 { | 66 { |
62 test.equal(e.name, errorName); | 67 assert.equal(e.name, errorName); |
63 exceptionCaught = true; | 68 exceptionCaught = true; |
64 } | 69 } |
65 | 70 |
66 test.equal( | 71 assert.equal( |
67 exceptionCaught, | 72 exceptionCaught, |
68 result, | 73 result, |
69 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an
exception.` | 74 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an
exception.` |
70 ); | 75 ); |
71 test.equal( | 76 assert.equal( |
72 value, | 77 value, |
73 result ? 1 : undefined, | 78 result ? 1 : undefined, |
74 `The value for "${property}" ${result ? "shouldn't" : "should"} have been
read.` | 79 `The value for "${property}" ${result ? "shouldn't" : "should"} have been
read.` |
75 ); | 80 ); |
76 } | 81 } |
77 | 82 |
78 window.abpTest = "fortytwo"; | 83 it("abort-property-read", async() => |
79 await runSnippet(test, "abort-on-property-read", "abpTest"); | 84 { |
80 testProperty("abpTest"); | 85 window.abpTest = "fortytwo"; |
81 | 86 await runSnippet("abort-on-property-read", "abpTest"); |
82 window.abpTest2 = {prop1: "fortytwo"}; | 87 testProperty("abpTest"); |
83 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | 88 |
84 testProperty("abpTest2.prop1"); | 89 window.abpTest2 = {prop1: "fortytwo"}; |
85 | 90 await runSnippet("abort-on-property-read", "abpTest2.prop1"); |
86 // Test that we try to catch a property that doesn't exist yet. | 91 testProperty("abpTest2.prop1"); |
87 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | 92 |
88 window.abpTest3 = {prop1: "fortytwo"}; | 93 // Test that we try to catch a property that doesn't exist yet. |
89 testProperty("abpTest3.prop1"); | 94 await runSnippet("abort-on-property-read", "abpTest3.prop1"); |
90 | 95 window.abpTest3 = {prop1: "fortytwo"}; |
91 // Test that other properties don't trigger. | 96 testProperty("abpTest3.prop1"); |
92 testProperty("abpTest3.prop2", false); | 97 |
93 | 98 // Test that other properties don't trigger. |
94 // Test overwriting the object with another object. | 99 testProperty("abpTest3.prop2", false); |
95 window.abpTest4 = {prop3: {}}; | 100 |
96 await runSnippet(test, "abort-on-property-read", "abpTest4.prop3.foo"); | 101 // Test overwriting the object with another object. |
97 testProperty("abpTest4.prop3.foo"); | 102 window.abpTest4 = {prop3: {}}; |
98 window.abpTest4.prop3 = {}; | 103 await runSnippet("abort-on-property-read", "abpTest4.prop3.foo"); |
99 testProperty("abpTest4.prop3.foo"); | 104 testProperty("abpTest4.prop3.foo"); |
100 | 105 window.abpTest4.prop3 = {}; |
101 // Test if we start with a non-object. | 106 testProperty("abpTest4.prop3.foo"); |
102 window.abpTest5 = 42; | 107 |
103 await runSnippet(test, "abort-on-property-read", "abpTest5.prop4.bar"); | 108 // Test if we start with a non-object. |
104 | 109 window.abpTest5 = 42; |
105 testProperty("abpTest5.prop4.bar", true, "TypeError"); | 110 await runSnippet("abort-on-property-read", "abpTest5.prop4.bar"); |
106 | 111 |
107 window.abpTest5 = {prop4: 42}; | 112 testProperty("abpTest5.prop4.bar", true, "TypeError"); |
108 testProperty("abpTest5.prop4.bar", false); | 113 |
109 window.abpTest5 = {prop4: {}}; | 114 window.abpTest5 = {prop4: 42}; |
110 testProperty("abpTest5.prop4.bar"); | 115 testProperty("abpTest5.prop4.bar", false); |
111 | 116 window.abpTest5 = {prop4: {}}; |
112 // Check that it works on properties that are functions. | 117 testProperty("abpTest5.prop4.bar"); |
113 // https://issues.adblockplus.org/ticket/7419 | 118 |
114 | 119 // Check that it works on properties that are functions. |
115 // Existing function (from the API). | 120 // https://issues.adblockplus.org/ticket/7419 |
116 await runSnippet(test, "abort-on-property-read", "Object.keys"); | 121 |
117 testProperty("Object.keys"); | 122 // Existing function (from the API). |
118 | 123 await runSnippet("abort-on-property-read", "Object.keys"); |
119 // Function properties. | 124 testProperty("Object.keys"); |
120 window.abpTest6 = function() {}; | 125 |
121 window.abpTest6.prop1 = function() {}; | 126 // Function properties. |
122 await runSnippet(test, "abort-on-property-read", "abpTest6.prop1"); | 127 window.abpTest6 = function() {}; |
123 testProperty("abpTest6.prop1"); | 128 window.abpTest6.prop1 = function() {}; |
124 | 129 await runSnippet("abort-on-property-read", "abpTest6.prop1"); |
125 // Function properties, with sub-property set afterwards. | 130 testProperty("abpTest6.prop1"); |
126 window.abpTest7 = function() {}; | 131 |
127 await runSnippet(test, "abort-on-property-read", "abpTest7.prop1"); | 132 // Function properties, with sub-property set afterwards. |
128 window.abpTest7.prop1 = function() {}; | 133 window.abpTest7 = function() {}; |
129 testProperty("abpTest7.prop1"); | 134 await runSnippet("abort-on-property-read", "abpTest7.prop1"); |
130 | 135 window.abpTest7.prop1 = function() {}; |
131 // Function properties, with base property as function set afterwards. | 136 testProperty("abpTest7.prop1"); |
132 await runSnippet(test, "abort-on-property-read", "abpTest8.prop1"); | 137 |
133 window.abpTest8 = function() {}; | 138 // Function properties, with base property as function set afterwards. |
134 window.abpTest8.prop1 = function() {}; | 139 await runSnippet("abort-on-property-read", "abpTest8.prop1"); |
135 testProperty("abpTest8.prop1"); | 140 window.abpTest8 = function() {}; |
136 | 141 window.abpTest8.prop1 = function() {}; |
137 // Arrow function properties. | 142 testProperty("abpTest8.prop1"); |
138 window.abpTest9 = () => {}; | 143 |
139 await runSnippet(test, "abort-on-property-read", "abpTest9"); | 144 // Arrow function properties. |
140 testProperty("abpTest9"); | 145 window.abpTest9 = () => {}; |
141 | 146 await runSnippet("abort-on-property-read", "abpTest9"); |
142 // Class function properties. | 147 testProperty("abpTest9"); |
143 window.abpTest10 = class {}; | 148 |
144 await runSnippet(test, "abort-on-property-read", "abpTest10"); | 149 // Class function properties. |
145 testProperty("abpTest10"); | 150 window.abpTest10 = class {}; |
146 | 151 await runSnippet("abort-on-property-read", "abpTest10"); |
147 // Class function properties with prototype function properties. | 152 testProperty("abpTest10"); |
148 window.abpTest11 = class {}; | 153 |
149 window.abpTest11.prototype.prop1 = function() {}; | 154 // Class function properties with prototype function properties. |
150 await runSnippet(test, "abort-on-property-read", "abpTest11.prototype.prop1"); | 155 window.abpTest11 = class {}; |
151 testProperty("abpTest11.prototype.prop1"); | 156 window.abpTest11.prototype.prop1 = function() {}; |
152 | 157 await runSnippet("abort-on-property-read", "abpTest11.prototype.prop1"); |
153 // Class function properties with prototype function properties, with | 158 testProperty("abpTest11.prototype.prop1"); |
154 // prototype property set afterwards. | 159 |
155 window.abpTest12 = class {}; | 160 // Class function properties with prototype function properties, with |
156 await runSnippet(test, "abort-on-property-read", "abpTest12.prototype.prop1"); | 161 // prototype property set afterwards. |
157 window.abpTest12.prototype.prop1 = function() {}; | 162 window.abpTest12 = class {}; |
158 testProperty("abpTest12.prototype.prop1"); | 163 await runSnippet("abort-on-property-read", "abpTest12.prototype.prop1"); |
159 | 164 window.abpTest12.prototype.prop1 = function() {}; |
160 test.done(); | 165 testProperty("abpTest12.prototype.prop1"); |
161 }; | 166 }); |
162 | 167 |
163 exports.testAbortCurrentInlineScriptSnippet = async function(test) | 168 it("abort-curent-inline-script", async() => |
164 { | 169 { |
165 function injectInlineScript(doc, script) | 170 function injectInlineScript(doc, script) |
166 { | 171 { |
167 let scriptElement = doc.createElement("script"); | 172 let scriptElement = doc.createElement("script"); |
168 scriptElement.type = "application/javascript"; | 173 scriptElement.type = "application/javascript"; |
169 scriptElement.async = false; | 174 scriptElement.async = false; |
170 scriptElement.textContent = script; | 175 scriptElement.textContent = script; |
171 doc.body.appendChild(scriptElement); | 176 doc.body.appendChild(scriptElement); |
172 } | 177 } |
173 | 178 |
174 await runSnippet( | 179 await runSnippet( |
175 test, "abort-current-inline-script", "document.write", "atob" | 180 "abort-current-inline-script", "document.write", "atob" |
176 ); | 181 ); |
177 await runSnippet( | 182 await runSnippet( |
178 test, "abort-current-inline-script", "document.write", "btoa" | 183 "abort-current-inline-script", "document.write", "btoa" |
179 ); | 184 ); |
180 | 185 |
181 document.body.innerHTML = "<p id=\"result1\"></p><p id=\"message1\"></p><p id=
\"result2\"></p><p id=\"message2\"></p>"; | 186 document.body.innerHTML = "<p id=\"result1\"></p><p id=\"message1\"></p><p i
d=\"result2\"></p><p id=\"message2\"></p>"; |
182 | 187 |
183 let script = ` | 188 let script = ` |
184 try | 189 try |
185 { | 190 { |
186 let element = document.getElementById("result1"); | 191 let element = document.getElementById("result1"); |
187 document.write("<p>atob: " + atob("dGhpcyBpcyBhIGJ1Zw==") + "</p>"); | 192 document.write("<p>atob: " + atob("dGhpcyBpcyBhIGJ1Zw==") + "</p>"); |
188 element.textContent = atob("dGhpcyBpcyBhIGJ1Zw=="); | 193 element.textContent = atob("dGhpcyBpcyBhIGJ1Zw=="); |
189 } | 194 } |
190 catch (e) | 195 catch (e) |
191 { | 196 { |
192 let msg = document.getElementById("message1"); | 197 let msg = document.getElementById("message1"); |
193 msg.textContent = e.name; | 198 msg.textContent = e.name; |
194 }`; | 199 }`; |
195 | 200 |
196 injectInlineScript(document, script); | 201 injectInlineScript(document, script); |
197 | 202 |
198 let element = document.getElementById("result1"); | 203 let element = document.getElementById("result1"); |
199 test.ok(element, "Element 'result1' was not found"); | 204 assert.ok(element, "Element 'result1' was not found"); |
200 | 205 |
201 let msg = document.getElementById("message1"); | 206 let msg = document.getElementById("message1"); |
202 test.ok(msg, "Element 'message1' was not found"); | 207 assert.ok(msg, "Element 'message1' was not found"); |
203 | 208 |
204 if (element && msg) | 209 if (element && msg) |
205 { | 210 { |
206 test.equals(element.textContent, "", "Result element should be empty"); | 211 assert.equals(element.textContent, "", "Result element should be empty"); |
207 test.equals(msg.textContent, "ReferenceError", | 212 assert.equals(msg.textContent, "ReferenceError", |
208 "There should have been an error"); | 213 "There should have been an error"); |
209 } | 214 } |
210 | 215 |
211 script = ` | 216 script = ` |
212 try | 217 try |
213 { | 218 { |
214 let element = document.getElementById("result2"); | 219 let element = document.getElementById("result2"); |
215 document.write("<p>btoa: " + btoa("this is a bug") + "</p>"); | 220 document.write("<p>btoa: " + btoa("this is a bug") + "</p>"); |
216 element.textContent = btoa("this is a bug"); | 221 element.textContent = btoa("this is a bug"); |
217 } | 222 } |
218 catch (e) | 223 catch (e) |
219 { | 224 { |
220 let msg = document.getElementById("message2"); | 225 let msg = document.getElementById("message2"); |
221 msg.textContent = e.name; | 226 msg.textContent = e.name; |
222 }`; | 227 }`; |
223 | 228 |
224 injectInlineScript(document, script); | 229 injectInlineScript(document, script); |
225 | 230 |
226 element = document.getElementById("result2"); | 231 element = document.getElementById("result2"); |
227 test.ok(element, "Element 'result2' was not found"); | 232 assert.ok(element, "Element 'result2' was not found"); |
228 | 233 |
229 msg = document.getElementById("message2"); | 234 msg = document.getElementById("message2"); |
230 test.ok(msg, "Element 'message2' was not found"); | 235 assert.ok(msg, "Element 'message2' was not found"); |
231 | 236 |
232 if (element && msg) | 237 if (element && msg) |
233 { | 238 { |
234 test.equals(element.textContent, "", "Result element should be empty"); | 239 assert.equals(element.textContent, "", "Result element should be empty"); |
235 test.equals(msg.textContent, "ReferenceError", | 240 assert.equals(msg.textContent, "ReferenceError", |
236 "There should have been an error"); | 241 "There should have been an error"); |
237 } | 242 } |
238 | 243 }); |
239 test.done(); | 244 }); |
240 }; | |
OLD | NEW |