public void Constructor_HashAlgorithmSet()
        {
            var hashAlgorithm = Mock.Of<IHashAlgorithm>();
            var target = new EncryptorImplementation(Mock.Of<IAESAlgorithm>(), hashAlgorithm, Mock.Of<IFile>());

            Assert.AreEqual(hashAlgorithm, target.HashAlgorithm);
        }
        public void Constructor_FileSystemSet()
        {
            var fileSystem = Mock.Of<IFile>();
            var target = new EncryptorImplementation(Mock.Of<IAESAlgorithm>(), Mock.Of<IHashAlgorithm>(), fileSystem);

            Assert.AreEqual(fileSystem, target.FileSystem);
        }
        public void DecryptTest()
        {
            byte[] bytesToDecrypt = new byte[] { 1, 2, 3, 4, 5, 6 };
            byte[] expectedDecryptedBytes = new byte[] { 2, 4, 6, 8, 10, 12 };
            byte[] hashedPasswordBytes = new byte[] {
                                                        1, 2, 3, 4, 5, 6, 7, 8,
                                                        1, 2, 3, 4, 5, 6, 7, 7,
                                                        1, 2, 3, 4, 5, 6, 7, 6,
                                                        1, 2, 3, 4, 5, 6, 7, 5,
                                                        1, 2, 3, 4, 5, 6, 7, 4,
                                                        1, 2, 3, 4, 5, 6, 7, 3,
                                                        1, 2, 3, 4, 5, 6, 7, 2,
                                                        1, 2, 3, 4, 5, 6, 7, 1,
                                                    };
            string password = "******";
            System.Func<byte, byte> transformMethod = (a) =>
            {
                if(a >= 127)
                    return System.Byte.MaxValue;
                return (byte)(a * 2);
            };

            MemoryStream streamToDecrypt = new MemoryStream(bytesToDecrypt);
            MemoryStream outputStream = new MemoryStream();

            Mock<IHashAlgorithm> hashMock = new Mock<IHashAlgorithm>();
            Mock<IAESAlgorithm> aesMock = new Mock<IAESAlgorithm>();
            DummyEncryptor transform = new DummyEncryptor(transformMethod, 16);

            hashMock.Setup(a => a.ComputeHash(It.IsAny<byte[]>())).Returns(hashedPasswordBytes);
            aesMock.Setup(a => a.CreateDecryptor(It.IsAny<byte[]>(), It.IsAny<byte[]>())).Returns(transform);

            EncryptorImplementation Encryptor = new EncryptorImplementation(aesMock.Object, hashMock.Object, Mock.Of<IFile>());

            Encryptor.Decrypt(streamToDecrypt, outputStream, password);

            outputStream.Position = 0;
            CollectionAssert.AreEqual(expectedDecryptedBytes, outputStream.ToArray());
        }
 public void Encrypt_File_WhiteSpacePassword()
 {
     try
     {
         EncryptorImplementation Encryptor = new EncryptorImplementation(Mock.Of<IAESAlgorithm>(), Mock.Of<IHashAlgorithm>(), Mock.Of<IFile>());
         Encryptor.Encrypt("TestInput", "TestOutput", "\t");
         Assert.Fail("An ArgumentException was expected.");
     }
     catch(System.ArgumentException) { }
 }
 public void Encrypt_File_EmptyInput()
 {
     try
     {
         EncryptorImplementation Encryptor = new EncryptorImplementation(Mock.Of<IAESAlgorithm>(), Mock.Of<IHashAlgorithm>(), Mock.Of<IFile>());
         Encryptor.Encrypt(string.Empty, "TestOutput", "TestPassword");
         Assert.Fail("An ArgumentException was expected.");
     }
     catch(System.ArgumentException) { }
 }
        public void Encrypt_File()
        {
            byte[] bytesToEncrypt = new byte[] { 1, 2, 3, 4, 5, 6 };
            byte[] expectedEncryptedBytes = new byte[] { 2, 4, 6, 8, 10, 12 };
            byte[] hashedPasswordBytes = new byte[] {
                                                        1, 2, 3, 4, 5, 6, 7, 8,
                                                        1, 2, 3, 4, 5, 6, 7, 7,
                                                        1, 2, 3, 4, 5, 6, 7, 6,
                                                        1, 2, 3, 4, 5, 6, 7, 5,
                                                        1, 2, 3, 4, 5, 6, 7, 4,
                                                        1, 2, 3, 4, 5, 6, 7, 3,
                                                        1, 2, 3, 4, 5, 6, 7, 2,
                                                        1, 2, 3, 4, 5, 6, 7, 1,
                                                    };
            string inputFilePath = "TestInputFilePath";
            string outputFilePath = "TestOutputFilePath";
            string password = "******";
            System.Func<byte, byte> transformMethod = (a) =>
            {
                if(a >= 127)
                    return System.Byte.MaxValue;
                return (byte)(a * 2);
            };

            DummyStream streamToEncrypt = new DummyStream(bytesToEncrypt);
            DummyStream outputStream = new DummyStream();

            Mock<IHashAlgorithm> hashMock = new Mock<IHashAlgorithm>();
            Mock<IAESAlgorithm> aesMock = new Mock<IAESAlgorithm>();
            Mock<IFile> fileMock = new Mock<IFile>();
            DummyEncryptor transform = new DummyEncryptor(transformMethod, 16);

            hashMock.Setup(a => a.ComputeHash(It.IsAny<byte[]>())).Returns(hashedPasswordBytes);
            aesMock.Setup(a => a.CreateEncryptor(It.IsAny<byte[]>(), It.IsAny<byte[]>())).Returns(transform);
            fileMock.Setup(a => a.Exists(inputFilePath)).Returns(true);
            fileMock.Setup(a => a.Exists(outputFilePath)).Returns(false);
            fileMock.Setup(a => a.OpenRead(inputFilePath)).Returns(streamToEncrypt);
            fileMock.Setup(a => a.OpenWrite(outputFilePath)).Returns(outputStream);

            EncryptorImplementation encryptor = new EncryptorImplementation(aesMock.Object, hashMock.Object, fileMock.Object);

            encryptor.Encrypt(inputFilePath, outputFilePath, password);

            outputStream.Position = 0;
            CollectionAssert.AreEqual(expectedEncryptedBytes, outputStream.ToArray());
        }
 public void EncryptTest_NullPassword()
 {
     try
     {
         var target = new EncryptorImplementation(Mock.Of<IAESAlgorithm>(), Mock.Of<IHashAlgorithm>(), Mock.Of<IFile>());
         target.Encrypt(Mock.Of<System.IO.Stream>(), Mock.Of<System.IO.Stream>(), null);
         Assert.Fail("An ArgumentException was expected.");
     }
     catch(System.ArgumentException) { }
 }
 public void Decrypt_File_NullOutput()
 {
     try
     {
         EncryptorImplementation Encryptor = new EncryptorImplementation(Mock.Of<IAESAlgorithm>(), Mock.Of<IHashAlgorithm>(), Mock.Of<IFile>());
         Encryptor.Decrypt("TestInput", null, "TestPassword");
         Assert.Fail("An ArgumentException was expected.");
     }
     catch(System.ArgumentException) { }
 }