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"
12 #if !defined(OS_ANDROID)
17 // MAXNS is normally 3, but let's test 4 if possible.
18 const char* const kNameserversIPv4
[] = {
26 const char* const kNameserversIPv6
[] = {
30 "::FFFF:129.144.52.38",
34 // Fills in |res| with sane configuration.
35 void InitializeResState(res_state res
) {
36 memset(res
, 0, sizeof(*res
));
37 res
->options
= RES_INIT
| RES_RECURSE
| RES_DEFNAMES
| RES_DNSRCH
|
43 const char kDnsrch
[] = "chromium.org" "\0" "example.com";
44 memcpy(res
->defdname
, kDnsrch
, sizeof(kDnsrch
));
45 res
->dnsrch
[0] = res
->defdname
;
46 res
->dnsrch
[1] = res
->defdname
+ sizeof("chromium.org");
48 for (unsigned i
= 0; i
< arraysize(kNameserversIPv4
) && i
< MAXNS
; ++i
) {
49 struct sockaddr_in sa
;
50 sa
.sin_family
= AF_INET
;
51 sa
.sin_port
= base::HostToNet16(NS_DEFAULTPORT
+ i
);
52 inet_pton(AF_INET
, kNameserversIPv4
[i
], &sa
.sin_addr
);
53 res
->nsaddr_list
[i
] = sa
;
58 // Install IPv6 addresses, replacing the corresponding IPv4 addresses.
59 unsigned nscount6
= 0;
60 for (unsigned i
= 0; i
< arraysize(kNameserversIPv6
) && i
< MAXNS
; ++i
) {
61 if (!kNameserversIPv6
[i
])
63 // Must use malloc to mimick res_ninit.
64 struct sockaddr_in6
*sa6
;
65 sa6
= (struct sockaddr_in6
*)malloc(sizeof(*sa6
));
66 sa6
->sin6_family
= AF_INET6
;
67 sa6
->sin6_port
= base::HostToNet16(NS_DEFAULTPORT
- i
);
68 inet_pton(AF_INET6
, kNameserversIPv6
[i
], &sa6
->sin6_addr
);
69 res
->_u
._ext
.nsaddrs
[i
] = sa6
;
70 memset(&res
->nsaddr_list
[i
], 0, sizeof res
->nsaddr_list
[i
]);
73 res
->_u
._ext
.nscount6
= nscount6
;
77 void CloseResState(res_state res
) {
79 for (int i
= 0; i
< res
->nscount
; ++i
) {
80 if (res
->_u
._ext
.nsaddrs
[i
] != NULL
)
81 free(res
->_u
._ext
.nsaddrs
[i
]);
86 void InitializeExpectedConfig(DnsConfig
* config
) {
88 config
->timeout
= base::TimeDelta::FromSeconds(4);
90 config
->rotate
= true;
91 config
->edns0
= false;
92 config
->append_to_multi_label_name
= true;
93 config
->search
.clear();
94 config
->search
.push_back("chromium.org");
95 config
->search
.push_back("example.com");
97 config
->nameservers
.clear();
98 for (unsigned i
= 0; i
< arraysize(kNameserversIPv4
) && i
< MAXNS
; ++i
) {
100 ParseIPLiteralToNumber(kNameserversIPv4
[i
], &ip
);
101 config
->nameservers
.push_back(IPEndPoint(ip
, NS_DEFAULTPORT
+ i
));
104 #if defined(OS_LINUX)
105 for (unsigned i
= 0; i
< arraysize(kNameserversIPv6
) && i
< MAXNS
; ++i
) {
106 if (!kNameserversIPv6
[i
])
109 ParseIPLiteralToNumber(kNameserversIPv6
[i
], &ip
);
110 config
->nameservers
[i
] = IPEndPoint(ip
, NS_DEFAULTPORT
- i
);
115 TEST(DnsConfigServicePosixTest
, ConvertResStateToDnsConfig
) {
116 struct __res_state res
;
118 EXPECT_FALSE(config
.IsValid());
119 InitializeResState(&res
);
120 ASSERT_EQ(internal::CONFIG_PARSE_POSIX_OK
,
121 internal::ConvertResStateToDnsConfig(res
, &config
));
123 EXPECT_TRUE(config
.IsValid());
125 DnsConfig expected_config
;
126 EXPECT_FALSE(expected_config
.EqualsIgnoreHosts(config
));
127 InitializeExpectedConfig(&expected_config
);
128 EXPECT_TRUE(expected_config
.EqualsIgnoreHosts(config
));
131 TEST(DnsConfigServicePosixTest
, RejectEmptyNameserver
) {
132 struct __res_state res
= {};
133 res
.options
= RES_INIT
| RES_RECURSE
| RES_DEFNAMES
| RES_DNSRCH
;
134 const char kDnsrch
[] = "chromium.org";
135 memcpy(res
.defdname
, kDnsrch
, sizeof(kDnsrch
));
136 res
.dnsrch
[0] = res
.defdname
;
138 struct sockaddr_in sa
= {};
139 sa
.sin_family
= AF_INET
;
140 sa
.sin_port
= base::HostToNet16(NS_DEFAULTPORT
);
141 sa
.sin_addr
.s_addr
= INADDR_ANY
;
142 res
.nsaddr_list
[0] = sa
;
143 sa
.sin_addr
.s_addr
= 0xCAFE1337;
144 res
.nsaddr_list
[1] = sa
;
148 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_NULL_ADDRESS
,
149 internal::ConvertResStateToDnsConfig(res
, &config
));
151 sa
.sin_addr
.s_addr
= 0xDEADBEEF;
152 res
.nsaddr_list
[0] = sa
;
153 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_OK
,
154 internal::ConvertResStateToDnsConfig(res
, &config
));
160 #endif // !OS_ANDROID