public static void TestInvalidHeaderBlockType() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); preambleHeaderBlock.Write(inputStream); BadHeaderBlock badHeaderBlock = new BadHeaderBlock(); badHeaderBlock.FakeHeaderBlockLength = 5; badHeaderBlock.SetHeaderBlockType(HeaderBlockType.Encrypted); 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.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 TestBadArguments() { PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); Assert.Throws <ArgumentNullException>(() => { preambleHeaderBlock.Hmac = null; }); }
public static void TestFindPreambleHeaderBlockFromSimpleFile() { using (Stream testStream = FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(testStream))) { bool blockFound = false; int headers = 0; while (axCryptReader.Read()) { switch (axCryptReader.CurrentItemType) { case AxCryptItemType.None: break; case AxCryptItemType.MagicGuid: break; case AxCryptItemType.HeaderBlock: if (axCryptReader.CurrentHeaderBlock.HeaderBlockType == HeaderBlockType.Preamble) { Assert.That(blockFound, Is.False, "We should only find one single PreambleHeaderBlock"); blockFound = true; Assert.That(headers, Is.EqualTo(0), "Preamble must be first"); PreambleHeaderBlock preambleHeaderBlock = (PreambleHeaderBlock)axCryptReader.CurrentHeaderBlock; Assert.That(preambleHeaderBlock.Hmac.Length, Is.EqualTo(V1Hmac.RequiredLength), "The HMAC in the preamble must be exactly 16 bytes."); } ++headers; break; case AxCryptItemType.Data: break; case AxCryptItemType.EndOfStream: break; default: break; } } Assert.That(blockFound, Is.True, "We're expecting a VersionHeaderBlock to be found!"); } } }
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 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 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"); } } }