1'use strict'; 2const common = require('../common'); 3if (!common.hasCrypto) 4 common.skip('missing crypto'); 5 6const assert = require('assert'); 7const crypto = require('crypto'); 8 9// https://github.com/nodejs/node/issues/32738 10// XXX(bnoordhuis) validateInt32() throwing ERR_OUT_OF_RANGE and RangeError 11// instead of ERR_INVALID_ARG_TYPE and TypeError is questionable, IMO. 12assert.throws(() => crypto.createDiffieHellman(13.37), { 13 code: 'ERR_OUT_OF_RANGE', 14 name: 'RangeError', 15 message: 'The value of "sizeOrKey" is out of range. ' + 16 'It must be an integer. Received 13.37', 17}); 18 19assert.throws(() => crypto.createDiffieHellman('abcdef', 13.37), { 20 code: 'ERR_OUT_OF_RANGE', 21 name: 'RangeError', 22 message: 'The value of "generator" is out of range. ' + 23 'It must be an integer. Received 13.37', 24}); 25 26for (const bits of [-1, 0, 1]) { 27 if (common.hasOpenSSL3) { 28 assert.throws(() => crypto.createDiffieHellman(bits), { 29 code: 'ERR_OSSL_DH_MODULUS_TOO_SMALL', 30 name: 'Error', 31 message: /modulus too small/, 32 }); 33 } else { 34 assert.throws(() => crypto.createDiffieHellman(bits), { 35 code: 'ERR_OSSL_BN_BITS_TOO_SMALL', 36 name: 'Error', 37 message: /bits too small/, 38 }); 39 } 40} 41 42for (const g of [-1, 1]) { 43 const ex = { 44 code: 'ERR_OSSL_DH_BAD_GENERATOR', 45 name: 'Error', 46 message: /bad generator/, 47 }; 48 assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex); 49 assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex); 50} 51 52for (const g of [Buffer.from([]), 53 Buffer.from([0]), 54 Buffer.from([1])]) { 55 const ex = { 56 code: 'ERR_OSSL_DH_BAD_GENERATOR', 57 name: 'Error', 58 message: /bad generator/, 59 }; 60 assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex); 61 assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex); 62} 63 64[ 65 [0x1, 0x2], 66 () => { }, 67 /abc/, 68 {}, 69].forEach((input) => { 70 assert.throws( 71 () => crypto.createDiffieHellman(input), 72 { 73 code: 'ERR_INVALID_ARG_TYPE', 74 name: 'TypeError', 75 } 76 ); 77}); 78 79// Invalid test: curve argument is undefined 80assert.throws( 81 () => crypto.createECDH(), 82 { 83 code: 'ERR_INVALID_ARG_TYPE', 84 name: 'TypeError', 85 message: 'The "curve" argument must be of type string. ' + 86 'Received undefined' 87 }); 88 89assert.throws( 90 function() { 91 crypto.getDiffieHellman('unknown-group'); 92 }, 93 { 94 name: 'Error', 95 code: 'ERR_CRYPTO_UNKNOWN_DH_GROUP', 96 message: 'Unknown DH group' 97 }, 98 'crypto.getDiffieHellman(\'unknown-group\') ' + 99 'failed to throw the expected error.' 100); 101 102assert.throws( 103 () => crypto.createDiffieHellman('', true), 104 { 105 code: 'ERR_INVALID_ARG_TYPE' 106 } 107); 108[true, Symbol(), {}, () => {}, []].forEach((generator) => assert.throws( 109 () => crypto.createDiffieHellman('', 'base64', generator), 110 { code: 'ERR_INVALID_ARG_TYPE' } 111)); 112