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