|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const fixtures = require('../common/fixtures'); |
| 9 | +const { hasMultiLocalhost } = require('../common/net'); |
| 10 | +const net = require('net'); |
| 11 | +const tls = require('tls'); |
| 12 | +const { once } = require('events'); |
| 13 | + |
| 14 | +async function connectWithSession( |
| 15 | + socket, session, maxVersion, expectedReuse = true, servername, |
| 16 | +) { |
| 17 | + const tlsSocket = tls.connect({ |
| 18 | + socket, |
| 19 | + session, |
| 20 | + servername, |
| 21 | + maxVersion, |
| 22 | + rejectUnauthorized: false, |
| 23 | + }); |
| 24 | + const close = once(tlsSocket, 'close'); |
| 25 | + tlsSocket.resume(); |
| 26 | + await once(tlsSocket, 'secureConnect'); |
| 27 | + assert.strictEqual(tlsSocket.isSessionReused(), expectedReuse); |
| 28 | + await close; |
| 29 | +} |
| 30 | + |
| 31 | +async function connectAndCaptureSession( |
| 32 | + port, maxVersion, host = '127.0.0.1', lookup, |
| 33 | +) { |
| 34 | + const socket = tls.connect({ |
| 35 | + host, |
| 36 | + lookup, |
| 37 | + port, |
| 38 | + maxVersion, |
| 39 | + rejectUnauthorized: false, |
| 40 | + }); |
| 41 | + const close = once(socket, 'close'); |
| 42 | + const sessionPromise = once(socket, 'session').then(([session]) => session); |
| 43 | + socket.resume(); |
| 44 | + await once(socket, 'secureConnect'); |
| 45 | + assert.strictEqual(socket.isSessionReused(), false); |
| 46 | + const session = await sessionPromise; |
| 47 | + assert(Buffer.isBuffer(session)); |
| 48 | + await close; |
| 49 | + return session; |
| 50 | +} |
| 51 | + |
| 52 | +async function testVersion(maxVersion) { |
| 53 | + const server = tls.createServer({ |
| 54 | + key: fixtures.readKey('agent1-key.pem'), |
| 55 | + cert: fixtures.readKey('agent1-cert.pem'), |
| 56 | + minVersion: maxVersion, |
| 57 | + maxVersion, |
| 58 | + }, (socket) => socket.end('ok')); |
| 59 | + |
| 60 | + server.listen(0, '0.0.0.0'); |
| 61 | + await once(server, 'listening'); |
| 62 | + const { port } = server.address(); |
| 63 | + const session = await connectAndCaptureSession(port, maxVersion); |
| 64 | + |
| 65 | + let socket = net.connect({ host: '127.0.0.1', port }); |
| 66 | + await once(socket, 'connect'); |
| 67 | + await connectWithSession(socket, session, maxVersion); |
| 68 | + |
| 69 | + socket = net.connect({ host: '127.0.0.1', port }); |
| 70 | + await connectWithSession(socket, session, maxVersion); |
| 71 | + |
| 72 | + if (hasMultiLocalhost()) { |
| 73 | + socket = net.connect({ host: '127.0.0.2', port }); |
| 74 | + await connectWithSession(socket, session, maxVersion, false); |
| 75 | + } |
| 76 | + |
| 77 | + const lookup = (_hostname, _options, callback) => { |
| 78 | + if (_options.all) { |
| 79 | + callback(null, [{ address: '127.0.0.1', family: 4 }]); |
| 80 | + } else { |
| 81 | + callback(null, '127.0.0.1', 4); |
| 82 | + } |
| 83 | + }; |
| 84 | + const hostnameSession = await connectAndCaptureSession( |
| 85 | + port, maxVersion, 'localhost', lookup); |
| 86 | + socket = net.connect({ host: 'localhost', port, lookup }); |
| 87 | + await connectWithSession(socket, hostnameSession, maxVersion); |
| 88 | + |
| 89 | + socket = net.connect({ host: '127.0.0.1', port }); |
| 90 | + await connectWithSession(socket, session, maxVersion, false, 'localhost'); |
| 91 | + |
| 92 | + server.close(); |
| 93 | + await once(server, 'close'); |
| 94 | +} |
| 95 | + |
| 96 | +(async function() { |
| 97 | + await testVersion('TLSv1.2'); |
| 98 | + await testVersion('TLSv1.3'); |
| 99 | +})().then(common.mustCall()); |
0 commit comments