You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.0 KiB

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');
var tls = require('tls');
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;
var connections = 0;
14 years ago
var keyfn = join(common.fixturesDir, 'agent.key');
var key = fs.readFileSync(keyfn).toString();
14 years ago
var certfn = join(common.fixturesDir, 'agent.crt');
var cert = fs.readFileSync(certfn).toString();
var server = spawn('openssl', ['s_server',
'-accept', PORT,
'-cert', certfn,
'-key', keyfn]);
server.stdout.pipe(process.stdout);
server.stderr.pipe(process.stdout);
14 years ago
var state = 'WAIT-ACCEPT';
14 years ago
var serverStdoutBuffer = '';
server.stdout.setEncoding('utf8');
14 years ago
server.stdout.on('data', function(s) {
serverStdoutBuffer += s;
console.error(state);
switch (state) {
14 years ago
case 'WAIT-ACCEPT':
if (/ACCEPT/g.test(serverStdoutBuffer)) {
startClient();
14 years ago
state = 'WAIT-HELLO';
}
break;
14 years ago
case 'WAIT-HELLO':
if (/hello/g.test(serverStdoutBuffer)) {
// End the current SSL connection and exit.
// See s_server(1ssl).
14 years ago
server.stdin.write('Q');
14 years ago
state = 'WAIT-SERVER-CLOSE';
}
break;
default:
break;
}
});
var serverExitCode = -1;
14 years ago
server.on('exit', function(code) {
serverExitCode = code;
});
14 years ago
function startClient() {
var s = new net.Stream();
var sslcontext = crypto.createCredentials({key: key, cert: cert});
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
var 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(PORT);
14 years ago
s.on('connect', function() {
console.log('client connected');
});
14 years ago
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());
14 years ago
setTimeout(function() {
pair.cleartext.write('hello\r\n');
}, 500);
});
14 years ago
pair.cleartext.on('data', function(d) {
console.log('cleartext: %s', d.toString());
});
14 years ago
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);
});
}
14 years ago
process.on('exit', function() {
assert.equal(0, serverExitCode);
14 years ago
assert.equal('WAIT-SERVER-CLOSE', state);
});