| 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" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/histogram_samples.h" | 13 #include "base/metrics/histogram_samples.h" |
| 14 #include "base/metrics/sample_map.h" | 14 #include "base/metrics/sample_map.h" |
| 15 #include "base/metrics/statistics_recorder.h" | 15 #include "base/metrics/statistics_recorder.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/test/simple_test_clock.h" |
| 17 #include "base/test/test_simple_task_runner.h" | 18 #include "base/test/test_simple_task_runner.h" |
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 #include "chrome/browser/invalidation/fake_invalidation_service.h" | 21 #include "chrome/browser/invalidation/fake_invalidation_service.h" |
| 21 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" | 22 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" |
| 22 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 23 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 23 #include "components/policy/core/common/cloud/cloud_policy_core.h" | 24 #include "components/policy/core/common/cloud/cloud_policy_core.h" |
| 24 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" | 25 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" |
| 25 #include "components/policy/core/common/cloud/enterprise_metrics.h" | 26 #include "components/policy/core/common/cloud/enterprise_metrics.h" |
| 26 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | 27 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation); | 141 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation); |
| 141 | 142 |
| 142 // Determines if the invalidator has registered for an object with the | 143 // Determines if the invalidator has registered for an object with the |
| 143 // invalidation service. | 144 // invalidation service. |
| 144 bool IsInvalidatorRegistered(); | 145 bool IsInvalidatorRegistered(); |
| 145 | 146 |
| 146 // Get the current count for the given metric. | 147 // Get the current count for the given metric. |
| 147 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); | 148 base::HistogramBase::Count GetCount(MetricPolicyRefresh metric); |
| 148 base::HistogramBase::Count GetInvalidationCount(bool with_payload); | 149 base::HistogramBase::Count GetInvalidationCount(bool with_payload); |
| 149 | 150 |
| 151 // Advance the test clock. |
| 152 void AdvanceClock(base::TimeDelta delta); |
| 153 |
| 150 private: | 154 private: |
| 151 // Checks that the policy was refreshed due to an invalidation with the given | 155 // Checks that the policy was refreshed due to an invalidation with the given |
| 152 // base delay. | 156 // base delay. |
| 153 bool CheckPolicyRefreshed(base::TimeDelta delay); | 157 bool CheckPolicyRefreshed(base::TimeDelta delay); |
| 154 | 158 |
| 155 // Checks that the policy was refreshed the given number of times. | 159 // Checks that the policy was refreshed the given number of times. |
| 156 bool CheckPolicyRefreshCount(int count); | 160 bool CheckPolicyRefreshCount(int count); |
| 157 | 161 |
| 158 // Returns the object id of the given policy object. | 162 // Returns the object id of the given policy object. |
| 159 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; | 163 const invalidation::ObjectId& GetPolicyObjectId(PolicyObject object) const; |
| 160 | 164 |
| 161 // Get histogram samples for the given histogram. | 165 // Get histogram samples for the given histogram. |
| 162 scoped_ptr<base::HistogramSamples> GetHistogramSamples( | 166 scoped_ptr<base::HistogramSamples> GetHistogramSamples( |
| 163 const std::string& name) const; | 167 const std::string& name) const; |
| 164 | 168 |
| 165 base::MessageLoop loop_; | 169 base::MessageLoop loop_; |
| 166 | 170 |
| 167 // Objects the invalidator depends on. | 171 // Objects the invalidator depends on. |
| 168 invalidation::FakeInvalidationService invalidation_service_; | 172 invalidation::FakeInvalidationService invalidation_service_; |
| 169 MockCloudPolicyStore store_; | 173 MockCloudPolicyStore store_; |
| 170 CloudPolicyCore core_; | 174 CloudPolicyCore core_; |
| 171 MockCloudPolicyClient* client_; | 175 MockCloudPolicyClient* client_; |
| 172 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 176 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 177 base::SimpleTestClock* clock_; |
| 173 | 178 |
| 174 // The invalidator which will be tested. | 179 // The invalidator which will be tested. |
| 175 scoped_ptr<CloudPolicyInvalidator> invalidator_; | 180 scoped_ptr<CloudPolicyInvalidator> invalidator_; |
| 176 | 181 |
| 177 // Object ids for the test policy objects. | 182 // Object ids for the test policy objects. |
| 178 invalidation::ObjectId object_id_a_; | 183 invalidation::ObjectId object_id_a_; |
| 179 invalidation::ObjectId object_id_b_; | 184 invalidation::ObjectId object_id_b_; |
| 180 | 185 |
| 181 // Increasing policy timestamp. | 186 // Increasing policy timestamp. |
| 182 int64 timestamp_; | 187 int64 timestamp_; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 196 scoped_ptr<base::HistogramSamples> invalidations_samples_; | 201 scoped_ptr<base::HistogramSamples> invalidations_samples_; |
| 197 }; | 202 }; |
| 198 | 203 |
| 199 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() | 204 CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest() |
| 200 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, | 205 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, |
| 201 std::string()), | 206 std::string()), |
| 202 &store_, | 207 &store_, |
| 203 loop_.message_loop_proxy()), | 208 loop_.message_loop_proxy()), |
| 204 client_(NULL), | 209 client_(NULL), |
| 205 task_runner_(new base::TestSimpleTaskRunner()), | 210 task_runner_(new base::TestSimpleTaskRunner()), |
| 211 clock_(new base::SimpleTestClock()), |
| 206 object_id_a_(135, "asdf"), | 212 object_id_a_(135, "asdf"), |
| 207 object_id_b_(246, "zxcv"), | 213 object_id_b_(246, "zxcv"), |
| 208 timestamp_(123456), | 214 timestamp_(123456), |
| 209 policy_value_a_("asdf"), | 215 policy_value_a_("asdf"), |
| 210 policy_value_b_("zxcv"), | 216 policy_value_b_("zxcv"), |
| 211 policy_value_cur_(policy_value_a_) {} | 217 policy_value_cur_(policy_value_a_) {} |
| 212 | 218 |
| 213 void CloudPolicyInvalidatorTest::SetUp() { | 219 void CloudPolicyInvalidatorTest::SetUp() { |
| 214 base::StatisticsRecorder::Initialize(); | 220 base::StatisticsRecorder::Initialize(); |
| 215 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); | 221 refresh_samples_ = GetHistogramSamples(kMetricPolicyRefresh); |
| 216 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); | 222 invalidations_samples_ = GetHistogramSamples(kMetricPolicyInvalidations); |
| 217 } | 223 } |
| 218 | 224 |
| 219 void CloudPolicyInvalidatorTest::TearDown() { | 225 void CloudPolicyInvalidatorTest::TearDown() { |
| 220 if (invalidator_) | 226 if (invalidator_) |
| 221 invalidator_->Shutdown(); | 227 invalidator_->Shutdown(); |
| 222 core_.Disconnect(); | 228 core_.Disconnect(); |
| 223 } | 229 } |
| 224 | 230 |
| 225 void CloudPolicyInvalidatorTest::StartInvalidator( | 231 void CloudPolicyInvalidatorTest::StartInvalidator( |
| 226 bool initialize, | 232 bool initialize, |
| 227 bool start_refresh_scheduler) { | 233 bool start_refresh_scheduler) { |
| 228 invalidator_.reset(new CloudPolicyInvalidator(&core_, task_runner_)); | 234 invalidator_.reset(new CloudPolicyInvalidator(&core_, task_runner_, clock_)); |
| 229 if (start_refresh_scheduler) { | 235 if (start_refresh_scheduler) { |
| 230 ConnectCore(); | 236 ConnectCore(); |
| 231 StartRefreshScheduler(); | 237 StartRefreshScheduler(); |
| 232 } | 238 } |
| 233 if (initialize) | 239 if (initialize) |
| 234 InitializeInvalidator(); | 240 InitializeInvalidator(); |
| 235 } | 241 } |
| 236 | 242 |
| 237 void CloudPolicyInvalidatorTest::InitializeInvalidator() { | 243 void CloudPolicyInvalidatorTest::InitializeInvalidator() { |
| 238 invalidator_->Initialize(&invalidation_service_); | 244 invalidator_->Initialize(&invalidation_service_); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 refresh_samples_->GetCount(metric); | 379 refresh_samples_->GetCount(metric); |
| 374 } | 380 } |
| 375 | 381 |
| 376 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( | 382 base::HistogramBase::Count CloudPolicyInvalidatorTest::GetInvalidationCount( |
| 377 bool with_payload) { | 383 bool with_payload) { |
| 378 int metric = with_payload ? 1 : 0; | 384 int metric = with_payload ? 1 : 0; |
| 379 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) - | 385 return GetHistogramSamples(kMetricPolicyInvalidations)->GetCount(metric) - |
| 380 invalidations_samples_->GetCount(metric); | 386 invalidations_samples_->GetCount(metric); |
| 381 } | 387 } |
| 382 | 388 |
| 389 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) { |
| 390 clock_->Advance(delta); |
| 391 } |
| 392 |
| 383 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) { | 393 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) { |
| 384 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds( | 394 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds( |
| 385 CloudPolicyInvalidator::kMaxFetchDelayMin); | 395 CloudPolicyInvalidator::kMaxFetchDelayMin); |
| 386 | 396 |
| 387 if (task_runner_->GetPendingTasks().empty()) | 397 if (task_runner_->GetPendingTasks().empty()) |
| 388 return false; | 398 return false; |
| 389 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; | 399 base::TimeDelta actual_delay = task_runner_->GetPendingTasks().back().delay; |
| 390 EXPECT_GE(actual_delay, delay); | 400 EXPECT_GE(actual_delay, delay); |
| 391 EXPECT_LE(actual_delay, max_delay); | 401 EXPECT_LE(actual_delay, max_delay); |
| 392 | 402 |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); | 779 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); |
| 770 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); | 780 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); |
| 771 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | 781 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); |
| 772 } | 782 } |
| 773 | 783 |
| 774 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsNoInvalidations) { | 784 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsNoInvalidations) { |
| 775 // Store loads occurring while registered should be differentiated depending | 785 // Store loads occurring while registered should be differentiated depending |
| 776 // on whether the invalidation service was enabled or not. | 786 // on whether the invalidation service was enabled or not. |
| 777 StorePolicy(POLICY_OBJECT_A); | 787 StorePolicy(POLICY_OBJECT_A); |
| 778 StartInvalidator(); | 788 StartInvalidator(); |
| 789 |
| 790 // Initially, invalidations have not been enabled past the grace period, so |
| 791 // invalidations are OFF. |
| 779 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 792 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| 780 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 793 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
| 794 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
| 795 |
| 796 // If the clock advances less than the grace period, invalidations are OFF. |
| 797 AdvanceClock(base::TimeDelta::FromSeconds(1)); |
| 798 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| 799 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
| 800 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
| 801 |
| 802 // After the grace period elapses, invalidations are ON. |
| 803 AdvanceClock(base::TimeDelta::FromSeconds( |
| 804 CloudPolicyInvalidator::kInvalidationGracePeriod)); |
| 805 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| 806 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
| 807 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED)); |
| 808 |
| 809 // After the invalidation service is disabled, invalidations are OFF. |
| 781 DisableInvalidationService(); | 810 DisableInvalidationService(); |
| 782 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 811 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| 783 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 812 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
| 813 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
| 814 |
| 815 // Enabling the invalidation service results in a new grace period, so |
| 816 // invalidations are OFF. |
| 817 EnableInvalidationService(); |
| 784 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 818 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| 785 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 819 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
| 786 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED)); | 820 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
| 787 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); | 821 |
| 788 EXPECT_EQ(3, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); | 822 // After the grace period elapses, invalidations are ON. |
| 823 AdvanceClock(base::TimeDelta::FromSeconds( |
| 824 CloudPolicyInvalidator::kInvalidationGracePeriod)); |
| 825 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| 826 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
| 827 |
| 828 EXPECT_EQ(2, GetCount(METRIC_POLICY_REFRESH_CHANGED)); |
| 829 EXPECT_EQ(4, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); |
| 830 EXPECT_EQ(6, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); |
| 789 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); | 831 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); |
| 790 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | 832 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); |
| 791 } | 833 } |
| 792 | |
| 793 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsStoreSameTimestamp) { | |
| 794 // Store loads with the same timestamp as the load which causes registration | |
| 795 // are not counted. | |
| 796 StartInvalidator(); | |
| 797 StorePolicy( | |
| 798 POLICY_OBJECT_A, 0, false /* policy_changed */, 12 /* timestamp */); | |
| 799 StorePolicy( | |
| 800 POLICY_OBJECT_A, 0, false /* policy_changed */, 12 /* timestamp */); | |
| 801 StorePolicy( | |
| 802 POLICY_OBJECT_A, 0, true /* policy_changed */, 12 /* timestamp */); | |
| 803 | |
| 804 // The next load with a different timestamp counts. | |
| 805 StorePolicy( | |
| 806 POLICY_OBJECT_A, 0, true /* policy_changed */, 13 /* timestamp */); | |
| 807 | |
| 808 EXPECT_EQ(1, GetCount(METRIC_POLICY_REFRESH_CHANGED)); | |
| 809 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS)); | |
| 810 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_UNCHANGED)); | |
| 811 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_CHANGED)); | |
| 812 EXPECT_EQ(0, GetCount(METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED)); | |
| 813 } | |
| 814 | 834 |
| 815 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) { | 835 TEST_F(CloudPolicyInvalidatorTest, RefreshMetricsInvalidation) { |
| 816 // Store loads after an invalidation are counted as invalidated, even if | 836 // Store loads after an invalidation are counted as invalidated, even if |
| 817 // the loads do not result in the invalidation being acknowledged. | 837 // the loads do not result in the invalidation being acknowledged. |
| 818 StartInvalidator(); | 838 StartInvalidator(); |
| 819 StorePolicy(POLICY_OBJECT_A); | 839 StorePolicy(POLICY_OBJECT_A); |
| 840 AdvanceClock(base::TimeDelta::FromSeconds( |
| 841 CloudPolicyInvalidator::kInvalidationGracePeriod)); |
| 820 FireInvalidation(POLICY_OBJECT_A, 5, "test"); | 842 FireInvalidation(POLICY_OBJECT_A, 5, "test"); |
| 821 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 843 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| 822 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 844 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
| 823 StorePolicy(POLICY_OBJECT_A, 5, true /* policy_changed */); | 845 StorePolicy(POLICY_OBJECT_A, 5, true /* policy_changed */); |
| 824 | 846 |
| 825 // Store loads after the invalidation is complete are not counted as | 847 // Store loads after the invalidation is complete are not counted as |
| 826 // invalidated. | 848 // invalidated. |
| 827 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 849 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| 828 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); | 850 StorePolicy(POLICY_OBJECT_A, 0, true /* policy_changed */); |
| 829 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); | 851 StorePolicy(POLICY_OBJECT_A, 0, false /* policy_changed */); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 852 FireUnknownVersionInvalidation(POLICY_OBJECT_A); | 874 FireUnknownVersionInvalidation(POLICY_OBJECT_A); |
| 853 FireInvalidation(POLICY_OBJECT_A, 3, "test"); | 875 FireInvalidation(POLICY_OBJECT_A, 3, "test"); |
| 854 FireInvalidation(POLICY_OBJECT_A, 4, "test"); | 876 FireInvalidation(POLICY_OBJECT_A, 4, "test"); |
| 855 | 877 |
| 856 // Verify that received invalidations metrics are correct. | 878 // Verify that received invalidations metrics are correct. |
| 857 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); | 879 EXPECT_EQ(3, GetInvalidationCount(false /* with_payload */)); |
| 858 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); | 880 EXPECT_EQ(4, GetInvalidationCount(true /* with_payload */)); |
| 859 } | 881 } |
| 860 | 882 |
| 861 } // namespace policy | 883 } // namespace policy |
| OLD | NEW |