| 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/base/net_util.h" | 5 #include "net/base/net_util.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "base/sys_byteorder.h" | 16 #include "base/sys_byteorder.h" |
| 17 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
| 18 #include "base/test/test_file_util.h" | 18 #include "base/test/test_file_util.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "net/base/sys_addrinfo.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 23 |
| 25 namespace net { | 24 namespace net { |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 static const size_t kNpos = string16::npos; | 28 static const size_t kNpos = string16::npos; |
| 30 | 29 |
| 31 struct FileCase { | 30 struct FileCase { |
| 32 const wchar_t* file; | 31 const wchar_t* file; |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 struct UrlTestData { | 397 struct UrlTestData { |
| 399 const char* description; | 398 const char* description; |
| 400 const char* input; | 399 const char* input; |
| 401 const char* languages; | 400 const char* languages; |
| 402 FormatUrlTypes format_types; | 401 FormatUrlTypes format_types; |
| 403 UnescapeRule::Type escape_rules; | 402 UnescapeRule::Type escape_rules; |
| 404 const wchar_t* output; // Use |wchar_t| to handle Unicode constants easily. | 403 const wchar_t* output; // Use |wchar_t| to handle Unicode constants easily. |
| 405 size_t prefix_len; | 404 size_t prefix_len; |
| 406 }; | 405 }; |
| 407 | 406 |
| 408 // Returns an addrinfo for the given 32-bit address (IPv4.) | 407 // Fills in sockaddr for the given 32-bit address (IPv4.) |
| 409 // The result lives in static storage, so don't delete it. | |
| 410 // |bytes| should be an array of length 4. | 408 // |bytes| should be an array of length 4. |
| 411 const struct addrinfo* GetIPv4Address(const uint8* bytes, int port) { | 409 void MakeIPv4Address(const uint8* bytes, int port, SockaddrStorage* storage) { |
| 412 static struct addrinfo static_ai; | 410 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); |
| 413 static struct sockaddr_in static_addr4; | 411 storage->addr_len = sizeof(struct sockaddr_in); |
| 414 | 412 struct sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(storage->addr); |
| 415 struct addrinfo* ai = &static_ai; | |
| 416 ai->ai_socktype = SOCK_STREAM; | |
| 417 memset(ai, 0, sizeof(static_ai)); | |
| 418 | |
| 419 ai->ai_family = AF_INET; | |
| 420 ai->ai_addrlen = sizeof(static_addr4); | |
| 421 | |
| 422 struct sockaddr_in* addr4 = &static_addr4; | |
| 423 memset(addr4, 0, sizeof(static_addr4)); | |
| 424 addr4->sin_port = base::HostToNet16(port); | 413 addr4->sin_port = base::HostToNet16(port); |
| 425 addr4->sin_family = ai->ai_family; | 414 addr4->sin_family = AF_INET; |
| 426 memcpy(&addr4->sin_addr, bytes, 4); | 415 memcpy(&addr4->sin_addr, bytes, 4); |
| 427 | |
| 428 ai->ai_addr = (sockaddr*)addr4; | |
| 429 return ai; | |
| 430 } | 416 } |
| 431 | 417 |
| 432 // Returns a addrinfo for the given 128-bit address (IPv6.) | 418 // Fills in sockaddr for the given 128-bit address (IPv6.) |
| 433 // The result lives in static storage, so don't delete it. | |
| 434 // |bytes| should be an array of length 16. | 419 // |bytes| should be an array of length 16. |
| 435 const struct addrinfo* GetIPv6Address(const uint8* bytes, int port) { | 420 void MakeIPv6Address(const uint8* bytes, int port, SockaddrStorage* storage) { |
| 436 static struct addrinfo static_ai; | 421 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); |
| 437 static struct sockaddr_in6 static_addr6; | 422 storage->addr_len = sizeof(struct sockaddr_in6); |
| 438 | 423 struct sockaddr_in6* addr6 = reinterpret_cast<sockaddr_in6*>(storage->addr); |
| 439 struct addrinfo* ai = &static_ai; | |
| 440 ai->ai_socktype = SOCK_STREAM; | |
| 441 memset(ai, 0, sizeof(static_ai)); | |
| 442 | |
| 443 ai->ai_family = AF_INET6; | |
| 444 ai->ai_addrlen = sizeof(static_addr6); | |
| 445 | |
| 446 struct sockaddr_in6* addr6 = &static_addr6; | |
| 447 memset(addr6, 0, sizeof(static_addr6)); | |
| 448 addr6->sin6_port = base::HostToNet16(port); | 424 addr6->sin6_port = base::HostToNet16(port); |
| 449 addr6->sin6_family = ai->ai_family; | 425 addr6->sin6_family = AF_INET6; |
| 450 memcpy(&addr6->sin6_addr, bytes, 16); | 426 memcpy(&addr6->sin6_addr, bytes, 16); |
| 451 | |
| 452 ai->ai_addr = (sockaddr*)addr6; | |
| 453 return ai; | |
| 454 } | 427 } |
| 455 | 428 |
| 456 // A helper for IDN*{Fast,Slow}. | 429 // A helper for IDN*{Fast,Slow}. |
| 457 // Append "::<language list>" to |expected| and |actual| to make it | 430 // Append "::<language list>" to |expected| and |actual| to make it |
| 458 // easy to tell which sub-case fails without debugging. | 431 // easy to tell which sub-case fails without debugging. |
| 459 void AppendLanguagesToOutputs(const char* languages, | 432 void AppendLanguagesToOutputs(const char* languages, |
| 460 string16* expected, | 433 string16* expected, |
| 461 string16* actual) { | 434 string16* actual) { |
| 462 string16 to_append = ASCIIToUTF16("::") + ASCIIToUTF16(languages); | 435 string16 to_append = ASCIIToUTF16("::") + ASCIIToUTF16(languages); |
| 463 expected->append(to_append); | 436 expected->append(to_append); |
| (...skipping 1799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2263 const struct { | 2236 const struct { |
| 2264 uint8 addr[4]; | 2237 uint8 addr[4]; |
| 2265 const char* result; | 2238 const char* result; |
| 2266 } tests[] = { | 2239 } tests[] = { |
| 2267 {{0, 0, 0, 0}, "0.0.0.0"}, | 2240 {{0, 0, 0, 0}, "0.0.0.0"}, |
| 2268 {{127, 0, 0, 1}, "127.0.0.1"}, | 2241 {{127, 0, 0, 1}, "127.0.0.1"}, |
| 2269 {{192, 168, 0, 1}, "192.168.0.1"}, | 2242 {{192, 168, 0, 1}, "192.168.0.1"}, |
| 2270 }; | 2243 }; |
| 2271 | 2244 |
| 2272 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 2245 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 2273 const addrinfo* ai = GetIPv4Address(tests[i].addr, 80); | 2246 SockaddrStorage storage; |
| 2274 std::string result = NetAddressToString(ai); | 2247 MakeIPv4Address(tests[i].addr, 80, &storage); |
| 2248 std::string result = NetAddressToString(storage.addr, storage.addr_len); |
| 2275 EXPECT_EQ(std::string(tests[i].result), result); | 2249 EXPECT_EQ(std::string(tests[i].result), result); |
| 2276 } | 2250 } |
| 2277 } | 2251 } |
| 2278 | 2252 |
| 2279 TEST(NetUtilTest, NetAddressToString_IPv6) { | 2253 TEST(NetUtilTest, NetAddressToString_IPv6) { |
| 2280 const struct { | 2254 const struct { |
| 2281 uint8 addr[16]; | 2255 uint8 addr[16]; |
| 2282 const char* result; | 2256 const char* result; |
| 2283 } tests[] = { | 2257 } tests[] = { |
| 2284 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | 2258 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, |
| 2285 0x98, 0x76, 0x54, 0x32, 0x10}, | 2259 0x98, 0x76, 0x54, 0x32, 0x10}, |
| 2286 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, | 2260 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, |
| 2287 }; | 2261 }; |
| 2288 | 2262 |
| 2289 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 2263 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 2290 const addrinfo* ai = GetIPv6Address(tests[i].addr, 80); | 2264 SockaddrStorage storage; |
| 2291 std::string result = NetAddressToString(ai); | 2265 MakeIPv6Address(tests[i].addr, 80, &storage); |
| 2266 std::string result = NetAddressToString(storage.addr, storage.addr_len); |
| 2292 // Allow NetAddressToString() to fail, in case the system doesn't | 2267 // Allow NetAddressToString() to fail, in case the system doesn't |
| 2293 // support IPv6. | 2268 // support IPv6. |
| 2294 if (!result.empty()) | 2269 if (!result.empty()) |
| 2295 EXPECT_EQ(std::string(tests[i].result), result); | 2270 EXPECT_EQ(std::string(tests[i].result), result); |
| 2296 } | 2271 } |
| 2297 } | 2272 } |
| 2298 | 2273 |
| 2299 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { | 2274 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { |
| 2300 uint8 addr[] = {127, 0, 0, 1}; | 2275 uint8 addr[] = {127, 0, 0, 1}; |
| 2301 const addrinfo* ai = GetIPv4Address(addr, 166); | 2276 SockaddrStorage storage; |
| 2302 std::string result = NetAddressToStringWithPort(ai); | 2277 MakeIPv4Address(addr, 166, &storage); |
| 2278 std::string result = NetAddressToStringWithPort(storage.addr, |
| 2279 storage.addr_len); |
| 2303 EXPECT_EQ("127.0.0.1:166", result); | 2280 EXPECT_EQ("127.0.0.1:166", result); |
| 2304 } | 2281 } |
| 2305 | 2282 |
| 2306 TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) { | 2283 TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) { |
| 2307 uint8 addr[] = { | 2284 uint8 addr[] = { |
| 2308 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | 2285 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, |
| 2309 0x98, 0x76, 0x54, 0x32, 0x10 | 2286 0x98, 0x76, 0x54, 0x32, 0x10 |
| 2310 }; | 2287 }; |
| 2311 const addrinfo* ai = GetIPv6Address(addr, 361); | 2288 SockaddrStorage storage; |
| 2312 std::string result = NetAddressToStringWithPort(ai); | 2289 MakeIPv6Address(addr, 361, &storage); |
| 2290 std::string result = NetAddressToStringWithPort(storage.addr, |
| 2291 storage.addr_len); |
| 2313 | 2292 |
| 2314 // May fail on systems that don't support IPv6. | 2293 // May fail on systems that don't support IPv6. |
| 2315 if (!result.empty()) | 2294 if (!result.empty()) |
| 2316 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result); | 2295 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result); |
| 2317 } | 2296 } |
| 2318 | 2297 |
| 2319 TEST(NetUtilTest, GetHostName) { | 2298 TEST(NetUtilTest, GetHostName) { |
| 2320 // We can't check the result of GetHostName() directly, since the result | 2299 // We can't check the result of GetHostName() directly, since the result |
| 2321 // will differ across machines. Our goal here is to simply exercise the | 2300 // will differ across machines. Our goal here is to simply exercise the |
| 2322 // code path, and check that things "look about right". | 2301 // code path, and check that things "look about right". |
| (...skipping 893 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3216 if (it->address[i] != 0) { | 3195 if (it->address[i] != 0) { |
| 3217 all_zeroes = false; | 3196 all_zeroes = false; |
| 3218 break; | 3197 break; |
| 3219 } | 3198 } |
| 3220 } | 3199 } |
| 3221 EXPECT_FALSE(all_zeroes); | 3200 EXPECT_FALSE(all_zeroes); |
| 3222 } | 3201 } |
| 3223 } | 3202 } |
| 3224 | 3203 |
| 3225 } // namespace net | 3204 } // namespace net |
| OLD | NEW |