11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciif (!common.hasCrypto)
251cb0ef41Sopenharmony_ci  common.skip('missing crypto');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst assert = require('assert');
281cb0ef41Sopenharmony_ciconst crypto = require('crypto');
291cb0ef41Sopenharmony_ciconst { Certificate } = crypto;
301cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// Test Certificates
331cb0ef41Sopenharmony_ciconst spkacValid = fixtures.readKey('rsa_spkac.spkac');
341cb0ef41Sopenharmony_ciconst spkacChallenge = 'this-is-a-challenge';
351cb0ef41Sopenharmony_ciconst spkacFail = fixtures.readKey('rsa_spkac_invalid.spkac');
361cb0ef41Sopenharmony_ciconst spkacPublicPem = fixtures.readKey('rsa_public.pem');
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cifunction copyArrayBuffer(buf) {
391cb0ef41Sopenharmony_ci  return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cifunction checkMethods(certificate) {
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  assert.strictEqual(certificate.verifySpkac(spkacValid), true);
451cb0ef41Sopenharmony_ci  assert.strictEqual(certificate.verifySpkac(spkacFail), false);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  assert.strictEqual(
481cb0ef41Sopenharmony_ci    stripLineEndings(certificate.exportPublicKey(spkacValid).toString('utf8')),
491cb0ef41Sopenharmony_ci    stripLineEndings(spkacPublicPem.toString('utf8'))
501cb0ef41Sopenharmony_ci  );
511cb0ef41Sopenharmony_ci  assert.strictEqual(certificate.exportPublicKey(spkacFail), '');
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  assert.strictEqual(
541cb0ef41Sopenharmony_ci    certificate.exportChallenge(spkacValid).toString('utf8'),
551cb0ef41Sopenharmony_ci    spkacChallenge
561cb0ef41Sopenharmony_ci  );
571cb0ef41Sopenharmony_ci  assert.strictEqual(certificate.exportChallenge(spkacFail), '');
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  const ab = copyArrayBuffer(spkacValid);
601cb0ef41Sopenharmony_ci  assert.strictEqual(certificate.verifySpkac(ab), true);
611cb0ef41Sopenharmony_ci  assert.strictEqual(certificate.verifySpkac(new Uint8Array(ab)), true);
621cb0ef41Sopenharmony_ci  assert.strictEqual(certificate.verifySpkac(new DataView(ab)), true);
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci{
661cb0ef41Sopenharmony_ci  // Test maximum size of input buffer
671cb0ef41Sopenharmony_ci  let buf;
681cb0ef41Sopenharmony_ci  let skip = false;
691cb0ef41Sopenharmony_ci  try {
701cb0ef41Sopenharmony_ci    buf = Buffer.alloc(2 ** 31);
711cb0ef41Sopenharmony_ci  } catch {
721cb0ef41Sopenharmony_ci    // The allocation may fail on some systems. That is expected due
731cb0ef41Sopenharmony_ci    // to architecture and memory constraints. If it does, go ahead
741cb0ef41Sopenharmony_ci    // and skip this test.
751cb0ef41Sopenharmony_ci    skip = true;
761cb0ef41Sopenharmony_ci  }
771cb0ef41Sopenharmony_ci  if (!skip) {
781cb0ef41Sopenharmony_ci    assert.throws(
791cb0ef41Sopenharmony_ci      () => Certificate.verifySpkac(buf), {
801cb0ef41Sopenharmony_ci        code: 'ERR_OUT_OF_RANGE'
811cb0ef41Sopenharmony_ci      });
821cb0ef41Sopenharmony_ci    assert.throws(
831cb0ef41Sopenharmony_ci      () => Certificate.exportChallenge(buf), {
841cb0ef41Sopenharmony_ci        code: 'ERR_OUT_OF_RANGE'
851cb0ef41Sopenharmony_ci      });
861cb0ef41Sopenharmony_ci    assert.throws(
871cb0ef41Sopenharmony_ci      () => Certificate.exportPublicKey(buf), {
881cb0ef41Sopenharmony_ci        code: 'ERR_OUT_OF_RANGE'
891cb0ef41Sopenharmony_ci      });
901cb0ef41Sopenharmony_ci  }
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci{
941cb0ef41Sopenharmony_ci  // Test instance methods
951cb0ef41Sopenharmony_ci  checkMethods(new Certificate());
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci{
991cb0ef41Sopenharmony_ci  // Test static methods
1001cb0ef41Sopenharmony_ci  checkMethods(Certificate);
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_cifunction stripLineEndings(obj) {
1041cb0ef41Sopenharmony_ci  return obj.replace(/\n/g, '');
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci// Direct call Certificate() should return instance
1081cb0ef41Sopenharmony_ciassert(Certificate() instanceof Certificate);
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci[1, {}, [], Infinity, true, undefined, null].forEach((val) => {
1111cb0ef41Sopenharmony_ci  assert.throws(
1121cb0ef41Sopenharmony_ci    () => Certificate.verifySpkac(val),
1131cb0ef41Sopenharmony_ci    { code: 'ERR_INVALID_ARG_TYPE' }
1141cb0ef41Sopenharmony_ci  );
1151cb0ef41Sopenharmony_ci});
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci[1, {}, [], Infinity, true, undefined, null].forEach((val) => {
1181cb0ef41Sopenharmony_ci  const errObj = { code: 'ERR_INVALID_ARG_TYPE' };
1191cb0ef41Sopenharmony_ci  assert.throws(() => Certificate.exportPublicKey(val), errObj);
1201cb0ef41Sopenharmony_ci  assert.throws(() => Certificate.exportChallenge(val), errObj);
1211cb0ef41Sopenharmony_ci});
122