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/* eslint-disable strict */ 23require('../common'); 24const assert = require('assert'); 25const net = require('net'); 26 27let binaryString = ''; 28for (let i = 255; i >= 0; i--) { 29 const s = `'\\${i.toString(8)}'`; 30 const S = eval(s); 31 assert.strictEqual(S.charCodeAt(0), i); 32 assert.strictEqual(S, String.fromCharCode(i)); 33 binaryString += S; 34} 35 36// safe constructor 37const echoServer = net.Server(function(connection) { 38 connection.setEncoding('latin1'); 39 connection.on('data', function(chunk) { 40 connection.write(chunk, 'latin1'); 41 }); 42 connection.on('end', function() { 43 connection.end(); 44 }); 45}); 46echoServer.listen(0); 47 48let recv = ''; 49 50echoServer.on('listening', function() { 51 let j = 0; 52 const c = net.createConnection({ 53 port: this.address().port 54 }); 55 56 c.setEncoding('latin1'); 57 c.on('data', function(chunk) { 58 const n = j + chunk.length; 59 while (j < n && j < 256) { 60 c.write(String.fromCharCode(j), 'latin1'); 61 j++; 62 } 63 if (j === 256) { 64 c.end(); 65 } 66 recv += chunk; 67 }); 68 69 c.on('connect', function() { 70 c.write(binaryString, 'binary'); 71 }); 72 73 c.on('close', function() { 74 echoServer.close(); 75 }); 76}); 77 78process.on('exit', function() { 79 assert.strictEqual(recv.length, 2 * 256); 80 81 const a = recv.split(''); 82 83 const first = a.slice(0, 256).reverse().join(''); 84 85 const second = a.slice(256, 2 * 256).join(''); 86 87 assert.strictEqual(first, second); 88}); 89