0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-vm-cached-data.js
XadillaX befede64f1
test: remove require('buffer') from 4 test files
We don't use the global Buffer throughout the lib/ to avoid circular
dependency issues in core, but we actually don't need to require it on
test files. So remove them on:

+ test/parallel/test-stream-uint8array.js
+ test/parallel/test-stream2-finish-pipe.js
+ test/parallel/test-tls-session-cache.js
+ test/parallel/test-vm-cached-data.js

Refs: https://github.com/nodejs/node/issues/13836
PR-URL: https://github.com/nodejs/node/pull/13844
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-06-24 05:01:20 +03:00

92 lines
2.1 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const vm = require('vm');
const spawnSync = require('child_process').spawnSync;
function getSource(tag) {
return `(function ${tag}() { return '${tag}'; })`;
}
function produce(source, count) {
if (!count)
count = 1;
const out = spawnSync(process.execPath, [ '-e', `
'use strict';
const assert = require('assert');
const vm = require('vm');
var data;
for (var i = 0; i < ${count}; i++) {
var script = new vm.Script(process.argv[1], {
produceCachedData: true
});
assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);
if (script.cachedDataProduced)
data = script.cachedData.toString('base64');
}
console.log(data);
`, source]);
assert.strictEqual(out.status, 0, String(out.stderr));
return Buffer.from(out.stdout.toString(), 'base64');
}
function testProduceConsume() {
const source = getSource('original');
const data = produce(source);
// It should consume code cache
const script = new vm.Script(source, {
cachedData: data
});
assert(!script.cachedDataRejected);
assert.strictEqual(script.runInThisContext()(), 'original');
}
testProduceConsume();
function testProduceMultiple() {
const source = getSource('original');
produce(source, 3);
}
testProduceMultiple();
function testRejectInvalid() {
const source = getSource('invalid');
const data = produce(source);
// It should reject invalid code cache
const script = new vm.Script(getSource('invalid_1'), {
cachedData: data
});
assert(script.cachedDataRejected);
assert.strictEqual(script.runInThisContext()(), 'invalid_1');
}
testRejectInvalid();
function testRejectSlice() {
const source = getSource('slice');
const data = produce(source).slice(4);
const script = new vm.Script(source, {
cachedData: data
});
assert(script.cachedDataRejected);
}
testRejectSlice();
// It should throw on non-Buffer cachedData
assert.throws(() => {
new vm.Script('function abc() {}', {
cachedData: 'ohai'
});
}, /^TypeError: options\.cachedData must be a Buffer instance$/);