File: drivefs_bootstrap_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (164 lines) | stat: -rw-r--r-- 5,673 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/drivefs/drivefs_bootstrap.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom-test-utils.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h"
#include "chromeos/components/mojo_bootstrap/pending_connection_manager.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drivefs {
namespace {

using testing::_;

class MockDriveFs : public mojom::DriveFsInterceptorForTesting {
 public:
  DriveFs* GetForwardingInterface() override { NOTREACHED(); }
};

class MockDriveFsDelegate : public mojom::DriveFsDelegateInterceptorForTesting {
 public:
  DriveFsDelegate* GetForwardingInterface() override { NOTREACHED(); }
};

class DriveFsBootstrapListenerForTest : public DriveFsBootstrapListener {
 public:
  DriveFsBootstrapListenerForTest(
      mojo::PendingRemote<mojom::DriveFsBootstrap> available_bootstrap)
      : available_bootstrap_(std::move(available_bootstrap)) {}

  DriveFsBootstrapListenerForTest(const DriveFsBootstrapListenerForTest&) =
      delete;
  DriveFsBootstrapListenerForTest& operator=(
      const DriveFsBootstrapListenerForTest&) = delete;

  mojo::PendingRemote<mojom::DriveFsBootstrap> bootstrap() override {
    return std::move(available_bootstrap_);
  }

  void SendInvitationOverPipe(base::ScopedFD) override {}

 private:
  mojo::PendingRemote<mojom::DriveFsBootstrap> available_bootstrap_;
};

class DriveFsBootstrapTest : public testing::Test,
                             public mojom::DriveFsBootstrap {
 public:
  DriveFsBootstrapTest() = default;

  DriveFsBootstrapTest(const DriveFsBootstrapTest&) = delete;
  DriveFsBootstrapTest& operator=(const DriveFsBootstrapTest&) = delete;

 protected:
  MOCK_CONST_METHOD0(OnDisconnect, void());
  MOCK_CONST_METHOD0(OnInit, void());

  void Init(mojom::DriveFsConfigurationPtr config,
            mojo::PendingReceiver<mojom::DriveFs> drive_fs_receiver,
            mojo::PendingRemote<mojom::DriveFsDelegate> delegate) override {
    receiver_.Bind(std::move(drive_fs_receiver));
    mojo::FusePipes(std::move(pending_delegate_receiver_), std::move(delegate));
    OnInit();
  }

  std::unique_ptr<DriveFsBootstrapListener> CreateListener() {
    pending_delegate_receiver_ = delegate_.BindNewPipeAndPassReceiver();
    return std::make_unique<DriveFsBootstrapListenerForTest>(
        bootstrap_receiver_.BindNewPipeAndPassRemote());
  }

  base::UnguessableToken ListenForConnection() {
    connection_ = DriveFsConnection::Create(CreateListener(),
                                            mojom::DriveFsConfiguration::New());
    return connection_->Connect(
        &mock_delegate_, base::BindOnce(&DriveFsBootstrapTest::OnDisconnect,
                                        base::Unretained(this)));
  }

  void WaitForConnection(const base::UnguessableToken& token) {
    ASSERT_TRUE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
        token.ToString(), {}));
    base::RunLoop run_loop;
    bootstrap_receiver_.set_disconnect_handler(run_loop.QuitClosure());
    run_loop.Run();
  }

  base::test::TaskEnvironment task_environment_;
  MockDriveFs mock_drivefs_;
  MockDriveFsDelegate mock_delegate_;

  mojo::Receiver<mojom::DriveFsBootstrap> bootstrap_receiver_{this};
  mojo::Receiver<mojom::DriveFs> receiver_{&mock_drivefs_};
  std::unique_ptr<DriveFsConnection> connection_;
  mojo::Remote<mojom::DriveFsDelegate> delegate_;
  mojo::PendingReceiver<mojom::DriveFsDelegate> pending_delegate_receiver_;
  std::string email_;
};

}  // namespace

TEST_F(DriveFsBootstrapTest, Listen_Connect_Disconnect) {
  auto token = ListenForConnection();
  EXPECT_CALL(*this, OnInit());
  WaitForConnection(token);
  EXPECT_CALL(*this, OnDisconnect());
  receiver_.reset();
  base::RunLoop().RunUntilIdle();
  ASSERT_FALSE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
      token.ToString(), {}));
}

TEST_F(DriveFsBootstrapTest, Listen_Connect_DisconnectDelegate) {
  auto token = ListenForConnection();
  EXPECT_CALL(*this, OnInit());
  WaitForConnection(token);
  EXPECT_CALL(*this, OnDisconnect());
  delegate_.reset();
  base::RunLoop().RunUntilIdle();
  ASSERT_FALSE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
      token.ToString(), {}));
}

TEST_F(DriveFsBootstrapTest, Listen_Connect_Destroy) {
  auto token = ListenForConnection();
  EXPECT_CALL(*this, OnInit());
  WaitForConnection(token);
  EXPECT_CALL(*this, OnDisconnect()).Times(0);
  connection_.reset();
  base::RunLoop().RunUntilIdle();
  ASSERT_FALSE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
      token.ToString(), {}));
}

TEST_F(DriveFsBootstrapTest, Listen_Destroy) {
  EXPECT_CALL(*this, OnDisconnect()).Times(0);
  auto token = ListenForConnection();
  connection_.reset();
  base::RunLoop().RunUntilIdle();
  ASSERT_FALSE(mojo_bootstrap::PendingConnectionManager::Get().OpenIpcChannel(
      token.ToString(), {}));
}

TEST_F(DriveFsBootstrapTest, Listen_DisconnectDelegate) {
  EXPECT_CALL(*this, OnDisconnect()).Times(0);
  ListenForConnection();
  delegate_.reset();
  base::RunLoop().RunUntilIdle();
}

}  // namespace drivefs