|
| 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 | +if (!process.versions.openssl) { |
| 23 | + console.error('Skipping because node compiled without OpenSSL.'); |
| 24 | + process.exit(0); |
| 25 | +} |
| 26 | + |
| 27 | +var assert = require('assert'); |
| 28 | +var fs = require('fs'); |
| 29 | +var net = require('net'); |
| 30 | +var tls = require('tls'); |
| 31 | +var crypto = require('crypto'); |
| 32 | + |
| 33 | +var common = require('../common'); |
| 34 | + |
| 35 | +var keys = crypto.randomBytes(48); |
| 36 | +var serverLog = []; |
| 37 | +var ticketLog = []; |
| 38 | + |
| 39 | +var serverCount = 0; |
| 40 | +function createServer() { |
| 41 | + var id = serverCount++; |
| 42 | + |
| 43 | + var server = tls.createServer({ |
| 44 | + key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), |
| 45 | + cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem'), |
| 46 | + ticketKeys: keys |
| 47 | + }, function(c) { |
| 48 | + serverLog.push(id); |
| 49 | + c.end(); |
| 50 | + }); |
| 51 | + |
| 52 | + return server; |
| 53 | +} |
| 54 | + |
| 55 | +var servers = [ createServer(), createServer(), createServer(), createServer(), createServer(), createServer() ]; |
| 56 | + |
| 57 | +// Create one TCP server and balance sockets to multiple TLS server instances |
| 58 | +var shared = net.createServer(function(c) { |
| 59 | + servers.shift().emit('connection', c); |
| 60 | +}).listen(common.PORT, function() { |
| 61 | + start(function() { |
| 62 | + shared.close(); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +function start(callback) { |
| 67 | + var sess = null; |
| 68 | + var left = servers.length; |
| 69 | + |
| 70 | + function connect() { |
| 71 | + var s = tls.connect(common.PORT, { |
| 72 | + session: sess, |
| 73 | + rejectUnauthorized: false |
| 74 | + }, function() { |
| 75 | + sess = s.getSession() || sess; |
| 76 | + ticketLog.push(s.getTLSTicket().toString('hex')); |
| 77 | + }); |
| 78 | + s.on('close', function() { |
| 79 | + if (--left === 0) |
| 80 | + callback(); |
| 81 | + else |
| 82 | + connect(); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + connect(); |
| 87 | +} |
| 88 | + |
| 89 | +process.on('exit', function() { |
| 90 | + assert.equal(ticketLog.length, serverLog.length); |
| 91 | + for (var i = 0; i < serverLog.length - 1; i++) { |
| 92 | + assert.notEqual(serverLog[i], serverLog[i + 1]); |
| 93 | + assert.equal(ticketLog[i], ticketLog[i + 1]); |
| 94 | + } |
| 95 | +}); |
0 commit comments