| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/test/multiprocess_test_helper.h" | 5 #include "mojo/edk/test/multiprocess_test_helper.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/posix/global_descriptors.h" |
| 9 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| 10 #include "mojo/edk/embedder/platform_channel_pair.h" | 11 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 12 #include "mojo/edk/platform/platform_handle.h" |
| 13 #include "mojo/edk/platform/scoped_platform_handle.h" |
| 11 | 14 |
| 15 using mojo::platform::PlatformHandle; |
| 12 using mojo::platform::ScopedPlatformHandle; | 16 using mojo::platform::ScopedPlatformHandle; |
| 13 | 17 |
| 14 namespace mojo { | 18 namespace mojo { |
| 15 namespace test { | 19 namespace test { |
| 16 | 20 |
| 17 const char kPlatformChannelHandleInfoSwitch[] = "platform-channel-handle-info"; | |
| 18 | |
| 19 MultiprocessTestHelper::MultiprocessTestHelper() | 21 MultiprocessTestHelper::MultiprocessTestHelper() |
| 20 : platform_channel_pair_(new embedder::PlatformChannelPair()) { | 22 : platform_channel_pair_(new embedder::PlatformChannelPair()) { |
| 21 server_platform_handle = platform_channel_pair_->PassServerHandle(); | 23 server_platform_handle = platform_channel_pair_->handle0.Pass(); |
| 22 } | 24 } |
| 23 | 25 |
| 24 MultiprocessTestHelper::~MultiprocessTestHelper() { | 26 MultiprocessTestHelper::~MultiprocessTestHelper() { |
| 25 CHECK(!test_child_.IsValid()); | 27 CHECK(!test_child_.IsValid()); |
| 26 server_platform_handle.reset(); | 28 server_platform_handle.reset(); |
| 27 platform_channel_pair_.reset(); | 29 platform_channel_pair_.reset(); |
| 28 } | 30 } |
| 29 | 31 |
| 30 void MultiprocessTestHelper::StartChild(const std::string& test_child_name) { | 32 void MultiprocessTestHelper::StartChild(const std::string& test_child_name) { |
| 31 StartChildWithExtraSwitch(test_child_name, std::string(), std::string()); | 33 StartChildWithExtraSwitch(test_child_name, std::string(), std::string()); |
| 32 } | 34 } |
| 33 | 35 |
| 34 void MultiprocessTestHelper::StartChildWithExtraSwitch( | 36 void MultiprocessTestHelper::StartChildWithExtraSwitch( |
| 35 const std::string& test_child_name, | 37 const std::string& test_child_name, |
| 36 const std::string& switch_string, | 38 const std::string& switch_string, |
| 37 const std::string& switch_value) { | 39 const std::string& switch_value) { |
| 38 CHECK(platform_channel_pair_); | 40 CHECK(platform_channel_pair_); |
| 39 CHECK(!test_child_name.empty()); | 41 CHECK(!test_child_name.empty()); |
| 40 CHECK(!test_child_.IsValid()); | 42 CHECK(!test_child_.IsValid()); |
| 41 | 43 |
| 42 std::string test_child_main = test_child_name + "TestChildMain"; | 44 std::string test_child_main = test_child_name + "TestChildMain"; |
| 43 | 45 |
| 44 std::string string_for_child; | |
| 45 embedder::HandlePassingInformation handle_passing_info; | |
| 46 platform_channel_pair_->PrepareToPassClientHandleToChildProcess( | |
| 47 &string_for_child, &handle_passing_info); | |
| 48 | |
| 49 base::CommandLine command_line( | 46 base::CommandLine command_line( |
| 50 base::GetMultiProcessTestChildBaseCommandLine()); | 47 base::GetMultiProcessTestChildBaseCommandLine()); |
| 51 command_line.AppendSwitchASCII(kPlatformChannelHandleInfoSwitch, | |
| 52 string_for_child); | |
| 53 | |
| 54 if (!switch_string.empty()) { | 48 if (!switch_string.empty()) { |
| 55 CHECK(!command_line.HasSwitch(switch_string)); | 49 CHECK(!command_line.HasSwitch(switch_string)); |
| 56 if (!switch_value.empty()) | 50 if (!switch_value.empty()) |
| 57 command_line.AppendSwitchASCII(switch_string, switch_value); | 51 command_line.AppendSwitchASCII(switch_string, switch_value); |
| 58 else | 52 else |
| 59 command_line.AppendSwitch(switch_string); | 53 command_line.AppendSwitch(switch_string); |
| 60 } | 54 } |
| 61 | 55 |
| 56 base::FileHandleMappingVector fds_to_remap; |
| 57 fds_to_remap.push_back( |
| 58 std::pair<int, int>(platform_channel_pair_->handle1.get().fd, |
| 59 base::GlobalDescriptors::kBaseDescriptor)); |
| 62 base::LaunchOptions options; | 60 base::LaunchOptions options; |
| 63 options.fds_to_remap = &handle_passing_info; | 61 options.fds_to_remap = &fds_to_remap; |
| 64 | 62 |
| 65 test_child_ = | 63 test_child_ = |
| 66 base::SpawnMultiProcessTestChild(test_child_main, command_line, options); | 64 base::SpawnMultiProcessTestChild(test_child_main, command_line, options); |
| 67 platform_channel_pair_->ChildProcessLaunched(); | 65 platform_channel_pair_->handle1.reset(); |
| 68 | 66 |
| 69 CHECK(test_child_.IsValid()); | 67 CHECK(test_child_.IsValid()); |
| 70 } | 68 } |
| 71 | 69 |
| 72 int MultiprocessTestHelper::WaitForChildShutdown() { | 70 int MultiprocessTestHelper::WaitForChildShutdown() { |
| 73 CHECK(test_child_.IsValid()); | 71 CHECK(test_child_.IsValid()); |
| 74 | 72 |
| 75 int rv = -1; | 73 int rv = -1; |
| 76 CHECK( | 74 CHECK( |
| 77 test_child_.WaitForExitWithTimeout(TestTimeouts::action_timeout(), &rv)); | 75 test_child_.WaitForExitWithTimeout(TestTimeouts::action_timeout(), &rv)); |
| 78 test_child_.Close(); | 76 test_child_.Close(); |
| 79 return rv; | 77 return rv; |
| 80 } | 78 } |
| 81 | 79 |
| 82 bool MultiprocessTestHelper::WaitForChildTestShutdown() { | 80 bool MultiprocessTestHelper::WaitForChildTestShutdown() { |
| 83 return WaitForChildShutdown() == 0; | 81 return WaitForChildShutdown() == 0; |
| 84 } | 82 } |
| 85 | 83 |
| 86 // static | 84 // static |
| 87 void MultiprocessTestHelper::ChildSetup() { | 85 void MultiprocessTestHelper::ChildSetup() { |
| 88 CHECK(base::CommandLine::InitializedForCurrentProcess()); | 86 CHECK(base::CommandLine::InitializedForCurrentProcess()); |
| 89 const base::CommandLine& command_line = | 87 client_platform_handle = ScopedPlatformHandle( |
| 90 *base::CommandLine::ForCurrentProcess(); | 88 PlatformHandle(base::GlobalDescriptors::kBaseDescriptor)); |
| 91 client_platform_handle = | |
| 92 embedder::PlatformChannelPair::PassClientHandleFromParentProcess( | |
| 93 command_line.GetSwitchValueASCII(kPlatformChannelHandleInfoSwitch)); | |
| 94 CHECK(client_platform_handle.is_valid()); | |
| 95 } | 89 } |
| 96 | 90 |
| 97 // static | 91 // static |
| 98 ScopedPlatformHandle MultiprocessTestHelper::client_platform_handle; | 92 ScopedPlatformHandle MultiprocessTestHelper::client_platform_handle; |
| 99 | 93 |
| 100 } // namespace test | 94 } // namespace test |
| 101 } // namespace mojo | 95 } // namespace mojo |
| OLD | NEW |