1'use strict';
2const common = require('../common');
3if (!common.hasCrypto)
4  common.skip('missing crypto');
5
6const assert = require('assert');
7const crypto = require('crypto');
8const https = require('https');
9const fixtures = require('../common/fixtures');
10
11const options = {
12  key: fixtures.readKey('agent1-key.pem'),
13  cert: fixtures.readKey('agent1-cert.pem'),
14  ca: fixtures.readKey('ca1-cert.pem'),
15  minVersion: 'TLSv1.1',
16  ciphers: 'ALL@SECLEVEL=0'
17};
18
19const server = https.Server(options, (req, res) => {
20  res.writeHead(200);
21  res.end('hello world\n');
22});
23
24function getBaseOptions(port) {
25  return {
26    path: '/',
27    port: port,
28    ca: options.ca,
29    rejectUnauthorized: true,
30    servername: 'agent1',
31    ciphers: 'ALL@SECLEVEL=0'
32  };
33}
34
35const updatedValues = new Map([
36  ['dhparam', fixtures.readKey('dh2048.pem')],
37  ['ecdhCurve', 'secp384r1'],
38  ['honorCipherOrder', true],
39  ['minVersion', 'TLSv1.1'],
40  ['maxVersion', 'TLSv1.3'],
41  ['secureOptions', crypto.constants.SSL_OP_CIPHER_SERVER_PREFERENCE],
42  ['secureProtocol', 'TLSv1_1_method'],
43  ['sessionIdContext', 'sessionIdContext'],
44]);
45
46let value;
47function variations(iter, port, cb) {
48  return common.mustCall((res) => {
49    res.resume();
50    https.globalAgent.once('free', common.mustCall(() => {
51      // Verify that the most recent connection is in the freeSockets pool.
52      const keys = Object.keys(https.globalAgent.freeSockets);
53      if (value) {
54        assert.ok(
55          keys.some((val) => val.startsWith(value.toString() + ':') ||
56                            val.endsWith(':' + value.toString()) ||
57                            val.includes(':' + value.toString() + ':')),
58          `missing value: ${value.toString()} in ${keys}`
59        );
60      }
61      const next = iter.next();
62
63      if (next.done) {
64        https.globalAgent.destroy();
65        server.close();
66      } else {
67        // Save `value` for check the next time.
68        const [key, val] = next.value;
69        value = val;
70        https.get({ ...getBaseOptions(port), [key]: val },
71                  variations(iter, port, cb));
72      }
73    }));
74  });
75}
76
77server.listen(0, common.mustCall(() => {
78  const port = server.address().port;
79  https.globalAgent.keepAlive = true;
80  https.get(getBaseOptions(port), variations(updatedValues.entries(), port));
81}));
82