OLD | NEW |
---|---|
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/policy/heartbeat_scheduler.h" | 5 #include "chrome/browser/chromeos/policy/heartbeat_scheduler.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/test/test_simple_task_runner.h" | 8 #include "base/test/test_simple_task_runner.h" |
9 #include "chrome/browser/chromeos/settings/cros_settings.h" | 9 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" |
10 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
11 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | |
12 #include "chromeos/settings/cros_settings_names.h" | 10 #include "chromeos/settings/cros_settings_names.h" |
13 #include "components/gcm_driver/fake_gcm_driver.h" | 11 #include "components/gcm_driver/fake_gcm_driver.h" |
14 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
17 | 15 |
18 using ::testing::_; | 16 using ::testing::_; |
19 using ::testing::SaveArg; | 17 using ::testing::SaveArg; |
20 | 18 |
21 namespace { | 19 namespace { |
(...skipping 25 matching lines...) Expand all Loading... | |
47 | 45 |
48 // Helper function to complete a send operation previously started by | 46 // Helper function to complete a send operation previously started by |
49 // Send(). | 47 // Send(). |
50 void CompleteSend(const std::string& app_id, | 48 void CompleteSend(const std::string& app_id, |
51 const std::string& message_id, | 49 const std::string& message_id, |
52 gcm::GCMClient::Result result) { | 50 gcm::GCMClient::Result result) { |
53 SendFinished(app_id, message_id, result); | 51 SendFinished(app_id, message_id, result); |
54 } | 52 } |
55 }; | 53 }; |
56 | 54 |
57 class HeartbeatSchedulerTest : public testing::Test { | 55 class HeartbeatSchedulerTest : public chromeos::OwnerSettingsTestBase { |
Mattias Nissler (ping if slow)
2015/03/27 13:45:58
Instead of pulling in the heavy-weight full device
Ivan Podogov
2015/03/27 14:40:59
True, sometimes it's the only option left... Altho
Mattias Nissler (ping if slow)
2015/03/27 16:20:23
The boilerplate reduction is certainly great. Howe
Ivan Podogov
2015/03/30 08:54:28
OK, I guess I'll make such a class.
Do you think w
Mattias Nissler (ping if slow)
2015/03/30 09:03:51
Judgement call, depends on how convenient it ends
Ivan Podogov
2015/03/30 09:10:23
I'm going to go over more than a dozen tests, and
Mattias Nissler (ping if slow)
2015/03/30 09:11:08
I'd say go a head and add them then :)
Ivan Podogov
2015/03/30 14:22:57
OK, so I suppose the new patchset should look more
| |
58 public: | 56 public: |
59 HeartbeatSchedulerTest() | 57 HeartbeatSchedulerTest() |
60 : task_runner_(new base::TestSimpleTaskRunner()), | 58 : task_runner_(new base::TestSimpleTaskRunner()), |
61 scheduler_( | 59 scheduler_( |
62 &gcm_driver_, kFakeEnrollmentDomain, kFakeDeviceId, task_runner_) { | 60 &gcm_driver_, kFakeEnrollmentDomain, kFakeDeviceId, task_runner_) { |
63 } | 61 } |
64 | 62 |
65 void SetUp() override { | 63 void SetUp() override { |
66 // Swap out the DeviceSettingsProvider with our stub settings provider | 64 chromeos::OwnerSettingsTestBase::SetUp(); |
67 // so we can set values for the heartbeat frequency. | 65 ReplaceProvider(chromeos::kHeartbeatEnabled); |
68 chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get(); | |
69 device_settings_provider_ = | |
70 cros_settings->GetProvider(chromeos::kReportDeviceVersionInfo); | |
71 EXPECT_TRUE(device_settings_provider_); | |
72 EXPECT_TRUE( | |
73 cros_settings->RemoveSettingsProvider(device_settings_provider_)); | |
74 cros_settings->AddSettingsProvider(&stub_settings_provider_); | |
75 } | 66 } |
76 | 67 |
77 void TearDown() override { | 68 void TearDown() override { |
69 chromeos::OwnerSettingsTestBase::TearDown(); | |
78 content::RunAllBlockingPoolTasksUntilIdle(); | 70 content::RunAllBlockingPoolTasksUntilIdle(); |
79 // Restore the real DeviceSettingsProvider. | |
80 chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get(); | |
81 EXPECT_TRUE(cros_settings->RemoveSettingsProvider( | |
82 &stub_settings_provider_)); | |
83 cros_settings->AddSettingsProvider(device_settings_provider_); | |
84 } | 71 } |
85 | 72 |
86 void CheckPendingTaskDelay(base::Time last_heartbeat, | 73 void CheckPendingTaskDelay(base::Time last_heartbeat, |
87 base::TimeDelta expected_delay) { | 74 base::TimeDelta expected_delay) { |
88 EXPECT_FALSE(last_heartbeat.is_null()); | 75 EXPECT_FALSE(last_heartbeat.is_null()); |
89 base::Time now = base::Time::NowFromSystemTime(); | 76 base::Time now = base::Time::NowFromSystemTime(); |
90 EXPECT_GE(now, last_heartbeat); | 77 EXPECT_GE(now, last_heartbeat); |
91 base::TimeDelta actual_delay = task_runner_->NextPendingTaskDelay(); | 78 base::TimeDelta actual_delay = task_runner_->NextPendingTaskDelay(); |
92 | 79 |
93 // NextPendingTaskDelay() returns the exact original delay value the task | 80 // NextPendingTaskDelay() returns the exact original delay value the task |
94 // was posted with. The heartbeat task would have been calculated to fire at | 81 // was posted with. The heartbeat task would have been calculated to fire at |
95 // |last_heartbeat| + |expected_delay|, but we don't know the exact time | 82 // |last_heartbeat| + |expected_delay|, but we don't know the exact time |
96 // when the task was posted (if it was a couple of milliseconds after | 83 // when the task was posted (if it was a couple of milliseconds after |
97 // |last_heartbeat|, then |actual_delay| would be a couple of milliseconds | 84 // |last_heartbeat|, then |actual_delay| would be a couple of milliseconds |
98 // smaller than |expected_delay|. | 85 // smaller than |expected_delay|. |
99 // | 86 // |
100 // We do know that the task was posted sometime between |last_heartbeat| | 87 // We do know that the task was posted sometime between |last_heartbeat| |
101 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <= | 88 // and |now|, so we know that 0 <= |expected_delay| - |actual_delay| <= |
102 // |now| - |last_heartbeat|. | 89 // |now| - |last_heartbeat|. |
103 base::TimeDelta delta = expected_delay - actual_delay; | 90 base::TimeDelta delta = expected_delay - actual_delay; |
104 EXPECT_LE(base::TimeDelta(), delta); | 91 EXPECT_LE(base::TimeDelta(), delta); |
105 EXPECT_GE(now - last_heartbeat, delta); | 92 EXPECT_GE(now - last_heartbeat, delta); |
106 } | 93 } |
107 | 94 |
108 base::MessageLoop loop_; | |
109 | |
110 // Helpers used to mock out cros settings. | |
111 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | |
112 chromeos::ScopedTestCrosSettings test_cros_settings_; | |
113 chromeos::CrosSettingsProvider* device_settings_provider_; | |
114 chromeos::StubCrosSettingsProvider stub_settings_provider_; | |
115 | |
116 MockGCMDriver gcm_driver_; | 95 MockGCMDriver gcm_driver_; |
117 | 96 |
118 // TaskRunner used to run individual tests. | 97 // TaskRunner used to run individual tests. |
119 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 98 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
120 | 99 |
121 // The HeartbeatScheduler instance under test. | 100 // The HeartbeatScheduler instance under test. |
122 policy::HeartbeatScheduler scheduler_; | 101 policy::HeartbeatScheduler scheduler_; |
123 }; | 102 }; |
124 | 103 |
125 TEST_F(HeartbeatSchedulerTest, Basic) { | 104 TEST_F(HeartbeatSchedulerTest, Basic) { |
126 // Just makes sure we can spin up and shutdown the scheduler with | 105 // Just makes sure we can spin up and shutdown the scheduler with |
127 // heartbeats disabled. | 106 // heartbeats disabled. |
128 chromeos::CrosSettings::Get()->SetBoolean( | 107 owner_settings_service_->SetBoolean(chromeos::kHeartbeatEnabled, false); |
129 chromeos::kHeartbeatEnabled, false); | |
130 ASSERT_TRUE(task_runner_->GetPendingTasks().empty()); | 108 ASSERT_TRUE(task_runner_->GetPendingTasks().empty()); |
131 } | 109 } |
132 | 110 |
133 TEST_F(HeartbeatSchedulerTest, PermanentlyFailedGCMRegistration) { | 111 TEST_F(HeartbeatSchedulerTest, PermanentlyFailedGCMRegistration) { |
134 // If heartbeats are enabled, we should register with GCMDriver. | 112 // If heartbeats are enabled, we should register with GCMDriver. |
135 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 113 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
136 chromeos::CrosSettings::Get()->SetBoolean( | 114 owner_settings_service_->SetBoolean(chromeos::kHeartbeatEnabled, true); |
137 chromeos::kHeartbeatEnabled, true); | |
138 gcm_driver_.CompleteRegistration( | 115 gcm_driver_.CompleteRegistration( |
139 kHeartbeatGCMAppID, gcm::GCMClient::GCM_DISABLED); | 116 kHeartbeatGCMAppID, gcm::GCMClient::GCM_DISABLED); |
140 | 117 |
141 // There should be no heartbeat tasks pending, because registration failed. | 118 // There should be no heartbeat tasks pending, because registration failed. |
142 ASSERT_TRUE(task_runner_->GetPendingTasks().empty()); | 119 ASSERT_TRUE(task_runner_->GetPendingTasks().empty()); |
143 } | 120 } |
144 | 121 |
145 TEST_F(HeartbeatSchedulerTest, TemporarilyFailedGCMRegistration) { | 122 TEST_F(HeartbeatSchedulerTest, TemporarilyFailedGCMRegistration) { |
146 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 123 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
147 chromeos::CrosSettings::Get()->SetBoolean( | 124 owner_settings_service_->SetBoolean(chromeos::kHeartbeatEnabled, true); |
148 chromeos::kHeartbeatEnabled, true); | |
149 gcm_driver_.CompleteRegistration( | 125 gcm_driver_.CompleteRegistration( |
150 kHeartbeatGCMAppID, gcm::GCMClient::SERVER_ERROR); | 126 kHeartbeatGCMAppID, gcm::GCMClient::SERVER_ERROR); |
151 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | 127 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
152 | 128 |
153 // Should have a pending task to try registering again. | 129 // Should have a pending task to try registering again. |
154 ASSERT_FALSE(task_runner_->GetPendingTasks().empty()); | 130 ASSERT_FALSE(task_runner_->GetPendingTasks().empty()); |
155 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 131 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
156 task_runner_->RunPendingTasks(); | 132 task_runner_->RunPendingTasks(); |
157 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | 133 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
158 | 134 |
159 // Once we have successfully registered, we should send a heartbeat. | 135 // Once we have successfully registered, we should send a heartbeat. |
160 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)); | 136 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)); |
161 gcm_driver_.CompleteRegistration( | 137 gcm_driver_.CompleteRegistration( |
162 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 138 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
163 task_runner_->RunPendingTasks(); | 139 task_runner_->RunPendingTasks(); |
164 } | 140 } |
165 | 141 |
166 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) { | 142 TEST_F(HeartbeatSchedulerTest, ChangeHeartbeatFrequency) { |
167 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 143 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
168 chromeos::CrosSettings::Get()->SetBoolean( | 144 owner_settings_service_->SetBoolean(chromeos::kHeartbeatEnabled, true); |
169 chromeos::kHeartbeatEnabled, true); | |
170 gcm_driver_.CompleteRegistration( | 145 gcm_driver_.CompleteRegistration( |
171 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 146 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
172 | 147 |
173 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | 148 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
174 // Should have a heartbeat task posted with zero delay on startup. | 149 // Should have a heartbeat task posted with zero delay on startup. |
175 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay()); | 150 EXPECT_EQ(base::TimeDelta(), task_runner_->NextPendingTaskDelay()); |
176 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | 151 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
177 | 152 |
178 const int new_delay = 1234*1000; // 1234 seconds. | 153 const int new_delay = 1234*1000; // 1234 seconds. |
179 chromeos::CrosSettings::Get()->SetInteger(chromeos::kHeartbeatFrequency, | 154 owner_settings_service_->SetInteger(chromeos::kHeartbeatFrequency, new_delay); |
180 new_delay); | |
181 // Now run pending heartbeat task, should send a heartbeat. | 155 // Now run pending heartbeat task, should send a heartbeat. |
182 gcm::GCMClient::OutgoingMessage message; | 156 gcm::GCMClient::OutgoingMessage message; |
183 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) | 157 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) |
184 .WillOnce(SaveArg<2>(&message)); | 158 .WillOnce(SaveArg<2>(&message)); |
185 task_runner_->RunPendingTasks(); | 159 task_runner_->RunPendingTasks(); |
186 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | 160 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); |
187 | 161 |
188 // Complete sending a message - we should queue up the next heartbeat | 162 // Complete sending a message - we should queue up the next heartbeat |
189 // even if the previous attempt failed. | 163 // even if the previous attempt failed. |
190 gcm_driver_.CompleteSend( | 164 gcm_driver_.CompleteSend( |
191 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SERVER_ERROR); | 165 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SERVER_ERROR); |
192 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | 166 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
193 CheckPendingTaskDelay(scheduler_.last_heartbeat(), | 167 CheckPendingTaskDelay(scheduler_.last_heartbeat(), |
194 base::TimeDelta::FromMilliseconds(new_delay)); | 168 base::TimeDelta::FromMilliseconds(new_delay)); |
195 } | 169 } |
196 | 170 |
197 TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) { | 171 TEST_F(HeartbeatSchedulerTest, DisableHeartbeats) { |
198 // Makes sure that we can disable heartbeats on the fly. | 172 // Makes sure that we can disable heartbeats on the fly. |
199 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 173 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
200 chromeos::CrosSettings::Get()->SetBoolean( | 174 owner_settings_service_->SetBoolean(chromeos::kHeartbeatEnabled, true); |
201 chromeos::kHeartbeatEnabled, true); | |
202 gcm::GCMClient::OutgoingMessage message; | 175 gcm::GCMClient::OutgoingMessage message; |
203 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) | 176 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) |
204 .WillOnce(SaveArg<2>(&message)); | 177 .WillOnce(SaveArg<2>(&message)); |
205 gcm_driver_.CompleteRegistration( | 178 gcm_driver_.CompleteRegistration( |
206 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 179 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
207 // Should have a heartbeat task posted. | 180 // Should have a heartbeat task posted. |
208 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); | 181 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); |
209 task_runner_->RunPendingTasks(); | 182 task_runner_->RunPendingTasks(); |
210 | 183 |
211 // Complete sending a message - we should queue up the next heartbeat. | 184 // Complete sending a message - we should queue up the next heartbeat. |
212 gcm_driver_.CompleteSend( | 185 gcm_driver_.CompleteSend( |
213 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SUCCESS); | 186 kHeartbeatGCMAppID, message.id, gcm::GCMClient::SUCCESS); |
214 | 187 |
215 // Should have a new heartbeat task posted. | 188 // Should have a new heartbeat task posted. |
216 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size()); | 189 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size()); |
217 CheckPendingTaskDelay( | 190 CheckPendingTaskDelay( |
218 scheduler_.last_heartbeat(), | 191 scheduler_.last_heartbeat(), |
219 base::TimeDelta::FromMilliseconds( | 192 base::TimeDelta::FromMilliseconds( |
220 policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs)); | 193 policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs)); |
221 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); | 194 testing::Mock::VerifyAndClearExpectations(&gcm_driver_); |
222 | 195 |
223 // Now disable heartbeats. Should get no more heartbeats sent. | 196 // Now disable heartbeats. Should get no more heartbeats sent. |
224 chromeos::CrosSettings::Get()->SetBoolean( | 197 owner_settings_service_->SetBoolean(chromeos::kHeartbeatEnabled, false); |
225 chromeos::kHeartbeatEnabled, false); | 198 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
226 task_runner_->RunPendingTasks(); | 199 task_runner_->RunPendingTasks(); |
227 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | 200 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); |
228 } | 201 } |
229 | 202 |
230 TEST_F(HeartbeatSchedulerTest, CheckMessageContents) { | 203 TEST_F(HeartbeatSchedulerTest, CheckMessageContents) { |
231 gcm::GCMClient::OutgoingMessage message; | 204 gcm::GCMClient::OutgoingMessage message; |
232 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); | 205 EXPECT_CALL(gcm_driver_, RegisterImpl(kHeartbeatGCMAppID, _)); |
233 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) | 206 EXPECT_CALL(gcm_driver_, SendImpl(kHeartbeatGCMAppID, _, _)) |
234 .WillOnce(SaveArg<2>(&message)); | 207 .WillOnce(SaveArg<2>(&message)); |
235 chromeos::CrosSettings::Get()->SetBoolean( | 208 owner_settings_service_->SetBoolean(chromeos::kHeartbeatEnabled, true); |
236 chromeos::kHeartbeatEnabled, true); | |
237 gcm_driver_.CompleteRegistration( | 209 gcm_driver_.CompleteRegistration( |
238 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); | 210 kHeartbeatGCMAppID, gcm::GCMClient::SUCCESS); |
239 task_runner_->RunPendingTasks(); | 211 task_runner_->RunPendingTasks(); |
240 | 212 |
241 // Heartbeats should have a time-to-live equivalent to the heartbeat frequency | 213 // Heartbeats should have a time-to-live equivalent to the heartbeat frequency |
242 // so we don't have more than one heartbeat queued at a time. | 214 // so we don't have more than one heartbeat queued at a time. |
243 EXPECT_EQ(policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs/1000, | 215 EXPECT_EQ(policy::HeartbeatScheduler::kDefaultHeartbeatIntervalMs/1000, |
244 message.time_to_live); | 216 message.time_to_live); |
245 | 217 |
246 // Check the values in the message payload. | 218 // Check the values in the message payload. |
247 EXPECT_EQ("hb", message.data["type"]); | 219 EXPECT_EQ("hb", message.data["type"]); |
248 int64 timestamp; | 220 int64 timestamp; |
249 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); | 221 EXPECT_TRUE(base::StringToInt64(message.data["timestamp"], ×tamp)); |
250 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); | 222 EXPECT_EQ(kFakeEnrollmentDomain, message.data["domain_name"]); |
251 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); | 223 EXPECT_EQ(kFakeDeviceId, message.data["device_id"]); |
252 } | 224 } |
253 | 225 |
254 } // namespace | 226 } // namespace |
OLD | NEW |