-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMailbox-test.cpp
274 lines (203 loc) · 9.97 KB
/
Mailbox-test.cpp
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
#include "Mailbox.hpp"
#include <iostream>
#include <glog/logging.h>
#include <fmt/format.h>
#include <fmt/ostream.h>
template <>
struct fmt::formatter<Mailbox> : ostream_formatter {};
using namespace std::string_literals;
int main(int argc, char* argv[])
{
CHECK_EQ(Mailbox{"\"a.b\"@foo.bar"}, Mailbox{"[email protected]"});
CHECK_EQ(Mailbox{"\"a..b\"@foo.bar"}, Mailbox{"\"\\a\\.\\.\\b\"@foo.bar"});
CHECK_EQ(Mailbox{"pOsTmAsTeR"}, Mailbox{"postmaster"});
Mailbox mb;
CHECK(mb.empty());
Mailbox dg0{"[email protected]"};
Mailbox dg1{"gene", Domain{"digilicious.com"}};
CHECK_EQ(dg0, dg1);
CHECK_EQ(std::string("digilicious.com"), dg0.domain().ascii());
auto dgstr = static_cast<std::string>(dg0);
CHECK_EQ(dgstr, "[email protected]");
dg0.clear();
CHECK(dg0.empty());
auto threw = false;
try {
Mailbox bad("should [email protected]");
}
catch (std::exception& e) {
threw = true;
}
CHECK(threw);
std::string msg;
Mailbox mbx;
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("[email protected]", msg,
mbx));
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// (may go to [email protected] inbox depending on mail server)
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// (space between the quotes)
CHECK(Mailbox::validate("\" \"@example.org", msg, mbx));
// (quoted angle brackets)
CHECK(Mailbox::validate("\"\\<foo-bar\\>\"@example.org", msg, mbx));
// (quoted double dot)
CHECK(Mailbox::validate("\"john..doe\"@example.org", msg, mbx));
// (bangified host route used for uucp mailers)
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// (% escaped mail route to [email protected] via example.org)
CHECK(Mailbox::validate("user%[email protected]", msg, mbx));
// Invalid email addresses
CHECK(!Mailbox::validate("Abc.example.com", msg, mbx)); // (no @ character)
CHECK_EQ(msg, "invalid mailbox syntax «Abc.example.com»"s);
CHECK(!Mailbox::validate("A@b@[email protected]", msg, mbx)); // (only one @ is
// allowed)
CHECK_EQ(msg, "invalid mailbox syntax «A@b@[email protected]»"s);
// (none of the special characters in this local-part are allowed
// outside quotation marks)
CHECK(!Mailbox::validate("a\"b(c)d,e:f;g<h>i[j\\k][email protected]", msg, mbx));
CHECK_EQ(msg,
"invalid mailbox syntax «a\"b(c)d,e:f;g<h>i[j\\k][email protected]»"s);
// (quoted strings must be dot separated or the only element making
// up the local-part)
CHECK(!Mailbox::validate("just\"not\"[email protected]", msg, mbx));
CHECK_EQ(msg, "invalid mailbox syntax «just\"not\"[email protected]»"s);
// (spaces, quotes, and backslashes may only exist when within
// quoted strings and preceded by a backslash)
CHECK(!Mailbox::validate("this is\"not\\[email protected]", msg, mbx));
CHECK_EQ(msg, "invalid mailbox syntax «this is\"not\\[email protected]»"s);
// (even if escaped (preceded by a backslash), spaces, quotes, and
// backslashes must still be contained by quotes)
CHECK(!Mailbox::validate("this\\ still\\\"not\\\\[email protected]", msg,
mbx));
CHECK_EQ(
msg,
"invalid mailbox syntax «this\\ still\\\"not\\\\[email protected]»"s);
// (local part is longer than 64 characters)
// Real world local-parts often longer than "offical" limit.
// CHECK(!Mailbox::validate(
// "1234567890123456789012345678901234567890123456789012345"
// "[email protected]"));
// Not fully qualified
Mailbox foobar{"foo@bar"};
CHECK(Mailbox::validate(foobar.as_string(Mailbox::domain_encoding::ascii),
msg, mbx));
CHECK(!domain::is_fully_qualified(foobar.domain(), msg));
CHECK_EQ(msg, "domain «bar» must have two or more labels"s);
// TLD too short, but okay at this level.
Mailbox foobarx{"[email protected]"};
CHECK(Mailbox::validate(foobarx.as_string(Mailbox::domain_encoding::ascii),
msg, mbx));
CHECK(!domain::is_fully_qualified(foobarx.domain(), msg));
CHECK_EQ(msg, "TLD «x» must be two or more octets"s);
// Label longer than 64 octets.
CHECK(!Mailbox::validate(
"foo@12345678901234567890123456789012345678901234567890123456789012345."
"com",
msg, mbx));
CHECK_EQ(
msg,
"domain label «12345678901234567890123456789012345678901234567890123456789012345» too long"s);
// Total domain length too long.
CHECK(!Mailbox::validate(
"foo@"
"123456789012345678901234567890123456789012345678901234567890123."
"123456789012345678901234567890123456789012345678901234567890123."
"123456789012345678901234567890123456789012345678901234567890123."
"123456789012345678901234567890123456789012345678901234567890123."
"com",
msg, mbx));
CHECK_EQ(msg,
"domain name «"
"123456789012345678901234567890123456789012345678901234567890123."
"123456789012345678901234567890123456789012345678901234567890123."
"123456789012345678901234567890123456789012345678901234567890123."
"123456789012345678901234567890123456789012345678901234567890123."
"com» too long"s);
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("gene@[127.0.0.1]", msg, mbx));
CHECK(!Mailbox::validate("gene@[127.999.0.1]", msg, mbx));
CHECK_EQ(msg, "invalid mailbox syntax «gene@[127.999.0.1]»"s);
CHECK(!Mailbox::validate("allen@bad_d0main.com", msg, mbx));
CHECK_EQ(msg, "invalid mailbox syntax «allen@bad_d0main.com»"s);
{
auto const res = Mailbox::parse("gene@[127.0.0.1]");
CHECK(res && res->local_type == Mailbox::local_types::dot_string);
CHECK(res && res->domain_type == Mailbox::domain_types::address_literal);
}
{
auto const res = Mailbox::parse("\"some string\"@example.com");
CHECK(res && res->local_type == Mailbox::local_types::quoted_string);
CHECK(res && res->domain_type == Mailbox::domain_types::domain);
}
CHECK(!Mailbox::validate("2962", msg, mbx));
CHECK_EQ(msg, "invalid mailbox syntax «2962»"s);
CHECK(Mailbox::validate("실례@실례.테스트", msg, mbx));
// <https://docs.microsoft.com/en-us/archive/blogs/testing123/email-address-test-cases>
// Valid email addresses:
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Email contains dot in the local part, a dot-atom-string.
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Multiple labels in domain.
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Plus sign is a valid character.
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Domain is valid IP address, but this is matched as a domain.
auto const raw_ips = "[email protected]";
CHECK(Mailbox::validate(raw_ips, msg, mbx));
// Square bracket around IP address is a "address literal."
auto const add_lit = "email@[123.123.123.123]";
CHECK(Mailbox::validate(add_lit, msg, mbx));
CHECK_EQ(Mailbox{raw_ips}, Mailbox{add_lit});
// Quotes around local part is valid.
CHECK(Mailbox::validate("\"email\"@domain.com", msg, mbx));
// But same mailbox.
CHECK_EQ(Mailbox{"\"email\"@domain.com"}, Mailbox{"[email protected]"});
// Digits in address are valid.
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Dash in domain name is valid.
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Underscore in the address field is valid.
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Dash in local part is valid.
CHECK(Mailbox::validate("[email protected]", msg, mbx));
CHECK(!Mailbox::validate("plainaddress", msg, mbx)); // Missing @ sign and
// domain
CHECK(!Mailbox::validate("#@%^%#$@#$@#.com", msg, mbx)); // Garbage
CHECK(!Mailbox::validate("@domain.com", msg, mbx)); // Missing username
CHECK(!Mailbox::validate("Joe Smith <[email protected]>", msg, mbx));
CHECK(!Mailbox::validate("email.domain.com", msg, mbx)); // Missing @
CHECK(!Mailbox::validate("email@[email protected]", msg, mbx)); // Two @ sign
// Leading dot in address is not allowed
CHECK(!Mailbox::validate("[email protected]", msg, mbx));
// Trailing dot in address is not allowed
CHECK(!Mailbox::validate("[email protected]", msg, mbx));
// Multiple dots
CHECK(!Mailbox::validate("[email protected]", msg, mbx));
// OK! Unicode char as address
CHECK(Mailbox::validate("あいうえお@domain.com", msg, mbx));
// Comment not allowed in 5321 mailbox.
CHECK(!Mailbox::validate("[email protected] (Joe Smith)", msg, mbx));
// Missing top level domain (.com/.net/.org/etc).
CHECK(Mailbox::validate("email@domain", msg, mbx)) << msg;
// Leading dash in front of domain is invalid.
CHECK(!Mailbox::validate("[email protected]", msg, mbx));
// .web is not a valid top level domain, oh yeah? says who?
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Invalid IP address.
CHECK(!Mailbox::validate("email@[111.222.333.44444]", msg, mbx));
// Invalid IP address, but valid domain name as it turns out.
CHECK(Mailbox::validate("[email protected]", msg, mbx));
// Not a valid domain name.
CHECK(!Mailbox::validate("[email protected]", msg, mbx));
// general_address_literal
CHECK(!Mailbox::validate("email@[x:~Foo_Bar_Baz<\?\?>]", msg, mbx));
std::cout << "sizeof(Mailbox) == " << sizeof(Mailbox) << '\n';
}