| 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> |
| (...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); | 696 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
| 697 EXPECT_FALSE(PathExists(file_name)); | 697 EXPECT_FALSE(PathExists(file_name)); |
| 698 | 698 |
| 699 const std::string kData("hello"); | 699 const std::string kData("hello"); |
| 700 | 700 |
| 701 int buffer_size = kData.length(); | 701 int buffer_size = kData.length(); |
| 702 char* buffer = new char[buffer_size]; | 702 char* buffer = new char[buffer_size]; |
| 703 | 703 |
| 704 // Write file. | 704 // Write file. |
| 705 EXPECT_EQ(static_cast<int>(kData.length()), | 705 EXPECT_EQ(static_cast<int>(kData.length()), |
| 706 file_util::WriteFile(file_name, kData.data(), kData.length())); | 706 WriteFile(file_name, kData.data(), kData.length())); |
| 707 EXPECT_TRUE(PathExists(file_name)); | 707 EXPECT_TRUE(PathExists(file_name)); |
| 708 | 708 |
| 709 // Make sure the file is readable. | 709 // Make sure the file is readable. |
| 710 int32 mode = 0; | 710 int32 mode = 0; |
| 711 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 711 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
| 712 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER); | 712 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER); |
| 713 | 713 |
| 714 // Get rid of the read permission. | 714 // Get rid of the read permission. |
| 715 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); | 715 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); |
| 716 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 716 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 735 | 735 |
| 736 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { | 736 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { |
| 737 // Create a file path. | 737 // Create a file path. |
| 738 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); | 738 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
| 739 EXPECT_FALSE(PathExists(file_name)); | 739 EXPECT_FALSE(PathExists(file_name)); |
| 740 | 740 |
| 741 const std::string kData("hello"); | 741 const std::string kData("hello"); |
| 742 | 742 |
| 743 // Write file. | 743 // Write file. |
| 744 EXPECT_EQ(static_cast<int>(kData.length()), | 744 EXPECT_EQ(static_cast<int>(kData.length()), |
| 745 file_util::WriteFile(file_name, kData.data(), kData.length())); | 745 WriteFile(file_name, kData.data(), kData.length())); |
| 746 EXPECT_TRUE(PathExists(file_name)); | 746 EXPECT_TRUE(PathExists(file_name)); |
| 747 | 747 |
| 748 // Make sure the file is writable. | 748 // Make sure the file is writable. |
| 749 int mode = 0; | 749 int mode = 0; |
| 750 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 750 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
| 751 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER); | 751 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER); |
| 752 EXPECT_TRUE(PathIsWritable(file_name)); | 752 EXPECT_TRUE(PathIsWritable(file_name)); |
| 753 | 753 |
| 754 // Get rid of the write permission. | 754 // Get rid of the write permission. |
| 755 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); | 755 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); |
| 756 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 756 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
| 757 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER); | 757 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER); |
| 758 // Make sure the file can't be write. | 758 // Make sure the file can't be write. |
| 759 EXPECT_EQ(-1, | 759 EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length())); |
| 760 file_util::WriteFile(file_name, kData.data(), kData.length())); | |
| 761 EXPECT_FALSE(PathIsWritable(file_name)); | 760 EXPECT_FALSE(PathIsWritable(file_name)); |
| 762 | 761 |
| 763 // Give read permission. | 762 // Give read permission. |
| 764 EXPECT_TRUE(SetPosixFilePermissions(file_name, | 763 EXPECT_TRUE(SetPosixFilePermissions(file_name, |
| 765 FILE_PERMISSION_WRITE_BY_USER)); | 764 FILE_PERMISSION_WRITE_BY_USER)); |
| 766 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 765 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
| 767 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER); | 766 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER); |
| 768 // Make sure the file can be write. | 767 // Make sure the file can be write. |
| 769 EXPECT_EQ(static_cast<int>(kData.length()), | 768 EXPECT_EQ(static_cast<int>(kData.length()), |
| 770 file_util::WriteFile(file_name, kData.data(), kData.length())); | 769 WriteFile(file_name, kData.data(), kData.length())); |
| 771 EXPECT_TRUE(PathIsWritable(file_name)); | 770 EXPECT_TRUE(PathIsWritable(file_name)); |
| 772 | 771 |
| 773 // Delete the file. | 772 // Delete the file. |
| 774 EXPECT_TRUE(DeleteFile(file_name, false)); | 773 EXPECT_TRUE(DeleteFile(file_name, false)); |
| 775 EXPECT_FALSE(PathExists(file_name)); | 774 EXPECT_FALSE(PathExists(file_name)); |
| 776 } | 775 } |
| 777 | 776 |
| 778 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { | 777 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { |
| 779 // Create a directory path. | 778 // Create a directory path. |
| 780 FilePath subdir_path = | 779 FilePath subdir_path = |
| 781 temp_dir_.path().Append(FPL("PermissionTest1")); | 780 temp_dir_.path().Append(FPL("PermissionTest1")); |
| 782 CreateDirectory(subdir_path); | 781 CreateDirectory(subdir_path); |
| 783 ASSERT_TRUE(PathExists(subdir_path)); | 782 ASSERT_TRUE(PathExists(subdir_path)); |
| 784 | 783 |
| 785 // Create a dummy file to enumerate. | 784 // Create a dummy file to enumerate. |
| 786 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); | 785 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); |
| 787 EXPECT_FALSE(PathExists(file_name)); | 786 EXPECT_FALSE(PathExists(file_name)); |
| 788 const std::string kData("hello"); | 787 const std::string kData("hello"); |
| 789 EXPECT_EQ(static_cast<int>(kData.length()), | 788 EXPECT_EQ(static_cast<int>(kData.length()), |
| 790 file_util::WriteFile(file_name, kData.data(), kData.length())); | 789 WriteFile(file_name, kData.data(), kData.length())); |
| 791 EXPECT_TRUE(PathExists(file_name)); | 790 EXPECT_TRUE(PathExists(file_name)); |
| 792 | 791 |
| 793 // Make sure the directory has the all permissions. | 792 // Make sure the directory has the all permissions. |
| 794 int mode = 0; | 793 int mode = 0; |
| 795 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode)); | 794 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode)); |
| 796 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK); | 795 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK); |
| 797 | 796 |
| 798 // Get rid of the permissions from the directory. | 797 // Get rid of the permissions from the directory. |
| 799 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u)); | 798 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u)); |
| 800 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode)); | 799 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode)); |
| (...skipping 1142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1943 // Create a fresh, empty copy of this directory. | 1942 // Create a fresh, empty copy of this directory. |
| 1944 if (PathExists(data_dir)) { | 1943 if (PathExists(data_dir)) { |
| 1945 ASSERT_TRUE(DeleteFile(data_dir, true)); | 1944 ASSERT_TRUE(DeleteFile(data_dir, true)); |
| 1946 } | 1945 } |
| 1947 ASSERT_TRUE(CreateDirectory(data_dir)); | 1946 ASSERT_TRUE(CreateDirectory(data_dir)); |
| 1948 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); | 1947 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 1949 | 1948 |
| 1950 std::string data("hello"); | 1949 std::string data("hello"); |
| 1951 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length())); | 1950 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length())); |
| 1952 EXPECT_EQ(static_cast<int>(data.length()), | 1951 EXPECT_EQ(static_cast<int>(data.length()), |
| 1953 file_util::WriteFile(foobar, data.c_str(), data.length())); | 1952 WriteFile(foobar, data.c_str(), data.length())); |
| 1954 EXPECT_EQ(static_cast<int>(data.length()), | 1953 EXPECT_EQ(static_cast<int>(data.length()), |
| 1955 file_util::AppendToFile(foobar, data.c_str(), data.length())); | 1954 file_util::AppendToFile(foobar, data.c_str(), data.length())); |
| 1956 | 1955 |
| 1957 const std::wstring read_content = ReadTextFile(foobar); | 1956 const std::wstring read_content = ReadTextFile(foobar); |
| 1958 EXPECT_EQ(L"hellohello", read_content); | 1957 EXPECT_EQ(L"hellohello", read_content); |
| 1959 } | 1958 } |
| 1960 | 1959 |
| 1961 TEST_F(FileUtilTest, ReadFileToString) { | 1960 TEST_F(FileUtilTest, ReadFileToString) { |
| 1962 const char kTestData[] = "0123"; | 1961 const char kTestData[] = "0123"; |
| 1963 std::string data; | 1962 std::string data; |
| 1964 | 1963 |
| 1965 FilePath file_path = | 1964 FilePath file_path = |
| 1966 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest")); | 1965 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest")); |
| 1967 | 1966 |
| 1968 ASSERT_EQ(4, file_util::WriteFile(file_path, kTestData, 4)); | 1967 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4)); |
| 1969 | 1968 |
| 1970 EXPECT_TRUE(ReadFileToString(file_path, &data)); | 1969 EXPECT_TRUE(ReadFileToString(file_path, &data)); |
| 1971 EXPECT_EQ(kTestData, data); | 1970 EXPECT_EQ(kTestData, data); |
| 1972 | 1971 |
| 1973 data = "temp"; | 1972 data = "temp"; |
| 1974 EXPECT_FALSE(ReadFileToString(file_path, &data, 0)); | 1973 EXPECT_FALSE(ReadFileToString(file_path, &data, 0)); |
| 1975 EXPECT_EQ(data.length(), 0u); | 1974 EXPECT_EQ(data.length(), 0u); |
| 1976 | 1975 |
| 1977 data = "temp"; | 1976 data = "temp"; |
| 1978 EXPECT_FALSE(ReadFileToString(file_path, &data, 2)); | 1977 EXPECT_FALSE(ReadFileToString(file_path, &data, 2)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2010 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); | 2009 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); |
| 2011 | 2010 |
| 2012 // Create a fresh, empty copy of this directory. | 2011 // Create a fresh, empty copy of this directory. |
| 2013 if (PathExists(data_dir)) { | 2012 if (PathExists(data_dir)) { |
| 2014 ASSERT_TRUE(DeleteFile(data_dir, true)); | 2013 ASSERT_TRUE(DeleteFile(data_dir, true)); |
| 2015 } | 2014 } |
| 2016 ASSERT_TRUE(CreateDirectory(data_dir)); | 2015 ASSERT_TRUE(CreateDirectory(data_dir)); |
| 2017 | 2016 |
| 2018 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); | 2017 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
| 2019 std::string data("hello"); | 2018 std::string data("hello"); |
| 2020 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); | 2019 ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length())); |
| 2021 | 2020 |
| 2022 Time access_time; | 2021 Time access_time; |
| 2023 // This timestamp is divisible by one day (in local timezone), | 2022 // This timestamp is divisible by one day (in local timezone), |
| 2024 // to make it work on FAT too. | 2023 // to make it work on FAT too. |
| 2025 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00", | 2024 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00", |
| 2026 &access_time)); | 2025 &access_time)); |
| 2027 | 2026 |
| 2028 Time modification_time; | 2027 Time modification_time; |
| 2029 // Note that this timestamp is divisible by two (seconds) - FAT stores | 2028 // Note that this timestamp is divisible by two (seconds) - FAT stores |
| 2030 // modification times with 2s resolution. | 2029 // modification times with 2s resolution. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 2044 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir")); | 2043 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir")); |
| 2045 | 2044 |
| 2046 ASSERT_FALSE(PathExists(empty_dir)); | 2045 ASSERT_FALSE(PathExists(empty_dir)); |
| 2047 | 2046 |
| 2048 ASSERT_TRUE(CreateDirectory(empty_dir)); | 2047 ASSERT_TRUE(CreateDirectory(empty_dir)); |
| 2049 | 2048 |
| 2050 EXPECT_TRUE(IsDirectoryEmpty(empty_dir)); | 2049 EXPECT_TRUE(IsDirectoryEmpty(empty_dir)); |
| 2051 | 2050 |
| 2052 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); | 2051 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); |
| 2053 std::string bar("baz"); | 2052 std::string bar("baz"); |
| 2054 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length())); | 2053 ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length())); |
| 2055 | 2054 |
| 2056 EXPECT_FALSE(IsDirectoryEmpty(empty_dir)); | 2055 EXPECT_FALSE(IsDirectoryEmpty(empty_dir)); |
| 2057 } | 2056 } |
| 2058 | 2057 |
| 2059 #if defined(OS_POSIX) | 2058 #if defined(OS_POSIX) |
| 2060 | 2059 |
| 2061 // Testing VerifyPathControlledByAdmin() is hard, because there is no | 2060 // Testing VerifyPathControlledByAdmin() is hard, because there is no |
| 2062 // way a test can make a file owned by root, or change file paths | 2061 // way a test can make a file owned by root, or change file paths |
| 2063 // at the root of the file system. VerifyPathControlledByAdmin() | 2062 // at the root of the file system. VerifyPathControlledByAdmin() |
| 2064 // is implemented as a call to VerifyPathControlledByUser, which gives | 2063 // is implemented as a call to VerifyPathControlledByUser, which gives |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2504 // Trying to close it should crash. This is important for security. | 2503 // Trying to close it should crash. This is important for security. |
| 2505 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); | 2504 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); |
| 2506 #endif | 2505 #endif |
| 2507 } | 2506 } |
| 2508 | 2507 |
| 2509 #endif // defined(OS_POSIX) | 2508 #endif // defined(OS_POSIX) |
| 2510 | 2509 |
| 2511 } // namespace | 2510 } // namespace |
| 2512 | 2511 |
| 2513 } // namespace base | 2512 } // namespace base |
| OLD | NEW |