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/sys_byteorder.h"
8 #include "net/dns/dns_config_service_posix.h"
10 #include "testing/gtest/include/gtest/gtest.h"
15 // MAXNS is normally 3, but let's test 4 if possible.
16 const char* kNameserversIPv4
[] = {
24 const char* kNameserversIPv6
[] = {
28 "::FFFF:129.144.52.38",
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
|
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
;
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
])
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
]);
71 res
->_u
._ext
.nscount6
= nscount6
;
75 void CloseResState(res_state res
) {
77 for (int i
= 0; i
< res
->nscount
; ++i
) {
78 if (res
->_u
._ext
.nsaddrs
[i
] != NULL
)
79 free(res
->_u
._ext
.nsaddrs
[i
]);
84 void InitializeExpectedConfig(DnsConfig
* config
) {
86 config
->timeout
= base::TimeDelta::FromSeconds(4);
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
) {
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
])
107 ParseIPLiteralToNumber(kNameserversIPv6
[i
], &ip
);
108 config
->nameservers
[i
] = IPEndPoint(ip
, NS_DEFAULTPORT
- i
);
113 TEST(DnsConfigServicePosixTest
, ConvertResStateToDnsConfig
) {
114 struct __res_state res
;
116 EXPECT_FALSE(config
.IsValid());
117 InitializeResState(&res
);
118 ASSERT_EQ(internal::CONFIG_PARSE_POSIX_OK
,
119 internal::ConvertResStateToDnsConfig(res
, &config
));
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
;
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
));