OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shellapi.h> | 9 #include <shellapi.h> |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
11 #include <tchar.h> | 11 #include <tchar.h> |
12 #include <winioctl.h> | 12 #include <winioctl.h> |
13 #endif | 13 #endif |
14 | 14 |
15 #include <algorithm> | 15 #include <algorithm> |
16 #include <fstream> | 16 #include <fstream> |
17 #include <set> | 17 #include <set> |
18 | 18 |
19 #include "base/base_paths.h" | 19 #include "base/base_paths.h" |
20 #include "base/file_path.h" | 20 #include "base/file_path.h" |
21 #include "base/file_util.h" | 21 #include "base/file_util.h" |
22 #include "base/path_service.h" | 22 #include "base/path_service.h" |
23 #include "base/scoped_temp_dir.h" | 23 #include "base/scoped_temp_dir.h" |
| 24 #include "base/test/test_file_util.h" |
24 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
25 #include "base/utf_string_conversions.h" | 26 #include "base/utf_string_conversions.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 #include "testing/platform_test.h" | 28 #include "testing/platform_test.h" |
28 | 29 |
29 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
30 #include "base/win/scoped_handle.h" | 31 #include "base/win/scoped_handle.h" |
31 #endif | 32 #endif |
32 | 33 |
33 // This macro helps avoid wrapped lines in the test structs. | 34 // This macro helps avoid wrapped lines in the test structs. |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. | 620 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. |
620 NULL)); | 621 NULL)); |
621 ASSERT_TRUE(dir.IsValid()); | 622 ASSERT_TRUE(dir.IsValid()); |
622 base::PlatformFileInfo info; | 623 base::PlatformFileInfo info; |
623 EXPECT_TRUE(base::GetPlatformFileInfo(dir.Get(), &info)); | 624 EXPECT_TRUE(base::GetPlatformFileInfo(dir.Get(), &info)); |
624 EXPECT_TRUE(info.is_directory); | 625 EXPECT_TRUE(info.is_directory); |
625 EXPECT_FALSE(info.is_symbolic_link); | 626 EXPECT_FALSE(info.is_symbolic_link); |
626 EXPECT_EQ(0, info.size); | 627 EXPECT_EQ(0, info.size); |
627 } | 628 } |
628 | 629 |
| 630 TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) { |
| 631 // Test that CreateTemporaryFileInDir() creates a path and returns a long path |
| 632 // if it is available. This test requires that: |
| 633 // - the filesystem at |temp_dir_| supports long filenames. |
| 634 // - the account has FILE_LIST_DIRECTORY permission for all ancestor |
| 635 // directories of |temp_dir_|. |
| 636 const FilePath::CharType kLongDirName[] = FPL("A long path"); |
| 637 const FilePath::CharType kTestSubDirName[] = FPL("test"); |
| 638 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName); |
| 639 ASSERT_TRUE(file_util::CreateDirectory(long_test_dir)); |
| 640 |
| 641 // kLongDirName is not a 8.3 component. So GetShortName() should give us a |
| 642 // different short name. |
| 643 WCHAR path_buffer[MAX_PATH]; |
| 644 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(), |
| 645 path_buffer, MAX_PATH); |
| 646 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH)); |
| 647 ASSERT_NE(DWORD(0), path_buffer_length); |
| 648 FilePath short_test_dir(path_buffer); |
| 649 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str()); |
| 650 |
| 651 FilePath temp_file; |
| 652 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir, &temp_file)); |
| 653 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str()); |
| 654 EXPECT_TRUE(file_util::PathExists(temp_file)); |
| 655 |
| 656 // Create a subdirectory of |long_test_dir| and make |long_test_dir| |
| 657 // unreadable. We should still be able to create a temp file in the |
| 658 // subdirectory, but we won't be able to determine the long path for it. This |
| 659 // mimics the environment that some users run where their user profiles reside |
| 660 // in a location where the don't have full access to the higher level |
| 661 // directories. (Note that this assumption is true for NTFS, but not for some |
| 662 // network file systems. E.g. AFS). |
| 663 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName); |
| 664 ASSERT_TRUE(file_util::CreateDirectory(access_test_dir)); |
| 665 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir); |
| 666 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir)); |
| 667 |
| 668 // Use the short form of the directory to create a temporary filename. |
| 669 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( |
| 670 short_test_dir.Append(kTestSubDirName), &temp_file)); |
| 671 EXPECT_TRUE(file_util::PathExists(temp_file)); |
| 672 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName())); |
| 673 |
| 674 // Check that the long path can't be determined for |temp_file|. |
| 675 path_buffer_length = GetLongPathName(temp_file.value().c_str(), |
| 676 path_buffer, MAX_PATH); |
| 677 EXPECT_EQ(DWORD(0), path_buffer_length); |
| 678 } |
| 679 |
629 #endif // defined(OS_WIN) | 680 #endif // defined(OS_WIN) |
630 | 681 |
631 #if defined(OS_POSIX) | 682 #if defined(OS_POSIX) |
632 | 683 |
633 TEST_F(FileUtilTest, CreateAndReadSymlinks) { | 684 TEST_F(FileUtilTest, CreateAndReadSymlinks) { |
634 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); | 685 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); |
635 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); | 686 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); |
636 CreateTextFile(link_to, bogus_content); | 687 CreateTextFile(link_to, bogus_content); |
637 | 688 |
638 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 689 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
(...skipping 1750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2389 file_util::VerifyPathControlledByUser( | 2440 file_util::VerifyPathControlledByUser( |
2390 base_dir_, text_file_, uid_, ok_gids_)); | 2441 base_dir_, text_file_, uid_, ok_gids_)); |
2391 EXPECT_TRUE( | 2442 EXPECT_TRUE( |
2392 file_util::VerifyPathControlledByUser( | 2443 file_util::VerifyPathControlledByUser( |
2393 sub_dir_, text_file_, uid_, ok_gids_)); | 2444 sub_dir_, text_file_, uid_, ok_gids_)); |
2394 } | 2445 } |
2395 | 2446 |
2396 #endif // defined(OS_POSIX) | 2447 #endif // defined(OS_POSIX) |
2397 | 2448 |
2398 } // namespace | 2449 } // namespace |
OLD | NEW |