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

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: Avoid mocking QueryDosDevice 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(
601 actual_device_path,
rvargas (doing something else) 2012/01/12 21:01:04 nit: it looks like this fits in the previous line
Sam Kerner (Chrome) 2012/01/12 21:19:12 Done.
602 &win32_path));
603 ASSERT_EQ(real_drive_letter, win32_path.value());
604
605 // Add some directories to the path. Expect those extra path componenets
606 // to be preserved.
607 FilePath kRelitivePath(FPL("dir1\\dir2\\file.txt"));
rvargas (doing something else) 2012/01/12 21:01:04 nit: kRelative
Sam Kerner (Chrome) 2012/01/12 21:19:12 Done.
608 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
609 actual_device_path.Append(kRelitivePath),
610 &win32_path));
611 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelitivePath).value(),
612 win32_path.value());
613
614 // Deform the real path so that it is invalid by removing the last four
615 // characters. The way windows names devices that are hard disks
616 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
617 // than three characters. The only way the truncated string could be a
618 // real drive is if more than 10^3 disks are mounted:
619 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
620 // Check that DevicePathToDriveLetterPath fails.
621 int path_length = actual_device_path.value().length();
622 int new_length = path_length - 4;
623 ASSERT_LT(0, new_length);
624 FilePath prefix_of_real_device_path(
625 actual_device_path.value().substr(0, new_length));
626 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
627 prefix_of_real_device_path,
628 &win32_path));
629
630 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
631 prefix_of_real_device_path.Append(kRelitivePath),
632 &win32_path));
633
634 // Deform the real path so that it is invalid by adding some characters. For
635 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
636 // request for the drive letter whose native path is
637 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
638 // because drives are numbered in order and mounting 112345 hard disks will
639 // never happen.
640 const FilePath::StringType kExtraChars = FPL("12345");
641
642 FilePath real_device_path_plus_numbers(
643 actual_device_path.value() + kExtraChars);
644
645 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
Sam Kerner (Chrome) 2012/01/12 21:19:12 This assert will fail without the fix in file_util
646 real_device_path_plus_numbers,
647 &win32_path));
648
649 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
Sam Kerner (Chrome) 2012/01/12 21:19:12 This one will also fail.
650 real_device_path_plus_numbers.Append(kRelitivePath),
651 &win32_path));
652 }
653
583 TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) { 654 TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
584 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test")); 655 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test"));
585 ASSERT_TRUE(file_util::CreateDirectory(empty_dir)); 656 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
586 base::win::ScopedHandle dir( 657 base::win::ScopedHandle dir(
587 ::CreateFile(empty_dir.value().c_str(), 658 ::CreateFile(empty_dir.value().c_str(),
588 FILE_ALL_ACCESS, 659 FILE_ALL_ACCESS,
589 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 660 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
590 NULL, 661 NULL,
591 OPEN_EXISTING, 662 OPEN_EXISTING,
592 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. 663 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( 2287 file_util::VerifyPathControlledByUser(
2217 base_dir_, text_file_, uid_, ok_gids_)); 2288 base_dir_, text_file_, uid_, ok_gids_));
2218 EXPECT_TRUE( 2289 EXPECT_TRUE(
2219 file_util::VerifyPathControlledByUser( 2290 file_util::VerifyPathControlledByUser(
2220 sub_dir_, text_file_, uid_, ok_gids_)); 2291 sub_dir_, text_file_, uid_, ok_gids_));
2221 } 2292 }
2222 2293
2223 #endif // defined(OS_POSIX) 2294 #endif // defined(OS_POSIX)
2224 2295
2225 } // namespace 2296 } // 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