11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst https = require('https');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cifunction toArrayBuffer(buf) {
131cb0ef41Sopenharmony_ci  const ab = new ArrayBuffer(buf.length);
141cb0ef41Sopenharmony_ci  const view = new Uint8Array(ab);
151cb0ef41Sopenharmony_ci  return buf.map((b, i) => view[i] = b);
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifunction toDataView(buf) {
191cb0ef41Sopenharmony_ci  const ab = new ArrayBuffer(buf.length);
201cb0ef41Sopenharmony_ci  const view = new DataView(ab);
211cb0ef41Sopenharmony_ci  return buf.map((b, i) => view[i] = b);
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst keyBuff = fixtures.readKey('agent1-key.pem');
251cb0ef41Sopenharmony_ciconst certBuff = fixtures.readKey('agent1-cert.pem');
261cb0ef41Sopenharmony_ciconst keyBuff2 = fixtures.readKey('ec-key.pem');
271cb0ef41Sopenharmony_ciconst certBuff2 = fixtures.readKey('ec-cert.pem');
281cb0ef41Sopenharmony_ciconst caCert = fixtures.readKey('ca1-cert.pem');
291cb0ef41Sopenharmony_ciconst caCert2 = fixtures.readKey('ca2-cert.pem');
301cb0ef41Sopenharmony_ciconst keyStr = keyBuff.toString();
311cb0ef41Sopenharmony_ciconst certStr = certBuff.toString();
321cb0ef41Sopenharmony_ciconst keyStr2 = keyBuff2.toString();
331cb0ef41Sopenharmony_ciconst certStr2 = certBuff2.toString();
341cb0ef41Sopenharmony_ciconst caCertStr = caCert.toString();
351cb0ef41Sopenharmony_ciconst caCertStr2 = caCert2.toString();
361cb0ef41Sopenharmony_ciconst keyArrBuff = toArrayBuffer(keyBuff);
371cb0ef41Sopenharmony_ciconst certArrBuff = toArrayBuffer(certBuff);
381cb0ef41Sopenharmony_ciconst caArrBuff = toArrayBuffer(caCert);
391cb0ef41Sopenharmony_ciconst keyDataView = toDataView(keyBuff);
401cb0ef41Sopenharmony_ciconst certDataView = toDataView(certBuff);
411cb0ef41Sopenharmony_ciconst caArrDataView = toDataView(caCert);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// Checks to ensure https.createServer doesn't throw an error
441cb0ef41Sopenharmony_ci// Format ['key', 'cert']
451cb0ef41Sopenharmony_ci[
461cb0ef41Sopenharmony_ci  [keyBuff, certBuff],
471cb0ef41Sopenharmony_ci  [false, certBuff],
481cb0ef41Sopenharmony_ci  [keyBuff, false],
491cb0ef41Sopenharmony_ci  [keyStr, certStr],
501cb0ef41Sopenharmony_ci  [false, certStr],
511cb0ef41Sopenharmony_ci  [keyStr, false],
521cb0ef41Sopenharmony_ci  [false, false],
531cb0ef41Sopenharmony_ci  [keyArrBuff, certArrBuff],
541cb0ef41Sopenharmony_ci  [keyArrBuff, false],
551cb0ef41Sopenharmony_ci  [false, certArrBuff],
561cb0ef41Sopenharmony_ci  [keyDataView, certDataView],
571cb0ef41Sopenharmony_ci  [keyDataView, false],
581cb0ef41Sopenharmony_ci  [false, certDataView],
591cb0ef41Sopenharmony_ci  [[keyBuff, keyBuff2], [certBuff, certBuff2]],
601cb0ef41Sopenharmony_ci  [[keyStr, keyStr2], [certStr, certStr2]],
611cb0ef41Sopenharmony_ci  [[keyStr, keyStr2], false],
621cb0ef41Sopenharmony_ci  [false, [certStr, certStr2]],
631cb0ef41Sopenharmony_ci  [[{ pem: keyBuff }], false],
641cb0ef41Sopenharmony_ci  [[{ pem: keyBuff }, { pem: keyBuff }], false],
651cb0ef41Sopenharmony_ci].forEach(([key, cert]) => {
661cb0ef41Sopenharmony_ci  https.createServer({ key, cert });
671cb0ef41Sopenharmony_ci});
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci// Checks to ensure https.createServer predictably throws an error
701cb0ef41Sopenharmony_ci// Format ['key', 'cert', 'expected message']
711cb0ef41Sopenharmony_ci[
721cb0ef41Sopenharmony_ci  [true, certBuff],
731cb0ef41Sopenharmony_ci  [true, certStr],
741cb0ef41Sopenharmony_ci  [true, certArrBuff],
751cb0ef41Sopenharmony_ci  [true, certDataView],
761cb0ef41Sopenharmony_ci  [true, false],
771cb0ef41Sopenharmony_ci  [true, false],
781cb0ef41Sopenharmony_ci  [{ pem: keyBuff }, false],
791cb0ef41Sopenharmony_ci  [1, false],
801cb0ef41Sopenharmony_ci  [[keyBuff, true], [certBuff, certBuff2], 1],
811cb0ef41Sopenharmony_ci  [[true, keyStr2], [certStr, certStr2], 0],
821cb0ef41Sopenharmony_ci  [[true, false], [certBuff, certBuff2], 0],
831cb0ef41Sopenharmony_ci  [true, [certBuff, certBuff2]],
841cb0ef41Sopenharmony_ci].forEach(([key, cert, index]) => {
851cb0ef41Sopenharmony_ci  const val = index === undefined ? key : key[index];
861cb0ef41Sopenharmony_ci  assert.throws(() => {
871cb0ef41Sopenharmony_ci    https.createServer({ key, cert });
881cb0ef41Sopenharmony_ci  }, {
891cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
901cb0ef41Sopenharmony_ci    name: 'TypeError',
911cb0ef41Sopenharmony_ci    message: 'The "options.key" property must be of type string or an ' +
921cb0ef41Sopenharmony_ci             'instance of Buffer, TypedArray, or DataView.' +
931cb0ef41Sopenharmony_ci             common.invalidArgTypeHelper(val)
941cb0ef41Sopenharmony_ci  });
951cb0ef41Sopenharmony_ci});
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci[
981cb0ef41Sopenharmony_ci  [keyBuff, true],
991cb0ef41Sopenharmony_ci  [keyStr, true],
1001cb0ef41Sopenharmony_ci  [keyArrBuff, true],
1011cb0ef41Sopenharmony_ci  [keyDataView, true],
1021cb0ef41Sopenharmony_ci  [true, true],
1031cb0ef41Sopenharmony_ci  [false, true],
1041cb0ef41Sopenharmony_ci  [false, { pem: keyBuff }],
1051cb0ef41Sopenharmony_ci  [false, 1],
1061cb0ef41Sopenharmony_ci  [[keyBuff, keyBuff2], [true, certBuff2], 0],
1071cb0ef41Sopenharmony_ci  [[keyStr, keyStr2], [certStr, true], 1],
1081cb0ef41Sopenharmony_ci  [[keyStr, keyStr2], [true, false], 0],
1091cb0ef41Sopenharmony_ci  [[keyStr, keyStr2], true],
1101cb0ef41Sopenharmony_ci].forEach(([key, cert, index]) => {
1111cb0ef41Sopenharmony_ci  const val = index === undefined ? cert : cert[index];
1121cb0ef41Sopenharmony_ci  assert.throws(() => {
1131cb0ef41Sopenharmony_ci    https.createServer({ key, cert });
1141cb0ef41Sopenharmony_ci  }, {
1151cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1161cb0ef41Sopenharmony_ci    name: 'TypeError',
1171cb0ef41Sopenharmony_ci    message: 'The "options.cert" property must be of type string or an ' +
1181cb0ef41Sopenharmony_ci             'instance of Buffer, TypedArray, or DataView.' +
1191cb0ef41Sopenharmony_ci             common.invalidArgTypeHelper(val)
1201cb0ef41Sopenharmony_ci  });
1211cb0ef41Sopenharmony_ci});
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci// Checks to ensure https.createServer works with the CA parameter
1241cb0ef41Sopenharmony_ci// Format ['key', 'cert', 'ca']
1251cb0ef41Sopenharmony_ci[
1261cb0ef41Sopenharmony_ci  [keyBuff, certBuff, caCert],
1271cb0ef41Sopenharmony_ci  [keyBuff, certBuff, [caCert, caCert2]],
1281cb0ef41Sopenharmony_ci  [keyBuff, certBuff, caCertStr],
1291cb0ef41Sopenharmony_ci  [keyBuff, certBuff, [caCertStr, caCertStr2]],
1301cb0ef41Sopenharmony_ci  [keyBuff, certBuff, caArrBuff],
1311cb0ef41Sopenharmony_ci  [keyBuff, certBuff, caArrDataView],
1321cb0ef41Sopenharmony_ci  [keyBuff, certBuff, false],
1331cb0ef41Sopenharmony_ci].forEach(([key, cert, ca]) => {
1341cb0ef41Sopenharmony_ci  https.createServer({ key, cert, ca });
1351cb0ef41Sopenharmony_ci});
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci// Checks to ensure https.createServer throws an error for CA assignment
1381cb0ef41Sopenharmony_ci// Format ['key', 'cert', 'ca']
1391cb0ef41Sopenharmony_ci[
1401cb0ef41Sopenharmony_ci  [keyBuff, certBuff, true],
1411cb0ef41Sopenharmony_ci  [keyBuff, certBuff, {}],
1421cb0ef41Sopenharmony_ci  [keyBuff, certBuff, 1],
1431cb0ef41Sopenharmony_ci  [keyBuff, certBuff, true],
1441cb0ef41Sopenharmony_ci  [keyBuff, certBuff, [caCert, true], 1],
1451cb0ef41Sopenharmony_ci].forEach(([key, cert, ca, index]) => {
1461cb0ef41Sopenharmony_ci  const val = index === undefined ? ca : ca[index];
1471cb0ef41Sopenharmony_ci  assert.throws(() => {
1481cb0ef41Sopenharmony_ci    https.createServer({ key, cert, ca });
1491cb0ef41Sopenharmony_ci  }, {
1501cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1511cb0ef41Sopenharmony_ci    name: 'TypeError',
1521cb0ef41Sopenharmony_ci    message: 'The "options.ca" property must be of type string or an instance' +
1531cb0ef41Sopenharmony_ci             ' of Buffer, TypedArray, or DataView.' +
1541cb0ef41Sopenharmony_ci             common.invalidArgTypeHelper(val)
1551cb0ef41Sopenharmony_ci  });
1561cb0ef41Sopenharmony_ci});
157