| 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/network_interfaces.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string> |
| 8 | 8 |
| 9 #include <ostream> | 9 #include <ostream> |
| 10 | 10 |
| 11 // TODO(eroman): Remove unneeeded headers. |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 13 #include "base/scoped_native_library.h" | 14 #include "base/scoped_native_library.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/sys_byteorder.h" | 19 #include "base/sys_byteorder.h" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 20 #include "net/base/ip_endpoint.h" | 21 #include "net/base/ip_endpoint.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 36 #include <iphlpapi.h> | 37 #include <iphlpapi.h> |
| 37 #include <objbase.h> | 38 #include <objbase.h> |
| 38 #include "base/win/windows_version.h" | 39 #include "base/win/windows_version.h" |
| 39 #endif // OS_WIN | 40 #endif // OS_WIN |
| 40 | 41 |
| 41 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_WIN) | 42 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_WIN) |
| 42 #include "net/base/address_tracker_linux.h" | 43 #include "net/base/address_tracker_linux.h" |
| 43 #endif // !OS_MACOSX && !OS_NACL && !OS_WIN | 44 #endif // !OS_MACOSX && !OS_NACL && !OS_WIN |
| 44 | 45 |
| 45 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
| 46 #include "net/base/net_util_win.h" | 47 #include "net/base/network_interfaces_win.h" |
| 47 #else // OS_WIN | 48 #else // OS_WIN |
| 48 #include "net/base/net_util_posix.h" | 49 #include "net/base/network_interfaces_posix.h" |
| 49 #if defined(OS_MACOSX) | 50 #if defined(OS_MACOSX) |
| 50 #include "net/base/net_util_mac.h" | 51 #include "net/base/network_interfaces_mac.h" |
| 51 #else // OS_MACOSX | 52 #else // OS_MACOSX |
| 52 #include "net/base/net_util_linux.h" | 53 #include "net/base/network_interfaces_linux.h" |
| 53 #endif // OS_MACOSX | 54 #endif // OS_MACOSX |
| 54 #endif // OS_WIN | 55 #endif // OS_WIN |
| 55 | 56 |
| 56 using base::ASCIIToUTF16; | |
| 57 using base::WideToUTF16; | |
| 58 | |
| 59 namespace net { | 57 namespace net { |
| 60 | 58 |
| 61 namespace { | 59 namespace { |
| 62 | 60 |
| 63 struct HeaderCase { | |
| 64 const char* const header_name; | |
| 65 const char* const expected; | |
| 66 }; | |
| 67 | |
| 68 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_CHROMEOS) | 61 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 69 const char kWiFiSSID[] = "TestWiFi"; | 62 const char kWiFiSSID[] = "TestWiFi"; |
| 70 const char kInterfaceWithDifferentSSID[] = "wlan999"; | 63 const char kInterfaceWithDifferentSSID[] = "wlan999"; |
| 71 | 64 |
| 72 std::string TestGetInterfaceSSID(const std::string& ifname) { | 65 std::string TestGetInterfaceSSID(const std::string& ifname) { |
| 73 return (ifname == kInterfaceWithDifferentSSID) ? "AnotherSSID" : kWiFiSSID; | 66 return (ifname == kInterfaceWithDifferentSSID) ? "AnotherSSID" : kWiFiSSID; |
| 74 } | 67 } |
| 75 #endif | 68 #endif |
| 76 | 69 |
| 77 // Fills in sockaddr for the given 32-bit address (IPv4.) | |
| 78 // |bytes| should be an array of length 4. | |
| 79 void MakeIPv4Address(const uint8* bytes, int port, SockaddrStorage* storage) { | |
| 80 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); | |
| 81 storage->addr_len = sizeof(struct sockaddr_in); | |
| 82 struct sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(storage->addr); | |
| 83 addr4->sin_port = base::HostToNet16(port); | |
| 84 addr4->sin_family = AF_INET; | |
| 85 memcpy(&addr4->sin_addr, bytes, 4); | |
| 86 } | |
| 87 | |
| 88 // Fills in sockaddr for the given 128-bit address (IPv6.) | |
| 89 // |bytes| should be an array of length 16. | |
| 90 void MakeIPv6Address(const uint8* bytes, int port, SockaddrStorage* storage) { | |
| 91 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); | |
| 92 storage->addr_len = sizeof(struct sockaddr_in6); | |
| 93 struct sockaddr_in6* addr6 = reinterpret_cast<sockaddr_in6*>(storage->addr); | |
| 94 addr6->sin6_port = base::HostToNet16(port); | |
| 95 addr6->sin6_family = AF_INET6; | |
| 96 memcpy(&addr6->sin6_addr, bytes, 16); | |
| 97 } | |
| 98 | |
| 99 #if defined(OS_MACOSX) | 70 #if defined(OS_MACOSX) |
| 100 class IPAttributesGetterTest : public internal::IPAttributesGetterMac { | 71 class IPAttributesGetterTest : public internal::IPAttributesGetterMac { |
| 101 public: | 72 public: |
| 102 IPAttributesGetterTest() : native_attributes_(0) {} | 73 IPAttributesGetterTest() : native_attributes_(0) {} |
| 103 bool IsInitialized() const override { return true; } | 74 bool IsInitialized() const override { return true; } |
| 104 bool GetIPAttributes(const char* ifname, | 75 bool GetIPAttributes(const char* ifname, |
| 105 const sockaddr* sock_addr, | 76 const sockaddr* sock_addr, |
| 106 int* native_attributes) override { | 77 int* native_attributes) override { |
| 107 *native_attributes = native_attributes_; | 78 *native_attributes = native_attributes_; |
| 108 return true; | 79 return true; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 if (!IPEndPoint(ip_netmask, 0) | 111 if (!IPEndPoint(ip_netmask, 0) |
| 141 .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addrs[1]), | 112 .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addrs[1]), |
| 142 &sock_len)) { | 113 &sock_len)) { |
| 143 return false; | 114 return false; |
| 144 } | 115 } |
| 145 interfaces->ifa_netmask = reinterpret_cast<sockaddr*>(&sock_addrs[1]); | 116 interfaces->ifa_netmask = reinterpret_cast<sockaddr*>(&sock_addrs[1]); |
| 146 | 117 |
| 147 return true; | 118 return true; |
| 148 } | 119 } |
| 149 #endif // OS_MACOSX | 120 #endif // OS_MACOSX |
| 150 | |
| 151 } // anonymous namespace | |
| 152 | |
| 153 TEST(NetUtilTest, GetIdentityFromURL) { | |
| 154 struct { | |
| 155 const char* const input_url; | |
| 156 const char* const expected_username; | |
| 157 const char* const expected_password; | |
| 158 } tests[] = { | |
| 159 { | |
| 160 "http://username:password@google.com", | |
| 161 "username", | |
| 162 "password", | |
| 163 }, | |
| 164 { // Test for http://crbug.com/19200 | |
| 165 "http://username:p@ssword@google.com", | |
| 166 "username", | |
| 167 "p@ssword", | |
| 168 }, | |
| 169 { // Special URL characters should be unescaped. | |
| 170 "http://username:p%3fa%26s%2fs%23@google.com", | |
| 171 "username", | |
| 172 "p?a&s/s#", | |
| 173 }, | |
| 174 { // Username contains %20. | |
| 175 "http://use rname:password@google.com", | |
| 176 "use rname", | |
| 177 "password", | |
| 178 }, | |
| 179 { // Keep %00 as is. | |
| 180 "http://use%00rname:password@google.com", | |
| 181 "use%00rname", | |
| 182 "password", | |
| 183 }, | |
| 184 { // Use a '+' in the username. | |
| 185 "http://use+rname:password@google.com", | |
| 186 "use+rname", | |
| 187 "password", | |
| 188 }, | |
| 189 { // Use a '&' in the password. | |
| 190 "http://username:p&ssword@google.com", | |
| 191 "username", | |
| 192 "p&ssword", | |
| 193 }, | |
| 194 }; | |
| 195 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 196 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | |
| 197 tests[i].input_url)); | |
| 198 GURL url(tests[i].input_url); | |
| 199 | |
| 200 base::string16 username, password; | |
| 201 GetIdentityFromURL(url, &username, &password); | |
| 202 | |
| 203 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); | |
| 204 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 // Try extracting a username which was encoded with UTF8. | |
| 209 TEST(NetUtilTest, GetIdentityFromURL_UTF8) { | |
| 210 GURL url(WideToUTF16(L"http://foo:\x4f60\x597d@blah.com")); | |
| 211 | |
| 212 EXPECT_EQ("foo", url.username()); | |
| 213 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url.password()); | |
| 214 | |
| 215 // Extract the unescaped identity. | |
| 216 base::string16 username, password; | |
| 217 GetIdentityFromURL(url, &username, &password); | |
| 218 | |
| 219 // Verify that it was decoded as UTF8. | |
| 220 EXPECT_EQ(ASCIIToUTF16("foo"), username); | |
| 221 EXPECT_EQ(WideToUTF16(L"\x4f60\x597d"), password); | |
| 222 } | |
| 223 | |
| 224 // Just a bunch of fake headers. | |
| 225 const char google_headers[] = | |
| 226 "HTTP/1.1 200 OK\n" | |
| 227 "Content-TYPE: text/html; charset=utf-8\n" | |
| 228 "Content-disposition: attachment; filename=\"download.pdf\"\n" | |
| 229 "Content-Length: 378557\n" | |
| 230 "X-Google-Google1: 314159265\n" | |
| 231 "X-Google-Google2: aaaa2:7783,bbb21:9441\n" | |
| 232 "X-Google-Google4: home\n" | |
| 233 "Transfer-Encoding: chunked\n" | |
| 234 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n" | |
| 235 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n" | |
| 236 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee" | |
| 237 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n" | |
| 238 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n" | |
| 239 "Server: GFE/1.3\n" | |
| 240 "Transfer-Encoding: chunked\n" | |
| 241 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n" | |
| 242 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n" | |
| 243 "X-Malformed: bla; arg=test\"\n" | |
| 244 "X-Malformed2: bla; arg=\n" | |
| 245 "X-Test: bla; arg1=val1; arg2=val2"; | |
| 246 | |
| 247 TEST(NetUtilTest, GetSpecificHeader) { | |
| 248 const HeaderCase tests[] = { | |
| 249 {"content-type", "text/html; charset=utf-8"}, | |
| 250 {"CONTENT-LENGTH", "378557"}, | |
| 251 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"}, | |
| 252 {"Bad-Header", ""}, | |
| 253 {"", ""}, | |
| 254 }; | |
| 255 | |
| 256 // Test first with google_headers. | |
| 257 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 258 std::string result = | |
| 259 GetSpecificHeader(google_headers, tests[i].header_name); | |
| 260 EXPECT_EQ(result, tests[i].expected); | |
| 261 } | |
| 262 | |
| 263 // Test again with empty headers. | |
| 264 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 265 std::string result = GetSpecificHeader(std::string(), tests[i].header_name); | |
| 266 EXPECT_EQ(result, std::string()); | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 TEST(NetUtilTest, CompliantHost) { | |
| 271 struct CompliantHostCase { | |
| 272 const char* const host; | |
| 273 bool expected_output; | |
| 274 }; | |
| 275 | |
| 276 const CompliantHostCase compliant_host_cases[] = { | |
| 277 {"", false}, | |
| 278 {"a", true}, | |
| 279 {"-", false}, | |
| 280 {"_", false}, | |
| 281 {".", false}, | |
| 282 {"9", true}, | |
| 283 {"9a", true}, | |
| 284 {"9_", true}, | |
| 285 {"a.", true}, | |
| 286 {"a.a", true}, | |
| 287 {"9.a", true}, | |
| 288 {"a.9", true}, | |
| 289 {"_9a", false}, | |
| 290 {"-9a", false}, | |
| 291 {"a.a9", true}, | |
| 292 {"_.9a", true}, | |
| 293 {"a.-a9", false}, | |
| 294 {"a+9a", false}, | |
| 295 {"-a.a9", true}, | |
| 296 {"a_.a9", true}, | |
| 297 {"1-.a-b", true}, | |
| 298 {"1_.a-b", true}, | |
| 299 {"1-2.a_b", true}, | |
| 300 {"a.b.c.d.e", true}, | |
| 301 {"1.2.3.4.5", true}, | |
| 302 {"1.2.3.4.5.", true}, | |
| 303 }; | |
| 304 | |
| 305 for (size_t i = 0; i < arraysize(compliant_host_cases); ++i) { | |
| 306 EXPECT_EQ(compliant_host_cases[i].expected_output, | |
| 307 IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); | |
| 308 } | |
| 309 } | |
| 310 | |
| 311 TEST(NetUtilTest, ParseHostAndPort) { | |
| 312 const struct { | |
| 313 const char* const input; | |
| 314 bool success; | |
| 315 const char* const expected_host; | |
| 316 int expected_port; | |
| 317 } tests[] = { | |
| 318 // Valid inputs: | |
| 319 {"foo:10", true, "foo", 10}, | |
| 320 {"foo", true, "foo", -1}, | |
| 321 { | |
| 322 "[1080:0:0:0:8:800:200C:4171]:11", | |
| 323 true, | |
| 324 "1080:0:0:0:8:800:200C:4171", | |
| 325 11 | |
| 326 }, | |
| 327 { | |
| 328 "[1080:0:0:0:8:800:200C:4171]", | |
| 329 true, | |
| 330 "1080:0:0:0:8:800:200C:4171", | |
| 331 -1 | |
| 332 }, | |
| 333 | |
| 334 // Because no validation is done on the host, the following are accepted, | |
| 335 // even though they are invalid names. | |
| 336 {"]", true, "]", -1}, | |
| 337 {"::1", true, ":", 1}, | |
| 338 // Invalid inputs: | |
| 339 {"foo:bar", false, "", -1}, | |
| 340 {"foo:", false, "", -1}, | |
| 341 {":", false, "", -1}, | |
| 342 {":80", false, "", -1}, | |
| 343 {"", false, "", -1}, | |
| 344 {"porttoolong:300000", false, "", -1}, | |
| 345 {"usrname@host", false, "", -1}, | |
| 346 {"usrname:password@host", false, "", -1}, | |
| 347 {":password@host", false, "", -1}, | |
| 348 {":password@host:80", false, "", -1}, | |
| 349 {":password@host", false, "", -1}, | |
| 350 {"@host", false, "", -1}, | |
| 351 {"[", false, "", -1}, | |
| 352 {"[]", false, "", -1}, | |
| 353 }; | |
| 354 | |
| 355 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 356 std::string host; | |
| 357 int port; | |
| 358 bool ok = ParseHostAndPort(tests[i].input, &host, &port); | |
| 359 | |
| 360 EXPECT_EQ(tests[i].success, ok); | |
| 361 | |
| 362 if (tests[i].success) { | |
| 363 EXPECT_EQ(tests[i].expected_host, host); | |
| 364 EXPECT_EQ(tests[i].expected_port, port); | |
| 365 } | |
| 366 } | |
| 367 } | |
| 368 | |
| 369 TEST(NetUtilTest, GetHostAndPort) { | |
| 370 const struct { | |
| 371 GURL url; | |
| 372 const char* const expected_host_and_port; | |
| 373 } tests[] = { | |
| 374 { GURL("http://www.foo.com/x"), "www.foo.com:80"}, | |
| 375 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, | |
| 376 | |
| 377 // For IPv6 literals should always include the brackets. | |
| 378 { GURL("http://[1::2]/x"), "[1::2]:80"}, | |
| 379 { GURL("http://[::a]:33/x"), "[::a]:33"}, | |
| 380 }; | |
| 381 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 382 std::string host_and_port = GetHostAndPort(tests[i].url); | |
| 383 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 TEST(NetUtilTest, GetHostAndOptionalPort) { | |
| 388 const struct { | |
| 389 GURL url; | |
| 390 const char* const expected_host_and_port; | |
| 391 } tests[] = { | |
| 392 { GURL("http://www.foo.com/x"), "www.foo.com"}, | |
| 393 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, | |
| 394 | |
| 395 // For IPv6 literals should always include the brackets. | |
| 396 { GURL("http://[1::2]/x"), "[1::2]"}, | |
| 397 { GURL("http://[::a]:33/x"), "[::a]:33"}, | |
| 398 }; | |
| 399 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 400 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); | |
| 401 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); | |
| 402 } | |
| 403 } | |
| 404 | |
| 405 TEST(NetUtilTest, NetAddressToString_IPv4) { | |
| 406 const struct { | |
| 407 uint8 addr[4]; | |
| 408 const char* const result; | |
| 409 } tests[] = { | |
| 410 {{0, 0, 0, 0}, "0.0.0.0"}, | |
| 411 {{127, 0, 0, 1}, "127.0.0.1"}, | |
| 412 {{192, 168, 0, 1}, "192.168.0.1"}, | |
| 413 }; | |
| 414 | |
| 415 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 416 SockaddrStorage storage; | |
| 417 MakeIPv4Address(tests[i].addr, 80, &storage); | |
| 418 std::string result = NetAddressToString(storage.addr, storage.addr_len); | |
| 419 EXPECT_EQ(std::string(tests[i].result), result); | |
| 420 } | |
| 421 } | |
| 422 | |
| 423 TEST(NetUtilTest, NetAddressToString_IPv6) { | |
| 424 const struct { | |
| 425 uint8 addr[16]; | |
| 426 const char* const result; | |
| 427 } tests[] = { | |
| 428 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | |
| 429 0x98, 0x76, 0x54, 0x32, 0x10}, | |
| 430 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, | |
| 431 }; | |
| 432 | |
| 433 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 434 SockaddrStorage storage; | |
| 435 MakeIPv6Address(tests[i].addr, 80, &storage); | |
| 436 EXPECT_EQ(std::string(tests[i].result), | |
| 437 NetAddressToString(storage.addr, storage.addr_len)); | |
| 438 } | |
| 439 } | |
| 440 | |
| 441 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { | |
| 442 uint8 addr[] = {127, 0, 0, 1}; | |
| 443 SockaddrStorage storage; | |
| 444 MakeIPv4Address(addr, 166, &storage); | |
| 445 std::string result = NetAddressToStringWithPort(storage.addr, | |
| 446 storage.addr_len); | |
| 447 EXPECT_EQ("127.0.0.1:166", result); | |
| 448 } | |
| 449 | |
| 450 TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) { | |
| 451 uint8 addr[] = { | |
| 452 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | |
| 453 0x98, 0x76, 0x54, 0x32, 0x10 | |
| 454 }; | |
| 455 SockaddrStorage storage; | |
| 456 MakeIPv6Address(addr, 361, &storage); | |
| 457 std::string result = NetAddressToStringWithPort(storage.addr, | |
| 458 storage.addr_len); | |
| 459 | |
| 460 // May fail on systems that don't support IPv6. | |
| 461 if (!result.empty()) | |
| 462 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result); | |
| 463 } | |
| 464 | |
| 465 TEST(NetUtilTest, GetHostName) { | |
| 466 // We can't check the result of GetHostName() directly, since the result | |
| 467 // will differ across machines. Our goal here is to simply exercise the | |
| 468 // code path, and check that things "look about right". | |
| 469 std::string hostname = GetHostName(); | |
| 470 EXPECT_FALSE(hostname.empty()); | |
| 471 } | |
| 472 | |
| 473 TEST(NetUtilTest, SimplifyUrlForRequest) { | |
| 474 struct { | |
| 475 const char* const input_url; | |
| 476 const char* const expected_simplified_url; | |
| 477 } tests[] = { | |
| 478 { | |
| 479 // Reference section should be stripped. | |
| 480 "http://www.google.com:78/foobar?query=1#hash", | |
| 481 "http://www.google.com:78/foobar?query=1", | |
| 482 }, | |
| 483 { | |
| 484 // Reference section can itself contain #. | |
| 485 "http://192.168.0.1?query=1#hash#10#11#13#14", | |
| 486 "http://192.168.0.1?query=1", | |
| 487 }, | |
| 488 { // Strip username/password. | |
| 489 "http://user:pass@google.com", | |
| 490 "http://google.com/", | |
| 491 }, | |
| 492 { // Strip both the reference and the username/password. | |
| 493 "http://user:pass@google.com:80/sup?yo#X#X", | |
| 494 "http://google.com/sup?yo", | |
| 495 }, | |
| 496 { // Try an HTTPS URL -- strip both the reference and the username/password. | |
| 497 "https://user:pass@google.com:80/sup?yo#X#X", | |
| 498 "https://google.com:80/sup?yo", | |
| 499 }, | |
| 500 { // Try an FTP URL -- strip both the reference and the username/password. | |
| 501 "ftp://user:pass@google.com:80/sup?yo#X#X", | |
| 502 "ftp://google.com:80/sup?yo", | |
| 503 }, | |
| 504 { // Try a nonstandard URL | |
| 505 "foobar://user:pass@google.com:80/sup?yo#X#X", | |
| 506 "foobar://user:pass@google.com:80/sup?yo", | |
| 507 }, | |
| 508 }; | |
| 509 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 510 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | |
| 511 tests[i].input_url)); | |
| 512 GURL input_url(GURL(tests[i].input_url)); | |
| 513 GURL expected_url(GURL(tests[i].expected_simplified_url)); | |
| 514 EXPECT_EQ(expected_url, SimplifyUrlForRequest(input_url)); | |
| 515 } | |
| 516 } | |
| 517 | |
| 518 TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { | |
| 519 std::string invalid[] = { "1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12" }; | |
| 520 std::string valid[] = { "", "1", "1,2", "1,2,3", "10,11,12,13" }; | |
| 521 | |
| 522 for (size_t i = 0; i < arraysize(invalid); ++i) { | |
| 523 SetExplicitlyAllowedPorts(invalid[i]); | |
| 524 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts())); | |
| 525 } | |
| 526 | |
| 527 for (size_t i = 0; i < arraysize(valid); ++i) { | |
| 528 SetExplicitlyAllowedPorts(valid[i]); | |
| 529 EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts()); | |
| 530 } | |
| 531 } | |
| 532 | |
| 533 TEST(NetUtilTest, GetHostOrSpecFromURL) { | |
| 534 EXPECT_EQ("example.com", | |
| 535 GetHostOrSpecFromURL(GURL("http://example.com/test"))); | |
| 536 EXPECT_EQ("example.com", | |
| 537 GetHostOrSpecFromURL(GURL("http://example.com./test"))); | |
| 538 EXPECT_EQ("file:///tmp/test.html", | |
| 539 GetHostOrSpecFromURL(GURL("file:///tmp/test.html"))); | |
| 540 } | |
| 541 | |
| 542 TEST(NetUtilTest, GetAddressFamily) { | |
| 543 IPAddressNumber number; | |
| 544 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &number)); | |
| 545 EXPECT_EQ(ADDRESS_FAMILY_IPV4, GetAddressFamily(number)); | |
| 546 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); | |
| 547 EXPECT_EQ(ADDRESS_FAMILY_IPV6, GetAddressFamily(number)); | |
| 548 } | |
| 549 | |
| 550 TEST(NetUtilTest, IsLocalhost) { | |
| 551 EXPECT_TRUE(IsLocalhost("localhost")); | |
| 552 EXPECT_TRUE(IsLocalhost("localhost.localdomain")); | |
| 553 EXPECT_TRUE(IsLocalhost("localhost6")); | |
| 554 EXPECT_TRUE(IsLocalhost("localhost6.localdomain6")); | |
| 555 EXPECT_TRUE(IsLocalhost("127.0.0.1")); | |
| 556 EXPECT_TRUE(IsLocalhost("127.0.1.0")); | |
| 557 EXPECT_TRUE(IsLocalhost("127.1.0.0")); | |
| 558 EXPECT_TRUE(IsLocalhost("127.0.0.255")); | |
| 559 EXPECT_TRUE(IsLocalhost("127.0.255.0")); | |
| 560 EXPECT_TRUE(IsLocalhost("127.255.0.0")); | |
| 561 EXPECT_TRUE(IsLocalhost("::1")); | |
| 562 EXPECT_TRUE(IsLocalhost("0:0:0:0:0:0:0:1")); | |
| 563 EXPECT_TRUE(IsLocalhost("foo.localhost")); | |
| 564 | |
| 565 EXPECT_FALSE(IsLocalhost("localhostx")); | |
| 566 EXPECT_FALSE(IsLocalhost("foo.localdomain")); | |
| 567 EXPECT_FALSE(IsLocalhost("localhost6x")); | |
| 568 EXPECT_FALSE(IsLocalhost("localhost.localdomain6")); | |
| 569 EXPECT_FALSE(IsLocalhost("localhost6.localdomain")); | |
| 570 EXPECT_FALSE(IsLocalhost("127.0.0.1.1")); | |
| 571 EXPECT_FALSE(IsLocalhost(".127.0.0.255")); | |
| 572 EXPECT_FALSE(IsLocalhost("::2")); | |
| 573 EXPECT_FALSE(IsLocalhost("::1:1")); | |
| 574 EXPECT_FALSE(IsLocalhost("0:0:0:0:1:0:0:1")); | |
| 575 EXPECT_FALSE(IsLocalhost("::1:1")); | |
| 576 EXPECT_FALSE(IsLocalhost("0:0:0:0:0:0:0:0:1")); | |
| 577 EXPECT_FALSE(IsLocalhost("foo.localhost.com")); | |
| 578 EXPECT_FALSE(IsLocalhost("foo.localhoste")); | |
| 579 } | |
| 580 | |
| 581 TEST(NetUtilTest, IsLocalhostTLD) { | |
| 582 EXPECT_TRUE(IsLocalhostTLD("foo.localhost")); | |
| 583 EXPECT_TRUE(IsLocalhostTLD("foo.localhost.")); | |
| 584 EXPECT_FALSE(IsLocalhostTLD("foo.localhos")); | |
| 585 EXPECT_FALSE(IsLocalhostTLD("foo.localhost.com")); | |
| 586 EXPECT_FALSE(IsLocalhost("foo.localhoste")); | |
| 587 } | |
| 588 | |
| 589 TEST(NetUtilTest, GoogleHost) { | |
| 590 struct GoogleHostCase { | |
| 591 GURL url; | |
| 592 bool expected_output; | |
| 593 }; | |
| 594 | |
| 595 const GoogleHostCase google_host_cases[] = { | |
| 596 {GURL("http://.google.com"), true}, | |
| 597 {GURL("http://.youtube.com"), true}, | |
| 598 {GURL("http://.gmail.com"), true}, | |
| 599 {GURL("http://.doubleclick.net"), true}, | |
| 600 {GURL("http://.gstatic.com"), true}, | |
| 601 {GURL("http://.googlevideo.com"), true}, | |
| 602 {GURL("http://.googleusercontent.com"), true}, | |
| 603 {GURL("http://.googlesyndication.com"), true}, | |
| 604 {GURL("http://.google-analytics.com"), true}, | |
| 605 {GURL("http://.googleadservices.com"), true}, | |
| 606 {GURL("http://.googleapis.com"), true}, | |
| 607 {GURL("http://a.google.com"), true}, | |
| 608 {GURL("http://b.youtube.com"), true}, | |
| 609 {GURL("http://c.gmail.com"), true}, | |
| 610 {GURL("http://google.com"), false}, | |
| 611 {GURL("http://youtube.com"), false}, | |
| 612 {GURL("http://gmail.com"), false}, | |
| 613 {GURL("http://google.coma"), false}, | |
| 614 {GURL("http://agoogle.com"), false}, | |
| 615 {GURL("http://oogle.com"), false}, | |
| 616 {GURL("http://google.co"), false}, | |
| 617 {GURL("http://oggole.com"), false}, | |
| 618 }; | |
| 619 | |
| 620 for (size_t i = 0; i < arraysize(google_host_cases); ++i) { | |
| 621 EXPECT_EQ(google_host_cases[i].expected_output, | |
| 622 HasGoogleHost(google_host_cases[i].url)); | |
| 623 } | |
| 624 } | |
| 625 | |
| 626 // Verify GetNetworkList(). | 121 // Verify GetNetworkList(). |
| 627 TEST(NetUtilTest, GetNetworkList) { | 122 TEST(NetUtilTest, GetNetworkList) { |
| 628 NetworkInterfaceList list; | 123 NetworkInterfaceList list; |
| 629 ASSERT_TRUE(GetNetworkList(&list, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)); | 124 ASSERT_TRUE(GetNetworkList(&list, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)); |
| 630 for (NetworkInterfaceList::iterator it = list.begin(); | 125 for (NetworkInterfaceList::iterator it = list.begin(); |
| 631 it != list.end(); ++it) { | 126 it != list.end(); ++it) { |
| 632 // Verify that the names are not empty. | 127 // Verify that the names are not empty. |
| 633 EXPECT_FALSE(it->name.empty()); | 128 EXPECT_FALSE(it->name.empty()); |
| 634 EXPECT_FALSE(it->friendly_name.empty()); | 129 EXPECT_FALSE(it->friendly_name.empty()); |
| 635 | 130 |
| (...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1142 NetworkInterface interface4; | 637 NetworkInterface interface4; |
| 1143 interface4.name = "eth0"; | 638 interface4.name = "eth0"; |
| 1144 interface4.type = NetworkChangeNotifier::CONNECTION_ETHERNET; | 639 interface4.type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 1145 list.push_back(interface4); | 640 list.push_back(interface4); |
| 1146 ASSERT_EQ(3u, list.size()); | 641 ASSERT_EQ(3u, list.size()); |
| 1147 EXPECT_EQ(std::string(), internal::GetWifiSSIDFromInterfaceListInternal( | 642 EXPECT_EQ(std::string(), internal::GetWifiSSIDFromInterfaceListInternal( |
| 1148 list, TestGetInterfaceSSID)); | 643 list, TestGetInterfaceSSID)); |
| 1149 } | 644 } |
| 1150 #endif // OS_LINUX | 645 #endif // OS_LINUX |
| 1151 | 646 |
| 1152 namespace { | |
| 1153 | |
| 1154 #if defined(OS_WIN) | 647 #if defined(OS_WIN) |
| 1155 bool read_int_or_bool(DWORD data_size, | 648 bool read_int_or_bool(DWORD data_size, |
| 1156 PVOID data) { | 649 PVOID data) { |
| 1157 switch (data_size) { | 650 switch (data_size) { |
| 1158 case 1: | 651 case 1: |
| 1159 return !!*reinterpret_cast<uint8*>(data); | 652 return !!*reinterpret_cast<uint8*>(data); |
| 1160 case 4: | 653 case 4: |
| 1161 return !!*reinterpret_cast<uint32*>(data); | 654 return !!*reinterpret_cast<uint32*>(data); |
| 1162 default: | 655 default: |
| 1163 LOG(FATAL) << "That is not a type I know!"; | 656 LOG(FATAL) << "That is not a type I know!"; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1240 #endif // OS_WIN | 733 #endif // OS_WIN |
| 1241 | 734 |
| 1242 void TryChangeWifiOptions(int options) { | 735 void TryChangeWifiOptions(int options) { |
| 1243 int previous_options = GetWifiOptions(); | 736 int previous_options = GetWifiOptions(); |
| 1244 scoped_ptr<ScopedWifiOptions> scoped_options = SetWifiOptions(options); | 737 scoped_ptr<ScopedWifiOptions> scoped_options = SetWifiOptions(options); |
| 1245 EXPECT_EQ(previous_options | options, GetWifiOptions()); | 738 EXPECT_EQ(previous_options | options, GetWifiOptions()); |
| 1246 scoped_options.reset(); | 739 scoped_options.reset(); |
| 1247 EXPECT_EQ(previous_options, GetWifiOptions()); | 740 EXPECT_EQ(previous_options, GetWifiOptions()); |
| 1248 } | 741 } |
| 1249 | 742 |
| 1250 }; // namespace | |
| 1251 | |
| 1252 // Test SetWifiOptions(). | 743 // Test SetWifiOptions(). |
| 1253 TEST(NetUtilTest, SetWifiOptionsTest) { | 744 TEST(NetUtilTest, SetWifiOptionsTest) { |
| 1254 TryChangeWifiOptions(0); | 745 TryChangeWifiOptions(0); |
| 1255 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN); | 746 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN); |
| 1256 TryChangeWifiOptions(WIFI_OPTIONS_MEDIA_STREAMING_MODE); | 747 TryChangeWifiOptions(WIFI_OPTIONS_MEDIA_STREAMING_MODE); |
| 1257 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN | | 748 TryChangeWifiOptions(WIFI_OPTIONS_DISABLE_SCAN | |
| 1258 WIFI_OPTIONS_MEDIA_STREAMING_MODE); | 749 WIFI_OPTIONS_MEDIA_STREAMING_MODE); |
| 1259 } | 750 } |
| 1260 | 751 |
| 1261 struct NonUniqueNameTestData { | 752 } // namespace |
| 1262 bool is_unique; | |
| 1263 const char* const hostname; | |
| 1264 }; | |
| 1265 | |
| 1266 // Google Test pretty-printer. | |
| 1267 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { | |
| 1268 ASSERT_TRUE(data.hostname); | |
| 1269 *os << " hostname: " << testing::PrintToString(data.hostname) | |
| 1270 << "; is_unique: " << testing::PrintToString(data.is_unique); | |
| 1271 } | |
| 1272 | |
| 1273 const NonUniqueNameTestData kNonUniqueNameTestData[] = { | |
| 1274 // Domains under ICANN-assigned domains. | |
| 1275 { true, "google.com" }, | |
| 1276 { true, "google.co.uk" }, | |
| 1277 // Domains under private registries. | |
| 1278 { true, "appspot.com" }, | |
| 1279 { true, "test.appspot.com" }, | |
| 1280 // Unreserved IPv4 addresses (in various forms). | |
| 1281 { true, "8.8.8.8" }, | |
| 1282 { true, "99.64.0.0" }, | |
| 1283 { true, "212.15.0.0" }, | |
| 1284 { true, "212.15" }, | |
| 1285 { true, "212.15.0" }, | |
| 1286 { true, "3557752832" }, | |
| 1287 // Reserved IPv4 addresses (in various forms). | |
| 1288 { false, "192.168.0.0" }, | |
| 1289 { false, "192.168.0.6" }, | |
| 1290 { false, "10.0.0.5" }, | |
| 1291 { false, "10.0" }, | |
| 1292 { false, "10.0.0" }, | |
| 1293 { false, "3232235526" }, | |
| 1294 // Unreserved IPv6 addresses. | |
| 1295 { true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, | |
| 1296 { true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210" }, | |
| 1297 // Reserved IPv6 addresses. | |
| 1298 { false, "::192.9.5.5" }, | |
| 1299 { false, "FEED::BEEF" }, | |
| 1300 { false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, | |
| 1301 // 'internal'/non-IANA assigned domains. | |
| 1302 { false, "intranet" }, | |
| 1303 { false, "intranet." }, | |
| 1304 { false, "intranet.example" }, | |
| 1305 { false, "host.intranet.example" }, | |
| 1306 // gTLDs under discussion, but not yet assigned. | |
| 1307 { false, "intranet.corp" }, | |
| 1308 { false, "intranet.internal" }, | |
| 1309 // Invalid host names are treated as unique - but expected to be | |
| 1310 // filtered out before then. | |
| 1311 { true, "junk)(£)$*!@~#" }, | |
| 1312 { true, "w$w.example.com" }, | |
| 1313 { true, "nocolonsallowed:example" }, | |
| 1314 { true, "[::4.5.6.9]" }, | |
| 1315 }; | |
| 1316 | |
| 1317 class NetUtilNonUniqueNameTest | |
| 1318 : public testing::TestWithParam<NonUniqueNameTestData> { | |
| 1319 public: | |
| 1320 virtual ~NetUtilNonUniqueNameTest() {} | |
| 1321 | |
| 1322 protected: | |
| 1323 bool IsUnique(const std::string& hostname) { | |
| 1324 return !IsHostnameNonUnique(hostname); | |
| 1325 } | |
| 1326 }; | |
| 1327 | |
| 1328 // Test that internal/non-unique names are properly identified as such, but | |
| 1329 // that IP addresses and hosts beneath registry-controlled domains are flagged | |
| 1330 // as unique names. | |
| 1331 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { | |
| 1332 const NonUniqueNameTestData& test_data = GetParam(); | |
| 1333 | |
| 1334 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); | |
| 1335 } | |
| 1336 | |
| 1337 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, | |
| 1338 testing::ValuesIn(kNonUniqueNameTestData)); | |
| 1339 | 753 |
| 1340 } // namespace net | 754 } // namespace net |
| OLD | NEW |