Skip to content

Commit 7f3bc48

Browse files
committed
test: test crypto.setEngine() using an actual engine
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
1 parent b80b85e commit 7f3bc48

File tree

3 files changed

+116
-37
lines changed

3 files changed

+116
-37
lines changed

node.gyp

+26
Original file line numberDiff line numberDiff line change
@@ -1467,5 +1467,31 @@
14671467
},
14681468
]
14691469
}], # end aix section
1470+
['node_use_openssl=="true"', {
1471+
'targets': [
1472+
{
1473+
'target_name': 'test_crypto_engine',
1474+
'type': 'shared_library',
1475+
'include_dirs': ['deps/openssl/openssl/include'],
1476+
'sources': ['test/fixtures/test_crypto_engine.c'],
1477+
'conditions': [
1478+
['OS=="mac"', {
1479+
'dependencies': ['deps/openssl/openssl.gyp:openssl'],
1480+
'xcode_settings': {
1481+
'OTHER_CFLAGS': ['-Wno-deprecated-declarations'],
1482+
},
1483+
}],
1484+
['OS in "freebsd openbsd netbsd solaris android" or \
1485+
(OS=="linux" and target_arch!="ia32")', {
1486+
'cflags': [
1487+
'-Wno-deprecated-declarations',
1488+
'-fPIC',
1489+
],
1490+
'ldflags': ['-fPIC'],
1491+
}],
1492+
],
1493+
}, # test_crypto_engine
1494+
], # end targets
1495+
}], # end node_use_openssl section
14701496
], # end conditions block
14711497
}

test/fixtures/test_crypto_engine.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Refs: https://www.openssl.org/blog/blog/2015/10/08/engine-building-lesson-1-a-minimum-useless-engine/
2+
3+
#include <stdio.h>
4+
5+
#include <openssl/engine.h>
6+
7+
static const char *engine_id = "silly";
8+
static const char *engine_name = "A silly engine for demonstration purposes";
9+
10+
static int bind(ENGINE *e, const char *id)
11+
{
12+
int ret = 0;
13+
14+
if (!ENGINE_set_id(e, engine_id)) {
15+
fprintf(stderr, "ENGINE_set_id failed\n");
16+
goto end;
17+
}
18+
if (!ENGINE_set_name(e, engine_name)) {
19+
printf("ENGINE_set_name failed\n");
20+
goto end;
21+
}
22+
23+
ret = 1;
24+
end:
25+
return ret;
26+
}
27+
28+
IMPLEMENT_DYNAMIC_BIND_FN(bind)
29+
IMPLEMENT_DYNAMIC_CHECK_FN()

test/parallel/test-crypto-engine.js

+61-37
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,67 @@
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.isWindows) engineLib = `${engineName}.dll`;
35+
else if (common.isAIX) engineLib = `lib${engineName}.a`;
36+
else if (common.isOSX) engineLib = `lib${engineName}.dylib`;
37+
else engineLib = `lib${engineName}.so`;
38+
const execDir = path.dirname(process.execPath);
39+
const enginePath = path.join(execDir, engineLib);
40+
const engineId = path.parse(engineLib).name;
41+
42+
try {
43+
fs.accessSync(enginePath);
44+
} catch (err) {
45+
console.log(`${execDir}:`);
46+
fs.readdirSync(execDir).forEach((file) => console.log(` ${file}`));
47+
throw err;
48+
}
49+
50+
crypto.setEngine(enginePath);
51+
crypto.setEngine(enginePath);
52+
53+
crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA);
54+
crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA);
55+
56+
try {
57+
process.env.OPENSSL_ENGINES = execDir;
58+
59+
crypto.setEngine(engineId);
60+
crypto.setEngine(engineId);
61+
62+
crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA);
63+
crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA);
64+
} finally {
65+
process.env.OPENSSL_ENGINES = undefined;
66+
}
67+
}

0 commit comments

Comments
 (0)