File: heartbeat_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 (437 lines) | stat: -rw-r--r-- 17,087 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/policy/uploading/heartbeat_scheduler.h"

#include <stdint.h>

#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/gmock_move_support.h"
#include "base/test/test_simple_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/ash/settings/scoped_testing_cros_settings.h"
#include "chrome/browser/ash/settings/stub_cros_settings_provider.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "components/gcm_driver/common/gcm_message.h"
#include "components/gcm_driver/fake_gcm_driver.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "net/base/ip_endpoint.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {
namespace {

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::AtLeast;
using ::testing::Contains;
using ::testing::Field;
using ::testing::Key;
using ::testing::Matches;
using ::testing::Pair;
using ::testing::SaveArg;

const char* const kFakeCustomerId = "fake_customer_id";
const char* const kFakeDeviceId = "fake_device_id";
const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring";
const char* const kRegistrationId = "registration_id";
const char* const kDMToken = "fake_dm_token";

MATCHER(IsHeartbeatMsg, "") {
  return Matches(
      Field(&gcm::OutgoingMessage::data, Contains(Pair("type", "hb"))))(arg);
}

MATCHER(IsUpstreamNotificationMsg, "") {
  return Matches(Field(&gcm::OutgoingMessage::data, Contains(Key("notify"))))(
      arg);
}

class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> {
 public:
  MockGCMDriver() { IgnoreDefaultHeartbeatsInterval(); }

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

  ~MockGCMDriver() override = default;

  MOCK_METHOD2(RegisterImpl,
               void(const std::string&, const std::vector<std::string>&));
  MOCK_METHOD3(SendImpl,
               void(const std::string&,
                    const std::string&,
                    const gcm::OutgoingMessage& message));

  MOCK_METHOD2(AddHeartbeatInterval,
               void(const std::string& scope, int interval_ms));

  // Helper function to complete a registration previously started by
  // Register().
  void CompleteRegistration(const std::string& app_id,
                            gcm::GCMClient::Result result) {
    RegisterFinished(app_id, kRegistrationId, result);
  }

  // Helper function to complete a send operation previously started by
  // Send().
  void CompleteSend(const std::string& app_id,
                    const std::string& message_id,
                    gcm::GCMClient::Result result) {
    SendFinished(app_id, message_id, result);
  }

  void AddConnectionObserver(gcm::GCMConnectionObserver* observer) override {
    EXPECT_FALSE(observer_);
    observer_ = observer;
  }

  void RemoveConnectionObserver(gcm::GCMConnectionObserver* observer) override {
    EXPECT_TRUE(observer_);
    observer_ = nullptr;
  }

  void NotifyConnected() {
    ASSERT_TRUE(observer_);
    observer_->OnConnected(net::IPEndPoint());
  }

  void IgnoreDefaultHeartbeatsInterval() {
    // Ignore default setting for heartbeats interval.
    EXPECT_CALL(*this, AddHeartbeatInterval(_, 2 * 60 * 1000))
        .Times(AnyNumber());
  }

  void ResetStore() {
    // Defensive copy in case OnStoreReset calls Add/RemoveAppHandler.
    std::vector<gcm::GCMAppHandler*> app_handler_values;
    for (const auto& key_value : app_handlers())
      app_handler_values.push_back(key_value.second);
    for (gcm::GCMAppHandler* app_handler : app_handler_values) {
      app_handler->OnStoreReset();
      // app_handler might now have been deleted.
    }
  }

 private:
  raw_ptr<gcm::GCMConnectionObserver> observer_ = nullptr;
};

class HeartbeatSchedulerTest : public testing::Test {
 public:
  HeartbeatSchedulerTest()
      : task_runner_(new base::TestSimpleTaskRunner()),
        scheduler_(&gcm_driver_,
                   &cloud_policy_client_,
                   &cloud_policy_store_,
                   kFakeDeviceId,
                   task_runner_) {}

  void SetUp() override {
    auto policy_data = std::make_unique<enterprise_management::PolicyData>();
    policy_data->set_obfuscated_customer_id(kFakeCustomerId);
    cloud_policy_store_.set_policy_data_for_testing(std::move(policy_data));
  }

  void TearDown() override { content::RunAllTasksUntilIdle(); }

  void CheckPendingTaskDelay(base::Time last_heartbeat,
                             base::TimeDelta expected_delay) {
    EXPECT_FALSE(last_heartbeat.is_null());
    base::Time now = base::Time::NowFromSystemTime();
    EXPECT_GE(now, last_heartbeat);
    base::TimeDelta actual_delay = task_runner_->NextPendingTaskDelay();

    // NextPendingTaskDelay() returns the exact original delay value the task
    // was posted with. The heartbeat task would have been calculated to fire at
    // |last_heartbeat| + |expected_delay|, but we don't know the exact time
    // when the task was posted (if it was a couple of milliseconds after
    // |last_heartbeat|, then |actual_delay| would be a couple of milliseconds
    // smaller than |expected_delay|.
    //
    // We do know that the task was posted sometime between |last_heartbeat|
    // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <=
    // |now| - |last_heartbeat|.
    base::TimeDelta delta = expected_delay - actual_delay;
    EXPECT_LE(base::TimeDelta(), delta);
    EXPECT_GE(now - last_heartbeat, delta);
  }

  void IgnoreUpstreamNotificationMsg() {
    EXPECT_CALL(gcm_driver_,
                SendImpl(kHeartbeatGCMAppID, _, IsUpstreamNotificationMsg()))
        .Times(AnyNumber());
  }

  content::BrowserTaskEnvironment task_environment_;
  MockGCMDriver gcm_driver_;
  ash::ScopedTestingCrosSettings scoped_testing_cros_settings_;
  testing::NiceMock<MockCloudPolicyClient> cloud_policy_client_;
  testing::NiceMock<MockCloudPolicyStore> cloud_policy_store_;

  // TaskRunner used to run individual tests.
  scoped_refptr<base::TestSimpleTaskRunner> task_runner_;

  // The HeartbeatScheduler instance under test.
  HeartbeatScheduler scheduler_;
};

TEST_F(HeartbeatSchedulerTest, Basic) {
  // Just makes sure we can spin up and shutdown the scheduler with
  // heartbeats disabled.
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, false);
  ASSERT_FALSE(task_runner_->HasPendingTask());
}

TEST_F(HeartbeatSchedulerTest, PermanentlyFailedGCMRegistration) {
  // If heartbeats are enabled, we should register with GCMDriver.
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID,
                                   gcm::GCMClient::GCM_DISABLED);

  // There should be no heartbeat tasks pending, because registration failed.
  ASSERT_FALSE(task_runner_->HasPendingTask());
}

TEST_F(HeartbeatSchedulerTest, TemporarilyFailedGCMRegistration) {
  IgnoreUpstreamNotificationMsg();

  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID,
                                   gcm::GCMClient::SERVER_ERROR);
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);

  IgnoreUpstreamNotificationMsg();
  gcm_driver_.IgnoreDefaultHeartbeatsInterval();

  // Should have a pending task to try registering again.
  ASSERT_TRUE(task_runner_->HasPendingTask());
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  task_runner_->RunPendingTasks();
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);

  IgnoreUpstreamNotificationMsg();
  gcm_driver_.IgnoreDefaultHeartbeatsInterval();

  // Once we have successfully registered, we should send a heartbeat.
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg()));
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
  task_runner_->RunPendingTasks();
}

TEST_F(HeartbeatSchedulerTest, StoreResetDuringRegistration) {
  IgnoreUpstreamNotificationMsg();

  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);

  gcm_driver_.ResetStore();

  IgnoreUpstreamNotificationMsg();

  // Successful registration handled ok despite store reset.
  EXPECT_FALSE(task_runner_->HasPendingTask());
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg()));
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
  EXPECT_EQ(1U, task_runner_->NumPendingTasks());
  task_runner_->RunPendingTasks();
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
}

TEST_F(HeartbeatSchedulerTest, StoreResetAfterRegistration) {
  IgnoreUpstreamNotificationMsg();

  // Start from a successful registration.
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg()));
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
  EXPECT_EQ(1U, task_runner_->NumPendingTasks());
  task_runner_->RunPendingTasks();
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);

  gcm_driver_.IgnoreDefaultHeartbeatsInterval();

  // Reseting the store should trigger re-registration.
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  gcm_driver_.ResetStore();
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);

  IgnoreUpstreamNotificationMsg();

  // Once we have successfully re-registered, we should send a heartbeat.
  EXPECT_FALSE(task_runner_->HasPendingTask());
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg()));
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
  EXPECT_EQ(1U, task_runner_->NumPendingTasks());
  task_runner_->RunPendingTasks();
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
}

TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) {
  IgnoreUpstreamNotificationMsg();

  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);

  EXPECT_EQ(1U, task_runner_->NumPendingTasks());
  // Should have a heartbeat task posted with zero delay on startup.
  EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay());
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);

  IgnoreUpstreamNotificationMsg();
  gcm_driver_.IgnoreDefaultHeartbeatsInterval();

  const int new_delay = 1234 * 1000;  // 1234 seconds.
  EXPECT_CALL(gcm_driver_, AddHeartbeatInterval(_, new_delay));
  scoped_testing_cros_settings_.device_settings()->SetInteger(
      ash::kHeartbeatFrequency, new_delay);
  // Now run pending heartbeat task, should send a heartbeat.
  gcm::OutgoingMessage message;
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg()))
      .WillOnce(SaveArg<2>(&message));
  task_runner_->RunPendingTasks();
  EXPECT_FALSE(task_runner_->HasPendingTask());

  // Complete sending a message - we should queue up the next heartbeat
  // even if the previous attempt failed.
  gcm_driver_.CompleteSend(kHeartbeatGCMAppID, message.id,
                           gcm::GCMClient::SERVER_ERROR);
  EXPECT_EQ(1U, task_runner_->NumPendingTasks());
  CheckPendingTaskDelay(scheduler_.last_heartbeat(),
                        base::Milliseconds(new_delay));
}

TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) {
  IgnoreUpstreamNotificationMsg();

  // Makes sure that we can disable heartbeats on the fly.
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  gcm::OutgoingMessage message;
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg()))
      .WillOnce(SaveArg<2>(&message));
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
  // Should have a heartbeat task posted.
  EXPECT_EQ(1U, task_runner_->NumPendingTasks());
  task_runner_->RunPendingTasks();

  // Complete sending a message - we should queue up the next heartbeat.
  gcm_driver_.CompleteSend(kHeartbeatGCMAppID, message.id,
                           gcm::GCMClient::SUCCESS);

  // Should have a new heartbeat task posted.
  ASSERT_EQ(1U, task_runner_->NumPendingTasks());
  CheckPendingTaskDelay(scheduler_.last_heartbeat(),
                        HeartbeatScheduler::kDefaultHeartbeatInterval);
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);

  IgnoreUpstreamNotificationMsg();
  gcm_driver_.IgnoreDefaultHeartbeatsInterval();

  // Now disable heartbeats. Should get no more heartbeats sent.
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, false);
  task_runner_->RunPendingTasks();
  EXPECT_FALSE(task_runner_->HasPendingTask());
}

TEST_F(HeartbeatSchedulerTest, CheckMessageContents) {
  IgnoreUpstreamNotificationMsg();

  gcm::OutgoingMessage message;
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg()))
      .WillOnce(SaveArg<2>(&message));
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
  task_runner_->RunPendingTasks();

  // Heartbeats should have a time-to-live equivalent to the heartbeat frequency
  // so we don't have more than one heartbeat queued at a time.
  EXPECT_EQ(HeartbeatScheduler::kDefaultHeartbeatInterval,
            base::Seconds(message.time_to_live));

  // Check the values in the message payload.
  EXPECT_EQ("hb", message.data["type"]);
  int64_t timestamp;
  EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], &timestamp));
  EXPECT_EQ(kFakeCustomerId, message.data["customer_id"]);
  EXPECT_EQ(kFakeDeviceId, message.data["device_id"]);
}

TEST_F(HeartbeatSchedulerTest, SendGcmIdUpdate) {
  // Verifies that GCM id update request was sent after GCM registration.
  cloud_policy_client_.SetDMToken(kDMToken);
  CloudPolicyClient::StatusCallback callback;
  EXPECT_CALL(cloud_policy_client_, UpdateGcmId(kRegistrationId, _))
      .WillOnce(MoveArg<1>(&callback));

  // Enable heartbeats.
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
      .Times(AtLeast(1));
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
  task_runner_->RunPendingTasks();

  // Verifies that CloudPolicyClient got the update request, with a valid
  // callback.
  testing::Mock::VerifyAndClearExpectations(&cloud_policy_client_);
  EXPECT_FALSE(callback.is_null());
  std::move(callback).Run(true);
}

TEST_F(HeartbeatSchedulerTest, GcmUpstreamNotificationSignup) {
  // Verifies that upstream notification works as expected.
  cloud_policy_client_.SetDMToken(kDMToken);
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _))
      .Times(AnyNumber());
  EXPECT_CALL(cloud_policy_client_, UpdateGcmId(kRegistrationId, _));

  // GCM connected event before the registration should be ignored.
  scoped_testing_cros_settings_.device_settings()->SetBoolean(
      ash::kHeartbeatEnabled, true);
  gcm_driver_.NotifyConnected();
  task_runner_->RunPendingTasks();
  testing::Mock::VerifyAndClearExpectations(&gcm_driver_);

  // Ignore unintested calls.
  EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _))
      .Times(AnyNumber());
  EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, IsHeartbeatMsg()))
      .Times(AnyNumber());
  gcm_driver_.IgnoreDefaultHeartbeatsInterval();

  EXPECT_CALL(gcm_driver_,
              SendImpl(kHeartbeatGCMAppID, _, IsUpstreamNotificationMsg()))
      .Times(AtLeast(1));
  gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);

  gcm_driver_.NotifyConnected();
}

}  // namespace
}  // namespace policy