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 await timeout(100); | |
38 } | |
39 | |
40 exports.testAbortOnPropertyReadSnippet = async function(test) | |
41 { | |
42 window.abpTest = "foo"; | |
43 | |
44 function testProperty(property, result = true) | |
45 { | |
46 let path = property.split("."); | |
47 | |
48 let exceptionCaught = false; | |
49 let value = 1; | |
50 | |
51 try | |
52 { | |
53 let obj = window; | |
Manish Jethani
2019/03/14 10:58:13
What about just doing it exactly like in `lib/cont
hub
2019/03/14 12:32:55
I don't think it really matters either way.
Manish Jethani
2019/03/14 16:56:23
Alright.
| |
54 while (path.length > 1) | |
55 obj = obj[path.shift()]; | |
56 value = obj[path.shift()]; | |
57 } | |
58 catch (e) | |
59 { | |
60 if (path.length == 0) | |
Manish Jethani
2019/03/14 10:58:13
Will `path.length` ever be non-zero here?
hub
2019/03/14 12:32:55
if any other exception, it could.
Manish Jethani
2019/03/14 16:56:22
And why would there be any other exception? Can yo
hub
2019/03/14 22:22:11
it's gone then.
| |
61 exceptionCaught = true; | |
62 } | |
63 | |
64 test.equal(exceptionCaught, result, `The property "${property}" ${result ? " didn't" : "did"} trigger an exception.`); | |
Manish Jethani
2019/03/14 10:58:13
Nit: We could use "should" here for consistency: "
hub
2019/03/14 12:32:55
Done.
Manish Jethani
2019/03/14 16:56:23
"should" should be first, then "shouldn't".
hub
2019/03/14 22:22:11
my bad, fixed.
| |
65 test.equal(value, result ? 1 : undefined, `The value for "${property}" ${res ult ? "shouldn't" : "should"} have been read.`); | |
66 } | |
67 | |
68 await runSnippet(test, "abort-on-property-read", "abpTest"); | |
69 testProperty("abpTest"); | |
70 | |
71 window.abpTest2 = {prop1: "foo"}; | |
72 | |
73 await runSnippet(test, "abort-on-property-read", "abpTest2.prop1"); | |
74 testProperty("abpTest2.prop1"); | |
75 | |
76 // Test that we try to catch a property that don't exist yet. | |
77 await runSnippet(test, "abort-on-property-read", "abpTest3.prop1"); | |
78 window.abpTest3 = {prop1: "foo"}; | |
79 testProperty("abpTest3.prop1"); | |
80 | |
81 // Test that other properties don't trigger. | |
82 testProperty("abpTest3.prop2", false); | |
83 | |
84 // Test overwriting the object with another object | |
85 window.foo = {bar: {}}; | |
86 await runSnippet(test, "abort-on-property-read", "foo.bar.lambda"); | |
87 testProperty("foo.bar.lambda"); | |
88 window.foo.bar = {}; | |
89 testProperty("foo.bar.lambda"); | |
90 | |
91 // Test if we start with a non-object | |
92 window.foo2 = 5; | |
93 await runSnippet(test, "abort-on-property-read", "foo2.bar2.lambda"); | |
94 testProperty("foo2.bar2.lambda"); | |
95 window.foo2 = {}; | |
96 testProperty("foo2.bar2.lambda"); | |
97 | |
98 test.done(); | |
99 }; | |
100 | |
Manish Jethani
2019/03/14 10:58:13
Unnecessary blank line at the end.
hub
2019/03/14 12:32:55
Done.
| |
OLD | NEW |