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
|
// Copyright (c) 2009 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 <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <fstream>
#include <iostream>
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/scoped_comptr_win.h"
#include "chrome/installer/util/master_preferences.h"
#include "chrome/installer/util/shell_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
bool VerifyChromeShortcut(const std::wstring& exe_path,
const std::wstring& shortcut,
const std::wstring& description,
int icon_index) {
ScopedComPtr<IShellLink> i_shell_link;
ScopedComPtr<IPersistFile> i_persist_file;
// Get pointer to the IShellLink interface
bool failed = FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER));
EXPECT_FALSE(failed) << "Failed to get IShellLink";
if (failed)
return false;
// Query IShellLink for the IPersistFile interface
failed = FAILED(i_persist_file.QueryFrom(i_shell_link));
EXPECT_FALSE(failed) << "Failed to get IPersistFile";
if (failed)
return false;
failed = FAILED(i_persist_file->Load(shortcut.c_str(), 0));
EXPECT_FALSE(failed) << "Failed to load shortcut " << shortcut.c_str();
if (failed)
return false;
wchar_t long_path[MAX_PATH] = {0};
wchar_t short_path[MAX_PATH] = {0};
failed = ((::GetLongPathName(exe_path.c_str(), long_path, MAX_PATH) == 0) ||
(::GetShortPathName(exe_path.c_str(), short_path, MAX_PATH) == 0));
EXPECT_FALSE(failed) << "Failed to get long and short path names for "
<< exe_path;
if (failed)
return false;
wchar_t file_path[MAX_PATH] = {0};
failed = ((FAILED(i_shell_link->GetPath(file_path, MAX_PATH, NULL,
SLGP_UNCPRIORITY))) ||
((FilePath(file_path) != FilePath(long_path)) &&
(FilePath(file_path) != FilePath(short_path))));
EXPECT_FALSE(failed) << "File path " << file_path << " did not match with "
<< exe_path;
if (failed)
return false;
wchar_t desc[MAX_PATH] = {0};
failed = ((FAILED(i_shell_link->GetDescription(desc, MAX_PATH))) ||
(std::wstring(desc) != std::wstring(description)));
EXPECT_FALSE(failed) << "Description " << desc << " did not match with "
<< description;
if (failed)
return false;
wchar_t icon_path[MAX_PATH] = {0};
int index = 0;
failed = ((FAILED(i_shell_link->GetIconLocation(icon_path, MAX_PATH,
&index))) ||
((FilePath(file_path) != FilePath(long_path)) &&
(FilePath(file_path) != FilePath(short_path))) ||
(index != icon_index));
EXPECT_FALSE(failed);
if (failed)
return false;
return true;
}
class ShellUtilTest : public testing::Test {
protected:
virtual void SetUp() {
// Name a subdirectory of the user temp directory.
ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
test_dir_ = test_dir_.AppendASCII("ShellUtilTest");
// Create a fresh, empty copy of this test directory.
file_util::Delete(test_dir_, true);
file_util::CreateDirectoryW(test_dir_);
ASSERT_TRUE(file_util::PathExists(test_dir_));
}
virtual void TearDown() {
// Clean up test directory
ASSERT_TRUE(file_util::Delete(test_dir_, false));
ASSERT_FALSE(file_util::PathExists(test_dir_));
}
// The path to temporary directory used to contain the test operations.
FilePath test_dir_;
};
};
// Test that we can open archives successfully.
TEST_F(ShellUtilTest, UpdateChromeShortcutTest) {
// Create an executable in test path by copying ourself to it.
wchar_t exe_full_path_str[MAX_PATH];
EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0);
FilePath exe_full_path(exe_full_path_str);
FilePath exe_path = test_dir_.AppendASCII("setup.exe");
EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path));
FilePath shortcut_path = test_dir_.AppendASCII("shortcut.lnk");
const std::wstring description(L"dummy description");
EXPECT_TRUE(ShellUtil::UpdateChromeShortcut(exe_path.value(),
shortcut_path.value(),
description, true));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
shortcut_path.value(),
description, 0));
// Now specify an icon index in master prefs and make sure it works.
FilePath prefs_path = test_dir_.AppendASCII(
installer_util::kDefaultMasterPrefs);
std::ofstream file;
file.open(prefs_path.value().c_str());
ASSERT_TRUE(file.is_open());
file <<
"{"
" \"distribution\":{"
" \"chrome_shortcut_icon_index\" : 1"
" }"
"}";
file.close();
ASSERT_TRUE(file_util::Delete(shortcut_path, false));
EXPECT_TRUE(ShellUtil::UpdateChromeShortcut(exe_path.value(),
shortcut_path.value(),
description, true));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
shortcut_path.value(),
description, 1));
// Now change only description to update shortcut and make sure icon index
// doesn't change.
const std::wstring description2(L"dummy description 2");
EXPECT_TRUE(ShellUtil::UpdateChromeShortcut(exe_path.value(),
shortcut_path.value(),
description2, false));
EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(),
shortcut_path.value(),
description2, 1));
}
|