Пример #1
0
        public void TestParseExampleBodyRfc3501()
        {
            const string text = "(\"TEXT\" \"PLAIN\" (\"CHARSET\" \"US-ASCII\") NIL NIL \"7BIT\" 3028 92)\r\n";

            using (var memory = new MemoryStream(Encoding.ASCII.GetBytes(text), false)) {
                using (var tokenizer = new ImapStream(memory, null, new NullProtocolLogger())) {
                    using (var engine = new ImapEngine(null)) {
                        BodyPartText basic;
                        BodyPart     body;

                        engine.SetStream(tokenizer);

                        try {
                            body = ImapUtils.ParseBodyAsync(engine, "Unexpected token: {0}", string.Empty, false, CancellationToken.None).GetAwaiter().GetResult();
                        } catch (Exception ex) {
                            Assert.Fail("Parsing BODY failed: {0}", ex);
                            return;
                        }

                        var token = engine.ReadToken(CancellationToken.None);
                        Assert.AreEqual(ImapTokenType.Eoln, token.Type, "Expected new-line, but got: {0}", token);

                        Assert.IsInstanceOf <BodyPartText> (body, "Body types did not match.");
                        basic = (BodyPartText)body;

                        Assert.IsTrue(body.ContentType.IsMimeType("text", "plain"), "Content-Type did not match.");
                        Assert.AreEqual("US-ASCII", body.ContentType.Parameters["charset"], "charset param did not match");

                        Assert.IsNotNull(basic, "The parsed body is not BodyPartText.");
                        Assert.AreEqual("7BIT", basic.ContentTransferEncoding, "Content-Transfer-Encoding did not match.");
                        Assert.AreEqual(3028, basic.Octets, "Octet count did not match.");
                        Assert.AreEqual(92, basic.Lines, "Line count did not match.");
                    }
                }
            }
        }
Пример #2
0
        public void TestParseLabelsListWithNIL()
        {
            const string text = "(atom-label \\flag-label \"quoted-label\" NIL)\r\n";

            using (var memory = new MemoryStream(Encoding.ASCII.GetBytes(text), false)) {
                using (var tokenizer = new ImapStream(memory, null, new NullProtocolLogger())) {
                    using (var engine = new ImapEngine(null)) {
                        IList <string> labels;

                        engine.SetStream(tokenizer);

                        try {
                            labels = ImapUtils.ParseLabelsListAsync(engine, false, CancellationToken.None).GetAwaiter().GetResult();
                        } catch (Exception ex) {
                            Assert.Fail("Parsing X-GM-LABELS failed: {0}", ex);
                            return;
                        }

                        var token = engine.ReadToken(CancellationToken.None);
                        Assert.AreEqual(ImapTokenType.Eoln, token.Type, "Expected new-line, but got: {0}", token);
                    }
                }
            }
        }
Пример #3
0
        public async void TestWriteAsync()
        {
            using (var stream = new ImapStream(new DummyNetworkStream(), null, 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 ImapStream buffers it
                await stream.WriteAsync(buf1k, 0, buf1k.Length);

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

                // Test #2: make sure that flushing the ImapStream 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);
            }
        }
Пример #4
0
 public void TestSetLength()
 {
     using (var stream = new ImapStream(new DummyNetworkStream(), null, new NullProtocolLogger())) {
         Assert.Throws <NotSupportedException> (() => stream.SetLength(500));
     }
 }
Пример #5
0
        public async void TestReadTokenAsync()
        {
            using (var stream = new ImapStream(new DummyNetworkStream(), null, new NullProtocolLogger())) {
                var data = Encoding.ASCII.GetBytes("* atom (\\flag \"qstring\" NIL) [] \r\n");

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

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

                var token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.Asterisk, token.Type);
                Assert.AreEqual("'*'", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.Atom, token.Type);
                Assert.AreEqual("[atom: atom]", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.OpenParen, token.Type);
                Assert.AreEqual("'('", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.Flag, token.Type);
                Assert.AreEqual("[flag: \\flag]", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.QString, token.Type);
                Assert.AreEqual("[qstring: \"qstring\"]", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.Nil, token.Type);
                Assert.AreEqual("NIL", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.CloseParen, token.Type);
                Assert.AreEqual("')'", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.OpenBracket, token.Type);
                Assert.AreEqual("'['", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.CloseBracket, token.Type);
                Assert.AreEqual("']'", token.ToString());

                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.Eoln, token.Type);
                Assert.AreEqual("'\\n'", token.ToString());

                stream.UngetToken(token);
                token = await stream.ReadTokenAsync(CancellationToken.None);

                Assert.AreEqual(ImapTokenType.Eoln, token.Type);
                Assert.AreEqual("'\\n'", token.ToString());
            }
        }