1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24if (!common.hasCrypto) 25 common.skip('missing crypto'); 26 27const assert = require('assert'); 28const crypto = require('crypto'); 29const { Certificate } = crypto; 30const fixtures = require('../common/fixtures'); 31 32// Test Certificates 33const spkacValid = fixtures.readKey('rsa_spkac.spkac'); 34const spkacChallenge = 'this-is-a-challenge'; 35const spkacFail = fixtures.readKey('rsa_spkac_invalid.spkac'); 36const spkacPublicPem = fixtures.readKey('rsa_public.pem'); 37 38function checkMethods(certificate) { 39 40 assert.strictEqual(certificate.verifySpkac(spkacValid), true); 41 assert.strictEqual(certificate.verifySpkac(spkacFail), false); 42 43 assert.strictEqual( 44 stripLineEndings(certificate.exportPublicKey(spkacValid).toString('utf8')), 45 stripLineEndings(spkacPublicPem.toString('utf8')) 46 ); 47 assert.strictEqual(certificate.exportPublicKey(spkacFail), ''); 48 49 assert.strictEqual( 50 certificate.exportChallenge(spkacValid).toString('utf8'), 51 spkacChallenge 52 ); 53 assert.strictEqual(certificate.exportChallenge(spkacFail), ''); 54} 55 56{ 57 // Test instance methods 58 checkMethods(new Certificate()); 59} 60 61{ 62 // Test static methods 63 checkMethods(Certificate); 64} 65 66function stripLineEndings(obj) { 67 return obj.replace(/\n/g, ''); 68} 69 70// Direct call Certificate() should return instance 71assert(Certificate() instanceof Certificate); 72 73[1, {}, [], Infinity, true, 'test', undefined, null].forEach((val) => { 74 assert.throws( 75 () => Certificate.verifySpkac(val), 76 { 77 code: 'ERR_INVALID_ARG_TYPE', 78 message: 'The "spkac" argument must be an instance of Buffer, ' + 79 `TypedArray, or DataView.${common.invalidArgTypeHelper(val)}` 80 } 81 ); 82}); 83 84[1, {}, [], Infinity, true, undefined, null].forEach((val) => { 85 const errObj = { 86 code: 'ERR_INVALID_ARG_TYPE', 87 message: 'The "spkac" argument must be of type string or an instance of ' + 88 'Buffer, TypedArray, or DataView.' + 89 common.invalidArgTypeHelper(val) 90 }; 91 assert.throws(() => Certificate.exportPublicKey(val), errObj); 92 assert.throws(() => Certificate.exportChallenge(val), errObj); 93}); 94