node/test/parallel/test-tls-socket-close.js
Luigi Pinca b1ada0ad55
tls: handle cases where the raw socket is destroyed
Ensure that the `'close'` event is emitted on a `TLSSocket` when it is
created from an existing raw `net.Socket` and the raw socket is not
closed cleanly (destroyed).

Refs: 048e0bec51
Refs: https://github.com/nodejs/node/issues/49902#issuecomment-1741203813
Fixes: https://github.com/nodejs/node/issues/49902
PR-URL: https://github.com/nodejs/node/pull/49980
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2023-10-04 14:19:42 +00:00

72 lines
2 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const tls = require('tls');
const net = require('net');
const Countdown = require('../common/countdown');
const fixtures = require('../common/fixtures');
const key = fixtures.readKey('agent2-key.pem');
const cert = fixtures.readKey('agent2-cert.pem');
let serverTlsSocket;
const tlsServer = tls.createServer({ cert, key }, (socket) => {
serverTlsSocket = socket;
socket.on('close', dec);
});
// A plain net server, that manually passes connections to the TLS
// server to be upgraded.
let netSocket;
let netSocketCloseEmitted = false;
const netServer = net.createServer((socket) => {
netSocket = socket;
tlsServer.emit('connection', socket);
socket.on('close', () => {
netSocketCloseEmitted = true;
assert.strictEqual(serverTlsSocket.destroyed, true);
});
}).listen(0, common.mustCall(() => {
connectClient(netServer);
}));
const countdown = new Countdown(2, () => {
netServer.close();
});
// A client that connects, sends one message, and closes the raw connection:
function connectClient(server) {
const clientTlsSocket = tls.connect({
host: 'localhost',
port: server.address().port,
rejectUnauthorized: false
});
clientTlsSocket.write('foo', 'utf8', common.mustCall(() => {
assert(netSocket);
netSocket.setTimeout(common.platformTimeout(10), common.mustCall(() => {
assert(serverTlsSocket);
netSocket.destroy();
assert.strictEqual(netSocket.destroyed, true);
setImmediate(() => {
// Close callbacks are executed after `setImmediate()` callbacks.
assert.strictEqual(netSocketCloseEmitted, false);
assert.strictEqual(serverTlsSocket.destroyed, false);
setImmediate(() => {
assert.strictEqual(netSocketCloseEmitted, true);
});
});
}));
}));
clientTlsSocket.on('close', dec);
}
function dec() {
countdown.dec();
}