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() { 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()); }