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