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