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

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

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/dns/address_sorter_posix.h" 5 #include "net/dns/address_sorter_posix.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "net/base/net_util.h" 10 #include "net/base/net_util.h"
(...skipping 13 matching lines...) Expand all
24 IPAddressNumber ParseIP(const std::string& str) { 24 IPAddressNumber ParseIP(const std::string& str) {
25 IPAddressNumber addr; 25 IPAddressNumber addr;
26 CHECK(ParseIPLiteralToNumber(str, &addr)); 26 CHECK(ParseIPLiteralToNumber(str, &addr));
27 return addr; 27 return addr;
28 } 28 }
29 29
30 // A mock socket which binds to source address according to AddressMapping. 30 // A mock socket which binds to source address according to AddressMapping.
31 class TestUDPClientSocket : public DatagramClientSocket { 31 class TestUDPClientSocket : public DatagramClientSocket {
32 public: 32 public:
33 explicit TestUDPClientSocket(const AddressMapping* mapping) 33 explicit TestUDPClientSocket(const AddressMapping* mapping)
34 : mapping_(mapping), connected_(false) {} 34 : mapping_(mapping), connected_(false) {}
35 35
36 virtual ~TestUDPClientSocket() {} 36 virtual ~TestUDPClientSocket() {}
37 37
38 virtual int Read(IOBuffer*, int, const CompletionCallback&) OVERRIDE { 38 virtual int Read(IOBuffer*, int, const CompletionCallback&) OVERRIDE {
39 NOTIMPLEMENTED(); 39 NOTIMPLEMENTED();
40 return OK; 40 return OK;
41 } 41 }
42 virtual int Write(IOBuffer*, int, const CompletionCallback&) OVERRIDE { 42 virtual int Write(IOBuffer*, int, const CompletionCallback&) OVERRIDE {
43 NOTIMPLEMENTED(); 43 NOTIMPLEMENTED();
44 return OK; 44 return OK;
45 } 45 }
46 virtual int SetReceiveBufferSize(int32) OVERRIDE { 46 virtual int SetReceiveBufferSize(int32) OVERRIDE { return OK; }
47 return OK; 47 virtual int SetSendBufferSize(int32) OVERRIDE { return OK; }
48 }
49 virtual int SetSendBufferSize(int32) OVERRIDE {
50 return OK;
51 }
52 48
53 virtual void Close() OVERRIDE {} 49 virtual void Close() OVERRIDE {}
54 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { 50 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
55 NOTIMPLEMENTED(); 51 NOTIMPLEMENTED();
56 return OK; 52 return OK;
57 } 53 }
58 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { 54 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
59 if (!connected_) 55 if (!connected_)
60 return ERR_UNEXPECTED; 56 return ERR_UNEXPECTED;
61 *address = local_endpoint_; 57 *address = local_endpoint_;
62 return OK; 58 return OK;
63 } 59 }
64 60
65 virtual int Connect(const IPEndPoint& remote) OVERRIDE { 61 virtual int Connect(const IPEndPoint& remote) OVERRIDE {
66 if (connected_) 62 if (connected_)
67 return ERR_UNEXPECTED; 63 return ERR_UNEXPECTED;
68 AddressMapping::const_iterator it = mapping_->find(remote.address()); 64 AddressMapping::const_iterator it = mapping_->find(remote.address());
69 if (it == mapping_->end()) 65 if (it == mapping_->end())
70 return ERR_FAILED; 66 return ERR_FAILED;
71 connected_ = true; 67 connected_ = true;
72 local_endpoint_ = IPEndPoint(it->second, 39874 /* arbitrary port */); 68 local_endpoint_ = IPEndPoint(it->second, 39874 /* arbitrary port */);
73 return OK; 69 return OK;
74 } 70 }
75 71
76 virtual const BoundNetLog& NetLog() const OVERRIDE { 72 virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; }
77 return net_log_;
78 }
79 73
80 private: 74 private:
81 BoundNetLog net_log_; 75 BoundNetLog net_log_;
82 const AddressMapping* mapping_; 76 const AddressMapping* mapping_;
83 bool connected_; 77 bool connected_;
84 IPEndPoint local_endpoint_; 78 IPEndPoint local_endpoint_;
85 79
86 DISALLOW_COPY_AND_ASSIGN(TestUDPClientSocket); 80 DISALLOW_COPY_AND_ASSIGN(TestUDPClientSocket);
87 }; 81 };
88 82
(...skipping 18 matching lines...) Expand all
107 return scoped_ptr<StreamSocket>(); 101 return scoped_ptr<StreamSocket>();
108 } 102 }
109 virtual scoped_ptr<SSLClientSocket> CreateSSLClientSocket( 103 virtual scoped_ptr<SSLClientSocket> CreateSSLClientSocket(
110 scoped_ptr<ClientSocketHandle>, 104 scoped_ptr<ClientSocketHandle>,
111 const HostPortPair&, 105 const HostPortPair&,
112 const SSLConfig&, 106 const SSLConfig&,
113 const SSLClientSocketContext&) OVERRIDE { 107 const SSLClientSocketContext&) OVERRIDE {
114 NOTIMPLEMENTED(); 108 NOTIMPLEMENTED();
115 return scoped_ptr<SSLClientSocket>(); 109 return scoped_ptr<SSLClientSocket>();
116 } 110 }
117 virtual void ClearSSLSessionCache() OVERRIDE { 111 virtual void ClearSSLSessionCache() OVERRIDE { NOTIMPLEMENTED(); }
118 NOTIMPLEMENTED();
119 }
120 112
121 void AddMapping(const IPAddressNumber& dst, const IPAddressNumber& src) { 113 void AddMapping(const IPAddressNumber& dst, const IPAddressNumber& src) {
122 mapping_[dst] = src; 114 mapping_[dst] = src;
123 } 115 }
124 116
125 private: 117 private:
126 AddressMapping mapping_; 118 AddressMapping mapping_;
127 119
128 DISALLOW_COPY_AND_ASSIGN(TestSocketFactory); 120 DISALLOW_COPY_AND_ASSIGN(TestSocketFactory);
129 }; 121 };
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 // after sorting. 153 // after sorting.
162 void Verify(const char* addresses[], const int order[]) { 154 void Verify(const char* addresses[], const int order[]) {
163 AddressList list; 155 AddressList list;
164 for (const char** addr = addresses; *addr != NULL; ++addr) 156 for (const char** addr = addresses; *addr != NULL; ++addr)
165 list.push_back(IPEndPoint(ParseIP(*addr), 80)); 157 list.push_back(IPEndPoint(ParseIP(*addr), 80));
166 for (size_t i = 0; order[i] >= 0; ++i) 158 for (size_t i = 0; order[i] >= 0; ++i)
167 CHECK_LT(order[i], static_cast<int>(list.size())); 159 CHECK_LT(order[i], static_cast<int>(list.size()));
168 160
169 AddressList result; 161 AddressList result;
170 TestCompletionCallback callback; 162 TestCompletionCallback callback;
171 sorter_.Sort(list, base::Bind(&OnSortComplete, &result, 163 sorter_.Sort(list,
172 callback.callback())); 164 base::Bind(&OnSortComplete, &result, callback.callback()));
173 callback.WaitForResult(); 165 callback.WaitForResult();
174 166
175 for (size_t i = 0; (i < result.size()) || (order[i] >= 0); ++i) { 167 for (size_t i = 0; (i < result.size()) || (order[i] >= 0); ++i) {
176 IPEndPoint expected = order[i] >= 0 ? list[order[i]] : IPEndPoint(); 168 IPEndPoint expected = order[i] >= 0 ? list[order[i]] : IPEndPoint();
177 IPEndPoint actual = i < result.size() ? result[i] : IPEndPoint(); 169 IPEndPoint actual = i < result.size() ? result[i] : IPEndPoint();
178 EXPECT_TRUE(expected.address() == actual.address()) << 170 EXPECT_TRUE(expected.address() == actual.address())
179 "Address out of order at position " << i << "\n" << 171 << "Address out of order at position " << i << "\n"
180 " Actual: " << actual.ToStringWithoutPort() << "\n" << 172 << " Actual: " << actual.ToStringWithoutPort() << "\n"
181 "Expected: " << expected.ToStringWithoutPort(); 173 << "Expected: " << expected.ToStringWithoutPort();
182 } 174 }
183 } 175 }
184 176
185 TestSocketFactory socket_factory_; 177 TestSocketFactory socket_factory_;
186 AddressSorterPosix sorter_; 178 AddressSorterPosix sorter_;
187 }; 179 };
188 180
189 // Rule 1: Avoid unusable destinations. 181 // Rule 1: Avoid unusable destinations.
190 TEST_F(AddressSorterPosixTest, Rule1) { 182 TEST_F(AddressSorterPosixTest, Rule1) {
191 AddMapping("10.0.0.231", "10.0.0.1"); 183 AddMapping("10.0.0.231", "10.0.0.1");
192 const char* addresses[] = { "::1", "10.0.0.231", "127.0.0.1", NULL }; 184 const char* addresses[] = {"::1", "10.0.0.231", "127.0.0.1", NULL};
193 const int order[] = { 1, -1 }; 185 const int order[] = {1, -1};
194 Verify(addresses, order); 186 Verify(addresses, order);
195 } 187 }
196 188
197 // Rule 2: Prefer matching scope. 189 // Rule 2: Prefer matching scope.
198 TEST_F(AddressSorterPosixTest, Rule2) { 190 TEST_F(AddressSorterPosixTest, Rule2) {
199 AddMapping("3002::1", "4000::10"); // matching global 191 AddMapping("3002::1", "4000::10"); // matching global
200 AddMapping("ff32::1", "fe81::10"); // matching link-local 192 AddMapping("ff32::1", "fe81::10"); // matching link-local
201 AddMapping("fec1::1", "fec1::10"); // matching node-local 193 AddMapping("fec1::1", "fec1::10"); // matching node-local
202 AddMapping("3002::2", "::1"); // global vs. link-local 194 AddMapping("3002::2", "::1"); // global vs. link-local
203 AddMapping("fec1::2", "fe81::10"); // site-local vs. link-local 195 AddMapping("fec1::2", "fe81::10"); // site-local vs. link-local
204 AddMapping("8.0.0.1", "169.254.0.10"); // global vs. link-local 196 AddMapping("8.0.0.1", "169.254.0.10"); // global vs. link-local
205 // In all three cases, matching scope is preferred. 197 // In all three cases, matching scope is preferred.
206 const int order[] = { 1, 0, -1 }; 198 const int order[] = {1, 0, -1};
207 const char* addresses1[] = { "3002::2", "3002::1", NULL }; 199 const char* addresses1[] = {"3002::2", "3002::1", NULL};
208 Verify(addresses1, order); 200 Verify(addresses1, order);
209 const char* addresses2[] = { "fec1::2", "ff32::1", NULL }; 201 const char* addresses2[] = {"fec1::2", "ff32::1", NULL};
210 Verify(addresses2, order); 202 Verify(addresses2, order);
211 const char* addresses3[] = { "8.0.0.1", "fec1::1", NULL }; 203 const char* addresses3[] = {"8.0.0.1", "fec1::1", NULL};
212 Verify(addresses3, order); 204 Verify(addresses3, order);
213 } 205 }
214 206
215 // Rule 3: Avoid deprecated addresses. 207 // Rule 3: Avoid deprecated addresses.
216 TEST_F(AddressSorterPosixTest, Rule3) { 208 TEST_F(AddressSorterPosixTest, Rule3) {
217 // Matching scope. 209 // Matching scope.
218 AddMapping("3002::1", "4000::10"); 210 AddMapping("3002::1", "4000::10");
219 GetSourceInfo("4000::10")->deprecated = true; 211 GetSourceInfo("4000::10")->deprecated = true;
220 AddMapping("3002::2", "4000::20"); 212 AddMapping("3002::2", "4000::20");
221 const char* addresses[] = { "3002::1", "3002::2", NULL }; 213 const char* addresses[] = {"3002::1", "3002::2", NULL};
222 const int order[] = { 1, 0, -1 }; 214 const int order[] = {1, 0, -1};
223 Verify(addresses, order); 215 Verify(addresses, order);
224 } 216 }
225 217
226 // Rule 4: Prefer home addresses. 218 // Rule 4: Prefer home addresses.
227 TEST_F(AddressSorterPosixTest, Rule4) { 219 TEST_F(AddressSorterPosixTest, Rule4) {
228 AddMapping("3002::1", "4000::10"); 220 AddMapping("3002::1", "4000::10");
229 AddMapping("3002::2", "4000::20"); 221 AddMapping("3002::2", "4000::20");
230 GetSourceInfo("4000::20")->home = true; 222 GetSourceInfo("4000::20")->home = true;
231 const char* addresses[] = { "3002::1", "3002::2", NULL }; 223 const char* addresses[] = {"3002::1", "3002::2", NULL};
232 const int order[] = { 1, 0, -1 }; 224 const int order[] = {1, 0, -1};
233 Verify(addresses, order); 225 Verify(addresses, order);
234 } 226 }
235 227
236 // Rule 5: Prefer matching label. 228 // Rule 5: Prefer matching label.
237 TEST_F(AddressSorterPosixTest, Rule5) { 229 TEST_F(AddressSorterPosixTest, Rule5) {
238 AddMapping("::1", "::1"); // matching loopback 230 AddMapping("::1", "::1"); // matching loopback
239 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // matching IPv4-mapped 231 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // matching IPv4-mapped
240 AddMapping("2001::1", "::ffff:1234:10"); // Teredo vs. IPv4-mapped 232 AddMapping("2001::1", "::ffff:1234:10"); // Teredo vs. IPv4-mapped
241 AddMapping("2002::1", "2001::10"); // 6to4 vs. Teredo 233 AddMapping("2002::1", "2001::10"); // 6to4 vs. Teredo
242 const int order[] = { 1, 0, -1 }; 234 const int order[] = {1, 0, -1};
243 { 235 {
244 const char* addresses[] = { "2001::1", "::1", NULL }; 236 const char* addresses[] = {"2001::1", "::1", NULL};
245 Verify(addresses, order); 237 Verify(addresses, order);
246 } 238 }
247 { 239 {
248 const char* addresses[] = { "2002::1", "::ffff:1234:1", NULL }; 240 const char* addresses[] = {"2002::1", "::ffff:1234:1", NULL};
249 Verify(addresses, order); 241 Verify(addresses, order);
250 } 242 }
251 } 243 }
252 244
253 // Rule 6: Prefer higher precedence. 245 // Rule 6: Prefer higher precedence.
254 TEST_F(AddressSorterPosixTest, Rule6) { 246 TEST_F(AddressSorterPosixTest, Rule6) {
255 AddMapping("::1", "::1"); // loopback 247 AddMapping("::1", "::1"); // loopback
256 AddMapping("ff32::1", "fe81::10"); // multicast 248 AddMapping("ff32::1", "fe81::10"); // multicast
257 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // IPv4-mapped 249 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // IPv4-mapped
258 AddMapping("2001::1", "2001::10"); // Teredo 250 AddMapping("2001::1", "2001::10"); // Teredo
259 const char* addresses[] = { "2001::1", "::ffff:1234:1", "ff32::1", "::1", 251 const char* addresses[] = {"2001::1", "::ffff:1234:1", "ff32::1", "::1",
260 NULL }; 252 NULL};
261 const int order[] = { 3, 2, 1, 0, -1 }; 253 const int order[] = {3, 2, 1, 0, -1};
262 Verify(addresses, order); 254 Verify(addresses, order);
263 } 255 }
264 256
265 // Rule 7: Prefer native transport. 257 // Rule 7: Prefer native transport.
266 TEST_F(AddressSorterPosixTest, Rule7) { 258 TEST_F(AddressSorterPosixTest, Rule7) {
267 AddMapping("3002::1", "4000::10"); 259 AddMapping("3002::1", "4000::10");
268 AddMapping("3002::2", "4000::20"); 260 AddMapping("3002::2", "4000::20");
269 GetSourceInfo("4000::20")->native = true; 261 GetSourceInfo("4000::20")->native = true;
270 const char* addresses[] = { "3002::1", "3002::2", NULL }; 262 const char* addresses[] = {"3002::1", "3002::2", NULL};
271 const int order[] = { 1, 0, -1 }; 263 const int order[] = {1, 0, -1};
272 Verify(addresses, order); 264 Verify(addresses, order);
273 } 265 }
274 266
275 // Rule 8: Prefer smaller scope. 267 // Rule 8: Prefer smaller scope.
276 TEST_F(AddressSorterPosixTest, Rule8) { 268 TEST_F(AddressSorterPosixTest, Rule8) {
277 // Matching scope. Should precede the others by Rule 2. 269 // Matching scope. Should precede the others by Rule 2.
278 AddMapping("fe81::1", "fe81::10"); // link-local 270 AddMapping("fe81::1", "fe81::10"); // link-local
279 AddMapping("3000::1", "4000::10"); // global 271 AddMapping("3000::1", "4000::10"); // global
280 // Mismatched scope. 272 // Mismatched scope.
281 AddMapping("ff32::1", "4000::10"); // link-local 273 AddMapping("ff32::1", "4000::10"); // link-local
282 AddMapping("ff35::1", "4000::10"); // site-local 274 AddMapping("ff35::1", "4000::10"); // site-local
283 AddMapping("ff38::1", "4000::10"); // org-local 275 AddMapping("ff38::1", "4000::10"); // org-local
284 const char* addresses[] = { "ff38::1", "3000::1", "ff35::1", "ff32::1", 276 const char* addresses[] = {"ff38::1", "3000::1", "ff35::1",
285 "fe81::1", NULL }; 277 "ff32::1", "fe81::1", NULL};
286 const int order[] = { 4, 1, 3, 2, 0, -1 }; 278 const int order[] = {4, 1, 3, 2, 0, -1};
287 Verify(addresses, order); 279 Verify(addresses, order);
288 } 280 }
289 281
290 // Rule 9: Use longest matching prefix. 282 // Rule 9: Use longest matching prefix.
291 TEST_F(AddressSorterPosixTest, Rule9) { 283 TEST_F(AddressSorterPosixTest, Rule9) {
292 AddMapping("3000::1", "3000:ffff::10"); // 16 bit match 284 AddMapping("3000::1", "3000:ffff::10"); // 16 bit match
293 GetSourceInfo("3000:ffff::10")->prefix_length = 16; 285 GetSourceInfo("3000:ffff::10")->prefix_length = 16;
294 AddMapping("4000::1", "4000::10"); // 123 bit match, limited to 15 286 AddMapping("4000::1", "4000::10"); // 123 bit match, limited to 15
295 GetSourceInfo("4000::10")->prefix_length = 15; 287 GetSourceInfo("4000::10")->prefix_length = 15;
296 AddMapping("4002::1", "4000::10"); // 14 bit match 288 AddMapping("4002::1", "4000::10"); // 14 bit match
297 AddMapping("4080::1", "4000::10"); // 8 bit match 289 AddMapping("4080::1", "4000::10"); // 8 bit match
298 const char* addresses[] = { "4080::1", "4002::1", "4000::1", "3000::1", 290 const char* addresses[] = {"4080::1", "4002::1", "4000::1", "3000::1", NULL};
299 NULL }; 291 const int order[] = {3, 2, 1, 0, -1};
300 const int order[] = { 3, 2, 1, 0, -1 };
301 Verify(addresses, order); 292 Verify(addresses, order);
302 } 293 }
303 294
304 // Rule 10: Leave the order unchanged. 295 // Rule 10: Leave the order unchanged.
305 TEST_F(AddressSorterPosixTest, Rule10) { 296 TEST_F(AddressSorterPosixTest, Rule10) {
306 AddMapping("4000::1", "4000::10"); 297 AddMapping("4000::1", "4000::10");
307 AddMapping("4000::2", "4000::10"); 298 AddMapping("4000::2", "4000::10");
308 AddMapping("4000::3", "4000::10"); 299 AddMapping("4000::3", "4000::10");
309 const char* addresses[] = { "4000::1", "4000::2", "4000::3", NULL }; 300 const char* addresses[] = {"4000::1", "4000::2", "4000::3", NULL};
310 const int order[] = { 0, 1, 2, -1 }; 301 const int order[] = {0, 1, 2, -1};
311 Verify(addresses, order); 302 Verify(addresses, order);
312 } 303 }
313 304
314 TEST_F(AddressSorterPosixTest, MultipleRules) { 305 TEST_F(AddressSorterPosixTest, MultipleRules) {
315 AddMapping("::1", "::1"); // loopback 306 AddMapping("::1", "::1"); // loopback
316 AddMapping("ff32::1", "fe81::10"); // link-local multicast 307 AddMapping("ff32::1", "fe81::10"); // link-local multicast
317 AddMapping("ff3e::1", "4000::10"); // global multicast 308 AddMapping("ff3e::1", "4000::10"); // global multicast
318 AddMapping("4000::1", "4000::10"); // global unicast 309 AddMapping("4000::1", "4000::10"); // global unicast
319 AddMapping("ff32::2", "fe81::20"); // deprecated link-local multicast 310 AddMapping("ff32::2", "fe81::20"); // deprecated link-local multicast
320 GetSourceInfo("fe81::20")->deprecated = true; 311 GetSourceInfo("fe81::20")->deprecated = true;
321 const char* addresses[] = { "ff3e::1", "ff32::2", "4000::1", "ff32::1", "::1", 312 const char* addresses[] = {"ff3e::1", "ff32::2", "4000::1", "ff32::1",
322 "8.0.0.1", NULL }; 313 "::1", "8.0.0.1", NULL};
323 const int order[] = { 4, 3, 0, 2, 1, -1 }; 314 const int order[] = {4, 3, 0, 2, 1, -1};
324 Verify(addresses, order); 315 Verify(addresses, order);
325 } 316 }
326 317
327 } // namespace net 318 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698