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 <shlobj.h> | |
6 | |
7 #include <fstream> | |
8 #include <vector> | 5 #include <vector> |
9 | 6 |
| 7 #include "base/file_path.h" |
10 #include "base/file_util.h" | 8 #include "base/file_util.h" |
11 #include "base/path_service.h" | |
12 #include "base/md5.h" | 9 #include "base/md5.h" |
13 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
14 #include "base/string16.h" | 11 #include "base/string16.h" |
15 #include "base/string_util.h" | 12 #include "base/string_util.h" |
16 #include "base/win/windows_version.h" | |
17 #include "chrome/installer/util/browser_distribution.h" | 13 #include "chrome/installer/util/browser_distribution.h" |
18 #include "chrome/installer/util/master_preferences.h" | |
19 #include "chrome/installer/util/shell_util.h" | 14 #include "chrome/installer/util/shell_util.h" |
20 #include "chrome/installer/util/util_constants.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
22 | 16 |
23 namespace { | 17 namespace { |
24 | 18 |
25 class ShellUtilTestWithDirAndDist : public testing::Test { | 19 class ShellUtilShortcutTest : public testing::Test { |
26 protected: | 20 protected: |
27 virtual void SetUp() { | 21 virtual void SetUp() { |
28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
29 dist_ = BrowserDistribution::GetDistribution(); | 23 user_level_shortcut = |
30 ASSERT_TRUE(dist_ != NULL); | 24 FilePath(temp_dir_.path().Append(L"user shortcut.lnk")); |
| 25 system_level_shortcut = |
| 26 FilePath(temp_dir_.path().Append(L"system shortcut.lnk")); |
31 } | 27 } |
32 | 28 |
33 BrowserDistribution* dist_; | |
34 | |
35 ScopedTempDir temp_dir_; | 29 ScopedTempDir temp_dir_; |
| 30 FilePath user_level_shortcut; |
| 31 FilePath system_level_shortcut; |
36 }; | 32 }; |
37 | 33 |
| 34 } // namespace |
| 35 |
| 36 TEST_F(ShellUtilShortcutTest, |
| 37 DetermineShortcutPathToCreateFromOptionsUserLevel) { |
| 38 EXPECT_EQ(user_level_shortcut, |
| 39 ShellUtil::DetermineShortcutPathToCreateFromOptions( |
| 40 user_level_shortcut, system_level_shortcut, |
| 41 ShellUtil::SHORTCUT_NO_OPTIONS)); |
38 } | 42 } |
39 | 43 |
40 // Test that we can open archives successfully. | 44 TEST_F(ShellUtilShortcutTest, |
41 TEST_F(ShellUtilTestWithDirAndDist, UpdateChromeShortcutTest) { | 45 DetermineShortcutPathToCreateFromOptionsUserLevelSystemLevelEmpty) { |
42 // Create an executable in test path by copying ourself to it. | 46 EXPECT_EQ(user_level_shortcut, |
43 wchar_t exe_full_path_str[MAX_PATH]; | 47 ShellUtil::DetermineShortcutPathToCreateFromOptions( |
44 EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); | 48 user_level_shortcut, FilePath(), |
45 FilePath exe_full_path(exe_full_path_str); | 49 ShellUtil::SHORTCUT_NO_OPTIONS)); |
46 | |
47 FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe"); | |
48 EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path)); | |
49 | |
50 FilePath shortcut_path = temp_dir_.path().AppendASCII("shortcut.lnk"); | |
51 const string16 description(L"dummy description"); | |
52 EXPECT_TRUE(ShellUtil::UpdateChromeShortcut( | |
53 dist_, | |
54 exe_path.value(), | |
55 shortcut_path.value(), | |
56 L"", | |
57 description, | |
58 exe_path.value(), | |
59 dist_->GetIconIndex(), | |
60 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
61 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
62 ShellUtil::VerifyChromeShortcut( | |
63 exe_path.value(), shortcut_path.value(), description, 0)); | |
64 | |
65 // Now specify an icon index in master prefs and make sure it works. | |
66 FilePath prefs_path = temp_dir_.path().AppendASCII( | |
67 installer::kDefaultMasterPrefs); | |
68 std::ofstream file; | |
69 file.open(prefs_path.value().c_str()); | |
70 ASSERT_TRUE(file.is_open()); | |
71 file << | |
72 "{" | |
73 " \"distribution\":{" | |
74 " \"chrome_shortcut_icon_index\" : 1" | |
75 " }" | |
76 "}"; | |
77 file.close(); | |
78 ASSERT_TRUE(file_util::Delete(shortcut_path, false)); | |
79 EXPECT_TRUE(ShellUtil::UpdateChromeShortcut( | |
80 dist_, | |
81 exe_path.value(), | |
82 shortcut_path.value(), | |
83 L"", | |
84 description, | |
85 exe_path.value(), | |
86 dist_->GetIconIndex(), | |
87 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
88 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
89 ShellUtil::VerifyChromeShortcut( | |
90 exe_path.value(), shortcut_path.value(), description, 1)); | |
91 | |
92 // Now change only description to update shortcut and make sure icon index | |
93 // doesn't change. | |
94 const string16 description2(L"dummy description 2"); | |
95 EXPECT_TRUE(ShellUtil::UpdateChromeShortcut(dist_, | |
96 exe_path.value(), | |
97 shortcut_path.value(), | |
98 L"", | |
99 description2, | |
100 exe_path.value(), | |
101 dist_->GetIconIndex(), | |
102 ShellUtil::SHORTCUT_NO_OPTIONS)); | |
103 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
104 ShellUtil::VerifyChromeShortcut( | |
105 exe_path.value(), shortcut_path.value(), description2, 1)); | |
106 } | 50 } |
107 | 51 |
108 TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) { | 52 TEST_F(ShellUtilShortcutTest, |
109 // Run this test on Vista+ only if we are running elevated. | 53 DetermineShortcutPathToCreateFromOptionsUserLevelSystemLevelPresent) { |
110 if (base::win::GetVersion() > base::win::VERSION_XP && !IsUserAnAdmin()) { | 54 file_util::WriteFile(system_level_shortcut, "", 0); |
111 LOG(ERROR) << "Must be admin to run this test on Vista+"; | 55 EXPECT_EQ(FilePath(), |
112 return; | 56 ShellUtil::DetermineShortcutPathToCreateFromOptions( |
113 } | 57 user_level_shortcut, system_level_shortcut, |
114 | 58 ShellUtil::SHORTCUT_NO_OPTIONS)); |
115 // Create an executable in test path by copying ourself to it. | |
116 wchar_t exe_full_path_str[MAX_PATH]; | |
117 EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); | |
118 FilePath exe_full_path(exe_full_path_str); | |
119 | |
120 FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe"); | |
121 EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path)); | |
122 | |
123 const string16 description(L"dummy description"); | |
124 | |
125 FilePath user_desktop_path; | |
126 EXPECT_TRUE(ShellUtil::GetDesktopPath(false, &user_desktop_path)); | |
127 FilePath system_desktop_path; | |
128 EXPECT_TRUE(ShellUtil::GetDesktopPath(true, &system_desktop_path)); | |
129 | |
130 string16 shortcut_name; | |
131 EXPECT_TRUE(ShellUtil::GetChromeShortcutName(dist_, false, L"", | |
132 &shortcut_name)); | |
133 | |
134 string16 default_profile_shortcut_name; | |
135 const string16 default_profile_user_name = L"Minsk"; | |
136 EXPECT_TRUE(ShellUtil::GetChromeShortcutName(dist_, false, | |
137 default_profile_user_name, | |
138 &default_profile_shortcut_name)); | |
139 | |
140 string16 second_profile_shortcut_name; | |
141 const string16 second_profile_user_name = L"Pinsk"; | |
142 EXPECT_TRUE(ShellUtil::GetChromeShortcutName(dist_, false, | |
143 second_profile_user_name, | |
144 &second_profile_shortcut_name)); | |
145 | |
146 FilePath user_shortcut_path = user_desktop_path.Append(shortcut_name); | |
147 FilePath system_shortcut_path = system_desktop_path.Append(shortcut_name); | |
148 FilePath default_profile_shortcut_path = user_desktop_path.Append( | |
149 default_profile_shortcut_name); | |
150 FilePath second_profile_shortcut_path = user_desktop_path.Append( | |
151 second_profile_shortcut_name); | |
152 | |
153 // Test simple creation of a user-level shortcut. | |
154 EXPECT_TRUE(ShellUtil::CreateChromeDesktopShortcut( | |
155 dist_, | |
156 exe_path.value(), | |
157 description, | |
158 L"", | |
159 L"", | |
160 exe_path.value(), | |
161 dist_->GetIconIndex(), | |
162 ShellUtil::CURRENT_USER, | |
163 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
164 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
165 ShellUtil::VerifyChromeShortcut( | |
166 exe_path.value(), user_shortcut_path.value(), description, 0)); | |
167 EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut( | |
168 dist_, | |
169 ShellUtil::CURRENT_USER, | |
170 ShellUtil::SHORTCUT_NO_OPTIONS)); | |
171 | |
172 // Test simple creation of a system-level shortcut. | |
173 EXPECT_TRUE(ShellUtil::CreateChromeDesktopShortcut( | |
174 dist_, | |
175 exe_path.value(), | |
176 description, | |
177 L"", | |
178 L"", | |
179 exe_path.value(), | |
180 dist_->GetIconIndex(), | |
181 ShellUtil::SYSTEM_LEVEL, | |
182 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
183 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
184 ShellUtil::VerifyChromeShortcut( | |
185 exe_path.value(), system_shortcut_path.value(), description, | |
186 0)); | |
187 EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut( | |
188 dist_, | |
189 ShellUtil::SYSTEM_LEVEL, | |
190 ShellUtil::SHORTCUT_NO_OPTIONS)); | |
191 | |
192 // Test creation of a user-level shortcut when a system-level shortcut | |
193 // is already present (should fail). | |
194 EXPECT_TRUE(ShellUtil::CreateChromeDesktopShortcut( | |
195 dist_, | |
196 exe_path.value(), | |
197 description, | |
198 L"", | |
199 L"", | |
200 exe_path.value(), | |
201 dist_->GetIconIndex(), | |
202 ShellUtil::SYSTEM_LEVEL, | |
203 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
204 EXPECT_FALSE(ShellUtil::CreateChromeDesktopShortcut( | |
205 dist_, | |
206 exe_path.value(), | |
207 description, | |
208 L"", | |
209 L"", | |
210 exe_path.value(), | |
211 dist_->GetIconIndex(), | |
212 ShellUtil::CURRENT_USER, | |
213 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
214 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
215 ShellUtil::VerifyChromeShortcut( | |
216 exe_path.value(), system_shortcut_path.value(), description, | |
217 0)); | |
218 EXPECT_FALSE(file_util::PathExists(user_shortcut_path)); | |
219 EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut( | |
220 dist_, | |
221 ShellUtil::SYSTEM_LEVEL, | |
222 ShellUtil::SHORTCUT_NO_OPTIONS)); | |
223 | |
224 // Test creation of a system-level shortcut when a user-level shortcut | |
225 // is already present (should succeed). | |
226 EXPECT_TRUE(ShellUtil::CreateChromeDesktopShortcut( | |
227 dist_, | |
228 exe_path.value(), | |
229 description, | |
230 L"", | |
231 L"", | |
232 exe_path.value(), | |
233 dist_->GetIconIndex(), | |
234 ShellUtil::CURRENT_USER, | |
235 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
236 EXPECT_TRUE(ShellUtil::CreateChromeDesktopShortcut( | |
237 dist_, | |
238 exe_path.value(), | |
239 description, | |
240 L"", | |
241 L"", | |
242 exe_path.value(), | |
243 dist_->GetIconIndex(), | |
244 ShellUtil::SYSTEM_LEVEL, | |
245 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
246 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
247 ShellUtil::VerifyChromeShortcut( | |
248 exe_path.value(), user_shortcut_path.value(), description, 0)); | |
249 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
250 ShellUtil::VerifyChromeShortcut( | |
251 exe_path.value(), system_shortcut_path.value(), description, | |
252 0)); | |
253 EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut( | |
254 dist_, | |
255 ShellUtil::CURRENT_USER, | |
256 ShellUtil::SHORTCUT_NO_OPTIONS)); | |
257 EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcut( | |
258 dist_, | |
259 ShellUtil::SYSTEM_LEVEL, | |
260 ShellUtil::SHORTCUT_NO_OPTIONS)); | |
261 | |
262 // Test creation of two profile-specific shortcuts (these are always | |
263 // user-level). | |
264 EXPECT_TRUE(ShellUtil::CreateChromeDesktopShortcut( | |
265 dist_, | |
266 exe_path.value(), | |
267 description, | |
268 default_profile_user_name, | |
269 L"--profile-directory=\"Default\"", | |
270 exe_path.value(), | |
271 dist_->GetIconIndex(), | |
272 ShellUtil::CURRENT_USER, | |
273 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
274 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
275 ShellUtil::VerifyChromeShortcut( | |
276 exe_path.value(), default_profile_shortcut_path.value(), | |
277 description, 0)); | |
278 EXPECT_TRUE(ShellUtil::CreateChromeDesktopShortcut( | |
279 dist_, | |
280 exe_path.value(), | |
281 description, | |
282 second_profile_user_name, | |
283 L"--profile-directory=\"Profile 1\"", | |
284 exe_path.value(), | |
285 dist_->GetIconIndex(), | |
286 ShellUtil::CURRENT_USER, | |
287 ShellUtil::SHORTCUT_CREATE_ALWAYS)); | |
288 EXPECT_EQ(ShellUtil::VERIFY_SHORTCUT_SUCCESS, | |
289 ShellUtil::VerifyChromeShortcut( | |
290 exe_path.value(), second_profile_shortcut_path.value(), | |
291 description, 0)); | |
292 std::vector<string16> profile_names; | |
293 profile_names.push_back(default_profile_shortcut_name); | |
294 profile_names.push_back(second_profile_shortcut_name); | |
295 EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames( | |
296 profile_names)); | |
297 } | 59 } |
298 | 60 |
299 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdBasic) { | 61 TEST_F(ShellUtilShortcutTest, |
| 62 DetermineShortcutPathToCreateFromOptionsSystemLevel) { |
| 63 EXPECT_EQ(system_level_shortcut, |
| 64 ShellUtil::DetermineShortcutPathToCreateFromOptions( |
| 65 user_level_shortcut, system_level_shortcut, |
| 66 ShellUtil::SHORTCUT_SYSTEM_LEVEL)); |
| 67 } |
| 68 |
| 69 TEST_F(ShellUtilShortcutTest, |
| 70 DetermineShortcutPathToCreateFromOptionsSystemLevelUserLevelPresent) { |
| 71 file_util::WriteFile(user_level_shortcut, "", 0); |
| 72 EXPECT_EQ(system_level_shortcut, |
| 73 ShellUtil::DetermineShortcutPathToCreateFromOptions( |
| 74 user_level_shortcut, system_level_shortcut, |
| 75 ShellUtil::SHORTCUT_SYSTEM_LEVEL)); |
| 76 } |
| 77 |
| 78 TEST(ShellUtilTest, BuildAppModelIdBasic) { |
| 79 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
300 std::vector<string16> components; | 80 std::vector<string16> components; |
301 const string16 base_app_id(dist_->GetBaseAppId()); | 81 const string16 base_app_id(dist->GetBaseAppId()); |
302 components.push_back(base_app_id); | 82 components.push_back(base_app_id); |
303 ASSERT_EQ(base_app_id, ShellUtil::BuildAppModelId(components)); | 83 ASSERT_EQ(base_app_id, ShellUtil::BuildAppModelId(components)); |
304 } | 84 } |
305 | 85 |
306 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdManySmall) { | 86 TEST(ShellUtilTest, BuildAppModelIdManySmall) { |
| 87 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
307 std::vector<string16> components; | 88 std::vector<string16> components; |
308 const string16 suffixed_app_id(dist_->GetBaseAppId().append(L".gab")); | 89 const string16 suffixed_app_id(dist->GetBaseAppId().append(L".gab")); |
309 components.push_back(suffixed_app_id); | 90 components.push_back(suffixed_app_id); |
310 components.push_back(L"Default"); | 91 components.push_back(L"Default"); |
311 components.push_back(L"Test"); | 92 components.push_back(L"Test"); |
312 ASSERT_EQ(suffixed_app_id + L".Default.Test", | 93 ASSERT_EQ(suffixed_app_id + L".Default.Test", |
313 ShellUtil::BuildAppModelId(components)); | 94 ShellUtil::BuildAppModelId(components)); |
314 } | 95 } |
315 | 96 |
316 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdLongUsernameNormalProfile) { | 97 TEST(ShellUtilTest, BuildAppModelIdLongUsernameNormalProfile) { |
317 std::vector<string16> components; | 98 std::vector<string16> components; |
318 const string16 long_appname( | 99 const string16 long_appname( |
319 L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" | 100 L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" |
320 L"that_goes_over_64_characters"); | 101 L"that_goes_over_64_characters"); |
321 components.push_back(long_appname); | 102 components.push_back(long_appname); |
322 components.push_back(L"Default"); | 103 components.push_back(L"Default"); |
323 ASSERT_EQ(L"Chrome.a_user_wer_64_characters.Default", | 104 ASSERT_EQ(L"Chrome.a_user_wer_64_characters.Default", |
324 ShellUtil::BuildAppModelId(components)); | 105 ShellUtil::BuildAppModelId(components)); |
325 } | 106 } |
326 | 107 |
327 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdLongEverything) { | 108 TEST(ShellUtilTest, BuildAppModelIdLongEverything) { |
328 std::vector<string16> components; | 109 std::vector<string16> components; |
329 const string16 long_appname( | 110 const string16 long_appname( |
330 L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" | 111 L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" |
331 L"that_goes_over_64_characters"); | 112 L"that_goes_over_64_characters"); |
332 components.push_back(long_appname); | 113 components.push_back(long_appname); |
333 components.push_back( | 114 components.push_back( |
334 L"A_crazy_profile_name_not_even_sure_whether_that_is_possible"); | 115 L"A_crazy_profile_name_not_even_sure_whether_that_is_possible"); |
335 const string16 constructed_app_id(ShellUtil::BuildAppModelId(components)); | 116 const string16 constructed_app_id(ShellUtil::BuildAppModelId(components)); |
336 ASSERT_LE(constructed_app_id.length(), installer::kMaxAppModelIdLength); | 117 ASSERT_LE(constructed_app_id.length(), installer::kMaxAppModelIdLength); |
337 ASSERT_EQ(L"Chrome.a_user_wer_64_characters.A_crazy_profilethat_is_possible", | 118 ASSERT_EQ(L"Chrome.a_user_wer_64_characters.A_crazy_profilethat_is_possible", |
(...skipping 27 matching lines...) Expand all Loading... |
365 | 146 |
366 const string16 expected[] = { L"", L"MY", L"MZXQ", L"MZXW6", L"MZXW6YQ", | 147 const string16 expected[] = { L"", L"MY", L"MZXQ", L"MZXW6", L"MZXW6YQ", |
367 L"MZXW6YTB", L"MZXW6YTBOI"}; | 148 L"MZXW6YTB", L"MZXW6YTBOI"}; |
368 | 149 |
369 // Run the tests, with one more letter in the input every pass. | 150 // Run the tests, with one more letter in the input every pass. |
370 for (int i = 0; i < arraysize(expected); ++i) { | 151 for (int i = 0; i < arraysize(expected); ++i) { |
371 ASSERT_EQ(expected[i], | 152 ASSERT_EQ(expected[i], |
372 ShellUtil::ByteArrayToBase32(test_array, i)); | 153 ShellUtil::ByteArrayToBase32(test_array, i)); |
373 } | 154 } |
374 } | 155 } |
OLD | NEW |