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" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 | 76 |
77 // Disconnects the cloud policy core. | 77 // Disconnects the cloud policy core. |
78 void DisconnectCore(); | 78 void DisconnectCore(); |
79 | 79 |
80 // Simulates storing a new policy to the policy store. | 80 // Simulates storing a new policy to the policy store. |
81 // |object| determines which policy object the store will report the | 81 // |object| determines which policy object the store will report the |
82 // invalidator should register for. May be POLICY_OBJECT_NONE for no object. | 82 // invalidator should register for. May be POLICY_OBJECT_NONE for no object. |
83 // |invalidation_version| determines what invalidation the store will report. | 83 // |invalidation_version| determines what invalidation the store will report. |
84 // |policy_changed| determines whether a policy value different from the | 84 // |policy_changed| determines whether a policy value different from the |
85 // current value will be stored. | 85 // current value will be stored. |
86 // |timestamp| determines the response timestamp the store will report. | 86 // |time| determines the timestamp the store will report. |
87 void StorePolicy( | 87 void StorePolicy( |
88 PolicyObject object, | 88 PolicyObject object, |
89 int64 invalidation_version, | 89 int64 invalidation_version, |
90 bool policy_changed, | 90 bool policy_changed, |
91 int64 timestamp); | 91 base::Time time); |
Joao da Silva
2014/04/08 12:31:59
Pass by const&
Steve Condie
2014/04/08 18:09:39
Done.
| |
92 void StorePolicy( | 92 void StorePolicy( |
93 PolicyObject object, | 93 PolicyObject object, |
94 int64 invalidation_version, | 94 int64 invalidation_version, |
95 bool policy_changed) { | 95 bool policy_changed) { |
96 StorePolicy(object, invalidation_version, policy_changed, ++timestamp_); | 96 StorePolicy(object, |
97 invalidation_version, | |
98 policy_changed, | |
99 Now() - base::TimeDelta::FromMinutes(5)); | |
97 } | 100 } |
98 void StorePolicy(PolicyObject object, int64 invalidation_version) { | 101 void StorePolicy(PolicyObject object, int64 invalidation_version) { |
99 StorePolicy(object, invalidation_version, false); | 102 StorePolicy(object, invalidation_version, false); |
100 } | 103 } |
101 void StorePolicy(PolicyObject object) { | 104 void StorePolicy(PolicyObject object) { |
102 StorePolicy(object, 0); | 105 StorePolicy(object, 0); |
103 } | 106 } |
104 | 107 |
105 // Disables the invalidation service. It is enabled by default. | 108 // Disables the invalidation service. It is enabled by default. |
106 void DisableInvalidationService(); | 109 void DisableInvalidationService(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 // Determines if the invalidation with the given ack handle has been | 142 // Determines if the invalidation with the given ack handle has been |
140 // acknowledged. | 143 // acknowledged. |
141 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation); | 144 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation); |
142 | 145 |
143 // Determines if the invalidator has registered for an object with the | 146 // Determines if the invalidator has registered for an object with the |
144 // invalidation service. | 147 // invalidation service. |
145 bool IsInvalidatorRegistered(); | 148 bool IsInvalidatorRegistered(); |
146 | 149 |
147 // Get the current count for the given metric. | 150 // Get the current count for the given metric. |
148 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); | 151 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); |
149 base::HistogramBase::Count GetInvalidationCount(bool with_payload); | 152 base::HistogramBase::Count GetInvalidationCount(PolicyInvalidationType type); |
150 | 153 |
151 // Advance the test clock. | 154 // Advance the test clock. |
152 void AdvanceClock(base::TimeDelta delta); | 155 void AdvanceClock(base::TimeDelta delta); |
153 | 156 |
157 // Get the current time on the test clock. | |
158 base::Time Now(); | |
159 | |
160 // Translate a version number into an appropriate invalidation version (which | |
161 // is based on the current time). | |
162 int64 V(int version); | |
163 | |
164 // Get an invalidation version for the given time. | |
165 int64 GetVersion(base::Time time); | |
166 | |
154 private: | 167 private: |
155 // Checks that the policy was refreshed due to an invalidation with the given | 168 // Checks that the policy was refreshed due to an invalidation with the given |
156 // base delay. | 169 // base delay. |
157 bool CheckPolicyRefreshed(base::TimeDelta delay); | 170 bool CheckPolicyRefreshed(base::TimeDelta delay); |
158 | 171 |
159 // Checks that the policy was refreshed the given number of times. | 172 // Checks that the policy was refreshed the given number of times. |
160 bool CheckPolicyRefreshCount(int count); | 173 bool CheckPolicyRefreshCount(int count); |
161 | 174 |
162 // Returns the object id of the given policy object. | 175 // Returns the object id of the given policy object. |
163 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; | 176 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; |
(...skipping 12 matching lines...) Expand all Loading... | |
176 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 189 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
177 base::SimpleTestClock* clock_; | 190 base::SimpleTestClock* clock_; |
178 | 191 |
179 // The invalidator which will be tested. | 192 // The invalidator which will be tested. |
180 scoped_ptr<CloudPolicyInvalidator> invalidator_; | 193 scoped_ptr<CloudPolicyInvalidator> invalidator_; |
181 | 194 |
182 // Object ids for the test policy objects. | 195 // Object ids for the test policy objects. |
183 invalidation::ObjectId object_id_a_; | 196 invalidation::ObjectId object_id_a_; |
184 invalidation::ObjectId object_id_b_; | 197 invalidation::ObjectId object_id_b_; |
185 | 198 |
186 // Increasing policy timestamp. | |
187 int64 timestamp_; | |
188 | |
189 // Fake policy values which are alternated to cause the store to report a | 199 // Fake policy values which are alternated to cause the store to report a |
190 // changed policy. | 200 // changed policy. |
191 const char* policy_value_a_; | 201 const char* policy_value_a_; |
192 const char* policy_value_b_; | 202 const char* policy_value_b_; |
193 | 203 |
194 // The currently used policy value. | 204 // The currently used policy value. |
195 const char* policy_value_cur_; | 205 const char* policy_value_cur_; |
196 | 206 |
197 // Stores starting histogram counts for kMetricPolicyRefresh. | 207 // Stores starting histogram counts for kMetricPolicyRefresh. |
198 scoped_ptr<base::HistogramSamples> refresh_samples_; | 208 scoped_ptr<base::HistogramSamples> refresh_samples_; |
199 | 209 |
200 // Stores starting histogram counts for kMetricPolicyInvalidations. | 210 // Stores starting histogram counts for kMetricPolicyInvalidations. |
201 scoped_ptr<base::HistogramSamples> invalidations_samples_; | 211 scoped_ptr<base::HistogramSamples> invalidations_samples_; |
202 }; | 212 }; |
203 | 213 |
204 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() | 214 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() |
205 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, | 215 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, |
206 std::string()), | 216 std::string()), |
207 &store_, | 217 &store_, |
208 loop_.message_loop_proxy()), | 218 loop_.message_loop_proxy()), |
209 client_(NULL), | 219 client_(NULL), |
210 task_runner_(new base::TestSimpleTaskRunner()), | 220 task_runner_(new base::TestSimpleTaskRunner()), |
211 clock_(new base::SimpleTestClock()), | 221 clock_(new base::SimpleTestClock()), |
212 object_id_a_(135, "asdf"), | 222 object_id_a_(135, "asdf"), |
213 object_id_b_(246, "zxcv"), | 223 object_id_b_(246, "zxcv"), |
214 timestamp_(123456), | |
215 policy_value_a_("asdf"), | 224 policy_value_a_("asdf"), |
216 policy_value_b_("zxcv"), | 225 policy_value_b_("zxcv"), |
217 policy_value_cur_(policy_value_a_) {} | 226 policy_value_cur_(policy_value_a_) { |
227 clock_->SetNow( | |
228 base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(987654321)); | |
229 } | |
218 | 230 |
219 void CloudPolicyInvalidatorTest::SetUp() { | 231 void CloudPolicyInvalidatorTest::SetUp() { |
220 base::StatisticsRecorder::Initialize(); | 232 base::StatisticsRecorder::Initialize(); |
221 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); | 233 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); |
222 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); | 234 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); |
223 } | 235 } |
224 | 236 |
225 void CloudPolicyInvalidatorTest::TearDown() { | 237 void CloudPolicyInvalidatorTest::TearDown() { |
226 if (invalidator_) | 238 if (invalidator_) |
227 invalidator_->Shutdown(); | 239 invalidator_->Shutdown(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 | 279 |
268 void CloudPolicyInvalidatorTest::DisconnectCore() { | 280 void CloudPolicyInvalidatorTest::DisconnectCore() { |
269 client_ = NULL; | 281 client_ = NULL; |
270 core_.Disconnect(); | 282 core_.Disconnect(); |
271 } | 283 } |
272 | 284 |
273 void CloudPolicyInvalidatorTest::StorePolicy( | 285 void CloudPolicyInvalidatorTest::StorePolicy( |
274 PolicyObject object, | 286 PolicyObject object, |
275 int64 invalidation_version, | 287 int64 invalidation_version, |
276 bool policy_changed, | 288 bool policy_changed, |
277 int64 timestamp) { | 289 base::Time time) { |
278 enterprise_management::PolicyData* data = | 290 enterprise_management::PolicyData* data = |
279 new enterprise_management::PolicyData(); | 291 new enterprise_management::PolicyData(); |
280 if (object != POLICY_OBJECT_NONE) { | 292 if (object != POLICY_OBJECT_NONE) { |
281 data->set_invalidation_source(GetPolicyObjectId(object).source()); | 293 data->set_invalidation_source(GetPolicyObjectId(object).source()); |
282 data->set_invalidation_name(GetPolicyObjectId(object).name()); | 294 data->set_invalidation_name(GetPolicyObjectId(object).name()); |
283 } | 295 } |
284 data->set_timestamp(timestamp); | 296 data->set_timestamp((time - base::Time::UnixEpoch()).InMilliseconds()); |
285 // Swap the policy value if a policy change is desired. | 297 // Swap the policy value if a policy change is desired. |
286 if (policy_changed) | 298 if (policy_changed) |
287 policy_value_cur_ = policy_value_cur_ == policy_value_a_ ? | 299 policy_value_cur_ = policy_value_cur_ == policy_value_a_ ? |
288 policy_value_b_ : policy_value_a_; | 300 policy_value_b_ : policy_value_a_; |
289 data->set_policy_value(policy_value_cur_); | 301 data->set_policy_value(policy_value_cur_); |
290 store_.invalidation_version_ = invalidation_version; | 302 store_.invalidation_version_ = invalidation_version; |
291 store_.policy_.reset(data); | 303 store_.policy_.reset(data); |
292 base::DictionaryValue policies; | 304 base::DictionaryValue policies; |
293 policies.SetInteger( | 305 policies.SetInteger( |
294 key::kMaxInvalidationFetchDelay, | 306 key::kMaxInvalidationFetchDelay, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 .GetRegisteredIds(invalidator_.get()).empty(); | 388 .GetRegisteredIds(invalidator_.get()).empty(); |
377 } | 389 } |
378 | 390 |
379 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount( | 391 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount( |
380 MetricPolicyRefresh metric) { | 392 MetricPolicyRefresh metric) { |
381 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) - | 393 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) - |
382 refresh_samples_->GetCount(metric); | 394 refresh_samples_->GetCount(metric); |
383 } | 395 } |
384 | 396 |
385 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( | 397 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( |
386 bool with_payload) { | 398 PolicyInvalidationType type) { |
387 int metric = with_payload ? 1 : 0; | 399 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(type) - |
388 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) - | 400 invalidations_samples_->GetCount(type); |
389 invalidations_samples_->GetCount(metric); | |
390 } | 401 } |
391 | 402 |
392 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) { | 403 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) { |
393 clock_->Advance(delta); | 404 clock_->Advance(delta); |
394 } | 405 } |
395 | 406 |
407 base::Time CloudPolicyInvalidatorTest::Now() { | |
408 return clock_->Now(); | |
409 } | |
410 | |
411 int64 CloudPolicyInvalidatorTest::V(int version) { | |
412 return GetVersion(Now()) + version; | |
413 } | |
414 | |
415 int64 CloudPolicyInvalidatorTest::GetVersion(base::Time time) { | |
416 return (time - base::Time::UnixEpoch()).InMicroseconds(); | |
417 } | |
418 | |
396 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) { | 419 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) { |
397 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds( | 420 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds( |
398 CloudPolicyInvalidator::kMaxFetchDelayMin); | 421 CloudPolicyInvalidator::kMaxFetchDelayMin); |
399 | 422 |
400 if (task_runner_->GetPendingTasks().empty()) | 423 if (task_runner_->GetPendingTasks().empty()) |
401 return false; | 424 return false; |
402 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; | 425 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; |
403 EXPECT_GE(actual_delay, delay); | 426 EXPECT_GE(actual_delay, delay); |
404 EXPECT_LE(actual_delay, max_delay); | 427 EXPECT_LE(actual_delay, max_delay); |
405 | 428 |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 FireUnknownVersionInvalidation(POLICY_OBJECT_B); | 591 FireUnknownVersionInvalidation(POLICY_OBJECT_B); |
569 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion()); | 592 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion()); |
570 } | 593 } |
571 | 594 |
572 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) { | 595 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) { |
573 // Register and fire invalidation | 596 // Register and fire invalidation |
574 StorePolicy(POLICY_OBJECT_A); | 597 StorePolicy(POLICY_OBJECT_A); |
575 StartInvalidator(); | 598 StartInvalidator(); |
576 EXPECT_TRUE(InvalidationsEnabled()); | 599 EXPECT_TRUE(InvalidationsEnabled()); |
577 syncer::Invalidation inv = | 600 syncer::Invalidation inv = |
578 FireInvalidation(POLICY_OBJECT_A, 12, "test_payload"); | 601 FireInvalidation(POLICY_OBJECT_A, V(12), "test_payload"); |
579 | 602 |
580 // Make sure client info is set as soon as the invalidation is received. | 603 // Make sure client info is set as soon as the invalidation is received. |
581 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); | 604 EXPECT_TRUE(CheckInvalidationInfo(V(12), "test_payload")); |
582 EXPECT_TRUE(CheckPolicyRefreshed()); | 605 EXPECT_TRUE(CheckPolicyRefreshed()); |
583 | 606 |
584 // Make sure invalidation is not acknowledged until the store is loaded. | 607 // Make sure invalidation is not acknowledged until the store is loaded. |
585 EXPECT_FALSE(IsInvalidationAcknowledged(inv)); | 608 EXPECT_FALSE(IsInvalidationAcknowledged(inv)); |
586 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); | 609 EXPECT_TRUE(CheckInvalidationInfo(V(12), "test_payload")); |
587 StorePolicy(POLICY_OBJECT_A, 12); | 610 StorePolicy(POLICY_OBJECT_A, V(12)); |
588 EXPECT_TRUE(IsInvalidationAcknowledged(inv)); | 611 EXPECT_TRUE(IsInvalidationAcknowledged(inv)); |
589 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 612 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
590 } | 613 } |
591 | 614 |
592 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) { | 615 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) { |
593 // Register and fire invalidation with unknown version. | 616 // Register and fire invalidation with unknown version. |
594 StorePolicy(POLICY_OBJECT_A); | 617 StorePolicy(POLICY_OBJECT_A); |
595 StartInvalidator(); | 618 StartInvalidator(); |
596 syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A); | 619 syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A); |
597 | 620 |
598 // Make sure client info is not set until after the invalidation callback is | 621 // Make sure client info is not set until after the invalidation callback is |
599 // made. | 622 // made. |
600 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 623 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
601 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion()); | 624 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion()); |
602 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); | 625 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); |
603 | 626 |
604 // Make sure invalidation is not acknowledged until the store is loaded. | 627 // Make sure invalidation is not acknowledged until the store is loaded. |
605 EXPECT_FALSE(IsInvalidationAcknowledged(inv)); | 628 EXPECT_FALSE(IsInvalidationAcknowledged(inv)); |
606 StorePolicy(POLICY_OBJECT_A, -1); | 629 StorePolicy(POLICY_OBJECT_A, -1); |
607 EXPECT_TRUE(IsInvalidationAcknowledged(inv)); | 630 EXPECT_TRUE(IsInvalidationAcknowledged(inv)); |
608 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 631 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
609 } | 632 } |
610 | 633 |
611 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) { | 634 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) { |
612 // Generate multiple invalidations. | 635 // Generate multiple invalidations. |
613 StorePolicy(POLICY_OBJECT_A); | 636 StorePolicy(POLICY_OBJECT_A); |
614 StartInvalidator(); | 637 StartInvalidator(); |
615 syncer::Invalidation inv1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1"); | 638 syncer::Invalidation inv1 = FireInvalidation(POLICY_OBJECT_A, V(1), "test1"); |
616 EXPECT_TRUE(CheckInvalidationInfo(1, "test1")); | 639 EXPECT_TRUE(CheckInvalidationInfo(V(1), "test1")); |
617 syncer::Invalidation inv2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2"); | 640 syncer::Invalidation inv2 = FireInvalidation(POLICY_OBJECT_A, V(2), "test2"); |
618 EXPECT_TRUE(CheckInvalidationInfo(2, "test2")); | 641 EXPECT_TRUE(CheckInvalidationInfo(V(2), "test2")); |
619 syncer::Invalidation inv3 = FireInvalidation(POLICY_OBJECT_A, 3, "test3"); | 642 syncer::Invalidation inv3 = FireInvalidation(POLICY_OBJECT_A, V(3), "test3"); |
620 EXPECT_TRUE(CheckInvalidationInfo(3, "test3")); | 643 EXPECT_TRUE(CheckInvalidationInfo(V(3), "test3")); |
621 | 644 |
622 // Make sure the replaced invalidations are acknowledged. | 645 // Make sure the replaced invalidations are acknowledged. |
623 EXPECT_TRUE(IsInvalidationAcknowledged(inv1)); | 646 EXPECT_TRUE(IsInvalidationAcknowledged(inv1)); |
624 EXPECT_TRUE(IsInvalidationAcknowledged(inv2)); | 647 EXPECT_TRUE(IsInvalidationAcknowledged(inv2)); |
625 | 648 |
626 // Make sure the policy is refreshed once. | 649 // Make sure the policy is refreshed once. |
627 EXPECT_TRUE(CheckPolicyRefreshed()); | 650 EXPECT_TRUE(CheckPolicyRefreshed()); |
628 | 651 |
629 // Make sure that the last invalidation is only acknowledged after the store | 652 // Make sure that the last invalidation is only acknowledged after the store |
630 // is loaded with the latest version. | 653 // is loaded with the latest version. |
631 StorePolicy(POLICY_OBJECT_A, 1); | 654 StorePolicy(POLICY_OBJECT_A, V(1)); |
632 EXPECT_FALSE(IsInvalidationAcknowledged(inv3)); | 655 EXPECT_FALSE(IsInvalidationAcknowledged(inv3)); |
633 StorePolicy(POLICY_OBJECT_A, 2); | 656 StorePolicy(POLICY_OBJECT_A, V(2)); |
634 EXPECT_FALSE(IsInvalidationAcknowledged(inv3)); | 657 EXPECT_FALSE(IsInvalidationAcknowledged(inv3)); |
635 StorePolicy(POLICY_OBJECT_A, 3); | 658 StorePolicy(POLICY_OBJECT_A, V(3)); |
636 EXPECT_TRUE(IsInvalidationAcknowledged(inv3)); | 659 EXPECT_TRUE(IsInvalidationAcknowledged(inv3)); |
637 } | 660 } |
638 | 661 |
639 TEST_F(CloudPolicyInvalidatorTest, | 662 TEST_F(CloudPolicyInvalidatorTest, |
640 HandleMultipleInvalidationsWithUnknownVersion) { | 663 HandleMultipleInvalidationsWithUnknownVersion) { |
641 // Validate that multiple invalidations with unknown version each generate | 664 // Validate that multiple invalidations with unknown version each generate |
642 // unique invalidation version numbers. | 665 // unique invalidation version numbers. |
643 StorePolicy(POLICY_OBJECT_A); | 666 StorePolicy(POLICY_OBJECT_A); |
644 StartInvalidator(); | 667 StartInvalidator(); |
645 syncer::Invalidation inv1 = FireUnknownVersionInvalidation(POLICY_OBJECT_A); | 668 syncer::Invalidation inv1 = FireUnknownVersionInvalidation(POLICY_OBJECT_A); |
(...skipping 20 matching lines...) Expand all Loading... | |
666 StorePolicy(POLICY_OBJECT_A, -2); | 689 StorePolicy(POLICY_OBJECT_A, -2); |
667 EXPECT_FALSE(IsInvalidationAcknowledged(inv3)); | 690 EXPECT_FALSE(IsInvalidationAcknowledged(inv3)); |
668 StorePolicy(POLICY_OBJECT_A, -3); | 691 StorePolicy(POLICY_OBJECT_A, -3); |
669 EXPECT_TRUE(IsInvalidationAcknowledged(inv3)); | 692 EXPECT_TRUE(IsInvalidationAcknowledged(inv3)); |
670 } | 693 } |
671 | 694 |
672 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeRefresh) { | 695 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeRefresh) { |
673 // Generate an invalidation. | 696 // Generate an invalidation. |
674 StorePolicy(POLICY_OBJECT_A); | 697 StorePolicy(POLICY_OBJECT_A); |
675 StartInvalidator(); | 698 StartInvalidator(); |
676 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, 3, "test"); | 699 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(3), "test"); |
677 | 700 |
678 // Ensure that the policy is not refreshed and the invalidation is | 701 // Ensure that the policy is not refreshed and the invalidation is |
679 // acknowledged if the store is loaded with the latest version before the | 702 // acknowledged if the store is loaded with the latest version before the |
680 // refresh can occur. | 703 // refresh can occur. |
681 StorePolicy(POLICY_OBJECT_A, 3); | 704 StorePolicy(POLICY_OBJECT_A, V(3)); |
682 EXPECT_TRUE(IsInvalidationAcknowledged(inv)); | 705 EXPECT_TRUE(IsInvalidationAcknowledged(inv)); |
683 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 706 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
684 } | 707 } |
685 | 708 |
686 TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) { | 709 TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) { |
687 // Generate an invalidation. | 710 // Generate an invalidation. |
688 StorePolicy(POLICY_OBJECT_A); | 711 StorePolicy(POLICY_OBJECT_A); |
689 StartInvalidator(); | 712 StartInvalidator(); |
690 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, 3, "test"); | 713 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(3), "test"); |
691 | 714 |
692 // Ensure that the policy refresh is not made after the invalidator is shut | 715 // Ensure that the policy refresh is not made after the invalidator is shut |
693 // down. | 716 // down. |
694 ShutdownInvalidator(); | 717 ShutdownInvalidator(); |
695 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 718 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
696 DestroyInvalidator(); | 719 DestroyInvalidator(); |
697 } | 720 } |
698 | 721 |
699 TEST_F(CloudPolicyInvalidatorTest, StateChanged) { | 722 TEST_F(CloudPolicyInvalidatorTest, StateChanged) { |
700 // Test invalidation service state changes while not registered. | 723 // Test invalidation service state changes while not registered. |
(...skipping 29 matching lines...) Expand all Loading... | |
730 EXPECT_FALSE(InvalidationsEnabled()); | 753 EXPECT_FALSE(InvalidationsEnabled()); |
731 StorePolicy(POLICY_OBJECT_NONE); | 754 StorePolicy(POLICY_OBJECT_NONE); |
732 StorePolicy(POLICY_OBJECT_A); | 755 StorePolicy(POLICY_OBJECT_A); |
733 EXPECT_FALSE(InvalidationsEnabled()); | 756 EXPECT_FALSE(InvalidationsEnabled()); |
734 } | 757 } |
735 | 758 |
736 TEST_F(CloudPolicyInvalidatorTest, Disconnect) { | 759 TEST_F(CloudPolicyInvalidatorTest, Disconnect) { |
737 // Generate an invalidation. | 760 // Generate an invalidation. |
738 StorePolicy(POLICY_OBJECT_A); | 761 StorePolicy(POLICY_OBJECT_A); |
739 StartInvalidator(); | 762 StartInvalidator(); |
740 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, 1, "test"); | 763 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(1), "test"); |
741 EXPECT_TRUE(InvalidationsEnabled()); | 764 EXPECT_TRUE(InvalidationsEnabled()); |
742 | 765 |
743 // Ensure that the policy is not refreshed after disconnecting the core, but | 766 // Ensure that the policy is not refreshed after disconnecting the core, but |
744 // a call to indicate that invalidations are disabled is made. | 767 // a call to indicate that invalidations are disabled is made. |
745 DisconnectCore(); | 768 DisconnectCore(); |
746 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 769 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
747 | 770 |
748 // Ensure that invalidation service events do not cause refreshes while the | 771 // Ensure that invalidation service events do not cause refreshes while the |
749 // invalidator is stopped. | 772 // invalidator is stopped. |
750 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, 2, "test"))); | 773 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(2), "test"))); |
751 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 774 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
752 DisableInvalidationService(); | 775 DisableInvalidationService(); |
753 EnableInvalidationService(); | 776 EnableInvalidationService(); |
754 | 777 |
755 // Connect and disconnect without starting the refresh scheduler. | 778 // Connect and disconnect without starting the refresh scheduler. |
756 ConnectCore(); | 779 ConnectCore(); |
757 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, 3, "test"))); | 780 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(3), "test"))); |
758 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 781 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
759 DisconnectCore(); | 782 DisconnectCore(); |
760 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, 4, "test"))); | 783 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(4), "test"))); |
761 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 784 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
762 | 785 |
763 // Ensure that the invalidator returns to normal after reconnecting. | 786 // Ensure that the invalidator returns to normal after reconnecting. |
764 ConnectCore(); | 787 ConnectCore(); |
765 StartRefreshScheduler(); | 788 StartRefreshScheduler(); |
766 EXPECT_TRUE(CheckPolicyNotRefreshed()); | 789 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
767 EXPECT_TRUE(InvalidationsEnabled()); | 790 EXPECT_TRUE(InvalidationsEnabled()); |
768 FireInvalidation(POLICY_OBJECT_A, 5, "test"); | 791 FireInvalidation(POLICY_OBJECT_A, V(5), "test"); |
769 EXPECT_TRUE(CheckInvalidationInfo(5, "test")); | 792 EXPECT_TRUE(CheckInvalidationInfo(V(5), "test")); |
770 EXPECT_TRUE(CheckPolicyRefreshed()); | 793 EXPECT_TRUE(CheckPolicyRefreshed()); |
771 DisableInvalidationService(); | 794 DisableInvalidationService(); |
772 EXPECT_FALSE(InvalidationsEnabled()); | 795 EXPECT_FALSE(InvalidationsEnabled()); |
773 } | 796 } |
774 | 797 |
775 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { | 798 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { |
776 // Store loads occurring before invalidation registration are not counted. | 799 // Store loads occurring before invalidation registration are not counted. |
777 StartInvalidator(); | 800 StartInvalidator(); |
778 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); | 801 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); |
779 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); | 802 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
835 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | 858 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); |
836 } | 859 } |
837 | 860 |
838 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) { | 861 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) { |
839 // Store loads after an invalidation are counted as invalidated, even if | 862 // Store loads after an invalidation are counted as invalidated, even if |
840 // the loads do not result in the invalidation being acknowledged. | 863 // the loads do not result in the invalidation being acknowledged. |
841 StartInvalidator(); | 864 StartInvalidator(); |
842 StorePolicy(POLICY_OBJECT_A); | 865 StorePolicy(POLICY_OBJECT_A); |
843 AdvanceClock(base::TimeDelta::FromSeconds( | 866 AdvanceClock(base::TimeDelta::FromSeconds( |
844 CloudPolicyInvalidator::kInvalidationGracePeriod)); | 867 CloudPolicyInvalidator::kInvalidationGracePeriod)); |
845 FireInvalidation(POLICY_OBJECT_A, 5, "test"); | 868 FireInvalidation(POLICY_OBJECT_A, V(5), "test"); |
846 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 869 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
847 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 870 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
848 StorePolicy(POLICY_OBJECT_A, 5, true /* policy_changed */); | 871 StorePolicy(POLICY_OBJECT_A, V(5), true /* policy_changed */); |
849 | 872 |
850 // Store loads after the invalidation is complete are not counted as | 873 // Store loads after the invalidation is complete are not counted as |
851 // invalidated. | 874 // invalidated. |
852 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 875 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
853 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 876 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
854 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 877 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
855 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 878 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
856 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 879 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
857 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 880 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
858 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 881 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
859 | 882 |
860 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED)); | 883 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED)); |
861 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); | 884 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
862 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); | 885 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); |
863 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); | 886 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); |
864 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | 887 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); |
865 } | 888 } |
866 | 889 |
867 TEST_F(CloudPolicyInvalidatorTest, InvalidationMetrics) { | 890 TEST_F(CloudPolicyInvalidatorTest, ExpiredInvalidations) { |
868 // Generate a mix of versioned and unknown-version invalidations. | 891 StorePolicy(POLICY_OBJECT_A, 0, false, Now()); |
869 StorePolicy(POLICY_OBJECT_A); | |
870 StartInvalidator(); | 892 StartInvalidator(); |
871 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B))); | 893 |
872 FireUnknownVersionInvalidation(POLICY_OBJECT_A); | 894 // Invalidations fired before the last fetch time (adjusted by max time delta) |
873 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_B, 1, "test"))); | 895 // should be ignored. |
874 FireInvalidation(POLICY_OBJECT_A, 1, "test"); | 896 base::Time time = Now() - base::TimeDelta::FromMinutes( |
875 FireInvalidation(POLICY_OBJECT_A, 2, "test"); | 897 CloudPolicyInvalidator::kMaxInvalidationTimeDelta + 5); |
876 FireUnknownVersionInvalidation(POLICY_OBJECT_A); | 898 syncer::Invalidation inv = |
877 FireUnknownVersionInvalidation(POLICY_OBJECT_A); | 899 FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test"); |
878 FireInvalidation(POLICY_OBJECT_A, 3, "test"); | 900 ASSERT_TRUE(IsInvalidationAcknowledged(inv)); |
879 FireInvalidation(POLICY_OBJECT_A, 4, "test"); | 901 ASSERT_TRUE(CheckPolicyNotRefreshed()); |
902 | |
903 time += base::TimeDelta::FromMinutes(5) - base::TimeDelta::FromSeconds(1); | |
904 inv = FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test"); | |
905 ASSERT_TRUE(IsInvalidationAcknowledged(inv)); | |
906 ASSERT_TRUE(CheckPolicyNotRefreshed()); | |
907 | |
908 // Invalidations fired after the last fetch should not be ignored. | |
909 time += base::TimeDelta::FromSeconds(1); | |
910 inv = FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test"); | |
911 ASSERT_FALSE(IsInvalidationAcknowledged(inv)); | |
912 ASSERT_TRUE(CheckPolicyRefreshed()); | |
913 | |
914 time += base::TimeDelta::FromMinutes(10); | |
915 inv = FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test"); | |
916 ASSERT_FALSE(IsInvalidationAcknowledged(inv)); | |
917 ASSERT_TRUE(CheckPolicyRefreshed()); | |
918 | |
919 time += base::TimeDelta::FromMinutes(10); | |
920 inv = FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test"); | |
921 ASSERT_FALSE(IsInvalidationAcknowledged(inv)); | |
922 ASSERT_TRUE(CheckPolicyRefreshed()); | |
923 | |
924 // Unknown version invalidations fired just after the last fetch time should | |
925 // be ignored. | |
926 inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A); | |
927 ASSERT_TRUE(IsInvalidationAcknowledged(inv)); | |
928 ASSERT_TRUE(CheckPolicyNotRefreshed()); | |
929 | |
930 AdvanceClock(base::TimeDelta::FromSeconds( | |
931 CloudPolicyInvalidator::kUnknownVersionIgnorePeriod - 1)); | |
932 inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A); | |
933 ASSERT_TRUE(IsInvalidationAcknowledged(inv)); | |
934 ASSERT_TRUE(CheckPolicyNotRefreshed()); | |
935 | |
936 // Unknown version invalidations fired past the ignore period should not be | |
937 // ignored. | |
938 AdvanceClock(base::TimeDelta::FromSeconds(1)); | |
939 inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A); | |
940 ASSERT_FALSE(IsInvalidationAcknowledged(inv)); | |
941 ASSERT_TRUE(CheckPolicyRefreshedWithUnknownVersion()); | |
880 | 942 |
881 // Verify that received invalidations metrics are correct. | 943 // Verify that received invalidations metrics are correct. |
882 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); | 944 EXPECT_EQ(1, GetInvalidationCount(POLICY_INVALIDATION_TYPE_NO_PAYLOAD)); |
883 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); | 945 EXPECT_EQ(3, GetInvalidationCount(POLICY_INVALIDATION_TYPE_NORMAL)); |
946 EXPECT_EQ(2, | |
947 GetInvalidationCount(POLICY_INVALIDATION_TYPE_NO_PAYLOAD_EXPIRED)); | |
948 EXPECT_EQ(2, GetInvalidationCount(POLICY_INVALIDATION_TYPE_EXPIRED)); | |
884 } | 949 } |
885 | 950 |
886 } // namespace policy | 951 } // namespace policy |
OLD | NEW |