Mercurial > thymian
comparison 3rdparty/vmime/tests/parser/emailAddressTest.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 // | |
| 2 // VMime library (http://www.vmime.org) | |
| 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org> | |
| 4 // | |
| 5 // This program is free software; you can redistribute it and/or | |
| 6 // modify it under the terms of the GNU General Public License as | |
| 7 // published by the Free Software Foundation; either version 3 of | |
| 8 // the License, or (at your option) any later version. | |
| 9 // | |
| 10 // This program is distributed in the hope that it will be useful, | |
| 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 // General Public License for more details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU General Public License along | |
| 16 // with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 // | |
| 19 // Linking this library statically or dynamically with other modules is making | |
| 20 // a combined work based on this library. Thus, the terms and conditions of | |
| 21 // the GNU General Public License cover the whole combination. | |
| 22 // | |
| 23 | |
| 24 #include "tests/testUtils.hpp" | |
| 25 | |
| 26 #include "vmime/platform.hpp" | |
| 27 | |
| 28 #include <locale> | |
| 29 #include <clocale> | |
| 30 | |
| 31 | |
| 32 VMIME_TEST_SUITE_BEGIN(emailAddressTest) | |
| 33 | |
| 34 VMIME_TEST_LIST_BEGIN | |
| 35 VMIME_TEST(testParseASCII) | |
| 36 VMIME_TEST(testParseEAI) | |
| 37 VMIME_TEST(testParseInvalid) | |
| 38 VMIME_TEST(testGenerateASCII) | |
| 39 VMIME_TEST(testGenerateEAI) | |
| 40 VMIME_TEST(testParseSpecialChars) | |
| 41 VMIME_TEST(testParseCommentInLocalPart) | |
| 42 VMIME_TEST(testParseCommentInDomainPart) | |
| 43 VMIME_TEST(testGenerateSpecialChars) | |
| 44 VMIME_TEST_LIST_END | |
| 45 | |
| 46 | |
| 47 void setUp() | |
| 48 { | |
| 49 // Set the global C and C++ locale to the user-configured locale. | |
| 50 // The locale should use UTF-8 encoding for these tests to run successfully. | |
| 51 try | |
| 52 { | |
| 53 std::locale::global(std::locale("")); | |
| 54 } | |
| 55 catch (std::exception &) | |
| 56 { | |
| 57 std::setlocale(LC_ALL, ""); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 void tearDown() | |
| 62 { | |
| 63 // Restore default locale | |
| 64 std::locale::global(std::locale("C")); | |
| 65 } | |
| 66 | |
| 67 | |
| 68 void testParseASCII() | |
| 69 { | |
| 70 vmime::emailAddress eml1("local@domain"); | |
| 71 VASSERT_EQ("1/local", "local", eml1.getLocalName()); | |
| 72 VASSERT_EQ("1/domain", "domain", eml1.getDomainName()); | |
| 73 | |
| 74 // When not specified, domain should be local host name | |
| 75 vmime::emailAddress eml2("local"); | |
| 76 VASSERT_EQ("2/local", "local", eml2.getLocalName()); | |
| 77 VASSERT_EQ("2/domain", vmime::platform::getHandler()->getHostName(), eml2.getDomainName()); | |
| 78 } | |
| 79 | |
| 80 void testParseEAI() | |
| 81 { | |
| 82 // Examples taken from Wikipedia (http://en.wikipedia.org/wiki/Email_address) | |
| 83 | |
| 84 // Latin Alphabet (with diacritics): | |
| 85 vmime::emailAddress eml1("Pelé@example.com"); | |
| 86 VASSERT_EQ("1/local", "Pelé", eml1.getLocalName()); | |
| 87 VASSERT_EQ("1/domain", "example.com", eml1.getDomainName()); | |
| 88 | |
| 89 // Greek Alphabet | |
| 90 vmime::emailAddress eml2("δοκιμή@παράδειγμα.δοκιμή"); | |
| 91 VASSERT_EQ("2/local", "δοκιμή", eml2.getLocalName()); | |
| 92 VASSERT_EQ("2/domain", "παράδειγμα.δοκιμή", eml2.getDomainName()); | |
| 93 | |
| 94 // Japanese Characters | |
| 95 vmime::emailAddress eml3("甲斐@黒川.日本"); | |
| 96 VASSERT_EQ("3/local", "甲斐", eml3.getLocalName()); | |
| 97 VASSERT_EQ("3/domain", "黒川.日本", eml3.getDomainName()); | |
| 98 | |
| 99 // Cyrillic Characters | |
| 100 vmime::emailAddress eml4("чебурашка@ящик-с-апельсинами.рф"); | |
| 101 VASSERT_EQ("4/local", "чебурашка", eml4.getLocalName()); | |
| 102 VASSERT_EQ("4/domain", "ящик-с-апельсинами.рф", eml4.getDomainName()); | |
| 103 } | |
| 104 | |
| 105 void testParseInvalid() | |
| 106 { | |
| 107 // Only one @ is allowed outside quotation marks | |
| 108 vmime::emailAddress eml1("local@part@domain"); | |
| 109 VASSERT_EQ("1/local", "local", eml1.getLocalName()); | |
| 110 VASSERT_EQ("1/domain", "part@domain", eml1.getDomainName()); | |
| 111 | |
| 112 // Quoted strings must be dot separated, or the only element making up | |
| 113 // the local-part: should be parsed correctly, but it still represents | |
| 114 // an invalid email address | |
| 115 vmime::emailAddress eml2("Just\"not\"right@example.com"); | |
| 116 VASSERT_EQ("2/local", "Just\"not\"right", eml2.getLocalName()); | |
| 117 VASSERT_EQ("2/domain", "example.com", eml2.getDomainName()); | |
| 118 | |
| 119 // An @ character must separate the local and domain parts | |
| 120 vmime::emailAddress eml3("Abc.example.com"); | |
| 121 VASSERT_EQ("3/local", "Abc.example.com", eml3.getLocalName()); | |
| 122 VASSERT_EQ("3/domain", vmime::platform::getHandler()->getHostName(), eml3.getDomainName()); | |
| 123 | |
| 124 // Whitespace must be escaped | |
| 125 vmime::emailAddress eml4("local part@domain"); | |
| 126 VASSERT_EQ("4/local", "local", eml4.getLocalName()); | |
| 127 VASSERT_EQ("4/domain", vmime::platform::getHandler()->getHostName(), eml4.getDomainName()); | |
| 128 | |
| 129 vmime::emailAddress eml5("this\\ still\\\"not\\\\allowed@example.com"); | |
| 130 VASSERT_EQ("5/local", "this\\", eml5.getLocalName()); | |
| 131 VASSERT_EQ("5/domain", vmime::platform::getHandler()->getHostName(), eml5.getDomainName()); | |
| 132 } | |
| 133 | |
| 134 void testParseSpecialChars() | |
| 135 { | |
| 136 // Examples taken from Wikipedia (http://en.wikipedia.org/wiki/Email_address) | |
| 137 | |
| 138 vmime::emailAddress eml1("\" \"@example.org"); | |
| 139 VASSERT_EQ("1/local", " ", eml1.getLocalName()); | |
| 140 VASSERT_EQ("1/domain", "example.org", eml1.getDomainName()); | |
| 141 | |
| 142 vmime::emailAddress eml2("\"()<>[]:,;@\\\\\\\"!#$%&'*+-/=?^_`{}| ~.a\"@example.org"); | |
| 143 VASSERT_EQ("2/local", "()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a", eml2.getLocalName()); | |
| 144 VASSERT_EQ("3/domain", "example.org", eml2.getDomainName()); | |
| 145 | |
| 146 vmime::emailAddress eml3("!#$%&'*+-/=?^_`{}|~@example.org"); | |
| 147 VASSERT_EQ("3/local", "!#$%&'*+-/=?^_`{}|~", eml3.getLocalName()); | |
| 148 VASSERT_EQ("3/domain", "example.org", eml3.getDomainName()); | |
| 149 | |
| 150 vmime::emailAddress eml4("!#$%&'*+-/=?^_`{}|~@example.org"); | |
| 151 VASSERT_EQ("4/local", "!#$%&'*+-/=?^_`{}|~", eml4.getLocalName()); | |
| 152 VASSERT_EQ("4/domain", "example.org", eml4.getDomainName()); | |
| 153 | |
| 154 vmime::emailAddress eml5("\"very.unusual.@.unusual.com\"@example.com"); | |
| 155 VASSERT_EQ("5/local", "very.unusual.@.unusual.com", eml5.getLocalName()); | |
| 156 VASSERT_EQ("5/domain", "example.com", eml5.getDomainName()); | |
| 157 | |
| 158 vmime::emailAddress eml6("\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@strange.example.com"); | |
| 159 VASSERT_EQ("6/local", "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual", eml6.getLocalName()); | |
| 160 VASSERT_EQ("6/domain", "strange.example.com", eml6.getDomainName()); | |
| 161 } | |
| 162 | |
| 163 void testParseCommentInLocalPart() | |
| 164 { | |
| 165 vmime::emailAddress eml1("john.smith(comment)@example.com"); | |
| 166 VASSERT_EQ("1/local", "john.smith", eml1.getLocalName()); | |
| 167 VASSERT_EQ("1/domain", "example.com", eml1.getDomainName()); | |
| 168 | |
| 169 vmime::emailAddress eml2("(comment)john.smith@example.com"); | |
| 170 VASSERT_EQ("2/local", "john.smith", eml2.getLocalName()); | |
| 171 VASSERT_EQ("2/domain", "example.com", eml2.getDomainName()); | |
| 172 | |
| 173 vmime::emailAddress eml3("(comment (comment in comment))john.smith@example.com"); | |
| 174 VASSERT_EQ("3/local", "john.smith", eml3.getLocalName()); | |
| 175 VASSERT_EQ("3/domain", "example.com", eml3.getDomainName()); | |
| 176 | |
| 177 vmime::emailAddress eml4("(comment \\) end comment)john.smith@example.com"); | |
| 178 VASSERT_EQ("4/local", "john.smith", eml4.getLocalName()); | |
| 179 VASSERT_EQ("4/domain", "example.com", eml4.getDomainName()); | |
| 180 } | |
| 181 | |
| 182 void testParseCommentInDomainPart() | |
| 183 { | |
| 184 vmime::emailAddress eml1("john.smith@(comment)example.com"); | |
| 185 VASSERT_EQ("1/local", "john.smith", eml1.getLocalName()); | |
| 186 VASSERT_EQ("1/domain", "example.com", eml1.getDomainName()); | |
| 187 | |
| 188 vmime::emailAddress eml2("john.smith@example.com(comment)"); | |
| 189 VASSERT_EQ("2/local", "john.smith", eml2.getLocalName()); | |
| 190 VASSERT_EQ("2/domain", "example.com", eml2.getDomainName()); | |
| 191 | |
| 192 vmime::emailAddress eml3("john.smith@(comment (comment in comment))example.com"); | |
| 193 VASSERT_EQ("3/local", "john.smith", eml3.getLocalName()); | |
| 194 VASSERT_EQ("3/domain", "example.com", eml3.getDomainName()); | |
| 195 | |
| 196 vmime::emailAddress eml4("john.smith@(comment \\) end comment)example.com"); | |
| 197 VASSERT_EQ("4/local", "john.smith", eml4.getLocalName()); | |
| 198 VASSERT_EQ("4/domain", "example.com", eml4.getDomainName()); | |
| 199 } | |
| 200 | |
| 201 void testGenerateASCII() | |
| 202 { | |
| 203 VASSERT_EQ("email 1", "local@domain", vmime::emailAddress("local", "domain").generate()); | |
| 204 | |
| 205 VASSERT_EQ("email 2", "=?utf-8?Q?Pel=C3=A9?=@example.com", | |
| 206 vmime::emailAddress("Pelé", "example.com").generate()); | |
| 207 VASSERT_EQ("email 3", "=?utf-8?B?55Sy5paQ?=@xn--5rtw95l.xn--wgv71a", | |
| 208 vmime::emailAddress("甲斐", "黒川.日本").generate()); | |
| 209 VASSERT_EQ("email 4", "mailtest@xn--r8jz45g.xn--zckzah", | |
| 210 vmime::emailAddress("mailtest", "例え.テスト").generate()); | |
| 211 VASSERT_EQ("email 5", "mailtest@xn--mgbh0fb.xn--kgbechtv", | |
| 212 vmime::emailAddress("mailtest", "مثال.إختبار").generate()); | |
| 213 } | |
| 214 | |
| 215 void testGenerateEAI() | |
| 216 { | |
| 217 vmime::generationContext ctx(vmime::generationContext::getDefaultContext()); | |
| 218 ctx.setInternationalizedEmailSupport(true); | |
| 219 | |
| 220 vmime::generationContext::switcher <vmime::generationContext> contextSwitcher(ctx); | |
| 221 | |
| 222 VASSERT_EQ("email 1", "Pelé@example.com", | |
| 223 vmime::emailAddress("Pelé", "example.com").generate()); | |
| 224 VASSERT_EQ("email 2", "δοκιμή@παράδειγμα.δοκιμή", | |
| 225 vmime::emailAddress("δοκιμή", "παράδειγμα.δοκιμή").generate()); | |
| 226 VASSERT_EQ("email 3", "甲斐@黒川.日本", | |
| 227 vmime::emailAddress("甲斐", "黒川.日本").generate()); | |
| 228 VASSERT_EQ("email 4", "чебурашка@ящик-с-апельсинами.рф", | |
| 229 vmime::emailAddress("чебурашка", "ящик-с-апельсинами.рф").generate()); | |
| 230 } | |
| 231 | |
| 232 void testGenerateSpecialChars() | |
| 233 { | |
| 234 VASSERT_EQ("email 1", "\"very.unusual.@.unusual.com\"@example.com", | |
| 235 vmime::emailAddress("very.unusual.@.unusual.com", "example.com").generate()); | |
| 236 | |
| 237 VASSERT_EQ("email 2", "\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@strange.example.com", | |
| 238 vmime::emailAddress("very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual", "strange.example.com").generate()); | |
| 239 | |
| 240 VASSERT_EQ("email 3", "\" \"@example.com", | |
| 241 vmime::emailAddress(" ", "example.com").generate()); | |
| 242 } | |
| 243 | |
| 244 VMIME_TEST_SUITE_END |
