Skip to content

Commit ab2b331

Browse files
committedAug 11, 2017
test: make test-tls-alert-handling more strict
Use `common.mustCall()` and `common.mustNotCall()` to more rigorously check that functions (especially no-op error handlers) are called the expected number of times in test-tls-alert-handling. PR-URL: #14650 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: David Cai <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a6973a3 commit ab2b331

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed
 

‎test/parallel/test-tls-alert-handling.js

+28-21
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,43 @@ if (!common.hasCrypto)
55
common.skip('missing crypto');
66

77
if (!common.opensslCli)
8-
common.skip('node compiled without OpenSSL CLI.');
8+
common.skip('node compiled without OpenSSL CLI');
99

1010
const net = require('net');
1111
const tls = require('tls');
1212
const fixtures = require('../common/fixtures');
1313

14+
let clientClosed = false;
15+
let errorReceived = false;
16+
function canCloseServer() {
17+
return clientClosed && errorReceived;
18+
}
19+
1420
function loadPEM(n) {
15-
return fixtures.readKey(`${n}.pem`);
21+
return fixtures.readKey(`${n}.pem`, 'utf-8');
1622
}
1723

1824
const opts = {
1925
key: loadPEM('agent2-key'),
2026
cert: loadPEM('agent2-cert')
2127
};
28+
2229
const max_iter = 20;
2330
let iter = 0;
2431

25-
const server = tls.createServer(opts, function(s) {
26-
s.pipe(s);
27-
s.on('error', function() {
28-
// ignore error
29-
});
32+
const errorHandler = common.mustCall(() => {
33+
errorReceived = true;
34+
if (canCloseServer())
35+
server.close();
3036
});
37+
const server = tls.createServer(opts, common.mustCall(function(s) {
38+
s.pipe(s);
39+
s.on('error', errorHandler);
40+
}, 2));
3141

32-
server.listen(0, function() {
42+
server.listen(0, common.mustCall(function() {
3343
sendClient();
34-
});
44+
}));
3545

3646

3747
function sendClient() {
@@ -45,15 +55,14 @@ function sendClient() {
4555
return;
4656
}
4757
client.end();
48-
server.close();
4958
}, max_iter));
5059
client.write('a');
51-
client.on('error', function() {
52-
// ignore error
53-
});
54-
client.on('close', function() {
55-
server.close();
56-
});
60+
client.on('error', common.mustNotCall());
61+
client.on('close', common.mustCall(function() {
62+
clientClosed = true;
63+
if (canCloseServer())
64+
server.close();
65+
}));
5766
}
5867

5968

@@ -63,11 +72,9 @@ function sendBADTLSRecord() {
6372
const client = tls.connect({
6473
socket: socket,
6574
rejectUnauthorized: false
66-
}, function() {
75+
}, common.mustCall(function() {
6776
socket.write(BAD_RECORD);
6877
socket.end();
69-
});
70-
client.on('error', function() {
71-
// ignore error
72-
});
78+
}));
79+
client.on('error', common.mustCall());
7380
}

0 commit comments

Comments
 (0)
Please sign in to comment.