1 var path = require('path');
2 var fs = require('fs');
3 var assert = require('assert');
4 var os = require('os');
5 var child_process = require('child_process');
7 exports.testDir = path.dirname(__filename);
8 exports.fixturesDir = path.join(exports.testDir, 'fixtures');
9 exports.libDir = path.join(exports.testDir, '../lib');
10 exports.tmpDirName = 'tmp';
11 exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
13 if (process.env.TEST_THREAD_ID) {
14 // Distribute ports in parallel tests
15 if (!process.env.NODE_COMMON_PORT)
16 exports.PORT += +process.env.TEST_THREAD_ID * 100;
18 exports.tmpDirName += '.' + process.env.TEST_THREAD_ID;
20 exports.tmpDir = path.join(exports.testDir, exports.tmpDirName);
22 var opensslCli = null;
23 var inFreeBSDJail = null;
24 var localhostIPv4 = null;
26 Object.defineProperty(exports, 'inFreeBSDJail', {
28 if (inFreeBSDJail !== null) return inFreeBSDJail;
30 if (process.platform === 'freebsd' &&
31 child_process.execSync('sysctl -n security.jail.jailed').toString() ===
35 inFreeBSDJail = false;
41 Object.defineProperty(exports, 'localhostIPv4', {
43 if (localhostIPv4 !== null) return localhostIPv4;
45 if (exports.inFreeBSDJail) {
46 // Jailed network interfaces are a bit special - since we need to jump
47 // through loops, as well as this being an exception case, assume the
48 // user will provide this instead.
49 if (process.env.LOCALHOST) {
50 localhostIPv4 = process.env.LOCALHOST;
52 console.error('Looks like we\'re in a FreeBSD Jail. ' +
53 'Please provide your default interface address ' +
54 'as LOCALHOST or expect some tests to fail.');
58 if (localhostIPv4 === null) localhostIPv4 = '127.0.0.1';
64 // opensslCli defined lazily to reduce overhead of spawnSync
65 Object.defineProperty(exports, 'opensslCli', {get: function() {
66 if (opensslCli !== null) return opensslCli;
68 if (process.config.variables.node_shared_openssl) {
69 // use external command
70 opensslCli = 'openssl';
72 // use command built from sources included in io.js repository
73 opensslCli = path.join(path.dirname(process.execPath), 'openssl-cli');
76 if (process.platform === 'win32') opensslCli += '.exe';
78 var openssl_cmd = child_process.spawnSync(opensslCli, ['version']);
79 if (openssl_cmd.status !== 0 || openssl_cmd.error !== undefined) {
80 // openssl command cannot be executed
84 }, enumerable: true });
86 Object.defineProperty(exports, 'hasCrypto', {get: function() {
87 return process.versions.openssl ? true : false;
90 if (process.platform === 'win32') {
91 exports.PIPE = '\\\\.\\pipe\\libuv-test';
93 exports.PIPE = exports.tmpDir + '/test.sock';
96 if (process.env.NODE_COMMON_PIPE) {
97 exports.PIPE = process.env.NODE_COMMON_PIPE;
98 // Remove manually, the test runner won't do it
99 // for us like it does for files in test/tmp.
101 fs.unlinkSync(exports.PIPE);
107 if (process.platform === 'win32') {
108 exports.faketimeCli = false;
110 exports.faketimeCli = path.join(__dirname, "..", "tools", "faketime", "src",
114 var ifaces = os.networkInterfaces();
115 exports.hasIPv6 = Object.keys(ifaces).some(function(name) {
116 return /lo/.test(name) && ifaces[name].some(function(info) {
117 return info.family === 'IPv6';
121 var util = require('util');
122 for (var i in util) exports[i] = util[i];
123 //for (var i in exports) global[i] = exports[i];
125 function protoCtrChain(o) {
127 for (; o; o = o.__proto__) { result.push(o.constructor); }
128 return result.join();
131 exports.indirectInstanceOf = function(obj, cls) {
132 if (obj instanceof cls) { return true; }
133 var clsChain = protoCtrChain(cls.prototype);
134 var objChain = protoCtrChain(obj);
135 return objChain.slice(-clsChain.length) === clsChain;
139 exports.ddCommand = function(filename, kilobytes) {
140 if (process.platform === 'win32') {
141 var p = path.resolve(exports.fixturesDir, 'create-file.js');
142 return '"' + process.argv[0] + '" "' + p + '" "' +
143 filename + '" ' + (kilobytes * 1024);
145 return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes;
150 exports.spawnCat = function(options) {
151 var spawn = require('child_process').spawn;
153 if (process.platform === 'win32') {
154 return spawn('more', [], options);
156 return spawn('cat', [], options);
161 exports.spawnSyncCat = function(options) {
162 var spawnSync = require('child_process').spawnSync;
164 if (process.platform === 'win32') {
165 return spawnSync('more', [], options);
167 return spawnSync('cat', [], options);
172 exports.spawnPwd = function(options) {
173 var spawn = require('child_process').spawn;
175 if (process.platform === 'win32') {
176 return spawn('cmd.exe', ['/c', 'cd'], options);
178 return spawn('pwd', [], options);
182 exports.platformTimeout = function(ms) {
183 if (process.arch !== 'arm')
186 if (process.config.variables.arm_version === '6')
187 return 6 * ms; // ARMv6
189 return 2 * ms; // ARMv7 and up.
192 var knownGlobals = [setTimeout,
199 constructor, // Enumerable in V8 3.21.
205 knownGlobals.push(gc);
208 if (global.DTRACE_HTTP_SERVER_RESPONSE) {
209 knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
210 knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
211 knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
212 knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
213 knownGlobals.push(DTRACE_NET_STREAM_END);
214 knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
217 if (global.COUNTER_NET_SERVER_CONNECTION) {
218 knownGlobals.push(COUNTER_NET_SERVER_CONNECTION);
219 knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE);
220 knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST);
221 knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE);
222 knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST);
223 knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
226 if (global.LTTNG_HTTP_SERVER_RESPONSE) {
227 knownGlobals.push(LTTNG_HTTP_SERVER_RESPONSE);
228 knownGlobals.push(LTTNG_HTTP_SERVER_REQUEST);
229 knownGlobals.push(LTTNG_HTTP_CLIENT_RESPONSE);
230 knownGlobals.push(LTTNG_HTTP_CLIENT_REQUEST);
231 knownGlobals.push(LTTNG_NET_STREAM_END);
232 knownGlobals.push(LTTNG_NET_SERVER_CONNECTION);
235 if (global.ArrayBuffer) {
236 knownGlobals.push(ArrayBuffer);
237 knownGlobals.push(Int8Array);
238 knownGlobals.push(Uint8Array);
239 knownGlobals.push(Uint8ClampedArray);
240 knownGlobals.push(Int16Array);
241 knownGlobals.push(Uint16Array);
242 knownGlobals.push(Int32Array);
243 knownGlobals.push(Uint32Array);
244 knownGlobals.push(Float32Array);
245 knownGlobals.push(Float64Array);
246 knownGlobals.push(DataView);
251 knownGlobals.push(Proxy);
255 knownGlobals.push(Symbol);
258 function leakedGlobals() {
261 for (var val in global)
262 if (-1 === knownGlobals.indexOf(global[val]))
267 exports.leakedGlobals = leakedGlobals;
269 // Turn this off if the test should not check for global leaks.
270 exports.globalCheck = true;
272 process.on('exit', function() {
273 if (!exports.globalCheck) return;
274 var leaked = leakedGlobals();
275 if (leaked.length > 0) {
276 console.error('Unknown globals: %s', leaked);
277 assert.ok(false, 'Unknown global found');
282 var mustCallChecks = [];
285 function runCallChecks(exitCode) {
286 if (exitCode !== 0) return;
288 var failed = mustCallChecks.filter(function(context) {
289 return context.actual !== context.expected;
292 failed.forEach(function(context) {
293 console.log('Mismatched %s function calls. Expected %d, actual %d.',
297 console.log(context.stack.split('\n').slice(2).join('\n'));
300 if (failed.length) process.exit(1);
304 exports.mustCall = function(fn, expected) {
305 if (typeof expected !== 'number') expected = 1;
310 stack: (new Error).stack,
311 name: fn.name || '<anonymous>'
314 // add the exit listener only once to avoid listener leak warnings
315 if (mustCallChecks.length === 0) process.on('exit', runCallChecks);
317 mustCallChecks.push(context);
321 return fn.apply(this, arguments);
325 exports.checkSpawnSyncRet = function(ret) {
326 assert.strictEqual(ret.status, 0);
327 assert.strictEqual(ret.error, undefined);
330 var etcServicesFileName = path.join('/etc', 'services');
331 if (process.platform === 'win32') {
332 etcServicesFileName = path.join(process.env.SystemRoot, 'System32', 'drivers',
337 * Returns a string that represents the service name associated
338 * to the service bound to port "port" and using protocol "protocol".
340 * If the service is not defined in the services file, it returns
341 * the port number as a string.
343 * Returns undefined if /etc/services (or its equivalent on non-UNIX
344 * platforms) can't be read.
346 exports.getServiceName = function getServiceName(port, protocol) {
348 throw new Error("Missing port number");
351 if (typeof protocol !== 'string') {
352 throw new Error("Protocol must be a string");
356 * By default, if a service can't be found in /etc/services,
357 * its name is considered to be its port number.
359 var serviceName = port.toString();
363 * I'm not a big fan of readFileSync, but reading /etc/services asynchronously
364 * here would require implementing a simple line parser, which seems overkill
365 * for a simple utility function that is not running concurrently with any
368 var servicesContent = fs.readFileSync(etcServicesFileName,
369 { encoding: 'utf8'});
370 var regexp = util.format('^(\\w+)\\s+\\s%d/%s\\s', port, protocol);
371 var re = new RegExp(regexp, 'm');
373 var matches = re.exec(servicesContent);
374 if (matches && matches.length > 1) {
375 serviceName = matches[1];
378 console.error('Cannot read file: ', etcServicesFileName);
385 exports.hasMultiLocalhost = function hasMultiLocalhost() {
386 var TCP = process.binding('tcp_wrap').TCP;
388 var ret = t.bind('127.0.0.2', exports.PORT);
393 exports.isValidHostname = function(str) {
394 // See http://stackoverflow.com/a/3824105
396 '^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])' +
397 '(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$');
399 return !!str.match(re) && str.length <= 255;
402 exports.fileExists = function(pathname) {
404 fs.accessSync(pathname);