Chromium Code Reviews| 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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 11 #include "base/metrics/histogram_samples.h" | 13 #include "base/metrics/histogram_samples.h" |
| 12 #include "base/metrics/sample_map.h" | 14 #include "base/metrics/sample_map.h" |
| 13 #include "base/metrics/statistics_recorder.h" | 15 #include "base/metrics/statistics_recorder.h" |
| 14 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 16 #include "base/values.h" | 18 #include "base/values.h" |
| 17 #include "chrome/browser/invalidation/fake_invalidation_service.h" | 19 #include "chrome/browser/invalidation/fake_invalidation_service.h" |
| 20 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | |
| 18 #include "chrome/browser/policy/cloud/cloud_policy_core.h" | 21 #include "chrome/browser/policy/cloud/cloud_policy_core.h" |
| 19 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" | 22 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" |
| 20 #include "chrome/browser/policy/cloud/cloud_policy_service.h" | |
| 21 #include "chrome/browser/policy/cloud/enterprise_metrics.h" | 23 #include "chrome/browser/policy/cloud/enterprise_metrics.h" |
| 22 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 24 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
| 23 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 25 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
| 24 #include "chrome/browser/policy/policy_types.h" | 26 #include "chrome/browser/policy/policy_types.h" |
| 25 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | 27 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" |
| 26 #include "policy/policy_constants.h" | 28 #include "policy/policy_constants.h" |
| 27 #include "sync/notifier/invalidation_util.h" | 29 #include "sync/notifier/invalidation_util.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 32 |
| 31 namespace policy { | 33 namespace policy { |
| 32 | 34 |
| 33 class CloudPolicyInvalidatorTest : public testing::Test, | 35 class CloudPolicyInvalidatorTest : public testing::Test { |
| 34 public CloudPolicyInvalidationHandler { | |
| 35 protected: | 36 protected: |
| 36 // Policy objects which can be used in tests. | 37 // Policy objects which can be used in tests. |
| 37 enum PolicyObject { | 38 enum PolicyObject { |
| 38 POLICY_OBJECT_NONE, | 39 POLICY_OBJECT_NONE, |
| 39 POLICY_OBJECT_A, | 40 POLICY_OBJECT_A, |
| 40 POLICY_OBJECT_B | 41 POLICY_OBJECT_B |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 CloudPolicyInvalidatorTest(); | 44 CloudPolicyInvalidatorTest(); |
| 44 | 45 |
| 45 virtual void SetUp() OVERRIDE; | 46 virtual void SetUp() OVERRIDE; |
| 46 | 47 |
| 47 virtual void TearDown() OVERRIDE; | 48 virtual void TearDown() OVERRIDE; |
| 48 | 49 |
| 49 // Starts the invalidator which will be tested. | 50 // Starts the invalidator which will be tested. |
| 50 void StartInvalidator(bool initialize); | 51 // |initialize| determines if the invalidator should be initialized. |
| 52 // |start_refresh_scheduler| determines if the refresh scheduler should start. | |
| 53 void StartInvalidator(bool initialize, bool start_refresh_scheduler); | |
| 51 void StartInvalidator() { | 54 void StartInvalidator() { |
| 52 StartInvalidator(true /* initialize */); | 55 StartInvalidator(true /* initialize */, true /* start_refresh_scheduler */); |
| 53 } | 56 } |
| 54 | 57 |
| 58 // Calls Initialize on the invalidator. | |
| 59 void InitializeInvalidator(); | |
| 60 | |
| 61 // Calls Shutdown on the invalidator. Test must call DestroyInvalidator | |
| 62 // afterwards to prevent Shutdown from being called twice. | |
| 63 void ShutdownInvalidator(); | |
| 64 | |
| 65 // Destroys the invalidator. | |
| 66 void DestroyInvalidator(); | |
| 67 | |
| 68 // Connects the cloud policy core. | |
| 69 void ConnectCore(); | |
| 70 | |
| 71 // Starts the refresh scheduler. | |
| 72 void StartRefreshScheduler(); | |
| 73 | |
| 74 // Disconnects the cloud policy core. | |
| 75 void DisconnectCore(); | |
| 76 | |
| 55 // Simulates storing a new policy to the policy store. | 77 // Simulates storing a new policy to the policy store. |
| 56 // |object| determines which policy object the store will report the | 78 // |object| determines which policy object the store will report the |
| 57 // invalidator should register for. May be POLICY_OBJECT_NONE for no object. | 79 // invalidator should register for. May be POLICY_OBJECT_NONE for no object. |
| 58 // |invalidation_version| determines what invalidation the store will report. | 80 // |invalidation_version| determines what invalidation the store will report. |
| 59 // |policy_changed| determines whether the store will report that the | 81 // |policy_changed| determines whether the store will report that the |
| 60 // policy changed. | 82 // policy changed. |
| 61 // |timestamp| determines the response timestamp the store will report. | 83 // |timestamp| determines the response timestamp the store will report. |
| 62 void StorePolicy( | 84 void StorePolicy( |
| 63 PolicyObject object, | 85 PolicyObject object, |
| 64 int64 invalidation_version, | 86 int64 invalidation_version, |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 91 const std::string& payload); | 113 const std::string& payload); |
| 92 | 114 |
| 93 // Causes the invalidation service to fire an invalidation with unknown | 115 // Causes the invalidation service to fire an invalidation with unknown |
| 94 // version. Returns an ack handle which be used to verify that the | 116 // version. Returns an ack handle which be used to verify that the |
| 95 // invalidation was acknowledged. | 117 // invalidation was acknowledged. |
| 96 syncer::AckHandle FireInvalidation(PolicyObject object); | 118 syncer::AckHandle FireInvalidation(PolicyObject object); |
| 97 | 119 |
| 98 // Checks the expected value of the currently set invalidation info. | 120 // Checks the expected value of the currently set invalidation info. |
| 99 bool CheckInvalidationInfo(int64 version, const std::string& payload); | 121 bool CheckInvalidationInfo(int64 version, const std::string& payload); |
| 100 | 122 |
| 101 // Checks that the invalidate callback was not called. | 123 // Checks that the policy was not refreshed due to an invalidation. |
| 102 bool CheckInvalidateNotCalled(); | 124 bool CheckPolicyNotRefreshed(); |
| 103 | 125 |
| 104 // Checks that the invalidate callback was called within an appropriate | 126 // Checks that the policy was refreshed due to an invalidation within an |
| 105 // timeframe depending on whether the invalidation had unknown version. | 127 // appropriate timeframe depending on whether the invalidation had unknown |
| 106 bool CheckInvalidateCalled(bool unknown_version); | 128 // version. |
| 107 bool CheckInvalidateCalled() { | 129 bool CheckPolicyRefreshed(bool unknown_version); |
| 108 return CheckInvalidateCalled(true); | 130 bool CheckPolicyRefreshed() { |
| 131 return CheckPolicyRefreshed(true); | |
| 109 } | 132 } |
| 110 | 133 |
| 111 // Checks that the state changed callback of the invalidation handler was not | 134 // Checks that the call to change the invalidations enabled state was not |
| 112 // called. | 135 // performed. |
| 113 bool CheckStateChangedNotCalled(); | 136 bool CheckStateChangedNotCalled(); |
| 114 | 137 |
| 115 // Checks that the state changed callback of the invalidation handler was | 138 // Checks that the call to change the invalidations enabled state was |
| 116 // called with the given state. | 139 // performed with the given state. |
| 117 bool CheckStateChangedCalled(bool invalidations_enabled); | 140 bool CheckStateChangedCalled(bool invalidations_enabled); |
| 118 | 141 |
| 119 // Determines if the invalidation with the given ack handle has been | 142 // Determines if the invalidation with the given ack handle has been |
| 120 // acknowledged. | 143 // acknowledged. |
| 121 bool IsInvalidationAcknowledged(const syncer::AckHandle& ack_handle); | 144 bool IsInvalidationAcknowledged(const syncer::AckHandle& ack_handle); |
| 122 | 145 |
| 146 // Determines if the invalidator has registered for an object with the | |
| 147 // invalidation service. | |
| 148 bool IsInvalidatorRegistered(); | |
| 149 | |
| 123 // Get the current count for the given metric. | 150 // Get the current count for the given metric. |
| 124 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); | 151 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); |
| 125 base::HistogramBase::Count GetInvalidationCount(bool with_payload); | 152 base::HistogramBase::Count GetInvalidationCount(bool with_payload); |
| 126 | 153 |
| 127 // CloudPolicyInvalidationHandler: | 154 private: |
| 128 virtual void SetInvalidationInfo( | 155 // Get a pointer to the invalidation service. |
| 129 int64 version, | 156 invalidation::InvalidationService* GetInvalidationService(); |
| 130 const std::string& payload) OVERRIDE; | |
| 131 virtual void InvalidatePolicy() OVERRIDE; | |
| 132 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) OVERRIDE; | |
| 133 | 157 |
| 134 private: | |
| 135 // Returns the object id of the given policy object. | 158 // Returns the object id of the given policy object. |
| 136 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; | 159 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; |
| 137 | 160 |
| 138 // Get histogram samples for the given histogram. | 161 // Get histogram samples for the given histogram. |
| 139 scoped_ptr<base::HistogramSamples> GetHistogramSamples( | 162 scoped_ptr<base::HistogramSamples> GetHistogramSamples( |
| 140 const std::string& name) const; | 163 const std::string& name) const; |
| 141 | 164 |
| 142 // Objects the invalidator depends on. | 165 // Objects the invalidator depends on. |
| 143 invalidation::FakeInvalidationService invalidation_service_; | 166 invalidation::FakeInvalidationService invalidation_service_; |
| 144 MockCloudPolicyStore store_; | 167 MockCloudPolicyStore store_; |
| 168 CloudPolicyCore core_; | |
| 145 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 169 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 146 | 170 |
| 147 // The invalidator which will be tested. | 171 // The invalidator which will be tested. |
| 148 scoped_ptr<CloudPolicyInvalidator> invalidator_; | 172 scoped_ptr<CloudPolicyInvalidator> invalidator_; |
| 149 | 173 |
| 150 // The latest invalidation info set by the invalidator. | |
| 151 int64 invalidation_version_; | |
| 152 std::string invalidation_payload_; | |
| 153 | |
| 154 // Object ids for the test policy objects. | 174 // Object ids for the test policy objects. |
| 155 invalidation::ObjectId object_id_a_; | 175 invalidation::ObjectId object_id_a_; |
| 156 invalidation::ObjectId object_id_b_; | 176 invalidation::ObjectId object_id_b_; |
| 157 | 177 |
| 158 // Increasing policy timestamp. | 178 // Increasing policy timestamp. |
| 159 int64 timestamp_; | 179 int64 timestamp_; |
| 160 | 180 |
| 161 // Fake policy values which are alternated to cause the store to report a | 181 // Fake policy values which are alternated to cause the store to report a |
| 162 // changed policy. | 182 // changed policy. |
| 163 const char* policy_value_a_; | 183 const char* policy_value_a_; |
| 164 const char* policy_value_b_; | 184 const char* policy_value_b_; |
| 165 | 185 |
| 166 // The currently used policy value. | 186 // The currently used policy value. |
| 167 const char* policy_value_cur_; | 187 const char* policy_value_cur_; |
| 168 | 188 |
| 169 // Stores how many times the invalidate callback was called. | 189 // The latest invalidation status counters read from the invalidator. |
| 170 int invalidate_callback_count_; | 190 int last_enabled_count_; |
| 171 | 191 int last_disabled_count_; |
| 172 // Stores how many times the state change callback was called for each state. | |
| 173 int state_change_enabled_callback_count_; | |
| 174 int state_change_disabled_callback_count_; | |
| 175 | 192 |
| 176 // Stores starting histogram counts for kMetricPolicyRefresh. | 193 // Stores starting histogram counts for kMetricPolicyRefresh. |
| 177 scoped_ptr<base::HistogramSamples> refresh_samples_; | 194 scoped_ptr<base::HistogramSamples> refresh_samples_; |
| 178 | 195 |
| 179 // Stores starting histogram counts for kMetricPolicyInvalidations. | 196 // Stores starting histogram counts for kMetricPolicyInvalidations. |
| 180 scoped_ptr<base::HistogramSamples> invalidations_samples_; | 197 scoped_ptr<base::HistogramSamples> invalidations_samples_; |
| 198 | |
| 199 // Initialize message loop. | |
| 200 base::MessageLoop loop_; | |
| 181 }; | 201 }; |
| 182 | 202 |
| 183 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() | 203 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() |
| 184 : task_runner_(new base::TestSimpleTaskRunner()), | 204 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, |
| 185 invalidation_version_(0), | 205 std::string()), |
| 206 &store_), | |
| 207 task_runner_(new base::TestSimpleTaskRunner()), | |
| 186 object_id_a_(135, "asdf"), | 208 object_id_a_(135, "asdf"), |
| 187 object_id_b_(246, "zxcv"), | 209 object_id_b_(246, "zxcv"), |
| 188 timestamp_(123456), | 210 timestamp_(123456), |
| 189 policy_value_a_("asdf"), | 211 policy_value_a_("asdf"), |
| 190 policy_value_b_("zxcv"), | 212 policy_value_b_("zxcv"), |
| 191 policy_value_cur_(policy_value_a_), | 213 policy_value_cur_(policy_value_a_), |
| 192 invalidate_callback_count_(0), | 214 last_enabled_count_(0), |
| 193 state_change_enabled_callback_count_(0), | 215 last_disabled_count_(0) {} |
| 194 state_change_disabled_callback_count_(0) {} | |
| 195 | 216 |
| 196 void CloudPolicyInvalidatorTest::SetUp() { | 217 void CloudPolicyInvalidatorTest::SetUp() { |
| 197 base::StatisticsRecorder::Initialize(); | 218 base::StatisticsRecorder::Initialize(); |
| 198 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); | 219 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); |
| 199 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); | 220 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); |
| 200 } | 221 } |
| 201 | 222 |
| 202 void CloudPolicyInvalidatorTest::TearDown() { | 223 void CloudPolicyInvalidatorTest::TearDown() { |
| 203 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement()); | 224 EXPECT_FALSE(invalidation_service_.ReceivedInvalidAcknowledgement()); |
| 204 if (invalidator_) | 225 if (invalidator_) |
| 205 invalidator_->Shutdown(); | 226 invalidator_->Shutdown(); |
| 227 core_.Disconnect(); | |
| 206 } | 228 } |
| 207 | 229 |
| 208 void CloudPolicyInvalidatorTest::StartInvalidator(bool initialize) { | 230 void CloudPolicyInvalidatorTest::StartInvalidator( |
| 209 invalidator_.reset(new CloudPolicyInvalidator( | 231 bool initialize, |
| 210 this /* invalidation_handler */, | 232 bool start_refresh_scheduler) { |
| 211 &store_, | 233 invalidator_.reset(new CloudPolicyInvalidator(&core_, task_runner_)); |
| 212 task_runner_)); | 234 if (start_refresh_scheduler) { |
| 235 ConnectCore(); | |
| 236 StartRefreshScheduler(); | |
| 237 } | |
| 213 if (initialize) | 238 if (initialize) |
| 214 invalidator_->InitializeWithService(&invalidation_service_); | 239 InitializeInvalidator(); |
| 240 } | |
| 241 | |
| 242 void CloudPolicyInvalidatorTest::InitializeInvalidator() { | |
| 243 invalidator_->Initialize(base::Bind( | |
| 244 &CloudPolicyInvalidatorTest::GetInvalidationService, | |
| 245 base::Unretained(this))); | |
| 246 } | |
| 247 | |
| 248 void CloudPolicyInvalidatorTest::ShutdownInvalidator() { | |
| 249 invalidator_->Shutdown(); | |
| 250 } | |
| 251 | |
| 252 void CloudPolicyInvalidatorTest::DestroyInvalidator() { | |
| 253 invalidator_.reset(); | |
| 254 } | |
| 255 | |
| 256 void CloudPolicyInvalidatorTest::ConnectCore() { | |
| 257 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient())); | |
| 258 } | |
| 259 | |
| 260 void CloudPolicyInvalidatorTest::StartRefreshScheduler() { | |
| 261 core_.StartRefreshScheduler(); | |
| 262 } | |
| 263 | |
| 264 void CloudPolicyInvalidatorTest::DisconnectCore() { | |
| 265 core_.Disconnect(); | |
| 215 } | 266 } |
| 216 | 267 |
| 217 void CloudPolicyInvalidatorTest::StorePolicy( | 268 void CloudPolicyInvalidatorTest::StorePolicy( |
| 218 PolicyObject object, | 269 PolicyObject object, |
| 219 int64 invalidation_version, | 270 int64 invalidation_version, |
| 220 bool policy_changed, | 271 bool policy_changed, |
| 221 int64 timestamp) { | 272 int64 timestamp) { |
| 222 enterprise_management::PolicyData* data = | 273 enterprise_management::PolicyData* data = |
| 223 new enterprise_management::PolicyData(); | 274 new enterprise_management::PolicyData(); |
| 224 if (object != POLICY_OBJECT_NONE) { | 275 if (object != POLICY_OBJECT_NONE) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 PolicyObject object) { | 318 PolicyObject object) { |
| 268 return invalidation_service_.EmitInvalidationForTest( | 319 return invalidation_service_.EmitInvalidationForTest( |
| 269 GetPolicyObjectId(object), | 320 GetPolicyObjectId(object), |
| 270 syncer::Invalidation::kUnknownVersion, | 321 syncer::Invalidation::kUnknownVersion, |
| 271 std::string()); | 322 std::string()); |
| 272 } | 323 } |
| 273 | 324 |
| 274 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo( | 325 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo( |
| 275 int64 version, | 326 int64 version, |
| 276 const std::string& payload) { | 327 const std::string& payload) { |
| 277 return version == invalidation_version_ && payload == invalidation_payload_; | 328 MockCloudPolicyClient* client = |
| 329 static_cast<MockCloudPolicyClient*>(core_.client()); | |
| 330 return version == client->invalidation_version_ && | |
| 331 payload == client->invalidation_payload_; | |
| 278 } | 332 } |
| 279 | 333 |
| 280 bool CloudPolicyInvalidatorTest::CheckInvalidateNotCalled() { | 334 bool CloudPolicyInvalidatorTest::CheckPolicyNotRefreshed() { |
| 281 bool result = true; | 335 int start_count = invalidator_->policy_refresh_count(); |
| 282 if (invalidate_callback_count_ != 0) | |
| 283 result = false; | |
| 284 task_runner_->RunUntilIdle(); | 336 task_runner_->RunUntilIdle(); |
| 285 if (invalidate_callback_count_ != 0) | 337 return invalidator_->policy_refresh_count() == start_count; |
| 286 result = false; | |
| 287 return result; | |
| 288 } | 338 } |
| 289 | 339 |
| 290 bool CloudPolicyInvalidatorTest::CheckInvalidateCalled(bool unknown_version) { | 340 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(bool unknown_version) { |
| 291 base::TimeDelta min_delay; | 341 base::TimeDelta min_delay; |
| 292 base::TimeDelta max_delay = base::TimeDelta::FromMilliseconds( | 342 base::TimeDelta max_delay = base::TimeDelta::FromMilliseconds( |
| 293 CloudPolicyInvalidator::kMaxFetchDelayMin); | 343 CloudPolicyInvalidator::kMaxFetchDelayMin); |
| 294 if (unknown_version) { | 344 if (unknown_version) { |
| 295 base::TimeDelta additional_delay = base::TimeDelta::FromMinutes( | 345 base::TimeDelta additional_delay = base::TimeDelta::FromMinutes( |
| 296 CloudPolicyInvalidator::kMissingPayloadDelay); | 346 CloudPolicyInvalidator::kMissingPayloadDelay); |
| 297 min_delay += additional_delay; | 347 min_delay += additional_delay; |
| 298 max_delay += additional_delay; | 348 max_delay += additional_delay; |
| 299 } | 349 } |
| 300 | 350 |
| 301 if (task_runner_->GetPendingTasks().empty()) | 351 if (task_runner_->GetPendingTasks().empty()) |
| 302 return false; | 352 return false; |
| 303 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; | 353 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; |
| 304 EXPECT_GE(actual_delay, min_delay); | 354 EXPECT_GE(actual_delay, min_delay); |
| 305 EXPECT_LE(actual_delay, max_delay); | 355 EXPECT_LE(actual_delay, max_delay); |
| 306 | 356 |
| 307 bool result = true; | 357 int start_count = invalidator_->policy_refresh_count(); |
|
Mattias Nissler (ping if slow)
2013/09/02 11:54:52
nit: const
Steve Condie
2013/09/03 04:41:56
Done.
| |
| 308 if (invalidate_callback_count_ != 0) | |
| 309 result = false; | |
| 310 task_runner_->RunUntilIdle(); | 358 task_runner_->RunUntilIdle(); |
| 311 if (invalidate_callback_count_ != 1) | 359 return invalidator_->policy_refresh_count() == start_count + 1; |
| 312 result = false; | |
| 313 invalidate_callback_count_ = 0; | |
| 314 return result; | |
| 315 } | 360 } |
| 316 | 361 |
| 317 bool CloudPolicyInvalidatorTest::CheckStateChangedNotCalled() { | 362 bool CloudPolicyInvalidatorTest::CheckStateChangedNotCalled() { |
| 318 return state_change_enabled_callback_count_ == 0 && | 363 bool result = |
| 319 state_change_disabled_callback_count_ == 0; | 364 last_enabled_count_ == invalidator_->invalidations_enabled_count() && |
| 365 last_disabled_count_ == invalidator_->invalidations_disabled_count(); | |
| 366 last_enabled_count_ = invalidator_->invalidations_enabled_count(); | |
| 367 last_disabled_count_ = invalidator_->invalidations_disabled_count(); | |
| 368 return result; | |
| 320 } | 369 } |
| 321 | 370 |
| 322 bool CloudPolicyInvalidatorTest::CheckStateChangedCalled( | 371 bool CloudPolicyInvalidatorTest::CheckStateChangedCalled( |
| 323 bool invalidations_enabled) { | 372 bool invalidations_enabled) { |
| 324 int expected_enabled_count_ = invalidations_enabled ? 1 : 0; | 373 int expected_enabled_count_ = last_enabled_count_; |
| 325 int expected_disabled_count_ = invalidations_enabled ? 0 : 1; | 374 int expected_disabled_count_ = last_disabled_count_; |
| 326 bool result = state_change_enabled_callback_count_ == expected_enabled_count_ | 375 if (invalidations_enabled) |
| 327 && state_change_disabled_callback_count_ == expected_disabled_count_; | 376 expected_enabled_count_++; |
| 328 state_change_enabled_callback_count_ = 0; | 377 else |
| 329 state_change_disabled_callback_count_ = 0; | 378 expected_disabled_count_++; |
| 379 | |
| 380 bool result = | |
| 381 invalidator_->invalidations_enabled_count() == expected_enabled_count_ && | |
| 382 invalidator_->invalidations_disabled_count() == expected_disabled_count_; | |
| 383 last_enabled_count_ = invalidator_->invalidations_enabled_count(); | |
| 384 last_disabled_count_ = invalidator_->invalidations_disabled_count(); | |
| 330 return result; | 385 return result; |
| 331 } | 386 } |
| 332 | 387 |
| 333 bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged( | 388 bool CloudPolicyInvalidatorTest::IsInvalidationAcknowledged( |
| 334 const syncer::AckHandle& ack_handle) { | 389 const syncer::AckHandle& ack_handle) { |
| 335 return invalidation_service_.IsInvalidationAcknowledged(ack_handle); | 390 return invalidation_service_.IsInvalidationAcknowledged(ack_handle); |
| 336 } | 391 } |
| 337 | 392 |
| 393 bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() { | |
| 394 return !invalidation_service_.invalidator_registrar() | |
| 395 .GetRegisteredIds(invalidator_.get()).empty(); | |
| 396 } | |
| 397 | |
| 338 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount( | 398 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetCount( |
| 339 MetricPolicyRefresh metric) { | 399 MetricPolicyRefresh metric) { |
| 340 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) - | 400 return GetHistogramSamples(kMetricPolicyRefresh)->GetCount(metric) - |
| 341 refresh_samples_->GetCount(metric); | 401 refresh_samples_->GetCount(metric); |
| 342 } | 402 } |
| 343 | 403 |
| 344 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( | 404 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( |
| 345 bool with_payload) { | 405 bool with_payload) { |
| 346 int metric = with_payload ? 1 : 0; | 406 int metric = with_payload ? 1 : 0; |
| 347 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) - | 407 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) - |
| 348 invalidations_samples_->GetCount(metric); | 408 invalidations_samples_->GetCount(metric); |
| 349 } | 409 } |
| 350 | 410 |
| 351 void CloudPolicyInvalidatorTest::SetInvalidationInfo( | 411 invalidation::InvalidationService* |
| 352 int64 version, | 412 CloudPolicyInvalidatorTest::GetInvalidationService() { |
| 353 const std::string& payload) { | 413 return &invalidation_service_; |
| 354 invalidation_version_ = version; | |
| 355 invalidation_payload_ = payload; | |
| 356 } | |
| 357 | |
| 358 void CloudPolicyInvalidatorTest::InvalidatePolicy() { | |
| 359 ++invalidate_callback_count_; | |
| 360 } | |
| 361 | |
| 362 void CloudPolicyInvalidatorTest::OnInvalidatorStateChanged( | |
| 363 bool invalidations_enabled) { | |
| 364 if (invalidator_.get()) | |
| 365 EXPECT_EQ(invalidations_enabled, invalidator_->invalidations_enabled()); | |
| 366 if (invalidations_enabled) | |
| 367 ++state_change_enabled_callback_count_; | |
| 368 else | |
| 369 ++state_change_disabled_callback_count_; | |
| 370 } | 414 } |
| 371 | 415 |
| 372 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId( | 416 const invalidation::ObjectId& CloudPolicyInvalidatorTest::GetPolicyObjectId( |
| 373 PolicyObject object) const { | 417 PolicyObject object) const { |
| 374 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B); | 418 EXPECT_TRUE(object == POLICY_OBJECT_A || object == POLICY_OBJECT_B); |
| 375 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_; | 419 return object == POLICY_OBJECT_A ? object_id_a_ : object_id_b_; |
| 376 } | 420 } |
| 377 | 421 |
| 378 scoped_ptr<base::HistogramSamples> | 422 scoped_ptr<base::HistogramSamples> |
| 379 CloudPolicyInvalidatorTest::GetHistogramSamples( | 423 CloudPolicyInvalidatorTest::GetHistogramSamples( |
| 380 const std::string& name) const { | 424 const std::string& name) const { |
| 381 base::HistogramBase* histogram = | 425 base::HistogramBase* histogram = |
| 382 base::StatisticsRecorder::FindHistogram(name); | 426 base::StatisticsRecorder::FindHistogram(name); |
| 383 if (!histogram) | 427 if (!histogram) |
| 384 return scoped_ptr<base::HistogramSamples>(new base::SampleMap()); | 428 return scoped_ptr<base::HistogramSamples>(new base::SampleMap()); |
| 385 return histogram->SnapshotSamples(); | 429 return histogram->SnapshotSamples(); |
| 386 } | 430 } |
| 387 | 431 |
| 388 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) { | 432 TEST_F(CloudPolicyInvalidatorTest, Uninitialized) { |
| 389 // No invalidations should be processed if the invalidator is not intialized. | 433 // No invalidations should be processed if the invalidator is not initialized. |
| 390 StartInvalidator(false /* initialize */); | 434 StartInvalidator(false /* initialize */, true /* start_refresh_scheduler */); |
| 391 StorePolicy(POLICY_OBJECT_A); | 435 StorePolicy(POLICY_OBJECT_A); |
| 436 EXPECT_FALSE(IsInvalidatorRegistered()); | |
| 392 FireInvalidation(POLICY_OBJECT_A); | 437 FireInvalidation(POLICY_OBJECT_A); |
| 393 EXPECT_TRUE(CheckInvalidateNotCalled()); | 438 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 439 } | |
| 440 | |
| 441 TEST_F(CloudPolicyInvalidatorTest, RefreshSchedulerNotStarted) { | |
| 442 // No invalidations should be processed if the refresh scheduler is not | |
| 443 // started. | |
| 444 StartInvalidator(true /* initialize */, false /* start_refresh_scheduler */); | |
| 445 StorePolicy(POLICY_OBJECT_A); | |
| 446 EXPECT_FALSE(IsInvalidatorRegistered()); | |
| 447 FireInvalidation(POLICY_OBJECT_A); | |
| 448 EXPECT_TRUE(CheckPolicyNotRefreshed()); | |
| 449 } | |
| 450 | |
| 451 TEST_F(CloudPolicyInvalidatorTest, DisconnectCoreThenInitialize) { | |
| 452 // No invalidations should be processed if the core is disconnected before | |
| 453 // initialization. | |
| 454 StartInvalidator(false /* initialize */, true /* start_refresh_scheduler */); | |
| 455 DisconnectCore(); | |
| 456 InitializeInvalidator(); | |
| 457 StorePolicy(POLICY_OBJECT_A); | |
| 458 EXPECT_FALSE(IsInvalidatorRegistered()); | |
| 459 FireInvalidation(POLICY_OBJECT_A); | |
| 460 EXPECT_TRUE(CheckPolicyNotRefreshed()); | |
| 461 } | |
| 462 | |
| 463 TEST_F(CloudPolicyInvalidatorTest, InitializeThenStartRefreshScheduler) { | |
| 464 // Make sure registration occurs and invalidations are processed when | |
| 465 // Initialize is called before starting the refresh scheduler. | |
| 466 // Note that the reverse case (start refresh scheduler then initialize) is | |
| 467 // the default behavior for the test fixture, so will be tested in most other | |
| 468 // tests. | |
| 469 StartInvalidator(true /* initialize */, false /* start_refresh_scheduler */); | |
| 470 ConnectCore(); | |
| 471 StartRefreshScheduler(); | |
| 472 StorePolicy(POLICY_OBJECT_A); | |
| 473 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 474 FireInvalidation(POLICY_OBJECT_A); | |
| 475 EXPECT_TRUE(CheckPolicyRefreshed()); | |
| 394 } | 476 } |
| 395 | 477 |
| 396 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) { | 478 TEST_F(CloudPolicyInvalidatorTest, RegisterOnStoreLoaded) { |
| 397 // No registration when store is not loaded. | 479 // No registration when store is not loaded. |
| 398 StartInvalidator(); | 480 StartInvalidator(); |
| 481 EXPECT_FALSE(IsInvalidatorRegistered()); | |
| 399 EXPECT_TRUE(CheckStateChangedNotCalled()); | 482 EXPECT_TRUE(CheckStateChangedNotCalled()); |
| 400 FireInvalidation(POLICY_OBJECT_A); | 483 FireInvalidation(POLICY_OBJECT_A); |
| 401 FireInvalidation(POLICY_OBJECT_B); | 484 FireInvalidation(POLICY_OBJECT_B); |
| 402 EXPECT_TRUE(CheckInvalidateNotCalled()); | 485 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 403 | 486 |
| 404 // No registration when store is loaded with no invalidation object id. | 487 // No registration when store is loaded with no invalidation object id. |
| 405 StorePolicy(POLICY_OBJECT_NONE); | 488 StorePolicy(POLICY_OBJECT_NONE); |
| 489 EXPECT_FALSE(IsInvalidatorRegistered()); | |
| 406 EXPECT_TRUE(CheckStateChangedNotCalled()); | 490 EXPECT_TRUE(CheckStateChangedNotCalled()); |
| 407 FireInvalidation(POLICY_OBJECT_A); | 491 FireInvalidation(POLICY_OBJECT_A); |
| 408 FireInvalidation(POLICY_OBJECT_B); | 492 FireInvalidation(POLICY_OBJECT_B); |
| 409 EXPECT_TRUE(CheckInvalidateNotCalled()); | 493 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 410 | 494 |
| 411 // Check registration when store is loaded for object A. | 495 // Check registration when store is loaded for object A. |
| 412 StorePolicy(POLICY_OBJECT_A); | 496 StorePolicy(POLICY_OBJECT_A); |
| 497 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 413 EXPECT_TRUE(CheckStateChangedCalled(true)); | 498 EXPECT_TRUE(CheckStateChangedCalled(true)); |
| 414 FireInvalidation(POLICY_OBJECT_A); | 499 FireInvalidation(POLICY_OBJECT_A); |
| 415 EXPECT_TRUE(CheckInvalidateCalled()); | 500 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 416 FireInvalidation(POLICY_OBJECT_B); | 501 FireInvalidation(POLICY_OBJECT_B); |
| 417 EXPECT_TRUE(CheckInvalidateNotCalled()); | 502 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 418 } | 503 } |
| 419 | 504 |
| 420 TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) { | 505 TEST_F(CloudPolicyInvalidatorTest, ChangeRegistration) { |
| 421 // Register for object A. | 506 // Register for object A. |
| 422 StartInvalidator(); | 507 StartInvalidator(); |
| 423 StorePolicy(POLICY_OBJECT_A); | 508 StorePolicy(POLICY_OBJECT_A); |
| 509 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 424 EXPECT_TRUE(CheckStateChangedCalled(true)); | 510 EXPECT_TRUE(CheckStateChangedCalled(true)); |
| 425 FireInvalidation(POLICY_OBJECT_A); | 511 FireInvalidation(POLICY_OBJECT_A); |
| 426 EXPECT_TRUE(CheckInvalidateCalled()); | 512 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 427 FireInvalidation(POLICY_OBJECT_B); | 513 FireInvalidation(POLICY_OBJECT_B); |
| 428 EXPECT_TRUE(CheckInvalidateNotCalled()); | 514 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 429 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); | 515 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); |
| 430 | 516 |
| 431 // Check re-registration for object B. Make sure the pending invalidation for | 517 // Check re-registration for object B. Make sure the pending invalidation for |
| 432 // object A is acknowledged without making the callback. | 518 // object A is acknowledged without making the callback. |
| 433 StorePolicy(POLICY_OBJECT_B); | 519 StorePolicy(POLICY_OBJECT_B); |
| 520 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 434 EXPECT_TRUE(CheckStateChangedNotCalled()); | 521 EXPECT_TRUE(CheckStateChangedNotCalled()); |
| 435 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); | 522 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); |
| 436 EXPECT_TRUE(CheckInvalidateNotCalled()); | 523 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 437 | 524 |
| 438 // Make sure future invalidations for object A are ignored and for object B | 525 // Make sure future invalidations for object A are ignored and for object B |
| 439 // are processed. | 526 // are processed. |
| 440 FireInvalidation(POLICY_OBJECT_A); | 527 FireInvalidation(POLICY_OBJECT_A); |
| 441 EXPECT_TRUE(CheckInvalidateNotCalled()); | 528 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 442 FireInvalidation(POLICY_OBJECT_B); | 529 FireInvalidation(POLICY_OBJECT_B); |
| 443 EXPECT_TRUE(CheckInvalidateCalled()); | 530 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 444 } | 531 } |
| 445 | 532 |
| 446 TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) { | 533 TEST_F(CloudPolicyInvalidatorTest, UnregisterOnStoreLoaded) { |
| 447 // Register for object A. | 534 // Register for object A. |
| 448 StartInvalidator(); | 535 StartInvalidator(); |
| 449 StorePolicy(POLICY_OBJECT_A); | 536 StorePolicy(POLICY_OBJECT_A); |
| 537 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 450 EXPECT_TRUE(CheckStateChangedCalled(true)); | 538 EXPECT_TRUE(CheckStateChangedCalled(true)); |
| 451 FireInvalidation(POLICY_OBJECT_A); | 539 FireInvalidation(POLICY_OBJECT_A); |
| 452 EXPECT_TRUE(CheckInvalidateCalled()); | 540 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 453 | 541 |
| 454 // Check unregistration when store is loaded with no invalidation object id. | 542 // Check unregistration when store is loaded with no invalidation object id. |
| 455 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); | 543 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); |
| 456 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); | 544 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); |
| 457 StorePolicy(POLICY_OBJECT_NONE); | 545 StorePolicy(POLICY_OBJECT_NONE); |
| 546 EXPECT_FALSE(IsInvalidatorRegistered()); | |
| 458 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); | 547 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); |
| 459 EXPECT_TRUE(CheckStateChangedCalled(false)); | 548 EXPECT_TRUE(CheckStateChangedCalled(false)); |
| 460 FireInvalidation(POLICY_OBJECT_A); | 549 FireInvalidation(POLICY_OBJECT_A); |
| 461 FireInvalidation(POLICY_OBJECT_B); | 550 FireInvalidation(POLICY_OBJECT_B); |
| 462 EXPECT_TRUE(CheckInvalidateNotCalled()); | 551 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 463 | 552 |
| 464 // Check re-registration for object B. | 553 // Check re-registration for object B. |
| 465 StorePolicy(POLICY_OBJECT_B); | 554 StorePolicy(POLICY_OBJECT_B); |
| 555 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 466 EXPECT_TRUE(CheckStateChangedCalled(true)); | 556 EXPECT_TRUE(CheckStateChangedCalled(true)); |
| 467 FireInvalidation(POLICY_OBJECT_B); | 557 FireInvalidation(POLICY_OBJECT_B); |
| 468 EXPECT_TRUE(CheckInvalidateCalled()); | 558 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 469 } | 559 } |
| 470 | 560 |
| 471 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) { | 561 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidation) { |
| 472 // Register and fire invalidation | 562 // Register and fire invalidation |
| 473 StorePolicy(POLICY_OBJECT_A); | 563 StorePolicy(POLICY_OBJECT_A); |
| 474 StartInvalidator(); | 564 StartInvalidator(); |
| 475 EXPECT_TRUE(CheckStateChangedCalled(true)); | 565 EXPECT_TRUE(CheckStateChangedCalled(true)); |
| 476 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 12, "test_payload"); | 566 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 12, "test_payload"); |
| 477 | 567 |
| 478 // Make sure client info is set as soon as the invalidation is received. | 568 // Make sure client info is set as soon as the invalidation is received. |
| 479 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); | 569 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); |
| 480 EXPECT_TRUE(CheckInvalidateCalled(false /* unknown_version */)); | 570 EXPECT_TRUE(CheckPolicyRefreshed(false /* unknown_version */)); |
| 481 | 571 |
| 482 // Make sure invalidation is not acknowledged until the store is loaded. | 572 // Make sure invalidation is not acknowledged until the store is loaded. |
| 483 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); | 573 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); |
| 484 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); | 574 EXPECT_TRUE(CheckInvalidationInfo(12, "test_payload")); |
| 485 StorePolicy(POLICY_OBJECT_A, 12); | 575 StorePolicy(POLICY_OBJECT_A, 12); |
| 486 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); | 576 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); |
| 487 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 577 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
| 488 } | 578 } |
| 489 | 579 |
| 490 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) { | 580 TEST_F(CloudPolicyInvalidatorTest, HandleInvalidationWithUnknownVersion) { |
| 491 // Register and fire invalidation with unknown version. | 581 // Register and fire invalidation with unknown version. |
| 492 StorePolicy(POLICY_OBJECT_A); | 582 StorePolicy(POLICY_OBJECT_A); |
| 493 StartInvalidator(); | 583 StartInvalidator(); |
| 494 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); | 584 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A); |
| 495 | 585 |
| 496 // Make sure client info is not set until after the invalidation callback is | 586 // Make sure client info is not set until after the invalidation callback is |
| 497 // made. | 587 // made. |
| 498 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 588 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
| 499 EXPECT_TRUE(CheckInvalidateCalled()); | 589 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 500 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); | 590 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); |
| 501 | 591 |
| 502 // Make sure invalidation is not acknowledged until the store is loaded. | 592 // Make sure invalidation is not acknowledged until the store is loaded. |
| 503 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); | 593 EXPECT_FALSE(IsInvalidationAcknowledged(ack)); |
| 504 StorePolicy(POLICY_OBJECT_A, -1); | 594 StorePolicy(POLICY_OBJECT_A, -1); |
| 505 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); | 595 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); |
| 506 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 596 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
| 507 } | 597 } |
| 508 | 598 |
| 509 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) { | 599 TEST_F(CloudPolicyInvalidatorTest, HandleMultipleInvalidations) { |
| 510 // Generate multiple invalidations. | 600 // Generate multiple invalidations. |
| 511 StorePolicy(POLICY_OBJECT_A); | 601 StorePolicy(POLICY_OBJECT_A); |
| 512 StartInvalidator(); | 602 StartInvalidator(); |
| 513 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1"); | 603 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A, 1, "test1"); |
| 514 EXPECT_TRUE(CheckInvalidationInfo(1, "test1")); | 604 EXPECT_TRUE(CheckInvalidationInfo(1, "test1")); |
| 515 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2"); | 605 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A, 2, "test2"); |
| 516 EXPECT_TRUE(CheckInvalidationInfo(2, "test2")); | 606 EXPECT_TRUE(CheckInvalidationInfo(2, "test2")); |
| 517 syncer::AckHandle ack3= FireInvalidation(POLICY_OBJECT_A, 3, "test3"); | 607 syncer::AckHandle ack3= FireInvalidation(POLICY_OBJECT_A, 3, "test3"); |
| 518 EXPECT_TRUE(CheckInvalidationInfo(3, "test3")); | 608 EXPECT_TRUE(CheckInvalidationInfo(3, "test3")); |
| 519 | 609 |
| 520 // Make sure the replaced invalidations are acknowledged. | 610 // Make sure the replaced invalidations are acknowledged. |
| 521 EXPECT_TRUE(IsInvalidationAcknowledged(ack1)); | 611 EXPECT_TRUE(IsInvalidationAcknowledged(ack1)); |
| 522 EXPECT_TRUE(IsInvalidationAcknowledged(ack2)); | 612 EXPECT_TRUE(IsInvalidationAcknowledged(ack2)); |
| 523 | 613 |
| 524 // Make sure the invalidate callback is called once. | 614 // Make sure the policy is refreshed once. |
| 525 EXPECT_TRUE(CheckInvalidateCalled(false /* unknown_version */)); | 615 EXPECT_TRUE(CheckPolicyRefreshed(false /* unknown_version */)); |
| 526 | 616 |
| 527 // Make sure that the last invalidation is only acknowledged after the store | 617 // Make sure that the last invalidation is only acknowledged after the store |
| 528 // is loaded with the latest version. | 618 // is loaded with the latest version. |
| 529 StorePolicy(POLICY_OBJECT_A, 1); | 619 StorePolicy(POLICY_OBJECT_A, 1); |
| 530 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); | 620 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); |
| 531 StorePolicy(POLICY_OBJECT_A, 2); | 621 StorePolicy(POLICY_OBJECT_A, 2); |
| 532 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); | 622 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); |
| 533 StorePolicy(POLICY_OBJECT_A, 3); | 623 StorePolicy(POLICY_OBJECT_A, 3); |
| 534 EXPECT_TRUE(IsInvalidationAcknowledged(ack3)); | 624 EXPECT_TRUE(IsInvalidationAcknowledged(ack3)); |
| 535 } | 625 } |
| 536 | 626 |
| 537 TEST_F(CloudPolicyInvalidatorTest, | 627 TEST_F(CloudPolicyInvalidatorTest, |
| 538 HandleMultipleInvalidationsWithUnknownVersion) { | 628 HandleMultipleInvalidationsWithUnknownVersion) { |
| 539 // Validate that multiple invalidations with unknown version each generate | 629 // Validate that multiple invalidations with unknown version each generate |
| 540 // unique invalidation version numbers. | 630 // unique invalidation version numbers. |
| 541 StorePolicy(POLICY_OBJECT_A); | 631 StorePolicy(POLICY_OBJECT_A); |
| 542 StartInvalidator(); | 632 StartInvalidator(); |
| 543 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A); | 633 syncer::AckHandle ack1 = FireInvalidation(POLICY_OBJECT_A); |
| 544 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 634 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
| 545 EXPECT_TRUE(CheckInvalidateCalled()); | 635 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 546 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); | 636 EXPECT_TRUE(CheckInvalidationInfo(-1, std::string())); |
| 547 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A); | 637 syncer::AckHandle ack2 = FireInvalidation(POLICY_OBJECT_A); |
| 548 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 638 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
| 549 EXPECT_TRUE(CheckInvalidateCalled()); | 639 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 550 EXPECT_TRUE(CheckInvalidationInfo(-2, std::string())); | 640 EXPECT_TRUE(CheckInvalidationInfo(-2, std::string())); |
| 551 syncer::AckHandle ack3 = FireInvalidation(POLICY_OBJECT_A); | 641 syncer::AckHandle ack3 = FireInvalidation(POLICY_OBJECT_A); |
| 552 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); | 642 EXPECT_TRUE(CheckInvalidationInfo(0, std::string())); |
| 553 EXPECT_TRUE(CheckInvalidateCalled()); | 643 EXPECT_TRUE(CheckPolicyRefreshed()); |
| 554 EXPECT_TRUE(CheckInvalidationInfo(-3, std::string())); | 644 EXPECT_TRUE(CheckInvalidationInfo(-3, std::string())); |
| 555 | 645 |
| 556 // Make sure the replaced invalidations are acknowledged. | 646 // Make sure the replaced invalidations are acknowledged. |
| 557 EXPECT_TRUE(IsInvalidationAcknowledged(ack1)); | 647 EXPECT_TRUE(IsInvalidationAcknowledged(ack1)); |
| 558 EXPECT_TRUE(IsInvalidationAcknowledged(ack2)); | 648 EXPECT_TRUE(IsInvalidationAcknowledged(ack2)); |
| 559 | 649 |
| 560 // Make sure that the last invalidation is only acknowledged after the store | 650 // Make sure that the last invalidation is only acknowledged after the store |
| 561 // is loaded with the last unknown version. | 651 // is loaded with the last unknown version. |
| 562 StorePolicy(POLICY_OBJECT_A, -1); | 652 StorePolicy(POLICY_OBJECT_A, -1); |
| 563 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); | 653 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); |
| 564 StorePolicy(POLICY_OBJECT_A, -2); | 654 StorePolicy(POLICY_OBJECT_A, -2); |
| 565 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); | 655 EXPECT_FALSE(IsInvalidationAcknowledged(ack3)); |
| 566 StorePolicy(POLICY_OBJECT_A, -3); | 656 StorePolicy(POLICY_OBJECT_A, -3); |
| 567 EXPECT_TRUE(IsInvalidationAcknowledged(ack3)); | 657 EXPECT_TRUE(IsInvalidationAcknowledged(ack3)); |
| 568 } | 658 } |
| 569 | 659 |
| 570 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeInvalidateCallback) { | 660 TEST_F(CloudPolicyInvalidatorTest, AcknowledgeBeforeRefresh) { |
| 571 // Generate an invalidation. | 661 // Generate an invalidation. |
| 572 StorePolicy(POLICY_OBJECT_A); | 662 StorePolicy(POLICY_OBJECT_A); |
| 573 StartInvalidator(); | 663 StartInvalidator(); |
| 574 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test"); | 664 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test"); |
| 575 | 665 |
| 576 // Ensure that the invalidate callback is not made and the invalidation is | 666 // Ensure that the policy is not refreshed and the invalidation is |
| 577 // acknowledged if the store is loaded with the latest version before the | 667 // acknowledged if the store is loaded with the latest version before the |
| 578 // callback is invoked. | 668 // refresh can occur. |
| 579 StorePolicy(POLICY_OBJECT_A, 3); | 669 StorePolicy(POLICY_OBJECT_A, 3); |
| 580 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); | 670 EXPECT_TRUE(IsInvalidationAcknowledged(ack)); |
| 581 EXPECT_TRUE(CheckInvalidateNotCalled()); | 671 EXPECT_TRUE(CheckPolicyNotRefreshed()); |
| 672 } | |
| 673 | |
| 674 TEST_F(CloudPolicyInvalidatorTest, NoCallbackAfterShutdown) { | |
| 675 // Generate an invalidation. | |
| 676 StorePolicy(POLICY_OBJECT_A); | |
| 677 StartInvalidator(); | |
| 678 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 3, "test"); | |
| 679 | |
| 680 // Ensure that the policy refresh is not made after the invalidator is shut | |
| 681 // down. | |
| 682 ShutdownInvalidator(); | |
| 683 EXPECT_TRUE(CheckPolicyNotRefreshed()); | |
| 684 DestroyInvalidator(); | |
| 582 } | 685 } |
| 583 | 686 |
| 584 TEST_F(CloudPolicyInvalidatorTest, StateChanged) { | 687 TEST_F(CloudPolicyInvalidatorTest, StateChanged) { |
| 585 // Before registration, changes to the invalidation service state should not | 688 // Before registration, changes to the invalidation service state should not |
| 586 // generate change state notifications. | 689 // generate change state notifications. |
| 587 StartInvalidator(); | 690 StartInvalidator(); |
| 588 DisableInvalidationService(); | 691 DisableInvalidationService(); |
| 589 EnableInvalidationService(); | 692 EnableInvalidationService(); |
| 590 EXPECT_TRUE(CheckStateChangedNotCalled()); | 693 EXPECT_TRUE(CheckStateChangedNotCalled()); |
| 591 | 694 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 615 | 718 |
| 616 // When the invalidation service is disabled, changes to the registration | 719 // When the invalidation service is disabled, changes to the registration |
| 617 // state should not generate notifications. | 720 // state should not generate notifications. |
| 618 DisableInvalidationService(); | 721 DisableInvalidationService(); |
| 619 EXPECT_TRUE(CheckStateChangedCalled(false)); | 722 EXPECT_TRUE(CheckStateChangedCalled(false)); |
| 620 StorePolicy(POLICY_OBJECT_NONE); | 723 StorePolicy(POLICY_OBJECT_NONE); |
| 621 StorePolicy(POLICY_OBJECT_A); | 724 StorePolicy(POLICY_OBJECT_A); |
| 622 EXPECT_TRUE(CheckStateChangedNotCalled()); | 725 EXPECT_TRUE(CheckStateChangedNotCalled()); |
| 623 } | 726 } |
| 624 | 727 |
| 728 TEST_F(CloudPolicyInvalidatorTest, Disconnect) { | |
| 729 // Generate an invalidation. | |
| 730 StorePolicy(POLICY_OBJECT_A); | |
| 731 StartInvalidator(); | |
| 732 syncer::AckHandle ack = FireInvalidation(POLICY_OBJECT_A, 1, "test"); | |
| 733 EXPECT_TRUE(CheckStateChangedCalled(true)); | |
| 734 | |
| 735 // Ensure that the policy is not refreshed after disconnecting the core, but | |
| 736 // a call to indicate that invalidations are disabled is made. | |
| 737 DisconnectCore(); | |
| 738 EXPECT_TRUE(CheckPolicyNotRefreshed()); | |
| 739 EXPECT_TRUE(CheckStateChangedCalled(false)); | |
| 740 | |
| 741 // Ensure that invalidation service events do not cause refreshes while the | |
| 742 // invalidator is stopped. | |
| 743 FireInvalidation(POLICY_OBJECT_A, 2, "test"); | |
| 744 EXPECT_TRUE(CheckPolicyNotRefreshed()); | |
| 745 DisableInvalidationService(); | |
| 746 EXPECT_TRUE(CheckStateChangedNotCalled()); | |
| 747 EnableInvalidationService(); | |
| 748 EXPECT_TRUE(CheckStateChangedNotCalled()); | |
| 749 | |
| 750 // Connect and disconnect without starting the refresh scheduler. | |
| 751 ConnectCore(); | |
| 752 FireInvalidation(POLICY_OBJECT_A, 3, "test"); | |
| 753 EXPECT_TRUE(CheckPolicyNotRefreshed()); | |
| 754 DisconnectCore(); | |
| 755 FireInvalidation(POLICY_OBJECT_A, 4, "test"); | |
| 756 EXPECT_TRUE(CheckPolicyNotRefreshed()); | |
| 757 | |
| 758 // Ensure that the invalidator returns to normal after reconnecting. | |
| 759 ConnectCore(); | |
| 760 StartRefreshScheduler(); | |
| 761 EXPECT_TRUE(CheckPolicyNotRefreshed()); | |
| 762 EXPECT_TRUE(CheckStateChangedCalled(true)); | |
| 763 FireInvalidation(POLICY_OBJECT_A, 5, "test"); | |
| 764 EXPECT_TRUE(CheckInvalidationInfo(5, "test")); | |
| 765 EXPECT_TRUE(CheckPolicyRefreshed(false /* unknown_version */)); | |
| 766 DisableInvalidationService(); | |
| 767 EXPECT_TRUE(CheckStateChangedCalled(false)); | |
| 768 } | |
| 769 | |
| 625 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { | 770 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsUnregistered) { |
| 626 // Store loads occurring before invalidation registration are not counted. | 771 // Store loads occurring before invalidation registration are not counted. |
| 627 StartInvalidator(); | 772 StartInvalidator(); |
| 628 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); | 773 StorePolicy(POLICY_OBJECT_NONE, 0, false /* policy_changed */); |
| 629 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); | 774 StorePolicy(POLICY_OBJECT_NONE, 0, true /* policy_changed */); |
| 630 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED)); | 775 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED)); |
| 631 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); | 776 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
| 632 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); | 777 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); |
| 633 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); | 778 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); |
| 634 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | 779 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 715 FireInvalidation(POLICY_OBJECT_A); | 860 FireInvalidation(POLICY_OBJECT_A); |
| 716 FireInvalidation(POLICY_OBJECT_A, 3, "test"); | 861 FireInvalidation(POLICY_OBJECT_A, 3, "test"); |
| 717 FireInvalidation(POLICY_OBJECT_A, 4, "test"); | 862 FireInvalidation(POLICY_OBJECT_A, 4, "test"); |
| 718 | 863 |
| 719 // Verify that received invalidations metrics are correct. | 864 // Verify that received invalidations metrics are correct. |
| 720 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); | 865 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); |
| 721 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); | 866 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); |
| 722 } | 867 } |
| 723 | 868 |
| 724 } // namespace policy | 869 } // namespace policy |
| OLD | NEW |