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