mirror of
https://github.com/danbulant/jose
synced 2026-05-24 12:35:36 +00:00
test: refactor jwe long running tests
This commit is contained in:
parent
ddda88e4d7
commit
734a9dc2ab
13 changed files with 129 additions and 108 deletions
9
test/fixtures/index.js
vendored
9
test/fixtures/index.js
vendored
|
|
@ -1,5 +1,6 @@
|
||||||
const { readFileSync } = require('fs')
|
const { readFileSync } = require('fs')
|
||||||
const { join } = require('path')
|
const { join } = require('path')
|
||||||
|
const { improvedDH } = require('../../lib/help/runtime_support')
|
||||||
|
|
||||||
module.exports.JWK = {
|
module.exports.JWK = {
|
||||||
RSA_PUBLIC: {
|
RSA_PUBLIC: {
|
||||||
|
|
@ -89,26 +90,28 @@ module.exports.JWK = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.RSA_512 = readFileSync(join(__dirname, 'rsa_512.pem'))
|
|
||||||
|
|
||||||
module.exports.PEM = {
|
module.exports.PEM = {
|
||||||
RSA: {
|
RSA: {
|
||||||
private: readFileSync(join(__dirname, 'rsa.key')),
|
private: readFileSync(join(__dirname, 'rsa.key')),
|
||||||
public: readFileSync(join(__dirname, 'rsa.pem'))
|
public: readFileSync(join(__dirname, 'rsa.pem'))
|
||||||
},
|
},
|
||||||
Ed25519: {
|
Ed25519: {
|
||||||
|
testEnc: false,
|
||||||
private: readFileSync(join(__dirname, 'Ed25519.key')),
|
private: readFileSync(join(__dirname, 'Ed25519.key')),
|
||||||
public: readFileSync(join(__dirname, 'Ed25519.pem'))
|
public: readFileSync(join(__dirname, 'Ed25519.pem'))
|
||||||
},
|
},
|
||||||
Ed448: {
|
Ed448: {
|
||||||
|
testEnc: false,
|
||||||
private: readFileSync(join(__dirname, 'Ed448.key')),
|
private: readFileSync(join(__dirname, 'Ed448.key')),
|
||||||
public: readFileSync(join(__dirname, 'Ed448.pem'))
|
public: readFileSync(join(__dirname, 'Ed448.pem'))
|
||||||
},
|
},
|
||||||
X25519: {
|
X25519: {
|
||||||
|
testEnc: improvedDH && !('electron' in process.versions),
|
||||||
private: readFileSync(join(__dirname, 'X25519.key')),
|
private: readFileSync(join(__dirname, 'X25519.key')),
|
||||||
public: readFileSync(join(__dirname, 'X25519.pem'))
|
public: readFileSync(join(__dirname, 'X25519.pem'))
|
||||||
},
|
},
|
||||||
X448: {
|
X448: {
|
||||||
|
testEnc: improvedDH && !('electron' in process.versions),
|
||||||
private: readFileSync(join(__dirname, 'X448.key')),
|
private: readFileSync(join(__dirname, 'X448.key')),
|
||||||
public: readFileSync(join(__dirname, 'X448.pem'))
|
public: readFileSync(join(__dirname, 'X448.pem'))
|
||||||
},
|
},
|
||||||
|
|
@ -117,10 +120,12 @@ module.exports.PEM = {
|
||||||
public: readFileSync(join(__dirname, 'P-256.pem'))
|
public: readFileSync(join(__dirname, 'P-256.pem'))
|
||||||
},
|
},
|
||||||
secp256k1: {
|
secp256k1: {
|
||||||
|
testEnc: false,
|
||||||
private: readFileSync(join(__dirname, 'secp256k1.key')),
|
private: readFileSync(join(__dirname, 'secp256k1.key')),
|
||||||
public: readFileSync(join(__dirname, 'secp256k1.pem'))
|
public: readFileSync(join(__dirname, 'secp256k1.pem'))
|
||||||
},
|
},
|
||||||
'P-256K': {
|
'P-256K': {
|
||||||
|
testEnc: false,
|
||||||
private: readFileSync(join(__dirname, 'secp256k1.key')),
|
private: readFileSync(join(__dirname, 'secp256k1.key')),
|
||||||
public: readFileSync(join(__dirname, 'secp256k1.pem'))
|
public: readFileSync(join(__dirname, 'secp256k1.pem'))
|
||||||
},
|
},
|
||||||
|
|
|
||||||
4
test/fixtures/rsa_512.pem
vendored
4
test/fixtures/rsa_512.pem
vendored
|
|
@ -1,4 +0,0 @@
|
||||||
-----BEGIN PUBLIC KEY-----
|
|
||||||
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANfIj0FsIfWjNqyGxriALLDMU6qUTts+
|
|
||||||
LvQc2rVWj9iC4IPFZKIFZB10V+FDGBsA8o9VmQlA6/fDxsZNW+rRLckCAwEAAQ==
|
|
||||||
-----END PUBLIC KEY-----
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
// require 'secp256k1' renamed to 'P-256K'
|
|
||||||
require('../../P-256K')
|
|
||||||
|
|
||||||
const test = require('ava')
|
|
||||||
|
|
||||||
if ('electron' in process.versions) return
|
|
||||||
|
|
||||||
const { JWK: { asKey } } = require('../..')
|
|
||||||
|
|
||||||
const ENCS = [
|
|
||||||
'A128GCM',
|
|
||||||
'A192GCM',
|
|
||||||
'A256GCM',
|
|
||||||
'A128CBC-HS256',
|
|
||||||
'A192CBC-HS384',
|
|
||||||
'A256CBC-HS512'
|
|
||||||
]
|
|
||||||
|
|
||||||
const type = 'P-256K'
|
|
||||||
const { private: key, public: pub } = require('../fixtures').PEM[type]
|
|
||||||
|
|
||||||
const { JWE: { success, failure } } = require('../macros')
|
|
||||||
|
|
||||||
const eKey = asKey(pub)
|
|
||||||
const dKey = asKey(key)
|
|
||||||
|
|
||||||
;[...eKey.algorithms('wrapKey'), ...eKey.algorithms('deriveKey')].forEach((alg) => {
|
|
||||||
ENCS.forEach((enc) => {
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc}`, success, eKey, dKey, alg, enc)
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc} (negative cases)`, failure, eKey, dKey, alg, enc)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
52
test/jwe/smoke.oct.test.js
Normal file
52
test/jwe/smoke.oct.test.js
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
const test = require('ava')
|
||||||
|
|
||||||
|
const { randomBytes } = require('crypto')
|
||||||
|
|
||||||
|
const { keyObjectSupported } = require('../../lib/help/runtime_support')
|
||||||
|
const { JWK: { asKey } } = require('../..')
|
||||||
|
const registry = require('../../lib/registry')
|
||||||
|
|
||||||
|
const { JWE: { success, failure } } = require('../macros')
|
||||||
|
|
||||||
|
const ENCS = [...registry.JWA.encrypt.keys()]
|
||||||
|
|
||||||
|
;[16, 24, 32, 48, 64].forEach((len) => {
|
||||||
|
const sk = randomBytes(len)
|
||||||
|
const sym = asKey(sk)
|
||||||
|
;[...sym.algorithms('wrapKey'), 'dir'].forEach((alg) => {
|
||||||
|
(alg === 'dir' ? sym.algorithms('encrypt') : ENCS).forEach((enc) => {
|
||||||
|
test(`key ${sym.kty}(${len * 8} bits) > alg ${alg} > ${enc}`, success, sym, sym, alg, enc)
|
||||||
|
test(`key ${sym.kty}(${len * 8} bits) > alg ${alg} > ${enc} (key as bare input)`, success, sk, sk, alg, enc)
|
||||||
|
if (keyObjectSupported) {
|
||||||
|
test(`key ${sym.kty}(${len * 8} bits) > alg ${alg} > ${enc} (key as keyobject)`, success, sym.keyObject, sym.keyObject, alg, enc)
|
||||||
|
}
|
||||||
|
test(`key ${sym.kty}(${len * 8} bits) > alg ${alg} > ${enc} (key as JWK)`, success, sym.toJWK(true), sym.toJWK(true), alg, enc)
|
||||||
|
test(`key ${sym.kty}(${len * 8} bits) > alg ${alg} > ${enc} (negative cases, key as bare input)`, failure, sk, sk, alg, enc)
|
||||||
|
if (keyObjectSupported) {
|
||||||
|
test(`key ${sym.kty}(${len * 8} bits) > alg ${alg} > ${enc} (negative cases, key as keyobject)`, failure, sym.keyObject, sym.keyObject, alg, enc)
|
||||||
|
}
|
||||||
|
test(`key ${sym.kty}(${len * 8} bits) > alg ${alg} > ${enc} (negative cases, key as JWK)`, failure, sym.toJWK(true), sym.toJWK(true), alg, enc)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
{
|
||||||
|
// PBES2 derive only
|
||||||
|
const sk = Buffer.from('hunter2', 'utf-8')
|
||||||
|
const sym = asKey(sk)
|
||||||
|
sym.algorithms('deriveKey').forEach((alg) => {
|
||||||
|
ENCS.forEach((enc) => {
|
||||||
|
test(`key ${sym.kty}(password) > alg ${alg} > ${enc}`, success, sym, sym, alg, enc)
|
||||||
|
test(`key ${sym.kty}(password) > alg ${alg} > ${enc} (key as bare input)`, success, sk, sk, alg, enc)
|
||||||
|
if (keyObjectSupported) {
|
||||||
|
test(`key ${sym.kty}(password) > alg ${alg} > ${enc} (key as keyobject)`, success, sym.keyObject, sym.keyObject, alg, enc)
|
||||||
|
}
|
||||||
|
test(`key ${sym.kty}(password) > alg ${alg} > ${enc} (key as JWK)`, success, sym.toJWK(true), sym.toJWK(true), alg, enc)
|
||||||
|
test(`key ${sym.kty}(password) > alg ${alg} > ${enc} (negative cases, key as bare input)`, failure, sk, sk, alg, enc)
|
||||||
|
if (keyObjectSupported) {
|
||||||
|
test(`key ${sym.kty}(password) > alg ${alg} > ${enc} (negative cases, key as keyobject)`, failure, sym.keyObject, sym.keyObject, alg, enc)
|
||||||
|
}
|
||||||
|
test(`key ${sym.kty}(password) > alg ${alg} > ${enc} (negative cases, key as JWK)`, failure, sym.toJWK(true), sym.toJWK(true), alg, enc)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
1
test/jwe/smoke.p256.test.js
Normal file
1
test/jwe/smoke.p256.test.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
require('../macros/test_asymm_enc')('P-256')
|
||||||
1
test/jwe/smoke.p384.test.js
Normal file
1
test/jwe/smoke.p384.test.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
require('../macros/test_asymm_enc')('P-384')
|
||||||
1
test/jwe/smoke.p521.test.js
Normal file
1
test/jwe/smoke.p521.test.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
require('../macros/test_asymm_enc')('P-521')
|
||||||
1
test/jwe/smoke.rsa.test.js
Normal file
1
test/jwe/smoke.rsa.test.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
require('../macros/test_asymm_enc')('RSA')
|
||||||
20
test/jwe/smoke.rsamin.test.js
Normal file
20
test/jwe/smoke.rsamin.test.js
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
const test = require('ava')
|
||||||
|
|
||||||
|
const { JWK: { asKey, generateSync } } = require('../..')
|
||||||
|
const registry = require('../../lib/registry')
|
||||||
|
|
||||||
|
const { JWE: { success, failure } } = require('../macros')
|
||||||
|
|
||||||
|
const ENCS = [...registry.JWA.encrypt.keys()]
|
||||||
|
|
||||||
|
{
|
||||||
|
const rsa = generateSync('RSA')
|
||||||
|
const dKey = asKey({ kty: 'RSA', e: rsa.e, n: rsa.n, d: rsa.d }, { calculateMissingRSAPrimes: true })
|
||||||
|
const eKey = asKey({ kty: 'RSA', e: rsa.e, n: rsa.n })
|
||||||
|
eKey.algorithms('wrapKey').forEach((alg) => {
|
||||||
|
ENCS.forEach((enc) => {
|
||||||
|
test(`key RSA (min) > alg ${alg} > ${enc}`, success, eKey, dKey, alg, enc)
|
||||||
|
test(`key RSA (min) > alg ${alg} > ${enc} (negative cases)`, failure, eKey, dKey, alg, enc)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -1,77 +1,15 @@
|
||||||
const test = require('ava')
|
const test = require('ava')
|
||||||
|
|
||||||
const { randomBytes } = require('crypto')
|
const { existsSync } = require('fs')
|
||||||
|
const path = require('path')
|
||||||
const { edDSASupported, keyObjectSupported } = require('../../lib/help/runtime_support')
|
|
||||||
const { JWK: { asKey, generateSync } } = require('../..')
|
|
||||||
|
|
||||||
const ENCS = [
|
|
||||||
'A128GCM',
|
|
||||||
'A192GCM',
|
|
||||||
'A256GCM',
|
|
||||||
'A128CBC-HS256',
|
|
||||||
'A192CBC-HS384',
|
|
||||||
'A256CBC-HS512'
|
|
||||||
]
|
|
||||||
|
|
||||||
const fixtures = require('../fixtures')
|
const fixtures = require('../fixtures')
|
||||||
|
|
||||||
const { JWE: { success, failure } } = require('../macros')
|
Object.entries(fixtures.PEM).forEach(([type, { testEnc = true }]) => {
|
||||||
|
if (testEnc) {
|
||||||
Object.entries(fixtures.PEM).forEach(([type, { private: key, public: pub }]) => {
|
const filename = `smoke.${type.toLowerCase().replace('-', '')}.test.js`
|
||||||
if (type === 'P-256K') return
|
test(`${type} is tested`, t => {
|
||||||
if ('electron' in process.versions && (type.startsWith('X') || type === 'Ed448' || type === 'secp256k1')) return
|
t.true(existsSync(path.join(__dirname, filename)))
|
||||||
if (!edDSASupported && (type.startsWith('Ed') || type.startsWith('X'))) return
|
|
||||||
|
|
||||||
const eKey = asKey(pub)
|
|
||||||
const dKey = asKey(key)
|
|
||||||
|
|
||||||
;[...eKey.algorithms('wrapKey'), ...eKey.algorithms('deriveKey')].forEach((alg) => {
|
|
||||||
ENCS.forEach((enc) => {
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc}`, success, eKey, dKey, alg, enc)
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc} (key as bare input)`, success, pub, key, alg, enc)
|
|
||||||
if (keyObjectSupported) {
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc} (key as keyObject)`, success, eKey.keyObject, dKey.keyObject, alg, enc)
|
|
||||||
}
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc} (key as JWK)`, success, eKey.toJWK(false), dKey.toJWK(true), alg, enc)
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc} (negative cases)`, failure, eKey, dKey, alg, enc)
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc} (negative cases, key as bare input)`, failure, pub, key, alg, enc)
|
|
||||||
if (keyObjectSupported) {
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc} (negative cases, key as keyObject)`, failure, eKey.keyObject, dKey.keyObject, alg, enc)
|
|
||||||
}
|
|
||||||
test(`key ${type} > alg ${alg} > ${enc} (negative cases, key as JWK)`, failure, eKey.toJWK(false), dKey.toJWK(true), alg, enc)
|
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
;[16, 24, 32, 48, 64].forEach((len) => {
|
|
||||||
const sk = randomBytes(len)
|
|
||||||
const sym = asKey(sk)
|
|
||||||
;[...sym.algorithms('wrapKey'), ...sym.algorithms('deriveKey')].forEach((alg) => {
|
|
||||||
sym.algorithms('encrypt').forEach((enc) => {
|
|
||||||
test(`key ${sym.kty} > alg ${alg} > ${enc}`, success, sym, sym, alg, enc)
|
|
||||||
test(`key ${sym.kty} > alg ${alg} > ${enc} (key as bare input)`, success, sk, sk, alg, enc)
|
|
||||||
if (keyObjectSupported) {
|
|
||||||
test(`key ${sym.kty} > alg ${alg} > ${enc} (key as keyobject)`, success, sym.keyObject, sym.keyObject, alg, enc)
|
|
||||||
}
|
|
||||||
test(`key ${sym.kty} > alg ${alg} > ${enc} (key as JWK)`, success, sym.toJWK(true), sym.toJWK(true), alg, enc)
|
|
||||||
test(`key ${sym.kty} > alg ${alg} > ${enc} (negative cases, key as bare input)`, failure, sk, sk, alg, enc)
|
|
||||||
if (keyObjectSupported) {
|
|
||||||
test(`key ${sym.kty} > alg ${alg} > ${enc} (negative cases, key as keyobject)`, failure, sym.keyObject, sym.keyObject, alg, enc)
|
|
||||||
}
|
|
||||||
test(`key ${sym.kty} > alg ${alg} > ${enc} (negative cases, key as JWK)`, failure, sym.toJWK(true), sym.toJWK(true), alg, enc)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
{
|
|
||||||
const rsa = generateSync('RSA')
|
|
||||||
const dKey = asKey({ kty: 'RSA', e: rsa.e, n: rsa.n, d: rsa.d }, { calculateMissingRSAPrimes: true })
|
|
||||||
const eKey = asKey({ kty: 'RSA', e: rsa.e, n: rsa.n })
|
|
||||||
eKey.algorithms('wrapKey').forEach((alg) => {
|
|
||||||
ENCS.forEach((enc) => {
|
|
||||||
test(`key RSA (min) > alg ${alg} > ${enc}`, success, eKey, dKey, alg, enc)
|
|
||||||
test(`key RSA (min) > alg ${alg} > ${enc} (negative cases)`, failure, eKey, dKey, alg, enc)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
||||||
1
test/jwe/smoke.x25519.test.js
Normal file
1
test/jwe/smoke.x25519.test.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
require('../macros/test_asymm_enc')('X25519')
|
||||||
1
test/jwe/smoke.x448.test.js
Normal file
1
test/jwe/smoke.x448.test.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
require('../macros/test_asymm_enc')('X448')
|
||||||
36
test/macros/test_asymm_enc.js
Normal file
36
test/macros/test_asymm_enc.js
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
const test = require('ava')
|
||||||
|
|
||||||
|
const { keyObjectSupported } = require('../../lib/help/runtime_support')
|
||||||
|
const { JWK: { asKey } } = require('../..')
|
||||||
|
const registry = require('../../lib/registry')
|
||||||
|
|
||||||
|
const fixtures = require('../fixtures')
|
||||||
|
const { JWE: { success, failure } } = require('../macros')
|
||||||
|
|
||||||
|
const ENCS = [...registry.JWA.encrypt.keys()]
|
||||||
|
|
||||||
|
module.exports = (type) => {
|
||||||
|
const { private: key, public: pub, testEnc = true } = fixtures.PEM[type]
|
||||||
|
|
||||||
|
if (!testEnc) return
|
||||||
|
|
||||||
|
const eKey = asKey(pub)
|
||||||
|
const dKey = asKey(key)
|
||||||
|
|
||||||
|
;[...eKey.algorithms('wrapKey'), ...eKey.algorithms('deriveKey')].forEach((alg) => {
|
||||||
|
ENCS.forEach((enc) => {
|
||||||
|
test(`key ${type} > alg ${alg} > ${enc}`, success, eKey, dKey, alg, enc)
|
||||||
|
test(`key ${type} > alg ${alg} > ${enc} (key as bare input)`, success, pub, key, alg, enc)
|
||||||
|
if (keyObjectSupported) {
|
||||||
|
test(`key ${type} > alg ${alg} > ${enc} (key as keyObject)`, success, eKey.keyObject, dKey.keyObject, alg, enc)
|
||||||
|
}
|
||||||
|
test(`key ${type} > alg ${alg} > ${enc} (key as JWK)`, success, eKey.toJWK(false), dKey.toJWK(true), alg, enc)
|
||||||
|
test(`key ${type} > alg ${alg} > ${enc} (negative cases)`, failure, eKey, dKey, alg, enc)
|
||||||
|
test(`key ${type} > alg ${alg} > ${enc} (negative cases, key as bare input)`, failure, pub, key, alg, enc)
|
||||||
|
if (keyObjectSupported) {
|
||||||
|
test(`key ${type} > alg ${alg} > ${enc} (negative cases, key as keyObject)`, failure, eKey.keyObject, dKey.keyObject, alg, enc)
|
||||||
|
}
|
||||||
|
test(`key ${type} > alg ${alg} > ${enc} (negative cases, key as JWK)`, failure, eKey.toJWK(false), dKey.toJWK(true), alg, enc)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue