public void EncryptContentKeyToCertRoundTripTest()
        {
            var cert = new X509Certificate2("UnitTest.pfx");

            var dataContextMock = new Mock <IMediaDataServiceContext>();

            string testKey      = "1234567890123456";
            var    fakeResponse = new string[] { Convert.ToBase64String(new System.Text.UTF8Encoding().GetBytes(testKey)) };

            dataContextMock.Setup((ctxt) => ctxt
                                  .Execute <string>(It.IsAny <Uri>()))
            .Returns(() =>
            {
                return(fakeResponse);
            });

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            var contentKey = new ContentKeyData {
                Name = "testData", Id = "id"
            };

            contentKey.SetMediaContext(_mediaContext);

            byte[] encryptedKeyValue = contentKey.GetEncryptedKeyValue(cert);

            byte[] encryptedContentKey = CommonEncryption.EncryptContentKeyToCertificate(cert, encryptedKeyValue);

            byte[] decryptedContentKey = EncryptionUtils.DecryptSymmetricKey(cert, encryptedContentKey);

            Assert.IsTrue(encryptedKeyValue.SequenceEqual(decryptedContentKey));
        }
        public void ContentKeyUpdateRetry()
        {
            var data = new ContentKeyData {
                Name = "testData"
            };
            var fakeException   = new WebException("test", WebExceptionStatus.ConnectionClosed);
            var dataContextMock = TestMediaServicesClassFactory.CreateSaveChangesMock(fakeException, 2, data);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Assets", data));
            dataContextMock.Setup((ctxt) => ctxt.UpdateObject(data));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            data.SetMediaContext(_mediaContext);

            data.Update();

            dataContextMock.Verify((ctxt) => ctxt.SaveChangesAsync(data), Times.Exactly(2));
        }
        public void ContentKeyGetClearKeyValueRetry()
        {
            var data = new ContentKeyData {
                Name = "testData"
            };

            var dataContextMock = new Mock <IMediaDataServiceContext>();

            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            string testKey        = "testKey";
            var    fakeResponse   = new string[] { Convert.ToBase64String(new System.Text.UTF8Encoding().GetBytes(testKey)) };
            int    exceptionCount = 2;

            dataContextMock.Setup((ctxt) => ctxt
                                  .Execute <string>(It.IsAny <Uri>()))
            .Returns(() =>
            {
                if (--exceptionCount > 0)
                {
                    throw fakeException;
                }
                return(fakeResponse);
            });

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);
            data.SetMediaContext(_mediaContext);

            var cert   = new X509Certificate2("UnitTest.pfx");
            var actual = data.GetClearKeyValue();

            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Length > 0);

            dataContextMock.Verify((ctxt) => ctxt.Execute <string>(It.IsAny <Uri>()), Times.Exactly(2));
        }
        public void TestAssetFileDeleteRetry()
        {
            var dataContextMock = new Mock <IMediaDataServiceContext>();

            int exceptionCount = 2;

            var contentKey = new ContentKeyData {
                Name = "testData"
            };
            var fakeResponse = new TestMediaDataServiceResponse {
                AsyncState = contentKey
            };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("ContentKeys", contentKey));
            dataContextMock.Setup((ctxt) => ctxt.DeleteObject(contentKey));

            dataContextMock.Setup((ctxt) => ctxt
                                  .SaveChangesAsync(contentKey))
            .Returns(() => Task.Factory.StartNew <IMediaDataServiceResponse>(() =>
            {
                if (--exceptionCount > 0)
                {
                    throw fakeException;
                }
                return(fakeResponse);
            }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            contentKey.SetMediaContext(_mediaContext);

            contentKey.Delete();

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestAssetFileDeleteRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var contentKey = new ContentKeyData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = contentKey };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("ContentKeys", contentKey));
            dataContextMock.Setup((ctxt) => ctxt.DeleteObject(contentKey));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(contentKey))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            contentKey.SetMediaContext(_mediaContext);

            contentKey.Delete();

            Assert.AreEqual(0, exceptionCount);
        }
        public void ContentKeyUpdateRetry()
        {
            var data = new ContentKeyData { Name = "testData" };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);
            var dataContextMock = TestMediaServicesClassFactory.CreateSaveChangesMock(fakeException, 2, data);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Assets", data));
            dataContextMock.Setup((ctxt) => ctxt.UpdateObject(data));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            data.SetMediaContext(_mediaContext);

            data.Update();

            dataContextMock.Verify((ctxt) => ctxt.SaveChangesAsync(data), Times.Exactly(2));
        }
        public void ContentKeyGetEncryptedKeyValueRetry()
        {
            var data = new ContentKeyData { Name = "testData" };

            var dataContextMock = new Mock<IMediaDataServiceContext>();

            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            string testKey = "testKey";
            var fakeResponse = new string[] { Convert.ToBase64String(new System.Text.UTF8Encoding().GetBytes(testKey)) };
            int exceptionCount = 2;

            dataContextMock.Setup((ctxt) => ctxt
                .Execute<string>(It.IsAny<Uri>()))
                .Returns(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                });

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);
            data.SetMediaContext(_mediaContext);

            var cert = new X509Certificate2("UnitTest.pfx");
            var actual = data.GetEncryptedKeyValue(cert);

            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Length > 0);

            dataContextMock.Verify((ctxt) => ctxt.Execute<string>(It.IsAny<Uri>()), Times.Exactly(2));
        }