|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common');
|
| 3 | +if (!common.hasCrypto) common.skip('missing crypto'); |
3 | 4 |
|
4 |
| -if (!common.hasCrypto) |
5 |
| - common.skip('missing crypto'); |
| 5 | +// This tests crypto.setEngine(). |
6 | 6 |
|
7 | 7 | const assert = require('assert');
|
8 | 8 | 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 | +} |
0 commit comments