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.
9 #include "base/command_line.h"
10 #include "base/test/multiprocess_test.h"
11 #include "base/win/scoped_handle.h"
12 #include "base/win/scoped_process_information.h"
13 #include "base/win/startup_information.h"
14 #include "base/win/windows_version.h"
15 #include "testing/multiprocess_func_list.h"
17 const wchar_t kSectionName
[] = L
"EventTestSection";
18 const size_t kSectionSize
= 4096;
20 MULTIPROCESS_TEST_MAIN(FireInheritedEvents
) {
21 HANDLE section
= ::OpenFileMappingW(PAGE_READWRITE
, false, kSectionName
);
22 HANDLE
* events
= reinterpret_cast<HANDLE
*>(::MapViewOfFile(section
,
23 PAGE_READWRITE
, 0, 0, kSectionSize
));
24 // This event should not be valid because it wasn't explicitly inherited.
25 if (::SetEvent(events
[1]))
27 // This event should be valid because it was explicitly inherited.
28 if (!::SetEvent(events
[0]))
34 class StartupInformationTest
: public base::MultiProcessTest
{};
36 // Verify that only the explicitly specified event is inherited.
37 TEST_F(StartupInformationTest
, InheritStdOut
) {
38 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
41 base::win::StartupInformation startup_info
;
43 HANDLE section
= ::CreateFileMappingW(INVALID_HANDLE_VALUE
, NULL
,
44 PAGE_READWRITE
, 0, kSectionSize
,
48 HANDLE
* events
= reinterpret_cast<HANDLE
*>(::MapViewOfFile(section
,
49 FILE_MAP_READ
| FILE_MAP_WRITE
, 0, 0, kSectionSize
));
51 // Make two inheritable events.
52 SECURITY_ATTRIBUTES security_attributes
= { sizeof(security_attributes
),
54 events
[0] = ::CreateEvent(&security_attributes
, false, false, NULL
);
55 ASSERT_TRUE(events
[0]);
56 events
[1] = ::CreateEvent(&security_attributes
, false, false, NULL
);
57 ASSERT_TRUE(events
[1]);
59 ASSERT_TRUE(startup_info
.InitializeProcThreadAttributeList(1));
60 ASSERT_TRUE(startup_info
.UpdateProcThreadAttribute(
61 PROC_THREAD_ATTRIBUTE_HANDLE_LIST
, &events
[0],
64 std::wstring cmd_line
=
65 this->MakeCmdLine("FireInheritedEvents", false).GetCommandLineString();
67 PROCESS_INFORMATION temp_process_info
= {};
68 ASSERT_TRUE(::CreateProcess(NULL
, const_cast<wchar_t*>(cmd_line
.c_str()),
69 NULL
, NULL
, true, EXTENDED_STARTUPINFO_PRESENT
,
70 NULL
, NULL
, startup_info
.startup_info(),
71 &temp_process_info
)) << ::GetLastError();
72 base::win::ScopedProcessInformation
process_info(temp_process_info
);
74 // Only the first event should be signalled
75 EXPECT_EQ(WAIT_OBJECT_0
, ::WaitForMultipleObjects(2, events
, false,