File: secure_channel_host_connection_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 (252 lines) | stat: -rw-r--r-- 9,508 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2024 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/tether/secure_channel_host_connection.h"

#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/multidevice/remote_device_test_util.h"
#include "chromeos/ash/components/tether/fake_tether_host_fetcher.h"
#include "chromeos/ash/services/device_sync/public/cpp/fake_device_sync_client.h"
#include "chromeos/ash/services/secure_channel/public/cpp/client/fake_client_channel.h"
#include "chromeos/ash/services/secure_channel/public/cpp/client/fake_connection_attempt.h"
#include "chromeos/ash/services/secure_channel/public/cpp/client/fake_secure_channel_client.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::tether {

namespace {

class FakeHostConnectionPayloadListener
    : public HostConnection::PayloadListener {
 public:
  ~FakeHostConnectionPayloadListener() override = default;

  // HostConnection::PayloadListener:
  void OnMessageReceived(std::unique_ptr<MessageWrapper> message) override {
    received_messages_.push_back(std::move(message));
  }

  std::vector<std::unique_ptr<MessageWrapper>> received_messages_;
};

}  // namespace

class SecureChannelHostConnectionTest : public testing::Test {
 public:
  SecureChannelHostConnectionTest(const SecureChannelHostConnectionTest&) =
      delete;
  SecureChannelHostConnectionTest& operator=(
      const SecureChannelHostConnectionTest&) = delete;

  void OnSend() { message_sent_ = true; }

 protected:
  SecureChannelHostConnectionTest()
      : fake_local_device_(multidevice::RemoteDeviceRefBuilder()
                               .SetPublicKey("local device")
                               .Build()),
        fake_remote_device_(multidevice::RemoteDeviceRefBuilder()
                                .SetPublicKey("remote device")
                                .Build()) {}

  void SetUp() override {
    fake_device_sync_client_ =
        std::make_unique<device_sync::FakeDeviceSyncClient>();
    fake_device_sync_client_->set_local_device_metadata(fake_local_device_);
    fake_device_sync_client_->set_synced_devices(
        multidevice::RemoteDeviceRefList{fake_remote_device_});
    fake_secure_channel_client_ =
        std::make_unique<secure_channel::FakeSecureChannelClient>();
    fake_tether_host_fetcher_ =
        std::make_unique<FakeTetherHostFetcher>(fake_remote_device_);
    fake_host_payload_listener_ =
        std::make_unique<FakeHostConnectionPayloadListener>();
    host_connection_factory_ =
        std::make_unique<SecureChannelHostConnection::Factory>(
            fake_device_sync_client_.get(), fake_secure_channel_client_.get(),
            fake_tether_host_fetcher_.get());
  }

  void SetupNextConnectionAttempt(
      secure_channel::FakeConnectionAttempt* connection_attempt) {
    fake_secure_channel_client_->set_next_listen_connection_attempt(
        fake_remote_device_, fake_local_device_,
        base::WrapUnique<secure_channel::ConnectionAttempt>(
            connection_attempt));
  }

  bool message_sent_ = false;
  const multidevice::RemoteDeviceRef fake_local_device_;
  const multidevice::RemoteDeviceRef fake_remote_device_;
  std::unique_ptr<secure_channel::FakeSecureChannelClient>
      fake_secure_channel_client_;
  std::unique_ptr<device_sync::FakeDeviceSyncClient> fake_device_sync_client_;
  std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_;
  std::unique_ptr<FakeHostConnectionPayloadListener>
      fake_host_payload_listener_;
  std::unique_ptr<SecureChannelHostConnection::Factory>
      host_connection_factory_;

  base::test::TaskEnvironment task_environment_;
  base::WeakPtrFactory<SecureChannelHostConnectionTest> weak_ptr_factory_{this};
};

TEST_F(SecureChannelHostConnectionTest, TestDoesCreateConnectionByDeviceId) {
  // Create a connection attempt.
  auto* fake_connection_attempt = new secure_channel::FakeConnectionAttempt();
  SetupNextConnectionAttempt(fake_connection_attempt);

  base::test::TestFuture<std::unique_ptr<HostConnection>> future;

  // Create a host connection by device ID.
  host_connection_factory_->ScanForTetherHostAndCreateConnection(
      fake_remote_device_.GetDeviceId(),
      HostConnection::Factory::ConnectionPriority::kLow,
      fake_host_payload_listener_.get(), base::DoNothing(),
      future.GetCallback());

  // Finish the connection attempt.
  fake_connection_attempt->NotifyConnection(
      std::make_unique<secure_channel::FakeClientChannel>());

  // Expect the connection is now connected.
  EXPECT_TRUE(future.Get());
}

TEST_F(SecureChannelHostConnectionTest,
       TestDoesFailWhenConnectionAttemptFails) {
  // Create a connection attempt.
  auto* fake_connection_attempt = new secure_channel::FakeConnectionAttempt();
  SetupNextConnectionAttempt(fake_connection_attempt);

  base::test::TestFuture<std::unique_ptr<HostConnection>> future;

  // Create a host connection by device ID.
  host_connection_factory_->ScanForTetherHostAndCreateConnection(
      fake_remote_device_.GetDeviceId(),
      HostConnection::Factory::ConnectionPriority::kLow,
      fake_host_payload_listener_.get(), base::DoNothing(),
      future.GetCallback());

  // Finish the connection attempt.
  fake_connection_attempt->NotifyConnectionAttemptFailure(
      ash::secure_channel::mojom::ConnectionAttemptFailureReason::
          AUTHENTICATION_ERROR);

  // Expect the connection is now connected.
  EXPECT_FALSE(future.Get());
}

TEST_F(SecureChannelHostConnectionTest, TestDoesSendMessage) {
  // Create a connection attempt.
  auto* fake_connection_attempt = new secure_channel::FakeConnectionAttempt();
  SetupNextConnectionAttempt(fake_connection_attempt);

  // Setup client channel.
  secure_channel::FakeClientChannel* fake_client_channel =
      new secure_channel::FakeClientChannel();

  base::test::TestFuture<std::unique_ptr<HostConnection>> future;

  // Create the connection.
  host_connection_factory_->Create(
      TetherHost(fake_remote_device_),
      HostConnection::Factory::ConnectionPriority::kLow,
      fake_host_payload_listener_.get(), base::DoNothing(),
      future.GetCallback());

  // Finish the connection.
  fake_connection_attempt->NotifyConnection(
      base::WrapUnique(fake_client_channel));

  // Send the message.
  auto message = std::make_unique<MessageWrapper>(TetherAvailabilityResponse());
  std::string expected_payload = message->ToRawMessage();
  future.Get()->SendMessage(
      std::move(message),
      base::BindOnce(&SecureChannelHostConnectionTest::OnSend,
                     weak_ptr_factory_.GetWeakPtr()));

  base::RunLoop().RunUntilIdle();

  // Expect the message was sent.
  std::vector<std::pair<std::string, base::OnceClosure>>& sent_messages =
      fake_client_channel->sent_messages();
  EXPECT_EQ(1u, sent_messages.size());
  EXPECT_EQ(expected_payload, sent_messages[0].first);

  // Fire the callback - expect the delegate is notified of the message send.
  std::move(sent_messages[0].second).Run();
  EXPECT_TRUE(message_sent_);
}

TEST_F(SecureChannelHostConnectionTest, TestDoesReceiveMessage) {
  // Create a connection attempt.
  auto* fake_connection_attempt = new secure_channel::FakeConnectionAttempt();
  SetupNextConnectionAttempt(fake_connection_attempt);

  // Setup client channel.
  secure_channel::FakeClientChannel* fake_client_channel =
      new secure_channel::FakeClientChannel();

  base::test::TestFuture<std::unique_ptr<HostConnection>> future;

  // Create the connection.
  host_connection_factory_->Create(
      TetherHost(fake_remote_device_),
      HostConnection::Factory::ConnectionPriority::kLow,
      fake_host_payload_listener_.get(), base::DoNothing(),
      future.GetCallback());

  // Finish the connection.
  fake_connection_attempt->NotifyConnection(
      base::WrapUnique(fake_client_channel));

  EXPECT_TRUE(future.Wait());

  std::unique_ptr<MessageWrapper> message =
      std::make_unique<MessageWrapper>(TetherAvailabilityResponse());
  fake_client_channel->NotifyMessageReceived(message->ToRawMessage());

  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(1u, fake_host_payload_listener_->received_messages_.size());
  EXPECT_EQ(message->ToRawMessage(),
            fake_host_payload_listener_->received_messages_[0]->ToRawMessage());
}

TEST_F(SecureChannelHostConnectionTest, TestDoesSendDisconnection) {
  // Create a connection attempt.
  auto* fake_connection_attempt = new secure_channel::FakeConnectionAttempt();
  SetupNextConnectionAttempt(fake_connection_attempt);

  // Setup client channel.
  secure_channel::FakeClientChannel* fake_client_channel =
      new secure_channel::FakeClientChannel();

  base::test::TestFuture<std::unique_ptr<HostConnection>> future;
  base::test::TestFuture<void> disconnection_callback;

  // Create the connection.
  host_connection_factory_->Create(
      TetherHost(fake_remote_device_),
      HostConnection::Factory::ConnectionPriority::kLow,
      fake_host_payload_listener_.get(), disconnection_callback.GetCallback(),
      future.GetCallback());

  // Finish the connection.
  fake_connection_attempt->NotifyConnection(
      base::WrapUnique(fake_client_channel));

  EXPECT_TRUE(future.Wait());

  fake_client_channel->NotifyDisconnected();

  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(disconnection_callback.Wait());
}
}  // namespace ash::tether