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 window.abpTest = "foo"; |
| 47 |
| 48 function testProperty(property, result = true) |
| 49 { |
| 50 let path = property.split("."); |
| 51 |
| 52 let exceptionCaught = false; |
| 53 let value = 1; |
| 54 |
| 55 try |
| 56 { |
| 57 let obj = window; |
| 58 while (path.length > 1) |
| 59 obj = obj[path.shift()]; |
| 60 value = obj[path.shift()]; |
| 61 } |
| 62 catch (e) |
| 63 { |
| 64 exceptionCaught = true; |
| 65 } |
| 66 |
| 67 test.equal( |
| 68 exceptionCaught, |
| 69 result, |
| 70 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an
exception.` |
| 71 ); |
| 72 test.equal( |
| 73 value, |
| 74 result ? 1 : undefined, |
| 75 `The value for "${property}" ${result ? "shouldn't" : "should"} have been
read.` |
| 76 ); |
| 77 } |
| 78 |
| 79 await runSnippet(test, "abort-on-property-read", "abpTest"); |
| 80 testProperty("abpTest"); |
| 81 |
| 82 window.abpTest2 = {prop1: "foo"}; |
| 83 |
| 84 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); |
| 85 testProperty("abpTest2.prop1"); |
| 86 |
| 87 // Test that we try to catch a property that don't exist yet. |
| 88 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); |
| 89 window.abpTest3 = {prop1: "foo"}; |
| 90 testProperty("abpTest3.prop1"); |
| 91 |
| 92 // Test that other properties don't trigger. |
| 93 testProperty("abpTest3.prop2", false); |
| 94 |
| 95 // Test overwriting the object with another object |
| 96 window.foo = {bar: {}}; |
| 97 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); |
| 98 testProperty("foo.bar.lambda"); |
| 99 window.foo.bar = {}; |
| 100 testProperty("foo.bar.lambda"); |
| 101 |
| 102 // Test if we start with a non-object |
| 103 window.foo2 = 5; |
| 104 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); |
| 105 testProperty("foo2.bar2.lambda"); |
| 106 window.foo2 = {}; |
| 107 testProperty("foo2.bar2.lambda"); |
| 108 |
| 109 test.done(); |
| 110 }; |
OLD | NEW |