OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/test_shortcut_win.h" | 5 #include "base/test/test_shortcut_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <shlobj.h> | 8 #include <shlobj.h> |
9 #include <propkey.h> | 9 #include <propkey.h> |
10 #include <propvarutil.h> | 10 #include <propvarutil.h> |
11 | 11 |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
| 14 #include "base/utf_string_conversions.h" |
14 #include "base/win/scoped_comptr.h" | 15 #include "base/win/scoped_comptr.h" |
15 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 // propsys.lib is required for PropvariantTo*(). | 19 // propsys.lib is required for PropvariantTo*(). |
18 #pragma comment(lib, "propsys.lib") | 20 #pragma comment(lib, "propsys.lib") |
19 | 21 |
20 namespace base { | 22 namespace base { |
21 namespace win { | 23 namespace win { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 // Returns true if |actual_path|'s LongPathName case-insensitively matches | 27 // Validates |actual_path|'s LongPathName case-insensitively matches |
26 // |expected_path|'s LongPathName. | 28 // |expected_path|'s LongPathName. |
27 bool PathsAreEqual(const FilePath& expected_path, const FilePath& actual_path) { | 29 void ValidatePathsAreEqual(const FilePath& expected_path, |
| 30 const FilePath& actual_path) { |
28 wchar_t long_expected_path_chars[MAX_PATH] = {0}; | 31 wchar_t long_expected_path_chars[MAX_PATH] = {0}; |
29 wchar_t long_actual_path_chars[MAX_PATH] = {0}; | 32 wchar_t long_actual_path_chars[MAX_PATH] = {0}; |
30 | 33 |
31 if (::GetLongPathName( | 34 // If |expected_path| is empty confirm immediately that |actual_path| is also |
32 expected_path.value().c_str(), long_expected_path_chars, | 35 // empty. |
33 MAX_PATH) == 0 || | 36 if (expected_path.empty()) { |
34 ::GetLongPathName( | 37 EXPECT_TRUE(actual_path.empty()); |
35 actual_path.value().c_str(), long_actual_path_chars, | 38 return; |
36 MAX_PATH) == 0) { | |
37 return false; | |
38 } | 39 } |
39 | 40 |
| 41 // Proceed with LongPathName matching which will also confirm the paths exist. |
| 42 EXPECT_NE(0U, ::GetLongPathName( |
| 43 expected_path.value().c_str(), long_expected_path_chars, MAX_PATH)) |
| 44 << "Failed to get LongPathName of " << expected_path.value(); |
| 45 EXPECT_NE(0U, ::GetLongPathName( |
| 46 actual_path.value().c_str(), long_actual_path_chars, MAX_PATH)) |
| 47 << "Failed to get LongPathName of " << actual_path.value(); |
| 48 |
40 FilePath long_expected_path(long_expected_path_chars); | 49 FilePath long_expected_path(long_expected_path_chars); |
41 FilePath long_actual_path(long_actual_path_chars); | 50 FilePath long_actual_path(long_actual_path_chars); |
42 if(long_expected_path.empty() || long_actual_path.empty()) | 51 EXPECT_FALSE(long_expected_path.empty()); |
43 return false; | 52 EXPECT_FALSE(long_actual_path.empty()); |
44 | 53 |
45 return long_expected_path == long_actual_path; | 54 EXPECT_EQ(long_expected_path, long_actual_path); |
46 } | 55 } |
47 | 56 |
48 } // namespace | 57 } // namespace |
49 | 58 |
50 VerifyShortcutStatus VerifyShortcut(const FilePath& shortcut_path, | 59 void ValidateShortcut(const FilePath& shortcut_path, |
51 const ShortcutProperties& properties) { | 60 const ShortcutProperties& properties) { |
52 ScopedComPtr<IShellLink> i_shell_link; | 61 ScopedComPtr<IShellLink> i_shell_link; |
53 ScopedComPtr<IPersistFile> i_persist_file; | 62 ScopedComPtr<IPersistFile> i_persist_file; |
54 | 63 |
55 wchar_t read_target[MAX_PATH] = {0}; | 64 wchar_t read_target[MAX_PATH] = {0}; |
56 wchar_t read_working_dir[MAX_PATH] = {0}; | 65 wchar_t read_working_dir[MAX_PATH] = {0}; |
57 wchar_t read_arguments[MAX_PATH] = {0}; | 66 wchar_t read_arguments[MAX_PATH] = {0}; |
58 wchar_t read_description[MAX_PATH] = {0}; | 67 wchar_t read_description[MAX_PATH] = {0}; |
59 wchar_t read_icon[MAX_PATH] = {0}; | 68 wchar_t read_icon[MAX_PATH] = {0}; |
60 int read_icon_index = 0; | 69 int read_icon_index = 0; |
61 | 70 |
| 71 HRESULT hr; |
| 72 |
62 // Initialize the shell interfaces. | 73 // Initialize the shell interfaces. |
63 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | 74 EXPECT_TRUE(SUCCEEDED(hr = i_shell_link.CreateInstance( |
64 CLSCTX_INPROC_SERVER)) || | 75 CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER))); |
65 FAILED(i_persist_file.QueryFrom(i_shell_link))) { | 76 if (FAILED(hr)) |
66 return VERIFY_SHORTCUT_FAILURE_UNEXPECTED; | 77 return; |
| 78 |
| 79 EXPECT_TRUE(SUCCEEDED(hr = i_persist_file.QueryFrom(i_shell_link))); |
| 80 if (FAILED(hr)) |
| 81 return; |
| 82 |
| 83 // Load the shortcut. |
| 84 EXPECT_TRUE(SUCCEEDED(hr = i_persist_file->Load( |
| 85 shortcut_path.value().c_str(), 0))) << "Failed to load shortcut at " |
| 86 << shortcut_path.value(); |
| 87 if (FAILED(hr)) |
| 88 return; |
| 89 |
| 90 if (properties.options & ShortcutProperties::PROPERTIES_TARGET) { |
| 91 EXPECT_TRUE(SUCCEEDED( |
| 92 i_shell_link->GetPath(read_target, MAX_PATH, NULL, SLGP_SHORTPATH))); |
| 93 ValidatePathsAreEqual(properties.target, FilePath(read_target)); |
67 } | 94 } |
68 | 95 |
69 // Load the shortcut. | 96 if (properties.options & ShortcutProperties::PROPERTIES_WORKING_DIR) { |
70 if (FAILED(i_persist_file->Load(shortcut_path.value().c_str(), 0))) | 97 EXPECT_TRUE(SUCCEEDED( |
71 return VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND; | 98 i_shell_link->GetWorkingDirectory(read_working_dir, MAX_PATH))); |
72 | 99 ValidatePathsAreEqual(properties.working_dir, FilePath(read_working_dir)); |
73 if ((properties.options & ShortcutProperties::PROPERTIES_TARGET) && | |
74 (FAILED(i_shell_link->GetPath( | |
75 read_target, MAX_PATH, NULL, SLGP_SHORTPATH)) || | |
76 !PathsAreEqual(properties.target, FilePath(read_target)))) { | |
77 return VERIFY_SHORTCUT_FAILURE_TARGET; | |
78 } | 100 } |
79 | 101 |
80 if ((properties.options & ShortcutProperties::PROPERTIES_WORKING_DIR) && | 102 if (properties.options & ShortcutProperties::PROPERTIES_ARGUMENTS) { |
81 (FAILED(i_shell_link->GetWorkingDirectory(read_working_dir, MAX_PATH)) || | 103 EXPECT_TRUE(SUCCEEDED( |
82 FilePath(read_working_dir) != properties.working_dir)) { | 104 i_shell_link->GetArguments(read_arguments, MAX_PATH))); |
83 return VERIFY_SHORTCUT_FAILURE_WORKING_DIR; | 105 EXPECT_EQ(properties.arguments, read_arguments); |
84 } | 106 } |
85 | 107 |
86 if ((properties.options & ShortcutProperties::PROPERTIES_ARGUMENTS) && | 108 if (properties.options & ShortcutProperties::PROPERTIES_DESCRIPTION) { |
87 (FAILED(i_shell_link->GetArguments(read_arguments, MAX_PATH)) || | 109 EXPECT_TRUE(SUCCEEDED( |
88 string16(read_arguments) != properties.arguments)) { | 110 i_shell_link->GetDescription(read_description, MAX_PATH))); |
89 return VERIFY_SHORTCUT_FAILURE_ARGUMENTS; | 111 EXPECT_EQ(properties.description, read_description); |
90 } | 112 } |
91 | 113 |
92 if ((properties.options & ShortcutProperties::PROPERTIES_DESCRIPTION) && | 114 if (properties.options & ShortcutProperties::PROPERTIES_ICON) { |
93 (FAILED(i_shell_link->GetDescription(read_description, MAX_PATH)) || | 115 EXPECT_TRUE(SUCCEEDED( |
94 string16(read_description) != properties.description)) { | 116 i_shell_link->GetIconLocation(read_icon, MAX_PATH, &read_icon_index))); |
95 return VERIFY_SHORTCUT_FAILURE_DESCRIPTION; | 117 ValidatePathsAreEqual(properties.icon, FilePath(read_icon)); |
| 118 EXPECT_EQ(properties.icon_index, read_icon_index); |
96 } | 119 } |
97 | 120 |
98 if ((properties.options & ShortcutProperties::PROPERTIES_ICON) && | 121 if (GetVersion() >= VERSION_WIN7) { |
99 (FAILED(i_shell_link->GetIconLocation(read_icon, MAX_PATH, | |
100 &read_icon_index)) || | |
101 read_icon_index != properties.icon_index || | |
102 !PathsAreEqual(properties.icon, FilePath(read_icon)))) { | |
103 return VERIFY_SHORTCUT_FAILURE_ICON; | |
104 } | |
105 | |
106 if(GetVersion() >= VERSION_WIN7) { | |
107 ScopedComPtr<IPropertyStore> property_store; | 122 ScopedComPtr<IPropertyStore> property_store; |
108 // Note that, as mentioned on MSDN at http://goo.gl/M8h9g, if a property is | 123 // Note that, as mentioned on MSDN at http://goo.gl/M8h9g, if a property is |
109 // not set, GetValue will return S_OK and the PROPVARIANT will be set to | 124 // not set, GetValue will return S_OK and the PROPVARIANT will be set to |
110 // VT_EMPTY. | 125 // VT_EMPTY. |
111 PROPVARIANT pv_app_id, pv_dual_mode; | 126 PROPVARIANT pv_app_id, pv_dual_mode; |
112 if (FAILED(property_store.QueryFrom(i_shell_link)) || | 127 EXPECT_TRUE(SUCCEEDED(hr = property_store.QueryFrom(i_shell_link))); |
113 property_store->GetValue(PKEY_AppUserModel_ID, &pv_app_id) != S_OK || | 128 if (FAILED(hr)) |
114 property_store->GetValue(PKEY_AppUserModel_IsDualMode, | 129 return; |
115 &pv_dual_mode) != S_OK) { | 130 EXPECT_EQ(S_OK, property_store->GetValue(PKEY_AppUserModel_ID, &pv_app_id)); |
116 return VERIFY_SHORTCUT_FAILURE_UNEXPECTED; | 131 EXPECT_EQ(S_OK, property_store->GetValue(PKEY_AppUserModel_IsDualMode, |
117 } | 132 &pv_dual_mode)); |
118 | 133 |
119 // Note, as mentioned on MSDN at http://goo.gl/hZ3sO, if |pv_app_id| is a | 134 // Note, as mentioned on MSDN at |
120 // VT_EMPTY it is successfully converted to the empty string. | 135 // http://msdn.microsoft.com/library/windows/desktop/bb776559.aspx, if |
| 136 // |pv_app_id| is a VT_EMPTY it is successfully converted to the empty |
| 137 // string as desired. |
121 wchar_t read_app_id[MAX_PATH] = {0}; | 138 wchar_t read_app_id[MAX_PATH] = {0}; |
122 PropVariantToString(pv_app_id, read_app_id, MAX_PATH); | 139 PropVariantToString(pv_app_id, read_app_id, MAX_PATH); |
123 if((properties.options & ShortcutProperties::PROPERTIES_APP_ID) && | 140 if (properties.options & ShortcutProperties::PROPERTIES_APP_ID) |
124 string16(read_app_id) != properties.app_id) { | 141 EXPECT_EQ(properties.app_id, read_app_id); |
125 return VERIFY_SHORTCUT_FAILURE_APP_ID; | |
126 } | |
127 | 142 |
128 // Note, as mentioned on MSDN at http://goo.gl/9mBHB, if |pv_dual_mode| is a | 143 // Note, as mentioned on MSDN at |
129 // VT_EMPTY it is successfully converted to false. | 144 // http://msdn.microsoft.com/library/windows/desktop/bb776531.aspx, if |
| 145 // |pv_dual_mode| is a VT_EMPTY it is successfully converted to false as |
| 146 // desired. |
130 BOOL read_dual_mode; | 147 BOOL read_dual_mode; |
131 PropVariantToBoolean(pv_dual_mode, &read_dual_mode); | 148 PropVariantToBoolean(pv_dual_mode, &read_dual_mode); |
132 if((properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) && | 149 if (properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) |
133 static_cast<bool>(read_dual_mode) != properties.dual_mode) { | 150 EXPECT_EQ(properties.dual_mode, static_cast<bool>(read_dual_mode)); |
134 return VERIFY_SHORTCUT_FAILURE_DUAL_MODE; | |
135 } | |
136 } | 151 } |
137 | |
138 return VERIFY_SHORTCUT_SUCCESS; | |
139 } | 152 } |
140 | 153 |
141 } // namespace win | 154 } // namespace win |
142 } // namespace base | 155 } // namespace base |
OLD | NEW |