summaryrefslogtreecommitdiff
path: root/test/parallel/test-tls-interleave.js
blob: 0358c092b901fa2692220cab3f0877fd8572a747 (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
var common = require('../common');
var assert = require('assert');

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

var PORT = common.PORT;
var dir = common.fixturesDir;
var options = { key: fs.readFileSync(dir + '/test_key.pem'),
                cert: fs.readFileSync(dir + '/test_cert.pem'),
                ca: [ fs.readFileSync(dir + '/test_ca.pem') ] };

var writes = [
  'some server data',
  'and a separate packet',
  'and one more',
];
var receivedWrites = 0;

var server = tls.createServer(options, function(c) {
  writes.forEach(function(str) {
    c.write(str);
  });
}).listen(PORT, function() {
  var c = tls.connect(PORT, { rejectUnauthorized: false }, function() {
    c.write('some client data');
    c.on('readable', function() {
      var data = c.read();
      if (data === null)
        return;

      data = data.toString();
      while (data.length !== 0) {
        assert.strictEqual(data.indexOf(writes[receivedWrites]), 0);
        data = data.slice(writes[receivedWrites].length);

        if (++receivedWrites === writes.length) {
          c.end();
          server.close();
        }
      }
    });
  });
});

process.on('exit', function() {
  assert.equal(receivedWrites, writes.length);
});