Skip to content

Commit 2aa755e

Browse files
committed
test: split JWK async elliptic curve keygen tests
PR-URL: nodejs/node#49221 Refs: nodejs/node#49202 Refs: nodejs/node#41206 Reviewed-By: Luigi Pinca <[email protected]>
1 parent 6ebbd6c commit 2aa755e

3 files changed

+97
-84
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const {
9+
generateKeyPair,
10+
} = require('crypto');
11+
12+
// Test async elliptic curve key generation with 'jwk' encoding and named
13+
// curve.
14+
['P-384', 'P-256', 'P-521', 'secp256k1'].forEach((curve) => {
15+
generateKeyPair('ec', {
16+
namedCurve: curve,
17+
publicKeyEncoding: {
18+
format: 'jwk'
19+
},
20+
privateKeyEncoding: {
21+
format: 'jwk'
22+
}
23+
}, common.mustSucceed((publicKey, privateKey) => {
24+
assert.strictEqual(typeof publicKey, 'object');
25+
assert.strictEqual(typeof privateKey, 'object');
26+
assert.strictEqual(publicKey.x, privateKey.x);
27+
assert.strictEqual(publicKey.y, privateKey.y);
28+
assert(!publicKey.d);
29+
assert(privateKey.d);
30+
assert.strictEqual(publicKey.kty, 'EC');
31+
assert.strictEqual(publicKey.kty, privateKey.kty);
32+
assert.strictEqual(publicKey.crv, curve);
33+
assert.strictEqual(publicKey.crv, privateKey.crv);
34+
}));
35+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const {
9+
generateKeyPair,
10+
} = require('crypto');
11+
12+
// Test async elliptic curve key generation with 'jwk' encoding and RSA.
13+
{
14+
generateKeyPair('rsa', {
15+
modulusLength: 4096,
16+
publicKeyEncoding: {
17+
format: 'jwk'
18+
},
19+
privateKeyEncoding: {
20+
format: 'jwk'
21+
}
22+
}, common.mustSucceed((publicKey, privateKey) => {
23+
assert.strictEqual(typeof publicKey, 'object');
24+
assert.strictEqual(typeof privateKey, 'object');
25+
assert.strictEqual(publicKey.kty, 'RSA');
26+
assert.strictEqual(publicKey.kty, privateKey.kty);
27+
assert.strictEqual(typeof publicKey.n, 'string');
28+
assert.strictEqual(publicKey.n, privateKey.n);
29+
assert.strictEqual(typeof publicKey.e, 'string');
30+
assert.strictEqual(publicKey.e, privateKey.e);
31+
assert.strictEqual(typeof privateKey.d, 'string');
32+
assert.strictEqual(typeof privateKey.p, 'string');
33+
assert.strictEqual(typeof privateKey.q, 'string');
34+
assert.strictEqual(typeof privateKey.dp, 'string');
35+
assert.strictEqual(typeof privateKey.dq, 'string');
36+
assert.strictEqual(typeof privateKey.qi, 'string');
37+
}));
38+
}

graal-nodejs/test/parallel/test-crypto-keygen-async-elliptic-curve-jwk.js

+24-84
Original file line numberDiff line numberDiff line change
@@ -9,92 +9,32 @@ const {
99
generateKeyPair,
1010
} = require('crypto');
1111

12-
// Test async elliptic curve key generation with 'jwk' encoding
12+
// Test async elliptic curve key generation with 'jwk' encoding.
1313
{
1414
[
15-
['ec', ['P-384', 'P-256', 'P-521', 'secp256k1']],
16-
['rsa'],
17-
['ed25519'],
18-
['ed448'],
19-
['x25519'],
20-
['x448'],
21-
].forEach((types) => {
22-
const [type, options] = types;
23-
switch (type) {
24-
case 'ec': {
25-
return options.forEach((curve) => {
26-
generateKeyPair(type, {
27-
namedCurve: curve,
28-
publicKeyEncoding: {
29-
format: 'jwk'
30-
},
31-
privateKeyEncoding: {
32-
format: 'jwk'
33-
}
34-
}, common.mustSucceed((publicKey, privateKey) => {
35-
assert.strictEqual(typeof publicKey, 'object');
36-
assert.strictEqual(typeof privateKey, 'object');
37-
assert.strictEqual(publicKey.x, privateKey.x);
38-
assert.strictEqual(publicKey.y, privateKey.y);
39-
assert(!publicKey.d);
40-
assert(privateKey.d);
41-
assert.strictEqual(publicKey.kty, 'EC');
42-
assert.strictEqual(publicKey.kty, privateKey.kty);
43-
assert.strictEqual(publicKey.crv, curve);
44-
assert.strictEqual(publicKey.crv, privateKey.crv);
45-
}));
46-
});
15+
'ed25519',
16+
'ed448',
17+
'x25519',
18+
'x448',
19+
].forEach((type) => {
20+
generateKeyPair(type, {
21+
publicKeyEncoding: {
22+
format: 'jwk'
23+
},
24+
privateKeyEncoding: {
25+
format: 'jwk'
4726
}
48-
case 'rsa': {
49-
return generateKeyPair(type, {
50-
modulusLength: 4096,
51-
publicKeyEncoding: {
52-
format: 'jwk'
53-
},
54-
privateKeyEncoding: {
55-
format: 'jwk'
56-
}
57-
}, common.mustSucceed((publicKey, privateKey) => {
58-
assert.strictEqual(typeof publicKey, 'object');
59-
assert.strictEqual(typeof privateKey, 'object');
60-
assert.strictEqual(publicKey.kty, 'RSA');
61-
assert.strictEqual(publicKey.kty, privateKey.kty);
62-
assert.strictEqual(typeof publicKey.n, 'string');
63-
assert.strictEqual(publicKey.n, privateKey.n);
64-
assert.strictEqual(typeof publicKey.e, 'string');
65-
assert.strictEqual(publicKey.e, privateKey.e);
66-
assert.strictEqual(typeof privateKey.d, 'string');
67-
assert.strictEqual(typeof privateKey.p, 'string');
68-
assert.strictEqual(typeof privateKey.q, 'string');
69-
assert.strictEqual(typeof privateKey.dp, 'string');
70-
assert.strictEqual(typeof privateKey.dq, 'string');
71-
assert.strictEqual(typeof privateKey.qi, 'string');
72-
}));
73-
}
74-
case 'ed25519':
75-
case 'ed448':
76-
case 'x25519':
77-
case 'x448': {
78-
generateKeyPair(type, {
79-
publicKeyEncoding: {
80-
format: 'jwk'
81-
},
82-
privateKeyEncoding: {
83-
format: 'jwk'
84-
}
85-
}, common.mustSucceed((publicKey, privateKey) => {
86-
assert.strictEqual(typeof publicKey, 'object');
87-
assert.strictEqual(typeof privateKey, 'object');
88-
assert.strictEqual(publicKey.x, privateKey.x);
89-
assert(!publicKey.d);
90-
assert(privateKey.d);
91-
assert.strictEqual(publicKey.kty, 'OKP');
92-
assert.strictEqual(publicKey.kty, privateKey.kty);
93-
const expectedCrv = `${type.charAt(0).toUpperCase()}${type.slice(1)}`;
94-
assert.strictEqual(publicKey.crv, expectedCrv);
95-
assert.strictEqual(publicKey.crv, privateKey.crv);
96-
}));
97-
}
98-
}
27+
}, common.mustSucceed((publicKey, privateKey) => {
28+
assert.strictEqual(typeof publicKey, 'object');
29+
assert.strictEqual(typeof privateKey, 'object');
30+
assert.strictEqual(publicKey.x, privateKey.x);
31+
assert(!publicKey.d);
32+
assert(privateKey.d);
33+
assert.strictEqual(publicKey.kty, 'OKP');
34+
assert.strictEqual(publicKey.kty, privateKey.kty);
35+
const expectedCrv = `${type.charAt(0).toUpperCase()}${type.slice(1)}`;
36+
assert.strictEqual(publicKey.crv, expectedCrv);
37+
assert.strictEqual(publicKey.crv, privateKey.crv);
38+
}));
9939
});
10040
}

0 commit comments

Comments
 (0)