@Test
  public void testBlobEmptyHeaderSigningTest()
      throws URISyntaxException, StorageException, IOException {
    final String pageBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testPageBlob");
    final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(pageBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context
        .getSendingRequestEventHandler()
        .addListener(
            new StorageEvent<SendingRequestEvent>() {

              @Override
              public void eventOccurred(SendingRequestEvent eventArg) {
                HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
                connection.setRequestProperty("x-ms-foo", "");
              }
            });

    pageBlobRef.upload(srcStream, length, null, null, context);
    pageBlobRef.download(new ByteArrayOutputStream(), null, null, context);
  }
  @Test
  public void testBlobUploadWithoutMD5Validation()
      throws URISyntaxException, StorageException, IOException {
    final String pageBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testPageBlob");
    final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(pageBlobName);

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

    pageBlobRef.upload(srcStream, length, null, options, null);
    pageBlobRef.downloadAttributes();
    pageBlobRef.getProperties().setContentMD5("MDAwMDAwMDA=");
    pageBlobRef.uploadProperties(null, options, null);

    try {
      pageBlobRef.download(new ByteArrayOutputStream(), null, options, null);
      fail();
    } catch (StorageException ex) {
      assertEquals(306, ex.getHttpStatusCode());
      assertEquals("InvalidMd5", ex.getErrorCode());
    }

    options.setDisableContentMD5Validation(true);
    pageBlobRef.download(new ByteArrayOutputStream(), null, options, null);
  }
  @Test
  public void testPageBlobUploadFromStreamTest()
      throws URISyntaxException, StorageException, IOException {
    final String pageBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testPageBlob");
    final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(pageBlobName);

    int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);
    pageBlobRef.upload(srcStream, length);
    ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
    pageBlobRef.download(dstStream);
    BlobTestHelper.assertStreamsAreEqual(
        srcStream, new ByteArrayInputStream(dstStream.toByteArray()));

    length = 5 * 1024 * 1024;
    srcStream = BlobTestHelper.getRandomDataStream(length);
    pageBlobRef.upload(srcStream, length);
    dstStream = new ByteArrayOutputStream();
    pageBlobRef.download(dstStream);
    BlobTestHelper.assertStreamsAreEqual(
        srcStream, new ByteArrayInputStream(dstStream.toByteArray()));
  }
  @Test
  public void testUploadDownloadBlobProperties()
      throws URISyntaxException, StorageException, IOException {
    final int length = 512;

    // do this to make sure the set MD5 can be compared without an exception being thrown
    BlobRequestOptions options = new BlobRequestOptions();
    options.setDisableContentMD5Validation(true);

    // with explicit upload/download of properties
    String pageBlobName1 = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    CloudPageBlob pageBlobRef1 = this.container.getPageBlobReference(pageBlobName1);

    pageBlobRef1.upload(BlobTestHelper.getRandomDataStream(length), length);

    BlobTestHelper.setBlobProperties(pageBlobRef1);
    BlobProperties props1 = pageBlobRef1.getProperties();
    pageBlobRef1.uploadProperties();

    pageBlobRef1.downloadAttributes(null, options, null);
    BlobProperties props2 = pageBlobRef1.getProperties();

    BlobTestHelper.assertAreEqual(props1, props2);

    // by uploading/downloading the blob
    pageBlobName1 = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    pageBlobRef1 = this.container.getPageBlobReference(pageBlobName1);

    BlobTestHelper.setBlobProperties(pageBlobRef1);
    props1 = pageBlobRef1.getProperties();

    pageBlobRef1.upload(BlobTestHelper.getRandomDataStream(length), length);

    pageBlobRef1.download(new ByteArrayOutputStream(), null, options, null);
    props2 = pageBlobRef1.getProperties();

    BlobTestHelper.assertAreEqual(props1, props2);
  }
  /**
   * Create a blob and try to download a range of its contents
   *
   * @throws StorageException
   * @throws URISyntaxException
   * @throws IOException
   * @throws InterruptedException
   */
  @Test
  public void testPageBlobDownloadRangeValidationTest()
      throws StorageException, URISyntaxException, IOException {
    final int length = 5 * 1024 * 1024;

    final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(blockBlobName);

    pageBlobRef.upload(BlobTestHelper.getRandomDataStream(length), length);

    // Download full blob
    pageBlobRef.download(new ByteArrayOutputStream());
    assertEquals(length, pageBlobRef.getProperties().getLength());

    // Download blob range.
    byte[] downloadBuffer = new byte[100];
    int downloadLength = pageBlobRef.downloadRangeToByteArray(0, (long) 100, downloadBuffer, 0);
    assertEquals(length, pageBlobRef.getProperties().getLength());
    assertEquals(100, downloadLength);
  }
  @Test
  public void testCloudPageBlobUploadFromStreamWithAccessCondition()
      throws URISyntaxException, StorageException, IOException {
    CloudPageBlob blob1 = this.container.getPageBlobReference("blob1");
    AccessCondition accessCondition = AccessCondition.generateIfNoneMatchCondition("\"*\"");
    final int length = 6 * 512;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);
    blob1.upload(srcStream, length, accessCondition, null, null);

    srcStream.reset();
    blob1.create(1024);
    accessCondition = AccessCondition.generateIfNoneMatchCondition(blob1.getProperties().getEtag());
    try {
      blob1.upload(srcStream, length, accessCondition, null, null);
    } catch (StorageException ex) {
      assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, ex.getHttpStatusCode());
    }

    srcStream.reset();
    accessCondition = AccessCondition.generateIfMatchCondition(blob1.getProperties().getEtag());
    blob1.upload(srcStream, length, accessCondition, null, null);

    srcStream.reset();
    CloudPageBlob blob2 = this.container.getPageBlobReference("blob2");
    blob2.create(1024);
    accessCondition = AccessCondition.generateIfMatchCondition(blob1.getProperties().getEtag());
    try {
      blob1.upload(srcStream, length, accessCondition, null, null);
    } catch (StorageException ex) {
      assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, ex.getHttpStatusCode());
    }

    srcStream.reset();
    accessCondition = AccessCondition.generateIfNoneMatchCondition(blob2.getProperties().getEtag());
    blob1.upload(srcStream, length, accessCondition, null, null);
  }