| 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 // For snippets that run in the context of the document via a <script> | |
|
Manish Jethani
2019/03/19 07:44:00
Nit: Mind leaving a line before the comment? It's
hub
2019/03/19 17:25:11
Done.
| |
| 38 // element (i.e. snippets that use makeInjector()), we need to wait for | |
| 39 // execution to be complete. | |
| 40 await timeout(100); | |
| 41 } | |
| 42 | |
| 43 exports.testAbortOnPropertyReadSnippet = async function(test) | |
| 44 { | |
| 45 window.abpTest = "foo"; | |
| 46 | |
| 47 function testProperty(property, result = true) | |
| 48 { | |
| 49 let path = property.split("."); | |
| 50 | |
| 51 let exceptionCaught = false; | |
| 52 let value = 1; | |
| 53 | |
| 54 try | |
| 55 { | |
| 56 let obj = window; | |
| 57 while (path.length > 1) | |
| 58 obj = obj[path.shift()]; | |
| 59 value = obj[path.shift()]; | |
| 60 } | |
| 61 catch (e) | |
| 62 { | |
| 63 exceptionCaught = true; | |
| 64 } | |
| 65 | |
| 66 test.equal(exceptionCaught, result, | |
| 67 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.`); | |
|
Manish Jethani
2019/03/19 07:44:00
The opening tick of the template string should be
hub
2019/03/19 17:25:11
Done.
| |
| 68 test.equal(value, result ? 1 : undefined, | |
| 69 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.`); | |
| 70 } | |
| 71 | |
| 72 await runSnippet(test, "abort-on-property-read", "abpTest"); | |
| 73 testProperty("abpTest"); | |
| 74 | |
| 75 window.abpTest2 = {prop1: "foo"}; | |
| 76 | |
| 77 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | |
| 78 testProperty("abpTest2.prop1"); | |
| 79 | |
| 80 // Test that we try to catch a property that don't exist yet. | |
| 81 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | |
| 82 window.abpTest3 = {prop1: "foo"}; | |
| 83 testProperty("abpTest3.prop1"); | |
| 84 | |
| 85 // Test that other properties don't trigger. | |
| 86 testProperty("abpTest3.prop2", false); | |
| 87 | |
| 88 // Test overwriting the object with another object | |
| 89 window.foo = {bar: {}}; | |
| 90 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); | |
| 91 testProperty("foo.bar.lambda"); | |
| 92 window.foo.bar = {}; | |
| 93 testProperty("foo.bar.lambda"); | |
| 94 | |
| 95 // Test if we start with a non-object | |
| 96 window.foo2 = 5; | |
| 97 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); | |
| 98 testProperty("foo2.bar2.lambda"); | |
| 99 window.foo2 = {}; | |
| 100 testProperty("foo2.bar2.lambda"); | |
| 101 | |
| 102 test.done(); | |
| 103 }; | |
| OLD | NEW |