Skip to content

Commit b70b8a4

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

File tree

3 files changed

+102
-37
lines changed

3 files changed

+102
-37
lines changed

node.gyp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,5 +1467,30 @@
14671467
},
14681468
]
14691469
}], # end aix section
1470+
# TODO(RaisinTen): Enable this to build on other platforms as well.
1471+
['(OS=="mac" or OS=="linux") and node_use_openssl=="true"', {
1472+
'targets': [
1473+
{
1474+
'target_name': 'test_crypto_engine',
1475+
'type': 'shared_library',
1476+
'include_dirs': ['deps/openssl/openssl/include'],
1477+
'sources': ['test/fixtures/test_crypto_engine.c'],
1478+
'conditions': [
1479+
['OS=="mac"', {
1480+
'dependencies': ['deps/openssl/openssl.gyp:openssl'],
1481+
'xcode_settings': {
1482+
'OTHER_CFLAGS': ['-Wno-deprecated-declarations'],
1483+
},
1484+
}],
1485+
['OS=="linux"', {
1486+
'cflags': [
1487+
'-Wno-deprecated-declarations',
1488+
'-fPIC',
1489+
],
1490+
}],
1491+
],
1492+
}, # test_crypto_engine
1493+
], # end targets
1494+
}], # end node_use_openssl section
14701495
], # end conditions block
14711496
}

test/fixtures/test_crypto_engine.c

Lines changed: 20 additions & 0 deletions
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

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,63 @@
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+
let engineAvailable = false;
35+
if (common.isOSX) {
36+
engineLib = `lib${engineName}.dylib`;
37+
engineAvailable = true;
38+
} else if (common.isLinux) {
39+
engineLib = `lib${engineName}.so`;
40+
engineName = true;
41+
}
42+
const execDir = path.dirname(process.execPath);
43+
const enginePath = path.join(execDir, engineLib);
44+
const engineId = path.parse(engineLib).name;
45+
46+
if (engineAvailable) {
47+
fs.accessSync(enginePath);
48+
49+
crypto.setEngine(enginePath);
50+
crypto.setEngine(enginePath);
51+
52+
crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA);
53+
crypto.setEngine(enginePath, crypto.constants.ENGINE_METHOD_RSA);
54+
55+
process.env.OPENSSL_ENGINES = execDir;
56+
57+
crypto.setEngine(engineId);
58+
crypto.setEngine(engineId);
59+
60+
crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA);
61+
crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA);
62+
}
63+
}

0 commit comments

Comments
 (0)