| 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 "services/preferences/tracked/pref_hash_filter.h" | 5 #include "services/preferences/tracked/pref_hash_filter.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/callback_forward.h" | 17 #include "base/callback_forward.h" |
| 18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/macros.h" | 20 #include "base/macros.h" |
| 21 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 22 #include "base/message_loop/message_loop.h" | |
| 23 #include "base/metrics/histogram_base.h" | 22 #include "base/metrics/histogram_base.h" |
| 24 #include "base/metrics/histogram_samples.h" | 23 #include "base/metrics/histogram_samples.h" |
| 25 #include "base/metrics/statistics_recorder.h" | 24 #include "base/metrics/statistics_recorder.h" |
| 26 #include "base/run_loop.h" | |
| 27 #include "base/values.h" | 25 #include "base/values.h" |
| 28 #include "components/prefs/testing_pref_store.h" | 26 #include "components/prefs/testing_pref_store.h" |
| 29 #include "mojo/public/cpp/bindings/binding_set.h" | |
| 30 #include "services/preferences/public/cpp/tracked/configuration.h" | 27 #include "services/preferences/public/cpp/tracked/configuration.h" |
| 31 #include "services/preferences/public/cpp/tracked/mock_validation_delegate.h" | 28 #include "services/preferences/public/cpp/tracked/mock_validation_delegate.h" |
| 32 #include "services/preferences/public/cpp/tracked/pref_names.h" | 29 #include "services/preferences/public/cpp/tracked/pref_names.h" |
| 33 #include "services/preferences/tracked/hash_store_contents.h" | 30 #include "services/preferences/tracked/hash_store_contents.h" |
| 34 #include "services/preferences/tracked/pref_hash_store.h" | 31 #include "services/preferences/tracked/pref_hash_store.h" |
| 35 #include "services/preferences/tracked/pref_hash_store_transaction.h" | 32 #include "services/preferences/tracked/pref_hash_store_transaction.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 37 | 34 |
| 38 namespace { | 35 namespace { |
| 39 | 36 |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 | 534 |
| 538 std::string MockHashStoreContents::GetSuperMac() const { | 535 std::string MockHashStoreContents::GetSuperMac() const { |
| 539 ADD_FAILURE() << "Unexpected call."; | 536 ADD_FAILURE() << "Unexpected call."; |
| 540 return std::string(); | 537 return std::string(); |
| 541 } | 538 } |
| 542 | 539 |
| 543 void MockHashStoreContents::SetSuperMac(const std::string& super_mac) { | 540 void MockHashStoreContents::SetSuperMac(const std::string& super_mac) { |
| 544 ADD_FAILURE() << "Unexpected call."; | 541 ADD_FAILURE() << "Unexpected call."; |
| 545 } | 542 } |
| 546 | 543 |
| 547 class PrefHashFilterTest : public testing::TestWithParam<EnforcementLevel>, | 544 class PrefHashFilterTest : public testing::TestWithParam<EnforcementLevel> { |
| 548 public prefs::mojom::ResetOnLoadObserver { | |
| 549 public: | 545 public: |
| 550 PrefHashFilterTest() | 546 PrefHashFilterTest() |
| 551 : mock_pref_hash_store_(NULL), | 547 : mock_pref_hash_store_(NULL), |
| 552 pref_store_contents_(new base::DictionaryValue), | 548 pref_store_contents_(new base::DictionaryValue), |
| 553 mock_validation_delegate_record_(new MockValidationDelegateRecord), | 549 mock_validation_delegate_record_(new MockValidationDelegateRecord), |
| 554 mock_validation_delegate_(mock_validation_delegate_record_), | 550 mock_validation_delegate_(mock_validation_delegate_record_), |
| 555 reset_recorded_(false) {} | 551 reset_recorded_(false) {} |
| 556 | 552 |
| 557 void SetUp() override { | 553 void SetUp() override { |
| 558 base::StatisticsRecorder::Initialize(); | 554 base::StatisticsRecorder::Initialize(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 582 mock_external_validation_pref_hash_store_ = | 578 mock_external_validation_pref_hash_store_ = |
| 583 temp_mock_external_validation_pref_hash_store.get(); | 579 temp_mock_external_validation_pref_hash_store.get(); |
| 584 mock_external_validation_hash_store_contents_ = | 580 mock_external_validation_hash_store_contents_ = |
| 585 temp_mock_external_validation_hash_store_contents.get(); | 581 temp_mock_external_validation_hash_store_contents.get(); |
| 586 pref_hash_filter_.reset(new PrefHashFilter( | 582 pref_hash_filter_.reset(new PrefHashFilter( |
| 587 std::move(temp_mock_pref_hash_store), | 583 std::move(temp_mock_pref_hash_store), |
| 588 PrefHashFilter::StoreContentsPair( | 584 PrefHashFilter::StoreContentsPair( |
| 589 std::move(temp_mock_external_validation_pref_hash_store), | 585 std::move(temp_mock_external_validation_pref_hash_store), |
| 590 std::move(temp_mock_external_validation_hash_store_contents)), | 586 std::move(temp_mock_external_validation_hash_store_contents)), |
| 591 std::move(configuration), | 587 std::move(configuration), |
| 592 reset_on_load_observer_bindings_.CreateInterfacePtrAndBind(this), | 588 base::Bind(&PrefHashFilterTest::RecordReset, base::Unretained(this)), |
| 593 &mock_validation_delegate_, arraysize(kTestTrackedPrefs), true)); | 589 &mock_validation_delegate_, arraysize(kTestTrackedPrefs), true)); |
| 594 } | 590 } |
| 595 | 591 |
| 596 // Verifies whether a reset was reported by the PrefHashFiler. Also verifies | 592 // Verifies whether a reset was reported by the PrefHashFiler. Also verifies |
| 597 // that kPreferenceResetTime was set (or not) accordingly. | 593 // that kPreferenceResetTime was set (or not) accordingly. |
| 598 void VerifyRecordedReset(bool reset_expected) { | 594 void VerifyRecordedReset(bool reset_expected) { |
| 599 base::RunLoop().RunUntilIdle(); | |
| 600 EXPECT_EQ(reset_expected, reset_recorded_); | 595 EXPECT_EQ(reset_expected, reset_recorded_); |
| 601 EXPECT_EQ(reset_expected, pref_store_contents_->Get( | 596 EXPECT_EQ(reset_expected, pref_store_contents_->Get( |
| 602 user_prefs::kPreferenceResetTime, NULL)); | 597 user_prefs::kPreferenceResetTime, NULL)); |
| 603 } | 598 } |
| 604 | 599 |
| 605 // Calls FilterOnLoad() on |pref_hash_Filter_|. |pref_store_contents_| is | 600 // Calls FilterOnLoad() on |pref_hash_Filter_|. |pref_store_contents_| is |
| 606 // handed off, but should be given back to us synchronously through | 601 // handed off, but should be given back to us synchronously through |
| 607 // GetPrefsBack() as there is no FilterOnLoadInterceptor installed on | 602 // GetPrefsBack() as there is no FilterOnLoadInterceptor installed on |
| 608 // |pref_hash_filter_|. | 603 // |pref_hash_filter_|. |
| 609 void DoFilterOnLoad(bool expect_prefs_modifications) { | 604 void DoFilterOnLoad(bool expect_prefs_modifications) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 624 // Stores |prefs| back in |pref_store_contents| and ensure | 619 // Stores |prefs| back in |pref_store_contents| and ensure |
| 625 // |expected_schedule_write| matches the reported |schedule_write|. | 620 // |expected_schedule_write| matches the reported |schedule_write|. |
| 626 void GetPrefsBack(bool expected_schedule_write, | 621 void GetPrefsBack(bool expected_schedule_write, |
| 627 std::unique_ptr<base::DictionaryValue> prefs, | 622 std::unique_ptr<base::DictionaryValue> prefs, |
| 628 bool schedule_write) { | 623 bool schedule_write) { |
| 629 pref_store_contents_ = std::move(prefs); | 624 pref_store_contents_ = std::move(prefs); |
| 630 EXPECT_TRUE(pref_store_contents_); | 625 EXPECT_TRUE(pref_store_contents_); |
| 631 EXPECT_EQ(expected_schedule_write, schedule_write); | 626 EXPECT_EQ(expected_schedule_write, schedule_write); |
| 632 } | 627 } |
| 633 | 628 |
| 634 void OnResetOnLoad() override { | 629 void RecordReset() { |
| 635 // As-is |reset_recorded_| is only designed to remember a single reset, make | 630 // As-is |reset_recorded_| is only designed to remember a single reset, make |
| 636 // sure none was previously recorded. | 631 // sure none was previously recorded. |
| 637 EXPECT_FALSE(reset_recorded_); | 632 EXPECT_FALSE(reset_recorded_); |
| 638 reset_recorded_ = true; | 633 reset_recorded_ = true; |
| 639 } | 634 } |
| 640 | 635 |
| 641 base::MessageLoop message_loop_; | |
| 642 MockValidationDelegate mock_validation_delegate_; | 636 MockValidationDelegate mock_validation_delegate_; |
| 643 mojo::BindingSet<prefs::mojom::ResetOnLoadObserver> | |
| 644 reset_on_load_observer_bindings_; | |
| 645 bool reset_recorded_; | 637 bool reset_recorded_; |
| 646 | 638 |
| 647 DISALLOW_COPY_AND_ASSIGN(PrefHashFilterTest); | 639 DISALLOW_COPY_AND_ASSIGN(PrefHashFilterTest); |
| 648 }; | 640 }; |
| 649 | 641 |
| 650 TEST_P(PrefHashFilterTest, EmptyAndUnchanged) { | 642 TEST_P(PrefHashFilterTest, EmptyAndUnchanged) { |
| 651 DoFilterOnLoad(false); | 643 DoFilterOnLoad(false); |
| 652 // All paths checked. | 644 // All paths checked. |
| 653 ASSERT_EQ(arraysize(kTestTrackedPrefs), | 645 ASSERT_EQ(arraysize(kTestTrackedPrefs), |
| 654 mock_pref_hash_store_->checked_paths_count()); | 646 mock_pref_hash_store_->checked_paths_count()); |
| (...skipping 691 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1346 ValueState::CHANGED)); | 1338 ValueState::CHANGED)); |
| 1347 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u, | 1339 ASSERT_EQ(arraysize(kTestTrackedPrefs) - 2u, |
| 1348 mock_validation_delegate_record_->CountExternalValidationsOfState( | 1340 mock_validation_delegate_record_->CountExternalValidationsOfState( |
| 1349 ValueState::UNCHANGED)); | 1341 ValueState::UNCHANGED)); |
| 1350 } | 1342 } |
| 1351 | 1343 |
| 1352 INSTANTIATE_TEST_CASE_P(PrefHashFilterTestInstance, | 1344 INSTANTIATE_TEST_CASE_P(PrefHashFilterTestInstance, |
| 1353 PrefHashFilterTest, | 1345 PrefHashFilterTest, |
| 1354 testing::Values(EnforcementLevel::NO_ENFORCEMENT, | 1346 testing::Values(EnforcementLevel::NO_ENFORCEMENT, |
| 1355 EnforcementLevel::ENFORCE_ON_LOAD)); | 1347 EnforcementLevel::ENFORCE_ON_LOAD)); |
| OLD | NEW |