示例#1
0
 public void TestCanReadWriteSeek()
 {
     using (var stream = new Pop3Stream(new DummyNetworkStream(), new NullProtocolLogger())) {
         Assert.IsTrue(stream.CanRead);
         Assert.IsTrue(stream.CanWrite);
         Assert.IsFalse(stream.CanSeek);
         Assert.IsTrue(stream.CanTimeout);
     }
 }
示例#2
0
 public void TestSeek()
 {
     using (var stream = new Pop3Stream(new DummyNetworkStream(), new NullProtocolLogger())) {
         Assert.Throws <NotSupportedException> (() => stream.Seek(0, SeekOrigin.Begin));
         Assert.Throws <NotSupportedException> (() => stream.Position = 500);
         Assert.AreEqual(0, stream.Position);
         Assert.AreEqual(0, stream.Length);
     }
 }
示例#3
0
        public void TestGetSetTimeouts()
        {
            using (var stream = new Pop3Stream(new DummyNetworkStream(), new NullProtocolLogger())) {
                stream.ReadTimeout = 5;
                Assert.AreEqual(5, stream.ReadTimeout, "ReadTimeout");

                stream.WriteTimeout = 7;
                Assert.AreEqual(7, stream.WriteTimeout, "WriteTimeout");
            }
        }
示例#4
0
        public void TestRead()
        {
            using (var stream = new Pop3Stream(new DummyNetworkStream(), new NullProtocolLogger())) {
                var data   = Encoding.ASCII.GetBytes("+OK\r\n");
                var buffer = new byte[16];

                Assert.Throws <ArgumentNullException> (() => stream.Read(null, 0, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (() => stream.Read(buffer, -1, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (() => stream.Read(buffer, 0, -1));

                stream.Stream.Write(data, 0, data.Length);
                stream.Stream.Position = 0;

                stream.Mode = Pop3StreamMode.Line;
                Assert.Throws <InvalidOperationException> (() => stream.Read(buffer, 0, buffer.Length));

                stream.Mode = Pop3StreamMode.Data;
                var n = stream.Read(buffer, 0, buffer.Length);
                Assert.AreEqual(data.Length, n, "Read");
                Assert.AreEqual("+OK\r\n", Encoding.ASCII.GetString(buffer, 0, n), "Read");
            }
        }
示例#5
0
        public async Task TestWriteAsync()
        {
            using (var stream = new Pop3Stream(new DummyNetworkStream(), new NullProtocolLogger())) {
                var    memory = (MemoryStream)stream.Stream;
                var    buffer = new byte[8192];
                var    buf1k  = new byte[1024];
                var    buf4k  = new byte[4096];
                var    buf9k  = new byte[9216];
                byte[] mem;

                using (var rng = new RNGCryptoServiceProvider()) {
                    rng.GetBytes(buf1k);
                    rng.GetBytes(buf4k);
                    rng.GetBytes(buf9k);
                }

                // Test #1: write less than 4K to make sure that Pop3Stream buffers it
                await stream.WriteAsync(buf1k, 0, buf1k.Length);

                Assert.AreEqual(0, memory.Length, "#1");

                // Test #2: make sure that flushing the Pop3Stream flushes the entire buffer out to the network
                await stream.FlushAsync();

                Assert.AreEqual(buf1k.Length, memory.Length, "#2");
                mem = memory.GetBuffer();
                for (int i = 0; i < buf1k.Length; i++)
                {
                    Assert.AreEqual(buf1k[i], mem[i], "#2 byte[{0}]", i);
                }
                memory.SetLength(0);

                // Test #3: write exactly 4K to make sure it passes through w/o the need to flush
                await stream.WriteAsync(buf4k, 0, buf4k.Length);

                Assert.AreEqual(buf4k.Length, memory.Length, "#3");
                mem = memory.GetBuffer();
                for (int i = 0; i < buf4k.Length; i++)
                {
                    Assert.AreEqual(buf4k[i], mem[i], "#3 byte[{0}]", i);
                }
                memory.SetLength(0);

                // Test #4: write 1k and then write 4k, make sure that only 4k passes thru (last 1k gets buffered)
                await stream.WriteAsync(buf1k, 0, buf1k.Length);

                await stream.WriteAsync(buf4k, 0, buf4k.Length);

                Assert.AreEqual(4096, memory.Length, "#4");
                await stream.FlushAsync();

                Assert.AreEqual(buf1k.Length + buf4k.Length, memory.Length, "#4");
                Array.Copy(buf1k, 0, buffer, 0, buf1k.Length);
                Array.Copy(buf4k, 0, buffer, buf1k.Length, buf4k.Length);
                mem = memory.GetBuffer();
                for (int i = 0; i < buf1k.Length + buf4k.Length; i++)
                {
                    Assert.AreEqual(buffer[i], mem[i], "#4 byte[{0}]", i);
                }
                memory.SetLength(0);

                // Test #5: write 9k and make sure only the first 8k goes thru (last 1k gets buffered)
                await stream.WriteAsync(buf9k, 0, buf9k.Length);

                Assert.AreEqual(8192, memory.Length, "#5");
                await stream.FlushAsync();

                Assert.AreEqual(buf9k.Length, memory.Length, "#5");
                mem = memory.GetBuffer();
                for (int i = 0; i < buf9k.Length; i++)
                {
                    Assert.AreEqual(buf9k[i], mem[i], "#5 byte[{0}]", i);
                }
                memory.SetLength(0);
            }
        }
示例#6
0
 public void TestSetLength()
 {
     using (var stream = new Pop3Stream(new DummyNetworkStream(), new NullProtocolLogger())) {
         Assert.Throws <NotSupportedException> (() => stream.SetLength(500));
     }
 }