2011-03-10 09:54:52 +01: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 00:26:43 +01:00
|
|
|
|
2011-08-24 08:42:23 +02:00
|
|
|
|
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-12-05 23:33:52 +01:00
|
|
|
|
|
|
|
var net = require('net');
|
|
|
|
var fs = require('fs');
|
2009-11-22 03:58:08 +01:00
|
|
|
|
|
|
|
var tests_run = 0;
|
|
|
|
|
2010-12-05 23:33:52 +01:00
|
|
|
function tlsTest(port, host, caPem, keyPem, certPem) {
|
2009-11-22 03:58:08 +01:00
|
|
|
var N = 50;
|
|
|
|
var count = 0;
|
|
|
|
var sent_final_ping = false;
|
|
|
|
|
2010-12-05 23:33:52 +01:00
|
|
|
var server = net.createServer(function(socket) {
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(true, socket.remoteAddress !== null);
|
|
|
|
assert.equal(true, socket.remoteAddress !== undefined);
|
2010-12-05 23:33:52 +01:00
|
|
|
if (host === '127.0.0.1')
|
|
|
|
assert.equal(socket.remoteAddress, '127.0.0.1');
|
2009-11-22 03:58:08 +01:00
|
|
|
else if (host == null)
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal(socket.remoteAddress, '127.0.0.1');
|
2009-11-22 03:58:08 +01:00
|
|
|
|
2010-12-05 23:33:52 +01:00
|
|
|
socket.setEncoding('utf8');
|
2009-11-22 03:58:08 +01:00
|
|
|
socket.setNoDelay();
|
|
|
|
socket.timeout = 0;
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('data', function(data) {
|
2009-11-22 03:58:08 +01:00
|
|
|
var verified = socket.verifyPeer();
|
2010-12-05 23:33:52 +01:00
|
|
|
var peerDN = socket.getPeerCertificate('DNstring');
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(verified, 1);
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal(peerDN,
|
|
|
|
'C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js,' +
|
|
|
|
'OU=Test TLS Certificate,CN=localhost');
|
|
|
|
console.log('server got: ' + JSON.stringify(data));
|
|
|
|
assert.equal('open', socket.readyState);
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(true, count <= N);
|
2009-11-22 03:58:08 +01:00
|
|
|
if (/PING/.exec(data)) {
|
2010-12-05 23:33:52 +01:00
|
|
|
socket.write('PONG');
|
2009-11-22 03:58:08 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('end', function() {
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal('writeOnly', socket.readyState);
|
2010-04-08 19:44:22 +02:00
|
|
|
socket.end();
|
2009-11-22 03:58:08 +01:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
socket.on('close', function(had_error) {
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(false, had_error);
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal('closed', socket.readyState);
|
2009-11-22 03:58:08 +01:00
|
|
|
socket.server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.setSecure('X509_PEM', caPem, 0, keyPem, certPem);
|
|
|
|
server.listen(port, host);
|
|
|
|
|
2010-10-11 10:22:24 +02:00
|
|
|
var client = net.createConnection(port, host);
|
2009-11-22 03:58:08 +01:00
|
|
|
|
2010-12-05 23:33:52 +01:00
|
|
|
client.setEncoding('utf8');
|
2009-11-22 03:58:08 +01:00
|
|
|
client.setSecure('X509_PEM', caPem, 0, keyPem, caPem);
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('connect', function() {
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal('open', client.readyState);
|
2009-11-22 03:58:08 +01:00
|
|
|
var verified = client.verifyPeer();
|
2010-12-05 23:33:52 +01:00
|
|
|
var peerDN = client.getPeerCertificate('DNstring');
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(verified, 1);
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal(peerDN,
|
|
|
|
'C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js,' +
|
|
|
|
'OU=Test TLS Certificate,CN=localhost');
|
|
|
|
client.write('PING');
|
2009-11-22 03:58:08 +01:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('data', function(data) {
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal('PONG', data);
|
2009-11-22 03:58:08 +01:00
|
|
|
count += 1;
|
|
|
|
|
2010-12-05 23:33:52 +01:00
|
|
|
console.log('client got PONG');
|
2009-11-22 03:58:08 +01:00
|
|
|
|
|
|
|
if (sent_final_ping) {
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal('readOnly', client.readyState);
|
2009-11-22 03:58:08 +01:00
|
|
|
return;
|
|
|
|
} else {
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal('open', client.readyState);
|
2009-11-22 03:58:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count < N) {
|
2010-12-05 23:33:52 +01:00
|
|
|
client.write('PING');
|
2009-11-22 03:58:08 +01:00
|
|
|
} else {
|
|
|
|
sent_final_ping = true;
|
2010-12-05 23:33:52 +01:00
|
|
|
client.write('PING');
|
2010-04-08 19:44:22 +02:00
|
|
|
client.end();
|
2009-11-22 03:58:08 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('close', function() {
|
2010-12-05 23:33:52 +01:00
|
|
|
assert.equal(N + 1, count);
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(true, sent_final_ping);
|
2009-11-22 03:58:08 +01:00
|
|
|
tests_run += 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var have_tls;
|
|
|
|
try {
|
2010-10-11 10:22:24 +02:00
|
|
|
var dummy_server = net.createServer();
|
2009-11-22 03:58:08 +01:00
|
|
|
dummy_server.setSecure();
|
2010-12-05 23:33:52 +01:00
|
|
|
have_tls = true;
|
2009-11-22 03:58:08 +01:00
|
|
|
} catch (e) {
|
2010-12-05 23:33:52 +01:00
|
|
|
have_tls = false;
|
2010-06-30 08:12:46 +02:00
|
|
|
}
|
2009-11-22 03:58:08 +01:00
|
|
|
|
|
|
|
if (have_tls) {
|
2010-12-05 23:33:52 +01:00
|
|
|
var caPem = fs.readFileSync(common.fixturesDir + '/test_ca.pem');
|
|
|
|
var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem');
|
|
|
|
var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem');
|
2009-11-22 03:58:08 +01:00
|
|
|
|
|
|
|
/* All are run at once, so run on different ports */
|
2010-12-05 23:33:52 +01:00
|
|
|
tlsTest(common.PORT, 'localhost', caPem, keyPem, certPem);
|
|
|
|
tlsTest(common.PORT + 1, null, caPem, keyPem, certPem);
|
2009-11-22 03:58:08 +01:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(2, tests_run);
|
2009-11-22 03:58:08 +01:00
|
|
|
});
|
|
|
|
} else {
|
2010-12-05 23:33:52 +01:00
|
|
|
console.log('Not compiled with TLS support -- skipping test');
|
2010-02-20 02:17:54 +01:00
|
|
|
process.exit(0);
|
2009-11-22 03:58:08 +01:00
|
|
|
}
|