drivefs_bootstrap_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2018 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. #include "ash/components/drivefs/drivefs_bootstrap.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "ash/components/drivefs/mojom/drivefs.mojom-test-utils.h"
  8. #include "ash/components/drivefs/mojom/drivefs.mojom.h"
  9. #include "base/bind.h"
  10. #include "base/run_loop.h"
  11. #include "base/test/task_environment.h"
  12. #include "chromeos/components/mojo_bootstrap/pending_connection_manager.h"
  13. #include "mojo/public/cpp/bindings/pending_receiver.h"
  14. #include "mojo/public/cpp/bindings/pending_remote.h"
  15. #include "mojo/public/cpp/bindings/receiver.h"
  16. #include "mojo/public/cpp/bindings/remote.h"
  17. #include "testing/gmock/include/gmock/gmock.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. namespace drivefs {
  20. namespace {
  21. using testing::_;
  22. class MockDriveFs : public mojom::DriveFsInterceptorForTesting {
  23. public:
  24. DriveFs* GetForwardingInterface() override {
  25. NOTREACHED();
  26. return nullptr;
  27. }
  28. };
  29. class MockDriveFsDelegate : public mojom::DriveFsDelegateInterceptorForTesting {
  30. public:
  31. DriveFsDelegate* GetForwardingInterface() override {
  32. NOTREACHED();
  33. return nullptr;
  34. }
  35. };
  36. class DriveFsBootstrapListenerForTest : public DriveFsBootstrapListener {
  37. public:
  38. DriveFsBootstrapListenerForTest(
  39. mojo::PendingRemote<mojom::DriveFsBootstrap> available_bootstrap)
  40. : available_bootstrap_(std::move(available_bootstrap)) {}
  41. DriveFsBootstrapListenerForTest(const DriveFsBootstrapListenerForTest&) =
  42. delete;
  43. DriveFsBootstrapListenerForTest& operator=(
  44. const DriveFsBootstrapListenerForTest&) = delete;
  45. mojo::PendingRemote<mojom::DriveFsBootstrap> bootstrap() override {
  46. return std::move(available_bootstrap_);
  47. }
  48. void SendInvitationOverPipe(base::ScopedFD) override {}
  49. private:
  50. mojo::PendingRemote<mojom::DriveFsBootstrap> available_bootstrap_;
  51. };
  52. class DriveFsBootstrapTest : public testing::Test,
  53. public mojom::DriveFsBootstrap {
  54. public:
  55. DriveFsBootstrapTest() = default;
  56. DriveFsBootstrapTest(const DriveFsBootstrapTest&) = delete;
  57. DriveFsBootstrapTest& operator=(const DriveFsBootstrapTest&) = delete;
  58. protected:
  59. MOCK_CONST_METHOD0(OnDisconnect, void());
  60. MOCK_CONST_METHOD0(OnInit, void());
  61. void Init(mojom::DriveFsConfigurationPtr config,
  62. mojo::PendingReceiver<mojom::DriveFs> drive_fs_receiver,
  63. mojo::PendingRemote<mojom::DriveFsDelegate> delegate) override {
  64. receiver_.Bind(std::move(drive_fs_receiver));
  65. mojo::FusePipes(std::move(pending_delegate_receiver_), std::move(delegate));
  66. OnInit();
  67. }
  68. std::unique_ptr<DriveFsBootstrapListener> CreateListener() {
  69. pending_delegate_receiver_ = delegate_.BindNewPipeAndPassReceiver();
  70. return std::make_unique<DriveFsBootstrapListenerForTest>(
  71. bootstrap_receiver_.BindNewPipeAndPassRemote());
  72. }
  73. base::UnguessableToken ListenForConnection() {
  74. connection_ = DriveFsConnection::Create(CreateListener(),
  75. mojom::DriveFsConfiguration::New());
  76. return connection_->Connect(
  77. &mock_delegate_, base::BindOnce(&DriveFsBootstrapTest::OnDisconnect,
  78. base::Unretained(this)));
  79. }
  80. void WaitForConnection(const base::UnguessableToken& token) {
  81. ASSERT_TRUE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
  82. token.ToString(), {}));
  83. base::RunLoop run_loop;
  84. bootstrap_receiver_.set_disconnect_handler(run_loop.QuitClosure());
  85. run_loop.Run();
  86. }
  87. base::test::TaskEnvironment task_environment_;
  88. MockDriveFs mock_drivefs_;
  89. MockDriveFsDelegate mock_delegate_;
  90. mojo::Receiver<mojom::DriveFsBootstrap> bootstrap_receiver_{this};
  91. mojo::Receiver<mojom::DriveFs> receiver_{&mock_drivefs_};
  92. std::unique_ptr<DriveFsConnection> connection_;
  93. mojo::Remote<mojom::DriveFsDelegate> delegate_;
  94. mojo::PendingReceiver<mojom::DriveFsDelegate> pending_delegate_receiver_;
  95. std::string email_;
  96. };
  97. } // namespace
  98. TEST_F(DriveFsBootstrapTest, Listen_Connect_Disconnect) {
  99. auto token = ListenForConnection();
  100. EXPECT_CALL(*this, OnInit());
  101. WaitForConnection(token);
  102. EXPECT_CALL(*this, OnDisconnect());
  103. receiver_.reset();
  104. base::RunLoop().RunUntilIdle();
  105. ASSERT_FALSE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
  106. token.ToString(), {}));
  107. }
  108. TEST_F(DriveFsBootstrapTest, Listen_Connect_DisconnectDelegate) {
  109. auto token = ListenForConnection();
  110. EXPECT_CALL(*this, OnInit());
  111. WaitForConnection(token);
  112. EXPECT_CALL(*this, OnDisconnect());
  113. delegate_.reset();
  114. base::RunLoop().RunUntilIdle();
  115. ASSERT_FALSE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
  116. token.ToString(), {}));
  117. }
  118. TEST_F(DriveFsBootstrapTest, Listen_Connect_Destroy) {
  119. auto token = ListenForConnection();
  120. EXPECT_CALL(*this, OnInit());
  121. WaitForConnection(token);
  122. EXPECT_CALL(*this, OnDisconnect()).Times(0);
  123. connection_.reset();
  124. base::RunLoop().RunUntilIdle();
  125. ASSERT_FALSE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
  126. token.ToString(), {}));
  127. }
  128. TEST_F(DriveFsBootstrapTest, Listen_Destroy) {
  129. EXPECT_CALL(*this, OnDisconnect()).Times(0);
  130. auto token = ListenForConnection();
  131. connection_.reset();
  132. base::RunLoop().RunUntilIdle();
  133. ASSERT_FALSE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
  134. token.ToString(), {}));
  135. }
  136. TEST_F(DriveFsBootstrapTest, Listen_DisconnectDelegate) {
  137. EXPECT_CALL(*this, OnDisconnect()).Times(0);
  138. ListenForConnection();
  139. delegate_.reset();
  140. base::RunLoop().RunUntilIdle();
  141. }
  142. } // namespace drivefs