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