forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsystem_util_cleaner_unittest.cc
212 lines (174 loc) · 7.76 KB
/
system_util_cleaner_unittest.cc
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
// Copyright 2018 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 "chrome/chrome_cleaner/os/system_util_cleaner.h"
#include <windows.h>
#include <aclapi.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <stdint.h>
#include <wincrypt.h>
#include <algorithm>
#include <string>
#include <vector>
#include "base/base_paths_win.h"
#include "base/command_line.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/strings/string_util.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_path_override.h"
#include "base/test/test_shortcut_win.h"
#include "base/test/test_timeouts.h"
#include "base/win/shortcut.h"
#include "chrome/chrome_cleaner/constants/chrome_cleaner_switches.h"
#include "chrome/chrome_cleaner/constants/quarantine_constants.h"
#include "chrome/chrome_cleaner/os/disk_util.h"
#include "chrome/chrome_cleaner/os/file_path_sanitization.h"
#include "chrome/chrome_cleaner/os/layered_service_provider_api.h"
#include "chrome/chrome_cleaner/os/layered_service_provider_wrapper.h"
#include "chrome/chrome_cleaner/strings/string_util.h"
#include "chrome/chrome_cleaner/test/test_executables.h"
#include "chrome/chrome_cleaner/test/test_scoped_service_handle.h"
#include "chrome/chrome_cleaner/test/test_strings.h"
#include "chrome/chrome_cleaner/test/test_util.h"
#include "sandbox/win/src/sid.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_cleaner {
namespace {
class ServiceUtilCleanerRunningServiceTest : public testing::Test {
public:
static void SetUpTestCase() {
// Tests calling StartService() need this.
ASSERT_TRUE(chrome_cleaner::ResetAclForUcrtbase());
}
void SetUp() override {
// Cleanup previous run. This may happen when previous execution of unittest
// crashed, leaving background processes/services.
ASSERT_TRUE(EnsureNoTestServicesRunning());
}
};
} // namespace
TEST(SystemUtilCleanerTests, AcquireDebugRightsPrivileges) {
ASSERT_FALSE(HasDebugRightsPrivileges());
EXPECT_TRUE(AcquireDebugRightsPrivileges());
EXPECT_TRUE(HasDebugRightsPrivileges());
EXPECT_TRUE(ReleaseDebugRightsPrivileges());
EXPECT_FALSE(HasDebugRightsPrivileges());
}
TEST(SystemUtilCleanerTests, OpenRegistryKeyWithInvalidParameter) {
const RegKeyPath key_path(HKEY_LOCAL_MACHINE, L"non-existing key path");
base::win::RegKey key;
EXPECT_FALSE(key_path.Open(KEY_READ, &key));
}
TEST_F(ServiceUtilCleanerRunningServiceTest, DeleteService) {
TestScopedServiceHandle service_handle;
ASSERT_TRUE(service_handle.InstallService());
service_handle.Close();
EXPECT_TRUE(DoesServiceExist(service_handle.service_name()));
EXPECT_TRUE(DeleteService(service_handle.service_name()));
EXPECT_TRUE(WaitForServiceDeleted(service_handle.service_name()));
EXPECT_FALSE(DoesServiceExist(service_handle.service_name()));
}
// TODO(crbug.com/1061171): Test is flaky.
TEST_F(ServiceUtilCleanerRunningServiceTest, DISABLED_StopAndDeleteRunningService) {
// Install and launch the service.
TestScopedServiceHandle service_handle;
ASSERT_TRUE(service_handle.InstallService());
ASSERT_TRUE(service_handle.StartService());
EXPECT_TRUE(DoesServiceExist(service_handle.service_name()));
EXPECT_TRUE(IsProcessRunning(kTestServiceExecutableName));
service_handle.Close();
// Stop the service.
EXPECT_TRUE(StopService(service_handle.service_name()));
EXPECT_TRUE(WaitForProcessesStopped(kTestServiceExecutableName));
EXPECT_TRUE(WaitForServiceStopped(service_handle.service_name()));
// Delete the service
EXPECT_TRUE(DeleteService(service_handle.service_name()));
EXPECT_TRUE(WaitForServiceDeleted(service_handle.service_name()));
// The service must be fully stopped and deleted.
EXPECT_FALSE(DoesServiceExist(service_handle.service_name()));
EXPECT_FALSE(IsProcessRunning(kTestServiceExecutableName));
}
// TODO(crbug.com/1061171): Test is flaky.
TEST_F(ServiceUtilCleanerRunningServiceTest, DISABLED_DeleteRunningService) {
// Install and launch the service.
TestScopedServiceHandle service_handle;
ASSERT_TRUE(service_handle.InstallService());
ASSERT_TRUE(service_handle.StartService());
EXPECT_TRUE(DoesServiceExist(service_handle.service_name()));
EXPECT_TRUE(IsProcessRunning(kTestServiceExecutableName));
service_handle.Close();
// Delete the service
EXPECT_TRUE(DeleteService(service_handle.service_name()));
// The service must be fully stopped and deleted.
EXPECT_TRUE(WaitForProcessesStopped(kTestServiceExecutableName));
EXPECT_FALSE(DoesServiceExist(service_handle.service_name()));
EXPECT_FALSE(IsProcessRunning(kTestServiceExecutableName));
}
TEST_F(ServiceUtilCleanerRunningServiceTest, QuarantineFolderPermission) {
base::ScopedPathOverride local_app_data_override(
CsidlToPathServiceKey(CSIDL_LOCAL_APPDATA));
base::FilePath quarantine_path;
EXPECT_TRUE(InitializeQuarantineFolder(&quarantine_path));
PSID owner_sid;
PACL dacl;
PSECURITY_DESCRIPTOR security_descriptor;
// Get the ownership and ACL of the quarantine folder and check the values.
ASSERT_EQ(static_cast<DWORD>(ERROR_SUCCESS),
::GetNamedSecurityInfo(
quarantine_path.value().c_str(), SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
&owner_sid, /*psidGroup=*/nullptr, &dacl,
/*pSacl=*/nullptr, &security_descriptor));
sandbox::Sid admin_sid(WinBuiltinAdministratorsSid);
ASSERT_TRUE(admin_sid.IsValid());
// Check that the administrator group is the owner.
EXPECT_TRUE(::EqualSid(owner_sid, admin_sid.GetPSID()));
EXPLICIT_ACCESS* explicit_access;
ULONG entry_count;
ASSERT_EQ(static_cast<DWORD>(ERROR_SUCCESS),
::GetExplicitEntriesFromAcl(dacl, &entry_count, &explicit_access));
// ACL should only contains one rule.
ASSERT_EQ(1UL, entry_count);
// Administrator group should have full access.
EXPECT_EQ(static_cast<DWORD>(FILE_ALL_ACCESS),
FILE_ALL_ACCESS & explicit_access[0].grfAccessPermissions);
EXPECT_EQ(static_cast<DWORD>(NO_INHERITANCE),
explicit_access[0].grfInheritance);
EXPECT_EQ(TRUSTEE_IS_SID, explicit_access[0].Trustee.TrusteeForm);
// The trustee of the rule should be administrator group.
EXPECT_TRUE(
::EqualSid(explicit_access[0].Trustee.ptstrName, admin_sid.GetPSID()));
::LocalFree(explicit_access);
::LocalFree(security_descriptor);
}
TEST_F(ServiceUtilCleanerRunningServiceTest, DefaultQuarantineFolderPath) {
base::ScopedPathOverride local_app_data_override(
CsidlToPathServiceKey(CSIDL_LOCAL_APPDATA));
base::FilePath quarantine_path;
EXPECT_TRUE(InitializeQuarantineFolder(&quarantine_path));
base::FilePath product_path;
ASSERT_TRUE(GetAppDataProductDirectory(&product_path));
const base::FilePath default_path = product_path.Append(kQuarantineFolder);
EXPECT_EQ(quarantine_path, default_path);
}
TEST_F(ServiceUtilCleanerRunningServiceTest, SpecifiedQuarantineFolderPath) {
// Override the default path of local appdata, so if we fail to redirect the
// quarantine folder, the test won't drop any file in the real directory.
base::ScopedPathOverride local_app_data_override(
CsidlToPathServiceKey(CSIDL_LOCAL_APPDATA));
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::test::ScopedCommandLine scoped_command_line;
scoped_command_line.GetProcessCommandLine()->AppendSwitchPath(
kQuarantineDirSwitch, temp_dir.GetPath());
base::FilePath quarantine_path;
EXPECT_TRUE(InitializeQuarantineFolder(&quarantine_path));
EXPECT_EQ(quarantine_path, temp_dir.GetPath());
}
} // namespace chrome_cleaner