• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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'use strict';
23const common = require('../common');
24if (!common.hasCrypto)
25  common.skip('missing crypto');
26const fixtures = require('../common/fixtures');
27
28const assert = require('assert');
29const tls = require('tls');
30const net = require('net');
31
32const options = {
33  key: fixtures.readKey('rsa_private.pem'),
34  cert: fixtures.readKey('rsa_cert.crt')
35};
36
37const server = tls.createServer(options, common.mustCall((socket) => {
38  socket.end('Hello');
39}, 2)).listen(0, common.mustCall(() => {
40  let waiting = 2;
41  function establish(socket, calls) {
42    const client = tls.connect({
43      rejectUnauthorized: false,
44      socket: socket
45    }, common.mustCall(() => {
46      let data = '';
47      client.on('data', common.mustCall((chunk) => {
48        data += chunk.toString();
49      }));
50      client.on('end', common.mustCall(() => {
51        assert.strictEqual(data, 'Hello');
52        if (--waiting === 0)
53          server.close();
54      }));
55    }, calls));
56    assert(client.readable);
57    assert(client.writable);
58
59    return client;
60  }
61
62  const { port } = server.address();
63
64  // Immediate death socket
65  const immediateDeath = net.connect(port);
66  establish(immediateDeath, 0).destroy();
67
68  // Outliving
69  const outlivingTCP = net.connect(port, common.mustCall(() => {
70    outlivingTLS.destroy();
71    next();
72  }));
73  const outlivingTLS = establish(outlivingTCP, 0);
74
75  function next() {
76    // Already connected socket
77    const connected = net.connect(port, common.mustCall(() => {
78      establish(connected);
79    }));
80
81    // Connecting socket
82    const connecting = net.connect(port);
83    establish(connecting);
84  }
85}));
86