@@ -566,6 +566,40 @@ public void testCreateBlobWithEncryptionKey() throws IOException {
566566 assertEquals (-1 , byteStream .read (streamBytes ));
567567 }
568568
569+ @ Test
570+ public void testCreateBlobWithKmsKeyName () throws IOException {
571+ Capture <ByteArrayInputStream > capturedStream = Capture .newInstance ();
572+ EasyMock .expect (
573+ storageRpcMock .create (
574+ EasyMock .eq (
575+ BLOB_INFO1
576+ .toBuilder ()
577+ .setMd5 (CONTENT_MD5 )
578+ .setCrc32c (CONTENT_CRC32C )
579+ .build ()
580+ .toPb ()),
581+ EasyMock .capture (capturedStream ),
582+ EasyMock .eq (KMS_KEY_NAME_OPTIONS )))
583+ .andReturn (BLOB_INFO1 .toPb ())
584+ .times (2 );
585+ EasyMock .replay (storageRpcMock );
586+ initializeService ();
587+ Blob blob = storage .create (BLOB_INFO1 , BLOB_CONTENT , BlobTargetOption .kmsKeyName (KMS_KEY_NAME ));
588+ assertEquals (expectedBlob1 , blob );
589+ ByteArrayInputStream byteStream = capturedStream .getValue ();
590+ byte [] streamBytes = new byte [BLOB_CONTENT .length ];
591+ assertEquals (BLOB_CONTENT .length , byteStream .read (streamBytes ));
592+ assertArrayEquals (BLOB_CONTENT , streamBytes );
593+ assertEquals (-1 , byteStream .read (streamBytes ));
594+ blob = storage .create (BLOB_INFO1 , BLOB_CONTENT , BlobTargetOption .kmsKeyName (KMS_KEY_NAME ));
595+ assertEquals (expectedBlob1 , blob );
596+ byteStream = capturedStream .getValue ();
597+ streamBytes = new byte [BLOB_CONTENT .length ];
598+ assertEquals (BLOB_CONTENT .length , byteStream .read (streamBytes ));
599+ assertArrayEquals (BLOB_CONTENT , streamBytes );
600+ assertEquals (-1 , byteStream .read (streamBytes ));
601+ }
602+
569603 @ Test
570604 public void testCreateBlobFromStream () throws IOException {
571605 Capture <ByteArrayInputStream > capturedStream = Capture .newInstance ();
@@ -1243,6 +1277,43 @@ public void testCopyWithEncryptionKey() {
12431277 assertTrue (!writer .isDone ());
12441278 }
12451279
1280+ @ Test
1281+ public void testCopyFromEncryptionKeyToKmsKeyName () {
1282+ CopyRequest request =
1283+ Storage .CopyRequest .newBuilder ()
1284+ .setSource (BLOB_INFO2 .getBlobId ())
1285+ .setSourceOptions (BlobSourceOption .decryptionKey (KEY ))
1286+ .setTarget (BLOB_INFO1 , BlobTargetOption .kmsKeyName (KMS_KEY_NAME ))
1287+ .build ();
1288+ StorageRpc .RewriteRequest rpcRequest =
1289+ new StorageRpc .RewriteRequest (
1290+ request .getSource ().toPb (),
1291+ ENCRYPTION_KEY_OPTIONS ,
1292+ true ,
1293+ request .getTarget ().toPb (),
1294+ KMS_KEY_NAME_OPTIONS ,
1295+ null );
1296+ StorageRpc .RewriteResponse rpcResponse =
1297+ new StorageRpc .RewriteResponse (rpcRequest , null , 42L , false , "token" , 21L );
1298+ EasyMock .expect (storageRpcMock .openRewrite (rpcRequest )).andReturn (rpcResponse ).times (2 );
1299+ EasyMock .replay (storageRpcMock );
1300+ initializeService ();
1301+ CopyWriter writer = storage .copy (request );
1302+ assertEquals (42L , writer .getBlobSize ());
1303+ assertEquals (21L , writer .getTotalBytesCopied ());
1304+ assertTrue (!writer .isDone ());
1305+ request =
1306+ Storage .CopyRequest .newBuilder ()
1307+ .setSource (BLOB_INFO2 .getBlobId ())
1308+ .setSourceOptions (BlobSourceOption .decryptionKey (BASE64_KEY ))
1309+ .setTarget (BLOB_INFO1 , BlobTargetOption .kmsKeyName (KMS_KEY_NAME ))
1310+ .build ();
1311+ writer = storage .copy (request );
1312+ assertEquals (42L , writer .getBlobSize ());
1313+ assertEquals (21L , writer .getTotalBytesCopied ());
1314+ assertTrue (!writer .isDone ());
1315+ }
1316+
12461317 @ Test
12471318 public void testCopyWithOptionsFromBlobId () {
12481319 CopyRequest request =
@@ -1494,6 +1565,22 @@ public void testWriterWithEncryptionKey() {
14941565 assertTrue (channel .isOpen ());
14951566 }
14961567
1568+ @ Test
1569+ public void testWriterWithKmsKeyName () {
1570+ BlobInfo info = BLOB_INFO1 .toBuilder ().setMd5 (null ).setCrc32c (null ).build ();
1571+ EasyMock .expect (storageRpcMock .open (info .toPb (), KMS_KEY_NAME_OPTIONS ))
1572+ .andReturn ("upload-id" )
1573+ .times (2 );
1574+ EasyMock .replay (storageRpcMock );
1575+ initializeService ();
1576+ WriteChannel channel = storage .writer (info , BlobWriteOption .kmsKeyName (KMS_KEY_NAME ));
1577+ assertNotNull (channel );
1578+ assertTrue (channel .isOpen ());
1579+ channel = storage .writer (info , BlobWriteOption .kmsKeyName (KMS_KEY_NAME ));
1580+ assertNotNull (channel );
1581+ assertTrue (channel .isOpen ());
1582+ }
1583+
14971584 @ Test
14981585 public void testSignUrl ()
14991586 throws NoSuchAlgorithmException , InvalidKeyException , SignatureException ,
0 commit comments