Skip to content

Commit 085762f

Browse files
committed
test: move all supported checks to env.ts
1 parent 05bd090 commit 085762f

File tree

11 files changed

+225
-250
lines changed

11 files changed

+225
-250
lines changed

cookbook/jwe.mjs

+8-16
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ export default [
6767
{
6868
title:
6969
'https://www.rfc-editor.org/rfc/rfc7520#section-5.3 - Key Wrap using PBES2-AES-KeyWrap with AES-CBC-HMAC-SHA2',
70-
electron: false,
71-
reproducible: true,
70+
deterministic: true,
7271
input: {
7372
plaintext:
7473
'{"keys":[{"kty":"oct","kid":"77c7e2b8-6e13-45cf-8672-617b5b45243a","use":"enc","alg":"A128GCM","k":"XctOhJAkA-pD9Lh7ZgW_2A"},{"kty":"oct","kid":"81b20965-8332-43d9-a468-82160ad91ac8","use":"enc","alg":"A128KW","k":"GZy6sIZ6wl9NJOKB-jnmVQ"},{"kty":"oct","kid":"18ec08e1-bfa9-4d95-b205-2b4dd1d4321d","use":"enc","alg":"A256GCMKW","k":"qC57l_uxcm7Nm3K-ct4GFjx8tM1U8CZ0NLBvdQstiS8"}]}',
@@ -123,7 +122,6 @@ export default [
123122
{
124123
title:
125124
'https://www.rfc-editor.org/rfc/rfc7520#section-5.4 - Key Agreement with Key Wrapping using ECDH-ES and AES-KeyWrap with AES-GCM',
126-
electron: false,
127125
input: {
128126
plaintext:
129127
'You can trust us to stick with you through thick and thin–to the bitter end. And you can trust us to keep any secret of yours–closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.',
@@ -262,7 +260,7 @@ export default [
262260
{
263261
title:
264262
'https://www.rfc-editor.org/rfc/rfc7520#section-5.6 - Direction Encryption using AES-GCM',
265-
reproducible: true,
263+
deterministic: true,
266264
input: {
267265
plaintext:
268266
'You can trust us to stick with you through thick and thin–to the bitter end. And you can trust us to keep any secret of yours–closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.',
@@ -311,7 +309,7 @@ export default [
311309
{
312310
title:
313311
'https://www.rfc-editor.org/rfc/rfc7520#section-5.6 - Key Wrap using AES-GCM KeyWrap with AES-CBC-HMAC-SHA2',
314-
reproducible: true,
312+
deterministic: true,
315313
input: {
316314
plaintext:
317315
'You can trust us to stick with you through thick and thin–to the bitter end. And you can trust us to keep any secret of yours–closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.',
@@ -373,8 +371,7 @@ export default [
373371
{
374372
title:
375373
'https://www.rfc-editor.org/rfc/rfc7520#section-5.8 - Key Wrap using AES-KeyWrap with AES-GCM',
376-
electron: false,
377-
reproducible: true,
374+
deterministic: true,
378375
input: {
379376
plaintext:
380377
'You can trust us to stick with you through thick and thin–to the bitter end. And you can trust us to keep any secret of yours–closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.',
@@ -430,9 +427,7 @@ export default [
430427
},
431428
{
432429
title: 'https://www.rfc-editor.org/rfc/rfc7520#section-5.9 - Compressed Content',
433-
webapi: false,
434-
electron: false,
435-
reproducible: false, // https://github.com/nodejs/node/issues/50138
430+
deterministic: false, // https://www.rfc-editor.org/errata/eid7680
436431
input: {
437432
plaintext:
438433
'You can trust us to stick with you through thick and thin–to the bitter end. And you can trust us to keep any secret of yours–closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.',
@@ -493,8 +488,7 @@ export default [
493488
{
494489
title:
495490
'https://www.rfc-editor.org/rfc/rfc7520#section-5.10 - Including Additional Authenticated Data',
496-
electron: false,
497-
reproducible: true,
491+
deterministic: true,
498492
input: {
499493
plaintext:
500494
'You can trust us to stick with you through thick and thin–to the bitter end. And you can trust us to keep any secret of yours–closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.',
@@ -554,8 +548,7 @@ export default [
554548
{
555549
title:
556550
'https://www.rfc-editor.org/rfc/rfc7520#section-5.11 - Protecting Specific Header Fields',
557-
electron: false,
558-
reproducible: true,
551+
deterministic: true,
559552
input: {
560553
plaintext:
561554
'You can trust us to stick with you through thick and thin–to the bitter end. And you can trust us to keep any secret of yours–closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.',
@@ -617,8 +610,7 @@ export default [
617610
},
618611
{
619612
title: 'https://www.rfc-editor.org/rfc/rfc7520#section-5.12 - Protecting Content Only',
620-
electron: false,
621-
reproducible: true,
613+
deterministic: true,
622614
input: {
623615
plaintext:
624616
'You can trust us to stick with you through thick and thin–to the bitter end. And you can trust us to keep any secret of yours–closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.',

cookbook/jws.mjs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default [
22
{
33
title: 'https://www.rfc-editor.org/rfc/rfc7520#section-4.1 - RSA v1.5 Signature',
4-
reproducible: true,
4+
deterministic: true,
55
input: {
66
payload:
77
"It’s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there’s no knowing where you might be swept off to.",
@@ -52,7 +52,7 @@ export default [
5252
},
5353
{
5454
title: 'https://www.rfc-editor.org/rfc/rfc8037#appendix-A.4 - Ed25519 Signing',
55-
reproducible: true,
55+
deterministic: false, // https://github.com/WICG/webcrypto-secure-curves/issues/28
5656
input: {
5757
payload: 'Example of Ed25519 signing',
5858
key: {
@@ -188,7 +188,7 @@ export default [
188188
},
189189
{
190190
title: 'https://www.rfc-editor.org/rfc/rfc7520#section-4.4 - HMAC-SHA2 Integrity Protection',
191-
reproducible: true,
191+
deterministic: true,
192192
input: {
193193
payload:
194194
"It’s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there’s no knowing where you might be swept off to.",
@@ -233,7 +233,7 @@ export default [
233233
},
234234
{
235235
title: 'https://www.rfc-editor.org/rfc/rfc7520#section-4.6 - Protecting Specific Header Fields',
236-
reproducible: true,
236+
deterministic: true,
237237
input: {
238238
payload:
239239
"It’s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there’s no knowing where you might be swept off to.",
@@ -282,7 +282,7 @@ export default [
282282
},
283283
{
284284
title: 'https://www.rfc-editor.org/rfc/rfc7520#section-4.7 - Protecting Content Only',
285-
reproducible: true,
285+
deterministic: true,
286286
input: {
287287
payload:
288288
"It’s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there’s no knowing where you might be swept off to.",
@@ -329,7 +329,7 @@ export default [
329329
},
330330
{
331331
title: 'https://www.rfc-editor.org/rfc/rfc7797#section-4.1 - { "b64": false } JSON only',
332-
reproducible: true,
332+
deterministic: true,
333333
input: {
334334
payload: '$.02',
335335
key: {

tap/aes.ts

+16-19
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@ export default (
1111
const { module, test } = QUnit
1212
module('aes.ts')
1313

14-
type Vector = [string, boolean]
15-
const algorithms: Vector[] = [
16-
['A128GCM', true],
17-
['A192GCM', !env.isBlink],
18-
['A256GCM', true],
19-
['A128CBC-HS256', true],
20-
['A192CBC-HS384', !env.isBlink],
21-
['A256CBC-HS512', true],
14+
const algorithms = [
15+
'A128GCM',
16+
'A192GCM',
17+
'A256GCM',
18+
'A128CBC-HS256',
19+
'A192CBC-HS384',
20+
'A256CBC-HS512',
2221
]
2322

24-
function title(vector: Vector) {
25-
const [enc, works] = vector
23+
function title(algorithm: string) {
24+
const supported = env.supported(algorithm)
2625
let result = ''
27-
if (!works) {
26+
if (!supported) {
2827
result = '[not supported] '
2928
}
30-
result += `${enc}`
29+
result += `${algorithm}`
3130
return result
3231
}
3332

@@ -40,9 +39,7 @@ export default (
4039
]
4140
}
4241

43-
for (const vector of algorithms) {
44-
const [enc, works] = vector
45-
42+
for (const enc of algorithms) {
4643
const execute = async (t: typeof QUnit.assert) => {
4744
for await (const secret of secretsFor(enc)) {
4845
await roundtrip.jwe(t, lib, keys, 'dir', enc, secret)
@@ -53,11 +50,11 @@ export default (
5350
await roundtrip.jwt(t, lib, keys, 'dir', enc, await secretsFor(enc)[0])
5451
}
5552

56-
if (works) {
57-
test(title(vector), execute)
58-
test(`${title(vector)} JWT`, jwt)
53+
if (env.supported(enc)) {
54+
test(title(enc), execute)
55+
test(`${title(enc)} JWT`, jwt)
5956
} else {
60-
test(title(vector), async (t) => {
57+
test(title(enc), async (t) => {
6158
await t.rejects(execute(t))
6259
})
6360
}

tap/aeskw.ts

+9-20
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,14 @@ export default (
1111
const { module, test } = QUnit
1212
module('aeskw.ts')
1313

14-
type Vector = [string, boolean]
15-
const algorithms: Vector[] = [
16-
['A128KW', !env.isElectron],
17-
['A192KW', !(env.isBlink || env.isElectron)],
18-
['A256KW', !env.isElectron],
19-
['A128GCMKW', true],
20-
['A192GCMKW', !env.isBlink],
21-
['A256GCMKW', true],
22-
]
14+
const algorithms = ['A128KW', 'A192KW', 'A256KW', 'A128GCMKW', 'A192GCMKW', 'A256GCMKW']
2315

24-
function title(vector: Vector) {
25-
const [alg, works] = vector
16+
function title(algorithm: string, supported = true) {
2617
let result = ''
27-
if (!works) {
18+
if (!supported) {
2819
result = '[not supported] '
2920
}
30-
result += `${alg}`
21+
result += `${algorithm}`
3122
return result
3223
}
3324

@@ -38,9 +29,7 @@ export default (
3829
]
3930
}
4031

41-
for (const vector of algorithms) {
42-
const [alg, works] = vector
43-
32+
for (const alg of algorithms) {
4433
const execute = async (t: typeof QUnit.assert) => {
4534
for await (const secret of secretsFor(alg)) {
4635
await roundtrip.jwe(t, lib, keys, alg, 'A128GCM', secret)
@@ -51,11 +40,11 @@ export default (
5140
await roundtrip.jwt(t, lib, keys, alg, 'A128GCM', await secretsFor(alg)[0])
5241
}
5342

54-
if (works) {
55-
test(title(vector), execute)
56-
test(`${title(vector)} JWT`, jwt)
43+
if (env.supported(alg)) {
44+
test(title(alg), execute)
45+
test(`${title(alg)} JWT`, jwt)
5746
} else {
58-
test(title(vector), async (t) => {
47+
test(title(alg, false), async (t) => {
5948
await t.rejects(execute(t))
6049
})
6150
}

tap/cookbook.ts

+4-28
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ import jwsVectors from '../cookbook/jws.mjs'
77
// @ts-ignore
88
import jweVectors from '../cookbook/jwe.mjs'
99

10-
// https://bugs.webkit.org/show_bug.cgi?id=262499
11-
// https://github.com/web-platform-tests/wpt/pull/42292
12-
if (env.isWebKit) {
13-
// @ts-ignore
14-
const ed25519 = jwsVectors.find((vector) => vector.title.includes('Ed25519'))
15-
ed25519.reproducible = false
16-
}
17-
1810
export default (
1911
QUnit: QUnit,
2012
lib: typeof jose,
@@ -42,22 +34,14 @@ export default (
4234
}
4335

4436
function supported(vector: any) {
45-
if (vector.input.alg === 'ES512') {
46-
return !env.isDeno
47-
}
48-
if (vector.input.key?.crv === 'Ed25519') {
49-
return !env.isBlink
50-
}
51-
return true
37+
return env.supported(vector.input.alg)
5238
}
5339

5440
const execute = (vector: any) => async (t: typeof QUnit.assert) => {
55-
const reproducible = !!vector.reproducible
56-
5741
const privateKey = await keys.importJWK(vector.input.key, vector.input.alg)
5842
const publicKey = await keys.importJWK(pubjwk(vector.input.key), vector.input.alg)
5943

60-
if (reproducible) {
44+
if (vector.deterministic) {
6145
// sign and compare results are the same
6246
const runs = [[flattened, vector.output.json_flat]]
6347
if (vector.signing.protected?.b64 !== undefined) {
@@ -148,16 +132,10 @@ export default (
148132
}
149133

150134
function supported(vector: any) {
151-
if (vector.webcrypto === false) {
152-
return false
153-
}
154-
if (env.isElectron && vector.electron === false) {
155-
return false
156-
}
157135
if (vector.input.zip) {
158136
return false
159137
}
160-
return true
138+
return env.supported(vector.input.alg) && env.supported(vector.input.enc)
161139
}
162140

163141
const toJWK = (input: string | jose.JWK) => {
@@ -173,9 +151,7 @@ export default (
173151
const execute = (vector: any) => async (t: typeof QUnit.assert) => {
174152
const dir = vector.input.alg === 'dir'
175153

176-
const reproducible = !!vector.reproducible
177-
178-
if (reproducible) {
154+
if (vector.deterministic) {
179155
// encrypt and compare results are the same
180156
for (const [serialization, expectedResult] of [
181157
[flattened, vector.output.json_flat],

tap/ecdh.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,31 @@ export default (
1313

1414
const kps: Record<string, jose.GenerateKeyPairResult> = {}
1515

16-
type Vector = [string, boolean] | [string, boolean, jose.GenerateKeyPairOptions]
16+
type Vector = [string, jose.GenerateKeyPairOptions]
1717
const algorithms: Vector[] = [
18-
['ECDH-ES', true],
19-
['ECDH-ES', true, { crv: 'P-384' }],
20-
['ECDH-ES', !env.isDeno, { crv: 'P-521' }],
21-
['ECDH-ES', !env.isBun, { crv: 'X25519' }],
18+
['ECDH-ES', { crv: 'P-256' }],
19+
['ECDH-ES', { crv: 'P-384' }],
20+
['ECDH-ES', { crv: 'P-521' }],
21+
['ECDH-ES', { crv: 'X25519' }],
2222
]
2323

24-
function title(vector: Vector) {
25-
const [alg, works, options] = vector
24+
function curve(options?: jose.GenerateKeyPairOptions) {
25+
return options?.crv || 'P-256'
26+
}
27+
28+
function title(vector: Vector, supported = true) {
29+
const [alg, options] = vector
2630
let result = ''
27-
if (!works) {
31+
const crv = curve(options)
32+
if (!supported) {
2833
result = '[not supported] '
2934
}
30-
result += `${alg} ${options?.crv || 'P-256'}`
35+
result += `${alg} ${crv}`
3136
return result
3237
}
3338

3439
for (const vector of algorithms) {
35-
const [alg, works, options] = vector
40+
const [alg, options] = vector
3641
const k = options?.crv || alg
3742

3843
const execute = async (t: typeof QUnit.assert) => {
@@ -49,11 +54,11 @@ export default (
4954
await roundtrip.jwt(t, lib, keys, alg, 'A128GCM', kps[k])
5055
}
5156

52-
if (works) {
57+
if (env.supported(alg) && env.supported(curve(options))) {
5358
test(title(vector), execute)
5459
test(`${title(vector)} JWT`, jwt)
5560
} else {
56-
test(title(vector), async (t) => {
61+
test(title(vector, false), async (t) => {
5762
await t.rejects(execute(t))
5863
})
5964
}

0 commit comments

Comments
 (0)