test: test `crypto.setEngine()` using an actual engine by RaisinTen · Pull Request #40481 · nodejs/node
@@ -1,43 +1,60 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto) common.skip('missing crypto');
if (!common.hasCrypto) common.skip('missing crypto'); // This tests crypto.setEngine().
const assert = require('assert'); const crypto = require('crypto'); const invalidEngineName = 'xxx';
assert.throws( () => crypto.setEngine(true), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "id" argument must be of type string. Received type boolean' + ' (true)' });
assert.throws( () => crypto.setEngine('/path/to/engine', 'notANumber'), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "flags" argument must be of type number. Received type' + " string ('notANumber')" });
assert.throws( () => crypto.setEngine(invalidEngineName), { code: 'ERR_CRYPTO_ENGINE_UNKNOWN', name: 'Error', message: `Engine "${invalidEngineName}" was not found` });
assert.throws( () => crypto.setEngine(invalidEngineName, crypto.constants.ENGINE_METHOD_RSA), { code: 'ERR_CRYPTO_ENGINE_UNKNOWN', name: 'Error', message: `Engine "${invalidEngineName}" was not found` }); const fs = require('fs'); const path = require('path');
assert.throws(() => crypto.setEngine(true), /ERR_INVALID_ARG_TYPE/); assert.throws(() => crypto.setEngine('/path/to/engine', 'notANumber'), /ERR_INVALID_ARG_TYPE/);
{ const invalidEngineName = 'xxx'; assert.throws(() => crypto.setEngine(invalidEngineName), /ERR_CRYPTO_ENGINE_UNKNOWN/); assert.throws(() => crypto.setEngine(invalidEngineName, crypto.constants.ENGINE_METHOD_RSA), /ERR_CRYPTO_ENGINE_UNKNOWN/); }
crypto.setEngine('dynamic'); crypto.setEngine('dynamic');
crypto.setEngine('dynamic', crypto.constants.ENGINE_METHOD_RSA); crypto.setEngine('dynamic', crypto.constants.ENGINE_METHOD_RSA);
{ const engineName = 'test_crypto_engine'; let engineLib; if (common.isOSX) engineLib = `lib${engineName}.dylib`; else if (common.isLinux && process.arch === 'x64') engineLib = `lib${engineName}.so`;
if (engineLib !== undefined) { const execDir = path.dirname(process.execPath); const enginePath = path.join(execDir, engineLib); const engineId = path.parse(engineLib).name;
fs.accessSync(enginePath);
crypto.setEngine(enginePath); crypto.setEngine(enginePath);
crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA); crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA);
process.env.OPENSSL_ENGINES = execDir;
crypto.setEngine(engineId); crypto.setEngine(engineId);
crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA); crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA); } }
if (!common.hasCrypto) common.skip('missing crypto'); // This tests crypto.setEngine().
const assert = require('assert'); const crypto = require('crypto'); const invalidEngineName = 'xxx';
assert.throws( () => crypto.setEngine(true), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "id" argument must be of type string. Received type boolean' + ' (true)' });
assert.throws( () => crypto.setEngine('/path/to/engine', 'notANumber'), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "flags" argument must be of type number. Received type' + " string ('notANumber')" });
assert.throws( () => crypto.setEngine(invalidEngineName), { code: 'ERR_CRYPTO_ENGINE_UNKNOWN', name: 'Error', message: `Engine "${invalidEngineName}" was not found` });
assert.throws( () => crypto.setEngine(invalidEngineName, crypto.constants.ENGINE_METHOD_RSA), { code: 'ERR_CRYPTO_ENGINE_UNKNOWN', name: 'Error', message: `Engine "${invalidEngineName}" was not found` }); const fs = require('fs'); const path = require('path');
assert.throws(() => crypto.setEngine(true), /ERR_INVALID_ARG_TYPE/); assert.throws(() => crypto.setEngine('/path/to/engine', 'notANumber'), /ERR_INVALID_ARG_TYPE/);
{ const invalidEngineName = 'xxx'; assert.throws(() => crypto.setEngine(invalidEngineName), /ERR_CRYPTO_ENGINE_UNKNOWN/); assert.throws(() => crypto.setEngine(invalidEngineName, crypto.constants.ENGINE_METHOD_RSA), /ERR_CRYPTO_ENGINE_UNKNOWN/); }
crypto.setEngine('dynamic'); crypto.setEngine('dynamic');
crypto.setEngine('dynamic', crypto.constants.ENGINE_METHOD_RSA); crypto.setEngine('dynamic', crypto.constants.ENGINE_METHOD_RSA);
{ const engineName = 'test_crypto_engine'; let engineLib; if (common.isOSX) engineLib = `lib${engineName}.dylib`; else if (common.isLinux && process.arch === 'x64') engineLib = `lib${engineName}.so`;
if (engineLib !== undefined) { const execDir = path.dirname(process.execPath); const enginePath = path.join(execDir, engineLib); const engineId = path.parse(engineLib).name;
fs.accessSync(enginePath);
crypto.setEngine(enginePath); crypto.setEngine(enginePath);
crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA); crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA);
process.env.OPENSSL_ENGINES = execDir;
crypto.setEngine(engineId); crypto.setEngine(engineId);
crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA); crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA); } }