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

Side by Side Diff: base/files/file_util_unittest.cc

Issue 659303002: Fixed copying read-only directories (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: Created 6 years, 2 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
« no previous file with comments | « base/files/file_util_posix.cc ('k') | no next file » | 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 1360 matching lines...) Expand 10 before | Expand all | Expand 10 after
1371 1371
1372 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true)); 1372 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
1373 1373
1374 // Check everything has been copied. 1374 // Check everything has been copied.
1375 EXPECT_TRUE(PathExists(dir_name_from)); 1375 EXPECT_TRUE(PathExists(dir_name_from));
1376 EXPECT_TRUE(PathExists(file_name_from)); 1376 EXPECT_TRUE(PathExists(file_name_from));
1377 EXPECT_TRUE(PathExists(dir_name_to)); 1377 EXPECT_TRUE(PathExists(dir_name_to));
1378 EXPECT_TRUE(PathExists(file_name_to)); 1378 EXPECT_TRUE(PathExists(file_name_to));
1379 } 1379 }
1380 1380
1381 // Sets the source file to full access.
1382 void SetFullAccess(const FilePath& path) {
1383 #if defined(OS_WIN)
1384 // On Windows, it involves clearing a bit.
1385 DWORD attrs = GetFileAttributes(path.value().c_str());
1386 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1387 ASSERT_TRUE(SetFileAttributes(
1388 path.value().c_str(), attrs & ~FILE_ATTRIBUTE_READONLY));
1389 // Files in the temporary directory should not be indexed ever. If this
rvargas (doing something else) 2014/10/23 01:12:11 This seems out of place here (yes, it's that way o
ripp 2014/10/23 07:04:51 TODO added to SetReadOnly function
1390 // assumption change, fix this unit test accordingly.
1391 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1392 DWORD expected =
1393 (attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY));
1394 if (win::GetVersion() >= win::VERSION_VISTA)
1395 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1396 attrs = GetFileAttributes(path.value().c_str());
1397 ASSERT_EQ(expected, attrs);
1398 #else
1399 // On all other platforms, it involves setting of all bits.
1400 EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR | S_IXUSR | S_IWUSR));
1401 #endif
1402 }
1403
1381 // Sets the source file to read-only. 1404 // Sets the source file to read-only.
1382 void SetReadOnly(const FilePath& path) { 1405 void SetReadOnly(const FilePath& path) {
1383 #if defined(OS_WIN) 1406 #if defined(OS_WIN)
1384 // On Windows, it involves setting a bit. 1407 // On Windows, it involves setting a bit.
1385 DWORD attrs = GetFileAttributes(path.value().c_str()); 1408 DWORD attrs = GetFileAttributes(path.value().c_str());
1386 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs); 1409 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1387 ASSERT_TRUE(SetFileAttributes( 1410 ASSERT_TRUE(SetFileAttributes(
1388 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY)); 1411 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY));
1389 attrs = GetFileAttributes(path.value().c_str());
1390 // Files in the temporary directory should not be indexed ever. If this 1412 // Files in the temporary directory should not be indexed ever. If this
1391 // assumption change, fix this unit test accordingly. 1413 // assumption change, fix this unit test accordingly.
1392 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP. 1414 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1393 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY; 1415 DWORD expected =
1416 (attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)) |
1417 FILE_ATTRIBUTE_READONLY;
1394 if (win::GetVersion() >= win::VERSION_VISTA) 1418 if (win::GetVersion() >= win::VERSION_VISTA)
1395 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; 1419 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1420 attrs = GetFileAttributes(path.value().c_str());
1396 ASSERT_EQ(expected, attrs); 1421 ASSERT_EQ(expected, attrs);
1397 #else 1422 #else
1398 // On all other platforms, it involves removing the write bit. 1423 // On all other platforms, it involves removing the write bit.
1399 EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR)); 1424 EXPECT_TRUE(SetPosixFilePermissions(
1425 path, DirectoryExists(path) ? S_IRUSR | S_IXUSR : S_IRUSR));
1400 #endif 1426 #endif
1401 } 1427 }
1402 1428
1403 bool IsReadOnly(const FilePath& path) { 1429 bool IsReadOnly(const FilePath& path) {
1404 #if defined(OS_WIN) 1430 #if defined(OS_WIN)
1405 DWORD attrs = GetFileAttributes(path.value().c_str()); 1431 DWORD attrs = GetFileAttributes(path.value().c_str());
1406 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs); 1432 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1407 return attrs & FILE_ATTRIBUTE_READONLY; 1433 return attrs & FILE_ATTRIBUTE_READONLY;
1408 #else 1434 #else
1409 int mode = 0; 1435 int mode = 0;
1410 EXPECT_TRUE(GetPosixFilePermissions(path, &mode)); 1436 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1411 return !(mode & S_IWUSR); 1437 return !(mode & S_IWUSR);
1412 #endif 1438 #endif
1413 } 1439 }
1414 1440
1415 TEST_F(FileUtilTest, CopyDirectoryACL) { 1441 TEST_F(FileUtilTest, CopyDirectoryACL) {
1416 // Create a directory. 1442 // Create source directories.
1417 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src")); 1443 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
1418 CreateDirectory(src); 1444 FilePath src_subdir = src.Append(FILE_PATH_LITERAL("subdir"));
1445 CreateDirectory(src_subdir);
1419 ASSERT_TRUE(PathExists(src)); 1446 ASSERT_TRUE(PathExists(src));
rvargas (doing something else) 2014/10/23 01:12:11 src_subdir ?
ripp 2014/10/23 07:04:51 Done.
1420 1447
1421 // Create a file under the directory. 1448 // Create a file under the directory.
1422 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt")); 1449 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1423 CreateTextFile(src_file, L"Gooooooooooooooooooooogle"); 1450 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
1424 SetReadOnly(src_file); 1451 SetReadOnly(src_file);
1425 ASSERT_TRUE(IsReadOnly(src_file)); 1452 ASSERT_TRUE(IsReadOnly(src_file));
1426 1453
1454 // Make data directory read-only
rvargas (doing something else) 2014/10/23 01:12:11 nit: remove "data" and end with period.
ripp 2014/10/23 07:04:51 Done.
1455 SetReadOnly(src_subdir);
1456
rvargas (doing something else) 2014/10/23 01:12:11 ASERT_TRUE(IsReadOnly())
ripp 2014/10/23 07:04:51 Done.
1427 // Copy the directory recursively. 1457 // Copy the directory recursively.
1428 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst")); 1458 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1429 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt")); 1459 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1430 EXPECT_TRUE(CopyDirectory(src, dst, true)); 1460 EXPECT_TRUE(CopyDirectory(src, dst, true));
1431 1461
1462 FilePath dst_subdir = dst.Append(FILE_PATH_LITERAL("subdir"));
1463 ASSERT_FALSE(IsReadOnly(dst_subdir));
1432 ASSERT_FALSE(IsReadOnly(dst_file)); 1464 ASSERT_FALSE(IsReadOnly(dst_file));
1465
1466 // Give write permissions to allow deletion
rvargas (doing something else) 2014/10/23 01:12:11 nit: end with period.
ripp 2014/10/23 07:04:51 Done.
1467 SetFullAccess(src_subdir);
rvargas (doing something else) 2014/10/23 01:12:11 Why not SetReadOnly(path, false) ? There is a lot
ripp 2014/10/23 07:04:51 Ok, moved to SetReadOnly(path, false)
1433 } 1468 }
1434 1469
1435 TEST_F(FileUtilTest, CopyFile) { 1470 TEST_F(FileUtilTest, CopyFile) {
1436 // Create a directory 1471 // Create a directory
1437 FilePath dir_name_from = 1472 FilePath dir_name_from =
1438 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1473 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1439 CreateDirectory(dir_name_from); 1474 CreateDirectory(dir_name_from);
1440 ASSERT_TRUE(PathExists(dir_name_from)); 1475 ASSERT_TRUE(PathExists(dir_name_from));
1441 1476
1442 // Create a file under the directory 1477 // Create a file under the directory
(...skipping 1143 matching lines...) Expand 10 before | Expand all | Expand 10 after
2586 // Trying to close it should crash. This is important for security. 2621 // Trying to close it should crash. This is important for security.
2587 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2622 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2588 #endif 2623 #endif
2589 } 2624 }
2590 2625
2591 #endif // defined(OS_POSIX) 2626 #endif // defined(OS_POSIX)
2592 2627
2593 } // namespace 2628 } // namespace
2594 2629
2595 } // namespace base 2630 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698