OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/metrics/histogram_samples.h" | 13 #include "base/metrics/histogram_samples.h" |
14 #include "base/metrics/sample_map.h" | 14 #include "base/metrics/sample_map.h" |
15 #include "base/metrics/statistics_recorder.h" | 15 #include "base/metrics/statistics_recorder.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/test/simple_test_clock.h" | 17 #include "base/test/simple_test_clock.h" |
18 #include "base/test/test_simple_task_runner.h" | 18 #include "base/test/test_simple_task_runner.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/browser/invalidation/fake_invalidation_service.h" | 21 #include "chrome/browser/invalidation/fake_invalidation_service.h" |
22 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" | 22 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" |
| 23 #include "chrome/browser/policy/cloud/user_cloud_policy_invalidator.h" |
23 #include "components/invalidation/invalidation_util.h" | 24 #include "components/invalidation/invalidation_util.h" |
24 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 25 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
25 #include "components/policy/core/common/cloud/cloud_policy_core.h" | 26 #include "components/policy/core/common/cloud/cloud_policy_core.h" |
26 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" | 27 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" |
27 #include "components/policy/core/common/cloud/enterprise_metrics.h" | 28 #include "components/policy/core/common/cloud/enterprise_metrics.h" |
28 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | 29 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" |
29 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h" | 30 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h" |
30 #include "components/policy/core/common/policy_types.h" | 31 #include "components/policy/core/common/policy_types.h" |
31 #include "policy/policy_constants.h" | 32 #include "policy/policy_constants.h" |
32 #include "policy/proto/device_management_backend.pb.h" | |
33 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
35 | 35 |
| 36 namespace em = enterprise_management; |
| 37 |
36 namespace policy { | 38 namespace policy { |
37 | 39 |
38 class CloudPolicyInvalidatorTest : public testing::Test { | 40 class CloudPolicyInvalidatorTest : public testing::Test { |
39 protected: | 41 protected: |
40 // Policy objects which can be used in tests. | 42 // Policy objects which can be used in tests. |
41 enum PolicyObject { | 43 enum PolicyObject { |
42 POLICY_OBJECT_NONE, | 44 POLICY_OBJECT_NONE, |
43 POLICY_OBJECT_A, | 45 POLICY_OBJECT_A, |
44 POLICY_OBJECT_B | 46 POLICY_OBJECT_B |
45 }; | 47 }; |
46 | 48 |
47 CloudPolicyInvalidatorTest(); | 49 CloudPolicyInvalidatorTest(); |
48 | 50 |
49 virtual void SetUp() OVERRIDE; | |
50 | |
51 virtual void TearDown() OVERRIDE; | 51 virtual void TearDown() OVERRIDE; |
52 | 52 |
53 // Starts the invalidator which will be tested. | 53 // Starts the invalidator which will be tested. |
54 // |initialize| determines if the invalidator should be initialized. | 54 // |initialize| determines if the invalidator should be initialized. |
55 // |start_refresh_scheduler| determines if the refresh scheduler should start. | 55 // |start_refresh_scheduler| determines if the refresh scheduler should start. |
56 void StartInvalidator(bool initialize, bool start_refresh_scheduler); | 56 void StartInvalidator(bool initialize, bool start_refresh_scheduler); |
57 void StartInvalidator() { | 57 void StartInvalidator() { |
58 StartInvalidator(true /* initialize */, true /* start_refresh_scheduler */); | 58 StartInvalidator(true /* initialize */, true /* start_refresh_scheduler */); |
59 } | 59 } |
60 | 60 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 bool InvalidationsEnabled(); | 140 bool InvalidationsEnabled(); |
141 | 141 |
142 // Determines if the invalidation with the given ack handle has been | 142 // Determines if the invalidation with the given ack handle has been |
143 // acknowledged. | 143 // acknowledged. |
144 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation); | 144 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation); |
145 | 145 |
146 // Determines if the invalidator has registered for an object with the | 146 // Determines if the invalidator has registered for an object with the |
147 // invalidation service. | 147 // invalidation service. |
148 bool IsInvalidatorRegistered(); | 148 bool IsInvalidatorRegistered(); |
149 | 149 |
150 // Get the current count for the given metric. | |
151 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); | |
152 base::HistogramBase::Count GetInvalidationCount(PolicyInvalidationType type); | |
153 | |
154 // Advance the test clock. | 150 // Advance the test clock. |
155 void AdvanceClock(base::TimeDelta delta); | 151 void AdvanceClock(base::TimeDelta delta); |
156 | 152 |
157 // Get the current time on the test clock. | 153 // Get the current time on the test clock. |
158 base::Time Now(); | 154 base::Time Now(); |
159 | 155 |
160 // Translate a version number into an appropriate invalidation version (which | 156 // Translate a version number into an appropriate invalidation version (which |
161 // is based on the current time). | 157 // is based on the current time). |
162 int64 V(int version); | 158 int64 V(int version); |
163 | 159 |
164 // Get an invalidation version for the given time. | 160 // Get an invalidation version for the given time. |
165 int64 GetVersion(base::Time time); | 161 int64 GetVersion(base::Time time); |
166 | 162 |
| 163 // Get the policy type that the |invalidator_| is responsible for. |
| 164 virtual em::DeviceRegisterRequest::Type GetPolicyType() const; |
| 165 |
167 private: | 166 private: |
168 // Checks that the policy was refreshed due to an invalidation with the given | 167 // Checks that the policy was refreshed due to an invalidation with the given |
169 // base delay. | 168 // base delay. |
170 bool CheckPolicyRefreshed(base::TimeDelta delay); | 169 bool CheckPolicyRefreshed(base::TimeDelta delay); |
171 | 170 |
172 // Checks that the policy was refreshed the given number of times. | 171 // Checks that the policy was refreshed the given number of times. |
173 bool CheckPolicyRefreshCount(int count); | 172 bool CheckPolicyRefreshCount(int count); |
174 | 173 |
175 // Returns the object id of the given policy object. | 174 // Returns the object id of the given policy object. |
176 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; | 175 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; |
177 | 176 |
178 // Get histogram samples for the given histogram. | |
179 scoped_ptr<base::HistogramSamples> GetHistogramSamples( | |
180 const std::string& name) const; | |
181 | |
182 base::MessageLoop loop_; | 177 base::MessageLoop loop_; |
183 | 178 |
184 // Objects the invalidator depends on. | 179 // Objects the invalidator depends on. |
185 invalidation::FakeInvalidationService invalidation_service_; | 180 invalidation::FakeInvalidationService invalidation_service_; |
186 MockCloudPolicyStore store_; | 181 MockCloudPolicyStore store_; |
187 CloudPolicyCore core_; | 182 CloudPolicyCore core_; |
188 MockCloudPolicyClient* client_; | 183 MockCloudPolicyClient* client_; |
189 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 184 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
190 base::SimpleTestClock* clock_; | 185 base::SimpleTestClock* clock_; |
191 | 186 |
192 // The invalidator which will be tested. | 187 // The invalidator which will be tested. |
193 scoped_ptr<CloudPolicyInvalidator> invalidator_; | 188 scoped_ptr<CloudPolicyInvalidator> invalidator_; |
194 | 189 |
195 // Object ids for the test policy objects. | 190 // Object ids for the test policy objects. |
196 invalidation::ObjectId object_id_a_; | 191 invalidation::ObjectId object_id_a_; |
197 invalidation::ObjectId object_id_b_; | 192 invalidation::ObjectId object_id_b_; |
198 | 193 |
199 // Fake policy values which are alternated to cause the store to report a | 194 // Fake policy values which are alternated to cause the store to report a |
200 // changed policy. | 195 // changed policy. |
201 const char* policy_value_a_; | 196 const char* policy_value_a_; |
202 const char* policy_value_b_; | 197 const char* policy_value_b_; |
203 | 198 |
204 // The currently used policy value. | 199 // The currently used policy value. |
205 const char* policy_value_cur_; | 200 const char* policy_value_cur_; |
206 | |
207 // Stores starting histogram counts for kMetricPolicyRefresh. | |
208 scoped_ptr<base::HistogramSamples> refresh_samples_; | |
209 | |
210 // Stores starting histogram counts for kMetricPolicyInvalidations. | |
211 scoped_ptr<base::HistogramSamples> invalidations_samples_; | |
212 }; | 201 }; |
213 | 202 |
214 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() | 203 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() |
215 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, | 204 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, |
216 std::string()), | 205 std::string()), |
217 &store_, | 206 &store_, |
218 loop_.message_loop_proxy()), | 207 loop_.message_loop_proxy()), |
219 client_(NULL), | 208 client_(NULL), |
220 task_runner_(new base::TestSimpleTaskRunner()), | 209 task_runner_(new base::TestSimpleTaskRunner()), |
221 clock_(new base::SimpleTestClock()), | 210 clock_(new base::SimpleTestClock()), |
222 object_id_a_(135, "asdf"), | 211 object_id_a_(135, "asdf"), |
223 object_id_b_(246, "zxcv"), | 212 object_id_b_(246, "zxcv"), |
224 policy_value_a_("asdf"), | 213 policy_value_a_("asdf"), |
225 policy_value_b_("zxcv"), | 214 policy_value_b_("zxcv"), |
226 policy_value_cur_(policy_value_a_) { | 215 policy_value_cur_(policy_value_a_) { |
227 clock_->SetNow( | 216 clock_->SetNow( |
228 base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(987654321)); | 217 base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(987654321)); |
229 } | 218 } |
230 | 219 |
231 void CloudPolicyInvalidatorTest::SetUp() { | |
232 base::StatisticsRecorder::Initialize(); | |
233 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); | |
234 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); | |
235 } | |
236 | |
237 void CloudPolicyInvalidatorTest::TearDown() { | 220 void CloudPolicyInvalidatorTest::TearDown() { |
238 if (invalidator_) | 221 if (invalidator_) |
239 invalidator_->Shutdown(); | 222 invalidator_->Shutdown(); |
240 core_.Disconnect(); | 223 core_.Disconnect(); |
241 } | 224 } |
242 | 225 |
243 void CloudPolicyInvalidatorTest::StartInvalidator( | 226 void CloudPolicyInvalidatorTest::StartInvalidator( |
244 bool initialize, | 227 bool initialize, |
245 bool start_refresh_scheduler) { | 228 bool start_refresh_scheduler) { |
246 invalidator_.reset(new CloudPolicyInvalidator( | 229 invalidator_.reset(new CloudPolicyInvalidator( |
| 230 GetPolicyType(), |
247 &core_, | 231 &core_, |
248 task_runner_, | 232 task_runner_, |
249 scoped_ptr<base::Clock>(clock_))); | 233 scoped_ptr<base::Clock>(clock_))); |
250 if (start_refresh_scheduler) { | 234 if (start_refresh_scheduler) { |
251 ConnectCore(); | 235 ConnectCore(); |
252 StartRefreshScheduler(); | 236 StartRefreshScheduler(); |
253 } | 237 } |
254 if (initialize) | 238 if (initialize) |
255 InitializeInvalidator(); | 239 InitializeInvalidator(); |
256 } | 240 } |
(...skipping 23 matching lines...) Expand all Loading... |
280 void CloudPolicyInvalidatorTest::DisconnectCore() { | 264 void CloudPolicyInvalidatorTest::DisconnectCore() { |
281 client_ = NULL; | 265 client_ = NULL; |
282 core_.Disconnect(); | 266 core_.Disconnect(); |
283 } | 267 } |
284 | 268 |
285 void CloudPolicyInvalidatorTest::StorePolicy( | 269 void CloudPolicyInvalidatorTest::StorePolicy( |
286 PolicyObject object, | 270 PolicyObject object, |
287 int64 invalidation_version, | 271 int64 invalidation_version, |
288 bool policy_changed, | 272 bool policy_changed, |
289 const base::Time& time) { | 273 const base::Time& time) { |
290 enterprise_management::PolicyData* data = | 274 em::PolicyData* data = new em::PolicyData(); |
291 new enterprise_management::PolicyData(); | |
292 if (object != POLICY_OBJECT_NONE) { | 275 if (object != POLICY_OBJECT_NONE) { |
293 data->set_invalidation_source(GetPolicyObjectId(object).source()); | 276 data->set_invalidation_source(GetPolicyObjectId(object).source()); |
294 data->set_invalidation_name(GetPolicyObjectId(object).name()); | 277 data->set_invalidation_name(GetPolicyObjectId(object).name()); |
295 } | 278 } |
296 data->set_timestamp((time - base::Time::UnixEpoch()).InMilliseconds()); | 279 data->set_timestamp((time - base::Time::UnixEpoch()).InMilliseconds()); |
297 // Swap the policy value if a policy change is desired. | 280 // Swap the policy value if a policy change is desired. |
298 if (policy_changed) | 281 if (policy_changed) |
299 policy_value_cur_ = policy_value_cur_ == policy_value_a_ ? | 282 policy_value_cur_ = policy_value_cur_ == policy_value_a_ ? |
300 policy_value_b_ : policy_value_a_; | 283 policy_value_b_ : policy_value_a_; |
301 data->set_policy_value(policy_value_cur_); | 284 data->set_policy_value(policy_value_cur_); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 | 364 |
382 EXPECT_FALSE(IsUnsent(invalidation)); | 365 EXPECT_FALSE(IsUnsent(invalidation)); |
383 return !invalidation_service_.GetMockAckHandler()->IsUnacked(invalidation); | 366 return !invalidation_service_.GetMockAckHandler()->IsUnacked(invalidation); |
384 } | 367 } |
385 | 368 |
386 bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() { | 369 bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() { |
387 return !invalidation_service_.invalidator_registrar() | 370 return !invalidation_service_.invalidator_registrar() |
388 .GetRegisteredIds(invalidator_.get()).empty(); | 371 .GetRegisteredIds(invalidator_.get()).empty(); |
389 } | 372 } |
390 | 373 |
391 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount( | |
392 MetricPolicyRefresh metric) { | |
393 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) - | |
394 refresh_samples_->GetCount(metric); | |
395 } | |
396 | |
397 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( | |
398 PolicyInvalidationType type) { | |
399 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(type) - | |
400 invalidations_samples_->GetCount(type); | |
401 } | |
402 | |
403 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) { | 374 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) { |
404 clock_->Advance(delta); | 375 clock_->Advance(delta); |
405 } | 376 } |
406 | 377 |
407 base::Time CloudPolicyInvalidatorTest::Now() { | 378 base::Time CloudPolicyInvalidatorTest::Now() { |
408 return clock_->Now(); | 379 return clock_->Now(); |
409 } | 380 } |
410 | 381 |
411 int64 CloudPolicyInvalidatorTest::V(int version) { | 382 int64 CloudPolicyInvalidatorTest::V(int version) { |
412 return GetVersion(Now()) + version; | 383 return GetVersion(Now()) + version; |
413 } | 384 } |
414 | 385 |
415 int64 CloudPolicyInvalidatorTest::GetVersion(base::Time time) { | 386 int64 CloudPolicyInvalidatorTest::GetVersion(base::Time time) { |
416 return (time - base::Time::UnixEpoch()).InMicroseconds(); | 387 return (time - base::Time::UnixEpoch()).InMicroseconds(); |
417 } | 388 } |
418 | 389 |
| 390 em::DeviceRegisterRequest::Type |
| 391 CloudPolicyInvalidatorTest::GetPolicyType() const { |
| 392 return UserCloudPolicyInvalidator::GetPolicyType(); |
| 393 } |
| 394 |
419 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) { | 395 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) { |
420 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds( | 396 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds( |
421 CloudPolicyInvalidator::kMaxFetchDelayMin); | 397 CloudPolicyInvalidator::kMaxFetchDelayMin); |
422 | 398 |
423 if (task_runner_->GetPendingTasks().empty()) | 399 if (task_runner_->GetPendingTasks().empty()) |
424 return false; | 400 return false; |
425 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; | 401 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; |
426 EXPECT_GE(actual_delay, delay); | 402 EXPECT_GE(actual_delay, delay); |
427 EXPECT_LE(actual_delay, max_delay); | 403 EXPECT_LE(actual_delay, max_delay); |
428 | 404 |
(...skipping 17 matching lines...) Expand all Loading... |
446 base::RunLoop().RunUntilIdle(); | 422 base::RunLoop().RunUntilIdle(); |
447 return testing::Mock::VerifyAndClearExpectations(client_); | 423 return testing::Mock::VerifyAndClearExpectations(client_); |
448 } | 424 } |
449 | 425 |
450 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId( | 426 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId( |
451 PolicyObject object) const { | 427 PolicyObject object) const { |
452 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B); | 428 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B); |
453 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_; | 429 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_; |
454 } | 430 } |
455 | 431 |
456 scoped_ptr<base::HistogramSamples> | |
457 CloudPolicyInvalidatorTest::GetHistogramSamples( | |
458 const std::string& name) const { | |
459 base::HistogramBase* histogram = | |
460 base::StatisticsRecorder::FindHistogram(name); | |
461 if (!histogram) | |
462 return scoped_ptr<base::HistogramSamples>(new base::SampleMap()); | |
463 return histogram->SnapshotSamples(); | |
464 } | |
465 | |
466 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) { | 432 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) { |
467 // No invalidations should be processed if the invalidator is not initialized. | 433 // No invalidations should be processed if the invalidator is not initialized. |
468 StartInvalidator(false /* initialize */, true /* start_refresh_scheduler */); | 434 StartInvalidator(false /* initialize */, true /* start_refresh_scheduler */); |
469 StorePolicy(POLICY_OBJECT_A); | 435 StorePolicy(POLICY_OBJECT_A); |
470 EXPECT_FALSE(IsInvalidatorRegistered()); | 436 EXPECT_FALSE(IsInvalidatorRegistered()); |
471 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A))); | 437 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A))); |
472 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 438 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
473 } | 439 } |
474 | 440 |
475 TEST_F(CloudPolicyInvalidatorTest, RefreshSchedulerNotStarted) { | 441 TEST_F(CloudPolicyInvalidatorTest, RefreshSchedulerNotStarted) { |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 StartRefreshScheduler(); | 754 StartRefreshScheduler(); |
789 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 755 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
790 EXPECT_TRUE(InvalidationsEnabled()); | 756 EXPECT_TRUE(InvalidationsEnabled()); |
791 FireInvalidation(POLICY_OBJECT_A, V(5), "test"); | 757 FireInvalidation(POLICY_OBJECT_A, V(5), "test"); |
792 EXPECT_TRUE(CheckInvalidationInfo(V(5), "test")); | 758 EXPECT_TRUE(CheckInvalidationInfo(V(5), "test")); |
793 EXPECT_TRUE(CheckPolicyRefreshed()); | 759 EXPECT_TRUE(CheckPolicyRefreshed()); |
794 DisableInvalidationService(); | 760 DisableInvalidationService(); |
795 EXPECT_FALSE(InvalidationsEnabled()); | 761 EXPECT_FALSE(InvalidationsEnabled()); |
796 } | 762 } |
797 | 763 |
798 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { | 764 class CloudPolicyInvalidatorUserTypedTest |
| 765 : public CloudPolicyInvalidatorTest, |
| 766 public testing::WithParamInterface<em::DeviceRegisterRequest::Type> { |
| 767 protected: |
| 768 CloudPolicyInvalidatorUserTypedTest(); |
| 769 virtual ~CloudPolicyInvalidatorUserTypedTest(); |
| 770 |
| 771 // CloudPolicyInvalidatorTest: |
| 772 virtual void SetUp() OVERRIDE; |
| 773 |
| 774 // Get the current count for the given metric. |
| 775 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); |
| 776 base::HistogramBase::Count GetInvalidationCount(PolicyInvalidationType type); |
| 777 |
| 778 private: |
| 779 // CloudPolicyInvalidatorTest: |
| 780 virtual em::DeviceRegisterRequest::Type GetPolicyType() const OVERRIDE; |
| 781 |
| 782 // Get histogram samples for the given histogram. |
| 783 scoped_ptr<base::HistogramSamples> GetHistogramSamples( |
| 784 const std::string& name) const; |
| 785 |
| 786 // Stores starting histogram counts for kMetricPolicyRefresh. |
| 787 scoped_ptr<base::HistogramSamples> refresh_samples_; |
| 788 |
| 789 // Stores starting histogram counts for kMetricPolicyInvalidations. |
| 790 scoped_ptr<base::HistogramSamples> invalidations_samples_; |
| 791 |
| 792 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidatorUserTypedTest); |
| 793 }; |
| 794 |
| 795 CloudPolicyInvalidatorUserTypedTest::CloudPolicyInvalidatorUserTypedTest() { |
| 796 } |
| 797 |
| 798 CloudPolicyInvalidatorUserTypedTest::~CloudPolicyInvalidatorUserTypedTest() { |
| 799 } |
| 800 |
| 801 void CloudPolicyInvalidatorUserTypedTest::SetUp() { |
| 802 base::StatisticsRecorder::Initialize(); |
| 803 refresh_samples_ = GetHistogramSamples( |
| 804 GetPolicyType() == em::DeviceRegisterRequest::DEVICE ? |
| 805 kMetricDevicePolicyRefresh : kMetricUserPolicyRefresh); |
| 806 invalidations_samples_ = GetHistogramSamples( |
| 807 GetPolicyType() == em::DeviceRegisterRequest::DEVICE ? |
| 808 kMetricDevicePolicyInvalidations : kMetricUserPolicyInvalidations); |
| 809 } |
| 810 |
| 811 base::HistogramBase::Count CloudPolicyInvalidatorUserTypedTest::GetCount( |
| 812 MetricPolicyRefresh metric) { |
| 813 return GetHistogramSamples( |
| 814 GetPolicyType() == em::DeviceRegisterRequest::DEVICE ? |
| 815 kMetricDevicePolicyRefresh : kMetricUserPolicyRefresh)-> |
| 816 GetCount(metric) - refresh_samples_->GetCount(metric); |
| 817 } |
| 818 |
| 819 base::HistogramBase::Count |
| 820 CloudPolicyInvalidatorUserTypedTest::GetInvalidationCount( |
| 821 PolicyInvalidationType type) { |
| 822 return GetHistogramSamples( |
| 823 GetPolicyType() == em::DeviceRegisterRequest::DEVICE ? |
| 824 kMetricDevicePolicyInvalidations : kMetricUserPolicyInvalidations)-> |
| 825 GetCount(type) - invalidations_samples_->GetCount(type); |
| 826 } |
| 827 |
| 828 em::DeviceRegisterRequest::Type |
| 829 CloudPolicyInvalidatorUserTypedTest::GetPolicyType() const { |
| 830 return GetParam(); |
| 831 } |
| 832 |
| 833 scoped_ptr<base::HistogramSamples> |
| 834 CloudPolicyInvalidatorUserTypedTest::GetHistogramSamples( |
| 835 const std::string& name) const { |
| 836 base::HistogramBase* histogram = |
| 837 base::StatisticsRecorder::FindHistogram(name); |
| 838 if (!histogram) |
| 839 return scoped_ptr<base::HistogramSamples>(new base::SampleMap()); |
| 840 return histogram->SnapshotSamples(); |
| 841 } |
| 842 |
| 843 TEST_P(CloudPolicyInvalidatorUserTypedTest, RefreshMetricsUnregistered) { |
799 // Store loads occurring before invalidation registration are not counted. | 844 // Store loads occurring before invalidation registration are not counted. |
800 StartInvalidator(); | 845 StartInvalidator(); |
801 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); | 846 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); |
802 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); | 847 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); |
803 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED)); | 848 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED)); |
804 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); | 849 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
805 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); | 850 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); |
806 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); | 851 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); |
807 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | 852 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); |
808 } | 853 } |
809 | 854 |
810 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsNoInvalidations) { | 855 TEST_P(CloudPolicyInvalidatorUserTypedTest, RefreshMetricsNoInvalidations) { |
811 // Store loads occurring while registered should be differentiated depending | 856 // Store loads occurring while registered should be differentiated depending |
812 // on whether the invalidation service was enabled or not. | 857 // on whether the invalidation service was enabled or not. |
813 StorePolicy(POLICY_OBJECT_A); | 858 StorePolicy(POLICY_OBJECT_A); |
814 StartInvalidator(); | 859 StartInvalidator(); |
815 | 860 |
816 // Initially, invalidations have not been enabled past the grace period, so | 861 // Initially, invalidations have not been enabled past the grace period, so |
817 // invalidations are OFF. | 862 // invalidations are OFF. |
818 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 863 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
819 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 864 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
820 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); | 865 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
(...skipping 30 matching lines...) Expand all Loading... |
851 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 896 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
852 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 897 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
853 | 898 |
854 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED)); | 899 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED)); |
855 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); | 900 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
856 EXPECT_EQ(6, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); | 901 EXPECT_EQ(6, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); |
857 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); | 902 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); |
858 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | 903 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); |
859 } | 904 } |
860 | 905 |
861 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) { | 906 TEST_P(CloudPolicyInvalidatorUserTypedTest, RefreshMetricsInvalidation) { |
862 // Store loads after an invalidation are counted as invalidated, even if | 907 // Store loads after an invalidation are counted as invalidated, even if |
863 // the loads do not result in the invalidation being acknowledged. | 908 // the loads do not result in the invalidation being acknowledged. |
864 StartInvalidator(); | 909 StartInvalidator(); |
865 StorePolicy(POLICY_OBJECT_A); | 910 StorePolicy(POLICY_OBJECT_A); |
866 AdvanceClock(base::TimeDelta::FromSeconds( | 911 AdvanceClock(base::TimeDelta::FromSeconds( |
867 CloudPolicyInvalidator::kInvalidationGracePeriod)); | 912 CloudPolicyInvalidator::kInvalidationGracePeriod)); |
868 FireInvalidation(POLICY_OBJECT_A, V(5), "test"); | 913 FireInvalidation(POLICY_OBJECT_A, V(5), "test"); |
869 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 914 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
870 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 915 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
871 StorePolicy(POLICY_OBJECT_A, V(5), true /* policy_changed */); | 916 StorePolicy(POLICY_OBJECT_A, V(5), true /* policy_changed */); |
872 | 917 |
873 // Store loads after the invalidation is complete are not counted as | 918 // Store loads after the invalidation is complete are not counted as |
874 // invalidated. | 919 // invalidated. |
875 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 920 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
876 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 921 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
877 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 922 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
878 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 923 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
879 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 924 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
880 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 925 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
881 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 926 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
882 | 927 |
883 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED)); | 928 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED)); |
884 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); | 929 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
885 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); | 930 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); |
886 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); | 931 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); |
887 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | 932 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); |
888 } | 933 } |
889 | 934 |
890 TEST_F(CloudPolicyInvalidatorTest, ExpiredInvalidations) { | 935 TEST_P(CloudPolicyInvalidatorUserTypedTest, ExpiredInvalidations) { |
891 StorePolicy(POLICY_OBJECT_A, 0, false, Now()); | 936 StorePolicy(POLICY_OBJECT_A, 0, false, Now()); |
892 StartInvalidator(); | 937 StartInvalidator(); |
893 | 938 |
894 // Invalidations fired before the last fetch time (adjusted by max time delta) | 939 // Invalidations fired before the last fetch time (adjusted by max time delta) |
895 // should be ignored. | 940 // should be ignored. |
896 base::Time time = Now() - base::TimeDelta::FromSeconds( | 941 base::Time time = Now() - base::TimeDelta::FromSeconds( |
897 CloudPolicyInvalidator::kMaxInvalidationTimeDelta + 300); | 942 CloudPolicyInvalidator::kMaxInvalidationTimeDelta + 300); |
898 syncer::Invalidation inv = | 943 syncer::Invalidation inv = |
899 FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test"); | 944 FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test"); |
900 ASSERT_TRUE(IsInvalidationAcknowledged(inv)); | 945 ASSERT_TRUE(IsInvalidationAcknowledged(inv)); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
941 ASSERT_TRUE(CheckPolicyRefreshedWithUnknownVersion()); | 986 ASSERT_TRUE(CheckPolicyRefreshedWithUnknownVersion()); |
942 | 987 |
943 // Verify that received invalidations metrics are correct. | 988 // Verify that received invalidations metrics are correct. |
944 EXPECT_EQ(1, GetInvalidationCount(POLICY_INVALIDATION_TYPE_NO_PAYLOAD)); | 989 EXPECT_EQ(1, GetInvalidationCount(POLICY_INVALIDATION_TYPE_NO_PAYLOAD)); |
945 EXPECT_EQ(3, GetInvalidationCount(POLICY_INVALIDATION_TYPE_NORMAL)); | 990 EXPECT_EQ(3, GetInvalidationCount(POLICY_INVALIDATION_TYPE_NORMAL)); |
946 EXPECT_EQ(2, | 991 EXPECT_EQ(2, |
947 GetInvalidationCount(POLICY_INVALIDATION_TYPE_NO_PAYLOAD_EXPIRED)); | 992 GetInvalidationCount(POLICY_INVALIDATION_TYPE_NO_PAYLOAD_EXPIRED)); |
948 EXPECT_EQ(2, GetInvalidationCount(POLICY_INVALIDATION_TYPE_EXPIRED)); | 993 EXPECT_EQ(2, GetInvalidationCount(POLICY_INVALIDATION_TYPE_EXPIRED)); |
949 } | 994 } |
950 | 995 |
| 996 #if defined(OS_CHROMEOS) |
| 997 INSTANTIATE_TEST_CASE_P( |
| 998 CloudPolicyInvalidatorUserTypedTestInstance, |
| 999 CloudPolicyInvalidatorUserTypedTest, |
| 1000 testing::Values(em::DeviceRegisterRequest::USER, |
| 1001 em::DeviceRegisterRequest::DEVICE)); |
| 1002 #elif defined(OS_ANDROID) |
| 1003 INSTANTIATE_TEST_CASE_P( |
| 1004 CloudPolicyInvalidatorUserTypedTestInstance, |
| 1005 CloudPolicyInvalidatorUserTypedTest, |
| 1006 testing::Values(em::DeviceRegisterRequest::ANDROID_BROWSER)); |
| 1007 #elif defined(OS_IOS) |
| 1008 INSTANTIATE_TEST_CASE_P( |
| 1009 CloudPolicyInvalidatorUserTypedTestInstance, |
| 1010 CloudPolicyInvalidatorUserTypedTest, |
| 1011 testing::Values(em::DeviceRegisterRequest::IOS_BROWSER)); |
| 1012 #else |
| 1013 INSTANTIATE_TEST_CASE_P( |
| 1014 CloudPolicyInvalidatorUserTypedTestInstance, |
| 1015 CloudPolicyInvalidatorUserTypedTest, |
| 1016 testing::Values(em::DeviceRegisterRequest::BROWSER)); |
| 1017 #endif |
| 1018 |
951 } // namespace policy | 1019 } // namespace policy |
OLD | NEW |