Chromium Code Reviews| 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.process_id()); | |
| 49 ASSERT_TRUE(process_info.process_handle() == NULL); | |
| 50 ASSERT_NE(0u, process_info.thread_id()); | |
| 51 ASSERT_FALSE(process_info.thread_handle() == 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.process_id()); | |
| 60 ASSERT_FALSE(process_info.process_handle() == NULL); | |
| 61 ASSERT_EQ(0u, process_info.thread_id()); | |
| 62 ASSERT_TRUE(process_info.thread_handle() == 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.process_id()); | |
| 75 ASSERT_TRUE(process_info.process_handle() == NULL); | |
| 76 ASSERT_EQ(0u, process_info.thread_id()); | |
| 77 ASSERT_TRUE(process_info.thread_handle() == 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.thread_id()); | |
| 85 ASSERT_FALSE(process_info.thread_handle() == NULL); | |
| 86 ASSERT_NE(0u, process_info.process_id()); | |
| 87 ASSERT_FALSE(process_info.process_handle() == NULL); | |
| 88 } | |
| 89 | |
| 90 TEST_F(ScopedProcessInformationTest, TakeWholeStruct) { | |
| 91 base::win::ScopedProcessInformation process_info; | |
| 92 DoCreateProcess("ReturnSeven", process_info.Receive()); | |
| 93 base::win::ScopedProcessInformation other; | |
| 94 *other.Receive() = process_info.Take(); | |
|
alexeypa (please no reviews)
2012/03/29 04:51:35
Swap could be useful here: process_info.Swap(*othe
erikwright (departed)
2012/03/30 17:30:27
Keeping this as an actual test of Take, but there
| |
| 95 | |
| 96 ASSERT_FALSE(process_info.IsValid()); | |
| 97 ASSERT_EQ(0u, process_info.process_id()); | |
| 98 ASSERT_TRUE(process_info.process_handle() == NULL); | |
| 99 ASSERT_EQ(0u, process_info.thread_id()); | |
| 100 ASSERT_TRUE(process_info.thread_handle() == NULL); | |
| 101 | |
| 102 // Validate that what was taken is good. | |
| 103 ASSERT_NE(0u, other.thread_id()); | |
| 104 ASSERT_NE(0u, other.process_id()); | |
| 105 int exit_code = 0; | |
| 106 ASSERT_TRUE(base::WaitForExitCode(other.TakeProcessHandle(), | |
| 107 &exit_code)); | |
| 108 ASSERT_EQ(7, exit_code); | |
| 109 ASSERT_TRUE(::CloseHandle(other.TakeThreadHandle())); | |
| 110 } | |
| 111 | |
| 112 TEST_F(ScopedProcessInformationTest, InitiallyInvalid) { | |
| 113 base::win::ScopedProcessInformation process_info; | |
| 114 ASSERT_FALSE(process_info.IsValid()); | |
| 115 } | |
| OLD | NEW |