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

Side by Side Diff: base/file_util_unittest.cc

Issue 9167004: Match whole path components in DevicePathToDriveLetterPath(). Add tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rev comments. Created 8 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 | « base/file_util.h ('k') | base/file_util_win.cc » ('j') | 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) 2011 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>
11 #include <tchar.h> 11 #include <tchar.h>
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 // Delete the reparse points, and see that NormalizeFilePath() fails 573 // Delete the reparse points, and see that NormalizeFilePath() fails
574 // to traverse them. 574 // to traverse them.
575 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a)); 575 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
576 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b)); 576 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
577 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long)); 577 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
578 578
579 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")), 579 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
580 &normalized_path)); 580 &normalized_path));
581 } 581 }
582 582
583 TEST_F(FileUtilTest, DevicePathToDriveLetter) {
584 // Get a drive letter.
585 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
586 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
587 LOG(ERROR) << "Can't get a drive letter to test with.";
588 return;
589 }
590
591 // Get the NT style path to that drive.
592 wchar_t device_path[MAX_PATH] = {'\0'};
593 ASSERT_TRUE(
594 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
595 FilePath actual_device_path(device_path);
596 FilePath win32_path;
597
598 // Run DevicePathToDriveLetterPath() on the NT style path we got from
599 // QueryDosDevice(). Expect the drive letter we started with.
600 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path,
601 &win32_path));
602 ASSERT_EQ(real_drive_letter, win32_path.value());
603
604 // Add some directories to the path. Expect those extra path componenets
605 // to be preserved.
606 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
607 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
608 actual_device_path.Append(kRelativePath),
609 &win32_path));
610 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
611 win32_path.value());
612
613 // Deform the real path so that it is invalid by removing the last four
614 // characters. The way windows names devices that are hard disks
615 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
616 // than three characters. The only way the truncated string could be a
617 // real drive is if more than 10^3 disks are mounted:
618 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
619 // Check that DevicePathToDriveLetterPath fails.
620 int path_length = actual_device_path.value().length();
621 int new_length = path_length - 4;
622 ASSERT_LT(0, new_length);
623 FilePath prefix_of_real_device_path(
624 actual_device_path.value().substr(0, new_length));
625 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
626 prefix_of_real_device_path,
627 &win32_path));
628
629 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
630 prefix_of_real_device_path.Append(kRelativePath),
631 &win32_path));
632
633 // Deform the real path so that it is invalid by adding some characters. For
634 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
635 // request for the drive letter whose native path is
636 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
637 // because drives are numbered in order and mounting 112345 hard disks will
638 // never happen.
639 const FilePath::StringType kExtraChars = FPL("12345");
640
641 FilePath real_device_path_plus_numbers(
642 actual_device_path.value() + kExtraChars);
643
644 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
645 real_device_path_plus_numbers,
646 &win32_path));
647
648 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
649 real_device_path_plus_numbers.Append(kRelativePath),
650 &win32_path));
651 }
652
583 TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) { 653 TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
584 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test")); 654 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test"));
585 ASSERT_TRUE(file_util::CreateDirectory(empty_dir)); 655 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
586 base::win::ScopedHandle dir( 656 base::win::ScopedHandle dir(
587 ::CreateFile(empty_dir.value().c_str(), 657 ::CreateFile(empty_dir.value().c_str(),
588 FILE_ALL_ACCESS, 658 FILE_ALL_ACCESS,
589 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 659 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
590 NULL, 660 NULL,
591 OPEN_EXISTING, 661 OPEN_EXISTING,
592 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. 662 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
(...skipping 1623 matching lines...) Expand 10 before | Expand all | Expand 10 after
2216 file_util::VerifyPathControlledByUser( 2286 file_util::VerifyPathControlledByUser(
2217 base_dir_, text_file_, uid_, ok_gids_)); 2287 base_dir_, text_file_, uid_, ok_gids_));
2218 EXPECT_TRUE( 2288 EXPECT_TRUE(
2219 file_util::VerifyPathControlledByUser( 2289 file_util::VerifyPathControlledByUser(
2220 sub_dir_, text_file_, uid_, ok_gids_)); 2290 sub_dir_, text_file_, uid_, ok_gids_));
2221 } 2291 }
2222 2292
2223 #endif // defined(OS_POSIX) 2293 #endif // defined(OS_POSIX)
2224 2294
2225 } // namespace 2295 } // namespace
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | base/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698