-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathdns.cpp
212 lines (175 loc) Β· 6.88 KB
/
dns.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
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| @link https://www.swoole.com/ |
| @contact [email protected] |
| @license https://github.com/swoole/swoole-src/blob/master/LICENSE |
| @Author Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "test_coroutine.h"
#include "swoole_socket.h"
#include "swoole_util.h"
using namespace swoole;
using swoole::coroutine::Socket;
using swoole::coroutine::System;
using namespace swoole::test;
using namespace std;
TEST(dns, lookup1) {
test::coroutine::run([](void *arg) {
auto list = swoole::coroutine::dns_lookup("www.baidu.com", AF_INET, 10);
ASSERT_GE(list.size(), 1);
});
}
TEST(dns, lookup_ipv6) {
test::coroutine::run([](void *arg) {
auto list = swoole::coroutine::dns_lookup("www.google.com", AF_INET6, 2);
ASSERT_GE(list.size(), 1);
});
}
TEST(dns, domain_not_found) {
test::coroutine::run([](void *arg) {
auto list = swoole::coroutine::dns_lookup("www.baidu.com-not-found", AF_INET, 2);
ASSERT_EQ(list.size(), 0);
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_DNSLOOKUP_RESOLVE_FAILED);
});
}
TEST(dns, bad_family) {
test::coroutine::run([](void *arg) {
auto list = swoole::coroutine::dns_lookup("www.google.com", 9999, 2);
ASSERT_GE(list.size(), 1);
});
}
TEST(dns, cancel) {
// swoole_set_trace_flags(SW_TRACE_CARES);
// swoole_set_log_level(SW_LOG_TRACE);
test::coroutine::run([](void *arg) {
auto co = Coroutine::get_current_safe();
Coroutine::create([co](void *) {
System::sleep(0.002);
co->cancel();
});
auto list1 = swoole::coroutine::dns_lookup("www.baidu-not-found-for-cancel.com", AF_INET, 2);
ASSERT_EQ(list1.size(), 0);
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_CO_CANCELED);
});
}
TEST(dns, getaddrinfo) {
swoole::GetaddrinfoRequest req("www.baidu.com", AF_INET, SOCK_STREAM, 0, "");
ASSERT_EQ(swoole::network::getaddrinfo(&req), 0);
ASSERT_GT(req.count, 0);
vector<string> ip_list;
req.parse_result(ip_list);
for (auto &ip : ip_list) {
ASSERT_TRUE(swoole::network::Address::verify_ip(AF_INET, ip));
}
}
TEST(dns, load_resolv_conf) {
// reset
SwooleG.dns_server_host = "";
SwooleG.dns_server_port = 0;
int port = swoole::test::get_random_port();
auto dns_server = swoole_get_dns_server();
ASSERT_TRUE(dns_server.first.empty());
ASSERT_EQ(dns_server.second, 0);
// with port
std::string test_server = "127.0.0.1:" + std::to_string(port); // fake dns server
swoole_set_dns_server(test_server);
dns_server = swoole_get_dns_server();
ASSERT_STREQ(dns_server.first.c_str(), "127.0.0.1");
ASSERT_EQ(dns_server.second, port);
// invalid port
test_server = "127.0.0.1:808088";
swoole_set_dns_server(test_server);
dns_server = swoole_get_dns_server();
ASSERT_EQ(dns_server.second, SW_DNS_SERVER_PORT);
ASSERT_TRUE(swoole_load_resolv_conf());
dns_server = swoole_get_dns_server();
ASSERT_FALSE(dns_server.first.empty());
ASSERT_NE(dns_server.second, 0);
}
TEST(dns, gethosts) {
char hosts_file[] = "/tmp/swoole_hosts";
ofstream file(hosts_file);
if (!file.is_open()) {
std::cout << std::string("file open failed: ") + std::string(strerror(errno)) << std::endl;
throw strerror(errno);
}
ON_SCOPE_EXIT {
unlink(hosts_file);
};
file << "\n";
file << "127.0.0.1\n";
file << "127.0.0.1 localhost\n";
file << "# 127.0.0.1 aaa.com\n";
file << " 127.0.0.1 bbb.com ccc.com #ddd.com\n";
file.close();
swoole_set_hosts_path(hosts_file);
std::string ip = swoole::coroutine::get_ip_by_hosts("localhost");
ASSERT_EQ(ip, "127.0.0.1");
ip = swoole::coroutine::get_ip_by_hosts("aaa.com");
ASSERT_EQ(ip, "");
ip = swoole::coroutine::get_ip_by_hosts("bbb.com");
ASSERT_EQ(ip, "127.0.0.1");
ip = swoole::coroutine::get_ip_by_hosts("ccc.com");
ASSERT_EQ(ip, "127.0.0.1");
ip = swoole::coroutine::get_ip_by_hosts("ddd.com");
ASSERT_EQ(ip, "");
ip = swoole::coroutine::get_ip_by_hosts("non.exist.com");
ASSERT_EQ(ip, "");
}
void name_resolver_test_fn_1() {
NameResolver::Context ctx{};
ctx.type = AF_INET;
ctx.timeout = 1;
ASSERT_EQ("127.0.0.1", swoole_name_resolver_lookup("localhost", &ctx));
}
void name_resolver_test_fn_2() {
NameResolver::Context ctx;
std::string domain = "non.exist.com";
NameResolver nr{[](const std::string &domain, NameResolver::Context *ctx, void *) -> std::string {
if (domain == "name1") {
return "127.0.0.2";
} else if (domain == "www.baidu.com") {
ctx->final_ = true;
return "";
}
return "";
},
nullptr,
NameResolver::TYPE_USER};
swoole_name_resolver_add(nr);
ctx = {AF_INET};
ASSERT_EQ("127.0.0.2", swoole_name_resolver_lookup("name1", &ctx));
ctx = {AF_INET};
ASSERT_EQ("", swoole_name_resolver_lookup("www.baidu.com", &ctx));
ctx = {AF_INET};
ASSERT_EQ("127.0.0.1", swoole_name_resolver_lookup("localhost", &ctx));
swoole_name_resolver_each([](const std::list<NameResolver>::iterator &iter) -> swTraverseOperation {
if (iter->type == NameResolver::TYPE_USER) {
return SW_TRAVERSE_REMOVE;
} else {
return SW_TRAVERSE_KEEP;
}
});
ctx = {AF_INET};
auto ip = swoole_name_resolver_lookup("www.baidu.com", &ctx);
ASSERT_TRUE(swoole::network::Address::verify_ip(AF_INET, ip));
}
TEST(dns, name_resolver_1) {
name_resolver_test_fn_1();
test::coroutine::run([](void *arg) { name_resolver_test_fn_1(); });
}
TEST(dns, name_resolver_2) {
name_resolver_test_fn_2();
test::coroutine::run([](void *arg) { name_resolver_test_fn_2(); });
}