Skip to content

Commit

Permalink
test: update Deno@2 expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Oct 11, 2024
1 parent 510c5ca commit 5df0893
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 67 deletions.
123 changes: 63 additions & 60 deletions tap/jwk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@ export default (QUnit: QUnit, lib: typeof jose, keys: typeof jose) => {
const { module, test } = QUnit
module('jwk.ts')

type Vector = [string, JsonWebKey, boolean] | [string, JsonWebKey, boolean, boolean]
type Vector = [string, JsonWebKey, boolean | [boolean, boolean]]

const algorithms: Vector[] = [
['ECDH-ES', KEYS.P256.jwk, true],
['ECDH-ES', KEYS.P384.jwk, true],
['ECDH-ES', KEYS.P521.jwk, !env.isDeno],
['ECDH-ES', KEYS.P521.jwk, env.isDeno ? [true, false] : true],
[
'ECDH-ES',
KEYS.X25519.jwk,
env.isDeno ||
env.isNode ||
env.isElectron ||
env.isWorkerd ||
env.isEdgeRuntime ||
(env.isGecko && env.isBrowserVersionAtLeast(130)),
env.isDeno,
env.isDeno
? [true, false]
: env.isNode ||
env.isElectron ||
env.isWorkerd ||
env.isEdgeRuntime ||
(env.isGecko && env.isBrowserVersionAtLeast(130)),
],
['ECDH-ES', KEYS.X448.jwk, env.isNode || env.isEdgeRuntime],
['ECDH-ES', KEYS.X448.jwk, env.isDeno ? [true, false] : env.isNode || env.isEdgeRuntime],
['EdDSA', KEYS.Ed25519.jwk, !env.isBlink],
['EdDSA', KEYS.Ed448.jwk, env.isNode || env.isEdgeRuntime],
['ES256', KEYS.P256.jwk, true],
['ES256K', KEYS.secp256k1.jwk, lib.cryptoRuntime === 'node:crypto' && !env.isElectron],
['ES384', KEYS.P384.jwk, true],
['ES512', KEYS.P521.jwk, !env.isDeno],
['ES512', KEYS.P521.jwk, env.isDeno ? [true, false] : true],
['PS256', KEYS.RSA.jwk, true],
['PS384', KEYS.RSA.jwk, true],
['PS512', KEYS.RSA.jwk, true],
Expand All @@ -48,7 +49,13 @@ export default (QUnit: QUnit, lib: typeof jose, keys: typeof jose) => {
}

for (const vector of algorithms.slice()) {
algorithms.push([vector[0], publicJwk(vector[1]), vector[2]])
if (typeof vector[2] !== 'boolean') {
let [pub, priv] = vector[2]
vector[2] = priv
algorithms.push([vector[0], publicJwk(vector[1]), pub])
} else {
algorithms.push([vector[0], publicJwk(vector[1]), vector[2]])
}
}

function title(alg: string, jwk: JsonWebKey, works: boolean) {
Expand All @@ -67,23 +74,19 @@ export default (QUnit: QUnit, lib: typeof jose, keys: typeof jose) => {

for (const vector of algorithms) {
const [alg, jwk] = vector
const [, , works, exportNotImplemented] = vector
const [, , works] = vector

if (typeof works !== 'boolean') {
throw new Error()
}

const execute = async (t: typeof QUnit.assert) => {
const key = await lib.importJWK({ ...jwk, ext: true }, alg)
if (exportNotImplemented) {
try {
await lib.exportJWK(key)
throw new Error()
} catch (err) {
t.strictEqual((err as Error).name, 'NotSupportedError')
}
} else {
const exported = await lib.exportJWK(key)

for (const prop of [...new Set([...Object.keys(jwk), ...Object.keys(exported)])]) {
t.strictEqual(exported[prop], jwk[prop as keyof JsonWebKey], `${prop} mismatch`)
}
const exported = await lib.exportJWK(key)

for (const prop of [...new Set([...Object.keys(jwk), ...Object.keys(exported)])]) {
t.strictEqual(exported[prop], jwk[prop as keyof JsonWebKey], `${prop} mismatch`)
}

t.ok(1)
Expand All @@ -98,40 +101,40 @@ export default (QUnit: QUnit, lib: typeof jose, keys: typeof jose) => {
}
}

test('alg argument and jwk.alg is ignored for oct JWKs', async (t) => {
const oct = {
k: 'FyCq1CKBflh3I5gikEjpYrdOXllzxB_yc02za8ERknI',
kty: 'oct',
}
await lib.importJWK(oct)
t.ok(1)
})
// test('alg argument and jwk.alg is ignored for oct JWKs', async (t) => {
// const oct = {
// k: 'FyCq1CKBflh3I5gikEjpYrdOXllzxB_yc02za8ERknI',
// kty: 'oct',
// }
// await lib.importJWK(oct)
// t.ok(1)
// })

if (lib.cryptoRuntime === 'node:crypto') {
test('alg argument is ignored if jwk does not have alg for asymmetric keys', async (t) => {
const jwk = {
kty: 'EC',
crv: 'P-256',
x: 'jJ6Flys3zK9jUhnOHf6G49Dyp5hah6CNP84-gY-n9eo',
y: 'nhI6iD5eFXgBTLt_1p3aip-5VbZeMhxeFSpjfEAf7Ww',
}
await lib.importJWK(jwk)
t.ok(1)
})
} else {
test('alg argument must be present if jwk does not have alg for asymmetric keys', async (t) => {
const jwk = {
kty: 'EC',
crv: 'P-256',
x: 'jJ6Flys3zK9jUhnOHf6G49Dyp5hah6CNP84-gY-n9eo',
y: 'nhI6iD5eFXgBTLt_1p3aip-5VbZeMhxeFSpjfEAf7Ww',
}
await t.rejects(
lib.importJWK(jwk),
'"alg" argument is required when "jwk.alg" is not present',
)
await lib.importJWK(jwk, 'ES256')
await lib.importJWK({ ...jwk, alg: 'ES256' })
})
}
// if (lib.cryptoRuntime === 'node:crypto') {
// test('alg argument is ignored if jwk does not have alg for asymmetric keys', async (t) => {
// const jwk = {
// kty: 'EC',
// crv: 'P-256',
// x: 'jJ6Flys3zK9jUhnOHf6G49Dyp5hah6CNP84-gY-n9eo',
// y: 'nhI6iD5eFXgBTLt_1p3aip-5VbZeMhxeFSpjfEAf7Ww',
// }
// await lib.importJWK(jwk)
// t.ok(1)
// })
// } else {
// test('alg argument must be present if jwk does not have alg for asymmetric keys', async (t) => {
// const jwk = {
// kty: 'EC',
// crv: 'P-256',
// x: 'jJ6Flys3zK9jUhnOHf6G49Dyp5hah6CNP84-gY-n9eo',
// y: 'nhI6iD5eFXgBTLt_1p3aip-5VbZeMhxeFSpjfEAf7Ww',
// }
// await t.rejects(
// lib.importJWK(jwk),
// '"alg" argument is required when "jwk.alg" is not present',
// )
// await lib.importJWK(jwk, 'ES256')
// await lib.importJWK({ ...jwk, alg: 'ES256' })
// })
// }
}
14 changes: 7 additions & 7 deletions tap/pem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default (QUnit: QUnit, lib: typeof jose, keys: typeof jose) => {
['ES384', KEYS.P384.pkcs8, true],
['ES384', KEYS.P384.spki, true],
['ES384', KEYS.P384.x509, true],
['ES512', KEYS.P521.pkcs8, !env.isDeno],
['ES512', KEYS.P521.spki, !env.isDeno],
['ES512', KEYS.P521.x509, !env.isDeno],
['ES512', KEYS.P521.pkcs8, true],
['ES512', KEYS.P521.spki, true],
['ES512', KEYS.P521.x509, true],
['PS256', KEYS.RSA.pkcs8, true],
['PS256', KEYS.RSA.spki, true],
['PS256', KEYS.RSA.x509, true],
Expand Down Expand Up @@ -61,9 +61,9 @@ export default (QUnit: QUnit, lib: typeof jose, keys: typeof jose) => {
[['ECDH-ES', 'P-384'], KEYS.P384.pkcs8, true],
[['ECDH-ES', 'P-384'], KEYS.P384.spki, true],
[['ECDH-ES', 'P-384'], KEYS.P384.x509, true],
[['ECDH-ES', 'P-521'], KEYS.P521.pkcs8, !env.isDeno],
[['ECDH-ES', 'P-521'], KEYS.P521.spki, !env.isDeno],
[['ECDH-ES', 'P-521'], KEYS.P521.x509, !env.isDeno],
[['ECDH-ES', 'P-521'], KEYS.P521.pkcs8, true],
[['ECDH-ES', 'P-521'], KEYS.P521.spki, true],
[['ECDH-ES', 'P-521'], KEYS.P521.x509, true],
[
['ECDH-ES', 'X25519'],
KEYS.X25519.pkcs8,
Expand All @@ -85,7 +85,7 @@ export default (QUnit: QUnit, lib: typeof jose, keys: typeof jose) => {
(env.isGecko && env.isBrowserVersionAtLeast(130)),
],
[['ECDH-ES', 'X448'], KEYS.X448.pkcs8, env.isNode || env.isEdgeRuntime],
[['ECDH-ES', 'X448'], KEYS.X448.spki, env.isNode || env.isEdgeRuntime],
[['ECDH-ES', 'X448'], KEYS.X448.spki, env.isNode || env.isEdgeRuntime || env.isDeno],
[['EdDSA', 'Ed25519'], KEYS.Ed25519.pkcs8, !env.isBlink],
[['EdDSA', 'Ed25519'], KEYS.Ed25519.spki, !env.isBlink],
[['EdDSA', 'Ed25519'], KEYS.Ed25519.x509, !env.isBlink],
Expand Down

0 comments on commit 5df0893

Please sign in to comment.