| 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 window.abpTest = "foo"; | |
|
Manish Jethani
2019/03/20 01:42:00
This seems odd standing out on the top. Let's move
hub
2019/03/20 15:10:07
Done.
| |
| 47 | |
| 48 function testProperty(property, result = true, errorType = "ReferenceError") | |
| 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 test.equal(e.name, errorType); | |
|
Manish Jethani
2019/03/20 01:42:01
Nit: Since we're comparing with the `name` propert
hub
2019/03/20 15:10:05
Done.
| |
| 65 exceptionCaught = true; | |
| 66 } | |
| 67 | |
| 68 test.equal( | |
| 69 exceptionCaught, | |
| 70 result, | |
| 71 `The property "${property}" ${result ? "should" : "shouldn't"} trigger an exception.` | |
| 72 ); | |
| 73 test.equal( | |
| 74 value, | |
| 75 result ? 1 : undefined, | |
| 76 `The value for "${property}" ${result ? "shouldn't" : "should"} have been read.` | |
| 77 ); | |
| 78 } | |
| 79 | |
| 80 await runSnippet(test, "abort-on-property-read", "abpTest"); | |
| 81 testProperty("abpTest"); | |
| 82 | |
| 83 window.abpTest2 = {prop1: "foo"}; | |
| 84 | |
|
Manish Jethani
2019/03/20 01:41:59
Nit: Blank line here seems unnecessary if we are g
hub
2019/03/20 15:10:06
Done.
| |
| 85 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | |
| 86 testProperty("abpTest2.prop1"); | |
| 87 | |
| 88 // Test that we try to catch a property that don't exist yet. | |
|
Manish Jethani
2019/03/20 01:41:59
Nit: s/don't/doesn't/
hub
2019/03/20 15:10:06
Done.
| |
| 89 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | |
| 90 window.abpTest3 = {prop1: "foo"}; | |
| 91 testProperty("abpTest3.prop1"); | |
| 92 | |
| 93 // Test that other properties don't trigger. | |
| 94 testProperty("abpTest3.prop2", false); | |
| 95 | |
| 96 // Test overwriting the object with another object | |
|
Manish Jethani
2019/03/20 01:42:01
Nit: Period at the end (I don't mind too much but
hub
2019/03/20 15:10:06
Done.
| |
| 97 window.foo = {bar: {}}; | |
|
Manish Jethani
2019/03/20 01:41:59
There are some inconsistencies with the naming her
hub
2019/03/20 15:10:05
Done.
| |
| 98 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); | |
| 99 testProperty("foo.bar.lambda"); | |
| 100 window.foo.bar = {}; | |
| 101 testProperty("foo.bar.lambda"); | |
| 102 | |
| 103 // Test if we start with a non-object | |
| 104 window.foo2 = 5; | |
| 105 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); | |
| 106 | |
| 107 // We expect that accessing the property cause a TypeError. | |
|
Manish Jethani
2019/03/20 01:42:00
Nit: s/cause/causes/
(FWIW I'm not sure this comm
hub
2019/03/20 15:10:06
Deleted.
| |
| 108 testProperty("foo2.bar2.lambda", true, "TypeError"); | |
| 109 | |
| 110 window.foo2 = {bar2: {}}; | |
|
Manish Jethani
2019/03/20 01:42:00
Before this line, we could throw in another test:
hub
2019/03/20 15:10:07
Done.
| |
| 111 testProperty("foo2.bar2.lambda"); | |
| 112 | |
| 113 test.done(); | |
| 114 }; | |
| OLD | NEW |