d5db30ce11040639d915e7ada1bdedb1fe981f9f
[platform/upstream/nodejs.git] / test / parallel / test-tls-session-cache.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 doTest({ tickets: false } , function() {
30   doTest({ tickets: true } , function() {
31     console.error('all done');
32   });
33 });
34
35 function doTest(testOptions, callback) {
36   var assert = require('assert');
37   var tls = require('tls');
38   var fs = require('fs');
39   var join = require('path').join;
40   var spawn = require('child_process').spawn;
41
42   var keyFile = join(common.fixturesDir, 'agent.key');
43   var certFile = join(common.fixturesDir, 'agent.crt');
44   var key = fs.readFileSync(keyFile);
45   var cert = fs.readFileSync(certFile);
46   var options = {
47     key: key,
48     cert: cert,
49     ca: [cert],
50     requestCert: true
51   };
52   var requestCount = 0;
53   var resumeCount = 0;
54   var session;
55
56   var server = tls.createServer(options, function(cleartext) {
57     cleartext.on('error', function(er) {
58       // We're ok with getting ECONNRESET in this test, but it's
59       // timing-dependent, and thus unreliable. Any other errors
60       // are just failures, though.
61       if (er.code !== 'ECONNRESET')
62         throw er;
63     });
64     ++requestCount;
65     cleartext.end();
66   });
67   server.on('newSession', function(id, data, cb) {
68     // Emulate asynchronous store
69     setTimeout(function() {
70       assert.ok(!session);
71       session = {
72         id: id,
73         data: data
74       };
75       cb();
76     }, 1000);
77   });
78   server.on('resumeSession', function(id, callback) {
79     ++resumeCount;
80     assert.ok(session);
81     assert.equal(session.id.toString('hex'), id.toString('hex'));
82
83     // Just to check that async really works there
84     setTimeout(function() {
85       callback(null, session.data);
86     }, 100);
87   });
88   server.listen(common.PORT, function() {
89     var client = spawn(common.opensslCli, [
90       's_client',
91       '-tls1',
92       '-connect', 'localhost:' + common.PORT,
93       '-servername', 'ohgod',
94       '-key', join(common.fixturesDir, 'agent.key'),
95       '-cert', join(common.fixturesDir, 'agent.crt'),
96       '-reconnect'
97     ].concat(testOptions.tickets ? [] : '-no_ticket'), {
98       stdio: [ 0, 1, 'pipe' ]
99     });
100     var err = '';
101     client.stderr.setEncoding('utf8');
102     client.stderr.on('data', function(chunk) {
103       err += chunk;
104     });
105     client.on('exit', function(code) {
106       console.error('done');
107       assert.equal(code, 0);
108       server.close(function() {
109         setTimeout(callback, 100);
110       });
111     });
112   });
113
114   process.on('exit', function() {
115     if (testOptions.tickets) {
116       assert.equal(requestCount, 6);
117       assert.equal(resumeCount, 0);
118     } else {
119       // initial request + reconnect requests (5 times)
120       assert.ok(session);
121       assert.equal(requestCount, 6);
122       assert.equal(resumeCount, 5);
123     }
124   });
125 }