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