Skip to content

Commit 179a5c5

Browse files
RaisinTentargos
authored andcommitted
test: test crypto.setEngine() using an actual engine
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #40481 Reviewed-By: James M Snell <[email protected]>
1 parent aa98c6b commit 179a5c5

File tree

5 files changed

+103
-39
lines changed

5 files changed

+103
-39
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,7 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
13271327
test/cctest/*.h \
13281328
test/embedding/*.cc \
13291329
test/embedding/*.h \
1330+
test/fixtures/*.c \
13301331
test/js-native-api/*/*.cc \
13311332
test/js-native-api/*/*.h \
13321333
test/node-api/*/*.cc \

node.gyp

+26
Original file line numberDiff line numberDiff line change
@@ -1473,5 +1473,31 @@
14731473
},
14741474
]
14751475
}], # end aix section
1476+
# TODO(RaisinTen): Enable this to build on other platforms as well.
1477+
['(OS=="mac" or (OS=="linux" and target_arch=="x64")) and \
1478+
node_use_openssl=="true"', {
1479+
'targets': [
1480+
{
1481+
'target_name': 'test_crypto_engine',
1482+
'type': 'shared_library',
1483+
'include_dirs': ['deps/openssl/openssl/include'],
1484+
'sources': ['test/fixtures/test_crypto_engine.c'],
1485+
'conditions': [
1486+
['OS=="mac"', {
1487+
'dependencies': ['deps/openssl/openssl.gyp:openssl'],
1488+
'xcode_settings': {
1489+
'OTHER_CFLAGS': ['-Wno-deprecated-declarations'],
1490+
},
1491+
}],
1492+
['OS=="linux" and target_arch=="x64"', {
1493+
'cflags': [
1494+
'-Wno-deprecated-declarations',
1495+
'-fPIC',
1496+
],
1497+
}],
1498+
],
1499+
}, # test_crypto_engine
1500+
], # end targets
1501+
}], # end node_use_openssl section
14761502
], # end conditions block
14771503
}

test/fixtures/test_crypto_engine.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <openssl/engine.h>
2+
3+
#include <stdio.h>
4+
5+
int bind(ENGINE* e, const char* id) {
6+
if (ENGINE_set_id(e, "libtest_crypto_engine") == 0) {
7+
fprintf(stderr, "ENGINE_set_id() failed.\n");
8+
return 0;
9+
}
10+
11+
if (ENGINE_set_name(e, "A test crypto engine") == 0) {
12+
fprintf(stderr, "ENGINE_set_name() failed.\n");
13+
return 0;
14+
}
15+
16+
return 1;
17+
}
18+
19+
IMPLEMENT_DYNAMIC_BIND_FN(bind)
20+
IMPLEMENT_DYNAMIC_CHECK_FN()

test/parallel/test-crypto-engine.js

+54-37
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,60 @@
11
'use strict';
22
const common = require('../common');
3+
if (!common.hasCrypto) common.skip('missing crypto');
34

4-
if (!common.hasCrypto)
5-
common.skip('missing crypto');
5+
// This tests crypto.setEngine().
66

77
const assert = require('assert');
88
const crypto = require('crypto');
9-
const invalidEngineName = 'xxx';
10-
11-
assert.throws(
12-
() => crypto.setEngine(true),
13-
{
14-
code: 'ERR_INVALID_ARG_TYPE',
15-
name: 'TypeError',
16-
message: 'The "id" argument must be of type string. Received type boolean' +
17-
' (true)'
18-
});
19-
20-
assert.throws(
21-
() => crypto.setEngine('/path/to/engine', 'notANumber'),
22-
{
23-
code: 'ERR_INVALID_ARG_TYPE',
24-
name: 'TypeError',
25-
message: 'The "flags" argument must be of type number. Received type' +
26-
" string ('notANumber')"
27-
});
28-
29-
assert.throws(
30-
() => crypto.setEngine(invalidEngineName),
31-
{
32-
code: 'ERR_CRYPTO_ENGINE_UNKNOWN',
33-
name: 'Error',
34-
message: `Engine "${invalidEngineName}" was not found`
35-
});
36-
37-
assert.throws(
38-
() => crypto.setEngine(invalidEngineName, crypto.constants.ENGINE_METHOD_RSA),
39-
{
40-
code: 'ERR_CRYPTO_ENGINE_UNKNOWN',
41-
name: 'Error',
42-
message: `Engine "${invalidEngineName}" was not found`
43-
});
9+
const fs = require('fs');
10+
const path = require('path');
11+
12+
assert.throws(() => crypto.setEngine(true), /ERR_INVALID_ARG_TYPE/);
13+
assert.throws(() => crypto.setEngine('/path/to/engine', 'notANumber'),
14+
/ERR_INVALID_ARG_TYPE/);
15+
16+
{
17+
const invalidEngineName = 'xxx';
18+
assert.throws(() => crypto.setEngine(invalidEngineName),
19+
/ERR_CRYPTO_ENGINE_UNKNOWN/);
20+
assert.throws(() => crypto.setEngine(invalidEngineName,
21+
crypto.constants.ENGINE_METHOD_RSA),
22+
/ERR_CRYPTO_ENGINE_UNKNOWN/);
23+
}
24+
25+
crypto.setEngine('dynamic');
26+
crypto.setEngine('dynamic');
27+
28+
crypto.setEngine('dynamic', crypto.constants.ENGINE_METHOD_RSA);
29+
crypto.setEngine('dynamic', crypto.constants.ENGINE_METHOD_RSA);
30+
31+
{
32+
const engineName = 'test_crypto_engine';
33+
let engineLib;
34+
if (common.isOSX)
35+
engineLib = `lib${engineName}.dylib`;
36+
else if (common.isLinux && process.arch === 'x64')
37+
engineLib = `lib${engineName}.so`;
38+
39+
if (engineLib !== undefined) {
40+
const execDir = path.dirname(process.execPath);
41+
const enginePath = path.join(execDir, engineLib);
42+
const engineId = path.parse(engineLib).name;
43+
44+
fs.accessSync(enginePath);
45+
46+
crypto.setEngine(enginePath);
47+
crypto.setEngine(enginePath);
48+
49+
crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA);
50+
crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA);
51+
52+
process.env.OPENSSL_ENGINES = execDir;
53+
54+
crypto.setEngine(engineId);
55+
crypto.setEngine(engineId);
56+
57+
crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA);
58+
crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA);
59+
}
60+
}

tools/run-worker.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if (typeof require === 'undefined') {
55
}
66

77
const path = require('path');
8-
const { Worker } = require('worker_threads');
8+
const { Worker, SHARE_ENV } = require('worker_threads');
99

10-
new Worker(path.resolve(process.cwd(), process.argv[2]))
10+
new Worker(path.resolve(process.cwd(), process.argv[2]), { env: SHARE_ENV })
1111
.on('exit', (code) => process.exitCode = code);

0 commit comments

Comments
 (0)