xref: /aosp_15_r20/external/cronet/base/win/shortcut_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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/win/shortcut.h"
6 
7 #include <stdint.h>
8 
9 #include <string>
10 
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/test/test_file_util.h"
15 #include "base/test/test_shortcut_win.h"
16 #include "base/win/scoped_com_initializer.h"
17 #include "build/build_config.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 
20 namespace base {
21 namespace win {
22 
23 namespace {
24 
25 static const char kFileContents[] = "This is a target.";
26 static const char kFileContents2[] = "This is another target.";
27 
28 class ShortcutTest : public testing::Test {
29  protected:
SetUp()30   void SetUp() override {
31     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
32     ASSERT_TRUE(temp_dir_2_.CreateUniqueTempDir());
33 
34     link_file_ = temp_dir_.GetPath().Append(FILE_PATH_LITERAL("My Link.lnk"));
35 
36     // Shortcut 1's properties
37     {
38       const FilePath target_file(
39           temp_dir_.GetPath().Append(FILE_PATH_LITERAL("Target 1.txt")));
40       WriteFile(target_file, kFileContents, std::size(kFileContents));
41 
42       link_properties_.set_target(target_file);
43       link_properties_.set_working_dir(temp_dir_.GetPath());
44       link_properties_.set_arguments(L"--magic --awesome");
45       link_properties_.set_description(L"Chrome is awesome.");
46       link_properties_.set_icon(link_properties_.target, 4);
47       link_properties_.set_app_id(L"Chrome");
48       link_properties_.set_dual_mode(false);
49 
50       // The CLSID below was randomly selected.
51       static constexpr CLSID toast_activator_clsid = {
52           0x08d401c2,
53           0x3f79,
54           0x41d8,
55           {0x89, 0xd0, 0x99, 0x25, 0xee, 0x16, 0x28, 0x63}};
56       link_properties_.set_toast_activator_clsid(toast_activator_clsid);
57     }
58 
59     // Shortcut 2's properties (all different from properties of shortcut 1).
60     {
61       const FilePath target_file_2(
62           temp_dir_.GetPath().Append(FILE_PATH_LITERAL("Target 2.txt")));
63       WriteFile(target_file_2, kFileContents2, std::size(kFileContents2));
64 
65       FilePath icon_path_2;
66       CreateTemporaryFileInDir(temp_dir_.GetPath(), &icon_path_2);
67 
68       link_properties_2_.set_target(target_file_2);
69       link_properties_2_.set_working_dir(temp_dir_2_.GetPath());
70       link_properties_2_.set_arguments(L"--super --crazy");
71       link_properties_2_.set_description(L"The best in the west.");
72       link_properties_2_.set_icon(icon_path_2, 0);
73       link_properties_2_.set_app_id(L"Chrome.UserLevelCrazySuffix");
74       link_properties_2_.set_dual_mode(true);
75       link_properties_2_.set_toast_activator_clsid(CLSID_NULL);
76     }
77   }
78 
79   ScopedCOMInitializer com_initializer_;
80   ScopedTempDir temp_dir_;
81   ScopedTempDir temp_dir_2_;
82 
83   // The link file to be created/updated in the shortcut tests below.
84   FilePath link_file_;
85 
86   // Properties for the created shortcut.
87   ShortcutProperties link_properties_;
88 
89   // Properties for the updated shortcut.
90   ShortcutProperties link_properties_2_;
91 };
92 
93 }  // namespace
94 
TEST_F(ShortcutTest,CreateAndResolveShortcutProperties)95 TEST_F(ShortcutTest, CreateAndResolveShortcutProperties) {
96   // Test all properties.
97   FilePath file_1(temp_dir_.GetPath().Append(FILE_PATH_LITERAL("Link1.lnk")));
98   ASSERT_TRUE(CreateOrUpdateShortcutLink(file_1, link_properties_,
99                                          ShortcutOperation::kCreateAlways));
100 
101   ShortcutProperties properties_read_1;
102   ASSERT_TRUE(ResolveShortcutProperties(
103       file_1, ShortcutProperties::PROPERTIES_ALL, &properties_read_1));
104   EXPECT_EQ(static_cast<unsigned>(ShortcutProperties::PROPERTIES_ALL),
105             properties_read_1.options);
106   ValidatePathsAreEqual(link_properties_.target, properties_read_1.target);
107   ValidatePathsAreEqual(link_properties_.working_dir,
108                         properties_read_1.working_dir);
109   EXPECT_EQ(link_properties_.arguments, properties_read_1.arguments);
110   EXPECT_EQ(link_properties_.description, properties_read_1.description);
111   ValidatePathsAreEqual(link_properties_.icon, properties_read_1.icon);
112   EXPECT_EQ(link_properties_.icon_index, properties_read_1.icon_index);
113   EXPECT_EQ(link_properties_.app_id, properties_read_1.app_id);
114   EXPECT_EQ(link_properties_.dual_mode, properties_read_1.dual_mode);
115   EXPECT_EQ(link_properties_.toast_activator_clsid,
116             properties_read_1.toast_activator_clsid);
117 
118   // Test simple shortcut with no special properties set.
119   FilePath file_2(temp_dir_.GetPath().Append(FILE_PATH_LITERAL("Link2.lnk")));
120   ShortcutProperties only_target_properties;
121   only_target_properties.set_target(link_properties_.target);
122   ASSERT_TRUE(CreateOrUpdateShortcutLink(file_2, only_target_properties,
123                                          ShortcutOperation::kCreateAlways));
124 
125   ShortcutProperties properties_read_2;
126   ASSERT_TRUE(ResolveShortcutProperties(
127       file_2, ShortcutProperties::PROPERTIES_ALL, &properties_read_2));
128   EXPECT_EQ(static_cast<unsigned>(ShortcutProperties::PROPERTIES_ALL),
129             properties_read_2.options);
130   ValidatePathsAreEqual(only_target_properties.target,
131                         properties_read_2.target);
132   ValidatePathsAreEqual(FilePath(), properties_read_2.working_dir);
133   EXPECT_EQ(L"", properties_read_2.arguments);
134   EXPECT_EQ(L"", properties_read_2.description);
135   ValidatePathsAreEqual(FilePath(), properties_read_2.icon);
136   EXPECT_EQ(0, properties_read_2.icon_index);
137   EXPECT_EQ(L"", properties_read_2.app_id);
138   EXPECT_FALSE(properties_read_2.dual_mode);
139   EXPECT_EQ(CLSID_NULL, properties_read_2.toast_activator_clsid);
140 }
141 
TEST_F(ShortcutTest,CreateAndResolveShortcut)142 TEST_F(ShortcutTest, CreateAndResolveShortcut) {
143   ShortcutProperties only_target_properties;
144   only_target_properties.set_target(link_properties_.target);
145 
146   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, only_target_properties,
147                                          ShortcutOperation::kCreateAlways));
148 
149   FilePath resolved_name;
150   EXPECT_TRUE(ResolveShortcut(link_file_, &resolved_name, nullptr));
151 
152   char read_contents[std::size(kFileContents)];
153   ReadFile(resolved_name, read_contents);
154   EXPECT_STREQ(kFileContents, read_contents);
155 }
156 
TEST_F(ShortcutTest,ResolveShortcutWithArgs)157 TEST_F(ShortcutTest, ResolveShortcutWithArgs) {
158   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
159                                          ShortcutOperation::kCreateAlways));
160 
161   FilePath resolved_name;
162   std::wstring args;
163   EXPECT_TRUE(ResolveShortcut(link_file_, &resolved_name, &args));
164 
165   char read_contents[std::size(kFileContents)];
166   ReadFile(resolved_name, read_contents);
167   EXPECT_STREQ(kFileContents, read_contents);
168   EXPECT_EQ(link_properties_.arguments, args);
169 }
170 
TEST_F(ShortcutTest,CreateShortcutWithOnlySomeProperties)171 TEST_F(ShortcutTest, CreateShortcutWithOnlySomeProperties) {
172   ShortcutProperties target_and_args_properties;
173   target_and_args_properties.set_target(link_properties_.target);
174   target_and_args_properties.set_arguments(link_properties_.arguments);
175 
176   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, target_and_args_properties,
177                                          ShortcutOperation::kCreateAlways));
178 
179   ValidateShortcut(link_file_, target_and_args_properties);
180 }
181 
TEST_F(ShortcutTest,CreateShortcutVerifyProperties)182 TEST_F(ShortcutTest, CreateShortcutVerifyProperties) {
183   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
184                                          ShortcutOperation::kCreateAlways));
185 
186   ValidateShortcut(link_file_, link_properties_);
187 }
188 
TEST_F(ShortcutTest,UpdateShortcutVerifyPropertiess)189 TEST_F(ShortcutTest, UpdateShortcutVerifyPropertiess) {
190   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
191                                          ShortcutOperation::kCreateAlways));
192 
193   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_2_,
194                                          ShortcutOperation::kUpdateExisting));
195 
196   ValidateShortcut(link_file_, link_properties_2_);
197 }
198 
TEST_F(ShortcutTest,UpdateShortcutUpdateOnlyTargetAndResolve)199 TEST_F(ShortcutTest, UpdateShortcutUpdateOnlyTargetAndResolve) {
200   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
201                                          ShortcutOperation::kCreateAlways));
202 
203   ShortcutProperties update_only_target_properties;
204   update_only_target_properties.set_target(link_properties_2_.target);
205 
206   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_,
207                                          update_only_target_properties,
208                                          ShortcutOperation::kUpdateExisting));
209 
210   ShortcutProperties expected_properties = link_properties_;
211   expected_properties.set_target(link_properties_2_.target);
212   ValidateShortcut(link_file_, expected_properties);
213 
214   FilePath resolved_name;
215   EXPECT_TRUE(ResolveShortcut(link_file_, &resolved_name, nullptr));
216 
217   char read_contents[std::size(kFileContents2)];
218   ReadFile(resolved_name, read_contents);
219   EXPECT_STREQ(kFileContents2, read_contents);
220 }
221 
TEST_F(ShortcutTest,UpdateShortcutMakeDualMode)222 TEST_F(ShortcutTest, UpdateShortcutMakeDualMode) {
223   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
224                                          ShortcutOperation::kCreateAlways));
225 
226   ShortcutProperties make_dual_mode_properties;
227   make_dual_mode_properties.set_dual_mode(true);
228 
229   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, make_dual_mode_properties,
230                                          ShortcutOperation::kUpdateExisting));
231 
232   ShortcutProperties expected_properties = link_properties_;
233   expected_properties.set_dual_mode(true);
234   ValidateShortcut(link_file_, expected_properties);
235 }
236 
TEST_F(ShortcutTest,UpdateShortcutRemoveDualMode)237 TEST_F(ShortcutTest, UpdateShortcutRemoveDualMode) {
238   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_2_,
239                                          ShortcutOperation::kCreateAlways));
240 
241   ShortcutProperties remove_dual_mode_properties;
242   remove_dual_mode_properties.set_dual_mode(false);
243 
244   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_,
245                                          remove_dual_mode_properties,
246                                          ShortcutOperation::kUpdateExisting));
247 
248   ShortcutProperties expected_properties = link_properties_2_;
249   expected_properties.set_dual_mode(false);
250   ValidateShortcut(link_file_, expected_properties);
251 }
252 
TEST_F(ShortcutTest,UpdateShortcutClearArguments)253 TEST_F(ShortcutTest, UpdateShortcutClearArguments) {
254   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
255                                          ShortcutOperation::kCreateAlways));
256 
257   ShortcutProperties clear_arguments_properties;
258   clear_arguments_properties.set_arguments(std::wstring());
259 
260   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, clear_arguments_properties,
261                                          ShortcutOperation::kUpdateExisting));
262 
263   ShortcutProperties expected_properties = link_properties_;
264   expected_properties.set_arguments(std::wstring());
265   ValidateShortcut(link_file_, expected_properties);
266 }
267 
TEST_F(ShortcutTest,FailUpdateShortcutThatDoesNotExist)268 TEST_F(ShortcutTest, FailUpdateShortcutThatDoesNotExist) {
269   ASSERT_FALSE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
270                                           ShortcutOperation::kUpdateExisting));
271   ASSERT_FALSE(PathExists(link_file_));
272 }
273 
TEST_F(ShortcutTest,ReplaceShortcutAllProperties)274 TEST_F(ShortcutTest, ReplaceShortcutAllProperties) {
275   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
276                                          ShortcutOperation::kCreateAlways));
277 
278   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_2_,
279                                          ShortcutOperation::kReplaceExisting));
280 
281   ValidateShortcut(link_file_, link_properties_2_);
282 }
283 
TEST_F(ShortcutTest,ReplaceShortcutSomeProperties)284 TEST_F(ShortcutTest, ReplaceShortcutSomeProperties) {
285   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
286                                          ShortcutOperation::kCreateAlways));
287 
288   ShortcutProperties new_properties;
289   new_properties.set_target(link_properties_2_.target);
290   new_properties.set_arguments(link_properties_2_.arguments);
291   new_properties.set_description(link_properties_2_.description);
292   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, new_properties,
293                                          ShortcutOperation::kReplaceExisting));
294 
295   // Expect only properties in |new_properties| to be set, all other properties
296   // should have been overwritten.
297   ShortcutProperties expected_properties(new_properties);
298   expected_properties.set_working_dir(FilePath());
299   expected_properties.set_icon(FilePath(), 0);
300   expected_properties.set_app_id(std::wstring());
301   expected_properties.set_dual_mode(false);
302   ValidateShortcut(link_file_, expected_properties);
303 }
304 
TEST_F(ShortcutTest,FailReplaceShortcutThatDoesNotExist)305 TEST_F(ShortcutTest, FailReplaceShortcutThatDoesNotExist) {
306   ASSERT_FALSE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
307                                           ShortcutOperation::kReplaceExisting));
308   ASSERT_FALSE(PathExists(link_file_));
309 }
310 
311 // Test that the old arguments remain on the replaced shortcut when not
312 // otherwise specified.
TEST_F(ShortcutTest,ReplaceShortcutKeepOldArguments)313 TEST_F(ShortcutTest, ReplaceShortcutKeepOldArguments) {
314   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_,
315                                          ShortcutOperation::kCreateAlways));
316 
317   // Do not explicitly set the arguments.
318   link_properties_2_.options &= ~ShortcutProperties::PROPERTIES_ARGUMENTS;
319   ASSERT_TRUE(CreateOrUpdateShortcutLink(link_file_, link_properties_2_,
320                                          ShortcutOperation::kReplaceExisting));
321 
322   ShortcutProperties expected_properties(link_properties_2_);
323   expected_properties.set_arguments(link_properties_.arguments);
324   ValidateShortcut(link_file_, expected_properties);
325 }
326 
327 }  // namespace win
328 }  // namespace base
329