Example #1
0
        public ChainedStreamTests()
        {
            var bytes    = new byte[10 * 1024];
            int position = 0;

            random = new Random();
            random.NextBytes(bytes);

            // this is our master stream, all operations on the chained stream
            // should match the results on this stream
            master  = new MemoryStream(bytes);
            backing = new MemoryStream(master.ToArray());
            cbuf    = new byte[4096];
            mbuf    = new byte[4096];

            // make a handful of smaller streams based on master to chain together
            chained = new ChainedStream();
            while (position < bytes.Length)
            {
                int n = Math.Min(bytes.Length - position, random.Next() % 4096);

                var stream = new BoundStream(backing, position, position + n, true);

                lengths.Add(n);
                position += n;

                chained.Add(new ReadOneByteStream(stream));
            }
        }
Example #2
0
        public void TestStreamArguments()
        {
            using (var stream = new MeasuringStream())
                AssertStreamArguments(stream);

            using (var stream = new MemoryBlockStream())
                AssertStreamArguments(stream);

            using (var memory = new MemoryStream()) {
                using (var stream = new FilteredStream(memory))
                    AssertStreamArguments(stream);
            }

            using (var memory = new MemoryStream()) {
                using (var stream = new BoundStream(memory, 0, -1, true))
                    AssertStreamArguments(stream);
            }

            using (var memory = new MemoryStream()) {
                using (var stream = new ChainedStream()) {
                    stream.Add(memory);

                    AssertStreamArguments(stream);
                }
            }
        }
Example #3
0
        public void TestStreamArguments()
        {
            using (var stream = new MeasuringStream())
                AssertStreamArguments(stream);

            using (var stream = new MemoryBlockStream())
                AssertStreamArguments(stream);

            using (var memory = new MemoryStream()) {
                Assert.Throws <ArgumentNullException> (() => new FilteredStream(null));

                using (var stream = new FilteredStream(memory))
                    AssertStreamArguments(stream);
            }

            using (var memory = new MemoryStream()) {
                Assert.Throws <ArgumentNullException> (() => new BoundStream(null, 0, 10, true));
                Assert.Throws <ArgumentOutOfRangeException> (() => new BoundStream(memory, -1, 10, true));
                Assert.Throws <ArgumentOutOfRangeException> (() => new BoundStream(memory, 5, 1, true));

                using (var stream = new BoundStream(memory, 0, -1, true))
                    AssertStreamArguments(stream);
            }

            using (var memory = new MemoryStream()) {
                using (var stream = new ChainedStream()) {
                    stream.Add(memory);

                    Assert.Throws <ArgumentNullException> (() => stream.Add(null));

                    AssertStreamArguments(stream);
                }
            }
        }
Example #4
0
        async Task ConstructMimePartAsync(MimePart part, CancellationToken cancellationToken)
        {
            Stream content;
            bool   isEmpty;

            if (persistent)
            {
                long begin = GetOffset(inputIndex);
                long end;

                using (var measured = new MeasuringStream()) {
                    isEmpty = await ScanContentAsync(measured, true, cancellationToken).ConfigureAwait(false);

                    end = begin + measured.Length;
                }

                content = new BoundStream(stream, begin, end, true);
            }
            else
            {
                content = new MemoryBlockStream();
                isEmpty = await ScanContentAsync(content, true, cancellationToken).ConfigureAwait(false);

                content.Seek(0, SeekOrigin.Begin);
            }

            if (!isEmpty)
            {
                part.Content = new MimeContent(content, part.ContentTransferEncoding);
            }
            else
            {
                content.Dispose();
            }
        }
Example #5
0
        async Task <BoundaryType> ConstructMimePartAsync(MimePart part, CancellationToken cancellationToken)
        {
            ScanContentResults results;
            Stream             content;

            if (persistent)
            {
                long begin = GetOffset(inputIndex);
                long end;

                using (var measured = new MeasuringStream()) {
                    results = await ScanContentAsync(measured, true, cancellationToken).ConfigureAwait(false);

                    end = begin + measured.Length;
                }

                content = new BoundStream(stream, begin, end, true);
            }
            else
            {
                content = new MemoryBlockStream();
                results = await ScanContentAsync(content, true, cancellationToken).ConfigureAwait(false);

                content.Seek(0, SeekOrigin.Begin);
            }

            if (!results.IsEmpty)
            {
                part.ContentObject = new ContentObject(content, part.ContentTransferEncoding);
            }

            return(results.Boundary);
        }
        public void TestSetLength()
        {
            using (var memory = new MemoryStream()) {
                var buffer = Encoding.ASCII.GetBytes("This is some text...");

                memory.Write(buffer, 0, buffer.Length);

                using (var bounded = new BoundStream(memory, 0, -1, true)) {
                    var buf = new byte[1024];

                    Assert.AreEqual(buffer.Length, bounded.Length);

                    bounded.Read(buf, 0, buf.Length);                      // read the text
                    bounded.Read(buf, 0, buf.Length);                      // cause eos to be true

                    Assert.AreEqual(buffer.Length, bounded.Length);

                    bounded.SetLength(500);

                    Assert.AreEqual(500, bounded.Length);
                    Assert.AreEqual(500, memory.Length);
                }
            }

            using (var memory = new MemoryStream()) {
                var buffer = Encoding.ASCII.GetBytes("This is some text...");

                memory.Write(buffer, 0, buffer.Length);

                using (var bounded = new BoundStream(memory, 0, buffer.Length, true)) {
                    Assert.AreEqual(buffer.Length, bounded.Length);

                    bounded.SetLength(500);

                    Assert.AreEqual(500, bounded.Length);
                    Assert.AreEqual(500, memory.Length);
                }
            }

            using (var memory = new MemoryStream()) {
                var buffer = Encoding.ASCII.GetBytes("This is some text...");

                memory.Write(buffer, 0, buffer.Length);

                using (var bounded = new BoundStream(memory, 0, buffer.Length, true)) {
                    Assert.AreEqual(buffer.Length, bounded.Length);

                    bounded.SetLength(5);

                    Assert.AreEqual(5, bounded.Length);
                    Assert.AreEqual(buffer.Length, memory.Length);
                }
            }
        }
        public void TestGetSetTimeouts()
        {
            using (var bounded = new BoundStream(new TimeoutStream(), 0, -1, false)) {
                Assert.AreEqual(0, bounded.ReadTimeout);
                Assert.AreEqual(0, bounded.WriteTimeout);

                bounded.ReadTimeout = 10;
                Assert.AreEqual(10, bounded.ReadTimeout);

                bounded.WriteTimeout = 100;
                Assert.AreEqual(100, bounded.WriteTimeout);
            }
        }
Example #8
0
        public void TestWritingBeyondEndBoundaryAsync()
        {
            using (var memory = new MemoryStream()) {
                var buffer = new byte[] { (byte)'A' };

                memory.Write(buffer, 0, buffer.Length);
                memory.Position = 0;

                using (var bounded = new BoundStream(memory, 0, 2, true)) {
                    buffer = new byte[] { (byte)'b', (byte)'c', (byte)'d' };

                    Assert.ThrowsAsync <IOException> (async() => await bounded.WriteAsync(buffer, 0, buffer.Length));
                }
            }
        }
Example #9
0
        async Task ConstructMimePartAsync(MimePart part, MimeEntityEndEventArgs args, CancellationToken cancellationToken)
        {
            long endOffset, beginOffset = GetOffset(inputIndex);
            var  beginLineNumber = lineNumber;
            ScanContentResult result;
            Stream            content;

            if (persistent)
            {
                using (var measured = new MeasuringStream()) {
                    result = await ScanContentAsync(measured, true, cancellationToken).ConfigureAwait(false);

                    endOffset = beginOffset + measured.Length;
                }

                content = new BoundStream(stream, beginOffset, endOffset, true);
            }
            else
            {
                content = new MemoryBlockStream();

                try {
                    result = await ScanContentAsync(content, true, cancellationToken).ConfigureAwait(false);

                    content.Seek(0, SeekOrigin.Begin);
                } catch {
                    content.Dispose();
                    throw;
                }

                endOffset = beginOffset + content.Length;
            }

            args.Lines = GetLineCount(beginLineNumber, beginOffset, endOffset);

            if (!result.IsEmpty)
            {
                part.Content = new MimeContent(content, part.ContentTransferEncoding)
                {
                    NewLineFormat = result.Format
                }
            }
            ;
            else
            {
                content.Dispose();
            }
        }
Example #10
0
        public void TestSetPosition()
        {
            using (var memory = new MemoryStream()) {
                var buffer = Encoding.ASCII.GetBytes("This is some text...");

                memory.Write(buffer, 0, buffer.Length);
                memory.Position = 0;

                using (var bounded = new BoundStream(memory, 0, -1, true)) {
                    bounded.Position = 10;

                    Assert.AreEqual(10, bounded.Position, "BoundedStream position");
                    Assert.AreEqual(10, memory.Position, "MemoryStream position");
                }
            }
        }
        public void TestCanReadWriteSeek()
        {
            var buffer = new byte[1024];

            using (var bounded = new BoundStream(new CanReadWriteSeekStream(true, false, false, false), 0, -1, false)) {
                Assert.IsTrue(bounded.CanRead);
                Assert.IsFalse(bounded.CanWrite);
                Assert.IsFalse(bounded.CanSeek);
                Assert.IsFalse(bounded.CanTimeout);

                Assert.Throws <NotImplementedException> (() => bounded.Read(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => bounded.Write(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => bounded.Seek(0, SeekOrigin.End));
            }

            using (var bounded = new BoundStream(new CanReadWriteSeekStream(false, true, false, false), 0, -1, false)) {
                Assert.IsFalse(bounded.CanRead);
                Assert.IsTrue(bounded.CanWrite);
                Assert.IsFalse(bounded.CanSeek);
                Assert.IsFalse(bounded.CanTimeout);

                Assert.Throws <NotSupportedException> (() => bounded.Read(buffer, 0, buffer.Length));
                Assert.Throws <NotImplementedException> (() => bounded.Write(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => bounded.Seek(0, SeekOrigin.End));
            }

            using (var bounded = new BoundStream(new CanReadWriteSeekStream(false, false, true, false), 0, -1, false)) {
                Assert.IsFalse(bounded.CanRead);
                Assert.IsFalse(bounded.CanWrite);
                Assert.IsTrue(bounded.CanSeek);
                Assert.IsFalse(bounded.CanTimeout);

                Assert.Throws <NotSupportedException> (() => bounded.Read(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => bounded.Write(buffer, 0, buffer.Length));
                Assert.Throws <NotImplementedException> (() => bounded.Seek(0, SeekOrigin.End));
            }
        }
        public void TestSeek()
        {
            using (var memory = new MemoryStream()) {
                var buffer = Encoding.ASCII.GetBytes("This is some text...");

                memory.Write(buffer, 0, buffer.Length);

                using (var bounded = new BoundStream(memory, 5, -1, true)) {
                    long   position;
                    string text;
                    int    n;

                    // make sure that BoundStream will properly reset the underlying stream
                    Assert.AreEqual(0, bounded.Position);
                    n    = bounded.Read(buffer, 0, buffer.Length);
                    text = Encoding.ASCII.GetString(buffer, 0, n);
                    Assert.AreEqual("is some text...", text);

                    // force eos state to be true
                    bounded.Read(buffer, 0, buffer.Length);

                    position = bounded.Seek(-1 * n, SeekOrigin.End);
                    Assert.AreEqual(0, position, "SeekOrigin.End");
                    n    = bounded.Read(buffer, 0, buffer.Length);
                    text = Encoding.ASCII.GetString(buffer, 0, n);
                    Assert.AreEqual("is some text...", text);

                    position = bounded.Seek(0, SeekOrigin.Begin);
                    Assert.AreEqual(0, position, "SeekOrigin.Begin");
                    n    = bounded.Read(buffer, 0, buffer.Length);
                    text = Encoding.ASCII.GetString(buffer, 0, n);
                    Assert.AreEqual("is some text...", text);

                    position = bounded.Seek(-1 * n, SeekOrigin.Current);
                    Assert.AreEqual(0, position, "SeekOrigin.Current");
                    n    = bounded.Read(buffer, 0, buffer.Length);
                    text = Encoding.ASCII.GetString(buffer, 0, n);
                    Assert.AreEqual("is some text...", text);

                    // now try seeking out of bounds
                    Assert.Throws <IOException> (() => bounded.Seek(-1, SeekOrigin.Begin));
                    Assert.Throws <IOException> (() => bounded.Seek(-1 * buffer.Length, SeekOrigin.End));
                }
            }

            using (var memory = new MemoryStream()) {
                var buffer = Encoding.ASCII.GetBytes("This is some text...");

                memory.Write(buffer, 0, buffer.Length);

                using (var bounded = new BoundStream(memory, 5, buffer.Length, true)) {
                    long   position;
                    string text;
                    int    n;

                    // make sure that BoundStream will properly reset the underlying stream
                    Assert.AreEqual(0, bounded.Position);
                    n    = bounded.Read(buffer, 0, buffer.Length);
                    text = Encoding.ASCII.GetString(buffer, 0, n);
                    Assert.AreEqual("is some text...", text);

                    position = bounded.Seek(0, SeekOrigin.Begin);
                    Assert.AreEqual(0, position, "SeekOrigin.Begin");
                    n    = bounded.Read(buffer, 0, buffer.Length);
                    text = Encoding.ASCII.GetString(buffer, 0, n);
                    Assert.AreEqual("is some text...", text);

                    position = bounded.Seek(-1 * n, SeekOrigin.Current);
                    Assert.AreEqual(0, position, "SeekOrigin.Current");
                    n    = bounded.Read(buffer, 0, buffer.Length);
                    text = Encoding.ASCII.GetString(buffer, 0, n);
                    Assert.AreEqual("is some text...", text);

                    position = bounded.Seek(-1 * n, SeekOrigin.End);
                    Assert.AreEqual(0, position, "SeekOrigin.End");
                    n    = bounded.Read(buffer, 0, buffer.Length);
                    text = Encoding.ASCII.GetString(buffer, 0, n);
                    Assert.AreEqual("is some text...", text);

                    // now try seeking out of bounds
                    Assert.Throws <IOException> (() => bounded.Seek(-1, SeekOrigin.Begin));
                    Assert.Throws <IOException> (() => bounded.Seek(-1 * buffer.Length, SeekOrigin.End));
                    Assert.Throws <IOException> (() => bounded.Seek(5, SeekOrigin.End));
                }
            }
        }
		public void TestStreamArguments ()
		{
			using (var stream = new MeasuringStream ())
				AssertStreamArguments (stream);

			using (var stream = new MemoryBlockStream ())
				AssertStreamArguments (stream);

			using (var memory = new MemoryStream ()) {
				using (var stream = new FilteredStream (memory))
					AssertStreamArguments (stream);
			}

			using (var memory = new MemoryStream ()) {
				using (var stream = new BoundStream (memory, 0, -1, true))
					AssertStreamArguments (stream);
			}

			using (var memory = new MemoryStream ()) {
				using (var stream = new ChainedStream ()) {
					stream.Add (memory);

					AssertStreamArguments (stream);
				}
			}
		}
Example #14
0
        unsafe BoundaryType ConstructMimePart(MimePart part, byte* inbuf)
        {
            BoundaryType found;
            Stream content;

            if (persistent) {
                long begin = GetOffset (inputIndex);
                long end;

                using (var measured = new MeasuringStream ()) {
                    found = ScanContent (inbuf, measured, true);
                    end = begin + measured.Length;
                }

                content = new BoundStream (stream, begin, end, true);
            } else {
                content = new MemoryBlockStream ();
                found = ScanContent (inbuf, content, true);
                content.Seek (0, SeekOrigin.Begin);
            }

            part.ContentObject = new ContentObject (content, part.ContentTransferEncoding);

            return found;
        }