| OLD | NEW |
| 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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 const std::string& addr) { | 144 const std::string& addr) { |
| 145 IPAddressNumber address = ParseIP(addr); | 145 IPAddressNumber address = ParseIP(addr); |
| 146 AddressSorterPosix::SourceAddressInfo* info = &sorter_.source_map_[address]; | 146 AddressSorterPosix::SourceAddressInfo* info = &sorter_.source_map_[address]; |
| 147 if (info->scope == AddressSorterPosix::SCOPE_UNDEFINED) | 147 if (info->scope == AddressSorterPosix::SCOPE_UNDEFINED) |
| 148 sorter_.FillPolicy(address, info); | 148 sorter_.FillPolicy(address, info); |
| 149 return info; | 149 return info; |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Verify that NULL-terminated |addresses| matches (-1)-terminated |order| | 152 // Verify that NULL-terminated |addresses| matches (-1)-terminated |order| |
| 153 // after sorting. | 153 // after sorting. |
| 154 void Verify(const char* addresses[], const int order[]) { | 154 void Verify(const char* const addresses[], const int order[]) { |
| 155 AddressList list; | 155 AddressList list; |
| 156 for (const char** addr = addresses; *addr != NULL; ++addr) | 156 for (const char* const* addr = addresses; *addr != NULL; ++addr) |
| 157 list.push_back(IPEndPoint(ParseIP(*addr), 80)); | 157 list.push_back(IPEndPoint(ParseIP(*addr), 80)); |
| 158 for (size_t i = 0; order[i] >= 0; ++i) | 158 for (size_t i = 0; order[i] >= 0; ++i) |
| 159 CHECK_LT(order[i], static_cast<int>(list.size())); | 159 CHECK_LT(order[i], static_cast<int>(list.size())); |
| 160 | 160 |
| 161 AddressList result; | 161 AddressList result; |
| 162 TestCompletionCallback callback; | 162 TestCompletionCallback callback; |
| 163 sorter_.Sort(list, base::Bind(&OnSortComplete, &result, | 163 sorter_.Sort(list, base::Bind(&OnSortComplete, &result, |
| 164 callback.callback())); | 164 callback.callback())); |
| 165 callback.WaitForResult(); | 165 callback.WaitForResult(); |
| 166 | 166 |
| 167 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) { |
| 168 IPEndPoint expected = order[i] >= 0 ? list[order[i]] : IPEndPoint(); | 168 IPEndPoint expected = order[i] >= 0 ? list[order[i]] : IPEndPoint(); |
| 169 IPEndPoint actual = i < result.size() ? result[i] : IPEndPoint(); | 169 IPEndPoint actual = i < result.size() ? result[i] : IPEndPoint(); |
| 170 EXPECT_TRUE(expected.address() == actual.address()) << | 170 EXPECT_TRUE(expected.address() == actual.address()) << |
| 171 "Address out of order at position " << i << "\n" << | 171 "Address out of order at position " << i << "\n" << |
| 172 " Actual: " << actual.ToStringWithoutPort() << "\n" << | 172 " Actual: " << actual.ToStringWithoutPort() << "\n" << |
| 173 "Expected: " << expected.ToStringWithoutPort(); | 173 "Expected: " << expected.ToStringWithoutPort(); |
| 174 } | 174 } |
| 175 } | 175 } |
| 176 | 176 |
| 177 TestSocketFactory socket_factory_; | 177 TestSocketFactory socket_factory_; |
| 178 AddressSorterPosix sorter_; | 178 AddressSorterPosix sorter_; |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 // Rule 1: Avoid unusable destinations. | 181 // Rule 1: Avoid unusable destinations. |
| 182 TEST_F(AddressSorterPosixTest, Rule1) { | 182 TEST_F(AddressSorterPosixTest, Rule1) { |
| 183 AddMapping("10.0.0.231", "10.0.0.1"); | 183 AddMapping("10.0.0.231", "10.0.0.1"); |
| 184 const char* addresses[] = { "::1", "10.0.0.231", "127.0.0.1", NULL }; | 184 const char* const addresses[] = { "::1", "10.0.0.231", "127.0.0.1", NULL }; |
| 185 const int order[] = { 1, -1 }; | 185 const int order[] = { 1, -1 }; |
| 186 Verify(addresses, order); | 186 Verify(addresses, order); |
| 187 } | 187 } |
| 188 | 188 |
| 189 // Rule 2: Prefer matching scope. | 189 // Rule 2: Prefer matching scope. |
| 190 TEST_F(AddressSorterPosixTest, Rule2) { | 190 TEST_F(AddressSorterPosixTest, Rule2) { |
| 191 AddMapping("3002::1", "4000::10"); // matching global | 191 AddMapping("3002::1", "4000::10"); // matching global |
| 192 AddMapping("ff32::1", "fe81::10"); // matching link-local | 192 AddMapping("ff32::1", "fe81::10"); // matching link-local |
| 193 AddMapping("fec1::1", "fec1::10"); // matching node-local | 193 AddMapping("fec1::1", "fec1::10"); // matching node-local |
| 194 AddMapping("3002::2", "::1"); // global vs. link-local | 194 AddMapping("3002::2", "::1"); // global vs. link-local |
| 195 AddMapping("fec1::2", "fe81::10"); // site-local 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 | 196 AddMapping("8.0.0.1", "169.254.0.10"); // global vs. link-local |
| 197 // In all three cases, matching scope is preferred. | 197 // In all three cases, matching scope is preferred. |
| 198 const int order[] = { 1, 0, -1 }; | 198 const int order[] = { 1, 0, -1 }; |
| 199 const char* addresses1[] = { "3002::2", "3002::1", NULL }; | 199 const char* const addresses1[] = { "3002::2", "3002::1", NULL }; |
| 200 Verify(addresses1, order); | 200 Verify(addresses1, order); |
| 201 const char* addresses2[] = { "fec1::2", "ff32::1", NULL }; | 201 const char* const addresses2[] = { "fec1::2", "ff32::1", NULL }; |
| 202 Verify(addresses2, order); | 202 Verify(addresses2, order); |
| 203 const char* addresses3[] = { "8.0.0.1", "fec1::1", NULL }; | 203 const char* const addresses3[] = { "8.0.0.1", "fec1::1", NULL }; |
| 204 Verify(addresses3, order); | 204 Verify(addresses3, order); |
| 205 } | 205 } |
| 206 | 206 |
| 207 // Rule 3: Avoid deprecated addresses. | 207 // Rule 3: Avoid deprecated addresses. |
| 208 TEST_F(AddressSorterPosixTest, Rule3) { | 208 TEST_F(AddressSorterPosixTest, Rule3) { |
| 209 // Matching scope. | 209 // Matching scope. |
| 210 AddMapping("3002::1", "4000::10"); | 210 AddMapping("3002::1", "4000::10"); |
| 211 GetSourceInfo("4000::10")->deprecated = true; | 211 GetSourceInfo("4000::10")->deprecated = true; |
| 212 AddMapping("3002::2", "4000::20"); | 212 AddMapping("3002::2", "4000::20"); |
| 213 const char* addresses[] = { "3002::1", "3002::2", NULL }; | 213 const char* const addresses[] = { "3002::1", "3002::2", NULL }; |
| 214 const int order[] = { 1, 0, -1 }; | 214 const int order[] = { 1, 0, -1 }; |
| 215 Verify(addresses, order); | 215 Verify(addresses, order); |
| 216 } | 216 } |
| 217 | 217 |
| 218 // Rule 4: Prefer home addresses. | 218 // Rule 4: Prefer home addresses. |
| 219 TEST_F(AddressSorterPosixTest, Rule4) { | 219 TEST_F(AddressSorterPosixTest, Rule4) { |
| 220 AddMapping("3002::1", "4000::10"); | 220 AddMapping("3002::1", "4000::10"); |
| 221 AddMapping("3002::2", "4000::20"); | 221 AddMapping("3002::2", "4000::20"); |
| 222 GetSourceInfo("4000::20")->home = true; | 222 GetSourceInfo("4000::20")->home = true; |
| 223 const char* addresses[] = { "3002::1", "3002::2", NULL }; | 223 const char* const addresses[] = { "3002::1", "3002::2", NULL }; |
| 224 const int order[] = { 1, 0, -1 }; | 224 const int order[] = { 1, 0, -1 }; |
| 225 Verify(addresses, order); | 225 Verify(addresses, order); |
| 226 } | 226 } |
| 227 | 227 |
| 228 // Rule 5: Prefer matching label. | 228 // Rule 5: Prefer matching label. |
| 229 TEST_F(AddressSorterPosixTest, Rule5) { | 229 TEST_F(AddressSorterPosixTest, Rule5) { |
| 230 AddMapping("::1", "::1"); // matching loopback | 230 AddMapping("::1", "::1"); // matching loopback |
| 231 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // matching IPv4-mapped | 231 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // matching IPv4-mapped |
| 232 AddMapping("2001::1", "::ffff:1234:10"); // Teredo vs. IPv4-mapped | 232 AddMapping("2001::1", "::ffff:1234:10"); // Teredo vs. IPv4-mapped |
| 233 AddMapping("2002::1", "2001::10"); // 6to4 vs. Teredo | 233 AddMapping("2002::1", "2001::10"); // 6to4 vs. Teredo |
| 234 const int order[] = { 1, 0, -1 }; | 234 const int order[] = { 1, 0, -1 }; |
| 235 { | 235 { |
| 236 const char* addresses[] = { "2001::1", "::1", NULL }; | 236 const char* const addresses[] = { "2001::1", "::1", NULL }; |
| 237 Verify(addresses, order); | 237 Verify(addresses, order); |
| 238 } | 238 } |
| 239 { | 239 { |
| 240 const char* addresses[] = { "2002::1", "::ffff:1234:1", NULL }; | 240 const char* const addresses[] = { "2002::1", "::ffff:1234:1", NULL }; |
| 241 Verify(addresses, order); | 241 Verify(addresses, order); |
| 242 } | 242 } |
| 243 } | 243 } |
| 244 | 244 |
| 245 // Rule 6: Prefer higher precedence. | 245 // Rule 6: Prefer higher precedence. |
| 246 TEST_F(AddressSorterPosixTest, Rule6) { | 246 TEST_F(AddressSorterPosixTest, Rule6) { |
| 247 AddMapping("::1", "::1"); // loopback | 247 AddMapping("::1", "::1"); // loopback |
| 248 AddMapping("ff32::1", "fe81::10"); // multicast | 248 AddMapping("ff32::1", "fe81::10"); // multicast |
| 249 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // IPv4-mapped | 249 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // IPv4-mapped |
| 250 AddMapping("2001::1", "2001::10"); // Teredo | 250 AddMapping("2001::1", "2001::10"); // Teredo |
| 251 const char* addresses[] = { "2001::1", "::ffff:1234:1", "ff32::1", "::1", | 251 const char* const addresses[] = { "2001::1", "::ffff:1234:1", "ff32::1", |
| 252 NULL }; | 252 "::1", NULL }; |
| 253 const int order[] = { 3, 2, 1, 0, -1 }; | 253 const int order[] = { 3, 2, 1, 0, -1 }; |
| 254 Verify(addresses, order); | 254 Verify(addresses, order); |
| 255 } | 255 } |
| 256 | 256 |
| 257 // Rule 7: Prefer native transport. | 257 // Rule 7: Prefer native transport. |
| 258 TEST_F(AddressSorterPosixTest, Rule7) { | 258 TEST_F(AddressSorterPosixTest, Rule7) { |
| 259 AddMapping("3002::1", "4000::10"); | 259 AddMapping("3002::1", "4000::10"); |
| 260 AddMapping("3002::2", "4000::20"); | 260 AddMapping("3002::2", "4000::20"); |
| 261 GetSourceInfo("4000::20")->native = true; | 261 GetSourceInfo("4000::20")->native = true; |
| 262 const char* addresses[] = { "3002::1", "3002::2", NULL }; | 262 const char* const addresses[] = { "3002::1", "3002::2", NULL }; |
| 263 const int order[] = { 1, 0, -1 }; | 263 const int order[] = { 1, 0, -1 }; |
| 264 Verify(addresses, order); | 264 Verify(addresses, order); |
| 265 } | 265 } |
| 266 | 266 |
| 267 // Rule 8: Prefer smaller scope. | 267 // Rule 8: Prefer smaller scope. |
| 268 TEST_F(AddressSorterPosixTest, Rule8) { | 268 TEST_F(AddressSorterPosixTest, Rule8) { |
| 269 // Matching scope. Should precede the others by Rule 2. | 269 // Matching scope. Should precede the others by Rule 2. |
| 270 AddMapping("fe81::1", "fe81::10"); // link-local | 270 AddMapping("fe81::1", "fe81::10"); // link-local |
| 271 AddMapping("3000::1", "4000::10"); // global | 271 AddMapping("3000::1", "4000::10"); // global |
| 272 // Mismatched scope. | 272 // Mismatched scope. |
| 273 AddMapping("ff32::1", "4000::10"); // link-local | 273 AddMapping("ff32::1", "4000::10"); // link-local |
| 274 AddMapping("ff35::1", "4000::10"); // site-local | 274 AddMapping("ff35::1", "4000::10"); // site-local |
| 275 AddMapping("ff38::1", "4000::10"); // org-local | 275 AddMapping("ff38::1", "4000::10"); // org-local |
| 276 const char* addresses[] = { "ff38::1", "3000::1", "ff35::1", "ff32::1", | 276 const char* const addresses[] = { "ff38::1", "3000::1", "ff35::1", "ff32::1", |
| 277 "fe81::1", NULL }; | 277 "fe81::1", NULL }; |
| 278 const int order[] = { 4, 1, 3, 2, 0, -1 }; | 278 const int order[] = { 4, 1, 3, 2, 0, -1 }; |
| 279 Verify(addresses, order); | 279 Verify(addresses, order); |
| 280 } | 280 } |
| 281 | 281 |
| 282 // Rule 9: Use longest matching prefix. | 282 // Rule 9: Use longest matching prefix. |
| 283 TEST_F(AddressSorterPosixTest, Rule9) { | 283 TEST_F(AddressSorterPosixTest, Rule9) { |
| 284 AddMapping("3000::1", "3000:ffff::10"); // 16 bit match | 284 AddMapping("3000::1", "3000:ffff::10"); // 16 bit match |
| 285 GetSourceInfo("3000:ffff::10")->prefix_length = 16; | 285 GetSourceInfo("3000:ffff::10")->prefix_length = 16; |
| 286 AddMapping("4000::1", "4000::10"); // 123 bit match, limited to 15 | 286 AddMapping("4000::1", "4000::10"); // 123 bit match, limited to 15 |
| 287 GetSourceInfo("4000::10")->prefix_length = 15; | 287 GetSourceInfo("4000::10")->prefix_length = 15; |
| 288 AddMapping("4002::1", "4000::10"); // 14 bit match | 288 AddMapping("4002::1", "4000::10"); // 14 bit match |
| 289 AddMapping("4080::1", "4000::10"); // 8 bit match | 289 AddMapping("4080::1", "4000::10"); // 8 bit match |
| 290 const char* addresses[] = { "4080::1", "4002::1", "4000::1", "3000::1", | 290 const char* const addresses[] = { "4080::1", "4002::1", "4000::1", "3000::1", |
| 291 NULL }; | 291 NULL }; |
| 292 const int order[] = { 3, 2, 1, 0, -1 }; | 292 const int order[] = { 3, 2, 1, 0, -1 }; |
| 293 Verify(addresses, order); | 293 Verify(addresses, order); |
| 294 } | 294 } |
| 295 | 295 |
| 296 // Rule 10: Leave the order unchanged. | 296 // Rule 10: Leave the order unchanged. |
| 297 TEST_F(AddressSorterPosixTest, Rule10) { | 297 TEST_F(AddressSorterPosixTest, Rule10) { |
| 298 AddMapping("4000::1", "4000::10"); | 298 AddMapping("4000::1", "4000::10"); |
| 299 AddMapping("4000::2", "4000::10"); | 299 AddMapping("4000::2", "4000::10"); |
| 300 AddMapping("4000::3", "4000::10"); | 300 AddMapping("4000::3", "4000::10"); |
| 301 const char* addresses[] = { "4000::1", "4000::2", "4000::3", NULL }; | 301 const char* const addresses[] = { "4000::1", "4000::2", "4000::3", NULL }; |
| 302 const int order[] = { 0, 1, 2, -1 }; | 302 const int order[] = { 0, 1, 2, -1 }; |
| 303 Verify(addresses, order); | 303 Verify(addresses, order); |
| 304 } | 304 } |
| 305 | 305 |
| 306 TEST_F(AddressSorterPosixTest, MultipleRules) { | 306 TEST_F(AddressSorterPosixTest, MultipleRules) { |
| 307 AddMapping("::1", "::1"); // loopback | 307 AddMapping("::1", "::1"); // loopback |
| 308 AddMapping("ff32::1", "fe81::10"); // link-local multicast | 308 AddMapping("ff32::1", "fe81::10"); // link-local multicast |
| 309 AddMapping("ff3e::1", "4000::10"); // global multicast | 309 AddMapping("ff3e::1", "4000::10"); // global multicast |
| 310 AddMapping("4000::1", "4000::10"); // global unicast | 310 AddMapping("4000::1", "4000::10"); // global unicast |
| 311 AddMapping("ff32::2", "fe81::20"); // deprecated link-local multicast | 311 AddMapping("ff32::2", "fe81::20"); // deprecated link-local multicast |
| 312 GetSourceInfo("fe81::20")->deprecated = true; | 312 GetSourceInfo("fe81::20")->deprecated = true; |
| 313 const char* addresses[] = { "ff3e::1", "ff32::2", "4000::1", "ff32::1", "::1", | 313 const char* const addresses[] = { "ff3e::1", "ff32::2", "4000::1", "ff32::1", |
| 314 "8.0.0.1", NULL }; | 314 "::1", "8.0.0.1", NULL }; |
| 315 const int order[] = { 4, 3, 0, 2, 1, -1 }; | 315 const int order[] = { 4, 3, 0, 2, 1, -1 }; |
| 316 Verify(addresses, order); | 316 Verify(addresses, order); |
| 317 } | 317 } |
| 318 | 318 |
| 319 } // namespace net | 319 } // namespace net |
| OLD | NEW |