33
33
import java .io .FileOutputStream ;
34
34
import java .io .IOException ;
35
35
import java .net .HttpURLConnection ;
36
+ import java .net .URI ;
36
37
import java .net .URISyntaxException ;
38
+ import java .net .URL ;
39
+ import java .security .InvalidKeyException ;
37
40
import java .security .MessageDigest ;
38
41
import java .security .NoSuchAlgorithmException ;
39
42
import java .util .Calendar ;
@@ -1404,4 +1407,122 @@ public void testAppendBlobAppendFromStreamWithLength() throws StorageException,
1404
1407
blob .downloadAttributes ();
1405
1408
assertEquals (15 * 1024 * 1024 , blob .getProperties ().getLength ());
1406
1409
}
1410
+
1411
+ @ Test
1412
+ public void testAppendBlockCPK () throws URISyntaxException , StorageException , IOException {
1413
+
1414
+ final int BLOB_SIZE = 128 * Constants .KB ;
1415
+
1416
+ // load CPK into options
1417
+ BlobRequestOptions options = new BlobRequestOptions ();
1418
+ options .setCustomerProvidedKey (BlobTestHelper .generateCPK ());
1419
+
1420
+ // get blob reference
1421
+ CloudAppendBlob blob = this .container .getAppendBlobReference (
1422
+ BlobTestHelper .generateRandomBlobNameWithPrefix ("testAppendBlob" ));
1423
+ // force https
1424
+ blob = new CloudAppendBlob (
1425
+ new URL (blob .getUri ().toString ().replace ("http" , "https" )).toURI (),
1426
+ container .getServiceClient ().getCredentials ());
1427
+
1428
+ OperationContext operationContext = new OperationContext ();
1429
+ blob .createOrReplace (null , options , operationContext );
1430
+
1431
+ // validate response
1432
+ assertTrue (operationContext .getRequestResults ().get (0 ).isRequestServiceEncrypted ());
1433
+ assertEquals (
1434
+ options .getCustomerProvidedKey ().getKeySHA256 (),
1435
+ operationContext .getRequestResults ().get (0 ).getEncryptionKeySHA256 ());
1436
+
1437
+ // generate random data
1438
+ byte [] buffer = BlobTestHelper .getRandomBuffer (BLOB_SIZE );
1439
+ ByteArrayInputStream stream = new ByteArrayInputStream (buffer );
1440
+
1441
+ // append blocks with CPK
1442
+ operationContext = new OperationContext ();
1443
+ blob .appendBlock (stream , BLOB_SIZE , null , options , operationContext );
1444
+ stream .close ();
1445
+
1446
+ // validate response
1447
+ assertTrue (operationContext .getRequestResults ().get (0 ).isRequestServiceEncrypted ());
1448
+ assertEquals (
1449
+ options .getCustomerProvidedKey ().getKeySHA256 (),
1450
+ operationContext .getRequestResults ().get (0 ).getEncryptionKeySHA256 ());
1451
+ }
1452
+
1453
+ @ Test
1454
+ public void testAppendBlockFromURLCPK () throws URISyntaxException , StorageException , IOException , InvalidKeyException {
1455
+ // CPK on source blobs for append block from URL is not yet supported
1456
+ // TODO uncomment the marked comments AND the work in the actual web request factories when the feature is ready
1457
+
1458
+ ////// SETUP
1459
+
1460
+ final int BLOB_SIZE = 128 * Constants .KB ;
1461
+
1462
+ // make key for this blob
1463
+ // NOT YET SUPPORTED // BlobCustomerProvidedKey srcBlobKey = BlobTestHelper.generateCPK();
1464
+
1465
+ // load CPK into srcOptions
1466
+ BlobRequestOptions srcOptions = new BlobRequestOptions ();
1467
+ // NOT YET SUPPORTED // srcOptions.setCustomerProvidedKey(srcBlobKey);
1468
+
1469
+ // get blob reference
1470
+ CloudBlockBlob temp = this .container .getBlockBlobReference (
1471
+ BlobTestHelper .generateRandomBlobNameWithPrefix ("testAppendBlob" ));
1472
+ // force https
1473
+ temp = new CloudBlockBlob (
1474
+ new URL (temp .getUri ().toString ().replace ("http" , "https" )).toURI (),
1475
+ container .getServiceClient ().getCredentials ());
1476
+
1477
+ // generate random data
1478
+ byte [] buffer = BlobTestHelper .getRandomBuffer (BLOB_SIZE );
1479
+ ByteArrayInputStream stream = new ByteArrayInputStream (buffer );
1480
+
1481
+ // upload blob with CPK
1482
+ temp .upload (stream , buffer .length , null , srcOptions , null );
1483
+
1484
+ // save blob URI with SAS authentication (important for later when this is a source URL)
1485
+ SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy ();
1486
+ policy .setPermissions (EnumSet .of (SharedAccessBlobPermissions .READ , SharedAccessBlobPermissions .WRITE ));
1487
+ Calendar cal = Calendar .getInstance ();
1488
+ cal .add (Calendar .MINUTE , 10 );
1489
+ policy .setSharedAccessExpiryTime (cal .getTime ());
1490
+ String sas = temp .generateSharedAccessSignature (policy , null );
1491
+ URI srcBlobURI = new URI (temp .getUri ().toString () + "?" + sas );
1492
+
1493
+ // get blob reference
1494
+ CloudAppendBlob blob = this .container .getAppendBlobReference (
1495
+ BlobTestHelper .generateRandomBlobNameWithPrefix ("testAppendBlob" ));
1496
+ // force https
1497
+ blob = new CloudAppendBlob (
1498
+ new URL (blob .getUri ().toString ().replace ("http" , "https" )).toURI (),
1499
+ container .getServiceClient ().getCredentials ());
1500
+
1501
+ ////// ACT
1502
+
1503
+ // load CPK into srcOptions
1504
+ BlobCustomerProvidedKey blobKey = BlobTestHelper .generateCPK ();
1505
+ BlobRequestOptions options = new BlobRequestOptions ();
1506
+ options .setCustomerProvidedKey (blobKey );
1507
+
1508
+ // create append blob
1509
+ OperationContext operationContext = new OperationContext ();
1510
+ blob .createOrReplace (null , options , operationContext );
1511
+
1512
+ // validate response
1513
+ assertTrue (operationContext .getRequestResults ().get (0 ).isRequestServiceEncrypted ());
1514
+ assertEquals (
1515
+ options .getCustomerProvidedKey ().getKeySHA256 (),
1516
+ operationContext .getRequestResults ().get (0 ).getEncryptionKeySHA256 ());
1517
+
1518
+ // append blocks from URI
1519
+ operationContext = new OperationContext ();
1520
+ blob .appendBlockFromURI (srcBlobURI , null , null , null , null , null , options , operationContext );
1521
+
1522
+ // validate response
1523
+ assertTrue (operationContext .getRequestResults ().get (0 ).isRequestServiceEncrypted ());
1524
+ assertEquals (
1525
+ options .getCustomerProvidedKey ().getKeySHA256 (),
1526
+ operationContext .getRequestResults ().get (0 ).getEncryptionKeySHA256 ());
1527
+ }
1407
1528
}
0 commit comments