| Index: base/win/scoped_process_information_unittest.cc
|
| diff --git a/base/win/scoped_process_information_unittest.cc b/base/win/scoped_process_information_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..02e7f02e5440fad26fc8eb87524dd4ad307b8ce8
|
| --- /dev/null
|
| +++ b/base/win/scoped_process_information_unittest.cc
|
| @@ -0,0 +1,134 @@
|
| +// Copyright (c) 2012 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 <windows.h>
|
| +
|
| +#include "base/command_line.h"
|
| +#include "base/process_util.h"
|
| +#include "base/test/multiprocess_test.h"
|
| +#include "base/win/scoped_process_information.h"
|
| +#include "testing/multiprocess_func_list.h"
|
| +
|
| +class ScopedProcessInformationTest : public base::MultiProcessTest {
|
| + protected:
|
| + void DoCreateProcess(const std::string& main_id,
|
| + PROCESS_INFORMATION* process_handle);
|
| +};
|
| +
|
| +MULTIPROCESS_TEST_MAIN(ReturnSeven) {
|
| + return 7;
|
| +}
|
| +
|
| +MULTIPROCESS_TEST_MAIN(ReturnNine) {
|
| + return 9;
|
| +}
|
| +
|
| +void ScopedProcessInformationTest::DoCreateProcess(
|
| + const std::string& main_id, PROCESS_INFORMATION* process_handle) {
|
| + std::wstring cmd_line =
|
| + this->MakeCmdLine(main_id, false).GetCommandLineString();
|
| + STARTUPINFO startup_info = {};
|
| + startup_info.cb = sizeof(startup_info);
|
| +
|
| + EXPECT_TRUE(::CreateProcess(NULL,
|
| + const_cast<wchar_t*>(cmd_line.c_str()),
|
| + NULL, NULL, false, 0, NULL, NULL,
|
| + &startup_info, process_handle));
|
| +}
|
| +
|
| +TEST_F(ScopedProcessInformationTest, TakeProcess) {
|
| + base::win::ScopedProcessInformation process_info;
|
| + DoCreateProcess("ReturnSeven", process_info.Receive());
|
| + int exit_code = 0;
|
| + ASSERT_TRUE(base::WaitForExitCode(process_info.TakeProcessHandle(),
|
| + &exit_code));
|
| + ASSERT_EQ(7, exit_code);
|
| + ASSERT_TRUE(process_info.IsValid());
|
| + ASSERT_EQ(0u, process_info.Get().dwProcessId);
|
| + ASSERT_TRUE(process_info.Get().hProcess == NULL);
|
| + ASSERT_NE(0u, process_info.Get().dwThreadId);
|
| + ASSERT_FALSE(process_info.Get().hThread == NULL);
|
| +}
|
| +
|
| +TEST_F(ScopedProcessInformationTest, TakeThread) {
|
| + base::win::ScopedProcessInformation process_info;
|
| + DoCreateProcess("ReturnSeven", process_info.Receive());
|
| + ASSERT_TRUE(::CloseHandle(process_info.TakeThreadHandle()));
|
| + ASSERT_TRUE(process_info.IsValid());
|
| + ASSERT_NE(0u, process_info.Get().dwProcessId);
|
| + ASSERT_FALSE(process_info.Get().hProcess == NULL);
|
| + ASSERT_EQ(0u, process_info.Get().dwThreadId);
|
| + ASSERT_TRUE(process_info.Get().hThread == NULL);
|
| +}
|
| +
|
| +TEST_F(ScopedProcessInformationTest, TakeBoth) {
|
| + base::win::ScopedProcessInformation process_info;
|
| + DoCreateProcess("ReturnSeven", process_info.Receive());
|
| + int exit_code = 0;
|
| + ASSERT_TRUE(base::WaitForExitCode(process_info.TakeProcessHandle(),
|
| + &exit_code));
|
| + ASSERT_EQ(7, exit_code);
|
| + ASSERT_TRUE(::CloseHandle(process_info.TakeThreadHandle()));
|
| + ASSERT_FALSE(process_info.IsValid());
|
| + ASSERT_EQ(0u, process_info.Get().dwProcessId);
|
| + ASSERT_TRUE(process_info.Get().hProcess == NULL);
|
| + ASSERT_EQ(0u, process_info.Get().dwThreadId);
|
| + ASSERT_TRUE(process_info.Get().hThread == NULL);
|
| +}
|
| +
|
| +TEST_F(ScopedProcessInformationTest, TakeNothing) {
|
| + base::win::ScopedProcessInformation process_info;
|
| + DoCreateProcess("ReturnSeven", process_info.Receive());
|
| + ASSERT_TRUE(process_info.IsValid());
|
| + ASSERT_NE(0u, process_info.Get().dwThreadId);
|
| + ASSERT_FALSE(process_info.Get().hThread == NULL);
|
| + ASSERT_NE(0u, process_info.Get().dwProcessId);
|
| + ASSERT_FALSE(process_info.Get().hProcess == NULL);
|
| +}
|
| +
|
| +TEST_F(ScopedProcessInformationTest, BaseTake) {
|
| + base::win::ScopedProcessInformation process_info;
|
| + DoCreateProcess("ReturnSeven", process_info.Receive());
|
| + base::win::ScopedProcessInformation other(process_info.Take());
|
| + ASSERT_FALSE(process_info.IsValid());
|
| + ASSERT_EQ(0u, process_info.Get().dwProcessId);
|
| + ASSERT_TRUE(process_info.Get().hProcess == NULL);
|
| + ASSERT_EQ(0u, process_info.Get().dwThreadId);
|
| + ASSERT_TRUE(process_info.Get().hThread == NULL);
|
| +
|
| + // Validate that what was taken is good.
|
| + ASSERT_NE(0u, other.Get().dwThreadId);
|
| + ASSERT_NE(0u, other.Get().dwProcessId);
|
| + int exit_code = 0;
|
| + ASSERT_TRUE(base::WaitForExitCode(other.TakeProcessHandle(),
|
| + &exit_code));
|
| + ASSERT_EQ(7, exit_code);
|
| + ASSERT_TRUE(::CloseHandle(other.TakeThreadHandle()));
|
| +}
|
| +
|
| +
|
| +TEST_F(ScopedProcessInformationTest, InitiallyInvalid) {
|
| + base::win::ScopedProcessInformation process_info;
|
| + ASSERT_FALSE(process_info.IsValid());
|
| +}
|
| +
|
| +TEST_F(ScopedProcessInformationTest, Set) {
|
| + base::win::ScopedProcessInformation process_info;
|
| + DoCreateProcess("ReturnSeven", process_info.Receive());
|
| +
|
| + base::win::ScopedProcessInformation other;
|
| + DoCreateProcess("ReturnNine", other.Receive());
|
| +
|
| + process_info.Set(other.Take());
|
| +
|
| + // Validate that what was taken is good.
|
| + ASSERT_TRUE(process_info.IsValid());
|
| + ASSERT_NE(0u, process_info.Get().dwThreadId);
|
| + ASSERT_NE(0u, process_info.Get().dwProcessId);
|
| + int exit_code = 0;
|
| + ASSERT_TRUE(base::WaitForExitCode(process_info.TakeProcessHandle(),
|
| + &exit_code));
|
| + ASSERT_EQ(9, exit_code);
|
| + ASSERT_TRUE(::CloseHandle(process_info.TakeThreadHandle()));
|
| +}
|
|
|