Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #87 from WindowsAzure/dev

Dev
  • Loading branch information...
commit 811ff73250001b43e6a02354116002c7a3741d5a 2 parents 4ed9259 + 9a0d6fb
Albert Cheng authored
Showing with 250 additions and 206 deletions.
  1. +0 −6 microsoft-azure-api/pom.xml
  2. +0 −1  microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetInfo.java
  3. +0 −1  microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/JobInfo.java
  4. +0 −1  microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/MediaProcessorInfo.java
  5. +0 −2  microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/AccessPolicyIntegrationTest.java
  6. +54 −0 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/AssetIntegrationTest.java
  7. +18 −68 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/EncryptionHelper.java
  8. +151 −107 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/EncryptionIntegrationTest.java
  9. +7 −5 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/IntegrationTestBase.java
  10. +19 −9 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/JobIntegrationTest.java
  11. +0 −5 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/TaskIntegrationTest.java
  12. +1 −1  microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceScenarioTest.java
  13. BIN  microsoft-azure-api/src/test/resources/media/SmallWMV.wmv
View
6 microsoft-azure-api/pom.xml
@@ -104,12 +104,6 @@
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
- <dependency>
- <groupId>org.bouncycastle</groupId>
- <artifactId>bcprov-jdk16</artifactId>
- <version>1.46</version>
- <scope>test</scope>
- </dependency>
</dependencies>
<build>
View
1  microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/AssetInfo.java
@@ -135,7 +135,6 @@ public LinkInfo getLocatorsLink() {
* @return the link
*/
public LinkInfo getParentAssetsLink() {
- // TODO: NEEDS TESTS once we figure out how to create assets with parents
return getRelationLink("ParentAssets");
}
}
View
1  microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/JobInfo.java
@@ -21,7 +21,6 @@
import com.microsoft.windowsazure.services.media.implementation.atom.EntryType;
import com.microsoft.windowsazure.services.media.implementation.content.JobType;
-// TODO: Auto-generated Javadoc
/**
* The Class JobInfo.
*/
View
1  microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/media/models/MediaProcessorInfo.java
@@ -19,7 +19,6 @@
import com.microsoft.windowsazure.services.media.implementation.atom.EntryType;
import com.microsoft.windowsazure.services.media.implementation.content.MediaProcessorType;
-// TODO: Auto-generated Javadoc
/**
* Data about a Media Processor entity.
*
View
2  ...osoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/AccessPolicyIntegrationTest.java
@@ -82,8 +82,6 @@ public void canCreateAccessPolicyWithReadPermissions() throws Exception {
verifyPolicyProperties("policy", testName, duration, AccessPolicyPermission.READ, policy);
}
- // TODO: Null name or duration?
-
@Test
public void canGetSinglePolicyById() throws Exception {
String expectedName = testPolicyPrefix + "GetOne";
View
54 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/AssetIntegrationTest.java
@@ -17,23 +17,38 @@
import static org.junit.Assert.*;
+import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
+import java.util.EnumSet;
import java.util.List;
import java.util.UUID;
import org.junit.Test;
import com.microsoft.windowsazure.services.core.ServiceException;
+import com.microsoft.windowsazure.services.media.models.AccessPolicy;
+import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;
+import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission;
import com.microsoft.windowsazure.services.media.models.Asset;
+import com.microsoft.windowsazure.services.media.models.AssetFile;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.AssetOption;
import com.microsoft.windowsazure.services.media.models.AssetState;
import com.microsoft.windowsazure.services.media.models.ContentKey;
import com.microsoft.windowsazure.services.media.models.ContentKeyInfo;
import com.microsoft.windowsazure.services.media.models.ContentKeyType;
+import com.microsoft.windowsazure.services.media.models.Job;
+import com.microsoft.windowsazure.services.media.models.JobInfo;
+import com.microsoft.windowsazure.services.media.models.LinkInfo;
+import com.microsoft.windowsazure.services.media.models.ListResult;
+import com.microsoft.windowsazure.services.media.models.Locator;
+import com.microsoft.windowsazure.services.media.models.LocatorInfo;
+import com.microsoft.windowsazure.services.media.models.LocatorType;
+import com.microsoft.windowsazure.services.media.models.Task;
+import com.microsoft.windowsazure.services.media.models.Task.CreateBatchOperation;
public class AssetIntegrationTest extends IntegrationTestBase {
@@ -301,4 +316,43 @@ public void linkAssetContentKeyInvalidIdFailed() throws ServiceException, URISyn
// Assert
}
+
+ @Test
+ public void canGetParentBackFromAsset() throws ServiceException, InterruptedException {
+ // Arrange
+ String originalAssetName = testAssetPrefix + "canGetParentBackFromAsset";
+ AssetInfo originalAsset = service.create(Asset.create().setName(originalAssetName));
+
+ int durationInMinutes = 10;
+ AccessPolicyInfo accessPolicyInfo = service.create(AccessPolicy.create(testPolicyPrefix
+ + "uploadAesPortectedAssetSuccess", durationInMinutes, EnumSet.of(AccessPolicyPermission.WRITE)));
+
+ LocatorInfo locatorInfo = service.create(Locator.create(accessPolicyInfo.getId(), originalAsset.getId(),
+ LocatorType.SAS));
+ WritableBlobContainerContract blobWriter = service.createBlobWriter(locatorInfo);
+
+ InputStream mpeg4H264InputStream = getClass().getResourceAsStream("/media/MPEG4-H264.mp4");
+ blobWriter.createBlockBlob("MPEG4-H264.mp4", mpeg4H264InputStream);
+ service.action(AssetFile.createFileInfos(originalAsset.getId()));
+
+ String jobName = testJobPrefix + "createJobSuccess";
+ CreateBatchOperation taskCreator = Task
+ .create("nb:mpid:UUID:2f381738-c504-4e4a-a38e-d199e207fcd5",
+ "<taskBody>" + "<inputAsset>JobInputAsset(0)</inputAsset>"
+ + "<outputAsset>JobOutputAsset(0)</outputAsset>" + "</taskBody>")
+ .setConfiguration("H.264 256k DSL CBR").setName("My encoding Task");
+ JobInfo jobInfo = service.create(Job.create().setName(jobName).addInputMediaAsset(originalAsset.getId())
+ .addTaskCreator(taskCreator));
+
+ // Act
+ ListResult<AssetInfo> outputAssets = service.list(Asset.list(jobInfo.getOutputAssetsLink()));
+ assertEquals(1, outputAssets.size());
+ AssetInfo childAsset = outputAssets.get(0);
+
+ LinkInfo parentAssetLink = childAsset.getParentAssetsLink();
+ AssetInfo parentAsset = service.get(Asset.get(parentAssetLink));
+
+ // Assert
+ assertEquals(originalAsset.getId(), parentAsset.getId());
+ }
}
View
86 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/EncryptionHelper.java
@@ -16,100 +16,50 @@
package com.microsoft.windowsazure.services.media;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
import java.io.InputStream;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
import java.security.Key;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
import java.security.SecureRandom;
-import java.security.cert.CertificateException;
+import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
-import java.security.cert.X509Certificate;
-import java.util.Random;
-import java.util.UUID;
-import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.microsoft.windowsazure.services.core.storage.utils.Base64;
-public class EncryptionHelper {
-
- public static byte[] createRandomVector(int numberOfBits) {
- int numberOfBytes = numberOfBits / 8;
- byte[] randomVector = new byte[numberOfBytes];
- Random random = new Random();
- random.nextBytes(randomVector);
- return randomVector;
- }
-
- public static byte[] EncryptSymmetricKey(String protectionKey, byte[] inputData) throws Exception {
-
- X509Certificate x509Certificate = createX509CertificateFromString(protectionKey);
- return EncryptSymmetricKey(x509Certificate.getPublicKey(), inputData);
- }
-
- private static X509Certificate createX509CertificateFromString(String protectionKey) throws CertificateException {
+class EncryptionHelper {
+ public static byte[] encryptSymmetricKey(String protectionKey, byte[] inputData) throws Exception {
+ Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
- ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Base64.decode(protectionKey));
- X509Certificate x509cert = (X509Certificate) certificateFactory.generateCertificate(byteArrayInputStream);
- return x509cert;
-
- }
-
- public static byte[] EncryptSymmetricKey(Key publicKey, byte[] inputData) throws NoSuchAlgorithmException,
- NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
- BadPaddingException {
- Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
+ byte[] protectionKeyBytes = Base64.decode(protectionKey);
+ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(protectionKeyBytes);
+ Certificate certificate = certificateFactory.generateCertificate(byteArrayInputStream);
+ Key publicKey = certificate.getPublicKey();
SecureRandom secureRandom = new SecureRandom();
cipher.init(Cipher.ENCRYPT_MODE, publicKey, secureRandom);
byte[] cipherText = cipher.doFinal(inputData);
return cipherText;
}
- public static String calculateChecksum(UUID uuid, byte[] aesKey) throws NoSuchAlgorithmException,
- NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException,
- InvalidKeyException {
+ public static String calculateContentKeyChecksum(String uuid, byte[] aesKey) throws Exception {
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey, "AES");
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING", "BC");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
- byte[] encryptionResult = cipher.doFinal(uuid.toString().getBytes());
+ byte[] encryptionResult = cipher.doFinal(uuid.getBytes("UTF8"));
byte[] checksumByteArray = new byte[8];
- for (int i = 0; i < 8; i++) {
- checksumByteArray[i] = encryptionResult[i];
- }
+ System.arraycopy(encryptionResult, 0, checksumByteArray, 0, 8);
String checksum = Base64.encode(checksumByteArray);
return checksum;
}
- public static byte[] EncryptFile(InputStream inputStream, byte[] aesKey, byte[] initializationVector)
- throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException,
- InvalidAlgorithmParameterException, IOException {
- // preparation
- SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
- IvParameterSpec ivParameterSpec = new IvParameterSpec(initializationVector);
- Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
-
- // encryption
- cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
+ public static InputStream encryptFile(InputStream inputStream, byte[] key, byte[] iv) throws Exception {
+ Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
+ SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
+ cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-
- int ch;
- while ((ch = cipherInputStream.read()) >= 0) {
- byteArrayOutputStream.write(ch);
- }
-
- byte[] cipherText = byteArrayOutputStream.toByteArray();
- return cipherText;
+ return cipherInputStream;
}
-
}
View
258 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/EncryptionIntegrationTest.java
@@ -17,11 +17,13 @@
import static org.junit.Assert.*;
-import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.io.InputStream;
-import java.security.Security;
+import java.math.BigInteger;
+import java.net.URL;
import java.util.EnumSet;
import java.util.List;
+import java.util.Random;
import java.util.UUID;
import org.junit.Test;
@@ -55,141 +57,183 @@
import com.microsoft.windowsazure.services.media.models.TaskState;
public class EncryptionIntegrationTest extends IntegrationTestBase {
-
private final String strorageDecryptionProcessor = "Storage Decryption";
- private String createContentKeyId(UUID uuid) {
- String randomContentKey = String.format("nb:kid:UUID:%s", uuid);
- return randomContentKey;
- }
+ @Test
+ public void uploadAesProtectedAssetAndDownloadSuccess() throws Exception {
+ // Arrange
- private String getProtectionKeyId() throws ServiceException {
- String protectionKeyId = (String) service.action(ProtectionKey
- .getProtectionKeyId(ContentKeyType.StorageEncryption));
- return protectionKeyId;
- }
+ // Media Services requires 256-bit (32-byte) keys and
+ // 128-bit (16-byte) initialization vectors (IV) for AES encryption,
+ // and also requires that only the first 8 bytes of the IV is filled.
+ Random random = new Random();
+ byte[] aesKey = new byte[32];
+ random.nextBytes(aesKey);
+ byte[] effectiveIv = new byte[8];
+ random.nextBytes(effectiveIv);
+ byte[] iv = new byte[16];
+ System.arraycopy(effectiveIv, 0, iv, 0, effectiveIv.length);
+
+ InputStream mpeg4H264InputStream = getClass().getResourceAsStream("/media/MPEG4-H264.mp4");
+ InputStream encryptedContent = EncryptionHelper.encryptFile(mpeg4H264InputStream, aesKey, iv);
+ int durationInMinutes = 10;
- private JobInfo decodeAsset(String name, AssetInfo assetInfo) throws ServiceException, InterruptedException {
- MediaProcessorInfo mediaProcessorInfo = GetMediaProcessor(strorageDecryptionProcessor);
- JobInfo jobInfo = createJob(name, assetInfo, mediaProcessorInfo);
- return waitJobForCompletion(jobInfo);
+ // Act
+ AssetInfo assetInfo = service.create(Asset.create().setName(testAssetPrefix + "uploadAesProtectedAssetSuccess")
+ .setOptions(AssetOption.StorageEncrypted));
+ WritableBlobContainerContract blobWriter = getBlobWriter(assetInfo.getId(), durationInMinutes);
+
+ // gets the public key for storage encryption.
+ String contentKeyId = makeContentKeyId(aesKey);
+
+ // link the content key with the asset.
+ service.action(Asset.linkContentKey(assetInfo.getId(), contentKeyId));
+
+ // upload the encrypted file to the server.
+ uploadEncryptedAssetFile(assetInfo, blobWriter, "MPEG4-H264.mp4", encryptedContent, contentKeyId, iv);
+
+ // submit and execute the decoding job.
+ JobInfo jobInfo = decodeAsset("uploadAesProtectedAssetSuccess", assetInfo.getId());
+ // assert
+ LinkInfo taskLinkInfo = jobInfo.getTasksLink();
+ List<TaskInfo> taskInfos = service.list(Task.list(taskLinkInfo));
+ for (TaskInfo taskInfo : taskInfos) {
+ assertEquals(TaskState.Completed, taskInfo.getState());
+ ListResult<AssetInfo> outputs = service.list(Asset.list(taskInfo.getOutputAssetsLink()));
+ assertEquals(1, outputs.size());
+ }
+ assertEquals(JobState.Finished, jobInfo.getState());
+
+ // Verify that the contents match
+ InputStream expected = getClass().getResourceAsStream("/media/MPEG4-H264.mp4");
+
+ ListResult<AssetInfo> outputAssets = service.list(Asset.list(jobInfo.getOutputAssetsLink()));
+ assertEquals(1, outputAssets.size());
+ AssetInfo outputAsset = outputAssets.get(0);
+ ListResult<AssetFileInfo> assetFiles = service.list(AssetFile.list(assetInfo.getAssetFilesLink()));
+ assertEquals(1, assetFiles.size());
+ AssetFileInfo outputFile = assetFiles.get(0);
+
+ InputStream actual = getFileContents(outputAsset.getId(), outputFile.getName(), durationInMinutes);
+ assertStreamsEqual(expected, actual);
}
- private JobInfo createJob(String name, AssetInfo assetInfo, MediaProcessorInfo mediaProcessorInfo)
- throws ServiceException {
- // String configuration = "H.264 256k DSL CBR";
+ private JobInfo decodeAsset(String name, String inputAssetId) throws ServiceException, InterruptedException {
+ MediaProcessorInfo mediaProcessorInfo = service.list(
+ MediaProcessor.list().set("$filter", "Name eq '" + strorageDecryptionProcessor + "'")).get(0);
+
String taskBody = "<taskBody>"
+ "<inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetCreationOptions=\"0\" assetName=\"Output\">JobOutputAsset(0)</outputAsset></taskBody>";
- JobInfo jobInfo = service.create(Job.create().addInputMediaAsset(assetInfo.getId())
+ JobInfo jobInfo = service.create(Job.create().addInputMediaAsset(inputAssetId)
.addTaskCreator(Task.create(mediaProcessorInfo.getId(), taskBody).setName(name)));
- return jobInfo;
- }
- private JobInfo waitJobForCompletion(JobInfo jobInfo) throws ServiceException, InterruptedException {
JobInfo currentJobInfo = jobInfo;
- while (currentJobInfo.getState().getCode() < 3) {
+ int retryCounter = 0;
+ while (currentJobInfo.getState().getCode() < 3 && retryCounter < 10) {
+ Thread.sleep(10000);
currentJobInfo = service.get(Job.get(jobInfo.getId()));
- Thread.sleep(4000);
+ retryCounter++;
}
return currentJobInfo;
}
- private MediaProcessorInfo GetMediaProcessor(String mediaProcessorName) throws ServiceException {
- List<MediaProcessorInfo> mediaProcessorInfos = service.list(MediaProcessor.list());
- for (MediaProcessorInfo mediaProcessorInfo : mediaProcessorInfos) {
- if (mediaProcessorInfo.getName().equals(mediaProcessorName)) {
- return mediaProcessorInfo;
- }
- }
- return null;
- }
-
- private String getProtectionKey(String protectionKeyId) throws ServiceException {
+ private String makeContentKeyId(byte[] aesKey) throws ServiceException, Exception {
+ String protectionKeyId = (String) service.action(ProtectionKey
+ .getProtectionKeyId(ContentKeyType.StorageEncryption));
String protectionKey = (String) service.action(ProtectionKey.getProtectionKey(protectionKeyId));
- return protectionKey;
- }
- private AssetFileInfo uploadEncryptedAssetFile(AssetInfo assetInfo, LocatorInfo locatorInfo,
- ContentKeyInfo contentKeyInfo, String blobName, byte[] encryptedContent) throws ServiceException {
- WritableBlobContainerContract blobWriter = service.createBlobWriter(locatorInfo);
- InputStream blobContent = new ByteArrayInputStream(encryptedContent);
- blobWriter.createBlockBlob(blobName, blobContent);
- AssetFileInfo assetFileInfo = service.create(AssetFile.create(assetInfo.getId(), blobName).setIsPrimary(true)
- .setIsEncrypted(true).setContentFileSize(new Long(encryptedContent.length))
- .setEncryptionScheme("StorageEncryption").setEncryptionVersion("1.0")
- .setEncryptionKeyId(contentKeyInfo.getId()));
- return assetFileInfo;
- }
+ String contentKeyIdUuid = UUID.randomUUID().toString();
+ String contentKeyId = String.format("nb:kid:UUID:%s", contentKeyIdUuid);
- private ContentKeyInfo createContentKey(byte[] aesKey, ContentKeyType contentKeyType, String protectionKeyId,
- String protectionKey) throws Exception {
- UUID contentKeyIdUuid = UUID.randomUUID();
- String contentKeyId = createContentKeyId(contentKeyIdUuid);
- byte[] encryptedContentKey = EncryptionHelper.EncryptSymmetricKey(protectionKey, aesKey);
+ byte[] encryptedContentKey = EncryptionHelper.encryptSymmetricKey(protectionKey, aesKey);
String encryptedContentKeyString = Base64.encode(encryptedContentKey);
- String checksum = EncryptionHelper.calculateChecksum(contentKeyIdUuid, aesKey);
+ String checksum = EncryptionHelper.calculateContentKeyChecksum(contentKeyIdUuid, aesKey);
+
ContentKeyInfo contentKeyInfo = service.create(ContentKey
- .create(contentKeyId, contentKeyType, encryptedContentKeyString).setChecksum(checksum)
- .setProtectionKeyId(protectionKeyId));
- return contentKeyInfo;
+ .create(contentKeyId, ContentKeyType.StorageEncryption, encryptedContentKeyString)
+ .setChecksum(checksum).setProtectionKeyId(protectionKeyId));
+ return contentKeyInfo.getId();
}
- @Test
- public void uploadAesProtectedAssetAndDownloadSuccess() throws Exception {
- // Arrange
- Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
- InputStream smallWMVInputStream = getClass().getResourceAsStream("/media/SmallWMV.wmv");
- byte[] aesKey = EncryptionHelper.createRandomVector(256);
- byte[] initializationVector = EncryptionHelper.createRandomVector(128);
- int durationInMinutes = 10;
-
- // Act
-
- // creates asset
- AssetInfo assetInfo = service.create(Asset.create().setName("uploadAesProtectedAssetSuccess")
- .setOptions(AssetOption.StorageEncrypted));
+ private void uploadEncryptedAssetFile(AssetInfo asset, WritableBlobContainerContract blobWriter, String blobName,
+ InputStream blobContent, String encryptionKeyId, byte[] iv) throws ServiceException {
+ blobWriter.createBlockBlob(blobName, blobContent);
+ service.action(AssetFile.createFileInfos(asset.getId()));
+ ListResult<AssetFileInfo> files = service.list(AssetFile.list(asset.getAssetFilesLink()).set("$filter",
+ "Name eq '" + blobName + "'"));
+ assertEquals(1, files.size());
+ AssetFileInfo file = files.get(0);
+ byte[] sub = new byte[9];
+ // Offset bytes to ensure that the sign-bit is not set.
+ // Media Services expects unsigned Int64 values.
+ System.arraycopy(iv, 0, sub, 1, 8);
+ BigInteger longIv = new BigInteger(sub);
+ String initializationVector = longIv.toString();
+
+ service.update(AssetFile.update(file.getId()).setIsEncrypted(true).setEncryptionKeyId(encryptionKeyId)
+ .setEncryptionScheme("StorageEncryption").setEncryptionVersion("1.0")
+ .setInitializationVector(initializationVector));
+ }
- // creates writable access policy
- AccessPolicyInfo accessPolicyInfo = service.create(AccessPolicy.create("uploadAesPortectedAssetSuccess",
- durationInMinutes, EnumSet.of(AccessPolicyPermission.WRITE)));
+ private WritableBlobContainerContract getBlobWriter(String assetId, int durationInMinutes) throws ServiceException {
+ AccessPolicyInfo accessPolicyInfo = service.create(AccessPolicy.create(testPolicyPrefix
+ + "uploadAesPortectedAssetSuccess", durationInMinutes, EnumSet.of(AccessPolicyPermission.WRITE)));
// creates locator for the input media asset
- LocatorInfo locatorInfo = service.create(Locator.create(accessPolicyInfo.getId(), assetInfo.getId(),
- LocatorType.SAS));
-
- // gets the public key for storage encryption.
-
- String protectionKeyId = getProtectionKeyId();
- String protectionKey = getProtectionKey(protectionKeyId);
-
- // creates the content key with encrypted
- ContentKeyInfo contentKeyInfo = createContentKey(aesKey, ContentKeyType.StorageEncryption, protectionKeyId,
- protectionKey);
-
- // link the content key with the asset.
- service.action(Asset.linkContentKey(assetInfo.getId(), contentKeyInfo.getId()));
-
- // encrypt the file.
- byte[] encryptedContent = EncryptionHelper.EncryptFile(smallWMVInputStream, aesKey, initializationVector);
-
- // upload the encrypted file to the server.
- AssetFileInfo assetFileInfo = uploadEncryptedAssetFile(assetInfo, locatorInfo, contentKeyInfo,
- "uploadAesProtectedAssetSuccess", encryptedContent);
-
- // submit and execute the decoding job.
- JobInfo jobInfo = decodeAsset("uploadAesProtectedAssetSuccess", assetInfo);
+ LocatorInfo locatorInfo = service.create(Locator.create(accessPolicyInfo.getId(), assetId, LocatorType.SAS));
+ WritableBlobContainerContract blobWriter = service.createBlobWriter(locatorInfo);
+ return blobWriter;
+ }
- // assert
- LinkInfo taskLinkInfo = jobInfo.getTasksLink();
- List<TaskInfo> taskInfos = service.list(Task.list(taskLinkInfo));
- for (TaskInfo taskInfo : taskInfos) {
- assertEquals(TaskState.Completed, taskInfo.getState());
- ListResult<AssetInfo> outputs = service.list(Asset.list(taskInfo.getOutputAssetsLink()));
- assertEquals(1, outputs.size());
+ private InputStream getFileContents(String assetId, String fileName, int availabilityWindowInMinutes)
+ throws ServiceException, InterruptedException, IOException {
+ AccessPolicyInfo readAP = service.create(AccessPolicy.create(testPolicyPrefix + "tempAccessPolicy",
+ availabilityWindowInMinutes, EnumSet.of(AccessPolicyPermission.READ)));
+ LocatorInfo readLocator = service.create(Locator.create(readAP.getId(), assetId, LocatorType.SAS));
+ URL file = new URL(readLocator.getBaseUri() + "/" + fileName + readLocator.getContentAccessToken());
+
+ // There can be a delay before a new read locator is applied for the asset files.
+ InputStream reader = null;
+ for (int counter = 0; true; counter++) {
+ try {
+ reader = file.openConnection().getInputStream();
+ break;
+ }
+ catch (IOException e) {
+ System.out.println("Got error, wait a bit and try again");
+ if (counter < 6) {
+ Thread.sleep(10000);
+ }
+ else {
+ // No more retries.
+ throw e;
+ }
+ }
}
- assertEquals(JobState.Finished, jobInfo.getState());
+ return reader;
}
+ private void assertStreamsEqual(InputStream inputStream1, InputStream inputStream2) throws IOException {
+ byte[] buffer1 = new byte[1024];
+ byte[] buffer2 = new byte[1024];
+ try {
+ while (true) {
+ int n1 = inputStream1.read(buffer1);
+ int n2 = inputStream2.read(buffer2);
+ assertEquals("number of bytes read from streams", n1, n2);
+ if (n1 == -1) {
+ break;
+ }
+ for (int i = 0; i < n1; i++) {
+ assertEquals("byte " + i + " read from streams", buffer1[i], buffer2[i]);
+ }
+ }
+ }
+ finally {
+ inputStream1.close();
+ inputStream2.close();
+ }
+ }
}
View
12 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/IntegrationTestBase.java
@@ -80,10 +80,7 @@ public static void cleanup() throws Exception {
cleanupEnvironment();
}
- private static void cleanupEnvironment() {
- // TODO: This should be removed once cascade delete is implemented for Assets.
- // But for now, trying to delete an asset with fail if there are any
- // existing Locators associated with it.
+ protected static void cleanupEnvironment() {
removeAllTestLocators();
removeAllTestAssets();
removeAllTestAccessPolicies();
@@ -96,7 +93,12 @@ private static void removeAllTestContentKeys() {
List<ContentKeyInfo> contentKeyInfos = service.list(ContentKey.list());
for (ContentKeyInfo contentKeyInfo : contentKeyInfos) {
- service.delete(ContentKey.delete(contentKeyInfo.getId()));
+ try {
+ service.delete(ContentKey.delete(contentKeyInfo.getId()));
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
}
}
catch (Exception e) {
View
28 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/JobIntegrationTest.java
@@ -31,6 +31,7 @@
import com.microsoft.windowsazure.services.media.models.Job;
import com.microsoft.windowsazure.services.media.models.JobInfo;
import com.microsoft.windowsazure.services.media.models.JobState;
+import com.microsoft.windowsazure.services.media.models.LinkInfo;
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.microsoft.windowsazure.services.media.models.Task;
import com.microsoft.windowsazure.services.media.models.Task.CreateBatchOperation;
@@ -40,16 +41,16 @@
private static AssetInfo assetInfo;
- private void verifyJobInfoEqual(String message, JobInfo expected, JobInfo actual) {
+ private void verifyJobInfoEqual(String message, JobInfo expected, JobInfo actual) throws ServiceException {
verifyJobProperties(message, expected.getName(), expected.getPriority(), expected.getRunningDuration(),
expected.getState(), expected.getTemplateId(), expected.getCreated(), expected.getLastModified(),
- expected.getStartTime(), expected.getEndTime(), actual);
+ expected.getStartTime(), expected.getEndTime(), null, actual);
}
@SuppressWarnings("deprecation")
private void verifyJobProperties(String message, String testName, Integer priority, double runningDuration,
JobState state, String templateId, Date created, Date lastModified, Date startTime, Date endTime,
- JobInfo actualJob) {
+ Integer expectedTaskCount, JobInfo actualJob) throws ServiceException {
assertNotNull(message, actualJob);
assertNotNull(message + "Id", actualJob.getId());
@@ -66,8 +67,11 @@ private void verifyJobProperties(String message, String testName, Integer priori
assertDateApproxEquals(message + " StartTime", startTime, actualJob.getStartTime());
assertDateApproxEquals(message + " EndTime", endTime, actualJob.getEndTime());
- // TODO: Add test for accessing the tasks when fixed:
- // https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/531
+ if (expectedTaskCount != null) {
+ LinkInfo tasksLink = actualJob.getTasksLink();
+ ListResult<TaskInfo> actualTasks = service.list(Task.list(tasksLink));
+ assertEquals(message + " tasks size", expectedTaskCount.intValue(), actualTasks.size());
+ }
}
private JobInfo createJob(String name) throws ServiceException {
@@ -108,7 +112,7 @@ public void createJobSuccess() throws ServiceException {
// Assert
verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
- endTime, actualJob);
+ endTime, 1, actualJob);
}
@Test
@@ -133,7 +137,7 @@ public void createJobTwoTasksSuccess() throws ServiceException {
// Assert
verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
- endTime, actualJob);
+ endTime, 2, actualJob);
}
@Test
@@ -155,7 +159,7 @@ public void getJobSuccess() throws ServiceException {
// Assert
verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
- endTime, actualJob);
+ endTime, 1, actualJob);
}
@Test
@@ -180,7 +184,12 @@ public void listJobSuccess() throws ServiceException {
verifyListResultContains("listJobs", expectedListJobsResult, actualListJobResult, new ComponentDelegate() {
@Override
public void verifyEquals(String message, Object expected, Object actual) {
- verifyJobInfoEqual(message, (JobInfo) expected, (JobInfo) actual);
+ try {
+ verifyJobInfoEqual(message, (JobInfo) expected, (JobInfo) actual);
+ }
+ catch (ServiceException e) {
+ fail(e.getMessage());
+ }
}
});
}
@@ -293,6 +302,7 @@ public void canGetTasksFromJob() throws Exception {
@Test
public void canGetErrorDetailsFromTask() throws Exception {
+ cleanupEnvironment();
String name = testJobPrefix + "canGetErrorDetailsFromTask";
JobInfo actualJob = service.create(Job.create().setName(name).addInputMediaAsset(assetInfo.getId())
View
5 microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/media/TaskIntegrationTest.java
@@ -189,8 +189,6 @@ public void cancelTaskSuccess() throws ServiceException, InterruptedException {
//Assert
List<TaskInfo> taskInfos = service.list(Task.list(cancellingJobInfo.getTasksLink()));
for (TaskInfo taskInfo : taskInfos) {
- // Replace with enums
- // https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/522
verifyTaskPropertiesNoEncryption("canceled task", mediaProcessorId, TaskOption.None, taskBody,
configuration, name, 0, new Date(), null, 0.0, 0.0, null, TaskState.Canceled, taskInfo);
}
@@ -225,9 +223,6 @@ private void verifyTaskProperties(String message, String mediaProcessorId, TaskO
assertNotNull(message + " getErrorDetails", actual.getErrorDetails());
assertEquals(message + " getErrorDetails.size", 0, actual.getErrorDetails().size());
- // TODO: Add tests for Historical events when added
- // https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/494
-
ListResult<AssetInfo> inputAssets = service.list(Asset.list(actual.getInputAssetsLink()));
ListResult<AssetInfo> outputAssets = service.list(Asset.list(actual.getOutputAssetsLink()));
View
2  ...soft-azure-api/src/test/java/com/microsoft/windowsazure/services/scenarios/MediaServiceScenarioTest.java
@@ -127,7 +127,7 @@ public void transformAsset() throws Exception {
private void waitForJobToFinish(JobInfo job) throws InterruptedException, ServiceException {
for (int counter = 0; !wrapper.isJobFinished(job); counter++) {
- if (counter > 10) {
+ if (counter > 30) {
fail("Took took long for the job to finish");
}
Thread.sleep(10000);
View
BIN  microsoft-azure-api/src/test/resources/media/SmallWMV.wmv
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.