1'use strict';
2const common = require('../../common');
3
4// This tests crypto.setEngine().
5
6if (!common.hasCrypto)
7  common.skip('missing crypto');
8
9const assert = require('assert');
10const crypto = require('crypto');
11const fs = require('fs');
12const path = require('path');
13
14
15assert.throws(() => crypto.setEngine(true), /ERR_INVALID_ARG_TYPE/);
16assert.throws(() => crypto.setEngine('/path/to/engine', 'notANumber'),
17              /ERR_INVALID_ARG_TYPE/);
18
19{
20  const invalidEngineName = 'xxx';
21  assert.throws(() => crypto.setEngine(invalidEngineName),
22                /ERR_CRYPTO_ENGINE_UNKNOWN/);
23  assert.throws(() => crypto.setEngine(invalidEngineName,
24                                       crypto.constants.ENGINE_METHOD_RSA),
25                /ERR_CRYPTO_ENGINE_UNKNOWN/);
26}
27
28crypto.setEngine('dynamic');
29crypto.setEngine('dynamic');
30
31crypto.setEngine('dynamic', crypto.constants.ENGINE_METHOD_RSA);
32crypto.setEngine('dynamic', crypto.constants.ENGINE_METHOD_RSA);
33
34const engine = path.join(__dirname,
35                         `/build/${common.buildType}/testsetengine.engine`);
36
37if (!fs.existsSync(engine))
38  common.skip('no engine');
39
40{
41  const engineId = path.parse(engine).name;
42  const execDir = path.parse(engine).dir;
43
44  crypto.setEngine(engine);
45  // OpenSSL 3.0.1 and 1.1.1m now throw errors if an engine is loaded again
46  // with a duplicate absolute path.
47  // TODO(richardlau): figure out why this fails on macOS but not Linux.
48  // crypto.setEngine(engine);
49
50  // crypto.setEngine(engine, crypto.constants.ENGINE_METHOD_RSA);
51  // crypto.setEngine(engine, crypto.constants.ENGINE_METHOD_RSA);
52
53  process.env.OPENSSL_ENGINES = execDir;
54
55  crypto.setEngine(engineId);
56  crypto.setEngine(engineId);
57
58  crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA);
59  crypto.setEngine(engineId, crypto.constants.ENGINE_METHOD_RSA);
60}
61