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 | |
Ryan Dahl | a0159b4 | 2010-12-04 23:20:34 | [diff] [blame] | 22 | var common = require('../common'); |
| 23 | var assert = require('assert'); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 24 | |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 25 | common.globalCheck = false; |
| 26 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 27 | var net = require('net'), |
| 28 | repl = require('repl'), |
| 29 | message = 'Read, Eval, Print Loop', |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 30 | prompt_unix = 'node via Unix socket> ', |
| 31 | prompt_tcp = 'node via TCP socket> ', |
| 32 | prompt_multiline = '... ', |
isaacs | 28e851c | 2012-06-05 19:02:37 | [diff] [blame] | 33 | prompt_npm = 'npm should be run outside of the ' + |
| 34 | 'node repl, in your normal shell.\n' + |
| 35 | '(Press Control-D to exit.)\n', |
| 36 | expect_npm = prompt_npm + prompt_unix, |
Fedor Indutny | 1a52d6a | 2014-07-31 08:12:18 | [diff] [blame^] | 37 | server_tcp, server_unix, client_tcp, client_unix, timer; |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 38 | |
Ryan Dahl | 03a119e | 2011-08-09 23:06:32 | [diff] [blame] | 39 | |
Ryan Dahl | 4962702 | 2010-09-19 18:20:25 | [diff] [blame] | 40 | // absolute path to test/fixtures/a.js |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 41 | var moduleFilename = require('path').join(common.fixturesDir, 'a'); |
Ryan Dahl | 4962702 | 2010-09-19 18:20:25 | [diff] [blame] | 42 | |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 43 | console.error('repl test'); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 44 | |
| 45 | // function for REPL to run |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 46 | invoke_me = function(arg) { |
| 47 | return 'invoked ' + arg; |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | function send_expect(list) { |
| 51 | if (list.length > 0) { |
| 52 | var cur = list.shift(); |
| 53 | |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 54 | console.error('sending ' + JSON.stringify(cur.send)); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 55 | |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 56 | cur.client.expect = cur.expect; |
| 57 | cur.client.list = list; |
| 58 | if (cur.send.length > 0) { |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 59 | cur.client.write(cur.send + '\n'); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 64 | function clean_up() { |
| 65 | client_tcp.end(); |
| 66 | client_unix.end(); |
| 67 | clearTimeout(timer); |
| 68 | } |
| 69 | |
| 70 | function error_test() { |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 71 | // The other stuff is done so reuse unix socket |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 72 | var read_buffer = ''; |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 73 | client_unix.removeAllListeners('data'); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 74 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 75 | client_unix.on('data', function(data) { |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 76 | read_buffer += data.toString('ascii', 0, data.length); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 77 | console.error('Unix data: ' + JSON.stringify(read_buffer) + ', expecting ' + |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 78 | (client_unix.expect.exec ? |
| 79 | client_unix.expect : |
| 80 | JSON.stringify(client_unix.expect))); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 81 | |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 82 | if (read_buffer.indexOf(prompt_unix) !== -1) { |
isaacs | 28e851c | 2012-06-05 19:02:37 | [diff] [blame] | 83 | // if it's an exact match, then don't do the regexp |
| 84 | if (read_buffer !== client_unix.expect) { |
Yazhong Liu | 613654e | 2014-07-01 13:35:35 | [diff] [blame] | 85 | var expect = client_unix.expect; |
| 86 | if (expect === prompt_multiline) |
| 87 | expect = /[\.]{3} /; |
| 88 | assert.ok(read_buffer.match(expect)); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 89 | console.error('match'); |
isaacs | 28e851c | 2012-06-05 19:02:37 | [diff] [blame] | 90 | } |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 91 | read_buffer = ''; |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 92 | if (client_unix.list && client_unix.list.length > 0) { |
| 93 | send_expect(client_unix.list); |
| 94 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 95 | console.error('End of Error test, running TCP test.'); |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 96 | tcp_test(); |
| 97 | } |
| 98 | |
seebees | 3421f43 | 2011-11-12 01:44:39 | [diff] [blame] | 99 | } else if (read_buffer.indexOf(prompt_multiline) !== -1) { |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 100 | // Check that you meant to send a multiline test |
| 101 | assert.strictEqual(prompt_multiline, client_unix.expect); |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 102 | read_buffer = ''; |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 103 | if (client_unix.list && client_unix.list.length > 0) { |
| 104 | send_expect(client_unix.list); |
| 105 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 106 | console.error('End of Error test, running TCP test.\n'); |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 107 | tcp_test(); |
| 108 | } |
| 109 | |
| 110 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 111 | console.error('didn\'t see prompt yet, buffering.'); |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 112 | } |
| 113 | }); |
| 114 | |
| 115 | send_expect([ |
| 116 | // Uncaught error throws and prints out |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 117 | { client: client_unix, send: 'throw new Error(\'test error\');', |
| 118 | expect: /^Error: test error/ }, |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 119 | // Common syntax error is treated as multiline command |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 120 | { client: client_unix, send: 'function test_func() {', |
| 121 | expect: prompt_multiline }, |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 122 | // You can recover with the .break command |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 123 | { client: client_unix, send: '.break', |
| 124 | expect: prompt_unix }, |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 125 | // But passing the same string to eval() should throw |
| 126 | { client: client_unix, send: 'eval("function test_func() {")', |
| 127 | expect: /^SyntaxError: Unexpected end of input/ }, |
Ben Noordhuis | b6e9897 | 2012-11-10 17:21:13 | [diff] [blame] | 128 | // Floating point numbers are not interpreted as REPL commands. |
| 129 | { client: client_unix, send: '.1234', |
| 130 | expect: '0.1234' }, |
Nirk Niggler | 0459a23 | 2013-01-03 14:27:55 | [diff] [blame] | 131 | // Floating point expressions are not interpreted as REPL commands |
Ben Noordhuis | 8a65df9 | 2013-07-10 07:48:55 | [diff] [blame] | 132 | { client: client_unix, send: '.1+.1', |
Nirk Niggler | 0459a23 | 2013-01-03 14:27:55 | [diff] [blame] | 133 | expect: '0.2' }, |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 134 | // Can parse valid JSON |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 135 | { client: client_unix, send: 'JSON.parse(\'{"valid": "json"}\');', |
| 136 | expect: '{ valid: \'json\' }'}, |
| 137 | // invalid input to JSON.parse error is special case of syntax error, |
| 138 | // should throw |
| 139 | { client: client_unix, send: 'JSON.parse(\'{invalid: \\\'json\\\'}\');', |
koichik | 8faf941 | 2011-07-04 19:10:14 | [diff] [blame] | 140 | expect: /^SyntaxError: Unexpected token i/ }, |
Nathan Rajlich | 3b7312d | 2012-10-01 18:36:06 | [diff] [blame] | 141 | // end of input to JSON.parse error is special case of syntax error, |
| 142 | // should throw |
Brian White | 774b28f | 2013-05-26 16:26:39 | [diff] [blame] | 143 | { client: client_unix, send: 'JSON.parse(\'066\');', |
| 144 | expect: /^SyntaxError: Unexpected number/ }, |
| 145 | // should throw |
Nathan Rajlich | 3b7312d | 2012-10-01 18:36:06 | [diff] [blame] | 146 | { client: client_unix, send: 'JSON.parse(\'{\');', |
| 147 | expect: /^SyntaxError: Unexpected end of input/ }, |
Nathan Rajlich | 4a26707 | 2012-09-22 01:46:16 | [diff] [blame] | 148 | // invalid RegExps are a special case of syntax error, |
| 149 | // should throw |
| 150 | { client: client_unix, send: '/(/;', |
| 151 | expect: /^SyntaxError: Invalid regular expression\:/ }, |
Nathan Rajlich | f1722a2 | 2012-10-01 05:43:35 | [diff] [blame] | 152 | // invalid RegExp modifiers are a special case of syntax error, |
| 153 | // should throw (GH-4012) |
| 154 | { client: client_unix, send: 'new RegExp("foo", "wrong modifier");', |
| 155 | expect: /^SyntaxError: Invalid flags supplied to RegExp constructor/ }, |
Nathan Rajlich | 085f9d6 | 2013-03-30 20:10:30 | [diff] [blame] | 156 | // strict mode syntax errors should be caught (GH-5178) |
| 157 | { client: client_unix, send: '(function() { "use strict"; return 0755; })()', |
| 158 | expect: /^SyntaxError: Octal literals are not allowed in strict mode/ }, |
| 159 | { client: client_unix, send: '(function() { "use strict"; return { p: 1, p: 2 }; })()', |
| 160 | expect: /^SyntaxError: Duplicate data property in object literal not allowed in strict mode/ }, |
| 161 | { client: client_unix, send: '(function(a, a, b) { "use strict"; return a + b + c; })()', |
| 162 | expect: /^SyntaxError: Strict mode function may not have duplicate parameter names/ }, |
| 163 | { client: client_unix, send: '(function() { "use strict"; with (this) {} })()', |
| 164 | expect: /^SyntaxError: Strict mode code may not include a with statement/ }, |
| 165 | { client: client_unix, send: '(function() { "use strict"; var x; delete x; })()', |
| 166 | expect: /^SyntaxError: Delete of an unqualified identifier in strict mode/ }, |
| 167 | { client: client_unix, send: '(function() { "use strict"; eval = 17; })()', |
Fedor Indutny | ce04c72 | 2014-03-13 16:38:14 | [diff] [blame] | 168 | expect: /^SyntaxError: Unexpected eval or arguments in strict mode/ }, |
Nathan Rajlich | 085f9d6 | 2013-03-30 20:10:30 | [diff] [blame] | 169 | { client: client_unix, send: '(function() { "use strict"; if (true){ function f() { } } })()', |
| 170 | expect: /^SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function/ }, |
Ryan Dahl | feb77ea | 2011-01-03 02:08:08 | [diff] [blame] | 171 | // Named functions can be used: |
| 172 | { client: client_unix, send: 'function blah() { return 1; }', |
| 173 | expect: prompt_unix }, |
| 174 | { client: client_unix, send: 'blah()', |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 175 | expect: '1\n' + prompt_unix }, |
Fedor Indutny | ae5e233 | 2012-02-17 18:18:11 | [diff] [blame] | 176 | // Functions should not evaluate twice (#2773) |
| 177 | { client: client_unix, send: 'var I = [1,2,3,function() {}]; I.pop()', |
| 178 | expect: '[Function]' }, |
Ryan Dahl | feb77ea | 2011-01-03 02:08:08 | [diff] [blame] | 179 | // Multiline object |
| 180 | { client: client_unix, send: '{ a: ', |
| 181 | expect: prompt_multiline }, |
| 182 | { client: client_unix, send: '1 }', |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 183 | expect: '{ a: 1 }' }, |
Ryan Dahl | feb77ea | 2011-01-03 02:08:08 | [diff] [blame] | 184 | // Multiline anonymous function with comment |
| 185 | { client: client_unix, send: '(function () {', |
| 186 | expect: prompt_multiline }, |
| 187 | { client: client_unix, send: '// blah', |
| 188 | expect: prompt_multiline }, |
| 189 | { client: client_unix, send: 'return 1;', |
| 190 | expect: prompt_multiline }, |
| 191 | { client: client_unix, send: '})()', |
isaacs | 28e851c | 2012-06-05 19:02:37 | [diff] [blame] | 192 | expect: '1' }, |
| 193 | // npm prompt error message |
| 194 | { client: client_unix, send: 'npm install foobar', |
Maciej Małecki | 6a11f3e | 2012-07-03 02:13:24 | [diff] [blame] | 195 | expect: expect_npm }, |
| 196 | { client: client_unix, send: '(function () {\n\nreturn 1;\n})()', |
Nathan Rajlich | 9126dd2 | 2012-07-04 18:51:24 | [diff] [blame] | 197 | expect: '1' }, |
| 198 | { client: client_unix, send: '{\n\na: 1\n}', |
Felix Böhm | 9bce5e8 | 2013-01-12 20:07:06 | [diff] [blame] | 199 | expect: '{ a: 1 }' }, |
| 200 | { client: client_unix, send: 'url.format("http://google.com")', |
| 201 | expect: 'http://google.com/' }, |
| 202 | { client: client_unix, send: 'var path = 42; path', |
| 203 | expect: '42' } |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 204 | ]); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 205 | } |
| 206 | |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 207 | function tcp_test() { |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 208 | server_tcp = net.createServer(function(socket) { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 209 | assert.strictEqual(server_tcp, socket.server); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 210 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 211 | socket.on('end', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 212 | socket.end(); |
| 213 | }); |
| 214 | |
| 215 | repl.start(prompt_tcp, socket); |
| 216 | }); |
| 217 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 218 | server_tcp.listen(common.PORT, function() { |
| 219 | var read_buffer = ''; |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 220 | |
Ryan Dahl | 9fd5e3c | 2010-07-15 18:47:25 | [diff] [blame] | 221 | client_tcp = net.createConnection(common.PORT); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 222 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 223 | client_tcp.on('connect', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 224 | assert.equal(true, client_tcp.readable); |
| 225 | assert.equal(true, client_tcp.writable); |
| 226 | |
| 227 | send_expect([ |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 228 | { client: client_tcp, send: '', |
| 229 | expect: prompt_tcp }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 230 | { client: client_tcp, send: 'invoke_me(333)', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 231 | expect: ('\'' + 'invoked 333' + '\'\n' + prompt_tcp) }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 232 | { client: client_tcp, send: 'a += 1', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 233 | expect: ('12346' + '\n' + prompt_tcp) }, |
| 234 | { client: client_tcp, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 235 | send: 'require(' + JSON.stringify(moduleFilename) + ').number', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 236 | expect: ('42' + '\n' + prompt_tcp) } |
| 237 | ]); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 238 | }); |
| 239 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 240 | client_tcp.on('data', function(data) { |
Ryan Dahl | 4fe5e86 | 2010-09-06 19:12:36 | [diff] [blame] | 241 | read_buffer += data.toString('ascii', 0, data.length); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 242 | console.error('TCP data: ' + JSON.stringify(read_buffer) + |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 243 | ', expecting ' + JSON.stringify(client_tcp.expect)); |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 244 | if (read_buffer.indexOf(prompt_tcp) !== -1) { |
| 245 | assert.strictEqual(client_tcp.expect, read_buffer); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 246 | console.error('match'); |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 247 | read_buffer = ''; |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 248 | if (client_tcp.list && client_tcp.list.length > 0) { |
| 249 | send_expect(client_tcp.list); |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 250 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 251 | console.error('End of TCP test.\n'); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 252 | clean_up(); |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 253 | } |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 254 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 255 | console.error('didn\'t see prompt yet, buffering'); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 256 | } |
| 257 | }); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 258 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 259 | client_tcp.on('error', function(e) { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 260 | throw e; |
| 261 | }); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 262 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 263 | client_tcp.on('close', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 264 | server_tcp.close(); |
| 265 | }); |
| 266 | }); |
| 267 | |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | function unix_test() { |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 271 | server_unix = net.createServer(function(socket) { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 272 | assert.strictEqual(server_unix, socket.server); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 273 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 274 | socket.on('end', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 275 | socket.end(); |
| 276 | }); |
| 277 | |
Fedor Indutny | 1a52d6a | 2014-07-31 08:12:18 | [diff] [blame^] | 278 | repl.start({ |
Nathan Rajlich | b1e78ce | 2012-10-12 23:34:36 | [diff] [blame] | 279 | prompt: prompt_unix, |
| 280 | input: socket, |
| 281 | output: socket, |
| 282 | useGlobal: true |
Fedor Indutny | 1a52d6a | 2014-07-31 08:12:18 | [diff] [blame^] | 283 | }).context.message = message; |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 284 | }); |
| 285 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 286 | server_unix.on('listening', function() { |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 287 | var read_buffer = ''; |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 288 | |
Ryan Dahl | 03a119e | 2011-08-09 23:06:32 | [diff] [blame] | 289 | client_unix = net.createConnection(common.PIPE); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 290 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 291 | client_unix.on('connect', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 292 | assert.equal(true, client_unix.readable); |
| 293 | assert.equal(true, client_unix.writable); |
| 294 | |
| 295 | send_expect([ |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 296 | { client: client_unix, send: '', |
| 297 | expect: prompt_unix }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 298 | { client: client_unix, send: 'message', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 299 | expect: ('\'' + message + '\'\n' + prompt_unix) }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 300 | { client: client_unix, send: 'invoke_me(987)', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 301 | expect: ('\'' + 'invoked 987' + '\'\n' + prompt_unix) }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 302 | { client: client_unix, send: 'a = 12345', |
Ryan Dahl | 8b352bd | 2011-01-02 05:14:06 | [diff] [blame] | 303 | expect: ('12345' + '\n' + prompt_unix) }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 304 | { client: client_unix, send: '{a:1}', |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 305 | expect: ('{ a: 1 }' + '\n' + prompt_unix) } |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 306 | ]); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 307 | }); |
| 308 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 309 | client_unix.on('data', function(data) { |
Ryan Dahl | 4fe5e86 | 2010-09-06 19:12:36 | [diff] [blame] | 310 | read_buffer += data.toString('ascii', 0, data.length); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 311 | console.error('Unix data: ' + JSON.stringify(read_buffer) + |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 312 | ', expecting ' + JSON.stringify(client_unix.expect)); |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 313 | if (read_buffer.indexOf(prompt_unix) !== -1) { |
| 314 | assert.strictEqual(client_unix.expect, read_buffer); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 315 | console.error('match'); |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 316 | read_buffer = ''; |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 317 | if (client_unix.list && client_unix.list.length > 0) { |
| 318 | send_expect(client_unix.list); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 319 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 320 | console.error('End of Unix test, running Error test.\n'); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 321 | process.nextTick(error_test); |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 322 | } |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 323 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 324 | console.error('didn\'t see prompt yet, buffering.'); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 325 | } |
| 326 | }); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 327 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 328 | client_unix.on('error', function(e) { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 329 | throw e; |
| 330 | }); |
| 331 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 332 | client_unix.on('close', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 333 | server_unix.close(); |
| 334 | }); |
| 335 | }); |
| 336 | |
Ryan Dahl | 03a119e | 2011-08-09 23:06:32 | [diff] [blame] | 337 | server_unix.listen(common.PIPE); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | unix_test(); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 341 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 342 | timer = setTimeout(function() { |
| 343 | assert.fail('Timeout'); |
Ryan Dahl | 129217a | 2011-01-26 01:35:06 | [diff] [blame] | 344 | }, 5000); |