Enabling tests which should be fixed by r173829.
[chromium-blink-merge.git] / net / dns / dns_config_service_posix_unittest.cc
blob92e46ed19777f7b2eeb47c46ccc56d3ab03c112a
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/sys_byteorder.h"
8 #include "net/dns/dns_config_service_posix.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace net {
13 namespace {
15 // MAXNS is normally 3, but let's test 4 if possible.
16 const char* kNameserversIPv4[] = {
17 "8.8.8.8",
18 "192.168.1.1",
19 "63.1.2.4",
20 "1.0.0.1",
23 #if defined(OS_LINUX)
24 const char* kNameserversIPv6[] = {
25 NULL,
26 "2001:DB8:0::42",
27 NULL,
28 "::FFFF:129.144.52.38",
30 #endif
32 // Fills in |res| with sane configuration.
33 void InitializeResState(res_state res) {
34 memset(res, 0, sizeof(*res));
35 res->options = RES_INIT | RES_RECURSE | RES_DEFNAMES | RES_DNSRCH |
36 RES_ROTATE;
37 res->ndots = 2;
38 res->retrans = 4;
39 res->retry = 7;
41 const char kDnsrch[] = "chromium.org" "\0" "example.com";
42 memcpy(res->defdname, kDnsrch, sizeof(kDnsrch));
43 res->dnsrch[0] = res->defdname;
44 res->dnsrch[1] = res->defdname + sizeof("chromium.org");
46 for (unsigned i = 0; i < arraysize(kNameserversIPv4) && i < MAXNS; ++i) {
47 struct sockaddr_in sa;
48 sa.sin_family = AF_INET;
49 sa.sin_port = base::HostToNet16(NS_DEFAULTPORT + i);
50 inet_pton(AF_INET, kNameserversIPv4[i], &sa.sin_addr);
51 res->nsaddr_list[i] = sa;
52 ++res->nscount;
55 #if defined(OS_LINUX)
56 // Install IPv6 addresses, replacing the corresponding IPv4 addresses.
57 unsigned nscount6 = 0;
58 for (unsigned i = 0; i < arraysize(kNameserversIPv6) && i < MAXNS; ++i) {
59 if (!kNameserversIPv6[i])
60 continue;
61 // Must use malloc to mimick res_ninit.
62 struct sockaddr_in6 *sa6;
63 sa6 = (struct sockaddr_in6 *)malloc(sizeof(*sa6));
64 sa6->sin6_family = AF_INET6;
65 sa6->sin6_port = base::HostToNet16(NS_DEFAULTPORT - i);
66 inet_pton(AF_INET6, kNameserversIPv6[i], &sa6->sin6_addr);
67 res->_u._ext.nsaddrs[i] = sa6;
68 memset(&res->nsaddr_list[i], 0, sizeof res->nsaddr_list[i]);
69 ++nscount6;
71 res->_u._ext.nscount6 = nscount6;
72 #endif
75 void CloseResState(res_state res) {
76 #if defined(OS_LINUX)
77 for (int i = 0; i < res->nscount; ++i) {
78 if (res->_u._ext.nsaddrs[i] != NULL)
79 free(res->_u._ext.nsaddrs[i]);
81 #endif
84 void InitializeExpectedConfig(DnsConfig* config) {
85 config->ndots = 2;
86 config->timeout = base::TimeDelta::FromSeconds(4);
87 config->attempts = 7;
88 config->rotate = true;
89 config->edns0 = false;
90 config->append_to_multi_label_name = true;
91 config->search.clear();
92 config->search.push_back("chromium.org");
93 config->search.push_back("example.com");
95 config->nameservers.clear();
96 for (unsigned i = 0; i < arraysize(kNameserversIPv4) && i < MAXNS; ++i) {
97 IPAddressNumber ip;
98 ParseIPLiteralToNumber(kNameserversIPv4[i], &ip);
99 config->nameservers.push_back(IPEndPoint(ip, NS_DEFAULTPORT + i));
102 #if defined(OS_LINUX)
103 for (unsigned i = 0; i < arraysize(kNameserversIPv6) && i < MAXNS; ++i) {
104 if (!kNameserversIPv6[i])
105 continue;
106 IPAddressNumber ip;
107 ParseIPLiteralToNumber(kNameserversIPv6[i], &ip);
108 config->nameservers[i] = IPEndPoint(ip, NS_DEFAULTPORT - i);
110 #endif
113 TEST(DnsConfigServicePosixTest, ConvertResStateToDnsConfig) {
114 struct __res_state res;
115 DnsConfig config;
116 EXPECT_FALSE(config.IsValid());
117 InitializeResState(&res);
118 ASSERT_EQ(internal::CONFIG_PARSE_POSIX_OK,
119 internal::ConvertResStateToDnsConfig(res, &config));
120 CloseResState(&res);
121 EXPECT_TRUE(config.IsValid());
123 DnsConfig expected_config;
124 EXPECT_FALSE(expected_config.EqualsIgnoreHosts(config));
125 InitializeExpectedConfig(&expected_config);
126 EXPECT_TRUE(expected_config.EqualsIgnoreHosts(config));
129 TEST(DnsConfigServicePosixTest, RejectEmptyNameserver) {
130 struct __res_state res = {};
131 res.options = RES_INIT | RES_RECURSE | RES_DEFNAMES | RES_DNSRCH;
132 const char kDnsrch[] = "chromium.org";
133 memcpy(res.defdname, kDnsrch, sizeof(kDnsrch));
134 res.dnsrch[0] = res.defdname;
136 struct sockaddr_in sa = {};
137 sa.sin_family = AF_INET;
138 sa.sin_port = base::HostToNet16(NS_DEFAULTPORT);
139 sa.sin_addr.s_addr = INADDR_ANY;
140 res.nsaddr_list[0] = sa;
141 sa.sin_addr.s_addr = 0xCAFE1337;
142 res.nsaddr_list[1] = sa;
143 res.nscount = 2;
145 DnsConfig config;
146 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_NULL_ADDRESS,
147 internal::ConvertResStateToDnsConfig(res, &config));
149 sa.sin_addr.s_addr = 0xDEADBEEF;
150 res.nsaddr_list[0] = sa;
151 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_OK,
152 internal::ConvertResStateToDnsConfig(res, &config));
155 } // namespace
156 } // namespace net