Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / heartbeat_scheduler_unittest.cc
blob868c4cfed375024b9a6039ee4c0cc07c7428a858
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/heartbeat_scheduler.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
10 #include "chromeos/settings/cros_settings_names.h"
11 #include "components/gcm_driver/fake_gcm_driver.h"
12 #include "components/policy/core/common/cloud/cloud_policy_client.h"
13 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
14 #include "content/public/test/test_utils.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using ::testing::_;
19 using ::testing::SaveArg;
21 namespace {
22 const char* const kFakeEnrollmentDomain = "example.com";
23 const char* const kFakeDeviceId = "fake_device_id";
24 const char* const kHeartbeatGCMAppID = "com.google.chromeos.monitoring";
25 const char* const kRegistrationId = "registration_id";
26 const char* const kDMToken = "fake_dm_token";
28 class MockGCMDriver : public testing::StrictMock<gcm::FakeGCMDriver> {
29 public:
30 MockGCMDriver() {
33 ~MockGCMDriver() override {
36 MOCK_METHOD2(RegisterImpl,
37 void(const std::string&, const std::vector<std::string>&));
38 MOCK_METHOD3(SendImpl,
39 void(const std::string&,
40 const std::string&,
41 const gcm::OutgoingMessage& message));
43 // Helper function to complete a registration previously started by
44 // Register().
45 void CompleteRegistration(const std::string& app_id,
46 gcm::GCMClient::Result result) {
47 RegisterFinished(app_id, kRegistrationId, result);
50 // Helper function to complete a send operation previously started by
51 // Send().
52 void CompleteSend(const std::string& app_id,
53 const std::string& message_id,
54 gcm::GCMClient::Result result) {
55 SendFinished(app_id, message_id, result);
59 class HeartbeatSchedulerTest : public testing::Test {
60 public:
61 HeartbeatSchedulerTest()
62 : task_runner_(new base::TestSimpleTaskRunner()),
63 scheduler_(&gcm_driver_,
64 &cloud_policy_client_,
65 kFakeEnrollmentDomain,
66 kFakeDeviceId,
67 task_runner_) {}
69 void SetUp() override {
70 settings_helper_.ReplaceProvider(chromeos::kHeartbeatEnabled);
73 void TearDown() override {
74 content::RunAllBlockingPoolTasksUntilIdle();
77 void CheckPendingTaskDelay(base::Time last_heartbeat,
78 base::TimeDelta expected_delay) {
79 EXPECT_FALSE(last_heartbeat.is_null());
80 base::Time now = base::Time::NowFromSystemTime();
81 EXPECT_GE(now, last_heartbeat);
82 base::TimeDelta actual_delay = task_runner_->NextPendingTaskDelay();
84 // NextPendingTaskDelay() returns the exact original delay value the task
85 // was posted with. The heartbeat task would have been calculated to fire at
86 // |last_heartbeat| + |expected_delay|, but we don't know the exact time
87 // when the task was posted (if it was a couple of milliseconds after
88 // |last_heartbeat|, then |actual_delay| would be a couple of milliseconds
89 // smaller than |expected_delay|.
91 // We do know that the task was posted sometime between |last_heartbeat|
92 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <=
93 // |now| - |last_heartbeat|.
94 base::TimeDelta delta = expected_delay - actual_delay;
95 EXPECT_LE(base::TimeDelta(), delta);
96 EXPECT_GE(now - last_heartbeat, delta);
99 base::MessageLoop loop_;
100 MockGCMDriver gcm_driver_;
101 chromeos::ScopedCrosSettingsTestHelper settings_helper_;
102 testing::NiceMock<policy::MockCloudPolicyClient> cloud_policy_client_;
104 // TaskRunner used to run individual tests.
105 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
107 // The HeartbeatScheduler instance under test.
108 policy::HeartbeatScheduler scheduler_;
111 TEST_F(HeartbeatSchedulerTest, Basic) {
112 // Just makes sure we can spin up and shutdown the scheduler with
113 // heartbeats disabled.
114 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false);
115 ASSERT_TRUE(task_runner_->GetPendingTasks().empty());
118 TEST_F(HeartbeatSchedulerTest, PermanentlyFailedGCMRegistration) {
119 // If heartbeats are enabled, we should register with GCMDriver.
120 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
121 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
122 gcm_driver_.CompleteRegistration(
123 kHeartbeatGCMAppID, gcm::GCMClient::GCM_DISABLED);
125 // There should be no heartbeat tasks pending, because registration failed.
126 ASSERT_TRUE(task_runner_->GetPendingTasks().empty());
129 TEST_F(HeartbeatSchedulerTest, TemporarilyFailedGCMRegistration) {
130 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
131 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
132 gcm_driver_.CompleteRegistration(
133 kHeartbeatGCMAppID, gcm::GCMClient::SERVER_ERROR);
134 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
136 // Should have a pending task to try registering again.
137 ASSERT_FALSE(task_runner_->GetPendingTasks().empty());
138 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
139 task_runner_->RunPendingTasks();
140 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
142 // Once we have successfully registered, we should send a heartbeat.
143 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _));
144 gcm_driver_.CompleteRegistration(
145 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
146 task_runner_->RunPendingTasks();
149 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) {
150 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
151 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
152 gcm_driver_.CompleteRegistration(
153 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
155 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
156 // Should have a heartbeat task posted with zero delay on startup.
157 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay());
158 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
160 const int new_delay = 1234*1000; // 1234 seconds.
161 settings_helper_.SetInteger(chromeos::kHeartbeatFrequency, new_delay);
162 // Now run pending heartbeat task, should send a heartbeat.
163 gcm::OutgoingMessage message;
164 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
165 .WillOnce(SaveArg<2>(&message));
166 task_runner_->RunPendingTasks();
167 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
169 // Complete sending a message - we should queue up the next heartbeat
170 // even if the previous attempt failed.
171 gcm_driver_.CompleteSend(
172 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SERVER_ERROR);
173 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
174 CheckPendingTaskDelay(scheduler_.last_heartbeat(),
175 base::TimeDelta::FromMilliseconds(new_delay));
178 TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) {
179 // Makes sure that we can disable heartbeats on the fly.
180 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
181 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
182 gcm::OutgoingMessage message;
183 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
184 .WillOnce(SaveArg<2>(&message));
185 gcm_driver_.CompleteRegistration(
186 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
187 // Should have a heartbeat task posted.
188 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
189 task_runner_->RunPendingTasks();
191 // Complete sending a message - we should queue up the next heartbeat.
192 gcm_driver_.CompleteSend(
193 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SUCCESS);
195 // Should have a new heartbeat task posted.
196 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size());
197 CheckPendingTaskDelay(
198 scheduler_.last_heartbeat(),
199 base::TimeDelta::FromMilliseconds(
200 policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs));
201 testing::Mock::VerifyAndClearExpectations(&gcm_driver_);
203 // Now disable heartbeats. Should get no more heartbeats sent.
204 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, false);
205 task_runner_->RunPendingTasks();
206 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
209 TEST_F(HeartbeatSchedulerTest, CheckMessageContents) {
210 gcm::OutgoingMessage message;
211 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
212 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _))
213 .WillOnce(SaveArg<2>(&message));
214 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
215 gcm_driver_.CompleteRegistration(
216 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
217 task_runner_->RunPendingTasks();
219 // Heartbeats should have a time-to-live equivalent to the heartbeat frequency
220 // so we don't have more than one heartbeat queued at a time.
221 EXPECT_EQ(policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs/1000,
222 message.time_to_live);
224 // Check the values in the message payload.
225 EXPECT_EQ("hb", message.data["type"]);
226 int64 timestamp;
227 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], &timestamp));
228 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]);
229 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]);
232 TEST_F(HeartbeatSchedulerTest, SendGcmIdUpdate) {
233 // Verifies that GCM id update request was sent after GCM registration.
234 cloud_policy_client_.SetDMToken(kDMToken);
235 policy::CloudPolicyClient::StatusCallback callback;
236 EXPECT_CALL(cloud_policy_client_, UpdateGcmId(kRegistrationId, _))
237 .WillOnce(SaveArg<1>(&callback));
239 // Enable heartbeats.
240 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _));
241 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _));
242 settings_helper_.SetBoolean(chromeos::kHeartbeatEnabled, true);
243 gcm_driver_.CompleteRegistration(kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS);
244 task_runner_->RunPendingTasks();
246 // Verifies that CloudPolicyClient got the update request, with a valid
247 // callback.
248 testing::Mock::VerifyAndClearExpectations(&cloud_policy_client_);
249 EXPECT_FALSE(callback.is_null());
250 callback.Run(true);
253 } // namespace