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

Side by Side Diff: base/file_util_unittest.cc

Issue 141273010: Make CopyDirectory() not copy the read only bit on Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Made it gcc friendly by using ASSERT_FALSE() Created 6 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | base/file_util_win.cc » ('j') | base/file_util_win.cc » ('J')
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 1342 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 #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
1364 // Sets the source file to read-only.
1365 void SetReadOnly(const FilePath& path) {
1366 #if defined(OS_WIN)
1367 // On Windows, it involves setting a bit.
1368 DWORD attrs = GetFileAttributes(path.value().c_str());
1369 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1370 ASSERT_TRUE(SetFileAttributes(
1371 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY));
1372 attrs = GetFileAttributes(path.value().c_str());
1373 // Files in the temporary directory should not be indexed ever. If this
1374 // assumption change, fix this unit test accordingly.
1375 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1376 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY;
1377 if (win::GetVersion() >= win::VERSION_VISTA)
1378 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1379 ASSERT_EQ(expected, attrs);
1380 #else
1381 // On all other platforms, it involves removing the write bit.
1382 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.
1383 #endif
1384 }
1385
1386 bool IsReadOnly(const FilePath& path) {
1387 #if defined(OS_WIN)
1388 DWORD attrs = GetFileAttributes(path.value().c_str());
1389 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1390 return attrs & FILE_ATTRIBUTE_READONLY;
1391 #else
1392 int mode = 0;
1393 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1394 return !(mode & 0200);
Nico 2014/01/21 04:12:18 s/0200/S_IWUSR/
M-A Ruel 2014/01/21 15:41:36 Done.
1395 #endif
1396 }
1397
1398 TEST_F(FileUtilTest, CopyDirectoryACL) {
1399 // Create a directory.
1400 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
1401 CreateDirectory(src);
1402 ASSERT_TRUE(PathExists(src));
1403
1404 // Create a file under the directory.
1405 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1406 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
1407 SetReadOnly(src_file);
1408 ASSERT_TRUE(IsReadOnly(src_file));
1409
1410 // Copy the directory recursively.
1411 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1412 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1413 EXPECT_TRUE(CopyDirectory(src, dst, true));
1414
1415 #if defined(OS_WIN)
1416 // While the source file had RO bit set, the copied file doesn't.
1417 ASSERT_FALSE(IsReadOnly(dst_file));
1418 #elif defined(OS_MACOSX)
1419 // On OSX, file mode is copied.
1420 ASSERT_TRUE(IsReadOnly(dst_file));
1421 #else
1422 // On other POSIX, file mode is not copied.
1423 ASSERT_FALSE(IsReadOnly(dst_file));
1424 #endif
1425 }
1426 #endif // defined(OS_WIN) || defined(OS_POSIX)
1427
1363 TEST_F(FileUtilTest, CopyFile) { 1428 TEST_F(FileUtilTest, CopyFile) {
1364 // Create a directory 1429 // Create a directory
1365 FilePath dir_name_from = 1430 FilePath dir_name_from =
1366 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1431 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1367 CreateDirectory(dir_name_from); 1432 CreateDirectory(dir_name_from);
1368 ASSERT_TRUE(PathExists(dir_name_from)); 1433 ASSERT_TRUE(PathExists(dir_name_from));
1369 1434
1370 // Create a file under the directory 1435 // Create a file under the directory
1371 FilePath file_name_from = 1436 FilePath file_name_from =
1372 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1437 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
(...skipping 28 matching lines...) Expand all
1401 #if defined(OS_WIN) || defined(OS_POSIX) 1466 #if defined(OS_WIN) || defined(OS_POSIX)
1402 TEST_F(FileUtilTest, CopyFileACL) { 1467 TEST_F(FileUtilTest, CopyFileACL) {
1403 // While FileUtilTest.CopyFile asserts the content is correctly copied over, 1468 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1404 // this test case asserts the access control bits are meeting expectations in 1469 // this test case asserts the access control bits are meeting expectations in
1405 // CopyFileUnsafe(). 1470 // CopyFileUnsafe().
1406 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt")); 1471 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt"));
1407 const std::wstring file_contents(L"Gooooooooooooooooooooogle"); 1472 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1408 CreateTextFile(src, file_contents); 1473 CreateTextFile(src, file_contents);
1409 1474
1410 // Set the source file to read-only. 1475 // Set the source file to read-only.
1411 #if defined(OS_WIN) 1476 ASSERT_FALSE(IsReadOnly(src));
1412 // On Windows, it involves setting a bit. 1477 SetReadOnly(src);
1413 DWORD attrs = GetFileAttributes(src.value().c_str()); 1478 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 1479
1430 // Copy the file. 1480 // Copy the file.
1431 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt")); 1481 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt"));
1432 ASSERT_TRUE(CopyFile(src, dst)); 1482 ASSERT_TRUE(CopyFile(src, dst));
1433 EXPECT_EQ(file_contents, ReadTextFile(dst)); 1483 EXPECT_EQ(file_contents, ReadTextFile(dst));
1434 1484
1435 #if defined(OS_WIN) 1485 #if defined(OS_WIN)
1436 // While the source file had RO bit set, the copied file doesn't. Other file 1486 // While the source file had RO bit set, the copied file doesn't. Other file
1437 // modes are copied. 1487 // modes are copied.
1438 attrs = GetFileAttributes(src.value().c_str()); 1488 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) 1489 #elif defined(OS_MACOSX)
1446 // On OSX, file mode is copied. 1490 // On OSX, file mode is copied.
1447 int mode = 0; 1491 ASSERT_TRUE(IsReadOnly(dst));
1448 EXPECT_TRUE(GetPosixFilePermissions(dst, &mode));
1449 EXPECT_EQ(0400, mode & 0600);
1450 #else 1492 #else
1451 // On other POSIX, file mode is not copied. 1493 // On other POSIX, file mode is not copied.
1452 int mode = 0; 1494 ASSERT_FALSE(IsReadOnly(dst));
1453 EXPECT_TRUE(GetPosixFilePermissions(dst, &mode));
1454 EXPECT_EQ(0600, mode & 0600);
1455 #endif 1495 #endif
1456 } 1496 }
1457 #endif // defined(OS_WIN) || defined(OS_POSIX) 1497 #endif // defined(OS_WIN) || defined(OS_POSIX)
1458 1498
1459 // file_util winds up using autoreleased objects on the Mac, so this needs 1499 // file_util winds up using autoreleased objects on the Mac, so this needs
1460 // to be a PlatformTest. 1500 // to be a PlatformTest.
1461 typedef PlatformTest ReadOnlyFileUtilTest; 1501 typedef PlatformTest ReadOnlyFileUtilTest;
1462 1502
1463 TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { 1503 TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
1464 FilePath data_dir; 1504 FilePath data_dir;
(...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after
2386 int fd = OpenContentUriForRead(path); 2426 int fd = OpenContentUriForRead(path);
2387 EXPECT_EQ(-1, fd); 2427 EXPECT_EQ(-1, fd);
2388 } 2428 }
2389 #endif 2429 #endif
2390 2430
2391 #endif // defined(OS_POSIX) 2431 #endif // defined(OS_POSIX)
2392 2432
2393 } // namespace 2433 } // namespace
2394 2434
2395 } // namespace base 2435 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/file_util_win.cc » ('j') | base/file_util_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698