summaryrefslogtreecommitdiff
path: root/test/parallel/test-tls-alert-handling.js
blob: 3f3ddd1955a94a8389b7d179d9853c5caff6f189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
var common = require('../common');
var assert = require('assert');

if (!common.opensslCli) {
  common.skip('node compiled without OpenSSL CLI.');
  return;
}

if (!common.hasCrypto) {
  common.skip('missing crypto');
  return;
}

var tls = require('tls');
var net = require('net');
var fs = require('fs');

var success = false;

function filenamePEM(n) {
  return require('path').join(common.fixturesDir, 'keys', n + '.pem');
}

function loadPEM(n) {
  return fs.readFileSync(filenamePEM(n));
}

var opts = {
  key: loadPEM('agent2-key'),
  cert: loadPEM('agent2-cert')
};

var max_iter = 20;
var iter = 0;

var server = tls.createServer(opts, function(s) {
  s.pipe(s);
  s.on('error', function(e) {
    // ignore error
  });
});

server.listen(0, function() {
  sendClient();
});


function sendClient() {
  var client = tls.connect(server.address().port, {
    rejectUnauthorized: false
  });
  client.on('data', function(chunk) {
    if (iter++ === 2) sendBADTLSRecord();
    if (iter < max_iter) {
      client.write('a');
      return;
    }
    client.end();
    server.close();
    success = true;
  });
  client.write('a');
  client.on('error', function(e) {
    // ignore error
  });
  client.on('close', function() {
    server.close();
  });
}


function sendBADTLSRecord() {
  var BAD_RECORD = Buffer.from([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
  var socket = net.connect(server.address().port);
  var client = tls.connect({
    socket: socket,
    rejectUnauthorized: false
  }, function() {
    socket.write(BAD_RECORD);
    socket.end();
  });
  client.on('error', function(e) {
    // ignore error
  });
}

process.on('exit', function() {
  assert(iter === max_iter);
  assert(success);
});