summaryrefslogtreecommitdiffstats
path: root/mojo/common/test/multiprocess_test_base.cc
blob: 527c2275134a8636f7cafb7611adaa7b690aed4e (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2013 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 "mojo/common/test/multiprocess_test_base.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/process/kill.h"
#include "base/process/process_handle.h"
// TODO(vtl): Remove build_config.h include when fully implemented on Windows.
#include "build/build_config.h"
#include "mojo/system/platform_channel_pair.h"

namespace mojo {
namespace test {

MultiprocessTestBase::MultiprocessTestBase()
    : test_child_handle_(base::kNullProcessHandle) {
}

MultiprocessTestBase::~MultiprocessTestBase() {
  CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
}

void MultiprocessTestBase::SetUp() {
  CHECK_EQ(test_child_handle_, base::kNullProcessHandle);

  MultiProcessTest::SetUp();

// TODO(vtl): Not implemented on Windows yet.
#if defined(OS_POSIX)
  platform_channel_pair_.reset(new embedder::PlatformChannelPair());
  server_platform_handle = platform_channel_pair_->PassServerHandle();
#endif
}

void MultiprocessTestBase::TearDown() {
  CHECK_EQ(test_child_handle_, base::kNullProcessHandle);

  server_platform_handle.reset();
  platform_channel_pair_.reset();

  MultiProcessTest::TearDown();
}

void MultiprocessTestBase::StartChild(const std::string& test_child_name) {
  CHECK(platform_channel_pair_.get());
  CHECK(!test_child_name.empty());
  CHECK_EQ(test_child_handle_, base::kNullProcessHandle);

  std::string test_child_main = test_child_name + "TestChildMain";

#if defined(OS_POSIX)
  CommandLine unused(CommandLine::NO_PROGRAM);
  base::FileHandleMappingVector fds_to_map;
  platform_channel_pair_->PrepareToPassClientHandleToChildProcess(&unused,
                                                                  &fds_to_map);
  test_child_handle_ = SpawnChild(test_child_main, fds_to_map, false);
#elif defined(OS_WIN)
  test_child_handle_  = SpawnChild(test_child_main, false);
#else
#error "Not supported yet."
#endif
// TODO(vtl): Not implemented on Windows yet.
#if defined(OS_POSIX)
  platform_channel_pair_->ChildProcessLaunched();
#endif

  CHECK_NE(test_child_handle_, base::kNullProcessHandle);
}

int MultiprocessTestBase::WaitForChildShutdown() {
  CHECK_NE(test_child_handle_, base::kNullProcessHandle);

  static const int kTimeoutSeconds = 5;
  int rv = -1;
  CHECK(base::WaitForExitCodeWithTimeout(
      test_child_handle_, &rv, base::TimeDelta::FromSeconds(kTimeoutSeconds)));
  base::CloseProcessHandle(test_child_handle_);
  test_child_handle_ = base::kNullProcessHandle;
  return rv;
}

CommandLine MultiprocessTestBase::MakeCmdLine(const std::string& procname,
                                              bool debug_on_start) {
  CHECK(platform_channel_pair_.get());

  CommandLine command_line =
      base::MultiProcessTest::MakeCmdLine(procname, debug_on_start);
// TODO(vtl): Not implemented on Windows yet.
#if defined(OS_POSIX)
  base::FileHandleMappingVector unused;
  platform_channel_pair_->PrepareToPassClientHandleToChildProcess(&command_line,
                                                                  &unused);
#endif
  return command_line;
}

// static
void MultiprocessTestBase::ChildSetup() {
  CHECK(CommandLine::InitializedForCurrentProcess());
// TODO(vtl): Not implemented on Windows yet.
#if defined(OS_POSIX)
  client_platform_handle =
      embedder::PlatformChannelPair::PassClientHandleFromParentProcess(
          *CommandLine::ForCurrentProcess());
#endif
}

// static
embedder::ScopedPlatformHandle MultiprocessTestBase::client_platform_handle;

}  // namespace test
}  // namespace mojo