From 102b1d999d9ea973bd033aaad66e42d673163ac4 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sun, 28 Nov 2010 18:40:30 -0800 Subject: [PATCH] Add simple securepair client test --- lib/securepair.js | 12 ++- test/simple/test-securepair-client.js | 120 ++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 test/simple/test-securepair-client.js diff --git a/lib/securepair.js b/lib/securepair.js index 93110c0adf..7377bd06f8 100644 --- a/lib/securepair.js +++ b/lib/securepair.js @@ -357,10 +357,18 @@ SecurePair.prototype._error = function (err) { SecurePair.prototype.getPeerCertificate = function (err) { - return this._ssl.getPeerCertificate(); + if (this._ssl) { + return this._ssl.getPeerCertificate(); + } else { + return null; + } }; SecurePair.prototype.getCipher = function (err) { - return this._ssl.getCurrentCipher(); + if (this._ssl) { + return this._ssl.getCurrentCipher(); + } else { + return null; + } }; diff --git a/test/simple/test-securepair-client.js b/test/simple/test-securepair-client.js new file mode 100644 index 0000000000..db9fd55023 --- /dev/null +++ b/test/simple/test-securepair-client.js @@ -0,0 +1,120 @@ +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 spawn = require('child_process').spawn; + +var connections = 0; + +var keyfn = join(common.fixturesDir, "agent.key"); +var key = fs.readFileSync(keyfn).toString(); + +var certfn = join(common.fixturesDir, "agent.crt"); +var cert = fs.readFileSync(certfn).toString(); + +var server = spawn('openssl', ['s_server', + '-accept', common.PORT, + '-cert', certfn, + '-key', keyfn]); +server.stdout.pipe(process.stdout); +server.stderr.pipe(process.stdout); + + +function watchForAccept (d) { + if (/ACCEPT/g.test(d.toString())) { + server.stdout.removeListener('data', watchForAccept); + startClient(); + } +} + +var state = "WAIT-ACCEPT"; + +server.stdout.on('data', function (d) { + switch (state) { + case "WAIT-ACCEPT": + if (/ACCEPT/g.test(d.toString())) { + startClient(); + state = "WAIT-HELLO" + } + break; + + case "WAIT-HELLO": + if (/hello/g.test(d.toString())) { + + // End the current SSL connection and exit. + // See s_server(1ssl). + server.stdin.write("Q"); + + state = "WAIT-SERVER-CLOSE"; + } + break; + + default: + break; + } +}); + + +var serverExitCode = -1; +server.on('exit', function (code) { + serverExitCode = code; +}); + + +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 = crypto.createPair(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.getPeerCertificate(): %j', pair.getPeerCertificate()); + console.log('client pair.getCipher(): %j', pair.getCipher()); + setTimeout(function () { + pair.cleartext.write('hello\r\n'); + }, 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.equal(0, serverExitCode); + assert.equal("WAIT-SERVER-CLOSE", state); +});