OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 struct CompliantHostCase { | 379 struct CompliantHostCase { |
380 const char* host; | 380 const char* host; |
381 const char* desired_tld; | 381 const char* desired_tld; |
382 bool expected_output; | 382 bool expected_output; |
383 }; | 383 }; |
384 | 384 |
385 struct SuggestedFilenameCase { | 385 struct SuggestedFilenameCase { |
386 const char* url; | 386 const char* url; |
387 const char* content_disp_header; | 387 const char* content_disp_header; |
388 const char* referrer_charset; | 388 const char* referrer_charset; |
389 const char* suggested_filename; | |
389 const wchar_t* default_filename; | 390 const wchar_t* default_filename; |
390 const wchar_t* expected_filename; | 391 const wchar_t* expected_filename; |
391 }; | 392 }; |
392 | 393 |
393 struct UrlTestData { | 394 struct UrlTestData { |
394 const char* description; | 395 const char* description; |
395 const char* input; | 396 const char* input; |
396 const char* languages; | 397 const char* languages; |
397 FormatUrlTypes format_types; | 398 FormatUrlTypes format_types; |
398 UnescapeRule::Type escape_rules; | 399 UnescapeRule::Type escape_rules; |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1041 EXPECT_EQ(string16(), StripWWW(ASCIIToUTF16("www."))); | 1042 EXPECT_EQ(string16(), StripWWW(ASCIIToUTF16("www."))); |
1042 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("www.blah"))); | 1043 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("www.blah"))); |
1043 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("blah"))); | 1044 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("blah"))); |
1044 } | 1045 } |
1045 | 1046 |
1046 TEST(NetUtilTest, GetSuggestedFilename) { | 1047 TEST(NetUtilTest, GetSuggestedFilename) { |
1047 const SuggestedFilenameCase test_cases[] = { | 1048 const SuggestedFilenameCase test_cases[] = { |
1048 {"http://www.google.com/", | 1049 {"http://www.google.com/", |
1049 "Content-disposition: attachment; filename=test.html", | 1050 "Content-disposition: attachment; filename=test.html", |
1050 "", | 1051 "", |
1052 "", | |
1051 L"", | 1053 L"", |
1052 L"test.html"}, | 1054 L"test.html"}, |
1053 {"http://www.google.com/", | 1055 {"http://www.google.com/", |
1054 "Content-disposition: attachment; filename=\"test.html\"", | 1056 "Content-disposition: attachment; filename=\"test.html\"", |
1055 "", | 1057 "", |
1058 "", | |
1056 L"", | 1059 L"", |
1057 L"test.html"}, | 1060 L"test.html"}, |
1058 {"http://www.google.com/path/test.html", | 1061 {"http://www.google.com/path/test.html", |
1059 "Content-disposition: attachment", | 1062 "Content-disposition: attachment", |
1060 "", | 1063 "", |
1064 "", | |
1061 L"", | 1065 L"", |
1062 L"test.html"}, | 1066 L"test.html"}, |
1063 {"http://www.google.com/path/test.html", | 1067 {"http://www.google.com/path/test.html", |
1064 "Content-disposition: attachment;", | 1068 "Content-disposition: attachment;", |
1065 "", | 1069 "", |
1070 "", | |
1066 L"", | 1071 L"", |
1067 L"test.html"}, | 1072 L"test.html"}, |
1068 {"http://www.google.com/", | 1073 {"http://www.google.com/", |
1069 "", | 1074 "", |
1070 "", | 1075 "", |
1076 "", | |
1071 L"", | 1077 L"", |
1072 L"www.google.com"}, | 1078 L"www.google.com"}, |
1073 {"http://www.google.com/test.html", | 1079 {"http://www.google.com/test.html", |
1074 "", | 1080 "", |
1075 "", | 1081 "", |
1082 "", | |
1076 L"", | 1083 L"", |
1077 L"test.html"}, | 1084 L"test.html"}, |
1078 // Now that we use googleurl's ExtractFileName, this case falls back | 1085 // Now that we use googleurl's ExtractFileName, this case falls back |
1079 // to the hostname. If this behavior is not desirable, we'd better | 1086 // to the hostname. If this behavior is not desirable, we'd better |
1080 // change ExtractFileName (in url_parse). | 1087 // change ExtractFileName (in url_parse). |
1081 {"http://www.google.com/path/", | 1088 {"http://www.google.com/path/", |
1082 "", | 1089 "", |
1083 "", | 1090 "", |
1091 "", | |
1084 L"", | 1092 L"", |
1085 L"www.google.com"}, | 1093 L"www.google.com"}, |
1086 {"http://www.google.com/path", | 1094 {"http://www.google.com/path", |
1087 "", | 1095 "", |
1088 "", | 1096 "", |
1097 "", | |
1089 L"", | 1098 L"", |
1090 L"path"}, | 1099 L"path"}, |
1091 {"file:///", | 1100 {"file:///", |
1092 "", | 1101 "", |
1093 "", | 1102 "", |
1103 "", | |
1094 L"", | 1104 L"", |
1095 L"download"}, | 1105 L"download"}, |
1096 {"non-standard-scheme:", | 1106 {"non-standard-scheme:", |
1097 "", | 1107 "", |
1098 "", | 1108 "", |
1109 "", | |
1099 L"", | 1110 L"", |
1100 L"download"}, | 1111 L"download"}, |
1101 {"http://www.google.com/", | 1112 {"http://www.google.com/", |
1102 "Content-disposition: attachment; filename =\"test.html\"", | 1113 "Content-disposition: attachment; filename =\"test.html\"", |
1103 "", | 1114 "", |
1115 "", | |
1104 L"download", | 1116 L"download", |
1105 L"test.html"}, | 1117 L"test.html"}, |
1106 {"http://www.google.com/", | 1118 {"http://www.google.com/", |
1107 "", | 1119 "", |
1108 "", | 1120 "", |
1121 "", | |
1109 L"download", | 1122 L"download", |
1110 L"download"}, | 1123 L"download"}, |
1111 {"http://www.google.com/", | 1124 {"http://www.google.com/", |
1112 "Content-disposition: attachment; filename=\"../test.html\"", | 1125 "Content-disposition: attachment; filename=\"../test.html\"", |
1113 "", | 1126 "", |
1127 "", | |
1114 L"", | 1128 L"", |
1115 L"_test.html"}, | 1129 L"_test.html"}, |
1116 {"http://www.google.com/", | 1130 {"http://www.google.com/", |
1117 "Content-disposition: attachment; filename=\"..\\test.html\"", | 1131 "Content-disposition: attachment; filename=\"..\\test.html\"", |
1118 "", | 1132 "", |
1133 "", | |
1119 L"", | 1134 L"", |
1120 L"_test.html"}, | 1135 L"_test.html"}, |
1121 {"http://www.google.com/", | 1136 {"http://www.google.com/", |
1122 "Content-disposition: attachment; filename=\"..\"", | 1137 "Content-disposition: attachment; filename=\"..\"", |
1123 "", | 1138 "", |
1139 "", | |
1124 L"download", | 1140 L"download", |
1125 L"download"}, | 1141 L"download"}, |
1126 {"http://www.google.com/test.html", | 1142 {"http://www.google.com/test.html", |
1127 "Content-disposition: attachment; filename=\"..\"", | 1143 "Content-disposition: attachment; filename=\"..\"", |
1128 "", | 1144 "", |
1145 "", | |
1129 L"download", | 1146 L"download", |
1130 L"test.html"}, | 1147 L"test.html"}, |
1131 // Below is a small subset of cases taken from GetFileNameFromCD test above. | 1148 // Below is a small subset of cases taken from GetFileNameFromCD test above. |
1132 {"http://www.google.com/", | 1149 {"http://www.google.com/", |
1133 "Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20" | 1150 "Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20" |
1134 "%EC%98%88%EC%88%A0.jpg\"", | 1151 "%EC%98%88%EC%88%A0.jpg\"", |
1135 "", | 1152 "", |
1153 "", | |
1136 L"", | 1154 L"", |
1137 L"\uc608\uc220 \uc608\uc220.jpg"}, | 1155 L"\uc608\uc220 \uc608\uc220.jpg"}, |
1138 {"http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", | 1156 {"http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", |
1139 "", | 1157 "", |
1140 "", | 1158 "", |
1159 "", | |
1141 L"download", | 1160 L"download", |
1142 L"\uc608\uc220 \uc608\uc220.jpg"}, | 1161 L"\uc608\uc220 \uc608\uc220.jpg"}, |
1143 {"http://www.google.com/", | 1162 {"http://www.google.com/", |
1144 "Content-disposition: attachment;", | 1163 "Content-disposition: attachment;", |
1145 "", | 1164 "", |
1165 "", | |
1146 L"\uB2E4\uC6B4\uB85C\uB4DC", | 1166 L"\uB2E4\uC6B4\uB85C\uB4DC", |
1147 L"\uB2E4\uC6B4\uB85C\uB4DC"}, | 1167 L"\uB2E4\uC6B4\uB85C\uB4DC"}, |
1148 {"http://www.google.com/", | 1168 {"http://www.google.com/", |
1149 "Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" | 1169 "Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" |
1150 "D13=2Epng?=\"", | 1170 "D13=2Epng?=\"", |
1151 "", | 1171 "", |
1172 "", | |
1152 L"download", | 1173 L"download", |
1153 L"\u82b8\u88533.png"}, | 1174 L"\u82b8\u88533.png"}, |
1154 {"http://www.example.com/images?id=3", | 1175 {"http://www.example.com/images?id=3", |
1155 "Content-Disposition: attachment; filename=caf\xc3\xa9.png", | 1176 "Content-Disposition: attachment; filename=caf\xc3\xa9.png", |
1156 "iso-8859-1", | 1177 "iso-8859-1", |
1178 "", | |
1157 L"", | 1179 L"", |
1158 L"caf\u00e9.png"}, | 1180 L"caf\u00e9.png"}, |
1159 {"http://www.example.com/images?id=3", | 1181 {"http://www.example.com/images?id=3", |
1160 "Content-Disposition: attachment; filename=caf\xe5.png", | 1182 "Content-Disposition: attachment; filename=caf\xe5.png", |
1161 "windows-1253", | 1183 "windows-1253", |
1184 "", | |
1162 L"", | 1185 L"", |
1163 L"caf\u03b5.png"}, | 1186 L"caf\u03b5.png"}, |
1164 {"http://www.example.com/file?id=3", | 1187 {"http://www.example.com/file?id=3", |
1165 "Content-Disposition: attachment; name=\xcf\xc2\xd4\xd8.zip", | 1188 "Content-Disposition: attachment; name=\xcf\xc2\xd4\xd8.zip", |
1166 "GBK", | 1189 "GBK", |
1190 "", | |
1167 L"", | 1191 L"", |
1168 L"\u4e0b\u8f7d.zip"}, | 1192 L"\u4e0b\u8f7d.zip"}, |
1169 // Invalid C-D header. Extracts filename from url. | 1193 // Invalid C-D header. Extracts filename from url. |
1170 {"http://www.google.com/test.html", | 1194 {"http://www.google.com/test.html", |
1171 "Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=", | 1195 "Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=", |
1172 "", | 1196 "", |
1197 "", | |
1173 L"", | 1198 L"", |
1174 L"test.html"}, | 1199 L"test.html"}, |
1175 // about: and data: URLs | 1200 // about: and data: URLs |
1176 {"about:chrome", | 1201 {"about:chrome", |
1177 "", | 1202 "", |
1178 "", | 1203 "", |
1204 "", | |
1179 L"", | 1205 L"", |
1180 L"download"}, | 1206 L"download"}, |
1181 {"data:,looks/like/a.path", | 1207 {"data:,looks/like/a.path", |
1182 "", | 1208 "", |
1183 "", | 1209 "", |
1210 "", | |
1184 L"", | 1211 L"", |
1185 L"download"}, | 1212 L"download"}, |
1186 {"data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", | 1213 {"data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", |
1187 "", | 1214 "", |
1188 "", | 1215 "", |
1216 "", | |
1189 L"", | 1217 L"", |
1190 L"download"}, | 1218 L"download"}, |
1191 {"data:,looks/like/a.path", | 1219 {"data:,looks/like/a.path", |
1192 "", | 1220 "", |
1193 "", | 1221 "", |
1222 "", | |
1194 L"default_filename_is_given", | 1223 L"default_filename_is_given", |
1195 L"default_filename_is_given"}, | 1224 L"default_filename_is_given"}, |
1196 {"data:,looks/like/a.path", | 1225 {"data:,looks/like/a.path", |
1197 "", | 1226 "", |
1198 "", | 1227 "", |
1228 "", | |
1199 L"\u65e5\u672c\u8a9e", // Japanese Kanji. | 1229 L"\u65e5\u672c\u8a9e", // Japanese Kanji. |
1200 L"\u65e5\u672c\u8a9e"}, | 1230 L"\u65e5\u672c\u8a9e"}, |
1201 // Dotfiles. Ensures preceeding period(s) stripped. | 1231 // Dotfiles. Ensures preceeding period(s) stripped. |
1202 {"http://www.google.com/.test.html", | 1232 {"http://www.google.com/.test.html", |
1203 "", | 1233 "", |
1204 "", | 1234 "", |
1205 L"", | 1235 "", |
1206 L"test.html"}, | 1236 L"", |
1237 L"test.html"}, | |
1207 {"http://www.google.com/.test", | 1238 {"http://www.google.com/.test", |
1208 "", | 1239 "", |
1209 "", | 1240 "", |
1210 L"", | 1241 "", |
1211 L"test"}, | 1242 L"", |
1243 L"test"}, | |
1212 {"http://www.google.com/..test", | 1244 {"http://www.google.com/..test", |
1213 "", | 1245 "", |
1214 "", | 1246 "", |
1215 L"", | 1247 "", |
1216 L"test"}, | 1248 L"", |
1249 L"test"}, | |
1217 // The filename encoding is specified by the referrer charset. | 1250 // The filename encoding is specified by the referrer charset. |
1218 {"http://example.com/V%FDvojov%E1%20psychologie.doc", | 1251 {"http://example.com/V%FDvojov%E1%20psychologie.doc", |
1219 "", | 1252 "", |
1220 "iso-8859-1", | 1253 "iso-8859-1", |
1254 "", | |
1221 L"", | 1255 L"", |
1222 L"V\u00fdvojov\u00e1 psychologie.doc"}, | 1256 L"V\u00fdvojov\u00e1 psychologie.doc"}, |
1257 {"http://www.google.com/test", | |
1258 "", | |
1259 "", | |
1260 "suggested", | |
1261 L"", | |
1262 L"suggested"}, | |
asanka
2011/05/13 13:32:24
Could you also add a test to check that C-D has hi
jianli
2011/05/17 00:10:44
Done.
| |
1223 // The filename encoding doesn't match the referrer charset, the | 1263 // The filename encoding doesn't match the referrer charset, the |
1224 // system charset, or UTF-8. | 1264 // system charset, or UTF-8. |
1225 // TODO(jshin): we need to handle this case. | 1265 // TODO(jshin): we need to handle this case. |
1226 #if 0 | 1266 #if 0 |
1227 {"http://example.com/V%FDvojov%E1%20psychologie.doc", | 1267 {"http://example.com/V%FDvojov%E1%20psychologie.doc", |
1228 "", | 1268 "", |
1229 "utf-8", | 1269 "utf-8", |
1270 "", | |
1230 L"", | 1271 L"", |
1231 L"V\u00fdvojov\u00e1 psychologie.doc", | 1272 L"V\u00fdvojov\u00e1 psychologie.doc", |
1232 }, | 1273 }, |
1233 #endif | 1274 #endif |
1234 }; | 1275 }; |
1235 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 1276 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
1236 std::wstring default_name = test_cases[i].default_filename; | 1277 std::wstring default_name = test_cases[i].default_filename; |
1237 string16 filename = GetSuggestedFilename( | 1278 string16 filename = GetSuggestedFilename( |
1238 GURL(test_cases[i].url), test_cases[i].content_disp_header, | 1279 GURL(test_cases[i].url), test_cases[i].content_disp_header, |
1239 test_cases[i].referrer_charset, WideToUTF16(default_name)); | 1280 test_cases[i].referrer_charset, test_cases[i].suggested_filename, |
1281 WideToUTF16(default_name)); | |
1240 EXPECT_EQ(std::wstring(test_cases[i].expected_filename), | 1282 EXPECT_EQ(std::wstring(test_cases[i].expected_filename), |
1241 UTF16ToWide(filename)) | 1283 UTF16ToWide(filename)) |
1242 << "Iteration " << i << ": " << test_cases[i].url; | 1284 << "Iteration " << i << ": " << test_cases[i].url; |
1243 } | 1285 } |
1244 } | 1286 } |
1245 | 1287 |
1246 // This is currently a windows specific function. | 1288 // This is currently a windows specific function. |
1247 #if defined(OS_WIN) | 1289 #if defined(OS_WIN) |
1248 namespace { | 1290 namespace { |
1249 | 1291 |
(...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2279 if (it->address[i] != 0) { | 2321 if (it->address[i] != 0) { |
2280 all_zeroes = false; | 2322 all_zeroes = false; |
2281 break; | 2323 break; |
2282 } | 2324 } |
2283 } | 2325 } |
2284 EXPECT_FALSE(all_zeroes); | 2326 EXPECT_FALSE(all_zeroes); |
2285 } | 2327 } |
2286 } | 2328 } |
2287 | 2329 |
2288 } // namespace net | 2330 } // namespace net |
OLD | NEW |