node/test/simple/test-tls-securepair-client.js

173 lines
4.8 KiB
JavaScript
Raw Normal View History

2011-03-10 16:54:52 +08:00
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2011-11-03 07:26:43 +08:00
2011-08-24 14:42:23 +08:00
// There is a bug with 'openssl s_server' which makes it not flush certain
// important events to stdout when done over a pipe. Therefore we skip this
// test for all openssl versions less than 1.0.0.
if (!process.versions.openssl ||
2011-01-03 04:37:27 +08:00
parseInt(process.versions.openssl[0]) < 1) {
console.error('Skipping due to old OpenSSL version.');
process.exit(0);
}
2010-11-29 10:40:30 +08:00
var common = require('../common');
var join = require('path').join;
var net = require('net');
var assert = require('assert');
var fs = require('fs');
var crypto = require('crypto');
2010-12-06 10:19:18 +08:00
var tls = require('tls');
2010-11-29 10:40:30 +08:00
var spawn = require('child_process').spawn;
// FIXME: Avoid the common PORT as this test currently hits a C-level
// assertion error with node_g. The program aborts without HUPing
// the openssl s_server thus causing many tests to fail with
// EADDRINUSE.
var PORT = common.PORT + 5;
2010-11-29 10:40:30 +08:00
var connections = 0;
2010-12-05 06:45:52 +08:00
var keyfn = join(common.fixturesDir, 'agent.key');
2010-11-29 10:40:30 +08:00
var key = fs.readFileSync(keyfn).toString();
2010-12-05 06:45:52 +08:00
var certfn = join(common.fixturesDir, 'agent.crt');
2010-11-29 10:40:30 +08:00
var cert = fs.readFileSync(certfn).toString();
var server = spawn('openssl', ['s_server',
'-accept', PORT,
2010-11-29 10:40:30 +08:00
'-cert', certfn,
'-key', keyfn]);
server.stdout.pipe(process.stdout);
server.stderr.pipe(process.stdout);
2010-12-05 06:45:52 +08:00
var state = 'WAIT-ACCEPT';
2010-11-29 10:40:30 +08:00
2010-12-05 06:45:52 +08:00
var serverStdoutBuffer = '';
server.stdout.setEncoding('utf8');
2010-12-05 06:45:52 +08:00
server.stdout.on('data', function(s) {
serverStdoutBuffer += s;
console.error(state);
2010-11-29 10:40:30 +08:00
switch (state) {
2010-12-05 06:45:52 +08:00
case 'WAIT-ACCEPT':
if (/ACCEPT/g.test(serverStdoutBuffer)) {
// Give s_server half a second to start up.
setTimeout(startClient, 500);
2010-12-05 06:45:52 +08:00
state = 'WAIT-HELLO';
2010-11-29 10:40:30 +08:00
}
break;
2010-12-05 06:45:52 +08:00
case 'WAIT-HELLO':
if (/hello/g.test(serverStdoutBuffer)) {
2010-11-29 10:40:30 +08:00
// End the current SSL connection and exit.
// See s_server(1ssl).
2010-12-05 06:45:52 +08:00
server.stdin.write('Q');
2010-11-29 10:40:30 +08:00
2010-12-05 06:45:52 +08:00
state = 'WAIT-SERVER-CLOSE';
2010-11-29 10:40:30 +08:00
}
break;
default:
break;
}
});
var timeout = setTimeout(function() {
server.kill();
process.exit(1);
}, 5000);
2010-12-21 03:08:51 +08:00
var gotWriteCallback = false;
2010-11-29 10:40:30 +08:00
var serverExitCode = -1;
2010-12-21 03:08:51 +08:00
2010-12-05 06:45:52 +08:00
server.on('exit', function(code) {
2010-11-29 10:40:30 +08:00
serverExitCode = code;
clearTimeout(timeout);
2010-11-29 10:40:30 +08:00
});
2010-12-05 06:45:52 +08:00
function startClient() {
2010-11-29 10:40:30 +08:00
var s = new net.Stream();
var sslcontext = crypto.createCredentials({key: key, cert: cert});
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
2010-12-06 10:19:18 +08:00
var pair = tls.createSecurePair(sslcontext, false);
2010-11-29 10:40:30 +08:00
assert.ok(pair.encrypted.writable);
assert.ok(pair.cleartext.writable);
pair.encrypted.pipe(s);
s.pipe(pair.encrypted);
s.connect(PORT);
2010-11-29 10:40:30 +08:00
2010-12-05 06:45:52 +08:00
s.on('connect', function() {
console.log('client connected');
2010-11-29 10:40:30 +08:00
});
2010-12-05 06:45:52 +08:00
pair.on('secure', function() {
2010-11-29 10:40:30 +08:00
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());
2010-12-05 06:45:52 +08:00
setTimeout(function() {
pair.cleartext.write('hello\r\n', function() {
2010-12-21 03:08:51 +08:00
gotWriteCallback = true;
});
2010-11-29 10:40:30 +08:00
}, 500);
});
2010-12-05 06:45:52 +08:00
pair.cleartext.on('data', function(d) {
console.log('cleartext: %s', d.toString());
2010-11-29 10:40:30 +08:00
});
2010-12-05 06:45:52 +08:00
s.on('close', function() {
console.log('client close');
2010-11-29 10:40:30 +08:00
});
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);
});
}
2010-12-05 06:45:52 +08:00
process.on('exit', function() {
2010-11-29 10:40:30 +08:00
assert.equal(0, serverExitCode);
2010-12-05 06:45:52 +08:00
assert.equal('WAIT-SERVER-CLOSE', state);
2010-12-21 03:08:51 +08:00
assert.ok(gotWriteCallback);
2010-11-29 10:40:30 +08:00
});