1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4 5if (!common.hasCrypto) 6 common.skip('missing crypto'); 7 8const https = require('https'); 9const crypto = require('crypto'); 10const fixtures = require('../common/fixtures'); 11 12const options = { 13 key: fixtures.readKey('agent1-key.pem'), 14 cert: fixtures.readKey('agent1-cert.pem') 15}; 16 17const ca = fixtures.readKey('ca1-cert.pem'); 18 19const clientSessions = {}; 20let serverRequests = 0; 21 22const agent = new https.Agent({ 23 maxCachedSessions: 1 24}); 25 26const server = https.createServer(options, function(req, res) { 27 if (req.url === '/drop-key') 28 server.setTicketKeys(crypto.randomBytes(48)); 29 30 serverRequests++; 31 res.end('ok'); 32}).listen(0, function() { 33 const queue = [ 34 { 35 name: 'first', 36 37 method: 'GET', 38 path: '/', 39 servername: 'agent1', 40 ca: ca, 41 port: this.address().port 42 }, 43 { 44 name: 'first-reuse', 45 46 method: 'GET', 47 path: '/', 48 servername: 'agent1', 49 ca: ca, 50 port: this.address().port 51 }, 52 { 53 name: 'cipher-change', 54 55 method: 'GET', 56 path: '/', 57 servername: 'agent1', 58 59 // Choose different cipher to use different cache entry 60 ciphers: 'AES256-SHA', 61 ca: ca, 62 port: this.address().port 63 }, 64 // Change the ticket key to ensure session is updated in cache 65 { 66 name: 'before-drop', 67 68 method: 'GET', 69 path: '/drop-key', 70 servername: 'agent1', 71 ca: ca, 72 port: this.address().port 73 }, 74 75 // Ticket will be updated starting from this 76 { 77 name: 'after-drop', 78 79 method: 'GET', 80 path: '/', 81 servername: 'agent1', 82 ca: ca, 83 port: this.address().port 84 }, 85 { 86 name: 'after-drop-reuse', 87 88 method: 'GET', 89 path: '/', 90 servername: 'agent1', 91 ca: ca, 92 port: this.address().port 93 }, 94 ]; 95 96 function request() { 97 const options = queue.shift(); 98 options.agent = agent; 99 https.request(options, function(res) { 100 clientSessions[options.name] = res.socket.getSession(); 101 102 res.resume(); 103 res.on('end', function() { 104 if (queue.length !== 0) 105 return request(); 106 server.close(); 107 }); 108 }).end(); 109 } 110 request(); 111}); 112 113process.on('exit', function() { 114 assert.strictEqual(serverRequests, 6); 115 assert.strictEqual(clientSessions.first.toString('hex'), 116 clientSessions['first-reuse'].toString('hex')); 117 assert.notStrictEqual(clientSessions.first.toString('hex'), 118 clientSessions['cipher-change'].toString('hex')); 119 assert.notStrictEqual(clientSessions.first.toString('hex'), 120 clientSessions['before-drop'].toString('hex')); 121 assert.notStrictEqual(clientSessions['cipher-change'].toString('hex'), 122 clientSessions['before-drop'].toString('hex')); 123 assert.notStrictEqual(clientSessions['before-drop'].toString('hex'), 124 clientSessions['after-drop'].toString('hex')); 125 assert.strictEqual(clientSessions['after-drop'].toString('hex'), 126 clientSessions['after-drop-reuse'].toString('hex')); 127}); 128