public void Test_That_Default_Encrpytion_Can_Be_Restored_And_That_Algorithms_Are_Applied_Correctly()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Test_That_Default_Encrpytion_Can_Be_Restored_And_That_Algorithms_Are_Applied_Correctly" };

            var streamWithEncryption = new List<byte>();
            var streamWithDefaultEncryption = new List<byte>();
            var streamWithOverriddenEncryption = new List<byte>();
            var streamWithRestoredDefaultEncryption = new List<byte>();

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            // Setup a mock stream
            var mockStream = new Mock<Stream>();
            mockStream.SetupAllProperties();
            mockStream.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
                .Callback(
                    (byte[] buffer, int offset, int count) =>
                        streamWithEncryption.AddRange(buffer));
            mockStream.SetupGet(stream => stream.CanWrite).Returns(true);

            // Act
            // Setup default encrpytion output
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithDefaultEncryption.AddRange(streamWithEncryption);

            // Change and apply encrpytion
            serializerEncryptor.OverrideEncryption(
                new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
                new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
                new DESCryptoServiceProvider());

            streamWithEncryption.Clear();
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithOverriddenEncryption.AddRange(streamWithEncryption);

            // Now change the encrpytion back
            serializerEncryptor.RestoreDefaultEncryption();
            streamWithEncryption.Clear();
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithRestoredDefaultEncryption.AddRange(streamWithEncryption);

            // Assert
            // Our stream with default encrpytion should equal our stream with restored encryption
            Assert.IsTrue(streamWithDefaultEncryption.SequenceEqual(streamWithRestoredDefaultEncryption));

            // Both default and restored encryption schemes should be differed from the overriden one.
            Assert.IsFalse(streamWithDefaultEncryption.SequenceEqual(streamWithOverriddenEncryption));
            Assert.IsFalse(streamWithRestoredDefaultEncryption.SequenceEqual(streamWithOverriddenEncryption));
        }
        public void Verify_That_Overriden_Encryption_Is_Applied_When_Key_And_Iv_Are_Overriden()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Verify_That_Overriden_Encryption_Is_Applied_When_Key_And_Iv_Are_Overriden" };

            var streamWithEncryption = new List<byte>();
            var streamWithDefaultEncryption = new List<byte>();
            var streamWithOverriddenEncryption = new List<byte>();

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            // Setup a mock stream
            var mockStream = new Mock<Stream>();
            mockStream.SetupAllProperties();
            mockStream.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
                .Callback(
                    (byte[] buffer, int offset, int count) =>
                        streamWithEncryption.AddRange(buffer));
            mockStream.SetupGet(stream => stream.CanWrite).Returns(true);

            // Act
            // Setup default encrpytion output
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithDefaultEncryption.AddRange(streamWithEncryption);
            streamWithEncryption.Clear();

            // Change and apply encrpytion
            serializerEncryptor.OverrideEncryption(
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2 },
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 });
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithOverriddenEncryption.AddRange(streamWithEncryption);

            // Assert
            Assert.IsFalse(streamWithDefaultEncryption.SequenceEqual(streamWithOverriddenEncryption));
        }
        public void Verify_That_Overriden_Encryption_Is_Applied_When_Algorithm_Is_Overridden_With_Constructor_Overload()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Verify_That_Overriden_Encryption_Is_Applied_When_Algorithm_Is_Overridden_With_Constructor_Overload" };

            var streamWithEncryption = new List<byte>();
            var streamWithDefaultEncryption = new List<byte>();
            var streamWithOverriddenEncryption = new List<byte>();

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            var defaultEncrpytionKey = serializerEncryptor.EncryptionKey;
            var defaultEncrpytionIv = serializerEncryptor.EncryptionIv;

            // Setup a mock stream
            var mockStream = new Mock<Stream>();
            mockStream.SetupAllProperties();
            mockStream.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
                .Callback(
                    (byte[] buffer, int offset, int count) =>
                        streamWithEncryption.AddRange(buffer));
            mockStream.SetupGet(stream => stream.CanWrite).Returns(true);

            // Act
            // Setup default encrpytion output
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithDefaultEncryption.AddRange(streamWithEncryption);
            streamWithEncryption.Clear();

            // Create a new serializer encryptor that applies a different algorithm
            var serializerOverriddenEncryptor = new DataContractSerializerEncrpytor(
                dataContractSerializer,
                new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
                new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 },
                new DESCryptoServiceProvider());

            serializerOverriddenEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            streamWithOverriddenEncryption.AddRange(streamWithEncryption);

            // Assert
            Assert.IsFalse(streamWithDefaultEncryption.SequenceEqual(streamWithOverriddenEncryption));
            Assert.IsFalse(serializerOverriddenEncryptor.EncryptionKey.SequenceEqual(defaultEncrpytionKey));
            Assert.IsFalse(serializerOverriddenEncryptor.EncryptionIv.SequenceEqual(defaultEncrpytionIv));
        }
        public void Verify_That_Identical_Encrpytion_Is_Applied_With_When_Reapplied_With_Default_Encryption()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Verify_That_Identical_Encrpytion_Is_Applied_With_When_Reapplied_With_Default_Encryption" };

            var streamWithEncryption = new List<byte>();
            var firstStreamWithEncryption = new List<byte>();
            var secondStreamWithEncryption = new List<byte>();

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            // Setup a mock stream
            var mockStream = new Mock<Stream>();
            mockStream.SetupAllProperties();
            mockStream.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
                .Callback(
                    (byte[] buffer, int offset, int count) =>
                        streamWithEncryption.AddRange(buffer));
            mockStream.SetupGet(stream => stream.CanWrite).Returns(true);

            // Act
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            firstStreamWithEncryption.AddRange(streamWithEncryption);

            streamWithEncryption.Clear();
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);
            secondStreamWithEncryption.AddRange(streamWithEncryption);

            // Assert
            Assert.IsTrue(firstStreamWithEncryption.SequenceEqual(secondStreamWithEncryption));
        }
        public void Verify_That_Output_Stream_Is_Written_To()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Verify_That_Output_Stream_Is_Written_To" };

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            // Setup a mock stream
            var mockStream = new Mock<Stream>();
            mockStream.SetupAllProperties();
            mockStream.Setup(stream => stream.Flush());
            mockStream.SetupGet(stream => stream.CanWrite).Returns(true);

            // Act
            serializerEncryptor.WriteObjectEncrypted(mockStream.Object, serializableObject);

            // Assert
            mockStream.Verify(stream => stream.Flush(), Times.Once);
            mockStream.Verify(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.AtLeastOnce);
        }
        public void Test_That_We_Can_Decrpyt_A_Stream_That_Was_Encrypted_With_Overridden_Encryption_Parameters()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Test_That_We_Can_Decrpyt_A_Stream_That_Was_Encrypted_With_Overridden_Encryption_Parameters" };

            var streamWithEncryption = new List<byte>();

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            // Setup a mock stream for writing
            var mockStreamForWriting = new Mock<Stream>();
            mockStreamForWriting.SetupAllProperties();
            mockStreamForWriting.SetupGet(stream => stream.CanWrite).Returns(true);
            mockStreamForWriting.Setup(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
                .Callback(
                    (byte[] buffer, int offset, int count) =>
                        streamWithEncryption.AddRange(buffer));

            serializerEncryptor.OverrideEncryption(
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2 },
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 });

            // Perform encryption to get encrypted byte array
            serializerEncryptor.WriteObjectEncrypted(mockStreamForWriting.Object, serializableObject);

            // New create a memory stream based on this, so we can pass it back into the decrpytor
            var streamForReading = new MemoryStream(streamWithEncryption.ToArray());

            // Act
            var decryptedObject = (SerializableClass)serializerEncryptor.ReadObjectEncrypted(streamForReading);

            // Assert
            Assert.AreEqual(serializableObject.Field, decryptedObject.Field);
        }
        public void Test_That_Exception_Is_Thrown_When_Null_newEncryptionKey_Is_Passed_To_Encrpytion_Override()
        {
            // Arrange
            var serializableObject = new SerializableClass { Field = "Field" };

            var dataContractSerializer = new DataContractSerializer(serializableObject.GetType());
            var serializerEncryptor = new DataContractSerializerEncrpytor(dataContractSerializer);

            serializerEncryptor.OverrideEncryption(
                null,
                new byte[] { 9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 });
        }
 public void Test_That_Exception_Is_Thrown_When_Null_encryptionKey_Is_Passed_To_Constructor()
 {
     //Act
     var serializerEncryptor = new DataContractSerializerEncrpytor(
         new DataContractSerializer(typeof(SerializableClass)),
         null,
         new byte[] { 1 },
         new AesCryptoServiceProvider());
 }
 public void Test_That_Exception_Is_Thrown_When_Null_DataContractSerializer_Is_Passed_To_Second_Constructor()
 {
     //Act
     var serializerEncryptor = new DataContractSerializerEncrpytor(
         null,
         new byte[] { 1 },
         new byte[] { 1 },
         new AesCryptoServiceProvider());
 }
 public void Test_That_Exception_Is_Thrown_When_Null_DataContractSerializer_Is_Passed_To_First_Constructor()
 {
     //Act
     var serializerEncryptor = new DataContractSerializerEncrpytor(null);
 }