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

Unified 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 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 side-by-side diff with in-line comments
Download patch
Index: base/file_util_unittest.cc
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 0cade2ae6f07be030ff4c5056e5ddba8233c0da8..16afab0aefa5df713f295865a10dfaa40d98867f 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -23,16 +23,25 @@
#include "base/scoped_temp_dir.h"
#include "base/threading/platform_thread.h"
#include "base/utf_string_conversions.h"
+#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#if defined(OS_WIN)
#include "base/win/scoped_handle.h"
+#include "base/win/win_api_interface.h"
#endif
// This macro helps avoid wrapped lines in the test structs.
#define FPL(x) FILE_PATH_LITERAL(x)
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::SetArrayArgument;
+using ::testing::StrictMock;
+
namespace {
// To test that file_util::Normalize FilePath() deals with NTFS reparse points
@@ -599,6 +608,157 @@ TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
EXPECT_EQ(0, info.size);
}
+namespace {
+
+#if defined(OS_WIN)
+class MockWindowsApi : public base::win::WinApiInterface {
+ public:
+ MOCK_METHOD3(QueryDosDevice, DWORD (LPCTSTR lpDeviceName,
+ LPTSTR lpTargetPath,
+ DWORD ucchMax));
+ MOCK_METHOD2(GetLogicalDriveStrings, DWORD (DWORD nBufferLength,
+ LPTSTR lpBuffer));
+};
+#endif // defined(OS_WIN)
+
+} // namespace
+
+TEST_F(FileUtilTest, DevicePathToDriveLetter) {
+ FilePath drive_letter_path;
+ const FilePath kDevicePath(FPL("\\Device\\HardDiskVolume12\\foo\\bar"));
+
+ const TCHAR kEmptyDriveMapping[] = _TEXT("\0\0");
+ const int kEmptyDriveMappingLength = arraysize(kEmptyDriveMapping);
+ const TCHAR kSampleDriveMapping[] = _TEXT("G:\\\0H:\\\0I:\\\0\0");
+ const int kSampleDriveMappingLength = arraysize(kSampleDriveMapping);
+
+ const TCHAR kDeviceNoMatch[] = _TEXT("\\Device\\no_match");
+ const int kDeviceNoMatchLength = arraysize(kDeviceNoMatch);
+
+ const TCHAR kDeviceSubstring[] = _TEXT("\\Device\\HardDiskVolume1");
+ const int kDeviceSubstringLength = arraysize(kDeviceSubstring);
+
+ const TCHAR kDeviceShouldMatch[] = _TEXT("\\Device\\HardDiskVolume12");
+ const int kDeviceShouldMatchLength = arraysize(kDeviceShouldMatch);
+
+ {
+ // Fake failure of GetLogicalDriveStrings().
+ StrictMock<MockWindowsApi> mock_win_api;
+ EXPECT_CALL(mock_win_api, GetLogicalDriveStrings(_, _)).WillOnce(DoAll(
+ SetArrayArgument<1>(kEmptyDriveMapping,
+ kEmptyDriveMapping + kEmptyDriveMappingLength),
+ Return(0)));
+
+ ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(mock_win_api,
+ kDevicePath,
+ &drive_letter_path));
+ }
+ {
+ // Fake an empty set of mapped drives.
+ StrictMock<MockWindowsApi> mock_win_api;
+ EXPECT_CALL(mock_win_api, GetLogicalDriveStrings(_, _)).WillOnce(DoAll(
+ SetArrayArgument<1>(kEmptyDriveMapping,
+ kEmptyDriveMapping + kEmptyDriveMappingLength),
+ Return(1)));
+
+ ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(mock_win_api,
+ kDevicePath,
+ &drive_letter_path));
+ }
+ {
+ // QueryDosDevice always fails.
+ StrictMock<MockWindowsApi> mock_win_api;
+ EXPECT_CALL(mock_win_api, GetLogicalDriveStrings(_, _)).WillOnce(DoAll(
+ SetArrayArgument<1>(kSampleDriveMapping,
+ kSampleDriveMapping + kSampleDriveMappingLength),
+ Return(1)));
+
+ EXPECT_CALL(mock_win_api, QueryDosDevice(_, _, _)).Times(3)
+ .WillRepeatedly(Return(0));
+
+ ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(mock_win_api,
+ kDevicePath,
+ &drive_letter_path));
+ }
+ {
+ // QueryDosDevice succeeds, but no DOS device matches the NT path.
+ StrictMock<MockWindowsApi> mock_win_api;
+
+ EXPECT_CALL(mock_win_api, GetLogicalDriveStrings(_, _)).WillOnce(DoAll(
+ SetArrayArgument<1>(kSampleDriveMapping,
+ kSampleDriveMapping + kSampleDriveMappingLength),
+ Return(1)));
+
+ EXPECT_CALL(mock_win_api, QueryDosDevice(_, _, _)).Times(3)
+ .WillRepeatedly(DoAll(
+ SetArrayArgument<1>(kDeviceNoMatch,
+ kDeviceNoMatch + kDeviceNoMatchLength),
+ Return(1)));
+
+ ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(mock_win_api,
+ kDevicePath,
+ &drive_letter_path));
+ }
+ {
+ // QueryDosDevice matches the third item in the list of devices.
+ StrictMock<MockWindowsApi> mock_win_api;
+
+ EXPECT_CALL(mock_win_api, GetLogicalDriveStrings(_, _)).WillOnce(DoAll(
+ SetArrayArgument<1>(kSampleDriveMapping,
+ kSampleDriveMapping + kSampleDriveMappingLength),
+ Return(1)));
+
+ EXPECT_CALL(mock_win_api, QueryDosDevice(_, _, _)).Times(3)
+ // First drive letter does not match.
+ .WillOnce(DoAll(
+ SetArrayArgument<1>(kDeviceNoMatch,
+ kDeviceNoMatch + kDeviceNoMatchLength),
+ Return(1)))
+ // Second drive letter has a path that is a substring of the correct
+ // path. Should not match.
+ .WillOnce(DoAll(
+ SetArrayArgument<1>(kDeviceSubstring,
+ kDeviceSubstring + kDeviceSubstringLength),
+ Return(1)))
+ // Third drive letter matches. Should return this one.
+ .WillOnce(DoAll(
+ SetArrayArgument<1>(kDeviceShouldMatch,
+ kDeviceShouldMatch + kDeviceShouldMatchLength),
+ Return(1)));
+
+ ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(mock_win_api,
+ kDevicePath,
+ &drive_letter_path));
+ ASSERT_EQ(FPL("I:\\foo\\bar"), drive_letter_path.value());
+ }
+ {
+ // Match a path to a drive letter, with no extra path.
+ StrictMock<MockWindowsApi> mock_win_api;
+
+ EXPECT_CALL(mock_win_api, GetLogicalDriveStrings(_, _)).WillOnce(DoAll(
+ SetArrayArgument<1>(kSampleDriveMapping,
+ kSampleDriveMapping + kSampleDriveMappingLength),
+ Return(1)));
+
+ EXPECT_CALL(mock_win_api, QueryDosDevice(_, _, _)).Times(2)
+ // First call to QueryDosDevice fails. Shoudl keep going.
+ .WillOnce(Return(0))
+
+ // Next call matches a drive letter. Should return this one.
+ .WillOnce(DoAll(
+ SetArrayArgument<1>(kDeviceShouldMatch,
+ kDeviceShouldMatch + kDeviceShouldMatchLength),
+ Return(1)));
+ // After matching, QueryDosDevice should not be called again.
+
+ const FilePath kNtPathToDriveI(FPL("\\Device\\HardDiskVolume12"));
+ ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(mock_win_api,
+ kNtPathToDriveI,
+ &drive_letter_path));
+ ASSERT_EQ(FPL("H:"), drive_letter_path.value());
+ }
+}
+
#endif // defined(OS_WIN)
#if defined(OS_POSIX)

Powered by Google App Engine
This is Rietveld 408576698