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

Side by Side Diff: components/prefs_tracker/pref_hash_filter_unittest.cc

Issue 1227973003: Componentize //chrome/browser/prefs/tracked. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: modified folder to prefs_tracker Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/prefs/tracked/pref_hash_filter.h" 5 #include "components/prefs_tracker/pref_hash_filter.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/basictypes.h" 13 #include "base/basictypes.h"
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/callback_forward.h" 15 #include "base/callback_forward.h"
16 #include "base/compiler_specific.h" 16 #include "base/compiler_specific.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
19 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/scoped_ptr.h"
20 #include "base/metrics/histogram_base.h" 20 #include "base/metrics/histogram_base.h"
21 #include "base/metrics/histogram_samples.h" 21 #include "base/metrics/histogram_samples.h"
22 #include "base/metrics/statistics_recorder.h" 22 #include "base/metrics/statistics_recorder.h"
23 #include "base/prefs/testing_pref_store.h" 23 #include "base/prefs/testing_pref_store.h"
24 #include "base/values.h" 24 #include "base/values.h"
25 #include "chrome/browser/prefs/tracked/hash_store_contents.h" 25 #include "components/prefs_tracker/hash_store_contents.h"
26 #include "chrome/browser/prefs/tracked/mock_validation_delegate.h" 26 #include "components/prefs_tracker/mock_validation_delegate.h"
27 #include "chrome/browser/prefs/tracked/pref_hash_store.h" 27 #include "components/prefs_tracker/pref_hash_store.h"
28 #include "chrome/browser/prefs/tracked/pref_hash_store_transaction.h" 28 #include "components/prefs_tracker/pref_hash_store_transaction.h"
29 #include "chrome/common/pref_names.h" 29 #include "components/prefs_tracker/pref_names.h"
30 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
31 31
32 namespace { 32 namespace {
33 33
34 const char kAtomicPref[] = "atomic_pref"; 34 const char kAtomicPref[] = "atomic_pref";
35 const char kAtomicPref2[] = "atomic_pref2"; 35 const char kAtomicPref2[] = "atomic_pref2";
36 const char kAtomicPref3[] = "pref3"; 36 const char kAtomicPref3[] = "pref3";
37 const char kAtomicPref4[] = "pref4"; 37 const char kAtomicPref4[] = "pref4";
38 const char kReportOnlyPref[] = "report_only"; 38 const char kReportOnlyPref[] = "report_only";
39 const char kReportOnlySplitPref[] = "report_only_split_pref"; 39 const char kReportOnlySplitPref[] = "report_only_split_pref";
40 const char kSplitPref[] = "split_pref"; 40 const char kSplitPref[] = "split_pref";
41 41
42 const PrefHashFilter::TrackedPreferenceMetadata kTestTrackedPrefs[] = { 42 const PrefHashFilter::TrackedPreferenceMetadata kTestTrackedPrefs[] = {
43 { 43 {0,
44 0, kAtomicPref, PrefHashFilter::ENFORCE_ON_LOAD, 44 kAtomicPref,
45 PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 45 PrefHashFilter::ENFORCE_ON_LOAD,
46 PrefHashFilter::VALUE_PERSONAL 46 PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
47 }, 47 PrefHashFilter::VALUE_PERSONAL},
48 { 48 {1,
49 1, kReportOnlyPref, PrefHashFilter::NO_ENFORCEMENT, 49 kReportOnlyPref,
50 PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 50 PrefHashFilter::NO_ENFORCEMENT,
51 PrefHashFilter::VALUE_IMPERSONAL 51 PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
52 }, 52 PrefHashFilter::VALUE_IMPERSONAL},
53 { 53 {2,
54 2, kSplitPref, PrefHashFilter::ENFORCE_ON_LOAD, 54 kSplitPref,
55 PrefHashFilter::TRACKING_STRATEGY_SPLIT, 55 PrefHashFilter::ENFORCE_ON_LOAD,
56 PrefHashFilter::VALUE_IMPERSONAL 56 PrefHashFilter::TRACKING_STRATEGY_SPLIT,
57 }, 57 PrefHashFilter::VALUE_IMPERSONAL},
58 { 58 {3,
59 3, kReportOnlySplitPref, PrefHashFilter::NO_ENFORCEMENT, 59 kReportOnlySplitPref,
60 PrefHashFilter::TRACKING_STRATEGY_SPLIT, 60 PrefHashFilter::NO_ENFORCEMENT,
61 PrefHashFilter::VALUE_IMPERSONAL 61 PrefHashFilter::TRACKING_STRATEGY_SPLIT,
62 }, 62 PrefHashFilter::VALUE_IMPERSONAL},
63 { 63 {4,
64 4, kAtomicPref2, PrefHashFilter::ENFORCE_ON_LOAD, 64 kAtomicPref2,
65 PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 65 PrefHashFilter::ENFORCE_ON_LOAD,
66 PrefHashFilter::VALUE_IMPERSONAL 66 PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
67 }, 67 PrefHashFilter::VALUE_IMPERSONAL},
68 { 68 {5,
69 5, kAtomicPref3, PrefHashFilter::ENFORCE_ON_LOAD, 69 kAtomicPref3,
70 PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 70 PrefHashFilter::ENFORCE_ON_LOAD,
71 PrefHashFilter::VALUE_IMPERSONAL 71 PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
72 }, 72 PrefHashFilter::VALUE_IMPERSONAL},
73 { 73 {6,
74 6, kAtomicPref4, PrefHashFilter::ENFORCE_ON_LOAD, 74 kAtomicPref4,
75 PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 75 PrefHashFilter::ENFORCE_ON_LOAD,
76 PrefHashFilter::VALUE_IMPERSONAL 76 PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
77 }, 77 PrefHashFilter::VALUE_IMPERSONAL},
78 }; 78 };
79 79
80 } // namespace 80 } // namespace
81 81
82 // A PrefHashStore that allows simulation of CheckValue results and captures 82 // A PrefHashStore that allows simulation of CheckValue results and captures
83 // checked and stored values. 83 // checked and stored values.
84 class MockPrefHashStore : public PrefHashStore { 84 class MockPrefHashStore : public PrefHashStore {
85 public: 85 public:
86 typedef std::pair<const void*, PrefHashFilter::PrefTrackingStrategy> 86 typedef std::pair<const void*, PrefHashFilter::PrefTrackingStrategy>
87 ValuePtrStrategyPair; 87 ValuePtrStrategyPair;
(...skipping 27 matching lines...) Expand all
115 // Sets the value that will be returned from 115 // Sets the value that will be returned from
116 // PrefHashStoreTransaction::IsSuperMACValid(). 116 // PrefHashStoreTransaction::IsSuperMACValid().
117 void set_is_super_mac_valid_result(bool result) { 117 void set_is_super_mac_valid_result(bool result) {
118 is_super_mac_valid_result_ = result; 118 is_super_mac_valid_result_ = result;
119 } 119 }
120 120
121 // Returns the number of transactions that were performed. 121 // Returns the number of transactions that were performed.
122 size_t transactions_performed() { return transactions_performed_; } 122 size_t transactions_performed() { return transactions_performed_; }
123 123
124 // Returns the number of paths checked. 124 // Returns the number of paths checked.
125 size_t checked_paths_count() const { 125 size_t checked_paths_count() const { return checked_values_.size(); }
126 return checked_values_.size();
127 }
128 126
129 // Returns the number of paths stored. 127 // Returns the number of paths stored.
130 size_t stored_paths_count() const { 128 size_t stored_paths_count() const { return stored_values_.size(); }
131 return stored_values_.size();
132 }
133 129
134 // Returns the pointer value and strategy that was passed to 130 // Returns the pointer value and strategy that was passed to
135 // |CheckHash/CheckSplitHash| for |path|. The returned pointer could since 131 // |CheckHash/CheckSplitHash| for |path|. The returned pointer could since
136 // have been freed and is thus not safe to dereference. 132 // have been freed and is thus not safe to dereference.
137 ValuePtrStrategyPair checked_value(const std::string& path) const { 133 ValuePtrStrategyPair checked_value(const std::string& path) const {
138 std::map<std::string, ValuePtrStrategyPair>::const_iterator value = 134 std::map<std::string, ValuePtrStrategyPair>::const_iterator value =
139 checked_values_.find(path); 135 checked_values_.find(path);
140 if (value != checked_values_.end()) 136 if (value != checked_values_.end())
141 return value->second; 137 return value->second;
142 return std::make_pair( 138 return std::make_pair(
143 reinterpret_cast<void*>(0xBAD), 139 reinterpret_cast<void*>(0xBAD),
144 static_cast<PrefHashFilter::PrefTrackingStrategy>(-1)); 140 static_cast<PrefHashFilter::PrefTrackingStrategy>(-1));
145 } 141 }
146 142
147 // Returns the pointer value that was passed to |StoreHash/StoreSplitHash| for 143 // Returns the pointer value that was passed to |StoreHash/StoreSplitHash| for
148 // |path|. The returned pointer could since have been freed and is thus not 144 // |path|. The returned pointer could since have been freed and is thus not
149 // safe to dereference. 145 // safe to dereference.
150 ValuePtrStrategyPair stored_value(const std::string& path) const { 146 ValuePtrStrategyPair stored_value(const std::string& path) const {
151 std::map<std::string, ValuePtrStrategyPair>::const_iterator value = 147 std::map<std::string, ValuePtrStrategyPair>::const_iterator value =
152 stored_values_.find(path); 148 stored_values_.find(path);
153 if (value != stored_values_.end()) 149 if (value != stored_values_.end())
154 return value->second; 150 return value->second;
155 return std::make_pair( 151 return std::make_pair(
156 reinterpret_cast<void*>(0xBAD), 152 reinterpret_cast<void*>(0xBAD),
157 static_cast<PrefHashFilter::PrefTrackingStrategy>(-1)); 153 static_cast<PrefHashFilter::PrefTrackingStrategy>(-1));
158 } 154 }
159 155
160 // PrefHashStore implementation. 156 // PrefHashStore implementation.
161 scoped_ptr<PrefHashStoreTransaction> BeginTransaction( 157 scoped_ptr<PrefHashStoreTransaction> BeginTransaction(
162 scoped_ptr<HashStoreContents> storage) override; 158 scoped_ptr<HashStoreContents> storage) override;
163 159
164 private: 160 private:
165 // A MockPrefHashStoreTransaction is handed to the caller on 161 // A MockPrefHashStoreTransaction is handed to the caller on
166 // MockPrefHashStore::BeginTransaction(). It then stores state in its 162 // MockPrefHashStore::BeginTransaction(). It then stores state in its
167 // underlying MockPrefHashStore about calls it receives from that same caller 163 // underlying MockPrefHashStore about calls it receives from that same caller
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 const std::string& path, 201 const std::string& path,
206 const base::Value* value, 202 const base::Value* value,
207 PrefHashFilter::PrefTrackingStrategy strategy); 203 PrefHashFilter::PrefTrackingStrategy strategy);
208 204
209 // Records a call to this mock's StoreHash/StoreSplitHash methods. 205 // Records a call to this mock's StoreHash/StoreSplitHash methods.
210 void RecordStoreHash(const std::string& path, 206 void RecordStoreHash(const std::string& path,
211 const base::Value* new_value, 207 const base::Value* new_value,
212 PrefHashFilter::PrefTrackingStrategy strategy); 208 PrefHashFilter::PrefTrackingStrategy strategy);
213 209
214 std::map<std::string, PrefHashStoreTransaction::ValueState> check_results_; 210 std::map<std::string, PrefHashStoreTransaction::ValueState> check_results_;
215 std::map<std::string, std::vector<std::string> > invalid_keys_results_; 211 std::map<std::string, std::vector<std::string>> invalid_keys_results_;
216 212
217 bool stamp_super_mac_result_; 213 bool stamp_super_mac_result_;
218 bool is_super_mac_valid_result_; 214 bool is_super_mac_valid_result_;
219 215
220 std::map<std::string, ValuePtrStrategyPair> checked_values_; 216 std::map<std::string, ValuePtrStrategyPair> checked_values_;
221 std::map<std::string, ValuePtrStrategyPair> stored_values_; 217 std::map<std::string, ValuePtrStrategyPair> stored_values_;
222 218
223 // Number of transactions that were performed via this MockPrefHashStore. 219 // Number of transactions that were performed via this MockPrefHashStore.
224 size_t transactions_performed_; 220 size_t transactions_performed_;
225 221
226 // Whether a transaction is currently active (only one transaction should be 222 // Whether a transaction is currently active (only one transaction should be
227 // active at a time). 223 // active at a time).
228 bool transaction_active_; 224 bool transaction_active_;
229 225
230 DISALLOW_COPY_AND_ASSIGN(MockPrefHashStore); 226 DISALLOW_COPY_AND_ASSIGN(MockPrefHashStore);
231 }; 227 };
232 228
233 void MockPrefHashStore::SetCheckResult( 229 void MockPrefHashStore::SetCheckResult(
234 const std::string& path, PrefHashStoreTransaction::ValueState result) { 230 const std::string& path,
231 PrefHashStoreTransaction::ValueState result) {
235 check_results_.insert(std::make_pair(path, result)); 232 check_results_.insert(std::make_pair(path, result));
236 } 233 }
237 234
238 void MockPrefHashStore::SetInvalidKeysResult( 235 void MockPrefHashStore::SetInvalidKeysResult(
239 const std::string& path, 236 const std::string& path,
240 const std::vector<std::string>& invalid_keys_result) { 237 const std::vector<std::string>& invalid_keys_result) {
241 // Ensure |check_results_| has a CHANGED entry for |path|. 238 // Ensure |check_results_| has a CHANGED entry for |path|.
242 std::map<std::string, 239 std::map<std::string, PrefHashStoreTransaction::ValueState>::const_iterator
243 PrefHashStoreTransaction::ValueState>::const_iterator result = 240 result = check_results_.find(path);
244 check_results_.find(path);
245 ASSERT_TRUE(result != check_results_.end()); 241 ASSERT_TRUE(result != check_results_.end());
246 ASSERT_EQ(PrefHashStoreTransaction::CHANGED, result->second); 242 ASSERT_EQ(PrefHashStoreTransaction::CHANGED, result->second);
247 243
248 invalid_keys_results_.insert(std::make_pair(path, invalid_keys_result)); 244 invalid_keys_results_.insert(std::make_pair(path, invalid_keys_result));
249 } 245 }
250 246
251 scoped_ptr<PrefHashStoreTransaction> MockPrefHashStore::BeginTransaction( 247 scoped_ptr<PrefHashStoreTransaction> MockPrefHashStore::BeginTransaction(
252 scoped_ptr<HashStoreContents> storage) { 248 scoped_ptr<HashStoreContents> storage) {
253 EXPECT_FALSE(transaction_active_); 249 EXPECT_FALSE(transaction_active_);
254 return scoped_ptr<PrefHashStoreTransaction>( 250 return scoped_ptr<PrefHashStoreTransaction>(
255 new MockPrefHashStoreTransaction(this)); 251 new MockPrefHashStoreTransaction(this));
256 } 252 }
257 253
258 PrefHashStoreTransaction::ValueState MockPrefHashStore::RecordCheckValue( 254 PrefHashStoreTransaction::ValueState MockPrefHashStore::RecordCheckValue(
259 const std::string& path, 255 const std::string& path,
260 const base::Value* value, 256 const base::Value* value,
261 PrefHashFilter::PrefTrackingStrategy strategy) { 257 PrefHashFilter::PrefTrackingStrategy strategy) {
262 // Record that |path| was checked and validate that it wasn't previously 258 // Record that |path| was checked and validate that it wasn't previously
263 // checked. 259 // checked.
264 EXPECT_TRUE(checked_values_.insert( 260 EXPECT_TRUE(checked_values_.insert(std::make_pair(
265 std::make_pair(path, std::make_pair(value, strategy))).second); 261 path, std::make_pair(value, strategy)))
266 std::map<std::string, 262 .second);
267 PrefHashStoreTransaction::ValueState>::const_iterator result = 263 std::map<std::string, PrefHashStoreTransaction::ValueState>::const_iterator
268 check_results_.find(path); 264 result = check_results_.find(path);
269 if (result != check_results_.end()) 265 if (result != check_results_.end())
270 return result->second; 266 return result->second;
271 return PrefHashStoreTransaction::UNCHANGED; 267 return PrefHashStoreTransaction::UNCHANGED;
272 } 268 }
273 269
274 void MockPrefHashStore::RecordStoreHash( 270 void MockPrefHashStore::RecordStoreHash(
275 const std::string& path, 271 const std::string& path,
276 const base::Value* new_value, 272 const base::Value* new_value,
277 PrefHashFilter::PrefTrackingStrategy strategy) { 273 PrefHashFilter::PrefTrackingStrategy strategy) {
278 EXPECT_TRUE(stored_values_.insert( 274 EXPECT_TRUE(
279 std::make_pair(path, std::make_pair(new_value, strategy))).second); 275 stored_values_.insert(std::make_pair(path,
276 std::make_pair(new_value, strategy)))
277 .second);
280 } 278 }
281 279
282 PrefHashStoreTransaction::ValueState 280 PrefHashStoreTransaction::ValueState
283 MockPrefHashStore::MockPrefHashStoreTransaction::CheckValue( 281 MockPrefHashStore::MockPrefHashStoreTransaction::CheckValue(
284 const std::string& path, const base::Value* value) const { 282 const std::string& path,
283 const base::Value* value) const {
285 return outer_->RecordCheckValue(path, value, 284 return outer_->RecordCheckValue(path, value,
286 PrefHashFilter::TRACKING_STRATEGY_ATOMIC); 285 PrefHashFilter::TRACKING_STRATEGY_ATOMIC);
287 } 286 }
288 287
289 void MockPrefHashStore::MockPrefHashStoreTransaction::StoreHash( 288 void MockPrefHashStore::MockPrefHashStoreTransaction::StoreHash(
290 const std::string& path, 289 const std::string& path,
291 const base::Value* new_value) { 290 const base::Value* new_value) {
292 outer_->RecordStoreHash(path, new_value, 291 outer_->RecordStoreHash(path, new_value,
293 PrefHashFilter::TRACKING_STRATEGY_ATOMIC); 292 PrefHashFilter::TRACKING_STRATEGY_ATOMIC);
294 } 293 }
295 294
296 PrefHashStoreTransaction::ValueState 295 PrefHashStoreTransaction::ValueState
297 MockPrefHashStore::MockPrefHashStoreTransaction::CheckSplitValue( 296 MockPrefHashStore::MockPrefHashStoreTransaction::CheckSplitValue(
298 const std::string& path, 297 const std::string& path,
299 const base::DictionaryValue* initial_split_value, 298 const base::DictionaryValue* initial_split_value,
300 std::vector<std::string>* invalid_keys) const { 299 std::vector<std::string>* invalid_keys) const {
301 EXPECT_TRUE(invalid_keys && invalid_keys->empty()); 300 EXPECT_TRUE(invalid_keys && invalid_keys->empty());
302 301
303 std::map<std::string, std::vector<std::string> >::const_iterator 302 std::map<std::string, std::vector<std::string>>::const_iterator
304 invalid_keys_result = outer_->invalid_keys_results_.find(path); 303 invalid_keys_result = outer_->invalid_keys_results_.find(path);
305 if (invalid_keys_result != outer_->invalid_keys_results_.end()) { 304 if (invalid_keys_result != outer_->invalid_keys_results_.end()) {
306 invalid_keys->insert(invalid_keys->begin(), 305 invalid_keys->insert(invalid_keys->begin(),
307 invalid_keys_result->second.begin(), 306 invalid_keys_result->second.begin(),
308 invalid_keys_result->second.end()); 307 invalid_keys_result->second.end());
309 } 308 }
310 309
311 return outer_->RecordCheckValue(path, initial_split_value, 310 return outer_->RecordCheckValue(path, initial_split_value,
312 PrefHashFilter::TRACKING_STRATEGY_SPLIT); 311 PrefHashFilter::TRACKING_STRATEGY_SPLIT);
313 } 312 }
314 313
315 void MockPrefHashStore::MockPrefHashStoreTransaction::StoreSplitHash( 314 void MockPrefHashStore::MockPrefHashStoreTransaction::StoreSplitHash(
316 const std::string& path, 315 const std::string& path,
317 const base::DictionaryValue* new_value) { 316 const base::DictionaryValue* new_value) {
318 outer_->RecordStoreHash(path, new_value, 317 outer_->RecordStoreHash(path, new_value,
319 PrefHashFilter::TRACKING_STRATEGY_SPLIT); 318 PrefHashFilter::TRACKING_STRATEGY_SPLIT);
320 } 319 }
321 320
322 bool MockPrefHashStore::MockPrefHashStoreTransaction::HasHash( 321 bool MockPrefHashStore::MockPrefHashStoreTransaction::HasHash(
323 const std::string& path) const { 322 const std::string& path) const {
324 ADD_FAILURE() << "Unexpected call."; 323 ADD_FAILURE() << "Unexpected call.";
325 return false; 324 return false;
326 } 325 }
327 326
328 void MockPrefHashStore::MockPrefHashStoreTransaction::ImportHash( 327 void MockPrefHashStore::MockPrefHashStoreTransaction::ImportHash(
329 const std::string& path, 328 const std::string& path,
330 const base::Value* hash) { 329 const base::Value* hash) {
331 ADD_FAILURE() << "Unexpected call."; 330 ADD_FAILURE() << "Unexpected call.";
332 } 331 }
333 332
334 void MockPrefHashStore::MockPrefHashStoreTransaction::ClearHash( 333 void MockPrefHashStore::MockPrefHashStoreTransaction::ClearHash(
335 const std::string& path) { 334 const std::string& path) {
336 // Allow this to be called by PrefHashFilter's deprecated tracked prefs 335 // Allow this to be called by PrefHashFilter's deprecated tracked prefs
337 // cleanup tasks. 336 // cleanup tasks.
338 } 337 }
339 338
340 bool MockPrefHashStore::MockPrefHashStoreTransaction::IsSuperMACValid() const { 339 bool MockPrefHashStore::MockPrefHashStoreTransaction::IsSuperMACValid() const {
341 return outer_->is_super_mac_valid_result_; 340 return outer_->is_super_mac_valid_result_;
342 } 341 }
343 342
344 bool MockPrefHashStore::MockPrefHashStoreTransaction::StampSuperMac() { 343 bool MockPrefHashStore::MockPrefHashStoreTransaction::StampSuperMac() {
345 return outer_->stamp_super_mac_result_; 344 return outer_->stamp_super_mac_result_;
346 } 345 }
347 346
348 std::vector<PrefHashFilter::TrackedPreferenceMetadata> GetConfiguration( 347 std::vector<PrefHashFilter::TrackedPreferenceMetadata> GetConfiguration(
349 PrefHashFilter::EnforcementLevel max_enforcement_level) { 348 PrefHashFilter::EnforcementLevel max_enforcement_level) {
350 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration( 349 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration(
351 kTestTrackedPrefs, kTestTrackedPrefs + arraysize(kTestTrackedPrefs)); 350 kTestTrackedPrefs, kTestTrackedPrefs + arraysize(kTestTrackedPrefs));
352 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = 351 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it =
353 configuration.begin(); 352 configuration.begin();
354 it != configuration.end(); 353 it != configuration.end(); ++it) {
355 ++it) {
356 if (it->enforcement_level > max_enforcement_level) 354 if (it->enforcement_level > max_enforcement_level)
357 it->enforcement_level = max_enforcement_level; 355 it->enforcement_level = max_enforcement_level;
358 } 356 }
359 return configuration; 357 return configuration;
360 } 358 }
361 359
362 class PrefHashFilterTest 360 class PrefHashFilterTest
363 : public testing::TestWithParam<PrefHashFilter::EnforcementLevel> { 361 : public testing::TestWithParam<PrefHashFilter::EnforcementLevel> {
364 public: 362 public:
365 PrefHashFilterTest() : mock_pref_hash_store_(NULL), 363 PrefHashFilterTest()
366 pref_store_contents_(new base::DictionaryValue), 364 : mock_pref_hash_store_(NULL),
367 reset_recorded_(false) {} 365 pref_store_contents_(new base::DictionaryValue),
366 reset_recorded_(false) {}
368 367
369 void SetUp() override { 368 void SetUp() override {
370 base::StatisticsRecorder::Initialize(); 369 base::StatisticsRecorder::Initialize();
371 Reset(); 370 Reset();
372 } 371 }
373 372
374 protected: 373 protected:
375 // Reset the PrefHashFilter instance. 374 // Reset the PrefHashFilter instance.
376 void Reset() { 375 void Reset() {
377 // Construct a PrefHashFilter and MockPrefHashStore for the test. 376 // Construct a PrefHashFilter and MockPrefHashStore for the test.
378 InitializePrefHashFilter(GetConfiguration(GetParam())); 377 InitializePrefHashFilter(GetConfiguration(GetParam()));
379 } 378 }
380 379
381 // Initializes |pref_hash_filter_| with a PrefHashFilter that uses a 380 // Initializes |pref_hash_filter_| with a PrefHashFilter that uses a
382 // MockPrefHashStore. The raw pointer to the MockPrefHashStore (owned by the 381 // MockPrefHashStore. The raw pointer to the MockPrefHashStore (owned by the
383 // PrefHashFilter) is stored in |mock_pref_hash_store_|. 382 // PrefHashFilter) is stored in |mock_pref_hash_store_|.
384 void InitializePrefHashFilter(const std::vector< 383 void InitializePrefHashFilter(const std::vector<
385 PrefHashFilter::TrackedPreferenceMetadata>& configuration) { 384 PrefHashFilter::TrackedPreferenceMetadata>& configuration) {
386 scoped_ptr<MockPrefHashStore> temp_mock_pref_hash_store( 385 scoped_ptr<MockPrefHashStore> temp_mock_pref_hash_store(
387 new MockPrefHashStore); 386 new MockPrefHashStore);
388 mock_pref_hash_store_ = temp_mock_pref_hash_store.get(); 387 mock_pref_hash_store_ = temp_mock_pref_hash_store.get();
389 pref_hash_filter_.reset(new PrefHashFilter( 388 pref_hash_filter_.reset(new PrefHashFilter(
390 temp_mock_pref_hash_store.Pass(), 389 temp_mock_pref_hash_store.Pass(), configuration,
391 configuration,
392 base::Bind(&PrefHashFilterTest::RecordReset, base::Unretained(this)), 390 base::Bind(&PrefHashFilterTest::RecordReset, base::Unretained(this)),
393 &mock_validation_delegate_, 391 &mock_validation_delegate_, arraysize(kTestTrackedPrefs), true));
394 arraysize(kTestTrackedPrefs),
395 true));
396 } 392 }
397 393
398 // Verifies whether a reset was reported by the PrefHashFiler. Also verifies 394 // Verifies whether a reset was reported by the PrefHashFiler. Also verifies
399 // that kPreferenceResetTime was set (or not) accordingly. 395 // that kPreferenceResetTime was set (or not) accordingly.
400 void VerifyRecordedReset(bool reset_expected) { 396 void VerifyRecordedReset(bool reset_expected) {
401 EXPECT_EQ(reset_expected, reset_recorded_); 397 EXPECT_EQ(reset_expected, reset_recorded_);
402 EXPECT_EQ(reset_expected, 398 EXPECT_EQ(reset_expected,
403 pref_store_contents_->Get(prefs::kPreferenceResetTime, NULL)); 399 pref_store_contents_->Get(prefs::kPreferenceResetTime, NULL));
404 } 400 }
405 401
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 TEST_P(PrefHashFilterTest, EmptyAndUnchanged) { 441 TEST_P(PrefHashFilterTest, EmptyAndUnchanged) {
446 DoFilterOnLoad(false); 442 DoFilterOnLoad(false);
447 // All paths checked. 443 // All paths checked.
448 ASSERT_EQ(arraysize(kTestTrackedPrefs), 444 ASSERT_EQ(arraysize(kTestTrackedPrefs),
449 mock_pref_hash_store_->checked_paths_count()); 445 mock_pref_hash_store_->checked_paths_count());
450 // No paths stored, since they all return |UNCHANGED|. 446 // No paths stored, since they all return |UNCHANGED|.
451 ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count()); 447 ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
452 // Since there was nothing in |pref_store_contents_| the checked value should 448 // Since there was nothing in |pref_store_contents_| the checked value should
453 // have been NULL for all tracked preferences. 449 // have been NULL for all tracked preferences.
454 for (size_t i = 0; i < arraysize(kTestTrackedPrefs); ++i) { 450 for (size_t i = 0; i < arraysize(kTestTrackedPrefs); ++i) {
455 ASSERT_EQ(NULL, mock_pref_hash_store_->checked_value( 451 ASSERT_EQ(
456 kTestTrackedPrefs[i].name).first); 452 NULL,
453 mock_pref_hash_store_->checked_value(kTestTrackedPrefs[i].name).first);
457 } 454 }
458 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 455 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
459 VerifyRecordedReset(false); 456 VerifyRecordedReset(false);
460 457
461 // Delegate saw all paths, and all unchanged. 458 // Delegate saw all paths, and all unchanged.
462 ASSERT_EQ(arraysize(kTestTrackedPrefs), 459 ASSERT_EQ(arraysize(kTestTrackedPrefs),
463 mock_validation_delegate_.recorded_validations_count()); 460 mock_validation_delegate_.recorded_validations_count());
464 ASSERT_EQ(arraysize(kTestTrackedPrefs), 461 ASSERT_EQ(arraysize(kTestTrackedPrefs),
465 mock_validation_delegate_.CountValidationsOfState( 462 mock_validation_delegate_.CountValidationsOfState(
466 PrefHashStoreTransaction::UNCHANGED)); 463 PrefHashStoreTransaction::UNCHANGED));
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 root_dict.Set(kSplitPref, dict_value); 538 root_dict.Set(kSplitPref, dict_value);
542 539
543 // No path should be stored on FilterUpdate. 540 // No path should be stored on FilterUpdate.
544 pref_hash_filter_->FilterUpdate(kSplitPref); 541 pref_hash_filter_->FilterUpdate(kSplitPref);
545 ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count()); 542 ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
546 543
547 // One path should be stored on FilterSerializeData. 544 // One path should be stored on FilterSerializeData.
548 pref_hash_filter_->FilterSerializeData(&root_dict); 545 pref_hash_filter_->FilterSerializeData(&root_dict);
549 ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths_count()); 546 ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths_count());
550 MockPrefHashStore::ValuePtrStrategyPair stored_value = 547 MockPrefHashStore::ValuePtrStrategyPair stored_value =
551 mock_pref_hash_store_->stored_value(kSplitPref); 548 mock_pref_hash_store_->stored_value(kSplitPref);
552 ASSERT_EQ(dict_value, stored_value.first); 549 ASSERT_EQ(dict_value, stored_value.first);
553 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_value.second); 550 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_value.second);
554 551
555 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 552 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
556 VerifyRecordedReset(false); 553 VerifyRecordedReset(false);
557 } 554 }
558 555
559 TEST_P(PrefHashFilterTest, FilterUntrackedPrefUpdate) { 556 TEST_P(PrefHashFilterTest, FilterUntrackedPrefUpdate) {
560 base::DictionaryValue root_dict; 557 base::DictionaryValue root_dict;
561 root_dict.Set("untracked", new base::StringValue("some value")); 558 root_dict.Set("untracked", new base::StringValue("some value"));
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 stored_value_atomic1.second); 607 stored_value_atomic1.second);
611 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 608 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
612 609
613 MockPrefHashStore::ValuePtrStrategyPair stored_value_atomic3 = 610 MockPrefHashStore::ValuePtrStrategyPair stored_value_atomic3 =
614 mock_pref_hash_store_->stored_value(kAtomicPref3); 611 mock_pref_hash_store_->stored_value(kAtomicPref3);
615 ASSERT_EQ(int_value5, stored_value_atomic3.first); 612 ASSERT_EQ(int_value5, stored_value_atomic3.first);
616 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 613 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
617 stored_value_atomic3.second); 614 stored_value_atomic3.second);
618 615
619 MockPrefHashStore::ValuePtrStrategyPair stored_value_split = 616 MockPrefHashStore::ValuePtrStrategyPair stored_value_split =
620 mock_pref_hash_store_->stored_value(kSplitPref); 617 mock_pref_hash_store_->stored_value(kSplitPref);
621 ASSERT_EQ(dict_value, stored_value_split.first); 618 ASSERT_EQ(dict_value, stored_value_split.first);
622 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_value_split.second); 619 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_value_split.second);
623 } 620 }
624 621
625 TEST_P(PrefHashFilterTest, UnknownNullValue) { 622 TEST_P(PrefHashFilterTest, UnknownNullValue) {
626 ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL)); 623 ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
627 ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL)); 624 ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL));
628 // NULL values are always trusted by the PrefHashStore. 625 // NULL values are always trusted by the PrefHashStore.
629 mock_pref_hash_store_->SetCheckResult( 626 mock_pref_hash_store_->SetCheckResult(
630 kAtomicPref, PrefHashStoreTransaction::TRUSTED_NULL_VALUE); 627 kAtomicPref, PrefHashStoreTransaction::TRUSTED_NULL_VALUE);
631 mock_pref_hash_store_->SetCheckResult( 628 mock_pref_hash_store_->SetCheckResult(
632 kSplitPref, PrefHashStoreTransaction::TRUSTED_NULL_VALUE); 629 kSplitPref, PrefHashStoreTransaction::TRUSTED_NULL_VALUE);
633 DoFilterOnLoad(false); 630 DoFilterOnLoad(false);
634 ASSERT_EQ(arraysize(kTestTrackedPrefs), 631 ASSERT_EQ(arraysize(kTestTrackedPrefs),
635 mock_pref_hash_store_->checked_paths_count()); 632 mock_pref_hash_store_->checked_paths_count());
636 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count()); 633 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
637 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 634 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
638 635
639 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value = 636 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
640 mock_pref_hash_store_->stored_value(kAtomicPref); 637 mock_pref_hash_store_->stored_value(kAtomicPref);
641 ASSERT_EQ(NULL, stored_atomic_value.first); 638 ASSERT_EQ(NULL, stored_atomic_value.first);
642 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 639 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
643 stored_atomic_value.second); 640 stored_atomic_value.second);
644 641
645 MockPrefHashStore::ValuePtrStrategyPair stored_split_value = 642 MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
646 mock_pref_hash_store_->stored_value(kSplitPref); 643 mock_pref_hash_store_->stored_value(kSplitPref);
647 ASSERT_EQ(NULL, stored_split_value.first); 644 ASSERT_EQ(NULL, stored_split_value.first);
648 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, 645 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_split_value.second);
649 stored_split_value.second);
650 646
651 // Delegate saw all prefs, two of which had the expected value_state. 647 // Delegate saw all prefs, two of which had the expected value_state.
652 ASSERT_EQ(arraysize(kTestTrackedPrefs), 648 ASSERT_EQ(arraysize(kTestTrackedPrefs),
653 mock_validation_delegate_.recorded_validations_count()); 649 mock_validation_delegate_.recorded_validations_count());
654 ASSERT_EQ(2u, 650 ASSERT_EQ(2u, mock_validation_delegate_.CountValidationsOfState(
655 mock_validation_delegate_.CountValidationsOfState( 651 PrefHashStoreTransaction::TRUSTED_NULL_VALUE));
656 PrefHashStoreTransaction::TRUSTED_NULL_VALUE));
657 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u, 652 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u,
658 mock_validation_delegate_.CountValidationsOfState( 653 mock_validation_delegate_.CountValidationsOfState(
659 PrefHashStoreTransaction::UNCHANGED)); 654 PrefHashStoreTransaction::UNCHANGED));
660 655
661 const MockValidationDelegate::ValidationEvent* validated_split_pref = 656 const MockValidationDelegate::ValidationEvent* validated_split_pref =
662 mock_validation_delegate_.GetEventForPath(kSplitPref); 657 mock_validation_delegate_.GetEventForPath(kSplitPref);
663 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, 658 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT,
664 validated_split_pref->strategy); 659 validated_split_pref->strategy);
665 ASSERT_FALSE(validated_split_pref->is_personal); 660 ASSERT_FALSE(validated_split_pref->is_personal);
666 const MockValidationDelegate::ValidationEvent* validated_atomic_pref = 661 const MockValidationDelegate::ValidationEvent* validated_atomic_pref =
(...skipping 23 matching lines...) Expand all
690 // If we are enforcing, expect this to report changes. 685 // If we are enforcing, expect this to report changes.
691 DoFilterOnLoad(GetParam() >= PrefHashFilter::ENFORCE_ON_LOAD); 686 DoFilterOnLoad(GetParam() >= PrefHashFilter::ENFORCE_ON_LOAD);
692 ASSERT_EQ(arraysize(kTestTrackedPrefs), 687 ASSERT_EQ(arraysize(kTestTrackedPrefs),
693 mock_pref_hash_store_->checked_paths_count()); 688 mock_pref_hash_store_->checked_paths_count());
694 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count()); 689 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
695 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 690 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
696 691
697 // Delegate saw all prefs, two of which had the expected value_state. 692 // Delegate saw all prefs, two of which had the expected value_state.
698 ASSERT_EQ(arraysize(kTestTrackedPrefs), 693 ASSERT_EQ(arraysize(kTestTrackedPrefs),
699 mock_validation_delegate_.recorded_validations_count()); 694 mock_validation_delegate_.recorded_validations_count());
700 ASSERT_EQ(2u, 695 ASSERT_EQ(2u, mock_validation_delegate_.CountValidationsOfState(
701 mock_validation_delegate_.CountValidationsOfState( 696 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE));
702 PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE));
703 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u, 697 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u,
704 mock_validation_delegate_.CountValidationsOfState( 698 mock_validation_delegate_.CountValidationsOfState(
705 PrefHashStoreTransaction::UNCHANGED)); 699 PrefHashStoreTransaction::UNCHANGED));
706 700
707 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value = 701 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
708 mock_pref_hash_store_->stored_value(kAtomicPref); 702 mock_pref_hash_store_->stored_value(kAtomicPref);
709 MockPrefHashStore::ValuePtrStrategyPair stored_split_value = 703 MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
710 mock_pref_hash_store_->stored_value(kSplitPref); 704 mock_pref_hash_store_->stored_value(kSplitPref);
711 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 705 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
712 stored_atomic_value.second); 706 stored_atomic_value.second);
713 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, 707 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_split_value.second);
714 stored_split_value.second);
715 if (GetParam() == PrefHashFilter::ENFORCE_ON_LOAD) { 708 if (GetParam() == PrefHashFilter::ENFORCE_ON_LOAD) {
716 // Ensure the prefs were cleared and the hashes for NULL were restored if 709 // Ensure the prefs were cleared and the hashes for NULL were restored if
717 // the current enforcement level denies seeding. 710 // the current enforcement level denies seeding.
718 ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL)); 711 ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
719 ASSERT_EQ(NULL, stored_atomic_value.first); 712 ASSERT_EQ(NULL, stored_atomic_value.first);
720 713
721 ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL)); 714 ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL));
722 ASSERT_EQ(NULL, stored_split_value.first); 715 ASSERT_EQ(NULL, stored_split_value.first);
723 716
724 VerifyRecordedReset(true); 717 VerifyRecordedReset(true);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 kSplitPref, PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE); 751 kSplitPref, PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE);
759 DoFilterOnLoad(false); 752 DoFilterOnLoad(false);
760 ASSERT_EQ(arraysize(kTestTrackedPrefs), 753 ASSERT_EQ(arraysize(kTestTrackedPrefs),
761 mock_pref_hash_store_->checked_paths_count()); 754 mock_pref_hash_store_->checked_paths_count());
762 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count()); 755 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
763 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 756 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
764 757
765 // Delegate saw all prefs, two of which had the expected value_state. 758 // Delegate saw all prefs, two of which had the expected value_state.
766 ASSERT_EQ(arraysize(kTestTrackedPrefs), 759 ASSERT_EQ(arraysize(kTestTrackedPrefs),
767 mock_validation_delegate_.recorded_validations_count()); 760 mock_validation_delegate_.recorded_validations_count());
768 ASSERT_EQ(2u, 761 ASSERT_EQ(2u, mock_validation_delegate_.CountValidationsOfState(
769 mock_validation_delegate_.CountValidationsOfState( 762 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE));
770 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE));
771 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u, 763 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u,
772 mock_validation_delegate_.CountValidationsOfState( 764 mock_validation_delegate_.CountValidationsOfState(
773 PrefHashStoreTransaction::UNCHANGED)); 765 PrefHashStoreTransaction::UNCHANGED));
774 766
775 // Seeding is always allowed for trusted unknown values. 767 // Seeding is always allowed for trusted unknown values.
776 const base::Value* atomic_value_in_store; 768 const base::Value* atomic_value_in_store;
777 ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &atomic_value_in_store)); 769 ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &atomic_value_in_store));
778 ASSERT_EQ(string_value, atomic_value_in_store); 770 ASSERT_EQ(string_value, atomic_value_in_store);
779 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value = 771 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
780 mock_pref_hash_store_->stored_value(kAtomicPref); 772 mock_pref_hash_store_->stored_value(kAtomicPref);
781 ASSERT_EQ(string_value, stored_atomic_value.first); 773 ASSERT_EQ(string_value, stored_atomic_value.first);
782 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 774 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
783 stored_atomic_value.second); 775 stored_atomic_value.second);
784 776
785 const base::Value* split_value_in_store; 777 const base::Value* split_value_in_store;
786 ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store)); 778 ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store));
787 ASSERT_EQ(dict_value, split_value_in_store); 779 ASSERT_EQ(dict_value, split_value_in_store);
788 MockPrefHashStore::ValuePtrStrategyPair stored_split_value = 780 MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
789 mock_pref_hash_store_->stored_value(kSplitPref); 781 mock_pref_hash_store_->stored_value(kSplitPref);
790 ASSERT_EQ(dict_value, stored_split_value.first); 782 ASSERT_EQ(dict_value, stored_split_value.first);
791 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, 783 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_split_value.second);
792 stored_split_value.second);
793 } 784 }
794 785
795 TEST_P(PrefHashFilterTest, InitialValueChanged) { 786 TEST_P(PrefHashFilterTest, InitialValueChanged) {
796 // Ownership of this value is transfered to |pref_store_contents_|. 787 // Ownership of this value is transfered to |pref_store_contents_|.
797 base::Value* int_value = new base::FundamentalValue(1234); 788 base::Value* int_value = new base::FundamentalValue(1234);
798 pref_store_contents_->Set(kAtomicPref, int_value); 789 pref_store_contents_->Set(kAtomicPref, int_value);
799 790
800 base::DictionaryValue* dict_value = new base::DictionaryValue; 791 base::DictionaryValue* dict_value = new base::DictionaryValue;
801 dict_value->SetString("a", "foo"); 792 dict_value->SetString("a", "foo");
802 dict_value->SetInteger("b", 1234); 793 dict_value->SetInteger("b", 1234);
(...skipping 14 matching lines...) Expand all
817 mock_invalid_keys.push_back("c"); 808 mock_invalid_keys.push_back("c");
818 mock_pref_hash_store_->SetInvalidKeysResult(kSplitPref, mock_invalid_keys); 809 mock_pref_hash_store_->SetInvalidKeysResult(kSplitPref, mock_invalid_keys);
819 810
820 DoFilterOnLoad(GetParam() >= PrefHashFilter::ENFORCE_ON_LOAD); 811 DoFilterOnLoad(GetParam() >= PrefHashFilter::ENFORCE_ON_LOAD);
821 ASSERT_EQ(arraysize(kTestTrackedPrefs), 812 ASSERT_EQ(arraysize(kTestTrackedPrefs),
822 mock_pref_hash_store_->checked_paths_count()); 813 mock_pref_hash_store_->checked_paths_count());
823 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count()); 814 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
824 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 815 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
825 816
826 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value = 817 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
827 mock_pref_hash_store_->stored_value(kAtomicPref); 818 mock_pref_hash_store_->stored_value(kAtomicPref);
828 MockPrefHashStore::ValuePtrStrategyPair stored_split_value = 819 MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
829 mock_pref_hash_store_->stored_value(kSplitPref); 820 mock_pref_hash_store_->stored_value(kSplitPref);
830 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 821 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
831 stored_atomic_value.second); 822 stored_atomic_value.second);
832 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, 823 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_split_value.second);
833 stored_split_value.second);
834 if (GetParam() == PrefHashFilter::ENFORCE_ON_LOAD) { 824 if (GetParam() == PrefHashFilter::ENFORCE_ON_LOAD) {
835 // Ensure the atomic pref was cleared and the hash for NULL was restored if 825 // Ensure the atomic pref was cleared and the hash for NULL was restored if
836 // the current enforcement level prevents changes. 826 // the current enforcement level prevents changes.
837 ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL)); 827 ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
838 ASSERT_EQ(NULL, stored_atomic_value.first); 828 ASSERT_EQ(NULL, stored_atomic_value.first);
839 829
840 // The split pref on the other hand should only have been stripped of its 830 // The split pref on the other hand should only have been stripped of its
841 // invalid keys. 831 // invalid keys.
842 const base::Value* split_value_in_store; 832 const base::Value* split_value_in_store;
843 ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store)); 833 ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 PrefHashStoreTransaction::CLEARED); 870 PrefHashStoreTransaction::CLEARED);
881 DoFilterOnLoad(false); 871 DoFilterOnLoad(false);
882 ASSERT_EQ(arraysize(kTestTrackedPrefs), 872 ASSERT_EQ(arraysize(kTestTrackedPrefs),
883 mock_pref_hash_store_->checked_paths_count()); 873 mock_pref_hash_store_->checked_paths_count());
884 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count()); 874 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
885 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 875 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
886 876
887 // Delegate saw all prefs, two of which had the expected value_state. 877 // Delegate saw all prefs, two of which had the expected value_state.
888 ASSERT_EQ(arraysize(kTestTrackedPrefs), 878 ASSERT_EQ(arraysize(kTestTrackedPrefs),
889 mock_validation_delegate_.recorded_validations_count()); 879 mock_validation_delegate_.recorded_validations_count());
890 ASSERT_EQ(2u, 880 ASSERT_EQ(2u, mock_validation_delegate_.CountValidationsOfState(
891 mock_validation_delegate_.CountValidationsOfState( 881 PrefHashStoreTransaction::CLEARED));
892 PrefHashStoreTransaction::CLEARED));
893 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u, 882 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u,
894 mock_validation_delegate_.CountValidationsOfState( 883 mock_validation_delegate_.CountValidationsOfState(
895 PrefHashStoreTransaction::UNCHANGED)); 884 PrefHashStoreTransaction::UNCHANGED));
896 885
897 // Regardless of the enforcement level, the only thing that should be done is 886 // Regardless of the enforcement level, the only thing that should be done is
898 // to restore the hash for NULL. The value itself should still be NULL. 887 // to restore the hash for NULL. The value itself should still be NULL.
899 ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL)); 888 ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
900 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value = 889 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
901 mock_pref_hash_store_->stored_value(kAtomicPref); 890 mock_pref_hash_store_->stored_value(kAtomicPref);
902 ASSERT_EQ(NULL, stored_atomic_value.first); 891 ASSERT_EQ(NULL, stored_atomic_value.first);
903 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 892 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
904 stored_atomic_value.second); 893 stored_atomic_value.second);
905 894
906 ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL)); 895 ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL));
907 MockPrefHashStore::ValuePtrStrategyPair stored_split_value = 896 MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
908 mock_pref_hash_store_->stored_value(kSplitPref); 897 mock_pref_hash_store_->stored_value(kSplitPref);
909 ASSERT_EQ(NULL, stored_split_value.first); 898 ASSERT_EQ(NULL, stored_split_value.first);
910 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, 899 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_split_value.second);
911 stored_split_value.second);
912 } 900 }
913 901
914 TEST_P(PrefHashFilterTest, InitialValueUnchangedLegacyId) { 902 TEST_P(PrefHashFilterTest, InitialValueUnchangedLegacyId) {
915 // Ownership of these values is transfered to |pref_store_contents_|. 903 // Ownership of these values is transfered to |pref_store_contents_|.
916 base::StringValue* string_value = new base::StringValue("string value"); 904 base::StringValue* string_value = new base::StringValue("string value");
917 pref_store_contents_->Set(kAtomicPref, string_value); 905 pref_store_contents_->Set(kAtomicPref, string_value);
918 906
919 base::DictionaryValue* dict_value = new base::DictionaryValue; 907 base::DictionaryValue* dict_value = new base::DictionaryValue;
920 dict_value->SetString("a", "foo"); 908 dict_value->SetString("a", "foo");
921 dict_value->SetInteger("b", 1234); 909 dict_value->SetInteger("b", 1234);
922 pref_store_contents_->Set(kSplitPref, dict_value); 910 pref_store_contents_->Set(kSplitPref, dict_value);
923 911
924 ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, NULL)); 912 ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, NULL));
925 ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, NULL)); 913 ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, NULL));
926 914
927 mock_pref_hash_store_->SetCheckResult( 915 mock_pref_hash_store_->SetCheckResult(
928 kAtomicPref, PrefHashStoreTransaction::SECURE_LEGACY); 916 kAtomicPref, PrefHashStoreTransaction::SECURE_LEGACY);
929 mock_pref_hash_store_->SetCheckResult( 917 mock_pref_hash_store_->SetCheckResult(
930 kSplitPref, PrefHashStoreTransaction::SECURE_LEGACY); 918 kSplitPref, PrefHashStoreTransaction::SECURE_LEGACY);
931 DoFilterOnLoad(false); 919 DoFilterOnLoad(false);
932 ASSERT_EQ(arraysize(kTestTrackedPrefs), 920 ASSERT_EQ(arraysize(kTestTrackedPrefs),
933 mock_pref_hash_store_->checked_paths_count()); 921 mock_pref_hash_store_->checked_paths_count());
934 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 922 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
935 923
936 // Delegate saw all prefs, two of which had the expected value_state. 924 // Delegate saw all prefs, two of which had the expected value_state.
937 ASSERT_EQ(arraysize(kTestTrackedPrefs), 925 ASSERT_EQ(arraysize(kTestTrackedPrefs),
938 mock_validation_delegate_.recorded_validations_count()); 926 mock_validation_delegate_.recorded_validations_count());
939 ASSERT_EQ(2u, 927 ASSERT_EQ(2u, mock_validation_delegate_.CountValidationsOfState(
940 mock_validation_delegate_.CountValidationsOfState( 928 PrefHashStoreTransaction::SECURE_LEGACY));
941 PrefHashStoreTransaction::SECURE_LEGACY));
942 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u, 929 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u,
943 mock_validation_delegate_.CountValidationsOfState( 930 mock_validation_delegate_.CountValidationsOfState(
944 PrefHashStoreTransaction::UNCHANGED)); 931 PrefHashStoreTransaction::UNCHANGED));
945 932
946 // Ensure that both the atomic and split hashes were restored. 933 // Ensure that both the atomic and split hashes were restored.
947 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count()); 934 ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
948 935
949 // In all cases, the values should have remained intact and the hashes should 936 // In all cases, the values should have remained intact and the hashes should
950 // have been updated to match them. 937 // have been updated to match them.
951 938
952 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value = 939 MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
953 mock_pref_hash_store_->stored_value(kAtomicPref); 940 mock_pref_hash_store_->stored_value(kAtomicPref);
954 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC, 941 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
955 stored_atomic_value.second); 942 stored_atomic_value.second);
956 const base::Value* atomic_value_in_store; 943 const base::Value* atomic_value_in_store;
957 ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &atomic_value_in_store)); 944 ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &atomic_value_in_store));
958 ASSERT_EQ(string_value, atomic_value_in_store); 945 ASSERT_EQ(string_value, atomic_value_in_store);
959 ASSERT_EQ(string_value, stored_atomic_value.first); 946 ASSERT_EQ(string_value, stored_atomic_value.first);
960 947
961 MockPrefHashStore::ValuePtrStrategyPair stored_split_value = 948 MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
962 mock_pref_hash_store_->stored_value(kSplitPref); 949 mock_pref_hash_store_->stored_value(kSplitPref);
963 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, 950 ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_split_value.second);
964 stored_split_value.second);
965 const base::Value* split_value_in_store; 951 const base::Value* split_value_in_store;
966 ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store)); 952 ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store));
967 ASSERT_EQ(dict_value, split_value_in_store); 953 ASSERT_EQ(dict_value, split_value_in_store);
968 ASSERT_EQ(dict_value, stored_split_value.first); 954 ASSERT_EQ(dict_value, stored_split_value.first);
969 955
970 VerifyRecordedReset(false); 956 VerifyRecordedReset(false);
971 } 957 }
972 958
973 TEST_P(PrefHashFilterTest, DontResetReportOnly) { 959 TEST_P(PrefHashFilterTest, DontResetReportOnly) {
974 // Ownership of these values is transfered to |pref_store_contents_|. 960 // Ownership of these values is transfered to |pref_store_contents_|.
(...skipping 25 matching lines...) Expand all
1000 // All prefs should be checked and a new hash should be stored for each tested 986 // All prefs should be checked and a new hash should be stored for each tested
1001 // pref. 987 // pref.
1002 ASSERT_EQ(arraysize(kTestTrackedPrefs), 988 ASSERT_EQ(arraysize(kTestTrackedPrefs),
1003 mock_pref_hash_store_->checked_paths_count()); 989 mock_pref_hash_store_->checked_paths_count());
1004 ASSERT_EQ(4u, mock_pref_hash_store_->stored_paths_count()); 990 ASSERT_EQ(4u, mock_pref_hash_store_->stored_paths_count());
1005 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed()); 991 ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
1006 992
1007 // Delegate saw all prefs, four of which had the expected value_state. 993 // Delegate saw all prefs, four of which had the expected value_state.
1008 ASSERT_EQ(arraysize(kTestTrackedPrefs), 994 ASSERT_EQ(arraysize(kTestTrackedPrefs),
1009 mock_validation_delegate_.recorded_validations_count()); 995 mock_validation_delegate_.recorded_validations_count());
1010 ASSERT_EQ(4u, 996 ASSERT_EQ(4u, mock_validation_delegate_.CountValidationsOfState(
1011 mock_validation_delegate_.CountValidationsOfState( 997 PrefHashStoreTransaction::CHANGED));
1012 PrefHashStoreTransaction::CHANGED));
1013 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 4u, 998 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 4u,
1014 mock_validation_delegate_.CountValidationsOfState( 999 mock_validation_delegate_.CountValidationsOfState(
1015 PrefHashStoreTransaction::UNCHANGED)); 1000 PrefHashStoreTransaction::UNCHANGED));
1016 1001
1017 // No matter what the enforcement level is, the report only pref should never 1002 // No matter what the enforcement level is, the report only pref should never
1018 // be reset. 1003 // be reset.
1019 ASSERT_TRUE(pref_store_contents_->Get(kReportOnlyPref, NULL)); 1004 ASSERT_TRUE(pref_store_contents_->Get(kReportOnlyPref, NULL));
1020 ASSERT_TRUE(pref_store_contents_->Get(kReportOnlySplitPref, NULL)); 1005 ASSERT_TRUE(pref_store_contents_->Get(kReportOnlySplitPref, NULL));
1021 ASSERT_EQ(report_only_val, 1006 ASSERT_EQ(report_only_val,
1022 mock_pref_hash_store_->stored_value(kReportOnlyPref).first); 1007 mock_pref_hash_store_->stored_value(kReportOnlyPref).first);
(...skipping 17 matching lines...) Expand all
1040 ASSERT_EQ(int_value1, 1025 ASSERT_EQ(int_value1,
1041 mock_pref_hash_store_->stored_value(kAtomicPref).first); 1026 mock_pref_hash_store_->stored_value(kAtomicPref).first);
1042 ASSERT_EQ(int_value2, value_in_store2); 1027 ASSERT_EQ(int_value2, value_in_store2);
1043 ASSERT_EQ(int_value2, 1028 ASSERT_EQ(int_value2,
1044 mock_pref_hash_store_->stored_value(kAtomicPref2).first); 1029 mock_pref_hash_store_->stored_value(kAtomicPref2).first);
1045 1030
1046 VerifyRecordedReset(false); 1031 VerifyRecordedReset(false);
1047 } 1032 }
1048 } 1033 }
1049 1034
1050 INSTANTIATE_TEST_CASE_P( 1035 INSTANTIATE_TEST_CASE_P(PrefHashFilterTestInstance,
1051 PrefHashFilterTestInstance, PrefHashFilterTest, 1036 PrefHashFilterTest,
1052 testing::Values(PrefHashFilter::NO_ENFORCEMENT, 1037 testing::Values(PrefHashFilter::NO_ENFORCEMENT,
1053 PrefHashFilter::ENFORCE_ON_LOAD)); 1038 PrefHashFilter::ENFORCE_ON_LOAD));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698