OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 const char* content_disp_header; | 357 const char* content_disp_header; |
358 const char* referrer_charset; | 358 const char* referrer_charset; |
359 const wchar_t* default_filename; | 359 const wchar_t* default_filename; |
360 const wchar_t* expected_filename; | 360 const wchar_t* expected_filename; |
361 }; | 361 }; |
362 | 362 |
363 struct UrlTestData { | 363 struct UrlTestData { |
364 const char* description; | 364 const char* description; |
365 const char* input; | 365 const char* input; |
366 const std::wstring languages; | 366 const std::wstring languages; |
367 bool omit; | 367 net::FormatUrlTypes format_types; |
368 UnescapeRule::Type escape_rules; | 368 UnescapeRule::Type escape_rules; |
369 const std::wstring output; | 369 const std::wstring output; |
370 size_t prefix_len; | 370 size_t prefix_len; |
371 }; | 371 }; |
372 | 372 |
373 // Returns an addrinfo for the given 32-bit address (IPv4.) | 373 // Returns an addrinfo for the given 32-bit address (IPv4.) |
374 // The result lives in static storage, so don't delete it. | 374 // The result lives in static storage, so don't delete it. |
375 // |bytes| should be an array of length 4. | 375 // |bytes| should be an array of length 4. |
376 const struct addrinfo* GetIPv4Address(const uint8* bytes) { | 376 const struct addrinfo* GetIPv4Address(const uint8* bytes) { |
377 static struct addrinfo static_ai; | 377 static struct addrinfo static_ai; |
(...skipping 882 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1260 | 1260 |
1261 TEST(NetUtilTest, GetHostName) { | 1261 TEST(NetUtilTest, GetHostName) { |
1262 // We can't check the result of GetHostName() directly, since the result | 1262 // We can't check the result of GetHostName() directly, since the result |
1263 // will differ across machines. Our goal here is to simply exercise the | 1263 // will differ across machines. Our goal here is to simply exercise the |
1264 // code path, and check that things "look about right". | 1264 // code path, and check that things "look about right". |
1265 std::string hostname = net::GetHostName(); | 1265 std::string hostname = net::GetHostName(); |
1266 EXPECT_FALSE(hostname.empty()); | 1266 EXPECT_FALSE(hostname.empty()); |
1267 } | 1267 } |
1268 | 1268 |
1269 TEST(NetUtilTest, FormatUrl) { | 1269 TEST(NetUtilTest, FormatUrl) { |
| 1270 net::FormatUrlTypes default_format_type = net::kFormatUrlOmitUsernamePassword; |
1270 const UrlTestData tests[] = { | 1271 const UrlTestData tests[] = { |
1271 {"Empty URL", "", L"", true, UnescapeRule::NORMAL, L"", 0}, | 1272 {"Empty URL", "", L"", default_format_type, UnescapeRule::NORMAL, L"", 0}, |
1272 | 1273 |
1273 {"Simple URL", | 1274 {"Simple URL", |
1274 "http://www.google.com/", L"", true, UnescapeRule::NORMAL, | 1275 "http://www.google.com/", L"", default_format_type, UnescapeRule::NORMAL, |
1275 L"http://www.google.com/", 7}, | 1276 L"http://www.google.com/", 7}, |
1276 | 1277 |
1277 {"With a port number and a reference", | 1278 {"With a port number and a reference", |
1278 "http://www.google.com:8080/#\xE3\x82\xB0", L"", true, | 1279 "http://www.google.com:8080/#\xE3\x82\xB0", L"", default_format_type, |
1279 UnescapeRule::NORMAL, | 1280 UnescapeRule::NORMAL, |
1280 L"http://www.google.com:8080/#\x30B0", 7}, | 1281 L"http://www.google.com:8080/#\x30B0", 7}, |
1281 | 1282 |
1282 // -------- IDN tests -------- | 1283 // -------- IDN tests -------- |
1283 {"Japanese IDN with ja", | 1284 {"Japanese IDN with ja", |
1284 "http://xn--l8jvb1ey91xtjb.jp", L"ja", true, UnescapeRule::NORMAL, | 1285 "http://xn--l8jvb1ey91xtjb.jp", L"ja", default_format_type, |
1285 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, | 1286 UnescapeRule::NORMAL, L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, |
1286 | 1287 |
1287 {"Japanese IDN with en", | 1288 {"Japanese IDN with en", |
1288 "http://xn--l8jvb1ey91xtjb.jp", L"en", true, UnescapeRule::NORMAL, | 1289 "http://xn--l8jvb1ey91xtjb.jp", L"en", default_format_type, |
1289 L"http://xn--l8jvb1ey91xtjb.jp/", 7}, | 1290 UnescapeRule::NORMAL, L"http://xn--l8jvb1ey91xtjb.jp/", 7}, |
1290 | 1291 |
1291 {"Japanese IDN without any languages", | 1292 {"Japanese IDN without any languages", |
1292 "http://xn--l8jvb1ey91xtjb.jp", L"", true, UnescapeRule::NORMAL, | 1293 "http://xn--l8jvb1ey91xtjb.jp", L"", default_format_type, |
| 1294 UnescapeRule::NORMAL, |
1293 // Single script is safe for empty languages. | 1295 // Single script is safe for empty languages. |
1294 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, | 1296 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, |
1295 | 1297 |
1296 {"mailto: with Japanese IDN", | 1298 {"mailto: with Japanese IDN", |
1297 "mailto:foo@xn--l8jvb1ey91xtjb.jp", L"ja", true, UnescapeRule::NORMAL, | 1299 "mailto:foo@xn--l8jvb1ey91xtjb.jp", L"ja", default_format_type, |
| 1300 UnescapeRule::NORMAL, |
1298 // GURL doesn't assume an email address's domain part as a host name. | 1301 // GURL doesn't assume an email address's domain part as a host name. |
1299 L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7}, | 1302 L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7}, |
1300 | 1303 |
1301 {"file: with Japanese IDN", | 1304 {"file: with Japanese IDN", |
1302 "file://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, | 1305 "file://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", default_format_type, |
1303 UnescapeRule::NORMAL, | 1306 UnescapeRule::NORMAL, |
1304 L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7}, | 1307 L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7}, |
1305 | 1308 |
1306 {"ftp: with Japanese IDN", | 1309 {"ftp: with Japanese IDN", |
1307 "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, | 1310 "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", default_format_type, |
1308 UnescapeRule::NORMAL, | 1311 UnescapeRule::NORMAL, |
1309 L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6}, | 1312 L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6}, |
1310 | 1313 |
1311 // -------- omit_username_password flag tests -------- | 1314 // -------- omit_username_password flag tests -------- |
1312 {"With username and password, omit_username_password=false", | 1315 {"With username and password, omit_username_password=false", |
1313 "http://user:passwd@example.com/foo", L"", false, UnescapeRule::NORMAL, | 1316 "http://user:passwd@example.com/foo", L"", |
| 1317 net::kFormatUrlOmitNothing, UnescapeRule::NORMAL, |
1314 L"http://user:passwd@example.com/foo", 19}, | 1318 L"http://user:passwd@example.com/foo", 19}, |
1315 | 1319 |
1316 {"With username and password, omit_username_password=true", | 1320 {"With username and password, omit_username_password=true", |
1317 "http://user:passwd@example.com/foo", L"", true, UnescapeRule::NORMAL, | 1321 "http://user:passwd@example.com/foo", L"", default_format_type, |
1318 L"http://example.com/foo", 7}, | 1322 UnescapeRule::NORMAL, L"http://example.com/foo", 7}, |
1319 | 1323 |
1320 {"With username and no password", | 1324 {"With username and no password", |
1321 "http://user@example.com/foo", L"", true, UnescapeRule::NORMAL, | 1325 "http://user@example.com/foo", L"", default_format_type, |
1322 L"http://example.com/foo", 7}, | 1326 UnescapeRule::NORMAL, L"http://example.com/foo", 7}, |
1323 | 1327 |
1324 {"Just '@' without username and password", | 1328 {"Just '@' without username and password", |
1325 "http://@example.com/foo", L"", true, UnescapeRule::NORMAL, | 1329 "http://@example.com/foo", L"", default_format_type, UnescapeRule::NORMAL, |
1326 L"http://example.com/foo", 7}, | 1330 L"http://example.com/foo", 7}, |
1327 | 1331 |
1328 // GURL doesn't think local-part of an email address is username for URL. | 1332 // GURL doesn't think local-part of an email address is username for URL. |
1329 {"mailto:, omit_username_password=true", | 1333 {"mailto:, omit_username_password=true", |
1330 "mailto:foo@example.com", L"", true, UnescapeRule::NORMAL, | 1334 "mailto:foo@example.com", L"", default_format_type, UnescapeRule::NORMAL, |
1331 L"mailto:foo@example.com", 7}, | 1335 L"mailto:foo@example.com", 7}, |
1332 | 1336 |
1333 // -------- unescape flag tests -------- | 1337 // -------- unescape flag tests -------- |
1334 {"Do not unescape", | 1338 {"Do not unescape", |
1335 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" | 1339 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" |
1336 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" | 1340 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
1337 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, | 1341 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", default_format_type, |
1338 UnescapeRule::NONE, | 1342 UnescapeRule::NONE, |
1339 // GURL parses %-encoded hostnames into Punycode. | 1343 // GURL parses %-encoded hostnames into Punycode. |
1340 L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" | 1344 L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
1341 L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", 7}, | 1345 L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", 7}, |
1342 | 1346 |
1343 {"Unescape normally", | 1347 {"Unescape normally", |
1344 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" | 1348 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" |
1345 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" | 1349 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
1346 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, | 1350 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", default_format_type, |
1347 UnescapeRule::NORMAL, | 1351 UnescapeRule::NORMAL, |
1348 L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB" | 1352 L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB" |
1349 L"?q=\x30B0\x30FC\x30B0\x30EB", 7}, | 1353 L"?q=\x30B0\x30FC\x30B0\x30EB", 7}, |
1350 | 1354 |
1351 {"Unescape normally including unescape spaces", | 1355 {"Unescape normally including unescape spaces", |
1352 "http://www.google.com/search?q=Hello%20World", L"en", true, | 1356 "http://www.google.com/search?q=Hello%20World", L"en", default_format_type, |
1353 UnescapeRule::SPACES, | 1357 UnescapeRule::SPACES, L"http://www.google.com/search?q=Hello World", 7}, |
1354 L"http://www.google.com/search?q=Hello World", 7}, | |
1355 | 1358 |
1356 /* | 1359 /* |
1357 {"unescape=true with some special characters", | 1360 {"unescape=true with some special characters", |
1358 "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", L"", false, true, | 1361 "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", L"", |
| 1362 net::kFormatUrlOmitNothing, UnescapeRule::NORMAL, |
1359 L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25}, | 1363 L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25}, |
1360 */ | 1364 */ |
1361 // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". | 1365 // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". |
1362 | 1366 |
1363 // -------- view-source: -------- | 1367 // -------- view-source: -------- |
1364 {"view-source", | 1368 {"view-source", |
1365 "view-source:http://xn--qcka1pmc.jp/", L"ja", true, UnescapeRule::NORMAL, | 1369 "view-source:http://xn--qcka1pmc.jp/", L"ja", default_format_type, |
1366 L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", 12 + 7}, | 1370 UnescapeRule::NORMAL, L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", |
| 1371 12 + 7}, |
1367 | 1372 |
1368 {"view-source of view-source", | 1373 {"view-source of view-source", |
1369 "view-source:view-source:http://xn--qcka1pmc.jp/", L"ja", true, | 1374 "view-source:view-source:http://xn--qcka1pmc.jp/", L"ja", |
1370 UnescapeRule::NORMAL, | 1375 default_format_type, UnescapeRule::NORMAL, |
1371 L"view-source:view-source:http://xn--qcka1pmc.jp/", 12}, | 1376 L"view-source:view-source:http://xn--qcka1pmc.jp/", 12}, |
| 1377 |
| 1378 // -------- omit http: -------- |
| 1379 {"omit http with user name", |
| 1380 "http://user@example.com/foo", L"", net::kFormatUrlOmitAll, |
| 1381 UnescapeRule::NORMAL, L"example.com/foo", 0}, |
| 1382 |
| 1383 {"omit http", |
| 1384 "http://www.google.com/", L"en", net::kFormatUrlOmitHTTP, |
| 1385 UnescapeRule::NORMAL, L"www.google.com/", |
| 1386 0}, |
| 1387 |
| 1388 {"omit http with https", |
| 1389 "https://www.google.com/", L"en", net::kFormatUrlOmitHTTP, |
| 1390 UnescapeRule::NORMAL, L"https://www.google.com/", |
| 1391 8}, |
1372 }; | 1392 }; |
1373 | 1393 |
1374 for (size_t i = 0; i < arraysize(tests); ++i) { | 1394 for (size_t i = 0; i < arraysize(tests); ++i) { |
1375 size_t prefix_len; | 1395 size_t prefix_len; |
1376 std::wstring formatted = net::FormatUrl( | 1396 std::wstring formatted = net::FormatUrl( |
1377 GURL(tests[i].input), tests[i].languages, tests[i].omit, | 1397 GURL(tests[i].input), tests[i].languages, tests[i].format_types, |
1378 tests[i].escape_rules, NULL, &prefix_len, NULL); | 1398 tests[i].escape_rules, NULL, &prefix_len, NULL); |
1379 EXPECT_EQ(tests[i].output, formatted) << tests[i].description; | 1399 EXPECT_EQ(tests[i].output, formatted) << tests[i].description; |
1380 EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description; | 1400 EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description; |
1381 } | 1401 } |
1382 } | 1402 } |
1383 | 1403 |
1384 TEST(NetUtilTest, FormatUrlParsed) { | 1404 TEST(NetUtilTest, FormatUrlParsed) { |
1385 // No unescape case. | 1405 // No unescape case. |
1386 url_parse::Parsed parsed; | 1406 url_parse::Parsed parsed; |
1387 std::wstring formatted = net::FormatUrl( | 1407 std::wstring formatted = net::FormatUrl( |
1388 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" | 1408 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
1389 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), | 1409 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
1390 L"ja", false, UnescapeRule::NONE, &parsed, NULL, NULL); | 1410 L"ja", net::kFormatUrlOmitNothing, UnescapeRule::NONE, &parsed, NULL, |
| 1411 NULL); |
1391 EXPECT_EQ(L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080" | 1412 EXPECT_EQ(L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080" |
1392 L"/%E3%82%B0/?q=%E3%82%B0#\x30B0", formatted); | 1413 L"/%E3%82%B0/?q=%E3%82%B0#\x30B0", formatted); |
1393 EXPECT_EQ(L"%E3%82%B0", | 1414 EXPECT_EQ(L"%E3%82%B0", |
1394 formatted.substr(parsed.username.begin, parsed.username.len)); | 1415 formatted.substr(parsed.username.begin, parsed.username.len)); |
1395 EXPECT_EQ(L"%E3%83%BC", | 1416 EXPECT_EQ(L"%E3%83%BC", |
1396 formatted.substr(parsed.password.begin, parsed.password.len)); | 1417 formatted.substr(parsed.password.begin, parsed.password.len)); |
1397 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", | 1418 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", |
1398 formatted.substr(parsed.host.begin, parsed.host.len)); | 1419 formatted.substr(parsed.host.begin, parsed.host.len)); |
1399 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); | 1420 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); |
1400 EXPECT_EQ(L"/%E3%82%B0/", | 1421 EXPECT_EQ(L"/%E3%82%B0/", |
1401 formatted.substr(parsed.path.begin, parsed.path.len)); | 1422 formatted.substr(parsed.path.begin, parsed.path.len)); |
1402 EXPECT_EQ(L"q=%E3%82%B0", | 1423 EXPECT_EQ(L"q=%E3%82%B0", |
1403 formatted.substr(parsed.query.begin, parsed.query.len)); | 1424 formatted.substr(parsed.query.begin, parsed.query.len)); |
1404 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1425 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); |
1405 | 1426 |
1406 // Unescape case. | 1427 // Unescape case. |
1407 formatted = net::FormatUrl( | 1428 formatted = net::FormatUrl( |
1408 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" | 1429 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
1409 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), | 1430 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
1410 L"ja", false, UnescapeRule::NORMAL, &parsed, NULL, NULL); | 1431 L"ja", net::kFormatUrlOmitNothing, UnescapeRule::NORMAL, &parsed, NULL, |
| 1432 NULL); |
1411 EXPECT_EQ(L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080" | 1433 EXPECT_EQ(L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080" |
1412 L"/\x30B0/?q=\x30B0#\x30B0", formatted); | 1434 L"/\x30B0/?q=\x30B0#\x30B0", formatted); |
1413 EXPECT_EQ(L"\x30B0", | 1435 EXPECT_EQ(L"\x30B0", |
1414 formatted.substr(parsed.username.begin, parsed.username.len)); | 1436 formatted.substr(parsed.username.begin, parsed.username.len)); |
1415 EXPECT_EQ(L"\x30FC", | 1437 EXPECT_EQ(L"\x30FC", |
1416 formatted.substr(parsed.password.begin, parsed.password.len)); | 1438 formatted.substr(parsed.password.begin, parsed.password.len)); |
1417 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", | 1439 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", |
1418 formatted.substr(parsed.host.begin, parsed.host.len)); | 1440 formatted.substr(parsed.host.begin, parsed.host.len)); |
1419 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); | 1441 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); |
1420 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); | 1442 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); |
1421 EXPECT_EQ(L"q=\x30B0", | 1443 EXPECT_EQ(L"q=\x30B0", |
1422 formatted.substr(parsed.query.begin, parsed.query.len)); | 1444 formatted.substr(parsed.query.begin, parsed.query.len)); |
1423 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1445 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); |
1424 | 1446 |
1425 // Omit_username_password + unescape case. | 1447 // Omit_username_password + unescape case. |
1426 formatted = net::FormatUrl( | 1448 formatted = net::FormatUrl( |
1427 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" | 1449 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
1428 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), | 1450 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
1429 L"ja", true, UnescapeRule::NORMAL, &parsed, NULL, NULL); | 1451 L"ja", net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, |
| 1452 &parsed, NULL, NULL); |
1430 EXPECT_EQ(L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080" | 1453 EXPECT_EQ(L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080" |
1431 L"/\x30B0/?q=\x30B0#\x30B0", formatted); | 1454 L"/\x30B0/?q=\x30B0#\x30B0", formatted); |
1432 EXPECT_FALSE(parsed.username.is_valid()); | 1455 EXPECT_FALSE(parsed.username.is_valid()); |
1433 EXPECT_FALSE(parsed.password.is_valid()); | 1456 EXPECT_FALSE(parsed.password.is_valid()); |
1434 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", | 1457 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", |
1435 formatted.substr(parsed.host.begin, parsed.host.len)); | 1458 formatted.substr(parsed.host.begin, parsed.host.len)); |
1436 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); | 1459 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); |
1437 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); | 1460 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); |
1438 EXPECT_EQ(L"q=\x30B0", | 1461 EXPECT_EQ(L"q=\x30B0", |
1439 formatted.substr(parsed.query.begin, parsed.query.len)); | 1462 formatted.substr(parsed.query.begin, parsed.query.len)); |
1440 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1463 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); |
1441 | 1464 |
1442 // View-source case. | 1465 // View-source case. |
1443 formatted = net::FormatUrl( | 1466 formatted = net::FormatUrl( |
1444 GURL("view-source:http://user:passwd@host:81/path?query#ref"), | 1467 GURL("view-source:http://user:passwd@host:81/path?query#ref"), |
1445 L"", true, UnescapeRule::NORMAL, &parsed, NULL, NULL); | 1468 L"", net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, &parsed, |
| 1469 NULL, NULL); |
1446 EXPECT_EQ(L"view-source:http://host:81/path?query#ref", formatted); | 1470 EXPECT_EQ(L"view-source:http://host:81/path?query#ref", formatted); |
1447 EXPECT_EQ(L"view-source:http", | 1471 EXPECT_EQ(L"view-source:http", |
1448 formatted.substr(parsed.scheme.begin, parsed.scheme.len)); | 1472 formatted.substr(parsed.scheme.begin, parsed.scheme.len)); |
1449 EXPECT_FALSE(parsed.username.is_valid()); | 1473 EXPECT_FALSE(parsed.username.is_valid()); |
1450 EXPECT_FALSE(parsed.password.is_valid()); | 1474 EXPECT_FALSE(parsed.password.is_valid()); |
1451 EXPECT_EQ(L"host", formatted.substr(parsed.host.begin, parsed.host.len)); | 1475 EXPECT_EQ(L"host", formatted.substr(parsed.host.begin, parsed.host.len)); |
1452 EXPECT_EQ(L"81", formatted.substr(parsed.port.begin, parsed.port.len)); | 1476 EXPECT_EQ(L"81", formatted.substr(parsed.port.begin, parsed.port.len)); |
1453 EXPECT_EQ(L"/path", formatted.substr(parsed.path.begin, parsed.path.len)); | 1477 EXPECT_EQ(L"/path", formatted.substr(parsed.path.begin, parsed.path.len)); |
1454 EXPECT_EQ(L"query", formatted.substr(parsed.query.begin, parsed.query.len)); | 1478 EXPECT_EQ(L"query", formatted.substr(parsed.query.begin, parsed.query.len)); |
1455 EXPECT_EQ(L"ref", formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1479 EXPECT_EQ(L"ref", formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1480 |
| 1481 // omit http case. |
| 1482 formatted = net::FormatUrl( |
| 1483 GURL("http://host:8000/a?b=c#d"), |
| 1484 L"", net::kFormatUrlOmitHTTP, UnescapeRule::NORMAL, &parsed, NULL, NULL); |
| 1485 EXPECT_EQ(L"host:8000/a?b=c#d", formatted); |
| 1486 EXPECT_FALSE(parsed.scheme.is_valid()); |
| 1487 EXPECT_FALSE(parsed.username.is_valid()); |
| 1488 EXPECT_FALSE(parsed.password.is_valid()); |
| 1489 EXPECT_EQ(L"host", formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1490 EXPECT_EQ(L"8000", formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1491 EXPECT_EQ(L"/a", formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1492 EXPECT_EQ(L"b=c", formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1493 EXPECT_EQ(L"d", formatted.substr(parsed.ref.begin, parsed.ref.len)); |
1456 } | 1494 } |
1457 | 1495 |
1458 TEST(NetUtilTest, FormatUrlAdjustOffset) { | 1496 TEST(NetUtilTest, FormatUrlAdjustOffset) { |
1459 const AdjustOffsetCase basic_cases[] = { | 1497 const AdjustOffsetCase basic_cases[] = { |
1460 {0, 0}, | 1498 {0, 0}, |
1461 {3, 3}, | 1499 {3, 3}, |
1462 {5, 5}, | 1500 {5, 5}, |
1463 {6, 6}, | 1501 {6, 6}, |
1464 {13, 13}, | 1502 {13, 13}, |
1465 {21, 21}, | 1503 {21, 21}, |
1466 {22, 22}, | 1504 {22, 22}, |
1467 {23, 23}, | 1505 {23, 23}, |
1468 {25, 25}, | 1506 {25, 25}, |
1469 {26, std::wstring::npos}, | 1507 {26, std::wstring::npos}, |
1470 {500000, std::wstring::npos}, | 1508 {500000, std::wstring::npos}, |
1471 {std::wstring::npos, std::wstring::npos}, | 1509 {std::wstring::npos, std::wstring::npos}, |
1472 }; | 1510 }; |
1473 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(basic_cases); ++i) { | 1511 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(basic_cases); ++i) { |
1474 size_t offset = basic_cases[i].input_offset; | 1512 size_t offset = basic_cases[i].input_offset; |
1475 net::FormatUrl(GURL("http://www.google.com/foo/"), L"en", true, | 1513 net::FormatUrl(GURL("http://www.google.com/foo/"), L"en", |
1476 UnescapeRule::NORMAL, NULL, NULL, &offset); | 1514 net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, |
| 1515 NULL, NULL, &offset); |
1477 EXPECT_EQ(basic_cases[i].output_offset, offset); | 1516 EXPECT_EQ(basic_cases[i].output_offset, offset); |
1478 } | 1517 } |
1479 | 1518 |
1480 const struct { | 1519 const struct { |
1481 const char* input_url; | 1520 const char* input_url; |
1482 size_t input_offset; | 1521 size_t input_offset; |
1483 size_t output_offset; | 1522 size_t output_offset; |
1484 } omit_auth_cases[] = { | 1523 } omit_auth_cases[] = { |
1485 {"http://foo:bar@www.google.com/", 6, 6}, | 1524 {"http://foo:bar@www.google.com/", 6, 6}, |
1486 {"http://foo:bar@www.google.com/", 7, 7}, | 1525 {"http://foo:bar@www.google.com/", 7, 7}, |
1487 {"http://foo:bar@www.google.com/", 8, std::wstring::npos}, | 1526 {"http://foo:bar@www.google.com/", 8, std::wstring::npos}, |
1488 {"http://foo:bar@www.google.com/", 10, std::wstring::npos}, | 1527 {"http://foo:bar@www.google.com/", 10, std::wstring::npos}, |
1489 {"http://foo:bar@www.google.com/", 11, std::wstring::npos}, | 1528 {"http://foo:bar@www.google.com/", 11, std::wstring::npos}, |
1490 {"http://foo:bar@www.google.com/", 14, std::wstring::npos}, | 1529 {"http://foo:bar@www.google.com/", 14, std::wstring::npos}, |
1491 {"http://foo:bar@www.google.com/", 15, 7}, | 1530 {"http://foo:bar@www.google.com/", 15, 7}, |
1492 {"http://foo:bar@www.google.com/", 25, 17}, | 1531 {"http://foo:bar@www.google.com/", 25, 17}, |
1493 {"http://foo@www.google.com/", 9, std::wstring::npos}, | 1532 {"http://foo@www.google.com/", 9, std::wstring::npos}, |
1494 {"http://foo@www.google.com/", 11, 7}, | 1533 {"http://foo@www.google.com/", 11, 7}, |
1495 }; | 1534 }; |
1496 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_auth_cases); ++i) { | 1535 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_auth_cases); ++i) { |
1497 size_t offset = omit_auth_cases[i].input_offset; | 1536 size_t offset = omit_auth_cases[i].input_offset; |
1498 net::FormatUrl(GURL(omit_auth_cases[i].input_url), L"en", true, | 1537 net::FormatUrl(GURL(omit_auth_cases[i].input_url), L"en", |
1499 UnescapeRule::NORMAL, NULL, NULL, &offset); | 1538 net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, |
| 1539 NULL, NULL, &offset); |
1500 EXPECT_EQ(omit_auth_cases[i].output_offset, offset); | 1540 EXPECT_EQ(omit_auth_cases[i].output_offset, offset); |
1501 } | 1541 } |
1502 | 1542 |
1503 const AdjustOffsetCase view_source_cases[] = { | 1543 const AdjustOffsetCase view_source_cases[] = { |
1504 {0, 0}, | 1544 {0, 0}, |
1505 {3, 3}, | 1545 {3, 3}, |
1506 {11, 11}, | 1546 {11, 11}, |
1507 {12, 12}, | 1547 {12, 12}, |
1508 {13, 13}, | 1548 {13, 13}, |
1509 {19, 19}, | 1549 {19, 19}, |
1510 {20, std::wstring::npos}, | 1550 {20, std::wstring::npos}, |
1511 {23, 19}, | 1551 {23, 19}, |
1512 {26, 22}, | 1552 {26, 22}, |
1513 {std::wstring::npos, std::wstring::npos}, | 1553 {std::wstring::npos, std::wstring::npos}, |
1514 }; | 1554 }; |
1515 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(view_source_cases); ++i) { | 1555 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(view_source_cases); ++i) { |
1516 size_t offset = view_source_cases[i].input_offset; | 1556 size_t offset = view_source_cases[i].input_offset; |
1517 net::FormatUrl(GURL("view-source:http://foo@www.google.com/"), L"en", true, | 1557 net::FormatUrl(GURL("view-source:http://foo@www.google.com/"), L"en", |
1518 UnescapeRule::NORMAL, NULL, NULL, &offset); | 1558 net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, |
| 1559 NULL, NULL, &offset); |
1519 EXPECT_EQ(view_source_cases[i].output_offset, offset); | 1560 EXPECT_EQ(view_source_cases[i].output_offset, offset); |
1520 } | 1561 } |
1521 | 1562 |
1522 const AdjustOffsetCase idn_hostname_cases[] = { | 1563 const AdjustOffsetCase idn_hostname_cases[] = { |
1523 {8, std::wstring::npos}, | 1564 {8, std::wstring::npos}, |
1524 {16, std::wstring::npos}, | 1565 {16, std::wstring::npos}, |
1525 {24, std::wstring::npos}, | 1566 {24, std::wstring::npos}, |
1526 {25, 12}, | 1567 {25, 12}, |
1527 {30, 17}, | 1568 {30, 17}, |
1528 }; | 1569 }; |
1529 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_hostname_cases); ++i) { | 1570 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_hostname_cases); ++i) { |
1530 size_t offset = idn_hostname_cases[i].input_offset; | 1571 size_t offset = idn_hostname_cases[i].input_offset; |
1531 // "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/" | 1572 // "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/" |
1532 net::FormatUrl(GURL("http://xn--l8jvb1ey91xtjb.jp/foo/"), L"ja", true, | 1573 net::FormatUrl(GURL("http://xn--l8jvb1ey91xtjb.jp/foo/"), L"ja", |
1533 UnescapeRule::NORMAL, NULL, NULL, &offset); | 1574 net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, |
| 1575 NULL, NULL, &offset); |
1534 EXPECT_EQ(idn_hostname_cases[i].output_offset, offset); | 1576 EXPECT_EQ(idn_hostname_cases[i].output_offset, offset); |
1535 } | 1577 } |
1536 | 1578 |
1537 const AdjustOffsetCase unescape_cases[] = { | 1579 const AdjustOffsetCase unescape_cases[] = { |
1538 {25, 25}, | 1580 {25, 25}, |
1539 {26, std::wstring::npos}, | 1581 {26, std::wstring::npos}, |
1540 {27, std::wstring::npos}, | 1582 {27, std::wstring::npos}, |
1541 {28, 26}, | 1583 {28, 26}, |
1542 {35, std::wstring::npos}, | 1584 {35, std::wstring::npos}, |
1543 {41, 31}, | 1585 {41, 31}, |
1544 {59, 33}, | 1586 {59, 33}, |
1545 {60, std::wstring::npos}, | 1587 {60, std::wstring::npos}, |
1546 {67, std::wstring::npos}, | 1588 {67, std::wstring::npos}, |
1547 {68, std::wstring::npos}, | 1589 {68, std::wstring::npos}, |
1548 }; | 1590 }; |
1549 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(unescape_cases); ++i) { | 1591 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(unescape_cases); ++i) { |
1550 size_t offset = unescape_cases[i].input_offset; | 1592 size_t offset = unescape_cases[i].input_offset; |
1551 // "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB" | 1593 // "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB" |
1552 net::FormatUrl(GURL( | 1594 net::FormatUrl(GURL( |
1553 "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"), | 1595 "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"), |
1554 L"en", true, UnescapeRule::SPACES, NULL, NULL, &offset); | 1596 L"en", net::kFormatUrlOmitUsernamePassword, UnescapeRule::SPACES, NULL, |
| 1597 NULL, &offset); |
1555 EXPECT_EQ(unescape_cases[i].output_offset, offset); | 1598 EXPECT_EQ(unescape_cases[i].output_offset, offset); |
1556 } | 1599 } |
1557 | 1600 |
1558 const AdjustOffsetCase ref_cases[] = { | 1601 const AdjustOffsetCase ref_cases[] = { |
1559 {30, 30}, | 1602 {30, 30}, |
1560 {31, 31}, | 1603 {31, 31}, |
1561 {32, std::wstring::npos}, | 1604 {32, std::wstring::npos}, |
1562 {34, 32}, | 1605 {34, 32}, |
1563 {37, 33}, | 1606 {37, 33}, |
1564 {38, std::wstring::npos}, | 1607 {38, std::wstring::npos}, |
1565 }; | 1608 }; |
1566 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(ref_cases); ++i) { | 1609 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(ref_cases); ++i) { |
1567 size_t offset = ref_cases[i].input_offset; | 1610 size_t offset = ref_cases[i].input_offset; |
1568 // "http://www.google.com/foo.html#\x30B0\x30B0z" | 1611 // "http://www.google.com/foo.html#\x30B0\x30B0z" |
1569 net::FormatUrl(GURL( | 1612 net::FormatUrl(GURL( |
1570 "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z"), L"en", | 1613 "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z"), L"en", |
1571 true, UnescapeRule::NORMAL, NULL, NULL, &offset); | 1614 net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, NULL, NULL, |
| 1615 &offset); |
1572 EXPECT_EQ(ref_cases[i].output_offset, offset); | 1616 EXPECT_EQ(ref_cases[i].output_offset, offset); |
1573 } | 1617 } |
| 1618 |
| 1619 const AdjustOffsetCase omit_http_cases[] = { |
| 1620 {0, std::wstring::npos}, |
| 1621 {3, std::wstring::npos}, |
| 1622 {7, 0}, |
| 1623 {8, 1}, |
| 1624 }; |
| 1625 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_http_cases); ++i) { |
| 1626 size_t offset = omit_http_cases[i].input_offset; |
| 1627 net::FormatUrl(GURL("http://www.google.com"), L"en", |
| 1628 net::kFormatUrlOmitHTTP, UnescapeRule::NORMAL, NULL, NULL, &offset); |
| 1629 EXPECT_EQ(omit_http_cases[i].output_offset, offset); |
| 1630 } |
| 1631 |
| 1632 const AdjustOffsetCase omit_all_cases[] = { |
| 1633 {12, 0}, |
| 1634 {13, 1}, |
| 1635 {0, std::wstring::npos}, |
| 1636 {3, std::wstring::npos}, |
| 1637 }; |
| 1638 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_all_cases); ++i) { |
| 1639 size_t offset = omit_all_cases[i].input_offset; |
| 1640 net::FormatUrl(GURL("http://user@foo.com/"), L"en", net::kFormatUrlOmitAll, |
| 1641 UnescapeRule::NORMAL, NULL, NULL, &offset); |
| 1642 EXPECT_EQ(omit_all_cases[i].output_offset, offset); |
| 1643 } |
1574 } | 1644 } |
1575 | 1645 |
1576 TEST(NetUtilTest, SimplifyUrlForRequest) { | 1646 TEST(NetUtilTest, SimplifyUrlForRequest) { |
1577 struct { | 1647 struct { |
1578 const char* input_url; | 1648 const char* input_url; |
1579 const char* expected_simplified_url; | 1649 const char* expected_simplified_url; |
1580 } tests[] = { | 1650 } tests[] = { |
1581 { | 1651 { |
1582 // Reference section should be stripped. | 1652 // Reference section should be stripped. |
1583 "http://www.google.com:78/foobar?query=1#hash", | 1653 "http://www.google.com:78/foobar?query=1#hash", |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1624 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(invalid); ++i) { | 1694 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(invalid); ++i) { |
1625 net::SetExplicitlyAllowedPorts(invalid[i]); | 1695 net::SetExplicitlyAllowedPorts(invalid[i]); |
1626 EXPECT_EQ(0, static_cast<int>(net::explicitly_allowed_ports.size())); | 1696 EXPECT_EQ(0, static_cast<int>(net::explicitly_allowed_ports.size())); |
1627 } | 1697 } |
1628 | 1698 |
1629 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { | 1699 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { |
1630 net::SetExplicitlyAllowedPorts(valid[i]); | 1700 net::SetExplicitlyAllowedPorts(valid[i]); |
1631 EXPECT_EQ(i, net::explicitly_allowed_ports.size()); | 1701 EXPECT_EQ(i, net::explicitly_allowed_ports.size()); |
1632 } | 1702 } |
1633 } | 1703 } |
OLD | NEW |