1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var common = require('../common');
24 if (!common.opensslCli) {
25 console.error('Skipping because node compiled without OpenSSL CLI.');
29 doTest({ tickets: false } , function() {
30 doTest({ tickets: true } , function() {
31 console.error('all done');
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;
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);
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')
67 server.on('newSession', function(id, data, cb) {
68 // Emulate asynchronous store
69 setTimeout(function() {
78 server.on('resumeSession', function(id, callback) {
81 assert.equal(session.id.toString('hex'), id.toString('hex'));
83 // Just to check that async really works there
84 setTimeout(function() {
85 callback(null, session.data);
88 server.listen(common.PORT, function() {
89 var client = spawn(common.opensslCli, [
92 '-connect', 'localhost:' + common.PORT,
93 '-servername', 'ohgod',
94 '-key', join(common.fixturesDir, 'agent.key'),
95 '-cert', join(common.fixturesDir, 'agent.crt'),
97 ].concat(testOptions.tickets ? [] : '-no_ticket'), {
98 stdio: [ 0, 1, 'pipe' ]
101 client.stderr.setEncoding('utf8');
102 client.stderr.on('data', function(chunk) {
105 client.on('exit', function(code) {
106 console.error('done');
107 assert.equal(code, 0);
108 server.close(function() {
109 setTimeout(callback, 100);
114 process.on('exit', function() {
115 if (testOptions.tickets) {
116 assert.equal(requestCount, 6);
117 assert.equal(resumeCount, 0);
119 // initial request + reconnect requests (5 times)
121 assert.equal(requestCount, 6);
122 assert.equal(resumeCount, 5);