Exemple #1
0
        public async void TestReadWriteAsync()
        {
            using (var stream = new CompressedStream(new DummyNetworkStream())) {
                string    command          = "A00000001 APPEND INBOX (\\Seen \\Draft) {4096+}\r\nFrom: Sample Sender <*****@*****.**>\r\nTo: Sample Recipient <*****@*****.**>\r\nSubject: This is a test message...\r\nDate: Mon, 22 Oct 2018 18:22:56 EDT\r\nMessage-Id: <*****@*****.**>\r\n\r\nTesting... 1. 2. 3.\r\nTesting.\r\nOver and out.\r\n";
                var       output           = Encoding.ASCII.GetBytes(command);
                const int compressedLength = 221;
                var       buffer           = new byte[1024];
                int       n;

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

                await stream.FlushAsync();

                Assert.AreEqual(compressedLength, stream.BaseStream.Position, "Compressed output length");

                stream.BaseStream.Position = 0;

                n = await stream.ReadAsync(buffer, 0, buffer.Length);

                Assert.AreEqual(output.Length, n, "Decompressed input length");

                var text = Encoding.ASCII.GetString(buffer, 0, n);
                Assert.AreEqual(command, text);
            }
        }
Exemple #2
0
        public void TestArgumentExceptions()
        {
            using (var stream = new CompressedStream(new DummyNetworkStream())) {
                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));
                Assert.AreEqual(0, stream.Read(buffer, 0, 0));

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

                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));
                stream.Write(buffer, 0, 0);

                Assert.Throws <ArgumentNullException> (async() => await stream.WriteAsync(null, 0, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (async() => await stream.WriteAsync(buffer, -1, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (async() => await stream.WriteAsync(buffer, 0, -1));
            }
        }