From b70b8a4d4c97b358957e27dc8ed910d37dce3a0a Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 16 Oct 2021 14:46:48 +0530 Subject: [PATCH] test: test `crypto.setEngine()` using an actual engine Signed-off-by: Darshan Sen --- node.gyp | 25 ++++++++ test/fixtures/test_crypto_engine.c | 20 ++++++ test/parallel/test-crypto-engine.js | 94 +++++++++++++++++------------ 3 files changed, 102 insertions(+), 37 deletions(-) create mode 100644 test/fixtures/test_crypto_engine.c diff --git a/node.gyp b/node.gyp index 39496fd0507718..4d08901e6d1eb9 100644 --- a/node.gyp +++ b/node.gyp @@ -1467,5 +1467,30 @@ }, ] }], # end aix section + # TODO(RaisinTen): Enable this to build on other platforms as well. + ['(OS=="mac" or OS=="linux") and node_use_openssl=="true"', { + 'targets': [ + { + 'target_name': 'test_crypto_engine', + 'type': 'shared_library', + 'include_dirs': ['deps/openssl/openssl/include'], + 'sources': ['test/fixtures/test_crypto_engine.c'], + 'conditions': [ + ['OS=="mac"', { + 'dependencies': ['deps/openssl/openssl.gyp:openssl'], + 'xcode_settings': { + 'OTHER_CFLAGS': ['-Wno-deprecated-declarations'], + }, + }], + ['OS=="linux"', { + 'cflags': [ + '-Wno-deprecated-declarations', + '-fPIC', + ], + }], + ], + }, # test_crypto_engine + ], # end targets + }], # end node_use_openssl section ], # end conditions block } diff --git a/test/fixtures/test_crypto_engine.c b/test/fixtures/test_crypto_engine.c new file mode 100644 index 00000000000000..1715196bf5bb03 --- /dev/null +++ b/test/fixtures/test_crypto_engine.c @@ -0,0 +1,20 @@ +#include + +#include + +int bind(ENGINE* e, const char* id) { + if (ENGINE_set_id(e, "libtest_crypto_engine") == 0) { + fprintf(stderr, "ENGINE_set_id() failed.\n"); + return 0; + } + + if (ENGINE_set_name(e, "A test crypto engine") == 0) { + fprintf(stderr, "ENGINE_set_name() failed.\n"); + return 0; + } + + return 1; +} + +IMPLEMENT_DYNAMIC_BIND_FN(bind) +IMPLEMENT_DYNAMIC_CHECK_FN() diff --git a/test/parallel/test-crypto-engine.js b/test/parallel/test-crypto-engine.js index 8b33285b454eef..10b7342c4b7e92 100644 --- a/test/parallel/test-crypto-engine.js +++ b/test/parallel/test-crypto-engine.js @@ -1,43 +1,63 @@ '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; + let engineAvailable = false; + if (common.isOSX) { + engineLib = `lib${engineName}.dylib`; + engineAvailable = true; + } else if (common.isLinux) { + engineLib = `lib${engineName}.so`; + engineName = true; + } + const execDir = path.dirname(process.execPath); + const enginePath = path.join(execDir, engineLib); + const engineId = path.parse(engineLib).name; + + if (engineAvailable) { + 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); + } +}