| 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 1342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1353 | 1353 |
| 1354 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true)); | 1354 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true)); |
| 1355 | 1355 |
| 1356 // Check everything has been copied. | 1356 // Check everything has been copied. |
| 1357 EXPECT_TRUE(PathExists(dir_name_from)); | 1357 EXPECT_TRUE(PathExists(dir_name_from)); |
| 1358 EXPECT_TRUE(PathExists(file_name_from)); | 1358 EXPECT_TRUE(PathExists(file_name_from)); |
| 1359 EXPECT_TRUE(PathExists(dir_name_to)); | 1359 EXPECT_TRUE(PathExists(dir_name_to)); |
| 1360 EXPECT_TRUE(PathExists(file_name_to)); | 1360 EXPECT_TRUE(PathExists(file_name_to)); |
| 1361 } | 1361 } |
| 1362 | 1362 |
| 1363 // Sets the source file to read-only. |
| 1364 void SetReadOnly(const FilePath& path) { |
| 1365 #if defined(OS_WIN) |
| 1366 // On Windows, it involves setting a bit. |
| 1367 DWORD attrs = GetFileAttributes(path.value().c_str()); |
| 1368 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs); |
| 1369 ASSERT_TRUE(SetFileAttributes( |
| 1370 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY)); |
| 1371 attrs = GetFileAttributes(path.value().c_str()); |
| 1372 // Files in the temporary directory should not be indexed ever. If this |
| 1373 // assumption change, fix this unit test accordingly. |
| 1374 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP. |
| 1375 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY; |
| 1376 if (win::GetVersion() >= win::VERSION_VISTA) |
| 1377 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; |
| 1378 ASSERT_EQ(expected, attrs); |
| 1379 #else |
| 1380 // On all other platforms, it involves removing the write bit. |
| 1381 EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR)); |
| 1382 #endif |
| 1383 } |
| 1384 |
| 1385 bool IsReadOnly(const FilePath& path) { |
| 1386 #if defined(OS_WIN) |
| 1387 DWORD attrs = GetFileAttributes(path.value().c_str()); |
| 1388 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs); |
| 1389 return attrs & FILE_ATTRIBUTE_READONLY; |
| 1390 #else |
| 1391 int mode = 0; |
| 1392 EXPECT_TRUE(GetPosixFilePermissions(path, &mode)); |
| 1393 return !(mode & S_IWUSR); |
| 1394 #endif |
| 1395 } |
| 1396 |
| 1397 TEST_F(FileUtilTest, CopyDirectoryACL) { |
| 1398 // Create a directory. |
| 1399 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src")); |
| 1400 CreateDirectory(src); |
| 1401 ASSERT_TRUE(PathExists(src)); |
| 1402 |
| 1403 // Create a file under the directory. |
| 1404 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt")); |
| 1405 CreateTextFile(src_file, L"Gooooooooooooooooooooogle"); |
| 1406 SetReadOnly(src_file); |
| 1407 ASSERT_TRUE(IsReadOnly(src_file)); |
| 1408 |
| 1409 // Copy the directory recursively. |
| 1410 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst")); |
| 1411 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt")); |
| 1412 EXPECT_TRUE(CopyDirectory(src, dst, true)); |
| 1413 |
| 1414 #if defined(OS_WIN) |
| 1415 // While the source file had RO bit set, the copied file doesn't. |
| 1416 ASSERT_FALSE(IsReadOnly(dst_file)); |
| 1417 #elif defined(OS_MACOSX) |
| 1418 // On OSX, file mode is copied. |
| 1419 ASSERT_TRUE(IsReadOnly(dst_file)); |
| 1420 #else |
| 1421 // On other POSIX, file mode is not copied. |
| 1422 ASSERT_FALSE(IsReadOnly(dst_file)); |
| 1423 #endif |
| 1424 } |
| 1425 |
| 1363 TEST_F(FileUtilTest, CopyFile) { | 1426 TEST_F(FileUtilTest, CopyFile) { |
| 1364 // Create a directory | 1427 // Create a directory |
| 1365 FilePath dir_name_from = | 1428 FilePath dir_name_from = |
| 1366 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); | 1429 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); |
| 1367 CreateDirectory(dir_name_from); | 1430 CreateDirectory(dir_name_from); |
| 1368 ASSERT_TRUE(PathExists(dir_name_from)); | 1431 ASSERT_TRUE(PathExists(dir_name_from)); |
| 1369 | 1432 |
| 1370 // Create a file under the directory | 1433 // Create a file under the directory |
| 1371 FilePath file_name_from = | 1434 FilePath file_name_from = |
| 1372 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); | 1435 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1391 | 1454 |
| 1392 // Check everything has been copied. | 1455 // Check everything has been copied. |
| 1393 EXPECT_TRUE(PathExists(file_name_from)); | 1456 EXPECT_TRUE(PathExists(file_name_from)); |
| 1394 EXPECT_TRUE(PathExists(dest_file)); | 1457 EXPECT_TRUE(PathExists(dest_file)); |
| 1395 const std::wstring read_contents = ReadTextFile(dest_file); | 1458 const std::wstring read_contents = ReadTextFile(dest_file); |
| 1396 EXPECT_EQ(file_contents, read_contents); | 1459 EXPECT_EQ(file_contents, read_contents); |
| 1397 EXPECT_TRUE(PathExists(dest_file2_test)); | 1460 EXPECT_TRUE(PathExists(dest_file2_test)); |
| 1398 EXPECT_TRUE(PathExists(dest_file2)); | 1461 EXPECT_TRUE(PathExists(dest_file2)); |
| 1399 } | 1462 } |
| 1400 | 1463 |
| 1401 #if defined(OS_WIN) || defined(OS_POSIX) | |
| 1402 TEST_F(FileUtilTest, CopyFileACL) { | 1464 TEST_F(FileUtilTest, CopyFileACL) { |
| 1403 // While FileUtilTest.CopyFile asserts the content is correctly copied over, | 1465 // While FileUtilTest.CopyFile asserts the content is correctly copied over, |
| 1404 // this test case asserts the access control bits are meeting expectations in | 1466 // this test case asserts the access control bits are meeting expectations in |
| 1405 // CopyFileUnsafe(). | 1467 // CopyFileUnsafe(). |
| 1406 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt")); | 1468 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt")); |
| 1407 const std::wstring file_contents(L"Gooooooooooooooooooooogle"); | 1469 const std::wstring file_contents(L"Gooooooooooooooooooooogle"); |
| 1408 CreateTextFile(src, file_contents); | 1470 CreateTextFile(src, file_contents); |
| 1409 | 1471 |
| 1410 // Set the source file to read-only. | 1472 // Set the source file to read-only. |
| 1411 #if defined(OS_WIN) | 1473 ASSERT_FALSE(IsReadOnly(src)); |
| 1412 // On Windows, it involves setting a bit. | 1474 SetReadOnly(src); |
| 1413 DWORD attrs = GetFileAttributes(src.value().c_str()); | 1475 ASSERT_TRUE(IsReadOnly(src)); |
| 1414 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs); | |
| 1415 ASSERT_TRUE(SetFileAttributes( | |
| 1416 src.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY)); | |
| 1417 attrs = GetFileAttributes(src.value().c_str()); | |
| 1418 // Files in the temporary directory should not be indexed ever. If this | |
| 1419 // assumption change, fix this unit test accordingly. | |
| 1420 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP. | |
| 1421 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY; | |
| 1422 if (win::GetVersion() >= win::VERSION_VISTA) | |
| 1423 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; | |
| 1424 ASSERT_EQ(expected, attrs); | |
| 1425 #else | |
| 1426 // On all other platforms, it involves removing the write bit. | |
| 1427 EXPECT_TRUE(SetPosixFilePermissions(src, 0400)); | |
| 1428 #endif | |
| 1429 | 1476 |
| 1430 // Copy the file. | 1477 // Copy the file. |
| 1431 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt")); | 1478 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt")); |
| 1432 ASSERT_TRUE(CopyFile(src, dst)); | 1479 ASSERT_TRUE(CopyFile(src, dst)); |
| 1433 EXPECT_EQ(file_contents, ReadTextFile(dst)); | 1480 EXPECT_EQ(file_contents, ReadTextFile(dst)); |
| 1434 | 1481 |
| 1435 #if defined(OS_WIN) | 1482 #if defined(OS_WIN) |
| 1436 // While the source file had RO bit set, the copied file doesn't. Other file | 1483 // While the source file had RO bit set, the copied file doesn't. Other file |
| 1437 // modes are copied. | 1484 // modes are copied. |
| 1438 attrs = GetFileAttributes(src.value().c_str()); | 1485 ASSERT_FALSE(IsReadOnly(dst)); |
| 1439 ASSERT_EQ(expected, attrs); | |
| 1440 expected = FILE_ATTRIBUTE_ARCHIVE; | |
| 1441 if (win::GetVersion() >= win::VERSION_VISTA) | |
| 1442 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; | |
| 1443 attrs = GetFileAttributes(dst.value().c_str()); | |
| 1444 ASSERT_EQ(expected, attrs); | |
| 1445 #elif defined(OS_MACOSX) | 1486 #elif defined(OS_MACOSX) |
| 1446 // On OSX, file mode is copied. | 1487 // On OSX, file mode is copied. |
| 1447 int mode = 0; | 1488 ASSERT_TRUE(IsReadOnly(dst)); |
| 1448 EXPECT_TRUE(GetPosixFilePermissions(dst, &mode)); | |
| 1449 EXPECT_EQ(0400, mode & 0600); | |
| 1450 #else | 1489 #else |
| 1451 // On other POSIX, file mode is not copied. | 1490 // On other POSIX, file mode is not copied. |
| 1452 int mode = 0; | 1491 ASSERT_FALSE(IsReadOnly(dst)); |
| 1453 EXPECT_TRUE(GetPosixFilePermissions(dst, &mode)); | |
| 1454 EXPECT_EQ(0600, mode & 0600); | |
| 1455 #endif | 1492 #endif |
| 1456 } | 1493 } |
| 1457 #endif // defined(OS_WIN) || defined(OS_POSIX) | |
| 1458 | 1494 |
| 1459 // file_util winds up using autoreleased objects on the Mac, so this needs | 1495 // file_util winds up using autoreleased objects on the Mac, so this needs |
| 1460 // to be a PlatformTest. | 1496 // to be a PlatformTest. |
| 1461 typedef PlatformTest ReadOnlyFileUtilTest; | 1497 typedef PlatformTest ReadOnlyFileUtilTest; |
| 1462 | 1498 |
| 1463 TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { | 1499 TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { |
| 1464 FilePath data_dir; | 1500 FilePath data_dir; |
| 1465 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir)); | 1501 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir)); |
| 1466 data_dir = data_dir.AppendASCII("file_util"); | 1502 data_dir = data_dir.AppendASCII("file_util"); |
| 1467 ASSERT_TRUE(PathExists(data_dir)); | 1503 ASSERT_TRUE(PathExists(data_dir)); |
| (...skipping 918 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2386 int fd = OpenContentUriForRead(path); | 2422 int fd = OpenContentUriForRead(path); |
| 2387 EXPECT_EQ(-1, fd); | 2423 EXPECT_EQ(-1, fd); |
| 2388 } | 2424 } |
| 2389 #endif | 2425 #endif |
| 2390 | 2426 |
| 2391 #endif // defined(OS_POSIX) | 2427 #endif // defined(OS_POSIX) |
| 2392 | 2428 |
| 2393 } // namespace | 2429 } // namespace |
| 2394 | 2430 |
| 2395 } // namespace base | 2431 } // namespace base |
| OLD | NEW |