| 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 <utility> | 8 #include <utility> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/test/test_timeouts.h" | 16 #include "base/test/test_timeouts.h" |
| 17 #include "mojo/edk/embedder/master_process_delegate.h" | 17 #include "mojo/edk/embedder/master_process_delegate.h" |
| 18 #include "mojo/edk/embedder/platform_channel_pair.h" | 18 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 19 #include "mojo/edk/embedder/simple_platform_support.h" | 19 #include "mojo/edk/embedder/simple_platform_support.h" |
| 20 #include "mojo/edk/embedder/slave_process_delegate.h" | 20 #include "mojo/edk/embedder/slave_process_delegate.h" |
| 21 #include "mojo/edk/system/channel_manager.h" | 21 #include "mojo/edk/system/channel_manager.h" |
| 22 #include "mojo/edk/system/connection_identifier.h" | 22 #include "mojo/edk/system/connection_identifier.h" |
| 23 #include "mojo/edk/system/dispatcher.h" | 23 #include "mojo/edk/system/dispatcher.h" |
| 24 #include "mojo/edk/system/message_pipe.h" | 24 #include "mojo/edk/system/message_pipe.h" |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 TestSlave* slave() { return slave_.get(); } | 352 TestSlave* slave() { return slave_.get(); } |
| 353 // Note: To close the slave message pipe, use |PassSlaveMessagePipe()|. | 353 // Note: To close the slave message pipe, use |PassSlaveMessagePipe()|. |
| 354 MessagePipeDispatcher* slave_mp() { return slave_mp_.get(); } | 354 MessagePipeDispatcher* slave_mp() { return slave_mp_.get(); } |
| 355 | 355 |
| 356 private: | 356 private: |
| 357 embedder::SimplePlatformSupport* const platform_support_; | 357 embedder::SimplePlatformSupport* const platform_support_; |
| 358 TestIOThread* const test_io_thread_; | 358 TestIOThread* const test_io_thread_; |
| 359 TestMasterProcessDelegate* const master_process_delegate_; | 359 TestMasterProcessDelegate* const master_process_delegate_; |
| 360 IPCSupport* const master_ipc_support_; | 360 IPCSupport* const master_ipc_support_; |
| 361 | 361 |
| 362 scoped_ptr<TestSlaveConnection> slave_connection_; | 362 std::unique_ptr<TestSlaveConnection> slave_connection_; |
| 363 scoped_refptr<MessagePipeDispatcher> master_mp_; | 363 scoped_refptr<MessagePipeDispatcher> master_mp_; |
| 364 | 364 |
| 365 scoped_ptr<TestSlave> slave_; | 365 std::unique_ptr<TestSlave> slave_; |
| 366 scoped_refptr<MessagePipeDispatcher> slave_mp_; | 366 scoped_refptr<MessagePipeDispatcher> slave_mp_; |
| 367 | 367 |
| 368 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveSetup); | 368 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveSetup); |
| 369 }; | 369 }; |
| 370 | 370 |
| 371 class IPCSupportTest : public testing::Test { | 371 class IPCSupportTest : public testing::Test { |
| 372 public: | 372 public: |
| 373 // Note: Run master process delegate methods on the I/O thread. | 373 // Note: Run master process delegate methods on the I/O thread. |
| 374 IPCSupportTest() | 374 IPCSupportTest() |
| 375 : test_io_thread_(TestIOThread::kAutoStart), | 375 : test_io_thread_(TestIOThread::kAutoStart), |
| 376 master_ipc_support_(&platform_support_, | 376 master_ipc_support_(&platform_support_, |
| 377 embedder::ProcessType::MASTER, | 377 embedder::ProcessType::MASTER, |
| 378 test_io_thread_.task_runner(), | 378 test_io_thread_.task_runner(), |
| 379 &master_process_delegate_, | 379 &master_process_delegate_, |
| 380 test_io_thread_.task_runner(), | 380 test_io_thread_.task_runner(), |
| 381 embedder::ScopedPlatformHandle()) {} | 381 embedder::ScopedPlatformHandle()) {} |
| 382 ~IPCSupportTest() override {} | 382 ~IPCSupportTest() override {} |
| 383 | 383 |
| 384 scoped_ptr<TestSlaveSetup> SetupSlave() { | 384 std::unique_ptr<TestSlaveSetup> SetupSlave() { |
| 385 scoped_ptr<TestSlaveSetup> s( | 385 std::unique_ptr<TestSlaveSetup> s( |
| 386 new TestSlaveSetup(&platform_support_, &test_io_thread_, | 386 new TestSlaveSetup(&platform_support_, &test_io_thread_, |
| 387 &master_process_delegate_, &master_ipc_support_)); | 387 &master_process_delegate_, &master_ipc_support_)); |
| 388 s->Init(); | 388 s->Init(); |
| 389 return s; | 389 return s; |
| 390 } | 390 } |
| 391 | 391 |
| 392 void ShutdownMasterIPCSupport() { | 392 void ShutdownMasterIPCSupport() { |
| 393 test_io_thread_.PostTaskAndWait( | 393 test_io_thread_.PostTaskAndWait( |
| 394 FROM_HERE, base::Bind(&IPCSupport::ShutdownOnIOThread, | 394 FROM_HERE, base::Bind(&IPCSupport::ShutdownOnIOThread, |
| 395 base::Unretained(&master_ipc_support_))); | 395 base::Unretained(&master_ipc_support_))); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 424 MessagePipeDispatcher::kDefaultCreateOptions); | 424 MessagePipeDispatcher::kDefaultCreateOptions); |
| 425 rv.second = MessagePipeDispatcher::Create( | 425 rv.second = MessagePipeDispatcher::Create( |
| 426 MessagePipeDispatcher::kDefaultCreateOptions); | 426 MessagePipeDispatcher::kDefaultCreateOptions); |
| 427 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); | 427 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); |
| 428 rv.first->Init(mp, 0); | 428 rv.first->Init(mp, 0); |
| 429 rv.second->Init(mp, 1); | 429 rv.second->Init(mp, 1); |
| 430 return rv; | 430 return rv; |
| 431 } | 431 } |
| 432 | 432 |
| 433 TEST_F(IPCSupportTest, MasterSlave) { | 433 TEST_F(IPCSupportTest, MasterSlave) { |
| 434 scoped_ptr<TestSlaveSetup> s(SetupSlave()); | 434 std::unique_ptr<TestSlaveSetup> s(SetupSlave()); |
| 435 | 435 |
| 436 s->TestConnection(); | 436 s->TestConnection(); |
| 437 | 437 |
| 438 // Don't need the message pipe anymore. | 438 // Don't need the message pipe anymore. |
| 439 s->PassMasterMessagePipe()->Close(); | 439 s->PassMasterMessagePipe()->Close(); |
| 440 s->PassSlaveMessagePipe()->Close(); | 440 s->PassSlaveMessagePipe()->Close(); |
| 441 | 441 |
| 442 // A message was sent through the message pipe, |Channel|s must have been | 442 // A message was sent through the message pipe, |Channel|s must have been |
| 443 // established on both sides. The events have thus almost certainly been | 443 // established on both sides. The events have thus almost certainly been |
| 444 // signalled, but we'll wait just to be sure. | 444 // signalled, but we'll wait just to be sure. |
| 445 s->slave_connection()->WaitForChannelToSlave(); | 445 s->slave_connection()->WaitForChannelToSlave(); |
| 446 s->slave()->WaitForChannelToMaster(); | 446 s->slave()->WaitForChannelToMaster(); |
| 447 | 447 |
| 448 s->Shutdown(); | 448 s->Shutdown(); |
| 449 | 449 |
| 450 ShutdownMasterIPCSupport(); | 450 ShutdownMasterIPCSupport(); |
| 451 } | 451 } |
| 452 | 452 |
| 453 // Simulates a master and two slaves. Initially, there are just message pipes | 453 // Simulates a master and two slaves. Initially, there are just message pipes |
| 454 // from the master to the slaves. This tests the master creating a message pipe | 454 // from the master to the slaves. This tests the master creating a message pipe |
| 455 // and sending an end to each slave, which should result in a direct connection | 455 // and sending an end to each slave, which should result in a direct connection |
| 456 // between the two slaves (TODO(vtl): this part doesn't happen yet). | 456 // between the two slaves (TODO(vtl): this part doesn't happen yet). |
| 457 // TODO(vtl): There are various other similar scenarios we'll need to test, so | 457 // TODO(vtl): There are various other similar scenarios we'll need to test, so |
| 458 // we'll need to factor out some of the code. | 458 // we'll need to factor out some of the code. |
| 459 // TODO(vtl): In this scenario, we can't test the intermediary (the master) | 459 // TODO(vtl): In this scenario, we can't test the intermediary (the master) |
| 460 // going away. | 460 // going away. |
| 461 TEST_F(IPCSupportTest, ConnectTwoSlaves) { | 461 TEST_F(IPCSupportTest, ConnectTwoSlaves) { |
| 462 scoped_ptr<TestSlaveSetup> s1(SetupSlave()); | 462 std::unique_ptr<TestSlaveSetup> s1(SetupSlave()); |
| 463 scoped_ptr<TestSlaveSetup> s2(SetupSlave()); | 463 std::unique_ptr<TestSlaveSetup> s2(SetupSlave()); |
| 464 s1->TestConnection(); | 464 s1->TestConnection(); |
| 465 s2->TestConnection(); | 465 s2->TestConnection(); |
| 466 | 466 |
| 467 // Make a message pipe (logically "in" the master) and send one end to each | 467 // Make a message pipe (logically "in" the master) and send one end to each |
| 468 // slave. | 468 // slave. |
| 469 MessagePipeDispatcherPair send_mp = CreateMessagePipe(); | 469 MessagePipeDispatcherPair send_mp = CreateMessagePipe(); |
| 470 scoped_refptr<MessagePipeDispatcher> slave1_received_mp = | 470 scoped_refptr<MessagePipeDispatcher> slave1_received_mp = |
| 471 SendMessagePipeDispatcher(s1->master_mp(), s1->slave_mp(), send_mp.first); | 471 SendMessagePipeDispatcher(s1->master_mp(), s1->slave_mp(), send_mp.first); |
| 472 scoped_refptr<MessagePipeDispatcher> slave2_received_mp = | 472 scoped_refptr<MessagePipeDispatcher> slave2_received_mp = |
| 473 SendMessagePipeDispatcher(s2->master_mp(), s2->slave_mp(), | 473 SendMessagePipeDispatcher(s2->master_mp(), s2->slave_mp(), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 490 slave2_received_mp->Close(); | 490 slave2_received_mp->Close(); |
| 491 | 491 |
| 492 s1->Shutdown(); | 492 s1->Shutdown(); |
| 493 s2->Shutdown(); | 493 s2->Shutdown(); |
| 494 | 494 |
| 495 ShutdownMasterIPCSupport(); | 495 ShutdownMasterIPCSupport(); |
| 496 } | 496 } |
| 497 | 497 |
| 498 // Like |ConnectTwoSlaves|, but does it twice, to test reusing a connection. | 498 // Like |ConnectTwoSlaves|, but does it twice, to test reusing a connection. |
| 499 TEST_F(IPCSupportTest, ConnectTwoSlavesTwice) { | 499 TEST_F(IPCSupportTest, ConnectTwoSlavesTwice) { |
| 500 scoped_ptr<TestSlaveSetup> s1(SetupSlave()); | 500 std::unique_ptr<TestSlaveSetup> s1(SetupSlave()); |
| 501 scoped_ptr<TestSlaveSetup> s2(SetupSlave()); | 501 std::unique_ptr<TestSlaveSetup> s2(SetupSlave()); |
| 502 s1->TestConnection(); | 502 s1->TestConnection(); |
| 503 s2->TestConnection(); | 503 s2->TestConnection(); |
| 504 | 504 |
| 505 MessagePipeDispatcherPair send_mp1 = CreateMessagePipe(); | 505 MessagePipeDispatcherPair send_mp1 = CreateMessagePipe(); |
| 506 scoped_refptr<MessagePipeDispatcher> slave1_received_mp1 = | 506 scoped_refptr<MessagePipeDispatcher> slave1_received_mp1 = |
| 507 SendMessagePipeDispatcher(s1->master_mp(), s1->slave_mp(), | 507 SendMessagePipeDispatcher(s1->master_mp(), s1->slave_mp(), |
| 508 send_mp1.first); | 508 send_mp1.first); |
| 509 scoped_refptr<MessagePipeDispatcher> slave2_received_mp1 = | 509 scoped_refptr<MessagePipeDispatcher> slave2_received_mp1 = |
| 510 SendMessagePipeDispatcher(s2->master_mp(), s2->slave_mp(), | 510 SendMessagePipeDispatcher(s2->master_mp(), s2->slave_mp(), |
| 511 send_mp1.second); | 511 send_mp1.second); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 540 | 540 |
| 541 s1->Shutdown(); | 541 s1->Shutdown(); |
| 542 s2->Shutdown(); | 542 s2->Shutdown(); |
| 543 | 543 |
| 544 ShutdownMasterIPCSupport(); | 544 ShutdownMasterIPCSupport(); |
| 545 } | 545 } |
| 546 | 546 |
| 547 // Creates a message pipe in the slave, which sends both ends (in separate | 547 // Creates a message pipe in the slave, which sends both ends (in separate |
| 548 // messages) to the master. | 548 // messages) to the master. |
| 549 TEST_F(IPCSupportTest, SlavePassBackToMaster) { | 549 TEST_F(IPCSupportTest, SlavePassBackToMaster) { |
| 550 scoped_ptr<TestSlaveSetup> s(SetupSlave()); | 550 std::unique_ptr<TestSlaveSetup> s(SetupSlave()); |
| 551 | 551 |
| 552 s->TestConnection(); | 552 s->TestConnection(); |
| 553 | 553 |
| 554 // Make a message pipe (logically "in" the slave) and send both ends | 554 // Make a message pipe (logically "in" the slave) and send both ends |
| 555 // (separately) to the master. | 555 // (separately) to the master. |
| 556 MessagePipeDispatcherPair send_mp = CreateMessagePipe(); | 556 MessagePipeDispatcherPair send_mp = CreateMessagePipe(); |
| 557 scoped_refptr<MessagePipeDispatcher> received_mp1 = | 557 scoped_refptr<MessagePipeDispatcher> received_mp1 = |
| 558 SendMessagePipeDispatcher(s->slave_mp(), s->master_mp(), send_mp.first); | 558 SendMessagePipeDispatcher(s->slave_mp(), s->master_mp(), send_mp.first); |
| 559 | 559 |
| 560 TestWriteReadMessage(received_mp1, send_mp.second); | 560 TestWriteReadMessage(received_mp1, send_mp.second); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 test_io_thread.PostTaskAndWait(FROM_HERE, | 722 test_io_thread.PostTaskAndWait(FROM_HERE, |
| 723 base::Bind(&IPCSupport::ShutdownOnIOThread, | 723 base::Bind(&IPCSupport::ShutdownOnIOThread, |
| 724 base::Unretained(&ipc_support))); | 724 base::Unretained(&ipc_support))); |
| 725 } | 725 } |
| 726 | 726 |
| 727 // TODO(vtl): Also test the case of the master "dying" before the slave. (The | 727 // TODO(vtl): Also test the case of the master "dying" before the slave. (The |
| 728 // slave should get OnMasterDisconnect(), which we currently don't test.) | 728 // slave should get OnMasterDisconnect(), which we currently don't test.) |
| 729 | 729 |
| 730 } // namespace system | 730 } // namespace system |
| 731 } // namespace mojo | 731 } // namespace mojo |
| OLD | NEW |