Chromium Code Reviews| Index: base/file_util_unittest.cc |
| diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc |
| index d3c246e42928675bcf08a9e0b242294ba58a30e6..fcfbe4f10117cc7cc0c57280b5571e1578e26b04 100644 |
| --- a/base/file_util_unittest.cc |
| +++ b/base/file_util_unittest.cc |
| @@ -1360,6 +1360,71 @@ TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) { |
| EXPECT_TRUE(PathExists(file_name_to)); |
| } |
| +#if defined(OS_WIN) || defined(OS_POSIX) |
|
Nico
2014/01/21 04:12:18
What else is there? I think you can remove this de
M-A Ruel
2014/01/21 15:41:36
Oh, I was thinking about iOS but indeed, it's a no
|
| +// Sets the source file to read-only. |
| +void SetReadOnly(const FilePath& path) { |
| +#if defined(OS_WIN) |
| + // On Windows, it involves setting a bit. |
| + DWORD attrs = GetFileAttributes(path.value().c_str()); |
| + ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs); |
| + ASSERT_TRUE(SetFileAttributes( |
| + path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY)); |
| + attrs = GetFileAttributes(path.value().c_str()); |
| + // Files in the temporary directory should not be indexed ever. If this |
| + // assumption change, fix this unit test accordingly. |
| + // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP. |
| + DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY; |
| + if (win::GetVersion() >= win::VERSION_VISTA) |
| + expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; |
| + ASSERT_EQ(expected, attrs); |
| +#else |
| + // On all other platforms, it involves removing the write bit. |
| + EXPECT_TRUE(SetPosixFilePermissions(path, 0400)); |
|
Nico
2014/01/21 04:12:18
s/0400/S_IRUSR/
M-A Ruel
2014/01/21 15:41:36
Done.
|
| +#endif |
| +} |
| + |
| +bool IsReadOnly(const FilePath& path) { |
| +#if defined(OS_WIN) |
| + DWORD attrs = GetFileAttributes(path.value().c_str()); |
| + EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs); |
| + return attrs & FILE_ATTRIBUTE_READONLY; |
| +#else |
| + int mode = 0; |
| + EXPECT_TRUE(GetPosixFilePermissions(path, &mode)); |
| + return !(mode & 0200); |
|
Nico
2014/01/21 04:12:18
s/0200/S_IWUSR/
M-A Ruel
2014/01/21 15:41:36
Done.
|
| +#endif |
| +} |
| + |
| +TEST_F(FileUtilTest, CopyDirectoryACL) { |
| + // Create a directory. |
| + FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src")); |
| + CreateDirectory(src); |
| + ASSERT_TRUE(PathExists(src)); |
| + |
| + // Create a file under the directory. |
| + FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt")); |
| + CreateTextFile(src_file, L"Gooooooooooooooooooooogle"); |
| + SetReadOnly(src_file); |
| + ASSERT_TRUE(IsReadOnly(src_file)); |
| + |
| + // Copy the directory recursively. |
| + FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst")); |
| + FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt")); |
| + EXPECT_TRUE(CopyDirectory(src, dst, true)); |
| + |
| +#if defined(OS_WIN) |
| + // While the source file had RO bit set, the copied file doesn't. |
| + ASSERT_FALSE(IsReadOnly(dst_file)); |
| +#elif defined(OS_MACOSX) |
| + // On OSX, file mode is copied. |
| + ASSERT_TRUE(IsReadOnly(dst_file)); |
| +#else |
| + // On other POSIX, file mode is not copied. |
| + ASSERT_FALSE(IsReadOnly(dst_file)); |
| +#endif |
| +} |
| +#endif // defined(OS_WIN) || defined(OS_POSIX) |
| + |
| TEST_F(FileUtilTest, CopyFile) { |
| // Create a directory |
| FilePath dir_name_from = |
| @@ -1408,24 +1473,9 @@ TEST_F(FileUtilTest, CopyFileACL) { |
| CreateTextFile(src, file_contents); |
| // Set the source file to read-only. |
| -#if defined(OS_WIN) |
| - // On Windows, it involves setting a bit. |
| - DWORD attrs = GetFileAttributes(src.value().c_str()); |
| - ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs); |
| - ASSERT_TRUE(SetFileAttributes( |
| - src.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY)); |
| - attrs = GetFileAttributes(src.value().c_str()); |
| - // Files in the temporary directory should not be indexed ever. If this |
| - // assumption change, fix this unit test accordingly. |
| - // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP. |
| - DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY; |
| - if (win::GetVersion() >= win::VERSION_VISTA) |
| - expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; |
| - ASSERT_EQ(expected, attrs); |
| -#else |
| - // On all other platforms, it involves removing the write bit. |
| - EXPECT_TRUE(SetPosixFilePermissions(src, 0400)); |
| -#endif |
| + ASSERT_FALSE(IsReadOnly(src)); |
| + SetReadOnly(src); |
| + ASSERT_TRUE(IsReadOnly(src)); |
| // Copy the file. |
| FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt")); |
| @@ -1435,23 +1485,13 @@ TEST_F(FileUtilTest, CopyFileACL) { |
| #if defined(OS_WIN) |
| // While the source file had RO bit set, the copied file doesn't. Other file |
| // modes are copied. |
| - attrs = GetFileAttributes(src.value().c_str()); |
| - ASSERT_EQ(expected, attrs); |
| - expected = FILE_ATTRIBUTE_ARCHIVE; |
| - if (win::GetVersion() >= win::VERSION_VISTA) |
| - expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; |
| - attrs = GetFileAttributes(dst.value().c_str()); |
| - ASSERT_EQ(expected, attrs); |
| + ASSERT_FALSE(IsReadOnly(dst)); |
| #elif defined(OS_MACOSX) |
| // On OSX, file mode is copied. |
| - int mode = 0; |
| - EXPECT_TRUE(GetPosixFilePermissions(dst, &mode)); |
| - EXPECT_EQ(0400, mode & 0600); |
| + ASSERT_TRUE(IsReadOnly(dst)); |
| #else |
| // On other POSIX, file mode is not copied. |
| - int mode = 0; |
| - EXPECT_TRUE(GetPosixFilePermissions(dst, &mode)); |
| - EXPECT_EQ(0600, mode & 0600); |
| + ASSERT_FALSE(IsReadOnly(dst)); |
| #endif |
| } |
| #endif // defined(OS_WIN) || defined(OS_POSIX) |