| 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/watcher_client_win.h" | 5 #include "components/browser_watcher/watcher_client_win.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base_switches.h" | 9 #include "base/base_switches.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 130 |
| 131 return ret; | 131 return ret; |
| 132 } | 132 } |
| 133 | 133 |
| 134 WatcherClient::CommandLineGenerator GetBaseCommandLineGenerator( | 134 WatcherClient::CommandLineGenerator GetBaseCommandLineGenerator( |
| 135 HandlePolicy handle_policy) { | 135 HandlePolicy handle_policy) { |
| 136 return base::Bind(&WatcherClientTest::GetBaseCommandLine, | 136 return base::Bind(&WatcherClientTest::GetBaseCommandLine, |
| 137 base::Unretained(this), handle_policy); | 137 base::Unretained(this), handle_policy); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void AssertSuccessfulExitCode(base::ProcessHandle handle) { | 140 void AssertSuccessfulExitCode(base::Process process) { |
| 141 ASSERT_NE(base::kNullProcessHandle, handle); | 141 ASSERT_TRUE(process.IsValid()); |
| 142 | |
| 143 // Duplicate the process handle to work around the fact that | |
| 144 // WaitForExitCode closes it(!!!). | |
| 145 base::ProcessHandle dupe = NULL; | |
| 146 ASSERT_TRUE(::DuplicateHandle(base::GetCurrentProcessHandle(), | |
| 147 handle, | |
| 148 base::GetCurrentProcessHandle(), | |
| 149 &dupe, | |
| 150 SYNCHRONIZE | PROCESS_QUERY_INFORMATION, | |
| 151 FALSE, | |
| 152 0)); | |
| 153 ASSERT_NE(base::kNullProcessHandle, dupe); | |
| 154 int exit_code = 0; | 142 int exit_code = 0; |
| 155 if (!base::WaitForExitCode(dupe, &exit_code)) { | 143 if (!process.WaitForExit(&exit_code)) |
| 156 base::CloseProcessHandle(dupe); | 144 FAIL() << "Process::WaitForExit failed."; |
| 157 FAIL() << "WaitForExitCode failed."; | |
| 158 } | |
| 159 ASSERT_EQ(0, exit_code); | 145 ASSERT_EQ(0, exit_code); |
| 160 } | 146 } |
| 161 | 147 |
| 162 // Inheritable process handle used for testing. | 148 // Inheritable process handle used for testing. |
| 163 base::win::ScopedHandle self_; | 149 base::win::ScopedHandle self_; |
| 164 }; | 150 }; |
| 165 | 151 |
| 166 } // namespace | 152 } // namespace |
| 167 | 153 |
| 168 // TODO(siggi): More testing - test WatcherClient base implementation. | 154 // TODO(siggi): More testing - test WatcherClient base implementation. |
| 169 | 155 |
| 170 TEST_F(WatcherClientTest, LaunchWatcherSucceeds) { | 156 TEST_F(WatcherClientTest, LaunchWatcherSucceeds) { |
| 171 // We can only use the non-legacy launch method on Windows Vista or better. | 157 // We can only use the non-legacy launch method on Windows Vista or better. |
| 172 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 158 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 173 return; | 159 return; |
| 174 | 160 |
| 175 WatcherClient client(GetBaseCommandLineGenerator(NO_LEAK_HANDLE)); | 161 WatcherClient client(GetBaseCommandLineGenerator(NO_LEAK_HANDLE)); |
| 176 ASSERT_FALSE(client.use_legacy_launch()); | 162 ASSERT_FALSE(client.use_legacy_launch()); |
| 177 | 163 |
| 178 client.LaunchWatcher(); | 164 client.LaunchWatcher(); |
| 179 | 165 |
| 180 ASSERT_NO_FATAL_FAILURE(AssertSuccessfulExitCode(client.process())); | 166 ASSERT_NO_FATAL_FAILURE( |
| 167 AssertSuccessfulExitCode(client.process().Duplicate())); |
| 181 } | 168 } |
| 182 | 169 |
| 183 TEST_F(WatcherClientTest, LaunchWatcherLegacyModeSucceeds) { | 170 TEST_F(WatcherClientTest, LaunchWatcherLegacyModeSucceeds) { |
| 184 // Test the XP-compatible legacy launch mode. This is expected to leak | 171 // Test the XP-compatible legacy launch mode. This is expected to leak |
| 185 // a handle to the child process. | 172 // a handle to the child process. |
| 186 WatcherClient client(GetBaseCommandLineGenerator(LEAK_HANDLE)); | 173 WatcherClient client(GetBaseCommandLineGenerator(LEAK_HANDLE)); |
| 187 | 174 |
| 188 // Use the legacy launch mode. | 175 // Use the legacy launch mode. |
| 189 client.set_use_legacy_launch(true); | 176 client.set_use_legacy_launch(true); |
| 190 | 177 |
| 191 client.LaunchWatcher(); | 178 client.LaunchWatcher(); |
| 192 | 179 |
| 193 ASSERT_NO_FATAL_FAILURE(AssertSuccessfulExitCode(client.process())); | 180 ASSERT_NO_FATAL_FAILURE( |
| 181 AssertSuccessfulExitCode(client.process().Duplicate())); |
| 194 } | 182 } |
| 195 | 183 |
| 196 TEST_F(WatcherClientTest, LegacyModeDetectedOnXP) { | 184 TEST_F(WatcherClientTest, LegacyModeDetectedOnXP) { |
| 197 // This test only works on Windows XP. | 185 // This test only works on Windows XP. |
| 198 if (base::win::GetVersion() > base::win::VERSION_XP) | 186 if (base::win::GetVersion() > base::win::VERSION_XP) |
| 199 return; | 187 return; |
| 200 | 188 |
| 201 // Test that the client detects the need to use legacy launch mode, and that | 189 // Test that the client detects the need to use legacy launch mode, and that |
| 202 // it works on Windows XP. | 190 // it works on Windows XP. |
| 203 WatcherClient client(GetBaseCommandLineGenerator(LEAK_HANDLE)); | 191 WatcherClient client(GetBaseCommandLineGenerator(LEAK_HANDLE)); |
| 204 ASSERT_TRUE(client.use_legacy_launch()); | 192 ASSERT_TRUE(client.use_legacy_launch()); |
| 205 | 193 |
| 206 client.LaunchWatcher(); | 194 client.LaunchWatcher(); |
| 207 | 195 |
| 208 ASSERT_NO_FATAL_FAILURE(AssertSuccessfulExitCode(client.process())); | 196 ASSERT_NO_FATAL_FAILURE( |
| 197 AssertSuccessfulExitCode(client.process().Duplicate())); |
| 209 } | 198 } |
| 210 | 199 |
| 211 } // namespace browser_watcher | 200 } // namespace browser_watcher |
| OLD | NEW |