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

Side by Side Diff: base/file_util_unittest.cc

Issue 16950028: Move file_util::Delete to the base namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 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
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 720 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 731
732 // Infinite loop! 732 // Infinite loop!
733 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)); 733 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
734 } 734 }
735 #endif // defined(OS_POSIX) 735 #endif // defined(OS_POSIX)
736 736
737 TEST_F(FileUtilTest, DeleteNonExistent) { 737 TEST_F(FileUtilTest, DeleteNonExistent) {
738 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar"); 738 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
739 ASSERT_FALSE(file_util::PathExists(non_existent)); 739 ASSERT_FALSE(file_util::PathExists(non_existent));
740 740
741 EXPECT_TRUE(file_util::Delete(non_existent, false)); 741 EXPECT_TRUE(base::Delete(non_existent, false));
742 ASSERT_FALSE(file_util::PathExists(non_existent)); 742 ASSERT_FALSE(file_util::PathExists(non_existent));
743 EXPECT_TRUE(file_util::Delete(non_existent, true)); 743 EXPECT_TRUE(base::Delete(non_existent, true));
744 ASSERT_FALSE(file_util::PathExists(non_existent)); 744 ASSERT_FALSE(file_util::PathExists(non_existent));
745 } 745 }
746 746
747 TEST_F(FileUtilTest, DeleteFile) { 747 TEST_F(FileUtilTest, DeleteFile) {
748 // Create a file 748 // Create a file
749 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt")); 749 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
750 CreateTextFile(file_name, bogus_content); 750 CreateTextFile(file_name, bogus_content);
751 ASSERT_TRUE(file_util::PathExists(file_name)); 751 ASSERT_TRUE(file_util::PathExists(file_name));
752 752
753 // Make sure it's deleted 753 // Make sure it's deleted
754 EXPECT_TRUE(file_util::Delete(file_name, false)); 754 EXPECT_TRUE(base::Delete(file_name, false));
755 EXPECT_FALSE(file_util::PathExists(file_name)); 755 EXPECT_FALSE(file_util::PathExists(file_name));
756 756
757 // Test recursive case, create a new file 757 // Test recursive case, create a new file
758 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt")); 758 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
759 CreateTextFile(file_name, bogus_content); 759 CreateTextFile(file_name, bogus_content);
760 ASSERT_TRUE(file_util::PathExists(file_name)); 760 ASSERT_TRUE(file_util::PathExists(file_name));
761 761
762 // Make sure it's deleted 762 // Make sure it's deleted
763 EXPECT_TRUE(file_util::Delete(file_name, true)); 763 EXPECT_TRUE(base::Delete(file_name, true));
764 EXPECT_FALSE(file_util::PathExists(file_name)); 764 EXPECT_FALSE(file_util::PathExists(file_name));
765 } 765 }
766 766
767 #if defined(OS_POSIX) 767 #if defined(OS_POSIX)
768 TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) { 768 TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
769 // Create a file. 769 // Create a file.
770 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt")); 770 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
771 CreateTextFile(file_name, bogus_content); 771 CreateTextFile(file_name, bogus_content);
772 ASSERT_TRUE(file_util::PathExists(file_name)); 772 ASSERT_TRUE(file_util::PathExists(file_name));
773 773
774 // Create a symlink to the file. 774 // Create a symlink to the file.
775 FilePath file_link = temp_dir_.path().Append("file_link_2"); 775 FilePath file_link = temp_dir_.path().Append("file_link_2");
776 ASSERT_TRUE(file_util::CreateSymbolicLink(file_name, file_link)) 776 ASSERT_TRUE(file_util::CreateSymbolicLink(file_name, file_link))
777 << "Failed to create symlink."; 777 << "Failed to create symlink.";
778 778
779 // Delete the symbolic link. 779 // Delete the symbolic link.
780 EXPECT_TRUE(file_util::Delete(file_link, false)); 780 EXPECT_TRUE(base::Delete(file_link, false));
781 781
782 // Make sure original file is not deleted. 782 // Make sure original file is not deleted.
783 EXPECT_FALSE(file_util::PathExists(file_link)); 783 EXPECT_FALSE(file_util::PathExists(file_link));
784 EXPECT_TRUE(file_util::PathExists(file_name)); 784 EXPECT_TRUE(file_util::PathExists(file_name));
785 } 785 }
786 786
787 TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) { 787 TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
788 // Create a non-existent file path. 788 // Create a non-existent file path.
789 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt")); 789 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
790 EXPECT_FALSE(file_util::PathExists(non_existent)); 790 EXPECT_FALSE(file_util::PathExists(non_existent));
791 791
792 // Create a symlink to the non-existent file. 792 // Create a symlink to the non-existent file.
793 FilePath file_link = temp_dir_.path().Append("file_link_3"); 793 FilePath file_link = temp_dir_.path().Append("file_link_3");
794 ASSERT_TRUE(file_util::CreateSymbolicLink(non_existent, file_link)) 794 ASSERT_TRUE(file_util::CreateSymbolicLink(non_existent, file_link))
795 << "Failed to create symlink."; 795 << "Failed to create symlink.";
796 796
797 // Make sure the symbolic link is exist. 797 // Make sure the symbolic link is exist.
798 EXPECT_TRUE(file_util::IsLink(file_link)); 798 EXPECT_TRUE(file_util::IsLink(file_link));
799 EXPECT_FALSE(file_util::PathExists(file_link)); 799 EXPECT_FALSE(file_util::PathExists(file_link));
800 800
801 // Delete the symbolic link. 801 // Delete the symbolic link.
802 EXPECT_TRUE(file_util::Delete(file_link, false)); 802 EXPECT_TRUE(base::Delete(file_link, false));
803 803
804 // Make sure the symbolic link is deleted. 804 // Make sure the symbolic link is deleted.
805 EXPECT_FALSE(file_util::IsLink(file_link)); 805 EXPECT_FALSE(file_util::IsLink(file_link));
806 } 806 }
807 807
808 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) { 808 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
809 // Create a file path. 809 // Create a file path.
810 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); 810 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
811 EXPECT_FALSE(file_util::PathExists(file_name)); 811 EXPECT_FALSE(file_util::PathExists(file_name));
812 812
(...skipping 23 matching lines...) Expand all
836 EXPECT_TRUE(file_util::SetPosixFilePermissions( 836 EXPECT_TRUE(file_util::SetPosixFilePermissions(
837 file_name, 837 file_name,
838 file_util::FILE_PERMISSION_READ_BY_USER)); 838 file_util::FILE_PERMISSION_READ_BY_USER));
839 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); 839 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode));
840 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_READ_BY_USER); 840 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_READ_BY_USER);
841 // Make sure the file can be read. 841 // Make sure the file can be read.
842 EXPECT_EQ(static_cast<int>(kData.length()), 842 EXPECT_EQ(static_cast<int>(kData.length()),
843 file_util::ReadFile(file_name, buffer, buffer_size)); 843 file_util::ReadFile(file_name, buffer, buffer_size));
844 844
845 // Delete the file. 845 // Delete the file.
846 EXPECT_TRUE(file_util::Delete(file_name, false)); 846 EXPECT_TRUE(base::Delete(file_name, false));
847 EXPECT_FALSE(file_util::PathExists(file_name)); 847 EXPECT_FALSE(file_util::PathExists(file_name));
848 848
849 delete[] buffer; 849 delete[] buffer;
850 } 850 }
851 851
852 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { 852 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
853 // Create a file path. 853 // Create a file path.
854 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); 854 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
855 EXPECT_FALSE(file_util::PathExists(file_name)); 855 EXPECT_FALSE(file_util::PathExists(file_name));
856 856
(...skipping 24 matching lines...) Expand all
881 file_name, 881 file_name,
882 file_util::FILE_PERMISSION_WRITE_BY_USER)); 882 file_util::FILE_PERMISSION_WRITE_BY_USER));
883 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); 883 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode));
884 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER); 884 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER);
885 // Make sure the file can be write. 885 // Make sure the file can be write.
886 EXPECT_EQ(static_cast<int>(kData.length()), 886 EXPECT_EQ(static_cast<int>(kData.length()),
887 file_util::WriteFile(file_name, kData.data(), kData.length())); 887 file_util::WriteFile(file_name, kData.data(), kData.length()));
888 EXPECT_TRUE(file_util::PathIsWritable(file_name)); 888 EXPECT_TRUE(file_util::PathIsWritable(file_name));
889 889
890 // Delete the file. 890 // Delete the file.
891 EXPECT_TRUE(file_util::Delete(file_name, false)); 891 EXPECT_TRUE(base::Delete(file_name, false));
892 EXPECT_FALSE(file_util::PathExists(file_name)); 892 EXPECT_FALSE(file_util::PathExists(file_name));
893 } 893 }
894 894
895 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { 895 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
896 // Create a directory path. 896 // Create a directory path.
897 FilePath subdir_path = 897 FilePath subdir_path =
898 temp_dir_.path().Append(FPL("PermissionTest1")); 898 temp_dir_.path().Append(FPL("PermissionTest1"));
899 file_util::CreateDirectory(subdir_path); 899 file_util::CreateDirectory(subdir_path);
900 ASSERT_TRUE(file_util::PathExists(subdir_path)); 900 ASSERT_TRUE(file_util::PathExists(subdir_path));
901 901
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, 933 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK,
934 mode & file_util::FILE_PERMISSION_USER_MASK); 934 mode & file_util::FILE_PERMISSION_USER_MASK);
935 935
936 // Make sure the file in the directory can be enumerated. 936 // Make sure the file in the directory can be enumerated.
937 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES); 937 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
938 FindResultCollector c2(f2); 938 FindResultCollector c2(f2);
939 EXPECT_TRUE(c2.HasFile(file_name)); 939 EXPECT_TRUE(c2.HasFile(file_name));
940 EXPECT_EQ(c2.size(), 1); 940 EXPECT_EQ(c2.size(), 1);
941 941
942 // Delete the file. 942 // Delete the file.
943 EXPECT_TRUE(file_util::Delete(subdir_path, true)); 943 EXPECT_TRUE(base::Delete(subdir_path, true));
944 EXPECT_FALSE(file_util::PathExists(subdir_path)); 944 EXPECT_FALSE(file_util::PathExists(subdir_path));
945 } 945 }
946 946
947 #endif // defined(OS_POSIX) 947 #endif // defined(OS_POSIX)
948 948
949 #if defined(OS_WIN) 949 #if defined(OS_WIN)
950 // Tests that the Delete function works for wild cards, especially 950 // Tests that the Delete function works for wild cards, especially
951 // with the recursion flag. Also coincidentally tests PathExists. 951 // with the recursion flag. Also coincidentally tests PathExists.
952 // TODO(erikkay): see if anyone's actually using this feature of the API 952 // TODO(erikkay): see if anyone's actually using this feature of the API
953 TEST_F(FileUtilTest, DeleteWildCard) { 953 TEST_F(FileUtilTest, DeleteWildCard) {
954 // Create a file and a directory 954 // Create a file and a directory
955 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); 955 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
956 CreateTextFile(file_name, bogus_content); 956 CreateTextFile(file_name, bogus_content);
957 ASSERT_TRUE(file_util::PathExists(file_name)); 957 ASSERT_TRUE(file_util::PathExists(file_name));
958 958
959 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir")); 959 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
960 file_util::CreateDirectory(subdir_path); 960 file_util::CreateDirectory(subdir_path);
961 ASSERT_TRUE(file_util::PathExists(subdir_path)); 961 ASSERT_TRUE(file_util::PathExists(subdir_path));
962 962
963 // Create the wildcard path 963 // Create the wildcard path
964 FilePath directory_contents = temp_dir_.path(); 964 FilePath directory_contents = temp_dir_.path();
965 directory_contents = directory_contents.Append(FPL("*")); 965 directory_contents = directory_contents.Append(FPL("*"));
966 966
967 // Delete non-recursively and check that only the file is deleted 967 // Delete non-recursively and check that only the file is deleted
968 EXPECT_TRUE(file_util::Delete(directory_contents, false)); 968 EXPECT_TRUE(base::Delete(directory_contents, false));
969 EXPECT_FALSE(file_util::PathExists(file_name)); 969 EXPECT_FALSE(file_util::PathExists(file_name));
970 EXPECT_TRUE(file_util::PathExists(subdir_path)); 970 EXPECT_TRUE(file_util::PathExists(subdir_path));
971 971
972 // Delete recursively and make sure all contents are deleted 972 // Delete recursively and make sure all contents are deleted
973 EXPECT_TRUE(file_util::Delete(directory_contents, true)); 973 EXPECT_TRUE(base::Delete(directory_contents, true));
974 EXPECT_FALSE(file_util::PathExists(file_name)); 974 EXPECT_FALSE(file_util::PathExists(file_name));
975 EXPECT_FALSE(file_util::PathExists(subdir_path)); 975 EXPECT_FALSE(file_util::PathExists(subdir_path));
976 } 976 }
977 977
978 // TODO(erikkay): see if anyone's actually using this feature of the API 978 // TODO(erikkay): see if anyone's actually using this feature of the API
979 TEST_F(FileUtilTest, DeleteNonExistantWildCard) { 979 TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
980 // Create a file and a directory 980 // Create a file and a directory
981 FilePath subdir_path = 981 FilePath subdir_path =
982 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard")); 982 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
983 file_util::CreateDirectory(subdir_path); 983 file_util::CreateDirectory(subdir_path);
984 ASSERT_TRUE(file_util::PathExists(subdir_path)); 984 ASSERT_TRUE(file_util::PathExists(subdir_path));
985 985
986 // Create the wildcard path 986 // Create the wildcard path
987 FilePath directory_contents = subdir_path; 987 FilePath directory_contents = subdir_path;
988 directory_contents = directory_contents.Append(FPL("*")); 988 directory_contents = directory_contents.Append(FPL("*"));
989 989
990 // Delete non-recursively and check nothing got deleted 990 // Delete non-recursively and check nothing got deleted
991 EXPECT_TRUE(file_util::Delete(directory_contents, false)); 991 EXPECT_TRUE(base::Delete(directory_contents, false));
992 EXPECT_TRUE(file_util::PathExists(subdir_path)); 992 EXPECT_TRUE(file_util::PathExists(subdir_path));
993 993
994 // Delete recursively and check nothing got deleted 994 // Delete recursively and check nothing got deleted
995 EXPECT_TRUE(file_util::Delete(directory_contents, true)); 995 EXPECT_TRUE(base::Delete(directory_contents, true));
996 EXPECT_TRUE(file_util::PathExists(subdir_path)); 996 EXPECT_TRUE(file_util::PathExists(subdir_path));
997 } 997 }
998 #endif 998 #endif
999 999
1000 // Tests non-recursive Delete() for a directory. 1000 // Tests non-recursive Delete() for a directory.
1001 TEST_F(FileUtilTest, DeleteDirNonRecursive) { 1001 TEST_F(FileUtilTest, DeleteDirNonRecursive) {
1002 // Create a subdirectory and put a file and two directories inside. 1002 // Create a subdirectory and put a file and two directories inside.
1003 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive")); 1003 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
1004 file_util::CreateDirectory(test_subdir); 1004 file_util::CreateDirectory(test_subdir);
1005 ASSERT_TRUE(file_util::PathExists(test_subdir)); 1005 ASSERT_TRUE(file_util::PathExists(test_subdir));
1006 1006
1007 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt")); 1007 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
1008 CreateTextFile(file_name, bogus_content); 1008 CreateTextFile(file_name, bogus_content);
1009 ASSERT_TRUE(file_util::PathExists(file_name)); 1009 ASSERT_TRUE(file_util::PathExists(file_name));
1010 1010
1011 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); 1011 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
1012 file_util::CreateDirectory(subdir_path1); 1012 file_util::CreateDirectory(subdir_path1);
1013 ASSERT_TRUE(file_util::PathExists(subdir_path1)); 1013 ASSERT_TRUE(file_util::PathExists(subdir_path1));
1014 1014
1015 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); 1015 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
1016 file_util::CreateDirectory(subdir_path2); 1016 file_util::CreateDirectory(subdir_path2);
1017 ASSERT_TRUE(file_util::PathExists(subdir_path2)); 1017 ASSERT_TRUE(file_util::PathExists(subdir_path2));
1018 1018
1019 // Delete non-recursively and check that the empty dir got deleted 1019 // Delete non-recursively and check that the empty dir got deleted
1020 EXPECT_TRUE(file_util::Delete(subdir_path2, false)); 1020 EXPECT_TRUE(base::Delete(subdir_path2, false));
1021 EXPECT_FALSE(file_util::PathExists(subdir_path2)); 1021 EXPECT_FALSE(file_util::PathExists(subdir_path2));
1022 1022
1023 // Delete non-recursively and check that nothing got deleted 1023 // Delete non-recursively and check that nothing got deleted
1024 EXPECT_FALSE(file_util::Delete(test_subdir, false)); 1024 EXPECT_FALSE(base::Delete(test_subdir, false));
1025 EXPECT_TRUE(file_util::PathExists(test_subdir)); 1025 EXPECT_TRUE(file_util::PathExists(test_subdir));
1026 EXPECT_TRUE(file_util::PathExists(file_name)); 1026 EXPECT_TRUE(file_util::PathExists(file_name));
1027 EXPECT_TRUE(file_util::PathExists(subdir_path1)); 1027 EXPECT_TRUE(file_util::PathExists(subdir_path1));
1028 } 1028 }
1029 1029
1030 // Tests recursive Delete() for a directory. 1030 // Tests recursive Delete() for a directory.
1031 TEST_F(FileUtilTest, DeleteDirRecursive) { 1031 TEST_F(FileUtilTest, DeleteDirRecursive) {
1032 // Create a subdirectory and put a file and two directories inside. 1032 // Create a subdirectory and put a file and two directories inside.
1033 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive")); 1033 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
1034 file_util::CreateDirectory(test_subdir); 1034 file_util::CreateDirectory(test_subdir);
1035 ASSERT_TRUE(file_util::PathExists(test_subdir)); 1035 ASSERT_TRUE(file_util::PathExists(test_subdir));
1036 1036
1037 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt")); 1037 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
1038 CreateTextFile(file_name, bogus_content); 1038 CreateTextFile(file_name, bogus_content);
1039 ASSERT_TRUE(file_util::PathExists(file_name)); 1039 ASSERT_TRUE(file_util::PathExists(file_name));
1040 1040
1041 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); 1041 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
1042 file_util::CreateDirectory(subdir_path1); 1042 file_util::CreateDirectory(subdir_path1);
1043 ASSERT_TRUE(file_util::PathExists(subdir_path1)); 1043 ASSERT_TRUE(file_util::PathExists(subdir_path1));
1044 1044
1045 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); 1045 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
1046 file_util::CreateDirectory(subdir_path2); 1046 file_util::CreateDirectory(subdir_path2);
1047 ASSERT_TRUE(file_util::PathExists(subdir_path2)); 1047 ASSERT_TRUE(file_util::PathExists(subdir_path2));
1048 1048
1049 // Delete recursively and check that the empty dir got deleted 1049 // Delete recursively and check that the empty dir got deleted
1050 EXPECT_TRUE(file_util::Delete(subdir_path2, true)); 1050 EXPECT_TRUE(base::Delete(subdir_path2, true));
1051 EXPECT_FALSE(file_util::PathExists(subdir_path2)); 1051 EXPECT_FALSE(file_util::PathExists(subdir_path2));
1052 1052
1053 // Delete recursively and check that everything got deleted 1053 // Delete recursively and check that everything got deleted
1054 EXPECT_TRUE(file_util::Delete(test_subdir, true)); 1054 EXPECT_TRUE(base::Delete(test_subdir, true));
1055 EXPECT_FALSE(file_util::PathExists(file_name)); 1055 EXPECT_FALSE(file_util::PathExists(file_name));
1056 EXPECT_FALSE(file_util::PathExists(subdir_path1)); 1056 EXPECT_FALSE(file_util::PathExists(subdir_path1));
1057 EXPECT_FALSE(file_util::PathExists(test_subdir)); 1057 EXPECT_FALSE(file_util::PathExists(test_subdir));
1058 } 1058 }
1059 1059
1060 TEST_F(FileUtilTest, MoveFileNew) { 1060 TEST_F(FileUtilTest, MoveFileNew) {
1061 // Create a file 1061 // Create a file
1062 FilePath file_name_from = 1062 FilePath file_name_from =
1063 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt")); 1063 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1064 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1064 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after
1688 TEST_F(FileUtilTest, CreateTemporaryFileTest) { 1688 TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1689 FilePath temp_files[3]; 1689 FilePath temp_files[3];
1690 for (int i = 0; i < 3; i++) { 1690 for (int i = 0; i < 3; i++) {
1691 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i]))); 1691 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
1692 EXPECT_TRUE(file_util::PathExists(temp_files[i])); 1692 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1693 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i])); 1693 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1694 } 1694 }
1695 for (int i = 0; i < 3; i++) 1695 for (int i = 0; i < 3; i++)
1696 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); 1696 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1697 for (int i = 0; i < 3; i++) 1697 for (int i = 0; i < 3; i++)
1698 EXPECT_TRUE(file_util::Delete(temp_files[i], false)); 1698 EXPECT_TRUE(base::Delete(temp_files[i], false));
1699 } 1699 }
1700 1700
1701 TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) { 1701 TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
1702 FilePath names[3]; 1702 FilePath names[3];
1703 FILE* fps[3]; 1703 FILE* fps[3];
1704 int i; 1704 int i;
1705 1705
1706 // Create; make sure they are open and exist. 1706 // Create; make sure they are open and exist.
1707 for (i = 0; i < 3; ++i) { 1707 for (i = 0; i < 3; ++i) {
1708 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i])); 1708 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1709 ASSERT_TRUE(fps[i]); 1709 ASSERT_TRUE(fps[i]);
1710 EXPECT_TRUE(file_util::PathExists(names[i])); 1710 EXPECT_TRUE(file_util::PathExists(names[i]));
1711 } 1711 }
1712 1712
1713 // Make sure all names are unique. 1713 // Make sure all names are unique.
1714 for (i = 0; i < 3; ++i) { 1714 for (i = 0; i < 3; ++i) {
1715 EXPECT_FALSE(names[i] == names[(i+1)%3]); 1715 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1716 } 1716 }
1717 1717
1718 // Close and delete. 1718 // Close and delete.
1719 for (i = 0; i < 3; ++i) { 1719 for (i = 0; i < 3; ++i) {
1720 EXPECT_TRUE(file_util::CloseFile(fps[i])); 1720 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1721 EXPECT_TRUE(file_util::Delete(names[i], false)); 1721 EXPECT_TRUE(base::Delete(names[i], false));
1722 } 1722 }
1723 } 1723 }
1724 1724
1725 TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { 1725 TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
1726 FilePath temp_dir; 1726 FilePath temp_dir;
1727 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(), 1727 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1728 &temp_dir)); 1728 &temp_dir));
1729 EXPECT_TRUE(file_util::PathExists(temp_dir)); 1729 EXPECT_TRUE(file_util::PathExists(temp_dir));
1730 EXPECT_TRUE(file_util::Delete(temp_dir, false)); 1730 EXPECT_TRUE(base::Delete(temp_dir, false));
1731 } 1731 }
1732 1732
1733 TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) { 1733 TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1734 FilePath new_dir; 1734 FilePath new_dir;
1735 ASSERT_TRUE(file_util::CreateTemporaryDirInDir( 1735 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1736 temp_dir_.path(), 1736 temp_dir_.path(),
1737 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"), 1737 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1738 &new_dir)); 1738 &new_dir));
1739 EXPECT_TRUE(file_util::PathExists(new_dir)); 1739 EXPECT_TRUE(file_util::PathExists(new_dir));
1740 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir)); 1740 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
1741 EXPECT_TRUE(file_util::Delete(new_dir, false)); 1741 EXPECT_TRUE(base::Delete(new_dir, false));
1742 } 1742 }
1743 1743
1744 TEST_F(FileUtilTest, GetShmemTempDirTest) { 1744 TEST_F(FileUtilTest, GetShmemTempDirTest) {
1745 FilePath dir; 1745 FilePath dir;
1746 EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false)); 1746 EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false));
1747 EXPECT_TRUE(file_util::DirectoryExists(dir)); 1747 EXPECT_TRUE(file_util::DirectoryExists(dir));
1748 } 1748 }
1749 1749
1750 TEST_F(FileUtilTest, CreateDirectoryTest) { 1750 TEST_F(FileUtilTest, CreateDirectoryTest) {
1751 FilePath test_root = 1751 FilePath test_root =
(...skipping 12 matching lines...) Expand all
1764 // CreateDirectory returns true if the DirectoryExists returns true. 1764 // CreateDirectory returns true if the DirectoryExists returns true.
1765 EXPECT_TRUE(file_util::CreateDirectory(test_path)); 1765 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1766 1766
1767 // Doesn't work to create it on top of a non-dir 1767 // Doesn't work to create it on top of a non-dir
1768 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt")); 1768 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
1769 EXPECT_FALSE(file_util::PathExists(test_path)); 1769 EXPECT_FALSE(file_util::PathExists(test_path));
1770 CreateTextFile(test_path, L"test file"); 1770 CreateTextFile(test_path, L"test file");
1771 EXPECT_TRUE(file_util::PathExists(test_path)); 1771 EXPECT_TRUE(file_util::PathExists(test_path));
1772 EXPECT_FALSE(file_util::CreateDirectory(test_path)); 1772 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1773 1773
1774 EXPECT_TRUE(file_util::Delete(test_root, true)); 1774 EXPECT_TRUE(base::Delete(test_root, true));
1775 EXPECT_FALSE(file_util::PathExists(test_root)); 1775 EXPECT_FALSE(file_util::PathExists(test_root));
1776 EXPECT_FALSE(file_util::PathExists(test_path)); 1776 EXPECT_FALSE(file_util::PathExists(test_path));
1777 1777
1778 // Verify assumptions made by the Windows implementation: 1778 // Verify assumptions made by the Windows implementation:
1779 // 1. The current directory always exists. 1779 // 1. The current directory always exists.
1780 // 2. The root directory always exists. 1780 // 2. The root directory always exists.
1781 ASSERT_TRUE(file_util::DirectoryExists( 1781 ASSERT_TRUE(file_util::DirectoryExists(
1782 FilePath(FilePath::kCurrentDirectory))); 1782 FilePath(FilePath::kCurrentDirectory)));
1783 FilePath top_level = test_root; 1783 FilePath top_level = test_root;
1784 while (top_level != top_level.DirName()) { 1784 while (top_level != top_level.DirName()) {
(...skipping 25 matching lines...) Expand all
1810 EXPECT_TRUE(file_util::CreateDirectory(test_root)); 1810 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1811 EXPECT_TRUE(file_util::PathExists(test_root)); 1811 EXPECT_TRUE(file_util::PathExists(test_root));
1812 EXPECT_TRUE(file_util::DirectoryExists(test_root)); 1812 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1813 // Check a file 1813 // Check a file
1814 FilePath test_path = 1814 FilePath test_path =
1815 test_root.Append(FILE_PATH_LITERAL("foobar.txt")); 1815 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
1816 EXPECT_FALSE(file_util::PathExists(test_path)); 1816 EXPECT_FALSE(file_util::PathExists(test_path));
1817 CreateTextFile(test_path, L"test file"); 1817 CreateTextFile(test_path, L"test file");
1818 EXPECT_TRUE(file_util::PathExists(test_path)); 1818 EXPECT_TRUE(file_util::PathExists(test_path));
1819 EXPECT_FALSE(file_util::DirectoryExists(test_path)); 1819 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1820 EXPECT_TRUE(file_util::Delete(test_path, false)); 1820 EXPECT_TRUE(base::Delete(test_path, false));
1821 1821
1822 EXPECT_TRUE(file_util::Delete(test_root, true)); 1822 EXPECT_TRUE(base::Delete(test_root, true));
1823 } 1823 }
1824 1824
1825 TEST_F(FileUtilTest, FileEnumeratorTest) { 1825 TEST_F(FileUtilTest, FileEnumeratorTest) {
1826 // Test an empty directory. 1826 // Test an empty directory.
1827 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES); 1827 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1828 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); 1828 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1829 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); 1829 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1830 1830
1831 // Test an empty directory, non-recursively, including "..". 1831 // Test an empty directory, non-recursively, including "..".
1832 FileEnumerator f0_dotdot(temp_dir_.path(), false, 1832 FileEnumerator f0_dotdot(temp_dir_.path(), false,
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1930 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something 1930 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1931 // (we don't care what). 1931 // (we don't care what).
1932 } 1932 }
1933 1933
1934 TEST_F(FileUtilTest, AppendToFile) { 1934 TEST_F(FileUtilTest, AppendToFile) {
1935 FilePath data_dir = 1935 FilePath data_dir =
1936 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); 1936 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1937 1937
1938 // Create a fresh, empty copy of this directory. 1938 // Create a fresh, empty copy of this directory.
1939 if (file_util::PathExists(data_dir)) { 1939 if (file_util::PathExists(data_dir)) {
1940 ASSERT_TRUE(file_util::Delete(data_dir, true)); 1940 ASSERT_TRUE(base::Delete(data_dir, true));
1941 } 1941 }
1942 ASSERT_TRUE(file_util::CreateDirectory(data_dir)); 1942 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1943 1943
1944 // Create a fresh, empty copy of this directory. 1944 // Create a fresh, empty copy of this directory.
1945 if (file_util::PathExists(data_dir)) { 1945 if (file_util::PathExists(data_dir)) {
1946 ASSERT_TRUE(file_util::Delete(data_dir, true)); 1946 ASSERT_TRUE(base::Delete(data_dir, true));
1947 } 1947 }
1948 ASSERT_TRUE(file_util::CreateDirectory(data_dir)); 1948 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1949 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); 1949 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1950 1950
1951 std::string data("hello"); 1951 std::string data("hello");
1952 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length())); 1952 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length()));
1953 EXPECT_EQ(static_cast<int>(data.length()), 1953 EXPECT_EQ(static_cast<int>(data.length()),
1954 file_util::WriteFile(foobar, data.c_str(), data.length())); 1954 file_util::WriteFile(foobar, data.c_str(), data.length()));
1955 EXPECT_EQ(static_cast<int>(data.length()), 1955 EXPECT_EQ(static_cast<int>(data.length()),
1956 file_util::AppendToFile(foobar, data.c_str(), data.length())); 1956 file_util::AppendToFile(foobar, data.c_str(), data.length()));
1957 1957
1958 const std::wstring read_content = ReadTextFile(foobar); 1958 const std::wstring read_content = ReadTextFile(foobar);
1959 EXPECT_EQ(L"hellohello", read_content); 1959 EXPECT_EQ(L"hellohello", read_content);
1960 } 1960 }
1961 1961
1962 TEST_F(FileUtilTest, TouchFile) { 1962 TEST_F(FileUtilTest, TouchFile) {
1963 FilePath data_dir = 1963 FilePath data_dir =
1964 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); 1964 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1965 1965
1966 // Create a fresh, empty copy of this directory. 1966 // Create a fresh, empty copy of this directory.
1967 if (file_util::PathExists(data_dir)) { 1967 if (file_util::PathExists(data_dir)) {
1968 ASSERT_TRUE(file_util::Delete(data_dir, true)); 1968 ASSERT_TRUE(base::Delete(data_dir, true));
1969 } 1969 }
1970 ASSERT_TRUE(file_util::CreateDirectory(data_dir)); 1970 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1971 1971
1972 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); 1972 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1973 std::string data("hello"); 1973 std::string data("hello");
1974 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); 1974 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1975 1975
1976 base::Time access_time; 1976 base::Time access_time;
1977 // This timestamp is divisible by one day (in local timezone), 1977 // This timestamp is divisible by one day (in local timezone),
1978 // to make it work on FAT too. 1978 // to make it work on FAT too.
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
2376 file_util::VerifyPathControlledByUser( 2376 file_util::VerifyPathControlledByUser(
2377 base_dir_, text_file_, uid_, ok_gids_)); 2377 base_dir_, text_file_, uid_, ok_gids_));
2378 EXPECT_TRUE( 2378 EXPECT_TRUE(
2379 file_util::VerifyPathControlledByUser( 2379 file_util::VerifyPathControlledByUser(
2380 sub_dir_, text_file_, uid_, ok_gids_)); 2380 sub_dir_, text_file_, uid_, ok_gids_));
2381 } 2381 }
2382 2382
2383 #endif // defined(OS_POSIX) 2383 #endif // defined(OS_POSIX)
2384 2384
2385 } // namespace 2385 } // namespace
OLDNEW
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698