blob: 40f707283270f5bc35c8aa1481b778a51a9d328f [file] [log] [blame]
Ryan Dahl55048cd2011-03-10 08:54:521// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
Oleg Efimov0ce9cba2010-12-04 22:45:5222var path = require('path');
Fedor Indutny442d2d02014-01-18 12:18:2523var fs = require('fs');
Oleg Efimov093dfaf2010-12-05 22:33:5224var assert = require('assert');
Ryan Dahl7a2e7842009-10-31 18:02:3025
26exports.testDir = path.dirname(__filename);
Oleg Efimov0ce9cba2010-12-04 22:45:5227exports.fixturesDir = path.join(exports.testDir, 'fixtures');
28exports.libDir = path.join(exports.testDir, '../lib');
29exports.tmpDir = path.join(exports.testDir, 'tmp');
Timothy J Fontaine17a81262013-02-26 06:19:1630exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
Ryan Dahl4b8f5032009-09-20 16:19:3331
Your Namefb47a332012-03-18 09:44:5232if (process.platform === 'win32') {
Ryan Dahla6a3bf62011-07-21 21:19:2433 exports.PIPE = '\\\\.\\pipe\\libuv-test';
Fedor Indutny6f3d6032013-12-11 17:20:1734 exports.opensslCli = path.join(process.execPath, '..', 'openssl-cli.exe');
Ben Noordhuisbff96022011-07-21 19:21:0635} else {
36 exports.PIPE = exports.tmpDir + '/test.sock';
Fedor Indutny6f3d6032013-12-11 17:20:1737 exports.opensslCli = path.join(process.execPath, '..', 'openssl-cli');
Ben Noordhuisbff96022011-07-21 19:21:0638}
Fedor Indutny442d2d02014-01-18 12:18:2539if (!fs.existsSync(exports.opensslCli))
40 exports.opensslCli = false;
Ben Noordhuisbff96022011-07-21 19:21:0641
Oleg Efimov0ce9cba2010-12-04 22:45:5242var util = require('util');
Micheil Smithe38eb0c2010-10-11 21:04:0943for (var i in util) exports[i] = util[i];
Herbert Vojčíkcf2b2062010-08-17 15:21:4344//for (var i in exports) global[i] = exports[i];
45
Oleg Efimov0665f022010-12-05 19:15:3046function protoCtrChain(o) {
Herbert Vojčíkcf2b2062010-08-17 15:21:4347 var result = [];
48 for (; o; o = o.__proto__) { result.push(o.constructor); }
49 return result.join();
50}
51
Oleg Efimov0ce9cba2010-12-04 22:45:5252exports.indirectInstanceOf = function(obj, cls) {
Herbert Vojčíkcf2b2062010-08-17 15:21:4353 if (obj instanceof cls) { return true; }
54 var clsChain = protoCtrChain(cls.prototype);
55 var objChain = protoCtrChain(obj);
56 return objChain.slice(-clsChain.length) === clsChain;
57};
Ryan Dahl02cc39f2010-12-04 21:40:3958
59
Ryan Dahl3b0f2ce2011-08-10 00:43:5760exports.ddCommand = function(filename, kilobytes) {
Your Namefb47a332012-03-18 09:44:5261 if (process.platform === 'win32') {
isaacs0cdf85e2012-02-18 23:01:3562 var p = path.resolve(exports.fixturesDir, 'create-file.js');
63 return '"' + process.argv[0] + '" "' + p + '" "' +
64 filename + '" ' + (kilobytes * 1024);
Ryan Dahl3b0f2ce2011-08-10 00:43:5765 } else {
Ryan Dahl3b0f2ce2011-08-10 00:43:5766 return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes;
67 }
68};
69
70
isaacs3383d772013-01-11 01:15:5371exports.spawnCat = function(options) {
72 var spawn = require('child_process').spawn;
73
74 if (process.platform === 'win32') {
75 return spawn('more', [], options);
76 } else {
77 return spawn('cat', [], options);
78 }
79};
80
81
Ryan Dahl4983bd32011-08-10 18:22:5882exports.spawnPwd = function(options) {
83 var spawn = require('child_process').spawn;
84
Your Namefb47a332012-03-18 09:44:5285 if (process.platform === 'win32') {
Ryan Dahl4983bd32011-08-10 18:22:5886 return spawn('cmd.exe', ['/c', 'cd'], options);
87 } else {
88 return spawn('pwd', [], options);
89 }
90};
91
Ben Noordhuis2b6e0782014-01-30 12:02:5892var knownGlobals = [setTimeout,
93 setInterval,
94 setImmediate,
95 clearTimeout,
96 clearInterval,
97 clearImmediate,
98 console,
99 constructor, // Enumerable in V8 3.21.
100 Buffer,
101 process,
102 global];
103
104if (global.gc) {
105 knownGlobals.push(gc);
106}
107
108if (global.DTRACE_HTTP_SERVER_RESPONSE) {
109 knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
110 knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
111 knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
112 knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
113 knownGlobals.push(DTRACE_NET_STREAM_END);
114 knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
115 knownGlobals.push(DTRACE_NET_SOCKET_READ);
116 knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
117}
118
119if (global.COUNTER_NET_SERVER_CONNECTION) {
120 knownGlobals.push(COUNTER_NET_SERVER_CONNECTION);
121 knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE);
122 knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST);
123 knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE);
124 knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST);
125 knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
126}
127
128if (global.ArrayBuffer) {
129 knownGlobals.push(ArrayBuffer);
130 knownGlobals.push(Int8Array);
131 knownGlobals.push(Uint8Array);
132 knownGlobals.push(Uint8ClampedArray);
133 knownGlobals.push(Int16Array);
134 knownGlobals.push(Uint16Array);
135 knownGlobals.push(Int32Array);
136 knownGlobals.push(Uint32Array);
137 knownGlobals.push(Float32Array);
138 knownGlobals.push(Float64Array);
139 knownGlobals.push(DataView);
140}
141
Ben Noordhuisbbd56d82014-01-30 12:21:07142// Harmony features.
143if (global.Proxy) {
144 knownGlobals.push(Proxy);
145}
146
147if (global.Symbol) {
148 knownGlobals.push(Symbol);
149}
150
Ben Noordhuis2b6e0782014-01-30 12:02:58151function leakedGlobals() {
152 var leaked = [];
153
154 for (var val in global)
155 if (-1 === knownGlobals.indexOf(global[val]))
156 leaked.push(val);
157
158 return leaked;
159};
160exports.leakedGlobals = leakedGlobals;
Ryan Dahl3b0f2ce2011-08-10 00:43:57161
Ryan Dahl02cc39f2010-12-04 21:40:39162// Turn this off if the test should not check for global leaks.
163exports.globalCheck = true;
164
Oleg Efimov0665f022010-12-05 19:15:30165process.on('exit', function() {
Ryan Dahl02cc39f2010-12-04 21:40:39166 if (!exports.globalCheck) return;
Ben Noordhuis2b6e0782014-01-30 12:02:58167 var leaked = leakedGlobals();
168 if (leaked.length > 0) {
169 console.error('Unknown globals: %s', leaked);
170 assert.ok(false, 'Unknown global found');
Ryan Dahl02cc39f2010-12-04 21:40:39171 }
172});
Ryan Dahlaac5cbe2011-02-18 18:05:31173
174
Ben Noordhuisd0e6c3f2012-07-31 15:47:53175var mustCallChecks = [];
Ryan Dahlaac5cbe2011-02-18 18:05:31176
Ben Noordhuisd0e6c3f2012-07-31 15:47:53177
Ben Noordhuisd5a5ae32013-02-11 12:33:50178function runCallChecks(exitCode) {
179 if (exitCode !== 0) return;
180
Ben Noordhuisd0e6c3f2012-07-31 15:47:53181 var failed = mustCallChecks.filter(function(context) {
182 return context.actual !== context.expected;
183 });
184
185 failed.forEach(function(context) {
186 console.log('Mismatched %s function calls. Expected %d, actual %d.',
187 context.name,
188 context.expected,
189 context.actual);
190 console.log(context.stack.split('\n').slice(2).join('\n'));
191 });
192
193 if (failed.length) process.exit(1);
194}
195
196
197exports.mustCall = function(fn, expected) {
198 if (typeof expected !== 'number') expected = 1;
199
200 var context = {
201 expected: expected,
202 actual: 0,
203 stack: (new Error).stack,
204 name: fn.name || '<anonymous>'
205 };
206
207 // add the exit listener only once to avoid listener leak warnings
208 if (mustCallChecks.length === 0) process.on('exit', runCallChecks);
209
210 mustCallChecks.push(context);
211
212 return function() {
213 context.actual++;
214 return fn.apply(this, arguments);
215 };
216};
Timothy J Fontaine937e2e32014-02-18 00:29:23217
218exports.checkSpawnSyncRet = function(ret) {
219 assert.strictEqual(ret.status, 0);
220 assert.strictEqual(ret.error, undefined);
221};