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: https://github.com/nodejs/node/pull/14650
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: David Cai <davidcai1993@yahoo.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Rich Trott 2017-08-06 10:48:14 -07:00
parent a6973a3811
commit ab2b331f5e

View File

@ -5,33 +5,43 @@ if (!common.hasCrypto)
common.skip('missing crypto'); common.skip('missing crypto');
if (!common.opensslCli) if (!common.opensslCli)
common.skip('node compiled without OpenSSL CLI.'); common.skip('node compiled without OpenSSL CLI');
const net = require('net'); const net = require('net');
const tls = require('tls'); const tls = require('tls');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');
let clientClosed = false;
let errorReceived = false;
function canCloseServer() {
return clientClosed && errorReceived;
}
function loadPEM(n) { function loadPEM(n) {
return fixtures.readKey(`${n}.pem`); return fixtures.readKey(`${n}.pem`, 'utf-8');
} }
const opts = { const opts = {
key: loadPEM('agent2-key'), key: loadPEM('agent2-key'),
cert: loadPEM('agent2-cert') cert: loadPEM('agent2-cert')
}; };
const max_iter = 20; const max_iter = 20;
let iter = 0; let iter = 0;
const server = tls.createServer(opts, function(s) { const errorHandler = common.mustCall(() => {
errorReceived = true;
if (canCloseServer())
server.close();
});
const server = tls.createServer(opts, common.mustCall(function(s) {
s.pipe(s); s.pipe(s);
s.on('error', function() { s.on('error', errorHandler);
// ignore error }, 2));
});
});
server.listen(0, function() { server.listen(0, common.mustCall(function() {
sendClient(); sendClient();
}); }));
function sendClient() { function sendClient() {
@ -45,15 +55,14 @@ function sendClient() {
return; return;
} }
client.end(); client.end();
server.close();
}, max_iter)); }, max_iter));
client.write('a'); client.write('a');
client.on('error', function() { client.on('error', common.mustNotCall());
// ignore error client.on('close', common.mustCall(function() {
}); clientClosed = true;
client.on('close', function() { if (canCloseServer())
server.close(); server.close();
}); }));
} }
@ -63,11 +72,9 @@ function sendBADTLSRecord() {
const client = tls.connect({ const client = tls.connect({
socket: socket, socket: socket,
rejectUnauthorized: false rejectUnauthorized: false
}, function() { }, common.mustCall(function() {
socket.write(BAD_RECORD); socket.write(BAD_RECORD);
socket.end(); socket.end();
}); }));
client.on('error', function() { client.on('error', common.mustCall());
// ignore error
});
} }