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';
23
24// Check that the ticket from the first connection causes session resumption
25// when used to make a second connection.
26
27const common = require('../common');
28if (!common.hasCrypto)
29  common.skip('missing crypto');
30
31const assert = require('assert');
32const tls = require('tls');
33const fixtures = require('../common/fixtures');
34
35const options = {
36  key: fixtures.readKey('agent2-key.pem'),
37  cert: fixtures.readKey('agent2-cert.pem')
38};
39
40// create server
41const server = tls.Server(options, common.mustCall((socket) => {
42  socket.end('Goodbye');
43}, 2));
44
45// start listening
46server.listen(0, common.mustCall(function() {
47  let sessionx = null; // From right after connect, invalid for TLS1.3
48  let session1 = null; // Delivered by the session event, always valid.
49  let sessions = 0;
50  let tls13;
51  const client1 = tls.connect({
52    port: this.address().port,
53    rejectUnauthorized: false
54  }, common.mustCall(() => {
55    tls13 = client1.getProtocol() === 'TLSv1.3';
56    assert.strictEqual(client1.isSessionReused(), false);
57    sessionx = client1.getSession();
58    assert(sessionx);
59
60    if (session1)
61      reconnect();
62  }));
63
64  client1.on('data', common.mustCall());
65
66  client1.once('session', common.mustCall((session) => {
67    console.log('session1');
68    session1 = session;
69    assert(session1);
70    if (sessionx)
71      reconnect();
72  }));
73
74  client1.on('session', () => {
75    console.log('client1 session#', ++sessions);
76  });
77
78  client1.on('close', () => {
79    console.log('client1 close');
80    assert.strictEqual(sessions, tls13 ? 2 : 1);
81  });
82
83  function reconnect() {
84    assert(sessionx);
85    assert(session1);
86    if (tls13)
87      // For TLS1.3, the session immediately after handshake is a dummy,
88      // unresumable session. The one delivered later in session event is
89      // resumable.
90      assert.notStrictEqual(sessionx.compare(session1), 0);
91    else
92      // For TLS1.2, they are identical.
93      assert.strictEqual(sessionx.compare(session1), 0);
94
95    const opts = {
96      port: server.address().port,
97      rejectUnauthorized: false,
98      session: session1,
99    };
100
101    const client2 = tls.connect(opts, common.mustCall(() => {
102      console.log('connect2');
103      assert.strictEqual(client2.isSessionReused(), true);
104    }));
105
106    client2.on('close', common.mustCall(() => {
107      console.log('close2');
108      server.close();
109    }));
110
111    client2.resume();
112  }
113
114  client1.resume();
115}));
116