0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/pummel/test-tls-securepair-client.js
Gibson Fahnestock 3d2aef3979 test: s/assert.equal/assert.strictEqual/
Use assert.strictEqual instead of assert.equal in tests, manually
convert types where necessary.

PR-URL: https://github.com/nodejs/node/pull/10698
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
2017-01-11 14:19:26 +00:00

167 lines
4.1 KiB
JavaScript

'use strict';
//
const common = require('../common');
if (!common.opensslCli) {
common.skip('node compiled without OpenSSL CLI.');
return;
}
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
const join = require('path').join;
const net = require('net');
const assert = require('assert');
const fs = require('fs');
const tls = require('tls');
const spawn = require('child_process').spawn;
test1();
// simple/test-tls-securepair-client
function test1() {
test('agent.key', 'agent.crt', null, test2);
}
// simple/test-tls-ext-key-usage
function test2() {
function check(pair) {
// "TLS Web Client Authentication"
assert.strictEqual(pair.cleartext.getPeerCertificate().ext_key_usage.length,
1);
assert.strictEqual(pair.cleartext.getPeerCertificate().ext_key_usage[0],
'1.3.6.1.5.5.7.3.2');
}
test('keys/agent4-key.pem', 'keys/agent4-cert.pem', check);
}
function test(keyfn, certfn, check, next) {
keyfn = join(common.fixturesDir, keyfn);
const key = fs.readFileSync(keyfn).toString();
certfn = join(common.fixturesDir, certfn);
const cert = fs.readFileSync(certfn).toString();
const server = spawn(common.opensslCli, ['s_server',
'-accept', common.PORT,
'-cert', certfn,
'-key', keyfn]);
server.stdout.pipe(process.stdout);
server.stderr.pipe(process.stdout);
let state = 'WAIT-ACCEPT';
let serverStdoutBuffer = '';
server.stdout.setEncoding('utf8');
server.stdout.on('data', function(s) {
serverStdoutBuffer += s;
console.error(state);
switch (state) {
case 'WAIT-ACCEPT':
if (/ACCEPT/g.test(serverStdoutBuffer)) {
// Give s_server half a second to start up.
setTimeout(startClient, 500);
state = 'WAIT-HELLO';
}
break;
case 'WAIT-HELLO':
if (/hello/g.test(serverStdoutBuffer)) {
// End the current SSL connection and exit.
// See s_server(1ssl).
server.stdin.write('Q');
state = 'WAIT-SERVER-CLOSE';
}
break;
default:
break;
}
});
const timeout = setTimeout(function() {
server.kill();
process.exit(1);
}, 5000);
let gotWriteCallback = false;
let serverExitCode = -1;
server.on('exit', function(code) {
serverExitCode = code;
clearTimeout(timeout);
if (next) next();
});
function startClient() {
const s = new net.Stream();
const sslcontext = tls.createSecureContext({key: key, cert: cert});
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
const pair = tls.createSecurePair(sslcontext, false);
assert.ok(pair.encrypted.writable);
assert.ok(pair.cleartext.writable);
pair.encrypted.pipe(s);
s.pipe(pair.encrypted);
s.connect(common.PORT);
s.on('connect', function() {
console.log('client connected');
});
pair.on('secure', function() {
console.log('client: connected+secure!');
console.log('client pair.cleartext.getPeerCertificate(): %j',
pair.cleartext.getPeerCertificate());
console.log('client pair.cleartext.getCipher(): %j',
pair.cleartext.getCipher());
if (check) check(pair);
setTimeout(function() {
pair.cleartext.write('hello\r\n', function() {
gotWriteCallback = true;
});
}, 500);
});
pair.cleartext.on('data', function(d) {
console.log('cleartext: %s', d.toString());
});
s.on('close', function() {
console.log('client close');
});
pair.encrypted.on('error', function(err) {
console.log('encrypted error: ' + err);
});
s.on('error', function(err) {
console.log('socket error: ' + err);
});
pair.on('error', function(err) {
console.log('secure error: ' + err);
});
}
process.on('exit', function() {
assert.strictEqual(0, serverExitCode);
assert.strictEqual('WAIT-SERVER-CLOSE', state);
assert.ok(gotWriteCallback);
});
}