sort to folders
[ghsmtp.git] / Mailbox-test.cpp
blobbf1db7d2869983a905d04612ce69c312632feb23
1 #include "Mailbox.hpp"
3 #include <iostream>
5 #include <glog/logging.h>
7 int main(int argc, char* argv[])
9 Mailbox postmaster("postmaster@[127.0.0.1]");
11 Mailbox mb;
12 CHECK(mb.empty());
14 Mailbox dg0{"gene@digilicious.com"};
15 Mailbox dg1{"gene", "digilicious.com"};
17 CHECK_EQ(dg0, dg1);
19 CHECK_EQ(std::string("digilicious.com"), dg0.domain().ascii());
21 auto dgstr = static_cast<std::string>(dg0);
23 CHECK_EQ(dgstr, "gene@digilicious.com");
25 dg0.clear();
26 CHECK(dg0.empty());
28 auto threw = false;
29 try {
30 Mailbox bad("should throw@example.com");
32 catch (std::exception& e) {
33 threw = true;
35 CHECK(threw);
37 CHECK(Mailbox::validate("simple@example.com"));
38 CHECK(Mailbox::validate("very.common@example.com"));
39 CHECK(Mailbox::validate("disposable.style.email.with+symbol@example.com"));
40 CHECK(Mailbox::validate("other.email-with-hyphen@example.com"));
41 CHECK(Mailbox::validate("fully-qualified-domain@example.com"));
43 // (may go to user.name@example.com inbox depending on mail server)
44 CHECK(Mailbox::validate("user.name+tag+sorting@example.com"));
46 CHECK(Mailbox::validate("x@example.com"));
47 CHECK(Mailbox::validate("example-indeed@strange-example.com"));
49 // (local domain name with no TLD, although ICANN highly discourages
50 // dotless email addresses)
51 CHECK(Mailbox::validate("admin@mailserver1"));
53 // (see the List of Internet top-level domains)
54 CHECK(Mailbox::validate("example@s.example"));
56 // (space between the quotes)
57 CHECK(Mailbox::validate("\" \"@example.org"));
59 // (quoted double dot)
60 CHECK(Mailbox::validate("\"john..doe\"@example.org"));
62 // (bangified host route used for uucp mailers)
63 CHECK(Mailbox::validate("mailhost!username@example.org"));
65 // (% escaped mail route to user@example.com via example.org)
66 CHECK(Mailbox::validate("user%example.com@example.org"));
68 // Invalid email addresses
70 CHECK(!Mailbox::validate("Abc.example.com")); // (no @ character)
72 CHECK(!Mailbox::validate("A@b@c@example.com")); // (only one @ is allowed)
74 // (none of the special characters in this local-part are allowed
75 // outside quotation marks)
76 CHECK(!Mailbox::validate("a\"b(c)d,e:f;g<h>i[j\\k]l@example.com"));
78 // (quoted strings must be dot separated or the only element making
79 // up the local-part)
80 CHECK(!Mailbox::validate("just\"not\"right@example.com"));
82 // (spaces, quotes, and backslashes may only exist when within
83 // quoted strings and preceded by a backslash)
84 CHECK(!Mailbox::validate("this is\"not\\allowed@example.com"));
86 // (even if escaped (preceded by a backslash), spaces, quotes, and
87 // backslashes must still be contained by quotes)
88 CHECK(!Mailbox::validate("this\\ still\\\"not\\\\allowed@example.com"));
90 // (local part is longer than 64 characters)
91 CHECK(!Mailbox::validate_strict_lengths(
92 "1234567890123456789012345678901234567890123456789012345"
93 "678901234+x@example.com"));
95 CHECK(Mailbox::validate("gene@digilicious.com"));
96 CHECK(Mailbox::validate("gene@[127.0.0.1]"));
97 CHECK(!Mailbox::validate("gene@[127.999.0.1]"));
98 CHECK(!Mailbox::validate("allen@bad_d0main.com"));
101 auto const res = Mailbox::parse("gene@[127.0.0.1]");
102 CHECK(res && res->local_type == Mailbox::local_types::dot_string);
103 CHECK(res && res->domain_type == Mailbox::domain_types::address_literal);
106 auto const res = Mailbox::parse("\"some string\"@example.com");
107 CHECK(res && res->local_type == Mailbox::local_types::quoted_string);
108 CHECK(res && res->domain_type == Mailbox::domain_types::domain);
111 CHECK(!Mailbox::validate("2962"));
112 CHECK(Mailbox::validate("실례@실례.테스트"));
114 // <https://docs.microsoft.com/en-us/archive/blogs/testing123/email-address-test-cases>
116 // Valid email addresses:
117 CHECK(Mailbox::validate("email@domain.com"));
119 // Email contains dot in the local part, a dot-atom-string.
120 CHECK(Mailbox::validate("firstname.lastname@domain.com"));
122 // Multiple lables in domain.
123 CHECK(Mailbox::validate("email@subdomain.domain.com"));
125 // Plus sign is a valid character.
126 CHECK(Mailbox::validate("firstname+lastname@domain.com"));
128 // Domain is valid IP address, but this is matched as a domain.
129 CHECK(Mailbox::validate("email@123.123.123.123"));
131 // Square bracket around IP address is a "address literal."
132 CHECK(Mailbox::validate("email@[123.123.123.123]"));
134 // Quotes around local part is valid.
135 CHECK(Mailbox::validate("\"email\"@domain.com"));
137 // Digits in address are valid.
138 CHECK(Mailbox::validate("1234567890@domain.com"));
140 // Dash in domain name is valid.
141 CHECK(Mailbox::validate("email@domain-one.com"));
143 // Underscore in the address field is valid.
144 CHECK(Mailbox::validate("_______@domain.com"));
146 CHECK(Mailbox::validate("email@domain.name"));
147 CHECK(Mailbox::validate("email@domain.co.jp"));
149 // Dash in local part is valid.
150 CHECK(Mailbox::validate("firstname-lastname@domain.com"));
152 CHECK(!Mailbox::validate("plainaddress")); // Missing @ sign and domain
153 CHECK(!Mailbox::validate("#@%^%#$@#$@#.com")); // Garbage
154 CHECK(!Mailbox::validate("@domain.com")); // Missing username
156 CHECK(!Mailbox::validate("Joe Smith <email@domain.com>"));
158 CHECK(!Mailbox::validate("email.domain.com")); // Missing @
159 CHECK(!Mailbox::validate("email@domain@domain.com")); // Two @ sign
161 // Leading dot in address is not allowed
162 CHECK(!Mailbox::validate(".email@domain.com"));
164 // Trailing dot in address is not allowed
165 CHECK(!Mailbox::validate("email.@domain.com"));
167 // Multiple dots
168 CHECK(!Mailbox::validate("email..email@domain.com"));
170 // OK! Unicode char as address
171 CHECK(Mailbox::validate("あいうえお@domain.com"));
173 // Comment not allowed in 5321 mailbox.
174 CHECK(!Mailbox::validate("email@domain.com (Joe Smith)"));
176 // Missing top level domain (.com/.net/.org/etc).
177 CHECK(Mailbox::validate("email@domain"));
179 // Leading dash in front of domain is invalid.
180 CHECK(!Mailbox::validate("email@-domain.com"));
182 // .web is not a valid top level domain, oh yeah? says who?
183 CHECK(Mailbox::validate("email@domain.web"));
185 // Invalid IP address.
186 CHECK(!Mailbox::validate("email@[111.222.333.44444]"));
188 // Invalid IP address, but valid domain name as it turns out.
189 CHECK(Mailbox::validate("email@111.222.333.44444"));
191 // Not a valid domain name.
192 CHECK(!Mailbox::validate("email@domain..com"));
194 // general_address_literal
195 CHECK(Mailbox::validate("email@[x:~Foo_Bar_Baz<\?\?>]"));
197 std::cout << "sizeof(Mailbox) == " << sizeof(Mailbox) << '\n';