Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: chrome/browser/policy/cloud/cloud_policy_invalidator_unittest.cc

Issue 19733003: Implement cloud policy invalidations using the invalidation service framework. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #include "base/basictypes.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/sample_map.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "chrome/browser/invalidation/fake_invalidation_service.h"
16 #include "chrome/browser/policy/cloud/cloud_policy_core.h"
17 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"
18 #include "chrome/browser/policy/cloud/cloud_policy_service.h"
19 #include "chrome/browser/policy/cloud/enterprise_metrics.h"
20 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h"
21 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h"
22 #include "chrome/browser/policy/policy_types.h"
23 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
24 #include "policy/policy_constants.h"
25 #include "sync/notifier/invalidation_util.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28
29 namespace policy {
30
31 class CloudPolicyInvalidatorTest : public testing::Test,
32 public CloudPolicyInvalidationHandler {
33 protected:
34 // Policy objects which can be used in tests.
35 enum PolicyObject {
36 POLICY_OBJECT_NONE,
37 POLICY_OBJECT_A,
38 POLICY_OBJECT_B
39 };
40
41 CloudPolicyInvalidatorTest();
42
43 virtual void SetUp() OVERRIDE;
44
45 virtual void TearDown() OVERRIDE;
46
47 // Starts the invalidator which will be tested.
48 void StartInvalidator();
49
50 // Unregister the invalidator.
51 void UnregisterInvalidator();
52
53 // Simulates storing a new policy to the policy store.
54 // |object| determines which policy object the store will report the
55 // invalidator should register for. May be POLICY_OBJECT_NONE for no object.
56 // |invalidation_version| determines what invalidation the store will report.
57 // |policy_changed| determines whether the store will report that the
58 // policy changed.
59 // |timestamp| determines the response timestamp the store will report.
60 void StorePolicy(
61 PolicyObject object,
62 int64 invalidation_version,
63 bool policy_changed,
64 int64 timestamp);
65 void StorePolicy(
66 PolicyObject object,
67 int64 invalidation_version,
68 bool policy_changed) {
69 StorePolicy(object, invalidation_version, policy_changed, ++timestamp_);
70 }
71 void StorePolicy(PolicyObject object, int64 invalidation_version) {
72 StorePolicy(object, invalidation_version, false);
73 }
74 void StorePolicy(PolicyObject object) {
75 StorePolicy(object, 0);
76 }
77
78 // Disables the invalidation service. It is enabled by default.
79 void DisableInvalidationService();
80
81 // Enables the invalidation service. It is enabled by default.
82 void EnableInvalidationService();
83
84 // Causes the invalidation service to fire an invalidation. Returns an ack
85 // handle which be used to verify that the invalidation was acknowledged.
86 syncer::AckHandle FireInvalidation(
87 PolicyObject object,
88 int64 version,
89 const std::string& payload);
90
91 // Causes the invalidation service to fire an invalidation with unknown
92 // version. Returns an ack handle which be used to verify that the
93 // invalidation was acknowledged.
94 syncer::AckHandle FireInvalidation(PolicyObject object);
95
96 // Verifies expectations of the current invalidation info on the policy
97 // client object.
98 void ExpectClientInvalidationInfo(int64 version, const std::string& payload);
99
100 // Verifies expectation that the invalidate callback was not called.
101 void ExpectInvalidateNotCalled();
102
103 // Verifies the expectation that the invalidate callback of the invalidation
104 // handler was called within an appropriate timeframe depending on whether the
105 // invalidation had unknown version.
106 void ExpectInvalidateCalled(bool unknown_version);
107 void ExpectInvalidateCalled() {
108 ExpectInvalidateCalled(true);
109 }
110
111 // Verifies the expectation that the state changed callback of the
112 // invalidation handler was not called.
113 void ExpectStateChangedNotCalled();
114
115 // Verifies the expectation that the state changed callback of the
116 // invalidation handler was called with the given state.
117 void ExpectStateChangedCalled(bool invalidations_enabled);
118
119 // Determines if the invalidation with the given ack handle has been
120 // acknowledged.
121 bool IsInvalidationAcknowledged(const syncer::AckHandle& ack_handle);
122
123 // Get the current count for the given metric.
124 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric);
125 base::HistogramBase::Count GetCount(MetricPolicyInvalidations metric);
126
127 // CloudPolicyInvalidationHandler:
128 virtual void InvalidatePolicy() OVERRIDE;
129 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) OVERRIDE;
130
131 private:
132 // Returns the object id of the given policy object.
133 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const;
134
135 // Get histogram samples for the given histogram.
136 scoped_ptr<base::HistogramSamples> GetHistogramSamples(
137 const std::string& name) const;
138
139 // Objects the invalidator depends on.
140 invalidation::FakeInvalidationService invalidation_service_;
141 MockCloudPolicyClient client_;
142 MockCloudPolicyStore store_;
143 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
144
145 // The invalidator which will be tested.
146 scoped_ptr<CloudPolicyInvalidator> invalidator_;
147
148 // Object ids for the test policy objects.
149 invalidation::ObjectId object_id_a_;
150 invalidation::ObjectId object_id_b_;
151
152 // Increasing policy timestamp.
153 int64 timestamp_;
154
155 // Fake policy values which are alternated to cause the store to report a
156 // changed policy.
157 const char* policy_value_a_;
158 const char* policy_value_b_;
159
160 // The currently used policy value.
161 const char* policy_value_cur_;
162
163 // Stores how many times the invalidate callback was called.
164 int invalidate_callback_count_;
165
166 // Stores how many times the state change callback was called for each state.
167 int state_change_enabled_callback_count_;
168 int state_change_disabled_callback_count_;
169
170 // Stores starting histogram counts for kMetricPolicyRefresh.
171 scoped_ptr<base::HistogramSamples> refresh_samples_;
172
173 // Stores starting histogram counts for kMetricPolicyInvalidations.
174 scoped_ptr<base::HistogramSamples> invalidations_samples_;
175 };
176
177 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest()
178 : task_runner_(new base::TestSimpleTaskRunner()),
179 object_id_a_(135, "asdf"),
180 object_id_b_(246, "zxcv"),
181 timestamp_(123456),
182 policy_value_a_("asdf"),
183 policy_value_b_("zxcv"),
184 policy_value_cur_(policy_value_a_),
185 invalidate_callback_count_(0),
186 state_change_enabled_callback_count_(0),
187 state_change_disabled_callback_count_(0) {}
188
189 void CloudPolicyInvalidatorTest::SetUp() {
190 base::StatisticsRecorder::Initialize();
191 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh);
192 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations);
193 }
194
195 void CloudPolicyInvalidatorTest::TearDown() {
196 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement());
197 }
198
199 void CloudPolicyInvalidatorTest::StartInvalidator() {
200 invalidator_.reset(new CloudPolicyInvalidator(
201 &invalidation_service_,
202 &client_,
203 &store_,
204 task_runner_,
205 this /* invalidation_handler */));
206 }
207
208 void CloudPolicyInvalidatorTest::UnregisterInvalidator() {
209 invalidator_->Unregister();
210 }
211
212 void CloudPolicyInvalidatorTest::StorePolicy(
213 PolicyObject object,
214 int64 invalidation_version,
215 bool policy_changed,
216 int64 timestamp) {
217 enterprise_management::PolicyData* data =
218 new enterprise_management::PolicyData();
219 if (object != POLICY_OBJECT_NONE) {
220 data->set_invalidation_source(GetPolicyObjectId(object).source());
221 data->set_invalidation_name(GetPolicyObjectId(object).name());
222 }
223 data->set_timestamp(timestamp);
224 // Swap the policy value if a policy change is desired.
225 if (policy_changed)
226 policy_value_cur_ = policy_value_cur_ == policy_value_a_ ?
227 policy_value_b_ : policy_value_a_;
228 data->set_policy_value(policy_value_cur_);
229 store_.invalidation_version_ = invalidation_version;
230 store_.policy_.reset(data);
231 base::DictionaryValue policies;
232 policies.SetInteger(
233 key::kMaxInvalidationFetchDelay,
234 CloudPolicyInvalidator::kMaxFetchDelayMin);
235 store_.policy_map_.LoadFrom(
236 &policies,
237 POLICY_LEVEL_MANDATORY,
238 POLICY_SCOPE_MACHINE);
239 store_.NotifyStoreLoaded();
240 }
241
242 void CloudPolicyInvalidatorTest::DisableInvalidationService() {
243 invalidation_service_.SetInvalidatorState(
244 syncer::TRANSIENT_INVALIDATION_ERROR);
245 }
246
247 void CloudPolicyInvalidatorTest::EnableInvalidationService() {
248 invalidation_service_.SetInvalidatorState(syncer::INVALIDATIONS_ENABLED);
249 }
250
251 syncer::AckHandle CloudPolicyInvalidatorTest::FireInvalidation(
252 PolicyObject object,
253 int64 version,
254 const std::string& payload) {
255 return invalidation_service_.EmitInvalidationForTest(
256 GetPolicyObjectId(object),
257 version,
258 payload);
259 }
260
261 syncer::AckHandle CloudPolicyInvalidatorTest::FireInvalidation(
262 PolicyObject object) {
263 return invalidation_service_.EmitInvalidationForTest(
264 GetPolicyObjectId(object),
265 syncer::Invalidation::kUnknownVersion,
266 std::string());
267 }
268
269 void CloudPolicyInvalidatorTest::ExpectClientInvalidationInfo(
270 int64 version,
271 const std::string& payload) {
272 EXPECT_EQ(version, client_.invalidation_version_);
273 EXPECT_EQ(payload, client_.invalidation_payload_);
274 }
275
276 void CloudPolicyInvalidatorTest::ExpectInvalidateNotCalled() {
277 EXPECT_EQ(0, invalidate_callback_count_);
278 task_runner_->RunUntilIdle();
279 EXPECT_EQ(0, invalidate_callback_count_);
280 }
281
282 void CloudPolicyInvalidatorTest::ExpectInvalidateCalled(bool unknown_version) {
283 base::TimeDelta min_delay;
284 base::TimeDelta max_delay = base::TimeDelta::FromMilliseconds(
285 CloudPolicyInvalidator::kMaxFetchDelayMin);
286 if (unknown_version) {
287 base::TimeDelta additional_delay = base::TimeDelta::FromMinutes(
288 CloudPolicyInvalidator::kMissingPayloadDelay);
289 min_delay += additional_delay;
290 max_delay += additional_delay;
291 }
292
293 ASSERT_FALSE(task_runner_->GetPendingTasks().empty());
294 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay;
295 EXPECT_GE(actual_delay, min_delay);
296 EXPECT_LE(actual_delay, max_delay);
297
298 EXPECT_EQ(0, invalidate_callback_count_);
299 task_runner_->RunUntilIdle();
300 EXPECT_EQ(1, invalidate_callback_count_);
301 invalidate_callback_count_ = 0;
302 }
303
304 void CloudPolicyInvalidatorTest::ExpectStateChangedNotCalled() {
305 EXPECT_EQ(0, state_change_enabled_callback_count_);
306 EXPECT_EQ(0, state_change_disabled_callback_count_);
307 }
308
309 void CloudPolicyInvalidatorTest::ExpectStateChangedCalled(
310 bool invalidations_enabled) {
311 if (invalidations_enabled) {
312 EXPECT_EQ(1, state_change_enabled_callback_count_);
313 EXPECT_EQ(0, state_change_disabled_callback_count_);
314 } else {
315 EXPECT_EQ(0, state_change_enabled_callback_count_);
316 EXPECT_EQ(1, state_change_disabled_callback_count_);
317 }
318 state_change_enabled_callback_count_ = 0;
319 state_change_disabled_callback_count_ = 0;
320 }
321
322 bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged(
323 const syncer::AckHandle& ack_handle) {
324 return invalidation_service_.IsInvalidationAcknowledged(ack_handle);
325 }
326
327 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount(
328 MetricPolicyRefresh metric) {
329 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) -
330 refresh_samples_->GetCount(metric);
331 }
332
333 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount(
334 MetricPolicyInvalidations metric) {
335 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) -
336 invalidations_samples_->GetCount(metric);
337 }
338
339 void CloudPolicyInvalidatorTest::InvalidatePolicy() {
340 ++invalidate_callback_count_;
341 }
342
343 void CloudPolicyInvalidatorTest::OnInvalidatorStateChanged(
344 bool invalidations_enabled) {
345 if (invalidator_.get())
346 EXPECT_EQ(invalidations_enabled, invalidator_->invalidations_enabled());
347 if (invalidations_enabled)
348 ++state_change_enabled_callback_count_;
349 else
350 ++state_change_disabled_callback_count_;
351 }
352
353 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId(
354 PolicyObject object) const {
355 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B);
356 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_;
357 }
358
359 scoped_ptr<base::HistogramSamples>
360 CloudPolicyInvalidatorTest::GetHistogramSamples(
361 const std::string& name) const {
362 base::HistogramBase* histogram =
363 base::StatisticsRecorder::FindHistogram(name);
364 if (!histogram)
365 return scoped_ptr<base::HistogramSamples>(new base::SampleMap());
366 return histogram->SnapshotSamples();
367 }
368
369 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) {
370 // No registration when store is not loaded.
371 StartInvalidator();
372 ExpectStateChangedNotCalled();
373 FireInvalidation(POLICY_OBJECT_A);
374 FireInvalidation(POLICY_OBJECT_B);
375 ExpectInvalidateNotCalled();
376
377 // No registration when store is loaded with no invalidation object id.
378 StorePolicy(POLICY_OBJECT_NONE);
379 ExpectStateChangedNotCalled();
380 FireInvalidation(POLICY_OBJECT_A);
381 FireInvalidation(POLICY_OBJECT_B);
382 ExpectInvalidateNotCalled();
383
384 // Check registration when store is loaded for object A.
385 StorePolicy(POLICY_OBJECT_A);
386 ExpectStateChangedCalled(true);
387 FireInvalidation(POLICY_OBJECT_A);
388 ExpectInvalidateCalled();
389 FireInvalidation(POLICY_OBJECT_B);
390 ExpectInvalidateNotCalled();
391 }
392
393 TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) {
394 // Register for object A.
395 StartInvalidator();
396 StorePolicy(POLICY_OBJECT_A);
397 ExpectStateChangedCalled(true);
398 FireInvalidation(POLICY_OBJECT_A);
399 ExpectInvalidateCalled();
400 FireInvalidation(POLICY_OBJECT_B);
401 ExpectInvalidateNotCalled();
402 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
403
404 // Check re-registration for object B. Make sure the pending invalidation for
405 // object A is acknowledged without making the callback.
406 StorePolicy(POLICY_OBJECT_B);
407 ExpectStateChangedNotCalled();
408 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
409 ExpectInvalidateNotCalled();
410
411 // Make sure future invalidations for object A are ignored and for object B
412 // are processed.
413 FireInvalidation(POLICY_OBJECT_A);
414 ExpectInvalidateNotCalled();
415 FireInvalidation(POLICY_OBJECT_B);
416 ExpectInvalidateCalled();
417 }
418
419 TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) {
420 // Register for object A.
421 StartInvalidator();
422 StorePolicy(POLICY_OBJECT_A);
423 ExpectStateChangedCalled(true);
424 FireInvalidation(POLICY_OBJECT_A);
425 ExpectInvalidateCalled();
426
427 // Check unregistration when store is loaded with no invalidation object id.
428 StorePolicy(POLICY_OBJECT_NONE);
429 ExpectStateChangedCalled(false);
430 FireInvalidation(POLICY_OBJECT_A);
431 FireInvalidation(POLICY_OBJECT_B);
432 ExpectInvalidateNotCalled();
433
434 // Check re-registration for object B.
435 StorePolicy(POLICY_OBJECT_B);
436 ExpectStateChangedCalled(true);
437 FireInvalidation(POLICY_OBJECT_B);
438 ExpectInvalidateCalled();
439 }
440
441 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) {
442 // Register and fire invalidation
443 StorePolicy(POLICY_OBJECT_A);
444 StartInvalidator();
445 ExpectStateChangedCalled(true);
446 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 12, "test_payload");
447
448 // Make sure client info is set as soon as the invalidation is received.
449 ExpectClientInvalidationInfo(12, "test_payload");
450 ExpectInvalidateCalled(false /* unknown_version */);
451
452 // Make sure invalidation is not acknowledged until the store is loaded.
453 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
454 ExpectClientInvalidationInfo(12, "test_payload");
455 StorePolicy(POLICY_OBJECT_A, 12);
456 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
457 ExpectClientInvalidationInfo(0, std::string());
458 }
459
460 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) {
461 // Register and fire invalidation with unknown version.
462 StorePolicy(POLICY_OBJECT_A);
463 StartInvalidator();
464 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
465
466 // Make sure client info is not set until after the invalidation callback is
467 // made.
468 ExpectClientInvalidationInfo(0, std::string());
469 ExpectInvalidateCalled();
470 ExpectClientInvalidationInfo(-1, std::string());
471
472 // Make sure invalidation is not acknowledged until the store is loaded.
473 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
474 StorePolicy(POLICY_OBJECT_A, -1);
475 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
476 ExpectClientInvalidationInfo(0, std::string());
477 }
478
479 TEST_F(CloudPolicyInvalidatorTest, Unregister) {
480 StorePolicy(POLICY_OBJECT_A);
481 StartInvalidator();
482 ExpectStateChangedCalled(true);
483 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A);
484
485 // Make sure invalidation is acknowledged when Unregister is called, even
486 // if the store hasn't been reloaded.
487 EXPECT_FALSE(IsInvalidationAcknowledged(ack));
488 UnregisterInvalidator();
489 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
490
491 // Make sure that the invalidate callback is not made.
492 ExpectInvalidateNotCalled();
493
494 // Make sure the change state callback is made.
495 ExpectStateChangedCalled(false);
496 }
497
498 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) {
499 // Generate multiple invalidations.
500 StorePolicy(POLICY_OBJECT_A);
501 StartInvalidator();
502 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1");
503 ExpectClientInvalidationInfo(1, "test1");
504 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2");
505 ExpectClientInvalidationInfo(2, "test2");
506 syncer::AckHandle ack3= FireInvalidation(POLICY_OBJECT_A, 3, "test3");
507 ExpectClientInvalidationInfo(3, "test3");
508
509 // Make sure the replaced invalidations are acknowledged.
510 EXPECT_TRUE(IsInvalidationAcknowledged(ack1));
511 EXPECT_TRUE(IsInvalidationAcknowledged(ack2));
512
513 // Make sure the invalidate callback is called once.
514 ExpectInvalidateCalled(false /* unknown_version */);
515
516 // Make sure that the last invalidation is only acknowledged after the store
517 // is loaded with the latest version.
518 StorePolicy(POLICY_OBJECT_A, 1);
519 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
520 StorePolicy(POLICY_OBJECT_A, 2);
521 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
522 StorePolicy(POLICY_OBJECT_A, 3);
523 EXPECT_TRUE(IsInvalidationAcknowledged(ack3));
524 }
525
526 TEST_F(CloudPolicyInvalidatorTest,
527 HandleMultipleInvalidationsWithUnknownVersion) {
528 // Validate that multiple invalidations with unknown version each generate
529 // unique invalidation version numbers.
530 StorePolicy(POLICY_OBJECT_A);
531 StartInvalidator();
532 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A);
533 ExpectClientInvalidationInfo(0, std::string());
534 ExpectInvalidateCalled();
535 ExpectClientInvalidationInfo(-1, std::string());
536 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A);
537 ExpectClientInvalidationInfo(0, std::string());
538 ExpectInvalidateCalled();
539 ExpectClientInvalidationInfo(-2, std::string());
540 syncer::AckHandle ack3 = FireInvalidation(POLICY_OBJECT_A);
541 ExpectClientInvalidationInfo(0, std::string());
542 ExpectInvalidateCalled();
543 ExpectClientInvalidationInfo(-3, std::string());
544
545 // Make sure the replaced invalidations are acknowledged.
546 EXPECT_TRUE(IsInvalidationAcknowledged(ack1));
547 EXPECT_TRUE(IsInvalidationAcknowledged(ack2));
548
549 // Make sure that the last invalidation is only acknowledged after the store
550 // is loaded with the last unknown version.
551 StorePolicy(POLICY_OBJECT_A, -1);
552 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
553 StorePolicy(POLICY_OBJECT_A, -2);
554 EXPECT_FALSE(IsInvalidationAcknowledged(ack3));
555 StorePolicy(POLICY_OBJECT_A, -3);
556 EXPECT_TRUE(IsInvalidationAcknowledged(ack3));
557 }
558
559 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeInvalidateCallback) {
560 // Generate an invalidation.
561 StorePolicy(POLICY_OBJECT_A);
562 StartInvalidator();
563 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test");
564
565 // Ensure that the invalidate callback is not made and the invalidation is
566 // acknowledged if the store is loaded with the latest version before the
567 // callback is invoked.
568 StorePolicy(POLICY_OBJECT_A, 3);
569 EXPECT_TRUE(IsInvalidationAcknowledged(ack));
570 ExpectInvalidateNotCalled();
571 }
572
573 TEST_F(CloudPolicyInvalidatorTest, StateChanged) {
574 // Before registration, changes to the invalidation service state should not
575 // generate change state notifications.
576 StartInvalidator();
577 DisableInvalidationService();
578 EnableInvalidationService();
579 ExpectStateChangedNotCalled();
580
581 // After registration, changes to the invalidation service state should
582 // generate notifications.
583 StorePolicy(POLICY_OBJECT_A);
584 ExpectStateChangedCalled(true);
585 DisableInvalidationService();
586 ExpectStateChangedCalled(false);
587 DisableInvalidationService();
588 ExpectStateChangedNotCalled();
589 EnableInvalidationService();
590 ExpectStateChangedCalled(true);
591 EnableInvalidationService();
592 ExpectStateChangedNotCalled();
593
594 // When the invalidation service is enabled, changes to the registration
595 // state should generate notifications.
596 StorePolicy(POLICY_OBJECT_NONE);
597 ExpectStateChangedCalled(false);
598 StorePolicy(POLICY_OBJECT_NONE);
599 ExpectStateChangedNotCalled();
600 StorePolicy(POLICY_OBJECT_A);
601 ExpectStateChangedCalled(true);
602 StorePolicy(POLICY_OBJECT_A);
603 ExpectStateChangedNotCalled();
604
605 // When the invalidation service is disabled, changes to the registration
606 // state should not generate notifications.
607 DisableInvalidationService();
608 ExpectStateChangedCalled(false);
609 StorePolicy(POLICY_OBJECT_NONE);
610 StorePolicy(POLICY_OBJECT_A);
611 ExpectStateChangedNotCalled();
612 }
613
614 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) {
615 // Store loads occurring before invalidation registration are not counted.
616 StartInvalidator();
617 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */);
618 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */);
619 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshChanged));
620 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshChangedNoInvalidations));
621 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshUnchanged));
622 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshInvalidatedChanged));
623 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshInvalidatedUnchanged));
624 }
625
626 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsNoInvalidations) {
627 // Store loads occurring while registered should be differentiated depending
628 // on whether the invalidation service was enabled or not.
629 StorePolicy(POLICY_OBJECT_A);
630 StartInvalidator();
631 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
632 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
633 DisableInvalidationService();
634 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
635 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
636 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
637 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
638 EXPECT_EQ(1, GetCount(kMetricPolicyRefreshChanged));
639 EXPECT_EQ(2, GetCount(kMetricPolicyRefreshChangedNoInvalidations));
640 EXPECT_EQ(3, GetCount(kMetricPolicyRefreshUnchanged));
641 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshInvalidatedChanged));
642 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshInvalidatedUnchanged));
643 }
644
645 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsStoreSameTimestamp) {
646 // Store loads with the same timestamp as the load which causes registration
647 // are not counted.
648 StartInvalidator();
649 StorePolicy(
650 POLICY_OBJECT_A, 0, false /* policy_changed */, 12 /* timestamp */);
651 StorePolicy(
652 POLICY_OBJECT_A, 0, false /* policy_changed */, 12 /* timestamp */);
653 StorePolicy(
654 POLICY_OBJECT_A, 0, true /* policy_changed */, 12 /* timestamp */);
655
656 // The next load with a different timestamp counts.
657 StorePolicy(
658 POLICY_OBJECT_A, 0, true /* policy_changed */, 13 /* timestamp */);
659
660 EXPECT_EQ(1, GetCount(kMetricPolicyRefreshChanged));
661 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshChangedNoInvalidations));
662 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshUnchanged));
663 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshInvalidatedChanged));
664 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshInvalidatedUnchanged));
665 }
666
667 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) {
668 // Store loads after an invalidation are counted as invalidated, even if
669 // the loads do not result in the invalidation being acknowledged.
670 StartInvalidator();
671 StorePolicy(POLICY_OBJECT_A);
672 FireInvalidation(POLICY_OBJECT_A, 5, "test");
673 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
674 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
675 StorePolicy(POLICY_OBJECT_A, 5, true /* policy_changed */);
676
677 // Store loads after the invalidation is complete are not counted as
678 // invalidated.
679 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
680 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
681 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
682 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
683 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
684 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */);
685 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */);
686
687 EXPECT_EQ(3, GetCount(kMetricPolicyRefreshChanged));
688 EXPECT_EQ(0, GetCount(kMetricPolicyRefreshChangedNoInvalidations));
689 EXPECT_EQ(4, GetCount(kMetricPolicyRefreshUnchanged));
690 EXPECT_EQ(2, GetCount(kMetricPolicyRefreshInvalidatedChanged));
691 EXPECT_EQ(1, GetCount(kMetricPolicyRefreshInvalidatedUnchanged));
692 }
693
694 TEST_F(CloudPolicyInvalidatorTest, InvalidationMetrics) {
695 // Generate a mix of versioned and unknown-version invalidations.
696 StorePolicy(POLICY_OBJECT_A);
697 StartInvalidator();
698 FireInvalidation(POLICY_OBJECT_B);
699 FireInvalidation(POLICY_OBJECT_A);
700 FireInvalidation(POLICY_OBJECT_B, 1, "test");
701 FireInvalidation(POLICY_OBJECT_A, 1, "test");
702 FireInvalidation(POLICY_OBJECT_A, 2, "test");
703 FireInvalidation(POLICY_OBJECT_A);
704 FireInvalidation(POLICY_OBJECT_A);
705 FireInvalidation(POLICY_OBJECT_A, 3, "test");
706 FireInvalidation(POLICY_OBJECT_A, 4, "test");
707
708 // Verify that received invalidations metrics are correct.
709 EXPECT_EQ(3, GetCount(kMetricPolicyInvalidationsNoPayload));
710 EXPECT_EQ(4, GetCount(kMetricPolicyInvalidationsPayload));
711 }
712
713 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698