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