Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test crypto.setEngine() using an actual engine #40481

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,7 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
test/cctest/*.h \
test/embedding/*.cc \
test/embedding/*.h \
test/fixtures/*.c \
test/js-native-api/*/*.cc \
test/js-native-api/*/*.h \
test/node-api/*/*.cc \
Expand Down
26 changes: 26 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,5 +1467,31 @@
},
]
}], # end aix section
# TODO(RaisinTen): Enable this to build on other platforms as well.
['(OS=="mac" or (OS=="linux" and target_arch=="x64")) 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" and target_arch=="x64"', {
'cflags': [
'-Wno-deprecated-declarations',
'-fPIC',
],
}],
],
}, # test_crypto_engine
], # end targets
}], # end node_use_openssl section
], # end conditions block
}
20 changes: 20 additions & 0 deletions test/fixtures/test_crypto_engine.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <openssl/engine.h>

#include <stdio.h>

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()
91 changes: 54 additions & 37 deletions test/parallel/test-crypto-engine.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 2 additions & 2 deletions tools/run-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (typeof require === 'undefined') {
}

const path = require('path');
const { Worker } = require('worker_threads');
const { Worker, SHARE_ENV } = require('worker_threads');

new Worker(path.resolve(process.cwd(), process.argv[2]))
new Worker(path.resolve(process.cwd(), process.argv[2]), { env: SHARE_ENV })
Copy link
Contributor Author

@RaisinTen RaisinTen Oct 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using SHARE_ENV here as calling crypto.setEngine() with an engine id fails on Linux when run using ./node tools/run-worker.js test/parallel/test-crypto-engine.js even though process.env.OPENSSL_ENGINES has been set with the path to the directory containing the shared-object with the same engine id.

.on('exit', (code) => process.exitCode = code);