support 2nd RCPT TO and PRDR
[ghsmtp.git] / Mailbox-test.cpp
blob7a4e9ac5754a78655b8fadf3c98485e3b95155d5
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 angle brackets)
60 CHECK(Mailbox::validate("\"\\<foo-bar\\>\"@example.org"));
62 // (quoted double dot)
63 CHECK(Mailbox::validate("\"john..doe\"@example.org"));
65 // (bangified host route used for uucp mailers)
66 CHECK(Mailbox::validate("mailhost!username@example.org"));
68 // (% escaped mail route to user@example.com via example.org)
69 CHECK(Mailbox::validate("user%example.com@example.org"));
71 // Invalid email addresses
73 CHECK(!Mailbox::validate("Abc.example.com")); // (no @ character)
75 CHECK(!Mailbox::validate("A@b@c@example.com")); // (only one @ is allowed)
77 // (none of the special characters in this local-part are allowed
78 // outside quotation marks)
79 CHECK(!Mailbox::validate("a\"b(c)d,e:f;g<h>i[j\\k]l@example.com"));
81 // (quoted strings must be dot separated or the only element making
82 // up the local-part)
83 CHECK(!Mailbox::validate("just\"not\"right@example.com"));
85 // (spaces, quotes, and backslashes may only exist when within
86 // quoted strings and preceded by a backslash)
87 CHECK(!Mailbox::validate("this is\"not\\allowed@example.com"));
89 // (even if escaped (preceded by a backslash), spaces, quotes, and
90 // backslashes must still be contained by quotes)
91 CHECK(!Mailbox::validate("this\\ still\\\"not\\\\allowed@example.com"));
93 // (local part is longer than 64 characters)
94 CHECK(!Mailbox::validate_strict_lengths(
95 "1234567890123456789012345678901234567890123456789012345"
96 "678901234+x@example.com"));
98 CHECK(Mailbox::validate("gene@digilicious.com"));
99 CHECK(Mailbox::validate("gene@[127.0.0.1]"));
100 CHECK(!Mailbox::validate("gene@[127.999.0.1]"));
101 CHECK(!Mailbox::validate("allen@bad_d0main.com"));
104 auto const res = Mailbox::parse("gene@[127.0.0.1]");
105 CHECK(res && res->local_type == Mailbox::local_types::dot_string);
106 CHECK(res && res->domain_type == Mailbox::domain_types::address_literal);
109 auto const res = Mailbox::parse("\"some string\"@example.com");
110 CHECK(res && res->local_type == Mailbox::local_types::quoted_string);
111 CHECK(res && res->domain_type == Mailbox::domain_types::domain);
114 CHECK(!Mailbox::validate("2962"));
115 CHECK(Mailbox::validate("실례@실례.테스트"));
117 // <https://docs.microsoft.com/en-us/archive/blogs/testing123/email-address-test-cases>
119 // Valid email addresses:
120 CHECK(Mailbox::validate("email@domain.com"));
122 // Email contains dot in the local part, a dot-atom-string.
123 CHECK(Mailbox::validate("firstname.lastname@domain.com"));
125 // Multiple lables in domain.
126 CHECK(Mailbox::validate("email@subdomain.domain.com"));
128 // Plus sign is a valid character.
129 CHECK(Mailbox::validate("firstname+lastname@domain.com"));
131 // Domain is valid IP address, but this is matched as a domain.
132 CHECK(Mailbox::validate("email@123.123.123.123"));
134 // Square bracket around IP address is a "address literal."
135 CHECK(Mailbox::validate("email@[123.123.123.123]"));
137 // Quotes around local part is valid.
138 CHECK(Mailbox::validate("\"email\"@domain.com"));
140 // Digits in address are valid.
141 CHECK(Mailbox::validate("1234567890@domain.com"));
143 // Dash in domain name is valid.
144 CHECK(Mailbox::validate("email@domain-one.com"));
146 // Underscore in the address field is valid.
147 CHECK(Mailbox::validate("_______@domain.com"));
149 CHECK(Mailbox::validate("email@domain.name"));
150 CHECK(Mailbox::validate("email@domain.co.jp"));
152 // Dash in local part is valid.
153 CHECK(Mailbox::validate("firstname-lastname@domain.com"));
155 CHECK(!Mailbox::validate("plainaddress")); // Missing @ sign and domain
156 CHECK(!Mailbox::validate("#@%^%#$@#$@#.com")); // Garbage
157 CHECK(!Mailbox::validate("@domain.com")); // Missing username
159 CHECK(!Mailbox::validate("Joe Smith <email@domain.com>"));
161 CHECK(!Mailbox::validate("email.domain.com")); // Missing @
162 CHECK(!Mailbox::validate("email@domain@domain.com")); // Two @ sign
164 // Leading dot in address is not allowed
165 CHECK(!Mailbox::validate(".email@domain.com"));
167 // Trailing dot in address is not allowed
168 CHECK(!Mailbox::validate("email.@domain.com"));
170 // Multiple dots
171 CHECK(!Mailbox::validate("email..email@domain.com"));
173 // OK! Unicode char as address
174 CHECK(Mailbox::validate("あいうえお@domain.com"));
176 // Comment not allowed in 5321 mailbox.
177 CHECK(!Mailbox::validate("email@domain.com (Joe Smith)"));
179 // Missing top level domain (.com/.net/.org/etc).
180 CHECK(Mailbox::validate("email@domain"));
182 // Leading dash in front of domain is invalid.
183 CHECK(!Mailbox::validate("email@-domain.com"));
185 // .web is not a valid top level domain, oh yeah? says who?
186 CHECK(Mailbox::validate("email@domain.web"));
188 // Invalid IP address.
189 CHECK(!Mailbox::validate("email@[111.222.333.44444]"));
191 // Invalid IP address, but valid domain name as it turns out.
192 CHECK(Mailbox::validate("email@111.222.333.44444"));
194 // Not a valid domain name.
195 CHECK(!Mailbox::validate("email@domain..com"));
197 // general_address_literal
198 CHECK(Mailbox::validate("email@[x:~Foo_Bar_Baz<\?\?>]"));
200 std::cout << "sizeof(Mailbox) == " << sizeof(Mailbox) << '\n';