public static void TestConstructor()
        {
            Assert.Throws<ArgumentNullException>(() =>
            {
                using (AxCryptReader axCryptReader = new AxCryptStreamReader(null)) { }
            }, "A non-null input-stream must be specified.");

            using (MemoryStream inputStream = new MemoryStream())
            {
                AxCrypt1Guid.Write(inputStream);
                inputStream.Position = 0;
                using (AxCryptReader axCryptReader = new AxCryptStreamReader(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");
                }
            }

            using (MemoryStream inputStream = new MemoryStream())
            {
                AxCrypt1Guid.Write(inputStream);
                inputStream.Position = 0;

                // The stream reader supports both externally supplied LookAheadStream or will wrap it if it is not.
                using (AxCryptReader axCryptReader = new AxCryptStreamReader(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");
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Instantiate an AxCryptReader from a stream.
        /// </summary>
        /// <param name="inputStream">The stream to read from, will be disposed when this instance is disposed.</param>
        /// <returns></returns>
        public static AxCryptReader Create(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            AxCryptReader reader = new AxCryptStreamReader(inputStream);

            reader.CurrentItemType = AxCryptItemType.None;

            return(reader);
        }
Beispiel #3
0
        /// <summary>
        /// Instantiate an AxCryptReader from a stream.
        /// </summary>
        /// <param name="inputStream">The stream to read from, will be disposed when this instance is disposed.</param>
        /// <returns></returns>
        public static AxCryptReader Create(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            AxCryptReader reader = new AxCryptStreamReader(inputStream);
            reader.CurrentItemType = AxCryptItemType.None;

            return reader;
        }
        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.");
                    }
                }
            }
        }