| 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 <ostream> | |
| 8 | |
| 9 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "net/base/address_list.h" | 8 #include "net/base/address_list.h" |
| 14 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 #if !defined(OS_NACL) && !defined(OS_WIN) | |
| 19 #include <net/if.h> | |
| 20 #include <netinet/in.h> | |
| 21 #if defined(OS_MACOSX) | |
| 22 #include <ifaddrs.h> | |
| 23 #if !defined(OS_IOS) | |
| 24 #include <netinet/in_var.h> | |
| 25 #endif // !OS_IOS | |
| 26 #endif // OS_MACOSX | |
| 27 #endif // !OS_NACL && !OS_WIN | |
| 28 | |
| 29 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_WIN) | |
| 30 #include "net/base/address_tracker_linux.h" | |
| 31 #endif // !OS_MACOSX && !OS_NACL && !OS_WIN | |
| 32 | 11 |
| 33 namespace net { | 12 namespace net { |
| 34 | 13 |
| 35 namespace { | 14 namespace { |
| 36 | 15 |
| 37 const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1}; | 16 const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1}; |
| 38 const unsigned char kLocalhostIPv6[] = | 17 const unsigned char kLocalhostIPv6[] = |
| 39 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; | 18 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
| 40 const uint16_t kLocalhostLookupPort = 80; | 19 const uint16_t kLocalhostLookupPort = 80; |
| 41 | 20 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 &addresses)); | 160 &addresses)); |
| 182 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); | 161 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); |
| 183 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, | 162 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, |
| 184 &addresses)); | 163 &addresses)); |
| 185 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, | 164 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, |
| 186 &addresses)); | 165 &addresses)); |
| 187 EXPECT_FALSE( | 166 EXPECT_FALSE( |
| 188 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); | 167 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); |
| 189 } | 168 } |
| 190 | 169 |
| 191 struct NonUniqueNameTestData { | |
| 192 bool is_unique; | |
| 193 const char* const hostname; | |
| 194 }; | |
| 195 | |
| 196 // Google Test pretty-printer. | |
| 197 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { | |
| 198 ASSERT_TRUE(data.hostname); | |
| 199 *os << " hostname: " << testing::PrintToString(data.hostname) | |
| 200 << "; is_unique: " << testing::PrintToString(data.is_unique); | |
| 201 } | |
| 202 | |
| 203 const NonUniqueNameTestData kNonUniqueNameTestData[] = { | |
| 204 // Domains under ICANN-assigned domains. | |
| 205 { true, "google.com" }, | |
| 206 { true, "google.co.uk" }, | |
| 207 // Domains under private registries. | |
| 208 { true, "appspot.com" }, | |
| 209 { true, "test.appspot.com" }, | |
| 210 // Unreserved IPv4 addresses (in various forms). | |
| 211 { true, "8.8.8.8" }, | |
| 212 { true, "99.64.0.0" }, | |
| 213 { true, "212.15.0.0" }, | |
| 214 { true, "212.15" }, | |
| 215 { true, "212.15.0" }, | |
| 216 { true, "3557752832" }, | |
| 217 // Reserved IPv4 addresses (in various forms). | |
| 218 { false, "192.168.0.0" }, | |
| 219 { false, "192.168.0.6" }, | |
| 220 { false, "10.0.0.5" }, | |
| 221 { false, "10.0" }, | |
| 222 { false, "10.0.0" }, | |
| 223 { false, "3232235526" }, | |
| 224 // Unreserved IPv6 addresses. | |
| 225 { true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, | |
| 226 { true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210" }, | |
| 227 // Reserved IPv6 addresses. | |
| 228 { false, "::192.9.5.5" }, | |
| 229 { false, "FEED::BEEF" }, | |
| 230 { false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, | |
| 231 // 'internal'/non-IANA assigned domains. | |
| 232 { false, "intranet" }, | |
| 233 { false, "intranet." }, | |
| 234 { false, "intranet.example" }, | |
| 235 { false, "host.intranet.example" }, | |
| 236 // gTLDs under discussion, but not yet assigned. | |
| 237 { false, "intranet.corp" }, | |
| 238 { false, "intranet.internal" }, | |
| 239 // Invalid host names are treated as unique - but expected to be | |
| 240 // filtered out before then. | |
| 241 { true, "junk)(£)$*!@~#" }, | |
| 242 { true, "w$w.example.com" }, | |
| 243 { true, "nocolonsallowed:example" }, | |
| 244 { true, "[::4.5.6.9]" }, | |
| 245 }; | |
| 246 | |
| 247 class NetUtilNonUniqueNameTest | |
| 248 : public testing::TestWithParam<NonUniqueNameTestData> { | |
| 249 public: | |
| 250 virtual ~NetUtilNonUniqueNameTest() {} | |
| 251 | |
| 252 protected: | |
| 253 bool IsUnique(const std::string& hostname) { | |
| 254 return !IsHostnameNonUnique(hostname); | |
| 255 } | |
| 256 }; | |
| 257 | |
| 258 // Test that internal/non-unique names are properly identified as such, but | |
| 259 // that IP addresses and hosts beneath registry-controlled domains are flagged | |
| 260 // as unique names. | |
| 261 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { | |
| 262 const NonUniqueNameTestData& test_data = GetParam(); | |
| 263 | |
| 264 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); | |
| 265 } | |
| 266 | |
| 267 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, | |
| 268 testing::ValuesIn(kNonUniqueNameTestData)); | |
| 269 | |
| 270 } // namespace net | 170 } // namespace net |
| OLD | NEW |