@@ -10,23 +10,67 @@ import {
10
10
NodeEncryptionMaterial ,
11
11
NodeDecryptionMaterial ,
12
12
AlgorithmSuiteIdentifier ,
13
+ NodeBranchKeyMaterial ,
13
14
} from '@aws-crypto/material-management'
15
+ import { v4 } from 'uuid'
14
16
15
17
const nodeSuite = new NodeAlgorithmSuite (
16
18
AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256
17
19
)
18
20
const encryptionMaterial = new NodeEncryptionMaterial ( nodeSuite , { } )
19
21
const decryptionMaterial = new NodeDecryptionMaterial ( nodeSuite , { } )
22
+ const branchKeyMaterial = new NodeBranchKeyMaterial (
23
+ Buffer . alloc ( 32 ) ,
24
+ 'id' ,
25
+ v4 ( ) ,
26
+ { }
27
+ )
20
28
21
29
describe ( 'getLocalCryptographicMaterialsCache' , ( ) => {
22
30
const {
23
31
getEncryptionMaterial,
24
32
getDecryptionMaterial,
33
+ getBranchKeyMaterial,
25
34
del,
26
35
putEncryptionMaterial,
27
36
putDecryptionMaterial,
37
+ putBranchKeyMaterial,
28
38
} = getLocalCryptographicMaterialsCache ( 100 )
29
39
40
+ it ( 'putBranchKeyMaterial' , ( ) => {
41
+ const key = 'some encryption key'
42
+ const response : any = branchKeyMaterial
43
+
44
+ putBranchKeyMaterial ( key , response )
45
+ const test = getBranchKeyMaterial ( key )
46
+ if ( ! test ) throw new Error ( 'never' )
47
+ expect ( test . response === response ) . to . equal ( true )
48
+ expect ( Object . isFrozen ( test . response ) ) . to . equal ( true )
49
+ } )
50
+
51
+ it ( 'Precondition: Only cache BranchKeyMaterial' , ( ) => {
52
+ const key = 'some decryption key'
53
+ const response : any = 'not material'
54
+
55
+ expect ( ( ) => putBranchKeyMaterial ( key , response ) ) . to . throw ( )
56
+ } )
57
+
58
+ it ( 'Postcondition: If this key does not have a BranchKeyMaterial, return false' , ( ) => {
59
+ const test = getBranchKeyMaterial ( 'does-not-exist' )
60
+ expect ( test ) . to . equal ( false )
61
+ } )
62
+
63
+ it ( 'Postcondition: Only return BranchKeyMaterial' , ( ) => {
64
+ putDecryptionMaterial ( 'key1' , decryptionMaterial )
65
+ putEncryptionMaterial ( 'key2' , encryptionMaterial , 1 )
66
+
67
+ expect ( ( ) => getBranchKeyMaterial ( 'key1' ) ) . to . throw ( )
68
+ expect ( ( ) => getBranchKeyMaterial ( 'key2' ) ) . to . throw ( )
69
+
70
+ putBranchKeyMaterial ( 'key3' , branchKeyMaterial )
71
+ expect ( ( ) => getBranchKeyMaterial ( 'key3' ) )
72
+ } )
73
+
30
74
it ( 'putEncryptionMaterial' , ( ) => {
31
75
const key = 'some encryption key'
32
76
const response : any = encryptionMaterial
@@ -151,6 +195,52 @@ describe('getLocalCryptographicMaterialsCache', () => {
151
195
} )
152
196
153
197
describe ( 'cache eviction' , ( ) => {
198
+ it ( 'putBranchKeyMaterial can exceed capacity' , ( ) => {
199
+ const { getBranchKeyMaterial, putBranchKeyMaterial } =
200
+ getLocalCryptographicMaterialsCache ( 1 )
201
+
202
+ const key1 = 'key lost'
203
+ const key2 = 'key replace'
204
+ const response : any = branchKeyMaterial
205
+
206
+ putBranchKeyMaterial ( key1 , response )
207
+ putBranchKeyMaterial ( key2 , response )
208
+ const lost = getBranchKeyMaterial ( key1 )
209
+ const found = getBranchKeyMaterial ( key2 )
210
+ expect ( lost ) . to . equal ( false )
211
+ expect ( found ) . to . not . equal ( false )
212
+ } )
213
+
214
+ it ( 'putBranchKeyMaterial can be deleted' , ( ) => {
215
+ const { getBranchKeyMaterial, putBranchKeyMaterial, del } =
216
+ getLocalCryptographicMaterialsCache ( 1 )
217
+
218
+ const key = 'key deleted'
219
+ const response : any = branchKeyMaterial
220
+
221
+ putBranchKeyMaterial ( key , response )
222
+ del ( key )
223
+ const lost = getBranchKeyMaterial ( key )
224
+ expect ( lost ) . to . equal ( false )
225
+ } )
226
+
227
+ it ( 'putBranchKeyMaterial can be garbage collected' , async ( ) => {
228
+ const { getBranchKeyMaterial, putBranchKeyMaterial } =
229
+ // set TTL to 10 ms so that our branch key material entry is evicted between the
230
+ // put and get operation (which have a 20 ms gap). This will simulate a
231
+ // case where we try to query our branch key material but it was already
232
+ // garbage collected
233
+ getLocalCryptographicMaterialsCache ( 1 , 10 )
234
+
235
+ const key = 'key lost'
236
+ const response : any = branchKeyMaterial
237
+
238
+ putBranchKeyMaterial ( key , response , 1 )
239
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 20 ) )
240
+ const lost = getBranchKeyMaterial ( key )
241
+ expect ( lost ) . to . equal ( false )
242
+ } )
243
+
154
244
it ( 'putDecryptionMaterial can exceed capacity' , ( ) => {
155
245
const { getDecryptionMaterial, putDecryptionMaterial } =
156
246
getLocalCryptographicMaterialsCache ( 1 )
0 commit comments