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 <string>
8
9 #include "base/command_line.h"
10 #include "base/process/kill.h"
11 #include "base/test/multiprocess_test.h"
12 #include "base/win/scoped_process_information.h"
13 #include "testing/multiprocess_func_list.h"
14
15 namespace {
16
17 const DWORD kProcessId = 4321;
18 const DWORD kThreadId = 1234;
19 const HANDLE kProcessHandle = reinterpret_cast<HANDLE>(7651);
20 const HANDLE kThreadHandle = reinterpret_cast<HANDLE>(1567);
21
MockCreateProcess(base::win::ScopedProcessInformation * process_info)22 void MockCreateProcess(base::win::ScopedProcessInformation* process_info) {
23 PROCESS_INFORMATION process_information = {};
24 process_information.dwProcessId = kProcessId;
25 process_information.dwThreadId = kThreadId;
26 process_information.hProcess = kProcessHandle;
27 process_information.hThread = kThreadHandle;
28 process_info->Set(process_information);
29 }
30
31 } // namespace
32
33 class ScopedProcessInformationTest : public base::MultiProcessTest {
34 protected:
35 void DoCreateProcess(const std::string& main_id,
36 PROCESS_INFORMATION* process_handle);
37 };
38
MULTIPROCESS_TEST_MAIN(ReturnSeven)39 MULTIPROCESS_TEST_MAIN(ReturnSeven) {
40 return 7;
41 }
42
MULTIPROCESS_TEST_MAIN(ReturnNine)43 MULTIPROCESS_TEST_MAIN(ReturnNine) {
44 return 9;
45 }
46
DoCreateProcess(const std::string & main_id,PROCESS_INFORMATION * process_handle)47 void ScopedProcessInformationTest::DoCreateProcess(
48 const std::string& main_id, PROCESS_INFORMATION* process_handle) {
49 std::wstring cmd_line = MakeCmdLine(main_id).GetCommandLineString();
50 STARTUPINFO startup_info = {};
51 startup_info.cb = sizeof(startup_info);
52
53 EXPECT_TRUE(::CreateProcess(NULL, &cmd_line[0],
54 NULL, NULL, false, 0, NULL, NULL,
55 &startup_info, process_handle));
56 }
57
TEST_F(ScopedProcessInformationTest,InitiallyInvalid)58 TEST_F(ScopedProcessInformationTest, InitiallyInvalid) {
59 base::win::ScopedProcessInformation process_info;
60 ASSERT_FALSE(process_info.IsValid());
61 }
62
TEST_F(ScopedProcessInformationTest,Receive)63 TEST_F(ScopedProcessInformationTest, Receive) {
64 base::win::ScopedProcessInformation process_info;
65 MockCreateProcess(&process_info);
66
67 EXPECT_TRUE(process_info.IsValid());
68 EXPECT_EQ(kProcessId, process_info.process_id());
69 EXPECT_EQ(kThreadId, process_info.thread_id());
70 EXPECT_EQ(kProcessHandle, process_info.process_handle());
71 EXPECT_EQ(kThreadHandle, process_info.thread_handle());
72 PROCESS_INFORMATION to_discard = process_info.Take();
73 }
74
TEST_F(ScopedProcessInformationTest,TakeProcess)75 TEST_F(ScopedProcessInformationTest, TakeProcess) {
76 base::win::ScopedProcessInformation process_info;
77 MockCreateProcess(&process_info);
78
79 HANDLE process = process_info.TakeProcessHandle();
80 EXPECT_EQ(kProcessHandle, process);
81 EXPECT_EQ(NULL, process_info.process_handle());
82 EXPECT_EQ(0, process_info.process_id());
83 EXPECT_TRUE(process_info.IsValid());
84 PROCESS_INFORMATION to_discard = process_info.Take();
85 }
86
TEST_F(ScopedProcessInformationTest,TakeThread)87 TEST_F(ScopedProcessInformationTest, TakeThread) {
88 base::win::ScopedProcessInformation process_info;
89 MockCreateProcess(&process_info);
90
91 HANDLE thread = process_info.TakeThreadHandle();
92 EXPECT_EQ(kThreadHandle, thread);
93 EXPECT_EQ(NULL, process_info.thread_handle());
94 EXPECT_EQ(0, process_info.thread_id());
95 EXPECT_TRUE(process_info.IsValid());
96 PROCESS_INFORMATION to_discard = process_info.Take();
97 }
98
TEST_F(ScopedProcessInformationTest,TakeBoth)99 TEST_F(ScopedProcessInformationTest, TakeBoth) {
100 base::win::ScopedProcessInformation process_info;
101 MockCreateProcess(&process_info);
102
103 HANDLE process = process_info.TakeProcessHandle();
104 HANDLE thread = process_info.TakeThreadHandle();
105 EXPECT_FALSE(process_info.IsValid());
106 PROCESS_INFORMATION to_discard = process_info.Take();
107 }
108
TEST_F(ScopedProcessInformationTest,TakeWholeStruct)109 TEST_F(ScopedProcessInformationTest, TakeWholeStruct) {
110 base::win::ScopedProcessInformation process_info;
111 MockCreateProcess(&process_info);
112
113 PROCESS_INFORMATION to_discard = process_info.Take();
114 EXPECT_EQ(kProcessId, to_discard.dwProcessId);
115 EXPECT_EQ(kThreadId, to_discard.dwThreadId);
116 EXPECT_EQ(kProcessHandle, to_discard.hProcess);
117 EXPECT_EQ(kThreadHandle, to_discard.hThread);
118 EXPECT_FALSE(process_info.IsValid());
119 }
120
TEST_F(ScopedProcessInformationTest,Duplicate)121 TEST_F(ScopedProcessInformationTest, Duplicate) {
122 PROCESS_INFORMATION temp_process_information;
123 DoCreateProcess("ReturnSeven", &temp_process_information);
124 base::win::ScopedProcessInformation process_info;
125 process_info.Set(temp_process_information);
126
127 base::win::ScopedProcessInformation duplicate;
128 duplicate.DuplicateFrom(process_info);
129
130 ASSERT_TRUE(process_info.IsValid());
131 ASSERT_NE(0u, process_info.process_id());
132 ASSERT_EQ(duplicate.process_id(), process_info.process_id());
133 ASSERT_NE(0u, process_info.thread_id());
134 ASSERT_EQ(duplicate.thread_id(), process_info.thread_id());
135
136 // Validate that we have separate handles that are good.
137 int exit_code = 0;
138 ASSERT_TRUE(base::WaitForExitCode(process_info.TakeProcessHandle(),
139 &exit_code));
140 ASSERT_EQ(7, exit_code);
141
142 exit_code = 0;
143 ASSERT_TRUE(base::WaitForExitCode(duplicate.TakeProcessHandle(),
144 &exit_code));
145 ASSERT_EQ(7, exit_code);
146
147 ASSERT_TRUE(::CloseHandle(process_info.TakeThreadHandle()));
148 ASSERT_TRUE(::CloseHandle(duplicate.TakeThreadHandle()));
149 }
150
TEST_F(ScopedProcessInformationTest,Set)151 TEST_F(ScopedProcessInformationTest, Set) {
152 base::win::ScopedProcessInformation base_process_info;
153 MockCreateProcess(&base_process_info);
154
155 PROCESS_INFORMATION base_struct = base_process_info.Take();
156
157 base::win::ScopedProcessInformation process_info;
158 process_info.Set(base_struct);
159
160 EXPECT_EQ(kProcessId, process_info.process_id());
161 EXPECT_EQ(kThreadId, process_info.thread_id());
162 EXPECT_EQ(kProcessHandle, process_info.process_handle());
163 EXPECT_EQ(kThreadHandle, process_info.thread_handle());
164 base_struct = process_info.Take();
165 }
166