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

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: SetFullAccess functional moved to SetReadOnly(..., false); comments and asserts changed according t… Created 6 years, 1 month 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 1361 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 read-only. 1381 // Sets the source file to read-only.
1382 void SetReadOnly(const FilePath& path) { 1382 void SetReadOnly(const FilePath& path, bool read_only) {
1383 #if defined(OS_WIN) 1383 #if defined(OS_WIN)
1384 // On Windows, it involves setting a bit. 1384 // On Windows, it involves setting/removing the 'readonly' bit.
1385 DWORD attrs = GetFileAttributes(path.value().c_str()); 1385 DWORD attrs = GetFileAttributes(path.value().c_str());
1386 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs); 1386 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1387 ASSERT_TRUE(SetFileAttributes( 1387 ASSERT_TRUE(SetFileAttributes(
1388 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY)); 1388 path.value().c_str(),
1389 attrs = GetFileAttributes(path.value().c_str()); 1389 read_only ? (attrs | FILE_ATTRIBUTE_READONLY) :
1390 (attrs & ~FILE_ATTRIBUTE_READONLY)));
1390 // Files in the temporary directory should not be indexed ever. If this 1391 // Files in the temporary directory should not be indexed ever. If this
1391 // assumption change, fix this unit test accordingly. 1392 // assumption change, fix this unit test accordingly.
1392 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP. 1393 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1393 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY; 1394 // TODO(ripp@yandex-team.ru): this seems out of place here. If we really think
1395 // it is important to verify that temp files are not indexed there should be
1396 // a dedicated test for that (create a file, inspect the attributes)
1397 DWORD expected = read_only ?
1398 ((attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)) |
1399 FILE_ATTRIBUTE_READONLY) :
1400 (attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY));
rvargas (doing something else) 2014/10/23 19:15:34 nit: should not be extra indentation here.
ripp 2014/10/24 05:51:19 Done.
1394 if (win::GetVersion() >= win::VERSION_VISTA) 1401 if (win::GetVersion() >= win::VERSION_VISTA)
1395 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; 1402 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1403 attrs = GetFileAttributes(path.value().c_str());
1396 ASSERT_EQ(expected, attrs); 1404 ASSERT_EQ(expected, attrs);
1397 #else 1405 #else
1398 // On all other platforms, it involves removing the write bit. 1406 // On all other platforms, it involves removing/setting the write bit.
1399 EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR)); 1407 int mode = read_only ? S_IRUSR : (S_IRUSR | S_IWUSR);
1408 EXPECT_TRUE(SetPosixFilePermissions(
1409 path, DirectoryExists(path) ? (mode | S_IXUSR) : mode));
1400 #endif 1410 #endif
1401 } 1411 }
1402 1412
1413 void SetReadOnly(const FilePath& path) {
rvargas (doing something else) 2014/10/23 19:15:34 I meant we should remove this function.
ripp 2014/10/24 05:51:19 Done.
1414 SetReadOnly(path, true);
1415 }
1416
1403 bool IsReadOnly(const FilePath& path) { 1417 bool IsReadOnly(const FilePath& path) {
1404 #if defined(OS_WIN) 1418 #if defined(OS_WIN)
1405 DWORD attrs = GetFileAttributes(path.value().c_str()); 1419 DWORD attrs = GetFileAttributes(path.value().c_str());
1406 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs); 1420 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1407 return attrs & FILE_ATTRIBUTE_READONLY; 1421 return attrs & FILE_ATTRIBUTE_READONLY;
1408 #else 1422 #else
1409 int mode = 0; 1423 int mode = 0;
1410 EXPECT_TRUE(GetPosixFilePermissions(path, &mode)); 1424 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1411 return !(mode & S_IWUSR); 1425 return !(mode & S_IWUSR);
1412 #endif 1426 #endif
1413 } 1427 }
1414 1428
1415 TEST_F(FileUtilTest, CopyDirectoryACL) { 1429 TEST_F(FileUtilTest, CopyDirectoryACL) {
1416 // Create a directory. 1430 // Create source directories.
1417 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src")); 1431 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
1418 CreateDirectory(src); 1432 FilePath src_subdir = src.Append(FILE_PATH_LITERAL("subdir"));
1419 ASSERT_TRUE(PathExists(src)); 1433 CreateDirectory(src_subdir);
1434 ASSERT_TRUE(PathExists(src_subdir));
1420 1435
1421 // Create a file under the directory. 1436 // Create a file under the directory.
1422 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt")); 1437 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1423 CreateTextFile(src_file, L"Gooooooooooooooooooooogle"); 1438 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
1424 SetReadOnly(src_file); 1439 SetReadOnly(src_file);
1425 ASSERT_TRUE(IsReadOnly(src_file)); 1440 ASSERT_TRUE(IsReadOnly(src_file));
1426 1441
1442 // Make directory read-only.
1443 SetReadOnly(src_subdir);
1444 ASSERT_TRUE(IsReadOnly(src_subdir));
1445
1427 // Copy the directory recursively. 1446 // Copy the directory recursively.
1428 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst")); 1447 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1429 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt")); 1448 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1430 EXPECT_TRUE(CopyDirectory(src, dst, true)); 1449 EXPECT_TRUE(CopyDirectory(src, dst, true));
1431 1450
1451 FilePath dst_subdir = dst.Append(FILE_PATH_LITERAL("subdir"));
1452 ASSERT_FALSE(IsReadOnly(dst_subdir));
1432 ASSERT_FALSE(IsReadOnly(dst_file)); 1453 ASSERT_FALSE(IsReadOnly(dst_file));
1454
1455 // Give write permissions to allow deletion.
1456 SetReadOnly(src_subdir, false);
1457 ASSERT_FALSE(IsReadOnly(src_subdir));
1433 } 1458 }
1434 1459
1435 TEST_F(FileUtilTest, CopyFile) { 1460 TEST_F(FileUtilTest, CopyFile) {
1436 // Create a directory 1461 // Create a directory
1437 FilePath dir_name_from = 1462 FilePath dir_name_from =
1438 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1463 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1439 CreateDirectory(dir_name_from); 1464 CreateDirectory(dir_name_from);
1440 ASSERT_TRUE(PathExists(dir_name_from)); 1465 ASSERT_TRUE(PathExists(dir_name_from));
1441 1466
1442 // Create a file under the directory 1467 // 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. 2611 // Trying to close it should crash. This is important for security.
2587 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2612 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2588 #endif 2613 #endif
2589 } 2614 }
2590 2615
2591 #endif // defined(OS_POSIX) 2616 #endif // defined(OS_POSIX)
2592 2617
2593 } // namespace 2618 } // namespace
2594 2619
2595 } // namespace base 2620 } // 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