1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const assert = require('assert');
8const tls = require('tls');
9
10assert.throws(
11  () => tls.createSecureContext({ ciphers: 1 }),
12  {
13    code: 'ERR_INVALID_ARG_TYPE',
14    name: 'TypeError',
15    message: 'The "options.ciphers" property must be of type string.' +
16      ' Received type number (1)'
17  });
18
19assert.throws(
20  () => tls.createServer({ ciphers: 1 }),
21  {
22    code: 'ERR_INVALID_ARG_TYPE',
23    name: 'TypeError',
24    message: 'The "options.ciphers" property must be of type string.' +
25      ' Received type number (1)'
26  });
27
28assert.throws(
29  () => tls.createSecureContext({ key: 'dummykey', passphrase: 1 }),
30  {
31    code: 'ERR_INVALID_ARG_TYPE',
32    name: 'TypeError',
33    message: /The "options\.passphrase" property must be of type string/
34  });
35
36assert.throws(
37  () => tls.createServer({ key: 'dummykey', passphrase: 1 }),
38  {
39    code: 'ERR_INVALID_ARG_TYPE',
40    name: 'TypeError',
41    message: /The "options\.passphrase" property must be of type string/
42  });
43
44assert.throws(
45  () => tls.createServer({ ecdhCurve: 1 }),
46  {
47    code: 'ERR_INVALID_ARG_TYPE',
48    name: 'TypeError',
49    message: /The "options\.ecdhCurve" property must be of type string/
50  });
51
52assert.throws(
53  () => tls.createServer({ handshakeTimeout: 'abcd' }),
54  {
55    code: 'ERR_INVALID_ARG_TYPE',
56    name: 'TypeError',
57    message: 'The "options.handshakeTimeout" property must be of type number.' +
58              " Received type string ('abcd')"
59  }
60);
61
62assert.throws(
63  () => tls.createServer({ sessionTimeout: 'abcd' }),
64  {
65    code: 'ERR_INVALID_ARG_TYPE',
66    name: 'TypeError',
67    message: /The "options\.sessionTimeout" property must be of type number/
68  });
69
70assert.throws(
71  () => tls.createServer({ ticketKeys: 'abcd' }),
72  {
73    code: 'ERR_INVALID_ARG_TYPE',
74    name: 'TypeError',
75    message: /The "options\.ticketKeys" property must be an instance of/
76  });
77
78assert.throws(() => tls.createServer({ ticketKeys: Buffer.alloc(0) }), {
79  code: 'ERR_INVALID_ARG_VALUE',
80  message: /The property 'options\.ticketKeys' must be exactly 48 bytes/
81});
82
83assert.throws(
84  () => tls.createSecurePair({}),
85  {
86    message: 'context must be a SecureContext',
87    code: 'ERR_TLS_INVALID_CONTEXT',
88    name: 'TypeError',
89  }
90);
91
92{
93  const buffer = Buffer.from('abcd');
94  const out = {};
95  tls.convertALPNProtocols(buffer, out);
96  out.ALPNProtocols.write('efgh');
97  assert(buffer.equals(Buffer.from('abcd')));
98  assert(out.ALPNProtocols.equals(Buffer.from('efgh')));
99}
100
101{
102  const arrayBufferViewStr = 'abcd';
103  const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
104  for (const expectView of common.getArrayBufferViews(inputBuffer)) {
105    const out = {};
106    const expected = Buffer.from(expectView.buffer.slice(),
107                                 expectView.byteOffset,
108                                 expectView.byteLength);
109    tls.convertALPNProtocols(expectView, out);
110    assert(out.ALPNProtocols.equals(expected));
111  }
112}
113
114{
115  const protocols = [(new String('a')).repeat(500)];
116  const out = {};
117  assert.throws(
118    () => tls.convertALPNProtocols(protocols, out),
119    {
120      code: 'ERR_OUT_OF_RANGE',
121      message: 'The byte length of the protocol at index 0 exceeds the ' +
122        'maximum length. It must be <= 255. Received 500'
123    }
124  );
125}
126
127assert.throws(() => { tls.createSecureContext({ minVersion: 'fhqwhgads' }); },
128              {
129                code: 'ERR_TLS_INVALID_PROTOCOL_VERSION',
130                name: 'TypeError'
131              });
132
133assert.throws(() => { tls.createSecureContext({ maxVersion: 'fhqwhgads' }); },
134              {
135                code: 'ERR_TLS_INVALID_PROTOCOL_VERSION',
136                name: 'TypeError'
137              });
138