two functions now
[ghsmtp.git] / Mailbox-test.cpp
blobcf00dc26a6ec15ae91e4ce51aba423312371ee2c
1 #include "Mailbox.hpp"
3 #include <iostream>
5 #include <glog/logging.h>
7 int main(int argc, char* argv[])
9 Mailbox empty("");
10 Mailbox postmaster("postmaster@[127.0.0.1]");
12 Mailbox mb;
13 CHECK(mb.empty());
15 Mailbox dg0{"gene@digilicious.com"};
16 Mailbox dg1{"gene", "digilicious.com"};
18 CHECK_EQ(dg0, dg1);
20 CHECK_EQ(std::string("digilicious.com"), dg0.domain().ascii());
22 auto dgstr = static_cast<std::string>(dg0);
24 CHECK_EQ(dgstr, "gene@digilicious.com");
26 dg0.clear();
27 CHECK(dg0.empty());
29 auto threw = false;
30 try {
31 Mailbox bad("foo bar@example.com");
33 catch (std::exception& e) {
34 threw = true;
36 CHECK(threw);
38 CHECK(Mailbox::validate("simple@example.com"));
39 CHECK(Mailbox::validate("very.common@example.com"));
40 CHECK(Mailbox::validate("disposable.style.email.with+symbol@example.com"));
41 CHECK(Mailbox::validate("other.email-with-hyphen@example.com"));
42 CHECK(Mailbox::validate("fully-qualified-domain@example.com"));
44 // (may go to user.name@example.com inbox depending on mail server)
45 CHECK(Mailbox::validate("user.name+tag+sorting@example.com"));
47 CHECK(Mailbox::validate("x@example.com"));
48 CHECK(Mailbox::validate("example-indeed@strange-example.com"));
50 // (local domain name with no TLD, although ICANN highly discourages
51 // dotless email addresses)
52 CHECK(Mailbox::validate("admin@mailserver1"));
54 // (see the List of Internet top-level domains)
55 CHECK(Mailbox::validate("example@s.example"));
57 // (space between the quotes)
58 CHECK(Mailbox::validate("\" \"@example.org"));
60 // (quoted double dot)
61 CHECK(Mailbox::validate("\"john..doe\"@example.org"));
63 // (bangified host route used for uucp mailers)
64 CHECK(Mailbox::validate("mailhost!username@example.org"));
66 // (% escaped mail route to user@example.com via example.org)
67 CHECK(Mailbox::validate("user%example.com@example.org"));
69 // Invalid email addresses
71 CHECK(!Mailbox::validate("Abc.example.com")); // (no @ character)
73 CHECK(!Mailbox::validate("A@b@c@example.com")); // (only one @ is allowed)
75 // (none of the special characters in this local-part are allowed
76 // outside quotation marks)
77 CHECK(!Mailbox::validate("a\"b(c)d,e:f;g<h>i[j\\k]l@example.com"));
79 // (quoted strings must be dot separated or the only element making
80 // up the local-part)
81 CHECK(!Mailbox::validate("just\"not\"right@example.com"));
83 // (spaces, quotes, and backslashes may only exist when within
84 // quoted strings and preceded by a backslash)
85 CHECK(!Mailbox::validate("this is\"not\\allowed@example.com"));
87 // (even if escaped (preceded by a backslash), spaces, quotes, and
88 // backslashes must still be contained by quotes)
89 CHECK(!Mailbox::validate("this\\ still\\\"not\\\\allowed@example.com"));
91 // (local part is longer than 64 characters)
92 CHECK(!Mailbox::validate_strict_lengths(
93 "1234567890123456789012345678901234567890123456789012345"
94 "678901234+x@example.com"));
96 CHECK(Mailbox::validate("gene@digilicious.com"));
97 CHECK(Mailbox::validate("gene@[127.0.0.1]"));
98 CHECK(!Mailbox::validate("gene@[127.999.0.1]"));
99 CHECK(!Mailbox::validate("allen@bad_d0main.com"));
101 CHECK(!Mailbox::validate("2962"));
102 CHECK(Mailbox::validate("실례@실례.테스트"));
104 // <https://docs.microsoft.com/en-us/archive/blogs/testing123/email-address-test-cases>
106 // Valid email addresses:
107 CHECK(Mailbox::validate("email@domain.com"));
109 // Email contains dot in the local part, a dot-atom-string.
110 CHECK(Mailbox::validate("firstname.lastname@domain.com"));
112 // Multiple lables in domain.
113 CHECK(Mailbox::validate("email@subdomain.domain.com"));
115 // Plus sign is a valid character.
116 CHECK(Mailbox::validate("firstname+lastname@domain.com"));
118 // Domain is valid IP address, but this is matched as a domain.
119 CHECK(Mailbox::validate("email@123.123.123.123"));
121 // Square bracket around IP address is a "address literal."
122 CHECK(Mailbox::validate("email@[123.123.123.123]"));
124 // Quotes around local part is valid.
125 CHECK(Mailbox::validate("\"email\"@domain.com"));
127 // Digits in address are valid.
128 CHECK(Mailbox::validate("1234567890@domain.com"));
130 // Dash in domain name is valid.
131 CHECK(Mailbox::validate("email@domain-one.com"));
133 // Underscore in the address field is valid.
134 CHECK(Mailbox::validate("_______@domain.com"));
136 CHECK(Mailbox::validate("email@domain.name"));
137 CHECK(Mailbox::validate("email@domain.co.jp"));
139 // Dash in local part is valid.
140 CHECK(Mailbox::validate("firstname-lastname@domain.com"));
142 CHECK(!Mailbox::validate("plainaddress")); // Missing @ sign and domain
143 CHECK(!Mailbox::validate("#@%^%#$@#$@#.com")); // Garbage
144 CHECK(!Mailbox::validate("@domain.com")); // Missing username
146 CHECK(!Mailbox::validate("Joe Smith <email@domain.com>"));
148 CHECK(!Mailbox::validate("email.domain.com")); // Missing @
149 CHECK(!Mailbox::validate("email@domain@domain.com")); // Two @ sign
151 // Leading dot in address is not allowed
152 CHECK(!Mailbox::validate(".email@domain.com"));
154 // Trailing dot in address is not allowed
155 CHECK(!Mailbox::validate("email.@domain.com"));
157 // Multiple dots
158 CHECK(!Mailbox::validate("email..email@domain.com"));
160 // OK! Unicode char as address
161 CHECK(Mailbox::validate("あいうえお@domain.com"));
163 // Comment not allowed in 5321 mailbox.
164 CHECK(!Mailbox::validate("email@domain.com (Joe Smith)"));
166 // Missing top level domain (.com/.net/.org/etc).
167 CHECK(Mailbox::validate("email@domain"));
169 // Leading dash in front of domain is invalid.
170 CHECK(!Mailbox::validate("email@-domain.com"));
172 // .web is not a valid top level domain, oh yeah? says who?
173 CHECK(Mailbox::validate("email@domain.web"));
175 // Invalid IP address.
176 CHECK(!Mailbox::validate("email@[111.222.333.44444]"));
178 // Invalid IP address, but valid domain name as it turns out.
179 CHECK(Mailbox::validate("email@111.222.333.44444"));
181 // Not a valid domain name.
182 CHECK(!Mailbox::validate("email@domain..com"));
184 // general_address_literal
185 CHECK(Mailbox::validate("email@[x:~Foo_Bar_Baz<\?\?>]"));
187 std::cout << "sizeof(Mailbox) == " << sizeof(Mailbox) << '\n';