예제 #1
0
        public void TestWrite()
        {
            using (var stream = new SmtpStream(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);
                }

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

                // Test #1: write less than 4K to make sure that SmtpStream buffers it
                stream.Write(buf1k, 0, buf1k.Length);
                Assert.AreEqual(0, memory.Length, "#1");

                // Test #2: make sure that flushing the SmtpStream flushes the entire buffer out to the network
                stream.Flush();
                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
                stream.Write(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)
                stream.Write(buf1k, 0, buf1k.Length);
                stream.Write(buf4k, 0, buf4k.Length);
                Assert.AreEqual(4096, memory.Length, "#4");
                stream.Flush();
                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)
                stream.Write(buf9k, 0, buf9k.Length);
                Assert.AreEqual(8192, memory.Length, "#5");
                stream.Flush();
                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);
            }
        }