3636import java .util .EnumSet ;
3737import java .util .HashMap ;
3838import java .util .Map ;
39+ import java .util .concurrent .TimeUnit ;
3940
4041import org .junit .Assert ;
4142import org .junit .Before ;
4243import org .junit .Test ;
44+ import org .mockito .ArgumentCaptor ;
4345import org .mockito .Mockito ;
4446
47+ import com .amazonaws .encryptionsdk .caching .CachingCryptoMaterialsManager ;
48+ import com .amazonaws .encryptionsdk .caching .LocalCryptoMaterialsCache ;
4549import com .amazonaws .encryptionsdk .exception .AwsCryptoException ;
4650import com .amazonaws .encryptionsdk .exception .BadCiphertextException ;
4751import com .amazonaws .encryptionsdk .internal .StaticMasterKey ;
@@ -61,7 +65,7 @@ public void init() {
6165 masterKeyProvider = spy (new StaticMasterKey ("testmaterial" ));
6266
6367 encryptionClient_ = new AwsCrypto ();
64- encryptionClient_ .setEncryptionAlgorithm (CryptoAlgorithm .ALG_AES_192_GCM_IV12_TAG16_NO_KDF );
68+ encryptionClient_ .setEncryptionAlgorithm (CryptoAlgorithm .ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256 );
6569 }
6670
6771 private void doEncryptDecrypt (final CryptoAlgorithm cryptoAlg , final int byteSize , final int frameSize ) {
@@ -374,6 +378,28 @@ public void estimateCiphertextSizeWithoutEncContext() {
374378 assertTrue (errMsg , estimatedCiphertextSize - cipherText .length <= 16 );
375379 }
376380
381+ @ Test
382+ public void estimateCiphertextSize_usesCachedKeys () throws Exception {
383+ // Make sure estimateCiphertextSize works with cached CMMs
384+ CryptoMaterialsManager cmm = spy (new DefaultCryptoMaterialsManager (masterKeyProvider ));
385+
386+ CachingCryptoMaterialsManager cache = CachingCryptoMaterialsManager .newBuilder ()
387+ .withBackingMaterialsManager (cmm )
388+ .withMaxAge (Long .MAX_VALUE , TimeUnit .SECONDS )
389+ .withCache (new LocalCryptoMaterialsCache (1 ))
390+ .withMessageUseLimit (9999 )
391+ .withByteUseLimit (501 )
392+ .build ();
393+
394+ // These estimates should be cached, and should not consume any bytes from the byte use limit.
395+ encryptionClient_ .estimateCiphertextSize (cache , 500 , new HashMap <>());
396+ encryptionClient_ .estimateCiphertextSize (cache , 500 , new HashMap <>());
397+
398+ encryptionClient_ .encryptData (cache , new byte [500 ]);
399+
400+ verify (cmm , times (1 )).getMaterialsForEncrypt (any ());
401+ }
402+
377403 @ Test
378404 public void encryptDecryptWithoutEncContext () {
379405 final int ptSize = 1000000 ; // 1MB
0 commit comments