diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/parser/charsetTest.cpp | 88 | ||||
-rw-r--r-- | tests/parser/emailAddressTest.cpp | 224 | ||||
-rw-r--r-- | tests/parser/mailboxTest.cpp | 2 | ||||
-rw-r--r-- | tests/parser/textTest.cpp | 79 | ||||
-rw-r--r-- | tests/testUtils.hpp | 17 |
5 files changed, 388 insertions, 22 deletions
diff --git a/tests/parser/charsetTest.cpp b/tests/parser/charsetTest.cpp index f07694ca..eaedb218 100644 --- a/tests/parser/charsetTest.cpp +++ b/tests/parser/charsetTest.cpp @@ -105,6 +105,10 @@ VMIME_TEST_SUITE_BEGIN // Test invalid input VMIME_TEST(testFilterInvalid1) + // IDNA + VMIME_TEST(testEncodeIDNA) + VMIME_TEST(testDecodeIDNA) + // TODO: more tests VMIME_TEST_LIST_END @@ -147,14 +151,18 @@ VMIME_TEST_SUITE_BEGIN vmime::string actualOut; vmime::utility::outputStreamStringAdapter osa(actualOut); - vmime::utility::charsetFilteredOutputStream os - (inputCharset, outputCharset, osa); + + vmime::ref <vmime::charsetConverter> conv = + vmime::charsetConverter::create(inputCharset, outputCharset); + + vmime::ref <vmime::utility::charsetFilteredOutputStream> os = + conv->getFilteredOutputStream(osa); vmime::utility::inputStreamStringAdapter is(in); - vmime::utility::bufferedStreamCopy(is, os); + vmime::utility::bufferedStreamCopy(is, *os); - os.flush(); + os->flush(); VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut)); } @@ -167,17 +175,21 @@ VMIME_TEST_SUITE_BEGIN vmime::string actualOut; vmime::utility::outputStreamStringAdapter osa(actualOut); - vmime::utility::charsetFilteredOutputStream os - (inputCharset, outputCharset, osa); + + vmime::ref <vmime::charsetConverter> conv = + vmime::charsetConverter::create(inputCharset, outputCharset); + + vmime::ref <vmime::utility::charsetFilteredOutputStream> os = + conv->getFilteredOutputStream(osa); vmime::utility::inputStreamStringAdapter is(in); vmime::utility::stream::value_type buffer[16]; for (int i = 0 ; !is.eof() ; ++i) - os.write(buffer, is.read(buffer, 1)); + os->write(buffer, is.read(buffer, 1)); - os.flush(); + os->flush(); VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut)); } @@ -190,17 +202,21 @@ VMIME_TEST_SUITE_BEGIN vmime::string actualOut; vmime::utility::outputStreamStringAdapter osa(actualOut); - vmime::utility::charsetFilteredOutputStream os - (inputCharset, outputCharset, osa); + + vmime::ref <vmime::charsetConverter> conv = + vmime::charsetConverter::create(inputCharset, outputCharset); + + vmime::ref <vmime::utility::charsetFilteredOutputStream> os = + conv->getFilteredOutputStream(osa); vmime::utility::inputStreamStringAdapter is(in); vmime::utility::stream::value_type buffer[16]; for (int i = 0 ; !is.eof() ; ++i) - os.write(buffer, is.read(buffer, (i % 5) + 1)); + os->write(buffer, is.read(buffer, (i % 5) + 1)); - os.flush(); + os->flush(); VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut)); } @@ -212,18 +228,23 @@ VMIME_TEST_SUITE_BEGIN vmime::string actualOut; vmime::utility::outputStreamStringAdapter osa(actualOut); - vmime::utility::charsetFilteredOutputStream os - (vmime::charset("utf-8"), - vmime::charset("iso-8859-1"), osa); + + vmime::ref <vmime::charsetConverter> conv = + vmime::charsetConverter::create + (vmime::charset("utf-8"), + vmime::charset("iso-8859-1")); + + vmime::ref <vmime::utility::charsetFilteredOutputStream> os = + conv->getFilteredOutputStream(osa); vmime::utility::inputStreamStringAdapter is(in); vmime::utility::stream::value_type buffer[16]; for (int i = 0 ; !is.eof() ; ++i) - os.write(buffer, is.read(buffer, 1)); + os->write(buffer, is.read(buffer, 1)); - os.flush(); + os->flush(); VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut)); } @@ -276,5 +297,38 @@ VMIME_TEST_SUITE_BEGIN return res; } + static const vmime::string convertHelper + (const vmime::string& in, const vmime::charset& csrc, const vmime::charset& cdest) + { + vmime::string out; + vmime::charset::convert(in, out, csrc, cdest); + + return out; + } + + void testEncodeIDNA() + { + VASSERT_EQ("1", "xn--espaol-zwa", convertHelper("español", "utf-8", "idna")); + + // Tests from ICANN + VASSERT_EQ("2.1", "xn--hxajbheg2az3al", convertHelper("παράδειγμα", "utf-8", "idna")); + VASSERT_EQ("2.2", "xn--jxalpdlp", convertHelper("δοκιμή", "utf-8", "idna")); + + VASSERT_EQ("3.1", "xn--mgbh0fb", convertHelper("مثال", "utf-8", "idna")); + VASSERT_EQ("3.2", "xn--kgbechtv", convertHelper("إختبار", "utf-8", "idna")); + } + + void testDecodeIDNA() + { + VASSERT_EQ("1", "español", convertHelper("xn--espaol-zwa", "idna", "utf-8")); + + // Tests from ICANN + VASSERT_EQ("2.1", "παράδειγμα", convertHelper("xn--hxajbheg2az3al", "idna", "utf-8")); + VASSERT_EQ("2.2", "δοκιμή", convertHelper("xn--jxalpdlp", "idna", "utf-8")); + + VASSERT_EQ("3.1", "مثال", convertHelper("xn--mgbh0fb", "idna", "utf-8")); + VASSERT_EQ("3.2", "إختبار", convertHelper("xn--kgbechtv", "idna", "utf-8")); + } + VMIME_TEST_SUITE_END diff --git a/tests/parser/emailAddressTest.cpp b/tests/parser/emailAddressTest.cpp new file mode 100644 index 00000000..0185c220 --- /dev/null +++ b/tests/parser/emailAddressTest.cpp @@ -0,0 +1,224 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2013 Vincent Richard <[email protected]> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/platform.hpp" + + +#define VMIME_TEST_SUITE emailAddressTest +#define VMIME_TEST_SUITE_MODULE "Parser" + + +VMIME_TEST_SUITE_BEGIN + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testParseASCII) + VMIME_TEST(testParseEAI) + VMIME_TEST(testParseInvalid) + VMIME_TEST(testGenerateASCII) + VMIME_TEST(testGenerateEAI) + VMIME_TEST(testParseSpecialChars) + VMIME_TEST(testParseCommentInLocalPart) + VMIME_TEST(testParseCommentInDomainPart) + VMIME_TEST(testGenerateSpecialChars) + VMIME_TEST_LIST_END + + + void testParseASCII() + { + vmime::emailAddress eml1("local@domain"); + VASSERT_EQ("1/local", "local", eml1.getLocalName()); + VASSERT_EQ("1/domain", "domain", eml1.getDomainName()); + + // When not specified, domain should be local host name + vmime::emailAddress eml2("local"); + VASSERT_EQ("2/local", "local", eml2.getLocalName()); + VASSERT_EQ("2/domain", vmime::platform::getHandler()->getHostName(), eml2.getDomainName()); + } + + void testParseEAI() + { + // Examples taken from Wikipedia (http://en.wikipedia.org/wiki/Email_address) + + // Latin Alphabet (with diacritics): + vmime::emailAddress eml1("Pelé@example.com"); + VASSERT_EQ("1/local", "Pelé", eml1.getLocalName()); + VASSERT_EQ("1/domain", "example.com", eml1.getDomainName()); + + // Greek Alphabet + vmime::emailAddress eml2("δοκιμή@παράδειγμα.δοκιμή"); + VASSERT_EQ("2/local", "δοκιμή", eml2.getLocalName()); + VASSERT_EQ("2/domain", "παράδειγμα.δοκιμή", eml2.getDomainName()); + + // Japanese Characters + vmime::emailAddress eml3("甲斐@黒川.日本"); + VASSERT_EQ("3/local", "甲斐", eml3.getLocalName()); + VASSERT_EQ("3/domain", "黒川.日本", eml3.getDomainName()); + + // Cyrillic Characters + vmime::emailAddress eml4("чебурашка@ящик-с-апельсинами.рф"); + VASSERT_EQ("4/local", "чебурашка", eml4.getLocalName()); + VASSERT_EQ("4/domain", "ящик-с-апельсинами.рф", eml4.getDomainName()); + } + + void testParseInvalid() + { + // Only one @ is allowed outside quotation marks + vmime::emailAddress eml1("local@part@domain"); + VASSERT_EQ("1/local", "local", eml1.getLocalName()); + VASSERT_EQ("1/domain", "part@domain", eml1.getDomainName()); + + // Quoted strings must be dot separated, or the only element making up + // the local-part: should be parsed correctly, but it still represents + // an invalid email address + vmime::emailAddress eml2("Just\"not\"[email protected]"); + VASSERT_EQ("2/local", "Just\"not\"right", eml2.getLocalName()); + VASSERT_EQ("2/domain", "example.com", eml2.getDomainName()); + + // An @ character must separate the local and domain parts + vmime::emailAddress eml3("Abc.example.com"); + VASSERT_EQ("3/local", "Abc.example.com", eml3.getLocalName()); + VASSERT_EQ("3/domain", vmime::platform::getHandler()->getHostName(), eml3.getDomainName()); + + // Whitespace must be escaped + vmime::emailAddress eml4("local part@domain"); + VASSERT_EQ("4/local", "local", eml4.getLocalName()); + VASSERT_EQ("4/domain", vmime::platform::getHandler()->getHostName(), eml4.getDomainName()); + + vmime::emailAddress eml5("this\\ still\\\"not\\\\[email protected]"); + VASSERT_EQ("5/local", "this\\", eml5.getLocalName()); + VASSERT_EQ("5/domain", vmime::platform::getHandler()->getHostName(), eml5.getDomainName()); + } + + void testParseSpecialChars() + { + // Examples taken from Wikipedia (http://en.wikipedia.org/wiki/Email_address) + + vmime::emailAddress eml1("\" \"@example.org"); + VASSERT_EQ("1/local", " ", eml1.getLocalName()); + VASSERT_EQ("1/domain", "example.org", eml1.getDomainName()); + + vmime::emailAddress eml2("\"()<>[]:,;@\\\\\\\"!#$%&'*+-/=?^_`{}| ~.a\"@example.org"); + VASSERT_EQ("2/local", "()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a", eml2.getLocalName()); + VASSERT_EQ("3/domain", "example.org", eml2.getDomainName()); + + vmime::emailAddress eml3("!#$%&'*+-/=?^_`{}|[email protected]"); + VASSERT_EQ("3/local", "!#$%&'*+-/=?^_`{}|~", eml3.getLocalName()); + VASSERT_EQ("3/domain", "example.org", eml3.getDomainName()); + + vmime::emailAddress eml4("!#$%&'*+-/=?^_`{}|[email protected]"); + VASSERT_EQ("4/local", "!#$%&'*+-/=?^_`{}|~", eml4.getLocalName()); + VASSERT_EQ("4/domain", "example.org", eml4.getDomainName()); + + vmime::emailAddress eml5("\"[email protected]\"@example.com"); + VASSERT_EQ("5/local", "[email protected]", eml5.getLocalName()); + VASSERT_EQ("5/domain", "example.com", eml5.getDomainName()); + + vmime::emailAddress eml6("\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@strange.example.com"); + VASSERT_EQ("6/local", "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual", eml6.getLocalName()); + VASSERT_EQ("6/domain", "strange.example.com", eml6.getDomainName()); + } + + void testParseCommentInLocalPart() + { + vmime::emailAddress eml1("john.smith(comment)@example.com"); + VASSERT_EQ("1/local", "john.smith", eml1.getLocalName()); + VASSERT_EQ("1/domain", "example.com", eml1.getDomainName()); + + vmime::emailAddress eml2("(comment)[email protected]"); + VASSERT_EQ("2/local", "john.smith", eml2.getLocalName()); + VASSERT_EQ("2/domain", "example.com", eml2.getDomainName()); + + vmime::emailAddress eml3("(comment (comment in comment))[email protected]"); + VASSERT_EQ("3/local", "john.smith", eml3.getLocalName()); + VASSERT_EQ("3/domain", "example.com", eml3.getDomainName()); + + vmime::emailAddress eml4("(comment \\) end comment)[email protected]"); + VASSERT_EQ("4/local", "john.smith", eml4.getLocalName()); + VASSERT_EQ("4/domain", "example.com", eml4.getDomainName()); + } + + void testParseCommentInDomainPart() + { + vmime::emailAddress eml1("john.smith@(comment)example.com"); + VASSERT_EQ("1/local", "john.smith", eml1.getLocalName()); + VASSERT_EQ("1/domain", "example.com", eml1.getDomainName()); + + vmime::emailAddress eml2("[email protected](comment)"); + VASSERT_EQ("2/local", "john.smith", eml2.getLocalName()); + VASSERT_EQ("2/domain", "example.com", eml2.getDomainName()); + + vmime::emailAddress eml3("john.smith@(comment (comment in comment))example.com"); + VASSERT_EQ("3/local", "john.smith", eml3.getLocalName()); + VASSERT_EQ("3/domain", "example.com", eml3.getDomainName()); + + vmime::emailAddress eml4("john.smith@(comment \\) end comment)example.com"); + VASSERT_EQ("4/local", "john.smith", eml4.getLocalName()); + VASSERT_EQ("4/domain", "example.com", eml4.getDomainName()); + } + + void testGenerateASCII() + { + VASSERT_EQ("email 1", "local@domain", vmime::emailAddress("local", "domain").generate()); + + VASSERT_EQ("email 2", "[email protected]", + vmime::emailAddress("Pelé", "example.com").generate()); + VASSERT_EQ("email 3", "[email protected]", + vmime::emailAddress("甲斐", "黒川.日本").generate()); + VASSERT_EQ("email 4", "[email protected]", + vmime::emailAddress("mailtest", "例え.テスト").generate()); + VASSERT_EQ("email 5", "[email protected]", + vmime::emailAddress("mailtest", "مثال.إختبار").generate()); + } + + void testGenerateEAI() + { + vmime::generationContext ctx(vmime::generationContext::getDefaultContext()); + ctx.setInternationalizedEmailSupport(true); + + vmime::generationContext::switcher <vmime::generationContext> contextSwitcher(ctx); + + VASSERT_EQ("email 1", "Pelé@example.com", + vmime::emailAddress("Pelé", "example.com").generate()); + VASSERT_EQ("email 2", "δοκιμή@παράδειγμα.δοκιμή", + vmime::emailAddress("δοκιμή", "παράδειγμα.δοκιμή").generate()); + VASSERT_EQ("email 3", "甲斐@黒川.日本", + vmime::emailAddress("甲斐", "黒川.日本").generate()); + VASSERT_EQ("email 4", "чебурашка@ящик-с-апельсинами.рф", + vmime::emailAddress("чебурашка", "ящик-с-апельсинами.рф").generate()); + } + + void testGenerateSpecialChars() + { + VASSERT_EQ("email 1", "\"[email protected]\"@example.com", + vmime::emailAddress("[email protected]", "example.com").generate()); + + VASSERT_EQ("email 2", "\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@strange.example.com", + vmime::emailAddress("very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual", "strange.example.com").generate()); + + VASSERT_EQ("email 3", "\" \"@example.com", + vmime::emailAddress(" ", "example.com").generate()); + } + +VMIME_TEST_SUITE_END diff --git a/tests/parser/mailboxTest.cpp b/tests/parser/mailboxTest.cpp index 7de7abd3..9e48a235 100644 --- a/tests/parser/mailboxTest.cpp +++ b/tests/parser/mailboxTest.cpp @@ -61,7 +61,7 @@ VMIME_TEST_SUITE_BEGIN "[address-list: [[mailbox: name=[text: []], [email protected]]]]", // Test 5 - "John.Doe (ignore) @acme.com (John Doe)", + "John.Doe(ignore)@acme.com (John Doe)", "[address-list: [[mailbox: name=[text: []], [email protected]]]]", diff --git a/tests/parser/textTest.cpp b/tests/parser/textTest.cpp index 37c62072..d2047f02 100644 --- a/tests/parser/textTest.cpp +++ b/tests/parser/textTest.cpp @@ -55,6 +55,11 @@ VMIME_TEST_SUITE_BEGIN VMIME_TEST(testForcedNonEncoding) VMIME_TEST(testBugFix20110511) + + VMIME_TEST(testInternationalizedEmail_specialChars) + VMIME_TEST(testInternationalizedEmail_UTF8) + VMIME_TEST(testInternationalizedEmail_nonUTF8) + VMIME_TEST(testInternationalizedEmail_folding) VMIME_TEST_LIST_END @@ -355,19 +360,22 @@ VMIME_TEST_SUITE_BEGIN std::string str; vmime::utility::outputStreamStringAdapter os(str); + vmime::generationContext ctx; + ctx.setMaxLineLength(1000); + // ASCII-only text is quotable str.clear(); - vmime::word("Quoted text").generate(os, 1000, 0, NULL, vmime::text::QUOTE_IF_POSSIBLE, NULL); + vmime::word("Quoted text").generate(ctx, os, 0, NULL, vmime::text::QUOTE_IF_POSSIBLE, NULL); VASSERT_EQ("1", "\"Quoted text\"", cleanGeneratedWords(str)); // Text with CR/LF is not quotable str.clear(); - vmime::word("Non-quotable\ntext", "us-ascii").generate(os, 1000, 0, NULL, vmime::text::QUOTE_IF_POSSIBLE, NULL); + vmime::word("Non-quotable\ntext", "us-ascii").generate(ctx, os, 0, NULL, vmime::text::QUOTE_IF_POSSIBLE, NULL); VASSERT_EQ("2", "=?us-ascii?Q?Non-quotable=0Atext?=", cleanGeneratedWords(str)); // Text with non-ASCII chars is not quotable str.clear(); - vmime::word("Non-quotable text \xc3\xa9").generate(os, 1000, 0, NULL, vmime::text::QUOTE_IF_POSSIBLE, NULL); + vmime::word("Non-quotable text \xc3\xa9").generate(ctx, os, 0, NULL, vmime::text::QUOTE_IF_POSSIBLE, NULL); VASSERT_EQ("3", "=?UTF-8?Q?Non-quotable_text_=C3=A9?=", cleanGeneratedWords(str)); } @@ -493,5 +501,70 @@ VMIME_TEST_SUITE_BEGIN VASSERT_EQ("decode2", DECODED_TEXT, t.getWholeBuffer()); } + void testInternationalizedEmail_specialChars() + { + vmime::generationContext ctx(vmime::generationContext::getDefaultContext()); + ctx.setInternationalizedEmailSupport(true); + + vmime::generationContext::switcher <vmime::generationContext> contextSwitcher(ctx); + + // Special sequence/chars should still be encoded + VASSERT_EQ("1", "=?us-ascii?Q?Test=3D=3Frfc2047_sequence?=", + vmime::word("Test=?rfc2047 sequence", vmime::charset("us-ascii")).generate()); + + VASSERT_EQ("2", "=?us-ascii?Q?Line_One=0ALine_Two?=", + vmime::word("Line One\nLine Two", vmime::charset("us-ascii")).generate()); + } + + void testInternationalizedEmail_UTF8() + { + vmime::generationContext ctx(vmime::generationContext::getDefaultContext()); + ctx.setInternationalizedEmailSupport(true); + + vmime::generationContext::switcher <vmime::generationContext> contextSwitcher(ctx); + + // Already UTF-8 encoded text should be left as is + VASSERT_EQ("1", "Achim Br\xc3\xa4ndt", + vmime::word("Achim Br\xc3\xa4ndt", vmime::charset("utf-8")).generate()); + } + + void testInternationalizedEmail_nonUTF8() + { + vmime::generationContext ctx(vmime::generationContext::getDefaultContext()); + ctx.setInternationalizedEmailSupport(true); + + vmime::generationContext::switcher <vmime::generationContext> contextSwitcher(ctx); + + // Non UTF-8 encoded text should first be converted to UTF-8 + VASSERT_EQ("1", "Achim Br\xc3\xa4ndt", + vmime::word("Achim Br\xe4ndt", vmime::charset("iso-8859-1")).generate()); + } + + void testInternationalizedEmail_folding() + { + vmime::generationContext ctx(vmime::generationContext::getDefaultContext()); + ctx.setInternationalizedEmailSupport(true); + + vmime::generationContext::switcher <vmime::generationContext> contextSwitcher(ctx); + + // RFC-2047 encoding must be performed, as line folding is needed + vmime::word w1("01234567890123456789\xc3\xa0x012345678901234567890123456789" + "01234567890123456789\xc3\xa0x012345678901234567890123456789", vmime::charset("utf-8")); + + VASSERT_EQ("1", + "=?utf-8?Q?01234567890123456789=C3=A0x01234567890?=\r\n" + " =?utf-8?Q?1234567890123456789012345678901234567?=\r\n" + " =?utf-8?Q?89=C3=A0x0123456789012345678901234567?=\r\n" + " =?utf-8?Q?89?=", w1.generate(50)); + + // RFC-2047 encoding will not be forced, as words can be wrapped in a new line + vmime::word w2("bla bla bla This is some '\xc3\xa0\xc3\xa7' UTF-8 encoded text", vmime::charset("utf-8")); + + VASSERT_EQ("2", + "bla bla bla This is\r\n" + " some '\xc3\xa0\xc3\xa7' UTF-8\r\n" + " encoded text", w2.generate(20)); + } + VMIME_TEST_SUITE_END diff --git a/tests/testUtils.hpp b/tests/testUtils.hpp index 55c0424e..39d9e555 100644 --- a/tests/testUtils.hpp +++ b/tests/testUtils.hpp @@ -107,6 +107,13 @@ inline std::ostream& operator<<(std::ostream& os, const vmime::charset& ch) } +inline std::ostream& operator<<(std::ostream& os, const vmime::word& w) +{ + os << "[word: charset=" << w.getCharset().getName() << ", buffer=" << w.getBuffer() << "]"; + return (os); +} + + inline std::ostream& operator<<(std::ostream& os, const vmime::text& txt) { os << "[text: ["; @@ -118,7 +125,7 @@ inline std::ostream& operator<<(std::ostream& os, const vmime::text& txt) if (i != 0) os << ","; - os << "[word: charset=" << w.getCharset().getName() << ", buffer=" << w.getBuffer() << "]"; + os << w; } os << "]]"; @@ -127,6 +134,14 @@ inline std::ostream& operator<<(std::ostream& os, const vmime::text& txt) } +inline std::ostream& operator<<(std::ostream& os, const vmime::emailAddress& email) +{ + os << email.generate(); + + return (os); +} + + inline std::ostream& operator<<(std::ostream& os, const vmime::mailbox& mbox) { os << "[mailbox: name=" << mbox.getName() << ", email=" << mbox.getEmail() << "]"; |