Skip to content

Commit d29a620

Browse files
benglMylesBorins
authored andcommitted
test: crypto createClass instanceof Class
The crypto classes are also exposed as createClass for each class. This tests that each of them returns an instance of the class in question. Backport-PR-URL: #16245 PR-URL: #8188 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 7b801b5 commit d29a620

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

test/parallel/test-crypto-classes.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
if (!common.hasCrypto) {
6+
common.skip('missing crypto');
7+
return;
8+
}
9+
const crypto = require('crypto');
10+
11+
// 'ClassName' : ['args', 'for', 'constructor']
12+
const TEST_CASES = {
13+
'Hash': ['sha1'],
14+
'Hmac': ['sha1', 'Node'],
15+
'Cipheriv': ['des-ede3-cbc', '0123456789abcd0123456789', '12345678'],
16+
'Decipheriv': ['des-ede3-cbc', '0123456789abcd0123456789', '12345678'],
17+
'Sign': ['RSA-SHA1'],
18+
'Verify': ['RSA-SHA1'],
19+
'DiffieHellman': [1024],
20+
'DiffieHellmanGroup': ['modp5'],
21+
'Credentials': []
22+
};
23+
24+
if (!common.hasFipsCrypto) {
25+
TEST_CASES.Cipher = ['aes192', 'secret'];
26+
TEST_CASES.Decipher = ['aes192', 'secret'];
27+
}
28+
29+
function entries(obj) {
30+
const ownProps = Object.keys(obj);
31+
let i = ownProps.length;
32+
const resArray = new Array(i); // preallocate the Array
33+
while (i--)
34+
resArray[i] = [ownProps[i], obj[ownProps[i]]];
35+
36+
return resArray;
37+
}
38+
39+
for (const [clazz, args] of entries(TEST_CASES)) {
40+
assert(crypto[`create${clazz}`](...args) instanceof crypto[clazz]);
41+
}

0 commit comments

Comments
 (0)