Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / test / core / common.js
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
22 var path = require('path');
23 var assert = require('assert');
24
25 exports.testDir = path.dirname(__filename);
26 exports.fixturesDir = path.join(exports.testDir, 'fixtures');
27 exports.libDir = path.join(exports.testDir, '../lib');
28 exports.tmpDir = path.join(exports.testDir, 'tmp');
29 exports.PORT = 12346;
30 exports.PROXY_PORT = 1234567;
31
32 if (process.platform === 'win32') {
33   exports.PIPE = '\\\\.\\pipe\\libuv-test';
34 } else {
35   exports.PIPE = exports.tmpDir + '/test.sock';
36 }
37
38 var util = require('util');
39 for (var i in util) exports[i] = util[i];
40 //for (var i in exports) global[i] = exports[i];
41
42 function protoCtrChain(o) {
43   var result = [];
44   for (; o; o = o.__proto__) { result.push(o.constructor); }
45   return result.join();
46 }
47
48 exports.indirectInstanceOf = function (obj, cls) {
49   if (obj instanceof cls) { return true; }
50   var clsChain = protoCtrChain(cls.prototype);
51   var objChain = protoCtrChain(obj);
52   return objChain.slice(-clsChain.length) === clsChain;
53 };
54
55
56 exports.ddCommand = function (filename, kilobytes) {
57   if (process.platform === 'win32') {
58     var p = path.resolve(exports.fixturesDir, 'create-file.js');
59     return '"' + process.argv[0] + '" "' + p + '" "' +
60            filename + '" ' + (kilobytes * 1024);
61   } else {
62     return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes;
63   }
64 };
65
66
67 exports.spawnPwd = function (options) {
68   var spawn = require('child_process').spawn;
69
70   if (process.platform === 'win32') {
71     return spawn('cmd.exe', ['/c', 'cd'], options);
72   } else {
73     return spawn('pwd', [], options);
74   }
75 };
76
77
78 // Turn this off if the test should not check for global leaks.
79 exports.globalCheck = true;
80
81 process.on('exit', function () {
82   if (!exports.globalCheck) return;
83   var knownGlobals = [setTimeout,
84                       setInterval,
85                       clearTimeout,
86                       clearInterval,
87                       console,
88                       Buffer,
89                       process,
90                       global];
91
92   if (global.setImmediate) {
93     knownGlobals.push(setImmediate);
94     knownGlobals.push(clearImmediate);
95   }
96
97   if (global.errno) {
98     knownGlobals.push(errno);
99   }
100
101   if (global.gc) {
102     knownGlobals.push(gc);
103   }
104
105   if (global.DTRACE_HTTP_SERVER_RESPONSE) {
106     knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
107     knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
108     knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
109     knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
110     knownGlobals.push(DTRACE_NET_STREAM_END);
111     knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
112     knownGlobals.push(DTRACE_NET_SOCKET_READ);
113     knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
114   }
115
116   if (global.ArrayBuffer) {
117     knownGlobals.push(ArrayBuffer);
118     knownGlobals.push(Int8Array);
119     knownGlobals.push(Uint8Array);
120     knownGlobals.push(Int16Array);
121     knownGlobals.push(Uint16Array);
122     knownGlobals.push(Int32Array);
123     knownGlobals.push(Uint32Array);
124     knownGlobals.push(Float32Array);
125     knownGlobals.push(Float64Array);
126     knownGlobals.push(DataView);
127
128     if (global.Uint8ClampedArray) {
129       knownGlobals.push(Uint8ClampedArray);
130     }
131   }
132
133   for (var x in global) {
134     var found = false;
135
136     for (var y in knownGlobals) {
137       if (global[x] === knownGlobals[y]) {
138         found = true;
139         break;
140       }
141     }
142
143     if (!found) {
144       console.error('Unknown global: %s', x);
145       assert.ok(false, 'Unknown global found');
146     }
147   }
148 });
149
150
151 var mustCallChecks = [];
152
153
154 function runCallChecks() {
155   var failed = mustCallChecks.filter(function (context) {
156     return context.actual !== context.expected;
157   });
158
159   failed.forEach(function (context) {
160     console.log('Mismatched %s function calls. Expected %d, actual %d.',
161                 context.name,
162                 context.expected,
163                 context.actual);
164     console.log(context.stack.split('\n').slice(2).join('\n'));
165   });
166
167   if (failed.length) process.exit(1);
168 }
169
170
171 exports.mustCall = function (fn, expected) {
172   if (typeof expected !== 'number') expected = 1;
173
174   var context = {
175     expected: expected,
176     actual: 0,
177     stack: (new Error).stack,
178     name: fn.name || '<anonymous>'
179   };
180
181   // add the exit listener only once to avoid listener leak warnings
182   if (mustCallChecks.length === 0) process.on('exit', runCallChecks);
183
184   mustCallChecks.push(context);
185
186   return function () {
187     context.actual++;
188     return fn.apply(this, arguments);
189   };
190 };