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'; 23 24// Check that the ticket from the first connection causes session resumption 25// when used to make a second connection. 26 27const common = require('../common'); 28if (!common.hasCrypto) 29 common.skip('missing crypto'); 30 31const assert = require('assert'); 32const tls = require('tls'); 33const fixtures = require('../common/fixtures'); 34 35const options = { 36 key: fixtures.readKey('agent2-key.pem'), 37 cert: fixtures.readKey('agent2-cert.pem') 38}; 39 40// create server 41const server = tls.Server(options, common.mustCall((socket) => { 42 socket.end('Goodbye'); 43}, 2)); 44 45// start listening 46server.listen(0, common.mustCall(function() { 47 let sessionx = null; // From right after connect, invalid for TLS1.3 48 let session1 = null; // Delivered by the session event, always valid. 49 let sessions = 0; 50 let tls13; 51 const client1 = tls.connect({ 52 port: this.address().port, 53 rejectUnauthorized: false 54 }, common.mustCall(() => { 55 tls13 = client1.getProtocol() === 'TLSv1.3'; 56 assert.strictEqual(client1.isSessionReused(), false); 57 sessionx = client1.getSession(); 58 assert(sessionx); 59 60 if (session1) 61 reconnect(); 62 })); 63 64 client1.on('data', common.mustCall((d) => { 65 })); 66 67 client1.once('session', common.mustCall((session) => { 68 console.log('session1'); 69 session1 = session; 70 assert(session1); 71 if (sessionx) 72 reconnect(); 73 })); 74 75 client1.on('session', () => { 76 console.log('client1 session#', ++sessions); 77 }); 78 79 client1.on('close', () => { 80 console.log('client1 close'); 81 assert.strictEqual(sessions, tls13 ? 2 : 1); 82 }); 83 84 function reconnect() { 85 assert(sessionx); 86 assert(session1); 87 if (tls13) 88 // For TLS1.3, the session immediately after handshake is a dummy, 89 // unresumable session. The one delivered later in session event is 90 // resumable. 91 assert.notStrictEqual(sessionx.compare(session1), 0); 92 else 93 // For TLS1.2, they are identical. 94 assert.strictEqual(sessionx.compare(session1), 0); 95 96 const opts = { 97 port: server.address().port, 98 rejectUnauthorized: false, 99 session: session1, 100 }; 101 102 const client2 = tls.connect(opts, common.mustCall(() => { 103 console.log('connect2'); 104 assert.strictEqual(client2.isSessionReused(), true); 105 })); 106 107 client2.on('close', common.mustCall(() => { 108 console.log('close2'); 109 server.close(); 110 })); 111 112 client2.resume(); 113 } 114 115 client1.resume(); 116})); 117