public static void TestCreateEncryptedDataStreamErrorChecks() { using (MemoryStream inputStream = new MemoryStream()) { using (AxCryptReader axCryptReader = new AxCryptStreamReader(inputStream)) { Assert.Throws <ArgumentNullException>(() => { axCryptReader.CreateEncryptedDataStream(null, 0, new ProgressContext()); }, "A non-null HMAC key must be specified."); Assert.Throws <ArgumentNullException>(() => { axCryptReader.CreateEncryptedDataStream(new AesKey(), 0, null); }, "A non-null ProgresContext must be specified."); Assert.Throws <InvalidOperationException>(() => { axCryptReader.CreateEncryptedDataStream(new AesKey(), 0, new ProgressContext()); }, "The reader is not positioned properly to read encrypted data."); axCryptReader.Dispose(); Assert.Throws <ObjectDisposedException>(() => { axCryptReader.CreateEncryptedDataStream(new AesKey(), 0, new ProgressContext()); }, "The reader is disposed."); } } }
public static void TestObjectDisposed() { using (Stream inputStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader axCryptReader = new AxCryptStreamReader(inputStream)) { axCryptReader.Dispose(); Assert.Throws <ObjectDisposedException>(() => { bool isOk = axCryptReader.Read(); Object.Equals(isOk, null); }, "The reader is disposed."); } } }
public static void TestHmac() { using (Stream inputStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader axCryptReader = new AxCryptStreamReader(inputStream)) { Assert.Throws <InvalidOperationException>(() => { if (axCryptReader.Hmac == null) { } }, "The reader is not positioned properly to get the HMAC."); Passphrase passphrase = new Passphrase("a"); DocumentHeaders documentHeaders = new DocumentHeaders(passphrase.DerivedPassphrase); bool keyIsOk = documentHeaders.Load(axCryptReader); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); using (Stream encrypedDataStream = axCryptReader.CreateEncryptedDataStream(documentHeaders.HmacSubkey.Key, documentHeaders.CipherTextLength, new ProgressContext())) { Assert.Throws <InvalidOperationException>(() => { if (axCryptReader.Hmac == null) { } }, "We have not read the encrypted data yet."); Assert.That(axCryptReader.Read(), Is.False, "The reader should be at end of stream now, and Read() should return false."); encrypedDataStream.CopyTo(Stream.Null, 4096); Assert.That(documentHeaders.Hmac, Is.EqualTo(axCryptReader.Hmac), "The HMAC should be correct."); axCryptReader.Dispose(); Assert.Throws <ObjectDisposedException>(() => { DataHmac disposedHmac = axCryptReader.Hmac; Object.Equals(disposedHmac, null); }, "The reader is disposed."); } } } }