OLD | NEW |
---|---|
(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 "net/base/net_errors.h" | |
10 #include "net/base/net_util.h" | |
11 #include "net/base/test_completion_callback.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 OVERRIDE { | |
53 NOTIMPLEMENTED(); | |
54 return OK; | |
55 } | |
56 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { | |
57 if (!connected_) | |
58 return ERR_UNEXPECTED; | |
59 *address = local_endpoint_; | |
60 return OK; | |
61 } | |
62 | |
63 virtual int Connect(const IPEndPoint& remote) OVERRIDE { | |
64 if (connected_) | |
65 return ERR_UNEXPECTED; | |
66 AddressMapping::const_iterator it = mapping_->find(remote.address()); | |
67 if (it == mapping_->end()) | |
68 return ERR_FAILED; | |
69 connected_ = true; | |
70 local_endpoint_ = IPEndPoint(it->second, 39874 /* arbitrary port */); | |
71 return OK; | |
72 } | |
73 | |
74 virtual const BoundNetLog& NetLog() const OVERRIDE { | |
75 return net_log_; | |
76 } | |
77 | |
78 private: | |
79 BoundNetLog net_log_; | |
80 const AddressMapping* mapping_; | |
81 bool connected_; | |
82 IPEndPoint local_endpoint_; | |
83 | |
84 DISALLOW_COPY_AND_ASSIGN(TestUDPClientSocket); | |
85 }; | |
86 | |
87 // Creates TestUDPClientSockets and maintains an AddressMapping. | |
88 class TestSocketFactory : public ClientSocketFactory { | |
89 public: | |
90 TestSocketFactory() {} | |
91 virtual ~TestSocketFactory() {} | |
92 | |
93 virtual DatagramClientSocket* CreateDatagramClientSocket( | |
94 DatagramSocket::BindType, | |
95 const RandIntCallback&, | |
96 NetLog*, | |
97 const NetLog::Source&) OVERRIDE { | |
98 return new TestUDPClientSocket(&mapping_); | |
99 } | |
100 virtual StreamSocket* CreateTransportClientSocket( | |
101 const AddressList&, | |
102 NetLog*, | |
103 const NetLog::Source&) OVERRIDE { | |
104 NOTIMPLEMENTED(); | |
105 return NULL; | |
106 } | |
107 virtual SSLClientSocket* CreateSSLClientSocket( | |
108 ClientSocketHandle*, | |
109 const HostPortPair&, | |
110 const SSLConfig&, | |
111 const SSLClientSocketContext&) OVERRIDE { | |
112 NOTIMPLEMENTED(); | |
113 return NULL; | |
114 } | |
115 virtual void ClearSSLSessionCache() OVERRIDE { | |
116 NOTIMPLEMENTED(); | |
117 } | |
118 | |
119 void AddMapping(const IPAddressNumber& dst, const IPAddressNumber& src) { | |
120 mapping_[dst] = src; | |
121 } | |
122 | |
123 private: | |
124 AddressMapping mapping_; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(TestSocketFactory); | |
127 }; | |
128 | |
129 void OnSortComplete(AddressList* result_buf, | |
130 const CompletionCallback& callback, | |
131 bool success, | |
132 const AddressList& result) { | |
133 EXPECT_TRUE(success); | |
134 if (success) | |
135 *result_buf = result; | |
136 callback.Run(OK); | |
137 } | |
138 | |
139 } // namespace | |
140 | |
141 class AddressSorterPosixTest : public testing::Test { | |
142 protected: | |
143 AddressSorterPosixTest() : sorter_(&socket_factory_) {} | |
144 | |
145 void AddMapping(const std::string& dst, const std::string& src) { | |
146 socket_factory_.AddMapping(ParseIP(dst), ParseIP(src)); | |
147 } | |
148 | |
149 SourceAddressInfo* GetSourceInfo(const std::string& addr) { | |
150 IPAddressNumber address = ParseIP(addr); | |
151 SourceAddressInfo* info = &sorter_.source_map_[address]; | |
152 if (info->scope == SCOPE_UNDEFINED) | |
153 sorter_.FillPolicy(address, info); | |
154 return info; | |
155 } | |
156 | |
157 // Verify that NULL-terminated |addresses| matches (-1)-terminated |order| | |
158 // after sorting. | |
159 void Verify(const char* addresses[], const int order[]) { | |
160 AddressList list; | |
161 for (const char** addr = addresses; *addr != NULL; ++addr) | |
162 list.push_back(IPEndPoint(ParseIP(*addr), 80)); | |
163 for (size_t i = 0; order[i] >= 0; ++i) | |
164 CHECK_LT(order[i], static_cast<int>(list.size())); | |
165 | |
166 AddressList result; | |
167 TestCompletionCallback callback; | |
168 sorter_.Sort(list, base::Bind(&OnSortComplete, &result, | |
169 callback.callback())); | |
170 callback.WaitForResult(); | |
171 | |
172 for (size_t i = 0; (i < result.size()) || (order[i] >= 0); ++i) { | |
173 IPEndPoint expected = order[i] >= 0 ? list[order[i]] : IPEndPoint(); | |
174 IPEndPoint actual = i < result.size() ? result[i] : IPEndPoint(); | |
175 EXPECT_TRUE(expected.address() == actual.address()) << | |
176 "Address out of order at position " << i << "\n" << | |
177 " Actual: " << actual.ToStringWithoutPort() << "\n" << | |
178 "Expected: " << expected.ToStringWithoutPort(); | |
179 } | |
180 } | |
181 | |
182 TestSocketFactory socket_factory_; | |
183 AddressSorterPosix sorter_; | |
184 }; | |
185 | |
186 // Rule 1: Avoid unusable destinations. | |
187 TEST_F(AddressSorterPosixTest, Rule1) { | |
188 AddMapping("10.0.0.231", "10.0.0.1"); | |
189 const char* addresses[] = { "::1", "10.0.0.231", "127.0.0.1", NULL }; | |
190 const int order[] = { 1, -1 }; | |
191 Verify(addresses, order); | |
192 } | |
193 | |
194 // Rule 2: Prefer matching scope. | |
195 TEST_F(AddressSorterPosixTest, Rule2) { | |
196 AddMapping("3002::1", "4000::10"); // matching global | |
197 AddMapping("ff32::1", "fe81::10"); // matching link-local | |
198 AddMapping("fec1::1", "fec1::10"); // matching node-local | |
199 AddMapping("3002::2", "::1"); // global vs. link-local | |
200 AddMapping("fec1::2", "fe81::10"); // site-local vs. link-local | |
201 AddMapping("8.0.0.1", "169.254.0.10"); // global vs. link-local | |
202 // In all three cases, matching scope is preferred. | |
203 const int order[] = { 1, 0, -1 }; | |
204 const char* addresses1[] = { "3002::2", "3002::1", NULL }; | |
205 Verify(addresses1, order); | |
206 const char* addresses2[] = { "fec1::2", "ff32::1", NULL }; | |
207 Verify(addresses2, order); | |
208 const char* addresses3[] = { "8.0.0.1", "fec1::1", NULL }; | |
209 Verify(addresses3, order); | |
210 } | |
211 | |
212 // Rule 3: Avoid deprecated addresses. | |
213 TEST_F(AddressSorterPosixTest, Rule3) { | |
214 // Matching scope. | |
215 AddMapping("3002::1", "4000::10"); | |
216 GetSourceInfo("4000::10")->deprecated = true; | |
217 AddMapping("3002::2", "4000::20"); | |
218 const char* addresses[] = { "3002::1", "3002::2", NULL }; | |
219 const int order[] = { 1, 0, -1 }; | |
220 Verify(addresses, order); | |
221 } | |
222 | |
223 // Rule 4: Prefer home addresses. | |
224 TEST_F(AddressSorterPosixTest, Rule4) { | |
225 AddMapping("3002::1", "4000::10"); | |
226 AddMapping("3002::2", "4000::20"); | |
227 GetSourceInfo("4000::20")->home = true; | |
228 const char* addresses[] = { "3002::1", "3002::2", NULL }; | |
229 const int order[] = { 1, 0, -1 }; | |
230 Verify(addresses, order); | |
231 } | |
232 | |
233 // Rule 5: Prefer matching label. | |
234 TEST_F(AddressSorterPosixTest, Rule5) { | |
235 AddMapping("::1", "::1"); // matching loopback | |
236 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // matching IPv4-mapped | |
237 AddMapping("2001::1", "::ffff:1234:10"); // Teredo vs. IPv4-mapped | |
238 AddMapping("2002::1", "2001::10"); // 6to4 vs. Teredo | |
239 const int order[] = { 1, 0, -1 }; | |
240 { | |
241 const char* addresses[] = { "2001::1", "::1", NULL }; | |
242 Verify(addresses, order); | |
243 } | |
244 { | |
245 const char* addresses[] = { "2002::1", "::ffff:1234:1", NULL }; | |
246 Verify(addresses, order); | |
247 } | |
248 } | |
249 | |
250 // Rule 6: Prefer higher precedence. | |
251 TEST_F(AddressSorterPosixTest, Rule6) { | |
252 AddMapping("::1", "::1"); // loopback | |
253 AddMapping("ff32::1", "fe81::10"); // multicast | |
254 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // IPv4-mapped | |
255 AddMapping("2001::1", "2001::10"); // Teredo | |
256 const char* addresses[] = { "2001::1", "::ffff:1234:1", "ff32::1", "::1", | |
257 NULL }; | |
258 const int order[] = { 3, 2, 1, 0, -1 }; | |
259 Verify(addresses, order); | |
260 } | |
261 | |
262 // Rule 7: Prefer native transport. | |
263 TEST_F(AddressSorterPosixTest, Rule7) { | |
264 AddMapping("3002::1", "4000::10"); | |
265 AddMapping("3002::2", "4000::20"); | |
266 GetSourceInfo("4000::20")->native = true; | |
267 const char* addresses[] = { "3002::1", "3002::2", NULL }; | |
268 const int order[] = { 1, 0, -1 }; | |
269 Verify(addresses, order); | |
270 } | |
271 | |
272 // Rule 8: Prefer smaller scope. | |
273 TEST_F(AddressSorterPosixTest, Rule8) { | |
274 // Matching scope. Should precede the others by Rule 2. | |
275 AddMapping("fe81::1", "fe81::10"); // link-local | |
276 AddMapping("3000::1", "4000::10"); // global | |
277 // Mismatched scope. | |
278 AddMapping("ff32::1", "4000::10"); // link-local | |
279 AddMapping("ff35::1", "4000::10"); // site-local | |
280 AddMapping("ff38::1", "4000::10"); // org-local | |
mmenke
2012/08/13 20:11:50
nit: Two spaces before comments.
| |
281 const char* addresses[] = { "ff38::1", "3000::1", "ff35::1", "ff32::1", | |
282 "fe81::1", NULL }; | |
283 const int order[] = { 4, 1, 3, 2, 0, -1 }; | |
284 Verify(addresses, order); | |
285 } | |
286 | |
287 // Rule 9: Use longest matching prefix. | |
288 TEST_F(AddressSorterPosixTest, Rule9) { | |
289 AddMapping("3000::1", "3000:ffff::10"); // 16 bit match | |
290 GetSourceInfo("3000:ffff::10")->prefix_length = 16; | |
291 AddMapping("4000::1", "4000::10"); // 123 bit match, limited to 15 | |
292 GetSourceInfo("4000::10")->prefix_length = 15; | |
293 AddMapping("4002::1", "4000::10"); // 14 bit match | |
294 AddMapping("4080::1", "4000::10"); // 8 bit match | |
295 const char* addresses[] = { "4080::1", "4002::1", "4000::1", "3000::1", | |
296 NULL }; | |
297 const int order[] = { 3, 2, 1, 0, -1 }; | |
298 Verify(addresses, order); | |
299 } | |
300 | |
301 // Rule 10: Leave the order unchanged. | |
302 TEST_F(AddressSorterPosixTest, Rule10) { | |
303 AddMapping("4000::1", "4000::10"); | |
304 AddMapping("4000::2", "4000::10"); | |
305 AddMapping("4000::3", "4000::10"); | |
306 const char* addresses[] = { "4000::1", "4000::2", "4000::3", NULL }; | |
307 const int order[] = { 0, 1, 2, -1 }; | |
308 Verify(addresses, order); | |
309 } | |
310 | |
311 TEST_F(AddressSorterPosixTest, MultipleRules) { | |
312 AddMapping("::1", "::1"); // loopback | |
313 AddMapping("ff32::1", "fe81::10"); // link-local multicast | |
314 AddMapping("ff3e::1", "4000::10"); // global multicast | |
315 AddMapping("4000::1", "4000::10"); // global unicast | |
316 AddMapping("ff32::2", "fe81::20"); // deprecated link-local multicast | |
317 GetSourceInfo("fe81::20")->deprecated = true; | |
318 const char* addresses[] = { "ff3e::1", "ff32::2", "4000::1", "ff32::1", "::1", | |
319 "8.0.0.1", NULL }; | |
320 const int order[] = { 4, 3, 0, 2, 1, -1 }; | |
321 Verify(addresses, order); | |
322 } | |
323 | |
324 } // namespace net | |
OLD | NEW |