Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: net/dns/address_sorter_posix_unittest.cc

Issue 10442098: [net/dns] Resolve AF_UNSPEC on dual-stacked systems. Sort addresses according to RFC3484. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Handle ifa_netmnask == NULL and other errors after getifaddrs. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "net/dns/address_sorter_posix.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/net_util.h"
12 #include "net/socket/client_socket_factory.h"
13 #include "net/udp/datagram_client_socket.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace net {
17 namespace {
18
19 // Used to map destination address to source address.
20 typedef std::map<IPAddressNumber, IPAddressNumber> AddressMapping;
21
22 IPAddressNumber ParseIP(const std::string& str) {
23 IPAddressNumber addr;
24 CHECK(ParseIPLiteralToNumber(str, &addr));
25 return addr;
26 }
27
28 // A mock socket which binds to source address according to AddressMapping.
29 class TestUDPClientSocket : public DatagramClientSocket {
30 public:
31 explicit TestUDPClientSocket(const AddressMapping* mapping)
32 : mapping_(mapping), connected_(false) {}
33
34 virtual ~TestUDPClientSocket() {}
35
36 virtual int Read(IOBuffer*, int, const CompletionCallback&) OVERRIDE {
37 NOTIMPLEMENTED();
38 return OK;
39 }
40 virtual int Write(IOBuffer*, int, const CompletionCallback&) OVERRIDE {
41 NOTIMPLEMENTED();
42 return OK;
43 }
44 virtual bool SetReceiveBufferSize(int32) OVERRIDE {
45 return true;
46 }
47 virtual bool SetSendBufferSize(int32) OVERRIDE {
48 return true;
49 }
50
51 virtual void Close() OVERRIDE {}
52 virtual int GetPeerAddress(IPEndPoint* address) const {
53 if (!connected_)
54 return ERR_UNEXPECTED;
55 *address = remote_endpoint_;
56 return OK;
57 }
58 virtual int GetLocalAddress(IPEndPoint* address) const {
59 if (!connected_)
60 return ERR_UNEXPECTED;
61 *address = local_endpoint_;
62 return OK;
63 }
64
65 virtual int Connect(const IPEndPoint& remote) OVERRIDE {
66 if (connected_)
67 return ERR_UNEXPECTED;
68 AddressMapping::const_iterator it = mapping_->find(remote.address());
69 if (it == mapping_->end())
70 return ERR_FAILED;
71 connected_ = true;
72 remote_endpoint_ = remote;
73 local_endpoint_ = IPEndPoint(it->second, 39874 /* arbitrary port */);
74 return OK;
75 }
76
77 virtual const BoundNetLog& NetLog() const {
78 return net_log_;
79 }
80
81 private:
82 BoundNetLog net_log_;
83 const AddressMapping* mapping_;
84 bool connected_;
85 IPEndPoint remote_endpoint_;
86 IPEndPoint local_endpoint_;
87
88 DISALLOW_COPY_AND_ASSIGN(TestUDPClientSocket);
89 };
90
91 // Creates TestUDPClientSockets and maintains an AddressMapping.
92 class TestSocketFactory : public ClientSocketFactory {
93 public:
94 TestSocketFactory() {}
95 virtual ~TestSocketFactory() {}
96
97 virtual DatagramClientSocket* CreateDatagramClientSocket(
98 DatagramSocket::BindType,
99 const RandIntCallback&,
100 NetLog*,
101 const NetLog::Source&) OVERRIDE {
102 return new TestUDPClientSocket(&mapping_);
103 }
104 virtual StreamSocket* CreateTransportClientSocket(
105 const AddressList&,
106 NetLog*,
107 const NetLog::Source&) OVERRIDE {
108 NOTIMPLEMENTED();
109 return NULL;
110 }
111 virtual SSLClientSocket* CreateSSLClientSocket(
112 ClientSocketHandle*,
113 const HostPortPair&,
114 const SSLConfig&,
115 const SSLClientSocketContext&) OVERRIDE {
116 NOTIMPLEMENTED();
117 return NULL;
118 }
119 virtual void ClearSSLSessionCache() OVERRIDE {
120 NOTIMPLEMENTED();
121 }
122
123 void AddMapping(const IPAddressNumber& dst, const IPAddressNumber& src) {
124 mapping_[dst] = src;
125 }
126
127 private:
128 AddressMapping mapping_;
129
130 DISALLOW_COPY_AND_ASSIGN(TestSocketFactory);
131 };
132
133 void OnSortComplete(AddressList* result_buf,
134 base::WaitableEvent* event,
135 bool success,
136 const AddressList& result) {
137 EXPECT_TRUE(success);
138 if (success)
139 *result_buf = result;
140 event->Signal();
141 }
142
143 } // namespace
144
145 class AddressSorterPosixTest : public testing::Test {
146 protected:
147 AddressSorterPosixTest() : sorter_(&socket_factory_) {}
148
149 void AddMapping(const std::string& dst, const std::string& src) {
150 socket_factory_.AddMapping(ParseIP(dst), ParseIP(src));
151 }
152
153 SourceAddressInfo* GetSourceInfo(const std::string& addr) {
154 IPAddressNumber address = ParseIP(addr);
155 SourceAddressInfo* info = &sorter_.source_map_[address];
156 if (info->scope == SCOPE_UNDEFINED)
157 sorter_.FillPolicy(address, info);
158 return info;
159 }
160
161 // Verify that NULL-terminated |addresses| matches (-1)-terminated |order|
162 // after sorting.
163 void Verify(const char* addresses[], const int order[]) {
164 AddressList list;
165 for (const char** addr = addresses; *addr != NULL; ++addr)
166 list.push_back(IPEndPoint(ParseIP(*addr), 80));
167 for (size_t i = 0; order[i] >= 0; ++i)
168 CHECK_LT(order[i], static_cast<int>(list.size()));
169
170 base::WaitableEvent event(false, false);
171 AddressList result;
172 sorter_.Sort(list, base::Bind(&OnSortComplete, &result, &event));
173 event.Wait();
174
175 for (size_t i = 0; (i < result.size()) || (order[i] >= 0); ++i) {
176 IPEndPoint expected = order[i] >= 0 ? list[order[i]] : IPEndPoint();
177 IPEndPoint actual = i < result.size() ? result[i] : IPEndPoint();
178 EXPECT_TRUE(expected.address() == actual.address()) <<
179 "Address out of order at position " << i << "\n" <<
180 " Actual: " << actual.ToStringWithoutPort() << "\n" <<
181 "Expected: " << expected.ToStringWithoutPort();
182 }
183 }
184
185 TestSocketFactory socket_factory_;
186 AddressSorterPosix sorter_;
187 };
188
189 // Rule 1: Avoid unusable destinations.
190 TEST_F(AddressSorterPosixTest, Rule1) {
191 AddMapping("10.0.0.231", "10.0.0.1");
192 const char* addresses[] = { "1.2.3.4", "10.0.0.231", "127.0.0.1", NULL };
193 const int order[] = { 1, -1 };
194 Verify(addresses, order);
195 }
196
197 // Rule 2: Prefer matching scope.
198 TEST_F(AddressSorterPosixTest, Rule2) {
199 AddMapping("3002::1", "4000::10"); // matching global
200 AddMapping("ff32::1", "fe81::10"); // matching link-local
201 AddMapping("fec1::1", "fec1::10"); // matching node-local
202 AddMapping("8.0.0.1", "169.254.0.10"); // global vs. link-local
203 AddMapping("3002::2", "::1"); // global vs. link-local
204 AddMapping("fec1::2", "fe81::10"); // site-local vs. link-local
205 // In all three cases, matching scope is preferred.
206 const int order[] = { 1, 0, -1 };
207 {
208 const char* addresses[] = { "3002::2", "3002::1", NULL };
209 Verify(addresses, order);
210 }
211 {
212 const char* addresses[] = { "fec1::2", "ff32::1", NULL };
213 Verify(addresses, order);
214 }
215 {
216 const char* addresses[] = { "8.0.0.1", "fec1::1", NULL };
217 Verify(addresses, order);
218 }
219 }
220
221 // Rule 3: Avoid deprecated addresses.
222 TEST_F(AddressSorterPosixTest, Rule3) {
223 // Matching scope.
224 AddMapping("3002::1", "4000::10");
225 GetSourceInfo("4000::10")->deprecated = true;
226 AddMapping("3002::2", "4000::20");
227 const char* addresses[] = { "3002::1", "3002::2", NULL };
228 const int order[] = { 1, 0, -1 };
229 Verify(addresses, order);
230 }
231
232 // Rule 4: Prefer home addresses.
233 TEST_F(AddressSorterPosixTest, Rule4) {
234 AddMapping("3002::1", "4000::10");
235 AddMapping("3002::2", "4000::20");
236 GetSourceInfo("4000::20")->home = true;
237 const char* addresses[] = { "3002::1", "3002::2", NULL };
238 const int order[] = { 1, 0, -1 };
239 Verify(addresses, order);
240 }
241
242 // Rule 5: Prefer matching label.
243 TEST_F(AddressSorterPosixTest, Rule5) {
244 AddMapping("::1", "::1"); // matching loopback
245 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // matching IPv4-mapped
246 AddMapping("2001::1", "::ffff:1234:10"); // Teredo vs. IPv4-mapped
247 AddMapping("2002::1", "2001::10"); // 6to4 vs. Teredo
248 const int order[] = { 1, 0, -1 };
249 {
250 const char* addresses[] = { "2001::1", "::1", NULL };
251 Verify(addresses, order);
252 }
253 {
254 const char* addresses[] = { "2002::1", "::ffff:1234:1", NULL };
255 Verify(addresses, order);
256 }
257 }
258
259 // Rule 6: Prefer higher precedence.
260 TEST_F(AddressSorterPosixTest, Rule6) {
261 AddMapping("::1", "::1"); // loopback
262 AddMapping("ff32::1", "fe81::10"); // multicast
263 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // IPv4-mapped
264 AddMapping("2001::1", "2001::10"); // Teredo
265 const char* addresses[] = { "2001::1", "::ffff:1234:1", "ff32::1", "::1",
266 NULL };
267 const int order[] = { 3, 2, 1, 0, -1 };
268 Verify(addresses, order);
269 }
270
271 // Rule 7: Prefer native transport.
272 TEST_F(AddressSorterPosixTest, Rule7) {
273 AddMapping("3002::1", "4000::10");
274 AddMapping("3002::2", "4000::20");
275 GetSourceInfo("4000::20")->native = true;
276 const char* addresses[] = { "3002::1", "3002::2", NULL };
277 const int order[] = { 1, 0, -1 };
278 Verify(addresses, order);
279 }
280
281 // Rule 8: Prefer smaller scope.
282 TEST_F(AddressSorterPosixTest, Rule8) {
283 AddMapping("3000::1", "4000::10"); // matching global
284 AddMapping("fe81::1", "fe81::10"); // matching link-local
285 AddMapping("ff32::1", "4000::10"); // link-local
286 AddMapping("ff35::1", "4000::10"); // site-local
287 AddMapping("ff38::1", "4000::10"); // org-local
288 const char* addresses[] = { "ff38::1",
289 "3000::1",
290 "ff35::1",
291 "ff32::1",
292 "fe81::1",
293 NULL };
294 const int order[] = { 4, 1, 3, 2, 0, -1 };
295 Verify(addresses, order);
296 }
297
298 // Rule 9: Use longest matching prefix.
299 TEST_F(AddressSorterPosixTest, Rule9) {
300 GetSourceInfo("4000::10")->prefix_length = 15;
301 GetSourceInfo("3000:ffff::10")->prefix_length = 16;
302 AddMapping("4002::1", "4000::10"); // 14 bit match
303 AddMapping("4000::1", "4000::10"); // 120 bit match, limited to 15
304 AddMapping("3000::1", "3000:ffff::10"); // 16 bit match
305 const char* addresses[] = { "4002::1", "4000::1", "3000::1", NULL };
306 const int order[] = { 2, 1, 0, -1 };
307 Verify(addresses, order);
308 }
309
310 // Rule 10: Leave the order unchanged.
311 TEST_F(AddressSorterPosixTest, Rule10) {
312 AddMapping("4000::1", "4000::10");
313 AddMapping("4000::2", "4000::10");
314 AddMapping("4000::3", "4000::10");
315 const char* addresses[] = { "4000::1", "4000::2", "4000::3", NULL };
316 const int order[] = { 0, 1, 2, -1 };
317 Verify(addresses, order);
318 }
319
320 TEST_F(AddressSorterPosixTest, MultipleRules) {
321 AddMapping("::1", "::1"); // loopback
322 AddMapping("ff32::1", "fe81::10"); // link-local multicast
323 GetSourceInfo("fe81::20")->deprecated = true;
324 AddMapping("ff32::2", "fe81::20"); // deprecated link-local multicast
325 AddMapping("ff3e::1", "4000::10"); // global multicast, mismatched label
326 AddMapping("4000::1", "4000::10"); // global unicast
327 const char* addresses[] = { "ff3e::1", "ff32::2", "4000::1", "ff32::1", "::1",
328 "8.0.0.1", NULL };
329 const int order[] = { 4, 3, 0, 2, 1, -1 };
330 Verify(addresses, order);
331 }
332
333 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698