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