From ab2b331f5e3204f7b9b6362c7502910ecde6acae Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sun, 6 Aug 2017 10:48:14 -0700 Subject: [PATCH] test: make test-tls-alert-handling more strict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Tobias Nießen Reviewed-By: Luigi Pinca Reviewed-By: David Cai Reviewed-By: Yuta Hiroto Reviewed-By: James M Snell --- test/parallel/test-tls-alert-handling.js | 49 ++++++++++++++---------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/test/parallel/test-tls-alert-handling.js b/test/parallel/test-tls-alert-handling.js index dfe7ea640db..c686d68a182 100644 --- a/test/parallel/test-tls-alert-handling.js +++ b/test/parallel/test-tls-alert-handling.js @@ -5,33 +5,43 @@ if (!common.hasCrypto) common.skip('missing crypto'); if (!common.opensslCli) - common.skip('node compiled without OpenSSL CLI.'); + common.skip('node compiled without OpenSSL CLI'); const net = require('net'); const tls = require('tls'); const fixtures = require('../common/fixtures'); +let clientClosed = false; +let errorReceived = false; +function canCloseServer() { + return clientClosed && errorReceived; +} + function loadPEM(n) { - return fixtures.readKey(`${n}.pem`); + return fixtures.readKey(`${n}.pem`, 'utf-8'); } const opts = { key: loadPEM('agent2-key'), cert: loadPEM('agent2-cert') }; + const max_iter = 20; 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.on('error', function() { - // ignore error - }); -}); + s.on('error', errorHandler); +}, 2)); -server.listen(0, function() { +server.listen(0, common.mustCall(function() { sendClient(); -}); +})); function sendClient() { @@ -45,15 +55,14 @@ function sendClient() { return; } client.end(); - server.close(); }, max_iter)); client.write('a'); - client.on('error', function() { - // ignore error - }); - client.on('close', function() { - server.close(); - }); + client.on('error', common.mustNotCall()); + client.on('close', common.mustCall(function() { + clientClosed = true; + if (canCloseServer()) + server.close(); + })); } @@ -63,11 +72,9 @@ function sendBADTLSRecord() { const client = tls.connect({ socket: socket, rejectUnauthorized: false - }, function() { + }, common.mustCall(function() { socket.write(BAD_RECORD); socket.end(); - }); - client.on('error', function() { - // ignore error - }); + })); + client.on('error', common.mustCall()); }