Skip to content

Commit

Permalink
Merge pull request Azure#47 from jofriedm-msft/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jofriedm-msft committed Apr 28, 2017
2 parents 5d39c24 + 17c8f69 commit 05ac043
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -143,7 +144,41 @@ else if (type == BlobType.APPEND_BLOB) {
TestHelper.assertStreamsAreEqualAtIndex(stream, new ByteArrayInputStream(outputStream.toByteArray()), 0, 0,
size, 2 * 1024);
}


@Test
public void testDownloadUnencryptedBlobWithEncryptionPolicy() throws StorageException, IOException, URISyntaxException, NoSuchAlgorithmException
{
String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("test");
CloudBlockBlob blob = container.getBlockBlobReference(blobName);
blob.deleteIfExists();

byte[] msg = "my message".getBytes();
// Upload data without encryption
blob.uploadFromByteArray(msg, 0, msg.length);

// Create options with encryption policy
BlobRequestOptions options = new BlobRequestOptions();
options.setEncryptionPolicy(new BlobEncryptionPolicy(new RsaKey("myKey", 1024), null));
options.setRequireEncryption(true);

try {
blob.downloadText(Charset.defaultCharset().name(), null, options, null);
fail("Expect exception");
}
catch (StorageException e) {
assertEquals(SR.ENCRYPTION_DATA_NOT_PRESENT_ERROR, e.getMessage());
}

byte[] buffer = new byte[msg.length];
try {
blob.downloadRangeToByteArray(0, (long) buffer.length, buffer, 0, null, options, null);
fail("Expect exception");
}
catch (StorageException e) {
assertEquals(SR.ENCRYPTION_DATA_NOT_PRESENT_ERROR, e.getMessage());
}
}

@Test
public void testBlobEncryptionWithFile() throws URISyntaxException, StorageException, IOException,
InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import com.microsoft.azure.storage.file.SharedAccessFilePermissions;
import com.microsoft.azure.storage.file.SharedAccessFilePolicy;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -60,33 +58,11 @@
import java.util.Random;
import java.util.TimeZone;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.microsoft.azure.storage.AccessCondition;
import com.microsoft.azure.storage.Constants;
import com.microsoft.azure.storage.NameValidator;
import com.microsoft.azure.storage.OperationContext;
import com.microsoft.azure.storage.RetryNoRetry;
import com.microsoft.azure.storage.SendingRequestEvent;
import com.microsoft.azure.storage.StorageCredentialsAnonymous;
import com.microsoft.azure.storage.StorageCredentialsSharedAccessSignature;
import com.microsoft.azure.storage.StorageErrorCodeStrings;
import com.microsoft.azure.storage.StorageEvent;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.TestRunners.CloudTests;
import com.microsoft.azure.storage.TestRunners.DevFabricTests;
import com.microsoft.azure.storage.TestRunners.DevStoreTests;
import com.microsoft.azure.storage.TestRunners.SlowTests;
import com.microsoft.azure.storage.core.Utility;
import com.microsoft.azure.storage.file.CloudFile;
import com.microsoft.azure.storage.file.CloudFileShare;
import com.microsoft.azure.storage.file.FileProperties;
import com.microsoft.azure.storage.file.FileTestHelper;
import com.microsoft.azure.storage.file.SharedAccessFilePermissions;
import com.microsoft.azure.storage.file.SharedAccessFilePolicy;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -1197,6 +1173,29 @@ public void testBlobUploadWithoutMD5Validation() throws URISyntaxException, Stor
assertEquals("MDAwMDAwMDA=", blockBlobRef2.properties.getContentMD5());
}

@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testVerifyTransactionalMD5ValidationMissingOverallMD5() throws URISyntaxException, StorageException, IOException {
final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName);

final int length = 2 * 1024 * 1024;
ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);
BlobRequestOptions options = new BlobRequestOptions();
options.setSingleBlobPutThresholdInBytes(1024*1024);
options.setDisableContentMD5Validation(true);
options.setStoreBlobContentMD5(false);

blockBlobRef.upload(srcStream, -1, null, options, null);

options.setDisableContentMD5Validation(false);
options.setStoreBlobContentMD5(true);
options.setUseTransactionalContentMD5(true);
final CloudBlockBlob blockBlobRef2 = this.container.getBlockBlobReference(blockBlobName);
blockBlobRef2.downloadRange(1024, (long)1024, new ByteArrayOutputStream(), null, options, null);
assertNull(blockBlobRef2.getProperties().getContentMD5());
}

@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testBlockBlobUploadContentMD5() throws URISyntaxException, StorageException, IOException {
Expand Down Expand Up @@ -1747,6 +1746,23 @@ public void testBlobConditionalAccess() throws StorageException, IOException, UR
newETag = blob.getProperties().getEtag();
assertFalse("ETage should be modified on write metadata", newETag.equals(currentETag));
}

@Test
public void testBlobExceedMaxRange() throws URISyntaxException, StorageException, IOException
{
CloudBlockBlob blob = container.getBlockBlobReference("blockblob4");
blob.deleteIfExists();

byte[] msg = "my message".getBytes("UTF-8");
blob.uploadFromByteArray(msg, 0, msg.length);

byte[] buffer = new byte[msg.length + 5];

blob.downloadRangeToByteArray(0, (long) buffer.length, buffer, 0, null, null, null);
String expected = new String (msg, "UTF-8");
String actual = new String(buffer, "UTF-8").substring(0, 10);
assertEquals(expected, actual);
}

@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,8 @@ public void setKeyResolver(IKeyResolver keyResolver) {
OutputStream decryptBlob(OutputStream userProvidedStream, Map<String, String> metadata, Boolean requireEncryption,
byte[] iv, boolean noPadding) throws StorageException {
Utility.assertNotNull("metadata", metadata);

// If encryption policy is set but the encryption metadata is absent, throw
// an exception.
String encryptionDataString = metadata.get("encryptiondata");
if (requireEncryption != null && requireEncryption && encryptionDataString == null)
{
throw new StorageException(StorageErrorCodeStrings.DECRYPTION_ERROR, SR.ENCRYPTION_DATA_NOT_PRESENT_ERROR, null);
}


try
{
if (encryptionDataString != null) {
Expand Down Expand Up @@ -234,6 +227,14 @@ static OutputStream wrapUserStreamWithDecryptStream(CloudBlob blob, OutputStream
BlobRequestOptions options, Map<String, String> metadata, long blobLength, boolean rangeRead,
Long endOffset, Long userSpecifiedLength, int discardFirst, boolean bufferIV) throws StorageException
{
// If encryption policy is set but the encryption metadata is absent, throw
// an exception.
String encryptionDataString = metadata.get("encryptiondata");
if (options.requireEncryption() != null && options.requireEncryption() && encryptionDataString == null)
{
throw new StorageException(StorageErrorCodeStrings.DECRYPTION_ERROR, SR.ENCRYPTION_DATA_NOT_PRESENT_ERROR, null);
}

if (!rangeRead)
{
// The user provided stream should be wrapped in a TruncatingNonCloseableStream in order to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1354,12 +1354,6 @@ public Integer preProcessResponse(CloudBlob blob, CloudBlobClient client, Operat
final BlobAttributes retrievedAttributes = BlobResponse.getBlobAttributes(this.getConnection(),
blob.getStorageUri(), blob.snapshotID);

if (!options.getDisableContentMD5Validation() && options.getUseTransactionalContentMD5()
&& Utility.isNullOrEmpty(retrievedAttributes.getProperties().getContentMD5())) {
throw new StorageException(StorageErrorCodeStrings.MISSING_MD5_HEADER, SR.MISSING_MD5,
Constants.HeaderConstants.HTTP_UNUSED_306, null, null);
}

blob.properties = retrievedAttributes.getProperties();
blob.metadata = retrievedAttributes.getMetadata();

Expand Down

0 comments on commit 05ac043

Please sign in to comment.