Skip to content

Commit ace267b

Browse files
sam-githubaddaleax
authored andcommitted
test: do not race connection and rejection
Existing code assumed that the server completed the handshake before the client rejected the certificate, and destroyed the socket. This assumption is fragile, remove it, and instead check explicitly that data can or cannot be exchanged via TLS, whichever is expected. PR-URL: #25508 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 639dc07 commit ace267b

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

test/parallel/test-tls-client-reject.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,57 @@ const options = {
3333
cert: fixtures.readSync('test_cert.pem')
3434
};
3535

36-
const server = tls.createServer(options, common.mustCall(function(socket) {
37-
socket.on('data', function(data) {
38-
console.error(data.toString());
39-
assert.strictEqual(data.toString(), 'ok');
40-
});
41-
}, 3)).listen(0, function() {
36+
const server = tls.createServer(options, function(socket) {
37+
socket.pipe(socket);
38+
socket.on('end', () => socket.end());
39+
}).listen(0, common.mustCall(function() {
4240
unauthorized();
43-
});
41+
}));
4442

4543
function unauthorized() {
44+
console.log('connect unauthorized');
4645
const socket = tls.connect({
4746
port: server.address().port,
4847
servername: 'localhost',
4948
rejectUnauthorized: false
5049
}, common.mustCall(function() {
50+
console.log('... unauthorized');
5151
assert(!socket.authorized);
52-
socket.end();
53-
rejectUnauthorized();
52+
socket.on('data', common.mustCall((data) => {
53+
assert.strictEqual(data.toString(), 'ok');
54+
}));
55+
socket.on('end', () => rejectUnauthorized());
5456
}));
5557
socket.on('error', common.mustNotCall());
56-
socket.write('ok');
58+
socket.end('ok');
5759
}
5860

5961
function rejectUnauthorized() {
62+
console.log('reject unauthorized');
6063
const socket = tls.connect(server.address().port, {
6164
servername: 'localhost'
6265
}, common.mustNotCall());
66+
socket.on('data', common.mustNotCall());
6367
socket.on('error', common.mustCall(function(err) {
64-
console.error(err);
68+
console.log('... rejected:', err);
6569
authorized();
6670
}));
67-
socket.write('ng');
71+
socket.end('ng');
6872
}
6973

7074
function authorized() {
75+
console.log('connect authorized');
7176
const socket = tls.connect(server.address().port, {
7277
ca: [fixtures.readSync('test_cert.pem')],
7378
servername: 'localhost'
7479
}, common.mustCall(function() {
80+
console.log('... authorized');
7581
assert(socket.authorized);
76-
socket.end();
77-
server.close();
82+
socket.on('data', common.mustCall((data) => {
83+
assert.strictEqual(data.toString(), 'ok');
84+
}));
85+
socket.on('end', () => server.close());
7886
}));
7987
socket.on('error', common.mustNotCall());
80-
socket.write('ok');
88+
socket.end('ok');
8189
}

0 commit comments

Comments
 (0)