aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/tests/parser/emailAddressTest.cpp
blob: f2a707059e4eac64bd6e5b8d8ce66e4f1399c4d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// 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"

#include <locale>
#include <clocale>


VMIME_TEST_SUITE_BEGIN(emailAddressTest)

	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(testParseRFC2047EncodedLocalPart)
		VMIME_TEST(testGenerateSpecialChars)
	VMIME_TEST_LIST_END


	void setUp() {

		// Set the global C and C++ locale to the user-configured locale.
		// The locale should use UTF-8 encoding for these tests to run successfully.
		try {
			std::locale::global(std::locale(""));
		} catch (std::exception &) {
			std::setlocale(LC_ALL, "");
		}
	}

	void tearDown() {

		// Restore default locale
		std::locale::global(std::locale("C"));
	}


	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\"right@example.com");
		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\\\\allowed@example.com");
		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("!#$%&'*+-/=?^_`{}|~@example.org");
		VASSERT_EQ("3/local", "!#$%&'*+-/=?^_`{}|~", eml3.getLocalName());
		VASSERT_EQ("3/domain", "example.org", eml3.getDomainName());

		vmime::emailAddress eml4("!#$%&'*+-/=?^_`{}|~@example.org");
		VASSERT_EQ("4/local", "!#$%&'*+-/=?^_`{}|~", eml4.getLocalName());
		VASSERT_EQ("4/domain", "example.org", eml4.getDomainName());

		vmime::emailAddress eml5("\"very.unusual.@.unusual.com\"@example.com");
		VASSERT_EQ("5/local", "very.unusual.@.unusual.com", 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)john.smith@example.com");
		VASSERT_EQ("2/local", "john.smith", eml2.getLocalName());
		VASSERT_EQ("2/domain", "example.com", eml2.getDomainName());

		vmime::emailAddress eml3("(comment (comment in comment))john.smith@example.com");
		VASSERT_EQ("3/local", "john.smith", eml3.getLocalName());
		VASSERT_EQ("3/domain", "example.com", eml3.getDomainName());

		vmime::emailAddress eml4("(comment \\) end comment)john.smith@example.com");
		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("john.smith@example.com(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 testParseRFC2047EncodedLocalPart() {

		vmime::emailAddress eml1("=?utf-8?Q?Pel=C3=A9?=@example.com");
		VASSERT_EQ("1/local", "Pelé", eml1.getLocalName());
		VASSERT_EQ("1/domain", "example.com", eml1.getDomainName());

		vmime::emailAddress eml2("=?utf-8?B?55Sy5paQ?=@xn--5rtw95l.xn--wgv71a");
		VASSERT_EQ("2/local", "甲斐", eml2.getLocalName());
		VASSERT_EQ("2/domain", "黒川.日本", eml2.getDomainName());

		vmime::emailAddress eml3("=?utf-8?B?55Sy5paQ?=@xn--5rtw95l.com");
		VASSERT_EQ("3/local", "甲斐", eml3.getLocalName());
		VASSERT_EQ("3/domain", "黒川.com", eml3.getDomainName());
	}

	void testGenerateASCII() {

		VASSERT_EQ(
			"email 1", "local@domain",
			vmime::emailAddress("local", "domain").generate()
		);
		VASSERT_EQ(
			"email 2", "=?utf-8?Q?Pel=C3=A9?=@example.com",
			vmime::emailAddress("Pelé", "example.com").generate()
		);
		VASSERT_EQ(
			"email 3", "=?utf-8?B?55Sy5paQ?=@xn--5rtw95l.xn--wgv71a",
			vmime::emailAddress("甲斐", "黒川.日本").generate()
		);
		VASSERT_EQ(
			"email 4", "mailtest@xn--r8jz45g.xn--zckzah",
			vmime::emailAddress("mailtest", "例え.テスト").generate()
		);
		VASSERT_EQ(
			"email 5", "mailtest@xn--mgbh0fb.xn--kgbechtv",
			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", "\"very.unusual.@.unusual.com\"@example.com",
			vmime::emailAddress("very.unusual.@.unusual.com", "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