Skip to content

Commit a37a0ad

Browse files
committed
test: add test for prop interceptors on sandbox
PR-URL: #16409 Refs: #16293 Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent d4033c1 commit a37a0ad

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const vm = require('vm');
5+
6+
const dSymbol = Symbol('d');
7+
const sandbox = {
8+
a: 'a',
9+
dSymbol
10+
};
11+
12+
Object.defineProperties(sandbox, {
13+
b: {
14+
value: 'b'
15+
},
16+
c: {
17+
value: 'c',
18+
writable: true,
19+
enumerable: true
20+
},
21+
[dSymbol]: {
22+
value: 'd'
23+
},
24+
e: {
25+
value: 'e',
26+
configurable: true
27+
},
28+
f: {}
29+
});
30+
31+
const ctx = vm.createContext(sandbox);
32+
33+
const result = vm.runInContext(`
34+
const getDesc = (prop) => Object.getOwnPropertyDescriptor(this, prop);
35+
const result = {
36+
a: getDesc('a'),
37+
b: getDesc('b'),
38+
c: getDesc('c'),
39+
d: getDesc(dSymbol),
40+
e: getDesc('e'),
41+
f: getDesc('f'),
42+
g: getDesc('g')
43+
};
44+
result;
45+
`, ctx);
46+
47+
//eslint-disable-next-line no-restricted-properties
48+
assert.deepEqual(result, {
49+
a: { value: 'a', writable: true, enumerable: true, configurable: true },
50+
b: { value: 'b', writable: false, enumerable: false, configurable: false },
51+
c: { value: 'c', writable: true, enumerable: true, configurable: false },
52+
d: { value: 'd', writable: false, enumerable: false, configurable: false },
53+
e: { value: 'e', writable: false, enumerable: false, configurable: true },
54+
f: {
55+
value: undefined,
56+
writable: false,
57+
enumerable: false,
58+
configurable: false
59+
},
60+
g: undefined
61+
});
62+
63+
// define new properties
64+
vm.runInContext(`
65+
Object.defineProperty(this, 'h', {value: 'h'});
66+
Object.defineProperty(this, 'i', {});
67+
Object.defineProperty(this, 'j', {
68+
get() { return 'j'; }
69+
});
70+
let kValue = 0;
71+
Object.defineProperty(this, 'k', {
72+
get() { return kValue; },
73+
set(value) { kValue = value }
74+
});
75+
`, ctx);
76+
77+
assert.deepStrictEqual(Object.getOwnPropertyDescriptor(ctx, 'h'), {
78+
value: 'h',
79+
writable: false,
80+
enumerable: false,
81+
configurable: false
82+
});
83+
84+
assert.deepStrictEqual(Object.getOwnPropertyDescriptor(ctx, 'i'), {
85+
value: undefined,
86+
writable: false,
87+
enumerable: false,
88+
configurable: false
89+
});
90+
91+
const jDesc = Object.getOwnPropertyDescriptor(ctx, 'j');
92+
assert.strictEqual(typeof jDesc.get, 'function');
93+
assert.strictEqual(typeof jDesc.set, 'undefined');
94+
assert.strictEqual(jDesc.enumerable, false);
95+
assert.strictEqual(jDesc.configurable, false);
96+
97+
const kDesc = Object.getOwnPropertyDescriptor(ctx, 'k');
98+
assert.strictEqual(typeof kDesc.get, 'function');
99+
assert.strictEqual(typeof kDesc.set, 'function');
100+
assert.strictEqual(kDesc.enumerable, false);
101+
assert.strictEqual(kDesc.configurable, false);
102+
103+
assert.strictEqual(ctx.k, 0);
104+
ctx.k = 1;
105+
assert.strictEqual(ctx.k, 1);
106+
assert.strictEqual(vm.runInContext('k;', ctx), 1);
107+
vm.runInContext('k = 2;', ctx);
108+
assert.strictEqual(ctx.k, 2);
109+
assert.strictEqual(vm.runInContext('k;', ctx), 2);
110+
111+
// redefine properties on the global object
112+
assert.strictEqual(typeof vm.runInContext('encodeURI;', ctx), 'function');
113+
assert.strictEqual(ctx.encodeURI, undefined);
114+
vm.runInContext(`
115+
Object.defineProperty(this, 'encodeURI', { value: 42 });
116+
`, ctx);
117+
assert.strictEqual(vm.runInContext('encodeURI;', ctx), 42);
118+
assert.strictEqual(ctx.encodeURI, 42);
119+
120+
// redefine properties on the sandbox
121+
vm.runInContext(`
122+
Object.defineProperty(this, 'e', { value: 'newE' });
123+
`, ctx);
124+
assert.strictEqual(ctx.e, 'newE');
125+
126+
assert.throws(() => vm.runInContext(`
127+
'use strict';
128+
Object.defineProperty(this, 'f', { value: 'newF' });
129+
`, ctx), /TypeError: Cannot redefine property: f/);

0 commit comments

Comments
 (0)