1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/address_list.h"
#include "base/memory/scoped_ptr.h"
#include "base/string_util.h"
#include "net/base/host_resolver_proc.h"
#include "net/base/net_util.h"
#include "net/base/sys_addrinfo.h"
#if defined(OS_WIN)
#include "net/base/winsock_init.h"
#endif
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
void MutableSetPort(uint16 port, AddressList* addrlist) {
struct addrinfo* mutable_head =
const_cast<struct addrinfo*>(addrlist->head());
SetPortForAllAddrinfos(mutable_head, port);
}
// Use getaddrinfo() to allocate an addrinfo structure.
int CreateAddressList(const std::string& hostname, int port,
AddressList* addrlist) {
#if defined(OS_WIN)
EnsureWinsockInit();
#endif
int rv = SystemHostResolverProc(hostname,
ADDRESS_FAMILY_UNSPECIFIED,
0,
addrlist, NULL);
if (rv == 0)
MutableSetPort(port, addrlist);
return rv;
}
void CreateLongAddressList(AddressList* addrlist, int port) {
EXPECT_EQ(0, CreateAddressList("192.168.1.1", port, addrlist));
AddressList second_list;
EXPECT_EQ(0, CreateAddressList("192.168.1.2", port, &second_list));
addrlist->Append(second_list.head());
}
TEST(AddressListTest, GetPort) {
AddressList addrlist;
EXPECT_EQ(0, CreateAddressList("192.168.1.1", 81, &addrlist));
EXPECT_EQ(81, addrlist.GetPort());
MutableSetPort(83, &addrlist);
EXPECT_EQ(83, addrlist.GetPort());
}
TEST(AddressListTest, SetPortMakesCopy) {
AddressList addrlist1;
EXPECT_EQ(0, CreateAddressList("192.168.1.1", 85, &addrlist1));
EXPECT_EQ(85, addrlist1.GetPort());
AddressList addrlist2 = addrlist1;
EXPECT_EQ(85, addrlist2.GetPort());
// addrlist1 should not be affected by the assignment to
// addrlist2.
addrlist1.SetPort(80);
EXPECT_EQ(80, addrlist1.GetPort());
EXPECT_EQ(85, addrlist2.GetPort());
}
TEST(AddressListTest, Assignment) {
AddressList addrlist1;
EXPECT_EQ(0, CreateAddressList("192.168.1.1", 85, &addrlist1));
EXPECT_EQ(85, addrlist1.GetPort());
// Should reference the same data as addrlist1 -- so when we change addrlist1
// both are changed.
AddressList addrlist2 = addrlist1;
EXPECT_EQ(85, addrlist2.GetPort());
MutableSetPort(80, &addrlist1);
EXPECT_EQ(80, addrlist1.GetPort());
EXPECT_EQ(80, addrlist2.GetPort());
}
TEST(AddressListTest, CopyRecursive) {
AddressList addrlist1;
CreateLongAddressList(&addrlist1, 85);
EXPECT_EQ(85, addrlist1.GetPort());
AddressList addrlist2 =
AddressList::CreateByCopying(addrlist1.head());
ASSERT_TRUE(addrlist2.head()->ai_next != NULL);
// addrlist1 is the same as addrlist2 at this point.
EXPECT_EQ(85, addrlist1.GetPort());
EXPECT_EQ(85, addrlist2.GetPort());
// Changes to addrlist1 are not reflected in addrlist2.
MutableSetPort(70, &addrlist1);
MutableSetPort(90, &addrlist2);
EXPECT_EQ(70, addrlist1.GetPort());
EXPECT_EQ(90, addrlist2.GetPort());
}
TEST(AddressListTest, CopyNonRecursive) {
AddressList addrlist1;
CreateLongAddressList(&addrlist1, 85);
EXPECT_EQ(85, addrlist1.GetPort());
AddressList addrlist2 =
AddressList::CreateByCopyingFirstAddress(addrlist1.head());
ASSERT_TRUE(addrlist2.head()->ai_next == NULL);
// addrlist1 is the same as addrlist2 at this point.
EXPECT_EQ(85, addrlist1.GetPort());
EXPECT_EQ(85, addrlist2.GetPort());
// Changes to addrlist1 are not reflected in addrlist2.
MutableSetPort(70, &addrlist1);
MutableSetPort(90, &addrlist2);
EXPECT_EQ(70, addrlist1.GetPort());
EXPECT_EQ(90, addrlist2.GetPort());
}
TEST(AddressListTest, Append) {
AddressList addrlist1;
EXPECT_EQ(0, CreateAddressList("192.168.1.1", 11, &addrlist1));
EXPECT_EQ(11, addrlist1.GetPort());
AddressList addrlist2;
EXPECT_EQ(0, CreateAddressList("192.168.1.2", 12, &addrlist2));
EXPECT_EQ(12, addrlist2.GetPort());
ASSERT_TRUE(addrlist1.head()->ai_next == NULL);
addrlist1.Append(addrlist2.head());
ASSERT_TRUE(addrlist1.head()->ai_next != NULL);
AddressList addrlist3 =
AddressList::CreateByCopyingFirstAddress(addrlist1.head()->ai_next);
EXPECT_EQ(12, addrlist3.GetPort());
}
static const char* kCanonicalHostname = "canonical.bar.com";
TEST(AddressListTest, Canonical) {
// Create an addrinfo with a canonical name.
sockaddr_in address;
// The contents of address do not matter for this test,
// so just zero-ing them out for consistency.
memset(&address, 0x0, sizeof(address));
struct addrinfo ai;
memset(&ai, 0x0, sizeof(ai));
ai.ai_family = AF_INET;
ai.ai_socktype = SOCK_STREAM;
ai.ai_addrlen = sizeof(address);
ai.ai_addr = reinterpret_cast<sockaddr*>(&address);
ai.ai_canonname = const_cast<char *>(kCanonicalHostname);
// Copy the addrinfo struct into an AddressList object and
// make sure it seems correct.
AddressList addrlist1 = AddressList::CreateByCopying(&ai);
const struct addrinfo* addrinfo1 = addrlist1.head();
EXPECT_TRUE(addrinfo1 != NULL);
EXPECT_TRUE(addrinfo1->ai_next == NULL);
std::string canon_name1;
EXPECT_TRUE(addrlist1.GetCanonicalName(&canon_name1));
EXPECT_EQ("canonical.bar.com", canon_name1);
// Copy the AddressList to another one.
AddressList addrlist2 = AddressList::CreateByCopying(addrinfo1);
const struct addrinfo* addrinfo2 = addrlist2.head();
EXPECT_TRUE(addrinfo2 != NULL);
EXPECT_TRUE(addrinfo2->ai_next == NULL);
EXPECT_TRUE(addrinfo2->ai_canonname != NULL);
EXPECT_NE(addrinfo1, addrinfo2);
EXPECT_NE(addrinfo1->ai_canonname, addrinfo2->ai_canonname);
std::string canon_name2;
EXPECT_TRUE(addrlist2.GetCanonicalName(&canon_name2));
EXPECT_EQ("canonical.bar.com", canon_name2);
// Make sure that GetCanonicalName correctly returns false
// when ai_canonname is NULL.
ai.ai_canonname = NULL;
AddressList addrlist_no_canon = AddressList::CreateByCopying(&ai);
std::string canon_name3 = "blah";
EXPECT_FALSE(addrlist_no_canon.GetCanonicalName(&canon_name3));
EXPECT_EQ("blah", canon_name3);
}
TEST(AddressListTest, IPLiteralConstructor) {
struct TestData {
std::string ip_address;
std::string canonical_ip_address;
bool is_ipv6;
} tests[] = {
{ "127.0.00.1", "127.0.0.1", false },
{ "192.168.1.1", "192.168.1.1", false },
{ "::1", "::1", true },
{ "2001:db8:0::42", "2001:db8::42", true },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
AddressList expected_list;
int rv = CreateAddressList(tests[i].canonical_ip_address, 80,
&expected_list);
if (tests[i].is_ipv6 && rv != 0) {
LOG(WARNING) << "Unable to resolve ip literal '" << tests[i].ip_address
<< "' test skipped.";
continue;
}
ASSERT_EQ(0, rv);
const struct addrinfo* good_ai = expected_list.head();
IPAddressNumber ip_number;
ParseIPLiteralToNumber(tests[i].ip_address, &ip_number);
AddressList test_list = AddressList::CreateFromIPAddressWithCname(
ip_number, 80, true);
const struct addrinfo* test_ai = test_list.head();
EXPECT_EQ(good_ai->ai_family, test_ai->ai_family);
EXPECT_EQ(good_ai->ai_socktype, test_ai->ai_socktype);
EXPECT_EQ(good_ai->ai_addrlen, test_ai->ai_addrlen);
size_t sockaddr_size =
good_ai->ai_socktype == AF_INET ? sizeof(struct sockaddr_in) :
good_ai->ai_socktype == AF_INET6 ? sizeof(struct sockaddr_in6) : 0;
EXPECT_EQ(memcmp(good_ai->ai_addr, test_ai->ai_addr, sockaddr_size), 0);
EXPECT_EQ(good_ai->ai_next, test_ai->ai_next);
EXPECT_EQ(strcmp(tests[i].canonical_ip_address.c_str(),
test_ai->ai_canonname), 0);
}
}
TEST(AddressListTest, AddressFromAddrInfo) {
struct TestData {
std::string ip_address;
std::string canonical_ip_address;
bool is_ipv6;
} tests[] = {
{ "127.0.00.1", "127.0.0.1", false },
{ "192.168.1.1", "192.168.1.1", false },
{ "::1", "::1", true },
{ "2001:db8:0::42", "2001:db8::42", true },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
AddressList expected_list;
int rv = CreateAddressList(tests[i].canonical_ip_address, 80,
&expected_list);
if (tests[i].is_ipv6 && rv != 0) {
LOG(WARNING) << "Unable to resolve ip literal '" << tests[i].ip_address
<< "' test skipped.";
continue;
}
ASSERT_EQ(0, rv);
const struct addrinfo* good_ai = expected_list.head();
AddressList test_list =
AddressList::CreateFromSockaddr(good_ai->ai_addr,
good_ai->ai_addrlen,
SOCK_STREAM,
IPPROTO_TCP);
const struct addrinfo* test_ai = test_list.head();
EXPECT_EQ(good_ai->ai_family, test_ai->ai_family);
EXPECT_EQ(good_ai->ai_addrlen, test_ai->ai_addrlen);
size_t sockaddr_size =
good_ai->ai_family == AF_INET ? sizeof(struct sockaddr_in) :
good_ai->ai_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0;
EXPECT_EQ(memcmp(good_ai->ai_addr, test_ai->ai_addr, sockaddr_size), 0);
EXPECT_EQ(good_ai->ai_next, test_ai->ai_next);
}
}
TEST(AddressListTest, CreateFromIPAddressList) {
struct TestData {
std::string ip_address;
const char* in_addr;
int ai_family;
size_t ai_addrlen;
size_t in_addr_offset;
size_t in_addr_size;
} tests[] = {
{ "127.0.0.1",
"\x7f\x00\x00\x01",
AF_INET,
sizeof(struct sockaddr_in),
offsetof(struct sockaddr_in, sin_addr),
sizeof(struct in_addr),
},
{ "2001:db8:0::42",
"\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x42",
AF_INET6,
sizeof(struct sockaddr_in6),
offsetof(struct sockaddr_in6, sin6_addr),
sizeof(struct in6_addr),
},
{ "192.168.1.1",
"\xc0\xa8\x01\x01",
AF_INET,
sizeof(struct sockaddr_in),
offsetof(struct sockaddr_in, sin_addr),
sizeof(struct in_addr),
},
};
const uint16 kPort = 80;
// Construct a list of ip addresses.
IPAddressList ip_list;
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
IPAddressNumber ip_number;
ParseIPLiteralToNumber(tests[i].ip_address, &ip_number);
ip_list.push_back(ip_number);
}
AddressList test_list = AddressList::CreateFromIPAddressList(ip_list, kPort);
EXPECT_EQ(kPort, test_list.GetPort());
// Make sure that CreateFromIPAddressList has created an addrinfo
// chain of exactly the same length as the |tests| with correct content.
const struct addrinfo* next_ai = test_list.head();
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
ASSERT_TRUE(next_ai != NULL);
EXPECT_EQ(tests[i].ai_family, next_ai->ai_family);
EXPECT_EQ(tests[i].ai_addrlen, next_ai->ai_addrlen);
char* ai_addr = reinterpret_cast<char*>(next_ai->ai_addr);
int rv = memcmp(tests[i].in_addr,
ai_addr + tests[i].in_addr_offset,
tests[i].in_addr_size);
EXPECT_EQ(0, rv);
next_ai = next_ai->ai_next;
}
EXPECT_EQ(NULL, next_ai);
}
} // namespace
} // namespace net
|