Chromium Code Reviews| 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 <ostream> | 9 #include <ostream> |
| 10 | 10 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 } | 75 } |
| 76 | 76 |
| 77 } // anonymous namespace | 77 } // anonymous namespace |
| 78 | 78 |
| 79 TEST(NetUtilTest, GetIdentityFromURL) { | 79 TEST(NetUtilTest, GetIdentityFromURL) { |
| 80 struct { | 80 struct { |
| 81 const char* input_url; | 81 const char* input_url; |
| 82 const char* expected_username; | 82 const char* expected_username; |
| 83 const char* expected_password; | 83 const char* expected_password; |
| 84 } tests[] = { | 84 } tests[] = { |
| 85 { | 85 { |
| 86 "http://username:password@google.com", | 86 "http://username:password@google.com", "username", "password", |
| 87 "username", | 87 }, |
| 88 "password", | 88 {// Test for http://crbug.com/19200 |
| 89 }, | 89 "http://username:p@ssword@google.com", |
| 90 { // Test for http://crbug.com/19200 | 90 "username", |
| 91 "http://username:p@ssword@google.com", | 91 "p@ssword", |
| 92 "username", | 92 }, |
| 93 "p@ssword", | 93 {// Special URL characters should be unescaped. |
| 94 }, | 94 "http://username:p%3fa%26s%2fs%23@google.com", |
| 95 { // Special URL characters should be unescaped. | 95 "username", |
| 96 "http://username:p%3fa%26s%2fs%23@google.com", | 96 "p?a&s/s#", |
| 97 "username", | 97 }, |
| 98 "p?a&s/s#", | 98 {// Username contains %20. |
| 99 }, | 99 "http://use rname:password@google.com", |
| 100 { // Username contains %20. | 100 "use rname", |
| 101 "http://use rname:password@google.com", | 101 "password", |
| 102 "use rname", | 102 }, |
| 103 "password", | 103 {// Keep %00 as is. |
| 104 }, | 104 "http://use%00rname:password@google.com", |
| 105 { // Keep %00 as is. | 105 "use%00rname", |
| 106 "http://use%00rname:password@google.com", | 106 "password", |
| 107 "use%00rname", | 107 }, |
| 108 "password", | 108 {// Use a '+' in the username. |
| 109 }, | 109 "http://use+rname:password@google.com", |
| 110 { // Use a '+' in the username. | 110 "use+rname", |
| 111 "http://use+rname:password@google.com", | 111 "password", |
| 112 "use+rname", | 112 }, |
| 113 "password", | 113 {// Use a '&' in the password. |
| 114 }, | 114 "http://username:p&ssword@google.com", |
| 115 { // Use a '&' in the password. | 115 "username", |
| 116 "http://username:p&ssword@google.com", | 116 "p&ssword", |
| 117 "username", | 117 }, |
| 118 "p&ssword", | 118 }; |
| 119 }, | |
| 120 }; | |
| 121 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 119 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 122 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | 120 SCOPED_TRACE( |
| 123 tests[i].input_url)); | 121 base::StringPrintf("Test[%" PRIuS "]: %s", i, tests[i].input_url)); |
| 124 GURL url(tests[i].input_url); | 122 GURL url(tests[i].input_url); |
| 125 | 123 |
| 126 base::string16 username, password; | 124 base::string16 username, password; |
| 127 GetIdentityFromURL(url, &username, &password); | 125 GetIdentityFromURL(url, &username, &password); |
| 128 | 126 |
| 129 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); | 127 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); |
| 130 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); | 128 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); |
| 131 } | 129 } |
| 132 } | 130 } |
| 133 | 131 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 153 "Content-TYPE: text/html; charset=utf-8\n" | 151 "Content-TYPE: text/html; charset=utf-8\n" |
| 154 "Content-disposition: attachment; filename=\"download.pdf\"\n" | 152 "Content-disposition: attachment; filename=\"download.pdf\"\n" |
| 155 "Content-Length: 378557\n" | 153 "Content-Length: 378557\n" |
| 156 "X-Google-Google1: 314159265\n" | 154 "X-Google-Google1: 314159265\n" |
| 157 "X-Google-Google2: aaaa2:7783,bbb21:9441\n" | 155 "X-Google-Google2: aaaa2:7783,bbb21:9441\n" |
| 158 "X-Google-Google4: home\n" | 156 "X-Google-Google4: home\n" |
| 159 "Transfer-Encoding: chunked\n" | 157 "Transfer-Encoding: chunked\n" |
| 160 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n" | 158 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n" |
| 161 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n" | 159 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n" |
| 162 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee" | 160 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee" |
| 163 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n" | 161 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n" |
| 164 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n" | 162 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n" |
| 165 "Server: GFE/1.3\n" | 163 "Server: GFE/1.3\n" |
| 166 "Transfer-Encoding: chunked\n" | 164 "Transfer-Encoding: chunked\n" |
| 167 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n" | 165 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n" |
| 168 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n" | 166 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n" |
| 169 "X-Malformed: bla; arg=test\"\n" | 167 "X-Malformed: bla; arg=test\"\n" |
| 170 "X-Malformed2: bla; arg=\n" | 168 "X-Malformed2: bla; arg=\n" |
| 171 "X-Test: bla; arg1=val1; arg2=val2"; | 169 "X-Test: bla; arg1=val1; arg2=val2"; |
| 172 | 170 |
| 173 TEST(NetUtilTest, GetSpecificHeader) { | 171 TEST(NetUtilTest, GetSpecificHeader) { |
| 174 const HeaderCase tests[] = { | 172 const HeaderCase tests[] = { |
| 175 {"content-type", "text/html; charset=utf-8"}, | 173 {"content-type", "text/html; charset=utf-8"}, |
| 176 {"CONTENT-LENGTH", "378557"}, | 174 {"CONTENT-LENGTH", "378557"}, |
| 177 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"}, | 175 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"}, |
| 178 {"Bad-Header", ""}, | 176 {"Bad-Header", ""}, |
| 179 {"", ""}, | 177 {"", ""}, |
| 180 }; | 178 }; |
| 181 | 179 |
| 182 // Test first with google_headers. | 180 // Test first with google_headers. |
| 183 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 181 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 184 std::string result = | 182 std::string result = |
| 185 GetSpecificHeader(google_headers, tests[i].header_name); | 183 GetSpecificHeader(google_headers, tests[i].header_name); |
| 186 EXPECT_EQ(result, tests[i].expected); | 184 EXPECT_EQ(result, tests[i].expected); |
| 187 } | 185 } |
| 188 | 186 |
| 189 // Test again with empty headers. | 187 // Test again with empty headers. |
| 190 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 188 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 191 std::string result = GetSpecificHeader(std::string(), tests[i].header_name); | 189 std::string result = GetSpecificHeader(std::string(), tests[i].header_name); |
| 192 EXPECT_EQ(result, std::string()); | 190 EXPECT_EQ(result, std::string()); |
| 193 } | 191 } |
| 194 } | 192 } |
| 195 | 193 |
| 196 TEST(NetUtilTest, CompliantHost) { | 194 TEST(NetUtilTest, CompliantHost) { |
| 197 struct CompliantHostCase { | 195 struct CompliantHostCase { |
| 198 const char* host; | 196 const char* host; |
| 199 bool expected_output; | 197 bool expected_output; |
| 200 }; | 198 }; |
| 201 | 199 |
| 202 const CompliantHostCase compliant_host_cases[] = { | 200 const CompliantHostCase compliant_host_cases[] = { |
| 203 {"", false}, | 201 {"", false}, |
| 204 {"a", true}, | 202 {"a", true}, |
| 205 {"-", false}, | 203 {"-", false}, |
| 206 {".", false}, | 204 {".", false}, |
| 207 {"9", true}, | 205 {"9", true}, |
| 208 {"9a", true}, | 206 {"9a", true}, |
| 209 {"a.", true}, | 207 {"a.", true}, |
| 210 {"a.a", true}, | 208 {"a.a", true}, |
| 211 {"9.a", true}, | 209 {"9.a", true}, |
| 212 {"a.9", true}, | 210 {"a.9", true}, |
| 213 {"_9a", false}, | 211 {"_9a", false}, |
| 214 {"-9a", false}, | 212 {"-9a", false}, |
| 215 {"a.a9", true}, | 213 {"a.a9", true}, |
| 216 {"a.-a9", false}, | 214 {"a.-a9", false}, |
| 217 {"a+9a", false}, | 215 {"a+9a", false}, |
| 218 {"-a.a9", true}, | 216 {"-a.a9", true}, |
| 219 {"1-.a-b", true}, | 217 {"1-.a-b", true}, |
| 220 {"1_.a-b", false}, | 218 {"1_.a-b", false}, |
| 221 {"1-2.a_b", true}, | 219 {"1-2.a_b", true}, |
| 222 {"a.b.c.d.e", true}, | 220 {"a.b.c.d.e", true}, |
| 223 {"1.2.3.4.5", true}, | 221 {"1.2.3.4.5", true}, |
| 224 {"1.2.3.4.5.", true}, | 222 {"1.2.3.4.5.", true}, |
| 225 }; | 223 }; |
| 226 | 224 |
| 227 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(compliant_host_cases); ++i) { | 225 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(compliant_host_cases); ++i) { |
| 228 EXPECT_EQ(compliant_host_cases[i].expected_output, | 226 EXPECT_EQ(compliant_host_cases[i].expected_output, |
| 229 IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); | 227 IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); |
| 230 } | 228 } |
| 231 } | 229 } |
| 232 | 230 |
| 233 TEST(NetUtilTest, ParseHostAndPort) { | 231 TEST(NetUtilTest, ParseHostAndPort) { |
| 234 const struct { | 232 const struct { |
| 235 const char* input; | 233 const char* input; |
| 236 bool success; | 234 bool success; |
| 237 const char* expected_host; | 235 const char* expected_host; |
| 238 int expected_port; | 236 int expected_port; |
| 239 } tests[] = { | 237 } tests[] = {// Valid inputs: |
| 240 // Valid inputs: | 238 {"foo:10", true, "foo", 10}, |
|
davidben
2014/10/10 20:24:16
This looks worth a bug. I think it overlaps with a
| |
| 241 {"foo:10", true, "foo", 10}, | 239 {"foo", true, "foo", -1}, |
| 242 {"foo", true, "foo", -1}, | 240 {"[1080:0:0:0:8:800:200C:4171]:11", |
| 243 { | 241 true, |
| 244 "[1080:0:0:0:8:800:200C:4171]:11", | 242 "1080:0:0:0:8:800:200C:4171", |
| 245 true, | 243 11}, |
| 246 "1080:0:0:0:8:800:200C:4171", | 244 {"[1080:0:0:0:8:800:200C:4171]", |
| 247 11 | 245 true, |
| 248 }, | 246 "1080:0:0:0:8:800:200C:4171", |
| 249 { | 247 -1}, |
| 250 "[1080:0:0:0:8:800:200C:4171]", | |
| 251 true, | |
| 252 "1080:0:0:0:8:800:200C:4171", | |
| 253 -1 | |
| 254 }, | |
| 255 | 248 |
| 256 // Because no validation is done on the host, the following are accepted, | 249 // Because no validation is done on the host, the following are |
| 257 // even though they are invalid names. | 250 // accepted, |
| 258 {"]", true, "]", -1}, | 251 // even though they are invalid names. |
| 259 {"::1", true, ":", 1}, | 252 {"]", true, "]", -1}, |
| 260 // Invalid inputs: | 253 {"::1", true, ":", 1}, |
| 261 {"foo:bar", false, "", -1}, | 254 // Invalid inputs: |
| 262 {"foo:", false, "", -1}, | 255 {"foo:bar", false, "", -1}, |
| 263 {":", false, "", -1}, | 256 {"foo:", false, "", -1}, |
| 264 {":80", false, "", -1}, | 257 {":", false, "", -1}, |
| 265 {"", false, "", -1}, | 258 {":80", false, "", -1}, |
| 266 {"porttoolong:300000", false, "", -1}, | 259 {"", false, "", -1}, |
| 267 {"usrname@host", false, "", -1}, | 260 {"porttoolong:300000", false, "", -1}, |
| 268 {"usrname:password@host", false, "", -1}, | 261 {"usrname@host", false, "", -1}, |
| 269 {":password@host", false, "", -1}, | 262 {"usrname:password@host", false, "", -1}, |
| 270 {":password@host:80", false, "", -1}, | 263 {":password@host", false, "", -1}, |
| 271 {":password@host", false, "", -1}, | 264 {":password@host:80", false, "", -1}, |
| 272 {"@host", false, "", -1}, | 265 {":password@host", false, "", -1}, |
| 273 {"[", false, "", -1}, | 266 {"@host", false, "", -1}, |
| 274 {"[]", false, "", -1}, | 267 {"[", false, "", -1}, |
| 275 }; | 268 {"[]", false, "", -1}, |
| 269 }; | |
| 276 | 270 |
| 277 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 271 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 278 std::string host; | 272 std::string host; |
| 279 int port; | 273 int port; |
| 280 bool ok = ParseHostAndPort(tests[i].input, &host, &port); | 274 bool ok = ParseHostAndPort(tests[i].input, &host, &port); |
| 281 | 275 |
| 282 EXPECT_EQ(tests[i].success, ok); | 276 EXPECT_EQ(tests[i].success, ok); |
| 283 | 277 |
| 284 if (tests[i].success) { | 278 if (tests[i].success) { |
| 285 EXPECT_EQ(tests[i].expected_host, host); | 279 EXPECT_EQ(tests[i].expected_host, host); |
| 286 EXPECT_EQ(tests[i].expected_port, port); | 280 EXPECT_EQ(tests[i].expected_port, port); |
| 287 } | 281 } |
| 288 } | 282 } |
| 289 } | 283 } |
| 290 | 284 |
| 291 TEST(NetUtilTest, GetHostAndPort) { | 285 TEST(NetUtilTest, GetHostAndPort) { |
| 292 const struct { | 286 const struct { |
| 293 GURL url; | 287 GURL url; |
| 294 const char* expected_host_and_port; | 288 const char* expected_host_and_port; |
| 295 } tests[] = { | 289 } tests[] = { |
| 296 { GURL("http://www.foo.com/x"), "www.foo.com:80"}, | 290 {GURL("http://www.foo.com/x"), "www.foo.com:80"}, |
| 297 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, | 291 {GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, |
| 298 | 292 |
| 299 // For IPv6 literals should always include the brackets. | 293 // For IPv6 literals should always include the brackets. |
| 300 { GURL("http://[1::2]/x"), "[1::2]:80"}, | 294 {GURL("http://[1::2]/x"), "[1::2]:80"}, |
| 301 { GURL("http://[::a]:33/x"), "[::a]:33"}, | 295 {GURL("http://[::a]:33/x"), "[::a]:33"}, |
| 302 }; | 296 }; |
| 303 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 297 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 304 std::string host_and_port = GetHostAndPort(tests[i].url); | 298 std::string host_and_port = GetHostAndPort(tests[i].url); |
| 305 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); | 299 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); |
| 306 } | 300 } |
| 307 } | 301 } |
| 308 | 302 |
| 309 TEST(NetUtilTest, GetHostAndOptionalPort) { | 303 TEST(NetUtilTest, GetHostAndOptionalPort) { |
| 310 const struct { | 304 const struct { |
| 311 GURL url; | 305 GURL url; |
| 312 const char* expected_host_and_port; | 306 const char* expected_host_and_port; |
| 313 } tests[] = { | 307 } tests[] = { |
| 314 { GURL("http://www.foo.com/x"), "www.foo.com"}, | 308 {GURL("http://www.foo.com/x"), "www.foo.com"}, |
| 315 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, | 309 {GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, |
| 316 | 310 |
| 317 // For IPv6 literals should always include the brackets. | 311 // For IPv6 literals should always include the brackets. |
| 318 { GURL("http://[1::2]/x"), "[1::2]"}, | 312 {GURL("http://[1::2]/x"), "[1::2]"}, |
| 319 { GURL("http://[::a]:33/x"), "[::a]:33"}, | 313 {GURL("http://[::a]:33/x"), "[::a]:33"}, |
| 320 }; | 314 }; |
| 321 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 315 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 322 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); | 316 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); |
| 323 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); | 317 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); |
| 324 } | 318 } |
| 325 } | 319 } |
| 326 | 320 |
| 327 TEST(NetUtilTest, IPAddressToString) { | 321 TEST(NetUtilTest, IPAddressToString) { |
| 328 uint8 addr1[4] = {0, 0, 0, 0}; | 322 uint8 addr1[4] = {0, 0, 0, 0}; |
| 329 EXPECT_EQ("0.0.0.0", IPAddressToString(addr1, sizeof(addr1))); | 323 EXPECT_EQ("0.0.0.0", IPAddressToString(addr1, sizeof(addr1))); |
| 330 | 324 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 346 uint8 addr3[16] = {0xFE, 0xDC, 0xBA, 0x98}; | 340 uint8 addr3[16] = {0xFE, 0xDC, 0xBA, 0x98}; |
| 347 EXPECT_EQ("[fedc:ba98::]:8080", | 341 EXPECT_EQ("[fedc:ba98::]:8080", |
| 348 IPAddressToStringWithPort(addr3, sizeof(addr3), 8080)); | 342 IPAddressToStringWithPort(addr3, sizeof(addr3), 8080)); |
| 349 } | 343 } |
| 350 | 344 |
| 351 TEST(NetUtilTest, NetAddressToString_IPv4) { | 345 TEST(NetUtilTest, NetAddressToString_IPv4) { |
| 352 const struct { | 346 const struct { |
| 353 uint8 addr[4]; | 347 uint8 addr[4]; |
| 354 const char* result; | 348 const char* result; |
| 355 } tests[] = { | 349 } tests[] = { |
| 356 {{0, 0, 0, 0}, "0.0.0.0"}, | 350 {{0, 0, 0, 0}, "0.0.0.0"}, |
| 357 {{127, 0, 0, 1}, "127.0.0.1"}, | 351 {{127, 0, 0, 1}, "127.0.0.1"}, |
| 358 {{192, 168, 0, 1}, "192.168.0.1"}, | 352 {{192, 168, 0, 1}, "192.168.0.1"}, |
| 359 }; | 353 }; |
| 360 | 354 |
| 361 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 355 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 362 SockaddrStorage storage; | 356 SockaddrStorage storage; |
| 363 MakeIPv4Address(tests[i].addr, 80, &storage); | 357 MakeIPv4Address(tests[i].addr, 80, &storage); |
| 364 std::string result = NetAddressToString(storage.addr, storage.addr_len); | 358 std::string result = NetAddressToString(storage.addr, storage.addr_len); |
| 365 EXPECT_EQ(std::string(tests[i].result), result); | 359 EXPECT_EQ(std::string(tests[i].result), result); |
| 366 } | 360 } |
| 367 } | 361 } |
| 368 | 362 |
| 369 TEST(NetUtilTest, NetAddressToString_IPv6) { | 363 TEST(NetUtilTest, NetAddressToString_IPv6) { |
| 370 const struct { | 364 const struct { |
| 371 uint8 addr[16]; | 365 uint8 addr[16]; |
| 372 const char* result; | 366 const char* result; |
| 373 } tests[] = { | 367 } tests[] = { |
| 374 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | 368 {{0xFE, |
| 375 0x98, 0x76, 0x54, 0x32, 0x10}, | 369 0xDC, |
| 376 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, | 370 0xBA, |
| 377 }; | 371 0x98, |
| 372 0x76, | |
| 373 0x54, | |
| 374 0x32, | |
| 375 0x10, | |
| 376 0xFE, | |
| 377 0xDC, | |
| 378 0xBA, | |
| 379 0x98, | |
| 380 0x76, | |
| 381 0x54, | |
| 382 0x32, | |
| 383 0x10}, | |
| 384 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, | |
|
davidben
2014/10/10 20:24:16
This block also looks bizarrely indented.
| |
| 385 }; | |
| 378 | 386 |
| 379 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 387 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 380 SockaddrStorage storage; | 388 SockaddrStorage storage; |
| 381 MakeIPv6Address(tests[i].addr, 80, &storage); | 389 MakeIPv6Address(tests[i].addr, 80, &storage); |
| 382 EXPECT_EQ(std::string(tests[i].result), | 390 EXPECT_EQ(std::string(tests[i].result), |
| 383 NetAddressToString(storage.addr, storage.addr_len)); | 391 NetAddressToString(storage.addr, storage.addr_len)); |
| 384 } | 392 } |
| 385 } | 393 } |
| 386 | 394 |
| 387 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { | 395 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { |
| 388 uint8 addr[] = {127, 0, 0, 1}; | 396 uint8 addr[] = {127, 0, 0, 1}; |
| 389 SockaddrStorage storage; | 397 SockaddrStorage storage; |
| 390 MakeIPv4Address(addr, 166, &storage); | 398 MakeIPv4Address(addr, 166, &storage); |
| 391 std::string result = NetAddressToStringWithPort(storage.addr, | 399 std::string result = |
| 392 storage.addr_len); | 400 NetAddressToStringWithPort(storage.addr, storage.addr_len); |
| 393 EXPECT_EQ("127.0.0.1:166", result); | 401 EXPECT_EQ("127.0.0.1:166", result); |
| 394 } | 402 } |
| 395 | 403 |
| 396 TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) { | 404 TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) { |
| 397 uint8 addr[] = { | 405 uint8 addr[] = {0xFE, |
| 398 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | 406 0xDC, |
| 399 0x98, 0x76, 0x54, 0x32, 0x10 | 407 0xBA, |
| 400 }; | 408 0x98, |
| 409 0x76, | |
| 410 0x54, | |
| 411 0x32, | |
| 412 0x10, | |
| 413 0xFE, | |
| 414 0xDC, | |
| 415 0xBA, | |
| 416 0x98, | |
| 417 0x76, | |
| 418 0x54, | |
| 419 0x32, | |
| 420 0x10}; | |
|
davidben
2014/10/10 20:24:16
Might also be worth a bug to get this on one line?
| |
| 401 SockaddrStorage storage; | 421 SockaddrStorage storage; |
| 402 MakeIPv6Address(addr, 361, &storage); | 422 MakeIPv6Address(addr, 361, &storage); |
| 403 std::string result = NetAddressToStringWithPort(storage.addr, | 423 std::string result = |
| 404 storage.addr_len); | 424 NetAddressToStringWithPort(storage.addr, storage.addr_len); |
| 405 | 425 |
| 406 // May fail on systems that don't support IPv6. | 426 // May fail on systems that don't support IPv6. |
| 407 if (!result.empty()) | 427 if (!result.empty()) |
| 408 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result); | 428 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result); |
| 409 } | 429 } |
| 410 | 430 |
| 411 TEST(NetUtilTest, GetHostName) { | 431 TEST(NetUtilTest, GetHostName) { |
| 412 // We can't check the result of GetHostName() directly, since the result | 432 // We can't check the result of GetHostName() directly, since the result |
| 413 // will differ across machines. Our goal here is to simply exercise the | 433 // will differ across machines. Our goal here is to simply exercise the |
| 414 // code path, and check that things "look about right". | 434 // code path, and check that things "look about right". |
| 415 std::string hostname = GetHostName(); | 435 std::string hostname = GetHostName(); |
| 416 EXPECT_FALSE(hostname.empty()); | 436 EXPECT_FALSE(hostname.empty()); |
| 417 } | 437 } |
| 418 | 438 |
| 419 TEST(NetUtilTest, SimplifyUrlForRequest) { | 439 TEST(NetUtilTest, SimplifyUrlForRequest) { |
| 420 struct { | 440 struct { |
| 421 const char* input_url; | 441 const char* input_url; |
| 422 const char* expected_simplified_url; | 442 const char* expected_simplified_url; |
| 423 } tests[] = { | 443 } tests[] = { |
| 424 { | 444 {// Reference section should be stripped. |
|
davidben
2014/10/10 20:24:16
Array literal with line comment again.
| |
| 425 // Reference section should be stripped. | 445 "http://www.google.com:78/foobar?query=1#hash", |
| 426 "http://www.google.com:78/foobar?query=1#hash", | 446 "http://www.google.com:78/foobar?query=1", |
| 427 "http://www.google.com:78/foobar?query=1", | 447 }, |
| 428 }, | 448 {// Reference section can itself contain #. |
| 429 { | 449 "http://192.168.0.1?query=1#hash#10#11#13#14", |
| 430 // Reference section can itself contain #. | 450 "http://192.168.0.1?query=1", |
| 431 "http://192.168.0.1?query=1#hash#10#11#13#14", | 451 }, |
| 432 "http://192.168.0.1?query=1", | 452 {// Strip username/password. |
| 433 }, | 453 "http://user:pass@google.com", |
| 434 { // Strip username/password. | 454 "http://google.com/", |
| 435 "http://user:pass@google.com", | 455 }, |
| 436 "http://google.com/", | 456 {// Strip both the reference and the username/password. |
| 437 }, | 457 "http://user:pass@google.com:80/sup?yo#X#X", |
| 438 { // Strip both the reference and the username/password. | 458 "http://google.com/sup?yo", |
| 439 "http://user:pass@google.com:80/sup?yo#X#X", | 459 }, |
| 440 "http://google.com/sup?yo", | 460 {// Try an HTTPS URL -- strip both the reference and the |
| 441 }, | 461 // username/password. |
| 442 { // Try an HTTPS URL -- strip both the reference and the username/password. | 462 "https://user:pass@google.com:80/sup?yo#X#X", |
| 443 "https://user:pass@google.com:80/sup?yo#X#X", | 463 "https://google.com:80/sup?yo", |
| 444 "https://google.com:80/sup?yo", | 464 }, |
| 445 }, | 465 {// Try an FTP URL -- strip both the reference and the |
| 446 { // Try an FTP URL -- strip both the reference and the username/password. | 466 // username/password. |
| 447 "ftp://user:pass@google.com:80/sup?yo#X#X", | 467 "ftp://user:pass@google.com:80/sup?yo#X#X", |
| 448 "ftp://google.com:80/sup?yo", | 468 "ftp://google.com:80/sup?yo", |
| 449 }, | 469 }, |
| 450 { // Try a nonstandard URL | 470 {// Try a nonstandard URL |
| 451 "foobar://user:pass@google.com:80/sup?yo#X#X", | 471 "foobar://user:pass@google.com:80/sup?yo#X#X", |
| 452 "foobar://user:pass@google.com:80/sup?yo", | 472 "foobar://user:pass@google.com:80/sup?yo", |
| 453 }, | 473 }, |
| 454 }; | 474 }; |
| 455 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 475 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 456 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | 476 SCOPED_TRACE( |
| 457 tests[i].input_url)); | 477 base::StringPrintf("Test[%" PRIuS "]: %s", i, tests[i].input_url)); |
| 458 GURL input_url(GURL(tests[i].input_url)); | 478 GURL input_url(GURL(tests[i].input_url)); |
| 459 GURL expected_url(GURL(tests[i].expected_simplified_url)); | 479 GURL expected_url(GURL(tests[i].expected_simplified_url)); |
| 460 EXPECT_EQ(expected_url, SimplifyUrlForRequest(input_url)); | 480 EXPECT_EQ(expected_url, SimplifyUrlForRequest(input_url)); |
| 461 } | 481 } |
| 462 } | 482 } |
| 463 | 483 |
| 464 TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { | 484 TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { |
| 465 std::string invalid[] = { "1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12" }; | 485 std::string invalid[] = {"1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12"}; |
| 466 std::string valid[] = { "", "1", "1,2", "1,2,3", "10,11,12,13" }; | 486 std::string valid[] = {"", "1", "1,2", "1,2,3", "10,11,12,13"}; |
| 467 | 487 |
| 468 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(invalid); ++i) { | 488 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(invalid); ++i) { |
| 469 SetExplicitlyAllowedPorts(invalid[i]); | 489 SetExplicitlyAllowedPorts(invalid[i]); |
| 470 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts())); | 490 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts())); |
| 471 } | 491 } |
| 472 | 492 |
| 473 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { | 493 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { |
| 474 SetExplicitlyAllowedPorts(valid[i]); | 494 SetExplicitlyAllowedPorts(valid[i]); |
| 475 EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts()); | 495 EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts()); |
| 476 } | 496 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 519 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); | 539 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); |
| 520 EXPECT_EQ("0,1,171,205,0,0,0,0,0,0,0,3,0,4,0,255", DumpIPNumber(number)); | 540 EXPECT_EQ("0,1,171,205,0,0,0,0,0,0,0,3,0,4,0,255", DumpIPNumber(number)); |
| 521 EXPECT_EQ("1:abcd::3:4:ff", IPAddressToString(number)); | 541 EXPECT_EQ("1:abcd::3:4:ff", IPAddressToString(number)); |
| 522 } | 542 } |
| 523 | 543 |
| 524 // Test mapping an IPv4 address to an IPv6 address. | 544 // Test mapping an IPv4 address to an IPv6 address. |
| 525 TEST(NetUtilTest, ConvertIPv4NumberToIPv6Number) { | 545 TEST(NetUtilTest, ConvertIPv4NumberToIPv6Number) { |
| 526 IPAddressNumber ipv4_number; | 546 IPAddressNumber ipv4_number; |
| 527 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number)); | 547 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number)); |
| 528 | 548 |
| 529 IPAddressNumber ipv6_number = | 549 IPAddressNumber ipv6_number = ConvertIPv4NumberToIPv6Number(ipv4_number); |
| 530 ConvertIPv4NumberToIPv6Number(ipv4_number); | |
| 531 | 550 |
| 532 // ::ffff:192.168.0.1 | 551 // ::ffff:192.168.0.1 |
| 533 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1", | 552 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1", |
| 534 DumpIPNumber(ipv6_number)); | 553 DumpIPNumber(ipv6_number)); |
| 535 EXPECT_EQ("::ffff:c0a8:1", IPAddressToString(ipv6_number)); | 554 EXPECT_EQ("::ffff:c0a8:1", IPAddressToString(ipv6_number)); |
| 536 } | 555 } |
| 537 | 556 |
| 538 TEST(NetUtilTest, ParseURLHostnameToNumber_FailParse) { | 557 TEST(NetUtilTest, ParseURLHostnameToNumber_FailParse) { |
| 539 IPAddressNumber number; | 558 IPAddressNumber number; |
| 540 | 559 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 578 IPAddressNumber ipv4mapped_number; | 597 IPAddressNumber ipv4mapped_number; |
| 579 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number)); | 598 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number)); |
| 580 IPAddressNumber expected; | 599 IPAddressNumber expected; |
| 581 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected)); | 600 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected)); |
| 582 IPAddressNumber result = ConvertIPv4MappedToIPv4(ipv4mapped_number); | 601 IPAddressNumber result = ConvertIPv4MappedToIPv4(ipv4mapped_number); |
| 583 EXPECT_EQ(expected, result); | 602 EXPECT_EQ(expected, result); |
| 584 } | 603 } |
| 585 | 604 |
| 586 // Test parsing invalid CIDR notation literals. | 605 // Test parsing invalid CIDR notation literals. |
| 587 TEST(NetUtilTest, ParseCIDRBlock_Invalid) { | 606 TEST(NetUtilTest, ParseCIDRBlock_Invalid) { |
| 588 const char* bad_literals[] = { | 607 const char* bad_literals[] = {"foobar", |
| 589 "foobar", | 608 "", |
| 590 "", | 609 "192.168.0.1", |
| 591 "192.168.0.1", | 610 "::1", |
| 592 "::1", | 611 "/", |
| 593 "/", | 612 "/1", |
| 594 "/1", | 613 "1", |
| 595 "1", | 614 "192.168.1.1/-1", |
| 596 "192.168.1.1/-1", | 615 "192.168.1.1/33", |
| 597 "192.168.1.1/33", | 616 "::1/-3", |
| 598 "::1/-3", | 617 "a::3/129", |
| 599 "a::3/129", | 618 "::1/x", |
| 600 "::1/x", | 619 "192.168.0.1//11"}; |
| 601 "192.168.0.1//11" | |
| 602 }; | |
| 603 | 620 |
| 604 for (size_t i = 0; i < arraysize(bad_literals); ++i) { | 621 for (size_t i = 0; i < arraysize(bad_literals); ++i) { |
| 605 IPAddressNumber ip_number; | 622 IPAddressNumber ip_number; |
| 606 size_t prefix_length_in_bits; | 623 size_t prefix_length_in_bits; |
| 607 | 624 |
| 608 EXPECT_FALSE(ParseCIDRBlock(bad_literals[i], | 625 EXPECT_FALSE( |
| 609 &ip_number, | 626 ParseCIDRBlock(bad_literals[i], &ip_number, &prefix_length_in_bits)); |
| 610 &prefix_length_in_bits)); | |
| 611 } | 627 } |
| 612 } | 628 } |
| 613 | 629 |
| 614 // Test parsing a valid CIDR notation literal. | 630 // Test parsing a valid CIDR notation literal. |
| 615 TEST(NetUtilTest, ParseCIDRBlock_Valid) { | 631 TEST(NetUtilTest, ParseCIDRBlock_Valid) { |
| 616 IPAddressNumber ip_number; | 632 IPAddressNumber ip_number; |
| 617 size_t prefix_length_in_bits; | 633 size_t prefix_length_in_bits; |
| 618 | 634 |
| 619 EXPECT_TRUE(ParseCIDRBlock("192.168.0.1/11", | 635 EXPECT_TRUE( |
| 620 &ip_number, | 636 ParseCIDRBlock("192.168.0.1/11", &ip_number, &prefix_length_in_bits)); |
| 621 &prefix_length_in_bits)); | |
| 622 | 637 |
| 623 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number)); | 638 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number)); |
| 624 EXPECT_EQ(11u, prefix_length_in_bits); | 639 EXPECT_EQ(11u, prefix_length_in_bits); |
| 625 } | 640 } |
| 626 | 641 |
| 627 TEST(NetUtilTest, IPNumberMatchesPrefix) { | 642 TEST(NetUtilTest, IPNumberMatchesPrefix) { |
| 628 struct { | 643 struct { |
| 629 const char* cidr_literal; | 644 const char* cidr_literal; |
| 630 const char* ip_literal; | 645 const char* ip_literal; |
| 631 bool expected_to_match; | 646 bool expected_to_match; |
| 632 } tests[] = { | 647 } tests[] = {// IPv4 prefix with IPv4 inputs. |
| 633 // IPv4 prefix with IPv4 inputs. | 648 {"10.10.1.32/27", "10.10.1.44", true}, |
| 634 { | 649 {"10.10.1.32/27", "10.10.1.90", false}, |
| 635 "10.10.1.32/27", | 650 {"10.10.1.32/27", "10.10.1.90", false}, |
|
davidben
2014/10/10 20:24:16
Array literal with line comment again.
| |
| 636 "10.10.1.44", | |
| 637 true | |
| 638 }, | |
| 639 { | |
| 640 "10.10.1.32/27", | |
| 641 "10.10.1.90", | |
| 642 false | |
| 643 }, | |
| 644 { | |
| 645 "10.10.1.32/27", | |
| 646 "10.10.1.90", | |
| 647 false | |
| 648 }, | |
| 649 | 651 |
| 650 // IPv6 prefix with IPv6 inputs. | 652 // IPv6 prefix with IPv6 inputs. |
| 651 { | 653 {"2001:db8::/32", "2001:DB8:3:4::5", true}, |
| 652 "2001:db8::/32", | 654 {"2001:db8::/32", "2001:c8::", false}, |
| 653 "2001:DB8:3:4::5", | |
| 654 true | |
| 655 }, | |
| 656 { | |
| 657 "2001:db8::/32", | |
| 658 "2001:c8::", | |
| 659 false | |
| 660 }, | |
| 661 | 655 |
| 662 // IPv6 prefix with IPv4 inputs. | 656 // IPv6 prefix with IPv4 inputs. |
| 663 { | 657 {"2001:db8::/33", "192.168.0.1", false}, |
| 664 "2001:db8::/33", | 658 {"::ffff:192.168.0.1/112", "192.168.33.77", true}, |
| 665 "192.168.0.1", | |
| 666 false | |
| 667 }, | |
| 668 { | |
| 669 "::ffff:192.168.0.1/112", | |
| 670 "192.168.33.77", | |
| 671 true | |
| 672 }, | |
| 673 | 659 |
| 674 // IPv4 prefix with IPv6 inputs. | 660 // IPv4 prefix with IPv6 inputs. |
| 675 { | 661 {"10.11.33.44/16", "::ffff:0a0b:89", true}, |
| 676 "10.11.33.44/16", | 662 {"10.11.33.44/16", "::ffff:10.12.33.44", false}, |
| 677 "::ffff:0a0b:89", | 663 }; |
| 678 true | |
| 679 }, | |
| 680 { | |
| 681 "10.11.33.44/16", | |
| 682 "::ffff:10.12.33.44", | |
| 683 false | |
| 684 }, | |
| 685 }; | |
| 686 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 664 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 687 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", i, | 665 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", |
| 666 i, | |
| 688 tests[i].cidr_literal, | 667 tests[i].cidr_literal, |
| 689 tests[i].ip_literal)); | 668 tests[i].ip_literal)); |
| 690 | 669 |
| 691 IPAddressNumber ip_number; | 670 IPAddressNumber ip_number; |
| 692 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].ip_literal, &ip_number)); | 671 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].ip_literal, &ip_number)); |
| 693 | 672 |
| 694 IPAddressNumber ip_prefix; | 673 IPAddressNumber ip_prefix; |
| 695 size_t prefix_length_in_bits; | 674 size_t prefix_length_in_bits; |
| 696 | 675 |
| 697 EXPECT_TRUE(ParseCIDRBlock(tests[i].cidr_literal, | 676 EXPECT_TRUE(ParseCIDRBlock( |
| 698 &ip_prefix, | 677 tests[i].cidr_literal, &ip_prefix, &prefix_length_in_bits)); |
| 699 &prefix_length_in_bits)); | |
| 700 | 678 |
| 701 EXPECT_EQ(tests[i].expected_to_match, | 679 EXPECT_EQ( |
| 702 IPNumberMatchesPrefix(ip_number, | 680 tests[i].expected_to_match, |
| 703 ip_prefix, | 681 IPNumberMatchesPrefix(ip_number, ip_prefix, prefix_length_in_bits)); |
| 704 prefix_length_in_bits)); | |
| 705 } | 682 } |
| 706 } | 683 } |
| 707 | 684 |
| 708 TEST(NetUtilTest, IsLocalhost) { | 685 TEST(NetUtilTest, IsLocalhost) { |
| 709 EXPECT_TRUE(net::IsLocalhost("localhost")); | 686 EXPECT_TRUE(net::IsLocalhost("localhost")); |
| 710 EXPECT_TRUE(net::IsLocalhost("localhost.localdomain")); | 687 EXPECT_TRUE(net::IsLocalhost("localhost.localdomain")); |
| 711 EXPECT_TRUE(net::IsLocalhost("localhost6")); | 688 EXPECT_TRUE(net::IsLocalhost("localhost6")); |
| 712 EXPECT_TRUE(net::IsLocalhost("localhost6.localdomain6")); | 689 EXPECT_TRUE(net::IsLocalhost("localhost6.localdomain6")); |
| 713 EXPECT_TRUE(net::IsLocalhost("127.0.0.1")); | 690 EXPECT_TRUE(net::IsLocalhost("127.0.0.1")); |
| 714 EXPECT_TRUE(net::IsLocalhost("127.0.1.0")); | 691 EXPECT_TRUE(net::IsLocalhost("127.0.1.0")); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 730 EXPECT_FALSE(net::IsLocalhost("::1:1")); | 707 EXPECT_FALSE(net::IsLocalhost("::1:1")); |
| 731 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:1:0:0:1")); | 708 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:1:0:0:1")); |
| 732 EXPECT_FALSE(net::IsLocalhost("::1:1")); | 709 EXPECT_FALSE(net::IsLocalhost("::1:1")); |
| 733 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:0:0:0:0:1")); | 710 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:0:0:0:0:1")); |
| 734 } | 711 } |
| 735 | 712 |
| 736 // Verify GetNetworkList(). | 713 // Verify GetNetworkList(). |
| 737 TEST(NetUtilTest, GetNetworkList) { | 714 TEST(NetUtilTest, GetNetworkList) { |
| 738 NetworkInterfaceList list; | 715 NetworkInterfaceList list; |
| 739 ASSERT_TRUE(GetNetworkList(&list, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)); | 716 ASSERT_TRUE(GetNetworkList(&list, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)); |
| 740 for (NetworkInterfaceList::iterator it = list.begin(); | 717 for (NetworkInterfaceList::iterator it = list.begin(); it != list.end(); |
| 741 it != list.end(); ++it) { | 718 ++it) { |
| 742 // Verify that the names are not empty. | 719 // Verify that the names are not empty. |
| 743 EXPECT_FALSE(it->name.empty()); | 720 EXPECT_FALSE(it->name.empty()); |
| 744 EXPECT_FALSE(it->friendly_name.empty()); | 721 EXPECT_FALSE(it->friendly_name.empty()); |
| 745 | 722 |
| 746 // Verify that the address is correct. | 723 // Verify that the address is correct. |
| 747 EXPECT_TRUE(it->address.size() == kIPv4AddressSize || | 724 EXPECT_TRUE(it->address.size() == kIPv4AddressSize || |
| 748 it->address.size() == kIPv6AddressSize) | 725 it->address.size() == kIPv6AddressSize) |
| 749 << "Invalid address of size " << it->address.size(); | 726 << "Invalid address of size " << it->address.size(); |
| 750 bool all_zeroes = true; | 727 bool all_zeroes = true; |
| 751 for (size_t i = 0; i < it->address.size(); ++i) { | 728 for (size_t i = 0; i < it->address.size(); ++i) { |
| 752 if (it->address[i] != 0) { | 729 if (it->address[i] != 0) { |
| 753 all_zeroes = false; | 730 all_zeroes = false; |
| 754 break; | 731 break; |
| 755 } | 732 } |
| 756 } | 733 } |
| 757 EXPECT_FALSE(all_zeroes); | 734 EXPECT_FALSE(all_zeroes); |
| 758 EXPECT_GT(it->network_prefix, 1u); | 735 EXPECT_GT(it->network_prefix, 1u); |
| 759 EXPECT_LE(it->network_prefix, it->address.size() * 8); | 736 EXPECT_LE(it->network_prefix, it->address.size() * 8); |
| 760 | 737 |
| 761 #if defined(OS_WIN) | 738 #if defined(OS_WIN) |
| 762 // On Windows |name| is NET_LUID. | 739 // On Windows |name| is NET_LUID. |
| 763 base::ScopedNativeLibrary phlpapi_lib( | 740 base::ScopedNativeLibrary phlpapi_lib( |
| 764 base::FilePath(FILE_PATH_LITERAL("iphlpapi.dll"))); | 741 base::FilePath(FILE_PATH_LITERAL("iphlpapi.dll"))); |
| 765 ASSERT_TRUE(phlpapi_lib.is_valid()); | 742 ASSERT_TRUE(phlpapi_lib.is_valid()); |
| 766 typedef NETIO_STATUS (WINAPI* ConvertInterfaceIndexToLuid)(NET_IFINDEX, | 743 typedef NETIO_STATUS(WINAPI * ConvertInterfaceIndexToLuid)(NET_IFINDEX, |
| 767 PNET_LUID); | 744 PNET_LUID); |
| 768 ConvertInterfaceIndexToLuid interface_to_luid = | 745 ConvertInterfaceIndexToLuid interface_to_luid = |
| 769 reinterpret_cast<ConvertInterfaceIndexToLuid>( | 746 reinterpret_cast<ConvertInterfaceIndexToLuid>( |
| 770 phlpapi_lib.GetFunctionPointer("ConvertInterfaceIndexToLuid")); | 747 phlpapi_lib.GetFunctionPointer("ConvertInterfaceIndexToLuid")); |
| 771 | 748 |
| 772 typedef NETIO_STATUS (WINAPI* ConvertInterfaceLuidToGuid)(NET_LUID*, | 749 typedef NETIO_STATUS(WINAPI * ConvertInterfaceLuidToGuid)(NET_LUID*, GUID*); |
| 773 GUID*); | |
| 774 ConvertInterfaceLuidToGuid luid_to_guid = | 750 ConvertInterfaceLuidToGuid luid_to_guid = |
| 775 reinterpret_cast<ConvertInterfaceLuidToGuid>( | 751 reinterpret_cast<ConvertInterfaceLuidToGuid>( |
| 776 phlpapi_lib.GetFunctionPointer("ConvertInterfaceLuidToGuid")); | 752 phlpapi_lib.GetFunctionPointer("ConvertInterfaceLuidToGuid")); |
| 777 | 753 |
| 778 if (interface_to_luid && luid_to_guid) { | 754 if (interface_to_luid && luid_to_guid) { |
| 779 NET_LUID luid; | 755 NET_LUID luid; |
| 780 EXPECT_EQ(interface_to_luid(it->interface_index, &luid), NO_ERROR); | 756 EXPECT_EQ(interface_to_luid(it->interface_index, &luid), NO_ERROR); |
| 781 GUID guid; | 757 GUID guid; |
| 782 EXPECT_EQ(luid_to_guid(&luid, &guid), NO_ERROR); | 758 EXPECT_EQ(luid_to_guid(&luid, &guid), NO_ERROR); |
| 783 LPOLESTR name; | 759 LPOLESTR name; |
| 784 StringFromCLSID(guid, &name); | 760 StringFromCLSID(guid, &name); |
| 785 EXPECT_STREQ(base::UTF8ToWide(it->name).c_str(), name); | 761 EXPECT_STREQ(base::UTF8ToWide(it->name).c_str(), name); |
| 786 CoTaskMemFree(name); | 762 CoTaskMemFree(name); |
| 787 continue; | 763 continue; |
| 788 } else { | 764 } else { |
| 789 EXPECT_LT(base::win::GetVersion(), base::win::VERSION_VISTA); | 765 EXPECT_LT(base::win::GetVersion(), base::win::VERSION_VISTA); |
| 790 EXPECT_LT(it->interface_index, 1u << 24u); // Must fit 0.x.x.x. | 766 EXPECT_LT(it->interface_index, 1u << 24u); // Must fit 0.x.x.x. |
| 791 EXPECT_NE(it->interface_index, 0u); // 0 means to use default. | 767 EXPECT_NE(it->interface_index, 0u); // 0 means to use default. |
| 792 } | 768 } |
| 793 if (it->type == NetworkChangeNotifier::CONNECTION_WIFI) { | 769 if (it->type == NetworkChangeNotifier::CONNECTION_WIFI) { |
| 794 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol()); | 770 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol()); |
| 795 } | 771 } |
| 796 #elif !defined(OS_ANDROID) | 772 #elif !defined(OS_ANDROID) |
| 797 char name[IF_NAMESIZE]; | 773 char name[IF_NAMESIZE]; |
| 798 EXPECT_TRUE(if_indextoname(it->interface_index, name)); | 774 EXPECT_TRUE(if_indextoname(it->interface_index, name)); |
| 799 EXPECT_STREQ(it->name.c_str(), name); | 775 EXPECT_STREQ(it->name.c_str(), name); |
| 800 #endif | 776 #endif |
| 801 } | 777 } |
| 802 } | 778 } |
| 803 | 779 |
| 804 namespace { | 780 namespace { |
| 805 | 781 |
| 806 #if defined(OS_WIN) | 782 #if defined(OS_WIN) |
| 807 bool read_int_or_bool(DWORD data_size, | 783 bool read_int_or_bool(DWORD data_size, PVOID data) { |
| 808 PVOID data) { | |
| 809 switch (data_size) { | 784 switch (data_size) { |
| 810 case 1: | 785 case 1: |
| 811 return !!*reinterpret_cast<uint8*>(data); | 786 return !!*reinterpret_cast<uint8*>(data); |
| 812 case 4: | 787 case 4: |
| 813 return !!*reinterpret_cast<uint32*>(data); | 788 return !!*reinterpret_cast<uint32*>(data); |
| 814 default: | 789 default: |
| 815 LOG(FATAL) << "That is not a type I know!"; | 790 LOG(FATAL) << "That is not a type I know!"; |
| 816 return false; | 791 return false; |
| 817 } | 792 } |
| 818 } | 793 } |
| 819 | 794 |
| 820 int GetWifiOptions() { | 795 int GetWifiOptions() { |
| 821 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); | 796 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); |
| 822 if (!wlanapi.initialized) | 797 if (!wlanapi.initialized) |
| 823 return -1; | 798 return -1; |
| 824 | 799 |
| 825 internal::WlanHandle client; | 800 internal::WlanHandle client; |
| 826 DWORD cur_version = 0; | 801 DWORD cur_version = 0; |
| 827 const DWORD kMaxClientVersion = 2; | 802 const DWORD kMaxClientVersion = 2; |
| 828 DWORD result = wlanapi.OpenHandle( | 803 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); |
| 829 kMaxClientVersion, &cur_version, &client); | |
| 830 if (result != ERROR_SUCCESS) | 804 if (result != ERROR_SUCCESS) |
| 831 return -1; | 805 return -1; |
| 832 | 806 |
| 833 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL; | 807 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL; |
| 834 result = wlanapi.enum_interfaces_func(client.Get(), NULL, | 808 result = |
| 835 &interface_list_ptr); | 809 wlanapi.enum_interfaces_func(client.Get(), NULL, &interface_list_ptr); |
| 836 if (result != ERROR_SUCCESS) | 810 if (result != ERROR_SUCCESS) |
| 837 return -1; | 811 return -1; |
| 838 scoped_ptr<WLAN_INTERFACE_INFO_LIST, internal::WlanApiDeleter> interface_list( | 812 scoped_ptr<WLAN_INTERFACE_INFO_LIST, internal::WlanApiDeleter> interface_list( |
| 839 interface_list_ptr); | 813 interface_list_ptr); |
| 840 | 814 |
| 841 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) { | 815 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) { |
| 842 WLAN_INTERFACE_INFO* info = &interface_list->InterfaceInfo[i]; | 816 WLAN_INTERFACE_INFO* info = &interface_list->InterfaceInfo[i]; |
| 843 DWORD data_size; | 817 DWORD data_size; |
| 844 PVOID data; | 818 PVOID data; |
| 845 int options = 0; | 819 int options = 0; |
| 846 result = wlanapi.query_interface_func( | 820 result = |
| 847 client.Get(), | 821 wlanapi.query_interface_func(client.Get(), |
| 848 &info->InterfaceGuid, | 822 &info->InterfaceGuid, |
| 849 wlan_intf_opcode_background_scan_enabled, | 823 wlan_intf_opcode_background_scan_enabled, |
| 850 NULL, | 824 NULL, |
| 851 &data_size, | 825 &data_size, |
| 852 &data, | 826 &data, |
| 853 NULL); | 827 NULL); |
| 854 if (result != ERROR_SUCCESS) | 828 if (result != ERROR_SUCCESS) |
| 855 continue; | 829 continue; |
| 856 if (!read_int_or_bool(data_size, data)) { | 830 if (!read_int_or_bool(data_size, data)) { |
| 857 options |= WIFI_OPTIONS_DISABLE_SCAN; | 831 options |= WIFI_OPTIONS_DISABLE_SCAN; |
| 858 } | 832 } |
| 859 internal::WlanApi::GetInstance().free_memory_func(data); | 833 internal::WlanApi::GetInstance().free_memory_func(data); |
| 860 | 834 |
| 861 result = wlanapi.query_interface_func( | 835 result = wlanapi.query_interface_func(client.Get(), |
| 862 client.Get(), | 836 &info->InterfaceGuid, |
| 863 &info->InterfaceGuid, | 837 wlan_intf_opcode_media_streaming_mode, |
| 864 wlan_intf_opcode_media_streaming_mode, | 838 NULL, |
| 865 NULL, | 839 &data_size, |
| 866 &data_size, | 840 &data, |
| 867 &data, | 841 NULL); |
| 868 NULL); | |
| 869 if (result != ERROR_SUCCESS) | 842 if (result != ERROR_SUCCESS) |
| 870 continue; | 843 continue; |
| 871 if (read_int_or_bool(data_size, data)) { | 844 if (read_int_or_bool(data_size, data)) { |
| 872 options |= WIFI_OPTIONS_MEDIA_STREAMING_MODE; | 845 options |= WIFI_OPTIONS_MEDIA_STREAMING_MODE; |
| 873 } | 846 } |
| 874 internal::WlanApi::GetInstance().free_memory_func(data); | 847 internal::WlanApi::GetInstance().free_memory_func(data); |
| 875 | 848 |
| 876 // Just the the options from the first succesful | 849 // Just the the options from the first succesful |
| 877 // interface. | 850 // interface. |
| 878 return options; | 851 return options; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 917 | 890 |
| 918 // Google Test pretty-printer. | 891 // Google Test pretty-printer. |
| 919 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { | 892 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { |
| 920 ASSERT_TRUE(data.hostname); | 893 ASSERT_TRUE(data.hostname); |
| 921 *os << " hostname: " << testing::PrintToString(data.hostname) | 894 *os << " hostname: " << testing::PrintToString(data.hostname) |
| 922 << "; is_unique: " << testing::PrintToString(data.is_unique); | 895 << "; is_unique: " << testing::PrintToString(data.is_unique); |
| 923 } | 896 } |
| 924 | 897 |
| 925 const NonUniqueNameTestData kNonUniqueNameTestData[] = { | 898 const NonUniqueNameTestData kNonUniqueNameTestData[] = { |
| 926 // Domains under ICANN-assigned domains. | 899 // Domains under ICANN-assigned domains. |
| 927 { true, "google.com" }, | 900 {true, "google.com"}, |
| 928 { true, "google.co.uk" }, | 901 {true, "google.co.uk"}, |
| 929 // Domains under private registries. | 902 // Domains under private registries. |
| 930 { true, "appspot.com" }, | 903 {true, "appspot.com"}, |
| 931 { true, "test.appspot.com" }, | 904 {true, "test.appspot.com"}, |
| 932 // Unreserved IPv4 addresses (in various forms). | 905 // Unreserved IPv4 addresses (in various forms). |
| 933 { true, "8.8.8.8" }, | 906 {true, "8.8.8.8"}, |
| 934 { true, "99.64.0.0" }, | 907 {true, "99.64.0.0"}, |
| 935 { true, "212.15.0.0" }, | 908 {true, "212.15.0.0"}, |
| 936 { true, "212.15" }, | 909 {true, "212.15"}, |
| 937 { true, "212.15.0" }, | 910 {true, "212.15.0"}, |
| 938 { true, "3557752832" }, | 911 {true, "3557752832"}, |
| 939 // Reserved IPv4 addresses (in various forms). | 912 // Reserved IPv4 addresses (in various forms). |
| 940 { false, "192.168.0.0" }, | 913 {false, "192.168.0.0"}, |
| 941 { false, "192.168.0.6" }, | 914 {false, "192.168.0.6"}, |
| 942 { false, "10.0.0.5" }, | 915 {false, "10.0.0.5"}, |
| 943 { false, "10.0" }, | 916 {false, "10.0"}, |
| 944 { false, "10.0.0" }, | 917 {false, "10.0.0"}, |
| 945 { false, "3232235526" }, | 918 {false, "3232235526"}, |
| 946 // Unreserved IPv6 addresses. | 919 // Unreserved IPv6 addresses. |
| 947 { true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, | 920 {true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210"}, |
| 948 { true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210" }, | 921 {true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210"}, |
| 949 // Reserved IPv6 addresses. | 922 // Reserved IPv6 addresses. |
| 950 { false, "::192.9.5.5" }, | 923 {false, "::192.9.5.5"}, |
| 951 { false, "FEED::BEEF" }, | 924 {false, "FEED::BEEF"}, |
| 952 { false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, | 925 {false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210"}, |
| 953 // 'internal'/non-IANA assigned domains. | 926 // 'internal'/non-IANA assigned domains. |
| 954 { false, "intranet" }, | 927 {false, "intranet"}, |
| 955 { false, "intranet." }, | 928 {false, "intranet."}, |
| 956 { false, "intranet.example" }, | 929 {false, "intranet.example"}, |
| 957 { false, "host.intranet.example" }, | 930 {false, "host.intranet.example"}, |
| 958 // gTLDs under discussion, but not yet assigned. | 931 // gTLDs under discussion, but not yet assigned. |
| 959 { false, "intranet.corp" }, | 932 {false, "intranet.corp"}, |
| 960 { false, "example.tech" }, | 933 {false, "example.tech"}, |
| 961 { false, "intranet.internal" }, | 934 {false, "intranet.internal"}, |
| 962 // Invalid host names are treated as unique - but expected to be | 935 // Invalid host names are treated as unique - but expected to be |
| 963 // filtered out before then. | 936 // filtered out before then. |
| 964 { true, "junk)(£)$*!@~#" }, | 937 {true, "junk)(£)$*!@~#"}, |
| 965 { true, "w$w.example.com" }, | 938 {true, "w$w.example.com"}, |
| 966 { true, "nocolonsallowed:example" }, | 939 {true, "nocolonsallowed:example"}, |
| 967 { true, "[::4.5.6.9]" }, | 940 {true, "[::4.5.6.9]"}, |
| 968 }; | 941 }; |
| 969 | 942 |
| 970 class NetUtilNonUniqueNameTest | 943 class NetUtilNonUniqueNameTest |
| 971 : public testing::TestWithParam<NonUniqueNameTestData> { | 944 : public testing::TestWithParam<NonUniqueNameTestData> { |
| 972 public: | 945 public: |
| 973 virtual ~NetUtilNonUniqueNameTest() {} | 946 virtual ~NetUtilNonUniqueNameTest() {} |
| 974 | 947 |
| 975 protected: | 948 protected: |
| 976 bool IsUnique(const std::string& hostname) { | 949 bool IsUnique(const std::string& hostname) { |
| 977 return !IsHostnameNonUnique(hostname); | 950 return !IsHostnameNonUnique(hostname); |
| 978 } | 951 } |
| 979 }; | 952 }; |
| 980 | 953 |
| 981 // Test that internal/non-unique names are properly identified as such, but | 954 // Test that internal/non-unique names are properly identified as such, but |
| 982 // that IP addresses and hosts beneath registry-controlled domains are flagged | 955 // that IP addresses and hosts beneath registry-controlled domains are flagged |
| 983 // as unique names. | 956 // as unique names. |
| 984 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { | 957 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { |
| 985 const NonUniqueNameTestData& test_data = GetParam(); | 958 const NonUniqueNameTestData& test_data = GetParam(); |
| 986 | 959 |
| 987 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); | 960 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); |
| 988 } | 961 } |
| 989 | 962 |
| 990 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, | 963 INSTANTIATE_TEST_CASE_P(, |
|
davidben
2014/10/10 20:24:16
No action: I think this is going to look weird no
| |
| 964 NetUtilNonUniqueNameTest, | |
| 991 testing::ValuesIn(kNonUniqueNameTestData)); | 965 testing::ValuesIn(kNonUniqueNameTestData)); |
| 992 | 966 |
| 993 } // namespace net | 967 } // namespace net |
| OLD | NEW |