1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
if (!common.hasCrypto)
common.skip('missing crypto');
const tls = require('tls');
function toArrayBuffer(buf) {
const ab = new ArrayBuffer(buf.length);
const view = new Uint8Array(ab);
return buf.map((b, i) => view[i] = b);
}
function toDataView(buf) {
const ab = new ArrayBuffer(buf.length);
const view = new DataView(ab);
return buf.map((b, i) => view[i] = b);
}
const keyBuff = fixtures.readKey('agent1-key.pem');
const certBuff = fixtures.readKey('agent1-cert.pem');
const keyBuff2 = fixtures.readKey('ec-key.pem');
const certBuff2 = fixtures.readKey('ec-cert.pem');
const caCert = fixtures.readKey('ca1-cert.pem');
const caCert2 = fixtures.readKey('ca2-cert.pem');
const keyStr = keyBuff.toString();
const certStr = certBuff.toString();
const keyStr2 = keyBuff2.toString();
const certStr2 = certBuff2.toString();
const caCertStr = caCert.toString();
const caCertStr2 = caCert2.toString();
const keyArrBuff = toArrayBuffer(keyBuff);
const certArrBuff = toArrayBuffer(certBuff);
const caArrBuff = toArrayBuffer(caCert);
const keyDataView = toDataView(keyBuff);
const certDataView = toDataView(certBuff);
const caArrDataView = toDataView(caCert);
// Checks to ensure tls.createServer doesn't throw an error
// Format ['key', 'cert']
[
[keyBuff, certBuff],
[false, certBuff],
[keyBuff, false],
[keyStr, certStr],
[false, certStr],
[keyStr, false],
[false, false],
[keyArrBuff, certArrBuff],
[keyArrBuff, false],
[false, certArrBuff],
[keyDataView, certDataView],
[keyDataView, false],
[false, certDataView],
[[keyBuff, keyBuff2], [certBuff, certBuff2]],
[[keyStr, keyStr2], [certStr, certStr2]],
[[keyStr, keyStr2], false],
[false, [certStr, certStr2]],
[[{ pem: keyBuff }], false],
[[{ pem: keyBuff }, { pem: keyBuff }], false]
].forEach(([key, cert]) => {
tls.createServer({ key, cert });
});
// Checks to ensure tls.createServer predictably throws an error
// Format ['key', 'cert', 'expected message']
[
[true, certBuff],
[true, certStr],
[true, certArrBuff],
[true, certDataView],
[true, false],
[true, false],
[{ pem: keyBuff }, false, 'pem'],
[[keyBuff, true], [certBuff, certBuff2], 1],
[[true, keyStr2], [certStr, certStr2], 0],
[[true, false], [certBuff, certBuff2], 0],
[true, [certBuff, certBuff2]]
].forEach(([key, cert, index]) => {
const type = typeof (index === undefined ? key : key[index]);
common.expectsError(() => {
tls.createServer({ key, cert });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.key" property must be one of type string, Buffer, ' +
`TypedArray, or DataView. Received type ${type}`
});
});
[
[keyBuff, true],
[keyStr, true],
[keyArrBuff, true],
[keyDataView, true],
[true, true],
[false, true],
[false, { pem: keyBuff }, 'pem'],
[false, 1],
[[keyBuff, keyBuff2], [true, certBuff2], 0],
[[keyStr, keyStr2], [certStr, true], 1],
[[keyStr, keyStr2], [true, false], 0],
[[keyStr, keyStr2], true]
].forEach(([key, cert, index]) => {
const type = typeof (index === undefined ? cert : cert[index]);
common.expectsError(() => {
tls.createServer({ key, cert });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.cert" property must be one of type string, Buffer,' +
` TypedArray, or DataView. Received type ${type}`
});
});
// Checks to ensure tls.createServer works with the CA parameter
// Format ['key', 'cert', 'ca']
[
[keyBuff, certBuff, caCert],
[keyBuff, certBuff, [caCert, caCert2]],
[keyBuff, certBuff, caCertStr],
[keyBuff, certBuff, [caCertStr, caCertStr2]],
[keyBuff, certBuff, caArrBuff],
[keyBuff, certBuff, caArrDataView],
[keyBuff, certBuff, false],
].forEach(([key, cert, ca]) => {
tls.createServer({ key, cert, ca });
});
// Checks to ensure tls.createServer throws an error for CA assignment
// Format ['key', 'cert', 'ca']
[
[keyBuff, certBuff, true],
[keyBuff, certBuff, {}],
[keyBuff, certBuff, 1],
[keyBuff, certBuff, true],
[keyBuff, certBuff, [caCert, true], 1]
].forEach(([key, cert, ca, index]) => {
const type = typeof (index === undefined ? ca : ca[index]);
common.expectsError(() => {
tls.createServer({ key, cert, ca });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.ca" property must be one of type string, Buffer, ' +
`TypedArray, or DataView. Received type ${type}`
});
});
// Checks to ensure tls.createSecureContext works with false-y input
// Format ['key', 'cert', 'ca']
[
[null, null, null],
[false, false, false],
[undefined, undefined, undefined],
['', '', ''],
[0, 0, 0]
].forEach(([key, cert, ca]) => {
tls.createSecureContext({ key, cert, ca });
});
|