Skip to content

Commit c2b8877

Browse files
Sven ZhengCommit Bot
Sven Zheng
authored and
Commit Bot
committed
Refactor and reset acl for ServiceUtilCleanerRunningServiceTest
https://ci.chromium.org/p/chromium/builders/ci/Win7%20Tests%20%28dbg%29%281%29 https://ci.chromium.org/p/chromium/builders/ci/ToTWin%28dll%29 They don't fail anymore for CleanerSandboxInterfaceTest. So consider the fix works. I don't see it's necessary to make the resetacl more general(i.e. pass in base::FilePath) for now. But it is in a general place. Let me know if a general signature is favorable. Bug: 956016 Change-Id: I870e01dfa62a09ada372985dc5d9c9dcdc01b328 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1956145 Commit-Queue: Sven Zheng <svenzheng@chromium.org> Reviewed-by: Joe Mason <joenotcharles@chromium.org> Cr-Commit-Position: refs/heads/master@{#722661}
1 parent cb3ba7d commit c2b8877

File tree

4 files changed

+57
-36
lines changed

4 files changed

+57
-36
lines changed

chrome/chrome_cleaner/engines/broker/cleaner_sandbox_interface_unittest.cc

+5-27
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "base/path_service.h"
1919
#include "base/process/kill.h"
2020
#include "base/process/process.h"
21-
#include "base/process/launch.h"
2221
#include "base/strings/strcat.h"
2322
#include "base/strings/string16.h"
2423
#include "base/test/task_environment.h"
@@ -771,32 +770,11 @@ TEST_F(CleanerInterfaceRegistryTest, NtChangeRegistryValue_AllowNormalization) {
771770
normalize_all_values));
772771
}
773772

774-
// On Windows, sometimes the copied files don't have correct ACLs.
775-
// So we reset ACL before running the test.
776-
// See crbug.com/956016.
777-
void ResetACLs() {
778-
base::FilePath exe_path;
779-
ASSERT_TRUE(base::PathService::Get(base::BasePathKey::DIR_EXE, &exe_path));
780-
base::FilePath abs_path = base::MakeAbsoluteFilePath(exe_path);
781-
#ifdef NDEBUG
782-
base::FilePath ucrt_path = abs_path.Append(L"ucrtbase.dll");
783-
#else
784-
base::FilePath ucrt_path = abs_path.Append(L"ucrtbased.dll");
785-
#endif
786-
base::CommandLine cmd({L"icacls"});
787-
cmd.AppendArgPath(ucrt_path);
788-
cmd.AppendArg("/reset");
789-
cmd.AppendArg("/t");
790-
base::Process process = base::LaunchProcess(cmd, base::LaunchOptionsForTest());
791-
int exit_code = 0;
792-
ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(), &exit_code));
793-
ASSERT_EQ(exit_code, 0);
794-
}
795-
796-
class CleanerSandboxInterfaceTest : public ::testing::Test {
773+
class CleanerSandboxInterfaceRunningServiceTest : public ::testing::Test {
797774
public:
798775
static void SetUpTestCase() {
799-
ResetACLs();
776+
// Tests calling StartService() need this.
777+
ASSERT_TRUE(chrome_cleaner::ResetAclForUcrtbase());
800778
}
801779
};
802780

@@ -817,7 +795,7 @@ TEST(CleanerSandboxInterface, DeleteService_Success) {
817795
EXPECT_FALSE(chrome_cleaner::DoesServiceExist(service_handle.service_name()));
818796
}
819797

820-
TEST_F(CleanerSandboxInterfaceTest, DeleteService_Running) {
798+
TEST_F(CleanerSandboxInterfaceRunningServiceTest, DeleteService_Running) {
821799
ASSERT_TRUE(chrome_cleaner::EnsureNoTestServicesRunning());
822800

823801
chrome_cleaner::TestScopedServiceHandle service_handle;
@@ -830,7 +808,7 @@ TEST_F(CleanerSandboxInterfaceTest, DeleteService_Running) {
830808
EXPECT_FALSE(chrome_cleaner::DoesServiceExist(service_handle.service_name()));
831809
}
832810

833-
TEST_F(CleanerSandboxInterfaceTest, DeleteService_HandleHeld) {
811+
TEST_F(CleanerSandboxInterfaceRunningServiceTest, DeleteService_HandleHeld) {
834812
ASSERT_TRUE(chrome_cleaner::EnsureNoTestServicesRunning());
835813

836814
chrome_cleaner::TestScopedServiceHandle service_handle;

chrome/chrome_cleaner/os/system_util_cleaner_unittest.cc

+12-9
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ namespace chrome_cleaner {
4949

5050
namespace {
5151

52-
class ServiceUtilCleanerTest : public testing::Test {
52+
class ServiceUtilCleanerRunningServiceTest : public testing::Test {
5353
public:
54+
static void SetUpTestCase() {
55+
// Tests calling StartService() need this.
56+
ASSERT_TRUE(chrome_cleaner::ResetAclForUcrtbase());
57+
}
58+
5459
void SetUp() override {
5560
// Cleanup previous run. This may happen when previous execution of unittest
5661
// crashed, leaving background processes/services.
@@ -74,7 +79,7 @@ TEST(SystemUtilCleanerTests, OpenRegistryKeyWithInvalidParameter) {
7479
EXPECT_FALSE(key_path.Open(KEY_READ, &key));
7580
}
7681

77-
TEST_F(ServiceUtilCleanerTest, DeleteService) {
82+
TEST_F(ServiceUtilCleanerRunningServiceTest, DeleteService) {
7883
TestScopedServiceHandle service_handle;
7984
ASSERT_TRUE(service_handle.InstallService());
8085
service_handle.Close();
@@ -85,8 +90,7 @@ TEST_F(ServiceUtilCleanerTest, DeleteService) {
8590
EXPECT_FALSE(DoesServiceExist(service_handle.service_name()));
8691
}
8792

88-
// Disabled: https://crbug.com/956016
89-
TEST_F(ServiceUtilCleanerTest, DISABLED_StopAndDeleteRunningService) {
93+
TEST_F(ServiceUtilCleanerRunningServiceTest, StopAndDeleteRunningService) {
9094
// Install and launch the service.
9195
TestScopedServiceHandle service_handle;
9296
ASSERT_TRUE(service_handle.InstallService());
@@ -109,8 +113,7 @@ TEST_F(ServiceUtilCleanerTest, DISABLED_StopAndDeleteRunningService) {
109113
EXPECT_FALSE(IsProcessRunning(kTestServiceExecutableName));
110114
}
111115

112-
// Disabled: https://crbug.com/956016
113-
TEST_F(ServiceUtilCleanerTest, DISABLED_DeleteRunningService) {
116+
TEST_F(ServiceUtilCleanerRunningServiceTest, DeleteRunningService) {
114117
// Install and launch the service.
115118
TestScopedServiceHandle service_handle;
116119
ASSERT_TRUE(service_handle.InstallService());
@@ -128,7 +131,7 @@ TEST_F(ServiceUtilCleanerTest, DISABLED_DeleteRunningService) {
128131
EXPECT_FALSE(IsProcessRunning(kTestServiceExecutableName));
129132
}
130133

131-
TEST_F(ServiceUtilCleanerTest, QuarantineFolderPermission) {
134+
TEST_F(ServiceUtilCleanerRunningServiceTest, QuarantineFolderPermission) {
132135
base::ScopedPathOverride local_app_data_override(
133136
CsidlToPathServiceKey(CSIDL_LOCAL_APPDATA));
134137

@@ -174,7 +177,7 @@ TEST_F(ServiceUtilCleanerTest, QuarantineFolderPermission) {
174177
::LocalFree(security_descriptor);
175178
}
176179

177-
TEST_F(ServiceUtilCleanerTest, DefaultQuarantineFolderPath) {
180+
TEST_F(ServiceUtilCleanerRunningServiceTest, DefaultQuarantineFolderPath) {
178181
base::ScopedPathOverride local_app_data_override(
179182
CsidlToPathServiceKey(CSIDL_LOCAL_APPDATA));
180183

@@ -187,7 +190,7 @@ TEST_F(ServiceUtilCleanerTest, DefaultQuarantineFolderPath) {
187190
EXPECT_EQ(quarantine_path, default_path);
188191
}
189192

190-
TEST_F(ServiceUtilCleanerTest, SpecifiedQuarantineFolderPath) {
193+
TEST_F(ServiceUtilCleanerRunningServiceTest, SpecifiedQuarantineFolderPath) {
191194
// Override the default path of local appdata, so if we fail to redirect the
192195
// quarantine folder, the test won't drop any file in the real directory.
193196
base::ScopedPathOverride local_app_data_override(

chrome/chrome_cleaner/test/test_util.cc

+33
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
#include "base/files/file_util.h"
2121
#include "base/logging.h"
2222
#include "base/path_service.h"
23+
#include "base/process/launch.h"
2324
#include "base/strings/string_number_conversions.h"
2425
#include "base/strings/string_util.h"
2526
#include "base/strings/utf_string_conversions.h"
2627
#include "base/test/launcher/unit_test_launcher.h"
2728
#include "base/test/test_suite.h"
2829
#include "base/test/test_switches.h"
30+
#include "base/test/test_timeouts.h"
2931
#include "base/time/time.h"
3032
#include "base/win/scoped_com_initializer.h"
3133
#include "base/win/windows_version.h"
@@ -410,4 +412,35 @@ bool CheckTestPrivileges() {
410412
return true;
411413
}
412414

415+
bool ResetAclForUcrtbase() {
416+
base::FilePath exe_path;
417+
if (!base::PathService::Get(base::BasePathKey::DIR_EXE, &exe_path)) {
418+
LOG(ERROR) << "Failed to get directory path.";
419+
return false;
420+
}
421+
base::FilePath abs_path = base::MakeAbsoluteFilePath(exe_path);
422+
#ifdef NDEBUG
423+
base::FilePath ucrt_path = abs_path.Append(L"ucrtbase.dll");
424+
#else
425+
base::FilePath ucrt_path = abs_path.Append(L"ucrtbased.dll");
426+
#endif
427+
base::CommandLine cmd({L"icacls"});
428+
cmd.AppendArgPath(ucrt_path);
429+
cmd.AppendArg("/reset");
430+
cmd.AppendArg("/t");
431+
base::Process process =
432+
base::LaunchProcess(cmd, base::LaunchOptionsForTest());
433+
int exit_code = 0;
434+
if (!process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
435+
&exit_code)) {
436+
LOG(ERROR) << "Failed to reset acl for file " << ucrt_path.AsUTF16Unsafe();
437+
return false;
438+
}
439+
if (exit_code) {
440+
LOG(ERROR) << "Failed to reset acl for file " << ucrt_path.AsUTF16Unsafe()
441+
<< " with exit code " << exit_code;
442+
}
443+
return !exit_code;
444+
}
445+
413446
} // namespace chrome_cleaner

chrome/chrome_cleaner/test/test_util.h

+7
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ class ScopedTempDirNoWow64 : protected base::ScopedTempDir {
175175
// privileges. Returns false if the privileges could not be made correct.
176176
bool CheckTestPrivileges();
177177

178+
// On Windows, sometimes the copied files don't have correct ACLs.
179+
// So we reset ACL before running the test.
180+
// For debug, it will reset ucrtbased.dll. For release, it will reset
181+
// ucrtbase.dll.
182+
// See crbug.com/956016.
183+
bool ResetAclForUcrtbase();
184+
178185
// Accepts PUPData::PUP parameters with id equals to |expected_id|.
179186
MATCHER_P(PupHasId, expected_id, "") {
180187
return arg->signature().id == expected_id;

0 commit comments

Comments
 (0)