@@ -402,6 +402,33 @@ public void testCreateBlobWithOptions() throws IOException {
402402 assertEquals (-1 , byteStream .read (streamBytes ));
403403 }
404404
405+ @ Test
406+ public void testCreateBlobWithEncryptionKey () throws IOException {
407+ Capture <ByteArrayInputStream > capturedStream = Capture .newInstance ();
408+ EasyMock .expect (storageRpcMock .create (
409+ EasyMock .eq (BLOB_INFO1 .toBuilder ().md5 (CONTENT_MD5 ).crc32c (CONTENT_CRC32C ).build ().toPb ()),
410+ EasyMock .capture (capturedStream ),
411+ EasyMock .eq (ENCRYPTION_KEY_OPTIONS )))
412+ .andReturn (BLOB_INFO1 .toPb ())
413+ .times (2 );
414+ EasyMock .replay (storageRpcMock );
415+ initializeService ();
416+ Blob blob = storage .create (BLOB_INFO1 , BLOB_CONTENT , BlobTargetOption .encryptionKey (KEY ));
417+ assertEquals (expectedBlob1 , blob );
418+ ByteArrayInputStream byteStream = capturedStream .getValue ();
419+ byte [] streamBytes = new byte [BLOB_CONTENT .length ];
420+ assertEquals (BLOB_CONTENT .length , byteStream .read (streamBytes ));
421+ assertArrayEquals (BLOB_CONTENT , streamBytes );
422+ assertEquals (-1 , byteStream .read (streamBytes ));
423+ blob = storage .create (BLOB_INFO1 , BLOB_CONTENT , BlobTargetOption .encryptionKey (BASE64_KEY ));
424+ assertEquals (expectedBlob1 , blob );
425+ byteStream = capturedStream .getValue ();
426+ streamBytes = new byte [BLOB_CONTENT .length ];
427+ assertEquals (BLOB_CONTENT .length , byteStream .read (streamBytes ));
428+ assertArrayEquals (BLOB_CONTENT , streamBytes );
429+ assertEquals (-1 , byteStream .read (streamBytes ));
430+ }
431+
405432 @ Test
406433 public void testCreateBlobFromStream () {
407434 ByteArrayInputStream fileStream = new ByteArrayInputStream (BLOB_CONTENT );
@@ -416,6 +443,24 @@ public void testCreateBlobFromStream() {
416443 assertEquals (expectedBlob1 , blob );
417444 }
418445
446+ @ Test
447+ public void testCreateBlobFromStreamWithEncryptionKey () throws IOException {
448+ ByteArrayInputStream fileStream = new ByteArrayInputStream (BLOB_CONTENT );
449+ BlobInfo .Builder infoBuilder = BLOB_INFO1 .toBuilder ();
450+ BlobInfo infoWithHashes = infoBuilder .md5 (CONTENT_MD5 ).crc32c (CONTENT_CRC32C ).build ();
451+ BlobInfo infoWithoutHashes = infoBuilder .md5 (null ).crc32c (null ).build ();
452+ EasyMock .expect (
453+ storageRpcMock .create (infoWithoutHashes .toPb (), fileStream , ENCRYPTION_KEY_OPTIONS ))
454+ .andReturn (BLOB_INFO1 .toPb ()).times (2 );
455+ EasyMock .replay (storageRpcMock );
456+ initializeService ();
457+ Blob blob =
458+ storage .create (infoWithHashes , fileStream , BlobWriteOption .encryptionKey (BASE64_KEY ));
459+ assertEquals (expectedBlob1 , blob );
460+ blob = storage .create (infoWithHashes , fileStream , BlobWriteOption .encryptionKey (BASE64_KEY ));
461+ assertEquals (expectedBlob1 , blob );
462+ }
463+
419464 @ Test
420465 public void testGetBucket () {
421466 EasyMock .expect (storageRpcMock .get (BucketInfo .of (BUCKET_NAME1 ).toPb (), EMPTY_RPC_OPTIONS ))
@@ -933,13 +978,22 @@ public void testCopyWithEncryptionKey() {
933978 ENCRYPTION_KEY_OPTIONS , true , request .target ().toPb (), ENCRYPTION_KEY_OPTIONS , null );
934979 StorageRpc .RewriteResponse rpcResponse = new StorageRpc .RewriteResponse (rpcRequest , null , 42L ,
935980 false , "token" , 21L );
936- EasyMock .expect (storageRpcMock .openRewrite (rpcRequest )).andReturn (rpcResponse );
981+ EasyMock .expect (storageRpcMock .openRewrite (rpcRequest )).andReturn (rpcResponse ). times ( 2 ) ;
937982 EasyMock .replay (storageRpcMock );
938983 initializeService ();
939984 CopyWriter writer = storage .copy (request );
940985 assertEquals (42L , writer .blobSize ());
941986 assertEquals (21L , writer .totalBytesCopied ());
942987 assertTrue (!writer .isDone ());
988+ request = Storage .CopyRequest .builder ()
989+ .source (BLOB_INFO2 .blobId ())
990+ .sourceOptions (BlobSourceOption .decryptionKey (BASE64_KEY ))
991+ .target (BLOB_INFO1 , BlobTargetOption .encryptionKey (KEY ))
992+ .build ();
993+ writer = storage .copy (request );
994+ assertEquals (42L , writer .blobSize ());
995+ assertEquals (21L , writer .totalBytesCopied ());
996+ assertTrue (!writer .isDone ());
943997 }
944998
945999 @ Test
@@ -1012,35 +1066,41 @@ public void testReadAllBytesWithOptions() {
10121066 public void testReadAllBytesWithDecriptionKey () {
10131067 EasyMock .expect (
10141068 storageRpcMock .load (BlobId .of (BUCKET_NAME1 , BLOB_NAME1 ).toPb (), ENCRYPTION_KEY_OPTIONS ))
1015- .andReturn (BLOB_CONTENT );
1069+ .andReturn (BLOB_CONTENT ). times ( 2 ) ;
10161070 EasyMock .replay (storageRpcMock );
10171071 initializeService ();
10181072 byte [] readBytes = storage .readAllBytes (BUCKET_NAME1 , BLOB_NAME1 ,
10191073 BlobSourceOption .decryptionKey (KEY ));
10201074 assertArrayEquals (BLOB_CONTENT , readBytes );
1075+ readBytes = storage .readAllBytes (BUCKET_NAME1 , BLOB_NAME1 ,
1076+ BlobSourceOption .decryptionKey (BASE64_KEY ));
1077+ assertArrayEquals (BLOB_CONTENT , readBytes );
10211078 }
10221079
10231080 @ Test
1024- public void testReadAllBytesWithBase64DecriptionKey () {
1081+ public void testReadAllBytesFromBlobIdWithOptions () {
10251082 EasyMock .expect (
1026- storageRpcMock .load (BlobId . of ( BUCKET_NAME1 , BLOB_NAME1 ).toPb (), ENCRYPTION_KEY_OPTIONS ))
1083+ storageRpcMock .load (BLOB_INFO1 . blobId ( ).toPb (), BLOB_SOURCE_OPTIONS ))
10271084 .andReturn (BLOB_CONTENT );
10281085 EasyMock .replay (storageRpcMock );
10291086 initializeService ();
1030- byte [] readBytes = storage .readAllBytes (BUCKET_NAME1 , BLOB_NAME1 ,
1031- BlobSourceOption . decryptionKey ( BASE64_KEY ) );
1087+ byte [] readBytes = storage .readAllBytes (BLOB_INFO1 . blobId () ,
1088+ BLOB_SOURCE_GENERATION_FROM_BLOB_ID , BLOB_SOURCE_METAGENERATION );
10321089 assertArrayEquals (BLOB_CONTENT , readBytes );
10331090 }
10341091
10351092 @ Test
1036- public void testReadAllBytesWithOptionsFromBlobId () {
1093+ public void testReadAllBytesFromBlobIdWithDecriptionKey () {
10371094 EasyMock .expect (
1038- storageRpcMock .load (BLOB_INFO1 .blobId ().toPb (), BLOB_SOURCE_OPTIONS ))
1039- .andReturn (BLOB_CONTENT );
1095+ storageRpcMock .load (BLOB_INFO1 .blobId ().toPb (), ENCRYPTION_KEY_OPTIONS ))
1096+ .andReturn (BLOB_CONTENT ). times ( 2 ) ;
10401097 EasyMock .replay (storageRpcMock );
10411098 initializeService ();
1042- byte [] readBytes = storage .readAllBytes (BLOB_INFO1 .blobId (),
1043- BLOB_SOURCE_GENERATION_FROM_BLOB_ID , BLOB_SOURCE_METAGENERATION );
1099+ byte [] readBytes =
1100+ storage .readAllBytes (BLOB_INFO1 .blobId (), BlobSourceOption .decryptionKey (KEY ));
1101+ assertArrayEquals (BLOB_CONTENT , readBytes );
1102+ readBytes =
1103+ storage .readAllBytes (BLOB_INFO1 .blobId (), BlobSourceOption .decryptionKey (BASE64_KEY ));
10441104 assertArrayEquals (BLOB_CONTENT , readBytes );
10451105 }
10461106
@@ -1086,26 +1146,15 @@ public void testReaderWithDecryptionKey() throws IOException {
10861146 byte [] result = new byte [DEFAULT_CHUNK_SIZE ];
10871147 EasyMock .expect (
10881148 storageRpcMock .read (BLOB_INFO2 .toPb (), ENCRYPTION_KEY_OPTIONS , 0 , DEFAULT_CHUNK_SIZE ))
1089- .andReturn (StorageRpc .Tuple .of ("etag" , result ));
1149+ .andReturn (StorageRpc .Tuple .of ("etag" , result )). times ( 2 ) ;
10901150 EasyMock .replay (storageRpcMock );
10911151 initializeService ();
10921152 ReadChannel channel =
10931153 storage .reader (BUCKET_NAME1 , BLOB_NAME2 , BlobSourceOption .decryptionKey (KEY ));
10941154 assertNotNull (channel );
10951155 assertTrue (channel .isOpen ());
10961156 channel .read (ByteBuffer .allocate (42 ));
1097- }
1098-
1099- @ Test
1100- public void testReaderWithBase64DecryptionKey () throws IOException {
1101- byte [] result = new byte [DEFAULT_CHUNK_SIZE ];
1102- EasyMock .expect (
1103- storageRpcMock .read (BLOB_INFO2 .toPb (), ENCRYPTION_KEY_OPTIONS , 0 , DEFAULT_CHUNK_SIZE ))
1104- .andReturn (StorageRpc .Tuple .of ("etag" , result ));
1105- EasyMock .replay (storageRpcMock );
1106- initializeService ();
1107- ReadChannel channel =
1108- storage .reader (BUCKET_NAME1 , BLOB_NAME2 , BlobSourceOption .decryptionKey (BASE64_KEY ));
1157+ channel = storage .reader (BUCKET_NAME1 , BLOB_NAME2 , BlobSourceOption .decryptionKey (BASE64_KEY ));
11091158 assertNotNull (channel );
11101159 assertTrue (channel .isOpen ());
11111160 channel .read (ByteBuffer .allocate (42 ));
@@ -1157,22 +1206,13 @@ public void testWriterWithOptions() {
11571206 public void testWriterWithEncryptionKey () {
11581207 BlobInfo info = BLOB_INFO1 .toBuilder ().md5 (null ).crc32c (null ).build ();
11591208 EasyMock .expect (storageRpcMock .open (info .toPb (), ENCRYPTION_KEY_OPTIONS ))
1160- .andReturn ("upload-id" );
1209+ .andReturn ("upload-id" ). times ( 2 ) ;
11611210 EasyMock .replay (storageRpcMock );
11621211 initializeService ();
11631212 WriteChannel channel = storage .writer (info , BlobWriteOption .encryptionKey (KEY ));
11641213 assertNotNull (channel );
11651214 assertTrue (channel .isOpen ());
1166- }
1167-
1168- @ Test
1169- public void testWriterWithBase64EncryptionKey () {
1170- BlobInfo info = BLOB_INFO1 .toBuilder ().md5 (null ).crc32c (null ).build ();
1171- EasyMock .expect (storageRpcMock .open (info .toPb (), ENCRYPTION_KEY_OPTIONS ))
1172- .andReturn ("upload-id" );
1173- EasyMock .replay (storageRpcMock );
1174- initializeService ();
1175- WriteChannel channel = storage .writer (info , BlobWriteOption .encryptionKey (BASE64_KEY ));
1215+ channel = storage .writer (info , BlobWriteOption .encryptionKey (BASE64_KEY ));
11761216 assertNotNull (channel );
11771217 assertTrue (channel .isOpen ());
11781218 }
0 commit comments