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 = "fortytwo"; |
| 79 await runSnippet(test, "abort-on-property-read", "abpTest"); |
| 80 testProperty("abpTest"); |
| 81 |
| 82 window.abpTest2 = {prop1: "fortytwo"}; |
| 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: "fortytwo"}; |
| 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 = {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 |
| 112 test.done(); |
| 113 }; |
OLD | NEW |