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// Server sends a large string. Client counts bytes and pauses every few
24// seconds. Makes sure that pause and resume work properly.
25
26const common = require('../common');
27if (!common.hasCrypto)
28  common.skip('missing crypto');
29
30const assert = require('assert');
31const tls = require('tls');
32const fixtures = require('../common/fixtures');
33
34process.stdout.write('build body...');
35const body = 'hello world\n'.repeat(1024 * 1024);
36process.stdout.write('done\n');
37
38const options = {
39  key: fixtures.readKey('agent2-key.pem'),
40  cert: fixtures.readKey('agent2-cert.pem'),
41};
42
43const server = tls.Server(options, common.mustCall(function(socket) {
44  socket.end(body);
45}));
46
47let recvCount = 0;
48
49server.listen(0, function() {
50  const client = tls.connect({
51    port: server.address().port,
52    rejectUnauthorized: false,
53  });
54
55  client.on('data', function(d) {
56    process.stdout.write('.');
57    recvCount += d.length;
58
59    client.pause();
60    process.nextTick(function() {
61      client.resume();
62    });
63  });
64
65
66  client.on('close', function() {
67    console.error('close');
68    server.close();
69    clearTimeout(timeout);
70  });
71});
72
73
74function displayCounts() {
75  console.log(`body.length: ${body.length}`);
76  console.log(`  recvCount: ${recvCount}`);
77}
78
79
80const timeout = setTimeout(displayCounts, 10 * 1000);
81
82
83process.on('exit', function() {
84  displayCounts();
85  assert.strictEqual(body.length, recvCount);
86});
87