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

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

Issue 642403002: git cl format the first third of the net/base directory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/filename_util.h" 5 #include "net/base/filename_util.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 base::FilePath WStringAsFilePath(const std::wstring& str) { 43 base::FilePath WStringAsFilePath(const std::wstring& str) {
44 #if defined(OS_WIN) 44 #if defined(OS_WIN)
45 return base::FilePath(str); 45 return base::FilePath(str);
46 #else 46 #else
47 return base::FilePath(base::WideToUTF8(str)); 47 return base::FilePath(base::WideToUTF8(str));
48 #endif 48 #endif
49 } 49 }
50 50
51 void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) { 51 void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) {
52 std::string default_filename(base::WideToUTF8(test_case->default_filename)); 52 std::string default_filename(base::WideToUTF8(test_case->default_filename));
53 base::FilePath file_path = GenerateFileName( 53 base::FilePath file_path = GenerateFileName(GURL(test_case->url),
54 GURL(test_case->url), test_case->content_disp_header, 54 test_case->content_disp_header,
55 test_case->referrer_charset, test_case->suggested_filename, 55 test_case->referrer_charset,
56 test_case->mime_type, default_filename); 56 test_case->suggested_filename,
57 test_case->mime_type,
58 default_filename);
57 EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path)) 59 EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path))
58 << "test case at line number: " << test_case->lineno; 60 << "test case at line number: " << test_case->lineno;
59 } 61 }
60 62
61 } // namespace 63 } // namespace
62 64
63 static const base::FilePath::CharType* kSafePortableBasenames[] = { 65 static const base::FilePath::CharType* kSafePortableBasenames[] = {
64 FILE_PATH_LITERAL("a"), 66 FILE_PATH_LITERAL("a"),
65 FILE_PATH_LITERAL("a.txt"), 67 FILE_PATH_LITERAL("a.txt"),
66 FILE_PATH_LITERAL("a b.txt"), 68 FILE_PATH_LITERAL("a b.txt"),
67 FILE_PATH_LITERAL("a-b.txt"), 69 FILE_PATH_LITERAL("a-b.txt"),
68 FILE_PATH_LITERAL("My Computer"), 70 FILE_PATH_LITERAL("My Computer"),
69 FILE_PATH_LITERAL(" Computer"), 71 FILE_PATH_LITERAL(" Computer"),
70 }; 72 };
71 73
72 static const base::FilePath::CharType* kUnsafePortableBasenames[] = { 74 static const base::FilePath::CharType* kUnsafePortableBasenames[] = {
73 FILE_PATH_LITERAL(""), 75 FILE_PATH_LITERAL(""),
74 FILE_PATH_LITERAL("."), 76 FILE_PATH_LITERAL("."),
75 FILE_PATH_LITERAL(".."), 77 FILE_PATH_LITERAL(".."),
76 FILE_PATH_LITERAL("..."), 78 FILE_PATH_LITERAL("..."),
77 FILE_PATH_LITERAL("con"), 79 FILE_PATH_LITERAL("con"),
78 FILE_PATH_LITERAL("con.zip"), 80 FILE_PATH_LITERAL("con.zip"),
79 FILE_PATH_LITERAL("NUL"), 81 FILE_PATH_LITERAL("NUL"),
80 FILE_PATH_LITERAL("NUL.zip"), 82 FILE_PATH_LITERAL("NUL.zip"),
81 FILE_PATH_LITERAL(".a"), 83 FILE_PATH_LITERAL(".a"),
82 FILE_PATH_LITERAL("a."), 84 FILE_PATH_LITERAL("a."),
83 FILE_PATH_LITERAL("a\"a"), 85 FILE_PATH_LITERAL("a\"a"),
84 FILE_PATH_LITERAL("a<a"), 86 FILE_PATH_LITERAL("a<a"),
85 FILE_PATH_LITERAL("a>a"), 87 FILE_PATH_LITERAL("a>a"),
86 FILE_PATH_LITERAL("a?a"), 88 FILE_PATH_LITERAL("a?a"),
87 FILE_PATH_LITERAL("a/"), 89 FILE_PATH_LITERAL("a/"),
88 FILE_PATH_LITERAL("a\\"), 90 FILE_PATH_LITERAL("a\\"),
89 FILE_PATH_LITERAL("a "), 91 FILE_PATH_LITERAL("a "),
90 FILE_PATH_LITERAL("a . ."), 92 FILE_PATH_LITERAL("a . ."),
91 FILE_PATH_LITERAL("My Computer.{a}"), 93 FILE_PATH_LITERAL("My Computer.{a}"),
92 FILE_PATH_LITERAL("My Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}"), 94 FILE_PATH_LITERAL("My Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}"),
93 #if !defined(OS_WIN) 95 #if !defined(OS_WIN)
94 FILE_PATH_LITERAL("a\\a"), 96 FILE_PATH_LITERAL("a\\a"),
95 #endif 97 #endif
96 }; 98 };
97 99
98 static const base::FilePath::CharType* kSafePortableRelativePaths[] = { 100 static const base::FilePath::CharType* kSafePortableRelativePaths[] = {
99 FILE_PATH_LITERAL("a/a"), 101 FILE_PATH_LITERAL("a/a"),
100 #if defined(OS_WIN) 102 #if defined(OS_WIN)
101 FILE_PATH_LITERAL("a\\a"), 103 FILE_PATH_LITERAL("a\\a"),
102 #endif 104 #endif
103 }; 105 };
104 106
105 TEST(FilenameUtilTest, IsSafePortablePathComponent) { 107 TEST(FilenameUtilTest, IsSafePortablePathComponent) {
106 for (size_t i = 0 ; i < arraysize(kSafePortableBasenames); ++i) { 108 for (size_t i = 0; i < arraysize(kSafePortableBasenames); ++i) {
107 EXPECT_TRUE(IsSafePortablePathComponent(base::FilePath( 109 EXPECT_TRUE(
108 kSafePortableBasenames[i]))) << kSafePortableBasenames[i]; 110 IsSafePortablePathComponent(base::FilePath(kSafePortableBasenames[i])))
111 << kSafePortableBasenames[i];
109 } 112 }
110 for (size_t i = 0 ; i < arraysize(kUnsafePortableBasenames); ++i) { 113 for (size_t i = 0; i < arraysize(kUnsafePortableBasenames); ++i) {
111 EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath( 114 EXPECT_FALSE(IsSafePortablePathComponent(
112 kUnsafePortableBasenames[i]))) << kUnsafePortableBasenames[i]; 115 base::FilePath(kUnsafePortableBasenames[i])))
116 << kUnsafePortableBasenames[i];
113 } 117 }
114 for (size_t i = 0 ; i < arraysize(kSafePortableRelativePaths); ++i) { 118 for (size_t i = 0; i < arraysize(kSafePortableRelativePaths); ++i) {
115 EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath( 119 EXPECT_FALSE(IsSafePortablePathComponent(
116 kSafePortableRelativePaths[i]))) << kSafePortableRelativePaths[i]; 120 base::FilePath(kSafePortableRelativePaths[i])))
121 << kSafePortableRelativePaths[i];
117 } 122 }
118 } 123 }
119 124
120 TEST(FilenameUtilTest, IsSafePortableRelativePath) { 125 TEST(FilenameUtilTest, IsSafePortableRelativePath) {
121 base::FilePath safe_dirname(FILE_PATH_LITERAL("a")); 126 base::FilePath safe_dirname(FILE_PATH_LITERAL("a"));
122 for (size_t i = 0 ; i < arraysize(kSafePortableBasenames); ++i) { 127 for (size_t i = 0; i < arraysize(kSafePortableBasenames); ++i) {
123 EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath( 128 EXPECT_TRUE(
124 kSafePortableBasenames[i]))) << kSafePortableBasenames[i]; 129 IsSafePortableRelativePath(base::FilePath(kSafePortableBasenames[i])))
125 EXPECT_TRUE(IsSafePortableRelativePath(safe_dirname.Append(base::FilePath( 130 << kSafePortableBasenames[i];
126 kSafePortableBasenames[i])))) << kSafePortableBasenames[i]; 131 EXPECT_TRUE(IsSafePortableRelativePath(
132 safe_dirname.Append(base::FilePath(kSafePortableBasenames[i]))))
133 << kSafePortableBasenames[i];
127 } 134 }
128 for (size_t i = 0 ; i < arraysize(kSafePortableRelativePaths); ++i) { 135 for (size_t i = 0; i < arraysize(kSafePortableRelativePaths); ++i) {
129 EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath( 136 EXPECT_TRUE(IsSafePortableRelativePath(
130 kSafePortableRelativePaths[i]))) << kSafePortableRelativePaths[i]; 137 base::FilePath(kSafePortableRelativePaths[i])))
131 EXPECT_TRUE(IsSafePortableRelativePath(safe_dirname.Append(base::FilePath( 138 << kSafePortableRelativePaths[i];
132 kSafePortableRelativePaths[i])))) << kSafePortableRelativePaths[i]; 139 EXPECT_TRUE(IsSafePortableRelativePath(
140 safe_dirname.Append(base::FilePath(kSafePortableRelativePaths[i]))))
141 << kSafePortableRelativePaths[i];
133 } 142 }
134 for (size_t i = 0 ; i < arraysize(kUnsafePortableBasenames); ++i) { 143 for (size_t i = 0; i < arraysize(kUnsafePortableBasenames); ++i) {
135 EXPECT_FALSE(IsSafePortableRelativePath(base::FilePath( 144 EXPECT_FALSE(
136 kUnsafePortableBasenames[i]))) << kUnsafePortableBasenames[i]; 145 IsSafePortableRelativePath(base::FilePath(kUnsafePortableBasenames[i])))
146 << kUnsafePortableBasenames[i];
137 if (!base::FilePath::StringType(kUnsafePortableBasenames[i]).empty()) { 147 if (!base::FilePath::StringType(kUnsafePortableBasenames[i]).empty()) {
138 EXPECT_FALSE(IsSafePortableRelativePath(safe_dirname.Append( 148 EXPECT_FALSE(IsSafePortableRelativePath(
139 base::FilePath(kUnsafePortableBasenames[i])))) 149 safe_dirname.Append(base::FilePath(kUnsafePortableBasenames[i]))))
140 << kUnsafePortableBasenames[i]; 150 << kUnsafePortableBasenames[i];
141 } 151 }
142 } 152 }
143 } 153 }
144 154
145 TEST(FilenameUtilTest, FileURLConversion) { 155 TEST(FilenameUtilTest, FileURLConversion) {
146 // a list of test file names and the corresponding URLs 156 // a list of test file names and the corresponding URLs
147 const FileCase round_trip_cases[] = { 157 const FileCase round_trip_cases[] = {
148 #if defined(OS_WIN) 158 #if defined(OS_WIN)
149 {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"}, 159 {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"},
150 {L"\\\\some computer\\foo\\bar.txt", 160 {L"\\\\some computer\\foo\\bar.txt",
151 "file://some%20computer/foo/bar.txt"}, // UNC 161 "file://some%20computer/foo/bar.txt"}, // UNC
152 {L"D:\\Name;with%some symbols*#", 162 {L"D:\\Name;with%some symbols*#",
153 "file:///D:/Name%3Bwith%25some%20symbols*%23"}, 163 "file:///D:/Name%3Bwith%25some%20symbols*%23"},
154 // issue 14153: To be tested with the OS default codepage other than 1252. 164 // issue 14153: To be tested with the OS default codepage other than 1252.
155 {L"D:\\latin1\\caf\x00E9\x00DD.txt", 165 {L"D:\\latin1\\caf\x00E9\x00DD.txt",
156 "file:///D:/latin1/caf%C3%A9%C3%9D.txt"}, 166 "file:///D:/latin1/caf%C3%A9%C3%9D.txt"},
157 {L"D:\\otherlatin\\caf\x0119.txt", 167 {L"D:\\otherlatin\\caf\x0119.txt", "file:///D:/otherlatin/caf%C4%99.txt"},
158 "file:///D:/otherlatin/caf%C4%99.txt"},
159 {L"D:\\greek\\\x03B1\x03B2\x03B3.txt", 168 {L"D:\\greek\\\x03B1\x03B2\x03B3.txt",
160 "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"}, 169 "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"},
161 {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", 170 {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
162 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91" 171 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91"
163 "%E9%A1%B5.doc"}, 172 "%E9%A1%B5.doc"},
164 {L"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB" 173 {L"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB"
165 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, 174 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
166 #elif defined(OS_POSIX) 175 #elif defined(OS_POSIX)
167 {L"/foo/bar.txt", "file:///foo/bar.txt"}, 176 {L"/foo/bar.txt", "file:///foo/bar.txt"},
168 {L"/foo/BAR.txt", "file:///foo/BAR.txt"}, 177 {L"/foo/BAR.txt", "file:///foo/BAR.txt"},
169 {L"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"}, 178 {L"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"},
170 {L"/foo/bar?.txt", "file:///foo/bar%3F.txt"}, 179 {L"/foo/bar?.txt", "file:///foo/bar%3F.txt"},
171 {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"}, 180 {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"},
172 {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"}, 181 {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"},
173 {L"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"}, 182 {L"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"},
174 {L"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"}, 183 {L"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"},
175 {L"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"}, 184 {L"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"},
176 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", 185 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
177 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD" 186 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD"
178 "%91%E9%A1%B5.doc"}, 187 "%91%E9%A1%B5.doc"},
179 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" 188 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB"
180 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, 189 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
181 #endif 190 #endif
182 }; 191 };
183 192
184 // First, we'll test that we can round-trip all of the above cases of URLs 193 // First, we'll test that we can round-trip all of the above cases of URLs
185 base::FilePath output; 194 base::FilePath output;
186 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(round_trip_cases); i++) { 195 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(round_trip_cases); i++) {
187 // convert to the file URL 196 // convert to the file URL
188 GURL file_url(FilePathToFileURL( 197 GURL file_url(
189 WStringAsFilePath(round_trip_cases[i].file))); 198 FilePathToFileURL(WStringAsFilePath(round_trip_cases[i].file)));
190 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); 199 EXPECT_EQ(round_trip_cases[i].url, file_url.spec());
191 200
192 // Back to the filename. 201 // Back to the filename.
193 EXPECT_TRUE(FileURLToFilePath(file_url, &output)); 202 EXPECT_TRUE(FileURLToFilePath(file_url, &output));
194 EXPECT_EQ(round_trip_cases[i].file, FilePathAsWString(output)); 203 EXPECT_EQ(round_trip_cases[i].file, FilePathAsWString(output));
195 } 204 }
196 205
197 // Test that various file: URLs get decoded into the correct file type 206 // Test that various file: URLs get decoded into the correct file type
198 FileCase url_cases[] = { 207 FileCase url_cases[] = {
199 #if defined(OS_WIN) 208 #if defined(OS_WIN)
(...skipping 12 matching lines...) Expand all
212 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, 221 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
213 {L"/foo/bar.txt", "file:foo/bar.txt"}, 222 {L"/foo/bar.txt", "file:foo/bar.txt"},
214 {L"/bar.txt", "file://foo/bar.txt"}, 223 {L"/bar.txt", "file://foo/bar.txt"},
215 {L"/foo/bar.txt", "file:///foo/bar.txt"}, 224 {L"/foo/bar.txt", "file:///foo/bar.txt"},
216 {L"/foo/bar.txt", "file:////foo/bar.txt"}, 225 {L"/foo/bar.txt", "file:////foo/bar.txt"},
217 {L"/foo/bar.txt", "file:////foo//bar.txt"}, 226 {L"/foo/bar.txt", "file:////foo//bar.txt"},
218 {L"/foo/bar.txt", "file:////foo///bar.txt"}, 227 {L"/foo/bar.txt", "file:////foo///bar.txt"},
219 {L"/foo/bar.txt", "file:////foo////bar.txt"}, 228 {L"/foo/bar.txt", "file:////foo////bar.txt"},
220 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, 229 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
221 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"}, 230 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"},
222 // We get these wrong because GURL turns back slashes into forward 231 // We get these wrong because GURL turns back slashes into forward
223 // slashes. 232 // slashes.
224 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, 233 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
225 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, 234 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"},
226 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, 235 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
227 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, 236 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"},
228 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, 237 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"},
229 #endif 238 #endif
230 }; 239 };
231 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_cases); i++) { 240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_cases); i++) {
232 FileURLToFilePath(GURL(url_cases[i].url), &output); 241 FileURLToFilePath(GURL(url_cases[i].url), &output);
233 EXPECT_EQ(url_cases[i].file, FilePathAsWString(output)); 242 EXPECT_EQ(url_cases[i].file, FilePathAsWString(output));
234 } 243 }
235 244
236 // Unfortunately, UTF8ToWide discards invalid UTF8 input. 245 // Unfortunately, UTF8ToWide discards invalid UTF8 input.
237 #ifdef BUG_878908_IS_FIXED 246 #ifdef BUG_878908_IS_FIXED
238 // Test that no conversion happens if the UTF-8 input is invalid, and that 247 // Test that no conversion happens if the UTF-8 input is invalid, and that
239 // the input is preserved in UTF-8 248 // the input is preserved in UTF-8
240 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; 249 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc";
241 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; 250 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc";
242 EXPECT_TRUE(FileURLToFilePath( 251 EXPECT_TRUE(FileURLToFilePath(GURL(std::string(invalid_utf8)), &output));
243 GURL(std::string(invalid_utf8)), &output));
244 EXPECT_EQ(std::wstring(invalid_wide), output); 252 EXPECT_EQ(std::wstring(invalid_wide), output);
245 #endif 253 #endif
246 254
247 // Test that if a file URL is malformed, we get a failure 255 // Test that if a file URL is malformed, we get a failure
248 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output)); 256 EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output));
249 } 257 }
250 258
251 #if defined(OS_WIN) 259 #if defined(OS_WIN)
252 #define JPEG_EXT L".jpg" 260 #define JPEG_EXT L".jpg"
253 #define HTML_EXT L".htm" 261 #define HTML_EXT L".htm"
254 #elif defined(OS_MACOSX) 262 #elif defined(OS_MACOSX)
255 #define JPEG_EXT L".jpeg" 263 #define JPEG_EXT L".jpeg"
256 #define HTML_EXT L".html" 264 #define HTML_EXT L".html"
257 #else 265 #else
258 #define JPEG_EXT L".jpg" 266 #define JPEG_EXT L".jpg"
259 #define HTML_EXT L".html" 267 #define HTML_EXT L".html"
260 #endif 268 #endif
261 #define TXT_EXT L".txt" 269 #define TXT_EXT L".txt"
262 #define TAR_EXT L".tar" 270 #define TAR_EXT L".tar"
263 271
264 TEST(FilenameUtilTest, GenerateSafeFileName) { 272 TEST(FilenameUtilTest, GenerateSafeFileName) {
265 const struct { 273 const struct {
266 const char* mime_type; 274 const char* mime_type;
267 const base::FilePath::CharType* filename; 275 const base::FilePath::CharType* filename;
268 const base::FilePath::CharType* expected_filename; 276 const base::FilePath::CharType* expected_filename;
269 } safe_tests[] = { 277 } safe_tests[] = {
270 #if defined(OS_WIN) 278 #if defined(OS_WIN)
271 { 279 {"text/html",
272 "text/html", 280 FILE_PATH_LITERAL("C:\\foo\\bar.htm"),
273 FILE_PATH_LITERAL("C:\\foo\\bar.htm"), 281 FILE_PATH_LITERAL("C:\\foo\\bar.htm")},
274 FILE_PATH_LITERAL("C:\\foo\\bar.htm") 282 {"text/html",
275 }, 283 FILE_PATH_LITERAL("C:\\foo\\bar.html"),
276 { 284 FILE_PATH_LITERAL("C:\\foo\\bar.html")},
277 "text/html", 285 {"text/html",
278 FILE_PATH_LITERAL("C:\\foo\\bar.html"), 286 FILE_PATH_LITERAL("C:\\foo\\bar"),
279 FILE_PATH_LITERAL("C:\\foo\\bar.html") 287 FILE_PATH_LITERAL("C:\\foo\\bar.htm")},
280 }, 288 {"image/png",
281 { 289 FILE_PATH_LITERAL("C:\\bar.html"),
282 "text/html", 290 FILE_PATH_LITERAL("C:\\bar.html")},
283 FILE_PATH_LITERAL("C:\\foo\\bar"), 291 {"image/png",
284 FILE_PATH_LITERAL("C:\\foo\\bar.htm") 292 FILE_PATH_LITERAL("C:\\bar"),
285 }, 293 FILE_PATH_LITERAL("C:\\bar.png")},
286 { 294 {"text/html",
287 "image/png", 295 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
288 FILE_PATH_LITERAL("C:\\bar.html"), 296 FILE_PATH_LITERAL("C:\\foo\\bar.exe")},
289 FILE_PATH_LITERAL("C:\\bar.html") 297 {"image/gif",
290 }, 298 FILE_PATH_LITERAL("C:\\foo\\bar.exe"),
291 { 299 FILE_PATH_LITERAL("C:\\foo\\bar.exe")},
292 "image/png", 300 {"text/html",
293 FILE_PATH_LITERAL("C:\\bar"), 301 FILE_PATH_LITERAL("C:\\foo\\google.com"),
294 FILE_PATH_LITERAL("C:\\bar.png") 302 FILE_PATH_LITERAL("C:\\foo\\google.com")},
295 }, 303 {"text/html",
296 { 304 FILE_PATH_LITERAL("C:\\foo\\con.htm"),
297 "text/html", 305 FILE_PATH_LITERAL("C:\\foo\\_con.htm")},
298 FILE_PATH_LITERAL("C:\\foo\\bar.exe"), 306 {"text/html",
299 FILE_PATH_LITERAL("C:\\foo\\bar.exe") 307 FILE_PATH_LITERAL("C:\\foo\\con"),
300 }, 308 FILE_PATH_LITERAL("C:\\foo\\_con.htm")},
301 { 309 {"text/html",
302 "image/gif", 310 FILE_PATH_LITERAL("C:\\foo\\harmless.{not-really-this-may-be-a-guid}"),
303 FILE_PATH_LITERAL("C:\\foo\\bar.exe"), 311 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
304 FILE_PATH_LITERAL("C:\\foo\\bar.exe") 312 {"text/html",
305 }, 313 FILE_PATH_LITERAL("C:\\foo\\harmless.local"),
306 { 314 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
307 "text/html", 315 {"text/html",
308 FILE_PATH_LITERAL("C:\\foo\\google.com"), 316 FILE_PATH_LITERAL("C:\\foo\\harmless.lnk"),
309 FILE_PATH_LITERAL("C:\\foo\\google.com") 317 FILE_PATH_LITERAL("C:\\foo\\harmless.download")},
310 }, 318 {"text/html",
311 { 319 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-"),
312 "text/html", 320 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-")},
313 FILE_PATH_LITERAL("C:\\foo\\con.htm"),
314 FILE_PATH_LITERAL("C:\\foo\\_con.htm")
315 },
316 {
317 "text/html",
318 FILE_PATH_LITERAL("C:\\foo\\con"),
319 FILE_PATH_LITERAL("C:\\foo\\_con.htm")
320 },
321 {
322 "text/html",
323 FILE_PATH_LITERAL("C:\\foo\\harmless.{not-really-this-may-be-a-guid}"),
324 FILE_PATH_LITERAL("C:\\foo\\harmless.download")
325 },
326 {
327 "text/html",
328 FILE_PATH_LITERAL("C:\\foo\\harmless.local"),
329 FILE_PATH_LITERAL("C:\\foo\\harmless.download")
330 },
331 {
332 "text/html",
333 FILE_PATH_LITERAL("C:\\foo\\harmless.lnk"),
334 FILE_PATH_LITERAL("C:\\foo\\harmless.download")
335 },
336 {
337 "text/html",
338 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-"),
339 FILE_PATH_LITERAL("C:\\foo\\harmless.{mismatched-")
340 },
341 // Allow extension synonyms. 321 // Allow extension synonyms.
342 { 322 {"image/jpeg",
343 "image/jpeg", 323 FILE_PATH_LITERAL("C:\\foo\\bar.jpg"),
344 FILE_PATH_LITERAL("C:\\foo\\bar.jpg"), 324 FILE_PATH_LITERAL("C:\\foo\\bar.jpg")},
345 FILE_PATH_LITERAL("C:\\foo\\bar.jpg") 325 {"image/jpeg",
346 }, 326 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg"),
347 { 327 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg")},
348 "image/jpeg", 328 #else // !defined(OS_WIN)
349 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg"), 329 {"text/html",
350 FILE_PATH_LITERAL("C:\\foo\\bar.jpeg") 330 FILE_PATH_LITERAL("/foo/bar.htm"),
351 }, 331 FILE_PATH_LITERAL("/foo/bar.htm")},
352 #else // !defined(OS_WIN) 332 {"text/html",
353 { 333 FILE_PATH_LITERAL("/foo/bar.html"),
354 "text/html", 334 FILE_PATH_LITERAL("/foo/bar.html")},
355 FILE_PATH_LITERAL("/foo/bar.htm"), 335 {"text/html",
356 FILE_PATH_LITERAL("/foo/bar.htm") 336 FILE_PATH_LITERAL("/foo/bar"),
357 }, 337 FILE_PATH_LITERAL("/foo/bar.html")},
358 { 338 {"image/png",
359 "text/html", 339 FILE_PATH_LITERAL("/bar.html"),
360 FILE_PATH_LITERAL("/foo/bar.html"), 340 FILE_PATH_LITERAL("/bar.html")},
361 FILE_PATH_LITERAL("/foo/bar.html") 341 {"image/png", FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/bar.png")},
362 }, 342 {"image/gif",
363 { 343 FILE_PATH_LITERAL("/foo/bar.exe"),
364 "text/html", 344 FILE_PATH_LITERAL("/foo/bar.exe")},
365 FILE_PATH_LITERAL("/foo/bar"), 345 {"text/html",
366 FILE_PATH_LITERAL("/foo/bar.html") 346 FILE_PATH_LITERAL("/foo/google.com"),
367 }, 347 FILE_PATH_LITERAL("/foo/google.com")},
368 { 348 {"text/html",
369 "image/png", 349 FILE_PATH_LITERAL("/foo/con.htm"),
370 FILE_PATH_LITERAL("/bar.html"), 350 FILE_PATH_LITERAL("/foo/con.htm")},
371 FILE_PATH_LITERAL("/bar.html") 351 {"text/html",
372 }, 352 FILE_PATH_LITERAL("/foo/con"),
373 { 353 FILE_PATH_LITERAL("/foo/con.html")},
374 "image/png",
375 FILE_PATH_LITERAL("/bar"),
376 FILE_PATH_LITERAL("/bar.png")
377 },
378 {
379 "image/gif",
380 FILE_PATH_LITERAL("/foo/bar.exe"),
381 FILE_PATH_LITERAL("/foo/bar.exe")
382 },
383 {
384 "text/html",
385 FILE_PATH_LITERAL("/foo/google.com"),
386 FILE_PATH_LITERAL("/foo/google.com")
387 },
388 {
389 "text/html",
390 FILE_PATH_LITERAL("/foo/con.htm"),
391 FILE_PATH_LITERAL("/foo/con.htm")
392 },
393 {
394 "text/html",
395 FILE_PATH_LITERAL("/foo/con"),
396 FILE_PATH_LITERAL("/foo/con.html")
397 },
398 // Allow extension synonyms. 354 // Allow extension synonyms.
399 { 355 {"image/jpeg",
400 "image/jpeg", 356 FILE_PATH_LITERAL("/bar.jpg"),
401 FILE_PATH_LITERAL("/bar.jpg"), 357 FILE_PATH_LITERAL("/bar.jpg")},
402 FILE_PATH_LITERAL("/bar.jpg") 358 {"image/jpeg",
403 }, 359 FILE_PATH_LITERAL("/bar.jpeg"),
404 { 360 FILE_PATH_LITERAL("/bar.jpeg")},
405 "image/jpeg",
406 FILE_PATH_LITERAL("/bar.jpeg"),
407 FILE_PATH_LITERAL("/bar.jpeg")
408 },
409 #endif // !defined(OS_WIN) 361 #endif // !defined(OS_WIN)
410 }; 362 };
411 363
412 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(safe_tests); ++i) { 364 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(safe_tests); ++i) {
413 base::FilePath file_path(safe_tests[i].filename); 365 base::FilePath file_path(safe_tests[i].filename);
414 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path); 366 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path);
415 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value()) 367 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value())
416 << "Iteration " << i; 368 << "Iteration " << i;
417 } 369 }
418 } 370 }
419 371
420 TEST(FilenameUtilTest, GenerateFileName) { 372 TEST(FilenameUtilTest, GenerateFileName) {
421 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) 373 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
422 // This test doesn't run when the locale is not UTF-8 because some of the 374 // This test doesn't run when the locale is not UTF-8 because some of the
423 // string conversions fail. This is OK (we have the default value) but they 375 // string conversions fail. This is OK (we have the default value) but they
424 // don't match our expectations. 376 // don't match our expectations.
425 std::string locale = setlocale(LC_CTYPE, NULL); 377 std::string locale = setlocale(LC_CTYPE, NULL);
426 base::StringToLowerASCII(&locale); 378 base::StringToLowerASCII(&locale);
427 EXPECT_TRUE(locale.find("utf-8") != std::string::npos || 379 EXPECT_TRUE(locale.find("utf-8") != std::string::npos ||
428 locale.find("utf8") != std::string::npos) 380 locale.find("utf8") != std::string::npos)
429 << "Your locale (" << locale << ") must be set to UTF-8 " 381 << "Your locale (" << locale << ") must be set to UTF-8 "
430 << "for this test to pass!"; 382 << "for this test to pass!";
431 #endif 383 #endif
432 384
433 // Tests whether the correct filename is selected from the the given 385 // Tests whether the correct filename is selected from the the given
434 // parameters and that Content-Disposition headers are properly 386 // parameters and that Content-Disposition headers are properly
435 // handled including failovers when the header is malformed. 387 // handled including failovers when the header is malformed.
436 const GenerateFilenameCase selection_tests[] = { 388 const GenerateFilenameCase selection_tests[] = {
437 { 389 {__LINE__,
438 __LINE__, 390 "http://www.google.com/",
439 "http://www.google.com/", 391 "attachment; filename=test.html",
440 "attachment; filename=test.html", 392 "",
441 "", 393 "",
442 "", 394 "",
443 "", 395 L"",
444 L"", 396 L"test.html"},
445 L"test.html" 397 {__LINE__,
446 }, 398 "http://www.google.com/",
447 { 399 "attachment; filename=\"test.html\"",
448 __LINE__, 400 "",
449 "http://www.google.com/", 401 "",
450 "attachment; filename=\"test.html\"", 402 "",
451 "", 403 L"",
452 "", 404 L"test.html"},
453 "", 405 {__LINE__,
454 L"", 406 "http://www.google.com/",
455 L"test.html" 407 "attachment; filename= \"test.html\"",
456 }, 408 "",
457 { 409 "",
458 __LINE__, 410 "",
459 "http://www.google.com/", 411 L"",
460 "attachment; filename= \"test.html\"", 412 L"test.html"},
461 "", 413 {__LINE__,
462 "", 414 "http://www.google.com/",
463 "", 415 "attachment; filename = \"test.html\"",
464 L"", 416 "",
465 L"test.html" 417 "",
466 }, 418 "",
467 { 419 L"",
468 __LINE__, 420 L"test.html"},
469 "http://www.google.com/", 421 {// filename is whitespace. Should failover to URL host
470 "attachment; filename = \"test.html\"", 422 __LINE__,
471 "", 423 "http://www.google.com/",
472 "", 424 "attachment; filename= ",
473 "", 425 "",
474 L"", 426 "",
475 L"test.html" 427 "",
476 }, 428 L"",
477 { // filename is whitespace. Should failover to URL host 429 L"www.google.com"},
478 __LINE__, 430 {// No filename.
479 "http://www.google.com/", 431 __LINE__,
480 "attachment; filename= ", 432 "http://www.google.com/path/test.html",
481 "", 433 "attachment",
482 "", 434 "",
483 "", 435 "",
484 L"", 436 "",
485 L"www.google.com" 437 L"",
486 }, 438 L"test.html"},
487 { // No filename. 439 {// Ditto
488 __LINE__, 440 __LINE__,
489 "http://www.google.com/path/test.html", 441 "http://www.google.com/path/test.html",
490 "attachment", 442 "attachment;",
491 "", 443 "",
492 "", 444 "",
493 "", 445 "",
494 L"", 446 L"",
495 L"test.html" 447 L"test.html"},
496 }, 448 {// No C-D
497 { // Ditto 449 __LINE__,
498 __LINE__, 450 "http://www.google.com/",
499 "http://www.google.com/path/test.html", 451 "",
500 "attachment;", 452 "",
501 "", 453 "",
502 "", 454 "",
503 "", 455 L"",
504 L"", 456 L"www.google.com"},
505 L"test.html" 457 {__LINE__,
506 }, 458 "http://www.google.com/test.html",
507 { // No C-D 459 "",
508 __LINE__, 460 "",
509 "http://www.google.com/", 461 "",
510 "", 462 "",
511 "", 463 L"",
512 "", 464 L"test.html"},
513 "", 465 {// Now that we use src/url's ExtractFileName, this case falls back to
514 L"", 466 // the hostname. If this behavior is not desirable, we'd better change
515 L"www.google.com" 467 // ExtractFileName (in url_parse.cc).
516 }, 468 __LINE__,
517 { 469 "http://www.google.com/path/",
518 __LINE__, 470 "",
519 "http://www.google.com/test.html", 471 "",
520 "", 472 "",
521 "", 473 "",
522 "", 474 L"",
523 "", 475 L"www.google.com"},
524 L"", 476 {__LINE__, "http://www.google.com/path", "", "", "", "", L"", L"path"},
525 L"test.html" 477 {__LINE__, "file:///", "", "", "", "", L"", L"download"},
526 }, 478 {__LINE__, "file:///path/testfile", "", "", "", "", L"", L"testfile"},
527 { // Now that we use src/url's ExtractFileName, this case falls back to 479 {__LINE__, "non-standard-scheme:", "", "", "", "", L"", L"download"},
528 // the hostname. If this behavior is not desirable, we'd better change 480 {// C-D should override default
529 // ExtractFileName (in url_parse.cc). 481 __LINE__,
530 __LINE__, 482 "http://www.google.com/",
531 "http://www.google.com/path/", 483 "attachment; filename =\"test.html\"",
532 "", 484 "",
533 "", 485 "",
534 "", 486 "",
535 "", 487 L"download",
536 L"", 488 L"test.html"},
537 L"www.google.com" 489 {// But the URL shouldn't
538 }, 490 __LINE__,
539 { 491 "http://www.google.com/",
540 __LINE__, 492 "",
541 "http://www.google.com/path", 493 "",
542 "", 494 "",
543 "", 495 "",
544 "", 496 L"download",
545 "", 497 L"download"},
546 L"", 498 {__LINE__,
547 L"path" 499 "http://www.google.com/",
548 }, 500 "attachment; filename=\"../test.html\"",
549 { 501 "",
550 __LINE__, 502 "",
551 "file:///", 503 "",
552 "", 504 L"",
553 "", 505 L"-test.html"},
554 "", 506 {__LINE__,
555 "", 507 "http://www.google.com/",
556 L"", 508 "attachment; filename=\"..\\test.html\"",
557 L"download" 509 "",
558 }, 510 "",
559 { 511 "",
560 __LINE__, 512 L"",
561 "file:///path/testfile", 513 L"test.html"},
562 "", 514 {__LINE__,
563 "", 515 "http://www.google.com/",
564 "", 516 "attachment; filename=\"..\\\\test.html\"",
565 "", 517 "",
566 L"", 518 "",
567 L"testfile" 519 "",
568 }, 520 L"",
569 { 521 L"-test.html"},
570 __LINE__, 522 {// Filename disappears after leading and trailing periods are removed.
571 "non-standard-scheme:", 523 __LINE__,
572 "", 524 "http://www.google.com/",
573 "", 525 "attachment; filename=\"..\"",
574 "", 526 "",
575 "", 527 "",
576 L"", 528 "",
577 L"download" 529 L"default",
578 }, 530 L"default"},
579 { // C-D should override default 531 {// C-D specified filename disappears. Failover to final filename.
580 __LINE__, 532 __LINE__,
581 "http://www.google.com/", 533 "http://www.google.com/test.html",
582 "attachment; filename =\"test.html\"", 534 "attachment; filename=\"..\"",
583 "", 535 "",
584 "", 536 "",
585 "", 537 "",
586 L"download", 538 L"default",
587 L"test.html" 539 L"default"},
588 },
589 { // But the URL shouldn't
590 __LINE__,
591 "http://www.google.com/",
592 "",
593 "",
594 "",
595 "",
596 L"download",
597 L"download"
598 },
599 {
600 __LINE__,
601 "http://www.google.com/",
602 "attachment; filename=\"../test.html\"",
603 "",
604 "",
605 "",
606 L"",
607 L"-test.html"
608 },
609 {
610 __LINE__,
611 "http://www.google.com/",
612 "attachment; filename=\"..\\test.html\"",
613 "",
614 "",
615 "",
616 L"",
617 L"test.html"
618 },
619 {
620 __LINE__,
621 "http://www.google.com/",
622 "attachment; filename=\"..\\\\test.html\"",
623 "",
624 "",
625 "",
626 L"",
627 L"-test.html"
628 },
629 { // Filename disappears after leading and trailing periods are removed.
630 __LINE__,
631 "http://www.google.com/",
632 "attachment; filename=\"..\"",
633 "",
634 "",
635 "",
636 L"default",
637 L"default"
638 },
639 { // C-D specified filename disappears. Failover to final filename.
640 __LINE__,
641 "http://www.google.com/test.html",
642 "attachment; filename=\"..\"",
643 "",
644 "",
645 "",
646 L"default",
647 L"default"
648 },
649 // Below is a small subset of cases taken from HttpContentDisposition tests. 540 // Below is a small subset of cases taken from HttpContentDisposition tests.
650 { 541 {__LINE__,
651 __LINE__, 542 "http://www.google.com/",
652 "http://www.google.com/", 543 "attachment; filename=\"%EC%98%88%EC%88%A0%20"
653 "attachment; filename=\"%EC%98%88%EC%88%A0%20" 544 "%EC%98%88%EC%88%A0.jpg\"",
654 "%EC%98%88%EC%88%A0.jpg\"", 545 "",
655 "", 546 "",
656 "", 547 "",
657 "", 548 L"",
658 L"", 549 L"\uc608\uc220 \uc608\uc220.jpg"},
659 L"\uc608\uc220 \uc608\uc220.jpg" 550 {__LINE__,
660 }, 551 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
661 { 552 "",
662 __LINE__, 553 "",
663 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", 554 "",
664 "", 555 "",
665 "", 556 L"download",
666 "", 557 L"\uc608\uc220 \uc608\uc220.jpg"},
667 "", 558 {__LINE__,
668 L"download", 559 "http://www.google.com/",
669 L"\uc608\uc220 \uc608\uc220.jpg" 560 "attachment;",
670 }, 561 "",
671 { 562 "",
672 __LINE__, 563 "",
673 "http://www.google.com/", 564 L"\uB2E4\uC6B4\uB85C\uB4DC",
674 "attachment;", 565 L"\uB2E4\uC6B4\uB85C\uB4DC"},
675 "", 566 {__LINE__,
676 "", 567 "http://www.google.com/",
677 "", 568 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
678 L"\uB2E4\uC6B4\uB85C\uB4DC", 569 "D13=2Epng?=\"",
679 L"\uB2E4\uC6B4\uB85C\uB4DC" 570 "",
680 }, 571 "",
681 { 572 "",
682 __LINE__, 573 L"download",
683 "http://www.google.com/", 574 L"\u82b8\u88533.png"},
684 "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" 575 {__LINE__,
685 "D13=2Epng?=\"", 576 "http://www.example.com/images?id=3",
686 "", 577 "attachment; filename=caf\xc3\xa9.png",
687 "", 578 "iso-8859-1",
688 "", 579 "",
689 L"download", 580 "",
690 L"\u82b8\u88533.png" 581 L"",
691 }, 582 L"caf\u00e9.png"},
692 { 583 {__LINE__,
693 __LINE__, 584 "http://www.example.com/images?id=3",
694 "http://www.example.com/images?id=3", 585 "attachment; filename=caf\xe5.png",
695 "attachment; filename=caf\xc3\xa9.png", 586 "windows-1253",
696 "iso-8859-1", 587 "",
697 "", 588 "",
698 "", 589 L"",
699 L"", 590 L"caf\u03b5.png"},
700 L"caf\u00e9.png" 591 {__LINE__,
701 }, 592 "http://www.example.com/file?id=3",
702 { 593 "attachment; name=\xcf\xc2\xd4\xd8.zip",
703 __LINE__, 594 "GBK",
704 "http://www.example.com/images?id=3", 595 "",
705 "attachment; filename=caf\xe5.png", 596 "",
706 "windows-1253", 597 L"",
707 "", 598 L"\u4e0b\u8f7d.zip"},
708 "", 599 {// Invalid C-D header. Extracts filename from url.
709 L"", 600 __LINE__,
710 L"caf\u03b5.png" 601 "http://www.google.com/test.html",
711 }, 602 "attachment; filename==?iiso88591?Q?caf=EG?=",
712 { 603 "",
713 __LINE__, 604 "",
714 "http://www.example.com/file?id=3", 605 "",
715 "attachment; name=\xcf\xc2\xd4\xd8.zip", 606 L"",
716 "GBK", 607 L"test.html"},
717 "",
718 "",
719 L"",
720 L"\u4e0b\u8f7d.zip"
721 },
722 { // Invalid C-D header. Extracts filename from url.
723 __LINE__,
724 "http://www.google.com/test.html",
725 "attachment; filename==?iiso88591?Q?caf=EG?=",
726 "",
727 "",
728 "",
729 L"",
730 L"test.html"
731 },
732 // about: and data: URLs 608 // about: and data: URLs
733 { 609 {__LINE__, "about:chrome", "", "", "", "", L"", L"download"},
734 __LINE__, 610 {__LINE__, "data:,looks/like/a.path", "", "", "", "", L"", L"download"},
735 "about:chrome", 611 {__LINE__,
736 "", 612 "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=",
737 "", 613 "",
738 "", 614 "",
739 "", 615 "",
740 L"", 616 "",
741 L"download" 617 L"",
742 }, 618 L"download"},
743 { 619 {__LINE__,
744 __LINE__, 620 "data:,looks/like/a.path",
745 "data:,looks/like/a.path", 621 "",
746 "", 622 "",
747 "", 623 "",
748 "", 624 "",
749 "", 625 L"default_filename_is_given",
750 L"", 626 L"default_filename_is_given"},
751 L"download" 627 {__LINE__,
752 }, 628 "data:,looks/like/a.path",
753 { 629 "",
754 __LINE__, 630 "",
755 "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", 631 "",
756 "", 632 "",
757 "", 633 L"\u65e5\u672c\u8a9e", // Japanese Kanji.
758 "", 634 L"\u65e5\u672c\u8a9e"},
759 "", 635 {// The filename encoding is specified by the referrer charset.
760 L"", 636 __LINE__,
761 L"download" 637 "http://example.com/V%FDvojov%E1%20psychologie.doc",
762 }, 638 "",
763 { 639 "iso-8859-1",
764 __LINE__, 640 "",
765 "data:,looks/like/a.path", 641 "",
766 "", 642 L"",
767 "", 643 L"V\u00fdvojov\u00e1 psychologie.doc"},
768 "", 644 {// Suggested filename takes precedence over URL
769 "", 645 __LINE__,
770 L"default_filename_is_given", 646 "http://www.google.com/test",
771 L"default_filename_is_given" 647 "",
772 }, 648 "",
773 { 649 "suggested",
774 __LINE__, 650 "",
775 "data:,looks/like/a.path", 651 L"",
776 "", 652 L"suggested"},
777 "", 653 {// The content-disposition has higher precedence over the suggested name.
778 "", 654 __LINE__,
779 "", 655 "http://www.google.com/test",
780 L"\u65e5\u672c\u8a9e", // Japanese Kanji. 656 "attachment; filename=test.html",
781 L"\u65e5\u672c\u8a9e" 657 "",
782 }, 658 "suggested",
783 { // The filename encoding is specified by the referrer charset. 659 "",
784 __LINE__, 660 L"",
785 "http://example.com/V%FDvojov%E1%20psychologie.doc", 661 L"test.html"},
786 "",
787 "iso-8859-1",
788 "",
789 "",
790 L"",
791 L"V\u00fdvojov\u00e1 psychologie.doc"
792 },
793 { // Suggested filename takes precedence over URL
794 __LINE__,
795 "http://www.google.com/test",
796 "",
797 "",
798 "suggested",
799 "",
800 L"",
801 L"suggested"
802 },
803 { // The content-disposition has higher precedence over the suggested name.
804 __LINE__,
805 "http://www.google.com/test",
806 "attachment; filename=test.html",
807 "",
808 "suggested",
809 "",
810 L"",
811 L"test.html"
812 },
813 #if 0 662 #if 0
814 { // The filename encoding doesn't match the referrer charset, the system 663 { // The filename encoding doesn't match the referrer charset, the system
815 // charset, or UTF-8. 664 // charset, or UTF-8.
816 // TODO(jshin): we need to handle this case. 665 // TODO(jshin): we need to handle this case.
817 __LINE__, 666 __LINE__,
818 "http://example.com/V%FDvojov%E1%20psychologie.doc", 667 "http://example.com/V%FDvojov%E1%20psychologie.doc",
819 "", 668 "",
820 "utf-8", 669 "utf-8",
821 "", 670 "",
822 "", 671 "",
823 L"", 672 L"",
824 L"V\u00fdvojov\u00e1 psychologie.doc", 673 L"V\u00fdvojov\u00e1 psychologie.doc",
825 }, 674 },
826 #endif 675 #endif
827 // Raw 8bit characters in C-D 676 // Raw 8bit characters in C-D
828 { 677 {__LINE__,
829 __LINE__, 678 "http://www.example.com/images?id=3",
830 "http://www.example.com/images?id=3", 679 "attachment; filename=caf\xc3\xa9.png",
831 "attachment; filename=caf\xc3\xa9.png", 680 "iso-8859-1",
832 "iso-8859-1", 681 "",
833 "", 682 "image/png",
834 "image/png", 683 L"",
835 L"", 684 L"caf\u00e9.png"},
836 L"caf\u00e9.png" 685 {__LINE__,
837 }, 686 "http://www.example.com/images?id=3",
838 { 687 "attachment; filename=caf\xe5.png",
839 __LINE__, 688 "windows-1253",
840 "http://www.example.com/images?id=3", 689 "",
841 "attachment; filename=caf\xe5.png", 690 "image/png",
842 "windows-1253", 691 L"",
843 "", 692 L"caf\u03b5.png"},
844 "image/png", 693 {// No 'filename' keyword in the disposition, use the URL
845 L"", 694 __LINE__,
846 L"caf\u03b5.png" 695 "http://www.evil.com/my_download.txt",
847 }, 696 "a_file_name.txt",
848 { // No 'filename' keyword in the disposition, use the URL 697 "",
849 __LINE__, 698 "",
850 "http://www.evil.com/my_download.txt", 699 "text/plain",
851 "a_file_name.txt", 700 L"download",
852 "", 701 L"my_download.txt"},
853 "", 702 {// Spaces in the disposition file name
854 "text/plain", 703 __LINE__,
855 L"download", 704 "http://www.frontpagehacker.com/a_download.exe",
856 L"my_download.txt" 705 "filename=My Downloaded File.exe",
857 }, 706 "",
858 { // Spaces in the disposition file name 707 "",
859 __LINE__, 708 "application/octet-stream",
860 "http://www.frontpagehacker.com/a_download.exe", 709 L"download",
861 "filename=My Downloaded File.exe", 710 L"My Downloaded File.exe"},
862 "", 711 {// % encoded
863 "", 712 __LINE__,
864 "application/octet-stream", 713 "http://www.examples.com/",
865 L"download", 714 "attachment; "
866 L"My Downloaded File.exe" 715 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"",
867 }, 716 "",
868 { // % encoded 717 "",
869 __LINE__, 718 "image/jpeg",
870 "http://www.examples.com/", 719 L"download",
871 "attachment; " 720 L"\uc608\uc220 \uc608\uc220.jpg"},
872 "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"", 721 {// name= parameter
873 "", 722 __LINE__,
874 "", 723 "http://www.examples.com/q.cgi?id=abc",
875 "image/jpeg", 724 "attachment; name=abc de.pdf",
876 L"download", 725 "",
877 L"\uc608\uc220 \uc608\uc220.jpg" 726 "",
878 }, 727 "application/octet-stream",
879 { // name= parameter 728 L"download",
880 __LINE__, 729 L"abc de.pdf"},
881 "http://www.examples.com/q.cgi?id=abc", 730 {__LINE__,
882 "attachment; name=abc de.pdf", 731 "http://www.example.com/path",
883 "", 732 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"",
884 "", 733 "",
885 "application/octet-stream", 734 "",
886 L"download", 735 "image/png",
887 L"abc de.pdf" 736 L"download",
888 }, 737 L"\x82b8\x8853"
889 { 738 L"3.png"},
890 __LINE__, 739 {// The following two have invalid CD headers and filenames come from the
891 "http://www.example.com/path", 740 // URL.
892 "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"", 741 __LINE__,
893 "", 742 "http://www.example.com/test%20123",
894 "", 743 "attachment; filename==?iiso88591?Q?caf=EG?=",
895 "image/png", 744 "",
896 L"download", 745 "",
897 L"\x82b8\x8853" L"3.png" 746 "image/jpeg",
898 }, 747 L"download",
899 { // The following two have invalid CD headers and filenames come from the 748 L"test 123" JPEG_EXT},
900 // URL. 749 {__LINE__,
901 __LINE__, 750 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
902 "http://www.example.com/test%20123", 751 "malformed_disposition",
903 "attachment; filename==?iiso88591?Q?caf=EG?=", 752 "",
904 "", 753 "",
905 "", 754 "image/jpeg",
906 "image/jpeg", 755 L"download",
907 L"download", 756 L"\uc608\uc220 \uc608\uc220.jpg"},
908 L"test 123" JPEG_EXT 757 {// Invalid C-D. No filename from URL. Falls back to 'download'.
909 }, 758 __LINE__,
910 { 759 "http://www.google.com/path1/path2/",
911 __LINE__, 760 "attachment; filename==?iso88591?Q?caf=E3?",
912 "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", 761 "",
913 "malformed_disposition", 762 "",
914 "", 763 "image/jpeg",
915 "", 764 L"download",
916 "image/jpeg", 765 L"download" JPEG_EXT},
917 L"download",
918 L"\uc608\uc220 \uc608\uc220.jpg"
919 },
920 { // Invalid C-D. No filename from URL. Falls back to 'download'.
921 __LINE__,
922 "http://www.google.com/path1/path2/",
923 "attachment; filename==?iso88591?Q?caf=E3?",
924 "",
925 "",
926 "image/jpeg",
927 L"download",
928 L"download" JPEG_EXT
929 },
930 }; 766 };
931 767
932 // Tests filename generation. Once the correct filename is 768 // Tests filename generation. Once the correct filename is
933 // selected, they should be passed through the validation steps and 769 // selected, they should be passed through the validation steps and
934 // a correct extension should be added if necessary. 770 // a correct extension should be added if necessary.
935 const GenerateFilenameCase generation_tests[] = { 771 const GenerateFilenameCase generation_tests[] = {
936 // Dotfiles. Ensures preceeding period(s) stripped. 772 // Dotfiles. Ensures preceeding period(s) stripped.
937 { 773 {__LINE__,
938 __LINE__, 774 "http://www.google.com/.test.html",
939 "http://www.google.com/.test.html", 775 "",
940 "", 776 "",
941 "", 777 "",
942 "", 778 "",
943 "", 779 L"",
944 L"", 780 L"test.html"},
945 L"test.html" 781 {__LINE__, "http://www.google.com/.test", "", "", "", "", L"", L"test"},
946 }, 782 {__LINE__, "http://www.google.com/..test", "", "", "", "", L"", L"test"},
947 { 783 {// Disposition has relative paths, remove directory separators
948 __LINE__, 784 __LINE__,
949 "http://www.google.com/.test", 785 "http://www.evil.com/my_download.txt",
950 "", 786 "filename=../../../../././../a_file_name.txt",
951 "", 787 "",
952 "", 788 "",
953 "", 789 "text/plain",
954 L"", 790 L"download",
955 L"test" 791 L"-..-..-..-.-.-..-a_file_name.txt"},
956 }, 792 {// Disposition has parent directories, remove directory separators
957 { 793 __LINE__,
958 __LINE__, 794 "http://www.evil.com/my_download.txt",
959 "http://www.google.com/..test", 795 "filename=dir1/dir2/a_file_name.txt",
960 "", 796 "",
961 "", 797 "",
962 "", 798 "text/plain",
963 "", 799 L"download",
964 L"", 800 L"dir1-dir2-a_file_name.txt"},
965 L"test" 801 {// Disposition has relative paths, remove directory separators
966 }, 802 __LINE__,
967 { // Disposition has relative paths, remove directory separators 803 "http://www.evil.com/my_download.txt",
968 __LINE__, 804 "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt",
969 "http://www.evil.com/my_download.txt", 805 "",
970 "filename=../../../../././../a_file_name.txt", 806 "",
971 "", 807 "text/plain",
972 "", 808 L"download",
973 "text/plain", 809 L"-..-..-..-.-.-..-a_file_name.txt"},
974 L"download", 810 {// Disposition has parent directories, remove directory separators
975 L"-..-..-..-.-.-..-a_file_name.txt" 811 __LINE__,
976 }, 812 "http://www.evil.com/my_download.txt",
977 { // Disposition has parent directories, remove directory separators 813 "filename=dir1\\dir2\\a_file_name.txt",
978 __LINE__, 814 "",
979 "http://www.evil.com/my_download.txt", 815 "",
980 "filename=dir1/dir2/a_file_name.txt", 816 "text/plain",
981 "", 817 L"download",
982 "", 818 L"dir1-dir2-a_file_name.txt"},
983 "text/plain", 819 {// No useful information in disposition or URL, use default
984 L"download", 820 __LINE__,
985 L"dir1-dir2-a_file_name.txt" 821 "http://www.truncated.com/path/",
986 }, 822 "",
987 { // Disposition has relative paths, remove directory separators 823 "",
988 __LINE__, 824 "",
989 "http://www.evil.com/my_download.txt", 825 "text/plain",
990 "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt", 826 L"download",
991 "", 827 L"download" TXT_EXT},
992 "", 828 {// Filename looks like HTML?
993 "text/plain", 829 __LINE__,
994 L"download", 830 "http://www.evil.com/get/malware/here",
995 L"-..-..-..-.-.-..-a_file_name.txt" 831 "filename=\"<blink>Hello kitty</blink>\"",
996 }, 832 "",
997 { // Disposition has parent directories, remove directory separators 833 "",
998 __LINE__, 834 "text/plain",
999 "http://www.evil.com/my_download.txt", 835 L"default",
1000 "filename=dir1\\dir2\\a_file_name.txt", 836 L"-blink-Hello kitty--blink-" TXT_EXT},
1001 "", 837 {// A normal avi should get .avi and not .avi.avi
1002 "", 838 __LINE__,
1003 "text/plain", 839 "https://blah.google.com/misc/2.avi",
1004 L"download", 840 "",
1005 L"dir1-dir2-a_file_name.txt" 841 "",
1006 }, 842 "",
1007 { // No useful information in disposition or URL, use default 843 "video/x-msvideo",
1008 __LINE__, 844 L"download",
1009 "http://www.truncated.com/path/", 845 L"2.avi"},
1010 "", 846 {// Extension generation
1011 "", 847 __LINE__,
1012 "", 848 "http://www.example.com/my-cat",
1013 "text/plain", 849 "filename=my-cat",
1014 L"download", 850 "",
1015 L"download" TXT_EXT 851 "",
1016 }, 852 "image/jpeg",
1017 { // Filename looks like HTML? 853 L"download",
1018 __LINE__, 854 L"my-cat" JPEG_EXT},
1019 "http://www.evil.com/get/malware/here", 855 {__LINE__,
1020 "filename=\"<blink>Hello kitty</blink>\"", 856 "http://www.example.com/my-cat",
1021 "", 857 "filename=my-cat",
1022 "", 858 "",
1023 "text/plain", 859 "",
1024 L"default", 860 "text/plain",
1025 L"-blink-Hello kitty--blink-" TXT_EXT 861 L"download",
1026 }, 862 L"my-cat.txt"},
1027 { // A normal avi should get .avi and not .avi.avi 863 {__LINE__,
1028 __LINE__, 864 "http://www.example.com/my-cat",
1029 "https://blah.google.com/misc/2.avi", 865 "filename=my-cat",
1030 "", 866 "",
1031 "", 867 "",
1032 "", 868 "text/html",
1033 "video/x-msvideo", 869 L"download",
1034 L"download", 870 L"my-cat" HTML_EXT},
1035 L"2.avi" 871 {// Unknown MIME type
1036 }, 872 __LINE__,
1037 { // Extension generation 873 "http://www.example.com/my-cat",
1038 __LINE__, 874 "filename=my-cat",
1039 "http://www.example.com/my-cat", 875 "",
1040 "filename=my-cat", 876 "",
1041 "", 877 "dance/party",
1042 "", 878 L"download",
1043 "image/jpeg", 879 L"my-cat"},
1044 L"download", 880 {__LINE__,
1045 L"my-cat" JPEG_EXT 881 "http://www.example.com/my-cat.jpg",
1046 }, 882 "filename=my-cat.jpg",
1047 { 883 "",
1048 __LINE__, 884 "",
1049 "http://www.example.com/my-cat", 885 "text/plain",
1050 "filename=my-cat", 886 L"download",
1051 "", 887 L"my-cat.jpg"},
1052 "", 888 // Windows specific tests
1053 "text/plain", 889 #if defined(OS_WIN)
1054 L"download", 890 {__LINE__,
1055 L"my-cat.txt" 891 "http://www.goodguy.com/evil.exe",
1056 }, 892 "filename=evil.exe",
1057 { 893 "",
1058 __LINE__, 894 "",
1059 "http://www.example.com/my-cat", 895 "image/jpeg",
1060 "filename=my-cat", 896 L"download",
1061 "", 897 L"evil.exe"},
1062 "", 898 {__LINE__,
1063 "text/html", 899 "http://www.goodguy.com/ok.exe",
1064 L"download", 900 "filename=ok.exe",
1065 L"my-cat" HTML_EXT 901 "",
1066 }, 902 "",
1067 { // Unknown MIME type 903 "binary/octet-stream",
1068 __LINE__, 904 L"download",
1069 "http://www.example.com/my-cat", 905 L"ok.exe"},
1070 "filename=my-cat", 906 {__LINE__,
1071 "", 907 "http://www.goodguy.com/evil.dll",
1072 "", 908 "filename=evil.dll",
1073 "dance/party", 909 "",
1074 L"download", 910 "",
1075 L"my-cat" 911 "dance/party",
1076 }, 912 L"download",
1077 { 913 L"evil.dll"},
1078 __LINE__, 914 {__LINE__,
1079 "http://www.example.com/my-cat.jpg", 915 "http://www.goodguy.com/evil.exe",
1080 "filename=my-cat.jpg", 916 "filename=evil",
1081 "", 917 "",
1082 "", 918 "",
1083 "text/plain", 919 "application/rss+xml",
1084 L"download", 920 L"download",
1085 L"my-cat.jpg" 921 L"evil"},
1086 },
1087 // Windows specific tests
1088 #if defined(OS_WIN)
1089 {
1090 __LINE__,
1091 "http://www.goodguy.com/evil.exe",
1092 "filename=evil.exe",
1093 "",
1094 "",
1095 "image/jpeg",
1096 L"download",
1097 L"evil.exe"
1098 },
1099 {
1100 __LINE__,
1101 "http://www.goodguy.com/ok.exe",
1102 "filename=ok.exe",
1103 "",
1104 "",
1105 "binary/octet-stream",
1106 L"download",
1107 L"ok.exe"
1108 },
1109 {
1110 __LINE__,
1111 "http://www.goodguy.com/evil.dll",
1112 "filename=evil.dll",
1113 "",
1114 "",
1115 "dance/party",
1116 L"download",
1117 L"evil.dll"
1118 },
1119 {
1120 __LINE__,
1121 "http://www.goodguy.com/evil.exe",
1122 "filename=evil",
1123 "",
1124 "",
1125 "application/rss+xml",
1126 L"download",
1127 L"evil"
1128 },
1129 // Test truncation of trailing dots and spaces 922 // Test truncation of trailing dots and spaces
1130 { 923 {__LINE__,
1131 __LINE__, 924 "http://www.goodguy.com/evil.exe ",
1132 "http://www.goodguy.com/evil.exe ", 925 "filename=evil.exe ",
1133 "filename=evil.exe ", 926 "",
1134 "", 927 "",
1135 "", 928 "binary/octet-stream",
1136 "binary/octet-stream", 929 L"download",
1137 L"download", 930 L"evil.exe"},
1138 L"evil.exe" 931 {__LINE__,
1139 }, 932 "http://www.goodguy.com/evil.exe.",
1140 { 933 "filename=evil.exe.",
1141 __LINE__, 934 "",
1142 "http://www.goodguy.com/evil.exe.", 935 "",
1143 "filename=evil.exe.", 936 "binary/octet-stream",
1144 "", 937 L"download",
1145 "", 938 L"evil.exe-"},
1146 "binary/octet-stream", 939 {__LINE__,
1147 L"download", 940 "http://www.goodguy.com/evil.exe. . .",
1148 L"evil.exe-" 941 "filename=evil.exe. . .",
1149 }, 942 "",
1150 { 943 "",
1151 __LINE__, 944 "binary/octet-stream",
1152 "http://www.goodguy.com/evil.exe. . .", 945 L"download",
1153 "filename=evil.exe. . .", 946 L"evil.exe-------"},
1154 "", 947 {__LINE__,
1155 "", 948 "http://www.goodguy.com/evil.",
1156 "binary/octet-stream", 949 "filename=evil.",
1157 L"download", 950 "",
1158 L"evil.exe-------" 951 "",
1159 }, 952 "binary/octet-stream",
1160 { 953 L"download",
1161 __LINE__, 954 L"evil-"},
1162 "http://www.goodguy.com/evil.", 955 {__LINE__,
1163 "filename=evil.", 956 "http://www.goodguy.com/. . . . .",
1164 "", 957 "filename=. . . . .",
1165 "", 958 "",
1166 "binary/octet-stream", 959 "",
1167 L"download", 960 "binary/octet-stream",
1168 L"evil-" 961 L"download",
1169 }, 962 L"download"},
1170 { 963 {__LINE__,
1171 __LINE__, 964 "http://www.badguy.com/attachment?name=meh.exe%C2%A0",
1172 "http://www.goodguy.com/. . . . .", 965 "attachment; filename=\"meh.exe\xC2\xA0\"",
1173 "filename=. . . . .", 966 "",
1174 "", 967 "",
1175 "", 968 "binary/octet-stream",
1176 "binary/octet-stream", 969 L"",
1177 L"download", 970 L"meh.exe-"},
1178 L"download"
1179 },
1180 {
1181 __LINE__,
1182 "http://www.badguy.com/attachment?name=meh.exe%C2%A0",
1183 "attachment; filename=\"meh.exe\xC2\xA0\"",
1184 "",
1185 "",
1186 "binary/octet-stream",
1187 L"",
1188 L"meh.exe-"
1189 },
1190 #endif // OS_WIN 971 #endif // OS_WIN
1191 { 972 {__LINE__,
1192 __LINE__, 973 "http://www.goodguy.com/utils.js",
1193 "http://www.goodguy.com/utils.js", 974 "filename=utils.js",
1194 "filename=utils.js", 975 "",
1195 "", 976 "",
1196 "", 977 "application/x-javascript",
1197 "application/x-javascript", 978 L"download",
1198 L"download", 979 L"utils.js"},
1199 L"utils.js" 980 {__LINE__,
1200 }, 981 "http://www.goodguy.com/contacts.js",
1201 { 982 "filename=contacts.js",
1202 __LINE__, 983 "",
1203 "http://www.goodguy.com/contacts.js", 984 "",
1204 "filename=contacts.js", 985 "application/json",
1205 "", 986 L"download",
1206 "", 987 L"contacts.js"},
1207 "application/json", 988 {__LINE__,
1208 L"download", 989 "http://www.goodguy.com/utils.js",
1209 L"contacts.js" 990 "filename=utils.js",
1210 }, 991 "",
1211 { 992 "",
1212 __LINE__, 993 "text/javascript",
1213 "http://www.goodguy.com/utils.js", 994 L"download",
1214 "filename=utils.js", 995 L"utils.js"},
1215 "", 996 {__LINE__,
1216 "", 997 "http://www.goodguy.com/utils.js",
1217 "text/javascript", 998 "filename=utils.js",
1218 L"download", 999 "",
1219 L"utils.js" 1000 "",
1220 }, 1001 "text/javascript;version=2",
1221 { 1002 L"download",
1222 __LINE__, 1003 L"utils.js"},
1223 "http://www.goodguy.com/utils.js", 1004 {__LINE__,
1224 "filename=utils.js", 1005 "http://www.goodguy.com/utils.js",
1225 "", 1006 "filename=utils.js",
1226 "", 1007 "",
1227 "text/javascript;version=2", 1008 "",
1228 L"download", 1009 "application/ecmascript",
1229 L"utils.js" 1010 L"download",
1230 }, 1011 L"utils.js"},
1231 { 1012 {__LINE__,
1232 __LINE__, 1013 "http://www.goodguy.com/utils.js",
1233 "http://www.goodguy.com/utils.js", 1014 "filename=utils.js",
1234 "filename=utils.js", 1015 "",
1235 "", 1016 "",
1236 "", 1017 "application/ecmascript;version=4",
1237 "application/ecmascript", 1018 L"download",
1238 L"download", 1019 L"utils.js"},
1239 L"utils.js" 1020 {__LINE__,
1240 }, 1021 "http://www.goodguy.com/program.exe",
1241 { 1022 "filename=program.exe",
1242 __LINE__, 1023 "",
1243 "http://www.goodguy.com/utils.js", 1024 "",
1244 "filename=utils.js", 1025 "application/foo-bar",
1245 "", 1026 L"download",
1246 "", 1027 L"program.exe"},
1247 "application/ecmascript;version=4", 1028 {__LINE__,
1248 L"download", 1029 "http://www.evil.com/../foo.txt",
1249 L"utils.js" 1030 "filename=../foo.txt",
1250 }, 1031 "",
1251 { 1032 "",
1252 __LINE__, 1033 "text/plain",
1253 "http://www.goodguy.com/program.exe", 1034 L"download",
1254 "filename=program.exe", 1035 L"-foo.txt"},
1255 "", 1036 {__LINE__,
1256 "", 1037 "http://www.evil.com/..\\foo.txt",
1257 "application/foo-bar", 1038 "filename=..\\foo.txt",
1258 L"download", 1039 "",
1259 L"program.exe" 1040 "",
1260 }, 1041 "text/plain",
1261 { 1042 L"download",
1262 __LINE__, 1043 L"-foo.txt"},
1263 "http://www.evil.com/../foo.txt", 1044 {__LINE__,
1264 "filename=../foo.txt", 1045 "http://www.evil.com/.hidden",
1265 "", 1046 "filename=.hidden",
1266 "", 1047 "",
1267 "text/plain", 1048 "",
1268 L"download", 1049 "text/plain",
1269 L"-foo.txt" 1050 L"download",
1270 }, 1051 L"hidden" TXT_EXT},
1271 { 1052 {__LINE__,
1272 __LINE__, 1053 "http://www.evil.com/trailing.",
1273 "http://www.evil.com/..\\foo.txt", 1054 "filename=trailing.",
1274 "filename=..\\foo.txt", 1055 "",
1275 "", 1056 "",
1276 "", 1057 "dance/party",
1277 "text/plain", 1058 L"download",
1278 L"download", 1059 #if defined(OS_WIN)
1279 L"-foo.txt" 1060 L"trailing-"
1280 }, 1061 #else
1281 { 1062 L"trailing"
1282 __LINE__, 1063 #endif
1283 "http://www.evil.com/.hidden", 1064 },
1284 "filename=.hidden", 1065 {__LINE__,
1285 "", 1066 "http://www.evil.com/trailing.",
1286 "", 1067 "filename=trailing.",
1287 "text/plain", 1068 "",
1288 L"download", 1069 "",
1289 L"hidden" TXT_EXT 1070 "text/plain",
1290 }, 1071 L"download",
1291 { 1072 #if defined(OS_WIN)
1292 __LINE__, 1073 L"trailing-" TXT_EXT
1293 "http://www.evil.com/trailing.", 1074 #else
1294 "filename=trailing.", 1075 L"trailing" TXT_EXT
1295 "", 1076 #endif
1296 "", 1077 },
1297 "dance/party", 1078 {__LINE__,
1298 L"download", 1079 "http://www.evil.com/.",
1299 #if defined(OS_WIN) 1080 "filename=.",
1300 L"trailing-" 1081 "",
1301 #else 1082 "",
1302 L"trailing" 1083 "dance/party",
1303 #endif 1084 L"download",
1304 }, 1085 L"download"},
1305 { 1086 {__LINE__,
1306 __LINE__, 1087 "http://www.evil.com/..",
1307 "http://www.evil.com/trailing.", 1088 "filename=..",
1308 "filename=trailing.", 1089 "",
1309 "", 1090 "",
1310 "", 1091 "dance/party",
1311 "text/plain", 1092 L"download",
1312 L"download", 1093 L"download"},
1313 #if defined(OS_WIN) 1094 {__LINE__,
1314 L"trailing-" TXT_EXT 1095 "http://www.evil.com/...",
1315 #else 1096 "filename=...",
1316 L"trailing" TXT_EXT 1097 "",
1317 #endif 1098 "",
1318 }, 1099 "dance/party",
1319 { 1100 L"download",
1320 __LINE__, 1101 L"download"},
1321 "http://www.evil.com/.", 1102 {// Note that this one doesn't have "filename=" on it.
1322 "filename=.", 1103 __LINE__,
1323 "", 1104 "http://www.evil.com/",
1324 "", 1105 "a_file_name.txt",
1325 "dance/party", 1106 "",
1326 L"download", 1107 "",
1327 L"download" 1108 "image/jpeg",
1328 }, 1109 L"download",
1329 { 1110 L"download" JPEG_EXT},
1330 __LINE__, 1111 {__LINE__,
1331 "http://www.evil.com/..", 1112 "http://www.evil.com/",
1332 "filename=..", 1113 "filename=",
1333 "", 1114 "",
1334 "", 1115 "",
1335 "dance/party", 1116 "image/jpeg",
1336 L"download", 1117 L"download",
1337 L"download" 1118 L"download" JPEG_EXT},
1338 }, 1119 {__LINE__,
1339 { 1120 "http://www.example.com/simple",
1340 __LINE__, 1121 "filename=simple",
1341 "http://www.evil.com/...", 1122 "",
1342 "filename=...", 1123 "",
1343 "", 1124 "application/octet-stream",
1344 "", 1125 L"download",
1345 "dance/party", 1126 L"simple"},
1346 L"download",
1347 L"download"
1348 },
1349 { // Note that this one doesn't have "filename=" on it.
1350 __LINE__,
1351 "http://www.evil.com/",
1352 "a_file_name.txt",
1353 "",
1354 "",
1355 "image/jpeg",
1356 L"download",
1357 L"download" JPEG_EXT
1358 },
1359 {
1360 __LINE__,
1361 "http://www.evil.com/",
1362 "filename=",
1363 "",
1364 "",
1365 "image/jpeg",
1366 L"download",
1367 L"download" JPEG_EXT
1368 },
1369 {
1370 __LINE__,
1371 "http://www.example.com/simple",
1372 "filename=simple",
1373 "",
1374 "",
1375 "application/octet-stream",
1376 L"download",
1377 L"simple"
1378 },
1379 // Reserved words on Windows 1127 // Reserved words on Windows
1380 { 1128 {__LINE__,
1381 __LINE__, 1129 "http://www.goodguy.com/COM1",
1382 "http://www.goodguy.com/COM1", 1130 "filename=COM1",
1383 "filename=COM1", 1131 "",
1384 "", 1132 "",
1385 "", 1133 "application/foo-bar",
1386 "application/foo-bar", 1134 L"download",
1387 L"download", 1135 #if defined(OS_WIN)
1388 #if defined(OS_WIN) 1136 L"_COM1"
1389 L"_COM1" 1137 #else
1390 #else 1138 L"COM1"
1391 L"COM1" 1139 #endif
1392 #endif 1140 },
1393 }, 1141 {__LINE__,
1394 { 1142 "http://www.goodguy.com/COM4.txt",
1395 __LINE__, 1143 "filename=COM4.txt",
1396 "http://www.goodguy.com/COM4.txt", 1144 "",
1397 "filename=COM4.txt", 1145 "",
1398 "", 1146 "text/plain",
1399 "", 1147 L"download",
1400 "text/plain", 1148 #if defined(OS_WIN)
1401 L"download", 1149 L"_COM4.txt"
1402 #if defined(OS_WIN) 1150 #else
1403 L"_COM4.txt" 1151 L"COM4.txt"
1404 #else 1152 #endif
1405 L"COM4.txt" 1153 },
1406 #endif 1154 {__LINE__,
1407 }, 1155 "http://www.goodguy.com/lpt1.TXT",
1408 { 1156 "filename=lpt1.TXT",
1409 __LINE__, 1157 "",
1410 "http://www.goodguy.com/lpt1.TXT", 1158 "",
1411 "filename=lpt1.TXT", 1159 "text/plain",
1412 "", 1160 L"download",
1413 "", 1161 #if defined(OS_WIN)
1414 "text/plain", 1162 L"_lpt1.TXT"
1415 L"download", 1163 #else
1416 #if defined(OS_WIN) 1164 L"lpt1.TXT"
1417 L"_lpt1.TXT" 1165 #endif
1418 #else 1166 },
1419 L"lpt1.TXT" 1167 {__LINE__,
1420 #endif 1168 "http://www.goodguy.com/clock$.txt",
1421 }, 1169 "filename=clock$.txt",
1422 { 1170 "",
1423 __LINE__, 1171 "",
1424 "http://www.goodguy.com/clock$.txt", 1172 "text/plain",
1425 "filename=clock$.txt", 1173 L"download",
1426 "", 1174 #if defined(OS_WIN)
1427 "", 1175 L"_clock$.txt"
1428 "text/plain", 1176 #else
1429 L"download", 1177 L"clock$.txt"
1430 #if defined(OS_WIN) 1178 #endif
1431 L"_clock$.txt" 1179 },
1432 #else 1180 {// Validation should also apply to sugested name
1433 L"clock$.txt" 1181 __LINE__,
1434 #endif 1182 "http://www.goodguy.com/blah$.txt",
1435 }, 1183 "filename=clock$.txt",
1436 { // Validation should also apply to sugested name 1184 "",
1437 __LINE__, 1185 "clock$.txt",
1438 "http://www.goodguy.com/blah$.txt", 1186 "text/plain",
1439 "filename=clock$.txt", 1187 L"download",
1440 "", 1188 #if defined(OS_WIN)
1441 "clock$.txt", 1189 L"_clock$.txt"
1442 "text/plain", 1190 #else
1443 L"download", 1191 L"clock$.txt"
1444 #if defined(OS_WIN) 1192 #endif
1445 L"_clock$.txt" 1193 },
1446 #else 1194 {__LINE__,
1447 L"clock$.txt" 1195 "http://www.goodguy.com/mycom1.foo",
1448 #endif 1196 "filename=mycom1.foo",
1449 }, 1197 "",
1450 { 1198 "",
1451 __LINE__, 1199 "text/plain",
1452 "http://www.goodguy.com/mycom1.foo", 1200 L"download",
1453 "filename=mycom1.foo", 1201 L"mycom1.foo"},
1454 "", 1202 {__LINE__,
1455 "", 1203 "http://www.badguy.com/Setup.exe.local",
1456 "text/plain", 1204 "filename=Setup.exe.local",
1457 L"download", 1205 "",
1458 L"mycom1.foo" 1206 "",
1459 }, 1207 "application/foo-bar",
1460 { 1208 L"download",
1461 __LINE__, 1209 #if defined(OS_WIN)
1462 "http://www.badguy.com/Setup.exe.local", 1210 L"Setup.exe.download"
1463 "filename=Setup.exe.local", 1211 #else
1464 "", 1212 L"Setup.exe.local"
1465 "", 1213 #endif
1466 "application/foo-bar", 1214 },
1467 L"download", 1215 {__LINE__,
1468 #if defined(OS_WIN) 1216 "http://www.badguy.com/Setup.exe.local",
1469 L"Setup.exe.download" 1217 "filename=Setup.exe.local.local",
1470 #else 1218 "",
1471 L"Setup.exe.local" 1219 "",
1472 #endif 1220 "application/foo-bar",
1473 }, 1221 L"download",
1474 { 1222 #if defined(OS_WIN)
1475 __LINE__, 1223 L"Setup.exe.local.download"
1476 "http://www.badguy.com/Setup.exe.local", 1224 #else
1477 "filename=Setup.exe.local.local", 1225 L"Setup.exe.local.local"
1478 "", 1226 #endif
1479 "", 1227 },
1480 "application/foo-bar", 1228 {__LINE__,
1481 L"download", 1229 "http://www.badguy.com/Setup.exe.lnk",
1482 #if defined(OS_WIN) 1230 "filename=Setup.exe.lnk",
1483 L"Setup.exe.local.download" 1231 "",
1484 #else 1232 "",
1485 L"Setup.exe.local.local" 1233 "application/foo-bar",
1486 #endif 1234 L"download",
1487 }, 1235 #if defined(OS_WIN)
1488 { 1236 L"Setup.exe.download"
1489 __LINE__, 1237 #else
1490 "http://www.badguy.com/Setup.exe.lnk", 1238 L"Setup.exe.lnk"
1491 "filename=Setup.exe.lnk", 1239 #endif
1492 "", 1240 },
1493 "", 1241 {__LINE__,
1494 "application/foo-bar", 1242 "http://www.badguy.com/Desktop.ini",
1495 L"download", 1243 "filename=Desktop.ini",
1496 #if defined(OS_WIN) 1244 "",
1497 L"Setup.exe.download" 1245 "",
1498 #else 1246 "application/foo-bar",
1499 L"Setup.exe.lnk" 1247 L"download",
1500 #endif 1248 #if defined(OS_WIN)
1501 }, 1249 L"_Desktop.ini"
1502 { 1250 #else
1503 __LINE__, 1251 L"Desktop.ini"
1504 "http://www.badguy.com/Desktop.ini", 1252 #endif
1505 "filename=Desktop.ini", 1253 },
1506 "", 1254 {__LINE__,
1507 "", 1255 "http://www.badguy.com/Thumbs.db",
1508 "application/foo-bar", 1256 "filename=Thumbs.db",
1509 L"download", 1257 "",
1510 #if defined(OS_WIN) 1258 "",
1511 L"_Desktop.ini" 1259 "application/foo-bar",
1512 #else 1260 L"download",
1513 L"Desktop.ini" 1261 #if defined(OS_WIN)
1514 #endif 1262 L"_Thumbs.db"
1515 }, 1263 #else
1516 { 1264 L"Thumbs.db"
1517 __LINE__, 1265 #endif
1518 "http://www.badguy.com/Thumbs.db", 1266 },
1519 "filename=Thumbs.db", 1267 {__LINE__,
1520 "", 1268 "http://www.hotmail.com",
1521 "", 1269 "filename=source.jpg",
1522 "application/foo-bar", 1270 "",
1523 L"download", 1271 "",
1524 #if defined(OS_WIN) 1272 "application/x-javascript",
1525 L"_Thumbs.db" 1273 L"download",
1526 #else 1274 L"source.jpg"},
1527 L"Thumbs.db" 1275 {// http://crbug.com/5772.
1528 #endif 1276 __LINE__,
1529 }, 1277 "http://www.example.com/foo.tar.gz",
1530 { 1278 "",
1531 __LINE__, 1279 "",
1532 "http://www.hotmail.com", 1280 "",
1533 "filename=source.jpg", 1281 "application/x-tar",
1534 "", 1282 L"download",
1535 "", 1283 L"foo.tar.gz"},
1536 "application/x-javascript", 1284 {// http://crbug.com/52250.
1537 L"download", 1285 __LINE__,
1538 L"source.jpg" 1286 "http://www.example.com/foo.tgz",
1539 }, 1287 "",
1540 { // http://crbug.com/5772. 1288 "",
1541 __LINE__, 1289 "",
1542 "http://www.example.com/foo.tar.gz", 1290 "application/x-tar",
1543 "", 1291 L"download",
1544 "", 1292 L"foo.tgz"},
1545 "", 1293 {// http://crbug.com/7337.
1546 "application/x-tar", 1294 __LINE__,
1547 L"download", 1295 "http://maged.lordaeron.org/blank.reg",
1548 L"foo.tar.gz" 1296 "",
1549 }, 1297 "",
1550 { // http://crbug.com/52250. 1298 "",
1551 __LINE__, 1299 "text/x-registry",
1552 "http://www.example.com/foo.tgz", 1300 L"download",
1553 "", 1301 L"blank.reg"},
1554 "", 1302 {__LINE__,
1555 "", 1303 "http://www.example.com/bar.tar",
1556 "application/x-tar", 1304 "",
1557 L"download", 1305 "",
1558 L"foo.tgz" 1306 "",
1559 }, 1307 "application/x-tar",
1560 { // http://crbug.com/7337. 1308 L"download",
1561 __LINE__, 1309 L"bar.tar"},
1562 "http://maged.lordaeron.org/blank.reg", 1310 {__LINE__,
1563 "", 1311 "http://www.example.com/bar.bogus",
1564 "", 1312 "",
1565 "", 1313 "",
1566 "text/x-registry", 1314 "",
1567 L"download", 1315 "application/x-tar",
1568 L"blank.reg" 1316 L"download",
1569 }, 1317 L"bar.bogus"},
1570 { 1318 {// http://crbug.com/20337
1571 __LINE__, 1319 __LINE__,
1572 "http://www.example.com/bar.tar", 1320 "http://www.example.com/.download.txt",
1573 "", 1321 "filename=.download.txt",
1574 "", 1322 "",
1575 "", 1323 "",
1576 "application/x-tar", 1324 "text/plain",
1577 L"download", 1325 L"-download",
1578 L"bar.tar" 1326 L"download.txt"},
1579 }, 1327 {// http://crbug.com/56855.
1580 { 1328 __LINE__,
1581 __LINE__, 1329 "http://www.example.com/bar.sh",
1582 "http://www.example.com/bar.bogus", 1330 "",
1583 "", 1331 "",
1584 "", 1332 "",
1585 "", 1333 "application/x-sh",
1586 "application/x-tar", 1334 L"download",
1587 L"download", 1335 L"bar.sh"},
1588 L"bar.bogus" 1336 {// http://crbug.com/61571
1589 }, 1337 __LINE__,
1590 { // http://crbug.com/20337 1338 "http://www.example.com/npdf.php?fn=foobar.pdf",
1591 __LINE__, 1339 "",
1592 "http://www.example.com/.download.txt", 1340 "",
1593 "filename=.download.txt", 1341 "",
1594 "", 1342 "text/plain",
1595 "", 1343 L"download",
1596 "text/plain", 1344 L"npdf" TXT_EXT},
1597 L"-download", 1345 {// Shouldn't overwrite C-D specified extension.
1598 L"download.txt" 1346 __LINE__,
1599 }, 1347 "http://www.example.com/npdf.php?fn=foobar.pdf",
1600 { // http://crbug.com/56855. 1348 "filename=foobar.jpg",
1601 __LINE__, 1349 "",
1602 "http://www.example.com/bar.sh", 1350 "",
1603 "", 1351 "text/plain",
1604 "", 1352 L"download",
1605 "", 1353 L"foobar.jpg"},
1606 "application/x-sh", 1354 {// http://crbug.com/87719
1607 L"download", 1355 __LINE__,
1608 L"bar.sh" 1356 "http://www.example.com/image.aspx?id=blargh",
1609 }, 1357 "",
1610 { // http://crbug.com/61571 1358 "",
1611 __LINE__, 1359 "",
1612 "http://www.example.com/npdf.php?fn=foobar.pdf", 1360 "image/jpeg",
1613 "", 1361 L"download",
1614 "", 1362 L"image" JPEG_EXT},
1615 "",
1616 "text/plain",
1617 L"download",
1618 L"npdf" TXT_EXT
1619 },
1620 { // Shouldn't overwrite C-D specified extension.
1621 __LINE__,
1622 "http://www.example.com/npdf.php?fn=foobar.pdf",
1623 "filename=foobar.jpg",
1624 "",
1625 "",
1626 "text/plain",
1627 L"download",
1628 L"foobar.jpg"
1629 },
1630 { // http://crbug.com/87719
1631 __LINE__,
1632 "http://www.example.com/image.aspx?id=blargh",
1633 "",
1634 "",
1635 "",
1636 "image/jpeg",
1637 L"download",
1638 L"image" JPEG_EXT
1639 },
1640 #if defined(OS_CHROMEOS) 1363 #if defined(OS_CHROMEOS)
1641 { // http://crosbug.com/26028 1364 {// http://crosbug.com/26028
1642 __LINE__, 1365 __LINE__,
1643 "http://www.example.com/fooa%cc%88.txt", 1366 "http://www.example.com/fooa%cc%88.txt",
1644 "", 1367 "",
1645 "", 1368 "",
1646 "", 1369 "",
1647 "image/jpeg", 1370 "image/jpeg",
1648 L"foo\xe4", 1371 L"foo\xe4",
1649 L"foo\xe4.txt" 1372 L"foo\xe4.txt"},
1650 },
1651 #endif 1373 #endif
1652 }; 1374 };
1653 1375
1654 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(selection_tests); ++i) 1376 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(selection_tests); ++i)
1655 RunGenerateFileNameTestCase(&selection_tests[i]); 1377 RunGenerateFileNameTestCase(&selection_tests[i]);
1656 1378
1657 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generation_tests); ++i) 1379 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generation_tests); ++i)
1658 RunGenerateFileNameTestCase(&generation_tests[i]); 1380 RunGenerateFileNameTestCase(&generation_tests[i]);
1659 1381
1660 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generation_tests); ++i) { 1382 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generation_tests); ++i) {
1661 GenerateFilenameCase test_case = generation_tests[i]; 1383 GenerateFilenameCase test_case = generation_tests[i];
1662 test_case.referrer_charset = "GBK"; 1384 test_case.referrer_charset = "GBK";
1663 RunGenerateFileNameTestCase(&test_case); 1385 RunGenerateFileNameTestCase(&test_case);
1664 } 1386 }
1665 } 1387 }
1666 1388
1667 } // namespace net 1389 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698