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 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); | 690 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
691 EXPECT_FALSE(PathExists(file_name)); | 691 EXPECT_FALSE(PathExists(file_name)); |
692 | 692 |
693 const std::string kData("hello"); | 693 const std::string kData("hello"); |
694 | 694 |
695 int buffer_size = kData.length(); | 695 int buffer_size = kData.length(); |
696 char* buffer = new char[buffer_size]; | 696 char* buffer = new char[buffer_size]; |
697 | 697 |
698 // Write file. | 698 // Write file. |
699 EXPECT_EQ(static_cast<int>(kData.length()), | 699 EXPECT_EQ(static_cast<int>(kData.length()), |
700 file_util::WriteFile(file_name, kData.data(), kData.length())); | 700 WriteFile(file_name, kData.data(), kData.length())); |
701 EXPECT_TRUE(PathExists(file_name)); | 701 EXPECT_TRUE(PathExists(file_name)); |
702 | 702 |
703 // Make sure the file is readable. | 703 // Make sure the file is readable. |
704 int32 mode = 0; | 704 int32 mode = 0; |
705 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 705 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
706 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER); | 706 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER); |
707 | 707 |
708 // Get rid of the read permission. | 708 // Get rid of the read permission. |
709 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); | 709 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); |
710 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 710 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
(...skipping 18 matching lines...) Expand all Loading... |
729 | 729 |
730 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { | 730 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { |
731 // Create a file path. | 731 // Create a file path. |
732 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); | 732 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
733 EXPECT_FALSE(PathExists(file_name)); | 733 EXPECT_FALSE(PathExists(file_name)); |
734 | 734 |
735 const std::string kData("hello"); | 735 const std::string kData("hello"); |
736 | 736 |
737 // Write file. | 737 // Write file. |
738 EXPECT_EQ(static_cast<int>(kData.length()), | 738 EXPECT_EQ(static_cast<int>(kData.length()), |
739 file_util::WriteFile(file_name, kData.data(), kData.length())); | 739 WriteFile(file_name, kData.data(), kData.length())); |
740 EXPECT_TRUE(PathExists(file_name)); | 740 EXPECT_TRUE(PathExists(file_name)); |
741 | 741 |
742 // Make sure the file is writable. | 742 // Make sure the file is writable. |
743 int mode = 0; | 743 int mode = 0; |
744 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 744 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
745 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER); | 745 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER); |
746 EXPECT_TRUE(PathIsWritable(file_name)); | 746 EXPECT_TRUE(PathIsWritable(file_name)); |
747 | 747 |
748 // Get rid of the write permission. | 748 // Get rid of the write permission. |
749 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); | 749 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); |
750 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 750 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
751 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER); | 751 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER); |
752 // Make sure the file can't be write. | 752 // Make sure the file can't be write. |
753 EXPECT_EQ(-1, | 753 EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length())); |
754 file_util::WriteFile(file_name, kData.data(), kData.length())); | |
755 EXPECT_FALSE(PathIsWritable(file_name)); | 754 EXPECT_FALSE(PathIsWritable(file_name)); |
756 | 755 |
757 // Give read permission. | 756 // Give read permission. |
758 EXPECT_TRUE(SetPosixFilePermissions(file_name, | 757 EXPECT_TRUE(SetPosixFilePermissions(file_name, |
759 FILE_PERMISSION_WRITE_BY_USER)); | 758 FILE_PERMISSION_WRITE_BY_USER)); |
760 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); | 759 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); |
761 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER); | 760 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER); |
762 // Make sure the file can be write. | 761 // Make sure the file can be write. |
763 EXPECT_EQ(static_cast<int>(kData.length()), | 762 EXPECT_EQ(static_cast<int>(kData.length()), |
764 file_util::WriteFile(file_name, kData.data(), kData.length())); | 763 WriteFile(file_name, kData.data(), kData.length())); |
765 EXPECT_TRUE(PathIsWritable(file_name)); | 764 EXPECT_TRUE(PathIsWritable(file_name)); |
766 | 765 |
767 // Delete the file. | 766 // Delete the file. |
768 EXPECT_TRUE(DeleteFile(file_name, false)); | 767 EXPECT_TRUE(DeleteFile(file_name, false)); |
769 EXPECT_FALSE(PathExists(file_name)); | 768 EXPECT_FALSE(PathExists(file_name)); |
770 } | 769 } |
771 | 770 |
772 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { | 771 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { |
773 // Create a directory path. | 772 // Create a directory path. |
774 FilePath subdir_path = | 773 FilePath subdir_path = |
775 temp_dir_.path().Append(FPL("PermissionTest1")); | 774 temp_dir_.path().Append(FPL("PermissionTest1")); |
776 CreateDirectory(subdir_path); | 775 CreateDirectory(subdir_path); |
777 ASSERT_TRUE(PathExists(subdir_path)); | 776 ASSERT_TRUE(PathExists(subdir_path)); |
778 | 777 |
779 // Create a dummy file to enumerate. | 778 // Create a dummy file to enumerate. |
780 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); | 779 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); |
781 EXPECT_FALSE(PathExists(file_name)); | 780 EXPECT_FALSE(PathExists(file_name)); |
782 const std::string kData("hello"); | 781 const std::string kData("hello"); |
783 EXPECT_EQ(static_cast<int>(kData.length()), | 782 EXPECT_EQ(static_cast<int>(kData.length()), |
784 file_util::WriteFile(file_name, kData.data(), kData.length())); | 783 WriteFile(file_name, kData.data(), kData.length())); |
785 EXPECT_TRUE(PathExists(file_name)); | 784 EXPECT_TRUE(PathExists(file_name)); |
786 | 785 |
787 // Make sure the directory has the all permissions. | 786 // Make sure the directory has the all permissions. |
788 int mode = 0; | 787 int mode = 0; |
789 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode)); | 788 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode)); |
790 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK); | 789 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK); |
791 | 790 |
792 // Get rid of the permissions from the directory. | 791 // Get rid of the permissions from the directory. |
793 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u)); | 792 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u)); |
794 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode)); | 793 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode)); |
(...skipping 1142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1937 // Create a fresh, empty copy of this directory. | 1936 // Create a fresh, empty copy of this directory. |
1938 if (PathExists(data_dir)) { | 1937 if (PathExists(data_dir)) { |
1939 ASSERT_TRUE(DeleteFile(data_dir, true)); | 1938 ASSERT_TRUE(DeleteFile(data_dir, true)); |
1940 } | 1939 } |
1941 ASSERT_TRUE(CreateDirectory(data_dir)); | 1940 ASSERT_TRUE(CreateDirectory(data_dir)); |
1942 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); | 1941 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
1943 | 1942 |
1944 std::string data("hello"); | 1943 std::string data("hello"); |
1945 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length())); | 1944 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length())); |
1946 EXPECT_EQ(static_cast<int>(data.length()), | 1945 EXPECT_EQ(static_cast<int>(data.length()), |
1947 file_util::WriteFile(foobar, data.c_str(), data.length())); | 1946 WriteFile(foobar, data.c_str(), data.length())); |
1948 EXPECT_EQ(static_cast<int>(data.length()), | 1947 EXPECT_EQ(static_cast<int>(data.length()), |
1949 file_util::AppendToFile(foobar, data.c_str(), data.length())); | 1948 file_util::AppendToFile(foobar, data.c_str(), data.length())); |
1950 | 1949 |
1951 const std::wstring read_content = ReadTextFile(foobar); | 1950 const std::wstring read_content = ReadTextFile(foobar); |
1952 EXPECT_EQ(L"hellohello", read_content); | 1951 EXPECT_EQ(L"hellohello", read_content); |
1953 } | 1952 } |
1954 | 1953 |
1955 TEST_F(FileUtilTest, ReadFileToString) { | 1954 TEST_F(FileUtilTest, ReadFileToString) { |
1956 const char kTestData[] = "0123"; | 1955 const char kTestData[] = "0123"; |
1957 std::string data; | 1956 std::string data; |
1958 | 1957 |
1959 FilePath file_path = | 1958 FilePath file_path = |
1960 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest")); | 1959 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest")); |
1961 | 1960 |
1962 ASSERT_EQ(4, file_util::WriteFile(file_path, kTestData, 4)); | 1961 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4)); |
1963 | 1962 |
1964 EXPECT_TRUE(ReadFileToString(file_path, &data)); | 1963 EXPECT_TRUE(ReadFileToString(file_path, &data)); |
1965 EXPECT_EQ(kTestData, data); | 1964 EXPECT_EQ(kTestData, data); |
1966 | 1965 |
1967 data = "temp"; | 1966 data = "temp"; |
1968 EXPECT_FALSE(ReadFileToString(file_path, &data, 0)); | 1967 EXPECT_FALSE(ReadFileToString(file_path, &data, 0)); |
1969 EXPECT_EQ(data.length(), 0u); | 1968 EXPECT_EQ(data.length(), 0u); |
1970 | 1969 |
1971 data = "temp"; | 1970 data = "temp"; |
1972 EXPECT_FALSE(ReadFileToString(file_path, &data, 2)); | 1971 EXPECT_FALSE(ReadFileToString(file_path, &data, 2)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2004 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); | 2003 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); |
2005 | 2004 |
2006 // Create a fresh, empty copy of this directory. | 2005 // Create a fresh, empty copy of this directory. |
2007 if (PathExists(data_dir)) { | 2006 if (PathExists(data_dir)) { |
2008 ASSERT_TRUE(DeleteFile(data_dir, true)); | 2007 ASSERT_TRUE(DeleteFile(data_dir, true)); |
2009 } | 2008 } |
2010 ASSERT_TRUE(CreateDirectory(data_dir)); | 2009 ASSERT_TRUE(CreateDirectory(data_dir)); |
2011 | 2010 |
2012 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); | 2011 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); |
2013 std::string data("hello"); | 2012 std::string data("hello"); |
2014 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); | 2013 ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length())); |
2015 | 2014 |
2016 Time access_time; | 2015 Time access_time; |
2017 // This timestamp is divisible by one day (in local timezone), | 2016 // This timestamp is divisible by one day (in local timezone), |
2018 // to make it work on FAT too. | 2017 // to make it work on FAT too. |
2019 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00", | 2018 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00", |
2020 &access_time)); | 2019 &access_time)); |
2021 | 2020 |
2022 Time modification_time; | 2021 Time modification_time; |
2023 // Note that this timestamp is divisible by two (seconds) - FAT stores | 2022 // Note that this timestamp is divisible by two (seconds) - FAT stores |
2024 // modification times with 2s resolution. | 2023 // modification times with 2s resolution. |
(...skipping 13 matching lines...) Expand all Loading... |
2038 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir")); | 2037 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir")); |
2039 | 2038 |
2040 ASSERT_FALSE(PathExists(empty_dir)); | 2039 ASSERT_FALSE(PathExists(empty_dir)); |
2041 | 2040 |
2042 ASSERT_TRUE(CreateDirectory(empty_dir)); | 2041 ASSERT_TRUE(CreateDirectory(empty_dir)); |
2043 | 2042 |
2044 EXPECT_TRUE(IsDirectoryEmpty(empty_dir)); | 2043 EXPECT_TRUE(IsDirectoryEmpty(empty_dir)); |
2045 | 2044 |
2046 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); | 2045 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); |
2047 std::string bar("baz"); | 2046 std::string bar("baz"); |
2048 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length())); | 2047 ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length())); |
2049 | 2048 |
2050 EXPECT_FALSE(IsDirectoryEmpty(empty_dir)); | 2049 EXPECT_FALSE(IsDirectoryEmpty(empty_dir)); |
2051 } | 2050 } |
2052 | 2051 |
2053 #if defined(OS_POSIX) | 2052 #if defined(OS_POSIX) |
2054 | 2053 |
2055 // Testing VerifyPathControlledByAdmin() is hard, because there is no | 2054 // Testing VerifyPathControlledByAdmin() is hard, because there is no |
2056 // way a test can make a file owned by root, or change file paths | 2055 // way a test can make a file owned by root, or change file paths |
2057 // at the root of the file system. VerifyPathControlledByAdmin() | 2056 // at the root of the file system. VerifyPathControlledByAdmin() |
2058 // is implemented as a call to VerifyPathControlledByUser, which gives | 2057 // is implemented as a call to VerifyPathControlledByUser, which gives |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2461 int fd = OpenContentUriForRead(path); | 2460 int fd = OpenContentUriForRead(path); |
2462 EXPECT_EQ(-1, fd); | 2461 EXPECT_EQ(-1, fd); |
2463 } | 2462 } |
2464 #endif | 2463 #endif |
2465 | 2464 |
2466 #endif // defined(OS_POSIX) | 2465 #endif // defined(OS_POSIX) |
2467 | 2466 |
2468 } // namespace | 2467 } // namespace |
2469 | 2468 |
2470 } // namespace base | 2469 } // namespace base |
OLD | NEW |