-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDomain-test.cpp
150 lines (120 loc) · 4.75 KB
/
Domain-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
#include "Domain.hpp"
#include <iostream>
#include <glog/logging.h>
#include <fmt/format.h>
#include <fmt/ostream.h>
template <>
struct fmt::formatter<Domain> : ostream_formatter {};
using namespace std::string_literals;
int main(int argc, char const* argv[])
{
std::string msg;
auto const raw_ips = "123.123.123.123";
auto const add_lit = "[123.123.123.123]";
CHECK_EQ(Domain{raw_ips}, Domain{add_lit});
Domain d0{"EXAMPLE.COM"};
Domain d1{"example.com."};
CHECK_EQ(d0, d1);
Domain const d3{""};
Domain const d4{"."};
CHECK_EQ(d3, d4);
Domain const dom2{"黒川.日本"};
Domain const dom3{"xn--5rtw95l.xn--wgv71a"};
CHECK_EQ(dom2, dom3);
Domain const poop1{"💩.la"};
Domain const poop2{"xn--ls8h.la"};
CHECK_EQ(poop1, poop2);
Domain const norm0{"hi⒌com"}; // non-ascii "dot" before "com"
Domain const norm1{"hi5.com"};
CHECK_EQ(norm0, norm1);
Domain dom;
CHECK(Domain::validate("hi⒌com", msg, dom));
CHECK(Domain::validate("hi5.com", msg, dom));
CHECK(!Domain::validate("$?%^&*(", msg, dom));
CHECK_EQ(msg, "failed to parse domain «$?%^&*(»"s);
CHECK(!Domain::validate("[email protected]", msg, dom));
CHECK_EQ(msg, "failed to parse domain «[email protected]»"s);
CHECK(!Domain::validate("email@[123.123.123.123]", msg, dom));
CHECK_EQ(msg, "failed to parse domain «email@[123.123.123.123]»"s);
auto constexpr long_dom =
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
CHECK(Domain::validate(long_dom, msg, dom)) << msg;
CHECK(!Domain::validate(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.com",
msg, dom));
CHECK_EQ(msg,
"domain name "
"«xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.com» "
"too long");
CHECK(!Domain::validate(
"a.b.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"com",
msg, dom));
CHECK_EQ(
msg,
"domain label «xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx» too long"s);
CHECK(!Domain::validate(
"💩.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.com",
msg, dom));
CHECK_EQ(
msg,
"domain label «💩.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.com» too long"s);
CHECK(!Domain::validate(
"💩."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
msg, dom));
CHECK_EQ(
msg,
"domain name «"
"💩."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx."
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx» too long"s);
try {
Domain const junk{"$?%^&*("};
// idn2 allows this
LOG(FATAL) << "should have thrown";
}
catch (std::exception const& ex) {
CHECK_EQ(0, strcmp(ex.what(), "failed to parse domain"));
}
try {
Domain const ip_addr{"[127.0.0.1]"};
CHECK(ip_addr.is_address_literal());
}
catch (std::exception const& ex) {
LOG(FATAL) << "should not throw " << ex.what();
}
try {
Domain const ip_addr{"127.0.0.1"};
CHECK(ip_addr.is_address_literal());
}
catch (std::exception const& ex) {
LOG(FATAL) << "should not throw " << ex.what();
}
CHECK_EQ(Domain{"127.0.0.1"}, Domain{"[127.0.0.1]"});
Domain const mixed_case{"ExAmPle.COM"};
CHECK_EQ(mixed_case.ascii(), "example.com");
CHECK(domain::is_fully_qualified(Domain{"foo.bar"}, msg));
CHECK(!domain::is_fully_qualified(Domain{"foo.b"}, msg));
CHECK_EQ(msg, "TLD «b» must be two or more octets");
CHECK(!domain::is_fully_qualified(Domain{"foo"}, msg));
CHECK_EQ(msg, "domain «foo» must have two or more labels");
for (auto arg{1}; arg < argc; ++arg) {
Domain const a{argv[arg]};
std::cout << a << '\n';
}
}