1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/common/path_util.h"
16 :
17 : #include <windows.h>
18 :
19 : #include "gmock/gmock.h"
20 : #include "gtest/gtest.h"
21 :
22 : namespace common {
23 :
24 : namespace {
25 :
26 : void GetCurrentDriveAndDevice(std::wstring* drive_out,
27 E : std::wstring* device_out) {
28 E : ASSERT_TRUE(drive_out != NULL);
29 E : ASSERT_TRUE(device_out != NULL);
30 :
31 E : std::wstring cwd(MAX_PATH, 0);
32 E : DWORD status = ::GetCurrentDirectory(cwd.size(), &cwd[0]);
33 E : ASSERT_GT(status, 0u);
34 :
35 E : std::vector<std::wstring> cwd_components;
36 E : base::FilePath(cwd).GetComponents(&cwd_components);
37 E : ASSERT_GT(cwd_components.size(), 0u);
38 E : const std::wstring& drive = cwd_components[0];
39 :
40 : // Get the device name associated with the current drive. We know it exists
41 : // as the drive exists.
42 E : wchar_t device[MAX_PATH] = { 0 };
43 E : status = ::QueryDosDevice(drive.c_str(), device, arraysize(device));
44 E : ASSERT_GT(status, 0u);
45 E : ASSERT_LE(status, arraysize(device));
46 :
47 E : *drive_out = drive;
48 E : *device_out = device;
49 :
50 E : ASSERT_GT(drive_out->size(), 0u);
51 E : ASSERT_GT(device_out->size(), 0u);
52 E : }
53 :
54 : class PathUtilTest : public ::testing::Test {
55 : public:
56 E : virtual void SetUp() override {
57 E : ASSERT_NO_FATAL_FAILURE(GetCurrentDriveAndDevice(&cur_drive_,
58 : &cur_device_));
59 E : }
60 :
61 : std::wstring cur_drive_;
62 : std::wstring cur_device_;
63 : };
64 :
65 : } // namespace
66 :
67 E : TEST_F(PathUtilTest, ConvertDevicePathToDrivePathWithDrivePath) {
68 E : base::FilePath device(L"C:\\foo.txt");
69 E : base::FilePath drive;
70 E : ASSERT_TRUE(ConvertDevicePathToDrivePath(device, &drive));
71 E : ASSERT_EQ(device.value(), drive.value());
72 E : }
73 :
74 E : TEST_F(PathUtilTest, ConvertDevicePathToDrivePathWithNonExistentDevicePath) {
75 E : base::FilePath device(L"\\Device\\ThisDeviceDoesNotExist\\foo.txt");
76 E : base::FilePath drive;
77 E : ASSERT_TRUE(ConvertDevicePathToDrivePath(device, &drive));
78 E : ASSERT_EQ(device.value(), drive.value());
79 E : }
80 :
81 E : TEST_F(PathUtilTest, ConvertDevicePathToDrivePathWithDevicePath) {
82 E : base::FilePath device(cur_device_);
83 E : device = device.Append(L"foo.txt");
84 :
85 E : base::FilePath drive;
86 E : ASSERT_TRUE(ConvertDevicePathToDrivePath(device, &drive));
87 :
88 : // We can't use FilePath::Append directly, as both ":" and "\" are seen as
89 : // delimiters. Thus, appending "foo.txt" to "C:" yields "C:foo.txt", which
90 : // is not exactly what we want.
91 E : base::FilePath expected_drive(std::wstring(cur_drive_).append(L"\\foo.txt"));
92 E : ASSERT_THAT(expected_drive.value(), ::testing::StrCaseEq(drive.value()));
93 E : }
94 :
95 E : TEST_F(PathUtilTest, ConvertDevicePathToDrivePathWithDeviceOnly) {
96 E : base::FilePath device(cur_device_);
97 E : base::FilePath drive;
98 E : ASSERT_TRUE(ConvertDevicePathToDrivePath(device, &drive));
99 :
100 E : ASSERT_THAT(cur_drive_, ::testing::StrCaseEq(drive.value()));
101 E : }
102 :
103 E : TEST_F(PathUtilTest, ConvertDevicePathToDrivePathWithDeviceWithPrefix) {
104 : // This tries to convert an invalid device name that contains a valid
105 : // device as a prefix. The conversion should do nothing.
106 E : base::FilePath device(std::wstring(cur_device_).append(L"1234567"));
107 E : device = device.Append(L"foo.txt");
108 :
109 E : base::FilePath drive;
110 E : ASSERT_TRUE(ConvertDevicePathToDrivePath(device, &drive));
111 :
112 E : ASSERT_EQ(device.value(), drive.value());
113 E : }
114 :
115 : } // namespace common
|