| 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 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 | 427 |
| 428 // Get the NT style path to that drive. | 428 // Get the NT style path to that drive. |
| 429 wchar_t device_path[MAX_PATH] = {'\0'}; | 429 wchar_t device_path[MAX_PATH] = {'\0'}; |
| 430 ASSERT_TRUE( | 430 ASSERT_TRUE( |
| 431 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH)); | 431 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH)); |
| 432 FilePath actual_device_path(device_path); | 432 FilePath actual_device_path(device_path); |
| 433 FilePath win32_path; | 433 FilePath win32_path; |
| 434 | 434 |
| 435 // Run DevicePathToDriveLetterPath() on the NT style path we got from | 435 // Run DevicePathToDriveLetterPath() on the NT style path we got from |
| 436 // QueryDosDevice(). Expect the drive letter we started with. | 436 // QueryDosDevice(). Expect the drive letter we started with. |
| 437 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path, | 437 ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path, &win32_path)); |
| 438 &win32_path)); | |
| 439 ASSERT_EQ(real_drive_letter, win32_path.value()); | 438 ASSERT_EQ(real_drive_letter, win32_path.value()); |
| 440 | 439 |
| 441 // Add some directories to the path. Expect those extra path componenets | 440 // Add some directories to the path. Expect those extra path componenets |
| 442 // to be preserved. | 441 // to be preserved. |
| 443 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt")); | 442 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt")); |
| 444 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath( | 443 ASSERT_TRUE(DevicePathToDriveLetterPath( |
| 445 actual_device_path.Append(kRelativePath), | 444 actual_device_path.Append(kRelativePath), |
| 446 &win32_path)); | 445 &win32_path)); |
| 447 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(), | 446 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(), |
| 448 win32_path.value()); | 447 win32_path.value()); |
| 449 | 448 |
| 450 // Deform the real path so that it is invalid by removing the last four | 449 // Deform the real path so that it is invalid by removing the last four |
| 451 // characters. The way windows names devices that are hard disks | 450 // characters. The way windows names devices that are hard disks |
| 452 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer | 451 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer |
| 453 // than three characters. The only way the truncated string could be a | 452 // than three characters. The only way the truncated string could be a |
| 454 // real drive is if more than 10^3 disks are mounted: | 453 // real drive is if more than 10^3 disks are mounted: |
| 455 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1 | 454 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1 |
| 456 // Check that DevicePathToDriveLetterPath fails. | 455 // Check that DevicePathToDriveLetterPath fails. |
| 457 int path_length = actual_device_path.value().length(); | 456 int path_length = actual_device_path.value().length(); |
| 458 int new_length = path_length - 4; | 457 int new_length = path_length - 4; |
| 459 ASSERT_LT(0, new_length); | 458 ASSERT_LT(0, new_length); |
| 460 FilePath prefix_of_real_device_path( | 459 FilePath prefix_of_real_device_path( |
| 461 actual_device_path.value().substr(0, new_length)); | 460 actual_device_path.value().substr(0, new_length)); |
| 462 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath( | 461 ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path, |
| 463 prefix_of_real_device_path, | 462 &win32_path)); |
| 464 &win32_path)); | |
| 465 | 463 |
| 466 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath( | 464 ASSERT_FALSE(DevicePathToDriveLetterPath( |
| 467 prefix_of_real_device_path.Append(kRelativePath), | 465 prefix_of_real_device_path.Append(kRelativePath), |
| 468 &win32_path)); | 466 &win32_path)); |
| 469 | 467 |
| 470 // Deform the real path so that it is invalid by adding some characters. For | 468 // Deform the real path so that it is invalid by adding some characters. For |
| 471 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a | 469 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a |
| 472 // request for the drive letter whose native path is | 470 // request for the drive letter whose native path is |
| 473 // \Device\HardDiskVolume812345 . We assume such a device does not exist, | 471 // \Device\HardDiskVolume812345 . We assume such a device does not exist, |
| 474 // because drives are numbered in order and mounting 112345 hard disks will | 472 // because drives are numbered in order and mounting 112345 hard disks will |
| 475 // never happen. | 473 // never happen. |
| 476 const FilePath::StringType kExtraChars = FPL("12345"); | 474 const FilePath::StringType kExtraChars = FPL("12345"); |
| 477 | 475 |
| 478 FilePath real_device_path_plus_numbers( | 476 FilePath real_device_path_plus_numbers( |
| 479 actual_device_path.value() + kExtraChars); | 477 actual_device_path.value() + kExtraChars); |
| 480 | 478 |
| 481 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath( | 479 ASSERT_FALSE(DevicePathToDriveLetterPath( |
| 482 real_device_path_plus_numbers, | 480 real_device_path_plus_numbers, |
| 483 &win32_path)); | 481 &win32_path)); |
| 484 | 482 |
| 485 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath( | 483 ASSERT_FALSE(DevicePathToDriveLetterPath( |
| 486 real_device_path_plus_numbers.Append(kRelativePath), | 484 real_device_path_plus_numbers.Append(kRelativePath), |
| 487 &win32_path)); | 485 &win32_path)); |
| 488 } | 486 } |
| 489 | 487 |
| 490 TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) { | 488 TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) { |
| 491 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test")); | 489 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test")); |
| 492 ASSERT_TRUE(base::CreateDirectory(empty_dir)); | 490 ASSERT_TRUE(base::CreateDirectory(empty_dir)); |
| 493 win::ScopedHandle dir( | 491 win::ScopedHandle dir( |
| 494 ::CreateFile(empty_dir.value().c_str(), | 492 ::CreateFile(empty_dir.value().c_str(), |
| 495 FILE_ALL_ACCESS, | 493 FILE_ALL_ACCESS, |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 // Create a non-existent file path. | 687 // Create a non-existent file path. |
| 690 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt")); | 688 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt")); |
| 691 EXPECT_FALSE(PathExists(non_existent)); | 689 EXPECT_FALSE(PathExists(non_existent)); |
| 692 | 690 |
| 693 // Create a symlink to the non-existent file. | 691 // Create a symlink to the non-existent file. |
| 694 FilePath file_link = temp_dir_.path().Append("file_link_3"); | 692 FilePath file_link = temp_dir_.path().Append("file_link_3"); |
| 695 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link)) | 693 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link)) |
| 696 << "Failed to create symlink."; | 694 << "Failed to create symlink."; |
| 697 | 695 |
| 698 // Make sure the symbolic link is exist. | 696 // Make sure the symbolic link is exist. |
| 699 EXPECT_TRUE(file_util::IsLink(file_link)); | 697 EXPECT_TRUE(IsLink(file_link)); |
| 700 EXPECT_FALSE(PathExists(file_link)); | 698 EXPECT_FALSE(PathExists(file_link)); |
| 701 | 699 |
| 702 // Delete the symbolic link. | 700 // Delete the symbolic link. |
| 703 EXPECT_TRUE(DeleteFile(file_link, false)); | 701 EXPECT_TRUE(DeleteFile(file_link, false)); |
| 704 | 702 |
| 705 // Make sure the symbolic link is deleted. | 703 // Make sure the symbolic link is deleted. |
| 706 EXPECT_FALSE(file_util::IsLink(file_link)); | 704 EXPECT_FALSE(IsLink(file_link)); |
| 707 } | 705 } |
| 708 | 706 |
| 709 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) { | 707 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) { |
| 710 // Create a file path. | 708 // Create a file path. |
| 711 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); | 709 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
| 712 EXPECT_FALSE(PathExists(file_name)); | 710 EXPECT_FALSE(PathExists(file_name)); |
| 713 | 711 |
| 714 const std::string kData("hello"); | 712 const std::string kData("hello"); |
| 715 | 713 |
| 716 int buffer_size = kData.length(); | 714 int buffer_size = kData.length(); |
| (...skipping 1191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1908 &access_time)); | 1906 &access_time)); |
| 1909 | 1907 |
| 1910 Time modification_time; | 1908 Time modification_time; |
| 1911 // Note that this timestamp is divisible by two (seconds) - FAT stores | 1909 // Note that this timestamp is divisible by two (seconds) - FAT stores |
| 1912 // modification times with 2s resolution. | 1910 // modification times with 2s resolution. |
| 1913 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", | 1911 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", |
| 1914 &modification_time)); | 1912 &modification_time)); |
| 1915 | 1913 |
| 1916 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time)); | 1914 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time)); |
| 1917 PlatformFileInfo file_info; | 1915 PlatformFileInfo file_info; |
| 1918 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info)); | 1916 ASSERT_TRUE(GetFileInfo(foobar, &file_info)); |
| 1919 EXPECT_EQ(file_info.last_accessed.ToInternalValue(), | 1917 EXPECT_EQ(file_info.last_accessed.ToInternalValue(), |
| 1920 access_time.ToInternalValue()); | 1918 access_time.ToInternalValue()); |
| 1921 EXPECT_EQ(file_info.last_modified.ToInternalValue(), | 1919 EXPECT_EQ(file_info.last_modified.ToInternalValue(), |
| 1922 modification_time.ToInternalValue()); | 1920 modification_time.ToInternalValue()); |
| 1923 } | 1921 } |
| 1924 | 1922 |
| 1925 TEST_F(FileUtilTest, IsDirectoryEmpty) { | 1923 TEST_F(FileUtilTest, IsDirectoryEmpty) { |
| 1926 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir")); | 1924 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir")); |
| 1927 | 1925 |
| 1928 ASSERT_FALSE(PathExists(empty_dir)); | 1926 ASSERT_FALSE(PathExists(empty_dir)); |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2349 int fd = OpenContentUriForRead(path); | 2347 int fd = OpenContentUriForRead(path); |
| 2350 EXPECT_EQ(-1, fd); | 2348 EXPECT_EQ(-1, fd); |
| 2351 } | 2349 } |
| 2352 #endif | 2350 #endif |
| 2353 | 2351 |
| 2354 #endif // defined(OS_POSIX) | 2352 #endif // defined(OS_POSIX) |
| 2355 | 2353 |
| 2356 } // namespace | 2354 } // namespace |
| 2357 | 2355 |
| 2358 } // namespace base | 2356 } // namespace base |
| OLD | NEW |