Chromium Code Reviews| 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 |
| 34 // If |expected_path| is empty confirm immediately that |actual_path| is also | |
| 35 // empty. Otherwise proceed with LongPathName matching which will also confirm | |
| 36 // the paths exist. | |
| 37 if (expected_path.empty()) { | |
| 38 EXPECT_TRUE(actual_path.empty()); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 31 if (::GetLongPathName( | 42 if (::GetLongPathName( |
| 32 expected_path.value().c_str(), long_expected_path_chars, | 43 expected_path.value().c_str(), long_expected_path_chars, |
| 33 MAX_PATH) == 0 || | 44 MAX_PATH) == 0 || |
| 34 ::GetLongPathName( | 45 ::GetLongPathName( |
| 35 actual_path.value().c_str(), long_actual_path_chars, | 46 actual_path.value().c_str(), long_actual_path_chars, |
| 36 MAX_PATH) == 0) { | 47 MAX_PATH) == 0) { |
| 37 return false; | 48 GTEST_NONFATAL_FAILURE_("Failed to get LongPathNames."); |
| 49 return; | |
| 38 } | 50 } |
| 39 | 51 |
| 40 FilePath long_expected_path(long_expected_path_chars); | 52 FilePath long_expected_path(long_expected_path_chars); |
| 41 FilePath long_actual_path(long_actual_path_chars); | 53 FilePath long_actual_path(long_actual_path_chars); |
| 42 if(long_expected_path.empty() || long_actual_path.empty()) | 54 if(long_expected_path.empty() || long_actual_path.empty()) { |
| 43 return false; | 55 GTEST_NONFATAL_FAILURE_("LongPathName unexpectingly empty."); |
| 56 return; | |
| 57 } | |
| 44 | 58 |
| 45 return long_expected_path == long_actual_path; | 59 EXPECT_EQ(long_expected_path, long_actual_path); |
| 46 } | 60 } |
| 47 | 61 |
| 48 } // namespace | 62 } // namespace |
| 49 | 63 |
| 50 VerifyShortcutStatus VerifyShortcut(const FilePath& shortcut_path, | 64 void ValidateShortcut(const FilePath& shortcut_path, |
| 51 const ShortcutProperties& properties) { | 65 const ShortcutProperties& properties) { |
| 52 ScopedComPtr<IShellLink> i_shell_link; | 66 ScopedComPtr<IShellLink> i_shell_link; |
| 53 ScopedComPtr<IPersistFile> i_persist_file; | 67 ScopedComPtr<IPersistFile> i_persist_file; |
| 54 | 68 |
| 55 wchar_t read_target[MAX_PATH] = {0}; | 69 wchar_t read_target[MAX_PATH] = {0}; |
| 56 wchar_t read_working_dir[MAX_PATH] = {0}; | 70 wchar_t read_working_dir[MAX_PATH] = {0}; |
| 57 wchar_t read_arguments[MAX_PATH] = {0}; | 71 wchar_t read_arguments[MAX_PATH] = {0}; |
| 58 wchar_t read_description[MAX_PATH] = {0}; | 72 wchar_t read_description[MAX_PATH] = {0}; |
| 59 wchar_t read_icon[MAX_PATH] = {0}; | 73 wchar_t read_icon[MAX_PATH] = {0}; |
| 60 int read_icon_index = 0; | 74 int read_icon_index = 0; |
| 61 | 75 |
| 62 // Initialize the shell interfaces. | 76 // Initialize the shell interfaces. |
| 63 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | 77 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, |
| 64 CLSCTX_INPROC_SERVER)) || | 78 CLSCTX_INPROC_SERVER)) || |
| 65 FAILED(i_persist_file.QueryFrom(i_shell_link))) { | 79 FAILED(i_persist_file.QueryFrom(i_shell_link))) { |
| 66 return VERIFY_SHORTCUT_FAILURE_UNEXPECTED; | 80 GTEST_NONFATAL_FAILURE_("Failed to initialize COM interfaces."); |
| 81 return; | |
| 67 } | 82 } |
| 68 | 83 |
| 69 // Load the shortcut. | 84 // Load the shortcut. |
| 70 if (FAILED(i_persist_file->Load(shortcut_path.value().c_str(), 0))) | 85 if (FAILED(i_persist_file->Load(shortcut_path.value().c_str(), 0))) { |
| 71 return VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND; | 86 GTEST_NONFATAL_FAILURE_(("Failed to load shortcut at " + |
| 72 | 87 UTF16ToUTF8(shortcut_path.value())).c_str()); |
|
robertshield
2012/09/25 21:06:34
I don't see any other precedent in the code base f
gab
2012/09/25 22:10:00
Done all over the place as suggested. Only added l
| |
| 73 if ((properties.options & ShortcutProperties::PROPERTIES_TARGET) && | 88 return; |
| 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 } | 89 } |
| 79 | 90 |
| 80 if ((properties.options & ShortcutProperties::PROPERTIES_WORKING_DIR) && | 91 if (FAILED(i_shell_link->GetPath(read_target, MAX_PATH, NULL, |
| 81 (FAILED(i_shell_link->GetWorkingDirectory(read_working_dir, MAX_PATH)) || | 92 SLGP_SHORTPATH)) || |
| 82 FilePath(read_working_dir) != properties.working_dir)) { | 93 FAILED(i_shell_link->GetWorkingDirectory(read_working_dir, MAX_PATH)) || |
| 83 return VERIFY_SHORTCUT_FAILURE_WORKING_DIR; | 94 FAILED(i_shell_link->GetDescription(read_description, MAX_PATH)) || |
| 95 FAILED(i_shell_link->GetArguments(read_arguments, MAX_PATH)) || | |
| 96 FAILED(i_shell_link->GetIconLocation(read_icon, MAX_PATH, | |
| 97 &read_icon_index))) { | |
| 98 GTEST_NONFATAL_FAILURE_(("Failed to read properties from shortcut at " + | |
| 99 UTF16ToUTF8(shortcut_path.value())).c_str()); | |
|
robertshield
2012/09/25 21:06:34
similar comment here, and in the other places in t
gab
2012/09/25 22:10:00
Done.
| |
| 100 return; | |
| 84 } | 101 } |
| 85 | 102 |
| 86 if ((properties.options & ShortcutProperties::PROPERTIES_ARGUMENTS) && | 103 if (properties.options & ShortcutProperties::PROPERTIES_TARGET) |
| 87 (FAILED(i_shell_link->GetArguments(read_arguments, MAX_PATH)) || | 104 ValidatePathsAreEqual(properties.target, FilePath(read_target)); |
| 88 string16(read_arguments) != properties.arguments)) { | 105 |
| 89 return VERIFY_SHORTCUT_FAILURE_ARGUMENTS; | 106 if (properties.options & ShortcutProperties::PROPERTIES_WORKING_DIR) |
| 107 ValidatePathsAreEqual(properties.working_dir, FilePath(read_working_dir)); | |
| 108 | |
| 109 if (properties.options & ShortcutProperties::PROPERTIES_ARGUMENTS) | |
| 110 EXPECT_EQ(properties.arguments, read_arguments); | |
| 111 | |
| 112 if (properties.options & ShortcutProperties::PROPERTIES_DESCRIPTION) | |
| 113 EXPECT_EQ(properties.description, read_description); | |
| 114 | |
| 115 if (properties.options & ShortcutProperties::PROPERTIES_ICON) { | |
| 116 ValidatePathsAreEqual(properties.icon, FilePath(read_icon)); | |
| 117 EXPECT_EQ(properties.icon_index, read_icon_index); | |
| 90 } | 118 } |
| 91 | 119 |
| 92 if ((properties.options & ShortcutProperties::PROPERTIES_DESCRIPTION) && | 120 if (GetVersion() >= VERSION_WIN7) { |
| 93 (FAILED(i_shell_link->GetDescription(read_description, MAX_PATH)) || | |
| 94 string16(read_description) != properties.description)) { | |
| 95 return VERIFY_SHORTCUT_FAILURE_DESCRIPTION; | |
| 96 } | |
| 97 | |
| 98 if ((properties.options & ShortcutProperties::PROPERTIES_ICON) && | |
| 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; | 121 ScopedComPtr<IPropertyStore> property_store; |
| 108 // Note that, as mentioned on MSDN at http://goo.gl/M8h9g, if a property is | 122 // 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 | 123 // not set, GetValue will return S_OK and the PROPVARIANT will be set to |
| 110 // VT_EMPTY. | 124 // VT_EMPTY. |
| 111 PROPVARIANT pv_app_id, pv_dual_mode; | 125 PROPVARIANT pv_app_id, pv_dual_mode; |
| 112 if (FAILED(property_store.QueryFrom(i_shell_link)) || | 126 if (FAILED(property_store.QueryFrom(i_shell_link)) || |
| 113 property_store->GetValue(PKEY_AppUserModel_ID, &pv_app_id) != S_OK || | 127 property_store->GetValue(PKEY_AppUserModel_ID, &pv_app_id) != S_OK || |
| 114 property_store->GetValue(PKEY_AppUserModel_IsDualMode, | 128 property_store->GetValue(PKEY_AppUserModel_IsDualMode, |
| 115 &pv_dual_mode) != S_OK) { | 129 &pv_dual_mode) != S_OK) { |
| 116 return VERIFY_SHORTCUT_FAILURE_UNEXPECTED; | 130 GTEST_NONFATAL_FAILURE_(("Failed to read property store for shortcut at" + |
| 131 UTF16ToUTF8(shortcut_path.value())).c_str()); | |
| 132 return; | |
| 117 } | 133 } |
| 118 | 134 |
| 119 // Note, as mentioned on MSDN at http://goo.gl/hZ3sO, if |pv_app_id| is a | 135 // Note, as mentioned on MSDN at |
| 120 // VT_EMPTY it is successfully converted to the empty string. | 136 // http://msdn.microsoft.com/library/windows/desktop/bb776559.aspx, if |
| 137 // |pv_app_id| is a VT_EMPTY it is successfully converted to the empty | |
| 138 // string as desired. | |
| 121 wchar_t read_app_id[MAX_PATH] = {0}; | 139 wchar_t read_app_id[MAX_PATH] = {0}; |
| 122 PropVariantToString(pv_app_id, read_app_id, MAX_PATH); | 140 PropVariantToString(pv_app_id, read_app_id, MAX_PATH); |
| 123 if((properties.options & ShortcutProperties::PROPERTIES_APP_ID) && | 141 if (properties.options & ShortcutProperties::PROPERTIES_APP_ID) |
| 124 string16(read_app_id) != properties.app_id) { | 142 EXPECT_EQ(properties.app_id, read_app_id); |
| 125 return VERIFY_SHORTCUT_FAILURE_APP_ID; | |
| 126 } | |
| 127 | 143 |
| 128 // Note, as mentioned on MSDN at http://goo.gl/9mBHB, if |pv_dual_mode| is a | 144 // Note, as mentioned on MSDN at |
| 129 // VT_EMPTY it is successfully converted to false. | 145 // http://msdn.microsoft.com/library/windows/desktop/bb776531.aspx, if |
| 146 // |pv_dual_mode| is a VT_EMPTY it is successfully converted to false as | |
| 147 // desired. | |
| 130 BOOL read_dual_mode; | 148 BOOL read_dual_mode; |
| 131 PropVariantToBoolean(pv_dual_mode, &read_dual_mode); | 149 PropVariantToBoolean(pv_dual_mode, &read_dual_mode); |
| 132 if((properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) && | 150 if (properties.options & ShortcutProperties::PROPERTIES_DUAL_MODE) |
| 133 static_cast<bool>(read_dual_mode) != properties.dual_mode) { | 151 EXPECT_EQ(properties.dual_mode, static_cast<bool>(read_dual_mode)); |
| 134 return VERIFY_SHORTCUT_FAILURE_DUAL_MODE; | |
| 135 } | |
| 136 } | 152 } |
| 137 | |
| 138 return VERIFY_SHORTCUT_SUCCESS; | |
| 139 } | 153 } |
| 140 | 154 |
| 141 } // namespace win | 155 } // namespace win |
| 142 } // namespace base | 156 } // namespace base |
| OLD | NEW |