Roll src/third_party/WebKit 46ff567:25f83be (svn 200502:200512)
[chromium-blink-merge.git] / base / win / scoped_process_information_unittest.cc
blob614504d414efa87b33bad5d78c17f715cbbe12da
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.
5 #include <windows.h>
7 #include <string>
9 #include "base/command_line.h"
10 #include "base/process/kill.h"
11 #include "base/process/process.h"
12 #include "base/test/multiprocess_test.h"
13 #include "base/win/scoped_process_information.h"
14 #include "testing/multiprocess_func_list.h"
16 namespace {
18 const DWORD kProcessId = 4321;
19 const DWORD kThreadId = 1234;
20 const HANDLE kProcessHandle = reinterpret_cast<HANDLE>(7651);
21 const HANDLE kThreadHandle = reinterpret_cast<HANDLE>(1567);
23 void MockCreateProcess(base::win::ScopedProcessInformation* process_info) {
24 PROCESS_INFORMATION process_information = {};
25 process_information.dwProcessId = kProcessId;
26 process_information.dwThreadId = kThreadId;
27 process_information.hProcess = kProcessHandle;
28 process_information.hThread = kThreadHandle;
29 process_info->Set(process_information);
32 } // namespace
34 class ScopedProcessInformationTest : public base::MultiProcessTest {
35 protected:
36 void DoCreateProcess(const std::string& main_id,
37 PROCESS_INFORMATION* process_handle);
40 MULTIPROCESS_TEST_MAIN(ReturnSeven) {
41 return 7;
44 MULTIPROCESS_TEST_MAIN(ReturnNine) {
45 return 9;
48 void ScopedProcessInformationTest::DoCreateProcess(
49 const std::string& main_id, PROCESS_INFORMATION* process_handle) {
50 std::wstring cmd_line = MakeCmdLine(main_id).GetCommandLineString();
51 STARTUPINFO startup_info = {};
52 startup_info.cb = sizeof(startup_info);
54 EXPECT_TRUE(::CreateProcess(NULL, &cmd_line[0],
55 NULL, NULL, false, 0, NULL, NULL,
56 &startup_info, process_handle));
59 TEST_F(ScopedProcessInformationTest, InitiallyInvalid) {
60 base::win::ScopedProcessInformation process_info;
61 ASSERT_FALSE(process_info.IsValid());
64 TEST_F(ScopedProcessInformationTest, Receive) {
65 base::win::ScopedProcessInformation process_info;
66 MockCreateProcess(&process_info);
68 EXPECT_TRUE(process_info.IsValid());
69 EXPECT_EQ(kProcessId, process_info.process_id());
70 EXPECT_EQ(kThreadId, process_info.thread_id());
71 EXPECT_EQ(kProcessHandle, process_info.process_handle());
72 EXPECT_EQ(kThreadHandle, process_info.thread_handle());
73 process_info.Take();
76 TEST_F(ScopedProcessInformationTest, TakeProcess) {
77 base::win::ScopedProcessInformation process_info;
78 MockCreateProcess(&process_info);
80 HANDLE process = process_info.TakeProcessHandle();
81 EXPECT_EQ(kProcessHandle, process);
82 EXPECT_EQ(NULL, process_info.process_handle());
83 EXPECT_EQ(0, process_info.process_id());
84 EXPECT_TRUE(process_info.IsValid());
85 process_info.Take();
88 TEST_F(ScopedProcessInformationTest, TakeThread) {
89 base::win::ScopedProcessInformation process_info;
90 MockCreateProcess(&process_info);
92 HANDLE thread = process_info.TakeThreadHandle();
93 EXPECT_EQ(kThreadHandle, thread);
94 EXPECT_EQ(NULL, process_info.thread_handle());
95 EXPECT_EQ(0, process_info.thread_id());
96 EXPECT_TRUE(process_info.IsValid());
97 process_info.Take();
100 TEST_F(ScopedProcessInformationTest, TakeBoth) {
101 base::win::ScopedProcessInformation process_info;
102 MockCreateProcess(&process_info);
104 process_info.TakeProcessHandle();
105 process_info.TakeThreadHandle();
106 EXPECT_FALSE(process_info.IsValid());
107 process_info.Take();
110 TEST_F(ScopedProcessInformationTest, TakeWholeStruct) {
111 base::win::ScopedProcessInformation process_info;
112 MockCreateProcess(&process_info);
114 PROCESS_INFORMATION to_discard = process_info.Take();
115 EXPECT_EQ(kProcessId, to_discard.dwProcessId);
116 EXPECT_EQ(kThreadId, to_discard.dwThreadId);
117 EXPECT_EQ(kProcessHandle, to_discard.hProcess);
118 EXPECT_EQ(kThreadHandle, to_discard.hThread);
119 EXPECT_FALSE(process_info.IsValid());
122 TEST_F(ScopedProcessInformationTest, Duplicate) {
123 PROCESS_INFORMATION temp_process_information;
124 DoCreateProcess("ReturnSeven", &temp_process_information);
125 base::win::ScopedProcessInformation process_info;
126 process_info.Set(temp_process_information);
128 base::win::ScopedProcessInformation duplicate;
129 duplicate.DuplicateFrom(process_info);
131 ASSERT_TRUE(process_info.IsValid());
132 ASSERT_NE(0u, process_info.process_id());
133 ASSERT_EQ(duplicate.process_id(), process_info.process_id());
134 ASSERT_NE(0u, process_info.thread_id());
135 ASSERT_EQ(duplicate.thread_id(), process_info.thread_id());
137 // Validate that we have separate handles that are good.
138 int exit_code = 0;
139 base::Process process(process_info.TakeProcessHandle());
140 ASSERT_TRUE(process.WaitForExit(&exit_code));
141 ASSERT_EQ(7, exit_code);
143 exit_code = 0;
144 base::Process dup_process(duplicate.TakeProcessHandle());
145 ASSERT_TRUE(dup_process.WaitForExit(&exit_code));
146 ASSERT_EQ(7, exit_code);
148 ASSERT_TRUE(::CloseHandle(process_info.TakeThreadHandle()));
149 ASSERT_TRUE(::CloseHandle(duplicate.TakeThreadHandle()));
152 TEST_F(ScopedProcessInformationTest, Set) {
153 base::win::ScopedProcessInformation base_process_info;
154 MockCreateProcess(&base_process_info);
156 PROCESS_INFORMATION base_struct = base_process_info.Take();
158 base::win::ScopedProcessInformation process_info;
159 process_info.Set(base_struct);
161 EXPECT_EQ(kProcessId, process_info.process_id());
162 EXPECT_EQ(kThreadId, process_info.thread_id());
163 EXPECT_EQ(kProcessHandle, process_info.process_handle());
164 EXPECT_EQ(kThreadHandle, process_info.thread_handle());
165 base_struct = process_info.Take();