File: keep_alive_scheduler_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 (258 lines) | stat: -rw-r--r-- 10,515 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
253
254
255
256
257
258
// 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/keep_alive_scheduler.h"

#include <memory>
#include <vector>

#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "base/timer/mock_timer.h"
#include "chromeos/ash/components/multidevice/remote_device_test_util.h"
#include "chromeos/ash/components/tether/device_id_tether_network_guid_map.h"
#include "chromeos/ash/components/tether/fake_active_host.h"
#include "chromeos/ash/components/tether/fake_host_connection.h"
#include "chromeos/ash/components/tether/fake_host_scan_cache.h"
#include "chromeos/ash/components/tether/proto_test_util.h"
#include "chromeos/ash/services/device_sync/public/cpp/fake_device_sync_client.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 "chromeos/ash/services/secure_channel/public/cpp/client/secure_channel_client.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const char kTetherNetworkGuid[] = "tetherNetworkGuid";
const char kWifiNetworkGuid[] = "wifiNetworkGuid";
const char kRemoteDevicePublicKey[] = "remoteDevicePublicKey";
const char kTestCellProvider[] = "cellProvider";

class OperationDeletedHandler {
 public:
  virtual void OnOperationDeleted() = 0;
};

}  // namespace

namespace ash::tether {

class FakeKeepAliveOperation : public KeepAliveOperation {
 public:
  FakeKeepAliveOperation(
      const TetherHost& tether_host,
      raw_ptr<HostConnection::Factory> host_connection_factory,
      OperationDeletedHandler* handler)
      : KeepAliveOperation(tether_host, host_connection_factory),
        tether_host_(tether_host),
        handler_(handler) {}

  ~FakeKeepAliveOperation() override { handler_->OnOperationDeleted(); }

  void SendOperationFinishedEvent(std::unique_ptr<DeviceStatus> device_status) {
    device_status_ = std::move(device_status);
    OnOperationFinished();
  }

  const TetherHost& get_tether_host() { return tether_host_; }

 private:
  TetherHost tether_host_;
  raw_ptr<OperationDeletedHandler> handler_;
};

class FakeKeepAliveOperationFactory final : public KeepAliveOperation::Factory,
                                            public OperationDeletedHandler {
 public:
  FakeKeepAliveOperationFactory()
      : num_created_(0), num_deleted_(0), last_created_(nullptr) {}
  ~FakeKeepAliveOperationFactory() override = default;

  uint32_t num_created() { return num_created_; }

  uint32_t num_deleted() { return num_deleted_; }

  FakeKeepAliveOperation* last_created() { return last_created_; }

  void OnOperationDeleted() override {
    num_deleted_++;
    last_created_ = nullptr;
  }

 protected:
  std::unique_ptr<KeepAliveOperation> CreateInstance(
      const TetherHost& tether_host,
      raw_ptr<HostConnection::Factory> host_connection_factory) override {
    num_created_++;
    last_created_ =
        new FakeKeepAliveOperation(tether_host, host_connection_factory, this);
    return base::WrapUnique(last_created_.get());
  }

 private:
  uint32_t num_created_;
  uint32_t num_deleted_;
  raw_ptr<FakeKeepAliveOperation> last_created_;
};

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

 protected:
  KeepAliveSchedulerTest()
      : test_remote_device_(multidevice::RemoteDeviceRefBuilder()
                                .SetPublicKey(kRemoteDevicePublicKey)
                                .Build()) {}

  void SetUp() override {
    fake_host_connection_factory_ =
        std::make_unique<FakeHostConnection::Factory>();
    fake_active_host_ = std::make_unique<FakeActiveHost>();
    fake_host_scan_cache_ = std::make_unique<FakeHostScanCache>();
    device_id_tether_network_guid_map_ =
        std::make_unique<DeviceIdTetherNetworkGuidMap>();

    fake_operation_factory_ =
        base::WrapUnique(new FakeKeepAliveOperationFactory());
    KeepAliveOperation::Factory::SetFactoryForTesting(
        fake_operation_factory_.get());

    scheduler_ = base::WrapUnique(new KeepAliveScheduler(
        fake_host_connection_factory_.get(), fake_active_host_.get(),
        fake_host_scan_cache_.get(), device_id_tether_network_guid_map_.get(),
        std::make_unique<base::MockRepeatingTimer>()));
  }

  void VerifyTimerRunning(bool is_running) {
    EXPECT_EQ(is_running, GetSchedulerTimer()->IsRunning());

    if (is_running) {
      EXPECT_EQ(base::Minutes(KeepAliveScheduler::kKeepAliveIntervalMinutes),
                GetSchedulerTimer()->GetCurrentDelay());
    }
  }

  void SendOperationFinishedEventFromLastCreatedOperation(
      const std::string& cell_provider,
      int battery_percentage,
      int connection_strength) {
    fake_operation_factory_->last_created()->SendOperationFinishedEvent(
        std::make_unique<DeviceStatus>(CreateTestDeviceStatus(
            cell_provider, battery_percentage, connection_strength)));
  }

  void VerifyCacheUpdated(multidevice::RemoteDeviceRef remote_device,
                          const std::string& carrier,
                          int battery_percentage,
                          int signal_strength) {
    const HostScanCacheEntry* entry = fake_host_scan_cache_->GetCacheEntry(
        device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId(
            remote_device.GetDeviceId()));
    ASSERT_TRUE(entry);
    EXPECT_EQ(carrier, entry->carrier);
    EXPECT_EQ(battery_percentage, entry->battery_percentage);
    EXPECT_EQ(signal_strength, entry->signal_strength);
  }

  base::MockRepeatingTimer* GetSchedulerTimer() {
    return static_cast<base::MockRepeatingTimer*>(scheduler_->timer_.get());
  }

  base::test::TaskEnvironment task_environment_;

  multidevice::RemoteDeviceRef test_remote_device_;
  std::unique_ptr<FakeHostConnection::Factory> fake_host_connection_factory_;
  std::unique_ptr<FakeActiveHost> fake_active_host_;
  std::unique_ptr<FakeHostScanCache> fake_host_scan_cache_;
  // TODO(hansberry): Use a fake for this when a real mapping scheme is created.
  std::unique_ptr<DeviceIdTetherNetworkGuidMap>
      device_id_tether_network_guid_map_;
  // raw_ptr<base::MockRepeatingTimer> GetSchedulerTimer();

  std::unique_ptr<FakeKeepAliveOperationFactory> fake_operation_factory_;

  std::unique_ptr<KeepAliveScheduler> scheduler_;
};

TEST_F(KeepAliveSchedulerTest, TestSendTickle_OneActiveHost) {
  EXPECT_FALSE(fake_operation_factory_->num_created());
  EXPECT_FALSE(fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/false);

  // Start connecting to a device. No operation should be started.
  fake_active_host_->SetActiveHostConnecting(test_remote_device_.GetDeviceId(),
                                             std::string(kTetherNetworkGuid));
  EXPECT_FALSE(fake_operation_factory_->num_created());
  EXPECT_FALSE(fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/false);

  // Connect to the device; the operation should be started.
  fake_active_host_->SetActiveHostConnected(test_remote_device_.GetDeviceId(),
                                            std::string(kTetherNetworkGuid),
                                            std::string(kWifiNetworkGuid));
  EXPECT_EQ(1u, fake_operation_factory_->num_created());
  EXPECT_EQ(test_remote_device_, fake_operation_factory_->last_created()
                                     ->get_tether_host()
                                     .remote_device_ref());
  EXPECT_FALSE(fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/true);

  // Ensure that once the operation is finished, it is deleted.
  SendOperationFinishedEventFromLastCreatedOperation(
      kTestCellProvider, /*battery_percentage=*/50, /*connection_strength=*/2);
  EXPECT_EQ(1u, fake_operation_factory_->num_created());
  EXPECT_EQ(1u, fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/true);
  VerifyCacheUpdated(test_remote_device_, kTestCellProvider,
                     /*battery_percentage=*/50, /*signal_strength=*/50);

  // Fire the timer; this should result in tickle #2 being sent.
  GetSchedulerTimer()->Fire();
  EXPECT_EQ(2u, fake_operation_factory_->num_created());
  EXPECT_EQ(test_remote_device_, fake_operation_factory_->last_created()
                                     ->get_tether_host()
                                     .remote_device_ref());
  EXPECT_EQ(1u, fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/true);

  // Finish tickle operation #2.
  SendOperationFinishedEventFromLastCreatedOperation(
      kTestCellProvider, /*battery_percentage=*/40, /*connection_strength=*/3);
  EXPECT_EQ(2u, fake_operation_factory_->num_created());
  EXPECT_EQ(2u, fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/true);
  VerifyCacheUpdated(test_remote_device_, kTestCellProvider,
                     /*battery_percentage=*/40, /*signal_strength=*/75);

  // Fire the timer; this should result in tickle #3 being sent.
  GetSchedulerTimer()->Fire();
  EXPECT_EQ(3u, fake_operation_factory_->num_created());
  EXPECT_EQ(test_remote_device_, fake_operation_factory_->last_created()
                                     ->get_tether_host()
                                     .remote_device_ref());
  EXPECT_EQ(2u, fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/true);

  // Finish tickler operation #3. This time, simulate a failure to receive a
  // DeviceStatus back.
  fake_operation_factory_->last_created()->SendOperationFinishedEvent(nullptr);
  EXPECT_EQ(3u, fake_operation_factory_->num_created());
  EXPECT_EQ(3u, fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/true);

  // The same data returned by tickle #2 should be present.
  VerifyCacheUpdated(test_remote_device_, kTestCellProvider,
                     /*battery_percentage=*/40, /*signal_strength=*/75);

  // Disconnect that device.
  fake_active_host_->SetActiveHostDisconnected();
  EXPECT_EQ(3u, fake_operation_factory_->num_created());
  EXPECT_EQ(3u, fake_operation_factory_->num_deleted());
  VerifyTimerRunning(/*is_running=*/false);
}

}  // namespace ash::tether