shortcut_unittest.cc 16 KB

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