@@ -264,6 +264,135 @@ describe('Cryptographic Material Functions', () => {
264
264
expect ( test . encryptionContext ) . to . deep . equal ( encryptionMaterial . encryptionContext )
265
265
expect ( test . getUnencryptedDataKey ( ) ) . to . deep . equal ( encryptionMaterial . getUnencryptedDataKey ( ) )
266
266
} )
267
+
268
+ it ( 'Check for early return (Postcondition): If I can not cache the EncryptionMaterial, do not even look.' , async ( ) => {
269
+ const testCMM = {
270
+ _partition,
271
+ _maxAge,
272
+ _maxBytesEncrypted,
273
+ _maxMessagesEncrypted,
274
+ _cache : {
275
+ getEncryptionMaterial ( ) {
276
+ throw new Error ( 'should not happen' )
277
+ }
278
+ } ,
279
+ _backingMaterialsManager,
280
+ _cacheEntryHasExceededLimits : cacheEntryHasExceededLimits ( ) ,
281
+ getEncryptionMaterials : getEncryptionMaterials ( cacheKeyHelpers ) ,
282
+ decryptMaterials : decryptMaterials ( cacheKeyHelpers )
283
+ } as any
284
+
285
+ const testSuiteCacheSafe = await testCMM . getEncryptionMaterials ( {
286
+ suite : new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 ) ,
287
+ encryptionContext : context ,
288
+ frameLength : 10 ,
289
+ plaintextLength : 10
290
+ } )
291
+ // The response was not cloned... because it did not come from the cache
292
+ expect ( testSuiteCacheSafe === encryptionMaterial ) . to . equal ( true )
293
+
294
+ const testPlaintext = await testCMM . getEncryptionMaterials ( {
295
+ suite : nodeSuite ,
296
+ encryptionContext : context ,
297
+ frameLength : 10
298
+ } )
299
+ // The response was not cloned... because it did not come from the cache
300
+ expect ( testPlaintext === encryptionMaterial ) . to . equal ( true )
301
+ } )
302
+
303
+ it ( 'Check for early return (Postcondition): If I have a valid EncryptionMaterial, return it.' , async ( ) => {
304
+ let assertCount = 0
305
+ const testCMM = {
306
+ _partition,
307
+ _maxAge,
308
+ _maxBytesEncrypted,
309
+ _maxMessagesEncrypted,
310
+ _cache : {
311
+ getEncryptionMaterial ( ) {
312
+ assertCount += 1
313
+ return {
314
+ response : encryptionMaterial
315
+ }
316
+ }
317
+ } ,
318
+ _backingMaterialsManager,
319
+ _cacheEntryHasExceededLimits : ( ) => {
320
+ assertCount += 1
321
+ return false
322
+ } ,
323
+ getEncryptionMaterials : getEncryptionMaterials ( cacheKeyHelpers ) ,
324
+ decryptMaterials : ( ) => {
325
+ throw new Error ( 'this should never happen' )
326
+ }
327
+ } as any
328
+
329
+ await testCMM . getEncryptionMaterials ( {
330
+ suite : nodeSuite ,
331
+ encryptionContext : context ,
332
+ frameLength : 10 ,
333
+ plaintextLength : 10
334
+ } )
335
+
336
+ expect ( assertCount ) . to . equal ( 2 )
337
+ } )
338
+
339
+ it ( 'Check for early return (Postcondition): If I can not cache the EncryptionMaterial, just return it.' , async ( ) => {
340
+ let assertCount = 0
341
+
342
+ const suiteId = AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16
343
+
344
+ const nodeSuite = new NodeAlgorithmSuite ( suiteId )
345
+ const udk128 = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ] )
346
+ const trace = {
347
+ keyNamespace : 'keyNamespace' ,
348
+ keyName : 'keyName' ,
349
+ flags : KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY | KeyringTraceFlag . WRAPPING_KEY_DECRYPTED_DATA_KEY
350
+ }
351
+
352
+ const edk1 = new EncryptedDataKey ( { providerId : 'keyNamespace' , providerInfo : 'keyName' , encryptedDataKey : new Uint8Array ( [ 1 ] ) } )
353
+ const edk2 = new EncryptedDataKey ( { providerId : 'p2' , providerInfo : 'pi2' , encryptedDataKey : new Uint8Array ( [ 2 ] ) } )
354
+
355
+ const encryptionMaterial = new NodeEncryptionMaterial ( nodeSuite , { } )
356
+ . setUnencryptedDataKey ( udk128 , trace )
357
+ . addEncryptedDataKey ( edk1 , KeyringTraceFlag . WRAPPING_KEY_ENCRYPTED_DATA_KEY )
358
+ . addEncryptedDataKey ( edk2 , KeyringTraceFlag . WRAPPING_KEY_ENCRYPTED_DATA_KEY )
359
+
360
+ const testCMM = {
361
+ _partition,
362
+ _maxAge,
363
+ _maxBytesEncrypted,
364
+ _maxMessagesEncrypted,
365
+ _cache : {
366
+ getEncryptionMaterial ( ) {
367
+ throw new Error ( 'this should never happen' )
368
+ } ,
369
+ del ( ) { }
370
+ } ,
371
+ _backingMaterialsManager : {
372
+ getEncryptionMaterials ( ) {
373
+ assertCount += 1
374
+ return encryptionMaterial
375
+ }
376
+ } ,
377
+ _cacheEntryHasExceededLimits : ( ) => {
378
+ throw new Error ( 'this should never happen' )
379
+ } ,
380
+ getEncryptionMaterials : getEncryptionMaterials ( cacheKeyHelpers ) ,
381
+ decryptMaterials : ( ) => {
382
+ throw new Error ( 'this should never happen' )
383
+ }
384
+ } as any
385
+
386
+ const test = await testCMM . getEncryptionMaterials ( {
387
+ suite : nodeSuite ,
388
+ encryptionContext : context ,
389
+ frameLength : 10 ,
390
+ plaintextLength : 10
391
+ } )
392
+
393
+ expect ( assertCount ) . to . equal ( 1 )
394
+ expect ( test === encryptionMaterial ) . to . equal ( true )
395
+ } )
267
396
} )
268
397
269
398
describe ( 'decryptionMaterial' , ( ) => {
@@ -278,5 +407,63 @@ describe('Cryptographic Material Functions', () => {
278
407
expect ( test . encryptionContext ) . to . deep . equal ( decryptionMaterial . encryptionContext )
279
408
expect ( test . getUnencryptedDataKey ( ) ) . to . deep . equal ( decryptionMaterial . getUnencryptedDataKey ( ) )
280
409
} )
410
+
411
+ it ( 'Check for early return (Postcondition): If I can not cache the DecryptionMaterial, do not even look.' , async ( ) => {
412
+ const testCMM = {
413
+ _partition,
414
+ _maxAge,
415
+ _maxBytesEncrypted,
416
+ _maxMessagesEncrypted,
417
+ _cache : {
418
+ getDecryptionMaterial ( ) {
419
+ throw new Error ( 'should not happen' )
420
+ }
421
+ } ,
422
+ _backingMaterialsManager,
423
+ _cacheEntryHasExceededLimits : cacheEntryHasExceededLimits ( ) ,
424
+ getEncryptionMaterials : getEncryptionMaterials ( cacheKeyHelpers ) ,
425
+ decryptMaterials : decryptMaterials ( cacheKeyHelpers )
426
+ } as any
427
+
428
+ const testSuiteCacheSafe = await testCMM . decryptMaterials ( {
429
+ suite : new NodeAlgorithmSuite ( AlgorithmSuiteIdentifier . ALG_AES128_GCM_IV12_TAG16 ) ,
430
+ encryptionContext : context ,
431
+ encryptedDataKeys : [ edk1 ]
432
+ } )
433
+ // The response was not cloned... because it did not come from the cache
434
+ expect ( testSuiteCacheSafe === decryptionMaterial ) . to . equal ( true )
435
+ } )
436
+
437
+ it ( 'Check for early return (Postcondition): If I have a valid DecryptionMaterial, return it.' , async ( ) => {
438
+ let assertCount = 0
439
+ const testCMM = {
440
+ _partition,
441
+ _maxAge,
442
+ _maxBytesEncrypted,
443
+ _maxMessagesEncrypted,
444
+ _cache : {
445
+ getDecryptionMaterial ( ) {
446
+ assertCount += 1
447
+ return {
448
+ response : decryptionMaterial
449
+ }
450
+ }
451
+ } ,
452
+ _backingMaterialsManager,
453
+ _cacheEntryHasExceededLimits : ( ) => {
454
+ assertCount += 1
455
+ return false
456
+ } ,
457
+ getEncryptionMaterials : getEncryptionMaterials ( cacheKeyHelpers ) ,
458
+ decryptMaterials : decryptMaterials ( cacheKeyHelpers )
459
+ } as any
460
+
461
+ await testCMM . decryptMaterials ( {
462
+ suite : nodeSuite ,
463
+ encryptionContext : context ,
464
+ encryptedDataKeys : [ edk1 ]
465
+ } )
466
+ expect ( assertCount ) . to . equal ( 2 )
467
+ } )
281
468
} )
282
469
} )
0 commit comments