0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/disabled/test-net-tls-pummel.js

151 lines
4.4 KiB
JavaScript
Raw Normal View History

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');
var net = require('net');
var fs = require('fs');
2009-11-22 03:58:08 +01:00
var tests_run = 0;
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;
var server = net.createServer(function(socket) {
assert.equal(true, socket.remoteAddress !== null);
assert.equal(true, socket.remoteAddress !== undefined);
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)
assert.equal(socket.remoteAddress, '127.0.0.1');
2009-11-22 03:58:08 +01:00
socket.setEncoding('utf8');
2009-11-22 03:58:08 +01:00
socket.setNoDelay();
socket.timeout = 0;
socket.on('data', function(data) {
2009-11-22 03:58:08 +01:00
var verified = socket.verifyPeer();
var peerDN = socket.getPeerCertificate('DNstring');
assert.equal(verified, 1);
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);
assert.equal(true, count <= N);
2009-11-22 03:58:08 +01:00
if (/PING/.exec(data)) {
socket.write('PONG');
2009-11-22 03:58:08 +01:00
}
});
socket.on('end', function() {
assert.equal('writeOnly', socket.readyState);
socket.end();
2009-11-22 03:58:08 +01:00
});
socket.on('close', function(had_error) {
assert.equal(false, had_error);
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
client.setEncoding('utf8');
2009-11-22 03:58:08 +01:00
client.setSecure('X509_PEM', caPem, 0, keyPem, caPem);
client.on('connect', function() {
assert.equal('open', client.readyState);
2009-11-22 03:58:08 +01:00
var verified = client.verifyPeer();
var peerDN = client.getPeerCertificate('DNstring');
assert.equal(verified, 1);
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
});
client.on('data', function(data) {
assert.equal('PONG', data);
2009-11-22 03:58:08 +01:00
count += 1;
console.log('client got PONG');
2009-11-22 03:58:08 +01:00
if (sent_final_ping) {
assert.equal('readOnly', client.readyState);
2009-11-22 03:58:08 +01:00
return;
} else {
assert.equal('open', client.readyState);
2009-11-22 03:58:08 +01:00
}
if (count < N) {
client.write('PING');
2009-11-22 03:58:08 +01:00
} else {
sent_final_ping = true;
client.write('PING');
client.end();
2009-11-22 03:58:08 +01:00
}
});
client.on('close', function() {
assert.equal(N + 1, count);
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();
have_tls = true;
2009-11-22 03:58:08 +01:00
} catch (e) {
have_tls = false;
2010-06-30 08:12:46 +02:00
}
2009-11-22 03:58:08 +01:00
if (have_tls) {
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 */
tlsTest(common.PORT, 'localhost', caPem, keyPem, certPem);
tlsTest(common.PORT + 1, null, caPem, keyPem, certPem);
2009-11-22 03:58:08 +01:00
process.on('exit', function() {
assert.equal(2, tests_run);
2009-11-22 03:58:08 +01:00
});
} else {
console.log('Not compiled with TLS support -- skipping test');
process.exit(0);
2009-11-22 03:58:08 +01:00
}