summaryrefslogtreecommitdiffstats
path: root/base/win/startup_information_unittest.cc
blob: 1903564d87b61a43eee7e3ac98118b8de1d89e05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <windows.h>

#include <string>

#include "base/command_line.h"
#include "base/test/multiprocess_test.h"
#include "base/win/scoped_handle.h"
#include "base/win/scoped_process_information.h"
#include "base/win/startup_information.h"
#include "base/win/windows_version.h"
#include "testing/multiprocess_func_list.h"

const wchar_t kSectionName[] = L"EventTestSection";
const size_t kSectionSize = 4096;

MULTIPROCESS_TEST_MAIN(FireInheritedEvents) {
  HANDLE section = ::OpenFileMappingW(PAGE_READWRITE, false, kSectionName);
  HANDLE* events = reinterpret_cast<HANDLE*>(::MapViewOfFile(section,
      PAGE_READWRITE, 0, 0, kSectionSize));
  // This event should not be valid because it wasn't explicitly inherited.
  if (::SetEvent(events[1]))
    return -1;
  // This event should be valid because it was explicitly inherited.
  if (!::SetEvent(events[0]))
    return -1;

  return 0;
}

class StartupInformationTest : public base::MultiProcessTest {};

// Verify that only the explicitly specified event is inherited.
TEST_F(StartupInformationTest, InheritStdOut) {
  if (base::win::GetVersion() < base::win::VERSION_VISTA)
    return;

  base::win::ScopedProcessInformation process_info;
  base::win::StartupInformation startup_info;

  HANDLE section = ::CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
                                        PAGE_READWRITE, 0, kSectionSize,
                                        kSectionName);
  ASSERT_TRUE(section);

  HANDLE* events = reinterpret_cast<HANDLE*>(::MapViewOfFile(section,
      FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kSectionSize));

  // Make two inheritable events.
  SECURITY_ATTRIBUTES security_attributes = { sizeof(security_attributes),
                                              NULL, true };
  events[0] = ::CreateEvent(&security_attributes, false, false, NULL);
  ASSERT_TRUE(events[0]);
  events[1] = ::CreateEvent(&security_attributes, false, false, NULL);
  ASSERT_TRUE(events[1]);

  ASSERT_TRUE(startup_info.InitializeProcThreadAttributeList(1));
  ASSERT_TRUE(startup_info.UpdateProcThreadAttribute(
      PROC_THREAD_ATTRIBUTE_HANDLE_LIST, &events[0],
      sizeof(events[0])));

  std::wstring cmd_line =
      this->MakeCmdLine("FireInheritedEvents", false).GetCommandLineString();

  ASSERT_TRUE(::CreateProcess(NULL, const_cast<wchar_t*>(cmd_line.c_str()),
                              NULL, NULL, true, EXTENDED_STARTUPINFO_PRESENT,
                              NULL, NULL, startup_info.startup_info(),
                              process_info.Receive())) << ::GetLastError();
  // Only the first event should be signalled
  EXPECT_EQ(WAIT_OBJECT_0, ::WaitForMultipleObjects(2, events, false,
                                                    4000));
}