blobWriteOptions.add(Storage.BlobWriteOption.doesNotExist()); if (md5 != null) { blobInfoBuilder.setMd5(md5); blobWriteOptions.add(Storage.BlobWriteOption.md5Match()); if (crc32c != null) { blobInfoBuilder.setCrc32c(crc32c); blobWriteOptions.add(Storage.BlobWriteOption.crc32cMatch()); blobWriteOptions.add(Storage.BlobWriteOption.predefinedAcl( Storage.PredefinedAcl.valueOf(acl) )); .evaluateAttributeExpressions(ff).getValue(); if (encryptionKey != null) { blobWriteOptions.add(Storage.BlobWriteOption.encryptionKey(encryptionKey));
writeOptions.add(Storage.BlobWriteOption.doesNotExist()); break; case READ: writeOptions.add(Storage.BlobWriteOption.userProject(userProject));
@Test public void testCreateFromStreamNotExists() throws Exception { initializeExpectedBucket(5); BlobInfo info = BlobInfo.newBuilder(BlobId.of("b", "n", 0L)).setContentType(CONTENT_TYPE).build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; InputStream streamContent = new ByteArrayInputStream(content); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, streamContent, Storage.BlobWriteOption.generationMatch())) .andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", streamContent, CONTENT_TYPE, Bucket.BlobWriteOption.doesNotExist()); assertEquals(expectedBlob, blob); }
writeOptions.add(Storage.BlobWriteOption.doesNotExist()); break; case READ:
case PREDEFINED_ACL: return Tuple.of( blobInfo, Storage.BlobWriteOption.predefinedAcl((Storage.PredefinedAcl) value)); case IF_GENERATION_MATCH: blobId = BlobId.of(blobId.getBucket(), blobId.getName(), (Long) value); return Tuple.of( blobInfo.toBuilder().setBlobId(blobId).build(), Storage.BlobWriteOption.generationMatch()); case IF_GENERATION_NOT_MATCH: blobId = BlobId.of(blobId.getBucket(), blobId.getName(), (Long) value); return Tuple.of( blobInfo.toBuilder().setBlobId(blobId).build(), Storage.BlobWriteOption.generationNotMatch()); case IF_METAGENERATION_MATCH: return Tuple.of( blobInfo.toBuilder().setMetageneration((Long) value).build(), Storage.BlobWriteOption.metagenerationMatch()); case IF_METAGENERATION_NOT_MATCH: return Tuple.of( blobInfo.toBuilder().setMetageneration((Long) value).build(), Storage.BlobWriteOption.metagenerationNotMatch()); case IF_MD5_MATCH: return Tuple.of( blobInfo.toBuilder().setMd5((String) value).build(), Storage.BlobWriteOption.md5Match()); case IF_CRC32C_MATCH: return Tuple.of( blobInfo.toBuilder().setCrc32c((String) value).build(), Storage.BlobWriteOption.crc32cMatch());
storage.create(blobInfoBuilder.build(), fileInputStream, BlobWriteOption.encryptionKey(uploadMetadata.getEncryptionKey().get())); } else { storage.create(blobInfoBuilder.build(), fileInputStream);
info, streamContent, Storage.BlobWriteOption.generationMatch(), Storage.BlobWriteOption.metagenerationMatch(), Storage.BlobWriteOption.predefinedAcl(acl), Storage.BlobWriteOption.crc32cMatch(), Storage.BlobWriteOption.md5Match(), Storage.BlobWriteOption.encryptionKey(BASE64_KEY), Storage.BlobWriteOption.userProject(USER_PROJECT))) .andReturn(expectedBlob); replay(storage);
case PREDEFINED_ACL: return Tuple.of( blobInfo, Storage.BlobWriteOption.predefinedAcl((Storage.PredefinedAcl) value)); case IF_GENERATION_MATCH: blobId = BlobId.of(blobId.getBucket(), blobId.getName(), (Long) value); return Tuple.of( blobInfo.toBuilder().setBlobId(blobId).build(), Storage.BlobWriteOption.generationMatch()); case IF_GENERATION_NOT_MATCH: blobId = BlobId.of(blobId.getBucket(), blobId.getName(), (Long) value); return Tuple.of( blobInfo.toBuilder().setBlobId(blobId).build(), Storage.BlobWriteOption.generationNotMatch()); case IF_METAGENERATION_MATCH: return Tuple.of( blobInfo.toBuilder().setMetageneration((Long) value).build(), Storage.BlobWriteOption.metagenerationMatch()); case IF_METAGENERATION_NOT_MATCH: return Tuple.of( blobInfo.toBuilder().setMetageneration((Long) value).build(), Storage.BlobWriteOption.metagenerationNotMatch()); case IF_MD5_MATCH: return Tuple.of( blobInfo.toBuilder().setMd5((String) value).build(), Storage.BlobWriteOption.md5Match()); case IF_CRC32C_MATCH: return Tuple.of( blobInfo.toBuilder().setCrc32c((String) value).build(), Storage.BlobWriteOption.crc32cMatch());
@Test public void testReadAndWriteChannelWithEncryptionKey() throws IOException { String blobName = "test-read-write-channel-with-customer-key-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); byte[] stringBytes; try (WriteChannel writer = storage.writer(blob, Storage.BlobWriteOption.encryptionKey(BASE64_KEY))) { stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8); writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT)); writer.write(ByteBuffer.wrap(stringBytes)); } ByteBuffer readBytes; ByteBuffer readStringBytes; try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.decryptionKey(KEY))) { readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length); readStringBytes = ByteBuffer.allocate(stringBytes.length); reader.read(readBytes); reader.read(readStringBytes); } assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array()); assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8)); assertTrue(storage.delete(BUCKET, blobName)); }
@Test public void testCreateBlobFromStreamWithEncryptionKey() throws IOException { ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT); BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder(); BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build(); EasyMock.expect( storageRpcMock.create(infoWithoutHashes.toPb(), fileStream, ENCRYPTION_KEY_OPTIONS)) .andReturn(BLOB_INFO1.toPb()) .times(2); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.create(infoWithHashes, fileStream, BlobWriteOption.encryptionKey(BASE64_KEY)); assertEquals(expectedBlob1, blob); blob = storage.create(infoWithHashes, fileStream, BlobWriteOption.encryptionKey(BASE64_KEY)); assertEquals(expectedBlob1, blob); }
@Test public void testWriterWithEncryptionKey() { BlobInfo info = BLOB_INFO1.toBuilder().setMd5(null).setCrc32c(null).build(); EasyMock.expect(storageRpcMock.open(info.toPb(), ENCRYPTION_KEY_OPTIONS)) .andReturn("upload-id") .times(2); EasyMock.replay(storageRpcMock); initializeService(); WriteChannel channel = storage.writer(info, BlobWriteOption.encryptionKey(KEY)); assertNotNull(channel); assertTrue(channel.isOpen()); channel = storage.writer(info, BlobWriteOption.encryptionKey(BASE64_KEY)); assertNotNull(channel); assertTrue(channel.isOpen()); }
@Test public void testCreateBlobMd5Fail() { String blobName = "test-create-blob-md5-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName) .setContentType(CONTENT_TYPE) .setMd5("O1R4G1HJSDUISJjoIYmVhQ==") .build(); ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8)); try { storage.create(blob, stream, Storage.BlobWriteOption.md5Match()); fail("StorageException was expected"); } catch (StorageException ex) { // expected } }
@Test public void testCreateFromStreamWithEncryptionKey() throws Exception { initializeExpectedBucket(5); BlobInfo info = BlobInfo.newBuilder(BlobId.of("b", "n")).setContentType(CONTENT_TYPE).build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; InputStream streamContent = new ByteArrayInputStream(content); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, streamContent, Storage.BlobWriteOption.encryptionKey(KEY))) .andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", streamContent, CONTENT_TYPE, Bucket.BlobWriteOption.encryptionKey(KEY)); assertEquals(expectedBlob, blob); }
@Test public void testWriterWithKmsKeyName() { BlobInfo info = BLOB_INFO1.toBuilder().setMd5(null).setCrc32c(null).build(); EasyMock.expect(storageRpcMock.open(info.toPb(), KMS_KEY_NAME_OPTIONS)) .andReturn("upload-id") .times(2); EasyMock.replay(storageRpcMock); initializeService(); WriteChannel channel = storage.writer(info, BlobWriteOption.kmsKeyName(KMS_KEY_NAME)); assertNotNull(channel); assertTrue(channel.isOpen()); channel = storage.writer(info, BlobWriteOption.kmsKeyName(KMS_KEY_NAME)); assertNotNull(channel); assertTrue(channel.isOpen()); }
@Test public void testWriterWithEncryptionKey() throws Exception { initializeExpectedBlob(2); BlobWriteChannel channel = createMock(BlobWriteChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.encryptionKey(BASE64_KEY)))) .andReturn(channel) .times(2); replay(storage); initializeBlob(); assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(BASE64_KEY))); assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(KEY))); }
@Test public void testWriteChannelFail() throws IOException { String blobName = "test-write-channel-blob-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build(); try { try (WriteChannel writer = storage.writer(blob, Storage.BlobWriteOption.generationMatch())) { writer.write(ByteBuffer.allocate(42)); } fail("StorageException was expected"); } catch (StorageException ex) { // expected } }
@Test public void testWriterWithKmsKeyName() throws Exception { initializeExpectedBlob(2); BlobWriteChannel channel = createMock(BlobWriteChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.kmsKeyName(KMS_KEY_NAME)))) .andReturn(channel); replay(storage); initializeBlob(); assertSame(channel, blob.writer(BlobWriteOption.kmsKeyName(KMS_KEY_NAME))); }