Skip to content

Commit 9bf36bc

Browse files
oyydtargos
authored andcommitted
test: add a test for tls.Socket with allowHalfOpen
This test ensures that a tls client socket using `StreamWrap` with `allowHalfOpen` option won't hang. PR-URL: #23866 Refs: #23654 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d152705 commit 9bf36bc

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const fixtures = require('../common/fixtures');
8+
const tls = require('tls');
9+
const net = require('net');
10+
11+
// This test ensures that when tls sockets are created with `allowHalfOpen`,
12+
// they won't hang.
13+
const key = fixtures.readKey('agent1-key.pem');
14+
const cert = fixtures.readKey('agent1-cert.pem');
15+
const ca = fixtures.readKey('ca1-cert.pem');
16+
const options = {
17+
key,
18+
cert,
19+
ca: [ca],
20+
};
21+
22+
const server = tls.createServer(options, common.mustCall((conn) => {
23+
conn.write('hello');
24+
conn.on('data', common.mustCall());
25+
conn.end();
26+
})).listen(0, common.mustCall(() => {
27+
const netSocket = new net.Socket({
28+
allowHalfOpen: true,
29+
});
30+
31+
const socket = tls.connect({
32+
socket: netSocket,
33+
rejectUnauthorized: false,
34+
});
35+
36+
const { port, address } = server.address();
37+
38+
// Doing `net.Socket.connect()` after `tls.connect()` will make tls module
39+
// wrap the socket in StreamWrap.
40+
netSocket.connect({
41+
port,
42+
address,
43+
});
44+
45+
socket.on('end', common.mustCall());
46+
socket.on('data', common.mustCall());
47+
socket.on('close', common.mustCall(() => {
48+
server.close();
49+
}));
50+
51+
socket.write('hello');
52+
socket.end();
53+
}));

0 commit comments

Comments
 (0)