public static void TestUnexpectedBlockType() { byte[] buffer = new byte[2000]; using (V2AxCryptDataStream axCryptDataStreamWriter = V2AxCryptDataStream.Create(new MemoryStream(buffer))) { AxCrypt1Guid.Write(axCryptDataStreamWriter.Chained); new PreambleHeaderBlock().Write(axCryptDataStreamWriter.Chained); new DataHeaderBlock().Write(axCryptDataStreamWriter.Chained); byte[] bytes = Encoding.UTF8.GetBytes("This is a short text."); axCryptDataStreamWriter.Write(bytes, 0, bytes.Length); axCryptDataStreamWriter.Flush(); new DataHeaderBlock().Write(axCryptDataStreamWriter.Chained); } using (AxCryptReader reader = new TestingAxCryptReader(new LookAheadStream(new MemoryStream(buffer)))) { while (reader.Read()) { ; } reader.SetStartOfData(); using (TextReader textReader = new StreamReader(V2AxCryptDataStream.Create(reader, Stream.Null), Encoding.UTF8)) { string text = null; Assert.Throws <FileFormatException>(() => text = textReader.ReadToEnd()); Assert.That(text, Is.Null); } } }
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)); } } }