|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +if (!common.hasOpenSSL3) |
| 8 | + common.skip('this test requires OpenSSL 3.x'); |
| 9 | +const assert = require('node:assert'); |
| 10 | +const { createHash, getCiphers, getHashes } = require('node:crypto'); |
| 11 | +const { debuglog } = require('node:util'); |
| 12 | +const { getProviders } = require(`./build/${common.buildType}/binding`); |
| 13 | + |
| 14 | +// For the providers defined here, the expectation is that the listed ciphers |
| 15 | +// and hash algorithms are only provided by the named provider. These are for |
| 16 | +// basic checks and are not intended to list evey cipher or hash algorithm |
| 17 | +// supported by the provider. |
| 18 | +const providers = { |
| 19 | + 'default': { |
| 20 | + ciphers: ['des3-wrap'], |
| 21 | + hashes: ['sha512-256'], |
| 22 | + }, |
| 23 | + 'legacy': { |
| 24 | + ciphers: ['blowfish', 'idea'], |
| 25 | + hashes: ['md4', 'whirlpool'], |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +const debug = debuglog('test'); |
| 30 | + |
| 31 | +module.exports = { |
| 32 | + getCurrentProviders: getProviders, |
| 33 | + testProviderPresent, |
| 34 | + testProviderAbsent, |
| 35 | +}; |
| 36 | + |
| 37 | +function assertArrayDoesNotInclude(array, item, desc) { |
| 38 | + assert(!array.includes(item), |
| 39 | + `${desc} [${array}] unexpectedly includes "${item}"`); |
| 40 | +} |
| 41 | + |
| 42 | +function assertArrayIncludes(array, item, desc) { |
| 43 | + assert(array.includes(item), |
| 44 | + `${desc} [${array}] does not include "${item}"`); |
| 45 | +} |
| 46 | + |
| 47 | +function testProviderPresent(provider) { |
| 48 | + debug(`Checking '${provider}' is present`); |
| 49 | + assertArrayIncludes(getProviders(), provider, 'Loaded providers'); |
| 50 | + for (const cipher of providers[provider].ciphers || []) { |
| 51 | + debug(`Checking '${cipher}' cipher is available`); |
| 52 | + assertArrayIncludes(getCiphers(), cipher, 'Available ciphers'); |
| 53 | + } |
| 54 | + for (const hash of providers[provider].hashes || []) { |
| 55 | + debug(`Checking '${hash}' hash is available`); |
| 56 | + assertArrayIncludes(getHashes(), hash, 'Available hashes'); |
| 57 | + createHash(hash); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function testProviderAbsent(provider) { |
| 62 | + debug(`Checking '${provider}' is absent`); |
| 63 | + assertArrayDoesNotInclude(getProviders(), provider, 'Loaded providers'); |
| 64 | + for (const cipher of providers[provider].ciphers || []) { |
| 65 | + debug(`Checking '${cipher}' cipher is unavailable`); |
| 66 | + assertArrayDoesNotInclude(getCiphers(), cipher, 'Available ciphers'); |
| 67 | + } |
| 68 | + for (const hash of providers[provider].hashes || []) { |
| 69 | + debug(`Checking '${hash}' hash is unavailable`); |
| 70 | + assertArrayDoesNotInclude(getHashes(), hash, 'Available hashes'); |
| 71 | + assert.throws(() => { createHash(hash); }, { code: 'ERR_OSSL_EVP_UNSUPPORTED' }); |
| 72 | + } |
| 73 | +} |
0 commit comments