Convert remoting_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / chrome / browser / policy / cloud / cloud_policy_invalidator_unittest.cc
blob6ba4978259e4a1d341a2aab4d04195e20e47b8ea
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
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/histogram_samples.h"
13 #include "base/metrics/sample_map.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/run_loop.h"
16 #include "base/test/simple_test_clock.h"
17 #include "base/test/test_simple_task_runner.h"
18 #include "base/time/time.h"
19 #include "base/values.h"
20 #include "chrome/browser/invalidation/fake_invalidation_service.h"
21 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"
22 #include "chrome/browser/policy/cloud/user_cloud_policy_invalidator.h"
23 #include "components/invalidation/public/invalidation_util.h"
24 #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_refresh_scheduler.h"
27 #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_store.h"
30 #include "components/policy/core/common/policy_types.h"
31 #include "policy/policy_constants.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 namespace em = enterprise_management;
37 namespace policy {
39 class CloudPolicyInvalidatorTest : public testing::Test {
40 protected:
41 // Policy objects which can be used in tests.
42 enum PolicyObject {
43 POLICY_OBJECT_NONE,
44 POLICY_OBJECT_A,
45 POLICY_OBJECT_B
48 CloudPolicyInvalidatorTest();
50 void TearDown() override;
52 // Starts the invalidator which will be tested.
53 // |initialize| determines if the invalidator should be initialized.
54 // |start_refresh_scheduler| determines if the refresh scheduler should start.
55 // |highest_handled_invalidation_version| is the highest invalidation version
56 // that was handled already before this invalidator was created.
57 void StartInvalidator(bool initialize,
58 bool start_refresh_scheduler,
59 int64 highest_handled_invalidation_version);
60 void StartInvalidator() {
61 StartInvalidator(true, /* initialize */
62 true, /* start_refresh_scheduler */
63 0 /* highest_handled_invalidation_version */);
66 // Calls Initialize on the invalidator.
67 void InitializeInvalidator();
69 // Calls Shutdown on the invalidator. Test must call DestroyInvalidator
70 // afterwards to prevent Shutdown from being called twice.
71 void ShutdownInvalidator();
73 // Destroys the invalidator.
74 void DestroyInvalidator();
76 // Connects the cloud policy core.
77 void ConnectCore();
79 // Starts the refresh scheduler.
80 void StartRefreshScheduler();
82 // Disconnects the cloud policy core.
83 void DisconnectCore();
85 // Simulates storing a new policy to the policy store.
86 // |object| determines which policy object the store will report the
87 // invalidator should register for. May be POLICY_OBJECT_NONE for no object.
88 // |invalidation_version| determines what invalidation the store will report.
89 // |policy_changed| determines whether a policy value different from the
90 // current value will be stored.
91 // |time| determines the timestamp the store will report.
92 void StorePolicy(
93 PolicyObject object,
94 int64 invalidation_version,
95 bool policy_changed,
96 const base::Time& time);
97 void StorePolicy(
98 PolicyObject object,
99 int64 invalidation_version,
100 bool policy_changed) {
101 StorePolicy(object,
102 invalidation_version,
103 policy_changed,
104 Now() - base::TimeDelta::FromMinutes(5));
106 void StorePolicy(PolicyObject object, int64 invalidation_version) {
107 StorePolicy(object, invalidation_version, false);
109 void StorePolicy(PolicyObject object) {
110 StorePolicy(object, 0);
113 // Disables the invalidation service. It is enabled by default.
114 void DisableInvalidationService();
116 // Enables the invalidation service. It is enabled by default.
117 void EnableInvalidationService();
119 // Causes the invalidation service to fire an invalidation.
120 syncer::Invalidation FireInvalidation(
121 PolicyObject object,
122 int64 version,
123 const std::string& payload);
125 // Causes the invalidation service to fire an invalidation with unknown
126 // version.
127 syncer::Invalidation FireUnknownVersionInvalidation(PolicyObject object);
129 // Checks the expected value of the currently set invalidation info.
130 bool CheckInvalidationInfo(int64 version, const std::string& payload);
132 // Checks that the policy was not refreshed due to an invalidation.
133 bool CheckPolicyNotRefreshed();
135 // Checks that the policy was refreshed due to an invalidation within an
136 // appropriate timeframe depending on whether the invalidation had unknown
137 // version.
138 bool CheckPolicyRefreshed();
139 bool CheckPolicyRefreshedWithUnknownVersion();
141 bool IsUnsent(const syncer::Invalidation& invalidation);
143 // Returns the invalidations enabled state set by the invalidator on the
144 // refresh scheduler.
145 bool InvalidationsEnabled();
147 // Determines if the invalidation with the given ack handle has been
148 // acknowledged.
149 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation);
151 // Determines if the invalidator has registered for an object with the
152 // invalidation service.
153 bool IsInvalidatorRegistered();
155 // Returns the highest invalidation version that was handled already according
156 // to the |invalidator_|.
157 int64 GetHighestHandledInvalidationVersion() const;
159 // Advance the test clock.
160 void AdvanceClock(base::TimeDelta delta);
162 // Get the current time on the test clock.
163 base::Time Now();
165 // Translate a version number into an appropriate invalidation version (which
166 // is based on the current time).
167 int64 V(int version);
169 // Get an invalidation version for the given time.
170 int64 GetVersion(base::Time time);
172 // Get the policy type that the |invalidator_| is responsible for.
173 virtual em::DeviceRegisterRequest::Type GetPolicyType() const;
175 private:
176 // Checks that the policy was refreshed due to an invalidation with the given
177 // base delay.
178 bool CheckPolicyRefreshed(base::TimeDelta delay);
180 // Checks that the policy was refreshed the given number of times.
181 bool CheckPolicyRefreshCount(int count);
183 // Returns the object id of the given policy object.
184 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const;
186 base::MessageLoop loop_;
188 // Objects the invalidator depends on.
189 invalidation::FakeInvalidationService invalidation_service_;
190 MockCloudPolicyStore store_;
191 CloudPolicyCore core_;
192 MockCloudPolicyClient* client_;
193 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
194 base::SimpleTestClock* clock_;
196 // The invalidator which will be tested.
197 scoped_ptr<CloudPolicyInvalidator> invalidator_;
199 // Object ids for the test policy objects.
200 invalidation::ObjectId object_id_a_;
201 invalidation::ObjectId object_id_b_;
203 // Fake policy values which are alternated to cause the store to report a
204 // changed policy.
205 const char* policy_value_a_;
206 const char* policy_value_b_;
208 // The currently used policy value.
209 const char* policy_value_cur_;
212 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest()
213 : core_(dm_protocol::kChromeUserPolicyType,
214 std::string(),
215 &store_,
216 loop_.task_runner()),
217 client_(nullptr),
218 task_runner_(new base::TestSimpleTaskRunner()),
219 clock_(new base::SimpleTestClock()),
220 object_id_a_(135, "asdf"),
221 object_id_b_(246, "zxcv"),
222 policy_value_a_("asdf"),
223 policy_value_b_("zxcv"),
224 policy_value_cur_(policy_value_a_) {
225 clock_->SetNow(
226 base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(987654321));
229 void CloudPolicyInvalidatorTest::TearDown() {
230 if (invalidator_)
231 invalidator_->Shutdown();
232 core_.Disconnect();
235 void CloudPolicyInvalidatorTest::StartInvalidator(
236 bool initialize,
237 bool start_refresh_scheduler,
238 int64 highest_handled_invalidation_version) {
239 invalidator_.reset(new CloudPolicyInvalidator(
240 GetPolicyType(),
241 &core_,
242 task_runner_,
243 scoped_ptr<base::Clock>(clock_),
244 highest_handled_invalidation_version));
245 if (start_refresh_scheduler) {
246 ConnectCore();
247 StartRefreshScheduler();
249 if (initialize)
250 InitializeInvalidator();
253 void CloudPolicyInvalidatorTest::InitializeInvalidator() {
254 invalidator_->Initialize(&invalidation_service_);
257 void CloudPolicyInvalidatorTest::ShutdownInvalidator() {
258 invalidator_->Shutdown();
261 void CloudPolicyInvalidatorTest::DestroyInvalidator() {
262 invalidator_.reset();
265 void CloudPolicyInvalidatorTest::ConnectCore() {
266 client_ = new MockCloudPolicyClient();
267 client_->SetDMToken("dm");
268 core_.Connect(scoped_ptr<CloudPolicyClient>(client_));
271 void CloudPolicyInvalidatorTest::StartRefreshScheduler() {
272 core_.StartRefreshScheduler();
275 void CloudPolicyInvalidatorTest::DisconnectCore() {
276 client_ = nullptr;
277 core_.Disconnect();
280 void CloudPolicyInvalidatorTest::StorePolicy(
281 PolicyObject object,
282 int64 invalidation_version,
283 bool policy_changed,
284 const base::Time& time) {
285 em::PolicyData* data = new em::PolicyData();
286 if (object != POLICY_OBJECT_NONE) {
287 data->set_invalidation_source(GetPolicyObjectId(object).source());
288 data->set_invalidation_name(GetPolicyObjectId(object).name());
290 data->set_timestamp((time - base::Time::UnixEpoch()).InMilliseconds());
291 // Swap the policy value if a policy change is desired.
292 if (policy_changed)
293 policy_value_cur_ = policy_value_cur_ == policy_value_a_ ?
294 policy_value_b_ : policy_value_a_;
295 data->set_policy_value(policy_value_cur_);
296 store_.invalidation_version_ = invalidation_version;
297 store_.policy_.reset(data);
298 base::DictionaryValue policies;
299 policies.SetInteger(
300 key::kMaxInvalidationFetchDelay,
301 CloudPolicyInvalidator::kMaxFetchDelayMin);
302 store_.policy_map_.LoadFrom(
303 &policies,
304 POLICY_LEVEL_MANDATORY,
305 POLICY_SCOPE_MACHINE);
306 store_.NotifyStoreLoaded();
309 void CloudPolicyInvalidatorTest::DisableInvalidationService() {
310 invalidation_service_.SetInvalidatorState(
311 syncer::TRANSIENT_INVALIDATION_ERROR);
314 void CloudPolicyInvalidatorTest::EnableInvalidationService() {
315 invalidation_service_.SetInvalidatorState(syncer::INVALIDATIONS_ENABLED);
318 syncer::Invalidation CloudPolicyInvalidatorTest::FireInvalidation(
319 PolicyObject object,
320 int64 version,
321 const std::string& payload) {
322 syncer::Invalidation invalidation = syncer::Invalidation::Init(
323 GetPolicyObjectId(object),
324 version,
325 payload);
326 invalidation_service_.EmitInvalidationForTest(invalidation);
327 return invalidation;
330 syncer::Invalidation CloudPolicyInvalidatorTest::FireUnknownVersionInvalidation(
331 PolicyObject object) {
332 syncer::Invalidation invalidation = syncer::Invalidation::InitUnknownVersion(
333 GetPolicyObjectId(object));
334 invalidation_service_.EmitInvalidationForTest(invalidation);
335 return invalidation;
338 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo(
339 int64 version,
340 const std::string& payload) {
341 MockCloudPolicyClient* client =
342 static_cast<MockCloudPolicyClient*>(core_.client());
343 return version == client->invalidation_version_ &&
344 payload == client->invalidation_payload_;
347 bool CloudPolicyInvalidatorTest::CheckPolicyNotRefreshed() {
348 return CheckPolicyRefreshCount(0);
351 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed() {
352 return CheckPolicyRefreshed(base::TimeDelta());
355 bool CloudPolicyInvalidatorTest::IsUnsent(
356 const syncer::Invalidation& invalidation) {
357 return invalidation_service_.GetMockAckHandler()->IsUnsent(invalidation);
360 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshedWithUnknownVersion() {
361 return CheckPolicyRefreshed(base::TimeDelta::FromMinutes(
362 CloudPolicyInvalidator::kMissingPayloadDelay));
365 bool CloudPolicyInvalidatorTest::InvalidationsEnabled() {
366 return core_.refresh_scheduler()->invalidations_available();
369 bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged(
370 const syncer::Invalidation& invalidation) {
371 // The acknowledgement task is run through a WeakHandle that posts back to our
372 // own thread. We need to run any posted tasks before we can check
373 // acknowledgement status.
374 loop_.RunUntilIdle();
376 EXPECT_FALSE(IsUnsent(invalidation));
377 return !invalidation_service_.GetMockAckHandler()->IsUnacked(invalidation);
380 bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() {
381 return !invalidation_service_.invalidator_registrar()
382 .GetRegisteredIds(invalidator_.get()).empty();
385 int64 CloudPolicyInvalidatorTest::GetHighestHandledInvalidationVersion() const {
386 return invalidator_->highest_handled_invalidation_version();
389 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) {
390 clock_->Advance(delta);
393 base::Time CloudPolicyInvalidatorTest::Now() {
394 return clock_->Now();
397 int64 CloudPolicyInvalidatorTest::V(int version) {
398 return GetVersion(Now()) + version;
401 int64 CloudPolicyInvalidatorTest::GetVersion(base::Time time) {
402 return (time - base::Time::UnixEpoch()).InMicroseconds();
405 em::DeviceRegisterRequest::Type
406 CloudPolicyInvalidatorTest::GetPolicyType() const {
407 return UserCloudPolicyInvalidator::GetPolicyType();
410 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) {
411 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds(
412 CloudPolicyInvalidator::kMaxFetchDelayMin);
414 if (task_runner_->GetPendingTasks().empty())
415 return false;
416 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay;
417 EXPECT_GE(actual_delay, delay);
418 EXPECT_LE(actual_delay, max_delay);
420 return CheckPolicyRefreshCount(1);
423 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshCount(int count) {
424 if (!client_) {
425 task_runner_->RunUntilIdle();
426 return count == 0;
429 // Clear any non-invalidation refreshes which may be pending.
430 EXPECT_CALL(*client_, FetchPolicy()).Times(testing::AnyNumber());
431 base::RunLoop().RunUntilIdle();
432 testing::Mock::VerifyAndClearExpectations(client_);
434 // Run the invalidator tasks then check for invalidation refreshes.
435 EXPECT_CALL(*client_, FetchPolicy()).Times(count);
436 task_runner_->RunUntilIdle();
437 base::RunLoop().RunUntilIdle();
438 return testing::Mock::VerifyAndClearExpectations(client_);
441 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId(
442 PolicyObject object) const {
443 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B);
444 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_;
447 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) {
448 // No invalidations should be processed if the invalidator is not initialized.
449 StartInvalidator(false, /* initialize */
450 true, /* start_refresh_scheduler */
451 0 /* highest_handled_invalidation_version*/);
452 StorePolicy(POLICY_OBJECT_A);
453 EXPECT_FALSE(IsInvalidatorRegistered());
454 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
455 EXPECT_TRUE(CheckPolicyNotRefreshed());
456 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
459 TEST_F(CloudPolicyInvalidatorTest, RefreshSchedulerNotStarted) {
460 // No invalidations should be processed if the refresh scheduler is not
461 // started.
462 StartInvalidator(true, /* initialize */
463 false, /* start_refresh_scheduler */
464 0 /* highest_handled_invalidation_version*/);
465 StorePolicy(POLICY_OBJECT_A);
466 EXPECT_FALSE(IsInvalidatorRegistered());
467 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
468 EXPECT_TRUE(CheckPolicyNotRefreshed());
469 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
472 TEST_F(CloudPolicyInvalidatorTest, DisconnectCoreThenInitialize) {
473 // No invalidations should be processed if the core is disconnected before
474 // initialization.
475 StartInvalidator(false, /* initialize */
476 true, /* start_refresh_scheduler */
477 0 /* highest_handled_invalidation_version*/);
478 DisconnectCore();
479 InitializeInvalidator();
480 StorePolicy(POLICY_OBJECT_A);
481 EXPECT_FALSE(IsInvalidatorRegistered());
482 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
483 EXPECT_TRUE(CheckPolicyNotRefreshed());
484 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
487 TEST_F(CloudPolicyInvalidatorTest, InitializeThenStartRefreshScheduler) {
488 // Make sure registration occurs and invalidations are processed when
489 // Initialize is called before starting the refresh scheduler.
490 // Note that the reverse case (start refresh scheduler then initialize) is
491 // the default behavior for the test fixture, so will be tested in most other
492 // tests.
493 StartInvalidator(true, /* initialize */
494 false, /* start_refresh_scheduler */
495 0 /* highest_handled_invalidation_version*/);
496 ConnectCore();
497 StartRefreshScheduler();
498 StorePolicy(POLICY_OBJECT_A);
499 EXPECT_TRUE(IsInvalidatorRegistered());
500 FireUnknownVersionInvalidation(POLICY_OBJECT_A);
501 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
502 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
505 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) {
506 // No registration when store is not loaded.
507 StartInvalidator();
508 EXPECT_FALSE(IsInvalidatorRegistered());
509 EXPECT_FALSE(InvalidationsEnabled());
510 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
511 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
512 EXPECT_TRUE(CheckPolicyNotRefreshed());
514 // No registration when store is loaded with no invalidation object id.
515 StorePolicy(POLICY_OBJECT_NONE);
516 EXPECT_FALSE(IsInvalidatorRegistered());
517 EXPECT_FALSE(InvalidationsEnabled());
518 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
519 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
520 EXPECT_TRUE(CheckPolicyNotRefreshed());
522 // Check registration when store is loaded for object A.
523 StorePolicy(POLICY_OBJECT_A);
524 EXPECT_TRUE(IsInvalidatorRegistered());
525 EXPECT_TRUE(InvalidationsEnabled());
526 FireUnknownVersionInvalidation(POLICY_OBJECT_A);
527 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
528 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
529 EXPECT_TRUE(CheckPolicyNotRefreshed());
530 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
533 TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) {
534 // Register for object A.
535 StartInvalidator();
536 StorePolicy(POLICY_OBJECT_A);
537 EXPECT_TRUE(IsInvalidatorRegistered());
538 EXPECT_TRUE(InvalidationsEnabled());
539 FireUnknownVersionInvalidation(POLICY_OBJECT_A);
540 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
541 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
542 EXPECT_TRUE(CheckPolicyNotRefreshed());
543 syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
545 // Check re-registration for object B. Make sure the pending invalidation for
546 // object A is acknowledged without making the callback.
547 StorePolicy(POLICY_OBJECT_B);
548 EXPECT_TRUE(IsInvalidatorRegistered());
549 EXPECT_TRUE(InvalidationsEnabled());
550 EXPECT_TRUE(IsInvalidationAcknowledged(inv));
551 EXPECT_TRUE(CheckPolicyNotRefreshed());
553 // Make sure future invalidations for object A are ignored and for object B
554 // are processed.
555 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
556 EXPECT_TRUE(CheckPolicyNotRefreshed());
557 FireUnknownVersionInvalidation(POLICY_OBJECT_B);
558 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
559 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
562 TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) {
563 // Register for object A.
564 StartInvalidator();
565 StorePolicy(POLICY_OBJECT_A);
566 EXPECT_TRUE(IsInvalidatorRegistered());
567 EXPECT_TRUE(InvalidationsEnabled());
568 FireUnknownVersionInvalidation(POLICY_OBJECT_A);
569 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
571 // Check unregistration when store is loaded with no invalidation object id.
572 syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
573 EXPECT_FALSE(IsInvalidationAcknowledged(inv));
574 StorePolicy(POLICY_OBJECT_NONE);
575 EXPECT_FALSE(IsInvalidatorRegistered());
576 EXPECT_TRUE(IsInvalidationAcknowledged(inv));
577 EXPECT_FALSE(InvalidationsEnabled());
578 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_A)));
579 EXPECT_TRUE(IsUnsent(FireUnknownVersionInvalidation(POLICY_OBJECT_B)));
580 EXPECT_TRUE(CheckPolicyNotRefreshed());
582 // Check re-registration for object B.
583 StorePolicy(POLICY_OBJECT_B);
584 EXPECT_TRUE(IsInvalidatorRegistered());
585 EXPECT_TRUE(InvalidationsEnabled());
586 FireUnknownVersionInvalidation(POLICY_OBJECT_B);
587 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
588 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
591 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) {
592 // Register and fire invalidation
593 StorePolicy(POLICY_OBJECT_A);
594 StartInvalidator();
595 EXPECT_TRUE(InvalidationsEnabled());
596 syncer::Invalidation inv =
597 FireInvalidation(POLICY_OBJECT_A, V(12), "test_payload");
599 // Make sure client info is set as soon as the invalidation is received.
600 EXPECT_TRUE(CheckInvalidationInfo(V(12), "test_payload"));
601 EXPECT_TRUE(CheckPolicyRefreshed());
603 // Make sure invalidation is not acknowledged until the store is loaded.
604 EXPECT_FALSE(IsInvalidationAcknowledged(inv));
605 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
606 EXPECT_TRUE(CheckInvalidationInfo(V(12), "test_payload"));
607 StorePolicy(POLICY_OBJECT_A, V(12));
608 EXPECT_TRUE(IsInvalidationAcknowledged(inv));
609 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
610 EXPECT_EQ(V(12), GetHighestHandledInvalidationVersion());
613 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) {
614 // Register and fire invalidation with unknown version.
615 StorePolicy(POLICY_OBJECT_A);
616 StartInvalidator();
617 syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
619 // Make sure client info is not set until after the invalidation callback is
620 // made.
621 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
622 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
623 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
625 // Make sure invalidation is not acknowledged until the store is loaded.
626 EXPECT_FALSE(IsInvalidationAcknowledged(inv));
627 StorePolicy(POLICY_OBJECT_A, -1);
628 EXPECT_TRUE(IsInvalidationAcknowledged(inv));
629 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
630 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
633 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) {
634 // Generate multiple invalidations.
635 StorePolicy(POLICY_OBJECT_A);
636 StartInvalidator();
637 syncer::Invalidation inv1 = FireInvalidation(POLICY_OBJECT_A, V(1), "test1");
638 EXPECT_TRUE(CheckInvalidationInfo(V(1), "test1"));
639 syncer::Invalidation inv2 = FireInvalidation(POLICY_OBJECT_A, V(2), "test2");
640 EXPECT_TRUE(CheckInvalidationInfo(V(2), "test2"));
641 syncer::Invalidation inv3 = FireInvalidation(POLICY_OBJECT_A, V(3), "test3");
642 EXPECT_TRUE(CheckInvalidationInfo(V(3), "test3"));
644 // Make sure the replaced invalidations are acknowledged.
645 EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
646 EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
648 // Make sure the policy is refreshed once.
649 EXPECT_TRUE(CheckPolicyRefreshed());
651 // Make sure that the last invalidation is only acknowledged after the store
652 // is loaded with the latest version.
653 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
654 StorePolicy(POLICY_OBJECT_A, V(1));
655 EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
656 EXPECT_EQ(V(1), GetHighestHandledInvalidationVersion());
657 StorePolicy(POLICY_OBJECT_A, V(2));
658 EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
659 EXPECT_EQ(V(2), GetHighestHandledInvalidationVersion());
660 StorePolicy(POLICY_OBJECT_A, V(3));
661 EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
662 EXPECT_EQ(V(3), GetHighestHandledInvalidationVersion());
665 TEST_F(CloudPolicyInvalidatorTest,
666 HandleMultipleInvalidationsWithUnknownVersion) {
667 // Validate that multiple invalidations with unknown version each generate
668 // unique invalidation version numbers.
669 StorePolicy(POLICY_OBJECT_A);
670 StartInvalidator();
671 syncer::Invalidation inv1 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
672 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
673 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
674 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
675 syncer::Invalidation inv2 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
676 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
677 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
678 EXPECT_TRUE(CheckInvalidationInfo(-2, std::string()));
679 syncer::Invalidation inv3 = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
680 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
681 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
682 EXPECT_TRUE(CheckInvalidationInfo(-3, std::string()));
684 // Make sure the replaced invalidations are acknowledged.
685 EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
686 EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
688 // Make sure that the last invalidation is only acknowledged after the store
689 // is loaded with the last unknown version.
690 StorePolicy(POLICY_OBJECT_A, -1);
691 EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
692 StorePolicy(POLICY_OBJECT_A, -2);
693 EXPECT_FALSE(IsInvalidationAcknowledged(inv3));
694 StorePolicy(POLICY_OBJECT_A, -3);
695 EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
696 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
699 TEST_F(CloudPolicyInvalidatorTest,
700 InitialHighestHandledInvalidationVersionNonZero) {
701 StorePolicy(POLICY_OBJECT_A);
702 StartInvalidator(true, /* initialize */
703 true, /* start_refresh_scheduler */
704 V(2) /* highest_handled_invalidation_version*/);
706 // Check that an invalidation whose version is lower than the highest handled
707 // so far is acknowledged but ignored otherwise.
708 syncer::Invalidation inv1 = FireInvalidation(POLICY_OBJECT_A, V(1), "test1");
709 EXPECT_TRUE(CheckPolicyNotRefreshed());
710 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
711 EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
712 EXPECT_EQ(V(2), GetHighestHandledInvalidationVersion());
714 // Check that an invalidation with an unknown version is handled.
715 syncer::Invalidation inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
716 EXPECT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
717 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string()));
718 StorePolicy(POLICY_OBJECT_A, -1);
719 EXPECT_TRUE(IsInvalidationAcknowledged(inv));
720 EXPECT_EQ(V(2), GetHighestHandledInvalidationVersion());
722 // Check that an invalidation whose version matches the highest handled so far
723 // is acknowledged but ignored otherwise.
724 syncer::Invalidation inv2 = FireInvalidation(POLICY_OBJECT_A, V(2), "test2");
725 EXPECT_TRUE(CheckPolicyNotRefreshed());
726 EXPECT_TRUE(CheckInvalidationInfo(0, std::string()));
727 EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
728 EXPECT_EQ(V(2), GetHighestHandledInvalidationVersion());
730 // Check that an invalidation whose version is higher than the highest handled
731 // so far is handled, causing a policy refresh.
732 syncer::Invalidation inv3 = FireInvalidation(POLICY_OBJECT_A, V(3), "test3");
733 EXPECT_TRUE(CheckPolicyRefreshed());
734 EXPECT_TRUE(CheckInvalidationInfo(V(3), "test3"));
735 StorePolicy(POLICY_OBJECT_A, V(3));
736 EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
737 EXPECT_EQ(V(3), GetHighestHandledInvalidationVersion());
740 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeRefresh) {
741 // Generate an invalidation.
742 StorePolicy(POLICY_OBJECT_A);
743 StartInvalidator();
744 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(3), "test");
746 // Ensure that the policy is not refreshed and the invalidation is
747 // acknowledged if the store is loaded with the latest version before the
748 // refresh can occur.
749 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
750 StorePolicy(POLICY_OBJECT_A, V(3));
751 EXPECT_TRUE(IsInvalidationAcknowledged(inv));
752 EXPECT_TRUE(CheckPolicyNotRefreshed());
753 EXPECT_EQ(V(3), GetHighestHandledInvalidationVersion());
756 TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) {
757 // Generate an invalidation.
758 StorePolicy(POLICY_OBJECT_A);
759 StartInvalidator();
760 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(3), "test");
762 // Ensure that the policy refresh is not made after the invalidator is shut
763 // down.
764 ShutdownInvalidator();
765 EXPECT_TRUE(CheckPolicyNotRefreshed());
766 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
767 DestroyInvalidator();
770 TEST_F(CloudPolicyInvalidatorTest, StateChanged) {
771 // Test invalidation service state changes while not registered.
772 StartInvalidator();
773 DisableInvalidationService();
774 EnableInvalidationService();
775 EXPECT_FALSE(InvalidationsEnabled());
777 // Test invalidation service state changes while registered.
778 StorePolicy(POLICY_OBJECT_A);
779 EXPECT_TRUE(InvalidationsEnabled());
780 DisableInvalidationService();
781 EXPECT_FALSE(InvalidationsEnabled());
782 DisableInvalidationService();
783 EXPECT_FALSE(InvalidationsEnabled());
784 EnableInvalidationService();
785 EXPECT_TRUE(InvalidationsEnabled());
786 EnableInvalidationService();
787 EXPECT_TRUE(InvalidationsEnabled());
789 // Test registration changes with invalidation service enabled.
790 StorePolicy(POLICY_OBJECT_NONE);
791 EXPECT_FALSE(InvalidationsEnabled());
792 StorePolicy(POLICY_OBJECT_NONE);
793 EXPECT_FALSE(InvalidationsEnabled());
794 StorePolicy(POLICY_OBJECT_A);
795 EXPECT_TRUE(InvalidationsEnabled());
796 StorePolicy(POLICY_OBJECT_A);
797 EXPECT_TRUE(InvalidationsEnabled());
799 // Test registration changes with invalidation service disabled.
800 DisableInvalidationService();
801 EXPECT_FALSE(InvalidationsEnabled());
802 StorePolicy(POLICY_OBJECT_NONE);
803 StorePolicy(POLICY_OBJECT_A);
804 EXPECT_FALSE(InvalidationsEnabled());
805 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
808 TEST_F(CloudPolicyInvalidatorTest, Disconnect) {
809 // Generate an invalidation.
810 StorePolicy(POLICY_OBJECT_A);
811 StartInvalidator();
812 syncer::Invalidation inv = FireInvalidation(POLICY_OBJECT_A, V(1), "test");
813 EXPECT_TRUE(InvalidationsEnabled());
815 // Ensure that the policy is not refreshed after disconnecting the core, but
816 // a call to indicate that invalidations are disabled is made.
817 DisconnectCore();
818 EXPECT_TRUE(CheckPolicyNotRefreshed());
820 // Ensure that invalidation service events do not cause refreshes while the
821 // invalidator is stopped.
822 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(2), "test")));
823 EXPECT_TRUE(CheckPolicyNotRefreshed());
824 DisableInvalidationService();
825 EnableInvalidationService();
827 // Connect and disconnect without starting the refresh scheduler.
828 ConnectCore();
829 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(3), "test")));
830 EXPECT_TRUE(CheckPolicyNotRefreshed());
831 DisconnectCore();
832 EXPECT_TRUE(IsUnsent(FireInvalidation(POLICY_OBJECT_A, V(4), "test")));
833 EXPECT_TRUE(CheckPolicyNotRefreshed());
835 // Ensure that the invalidator returns to normal after reconnecting.
836 ConnectCore();
837 StartRefreshScheduler();
838 EXPECT_TRUE(CheckPolicyNotRefreshed());
839 EXPECT_TRUE(InvalidationsEnabled());
840 FireInvalidation(POLICY_OBJECT_A, V(5), "test");
841 EXPECT_TRUE(CheckInvalidationInfo(V(5), "test"));
842 EXPECT_TRUE(CheckPolicyRefreshed());
843 DisableInvalidationService();
844 EXPECT_FALSE(InvalidationsEnabled());
845 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
848 class CloudPolicyInvalidatorUserTypedTest
849 : public CloudPolicyInvalidatorTest,
850 public testing::WithParamInterface<em::DeviceRegisterRequest::Type> {
851 protected:
852 CloudPolicyInvalidatorUserTypedTest();
853 virtual ~CloudPolicyInvalidatorUserTypedTest();
855 // CloudPolicyInvalidatorTest:
856 void SetUp() override;
858 // Get the current count for the given metric.
859 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric);
860 base::HistogramBase::Count GetInvalidationCount(PolicyInvalidationType type);
862 private:
863 // CloudPolicyInvalidatorTest:
864 em::DeviceRegisterRequest::Type GetPolicyType() const override;
866 // Get histogram samples for the given histogram.
867 scoped_ptr<base::HistogramSamples> GetHistogramSamples(
868 const std::string& name) const;
870 // Stores starting histogram counts for kMetricPolicyRefresh.
871 scoped_ptr<base::HistogramSamples> refresh_samples_;
873 // Stores starting histogram counts for kMetricPolicyInvalidations.
874 scoped_ptr<base::HistogramSamples> invalidations_samples_;
876 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidatorUserTypedTest);
879 CloudPolicyInvalidatorUserTypedTest::CloudPolicyInvalidatorUserTypedTest() {
882 CloudPolicyInvalidatorUserTypedTest::~CloudPolicyInvalidatorUserTypedTest() {
885 void CloudPolicyInvalidatorUserTypedTest::SetUp() {
886 base::StatisticsRecorder::Initialize();
887 refresh_samples_ = GetHistogramSamples(
888 GetPolicyType() == em::DeviceRegisterRequest::DEVICE ?
889 kMetricDevicePolicyRefresh : kMetricUserPolicyRefresh);
890 invalidations_samples_ = GetHistogramSamples(
891 GetPolicyType() == em::DeviceRegisterRequest::DEVICE ?
892 kMetricDevicePolicyInvalidations : kMetricUserPolicyInvalidations);
895 base::HistogramBase::Count CloudPolicyInvalidatorUserTypedTest::GetCount(
896 MetricPolicyRefresh metric) {
897 return GetHistogramSamples(
898 GetPolicyType() == em::DeviceRegisterRequest::DEVICE ?
899 kMetricDevicePolicyRefresh : kMetricUserPolicyRefresh)->
900 GetCount(metric) - refresh_samples_->GetCount(metric);
903 base::HistogramBase::Count
904 CloudPolicyInvalidatorUserTypedTest::GetInvalidationCount(
905 PolicyInvalidationType type) {
906 return GetHistogramSamples(
907 GetPolicyType() == em::DeviceRegisterRequest::DEVICE ?
908 kMetricDevicePolicyInvalidations : kMetricUserPolicyInvalidations)->
909 GetCount(type) - invalidations_samples_->GetCount(type);
912 em::DeviceRegisterRequest::Type
913 CloudPolicyInvalidatorUserTypedTest::GetPolicyType() const {
914 return GetParam();
917 scoped_ptr<base::HistogramSamples>
918 CloudPolicyInvalidatorUserTypedTest::GetHistogramSamples(
919 const std::string& name) const {
920 base::HistogramBase* histogram =
921 base::StatisticsRecorder::FindHistogram(name);
922 if (!histogram)
923 return scoped_ptr<base::HistogramSamples>(new base::SampleMap());
924 return histogram->SnapshotSamples();
927 TEST_P(CloudPolicyInvalidatorUserTypedTest, RefreshMetricsUnregistered) {
928 // Store loads occurring before invalidation registration are not counted.
929 StartInvalidator();
930 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */);
931 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */);
932 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED));
933 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
934 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
935 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
936 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
937 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
940 TEST_P(CloudPolicyInvalidatorUserTypedTest, RefreshMetricsNoInvalidations) {
941 // Store loads occurring while registered should be differentiated depending
942 // on whether the invalidation service was enabled or not.
943 StorePolicy(POLICY_OBJECT_A);
944 StartInvalidator();
946 // Initially, invalidations have not been enabled past the grace period, so
947 // invalidations are OFF.
948 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
949 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
950 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
952 // If the clock advances less than the grace period, invalidations are OFF.
953 AdvanceClock(base::TimeDelta::FromSeconds(1));
954 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
955 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
956 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
958 // After the grace period elapses, invalidations are ON.
959 AdvanceClock(base::TimeDelta::FromSeconds(
960 CloudPolicyInvalidator::kInvalidationGracePeriod));
961 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
962 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
963 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED));
965 // After the invalidation service is disabled, invalidations are OFF.
966 DisableInvalidationService();
967 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
968 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
969 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
971 // Enabling the invalidation service results in a new grace period, so
972 // invalidations are OFF.
973 EnableInvalidationService();
974 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
975 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
976 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
978 // After the grace period elapses, invalidations are ON.
979 AdvanceClock(base::TimeDelta::FromSeconds(
980 CloudPolicyInvalidator::kInvalidationGracePeriod));
981 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
982 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
984 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED));
985 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
986 EXPECT_EQ(6, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
987 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
988 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
989 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
992 TEST_P(CloudPolicyInvalidatorUserTypedTest, RefreshMetricsInvalidation) {
993 // Store loads after an invalidation are counted as invalidated, even if
994 // the loads do not result in the invalidation being acknowledged.
995 StartInvalidator();
996 StorePolicy(POLICY_OBJECT_A);
997 AdvanceClock(base::TimeDelta::FromSeconds(
998 CloudPolicyInvalidator::kInvalidationGracePeriod));
999 FireInvalidation(POLICY_OBJECT_A, V(5), "test");
1000 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
1001 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
1002 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
1003 StorePolicy(POLICY_OBJECT_A, V(5), true /* policy_changed */);
1004 EXPECT_EQ(V(5), GetHighestHandledInvalidationVersion());
1006 // Store loads after the invalidation is complete are not counted as
1007 // invalidated.
1008 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
1009 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
1010 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
1011 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
1012 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
1013 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
1014 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
1016 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED));
1017 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS));
1018 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_UNCHANGED));
1019 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED));
1020 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED));
1021 EXPECT_EQ(V(5), GetHighestHandledInvalidationVersion());
1024 TEST_P(CloudPolicyInvalidatorUserTypedTest, ExpiredInvalidations) {
1025 StorePolicy(POLICY_OBJECT_A, 0, false, Now());
1026 StartInvalidator();
1028 // Invalidations fired before the last fetch time (adjusted by max time delta)
1029 // should be ignored.
1030 base::Time time = Now() - base::TimeDelta::FromSeconds(
1031 CloudPolicyInvalidator::kMaxInvalidationTimeDelta + 300);
1032 syncer::Invalidation inv =
1033 FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test");
1034 ASSERT_TRUE(IsInvalidationAcknowledged(inv));
1035 ASSERT_TRUE(CheckPolicyNotRefreshed());
1037 time += base::TimeDelta::FromMinutes(5) - base::TimeDelta::FromSeconds(1);
1038 inv = FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test");
1039 ASSERT_TRUE(IsInvalidationAcknowledged(inv));
1040 ASSERT_TRUE(CheckPolicyNotRefreshed());
1042 // Invalidations fired after the last fetch should not be ignored.
1043 time += base::TimeDelta::FromSeconds(1);
1044 inv = FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test");
1045 ASSERT_FALSE(IsInvalidationAcknowledged(inv));
1046 ASSERT_TRUE(CheckPolicyRefreshed());
1048 time += base::TimeDelta::FromMinutes(10);
1049 inv = FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test");
1050 ASSERT_FALSE(IsInvalidationAcknowledged(inv));
1051 ASSERT_TRUE(CheckPolicyRefreshed());
1053 time += base::TimeDelta::FromMinutes(10);
1054 inv = FireInvalidation(POLICY_OBJECT_A, GetVersion(time), "test");
1055 ASSERT_FALSE(IsInvalidationAcknowledged(inv));
1056 ASSERT_TRUE(CheckPolicyRefreshed());
1058 // Unknown version invalidations fired just after the last fetch time should
1059 // be ignored.
1060 inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
1061 ASSERT_TRUE(IsInvalidationAcknowledged(inv));
1062 ASSERT_TRUE(CheckPolicyNotRefreshed());
1064 AdvanceClock(base::TimeDelta::FromSeconds(
1065 CloudPolicyInvalidator::kUnknownVersionIgnorePeriod - 1));
1066 inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
1067 ASSERT_TRUE(IsInvalidationAcknowledged(inv));
1068 ASSERT_TRUE(CheckPolicyNotRefreshed());
1070 // Unknown version invalidations fired past the ignore period should not be
1071 // ignored.
1072 AdvanceClock(base::TimeDelta::FromSeconds(1));
1073 inv = FireUnknownVersionInvalidation(POLICY_OBJECT_A);
1074 ASSERT_FALSE(IsInvalidationAcknowledged(inv));
1075 ASSERT_TRUE(CheckPolicyRefreshedWithUnknownVersion());
1077 // Verify that received invalidations metrics are correct.
1078 EXPECT_EQ(1, GetInvalidationCount(POLICY_INVALIDATION_TYPE_NO_PAYLOAD));
1079 EXPECT_EQ(3, GetInvalidationCount(POLICY_INVALIDATION_TYPE_NORMAL));
1080 EXPECT_EQ(2,
1081 GetInvalidationCount(POLICY_INVALIDATION_TYPE_NO_PAYLOAD_EXPIRED));
1082 EXPECT_EQ(2, GetInvalidationCount(POLICY_INVALIDATION_TYPE_EXPIRED));
1083 EXPECT_EQ(0, GetHighestHandledInvalidationVersion());
1086 #if defined(OS_CHROMEOS)
1087 INSTANTIATE_TEST_CASE_P(
1088 CloudPolicyInvalidatorUserTypedTestInstance,
1089 CloudPolicyInvalidatorUserTypedTest,
1090 testing::Values(em::DeviceRegisterRequest::USER,
1091 em::DeviceRegisterRequest::DEVICE));
1092 #elif defined(OS_ANDROID)
1093 INSTANTIATE_TEST_CASE_P(
1094 CloudPolicyInvalidatorUserTypedTestInstance,
1095 CloudPolicyInvalidatorUserTypedTest,
1096 testing::Values(em::DeviceRegisterRequest::ANDROID_BROWSER));
1097 #elif defined(OS_IOS)
1098 INSTANTIATE_TEST_CASE_P(
1099 CloudPolicyInvalidatorUserTypedTestInstance,
1100 CloudPolicyInvalidatorUserTypedTest,
1101 testing::Values(em::DeviceRegisterRequest::IOS_BROWSER));
1102 #else
1103 INSTANTIATE_TEST_CASE_P(
1104 CloudPolicyInvalidatorUserTypedTestInstance,
1105 CloudPolicyInvalidatorUserTypedTest,
1106 testing::Values(em::DeviceRegisterRequest::BROWSER));
1107 #endif
1109 } // namespace policy