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'use strict'; 23const common = require('../common'); 24 25if (!common.hasCrypto) 26 common.skip('missing crypto'); 27 28const assert = require('assert'); 29const tls = require('tls'); 30 31const fixtures = require('../common/fixtures'); 32 33const options = { key: fixtures.readKey('rsa_private.pem'), 34 cert: fixtures.readKey('rsa_cert.crt'), 35 ca: [ fixtures.readKey('rsa_ca.crt') ] }; 36 37const writes = [ 38 'some server data', 39 'and a separate packet', 40 'and one more', 41]; 42let receivedWrites = 0; 43 44const server = tls.createServer(options, function(c) { 45 c.resume(); 46 writes.forEach(function(str) { 47 c.write(str); 48 }); 49}).listen(0, common.mustCall(function() { 50 const connectOpts = { rejectUnauthorized: false }; 51 const c = tls.connect(this.address().port, connectOpts, function() { 52 c.write('some client data'); 53 c.on('readable', function() { 54 let data = c.read(); 55 if (data === null) 56 return; 57 58 data = data.toString(); 59 while (data.length !== 0) { 60 assert(data.startsWith(writes[receivedWrites])); 61 data = data.slice(writes[receivedWrites].length); 62 63 if (++receivedWrites === writes.length) { 64 c.end(); 65 server.close(); 66 } 67 } 68 }); 69 }); 70})); 71 72 73process.on('exit', function() { 74 assert.strictEqual(receivedWrites, writes.length); 75}); 76