node/test/parallel/test-tls-destroy-stream.js
Antoine du Hamel ef6b8cc3c3
test: ensure assertions are reached on more tests
PR-URL: https://github.com/nodejs/node/pull/60728
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2025-11-17 17:10:24 +00:00

78 lines
2.4 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto) common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const { duplexPair } = require('stream');
const net = require('net');
const assert = require('assert');
const tls = require('tls');
tls.DEFAULT_MAX_VERSION = 'TLSv1.3';
// This test ensures that an instance of StreamWrap should emit "end" and
// "close" when the socket on the other side call `destroy()` instead of
// `end()`.
// Refs: https://github.com/nodejs/node/issues/14605
const CONTENT = 'Hello World';
const tlsServer = tls.createServer(
{
key: fixtures.readKey('rsa_private.pem'),
cert: fixtures.readKey('rsa_cert.crt'),
ca: [fixtures.readKey('rsa_ca.crt')],
},
common.mustCall((socket) => {
socket.on('close', common.mustCall());
socket.write(CONTENT);
socket.destroy();
socket.on('error', common.mustCallAtLeast((err) => {
// destroy() is sync, write() is async, whether write completes depends
// on the protocol, it is not guaranteed by stream API.
if (err.code === 'ERR_STREAM_DESTROYED')
return;
assert.ifError(err);
}, 0));
}),
);
const server = net.createServer(common.mustCall((conn) => {
conn.on('error', common.mustNotCall());
// Assume that we want to use data to determine what to do with connections.
conn.once('data', common.mustCall((chunk) => {
const [ clientSide, serverSide ] = duplexPair();
serverSide.on('close', common.mustCall(() => {
conn.destroy();
}));
clientSide.pipe(conn);
conn.pipe(clientSide);
conn.on('close', common.mustCall(() => {
clientSide.destroy();
}));
clientSide.on('close', common.mustCall(() => {
conn.destroy();
}));
process.nextTick(() => {
conn.unshift(chunk);
});
tlsServer.emit('connection', serverSide);
}));
}));
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const conn = tls.connect({ port, rejectUnauthorized: false }, common.mustCall(() => {
// Whether the server's write() completed before its destroy() is
// indeterminate, but if data was written, we should receive it correctly.
conn.on('data', common.mustCallAtLeast((data) => {
assert.strictEqual(data.toString('utf8'), CONTENT);
}, 0));
conn.on('error', common.mustNotCall());
conn.on('close', common.mustCall(() => server.close()));
}));
}));