test: refactor jwe long running tests

This commit is contained in:
Filip Skokan 2020-02-19 10:53:08 +01:00
parent ddda88e4d7
commit 734a9dc2ab
13 changed files with 129 additions and 108 deletions

View file

@ -1,5 +1,6 @@
const { readFileSync } = require('fs')
const { join } = require('path')
const { improvedDH } = require('../../lib/help/runtime_support')
module.exports.JWK = {
RSA_PUBLIC: {
@ -89,26 +90,28 @@ module.exports.JWK = {
}
}
module.exports.RSA_512 = readFileSync(join(__dirname, 'rsa_512.pem'))
module.exports.PEM = {
RSA: {
private: readFileSync(join(__dirname, 'rsa.key')),
public: readFileSync(join(__dirname, 'rsa.pem'))
},
Ed25519: {
testEnc: false,
private: readFileSync(join(__dirname, 'Ed25519.key')),
public: readFileSync(join(__dirname, 'Ed25519.pem'))
},
Ed448: {
testEnc: false,
private: readFileSync(join(__dirname, 'Ed448.key')),
public: readFileSync(join(__dirname, 'Ed448.pem'))
},
X25519: {
testEnc: improvedDH && !('electron' in process.versions),
private: readFileSync(join(__dirname, 'X25519.key')),
public: readFileSync(join(__dirname, 'X25519.pem'))
},
X448: {
testEnc: improvedDH && !('electron' in process.versions),
private: readFileSync(join(__dirname, 'X448.key')),
public: readFileSync(join(__dirname, 'X448.pem'))
},
@ -117,10 +120,12 @@ module.exports.PEM = {
public: readFileSync(join(__dirname, 'P-256.pem'))
},
secp256k1: {
testEnc: false,
private: readFileSync(join(__dirname, 'secp256k1.key')),
public: readFileSync(join(__dirname, 'secp256k1.pem'))
},
'P-256K': {
testEnc: false,
private: readFileSync(join(__dirname, 'secp256k1.key')),
public: readFileSync(join(__dirname, 'secp256k1.pem'))
},

View file

@ -1,4 +0,0 @@
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANfIj0FsIfWjNqyGxriALLDMU6qUTts+
LvQc2rVWj9iC4IPFZKIFZB10V+FDGBsA8o9VmQlA6/fDxsZNW+rRLckCAwEAAQ==
-----END PUBLIC KEY-----

View file

@ -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)
})
})

View 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)
})
})
}

View file

@ -0,0 +1 @@
require('../macros/test_asymm_enc')('P-256')

View file

@ -0,0 +1 @@
require('../macros/test_asymm_enc')('P-384')

View file

@ -0,0 +1 @@
require('../macros/test_asymm_enc')('P-521')

View file

@ -0,0 +1 @@
require('../macros/test_asymm_enc')('RSA')

View 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)
})
})
}

View file

@ -1,77 +1,15 @@
const test = require('ava')
const { randomBytes } = require('crypto')
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 { existsSync } = require('fs')
const path = require('path')
const fixtures = require('../fixtures')
const { JWE: { success, failure } } = require('../macros')
Object.entries(fixtures.PEM).forEach(([type, { private: key, public: pub }]) => {
if (type === 'P-256K') return
if ('electron' in process.versions && (type.startsWith('X') || type === 'Ed448' || type === 'secp256k1')) return
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)
Object.entries(fixtures.PEM).forEach(([type, { testEnc = true }]) => {
if (testEnc) {
const filename = `smoke.${type.toLowerCase().replace('-', '')}.test.js`
test(`${type} is tested`, t => {
t.true(existsSync(path.join(__dirname, filename)))
})
})
}
})
;[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)
})
})
}

View file

@ -0,0 +1 @@
require('../macros/test_asymm_enc')('X25519')

View file

@ -0,0 +1 @@
require('../macros/test_asymm_enc')('X448')

View 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)
})
})
}