0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/test/parallel/test-tls-alert-handling.js
Dany Shaanan d469321946 tools: add eslint rule prefer-assert-methods
PR-URL: https://github.com/nodejs/node/pull/8622
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
2016-09-20 16:35:39 -07:00

92 lines
1.7 KiB
JavaScript

'use strict';
var common = require('../common');
var assert = require('assert');
if (!common.opensslCli) {
common.skip('node compiled without OpenSSL CLI.');
return;
}
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
var tls = require('tls');
var net = require('net');
var fs = require('fs');
var success = false;
function filenamePEM(n) {
return require('path').join(common.fixturesDir, 'keys', n + '.pem');
}
function loadPEM(n) {
return fs.readFileSync(filenamePEM(n));
}
var opts = {
key: loadPEM('agent2-key'),
cert: loadPEM('agent2-cert')
};
var max_iter = 20;
var iter = 0;
var server = tls.createServer(opts, function(s) {
s.pipe(s);
s.on('error', function(e) {
// ignore error
});
});
server.listen(0, function() {
sendClient();
});
function sendClient() {
var client = tls.connect(server.address().port, {
rejectUnauthorized: false
});
client.on('data', function(chunk) {
if (iter++ === 2) sendBADTLSRecord();
if (iter < max_iter) {
client.write('a');
return;
}
client.end();
server.close();
success = true;
});
client.write('a');
client.on('error', function(e) {
// ignore error
});
client.on('close', function() {
server.close();
});
}
function sendBADTLSRecord() {
var BAD_RECORD = Buffer.from([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
var socket = net.connect(server.address().port);
var client = tls.connect({
socket: socket,
rejectUnauthorized: false
}, function() {
socket.write(BAD_RECORD);
socket.end();
});
client.on('error', function(e) {
// ignore error
});
}
process.on('exit', function() {
assert.strictEqual(iter, max_iter);
assert(success);
});