Browse Source

test: don't compute knownGlobals lazily

Conditional globals like 'gc' should only be recognized when --expose_gc
is set.  The global.gc feature check works only when done eagerly, else
it lets through a leaked variable called 'gc'.
v0.11.12-release
Ben Noordhuis 11 years ago
committed by Fedor Indutny
parent
commit
2b6e078833
  1. 125
      test/common.js
  2. 27
      test/simple/test-common.js

125
test/common.js

@ -89,75 +89,76 @@ exports.spawnPwd = function(options) {
} }
}; };
var knownGlobals = [setTimeout,
setInterval,
setImmediate,
clearTimeout,
clearInterval,
clearImmediate,
console,
constructor, // Enumerable in V8 3.21.
Buffer,
process,
global];
if (global.gc) {
knownGlobals.push(gc);
}
// Turn this off if the test should not check for global leaks. if (global.DTRACE_HTTP_SERVER_RESPONSE) {
exports.globalCheck = true; 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);
knownGlobals.push(DTRACE_NET_SOCKET_READ);
knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
}
process.on('exit', function() { if (global.COUNTER_NET_SERVER_CONNECTION) {
if (!exports.globalCheck) return; knownGlobals.push(COUNTER_NET_SERVER_CONNECTION);
var knownGlobals = [setTimeout, knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE);
setInterval, knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST);
setImmediate, knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE);
clearTimeout, knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST);
clearInterval, knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
clearImmediate, }
console,
constructor, // Enumerable in V8 3.21.
Buffer,
process,
global];
if (global.gc) {
knownGlobals.push(gc);
}
if (global.DTRACE_HTTP_SERVER_RESPONSE) { if (global.ArrayBuffer) {
knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); knownGlobals.push(ArrayBuffer);
knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); knownGlobals.push(Int8Array);
knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); knownGlobals.push(Uint8Array);
knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); knownGlobals.push(Uint8ClampedArray);
knownGlobals.push(DTRACE_NET_STREAM_END); knownGlobals.push(Int16Array);
knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); knownGlobals.push(Uint16Array);
knownGlobals.push(DTRACE_NET_SOCKET_READ); knownGlobals.push(Int32Array);
knownGlobals.push(DTRACE_NET_SOCKET_WRITE); knownGlobals.push(Uint32Array);
} knownGlobals.push(Float32Array);
if (global.COUNTER_NET_SERVER_CONNECTION) { knownGlobals.push(Float64Array);
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); knownGlobals.push(DataView);
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.ArrayBuffer) { function leakedGlobals() {
knownGlobals.push(ArrayBuffer); var leaked = [];
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);
}
for (var x in global) { for (var val in global)
var found = false; if (-1 === knownGlobals.indexOf(global[val]))
leaked.push(val);
for (var y in knownGlobals) { return leaked;
if (global[x] === knownGlobals[y]) { };
found = true; exports.leakedGlobals = leakedGlobals;
break;
}
}
if (!found) { // Turn this off if the test should not check for global leaks.
console.error('Unknown global: %s', x); exports.globalCheck = true;
assert.ok(false, 'Unknown global found');
} process.on('exit', function() {
if (!exports.globalCheck) return;
var leaked = leakedGlobals();
if (leaked.length > 0) {
console.error('Unknown globals: %s', leaked);
assert.ok(false, 'Unknown global found');
} }
}); });

27
test/simple/test-common.js

@ -0,0 +1,27 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var common = require('../common');
var assert = require('assert');
common.globalCheck = false;
global.gc = 42; // Not a valid global unless --expose_gc is set.
assert.deepEqual(common.leakedGlobals(), ['gc']);
Loading…
Cancel
Save