public static void TestDoubleDispose()
 {
     using (Stream stream = Stream.Null)
     {
         Assert.DoesNotThrow(() =>
         {
             using (ProgressStream progressStream = new ProgressStream(stream, new ProgressContext()))
             {
                 progressStream.Dispose();
             }
         });
     }
 }
        public static void TestInvalidArguments()
        {
            Stream nullStream = null;
            ProgressContext nullProgress = null;

            ProgressStream progressStream;
            Assert.Throws<ArgumentNullException>(() => { progressStream = new ProgressStream(nullStream, new ProgressContext()); });
            Assert.Throws<ArgumentNullException>(() => { progressStream = new ProgressStream(new MemoryStream(), nullProgress); });

            progressStream = new ProgressStream(new MemoryStream(), new ProgressContext());
            byte[] nullBuffer = null;
            Assert.Throws<ArgumentNullException>(() => { progressStream.Write(nullBuffer, 0, 0); });
            Assert.Throws<ArgumentNullException>(() => { progressStream.Read(nullBuffer, 0, 0); });
        }
 public static void TestPropertiesAndMethods()
 {
     using (MemoryStream memoryStream = FakeRuntimeFileInfo.ExpandableMemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }))
     {
         string kilroy = String.Empty;
         using (FakeStream testStream = new FakeStream(memoryStream, (string wasHere) => { kilroy += wasHere; }))
         {
             using (ProgressStream progressStream = new ProgressStream(testStream, new ProgressContext()))
             {
                 kilroy = String.Empty;
                 Assert.That(progressStream.CanRead, Is.True, "The underlying stream is readable.");
                 Assert.That(kilroy, Is.EqualTo("CanRead"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.CanWrite, Is.True, "The underlying stream is writable.");
                 Assert.That(kilroy, Is.EqualTo("CanWrite"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.CanSeek, Is.True, "The underlying stream is seekable.");
                 Assert.That(kilroy, Is.EqualTo("CanSeek"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 progressStream.Flush();
                 Assert.That(kilroy, Is.EqualTo("Flush"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.Length, Is.EqualTo(10), "There are 10 bytes  in the underlying stream.");
                 Assert.That(kilroy, Is.EqualTo("Length"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.Seek(-4, SeekOrigin.End), Is.EqualTo(6), "4 bytes from the end of 10 should be 6.");
                 Assert.That(kilroy, Is.EqualTo("Seek"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.Position, Is.EqualTo(6), "The position should still be at 6.");
                 Assert.That(kilroy, Is.EqualTo("getPosition"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 progressStream.Position = 0;
                 Assert.That(kilroy, Is.EqualTo("setPosition"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 progressStream.Write(new byte[] { 13 }, 0, 1);
                 Assert.That(kilroy.Contains("Write"), Is.True, "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 progressStream.Position = 0;
                 byte[] firstByte = new byte[1];
                 progressStream.Read(firstByte, 0, 1);
                 Assert.That(kilroy.Contains("Read"), Is.True, "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(firstByte[0], Is.EqualTo(13), "13 was just written to the first position.");
                 progressStream.SetLength(5);
                 Assert.That(kilroy, Is.EqualTo("SetLength"), "ProgressStream should delegate to the underlying stream.");
             }
         }
     }
 }
Example #4
0
        /// <summary>
        /// Load an AxCryptDocument from a source file with a passphrase
        /// </summary>
        /// <param name="sourceFile">The source file</param>
        /// <param name="passphrase">The passphrase</param>
        /// <returns>An instance of AxCryptDocument. Use IsPassphraseValid property to determine validity.</returns>
        public static AxCryptDocument Document(IRuntimeFileInfo sourceFile, AesKey key, ProgressContext progress)
        {
            if (sourceFile == null)
            {
                throw new ArgumentNullException("sourceFile");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (progress == null)
            {
                throw new ArgumentNullException("progress");
            }

            AxCryptDocument document = new AxCryptDocument();
            Stream stream = new ProgressStream(sourceFile.OpenRead(), progress);
            progress.AddTotal(stream.Length);
            document.Load(stream, key);
            return document;
        }