ece965c544ebb871ff4b7bac8c3cf7d7d145ebc9
[platform/upstream/nodejs.git] / test / parallel / test-tls-securepair-server.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 var common = require('../common');
23
24 if (!common.opensslCli) {
25   console.error('Skipping because node compiled without OpenSSL CLI.');
26   process.exit(0);
27 }
28
29 var assert = require('assert');
30
31 var join = require('path').join;
32 var net = require('net');
33 var fs = require('fs');
34 var tls = require('tls');
35 var spawn = require('child_process').spawn;
36
37 var connections = 0;
38 var key = fs.readFileSync(join(common.fixturesDir, 'agent.key')).toString();
39 var cert = fs.readFileSync(join(common.fixturesDir, 'agent.crt')).toString();
40
41 function log(a) {
42   console.error('***server*** ' + a);
43 }
44
45 var server = net.createServer(function(socket) {
46   connections++;
47   log('connection fd=' + socket.fd);
48   var sslcontext = tls.createSecureContext({key: key, cert: cert});
49   sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
50
51   var pair = tls.createSecurePair(sslcontext, true);
52
53   assert.ok(pair.encrypted.writable);
54   assert.ok(pair.cleartext.writable);
55
56   pair.encrypted.pipe(socket);
57   socket.pipe(pair.encrypted);
58
59   log('i set it secure');
60
61   pair.on('secure', function() {
62     log('connected+secure!');
63     pair.cleartext.write('hello\r\n');
64     log(pair.cleartext.getPeerCertificate());
65     log(pair.cleartext.getCipher());
66   });
67
68   pair.cleartext.on('data', function(data) {
69     log('read bytes ' + data.length);
70     pair.cleartext.write(data);
71   });
72
73   socket.on('end', function() {
74     log('socket end');
75   });
76
77   pair.cleartext.on('error', function(err) {
78     log('got error: ');
79     log(err);
80     log(err.stack);
81     socket.destroy();
82   });
83
84   pair.encrypted.on('error', function(err) {
85     log('encrypted error: ');
86     log(err);
87     log(err.stack);
88     socket.destroy();
89   });
90
91   socket.on('error', function(err) {
92     log('socket error: ');
93     log(err);
94     log(err.stack);
95     socket.destroy();
96   });
97
98   socket.on('close', function(err) {
99     log('socket closed');
100   });
101
102   pair.on('error', function(err) {
103     log('secure error: ');
104     log(err);
105     log(err.stack);
106     socket.destroy();
107   });
108 });
109
110 var gotHello = false;
111 var sentWorld = false;
112 var gotWorld = false;
113 var opensslExitCode = -1;
114
115 server.listen(common.PORT, function() {
116   // To test use: openssl s_client -connect localhost:8000
117   var client = spawn(common.opensslCli, ['s_client', '-connect', '127.0.0.1:' +
118         common.PORT]);
119
120
121   var out = '';
122
123   client.stdout.setEncoding('utf8');
124   client.stdout.on('data', function(d) {
125     out += d;
126
127     if (!gotHello && /hello/.test(out)) {
128       gotHello = true;
129       client.stdin.write('world\r\n');
130       sentWorld = true;
131     }
132
133     if (!gotWorld && /world/.test(out)) {
134       gotWorld = true;
135       client.stdin.end();
136     }
137   });
138
139   client.stdout.pipe(process.stdout, { end: false });
140
141   client.on('exit', function(code) {
142     opensslExitCode = code;
143     server.close();
144   });
145 });
146
147 process.on('exit', function() {
148   assert.equal(1, connections);
149   assert.ok(gotHello);
150   assert.ok(sentWorld);
151   assert.ok(gotWorld);
152   assert.equal(0, opensslExitCode);
153 });