0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test/parallel/test-tls-alert-handling.js
Rich Trott ab2b331f5e 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>
2017-08-11 16:25:09 -07:00

81 lines
1.7 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
if (!common.opensslCli)
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`, 'utf-8');
}
const opts = {
key: loadPEM('agent2-key'),
cert: loadPEM('agent2-cert')
};
const max_iter = 20;
let iter = 0;
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', errorHandler);
}, 2));
server.listen(0, common.mustCall(function() {
sendClient();
}));
function sendClient() {
const client = tls.connect(server.address().port, {
rejectUnauthorized: false
});
client.on('data', common.mustCall(function() {
if (iter++ === 2) sendBADTLSRecord();
if (iter < max_iter) {
client.write('a');
return;
}
client.end();
}, max_iter));
client.write('a');
client.on('error', common.mustNotCall());
client.on('close', common.mustCall(function() {
clientClosed = true;
if (canCloseServer())
server.close();
}));
}
function sendBADTLSRecord() {
const BAD_RECORD = Buffer.from([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
const socket = net.connect(server.address().port);
const client = tls.connect({
socket: socket,
rejectUnauthorized: false
}, common.mustCall(function() {
socket.write(BAD_RECORD);
socket.end();
}));
client.on('error', common.mustCall());
}