1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/cancelable_callback.h"
8 #include "base/files/file_util.h"
9 #include "base/sys_byteorder.h"
10 #include "base/test/test_timeouts.h"
11 #include "base/threading/platform_thread.h"
12 #include "net/dns/dns_config_service_posix.h"
13 #include "net/dns/dns_protocol.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 #if defined(OS_ANDROID)
18 #include "base/android/path_utils.h"
19 #endif // defined(OS_ANDROID)
23 #if !defined(OS_ANDROID)
27 // MAXNS is normally 3, but let's test 4 if possible.
28 const char* const kNameserversIPv4
[] = {
36 const char* const kNameserversIPv6
[] = {
40 "::FFFF:129.144.52.38",
44 // Fills in |res| with sane configuration.
45 void InitializeResState(res_state res
) {
46 memset(res
, 0, sizeof(*res
));
47 res
->options
= RES_INIT
| RES_RECURSE
| RES_DEFNAMES
| RES_DNSRCH
|
53 const char kDnsrch
[] = "chromium.org" "\0" "example.com";
54 memcpy(res
->defdname
, kDnsrch
, sizeof(kDnsrch
));
55 res
->dnsrch
[0] = res
->defdname
;
56 res
->dnsrch
[1] = res
->defdname
+ sizeof("chromium.org");
58 for (unsigned i
= 0; i
< arraysize(kNameserversIPv4
) && i
< MAXNS
; ++i
) {
59 struct sockaddr_in sa
;
60 sa
.sin_family
= AF_INET
;
61 sa
.sin_port
= base::HostToNet16(NS_DEFAULTPORT
+ i
);
62 inet_pton(AF_INET
, kNameserversIPv4
[i
], &sa
.sin_addr
);
63 res
->nsaddr_list
[i
] = sa
;
68 // Install IPv6 addresses, replacing the corresponding IPv4 addresses.
69 unsigned nscount6
= 0;
70 for (unsigned i
= 0; i
< arraysize(kNameserversIPv6
) && i
< MAXNS
; ++i
) {
71 if (!kNameserversIPv6
[i
])
73 // Must use malloc to mimick res_ninit.
74 struct sockaddr_in6
*sa6
;
75 sa6
= (struct sockaddr_in6
*)malloc(sizeof(*sa6
));
76 sa6
->sin6_family
= AF_INET6
;
77 sa6
->sin6_port
= base::HostToNet16(NS_DEFAULTPORT
- i
);
78 inet_pton(AF_INET6
, kNameserversIPv6
[i
], &sa6
->sin6_addr
);
79 res
->_u
._ext
.nsaddrs
[i
] = sa6
;
80 memset(&res
->nsaddr_list
[i
], 0, sizeof res
->nsaddr_list
[i
]);
83 res
->_u
._ext
.nscount6
= nscount6
;
87 void CloseResState(res_state res
) {
89 for (int i
= 0; i
< res
->nscount
; ++i
) {
90 if (res
->_u
._ext
.nsaddrs
[i
] != NULL
)
91 free(res
->_u
._ext
.nsaddrs
[i
]);
96 void InitializeExpectedConfig(DnsConfig
* config
) {
98 config
->timeout
= base::TimeDelta::FromSeconds(4);
100 config
->rotate
= true;
101 config
->edns0
= false;
102 config
->append_to_multi_label_name
= true;
103 config
->search
.clear();
104 config
->search
.push_back("chromium.org");
105 config
->search
.push_back("example.com");
107 config
->nameservers
.clear();
108 for (unsigned i
= 0; i
< arraysize(kNameserversIPv4
) && i
< MAXNS
; ++i
) {
110 ParseIPLiteralToNumber(kNameserversIPv4
[i
], &ip
);
111 config
->nameservers
.push_back(IPEndPoint(ip
, NS_DEFAULTPORT
+ i
));
114 #if defined(OS_LINUX)
115 for (unsigned i
= 0; i
< arraysize(kNameserversIPv6
) && i
< MAXNS
; ++i
) {
116 if (!kNameserversIPv6
[i
])
119 ParseIPLiteralToNumber(kNameserversIPv6
[i
], &ip
);
120 config
->nameservers
[i
] = IPEndPoint(ip
, NS_DEFAULTPORT
- i
);
125 TEST(DnsConfigServicePosixTest
, ConvertResStateToDnsConfig
) {
126 struct __res_state res
;
128 EXPECT_FALSE(config
.IsValid());
129 InitializeResState(&res
);
130 ASSERT_EQ(internal::CONFIG_PARSE_POSIX_OK
,
131 internal::ConvertResStateToDnsConfig(res
, &config
));
133 EXPECT_TRUE(config
.IsValid());
135 DnsConfig expected_config
;
136 EXPECT_FALSE(expected_config
.EqualsIgnoreHosts(config
));
137 InitializeExpectedConfig(&expected_config
);
138 EXPECT_TRUE(expected_config
.EqualsIgnoreHosts(config
));
141 TEST(DnsConfigServicePosixTest
, RejectEmptyNameserver
) {
142 struct __res_state res
= {};
143 res
.options
= RES_INIT
| RES_RECURSE
| RES_DEFNAMES
| RES_DNSRCH
;
144 const char kDnsrch
[] = "chromium.org";
145 memcpy(res
.defdname
, kDnsrch
, sizeof(kDnsrch
));
146 res
.dnsrch
[0] = res
.defdname
;
148 struct sockaddr_in sa
= {};
149 sa
.sin_family
= AF_INET
;
150 sa
.sin_port
= base::HostToNet16(NS_DEFAULTPORT
);
151 sa
.sin_addr
.s_addr
= INADDR_ANY
;
152 res
.nsaddr_list
[0] = sa
;
153 sa
.sin_addr
.s_addr
= 0xCAFE1337;
154 res
.nsaddr_list
[1] = sa
;
158 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_NULL_ADDRESS
,
159 internal::ConvertResStateToDnsConfig(res
, &config
));
161 sa
.sin_addr
.s_addr
= 0xDEADBEEF;
162 res
.nsaddr_list
[0] = sa
;
163 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_OK
,
164 internal::ConvertResStateToDnsConfig(res
, &config
));
173 const char kTempHosts1
[] = "127.0.0.1 localhost";
174 const char kTempHosts2
[] = "127.0.0.2 localhost";
176 class DnsConfigServicePosixTest
: public testing::Test
{
178 DnsConfigServicePosixTest() : seen_config_(false) {}
179 ~DnsConfigServicePosixTest() override
{}
181 void OnConfigChanged(const DnsConfig
& config
) {
182 EXPECT_TRUE(config
.IsValid());
184 base::MessageLoop::current()->Quit();
187 void WriteMockHostsFile(const char* hosts_string
) {
188 ASSERT_EQ(base::WriteFile(temp_file_
, hosts_string
, strlen(hosts_string
)),
189 static_cast<int>(strlen(hosts_string
)));
192 void MockDNSConfig(const char* dns_server
) {
193 IPAddressNumber dns_number
;
194 ASSERT_TRUE(ParseIPLiteralToNumber(dns_server
, &dns_number
));
195 test_config_
.nameservers
.clear();
196 test_config_
.nameservers
.push_back(
197 IPEndPoint(dns_number
, dns_protocol::kDefaultPort
));
198 service_
->SetDnsConfigForTesting(&test_config_
);
201 void SetUp() override
{
202 // TODO(pauljensen): Get rid of GetExternalStorageDirectory() when
203 // crbug.com/475568 is fixed. For now creating a temp file in the
204 // default temp directory (/data/data/...) will cause FilePathWatcher
205 // to fail, so create the temp file in /sdcard.
206 base::FilePath parent_dir
;
207 ASSERT_TRUE(base::android::GetExternalStorageDirectory(&parent_dir
));
208 ASSERT_TRUE(base::CreateTemporaryFileInDir(parent_dir
, &temp_file_
));
209 WriteMockHostsFile(kTempHosts1
);
210 // Set the time on the hosts file back so it appears older than the
211 // 1s safety offset in DnsConfigServicePosix::SeenChangeSince().
212 // TODO(pauljensen): Switch from Sleep() to TouchFile() when
213 // crbug.com/475568 is fixed. For now TouchFile() will fail in /sdcard.
214 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1100));
215 // // Copy real hosts file's last modified time to mock hosts file.
216 // base::File hosts(base::FilePath(DnsConfigServicePosix::kFilePathHosts),
217 // base::File::FLAG_OPEN | base::File::FLAG_READ);
218 // base::File::Info hosts_info;
219 // ASSERT_TRUE(hosts.GetInfo(&hosts_info));
220 // ASSERT_TRUE(base::TouchFile(temp_file_, hosts_info.last_modified,
221 // hosts_info.last_accessed));
224 void TearDown() override
{ ASSERT_TRUE(base::DeleteFile(temp_file_
, false)); }
226 void StartWatching() {
227 creation_time_
= base::Time::Now();
228 service_
.reset(new DnsConfigServicePosix());
229 service_
->file_path_hosts_
= temp_file_
.value().c_str();
230 MockDNSConfig("8.8.8.8");
231 seen_config_
= false;
232 service_
->WatchConfig(base::Bind(
233 &DnsConfigServicePosixTest::OnConfigChanged
, base::Unretained(this)));
237 void ExpectChange() {
238 EXPECT_FALSE(seen_config_
);
239 base::MessageLoop::current()->Run();
240 EXPECT_TRUE(seen_config_
);
241 seen_config_
= false;
245 base::Time creation_time_
;
246 base::FilePath temp_file_
;
247 scoped_ptr
<DnsConfigServicePosix
> service_
;
248 DnsConfig test_config_
;
251 TEST_F(DnsConfigServicePosixTest
, SeenChangeSince
) {
252 // Verify SeenChangeSince() returns false if no changes
254 EXPECT_FALSE(service_
->SeenChangeSince(creation_time_
));
255 // Verify SeenChangeSince() returns true if network change
256 MockDNSConfig("8.8.4.4");
257 service_
->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI
);
258 EXPECT_TRUE(service_
->SeenChangeSince(creation_time_
));
260 // Verify SeenChangeSince() returns true if hosts file changes
262 EXPECT_FALSE(service_
->SeenChangeSince(creation_time_
));
263 WriteMockHostsFile(kTempHosts2
);
264 EXPECT_TRUE(service_
->SeenChangeSince(creation_time_
));
268 } // namespace internal