| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> |
| 10 |
| 9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 10 #include "base/process/process.h" | 12 #include "base/process/process.h" |
| 11 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 14 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/test/multiprocess_test.h" | 17 #include "base/test/multiprocess_test.h" |
| 16 #include "base/test/test_reg_util_win.h" | 18 #include "base/test/test_reg_util_win.h" |
| 17 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
| 18 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 118 |
| 117 } // namespace | 119 } // namespace |
| 118 | 120 |
| 119 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherInvalidHandleFailsInit) { | 121 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherInvalidHandleFailsInit) { |
| 120 ExitCodeWatcher watcher(kRegistryPath); | 122 ExitCodeWatcher watcher(kRegistryPath); |
| 121 | 123 |
| 122 // A waitable event has a non process-handle. | 124 // A waitable event has a non process-handle. |
| 123 base::Process event(::CreateEvent(NULL, false, false, NULL)); | 125 base::Process event(::CreateEvent(NULL, false, false, NULL)); |
| 124 | 126 |
| 125 // A non-process handle should fail. | 127 // A non-process handle should fail. |
| 126 EXPECT_FALSE(watcher.Initialize(event.Pass())); | 128 EXPECT_FALSE(watcher.Initialize(std::move(event))); |
| 127 } | 129 } |
| 128 | 130 |
| 129 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherNoAccessHandleFailsInit) { | 131 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherNoAccessHandleFailsInit) { |
| 130 ExitCodeWatcher watcher(kRegistryPath); | 132 ExitCodeWatcher watcher(kRegistryPath); |
| 131 | 133 |
| 132 // Open a SYNCHRONIZE-only handle to this process. | 134 // Open a SYNCHRONIZE-only handle to this process. |
| 133 base::Process self = OpenSelfWithAccess(SYNCHRONIZE); | 135 base::Process self = OpenSelfWithAccess(SYNCHRONIZE); |
| 134 ASSERT_TRUE(self.IsValid()); | 136 ASSERT_TRUE(self.IsValid()); |
| 135 | 137 |
| 136 // A process handle with insufficient access should fail. | 138 // A process handle with insufficient access should fail. |
| 137 EXPECT_FALSE(watcher.Initialize(self.Pass())); | 139 EXPECT_FALSE(watcher.Initialize(std::move(self))); |
| 138 } | 140 } |
| 139 | 141 |
| 140 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherSucceedsInit) { | 142 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherSucceedsInit) { |
| 141 ExitCodeWatcher watcher(kRegistryPath); | 143 ExitCodeWatcher watcher(kRegistryPath); |
| 142 | 144 |
| 143 // Open a handle to this process with sufficient access for the watcher. | 145 // Open a handle to this process with sufficient access for the watcher. |
| 144 base::Process self = | 146 base::Process self = |
| 145 OpenSelfWithAccess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION); | 147 OpenSelfWithAccess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION); |
| 146 ASSERT_TRUE(self.IsValid()); | 148 ASSERT_TRUE(self.IsValid()); |
| 147 | 149 |
| 148 // A process handle with sufficient access should succeed init. | 150 // A process handle with sufficient access should succeed init. |
| 149 EXPECT_TRUE(watcher.Initialize(self.Pass())); | 151 EXPECT_TRUE(watcher.Initialize(std::move(self))); |
| 150 } | 152 } |
| 151 | 153 |
| 152 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherOnExitedProcess) { | 154 TEST_F(ExitCodeWatcherTest, ExitCodeWatcherOnExitedProcess) { |
| 153 ScopedSleeperProcess sleeper; | 155 ScopedSleeperProcess sleeper; |
| 154 ASSERT_NO_FATAL_FAILURE(sleeper.Launch()); | 156 ASSERT_NO_FATAL_FAILURE(sleeper.Launch()); |
| 155 | 157 |
| 156 ExitCodeWatcher watcher(kRegistryPath); | 158 ExitCodeWatcher watcher(kRegistryPath); |
| 157 | 159 |
| 158 EXPECT_TRUE(watcher.Initialize(sleeper.process().Duplicate())); | 160 EXPECT_TRUE(watcher.Initialize(sleeper.process().Duplicate())); |
| 159 | 161 |
| 160 // Verify that the watcher wrote a sentinel for the process. | 162 // Verify that the watcher wrote a sentinel for the process. |
| 161 VerifyWroteExitCode(sleeper.process().Pid(), STILL_ACTIVE); | 163 VerifyWroteExitCode(sleeper.process().Pid(), STILL_ACTIVE); |
| 162 | 164 |
| 163 // Kill the sleeper, and make sure it's exited before we continue. | 165 // Kill the sleeper, and make sure it's exited before we continue. |
| 164 ASSERT_NO_FATAL_FAILURE(sleeper.Kill(kExitCode, true)); | 166 ASSERT_NO_FATAL_FAILURE(sleeper.Kill(kExitCode, true)); |
| 165 | 167 |
| 166 watcher.WaitForExit(); | 168 watcher.WaitForExit(); |
| 167 EXPECT_EQ(kExitCode, watcher.exit_code()); | 169 EXPECT_EQ(kExitCode, watcher.exit_code()); |
| 168 | 170 |
| 169 VerifyWroteExitCode(sleeper.process().Pid(), kExitCode); | 171 VerifyWroteExitCode(sleeper.process().Pid(), kExitCode); |
| 170 } | 172 } |
| 171 | 173 |
| 172 } // namespace browser_watcher | 174 } // namespace browser_watcher |
| OLD | NEW |