11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst https = require('https');
91cb0ef41Sopenharmony_ciconst crypto = require('crypto');
101cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst options = {
131cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
141cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
151cb0ef41Sopenharmony_ci};
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst ca = fixtures.readKey('ca1-cert.pem');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst clientSessions = {};
201cb0ef41Sopenharmony_cilet serverRequests = 0;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst agent = new https.Agent({
231cb0ef41Sopenharmony_ci  maxCachedSessions: 1
241cb0ef41Sopenharmony_ci});
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst server = https.createServer(options, function(req, res) {
271cb0ef41Sopenharmony_ci  if (req.url === '/drop-key')
281cb0ef41Sopenharmony_ci    server.setTicketKeys(crypto.randomBytes(48));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  serverRequests++;
311cb0ef41Sopenharmony_ci  res.end('ok');
321cb0ef41Sopenharmony_ci}).listen(0, function() {
331cb0ef41Sopenharmony_ci  const queue = [
341cb0ef41Sopenharmony_ci    {
351cb0ef41Sopenharmony_ci      name: 'first',
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci      method: 'GET',
381cb0ef41Sopenharmony_ci      path: '/',
391cb0ef41Sopenharmony_ci      servername: 'agent1',
401cb0ef41Sopenharmony_ci      ca: ca,
411cb0ef41Sopenharmony_ci      port: this.address().port
421cb0ef41Sopenharmony_ci    },
431cb0ef41Sopenharmony_ci    {
441cb0ef41Sopenharmony_ci      name: 'first-reuse',
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci      method: 'GET',
471cb0ef41Sopenharmony_ci      path: '/',
481cb0ef41Sopenharmony_ci      servername: 'agent1',
491cb0ef41Sopenharmony_ci      ca: ca,
501cb0ef41Sopenharmony_ci      port: this.address().port
511cb0ef41Sopenharmony_ci    },
521cb0ef41Sopenharmony_ci    {
531cb0ef41Sopenharmony_ci      name: 'cipher-change',
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci      method: 'GET',
561cb0ef41Sopenharmony_ci      path: '/',
571cb0ef41Sopenharmony_ci      servername: 'agent1',
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci      // Choose different cipher to use different cache entry
601cb0ef41Sopenharmony_ci      ciphers: 'AES256-SHA',
611cb0ef41Sopenharmony_ci      ca: ca,
621cb0ef41Sopenharmony_ci      port: this.address().port
631cb0ef41Sopenharmony_ci    },
641cb0ef41Sopenharmony_ci    // Change the ticket key to ensure session is updated in cache
651cb0ef41Sopenharmony_ci    {
661cb0ef41Sopenharmony_ci      name: 'before-drop',
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci      method: 'GET',
691cb0ef41Sopenharmony_ci      path: '/drop-key',
701cb0ef41Sopenharmony_ci      servername: 'agent1',
711cb0ef41Sopenharmony_ci      ca: ca,
721cb0ef41Sopenharmony_ci      port: this.address().port
731cb0ef41Sopenharmony_ci    },
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci    // Ticket will be updated starting from this
761cb0ef41Sopenharmony_ci    {
771cb0ef41Sopenharmony_ci      name: 'after-drop',
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci      method: 'GET',
801cb0ef41Sopenharmony_ci      path: '/',
811cb0ef41Sopenharmony_ci      servername: 'agent1',
821cb0ef41Sopenharmony_ci      ca: ca,
831cb0ef41Sopenharmony_ci      port: this.address().port
841cb0ef41Sopenharmony_ci    },
851cb0ef41Sopenharmony_ci    {
861cb0ef41Sopenharmony_ci      name: 'after-drop-reuse',
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci      method: 'GET',
891cb0ef41Sopenharmony_ci      path: '/',
901cb0ef41Sopenharmony_ci      servername: 'agent1',
911cb0ef41Sopenharmony_ci      ca: ca,
921cb0ef41Sopenharmony_ci      port: this.address().port
931cb0ef41Sopenharmony_ci    },
941cb0ef41Sopenharmony_ci  ];
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  function request() {
971cb0ef41Sopenharmony_ci    const options = queue.shift();
981cb0ef41Sopenharmony_ci    options.agent = agent;
991cb0ef41Sopenharmony_ci    https.request(options, function(res) {
1001cb0ef41Sopenharmony_ci      clientSessions[options.name] = res.socket.getSession();
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci      res.resume();
1031cb0ef41Sopenharmony_ci      res.on('end', function() {
1041cb0ef41Sopenharmony_ci        if (queue.length !== 0)
1051cb0ef41Sopenharmony_ci          return request();
1061cb0ef41Sopenharmony_ci        server.close();
1071cb0ef41Sopenharmony_ci      });
1081cb0ef41Sopenharmony_ci    }).end();
1091cb0ef41Sopenharmony_ci  }
1101cb0ef41Sopenharmony_ci  request();
1111cb0ef41Sopenharmony_ci});
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ciprocess.on('exit', function() {
1141cb0ef41Sopenharmony_ci  assert.strictEqual(serverRequests, 6);
1151cb0ef41Sopenharmony_ci  assert.strictEqual(clientSessions.first.toString('hex'),
1161cb0ef41Sopenharmony_ci                     clientSessions['first-reuse'].toString('hex'));
1171cb0ef41Sopenharmony_ci  assert.notStrictEqual(clientSessions.first.toString('hex'),
1181cb0ef41Sopenharmony_ci                        clientSessions['cipher-change'].toString('hex'));
1191cb0ef41Sopenharmony_ci  assert.notStrictEqual(clientSessions.first.toString('hex'),
1201cb0ef41Sopenharmony_ci                        clientSessions['before-drop'].toString('hex'));
1211cb0ef41Sopenharmony_ci  assert.notStrictEqual(clientSessions['cipher-change'].toString('hex'),
1221cb0ef41Sopenharmony_ci                        clientSessions['before-drop'].toString('hex'));
1231cb0ef41Sopenharmony_ci  assert.notStrictEqual(clientSessions['before-drop'].toString('hex'),
1241cb0ef41Sopenharmony_ci                        clientSessions['after-drop'].toString('hex'));
1251cb0ef41Sopenharmony_ci  assert.strictEqual(clientSessions['after-drop'].toString('hex'),
1261cb0ef41Sopenharmony_ci                     clientSessions['after-drop-reuse'].toString('hex'));
1271cb0ef41Sopenharmony_ci});
128