LEFT | RIGHT |
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 library = require("../../lib/content/snippets.js"); | 20 const library = require("../../lib/content/snippets.js"); |
| 21 const {timeout} = require("./_utils"); |
21 | 22 |
22 // We need this stub for the injector. | 23 // We need this stub for the injector. |
23 window.browser = | 24 window.browser = { |
24 { | 25 runtime: { |
25 runtime: | |
26 { | |
27 getURL: () => "" | 26 getURL: () => "" |
28 } | 27 } |
29 }; | 28 }; |
30 | |
31 let testDocument = null; | |
32 | |
33 exports.setUp = function(callback) | |
34 { | |
35 let iframe = document.createElement("iframe"); | |
36 document.body.appendChild(iframe); | |
37 testDocument = iframe.contentDocument; | |
38 | |
39 callback(); | |
40 }; | |
41 | |
42 exports.tearDown = function(callback) | |
43 { | |
44 let iframe = testDocument.defaultView.frameElement; | |
45 iframe.parentNode.removeChild(iframe); | |
46 testDocument = null; | |
47 | |
48 callback(); | |
49 }; | |
50 | |
51 function timeout(delay) | |
52 { | |
53 return new Promise((resolve, reject) => | |
54 { | |
55 window.setTimeout(resolve, delay); | |
56 }); | |
57 } | |
58 | 29 |
59 async function runSnippet(test, snippetName, ...args) | 30 async function runSnippet(test, snippetName, ...args) |
60 { | 31 { |
61 let snippet = library[snippetName]; | 32 let snippet = library[snippetName]; |
62 | 33 |
63 test.ok(snippet); | 34 test.ok(snippet); |
64 | 35 |
65 snippet(...args); | 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. |
66 await timeout(100); | 41 await timeout(100); |
67 } | 42 } |
68 | 43 |
69 exports.testSnippet = async function(test) | 44 exports.testAbortOnPropertyReadSnippet = async function(test) |
70 { | 45 { |
71 window.abpTest = "foo"; | 46 function testProperty(property, result = true, errorName = "ReferenceError") |
72 | |
73 async function testProperty(property, result = true) | |
74 { | 47 { |
75 let properties = property.split("."); | 48 let path = property.split("."); |
76 test.ok(properties.length >= 1); | |
77 | 49 |
78 let exceptionCaught = false; | 50 let exceptionCaught = false; |
79 let value = 1; | 51 let value = 1; |
| 52 |
80 try | 53 try |
81 { | 54 { |
82 let obj = window; | 55 let obj = window; |
83 while (properties.length > 1) | 56 while (path.length > 1) |
84 obj = obj[properties.shift()]; | 57 obj = obj[path.shift()]; |
85 value = obj[properties.shift()]; | 58 value = obj[path.shift()]; |
86 } | 59 } |
87 catch (e) | 60 catch (e) |
88 { | 61 { |
89 if (properties.length == 0) | 62 test.equal(e.name, errorName); |
90 exceptionCaught = true; | 63 exceptionCaught = true; |
91 } | 64 } |
92 test.equal(exceptionCaught, result, `The property "${property}" didn't trigg
er and exception.`); | 65 |
93 test.equal(value, result ? 1 : undefined, `The value for "${property}" shoul
dn't have been read.`); | 66 test.equal( |
| 67 exceptionCaught, |
| 68 result, |
| 69 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an
exception.` |
| 70 ); |
| 71 test.equal( |
| 72 value, |
| 73 result ? 1 : undefined, |
| 74 `The value for "${property}" ${result ? "shouldn't" : "should"} have been
read.` |
| 75 ); |
94 } | 76 } |
95 | 77 |
| 78 window.abpTest = "fortytwo"; |
96 await runSnippet(test, "abort-on-property-read", "abpTest"); | 79 await runSnippet(test, "abort-on-property-read", "abpTest"); |
97 await testProperty("abpTest"); | 80 testProperty("abpTest"); |
98 | 81 |
99 window.abpTest2 = {prop1: "foo"}; | 82 window.abpTest2 = {prop1: "fortytwo"}; |
| 83 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); |
| 84 testProperty("abpTest2.prop1"); |
100 | 85 |
101 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | 86 // Test that we try to catch a property that doesn't exist yet. |
102 await testProperty("abpTest2.prop1"); | |
103 | |
104 // Test that we try to catch a property that don't exist yet. | |
105 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | 87 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); |
106 window.abpTest3 = {prop1: "foo"}; | 88 window.abpTest3 = {prop1: "fortytwo"}; |
107 await testProperty("abpTest3.prop1"); | 89 testProperty("abpTest3.prop1"); |
108 | 90 |
109 // Test that other properties don't trigger. | 91 // Test that other properties don't trigger. |
110 await testProperty("abpTest3.prop2", false); | 92 testProperty("abpTest3.prop2", false); |
| 93 |
| 94 // Test overwriting the object with another object. |
| 95 window.abpTest4 = {prop3: {}}; |
| 96 await runSnippet(test, "abort-on-property-read", "abpTest4.prop3.foo"); |
| 97 testProperty("abpTest4.prop3.foo"); |
| 98 window.abpTest4.prop3 = {}; |
| 99 testProperty("abpTest4.prop3.foo"); |
| 100 |
| 101 // Test if we start with a non-object. |
| 102 window.abpTest5 = 42; |
| 103 await runSnippet(test, "abort-on-property-read", "abpTest5.prop4.bar"); |
| 104 |
| 105 testProperty("abpTest5.prop4.bar", true, "TypeError"); |
| 106 |
| 107 window.abpTest5 = {prop4: 42}; |
| 108 testProperty("abpTest5.prop4.bar", false); |
| 109 window.abpTest5 = {prop4: {}}; |
| 110 testProperty("abpTest5.prop4.bar"); |
111 | 111 |
112 test.done(); | 112 test.done(); |
113 }; | 113 }; |
LEFT | RIGHT |