• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3if (!common.hasCrypto)
4  common.skip('missing crypto');
5
6const assert = require('assert');
7const tls = require('tls');
8const fixtures = require('../common/fixtures');
9
10const options = {
11  key: fixtures.readKey('agent2-key.pem'),
12  cert: fixtures.readKey('agent2-cert.pem')
13};
14
15const smallMessage = Buffer.from('hello world');
16// Used to test .pause(), so needs to be larger than the internal buffer
17const largeMessage = Buffer.alloc(64 * 1024).fill('hello world');
18
19// Test typical usage
20tls.createServer(options, common.mustCall(function(socket) {
21  this.close();
22  socket.end(smallMessage);
23})).listen(0, function() {
24  let received = 0;
25  const buffers = [];
26  const sockBuf = Buffer.alloc(8);
27  tls.connect({
28    port: this.address().port,
29    rejectUnauthorized: false,
30    onread: {
31      buffer: sockBuf,
32      callback: function(nread, buf) {
33        assert.strictEqual(buf, sockBuf);
34        received += nread;
35        buffers.push(Buffer.from(buf.slice(0, nread)));
36      }
37    }
38  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
39    assert.strictEqual(received, smallMessage.length);
40    assert.deepStrictEqual(Buffer.concat(buffers), smallMessage);
41  }));
42});
43
44// Test Uint8Array support
45tls.createServer(options, common.mustCall(function(socket) {
46  this.close();
47  socket.end(smallMessage);
48})).listen(0, function() {
49  let received = 0;
50  let incoming = new Uint8Array(0);
51  const sockBuf = new Uint8Array(8);
52  tls.connect({
53    port: this.address().port,
54    rejectUnauthorized: false,
55    onread: {
56      buffer: sockBuf,
57      callback: function(nread, buf) {
58        assert.strictEqual(buf, sockBuf);
59        received += nread;
60        const newIncoming = new Uint8Array(incoming.length + nread);
61        newIncoming.set(incoming);
62        newIncoming.set(buf.slice(0, nread), incoming.length);
63        incoming = newIncoming;
64      }
65    }
66  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
67    assert.strictEqual(received, smallMessage.length);
68    assert.deepStrictEqual(incoming, new Uint8Array(smallMessage));
69  }));
70});
71
72// Test Buffer callback usage
73tls.createServer(options, common.mustCall(function(socket) {
74  this.close();
75  socket.end(smallMessage);
76})).listen(0, function() {
77  let received = 0;
78  const incoming = [];
79  const bufPool = [ Buffer.alloc(2), Buffer.alloc(2), Buffer.alloc(2) ];
80  let bufPoolIdx = -1;
81  let bufPoolUsage = 0;
82  tls.connect({
83    port: this.address().port,
84    rejectUnauthorized: false,
85    onread: {
86      buffer: () => {
87        ++bufPoolUsage;
88        bufPoolIdx = (bufPoolIdx + 1) % bufPool.length;
89        return bufPool[bufPoolIdx];
90      },
91      callback: function(nread, buf) {
92        assert.strictEqual(buf, bufPool[bufPoolIdx]);
93        received += nread;
94        incoming.push(Buffer.from(buf.slice(0, nread)));
95      }
96    }
97  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
98    assert.strictEqual(received, smallMessage.length);
99    assert.deepStrictEqual(Buffer.concat(incoming), smallMessage);
100    assert.strictEqual(bufPoolUsage, 7);
101  }));
102});
103
104// Test Uint8Array callback support
105tls.createServer(options, common.mustCall(function(socket) {
106  this.close();
107  socket.end(smallMessage);
108})).listen(0, function() {
109  let received = 0;
110  let incoming = new Uint8Array(0);
111  const bufPool = [ new Uint8Array(2), new Uint8Array(2), new Uint8Array(2) ];
112  let bufPoolIdx = -1;
113  let bufPoolUsage = 0;
114  tls.connect({
115    port: this.address().port,
116    rejectUnauthorized: false,
117    onread: {
118      buffer: () => {
119        ++bufPoolUsage;
120        bufPoolIdx = (bufPoolIdx + 1) % bufPool.length;
121        return bufPool[bufPoolIdx];
122      },
123      callback: function(nread, buf) {
124        assert.strictEqual(buf, bufPool[bufPoolIdx]);
125        received += nread;
126        const newIncoming = new Uint8Array(incoming.length + nread);
127        newIncoming.set(incoming);
128        newIncoming.set(buf.slice(0, nread), incoming.length);
129        incoming = newIncoming;
130      }
131    }
132  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
133    assert.strictEqual(received, smallMessage.length);
134    assert.deepStrictEqual(incoming, new Uint8Array(smallMessage));
135    assert.strictEqual(bufPoolUsage, 7);
136  }));
137});
138
139// Test explicit socket pause
140tls.createServer(options, common.mustCall(function(socket) {
141  this.close();
142  // Need larger message here to observe the pause
143  socket.end(largeMessage);
144})).listen(0, function() {
145  let received = 0;
146  const buffers = [];
147  const sockBuf = Buffer.alloc(64);
148  let pauseScheduled = false;
149  const client = tls.connect({
150    port: this.address().port,
151    rejectUnauthorized: false,
152    onread: {
153      buffer: sockBuf,
154      callback: function(nread, buf) {
155        assert.strictEqual(buf, sockBuf);
156        received += nread;
157        buffers.push(Buffer.from(buf.slice(0, nread)));
158        if (!pauseScheduled) {
159          pauseScheduled = true;
160          client.pause();
161          setTimeout(() => {
162            client.resume();
163          }, 100);
164        }
165      }
166    }
167  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
168    assert.strictEqual(received, largeMessage.length);
169    assert.deepStrictEqual(Buffer.concat(buffers), largeMessage);
170  }));
171});
172
173// Test implicit socket pause
174tls.createServer(options, common.mustCall(function(socket) {
175  this.close();
176  // Need larger message here to observe the pause
177  socket.end(largeMessage);
178})).listen(0, function() {
179  let received = 0;
180  const buffers = [];
181  const sockBuf = Buffer.alloc(64);
182  let pauseScheduled = false;
183  const client = tls.connect({
184    port: this.address().port,
185    rejectUnauthorized: false,
186    onread: {
187      buffer: sockBuf,
188      callback: function(nread, buf) {
189        assert.strictEqual(buf, sockBuf);
190        received += nread;
191        buffers.push(Buffer.from(buf.slice(0, nread)));
192        if (!pauseScheduled) {
193          pauseScheduled = true;
194          setTimeout(() => {
195            client.resume();
196          }, 100);
197          return false;
198        }
199        return true;
200      }
201    }
202  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
203    assert.strictEqual(received, largeMessage.length);
204    assert.deepStrictEqual(Buffer.concat(buffers), largeMessage);
205  }));
206});
207