|
| 1 | +var common = require('../common'); |
| 2 | +var assert = require('assert'); |
| 3 | + |
| 4 | +if (!common.opensslCli) { |
| 5 | + console.error('Skipping because node compiled without OpenSSL CLI.'); |
| 6 | + process.exit(0); |
| 7 | +} |
| 8 | + |
| 9 | +if (!common.hasCrypto) { |
| 10 | + console.log('1..0 # Skipped: missing crypto'); |
| 11 | + process.exit(); |
| 12 | +} |
| 13 | + |
| 14 | +var tls = require('tls'); |
| 15 | +var net = require('net'); |
| 16 | +var fs = require('fs'); |
| 17 | + |
| 18 | +var success = false; |
| 19 | + |
| 20 | +function filenamePEM(n) { |
| 21 | + return require('path').join(common.fixturesDir, 'keys', n + '.pem'); |
| 22 | +} |
| 23 | + |
| 24 | +function loadPEM(n) { |
| 25 | + return fs.readFileSync(filenamePEM(n)); |
| 26 | +} |
| 27 | + |
| 28 | +var opts = { |
| 29 | + key: loadPEM('agent2-key'), |
| 30 | + cert: loadPEM('agent2-cert') |
| 31 | +}; |
| 32 | + |
| 33 | +var max_iter = 20; |
| 34 | +var iter = 0; |
| 35 | + |
| 36 | +var server = tls.createServer(opts, function(s) { |
| 37 | + s.pipe(s); |
| 38 | + s.on('error', function(e) { |
| 39 | + // ignore error |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +server.listen(common.PORT, function() { |
| 44 | + sendClient(); |
| 45 | +}); |
| 46 | + |
| 47 | + |
| 48 | +function sendClient() { |
| 49 | + var client = tls.connect(common.PORT, { |
| 50 | + rejectUnauthorized: false |
| 51 | + }); |
| 52 | + client.on('data', function(chunk) { |
| 53 | + if (iter++ === 2) sendBADTLSRecord(); |
| 54 | + if (iter < max_iter) { |
| 55 | + client.write('a'); |
| 56 | + return; |
| 57 | + } |
| 58 | + client.end(); |
| 59 | + server.close(); |
| 60 | + success = true; |
| 61 | + }); |
| 62 | + client.write('a'); |
| 63 | + client.on('error', function(e) { |
| 64 | + // ignore error |
| 65 | + }); |
| 66 | + client.on('close', function() { |
| 67 | + server.close(); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +function sendBADTLSRecord() { |
| 73 | + var BAD_RECORD = new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); |
| 74 | + var socket = net.connect(common.PORT); |
| 75 | + var client = tls.connect({ |
| 76 | + socket: socket, |
| 77 | + rejectUnauthorized: false |
| 78 | + }, function() { |
| 79 | + socket.write(BAD_RECORD); |
| 80 | + socket.end(); |
| 81 | + }); |
| 82 | + client.on('error', function(e) { |
| 83 | + // ignore error |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +process.on('exit', function() { |
| 88 | + assert(iter === max_iter); |
| 89 | + assert(success); |
| 90 | +}); |
0 commit comments