From ecb1f17c1e7c91a7ef9570ac70e8d247b2b23fec Mon Sep 17 00:00:00 2001 From: WhoMeNope Date: Tue, 17 Oct 2017 20:34:27 +0200 Subject: [PATCH] test: add es6 module global leakage tests * created testing es6 module for global leakage tests * fixed a couple basic errors (vars -> lets, ...) * added globals leakage detection to es module tests * reversed whitespace change * whitespace change reversed PR-URL: https://github.com/nodejs/node/pull/16341 Reviewed-By: Jeremiah Senkpiel Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott Reviewed-By: Anatoli Papirovski Reviewed-By: Michael Dawson Reviewed-By: Ruben Bridgewater --- test/common/index.mjs | 109 ++++++++++++++++++++++ test/es-module/test-esm-json.mjs | 1 + test/es-module/test-esm-named-exports.mjs | 1 + test/es-module/test-esm-namespace.mjs | 1 + test/es-module/test-esm-snapshot.mjs | 1 + 5 files changed, 113 insertions(+) create mode 100644 test/common/index.mjs diff --git a/test/common/index.mjs b/test/common/index.mjs new file mode 100644 index 00000000000000..00eceb79d4c003 --- /dev/null +++ b/test/common/index.mjs @@ -0,0 +1,109 @@ +// Flags: --experimental-modules +/* eslint-disable required-modules */ + +import assert from 'assert'; + +let knownGlobals = [ + Buffer, + clearImmediate, + clearInterval, + clearTimeout, + console, + constructor, // Enumerable in V8 3.21. + global, + process, + setImmediate, + setInterval, + setTimeout +]; + +if (process.env.NODE_TEST_KNOWN_GLOBALS) { + const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(','); + allowGlobals(...knownFromEnv); +} + +export function allowGlobals(...whitelist) { + knownGlobals = knownGlobals.concat(whitelist); +} + +export function leakedGlobals() { + //add possible expected globals + if (global.gc) { + knownGlobals.push(global.gc); + } + + if (global.DTRACE_HTTP_SERVER_RESPONSE) { + knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); + knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); + knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); + knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); + knownGlobals.push(DTRACE_NET_STREAM_END); + knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); + } + + if (global.COUNTER_NET_SERVER_CONNECTION) { + knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); + knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); + knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); + knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); + knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); + knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); + } + + if (global.LTTNG_HTTP_SERVER_RESPONSE) { + knownGlobals.push(LTTNG_HTTP_SERVER_RESPONSE); + knownGlobals.push(LTTNG_HTTP_SERVER_REQUEST); + knownGlobals.push(LTTNG_HTTP_CLIENT_RESPONSE); + knownGlobals.push(LTTNG_HTTP_CLIENT_REQUEST); + knownGlobals.push(LTTNG_NET_STREAM_END); + knownGlobals.push(LTTNG_NET_SERVER_CONNECTION); + } + + if (global.ArrayBuffer) { + knownGlobals.push(ArrayBuffer); + knownGlobals.push(Int8Array); + knownGlobals.push(Uint8Array); + knownGlobals.push(Uint8ClampedArray); + knownGlobals.push(Int16Array); + knownGlobals.push(Uint16Array); + knownGlobals.push(Int32Array); + knownGlobals.push(Uint32Array); + knownGlobals.push(Float32Array); + knownGlobals.push(Float64Array); + knownGlobals.push(DataView); + } + + // Harmony features. + if (global.Proxy) { + knownGlobals.push(Proxy); + } + + if (global.Symbol) { + knownGlobals.push(Symbol); + } + + const leaked = []; + + for (const val in global) { + if (!knownGlobals.includes(global[val])) { + leaked.push(val); + } + } + + if (global.__coverage__) { + return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname)); + } else { + return leaked; + } +} + +// Turn this off if the test should not check for global leaks. +export let globalCheck = true; // eslint-disable-line + +process.on('exit', function() { + if (!globalCheck) return; + const leaked = leakedGlobals(); + if (leaked.length > 0) { + assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`); + } +}); diff --git a/test/es-module/test-esm-json.mjs b/test/es-module/test-esm-json.mjs index 864f4964def5b7..39279b74e5407c 100644 --- a/test/es-module/test-esm-json.mjs +++ b/test/es-module/test-esm-json.mjs @@ -1,5 +1,6 @@ // Flags: --experimental-modules /* eslint-disable required-modules */ +import '../common/index'; import assert from 'assert'; import ok from './test-esm-ok.mjs'; import json from './json.json'; diff --git a/test/es-module/test-esm-named-exports.mjs b/test/es-module/test-esm-named-exports.mjs index 89a290070260e8..c70e16e2167722 100644 --- a/test/es-module/test-esm-named-exports.mjs +++ b/test/es-module/test-esm-named-exports.mjs @@ -1,5 +1,6 @@ // Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs /* eslint-disable required-modules */ +import '../common/index'; import { readFile } from 'fs'; import assert from 'assert'; import ok from './test-esm-ok.mjs'; diff --git a/test/es-module/test-esm-namespace.mjs b/test/es-module/test-esm-namespace.mjs index 72b7fed4b33dfa..f6e550c2e5245e 100644 --- a/test/es-module/test-esm-namespace.mjs +++ b/test/es-module/test-esm-namespace.mjs @@ -1,6 +1,7 @@ // Flags: --experimental-modules /* eslint-disable required-modules */ +import '../common/index'; import * as fs from 'fs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-snapshot.mjs b/test/es-module/test-esm-snapshot.mjs index 89034f56681238..878be79a34044c 100644 --- a/test/es-module/test-esm-snapshot.mjs +++ b/test/es-module/test-esm-snapshot.mjs @@ -1,5 +1,6 @@ // Flags: --experimental-modules /* eslint-disable required-modules */ +import '../common/index'; import './esm-snapshot-mutator'; import one from './esm-snapshot'; import assert from 'assert';