-
-
Notifications
You must be signed in to change notification settings - Fork 318
/
5_12.protecting_content_only.test.js
74 lines (58 loc) · 2.96 KB
/
5_12.protecting_content_only.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const test = require('ava')
if ('electron' in process.versions) return
const recipe = require('./recipes').get('5.12')
const { enc: verifiers } = require('./verifiers')
const { JWE, JWK: { asKey, generateSync }, JWKS: { KeyStore }, errors } = require('../..')
const {
input: { plaintext, key: jwk },
encrypting_content: { unprotected }
} = recipe
const key = asKey(jwk)
const keystoreEmpty = new KeyStore()
const keystoreMatchOne = new KeyStore(generateSync(key.kty, key.length, { alg: key.alg, use: key.use }), key)
const keystoreMatchMore = new KeyStore(generateSync(key.kty, key.length, { alg: key.alg, use: key.use, kid: key.kid }), key, asKey(key))
const keystoreMatchNone = new KeyStore(generateSync(key.kty), generateSync(key.kty))
test(`${recipe.title} - flattened encrypt`, t => {
const res = JWE.encrypt.flattened(plaintext, key, undefined, undefined, unprotected)
verifiers.flattened(t, res, recipe.output.json_flat)
t.deepEqual(JWE.decrypt(res, key), Buffer.from(plaintext))
})
test(`${recipe.title} - general encrypt`, t => {
const res = JWE.encrypt.general(plaintext, key, undefined, undefined, unprotected)
verifiers.general(t, res, recipe.output.json)
t.deepEqual(JWE.decrypt(res, key), Buffer.from(plaintext))
})
test(`${recipe.title} - flattened decrypt`, t => {
t.deepEqual(JWE.decrypt(recipe.output.json_flat, key), Buffer.from(plaintext))
})
test(`${recipe.title} - general decrypt`, t => {
t.deepEqual(JWE.decrypt(recipe.output.json, key), Buffer.from(plaintext))
})
;[keystoreMatchOne, keystoreMatchMore].forEach((keystore, i) => {
test(`${recipe.title} - flattened decrypt (using keystore ${i + 1}/2)`, t => {
t.deepEqual(JWE.decrypt(recipe.output.json_flat, keystore), Buffer.from(plaintext))
})
test(`${recipe.title} - general decrypt (using keystore ${i + 1}/2)`, t => {
t.deepEqual(JWE.decrypt(recipe.output.json, keystore), Buffer.from(plaintext))
})
})
test(`${recipe.title} - flattened decrypt (failing)`, t => {
t.throws(() => {
JWE.decrypt(recipe.output.json_flat, keystoreMatchNone)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - general decrypt (failing)`, t => {
t.throws(() => {
JWE.decrypt(recipe.output.json, keystoreMatchNone)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - flattened decrypt (using empty keystore)`, t => {
t.throws(() => {
JWE.decrypt(recipe.output.json_flat, keystoreEmpty)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - general decrypt (using empty keystore)`, t => {
t.throws(() => {
JWE.decrypt(recipe.output.json, keystoreEmpty)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})