Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / dns / dns_config_service_posix_unittest.cc
blob4a9937014e225a2b33058725f9ad7e89c84e3f59
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.
5 #include <resolv.h>
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)
21 namespace net {
23 #if !defined(OS_ANDROID)
25 namespace {
27 // MAXNS is normally 3, but let's test 4 if possible.
28 const char* const kNameserversIPv4[] = {
29 "8.8.8.8",
30 "192.168.1.1",
31 "63.1.2.4",
32 "1.0.0.1",
35 #if defined(OS_LINUX)
36 const char* const kNameserversIPv6[] = {
37 NULL,
38 "2001:DB8:0::42",
39 NULL,
40 "::FFFF:129.144.52.38",
42 #endif
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 |
48 RES_ROTATE;
49 res->ndots = 2;
50 res->retrans = 4;
51 res->retry = 7;
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;
64 ++res->nscount;
67 #if defined(OS_LINUX)
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])
72 continue;
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]);
81 ++nscount6;
83 res->_u._ext.nscount6 = nscount6;
84 #endif
87 void CloseResState(res_state res) {
88 #if defined(OS_LINUX)
89 for (int i = 0; i < res->nscount; ++i) {
90 if (res->_u._ext.nsaddrs[i] != NULL)
91 free(res->_u._ext.nsaddrs[i]);
93 #endif
96 void InitializeExpectedConfig(DnsConfig* config) {
97 config->ndots = 2;
98 config->timeout = base::TimeDelta::FromSeconds(4);
99 config->attempts = 7;
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) {
109 IPAddressNumber ip;
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])
117 continue;
118 IPAddressNumber ip;
119 ParseIPLiteralToNumber(kNameserversIPv6[i], &ip);
120 config->nameservers[i] = IPEndPoint(ip, NS_DEFAULTPORT - i);
122 #endif
125 TEST(DnsConfigServicePosixTest, ConvertResStateToDnsConfig) {
126 struct __res_state res;
127 DnsConfig config;
128 EXPECT_FALSE(config.IsValid());
129 InitializeResState(&res);
130 ASSERT_EQ(internal::CONFIG_PARSE_POSIX_OK,
131 internal::ConvertResStateToDnsConfig(res, &config));
132 CloseResState(&res);
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;
155 res.nscount = 2;
157 DnsConfig config;
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));
167 } // namespace
169 #else // OS_ANDROID
171 namespace internal {
173 const char kTempHosts1[] = "127.0.0.1 localhost";
174 const char kTempHosts2[] = "127.0.0.2 localhost";
176 class DnsConfigServicePosixTest : public testing::Test {
177 public:
178 DnsConfigServicePosixTest() : seen_config_(false) {}
179 ~DnsConfigServicePosixTest() override {}
181 void OnConfigChanged(const DnsConfig& config) {
182 EXPECT_TRUE(config.IsValid());
183 seen_config_ = true;
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)));
234 ExpectChange();
237 void ExpectChange() {
238 EXPECT_FALSE(seen_config_);
239 base::MessageLoop::current()->Run();
240 EXPECT_TRUE(seen_config_);
241 seen_config_ = false;
244 bool seen_config_;
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
253 StartWatching();
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_));
259 ExpectChange();
260 // Verify SeenChangeSince() returns true if hosts file changes
261 StartWatching();
262 EXPECT_FALSE(service_->SeenChangeSince(creation_time_));
263 WriteMockHostsFile(kTempHosts2);
264 EXPECT_TRUE(service_->SeenChangeSince(creation_time_));
265 ExpectChange();
268 } // namespace internal
270 #endif // OS_ANDROID
272 } // namespace net