| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/system/ipc_support.h" | 5 #include "mojo/edk/system/ipc_support.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/test/test_timeouts.h" | 15 #include "base/test/test_timeouts.h" |
| 16 #include "mojo/edk/embedder/master_process_delegate.h" | 16 #include "mojo/edk/embedder/master_process_delegate.h" |
| 17 #include "mojo/edk/embedder/platform_channel_pair.h" | 17 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 18 #include "mojo/edk/embedder/simple_platform_support.h" | 18 #include "mojo/edk/embedder/simple_platform_support.h" |
| 19 #include "mojo/edk/embedder/slave_process_delegate.h" | 19 #include "mojo/edk/embedder/slave_process_delegate.h" |
| 20 #include "mojo/edk/system/channel_manager.h" | 20 #include "mojo/edk/system/channel_manager.h" |
| 21 #include "mojo/edk/system/connection_identifier.h" | 21 #include "mojo/edk/system/connection_identifier.h" |
| 22 #include "mojo/edk/system/dispatcher.h" | 22 #include "mojo/edk/system/dispatcher.h" |
| 23 #include "mojo/edk/system/message_pipe.h" | 23 #include "mojo/edk/system/message_pipe.h" |
| 24 #include "mojo/edk/system/message_pipe_dispatcher.h" | 24 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 25 #include "mojo/edk/system/process_identifier.h" | 25 #include "mojo/edk/system/process_identifier.h" |
| 26 #include "mojo/edk/system/test/test_io_thread.h" |
| 26 #include "mojo/edk/system/test/timeouts.h" | 27 #include "mojo/edk/system/test/timeouts.h" |
| 27 #include "mojo/edk/system/waiter.h" | 28 #include "mojo/edk/system/waiter.h" |
| 28 #include "mojo/edk/test/multiprocess_test_helper.h" | 29 #include "mojo/edk/test/multiprocess_test_helper.h" |
| 29 #include "mojo/edk/test/test_io_thread.h" | |
| 30 #include "mojo/edk/test/test_utils.h" | 30 #include "mojo/edk/test/test_utils.h" |
| 31 #include "mojo/public/cpp/system/macros.h" | 31 #include "mojo/public/cpp/system/macros.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 33 |
| 34 namespace mojo { | 34 namespace mojo { |
| 35 | |
| 36 using test::TestIOThread; | |
| 37 | |
| 38 namespace system { | 35 namespace system { |
| 39 namespace { | 36 namespace { |
| 40 | 37 |
| 41 const char kConnectionIdFlag[] = "test-connection-id"; | 38 const char kConnectionIdFlag[] = "test-connection-id"; |
| 42 | 39 |
| 43 // Tests writing a message (containing just data) to |write_mp| and then reading | 40 // Tests writing a message (containing just data) to |write_mp| and then reading |
| 44 // it from |read_mp| (it should be the next message, i.e., there should be no | 41 // it from |read_mp| (it should be the next message, i.e., there should be no |
| 45 // other messages already enqueued in that direction). | 42 // other messages already enqueued in that direction). |
| 46 void TestWriteReadMessage(MessagePipeDispatcher* write_mp, | 43 void TestWriteReadMessage(MessagePipeDispatcher* write_mp, |
| 47 MessagePipeDispatcher* read_mp) { | 44 MessagePipeDispatcher* read_mp) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 void OnShutdownComplete() override { NOTREACHED(); } | 151 void OnShutdownComplete() override { NOTREACHED(); } |
| 155 | 152 |
| 156 void OnMasterDisconnect() override { NOTREACHED(); } | 153 void OnMasterDisconnect() override { NOTREACHED(); } |
| 157 | 154 |
| 158 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveProcessDelegate); | 155 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveProcessDelegate); |
| 159 }; | 156 }; |
| 160 | 157 |
| 161 // Represents the master's side of its connection to a slave. | 158 // Represents the master's side of its connection to a slave. |
| 162 class TestSlaveConnection { | 159 class TestSlaveConnection { |
| 163 public: | 160 public: |
| 164 TestSlaveConnection(TestIOThread* test_io_thread, | 161 TestSlaveConnection(test::TestIOThread* test_io_thread, |
| 165 IPCSupport* master_ipc_support) | 162 IPCSupport* master_ipc_support) |
| 166 : test_io_thread_(test_io_thread), | 163 : test_io_thread_(test_io_thread), |
| 167 master_ipc_support_(master_ipc_support), | 164 master_ipc_support_(master_ipc_support), |
| 168 connection_id_(master_ipc_support_->GenerateConnectionIdentifier()), | 165 connection_id_(master_ipc_support_->GenerateConnectionIdentifier()), |
| 169 slave_id_(kInvalidProcessIdentifier), | 166 slave_id_(kInvalidProcessIdentifier), |
| 170 event_(true, false) {} | 167 event_(true, false) {} |
| 171 ~TestSlaveConnection() {} | 168 ~TestSlaveConnection() {} |
| 172 | 169 |
| 173 // After this is called, |ShutdownChannelToSlave()| must be called (possibly | 170 // After this is called, |ShutdownChannelToSlave()| must be called (possibly |
| 174 // after |WaitForChannelToSlave()|) before destruction. | 171 // after |WaitForChannelToSlave()|) before destruction. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 200 slave_id_)); | 197 slave_id_)); |
| 201 } | 198 } |
| 202 | 199 |
| 203 embedder::ScopedPlatformHandle PassSlavePlatformHandle() { | 200 embedder::ScopedPlatformHandle PassSlavePlatformHandle() { |
| 204 return slave_platform_handle_.Pass(); | 201 return slave_platform_handle_.Pass(); |
| 205 } | 202 } |
| 206 | 203 |
| 207 const ConnectionIdentifier& connection_id() const { return connection_id_; } | 204 const ConnectionIdentifier& connection_id() const { return connection_id_; } |
| 208 | 205 |
| 209 private: | 206 private: |
| 210 TestIOThread* const test_io_thread_; | 207 test::TestIOThread* const test_io_thread_; |
| 211 IPCSupport* const master_ipc_support_; | 208 IPCSupport* const master_ipc_support_; |
| 212 const ConnectionIdentifier connection_id_; | 209 const ConnectionIdentifier connection_id_; |
| 213 // The master's message pipe dispatcher. | 210 // The master's message pipe dispatcher. |
| 214 RefPtr<MessagePipeDispatcher> message_pipe_; | 211 RefPtr<MessagePipeDispatcher> message_pipe_; |
| 215 ProcessIdentifier slave_id_; | 212 ProcessIdentifier slave_id_; |
| 216 base::WaitableEvent event_; | 213 base::WaitableEvent event_; |
| 217 embedder::ScopedPlatformHandle slave_platform_handle_; | 214 embedder::ScopedPlatformHandle slave_platform_handle_; |
| 218 | 215 |
| 219 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveConnection); | 216 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveConnection); |
| 220 }; | 217 }; |
| 221 | 218 |
| 222 // Encapsulates the state of a slave. (Note, however, that we share a | 219 // Encapsulates the state of a slave. (Note, however, that we share a |
| 223 // |PlatformSupport| and an I/O thread.) | 220 // |PlatformSupport| and an I/O thread.) |
| 224 class TestSlave { | 221 class TestSlave { |
| 225 public: | 222 public: |
| 226 // Note: Before destruction, |ShutdownIPCSupport()| must be called. | 223 // Note: Before destruction, |ShutdownIPCSupport()| must be called. |
| 227 TestSlave(embedder::PlatformSupport* platform_support, | 224 TestSlave(embedder::PlatformSupport* platform_support, |
| 228 TestIOThread* test_io_thread, | 225 test::TestIOThread* test_io_thread, |
| 229 embedder::ScopedPlatformHandle platform_handle) | 226 embedder::ScopedPlatformHandle platform_handle) |
| 230 : test_io_thread_(test_io_thread), | 227 : test_io_thread_(test_io_thread), |
| 231 slave_ipc_support_(platform_support, | 228 slave_ipc_support_(platform_support, |
| 232 embedder::ProcessType::SLAVE, | 229 embedder::ProcessType::SLAVE, |
| 233 test_io_thread->task_runner(), | 230 test_io_thread->task_runner(), |
| 234 &slave_process_delegate_, | 231 &slave_process_delegate_, |
| 235 test_io_thread->task_runner(), | 232 test_io_thread->task_runner(), |
| 236 platform_handle.Pass()), | 233 platform_handle.Pass()), |
| 237 event_(true, false) {} | 234 event_(true, false) {} |
| 238 ~TestSlave() {} | 235 ~TestSlave() {} |
| (...skipping 27 matching lines...) Expand all Loading... |
| 266 } | 263 } |
| 267 | 264 |
| 268 // No other methods may be called after this. | 265 // No other methods may be called after this. |
| 269 void ShutdownIPCSupport() { | 266 void ShutdownIPCSupport() { |
| 270 test_io_thread_->PostTaskAndWait( | 267 test_io_thread_->PostTaskAndWait( |
| 271 base::Bind(&IPCSupport::ShutdownOnIOThread, | 268 base::Bind(&IPCSupport::ShutdownOnIOThread, |
| 272 base::Unretained(&slave_ipc_support_))); | 269 base::Unretained(&slave_ipc_support_))); |
| 273 } | 270 } |
| 274 | 271 |
| 275 private: | 272 private: |
| 276 TestIOThread* const test_io_thread_; | 273 test::TestIOThread* const test_io_thread_; |
| 277 TestSlaveProcessDelegate slave_process_delegate_; | 274 TestSlaveProcessDelegate slave_process_delegate_; |
| 278 IPCSupport slave_ipc_support_; | 275 IPCSupport slave_ipc_support_; |
| 279 base::WaitableEvent event_; | 276 base::WaitableEvent event_; |
| 280 | 277 |
| 281 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlave); | 278 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlave); |
| 282 }; | 279 }; |
| 283 | 280 |
| 284 // Encapsulates both the master and slave sides for each slave. | 281 // Encapsulates both the master and slave sides for each slave. |
| 285 class TestSlaveSetup { | 282 class TestSlaveSetup { |
| 286 public: | 283 public: |
| 287 TestSlaveSetup(embedder::SimplePlatformSupport* platform_support, | 284 TestSlaveSetup(embedder::SimplePlatformSupport* platform_support, |
| 288 TestIOThread* test_io_thread, | 285 test::TestIOThread* test_io_thread, |
| 289 TestMasterProcessDelegate* master_process_delegate, | 286 TestMasterProcessDelegate* master_process_delegate, |
| 290 IPCSupport* master_ipc_support) | 287 IPCSupport* master_ipc_support) |
| 291 : platform_support_(platform_support), | 288 : platform_support_(platform_support), |
| 292 test_io_thread_(test_io_thread), | 289 test_io_thread_(test_io_thread), |
| 293 master_process_delegate_(master_process_delegate), | 290 master_process_delegate_(master_process_delegate), |
| 294 master_ipc_support_(master_ipc_support) {} | 291 master_ipc_support_(master_ipc_support) {} |
| 295 ~TestSlaveSetup() { | 292 ~TestSlaveSetup() { |
| 296 CHECK(!slave_connection_); | 293 CHECK(!slave_connection_); |
| 297 CHECK(!slave_); | 294 CHECK(!slave_); |
| 298 } | 295 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 TestSlaveConnection* slave_connection() { return slave_connection_.get(); } | 341 TestSlaveConnection* slave_connection() { return slave_connection_.get(); } |
| 345 // Note: To close the master message pipe, use |PassMasterMessagePipe()|. | 342 // Note: To close the master message pipe, use |PassMasterMessagePipe()|. |
| 346 MessagePipeDispatcher* master_mp() { return master_mp_.get(); } | 343 MessagePipeDispatcher* master_mp() { return master_mp_.get(); } |
| 347 | 344 |
| 348 TestSlave* slave() { return slave_.get(); } | 345 TestSlave* slave() { return slave_.get(); } |
| 349 // Note: To close the slave message pipe, use |PassSlaveMessagePipe()|. | 346 // Note: To close the slave message pipe, use |PassSlaveMessagePipe()|. |
| 350 MessagePipeDispatcher* slave_mp() { return slave_mp_.get(); } | 347 MessagePipeDispatcher* slave_mp() { return slave_mp_.get(); } |
| 351 | 348 |
| 352 private: | 349 private: |
| 353 embedder::SimplePlatformSupport* const platform_support_; | 350 embedder::SimplePlatformSupport* const platform_support_; |
| 354 TestIOThread* const test_io_thread_; | 351 test::TestIOThread* const test_io_thread_; |
| 355 TestMasterProcessDelegate* const master_process_delegate_; | 352 TestMasterProcessDelegate* const master_process_delegate_; |
| 356 IPCSupport* const master_ipc_support_; | 353 IPCSupport* const master_ipc_support_; |
| 357 | 354 |
| 358 std::unique_ptr<TestSlaveConnection> slave_connection_; | 355 std::unique_ptr<TestSlaveConnection> slave_connection_; |
| 359 RefPtr<MessagePipeDispatcher> master_mp_; | 356 RefPtr<MessagePipeDispatcher> master_mp_; |
| 360 | 357 |
| 361 std::unique_ptr<TestSlave> slave_; | 358 std::unique_ptr<TestSlave> slave_; |
| 362 RefPtr<MessagePipeDispatcher> slave_mp_; | 359 RefPtr<MessagePipeDispatcher> slave_mp_; |
| 363 | 360 |
| 364 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveSetup); | 361 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveSetup); |
| 365 }; | 362 }; |
| 366 | 363 |
| 367 class IPCSupportTest : public testing::Test { | 364 class IPCSupportTest : public testing::Test { |
| 368 public: | 365 public: |
| 369 // Note: Run master process delegate methods on the I/O thread. | 366 // Note: Run master process delegate methods on the I/O thread. |
| 370 IPCSupportTest() | 367 IPCSupportTest() |
| 371 : test_io_thread_(TestIOThread::StartMode::AUTO), | 368 : test_io_thread_(test::TestIOThread::StartMode::AUTO), |
| 372 master_ipc_support_(&platform_support_, | 369 master_ipc_support_(&platform_support_, |
| 373 embedder::ProcessType::MASTER, | 370 embedder::ProcessType::MASTER, |
| 374 test_io_thread_.task_runner(), | 371 test_io_thread_.task_runner(), |
| 375 &master_process_delegate_, | 372 &master_process_delegate_, |
| 376 test_io_thread_.task_runner(), | 373 test_io_thread_.task_runner(), |
| 377 embedder::ScopedPlatformHandle()) {} | 374 embedder::ScopedPlatformHandle()) {} |
| 378 ~IPCSupportTest() override {} | 375 ~IPCSupportTest() override {} |
| 379 | 376 |
| 380 std::unique_ptr<TestSlaveSetup> SetupSlave() { | 377 std::unique_ptr<TestSlaveSetup> SetupSlave() { |
| 381 std::unique_ptr<TestSlaveSetup> s( | 378 std::unique_ptr<TestSlaveSetup> s( |
| 382 new TestSlaveSetup(&platform_support_, &test_io_thread_, | 379 new TestSlaveSetup(&platform_support_, &test_io_thread_, |
| 383 &master_process_delegate_, &master_ipc_support_)); | 380 &master_process_delegate_, &master_ipc_support_)); |
| 384 s->Init(); | 381 s->Init(); |
| 385 return s; | 382 return s; |
| 386 } | 383 } |
| 387 | 384 |
| 388 void ShutdownMasterIPCSupport() { | 385 void ShutdownMasterIPCSupport() { |
| 389 test_io_thread_.PostTaskAndWait( | 386 test_io_thread_.PostTaskAndWait( |
| 390 base::Bind(&IPCSupport::ShutdownOnIOThread, | 387 base::Bind(&IPCSupport::ShutdownOnIOThread, |
| 391 base::Unretained(&master_ipc_support_))); | 388 base::Unretained(&master_ipc_support_))); |
| 392 } | 389 } |
| 393 | 390 |
| 394 embedder::SimplePlatformSupport& platform_support() { | 391 embedder::SimplePlatformSupport& platform_support() { |
| 395 return platform_support_; | 392 return platform_support_; |
| 396 } | 393 } |
| 397 TestIOThread& test_io_thread() { return test_io_thread_; } | 394 test::TestIOThread& test_io_thread() { return test_io_thread_; } |
| 398 TestMasterProcessDelegate& master_process_delegate() { | 395 TestMasterProcessDelegate& master_process_delegate() { |
| 399 return master_process_delegate_; | 396 return master_process_delegate_; |
| 400 } | 397 } |
| 401 IPCSupport& master_ipc_support() { return master_ipc_support_; } | 398 IPCSupport& master_ipc_support() { return master_ipc_support_; } |
| 402 | 399 |
| 403 private: | 400 private: |
| 404 embedder::SimplePlatformSupport platform_support_; | 401 embedder::SimplePlatformSupport platform_support_; |
| 405 TestIOThread test_io_thread_; | 402 test::TestIOThread test_io_thread_; |
| 406 | 403 |
| 407 // All tests require a master. | 404 // All tests require a master. |
| 408 TestMasterProcessDelegate master_process_delegate_; | 405 TestMasterProcessDelegate master_process_delegate_; |
| 409 IPCSupport master_ipc_support_; | 406 IPCSupport master_ipc_support_; |
| 410 | 407 |
| 411 MOJO_DISALLOW_COPY_AND_ASSIGN(IPCSupportTest); | 408 MOJO_DISALLOW_COPY_AND_ASSIGN(IPCSupportTest); |
| 412 }; | 409 }; |
| 413 | 410 |
| 414 using MessagePipeDispatcherPair = | 411 using MessagePipeDispatcherPair = |
| 415 std::pair<RefPtr<MessagePipeDispatcher>, RefPtr<MessagePipeDispatcher>>; | 412 std::pair<RefPtr<MessagePipeDispatcher>, RefPtr<MessagePipeDispatcher>>; |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 | 665 |
| 669 ShutdownMasterIPCSupport(); | 666 ShutdownMasterIPCSupport(); |
| 670 } | 667 } |
| 671 | 668 |
| 672 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlaveInternal) { | 669 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlaveInternal) { |
| 673 embedder::ScopedPlatformHandle client_platform_handle = | 670 embedder::ScopedPlatformHandle client_platform_handle = |
| 674 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | 671 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); |
| 675 ASSERT_TRUE(client_platform_handle.is_valid()); | 672 ASSERT_TRUE(client_platform_handle.is_valid()); |
| 676 | 673 |
| 677 embedder::SimplePlatformSupport platform_support; | 674 embedder::SimplePlatformSupport platform_support; |
| 678 TestIOThread test_io_thread(TestIOThread::StartMode::AUTO); | 675 test::TestIOThread test_io_thread(test::TestIOThread::StartMode::AUTO); |
| 679 TestSlaveProcessDelegate slave_process_delegate; | 676 TestSlaveProcessDelegate slave_process_delegate; |
| 680 // Note: Run process delegate methods on the I/O thread. | 677 // Note: Run process delegate methods on the I/O thread. |
| 681 IPCSupport ipc_support(&platform_support, embedder::ProcessType::SLAVE, | 678 IPCSupport ipc_support(&platform_support, embedder::ProcessType::SLAVE, |
| 682 test_io_thread.task_runner(), &slave_process_delegate, | 679 test_io_thread.task_runner(), &slave_process_delegate, |
| 683 test_io_thread.task_runner(), | 680 test_io_thread.task_runner(), |
| 684 client_platform_handle.Pass()); | 681 client_platform_handle.Pass()); |
| 685 | 682 |
| 686 const base::CommandLine& command_line = | 683 const base::CommandLine& command_line = |
| 687 *base::CommandLine::ForCurrentProcess(); | 684 *base::CommandLine::ForCurrentProcess(); |
| 688 ASSERT_TRUE(command_line.HasSwitch(kConnectionIdFlag)); | 685 ASSERT_TRUE(command_line.HasSwitch(kConnectionIdFlag)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 710 | 707 |
| 711 test_io_thread.PostTaskAndWait(base::Bind(&IPCSupport::ShutdownOnIOThread, | 708 test_io_thread.PostTaskAndWait(base::Bind(&IPCSupport::ShutdownOnIOThread, |
| 712 base::Unretained(&ipc_support))); | 709 base::Unretained(&ipc_support))); |
| 713 } | 710 } |
| 714 | 711 |
| 715 // TODO(vtl): Also test the case of the master "dying" before the slave. (The | 712 // TODO(vtl): Also test the case of the master "dying" before the slave. (The |
| 716 // slave should get OnMasterDisconnect(), which we currently don't test.) | 713 // slave should get OnMasterDisconnect(), which we currently don't test.) |
| 717 | 714 |
| 718 } // namespace system | 715 } // namespace system |
| 719 } // namespace mojo | 716 } // namespace mojo |
| OLD | NEW |