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

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

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

Powered by Google App Engine
This is Rietveld 408576698