48344bae0d3f3c0c132cd93675d1f29a0a1e1e1b
[platform/upstream/nodejs.git] / test / disabled / test-net-tls.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
23
24
25 var common = require('../common');
26 var assert = require('assert');
27 var fs = require('fs');
28 var net = require('net');
29
30 var have_openssl;
31 try {
32   var crypto = require('crypto');
33   have_openssl = true;
34 } catch (e) {
35   have_openssl = false;
36   console.log('Not compiled with OPENSSL support.');
37   process.exit();
38 }
39
40 var caPem = fs.readFileSync(common.fixturesDir + '/test_ca.pem', 'ascii');
41 var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
42 var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
43
44 try {
45   var credentials = crypto.createCredentials(
46                                              { key: keyPem,
47                                                cert: certPem,
48                                                ca: caPem
49                                              });
50 } catch (e) {
51   console.log('Not compiled with OPENSSL support.');
52   process.exit();
53 }
54
55 var testData = 'TEST123';
56 var serverData = '';
57 var clientData = '';
58 var gotSecureServer = false;
59 var gotSecureClient = false;
60
61 var secureServer = net.createServer(function(connection) {
62   var self = this;
63   connection.setSecure(credentials);
64   connection.setEncoding('UTF8');
65
66   connection.on('secure', function() {
67     gotSecureServer = true;
68     var verified = connection.verifyPeer();
69     var peerDN = JSON.stringify(connection.getPeerCertificate());
70     assert.equal(verified, true);
71     assert.equal(peerDN,
72                  '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' +
73                  '/O=node.js/OU=Test TLS Certificate/CN=localhost",' +
74                  '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' +
75                  '/OU=Test TLS Certificate/CN=localhost",' +
76                  '"valid_from":"Nov 11 09:52:22 2009 GMT",' +
77                  '"valid_to":"Nov  6 09:52:22 2029 GMT",' +
78                  '"fingerprint":"2A:7A:C2:DD:E5:F9:CC:53:72:35:99:7A:02:' +
79                  '5A:71:38:52:EC:8A:DF"}');
80
81   });
82
83   connection.on('data', function(chunk) {
84     serverData += chunk;
85     connection.write(chunk);
86   });
87
88   connection.on('end', function() {
89     assert.equal(serverData, testData);
90     connection.end();
91     self.close();
92   });
93 });
94 secureServer.listen(common.PORT);
95
96 secureServer.on('listening', function() {
97   var secureClient = net.createConnection(common.PORT);
98
99   secureClient.setEncoding('UTF8');
100   secureClient.on('connect', function() {
101     secureClient.setSecure(credentials);
102   });
103
104   secureClient.on('secure', function() {
105     gotSecureClient = true;
106     var verified = secureClient.verifyPeer();
107     var peerDN = JSON.stringify(secureClient.getPeerCertificate());
108     assert.equal(verified, true);
109     assert.equal(peerDN,
110                  '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' +
111                  '/O=node.js/OU=Test TLS Certificate/CN=localhost",' +
112                  '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' +
113                  '/OU=Test TLS Certificate/CN=localhost",' +
114                  '"valid_from":"Nov 11 09:52:22 2009 GMT",' +
115                  '"valid_to":"Nov  6 09:52:22 2029 GMT",' +
116                  '"fingerprint":"2A:7A:C2:DD:E5:F9:CC:53:72:35:99:7A:02:' +
117                  '5A:71:38:52:EC:8A:DF"}');
118
119     secureClient.write(testData);
120     secureClient.end();
121   });
122
123   secureClient.on('data', function(chunk) {
124     clientData += chunk;
125   });
126
127   secureClient.on('end', function() {
128     assert.equal(clientData, testData);
129   });
130 });
131
132 process.on('exit', function() {
133   assert.ok(gotSecureServer, 'Did not get secure event for server');
134   assert.ok(gotSecureClient, 'Did not get secure event for client');
135 });