| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <windows.h> |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/process_util.h" |
| 9 #include "base/test/multiprocess_test.h" |
| 10 #include "base/win/scoped_process_information.h" |
| 11 #include "testing/multiprocess_func_list.h" |
| 12 |
| 13 class ScopedProcessInformationTest : public base::MultiProcessTest { |
| 14 protected: |
| 15 void DoCreateProcess(const std::string& main_id, |
| 16 PROCESS_INFORMATION* process_handle); |
| 17 }; |
| 18 |
| 19 MULTIPROCESS_TEST_MAIN(ReturnSeven) { |
| 20 return 7; |
| 21 } |
| 22 |
| 23 MULTIPROCESS_TEST_MAIN(ReturnNine) { |
| 24 return 9; |
| 25 } |
| 26 |
| 27 void ScopedProcessInformationTest::DoCreateProcess( |
| 28 const std::string& main_id, PROCESS_INFORMATION* process_handle) { |
| 29 std::wstring cmd_line = |
| 30 this->MakeCmdLine(main_id, false).GetCommandLineString(); |
| 31 STARTUPINFO startup_info = {}; |
| 32 startup_info.cb = sizeof(startup_info); |
| 33 |
| 34 EXPECT_TRUE(::CreateProcess(NULL, |
| 35 const_cast<wchar_t*>(cmd_line.c_str()), |
| 36 NULL, NULL, false, 0, NULL, NULL, |
| 37 &startup_info, process_handle)); |
| 38 } |
| 39 |
| 40 TEST_F(ScopedProcessInformationTest, TakeProcess) { |
| 41 base::win::ScopedProcessInformation process_info; |
| 42 DoCreateProcess("ReturnSeven", process_info.Receive()); |
| 43 int exit_code = 0; |
| 44 ASSERT_TRUE(base::WaitForExitCode(process_info.TakeProcessHandle(), |
| 45 &exit_code)); |
| 46 ASSERT_EQ(7, exit_code); |
| 47 ASSERT_TRUE(process_info.IsValid()); |
| 48 ASSERT_EQ(0u, process_info.Get().dwProcessId); |
| 49 ASSERT_TRUE(process_info.Get().hProcess == NULL); |
| 50 ASSERT_NE(0u, process_info.Get().dwThreadId); |
| 51 ASSERT_FALSE(process_info.Get().hThread == NULL); |
| 52 } |
| 53 |
| 54 TEST_F(ScopedProcessInformationTest, TakeThread) { |
| 55 base::win::ScopedProcessInformation process_info; |
| 56 DoCreateProcess("ReturnSeven", process_info.Receive()); |
| 57 ASSERT_TRUE(::CloseHandle(process_info.TakeThreadHandle())); |
| 58 ASSERT_TRUE(process_info.IsValid()); |
| 59 ASSERT_NE(0u, process_info.Get().dwProcessId); |
| 60 ASSERT_FALSE(process_info.Get().hProcess == NULL); |
| 61 ASSERT_EQ(0u, process_info.Get().dwThreadId); |
| 62 ASSERT_TRUE(process_info.Get().hThread == NULL); |
| 63 } |
| 64 |
| 65 TEST_F(ScopedProcessInformationTest, TakeBoth) { |
| 66 base::win::ScopedProcessInformation process_info; |
| 67 DoCreateProcess("ReturnSeven", process_info.Receive()); |
| 68 int exit_code = 0; |
| 69 ASSERT_TRUE(base::WaitForExitCode(process_info.TakeProcessHandle(), |
| 70 &exit_code)); |
| 71 ASSERT_EQ(7, exit_code); |
| 72 ASSERT_TRUE(::CloseHandle(process_info.TakeThreadHandle())); |
| 73 ASSERT_FALSE(process_info.IsValid()); |
| 74 ASSERT_EQ(0u, process_info.Get().dwProcessId); |
| 75 ASSERT_TRUE(process_info.Get().hProcess == NULL); |
| 76 ASSERT_EQ(0u, process_info.Get().dwThreadId); |
| 77 ASSERT_TRUE(process_info.Get().hThread == NULL); |
| 78 } |
| 79 |
| 80 TEST_F(ScopedProcessInformationTest, TakeNothing) { |
| 81 base::win::ScopedProcessInformation process_info; |
| 82 DoCreateProcess("ReturnSeven", process_info.Receive()); |
| 83 ASSERT_TRUE(process_info.IsValid()); |
| 84 ASSERT_NE(0u, process_info.Get().dwThreadId); |
| 85 ASSERT_FALSE(process_info.Get().hThread == NULL); |
| 86 ASSERT_NE(0u, process_info.Get().dwProcessId); |
| 87 ASSERT_FALSE(process_info.Get().hProcess == NULL); |
| 88 } |
| 89 |
| 90 TEST_F(ScopedProcessInformationTest, BaseTake) { |
| 91 base::win::ScopedProcessInformation process_info; |
| 92 DoCreateProcess("ReturnSeven", process_info.Receive()); |
| 93 base::win::ScopedProcessInformation other(process_info.Take()); |
| 94 ASSERT_FALSE(process_info.IsValid()); |
| 95 ASSERT_EQ(0u, process_info.Get().dwProcessId); |
| 96 ASSERT_TRUE(process_info.Get().hProcess == NULL); |
| 97 ASSERT_EQ(0u, process_info.Get().dwThreadId); |
| 98 ASSERT_TRUE(process_info.Get().hThread == NULL); |
| 99 |
| 100 // Validate that what was taken is good. |
| 101 ASSERT_NE(0u, other.Get().dwThreadId); |
| 102 ASSERT_NE(0u, other.Get().dwProcessId); |
| 103 int exit_code = 0; |
| 104 ASSERT_TRUE(base::WaitForExitCode(other.TakeProcessHandle(), |
| 105 &exit_code)); |
| 106 ASSERT_EQ(7, exit_code); |
| 107 ASSERT_TRUE(::CloseHandle(other.TakeThreadHandle())); |
| 108 } |
| 109 |
| 110 |
| 111 TEST_F(ScopedProcessInformationTest, InitiallyInvalid) { |
| 112 base::win::ScopedProcessInformation process_info; |
| 113 ASSERT_FALSE(process_info.IsValid()); |
| 114 } |
| 115 |
| 116 TEST_F(ScopedProcessInformationTest, Set) { |
| 117 base::win::ScopedProcessInformation process_info; |
| 118 DoCreateProcess("ReturnSeven", process_info.Receive()); |
| 119 |
| 120 base::win::ScopedProcessInformation other; |
| 121 DoCreateProcess("ReturnNine", other.Receive()); |
| 122 |
| 123 process_info.Set(other.Take()); |
| 124 |
| 125 // Validate that what was taken is good. |
| 126 ASSERT_TRUE(process_info.IsValid()); |
| 127 ASSERT_NE(0u, process_info.Get().dwThreadId); |
| 128 ASSERT_NE(0u, process_info.Get().dwProcessId); |
| 129 int exit_code = 0; |
| 130 ASSERT_TRUE(base::WaitForExitCode(process_info.TakeProcessHandle(), |
| 131 &exit_code)); |
| 132 ASSERT_EQ(9, exit_code); |
| 133 ASSERT_TRUE(::CloseHandle(process_info.TakeThreadHandle())); |
| 134 } |
| OLD | NEW |