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');
24const fixtures = require('../common/fixtures');
25
26if (!common.hasCrypto)
27  common.skip('missing crypto');
28
29const assert = require('assert');
30const tls = require('tls');
31
32const buf = Buffer.allocUnsafe(10000);
33let received = 0;
34const maxChunk = 768;
35
36const invalidArgumentError = {
37  name: 'TypeError',
38  code: 'ERR_INVALID_ARG_TYPE'
39};
40
41const server = tls.createServer({
42  key: fixtures.readKey('agent1-key.pem'),
43  cert: fixtures.readKey('agent1-cert.pem')
44}, function(c) {
45
46  // No size is passed.
47  assert.throws(() => c.setMaxSendFragment(), invalidArgumentError);
48
49  // Invalid arg is passed.
50  [null, undefined, '', {}, false, true, []].forEach((arg) => {
51    assert.throws(() => c.setMaxSendFragment(arg), invalidArgumentError);
52  });
53
54  [NaN, Infinity, 2 ** 31].forEach((arg) => {
55    assert.throws(() => c.setMaxSendFragment(arg), {
56      name: 'RangeError',
57      code: 'ERR_OUT_OF_RANGE'
58    });
59  });
60
61  assert.throws(() => c.setMaxSendFragment(Symbol()), { name: 'TypeError' });
62
63  // Lower and upper limits.
64  assert(!c.setMaxSendFragment(511));
65  assert(!c.setMaxSendFragment(16385));
66
67  // Correct fragment size.
68  assert(c.setMaxSendFragment(maxChunk));
69
70  c.end(buf);
71}).listen(0, common.mustCall(function() {
72  const c = tls.connect(this.address().port, {
73    rejectUnauthorized: false
74  }, common.mustCall(function() {
75    c.on('data', function(chunk) {
76      assert(chunk.length <= maxChunk);
77      received += chunk.length;
78    });
79
80    // Ensure that we receive 'end' event anyway
81    c.on('end', common.mustCall(function() {
82      c.destroy();
83      server.close();
84      assert.strictEqual(received, buf.length);
85    }));
86  }));
87}));
88