Exemple #1
0
        public void TestParseMultipartBodyStructureWithoutBodyFldDsp()
        {
            // Test case from https://stackoverflow.com/questions/33481604/mailkit-fetch-unexpected-token-in-imap-response-qstring-multipart-message
            const string text = "((\"text\" \"plain\" (\"charset\" \"UTF-8\") NIL \"Message text\" \"Quoted-printable\" 209 6 NIL (\"inline\" NIL) NIL NIL)(\"text\" \"xml\" (\"name\" \"4441004299066.xml\") NIL \"4441004299066.xml\" \"Base64\" 10642 137 NIL (\"inline\" (\"filename\" \"4441004299066.xml\")) NIL NIL)(\"application\" \"pdf\" (\"name\" \"4441004299066.pdf\") NIL \"4441004299066.pdf\" \"Base64\" 48448 NIL (\"inline\" (\"filename\" \"4441004299066.pdf\")) NIL NIL) \"mixed\" (\"boundary\" \"6624CFB2_17170C36_Synapse_boundary\") \"Multipart message\" 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)) {
                        BodyPartMultipart multipart;
                        BodyPartBasic     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 BODYSTRUCTURE 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 <BodyPartMultipart> (body, "Body types did not match.");
                        multipart = (BodyPartMultipart)body;

                        Assert.IsTrue(body.ContentType.IsMimeType("multipart", "mixed"), "Content-Type did not match.");
                        Assert.AreEqual("6624CFB2_17170C36_Synapse_boundary", body.ContentType.Parameters ["boundary"], "boundary param did not match");
                        Assert.AreEqual(3, multipart.BodyParts.Count, "BodyParts count does not match.");

                        Assert.IsInstanceOf <BodyPartText> (multipart.BodyParts[0], "The type of the first child does not match.");
                        basic = (BodyPartBasic)multipart.BodyParts[0];
                        Assert.AreEqual("plain", basic.ContentType.MediaSubtype, "Content-Type did not match.");
                        Assert.AreEqual("Message text", basic.ContentDescription, "Content-Description does not match.");

                        Assert.IsInstanceOf <BodyPartText> (multipart.BodyParts[1], "The type of the second child does not match.");
                        basic = (BodyPartBasic)multipart.BodyParts[1];
                        Assert.AreEqual("xml", basic.ContentType.MediaSubtype, "Content-Type did not match.");
                        Assert.AreEqual("4441004299066.xml", basic.ContentDescription, "Content-Description does not match.");

                        Assert.IsInstanceOf <BodyPartBasic> (multipart.BodyParts[2], "The type of the third child does not match.");
                        basic = (BodyPartBasic)multipart.BodyParts[2];
                        Assert.AreEqual("application", basic.ContentType.MediaType, "Content-Type did not match.");
                        Assert.AreEqual("pdf", basic.ContentType.MediaSubtype, "Content-Type did not match.");
                        Assert.AreEqual("4441004299066.pdf", basic.ContentDescription, "Content-Description does not match.");
                    }
                }
            }
        }
Exemple #2
0
        public void TestParseMultipartBodyStructureWithNilBodyFldParam()
        {
            const string text = "(((\"text\" \"plain\" (\"charset\" \"UTF-8\") NIL NIL \"7bit\" 148 12 NIL NIL NIL NIL)(\"text\" \"html\" (\"charset\" \"UTF-8\") NIL NIL \"quoted-printable\" 337 6 NIL NIL NIL NIL) \"alternative\" (\"boundary\" \"6c7f221bed92d80548353834d8e2\") NIL NIL NIL)((\"text\" \"plain\" (\"charset\" \"us-ascii\") NIL NIL \"7bit\" 0 0) \"x-zip\" NIL (\"attachment\" (\"filename\" \"YSOZ 265230.ZIP\")) NIL NIL) \"mixed\" (\"boundary\" \"c52bbfc0dd5365efa39b9f80eac3\") NIL NIL 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)) {
                        BodyPartMultipart multipart, alternative, xzip;
                        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 BODYSTRUCTURE 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 <BodyPartMultipart> (body, "Body types did not match.");
                        multipart = (BodyPartMultipart)body;

                        Assert.IsTrue(body.ContentType.IsMimeType("multipart", "mixed"), "Content-Type did not match.");
                        Assert.AreEqual("c52bbfc0dd5365efa39b9f80eac3", body.ContentType.Parameters["boundary"], "boundary param did not match");
                        Assert.AreEqual(2, multipart.BodyParts.Count, "BodyParts count does not match.");

                        Assert.IsInstanceOf <BodyPartMultipart> (multipart.BodyParts[0], "The type of the first child does not match.");
                        alternative = (BodyPartMultipart)multipart.BodyParts[0];
                        Assert.AreEqual("alternative", alternative.ContentType.MediaSubtype, "Content-Type did not match.");

                        Assert.IsInstanceOf <BodyPartMultipart> (multipart.BodyParts[1], "The type of the second child does not match.");
                        xzip = (BodyPartMultipart)multipart.BodyParts[1];
                        Assert.AreEqual("x-zip", xzip.ContentType.MediaSubtype, "Content-Type did not match.");
                        Assert.AreEqual(0, xzip.ContentType.Parameters.Count, "Content-Type should not have params.");
                    }
                }
            }
        }
Exemple #3
0
        public void TestParseBadlyQuotedBodyStructure()
        {
            const string text = "((\"MOUNDARY=\"_006_5DBB50A5A54730AD4A54730AD4A54730AD4A54730AD42KOS_\"\" \"OCTET-STREAM\" (\"name\" \"test.dat\") NIL NIL \"quoted-printable\" 383137 NIL (\"attachment\" (\"filename\" \"test.dat\")))(\"MOUNDARY=\"_006_5DBB50A5D3ABEC4E85A03EAD527CA5474B3D0AF9E6EXMBXSVR02KOS_\"\" \"OCTET-STREAM\" (\"name\" \"test.dat\") NIL NIL \"quoted-printable\" 383137 NIL (\"attachment\" (\"filename\" \"test.dat\"))) \"MIXED\" (\"boundary\" \"----=_NextPart_000_730AD4A547.730AD4A547F40\"))\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)) {
                        BodyPartMultipart multipart;
                        BodyPartBasic     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 BODYSTRUCTURE 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 <BodyPartMultipart> (body, "Body types did not match.");
                        multipart = (BodyPartMultipart)body;

                        Assert.IsTrue(body.ContentType.IsMimeType("multipart", "mixed"), "Content-Type did not match.");
                        Assert.AreEqual("----=_NextPart_000_730AD4A547.730AD4A547F40", body.ContentType.Parameters ["boundary"], "boundary param did not match");
                        Assert.AreEqual(2, multipart.BodyParts.Count, "BodyParts count does not match.");

                        Assert.IsInstanceOf <BodyPartBasic> (multipart.BodyParts[0], "The type of the first child does not match.");
                        basic = (BodyPartBasic)multipart.BodyParts[0];
                        Assert.AreEqual("MOUNDARY=\"_006_5DBB50A5A54730AD4A54730AD4A54730AD4A54730AD42KOS_\"", basic.ContentType.MediaType, "ContentType.MediaType does not match for first child.");

                        Assert.IsInstanceOf <BodyPartBasic> (multipart.BodyParts[1], "The type of the second child does not match.");
                        basic = (BodyPartBasic)multipart.BodyParts[1];
                        Assert.AreEqual("MOUNDARY=\"_006_5DBB50A5D3ABEC4E85A03EAD527CA5474B3D0AF9E6EXMBXSVR02KOS_\"", basic.ContentType.MediaType, "ContentType.MediaType does not match for second child.");
                    }
                }
            }
        }
Exemple #4
0
        public void TestParseExampleMultiLevelDovecotBodyStructure()
        {
            const string text = "(((\"text\" \"plain\" (\"charset\" \"iso-8859-2\") NIL NIL \"quoted-printable\" 28 2 NIL NIL NIL NIL) (\"text\" \"html\" (\"charset\" \"iso-8859-2\") NIL NIL \"quoted-printable\" 1707 65 NIL NIL NIL NIL) \"alternative\" (\"boundary\" \"----=_NextPart_001_0078_01CBB179.57530990\") NIL NIL NIL) (\"message\" \"rfc822\" NIL NIL NIL \"7bit\" 641 (\"Sat, 8 Jan 2011 14:16:36 +0100\" \"Subj 2\" ((\"Some Name, SOMECOMPANY\" NIL \"recipient\" \"example.com\")) ((\"Some Name, SOMECOMPANY\" NIL \"recipient\" \"example.com\")) ((\"Some Name, SOMECOMPANY\" NIL \"recipient\" \"example.com\")) ((\"Recipient\" NIL \"example\" \"gmail.com\")) NIL NIL NIL NIL) (\"text\" \"plain\" (\"charset\" \"iso-8859-2\") NIL NIL \"quoted-printable\" 185 18 NIL NIL (\"cs\") NIL) 31 NIL (\"attachment\" NIL) NIL NIL) (\"message\" \"rfc822\" NIL NIL NIL \"7bit\" 50592 (\"Sat, 8 Jan 2011 13:58:39 +0100\" \"Subj 1\" ((\"Some Name, SOMECOMPANY\" NIL \"recipient\" \"example.com\")) ((\"Some Name, SOMECOMPANY\" NIL \"recipient\" \"example.com\")) ((\"Some Name, SOMECOMPANY\" NIL \"recipient\" \"example.com\")) ((\"Recipient\" NIL \"example\" \"gmail.com\")) NIL NIL NIL NIL) ( (\"text\" \"plain\" (\"charset\" \"iso-8859-2\") NIL NIL \"quoted-printable\" 4296 345 NIL NIL NIL NIL) (\"text\" \"html\" (\"charset\" \"iso-8859-2\") NIL NIL \"quoted-printable\" 45069 1295 NIL NIL NIL NIL) \"alternative\" (\"boundary\" \"----=_NextPart_000_0073_01CBB179.57530990\") NIL (\"cs\") NIL) 1669 NIL (\"attachment\" NIL) NIL NIL) \"mixed\" (\"boundary\" \"----=_NextPart_000_0077_01CBB179.57530990\") NIL (\"cs\") 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)) {
                        BodyPartMultipart multipart;
                        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 BODYSTRUCTURE 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 <BodyPartMultipart> (body, "Body types did not match.");
                        multipart = (BodyPartMultipart)body;

                        Assert.IsTrue(body.ContentType.IsMimeType("multipart", "mixed"), "Content-Type did not match.");
                        Assert.AreEqual("----=_NextPart_000_0077_01CBB179.57530990", body.ContentType.Parameters["boundary"], "boundary param did not match");
                        Assert.AreEqual(3, multipart.BodyParts.Count, "BodyParts count does not match.");
                        Assert.IsInstanceOf <BodyPartMultipart> (multipart.BodyParts[0], "The type of the first child does not match.");
                        Assert.IsInstanceOf <BodyPartMessage> (multipart.BodyParts[1], "The type of the second child does not match.");
                        Assert.IsInstanceOf <BodyPartMessage> (multipart.BodyParts[2], "The type of the third child does not match.");

                        // FIXME: assert more stuff?
                    }
                }
            }
        }
Exemple #5
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.");
                    }
                }
            }
        }