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'); |
Oleg Efimov | 093dfaf | 2010-12-05 22:33:52 | [diff] [blame] | 23 | var assert = require('assert'); |
Ryan Dahl | 7a2e784 | 2009-10-31 18:02:30 | [diff] [blame] | 24 | |
| 25 | exports.testDir = path.dirname(__filename); |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 26 | exports.fixturesDir = path.join(exports.testDir, 'fixtures'); |
| 27 | exports.libDir = path.join(exports.testDir, '../lib'); |
| 28 | exports.tmpDir = path.join(exports.testDir, 'tmp'); |
arlolra | 724ccf1 | 2010-02-26 20:06:32 | [diff] [blame] | 29 | exports.PORT = 12346; |
Ryan Dahl | 4b8f503 | 2009-09-20 16:19:33 | [diff] [blame] | 30 | |
Ben Noordhuis | bff9602 | 2011-07-21 19:21:06 | [diff] [blame] | 31 | if (process.platform == 'win32') { |
Ryan Dahl | a6a3bf6 | 2011-07-21 21:19:24 | [diff] [blame] | 32 | exports.PIPE = '\\\\.\\pipe\\libuv-test'; |
Ben Noordhuis | bff9602 | 2011-07-21 19:21:06 | [diff] [blame] | 33 | } else { |
| 34 | exports.PIPE = exports.tmpDir + '/test.sock'; |
| 35 | } |
| 36 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 37 | var util = require('util'); |
Micheil Smith | e38eb0c | 2010-10-11 21:04:09 | [diff] [blame] | 38 | for (var i in util) exports[i] = util[i]; |
Herbert Vojčík | cf2b206 | 2010-08-17 15:21:43 | [diff] [blame] | 39 | //for (var i in exports) global[i] = exports[i]; |
| 40 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 41 | function protoCtrChain(o) { |
Herbert Vojčík | cf2b206 | 2010-08-17 15:21:43 | [diff] [blame] | 42 | var result = []; |
| 43 | for (; o; o = o.__proto__) { result.push(o.constructor); } |
| 44 | return result.join(); |
| 45 | } |
| 46 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 47 | exports.indirectInstanceOf = function(obj, cls) { |
Herbert Vojčík | cf2b206 | 2010-08-17 15:21:43 | [diff] [blame] | 48 | if (obj instanceof cls) { return true; } |
| 49 | var clsChain = protoCtrChain(cls.prototype); |
| 50 | var objChain = protoCtrChain(obj); |
| 51 | return objChain.slice(-clsChain.length) === clsChain; |
| 52 | }; |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 53 | |
| 54 | |
Ryan Dahl | 3b0f2ce | 2011-08-10 00:43:57 | [diff] [blame] | 55 | exports.ddCommand = function(filename, kilobytes) { |
| 56 | if (process.platform == 'win32') { |
isaacs | 0cdf85e | 2012-02-18 23:01:35 | [diff] [blame^] | 57 | var p = path.resolve(exports.fixturesDir, 'create-file.js'); |
| 58 | return '"' + process.argv[0] + '" "' + p + '" "' + |
| 59 | filename + '" ' + (kilobytes * 1024); |
Ryan Dahl | 3b0f2ce | 2011-08-10 00:43:57 | [diff] [blame] | 60 | } else { |
Ryan Dahl | 3b0f2ce | 2011-08-10 00:43:57 | [diff] [blame] | 61 | return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | |
Ryan Dahl | 4983bd3 | 2011-08-10 18:22:58 | [diff] [blame] | 66 | exports.spawnPwd = function(options) { |
| 67 | var spawn = require('child_process').spawn; |
| 68 | |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 69 | if (process.platform == 'win32') { |
Ryan Dahl | 4983bd3 | 2011-08-10 18:22:58 | [diff] [blame] | 70 | return spawn('cmd.exe', ['/c', 'cd'], options); |
| 71 | } else { |
| 72 | return spawn('pwd', [], options); |
| 73 | } |
| 74 | }; |
| 75 | |
Ryan Dahl | 3b0f2ce | 2011-08-10 00:43:57 | [diff] [blame] | 76 | |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 77 | // Turn this off if the test should not check for global leaks. |
| 78 | exports.globalCheck = true; |
| 79 | |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 80 | process.on('exit', function() { |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 81 | if (!exports.globalCheck) return; |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 82 | var knownGlobals = [setTimeout, |
| 83 | setInterval, |
| 84 | clearTimeout, |
| 85 | clearInterval, |
| 86 | console, |
| 87 | Buffer, |
| 88 | process, |
| 89 | global]; |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 90 | |
Ryan Dahl | 86214c9 | 2011-06-14 16:02:58 | [diff] [blame] | 91 | if (global.errno) { |
| 92 | knownGlobals.push(errno); |
| 93 | } |
| 94 | |
Jorge Chamorro Bieling | e7604b1 | 2011-03-23 11:29:49 | [diff] [blame] | 95 | if (global.gc) { |
| 96 | knownGlobals.push(gc); |
| 97 | } |
| 98 | |
Ryan Dahl | e9257b8 | 2011-02-10 02:50:26 | [diff] [blame] | 99 | if (global.DTRACE_HTTP_SERVER_RESPONSE) { |
Ryan Dahl | 068b733 | 2011-01-25 01:50:10 | [diff] [blame] | 100 | knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); |
| 101 | knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); |
Ryan Dahl | e9257b8 | 2011-02-10 02:50:26 | [diff] [blame] | 102 | knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); |
| 103 | knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); |
Ryan Dahl | 068b733 | 2011-01-25 01:50:10 | [diff] [blame] | 104 | knownGlobals.push(DTRACE_NET_STREAM_END); |
| 105 | knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); |
Ryan Dahl | e9257b8 | 2011-02-10 02:50:26 | [diff] [blame] | 106 | knownGlobals.push(DTRACE_NET_SOCKET_READ); |
| 107 | knownGlobals.push(DTRACE_NET_SOCKET_WRITE); |
Ryan Dahl | 068b733 | 2011-01-25 01:50:10 | [diff] [blame] | 108 | } |
| 109 | |
Thomas Shinnick | 59b815b | 2011-08-13 21:51:31 | [diff] [blame] | 110 | if (global.ArrayBuffer) { |
| 111 | knownGlobals.push(ArrayBuffer); |
| 112 | knownGlobals.push(Int8Array); |
| 113 | knownGlobals.push(Uint8Array); |
| 114 | knownGlobals.push(Int16Array); |
| 115 | knownGlobals.push(Uint16Array); |
| 116 | knownGlobals.push(Int32Array); |
| 117 | knownGlobals.push(Uint32Array); |
| 118 | knownGlobals.push(Float32Array); |
| 119 | knownGlobals.push(Float64Array); |
| 120 | knownGlobals.push(DataView); |
| 121 | } |
| 122 | |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 123 | for (var x in global) { |
| 124 | var found = false; |
| 125 | |
| 126 | for (var y in knownGlobals) { |
| 127 | if (global[x] === knownGlobals[y]) { |
| 128 | found = true; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (!found) { |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 134 | console.error('Unknown global: %s', x); |
Ben Noordhuis | fa44659 | 2011-09-28 23:11:26 | [diff] [blame] | 135 | assert.ok(false, 'Unknown global found'); |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | }); |
Ryan Dahl | aac5cbe | 2011-02-18 18:05:31 | [diff] [blame] | 139 | |
| 140 | |
| 141 | // This function allows one two run an HTTP test agaist both HTTPS and |
| 142 | // normal HTTP modules. This ensures they fit the same API. |
| 143 | exports.httpTest = function httpTest(cb) { |
| 144 | }; |
| 145 | |