| 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 1361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 DWORD expected = read_only ? |
| 1395 ((attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)) | |
| 1396 FILE_ATTRIBUTE_READONLY) : |
| 1397 (attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)); |
| 1398 // TODO(ripp@yandex-team.ru): this seems out of place here. If we really think |
| 1399 // it is important to verify that temp files are not indexed there should be |
| 1400 // a dedicated test for that (create a file, inspect the attributes) |
| 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 mode_t 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 |
| 1403 bool IsReadOnly(const FilePath& path) { | 1413 bool IsReadOnly(const FilePath& path) { |
| 1404 #if defined(OS_WIN) | 1414 #if defined(OS_WIN) |
| 1405 DWORD attrs = GetFileAttributes(path.value().c_str()); | 1415 DWORD attrs = GetFileAttributes(path.value().c_str()); |
| 1406 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs); | 1416 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs); |
| 1407 return attrs & FILE_ATTRIBUTE_READONLY; | 1417 return attrs & FILE_ATTRIBUTE_READONLY; |
| 1408 #else | 1418 #else |
| 1409 int mode = 0; | 1419 int mode = 0; |
| 1410 EXPECT_TRUE(GetPosixFilePermissions(path, &mode)); | 1420 EXPECT_TRUE(GetPosixFilePermissions(path, &mode)); |
| 1411 return !(mode & S_IWUSR); | 1421 return !(mode & S_IWUSR); |
| 1412 #endif | 1422 #endif |
| 1413 } | 1423 } |
| 1414 | 1424 |
| 1415 TEST_F(FileUtilTest, CopyDirectoryACL) { | 1425 TEST_F(FileUtilTest, CopyDirectoryACL) { |
| 1416 // Create a directory. | 1426 // Create source directories. |
| 1417 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src")); | 1427 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src")); |
| 1418 CreateDirectory(src); | 1428 FilePath src_subdir = src.Append(FILE_PATH_LITERAL("subdir")); |
| 1419 ASSERT_TRUE(PathExists(src)); | 1429 CreateDirectory(src_subdir); |
| 1430 ASSERT_TRUE(PathExists(src_subdir)); |
| 1420 | 1431 |
| 1421 // Create a file under the directory. | 1432 // Create a file under the directory. |
| 1422 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt")); | 1433 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt")); |
| 1423 CreateTextFile(src_file, L"Gooooooooooooooooooooogle"); | 1434 CreateTextFile(src_file, L"Gooooooooooooooooooooogle"); |
| 1424 SetReadOnly(src_file); | 1435 SetReadOnly(src_file, true); |
| 1425 ASSERT_TRUE(IsReadOnly(src_file)); | 1436 ASSERT_TRUE(IsReadOnly(src_file)); |
| 1426 | 1437 |
| 1438 // Make directory read-only. |
| 1439 SetReadOnly(src_subdir, true); |
| 1440 ASSERT_TRUE(IsReadOnly(src_subdir)); |
| 1441 |
| 1427 // Copy the directory recursively. | 1442 // Copy the directory recursively. |
| 1428 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst")); | 1443 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst")); |
| 1429 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt")); | 1444 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt")); |
| 1430 EXPECT_TRUE(CopyDirectory(src, dst, true)); | 1445 EXPECT_TRUE(CopyDirectory(src, dst, true)); |
| 1431 | 1446 |
| 1447 FilePath dst_subdir = dst.Append(FILE_PATH_LITERAL("subdir")); |
| 1448 ASSERT_FALSE(IsReadOnly(dst_subdir)); |
| 1432 ASSERT_FALSE(IsReadOnly(dst_file)); | 1449 ASSERT_FALSE(IsReadOnly(dst_file)); |
| 1450 |
| 1451 // Give write permissions to allow deletion. |
| 1452 SetReadOnly(src_subdir, false); |
| 1453 ASSERT_FALSE(IsReadOnly(src_subdir)); |
| 1433 } | 1454 } |
| 1434 | 1455 |
| 1435 TEST_F(FileUtilTest, CopyFile) { | 1456 TEST_F(FileUtilTest, CopyFile) { |
| 1436 // Create a directory | 1457 // Create a directory |
| 1437 FilePath dir_name_from = | 1458 FilePath dir_name_from = |
| 1438 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); | 1459 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 1439 CreateDirectory(dir_name_from); | 1460 CreateDirectory(dir_name_from); |
| 1440 ASSERT_TRUE(PathExists(dir_name_from)); | 1461 ASSERT_TRUE(PathExists(dir_name_from)); |
| 1441 | 1462 |
| 1442 // Create a file under the directory | 1463 // Create a file under the directory |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1473 TEST_F(FileUtilTest, CopyFileACL) { | 1494 TEST_F(FileUtilTest, CopyFileACL) { |
| 1474 // While FileUtilTest.CopyFile asserts the content is correctly copied over, | 1495 // While FileUtilTest.CopyFile asserts the content is correctly copied over, |
| 1475 // this test case asserts the access control bits are meeting expectations in | 1496 // this test case asserts the access control bits are meeting expectations in |
| 1476 // CopyFileUnsafe(). | 1497 // CopyFileUnsafe(). |
| 1477 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt")); | 1498 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt")); |
| 1478 const std::wstring file_contents(L"Gooooooooooooooooooooogle"); | 1499 const std::wstring file_contents(L"Gooooooooooooooooooooogle"); |
| 1479 CreateTextFile(src, file_contents); | 1500 CreateTextFile(src, file_contents); |
| 1480 | 1501 |
| 1481 // Set the source file to read-only. | 1502 // Set the source file to read-only. |
| 1482 ASSERT_FALSE(IsReadOnly(src)); | 1503 ASSERT_FALSE(IsReadOnly(src)); |
| 1483 SetReadOnly(src); | 1504 SetReadOnly(src, true); |
| 1484 ASSERT_TRUE(IsReadOnly(src)); | 1505 ASSERT_TRUE(IsReadOnly(src)); |
| 1485 | 1506 |
| 1486 // Copy the file. | 1507 // Copy the file. |
| 1487 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt")); | 1508 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt")); |
| 1488 ASSERT_TRUE(CopyFile(src, dst)); | 1509 ASSERT_TRUE(CopyFile(src, dst)); |
| 1489 EXPECT_EQ(file_contents, ReadTextFile(dst)); | 1510 EXPECT_EQ(file_contents, ReadTextFile(dst)); |
| 1490 | 1511 |
| 1491 ASSERT_FALSE(IsReadOnly(dst)); | 1512 ASSERT_FALSE(IsReadOnly(dst)); |
| 1492 } | 1513 } |
| 1493 | 1514 |
| (...skipping 1092 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2586 // Trying to close it should crash. This is important for security. | 2607 // Trying to close it should crash. This is important for security. |
| 2587 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); | 2608 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); |
| 2588 #endif | 2609 #endif |
| 2589 } | 2610 } |
| 2590 | 2611 |
| 2591 #endif // defined(OS_POSIX) | 2612 #endif // defined(OS_POSIX) |
| 2592 | 2613 |
| 2593 } // namespace | 2614 } // namespace |
| 2594 | 2615 |
| 2595 } // namespace base | 2616 } // namespace base |
| OLD | NEW |