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.js

133 lines
4.7 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.
2010-12-05 00:20:34 +01:00
var common = require('../common');
var assert = require('assert');
2010-05-04 00:37:49 +02:00
var fs = require('fs');
var net = require('net');
var have_openssl;
try {
var crypto = require('crypto');
have_openssl = true;
2010-05-04 00:37:49 +02:00
} catch (e) {
have_openssl = false;
console.log('Not compiled with OPENSSL support.');
2010-05-04 00:37:49 +02:00
process.exit();
2010-06-30 08:12:46 +02:00
}
2010-05-04 00:37:49 +02:00
var caPem = fs.readFileSync(common.fixturesDir + '/test_ca.pem', 'ascii');
var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
2010-05-04 00:37:49 +02:00
try {
var credentials = crypto.createCredentials(
{ key: keyPem,
cert: certPem,
ca: caPem
});
} catch (e) {
console.log('Not compiled with OPENSSL support.');
process.exit();
}
2010-05-04 00:37:49 +02:00
var testData = 'TEST123';
2010-05-04 00:37:49 +02:00
var serverData = '';
var clientData = '';
var gotSecureServer = false;
var gotSecureClient = false;
var secureServer = net.createServer(function(connection) {
2010-05-04 00:37:49 +02:00
var self = this;
connection.setSecure(credentials);
connection.setEncoding('UTF8');
2010-05-04 00:37:49 +02:00
connection.addListener('secure', function() {
2010-05-04 00:37:49 +02:00
gotSecureServer = true;
var verified = connection.verifyPeer();
var peerDN = JSON.stringify(connection.getPeerCertificate());
assert.equal(verified, true);
assert.equal(peerDN,
'{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' +
'/O=node.js/OU=Test TLS Certificate/CN=localhost",' +
'"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' +
'/OU=Test TLS Certificate/CN=localhost",' +
'"valid_from":"Nov 11 09:52:22 2009 GMT",' +
'"valid_to":"Nov 6 09:52:22 2029 GMT",' +
'"fingerprint":"2A:7A:C2:DD:E5:F9:CC:53:72:35:99:7A:02:' +
'5A:71:38:52:EC:8A:DF"}');
2010-05-04 00:37:49 +02:00
});
connection.addListener('data', function(chunk) {
2010-05-04 00:37:49 +02:00
serverData += chunk;
connection.write(chunk);
});
connection.addListener('end', function() {
2010-05-04 00:37:49 +02:00
assert.equal(serverData, testData);
connection.end();
self.close();
});
});
secureServer.listen(common.PORT);
2010-05-04 00:37:49 +02:00
secureServer.addListener('listening', function() {
var secureClient = net.createConnection(common.PORT);
2010-05-04 00:37:49 +02:00
secureClient.setEncoding('UTF8');
secureClient.addListener('connect', function() {
secureClient.setSecure(credentials);
});
2010-05-04 00:37:49 +02:00
secureClient.addListener('secure', function() {
gotSecureClient = true;
var verified = secureClient.verifyPeer();
var peerDN = JSON.stringify(secureClient.getPeerCertificate());
assert.equal(verified, true);
assert.equal(peerDN,
'{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' +
'/O=node.js/OU=Test TLS Certificate/CN=localhost",' +
'"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' +
'/OU=Test TLS Certificate/CN=localhost",' +
'"valid_from":"Nov 11 09:52:22 2009 GMT",' +
'"valid_to":"Nov 6 09:52:22 2029 GMT",' +
'"fingerprint":"2A:7A:C2:DD:E5:F9:CC:53:72:35:99:7A:02:' +
'5A:71:38:52:EC:8A:DF"}');
secureClient.write(testData);
secureClient.end();
});
2010-05-04 00:37:49 +02:00
secureClient.addListener('data', function(chunk) {
clientData += chunk;
});
2010-05-04 00:37:49 +02:00
secureClient.addListener('end', function() {
assert.equal(clientData, testData);
});
2010-05-04 00:37:49 +02:00
});
process.addListener('exit', function() {
assert.ok(gotSecureServer, 'Did not get secure event for server');
assert.ok(gotSecureClient, 'Did not get secure event for client');
});