OLD | NEW |
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 30 matching lines...) Expand all Loading... |
41 #endif | 41 #endif |
42 } | 42 } |
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 std::string GetLocaleWarningString() { |
| 52 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 53 // The generate filename tests can fail on certain OS_POSIX platforms when |
| 54 // LC_CTYPE is not "utf8" or "utf-8" because some of the string conversions |
| 55 // fail. |
| 56 // This warning text is appended to any test failures to save people time if |
| 57 // this happens to be the cause of failure :) |
| 58 // Note: some platforms (MACOSX, Chromecast) don't have this problem: |
| 59 // setlocale returns "c" but it functions as utf8. And Android doesn't |
| 60 // have setlocale at all. |
| 61 std::string locale = setlocale(LC_CTYPE, NULL); |
| 62 return " this test may have failed because the current LC_CTYPE locale is " |
| 63 "not utf8 (currently set to " + |
| 64 locale + ")"; |
| 65 #else |
| 66 return ""; |
| 67 #endif |
| 68 } |
| 69 |
51 void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) { | 70 void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) { |
52 std::string default_filename(base::WideToUTF8(test_case->default_filename)); | 71 std::string default_filename(base::WideToUTF8(test_case->default_filename)); |
53 base::FilePath file_path = GenerateFileName( | 72 base::FilePath file_path = GenerateFileName( |
54 GURL(test_case->url), test_case->content_disp_header, | 73 GURL(test_case->url), test_case->content_disp_header, |
55 test_case->referrer_charset, test_case->suggested_filename, | 74 test_case->referrer_charset, test_case->suggested_filename, |
56 test_case->mime_type, default_filename); | 75 test_case->mime_type, default_filename); |
57 EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path)) | 76 EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path)) |
58 << "test case at line number: " << test_case->lineno; | 77 << "test case at line number: " << test_case->lineno << "; " |
| 78 << GetLocaleWarningString(); |
59 } | 79 } |
60 | 80 |
61 } // namespace | 81 } // namespace |
62 | 82 |
63 static const base::FilePath::CharType* kSafePortableBasenames[] = { | 83 static const base::FilePath::CharType* kSafePortableBasenames[] = { |
64 FILE_PATH_LITERAL("a"), | 84 FILE_PATH_LITERAL("a"), |
65 FILE_PATH_LITERAL("a.txt"), | 85 FILE_PATH_LITERAL("a.txt"), |
66 FILE_PATH_LITERAL("a b.txt"), | 86 FILE_PATH_LITERAL("a b.txt"), |
67 FILE_PATH_LITERAL("a-b.txt"), | 87 FILE_PATH_LITERAL("a-b.txt"), |
68 FILE_PATH_LITERAL("My Computer"), | 88 FILE_PATH_LITERAL("My Computer"), |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 | 431 |
412 for (size_t i = 0; i < arraysize(safe_tests); ++i) { | 432 for (size_t i = 0; i < arraysize(safe_tests); ++i) { |
413 base::FilePath file_path(safe_tests[i].filename); | 433 base::FilePath file_path(safe_tests[i].filename); |
414 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path); | 434 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path); |
415 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value()) | 435 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value()) |
416 << "Iteration " << i; | 436 << "Iteration " << i; |
417 } | 437 } |
418 } | 438 } |
419 | 439 |
420 TEST(FilenameUtilTest, GenerateFileName) { | 440 TEST(FilenameUtilTest, GenerateFileName) { |
421 #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 | |
423 // string conversions fail. This is OK (we have the default value) but they | |
424 // don't match our expectations. | |
425 std::string locale = setlocale(LC_CTYPE, NULL); | |
426 base::StringToLowerASCII(&locale); | |
427 EXPECT_TRUE(locale.find("utf-8") != std::string::npos || | |
428 locale.find("utf8") != std::string::npos) | |
429 << "Your locale (" << locale << ") must be set to UTF-8 " | |
430 << "for this test to pass!"; | |
431 #endif | |
432 | |
433 // Tests whether the correct filename is selected from the the given | 441 // Tests whether the correct filename is selected from the the given |
434 // parameters and that Content-Disposition headers are properly | 442 // parameters and that Content-Disposition headers are properly |
435 // handled including failovers when the header is malformed. | 443 // handled including failovers when the header is malformed. |
436 const GenerateFilenameCase selection_tests[] = { | 444 const GenerateFilenameCase selection_tests[] = { |
437 { | 445 { |
438 __LINE__, | 446 __LINE__, |
439 "http://www.google.com/", | 447 "http://www.google.com/", |
440 "attachment; filename=test.html", | 448 "attachment; filename=test.html", |
441 "", | 449 "", |
442 "", | 450 "", |
(...skipping 1225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1668 RunGenerateFileNameTestCase(&generation_tests[i]); | 1676 RunGenerateFileNameTestCase(&generation_tests[i]); |
1669 | 1677 |
1670 for (size_t i = 0; i < arraysize(generation_tests); ++i) { | 1678 for (size_t i = 0; i < arraysize(generation_tests); ++i) { |
1671 GenerateFilenameCase test_case = generation_tests[i]; | 1679 GenerateFilenameCase test_case = generation_tests[i]; |
1672 test_case.referrer_charset = "GBK"; | 1680 test_case.referrer_charset = "GBK"; |
1673 RunGenerateFileNameTestCase(&test_case); | 1681 RunGenerateFileNameTestCase(&test_case); |
1674 } | 1682 } |
1675 } | 1683 } |
1676 | 1684 |
1677 } // namespace net | 1685 } // namespace net |
OLD | NEW |