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

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

Issue 1548133002: Switch to standard integer types in chrome/browser/, part 3 of 4. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h>
6
5 #include <string> 7 #include <string>
6 8
7 #include "base/basictypes.h"
8 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/macros.h"
9 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
12 #include "base/metrics/histogram_samples.h" 14 #include "base/metrics/histogram_samples.h"
13 #include "base/metrics/sample_map.h" 15 #include "base/metrics/sample_map.h"
14 #include "base/metrics/statistics_recorder.h" 16 #include "base/metrics/statistics_recorder.h"
15 #include "base/run_loop.h" 17 #include "base/run_loop.h"
16 #include "base/test/simple_test_clock.h" 18 #include "base/test/simple_test_clock.h"
17 #include "base/test/test_simple_task_runner.h" 19 #include "base/test/test_simple_task_runner.h"
18 #include "base/time/time.h" 20 #include "base/time/time.h"
19 #include "base/values.h" 21 #include "base/values.h"
22 #include "build/build_config.h"
20 #include "chrome/browser/invalidation/fake_invalidation_service.h" 23 #include "chrome/browser/invalidation/fake_invalidation_service.h"
21 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" 24 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"
22 #include "chrome/browser/policy/cloud/user_cloud_policy_invalidator.h" 25 #include "chrome/browser/policy/cloud/user_cloud_policy_invalidator.h"
23 #include "components/invalidation/public/invalidation_util.h" 26 #include "components/invalidation/public/invalidation_util.h"
24 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 27 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
25 #include "components/policy/core/common/cloud/cloud_policy_core.h" 28 #include "components/policy/core/common/cloud/cloud_policy_core.h"
26 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" 29 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h"
27 #include "components/policy/core/common/cloud/enterprise_metrics.h" 30 #include "components/policy/core/common/cloud/enterprise_metrics.h"
28 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" 31 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
29 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h" 32 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
(...skipping 19 matching lines...) Expand all
49 52
50 void TearDown() override; 53 void TearDown() override;
51 54
52 // Starts the invalidator which will be tested. 55 // Starts the invalidator which will be tested.
53 // |initialize| determines if the invalidator should be initialized. 56 // |initialize| determines if the invalidator should be initialized.
54 // |start_refresh_scheduler| determines if the refresh scheduler should start. 57 // |start_refresh_scheduler| determines if the refresh scheduler should start.
55 // |highest_handled_invalidation_version| is the highest invalidation version 58 // |highest_handled_invalidation_version| is the highest invalidation version
56 // that was handled already before this invalidator was created. 59 // that was handled already before this invalidator was created.
57 void StartInvalidator(bool initialize, 60 void StartInvalidator(bool initialize,
58 bool start_refresh_scheduler, 61 bool start_refresh_scheduler,
59 int64 highest_handled_invalidation_version); 62 int64_t highest_handled_invalidation_version);
60 void StartInvalidator() { 63 void StartInvalidator() {
61 StartInvalidator(true, /* initialize */ 64 StartInvalidator(true, /* initialize */
62 true, /* start_refresh_scheduler */ 65 true, /* start_refresh_scheduler */
63 0 /* highest_handled_invalidation_version */); 66 0 /* highest_handled_invalidation_version */);
64 } 67 }
65 68
66 // Calls Initialize on the invalidator. 69 // Calls Initialize on the invalidator.
67 void InitializeInvalidator(); 70 void InitializeInvalidator();
68 71
69 // Calls Shutdown on the invalidator. Test must call DestroyInvalidator 72 // Calls Shutdown on the invalidator. Test must call DestroyInvalidator
(...skipping 12 matching lines...) Expand all
82 // Disconnects the cloud policy core. 85 // Disconnects the cloud policy core.
83 void DisconnectCore(); 86 void DisconnectCore();
84 87
85 // Simulates storing a new policy to the policy store. 88 // Simulates storing a new policy to the policy store.
86 // |object| determines which policy object the store will report the 89 // |object| determines which policy object the store will report the
87 // invalidator should register for. May be POLICY_OBJECT_NONE for no object. 90 // invalidator should register for. May be POLICY_OBJECT_NONE for no object.
88 // |invalidation_version| determines what invalidation the store will report. 91 // |invalidation_version| determines what invalidation the store will report.
89 // |policy_changed| determines whether a policy value different from the 92 // |policy_changed| determines whether a policy value different from the
90 // current value will be stored. 93 // current value will be stored.
91 // |time| determines the timestamp the store will report. 94 // |time| determines the timestamp the store will report.
92 void StorePolicy( 95 void StorePolicy(PolicyObject object,
93 PolicyObject object, 96 int64_t invalidation_version,
94 int64 invalidation_version, 97 bool policy_changed,
95 bool policy_changed, 98 const base::Time& time);
96 const base::Time& time); 99 void StorePolicy(PolicyObject object,
97 void StorePolicy( 100 int64_t invalidation_version,
98 PolicyObject object, 101 bool policy_changed) {
99 int64 invalidation_version,
100 bool policy_changed) {
101 StorePolicy(object, 102 StorePolicy(object,
102 invalidation_version, 103 invalidation_version,
103 policy_changed, 104 policy_changed,
104 Now() - base::TimeDelta::FromMinutes(5)); 105 Now() - base::TimeDelta::FromMinutes(5));
105 } 106 }
106 void StorePolicy(PolicyObject object, int64 invalidation_version) { 107 void StorePolicy(PolicyObject object, int64_t invalidation_version) {
107 StorePolicy(object, invalidation_version, false); 108 StorePolicy(object, invalidation_version, false);
108 } 109 }
109 void StorePolicy(PolicyObject object) { 110 void StorePolicy(PolicyObject object) {
110 StorePolicy(object, 0); 111 StorePolicy(object, 0);
111 } 112 }
112 113
113 // Disables the invalidation service. It is enabled by default. 114 // Disables the invalidation service. It is enabled by default.
114 void DisableInvalidationService(); 115 void DisableInvalidationService();
115 116
116 // Enables the invalidation service. It is enabled by default. 117 // Enables the invalidation service. It is enabled by default.
117 void EnableInvalidationService(); 118 void EnableInvalidationService();
118 119
119 // Causes the invalidation service to fire an invalidation. 120 // Causes the invalidation service to fire an invalidation.
120 syncer::Invalidation FireInvalidation( 121 syncer::Invalidation FireInvalidation(PolicyObject object,
121 PolicyObject object, 122 int64_t version,
122 int64 version, 123 const std::string& payload);
123 const std::string& payload);
124 124
125 // Causes the invalidation service to fire an invalidation with unknown 125 // Causes the invalidation service to fire an invalidation with unknown
126 // version. 126 // version.
127 syncer::Invalidation FireUnknownVersionInvalidation(PolicyObject object); 127 syncer::Invalidation FireUnknownVersionInvalidation(PolicyObject object);
128 128
129 // Checks the expected value of the currently set invalidation info. 129 // Checks the expected value of the currently set invalidation info.
130 bool CheckInvalidationInfo(int64 version, const std::string& payload); 130 bool CheckInvalidationInfo(int64_t version, const std::string& payload);
131 131
132 // Checks that the policy was not refreshed due to an invalidation. 132 // Checks that the policy was not refreshed due to an invalidation.
133 bool CheckPolicyNotRefreshed(); 133 bool CheckPolicyNotRefreshed();
134 134
135 // Checks that the policy was refreshed due to an invalidation within an 135 // Checks that the policy was refreshed due to an invalidation within an
136 // appropriate timeframe depending on whether the invalidation had unknown 136 // appropriate timeframe depending on whether the invalidation had unknown
137 // version. 137 // version.
138 bool CheckPolicyRefreshed(); 138 bool CheckPolicyRefreshed();
139 bool CheckPolicyRefreshedWithUnknownVersion(); 139 bool CheckPolicyRefreshedWithUnknownVersion();
140 140
141 bool IsUnsent(const syncer::Invalidation& invalidation); 141 bool IsUnsent(const syncer::Invalidation& invalidation);
142 142
143 // Returns the invalidations enabled state set by the invalidator on the 143 // Returns the invalidations enabled state set by the invalidator on the
144 // refresh scheduler. 144 // refresh scheduler.
145 bool InvalidationsEnabled(); 145 bool InvalidationsEnabled();
146 146
147 // Determines if the invalidation with the given ack handle has been 147 // Determines if the invalidation with the given ack handle has been
148 // acknowledged. 148 // acknowledged.
149 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation); 149 bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation);
150 150
151 // Determines if the invalidator has registered for an object with the 151 // Determines if the invalidator has registered for an object with the
152 // invalidation service. 152 // invalidation service.
153 bool IsInvalidatorRegistered(); 153 bool IsInvalidatorRegistered();
154 154
155 // Returns the highest invalidation version that was handled already according 155 // Returns the highest invalidation version that was handled already according
156 // to the |invalidator_|. 156 // to the |invalidator_|.
157 int64 GetHighestHandledInvalidationVersion() const; 157 int64_t GetHighestHandledInvalidationVersion() const;
158 158
159 // Advance the test clock. 159 // Advance the test clock.
160 void AdvanceClock(base::TimeDelta delta); 160 void AdvanceClock(base::TimeDelta delta);
161 161
162 // Get the current time on the test clock. 162 // Get the current time on the test clock.
163 base::Time Now(); 163 base::Time Now();
164 164
165 // Translate a version number into an appropriate invalidation version (which 165 // Translate a version number into an appropriate invalidation version (which
166 // is based on the current time). 166 // is based on the current time).
167 int64 V(int version); 167 int64_t V(int version);
168 168
169 // Get an invalidation version for the given time. 169 // Get an invalidation version for the given time.
170 int64 GetVersion(base::Time time); 170 int64_t GetVersion(base::Time time);
171 171
172 // Get the policy type that the |invalidator_| is responsible for. 172 // Get the policy type that the |invalidator_| is responsible for.
173 virtual em::DeviceRegisterRequest::Type GetPolicyType() const; 173 virtual em::DeviceRegisterRequest::Type GetPolicyType() const;
174 174
175 private: 175 private:
176 // Checks that the policy was refreshed due to an invalidation with the given 176 // Checks that the policy was refreshed due to an invalidation with the given
177 // base delay. 177 // base delay.
178 bool CheckPolicyRefreshed(base::TimeDelta delay); 178 bool CheckPolicyRefreshed(base::TimeDelta delay);
179 179
180 // Checks that the policy was refreshed the given number of times. 180 // Checks that the policy was refreshed the given number of times.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 228
229 void CloudPolicyInvalidatorTest::TearDown() { 229 void CloudPolicyInvalidatorTest::TearDown() {
230 if (invalidator_) 230 if (invalidator_)
231 invalidator_->Shutdown(); 231 invalidator_->Shutdown();
232 core_.Disconnect(); 232 core_.Disconnect();
233 } 233 }
234 234
235 void CloudPolicyInvalidatorTest::StartInvalidator( 235 void CloudPolicyInvalidatorTest::StartInvalidator(
236 bool initialize, 236 bool initialize,
237 bool start_refresh_scheduler, 237 bool start_refresh_scheduler,
238 int64 highest_handled_invalidation_version) { 238 int64_t highest_handled_invalidation_version) {
239 invalidator_.reset(new CloudPolicyInvalidator( 239 invalidator_.reset(new CloudPolicyInvalidator(
240 GetPolicyType(), 240 GetPolicyType(),
241 &core_, 241 &core_,
242 task_runner_, 242 task_runner_,
243 scoped_ptr<base::Clock>(clock_), 243 scoped_ptr<base::Clock>(clock_),
244 highest_handled_invalidation_version)); 244 highest_handled_invalidation_version));
245 if (start_refresh_scheduler) { 245 if (start_refresh_scheduler) {
246 ConnectCore(); 246 ConnectCore();
247 StartRefreshScheduler(); 247 StartRefreshScheduler();
248 } 248 }
(...skipping 21 matching lines...) Expand all
270 270
271 void CloudPolicyInvalidatorTest::StartRefreshScheduler() { 271 void CloudPolicyInvalidatorTest::StartRefreshScheduler() {
272 core_.StartRefreshScheduler(); 272 core_.StartRefreshScheduler();
273 } 273 }
274 274
275 void CloudPolicyInvalidatorTest::DisconnectCore() { 275 void CloudPolicyInvalidatorTest::DisconnectCore() {
276 client_ = nullptr; 276 client_ = nullptr;
277 core_.Disconnect(); 277 core_.Disconnect();
278 } 278 }
279 279
280 void CloudPolicyInvalidatorTest::StorePolicy( 280 void CloudPolicyInvalidatorTest::StorePolicy(PolicyObject object,
281 PolicyObject object, 281 int64_t invalidation_version,
282 int64 invalidation_version, 282 bool policy_changed,
283 bool policy_changed, 283 const base::Time& time) {
284 const base::Time& time) {
285 em::PolicyData* data = new em::PolicyData(); 284 em::PolicyData* data = new em::PolicyData();
286 if (object != POLICY_OBJECT_NONE) { 285 if (object != POLICY_OBJECT_NONE) {
287 data->set_invalidation_source(GetPolicyObjectId(object).source()); 286 data->set_invalidation_source(GetPolicyObjectId(object).source());
288 data->set_invalidation_name(GetPolicyObjectId(object).name()); 287 data->set_invalidation_name(GetPolicyObjectId(object).name());
289 } 288 }
290 data->set_timestamp((time - base::Time::UnixEpoch()).InMilliseconds()); 289 data->set_timestamp((time - base::Time::UnixEpoch()).InMilliseconds());
291 // Swap the policy value if a policy change is desired. 290 // Swap the policy value if a policy change is desired.
292 if (policy_changed) 291 if (policy_changed)
293 policy_value_cur_ = policy_value_cur_ == policy_value_a_ ? 292 policy_value_cur_ = policy_value_cur_ == policy_value_a_ ?
294 policy_value_b_ : policy_value_a_; 293 policy_value_b_ : policy_value_a_;
(...skipping 16 matching lines...) Expand all
311 invalidation_service_.SetInvalidatorState( 310 invalidation_service_.SetInvalidatorState(
312 syncer::TRANSIENT_INVALIDATION_ERROR); 311 syncer::TRANSIENT_INVALIDATION_ERROR);
313 } 312 }
314 313
315 void CloudPolicyInvalidatorTest::EnableInvalidationService() { 314 void CloudPolicyInvalidatorTest::EnableInvalidationService() {
316 invalidation_service_.SetInvalidatorState(syncer::INVALIDATIONS_ENABLED); 315 invalidation_service_.SetInvalidatorState(syncer::INVALIDATIONS_ENABLED);
317 } 316 }
318 317
319 syncer::Invalidation CloudPolicyInvalidatorTest::FireInvalidation( 318 syncer::Invalidation CloudPolicyInvalidatorTest::FireInvalidation(
320 PolicyObject object, 319 PolicyObject object,
321 int64 version, 320 int64_t version,
322 const std::string& payload) { 321 const std::string& payload) {
323 syncer::Invalidation invalidation = syncer::Invalidation::Init( 322 syncer::Invalidation invalidation = syncer::Invalidation::Init(
324 GetPolicyObjectId(object), 323 GetPolicyObjectId(object),
325 version, 324 version,
326 payload); 325 payload);
327 invalidation_service_.EmitInvalidationForTest(invalidation); 326 invalidation_service_.EmitInvalidationForTest(invalidation);
328 return invalidation; 327 return invalidation;
329 } 328 }
330 329
331 syncer::Invalidation CloudPolicyInvalidatorTest::FireUnknownVersionInvalidation( 330 syncer::Invalidation CloudPolicyInvalidatorTest::FireUnknownVersionInvalidation(
332 PolicyObject object) { 331 PolicyObject object) {
333 syncer::Invalidation invalidation = syncer::Invalidation::InitUnknownVersion( 332 syncer::Invalidation invalidation = syncer::Invalidation::InitUnknownVersion(
334 GetPolicyObjectId(object)); 333 GetPolicyObjectId(object));
335 invalidation_service_.EmitInvalidationForTest(invalidation); 334 invalidation_service_.EmitInvalidationForTest(invalidation);
336 return invalidation; 335 return invalidation;
337 } 336 }
338 337
339 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo( 338 bool CloudPolicyInvalidatorTest::CheckInvalidationInfo(
340 int64 version, 339 int64_t version,
341 const std::string& payload) { 340 const std::string& payload) {
342 MockCloudPolicyClient* client = 341 MockCloudPolicyClient* client =
343 static_cast<MockCloudPolicyClient*>(core_.client()); 342 static_cast<MockCloudPolicyClient*>(core_.client());
344 return version == client->invalidation_version_ && 343 return version == client->invalidation_version_ &&
345 payload == client->invalidation_payload_; 344 payload == client->invalidation_payload_;
346 } 345 }
347 346
348 bool CloudPolicyInvalidatorTest::CheckPolicyNotRefreshed() { 347 bool CloudPolicyInvalidatorTest::CheckPolicyNotRefreshed() {
349 return CheckPolicyRefreshCount(0); 348 return CheckPolicyRefreshCount(0);
350 } 349 }
(...skipping 25 matching lines...) Expand all
376 375
377 EXPECT_FALSE(IsUnsent(invalidation)); 376 EXPECT_FALSE(IsUnsent(invalidation));
378 return !invalidation_service_.GetMockAckHandler()->IsUnacked(invalidation); 377 return !invalidation_service_.GetMockAckHandler()->IsUnacked(invalidation);
379 } 378 }
380 379
381 bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() { 380 bool CloudPolicyInvalidatorTest::IsInvalidatorRegistered() {
382 return !invalidation_service_.invalidator_registrar() 381 return !invalidation_service_.invalidator_registrar()
383 .GetRegisteredIds(invalidator_.get()).empty(); 382 .GetRegisteredIds(invalidator_.get()).empty();
384 } 383 }
385 384
386 int64 CloudPolicyInvalidatorTest::GetHighestHandledInvalidationVersion() const { 385 int64_t CloudPolicyInvalidatorTest::GetHighestHandledInvalidationVersion()
386 const {
387 return invalidator_->highest_handled_invalidation_version(); 387 return invalidator_->highest_handled_invalidation_version();
388 } 388 }
389 389
390 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) { 390 void CloudPolicyInvalidatorTest::AdvanceClock(base::TimeDelta delta) {
391 clock_->Advance(delta); 391 clock_->Advance(delta);
392 } 392 }
393 393
394 base::Time CloudPolicyInvalidatorTest::Now() { 394 base::Time CloudPolicyInvalidatorTest::Now() {
395 return clock_->Now(); 395 return clock_->Now();
396 } 396 }
397 397
398 int64 CloudPolicyInvalidatorTest::V(int version) { 398 int64_t CloudPolicyInvalidatorTest::V(int version) {
399 return GetVersion(Now()) + version; 399 return GetVersion(Now()) + version;
400 } 400 }
401 401
402 int64 CloudPolicyInvalidatorTest::GetVersion(base::Time time) { 402 int64_t CloudPolicyInvalidatorTest::GetVersion(base::Time time) {
403 return (time - base::Time::UnixEpoch()).InMicroseconds(); 403 return (time - base::Time::UnixEpoch()).InMicroseconds();
404 } 404 }
405 405
406 em::DeviceRegisterRequest::Type 406 em::DeviceRegisterRequest::Type
407 CloudPolicyInvalidatorTest::GetPolicyType() const { 407 CloudPolicyInvalidatorTest::GetPolicyType() const {
408 return UserCloudPolicyInvalidator::GetPolicyType(); 408 return UserCloudPolicyInvalidator::GetPolicyType();
409 } 409 }
410 410
411 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) { 411 bool CloudPolicyInvalidatorTest::CheckPolicyRefreshed(base::TimeDelta delay) {
412 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds( 412 base::TimeDelta max_delay = delay + base::TimeDelta::FromMilliseconds(
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after
1101 CloudPolicyInvalidatorUserTypedTest, 1101 CloudPolicyInvalidatorUserTypedTest,
1102 testing::Values(em::DeviceRegisterRequest::IOS_BROWSER)); 1102 testing::Values(em::DeviceRegisterRequest::IOS_BROWSER));
1103 #else 1103 #else
1104 INSTANTIATE_TEST_CASE_P( 1104 INSTANTIATE_TEST_CASE_P(
1105 CloudPolicyInvalidatorUserTypedTestInstance, 1105 CloudPolicyInvalidatorUserTypedTestInstance,
1106 CloudPolicyInvalidatorUserTypedTest, 1106 CloudPolicyInvalidatorUserTypedTest,
1107 testing::Values(em::DeviceRegisterRequest::BROWSER)); 1107 testing::Values(em::DeviceRegisterRequest::BROWSER));
1108 #endif 1108 #endif
1109 1109
1110 } // namespace policy 1110 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698