Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(526)

Side by Side Diff: net/base/net_util_unittest.cc

Issue 56053: URL's not properly unescaping when displayed (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/net_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/escape.h"
10 #include "net/base/net_util.h" 11 #include "net/base/net_util.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 #if defined(OS_WIN) 14 #if defined(OS_WIN)
14 #include <ws2tcpip.h> 15 #include <ws2tcpip.h>
15 #else 16 #else
16 #include <netdb.h> 17 #include <netdb.h>
17 #endif 18 #endif
18 19
19 namespace { 20 namespace {
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 const char* referrer_charset; 336 const char* referrer_charset;
336 const wchar_t* default_filename; 337 const wchar_t* default_filename;
337 const wchar_t* expected_filename; 338 const wchar_t* expected_filename;
338 }; 339 };
339 340
340 struct UrlTestData { 341 struct UrlTestData {
341 const char* description; 342 const char* description;
342 const char* input; 343 const char* input;
343 const std::wstring languages; 344 const std::wstring languages;
344 bool omit; 345 bool omit;
345 bool unescape; 346 UnescapeRule::Type escape_rules;
346 const std::wstring output; 347 const std::wstring output;
347 size_t prefix_len; 348 size_t prefix_len;
348 }; 349 };
349 350
350 // Returns an addrinfo for the given 32-bit address (IPv4.) 351 // Returns an addrinfo for the given 32-bit address (IPv4.)
351 // The result lives in static storage, so don't delete it. 352 // The result lives in static storage, so don't delete it.
352 const struct addrinfo* GetIPv4Address(const uint8 bytes[4]) { 353 const struct addrinfo* GetIPv4Address(const uint8 bytes[4]) {
353 static struct addrinfo static_ai; 354 static struct addrinfo static_ai;
354 static struct sockaddr_in static_addr4; 355 static struct sockaddr_in static_addr4;
355 356
(...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 TEST(NetUtilTest, GetHostName) { 1015 TEST(NetUtilTest, GetHostName) {
1015 // We can't check the result of GetHostName() directly, since the result 1016 // We can't check the result of GetHostName() directly, since the result
1016 // will differ across machines. Our goal here is to simply exercise the 1017 // will differ across machines. Our goal here is to simply exercise the
1017 // code path, and check that things "look about right". 1018 // code path, and check that things "look about right".
1018 std::string hostname = net::GetHostName(); 1019 std::string hostname = net::GetHostName();
1019 EXPECT_FALSE(hostname.empty()); 1020 EXPECT_FALSE(hostname.empty());
1020 } 1021 }
1021 1022
1022 TEST(NetUtilTest, FormatUrl) { 1023 TEST(NetUtilTest, FormatUrl) {
1023 const UrlTestData tests[] = { 1024 const UrlTestData tests[] = {
1024 {"Empty URL", "", L"", true, true, L"", 0}, 1025 {"Empty URL", "", L"", true, UnescapeRule::NORMAL, L"", 0},
1025 1026
1026 {"Simple URL", 1027 {"Simple URL",
1027 "http://www.google.com/", L"", true, true, 1028 "http://www.google.com/", L"", true, UnescapeRule::NORMAL,
1028 L"http://www.google.com/", 7}, 1029 L"http://www.google.com/", 7},
1029 1030
1030 {"With a port number and a reference", 1031 {"With a port number and a reference",
1031 "http://www.google.com:8080/#\xE3\x82\xB0", L"", true, true, 1032 "http://www.google.com:8080/#\xE3\x82\xB0", L"", true,
1033 UnescapeRule::NORMAL,
1032 L"http://www.google.com:8080/#\x30B0", 7}, 1034 L"http://www.google.com:8080/#\x30B0", 7},
1033 1035
1034 // -------- IDN tests -------- 1036 // -------- IDN tests --------
1035 {"Japanese IDN with ja", 1037 {"Japanese IDN with ja",
1036 "http://xn--l8jvb1ey91xtjb.jp", L"ja", true, true, 1038 "http://xn--l8jvb1ey91xtjb.jp", L"ja", true, UnescapeRule::NORMAL,
1037 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, 1039 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7},
1038 1040
1039 {"Japanese IDN with en", 1041 {"Japanese IDN with en",
1040 "http://xn--l8jvb1ey91xtjb.jp", L"en", true, true, 1042 "http://xn--l8jvb1ey91xtjb.jp", L"en", true, UnescapeRule::NORMAL,
1041 L"http://xn--l8jvb1ey91xtjb.jp/", 7}, 1043 L"http://xn--l8jvb1ey91xtjb.jp/", 7},
1042 1044
1043 {"Japanese IDN without any languages", 1045 {"Japanese IDN without any languages",
1044 "http://xn--l8jvb1ey91xtjb.jp", L"", true, true, 1046 "http://xn--l8jvb1ey91xtjb.jp", L"", true, UnescapeRule::NORMAL,
1045 // Single script is safe for empty languages. 1047 // Single script is safe for empty languages.
1046 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, 1048 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7},
1047 1049
1048 {"mailto: with Japanese IDN", 1050 {"mailto: with Japanese IDN",
1049 "mailto:foo@xn--l8jvb1ey91xtjb.jp", L"ja", true, true, 1051 "mailto:foo@xn--l8jvb1ey91xtjb.jp", L"ja", true, UnescapeRule::NORMAL,
1050 // GURL doesn't assume an email address's domain part as a host name. 1052 // GURL doesn't assume an email address's domain part as a host name.
1051 L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7}, 1053 L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7},
1052 1054
1053 {"file: with Japanese IDN", 1055 {"file: with Japanese IDN",
1054 "file://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, true, 1056 "file://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true,
1057 UnescapeRule::NORMAL,
1055 L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7}, 1058 L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7},
1056 1059
1057 {"ftp: with Japanese IDN", 1060 {"ftp: with Japanese IDN",
1058 "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, true, 1061 "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true,
1062 UnescapeRule::NORMAL,
1059 L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6}, 1063 L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6},
1060 1064
1061 // -------- omit_username_password flag tests -------- 1065 // -------- omit_username_password flag tests --------
1062 {"With username and password, omit_username_password=false", 1066 {"With username and password, omit_username_password=false",
1063 "http://user:passwd@example.com/foo", L"", false, true, 1067 "http://user:passwd@example.com/foo", L"", false, UnescapeRule::NORMAL,
1064 L"http://user:passwd@example.com/foo", 19}, 1068 L"http://user:passwd@example.com/foo", 19},
1065 1069
1066 {"With username and password, omit_username_password=true", 1070 {"With username and password, omit_username_password=true",
1067 "http://user:passwd@example.com/foo", L"", true, true, 1071 "http://user:passwd@example.com/foo", L"", true, UnescapeRule::NORMAL,
1068 L"http://example.com/foo", 7}, 1072 L"http://example.com/foo", 7},
1069 1073
1070 {"With username and no password", 1074 {"With username and no password",
1071 "http://user@example.com/foo", L"", true, true, 1075 "http://user@example.com/foo", L"", true, UnescapeRule::NORMAL,
1072 L"http://example.com/foo", 7}, 1076 L"http://example.com/foo", 7},
1073 1077
1074 {"Just '@' without username and password", 1078 {"Just '@' without username and password",
1075 "http://@example.com/foo", L"", true, true, 1079 "http://@example.com/foo", L"", true, UnescapeRule::NORMAL,
1076 L"http://example.com/foo", 7}, 1080 L"http://example.com/foo", 7},
1077 1081
1078 // GURL doesn't think local-part of an email address is username for URL. 1082 // GURL doesn't think local-part of an email address is username for URL.
1079 {"mailto:, omit_username_password=true", 1083 {"mailto:, omit_username_password=true",
1080 "mailto:foo@example.com", L"", true, true, 1084 "mailto:foo@example.com", L"", true, UnescapeRule::NORMAL,
1081 L"mailto:foo@example.com", 7}, 1085 L"mailto:foo@example.com", 7},
1082 1086
1083 // -------- unescape flag tests -------- 1087 // -------- unescape flag tests --------
1084 {"unescape=false", 1088 {"Do not unescape",
1085 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" 1089 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/"
1086 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" 1090 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
1087 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, false, 1091 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true,
1092 UnescapeRule::NONE,
1088 // GURL parses %-encoded hostnames into Punycode. 1093 // GURL parses %-encoded hostnames into Punycode.
1089 L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" 1094 L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
1090 L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", 7}, 1095 L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", 7},
1091 1096
1092 {"unescape=true", 1097 {"Unescape normally",
1093 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" 1098 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/"
1094 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" 1099 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"
1095 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, true, 1100 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true,
1101 UnescapeRule::NORMAL,
1096 L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB" 1102 L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB"
1097 L"?q=\x30B0\x30FC\x30B0\x30EB", 7}, 1103 L"?q=\x30B0\x30FC\x30B0\x30EB", 7},
1098 1104
1105 {"Unescape normally including unescape spaces",
1106 "http://www.google.com/search?q=Hello%20World", L"en", true,
1107 UnescapeRule::SPACES,
1108 L"http://www.google.com/search?q=Hello World", 7}
1099 /* 1109 /*
1100 {"unescape=true with some special characters", 1110 {"unescape=true with some special characters",
1101 "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", L"", false, true, 1111 "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", L"", false, true,
1102 L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25}, 1112 L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25},
1103 */ 1113 */
1104 // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". 1114 // Disabled: the resultant URL becomes "...user%253A:%2540passwd...".
1105 }; 1115 };
1106 1116
1107 for (size_t i = 0; i < arraysize(tests); ++i) { 1117 for (size_t i = 0; i < arraysize(tests); ++i) {
1108 size_t prefix_len; 1118 size_t prefix_len;
1109 std::wstring formatted = net::FormatUrl( 1119 std::wstring formatted = net::FormatUrl(
1110 GURL(tests[i].input), tests[i].languages, tests[i].omit, 1120 GURL(tests[i].input), tests[i].languages, tests[i].omit,
1111 tests[i].unescape, NULL, &prefix_len); 1121 tests[i].escape_rules, NULL, &prefix_len);
1112 EXPECT_EQ(tests[i].output, formatted) << tests[i].description; 1122 EXPECT_EQ(tests[i].output, formatted) << tests[i].description;
1113 EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description; 1123 EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description;
1114 } 1124 }
1115 } 1125 }
1116 1126
1117 TEST(NetUtilTest, FormatUrlParsed) { 1127 TEST(NetUtilTest, FormatUrlParsed) {
1118 // No unescape case. 1128 // No unescape case.
1119 url_parse::Parsed parsed; 1129 url_parse::Parsed parsed;
1120 std::wstring formatted = net::FormatUrl( 1130 std::wstring formatted = net::FormatUrl(
1121 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" 1131 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/"
1122 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), 1132 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"),
1123 L"ja", false, false, &parsed, NULL); 1133 L"ja", false, UnescapeRule::NONE, &parsed, NULL);
1124 EXPECT_EQ(L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080" 1134 EXPECT_EQ(L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080"
1125 L"/%E3%82%B0/?q=%E3%82%B0#\x30B0", formatted); 1135 L"/%E3%82%B0/?q=%E3%82%B0#\x30B0", formatted);
1126 EXPECT_EQ(L"%E3%82%B0", 1136 EXPECT_EQ(L"%E3%82%B0",
1127 formatted.substr(parsed.username.begin, parsed.username.len)); 1137 formatted.substr(parsed.username.begin, parsed.username.len));
1128 EXPECT_EQ(L"%E3%83%BC", 1138 EXPECT_EQ(L"%E3%83%BC",
1129 formatted.substr(parsed.password.begin, parsed.password.len)); 1139 formatted.substr(parsed.password.begin, parsed.password.len));
1130 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", 1140 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp",
1131 formatted.substr(parsed.host.begin, parsed.host.len)); 1141 formatted.substr(parsed.host.begin, parsed.host.len));
1132 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); 1142 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len));
1133 EXPECT_EQ(L"/%E3%82%B0/", 1143 EXPECT_EQ(L"/%E3%82%B0/",
1134 formatted.substr(parsed.path.begin, parsed.path.len)); 1144 formatted.substr(parsed.path.begin, parsed.path.len));
1135 EXPECT_EQ(L"q=%E3%82%B0", 1145 EXPECT_EQ(L"q=%E3%82%B0",
1136 formatted.substr(parsed.query.begin, parsed.query.len)); 1146 formatted.substr(parsed.query.begin, parsed.query.len));
1137 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); 1147 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len));
1138 1148
1139 // Unescape case. 1149 // Unescape case.
1140 formatted = net::FormatUrl( 1150 formatted = net::FormatUrl(
1141 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" 1151 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/"
1142 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), 1152 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"),
1143 L"ja", false, true, &parsed, NULL); 1153 L"ja", false, UnescapeRule::NORMAL, &parsed, NULL);
1144 EXPECT_EQ(L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080" 1154 EXPECT_EQ(L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080"
1145 L"/\x30B0/?q=\x30B0#\x30B0", formatted); 1155 L"/\x30B0/?q=\x30B0#\x30B0", formatted);
1146 EXPECT_EQ(L"\x30B0", 1156 EXPECT_EQ(L"\x30B0",
1147 formatted.substr(parsed.username.begin, parsed.username.len)); 1157 formatted.substr(parsed.username.begin, parsed.username.len));
1148 EXPECT_EQ(L"\x30FC", 1158 EXPECT_EQ(L"\x30FC",
1149 formatted.substr(parsed.password.begin, parsed.password.len)); 1159 formatted.substr(parsed.password.begin, parsed.password.len));
1150 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", 1160 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp",
1151 formatted.substr(parsed.host.begin, parsed.host.len)); 1161 formatted.substr(parsed.host.begin, parsed.host.len));
1152 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); 1162 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len));
1153 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); 1163 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len));
1154 EXPECT_EQ(L"q=\x30B0", 1164 EXPECT_EQ(L"q=\x30B0",
1155 formatted.substr(parsed.query.begin, parsed.query.len)); 1165 formatted.substr(parsed.query.begin, parsed.query.len));
1156 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); 1166 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len));
1157 1167
1158 // Omit_username_password + unescape case. 1168 // Omit_username_password + unescape case.
1159 formatted = net::FormatUrl( 1169 formatted = net::FormatUrl(
1160 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" 1170 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/"
1161 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), 1171 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"),
1162 L"ja", true, true, &parsed, NULL); 1172 L"ja", true, UnescapeRule::NORMAL, &parsed, NULL);
1163 EXPECT_EQ(L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080" 1173 EXPECT_EQ(L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080"
1164 L"/\x30B0/?q=\x30B0#\x30B0", formatted); 1174 L"/\x30B0/?q=\x30B0#\x30B0", formatted);
1165 EXPECT_FALSE(parsed.username.is_valid()); 1175 EXPECT_FALSE(parsed.username.is_valid());
1166 EXPECT_FALSE(parsed.password.is_valid()); 1176 EXPECT_FALSE(parsed.password.is_valid());
1167 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", 1177 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp",
1168 formatted.substr(parsed.host.begin, parsed.host.len)); 1178 formatted.substr(parsed.host.begin, parsed.host.len));
1169 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); 1179 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len));
1170 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); 1180 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len));
1171 EXPECT_EQ(L"q=\x30B0", 1181 EXPECT_EQ(L"q=\x30B0",
1172 formatted.substr(parsed.query.begin, parsed.query.len)); 1182 formatted.substr(parsed.query.begin, parsed.query.len));
1173 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); 1183 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len));
1174 } 1184 }
OLDNEW
« no previous file with comments | « net/base/net_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698