Ryan Dahl | 55048cd | 2011-03-10 08:54:52 | [diff] [blame] | 1 | // 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 Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 22 | var path = require('path'); |
Fedor Indutny | 442d2d0 | 2014-01-18 12:18:25 | [diff] [blame] | 23 | var fs = require('fs'); |
Oleg Efimov | 093dfaf | 2010-12-05 22:33:52 | [diff] [blame] | 24 | var assert = require('assert'); |
Ryan Dahl | 7a2e784 | 2009-10-31 18:02:30 | [diff] [blame] | 25 | |
| 26 | exports.testDir = path.dirname(__filename); |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 27 | exports.fixturesDir = path.join(exports.testDir, 'fixtures'); |
| 28 | exports.libDir = path.join(exports.testDir, '../lib'); |
| 29 | exports.tmpDir = path.join(exports.testDir, 'tmp'); |
Timothy J Fontaine | 17a8126 | 2013-02-26 06:19:16 | [diff] [blame] | 30 | exports.PORT = +process.env.NODE_COMMON_PORT || 12346; |
Ryan Dahl | 4b8f503 | 2009-09-20 16:19:33 | [diff] [blame] | 31 | |
Your Name | fb47a33 | 2012-03-18 09:44:52 | [diff] [blame] | 32 | if (process.platform === 'win32') { |
Ryan Dahl | a6a3bf6 | 2011-07-21 21:19:24 | [diff] [blame] | 33 | exports.PIPE = '\\\\.\\pipe\\libuv-test'; |
Fedor Indutny | 6f3d603 | 2013-12-11 17:20:17 | [diff] [blame] | 34 | exports.opensslCli = path.join(process.execPath, '..', 'openssl-cli.exe'); |
Ben Noordhuis | bff9602 | 2011-07-21 19:21:06 | [diff] [blame] | 35 | } else { |
| 36 | exports.PIPE = exports.tmpDir + '/test.sock'; |
Fedor Indutny | 6f3d603 | 2013-12-11 17:20:17 | [diff] [blame] | 37 | exports.opensslCli = path.join(process.execPath, '..', 'openssl-cli'); |
Ben Noordhuis | bff9602 | 2011-07-21 19:21:06 | [diff] [blame] | 38 | } |
Fedor Indutny | 442d2d0 | 2014-01-18 12:18:25 | [diff] [blame] | 39 | if (!fs.existsSync(exports.opensslCli)) |
| 40 | exports.opensslCli = false; |
Ben Noordhuis | bff9602 | 2011-07-21 19:21:06 | [diff] [blame] | 41 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 42 | var util = require('util'); |
Micheil Smith | e38eb0c | 2010-10-11 21:04:09 | [diff] [blame] | 43 | for (var i in util) exports[i] = util[i]; |
Herbert Vojčík | cf2b206 | 2010-08-17 15:21:43 | [diff] [blame] | 44 | //for (var i in exports) global[i] = exports[i]; |
| 45 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 46 | function protoCtrChain(o) { |
Herbert Vojčík | cf2b206 | 2010-08-17 15:21:43 | [diff] [blame] | 47 | var result = []; |
| 48 | for (; o; o = o.__proto__) { result.push(o.constructor); } |
| 49 | return result.join(); |
| 50 | } |
| 51 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 52 | exports.indirectInstanceOf = function(obj, cls) { |
Herbert Vojčík | cf2b206 | 2010-08-17 15:21:43 | [diff] [blame] | 53 | 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 Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 58 | |
| 59 | |
Ryan Dahl | 3b0f2ce | 2011-08-10 00:43:57 | [diff] [blame] | 60 | exports.ddCommand = function(filename, kilobytes) { |
Your Name | fb47a33 | 2012-03-18 09:44:52 | [diff] [blame] | 61 | if (process.platform === 'win32') { |
isaacs | 0cdf85e | 2012-02-18 23:01:35 | [diff] [blame] | 62 | var p = path.resolve(exports.fixturesDir, 'create-file.js'); |
| 63 | return '"' + process.argv[0] + '" "' + p + '" "' + |
| 64 | filename + '" ' + (kilobytes * 1024); |
Ryan Dahl | 3b0f2ce | 2011-08-10 00:43:57 | [diff] [blame] | 65 | } else { |
Ryan Dahl | 3b0f2ce | 2011-08-10 00:43:57 | [diff] [blame] | 66 | return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | |
isaacs | 3383d77 | 2013-01-11 01:15:53 | [diff] [blame] | 71 | exports.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 Dahl | 4983bd3 | 2011-08-10 18:22:58 | [diff] [blame] | 82 | exports.spawnPwd = function(options) { |
| 83 | var spawn = require('child_process').spawn; |
| 84 | |
Your Name | fb47a33 | 2012-03-18 09:44:52 | [diff] [blame] | 85 | if (process.platform === 'win32') { |
Ryan Dahl | 4983bd3 | 2011-08-10 18:22:58 | [diff] [blame] | 86 | return spawn('cmd.exe', ['/c', 'cd'], options); |
| 87 | } else { |
| 88 | return spawn('pwd', [], options); |
| 89 | } |
| 90 | }; |
| 91 | |
Ben Noordhuis | 2b6e078 | 2014-01-30 12:02:58 | [diff] [blame] | 92 | var 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 | |
| 104 | if (global.gc) { |
| 105 | knownGlobals.push(gc); |
| 106 | } |
| 107 | |
| 108 | if (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 | |
| 119 | if (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 | |
| 128 | if (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 Noordhuis | bbd56d8 | 2014-01-30 12:21:07 | [diff] [blame] | 142 | // Harmony features. |
| 143 | if (global.Proxy) { |
| 144 | knownGlobals.push(Proxy); |
| 145 | } |
| 146 | |
| 147 | if (global.Symbol) { |
| 148 | knownGlobals.push(Symbol); |
| 149 | } |
| 150 | |
Ben Noordhuis | 2b6e078 | 2014-01-30 12:02:58 | [diff] [blame] | 151 | function 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 | }; |
| 160 | exports.leakedGlobals = leakedGlobals; |
Ryan Dahl | 3b0f2ce | 2011-08-10 00:43:57 | [diff] [blame] | 161 | |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 162 | // Turn this off if the test should not check for global leaks. |
| 163 | exports.globalCheck = true; |
| 164 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 165 | process.on('exit', function() { |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 166 | if (!exports.globalCheck) return; |
Ben Noordhuis | 2b6e078 | 2014-01-30 12:02:58 | [diff] [blame] | 167 | var leaked = leakedGlobals(); |
| 168 | if (leaked.length > 0) { |
| 169 | console.error('Unknown globals: %s', leaked); |
| 170 | assert.ok(false, 'Unknown global found'); |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 171 | } |
| 172 | }); |
Ryan Dahl | aac5cbe | 2011-02-18 18:05:31 | [diff] [blame] | 173 | |
| 174 | |
Ben Noordhuis | d0e6c3f | 2012-07-31 15:47:53 | [diff] [blame] | 175 | var mustCallChecks = []; |
Ryan Dahl | aac5cbe | 2011-02-18 18:05:31 | [diff] [blame] | 176 | |
Ben Noordhuis | d0e6c3f | 2012-07-31 15:47:53 | [diff] [blame] | 177 | |
Ben Noordhuis | d5a5ae3 | 2013-02-11 12:33:50 | [diff] [blame] | 178 | function runCallChecks(exitCode) { |
| 179 | if (exitCode !== 0) return; |
| 180 | |
Ben Noordhuis | d0e6c3f | 2012-07-31 15:47:53 | [diff] [blame] | 181 | 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 | |
| 197 | exports.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 Fontaine | 937e2e3 | 2014-02-18 00:29:23 | [diff] [blame] | 217 | |
| 218 | exports.checkSpawnSyncRet = function(ret) { |
| 219 | assert.strictEqual(ret.status, 0); |
| 220 | assert.strictEqual(ret.error, undefined); |
| 221 | }; |