Skip to content

Commit

Permalink
test: refactor test-tls-alert-handling
Browse files Browse the repository at this point in the history
* process.on('exit',...) checks -> common.mustCall()
* remove unused function parameters
* var -> const/let

PR-URL: #10482
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Italo A. Casas <[email protected]>
  • Loading branch information
Trott authored and MylesBorins committed Jan 24, 2017
1 parent 5215377 commit 06f934f
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions test/parallel/test-tls-alert-handling.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
var common = require('../common');
var assert = require('assert');
const common = require('../common');

if (!common.opensslCli) {
common.skip('node compiled without OpenSSL CLI.');
Expand All @@ -12,11 +11,9 @@ if (!common.hasCrypto) {
return;
}

var tls = require('tls');
var net = require('net');
var fs = require('fs');

var success = false;
const tls = require('tls');
const net = require('net');
const fs = require('fs');

function filenamePEM(n) {
return require('path').join(common.fixturesDir, 'keys', n + '.pem');
Expand All @@ -26,17 +23,16 @@ function loadPEM(n) {
return fs.readFileSync(filenamePEM(n));
}

var opts = {
const opts = {
key: loadPEM('agent2-key'),
cert: loadPEM('agent2-cert')
};
const max_iter = 20;
let iter = 0;

var max_iter = 20;
var iter = 0;

var server = tls.createServer(opts, function(s) {
const server = tls.createServer(opts, function(s) {
s.pipe(s);
s.on('error', function(e) {
s.on('error', function() {
// ignore error
});
});
Expand All @@ -47,21 +43,20 @@ server.listen(0, function() {


function sendClient() {
var client = tls.connect(server.address().port, {
const client = tls.connect(server.address().port, {
rejectUnauthorized: false
});
client.on('data', function(chunk) {
client.on('data', common.mustCall(function() {
if (iter++ === 2) sendBADTLSRecord();
if (iter < max_iter) {
client.write('a');
return;
}
client.end();
server.close();
success = true;
});
}, max_iter));
client.write('a');
client.on('error', function(e) {
client.on('error', function() {
// ignore error
});
client.on('close', function() {
Expand All @@ -71,21 +66,16 @@ function sendClient() {


function sendBADTLSRecord() {
var BAD_RECORD = Buffer.from([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
var socket = net.connect(server.address().port);
var client = tls.connect({
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
}, function() {
socket.write(BAD_RECORD);
socket.end();
});
client.on('error', function(e) {
client.on('error', function() {
// ignore error
});
}

process.on('exit', function() {
assert.strictEqual(iter, max_iter);
assert(success);
});

0 comments on commit 06f934f

Please sign in to comment.