1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/test/test_shortcut_win.h"
6
7 #include <windows.h>
8
9 #include <objbase.h>
10 #include <shlobj.h>
11 #include <propkey.h>
12 #include <wrl/client.h>
13
14 #include <string>
15
16 #include "base/files/file_path.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/win/scoped_propvariant.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace base {
23 namespace win {
24
ValidatePathsAreEqual(const FilePath & expected_path,const FilePath & actual_path)25 void ValidatePathsAreEqual(const FilePath& expected_path,
26 const FilePath& actual_path) {
27 wchar_t long_expected_path_chars[MAX_PATH] = {0};
28 wchar_t long_actual_path_chars[MAX_PATH] = {0};
29
30 // If |expected_path| is empty confirm immediately that |actual_path| is also
31 // empty.
32 if (expected_path.empty()) {
33 EXPECT_TRUE(actual_path.empty());
34 return;
35 }
36
37 // Proceed with LongPathName matching which will also confirm the paths exist.
38 EXPECT_NE(0U, ::GetLongPathName(expected_path.value().c_str(),
39 long_expected_path_chars, MAX_PATH))
40 << "Failed to get LongPathName of " << expected_path.value();
41 EXPECT_NE(0U, ::GetLongPathName(actual_path.value().c_str(),
42 long_actual_path_chars, MAX_PATH))
43 << "Failed to get LongPathName of " << actual_path.value();
44
45 FilePath long_expected_path(long_expected_path_chars);
46 FilePath long_actual_path(long_actual_path_chars);
47 EXPECT_FALSE(long_expected_path.empty());
48 EXPECT_FALSE(long_actual_path.empty());
49
50 EXPECT_TRUE(base::FilePath::CompareEqualIgnoreCase(long_expected_path.value(),
51 long_actual_path.value()));
52 }
53
ValidateShortcut(const FilePath & shortcut_path,const ShortcutProperties & properties)54 void ValidateShortcut(const FilePath& shortcut_path,
55 const ShortcutProperties& properties) {
56 Microsoft::WRL::ComPtr<IShellLink> i_shell_link;
57 Microsoft::WRL::ComPtr<IPersistFile> i_persist_file;
58
59 wchar_t read_target[MAX_PATH] = {0};
60 wchar_t read_working_dir[MAX_PATH] = {0};
61 wchar_t read_arguments[MAX_PATH] = {0};
62 wchar_t read_description[MAX_PATH] = {0};
63 wchar_t read_icon[MAX_PATH] = {0};
64 int read_icon_index = 0;
65
66 HRESULT hr;
67
68 // Initialize the shell interfaces.
69 EXPECT_TRUE(SUCCEEDED(hr = ::CoCreateInstance(CLSID_ShellLink, NULL,
70 CLSCTX_INPROC_SERVER,
71 IID_PPV_ARGS(&i_shell_link))));
72 if (FAILED(hr))
73 return;
74
75 EXPECT_TRUE(SUCCEEDED(hr = i_shell_link.As(&i_persist_file)));
76 if (FAILED(hr))
77 return;
78
79 // Load the shortcut.
80 EXPECT_TRUE(
81 SUCCEEDED(hr = i_persist_file->Load(shortcut_path.value().c_str(), 0)))
82 << "Failed to load shortcut at " << shortcut_path.value();
83 if (FAILED(hr))
84 return;
85
86 if (properties.options & ShortcutProperties::PROPERTIES_TARGET) {
87 EXPECT_TRUE(SUCCEEDED(
88 i_shell_link->GetPath(read_target, MAX_PATH, NULL, SLGP_SHORTPATH)));
89 ValidatePathsAreEqual(properties.target, FilePath(read_target));
90 }
91
92 if (properties.options & ShortcutProperties::PROPERTIES_WORKING_DIR) {
93 EXPECT_TRUE(SUCCEEDED(
94 i_shell_link->GetWorkingDirectory(read_working_dir, MAX_PATH)));
95 ValidatePathsAreEqual(properties.working_dir, FilePath(read_working_dir));
96 }
97
98 if (properties.options & ShortcutProperties::PROPERTIES_ARGUMENTS) {
99 EXPECT_TRUE(
100 SUCCEEDED(i_shell_link->GetArguments(read_arguments, MAX_PATH)));
101 EXPECT_EQ(properties.arguments, read_arguments);
102 }
103
104 if (properties.options & ShortcutProperties::PROPERTIES_DESCRIPTION) {
105 EXPECT_TRUE(
106 SUCCEEDED(i_shell_link->GetDescription(read_description, MAX_PATH)));
107 EXPECT_EQ(properties.description, read_description);
108 }
109
110 if (properties.options & ShortcutProperties::PROPERTIES_ICON) {
111 EXPECT_TRUE(SUCCEEDED(
112 i_shell_link->GetIconLocation(read_icon, MAX_PATH, &read_icon_index)));
113 ValidatePathsAreEqual(properties.icon, FilePath(read_icon));
114 EXPECT_EQ(properties.icon_index, read_icon_index);
115 }
116
117 Microsoft::WRL::ComPtr<IPropertyStore> property_store;
118 EXPECT_TRUE(SUCCEEDED(hr = i_shell_link.As(&property_store)));
119 if (FAILED(hr))
120 return;
121
122 if (properties.options & ShortcutProperties::PROPERTIES_APP_ID) {
123 ScopedPropVariant pv_app_id;
124 EXPECT_EQ(S_OK, property_store->GetValue(PKEY_AppUserModel_ID,
125 pv_app_id.Receive()));
126 switch (pv_app_id.get().vt) {
127 case VT_EMPTY:
128 EXPECT_TRUE(properties.app_id.empty());
129 break;
130 case VT_LPWSTR:
131 EXPECT_EQ(properties.app_id, pv_app_id.get().pwszVal);
132 break;
133 default:
134 ADD_FAILURE() << "Unexpected variant type: " << pv_app_id.get().vt;
135 }
136 }
137
138 if (properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) {
139 ScopedPropVariant pv_dual_mode;
140 EXPECT_EQ(S_OK, property_store->GetValue(PKEY_AppUserModel_IsDualMode,
141 pv_dual_mode.Receive()));
142 switch (pv_dual_mode.get().vt) {
143 case VT_EMPTY:
144 EXPECT_FALSE(properties.dual_mode);
145 break;
146 case VT_BOOL:
147 EXPECT_EQ(properties.dual_mode,
148 static_cast<bool>(pv_dual_mode.get().boolVal));
149 break;
150 default:
151 ADD_FAILURE() << "Unexpected variant type: " << pv_dual_mode.get().vt;
152 }
153 }
154 }
155
156 } // namespace win
157 } // namespace base
158