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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <resolv.h>
#include "base/cancelable_callback.h"
#include "base/files/file_util.h"
#include "base/sys_byteorder.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "net/dns/dns_config_service_posix.h"
#include "net/dns/dns_protocol.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_ANDROID)
#include "base/android/path_utils.h"
#endif // defined(OS_ANDROID)
namespace net {
#if !defined(OS_ANDROID)
namespace {
// MAXNS is normally 3, but let's test 4 if possible.
const char* const kNameserversIPv4[] = {
"8.8.8.8",
"192.168.1.1",
"63.1.2.4",
"1.0.0.1",
};
#if defined(OS_LINUX)
const char* const kNameserversIPv6[] = {
NULL,
"2001:DB8:0::42",
NULL,
"::FFFF:129.144.52.38",
};
#endif
// Fills in |res| with sane configuration.
void InitializeResState(res_state res) {
memset(res, 0, sizeof(*res));
res->options = RES_INIT | RES_RECURSE | RES_DEFNAMES | RES_DNSRCH |
RES_ROTATE;
res->ndots = 2;
res->retrans = 4;
res->retry = 7;
const char kDnsrch[] = "chromium.org" "\0" "example.com";
memcpy(res->defdname, kDnsrch, sizeof(kDnsrch));
res->dnsrch[0] = res->defdname;
res->dnsrch[1] = res->defdname + sizeof("chromium.org");
for (unsigned i = 0; i < arraysize(kNameserversIPv4) && i < MAXNS; ++i) {
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = base::HostToNet16(NS_DEFAULTPORT + i);
inet_pton(AF_INET, kNameserversIPv4[i], &sa.sin_addr);
res->nsaddr_list[i] = sa;
++res->nscount;
}
#if defined(OS_LINUX)
// Install IPv6 addresses, replacing the corresponding IPv4 addresses.
unsigned nscount6 = 0;
for (unsigned i = 0; i < arraysize(kNameserversIPv6) && i < MAXNS; ++i) {
if (!kNameserversIPv6[i])
continue;
// Must use malloc to mimick res_ninit.
struct sockaddr_in6 *sa6;
sa6 = (struct sockaddr_in6 *)malloc(sizeof(*sa6));
sa6->sin6_family = AF_INET6;
sa6->sin6_port = base::HostToNet16(NS_DEFAULTPORT - i);
inet_pton(AF_INET6, kNameserversIPv6[i], &sa6->sin6_addr);
res->_u._ext.nsaddrs[i] = sa6;
memset(&res->nsaddr_list[i], 0, sizeof res->nsaddr_list[i]);
++nscount6;
}
res->_u._ext.nscount6 = nscount6;
#endif
}
void CloseResState(res_state res) {
#if defined(OS_LINUX)
for (int i = 0; i < res->nscount; ++i) {
if (res->_u._ext.nsaddrs[i] != NULL)
free(res->_u._ext.nsaddrs[i]);
}
#endif
}
void InitializeExpectedConfig(DnsConfig* config) {
config->ndots = 2;
config->timeout = base::TimeDelta::FromSeconds(4);
config->attempts = 7;
config->rotate = true;
config->edns0 = false;
config->append_to_multi_label_name = true;
config->search.clear();
config->search.push_back("chromium.org");
config->search.push_back("example.com");
config->nameservers.clear();
for (unsigned i = 0; i < arraysize(kNameserversIPv4) && i < MAXNS; ++i) {
IPAddressNumber ip;
ParseIPLiteralToNumber(kNameserversIPv4[i], &ip);
config->nameservers.push_back(IPEndPoint(ip, NS_DEFAULTPORT + i));
}
#if defined(OS_LINUX)
for (unsigned i = 0; i < arraysize(kNameserversIPv6) && i < MAXNS; ++i) {
if (!kNameserversIPv6[i])
continue;
IPAddressNumber ip;
ParseIPLiteralToNumber(kNameserversIPv6[i], &ip);
config->nameservers[i] = IPEndPoint(ip, NS_DEFAULTPORT - i);
}
#endif
}
TEST(DnsConfigServicePosixTest, ConvertResStateToDnsConfig) {
struct __res_state res;
DnsConfig config;
EXPECT_FALSE(config.IsValid());
InitializeResState(&res);
ASSERT_EQ(internal::CONFIG_PARSE_POSIX_OK,
internal::ConvertResStateToDnsConfig(res, &config));
CloseResState(&res);
EXPECT_TRUE(config.IsValid());
DnsConfig expected_config;
EXPECT_FALSE(expected_config.EqualsIgnoreHosts(config));
InitializeExpectedConfig(&expected_config);
EXPECT_TRUE(expected_config.EqualsIgnoreHosts(config));
}
TEST(DnsConfigServicePosixTest, RejectEmptyNameserver) {
struct __res_state res = {};
res.options = RES_INIT | RES_RECURSE | RES_DEFNAMES | RES_DNSRCH;
const char kDnsrch[] = "chromium.org";
memcpy(res.defdname, kDnsrch, sizeof(kDnsrch));
res.dnsrch[0] = res.defdname;
struct sockaddr_in sa = {};
sa.sin_family = AF_INET;
sa.sin_port = base::HostToNet16(NS_DEFAULTPORT);
sa.sin_addr.s_addr = INADDR_ANY;
res.nsaddr_list[0] = sa;
sa.sin_addr.s_addr = 0xCAFE1337;
res.nsaddr_list[1] = sa;
res.nscount = 2;
DnsConfig config;
EXPECT_EQ(internal::CONFIG_PARSE_POSIX_NULL_ADDRESS,
internal::ConvertResStateToDnsConfig(res, &config));
sa.sin_addr.s_addr = 0xDEADBEEF;
res.nsaddr_list[0] = sa;
EXPECT_EQ(internal::CONFIG_PARSE_POSIX_OK,
internal::ConvertResStateToDnsConfig(res, &config));
}
} // namespace
#else // OS_ANDROID
namespace internal {
const char kTempHosts1[] = "127.0.0.1 localhost";
const char kTempHosts2[] = "127.0.0.2 localhost";
class DnsConfigServicePosixTest : public testing::Test {
public:
DnsConfigServicePosixTest() : seen_config_(false) {}
~DnsConfigServicePosixTest() override {}
void OnConfigChanged(const DnsConfig& config) {
EXPECT_TRUE(config.IsValid());
seen_config_ = true;
base::MessageLoop::current()->Quit();
}
void WriteMockHostsFile(const char* hosts_string) {
ASSERT_EQ(base::WriteFile(temp_file_, hosts_string, strlen(hosts_string)),
static_cast<int>(strlen(hosts_string)));
}
void MockDNSConfig(const char* dns_server) {
IPAddressNumber dns_number;
ASSERT_TRUE(ParseIPLiteralToNumber(dns_server, &dns_number));
test_config_.nameservers.clear();
test_config_.nameservers.push_back(
IPEndPoint(dns_number, dns_protocol::kDefaultPort));
service_->SetDnsConfigForTesting(&test_config_);
}
void SetUp() override {
// TODO(pauljensen): Get rid of GetExternalStorageDirectory() when
// crbug.com/475568 is fixed. For now creating a temp file in the
// default temp directory (/data/data/...) will cause FilePathWatcher
// to fail, so create the temp file in /sdcard.
base::FilePath parent_dir;
ASSERT_TRUE(base::android::GetExternalStorageDirectory(&parent_dir));
ASSERT_TRUE(base::CreateTemporaryFileInDir(parent_dir, &temp_file_));
WriteMockHostsFile(kTempHosts1);
// Set the time on the hosts file back so it appears older than the
// 1s safety offset in DnsConfigServicePosix::SeenChangeSince().
// TODO(pauljensen): Switch from Sleep() to TouchFile() when
// crbug.com/475568 is fixed. For now TouchFile() will fail in /sdcard.
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1100));
// // Copy real hosts file's last modified time to mock hosts file.
// base::File hosts(base::FilePath(DnsConfigServicePosix::kFilePathHosts),
// base::File::FLAG_OPEN | base::File::FLAG_READ);
// base::File::Info hosts_info;
// ASSERT_TRUE(hosts.GetInfo(&hosts_info));
// ASSERT_TRUE(base::TouchFile(temp_file_, hosts_info.last_modified,
// hosts_info.last_accessed));
}
void TearDown() override { ASSERT_TRUE(base::DeleteFile(temp_file_, false)); }
void StartWatching() {
creation_time_ = base::Time::Now();
service_.reset(new DnsConfigServicePosix());
service_->file_path_hosts_ = temp_file_.value().c_str();
MockDNSConfig("8.8.8.8");
seen_config_ = false;
service_->WatchConfig(base::Bind(
&DnsConfigServicePosixTest::OnConfigChanged, base::Unretained(this)));
ExpectChange();
}
void ExpectChange() {
EXPECT_FALSE(seen_config_);
base::MessageLoop::current()->Run();
EXPECT_TRUE(seen_config_);
seen_config_ = false;
}
bool seen_config_;
base::Time creation_time_;
base::FilePath temp_file_;
scoped_ptr<DnsConfigServicePosix> service_;
DnsConfig test_config_;
};
TEST_F(DnsConfigServicePosixTest, SeenChangeSince) {
// Verify SeenChangeSince() returns false if no changes
StartWatching();
EXPECT_FALSE(service_->SeenChangeSince(creation_time_));
// Verify SeenChangeSince() returns true if network change
MockDNSConfig("8.8.4.4");
service_->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_TRUE(service_->SeenChangeSince(creation_time_));
ExpectChange();
// Verify SeenChangeSince() returns true if hosts file changes
StartWatching();
EXPECT_FALSE(service_->SeenChangeSince(creation_time_));
WriteMockHostsFile(kTempHosts2);
EXPECT_TRUE(service_->SeenChangeSince(creation_time_));
ExpectChange();
}
} // namespace internal
#endif // OS_ANDROID
} // namespace net
|