summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-02-21 16:07:07 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2019-02-28 21:26:49 +0100
commita32cbe159749c645bf2da6b2af46f9b732b416e0 (patch)
tree76e738b2bf006b247c6b22023c2a5b78ecdb7086 /test
parent9edce1e12a7b69e7986dd15fce18d6e46590161a (diff)
downloadandroid-node-v8-a32cbe159749c645bf2da6b2af46f9b732b416e0.tar.gz
android-node-v8-a32cbe159749c645bf2da6b2af46f9b732b416e0.tar.bz2
android-node-v8-a32cbe159749c645bf2da6b2af46f9b732b416e0.zip
util: fix proxy inspection
This prevents any proxy traps from being called while inspecting proxy objects. That guarantees a side-effect free way of inspecting proxies. PR-URL: https://github.com/nodejs/node/pull/26241 Fixes: https://github.com/nodejs/node/issues/10731 Fixes: https://github.com/nodejs/node/issues/26231 Refs: https://github.com/nodejs/node/issues/25212 Refs: https://github.com/nodejs/node/issues/24765 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-util-inspect-proxy.js60
1 files changed, 48 insertions, 12 deletions
diff --git a/test/parallel/test-util-inspect-proxy.js b/test/parallel/test-util-inspect-proxy.js
index adc0810722..9aaa8fb13e 100644
--- a/test/parallel/test-util-inspect-proxy.js
+++ b/test/parallel/test-util-inspect-proxy.js
@@ -8,11 +8,35 @@ const { internalBinding } = require('internal/test/binding');
const processUtil = internalBinding('util');
const opts = { showProxy: true };
-const target = {};
+let proxyObj;
+let called = false;
+const target = {
+ [util.inspect.custom](depth, { showProxy }) {
+ if (showProxy === false) {
+ called = true;
+ if (proxyObj !== this) {
+ throw new Error('Failed');
+ }
+ }
+ return [1, 2, 3];
+ }
+};
const handler = {
- get: function() { throw new Error('Getter should not be called'); }
+ getPrototypeOf() { throw new Error('getPrototypeOf'); },
+ setPrototypeOf() { throw new Error('setPrototypeOf'); },
+ isExtensible() { throw new Error('isExtensible'); },
+ preventExtensions() { throw new Error('preventExtensions'); },
+ getOwnPropertyDescriptor() { throw new Error('getOwnPropertyDescriptor'); },
+ defineProperty() { throw new Error('defineProperty'); },
+ has() { throw new Error('has'); },
+ get() { throw new Error('get'); },
+ set() { throw new Error('set'); },
+ deleteProperty() { throw new Error('deleteProperty'); },
+ ownKeys() { throw new Error('ownKeys'); },
+ apply() { throw new Error('apply'); },
+ construct() { throw new Error('construct'); }
};
-const proxyObj = new Proxy(target, handler);
+proxyObj = new Proxy(target, handler);
// Inspecting the proxy should not actually walk it's properties
util.inspect(proxyObj, opts);
@@ -23,19 +47,31 @@ const details = processUtil.getProxyDetails(proxyObj);
assert.strictEqual(target, details[0]);
assert.strictEqual(handler, details[1]);
-assert.strictEqual(util.inspect(proxyObj, opts),
- 'Proxy [ {}, { get: [Function: get] } ]');
+assert.strictEqual(
+ util.inspect(proxyObj, opts),
+ 'Proxy [ [ 1, 2, 3 ],\n' +
+ ' { getPrototypeOf: [Function: getPrototypeOf],\n' +
+ ' setPrototypeOf: [Function: setPrototypeOf],\n' +
+ ' isExtensible: [Function: isExtensible],\n' +
+ ' preventExtensions: [Function: preventExtensions],\n' +
+ ' getOwnPropertyDescriptor: [Function: getOwnPropertyDescriptor],\n' +
+ ' defineProperty: [Function: defineProperty],\n' +
+ ' has: [Function: has],\n' +
+ ' get: [Function: get],\n' +
+ ' set: [Function: set],\n' +
+ ' deleteProperty: [Function: deleteProperty],\n' +
+ ' ownKeys: [Function: ownKeys],\n' +
+ ' apply: [Function: apply],\n' +
+ ' construct: [Function: construct] } ]'
+);
// Using getProxyDetails with non-proxy returns undefined
assert.strictEqual(processUtil.getProxyDetails({}), undefined);
-// This will throw because the showProxy option is not used
-// and the get function on the handler object defined above
-// is actually invoked.
-assert.throws(
- () => util.inspect(proxyObj),
- /^Error: Getter should not be called$/
-);
+// Inspecting a proxy without the showProxy option set to true should not
+// trigger any proxy handlers.
+assert.strictEqual(util.inspect(proxyObj), '[ 1, 2, 3 ]');
+assert(called);
// Yo dawg, I heard you liked Proxy so I put a Proxy
// inside your Proxy that proxies your Proxy's Proxy.