public static void TestFindMagicGuidWithOtherFirstButNoMore() { using (MemoryStream testStream = new MemoryStream()) { byte[] someBytes = Encoding.UTF8.GetBytes("This is a test string that we'll convert into some random bytes...."); testStream.Write(someBytes, 0, someBytes.Length); AxCrypt1Guid.Write(testStream); testStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); } } }
public static void TestInvalidItemType() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); new PreambleHeaderBlock().Write(inputStream); inputStream.Position = 0; using (AxCryptReaderForTest axCryptReader = new AxCryptReaderForTest(inputStream)) { DocumentHeaders documentHeaders = new DocumentHeaders(new AesKey()); Assert.Throws <InternalErrorException>(() => { documentHeaders.Load(axCryptReader); }); } } }
public void TestInvalidItemType() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); new PreambleHeaderBlock().Write(inputStream); inputStream.Position = 0; using (AxCryptReaderForTest axCryptReader = new AxCryptReaderForTest(new LookAheadStream(inputStream))) { V1DocumentHeaders documentHeaders = new V1DocumentHeaders(new Passphrase("secret"), 15); Assert.Throws <InternalErrorException>(() => { documentHeaders.Load(axCryptReader); }); } } }
public void TestDecryptAfterFailedLoad() { using (Stream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); testStream.Position = 0; Passphrase passphrase = new Passphrase("Å ä Ö"); using (V1AxCryptDocument document = new V1AxCryptDocument()) { Assert.Throws <FileFormatException>(() => { document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, testStream); }); using (MemoryStream plaintextStream = new MemoryStream()) { Assert.Throws <InternalErrorException>(() => { document.DecryptTo(plaintextStream); }); } } } }
public static void TestDecryptAfterFailedLoad() { using (Stream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); testStream.Position = 0; using (AxCryptDocument document = new AxCryptDocument()) { Passphrase passphrase = new Passphrase("Å ä Ö"); Assert.Throws <FileFormatException>(() => { document.Load(testStream, passphrase.DerivedPassphrase); }); using (MemoryStream plaintextStream = new MemoryStream()) { Assert.Throws <InternalErrorException>(() => { document.DecryptTo(plaintextStream, new ProgressContext()); }); } } } }
public void TestFindPreambleHeaderBlockNotFirstShouldThrow() { using (MemoryStream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); VersionHeaderBlock versionHeaderBlock = new VersionHeaderBlock(new byte[] { 3, 2, 2, 0, 0 }); versionHeaderBlock.Write(testStream); PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); preambleHeaderBlock.Write(testStream); testStream.Position = 0; using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(testStream))) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.Throws <FileFormatException>(() => axCryptReader.Read()); } } }
public static void TestFactoryMethod() { Assert.Throws <ArgumentNullException>(() => { using (AxCryptReader axCryptReader = AxCryptReader.Create(null)) { } }, "A non-null input-stream must be specified."); using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); inputStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(inputStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); } } }
public void TestFindPreambleHeaderBlockFirstButMoreThanOnceShouldThrow() { using (MemoryStream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); preambleHeaderBlock.Write(testStream); preambleHeaderBlock.Write(testStream); testStream.Position = 0; using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(testStream))) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "We're expecting to have found a HeaderBlock"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Preamble), "We're expecting to have found a Preamble specifically"); Assert.Throws <FileFormatException>(() => axCryptReader.Read()); } } }
public static void TestTooShortStream() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); BadHeaderBlock badHeaderBlock = new BadHeaderBlock(); badHeaderBlock.FakeHeaderBlockLength = 5 + 1; badHeaderBlock.Write(inputStream); inputStream.Position = 0; using (AxCryptReaderForTest axCryptReader = new AxCryptReaderForTest(inputStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.That(axCryptReader.Read(), Is.False, "The stream is too short and end prematurely and should thus be able to read the block"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.EndOfStream), "The stream is at an end and current item type should reflect this."); } } }
public static void TestFindMagicGuidWithMuchOtherFirstAndMore() { using (MemoryStream testStream = new MemoryStream()) { byte[] someBytes = Encoding.UTF8.GetBytes("This is a test string that we'll convert into some random bytes...."); int targetAmountOfBytesBefore = 1024 * 1024 * 4; while (targetAmountOfBytesBefore > 0) { testStream.Write(someBytes, 0, someBytes.Length); targetAmountOfBytesBefore -= someBytes.Length; } AxCrypt1Guid.Write(testStream); testStream.Write(someBytes, 0, someBytes.Length); testStream.Position = 0; using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(testStream))) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); } } }
public static void TestTooLargeHeaderBlockLength() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); BadHeaderBlock badHeaderBlock = new BadHeaderBlock(); badHeaderBlock.FakeHeaderBlockLength = 0x1000000; badHeaderBlock.Write(inputStream); inputStream.Position = 0; using (AxCryptReaderForTest axCryptReader = new AxCryptReaderForTest(inputStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.Throws <FileFormatException>(() => { axCryptReader.Read(); }, "A too large header block length is not valid."); } } }
public static void TestNegativeHeaderBlockType() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); BadHeaderBlock badHeaderBlock = new BadHeaderBlock(); badHeaderBlock.SetHeaderBlockType((HeaderBlockType)(-1)); badHeaderBlock.Write(inputStream); inputStream.Position = 0; using (AxCryptReaderForTest axCryptReader = new AxCryptReaderForTest(inputStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.Throws <FileFormatException>(() => { axCryptReader.Read(); }, "A negative header block type is not valid."); } } }
public void TestFindFindIdTag() { using (MemoryStream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); new PreambleHeaderBlock().Write(testStream); new V1IdTagHeaderBlock("A test").Write(testStream); testStream.Position = 0; using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(testStream))) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "This should be a header block"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Preamble), "This should be an Preamble block"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "This should be a header block"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.IdTag), "This should be an IdTag block"); V1IdTagHeaderBlock idTagHeaderBlock = (V1IdTagHeaderBlock)axCryptReader.CurrentHeaderBlock; Assert.That(idTagHeaderBlock.IdTag, Is.EqualTo("A test"), "We're expecting to be able to read the same tag we wrote"); } } }
public static void TestLongerReadWrite() { byte[] bytesToWrite = new FakeRandomGenerator().Generate(V2AxCryptDataStream.WriteChunkSize + V2AxCryptDataStream.WriteChunkSize / 2); byte[] buffer = new byte[bytesToWrite.Length + 2000]; using (V2AxCryptDataStream axCryptDataStreamWriter = V2AxCryptDataStream.Create(new MemoryStream(buffer))) { AxCrypt1Guid.Write(axCryptDataStreamWriter.Chained); new PreambleHeaderBlock().Write(axCryptDataStreamWriter.Chained); new DataHeaderBlock().Write(axCryptDataStreamWriter.Chained); axCryptDataStreamWriter.Write(bytesToWrite, 0, bytesToWrite.Length); axCryptDataStreamWriter.Flush(); new V2HmacHeaderBlock().Write(axCryptDataStreamWriter.Chained); } using (AxCryptReader reader = new TestingAxCryptReader(new LookAheadStream(new MemoryStream(buffer)))) { while (reader.Read()) { ; } reader.SetStartOfData(); using (V2AxCryptDataStream axCryptDataStreamReader = V2AxCryptDataStream.Create(reader, Stream.Null)) { byte[] bytesRead = new byte[bytesToWrite.Length]; int offset = 0; int count; do { count = axCryptDataStreamReader.Read(bytesRead, offset, 100); offset += count; } while (count > 0); Assert.That(bytesRead, Is.EquivalentTo(bytesToWrite)); } } }
public static void TestUnrecognizedHeaderBlock() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); preambleHeaderBlock.Write(inputStream); UnrecognizedHeaderBlock unrecognizedHeaderBlock = new UnrecognizedHeaderBlock(HeaderBlockType.Unrecognized, new byte[0]); unrecognizedHeaderBlock.Write(inputStream); inputStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(inputStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "We're expecting to have found a HeaderBlock"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Preamble), "We're expecting to have found a Preamble specifically"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "We're expecting to have found a HeaderBlock"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Unrecognized), "We're expecting to have found an unrecognized block specifically"); } } }
public static void TestKeyWrap2HeaderBlock() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); preambleHeaderBlock.Write(inputStream); V1KeyWrap2HeaderBlock keyWrap2HeaderBlock = new V1KeyWrap2HeaderBlock(new byte[0]); keyWrap2HeaderBlock.Write(inputStream); inputStream.Position = 0; using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(inputStream))) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "We're expecting to have found a HeaderBlock"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Preamble), "We're expecting to have found a Preamble specifically"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "We're expecting to have found a HeaderBlock"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.KeyWrap2), "We're expecting to have found a KeyWrap2 specifically"); } } }