OLD | NEW |
---|---|
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/browser_watcher/exit_code_watcher_win.h" | 5 #include "components/browser_watcher/exit_code_watcher_win.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/process/process.h" | 8 #include "base/process/process.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 base::Process process_; | 70 base::Process process_; |
71 bool is_killed_; | 71 bool is_killed_; |
72 }; | 72 }; |
73 | 73 |
74 class ExitCodeWatcherTest : public testing::Test { | 74 class ExitCodeWatcherTest : public testing::Test { |
75 public: | 75 public: |
76 typedef testing::Test Super; | 76 typedef testing::Test Super; |
77 | 77 |
78 static const int kExitCode = 0xCAFEBABE; | 78 static const int kExitCode = 0xCAFEBABE; |
79 | 79 |
80 ExitCodeWatcherTest() : | 80 ExitCodeWatcherTest() : cmd_line_(base::CommandLine::NO_PROGRAM) {} |
81 cmd_line_(base::CommandLine::NO_PROGRAM), | |
82 process_(base::kNullProcessHandle) { | |
83 } | |
84 | 81 |
85 virtual void SetUp() override { | 82 virtual void SetUp() override { |
86 Super::SetUp(); | 83 Super::SetUp(); |
87 | 84 |
88 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); | 85 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); |
89 } | 86 } |
90 | 87 |
91 virtual void TearDown() override { | |
92 if (process_ != base::kNullProcessHandle) { | |
Sigurður Ásgeirsson
2015/01/26 14:21:28
Ah - that was pretty redundant - thanks for the cl
| |
93 base::CloseProcessHandle(process_); | |
94 process_ = base::kNullProcessHandle; | |
95 } | |
96 | |
97 Super::TearDown(); | |
98 } | |
99 | |
100 base::Process OpenSelfWithAccess(uint32 access) { | 88 base::Process OpenSelfWithAccess(uint32 access) { |
101 return base::Process::OpenWithAccess(base::GetCurrentProcId(), access); | 89 return base::Process::OpenWithAccess(base::GetCurrentProcId(), access); |
102 } | 90 } |
103 | 91 |
104 void VerifyWroteExitCode(base::ProcessId proc_id, int exit_code) { | 92 void VerifyWroteExitCode(base::ProcessId proc_id, int exit_code) { |
105 base::win::RegistryValueIterator it( | 93 base::win::RegistryValueIterator it( |
106 HKEY_CURRENT_USER, kRegistryPath); | 94 HKEY_CURRENT_USER, kRegistryPath); |
107 | 95 |
108 ASSERT_EQ(1, it.ValueCount()); | 96 ASSERT_EQ(1, it.ValueCount()); |
109 base::win::RegKey key(HKEY_CURRENT_USER, | 97 base::win::RegKey key(HKEY_CURRENT_USER, |
110 kRegistryPath, | 98 kRegistryPath, |
111 KEY_QUERY_VALUE); | 99 KEY_QUERY_VALUE); |
112 | 100 |
113 // The value name should encode the process id at the start. | 101 // The value name should encode the process id at the start. |
114 EXPECT_TRUE(StartsWith(it.Name(), | 102 EXPECT_TRUE(StartsWith(it.Name(), |
115 base::StringPrintf(L"%d-", proc_id), | 103 base::StringPrintf(L"%d-", proc_id), |
116 false)); | 104 false)); |
117 DWORD value = 0; | 105 DWORD value = 0; |
118 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(it.Name(), &value)); | 106 ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(it.Name(), &value)); |
119 ASSERT_EQ(exit_code, value); | 107 ASSERT_EQ(exit_code, value); |
120 } | 108 } |
121 | 109 |
122 protected: | 110 protected: |
123 base::CommandLine cmd_line_; | 111 base::CommandLine cmd_line_; |
124 base::ProcessHandle process_; | |
125 registry_util::RegistryOverrideManager override_manager_; | 112 registry_util::RegistryOverrideManager override_manager_; |
126 }; | 113 }; |
127 | 114 |
128 } // namespace | 115 } // namespace |
129 | 116 |
130 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherInvalidHandleFailsInit) { | 117 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherInvalidHandleFailsInit) { |
131 ExitCodeWatcher watcher(kRegistryPath); | 118 ExitCodeWatcher watcher(kRegistryPath); |
132 | 119 |
133 // A waitable event has a non process-handle. | 120 // A waitable event has a non process-handle. |
134 base::Process event(::CreateEvent(NULL, false, false, NULL)); | 121 base::Process event(::CreateEvent(NULL, false, false, NULL)); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 // Kill the sleeper, and make sure it's exited before we continue. | 161 // Kill the sleeper, and make sure it's exited before we continue. |
175 ASSERT_NO_FATAL_FAILURE(sleeper.Kill(kExitCode, true)); | 162 ASSERT_NO_FATAL_FAILURE(sleeper.Kill(kExitCode, true)); |
176 | 163 |
177 watcher.WaitForExit(); | 164 watcher.WaitForExit(); |
178 EXPECT_EQ(kExitCode, watcher.exit_code()); | 165 EXPECT_EQ(kExitCode, watcher.exit_code()); |
179 | 166 |
180 VerifyWroteExitCode(sleeper.process().Pid(), kExitCode); | 167 VerifyWroteExitCode(sleeper.process().Pid(), kExitCode); |
181 } | 168 } |
182 | 169 |
183 } // namespace browser_watcher | 170 } // namespace browser_watcher |
OLD | NEW |