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 const {timeout} = require("./_utils"); |
22 | 22 |
23 // We need this stub for the injector. | 23 // We need this stub for the injector. |
24 window.browser = { | 24 window.browser = { |
25 runtime: | 25 runtime: { |
26 { | |
27 getURL: () => "" | 26 getURL: () => "" |
28 } | 27 } |
29 }; | |
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 }; | 28 }; |
50 | 29 |
51 async function runSnippet(test, snippetName, ...args) | 30 async function runSnippet(test, snippetName, ...args) |
52 { | 31 { |
53 let snippet = library[snippetName]; | 32 let snippet = library[snippetName]; |
54 | 33 |
55 test.ok(snippet); | 34 test.ok(snippet); |
56 | 35 |
57 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. |
58 await timeout(100); | 41 await timeout(100); |
59 } | 42 } |
60 | 43 |
61 exports.testAbortPropertyReadSnippet = async function(test) | 44 exports.testAbortOnPropertyReadSnippet = async function(test) |
62 { | 45 { |
63 window.abpTest = "foo"; | 46 function testProperty(property, result = true, errorName = "ReferenceError") |
64 | |
65 async function testProperty(property, result = true) | |
66 { | 47 { |
67 let properties = property.split("."); | 48 let path = property.split("."); |
68 test.ok(properties.length >= 1); | |
69 | 49 |
70 let exceptionCaught = false; | 50 let exceptionCaught = false; |
71 let value = 1; | 51 let value = 1; |
| 52 |
72 try | 53 try |
73 { | 54 { |
74 let obj = window; | 55 let obj = window; |
75 while (properties.length > 1) | 56 while (path.length > 1) |
76 obj = obj[properties.shift()]; | 57 obj = obj[path.shift()]; |
77 value = obj[properties.shift()]; | 58 value = obj[path.shift()]; |
78 } | 59 } |
79 catch (e) | 60 catch (e) |
80 { | 61 { |
81 if (properties.length == 0) | 62 test.equal(e.name, errorName); |
82 exceptionCaught = true; | 63 exceptionCaught = true; |
83 } | 64 } |
84 test.equal(exceptionCaught, result, `The property "${property}" didn't trigg
er and exception.`); | 65 |
85 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 ); |
86 } | 76 } |
87 | 77 |
| 78 window.abpTest = "fortytwo"; |
88 await runSnippet(test, "abort-on-property-read", "abpTest"); | 79 await runSnippet(test, "abort-on-property-read", "abpTest"); |
89 await testProperty("abpTest"); | 80 testProperty("abpTest"); |
90 | 81 |
91 window.abpTest2 = {prop1: "foo"}; | 82 window.abpTest2 = {prop1: "fortytwo"}; |
| 83 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); |
| 84 testProperty("abpTest2.prop1"); |
92 | 85 |
93 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | 86 // Test that we try to catch a property that doesn't exist yet. |
94 await testProperty("abpTest2.prop1"); | |
95 | |
96 // Test that we try to catch a property that don't exist yet. | |
97 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | 87 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); |
98 window.abpTest3 = {prop1: "foo"}; | 88 window.abpTest3 = {prop1: "fortytwo"}; |
99 await testProperty("abpTest3.prop1"); | 89 testProperty("abpTest3.prop1"); |
100 | 90 |
101 // Test that other properties don't trigger. | 91 // Test that other properties don't trigger. |
102 await testProperty("abpTest3.prop2", false); | 92 testProperty("abpTest3.prop2", false); |
103 | 93 |
104 // Test overwriting the object with another object | 94 // Test overwriting the object with another object. |
105 window.foo = {bar: {}}; | 95 window.abpTest4 = {prop3: {}}; |
106 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); | 96 await runSnippet(test, "abort-on-property-read", "abpTest4.prop3.foo"); |
107 await testProperty("foo.bar.lambda"); | 97 testProperty("abpTest4.prop3.foo"); |
108 window.foo.bar = {}; | 98 window.abpTest4.prop3 = {}; |
109 await testProperty("foo.bar.lambda"); | 99 testProperty("abpTest4.prop3.foo"); |
110 | 100 |
111 // Test if we start with a non-object | 101 // Test if we start with a non-object. |
112 window.foo2 = 5; | 102 window.abpTest5 = 42; |
113 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); | 103 await runSnippet(test, "abort-on-property-read", "abpTest5.prop4.bar"); |
114 await testProperty("foo2.bar2.lambda"); | 104 |
115 window.foo2 = {}; | 105 testProperty("abpTest5.prop4.bar", true, "TypeError"); |
116 await testProperty("foo2.bar2.lambda"); | 106 |
| 107 window.abpTest5 = {prop4: 42}; |
| 108 testProperty("abpTest5.prop4.bar", false); |
| 109 window.abpTest5 = {prop4: {}}; |
| 110 testProperty("abpTest5.prop4.bar"); |
117 | 111 |
118 test.done(); | 112 test.done(); |
119 }; | 113 }; |
120 | |
LEFT | RIGHT |