| 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"; | |
| 19 | |
| 20 const library = require("../../lib/content/snippets.js"); | |
| 21 const {timeout} = require("./_utils"); | |
| 22 | |
| 23 // We need this stub for the injector. | |
| 24 window.browser = { | |
| 25 runtime: { | |
| 26 getURL: () => "" | |
| 27 } | |
| 28 }; | |
| 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") | |
| 47 { | |
| 48 let path = property.split("."); | |
| 49 | |
| 50 let exceptionCaught = false; | |
| 51 let value = 1; | |
| 52 | |
| 53 try | |
| 54 { | |
| 55 let obj = window; | |
| 56 while (path.length > 1) | |
| 57 obj = obj[path.shift()]; | |
| 58 value = obj[path.shift()]; | |
| 59 } | |
| 60 catch (e) | |
| 61 { | |
| 62 test.equal(e.name, errorName); | |
| 63 exceptionCaught = true; | |
| 64 } | |
| 65 | |
| 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 ); | |
| 76 } | |
| 77 | |
| 78 window.abpTest = "foo"; | |
| 79 await runSnippet(test, "abort-on-property-read", "abpTest"); | |
| 80 testProperty("abpTest"); | |
| 81 | |
| 82 window.abpTest2 = {prop1: "foo"}; | |
|
Manish Jethani
2019/03/20 15:20:52
Would be nice if we did't use "foo" for the value
hub
2019/03/20 16:19:05
Done.
| |
| 83 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | |
| 84 testProperty("abpTest2.prop1"); | |
| 85 | |
| 86 // Test that we try to catch a property that doesn't exist yet. | |
| 87 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | |
| 88 window.abpTest3 = {prop1: "foo"}; | |
| 89 testProperty("abpTest3.prop1"); | |
| 90 | |
| 91 // Test that other properties don't trigger. | |
| 92 testProperty("abpTest3.prop2", false); | |
| 93 | |
| 94 // Test overwriting the object with another object. | |
| 95 window.abpTest4 = {bar: {}}; | |
|
Manish Jethani
2019/03/20 15:17:48
If we agree about the convention here then it shou
hub
2019/03/20 16:19:04
Done.
Manish Jethani
2019/03/20 16:43:32
LGTM
(What I really meant was that within a new l
hub
2019/03/20 16:52:16
the intent was to really get these unique across t
| |
| 96 await runSnippet(test, "abort-on-property-read", "abpTest4.bar.lambda"); | |
| 97 testProperty("abpTest4.bar.lambda"); | |
| 98 window.abpTest4.bar = {}; | |
| 99 testProperty("abpTest4.bar.lambda"); | |
| 100 | |
| 101 // Test if we start with a non-object. | |
| 102 window.abpTest5 = 5; | |
|
Manish Jethani
2019/03/20 15:20:52
Nit: Same as above, would be nice to use 42 consis
hub
2019/03/20 16:19:05
Done.
| |
| 103 await runSnippet(test, "abort-on-property-read", "abpTest5.bar2.lambda"); | |
| 104 | |
| 105 testProperty("abpTest5.bar2.lambda", true, "TypeError"); | |
|
Manish Jethani
2019/03/20 15:17:48
Similarly, `abpTest5.prop1.foo`
(Sorry, the next
hub
2019/03/20 16:19:04
Done.
| |
| 106 | |
| 107 window.abpTest5 = {bar2: 42}; | |
| 108 testProperty("abpTest5.bar2.lambda", false); | |
| 109 window.abpTest5 = {bar2: {}}; | |
| 110 testProperty("abpTest5.bar2.lambda"); | |
| 111 | |
| 112 test.done(); | |
| 113 }; | |
| OLD | NEW |