public void TestInvalidArguments() { using (Stream inputStream = FakeDataStore.ExpandableMemoryStream(Encoding.UTF8.GetBytes("AxCrypt is Great!"))) { using (Stream outputStream = new MemoryStream()) { using (V1AxCryptDocument document = new V1AxCryptDocument()) { Assert.Throws <ArgumentNullException>(() => { document.EncryptTo(inputStream, null, AxCryptOptions.EncryptWithCompression); }); Assert.Throws <ArgumentNullException>(() => { document.EncryptTo(null, outputStream, AxCryptOptions.EncryptWithCompression); }); Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, new NonSeekableStream(), AxCryptOptions.EncryptWithCompression); }); Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, outputStream, AxCryptOptions.EncryptWithCompression | AxCryptOptions.EncryptWithoutCompression); }); Assert.Throws <ArgumentException>(() => { document.EncryptTo(inputStream, outputStream, AxCryptOptions.None); }); Passphrase passphrase = new Passphrase("a"); V1DocumentHeaders headers = new V1DocumentHeaders(passphrase, 13); Assert.Throws <ArgumentNullException>(() => { document.CopyEncryptedTo(null, outputStream); }); Assert.Throws <ArgumentNullException>(() => { document.CopyEncryptedTo(headers, null); }); Assert.Throws <ArgumentException>(() => { document.CopyEncryptedTo(headers, new NonSeekableStream()); }); Assert.Throws <InternalErrorException>(() => { document.CopyEncryptedTo(headers, outputStream); }); } } } }
public void TestDecryptOfTooNewFileVersion() { DateTime creationTimeUtc = new DateTime(2012, 1, 1, 1, 2, 3, DateTimeKind.Utc); DateTime lastAccessTimeUtc = creationTimeUtc + new TimeSpan(1, 0, 0); DateTime lastWriteTimeUtc = creationTimeUtc + new TimeSpan(2, 0, 0);; using (Stream inputStream = FakeDataStore.ExpandableMemoryStream(Encoding.UTF8.GetBytes("AxCrypt is Great!"))) { using (Stream outputStream = new MemoryStream()) { Passphrase passphrase = new Passphrase("a"); using (V1AxCryptDocument document = new V1AxCryptDocument(passphrase, 101)) { document.FileName = "MyFile.txt"; document.CreationTimeUtc = creationTimeUtc; document.LastAccessTimeUtc = lastAccessTimeUtc; document.LastWriteTimeUtc = lastWriteTimeUtc; VersionHeaderBlock versionHeaderBlock = document.DocumentHeaders.VersionHeaderBlock; versionHeaderBlock.FileVersionMajor = (byte)(versionHeaderBlock.FileVersionMajor + 1); document.EncryptTo(inputStream, outputStream, AxCryptOptions.EncryptWithoutCompression); } outputStream.Position = 0; using (V1AxCryptDocument document = new V1AxCryptDocument()) { Assert.Throws <FileFormatException>(() => { document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, outputStream); }); } } } }
public void TestSimpleEncryptToWithoutCompression() { DateTime creationTimeUtc = new DateTime(2012, 1, 1, 1, 2, 3, DateTimeKind.Utc); DateTime lastAccessTimeUtc = creationTimeUtc + new TimeSpan(1, 0, 0); DateTime lastWriteTimeUtc = creationTimeUtc + new TimeSpan(2, 0, 0);; using (Stream inputStream = FakeDataStore.ExpandableMemoryStream(Encoding.UTF8.GetBytes("AxCrypt is Great!"))) { using (Stream outputStream = new MemoryStream()) { Passphrase passphrase = new Passphrase("a"); using (V1AxCryptDocument document = new V1AxCryptDocument(passphrase, 53)) { document.DocumentHeaders.FileName = "MyFile.txt"; document.DocumentHeaders.CreationTimeUtc = creationTimeUtc; document.DocumentHeaders.LastAccessTimeUtc = lastAccessTimeUtc; document.DocumentHeaders.LastWriteTimeUtc = lastWriteTimeUtc; document.EncryptTo(inputStream, outputStream, AxCryptOptions.EncryptWithoutCompression); } outputStream.Position = 0; using (V1AxCryptDocument document = new V1AxCryptDocument()) { bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, outputStream); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); Assert.That(document.DocumentHeaders.FileName, Is.EqualTo("MyFile.txt")); Assert.That(document.DocumentHeaders.CreationTimeUtc, Is.EqualTo(creationTimeUtc)); Assert.That(document.DocumentHeaders.LastAccessTimeUtc, Is.EqualTo(lastAccessTimeUtc)); Assert.That(document.DocumentHeaders.LastWriteTimeUtc, Is.EqualTo(lastWriteTimeUtc)); using (MemoryStream plaintextStream = new MemoryStream()) { document.DecryptTo(plaintextStream); Assert.That(document.DocumentHeaders.UncompressedLength, Is.EqualTo(-1), "'AxCrypt is Great!' should not return a value at all for uncompressed, since it was not compressed."); Assert.That(document.DocumentHeaders.PlaintextLength, Is.EqualTo(17), "'AxCrypt is Great!' is 17 bytes plaintext length."); Assert.That(Encoding.ASCII.GetString(plaintextStream.GetBuffer(), 0, (int)plaintextStream.Length), Is.EqualTo("AxCrypt is Great!"), "Unexpected result of decryption."); } } } } }
public void TestHmacThrowsWhenTooLittleData() { using (MemoryStream plaintext = new MemoryStream(Resources.uncompressable_zip)) { MemoryStream encryptedFile = new MemoryStream(); using (V1AxCryptDocument encryptingDocument = new V1AxCryptDocument(new Passphrase("a"), 10)) { encryptingDocument.EncryptTo(plaintext, encryptedFile, AxCryptOptions.EncryptWithoutCompression); } encryptedFile.Position = 0; Headers headers = new Headers(); AxCryptReader reader = headers.CreateReader(new LookAheadStream(encryptedFile)); using (V1AxCryptDocument document = new V1AxCryptDocument(reader)) { Passphrase key = new Passphrase("a"); bool keyIsOk = document.Load(key, new V1Aes128CryptoFactory().CryptoId, headers); Assert.That(keyIsOk, Is.True); reader.InputStream.Read(new byte[16], 0, 16); Assert.Throws <InvalidOperationException>(() => document.DecryptTo(Stream.Null)); } } }