1 // Copyright 2013 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 "mojo/common/test/multiprocess_test_base.h" 6 7 #include "base/command_line.h" 8 #include "base/logging.h" 9 #include "base/process/kill.h" 10 #include "base/process/process_handle.h" 11 // TODO(vtl): Remove build_config.h include when fully implemented on Windows. 12 #include "build/build_config.h" 13 14 namespace mojo { 15 namespace test { 16 17 MultiprocessTestBase::MultiprocessTestBase() 18 : test_child_handle_(base::kNullProcessHandle) { 19 } 20 21 MultiprocessTestBase::~MultiprocessTestBase() { 22 CHECK_EQ(test_child_handle_, base::kNullProcessHandle); 23 } 24 25 void MultiprocessTestBase::SetUp() { 26 CHECK_EQ(test_child_handle_, base::kNullProcessHandle); 27 28 MultiProcessTest::SetUp(); 29 30 // TODO(vtl): Not implemented on Windows yet. 31 #if defined(OS_POSIX) 32 platform_server_channel = 33 system::PlatformServerChannel::Create("TestChannel"); 34 #endif 35 } 36 37 void MultiprocessTestBase::TearDown() { 38 CHECK_EQ(test_child_handle_, base::kNullProcessHandle); 39 40 platform_server_channel.reset(); 41 42 MultiProcessTest::TearDown(); 43 } 44 45 void MultiprocessTestBase::StartChild(const std::string& test_child_name) { 46 CHECK(platform_server_channel.get()); 47 CHECK(!test_child_name.empty()); 48 CHECK_EQ(test_child_handle_, base::kNullProcessHandle); 49 50 std::string test_child_main = test_child_name + "TestChildMain"; 51 52 #if defined(OS_POSIX) 53 CommandLine unused(CommandLine::NO_PROGRAM); 54 base::FileHandleMappingVector fds_to_map; 55 platform_server_channel->GetDataNeededToPassClientChannelToChildProcess( 56 &unused, &fds_to_map); 57 test_child_handle_ = SpawnChild(test_child_main, fds_to_map, false); 58 #elif defined(OS_WIN) 59 test_child_handle_ = SpawnChild(test_child_main, false); 60 #else 61 #error "Not supported yet." 62 #endif 63 // TODO(vtl): Not implemented on Windows yet. 64 #if defined(OS_POSIX) 65 platform_server_channel->ChildProcessLaunched(); 66 #endif 67 68 CHECK_NE(test_child_handle_, base::kNullProcessHandle); 69 } 70 71 int MultiprocessTestBase::WaitForChildShutdown() { 72 CHECK_NE(test_child_handle_, base::kNullProcessHandle); 73 74 static const int kTimeoutSeconds = 5; 75 int rv = -1; 76 CHECK(base::WaitForExitCodeWithTimeout( 77 test_child_handle_, &rv, base::TimeDelta::FromSeconds(kTimeoutSeconds))); 78 base::CloseProcessHandle(test_child_handle_); 79 test_child_handle_ = base::kNullProcessHandle; 80 return rv; 81 } 82 83 CommandLine MultiprocessTestBase::MakeCmdLine(const std::string& procname, 84 bool debug_on_start) { 85 CHECK(platform_server_channel.get()); 86 87 CommandLine command_line = 88 base::MultiProcessTest::MakeCmdLine(procname, debug_on_start); 89 // TODO(vtl): Not implemented on Windows yet. 90 #if defined(OS_POSIX) 91 base::FileHandleMappingVector unused; 92 platform_server_channel->GetDataNeededToPassClientChannelToChildProcess( 93 &command_line, &unused); 94 #endif 95 return command_line; 96 } 97 98 // static 99 void MultiprocessTestBase::ChildSetup() { 100 CHECK(CommandLine::InitializedForCurrentProcess()); 101 // TODO(vtl): Not implemented on Windows yet. 102 #if defined(OS_POSIX) 103 platform_client_channel = 104 system::PlatformClientChannel::CreateFromParentProcess( 105 *CommandLine::ForCurrentProcess()); 106 CHECK(platform_client_channel.get()); 107 #endif 108 } 109 110 // static 111 scoped_ptr<system::PlatformClientChannel> 112 MultiprocessTestBase::platform_client_channel; 113 114 } // namespace test 115 } // namespace mojo 116