OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 }; | 330 }; |
331 | 331 |
332 struct SuggestedFilenameCase { | 332 struct SuggestedFilenameCase { |
333 const char* url; | 333 const char* url; |
334 const char* content_disp_header; | 334 const char* content_disp_header; |
335 const char* referrer_charset; | 335 const char* referrer_charset; |
336 const wchar_t* default_filename; | 336 const wchar_t* default_filename; |
337 const wchar_t* expected_filename; | 337 const wchar_t* expected_filename; |
338 }; | 338 }; |
339 | 339 |
| 340 struct UrlTestData { |
| 341 const char* description; |
| 342 const char* input; |
| 343 const std::wstring languages; |
| 344 bool omit; |
| 345 bool unescape; |
| 346 const std::wstring output; |
| 347 size_t prefix_len; |
| 348 }; |
| 349 |
340 // Returns an addrinfo for the given 32-bit address (IPv4.) | 350 // Returns an addrinfo for the given 32-bit address (IPv4.) |
341 // The result lives in static storage, so don't delete it. | 351 // The result lives in static storage, so don't delete it. |
342 const struct addrinfo* GetIPv4Address(const uint8 bytes[4]) { | 352 const struct addrinfo* GetIPv4Address(const uint8 bytes[4]) { |
343 static struct addrinfo static_ai; | 353 static struct addrinfo static_ai; |
344 static struct sockaddr_in static_addr4; | 354 static struct sockaddr_in static_addr4; |
345 | 355 |
346 struct addrinfo* ai = &static_ai; | 356 struct addrinfo* ai = &static_ai; |
347 ai->ai_socktype = SOCK_STREAM; | 357 ai->ai_socktype = SOCK_STREAM; |
348 memset(ai, 0, sizeof(static_ai)); | 358 memset(ai, 0, sizeof(static_ai)); |
349 | 359 |
(...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
964 } | 974 } |
965 } | 975 } |
966 | 976 |
967 TEST(NetUtilTest, GetHostName) { | 977 TEST(NetUtilTest, GetHostName) { |
968 // We can't check the result of GetHostName() directly, since the result | 978 // We can't check the result of GetHostName() directly, since the result |
969 // will differ across machines. Our goal here is to simply exercise the | 979 // will differ across machines. Our goal here is to simply exercise the |
970 // code path, and check that things "look about right". | 980 // code path, and check that things "look about right". |
971 std::string hostname = net::GetHostName(); | 981 std::string hostname = net::GetHostName(); |
972 EXPECT_FALSE(hostname.empty()); | 982 EXPECT_FALSE(hostname.empty()); |
973 } | 983 } |
| 984 |
| 985 TEST(NetUtilTest, FormatUrl) { |
| 986 const UrlTestData tests[] = { |
| 987 {"Empty URL", "", L"", true, true, L"", 0}, |
| 988 |
| 989 {"Simple URL", |
| 990 "http://www.google.com/", L"", true, true, |
| 991 L"http://www.google.com/", 7}, |
| 992 |
| 993 {"With a port number and a reference", |
| 994 "http://www.google.com:8080/#\xE3\x82\xB0", L"", true, true, |
| 995 L"http://www.google.com:8080/#\x30B0", 7}, |
| 996 |
| 997 // -------- IDN tests -------- |
| 998 {"Japanese IDN with ja", |
| 999 "http://xn--l8jvb1ey91xtjb.jp", L"ja", true, true, |
| 1000 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, |
| 1001 |
| 1002 {"Japanese IDN with en", |
| 1003 "http://xn--l8jvb1ey91xtjb.jp", L"en", true, true, |
| 1004 L"http://xn--l8jvb1ey91xtjb.jp/", 7}, |
| 1005 |
| 1006 {"Japanese IDN without any languages", |
| 1007 "http://xn--l8jvb1ey91xtjb.jp", L"", true, true, |
| 1008 // Single script is safe for empty languages. |
| 1009 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, |
| 1010 |
| 1011 {"mailto: with Japanese IDN", |
| 1012 "mailto:foo@xn--l8jvb1ey91xtjb.jp", L"ja", true, true, |
| 1013 // GURL doesn't assume an email address's domain part as a host name. |
| 1014 L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7}, |
| 1015 |
| 1016 {"file: with Japanese IDN", |
| 1017 "file://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, true, |
| 1018 L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7}, |
| 1019 |
| 1020 {"ftp: with Japanese IDN", |
| 1021 "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, true, |
| 1022 L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6}, |
| 1023 |
| 1024 // -------- omit_username_password flag tests -------- |
| 1025 {"With username and password, omit_username_password=false", |
| 1026 "http://user:passwd@example.com/foo", L"", false, true, |
| 1027 L"http://user:passwd@example.com/foo", 19}, |
| 1028 |
| 1029 {"With username and password, omit_username_password=true", |
| 1030 "http://user:passwd@example.com/foo", L"", true, true, |
| 1031 L"http://example.com/foo", 7}, |
| 1032 |
| 1033 {"With username and no password", |
| 1034 "http://user@example.com/foo", L"", true, true, |
| 1035 L"http://example.com/foo", 7}, |
| 1036 |
| 1037 {"Just '@' without username and password", |
| 1038 "http://@example.com/foo", L"", true, true, |
| 1039 L"http://example.com/foo", 7}, |
| 1040 |
| 1041 // GURL doesn't think local-part of an email address is username for URL. |
| 1042 {"mailto:, omit_username_password=true", |
| 1043 "mailto:foo@example.com", L"", true, true, |
| 1044 L"mailto:foo@example.com", 7}, |
| 1045 |
| 1046 // -------- unescape flag tests -------- |
| 1047 {"unescape=false", |
| 1048 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" |
| 1049 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
| 1050 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, false, |
| 1051 // GURL parses %-encoded hostnames into Punycode. |
| 1052 L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
| 1053 L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", 7}, |
| 1054 |
| 1055 {"unescape=true", |
| 1056 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" |
| 1057 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
| 1058 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, true, |
| 1059 L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB" |
| 1060 L"?q=\x30B0\x30FC\x30B0\x30EB", 7}, |
| 1061 |
| 1062 /* |
| 1063 {"unescape=true with some special characters", |
| 1064 "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", L"", false, true, |
| 1065 L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25}, |
| 1066 */ |
| 1067 // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". |
| 1068 }; |
| 1069 |
| 1070 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 1071 size_t prefix_len; |
| 1072 std::wstring formatted = net::FormatUrl( |
| 1073 GURL(tests[i].input), tests[i].languages, tests[i].omit, |
| 1074 tests[i].unescape, NULL, &prefix_len); |
| 1075 EXPECT_EQ(tests[i].output, formatted) << tests[i].description; |
| 1076 EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description; |
| 1077 } |
| 1078 } |
| 1079 |
| 1080 TEST(NetUtilTest, FormatUrlParsed) { |
| 1081 // No unescape case. |
| 1082 url_parse::Parsed parsed; |
| 1083 std::wstring formatted = net::FormatUrl( |
| 1084 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
| 1085 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
| 1086 L"ja", false, false, &parsed, NULL); |
| 1087 EXPECT_EQ(L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080" |
| 1088 L"/%E3%82%B0/?q=%E3%82%B0#\x30B0", formatted); |
| 1089 EXPECT_EQ(L"%E3%82%B0", |
| 1090 formatted.substr(parsed.username.begin, parsed.username.len)); |
| 1091 EXPECT_EQ(L"%E3%83%BC", |
| 1092 formatted.substr(parsed.password.begin, parsed.password.len)); |
| 1093 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", |
| 1094 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1095 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1096 EXPECT_EQ(L"/%E3%82%B0/", |
| 1097 formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1098 EXPECT_EQ(L"q=%E3%82%B0", |
| 1099 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1100 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1101 |
| 1102 // Unescape case. |
| 1103 formatted = net::FormatUrl( |
| 1104 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
| 1105 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
| 1106 L"ja", false, true, &parsed, NULL); |
| 1107 EXPECT_EQ(L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080" |
| 1108 L"/\x30B0/?q=\x30B0#\x30B0", formatted); |
| 1109 EXPECT_EQ(L"\x30B0", |
| 1110 formatted.substr(parsed.username.begin, parsed.username.len)); |
| 1111 EXPECT_EQ(L"\x30FC", |
| 1112 formatted.substr(parsed.password.begin, parsed.password.len)); |
| 1113 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", |
| 1114 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1115 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1116 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1117 EXPECT_EQ(L"q=\x30B0", |
| 1118 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1119 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1120 |
| 1121 // Omit_username_password + unescape case. |
| 1122 formatted = net::FormatUrl( |
| 1123 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
| 1124 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
| 1125 L"ja", true, true, &parsed, NULL); |
| 1126 EXPECT_EQ(L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080" |
| 1127 L"/\x30B0/?q=\x30B0#\x30B0", formatted); |
| 1128 EXPECT_FALSE(parsed.username.is_valid()); |
| 1129 EXPECT_FALSE(parsed.password.is_valid()); |
| 1130 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", |
| 1131 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1132 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1133 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1134 EXPECT_EQ(L"q=\x30B0", |
| 1135 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1136 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1137 } |
OLD | NEW |