File: message_transfer_operation_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 (229 lines) | stat: -rw-r--r-- 8,233 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
// Copyright 2017 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/message_transfer_operation.h"

#include <memory>

#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/timer/mock_timer.h"
#include "chromeos/ash/components/multidevice/remote_device_test_util.h"
#include "chromeos/ash/components/tether/fake_host_connection.h"
#include "chromeos/ash/components/tether/message_wrapper.h"
#include "chromeos/ash/components/tether/proto_test_util.h"
#include "chromeos/ash/components/timer_factory/fake_one_shot_timer.h"
#include "chromeos/ash/components/timer_factory/fake_timer_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::tether {

namespace {

// Arbitrarily chosen value. The MessageType used in this test does not matter
// except that it must be consistent throughout the test.
const MessageType kTestMessageType = MessageType::TETHER_AVAILABILITY_REQUEST;

const uint32_t kTestTimeoutSeconds = 5;

// A test double for MessageTransferOperation is needed because
// MessageTransferOperation has pure virtual methods which must be overridden in
// order to create a concrete instantiation of the class.
class TestOperation : public MessageTransferOperation {
 public:
  TestOperation(const TetherHost& tether_host,
                raw_ptr<HostConnection::Factory> host_connection_factory)
      : MessageTransferOperation(
            tether_host,
            HostConnection::Factory::ConnectionPriority::kLow,
            host_connection_factory) {}

  ~TestOperation() override = default;

  // MessageTransferOperation:
  void OnDeviceAuthenticated() override { has_device_authenticated_ = true; }

  void OnMessageReceived(
      std::unique_ptr<MessageWrapper> message_wrapper) override {
    received_messages_.push_back(std::move(message_wrapper));

    if (should_stop_operation_on_message_received_) {
      StopOperation();
    }
  }

  void OnOperationStarted() override { has_operation_started_ = true; }

  void OnOperationFinished() override { has_operation_finished_ = true; }

  MessageType GetMessageTypeForConnection() override {
    return kTestMessageType;
  }

  uint32_t GetMessageTimeoutSeconds() override { return timeout_seconds_; }

  void set_timeout_seconds(uint32_t timeout_seconds) {
    timeout_seconds_ = timeout_seconds;
  }

  void set_should_stop_operation_on_message_received(
      bool should_stop_operation_on_message_received) {
    should_stop_operation_on_message_received_ =
        should_stop_operation_on_message_received;
  }

  bool has_device_authenticated() { return has_device_authenticated_; }

  bool has_operation_started() { return has_operation_started_; }

  bool has_operation_finished() { return has_operation_finished_; }

  const std::vector<std::unique_ptr<MessageWrapper>>& get_received_messages() {
    return received_messages_;
  }

 private:
  bool has_device_authenticated_ = false;
  std::vector<std::unique_ptr<MessageWrapper>> received_messages_;

  uint32_t timeout_seconds_ = kTestTimeoutSeconds;
  bool should_stop_operation_on_message_received_ = false;
  bool has_operation_started_ = false;
  bool has_operation_finished_ = false;
};

TetherAvailabilityResponse CreateTetherAvailabilityResponse() {
  TetherAvailabilityResponse response;
  response.set_response_code(
      TetherAvailabilityResponse_ResponseCode::
          TetherAvailabilityResponse_ResponseCode_TETHER_AVAILABLE);
  response.mutable_device_status()->CopyFrom(
      CreateDeviceStatusWithFakeFields());
  return response;
}

}  // namespace

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

 protected:
  MessageTransferOperationTest()
      : tether_host_(TetherHost(multidevice::CreateRemoteDeviceRefForTest())) {}

  void SetUp() override {
    fake_host_connection_factory_ =
        std::make_unique<FakeHostConnection::Factory>();
    operation_ = std::make_unique<TestOperation>(
        tether_host_, fake_host_connection_factory_.get());
    operation_->SetTimerFactoryForTest(
        std::make_unique<ash::timer_factory::FakeTimerFactory>());
    VerifyOperationStartedAndFinished(false /* has_started */,
                                      false /* has_finished */);
  }

  void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) {
    EXPECT_EQ(has_started, operation_->has_operation_started());
    EXPECT_EQ(has_finished, operation_->has_operation_finished());
  }

  ash::timer_factory::FakeOneShotTimer* GetOperationTimer() {
    return static_cast<ash::timer_factory::FakeOneShotTimer*>(
        operation_->remote_device_timer_.get());
  }

  void VerifyDefaultTimerCreated() { VerifyTimerCreated(kTestTimeoutSeconds); }

  void VerifyConnectionTimerCreated() {
    VerifyTimerCreated(MessageTransferOperation::kConnectionTimeoutSeconds);
  }

  void VerifyTimerCreated(uint32_t timeout_seconds) {
    EXPECT_TRUE(GetOperationTimer());
    EXPECT_EQ(base::Seconds(timeout_seconds),
              GetOperationTimer()->GetCurrentDelay());
  }

  void SendMessageToDevice(std::unique_ptr<MessageWrapper> message_wrapper) {
    return operation_->SendMessage(std::move(message_wrapper),
                                   base::DoNothing());
  }

  const TetherHost tether_host_;

  std::unique_ptr<FakeHostConnection::Factory> fake_host_connection_factory_;
  std::unique_ptr<TestOperation> operation_;
};

TEST_F(MessageTransferOperationTest, TestFailedConnection) {
  fake_host_connection_factory_->FailConnectionAttempt(tether_host_);

  operation_->Initialize();

  VerifyOperationStartedAndFinished(true /* has_started */,
                                    true /* has_finished */);
  EXPECT_FALSE(operation_->has_device_authenticated());
  EXPECT_TRUE(operation_->get_received_messages().empty());
}

TEST_F(MessageTransferOperationTest,
       TestSuccessfulConnectionSendAndReceiveMessage) {
  // Simulate how subclasses behave after a successful response: unregister the
  // device.
  operation_->set_should_stop_operation_on_message_received(true);

  fake_host_connection_factory_->SetupConnectionAttempt(tether_host_);

  operation_->Initialize();
  EXPECT_TRUE(operation_->has_device_authenticated());
  VerifyDefaultTimerCreated();

  auto message_wrapper =
      std::make_unique<MessageWrapper>(TetherAvailabilityRequest());
  std::string expected_payload = message_wrapper->ToRawMessage();
  SendMessageToDevice(std::move(message_wrapper));
  const std::vector<std::pair<std::unique_ptr<MessageWrapper>,
                              base::OnceClosure>>& sent_messages =
      fake_host_connection_factory_
          ->GetActiveConnection(tether_host_.GetDeviceId())
          ->sent_messages();
  EXPECT_EQ(1u, sent_messages.size());
  EXPECT_EQ(expected_payload, sent_messages[0].first->ToRawMessage());

  fake_host_connection_factory_->GetActiveConnection(tether_host_.GetDeviceId())
      ->ReceiveMessage(
          std::make_unique<MessageWrapper>(CreateTetherAvailabilityResponse()));

  EXPECT_EQ(1u, operation_->get_received_messages().size());
  const auto& message = operation_->get_received_messages()[0];
  EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE,
            message->GetMessageType());
  EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(),
            message->GetProto()->SerializeAsString());
}

TEST_F(MessageTransferOperationTest, TestTimesOutBeforeAuthentication) {
  operation_->Initialize();
  GetOperationTimer()->Fire();
  EXPECT_TRUE(operation_->has_operation_finished());
}

TEST_F(MessageTransferOperationTest, TestAuthenticatesButThenTimesOut) {
  fake_host_connection_factory_->SetupConnectionAttempt(tether_host_);

  operation_->Initialize();

  EXPECT_TRUE(operation_->has_device_authenticated());
  VerifyDefaultTimerCreated();

  GetOperationTimer()->Fire();

  EXPECT_TRUE(operation_->has_operation_finished());
}

}  // namespace ash::tether