123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350 |
- // Copyright 2013 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "services/preferences/tracked/pref_hash_filter.h"
- #include <stddef.h>
- #include <map>
- #include <memory>
- #include <set>
- #include <string>
- #include <utility>
- #include <vector>
- #include "base/bind.h"
- #include "base/compiler_specific.h"
- #include "base/memory/raw_ptr.h"
- #include "base/metrics/histogram_base.h"
- #include "base/metrics/histogram_samples.h"
- #include "base/metrics/statistics_recorder.h"
- #include "base/run_loop.h"
- #include "base/test/task_environment.h"
- #include "base/values.h"
- #include "components/prefs/testing_pref_store.h"
- #include "mojo/public/cpp/bindings/receiver.h"
- #include "mojo/public/cpp/bindings/receiver_set.h"
- #include "services/preferences/public/cpp/tracked/configuration.h"
- #include "services/preferences/public/cpp/tracked/mock_validation_delegate.h"
- #include "services/preferences/public/cpp/tracked/pref_names.h"
- #include "services/preferences/tracked/hash_store_contents.h"
- #include "services/preferences/tracked/pref_hash_store.h"
- #include "services/preferences/tracked/pref_hash_store_transaction.h"
- #include "testing/gtest/include/gtest/gtest.h"
- namespace {
- using EnforcementLevel =
- prefs::mojom::TrackedPreferenceMetadata::EnforcementLevel;
- using PrefTrackingStrategy =
- prefs::mojom::TrackedPreferenceMetadata::PrefTrackingStrategy;
- using ValueType = prefs::mojom::TrackedPreferenceMetadata::ValueType;
- using ValueState =
- prefs::mojom::TrackedPreferenceValidationDelegate::ValueState;
- const char kAtomicPref[] = "atomic_pref";
- const char kAtomicPref2[] = "atomic_pref2";
- const char kAtomicPref3[] = "pref3";
- const char kAtomicPref4[] = "pref4";
- const char kReportOnlyPref[] = "report_only";
- const char kReportOnlySplitPref[] = "report_only_split_pref";
- const char kSplitPref[] = "split_pref";
- const prefs::TrackedPreferenceMetadata kTestTrackedPrefs[] = {
- {0, kAtomicPref, EnforcementLevel::ENFORCE_ON_LOAD,
- PrefTrackingStrategy::ATOMIC, ValueType::PERSONAL},
- {1, kReportOnlyPref, EnforcementLevel::NO_ENFORCEMENT,
- PrefTrackingStrategy::ATOMIC, ValueType::IMPERSONAL},
- {2, kSplitPref, EnforcementLevel::ENFORCE_ON_LOAD,
- PrefTrackingStrategy::SPLIT, ValueType::IMPERSONAL},
- {3, kReportOnlySplitPref, EnforcementLevel::NO_ENFORCEMENT,
- PrefTrackingStrategy::SPLIT, ValueType::IMPERSONAL},
- {4, kAtomicPref2, EnforcementLevel::ENFORCE_ON_LOAD,
- PrefTrackingStrategy::ATOMIC, ValueType::IMPERSONAL},
- {5, kAtomicPref3, EnforcementLevel::ENFORCE_ON_LOAD,
- PrefTrackingStrategy::ATOMIC, ValueType::IMPERSONAL},
- {6, kAtomicPref4, EnforcementLevel::ENFORCE_ON_LOAD,
- PrefTrackingStrategy::ATOMIC, ValueType::IMPERSONAL},
- };
- } // namespace
- // A PrefHashStore that allows simulation of CheckValue results and captures
- // checked and stored values.
- class MockPrefHashStore : public PrefHashStore {
- public:
- typedef std::pair<const void*, PrefTrackingStrategy> ValuePtrStrategyPair;
- MockPrefHashStore()
- : stamp_super_mac_result_(false),
- is_super_mac_valid_result_(false),
- transactions_performed_(0),
- transaction_active_(false) {}
- MockPrefHashStore(const MockPrefHashStore&) = delete;
- MockPrefHashStore& operator=(const MockPrefHashStore&) = delete;
- ~MockPrefHashStore() override { EXPECT_FALSE(transaction_active_); }
- // Set the result that will be returned when |path| is passed to
- // |CheckValue/CheckSplitValue|.
- void SetCheckResult(const std::string& path, ValueState result);
- // Set the invalid_keys that will be returned when |path| is passed to
- // |CheckSplitValue|. SetCheckResult should already have been called for
- // |path| with |result == CHANGED| for this to make any sense.
- void SetInvalidKeysResult(
- const std::string& path,
- const std::vector<std::string>& invalid_keys_result);
- // Sets the value that will be returned from
- // PrefHashStoreTransaction::StampSuperMAC().
- void set_stamp_super_mac_result(bool result) {
- stamp_super_mac_result_ = result;
- }
- // Sets the value that will be returned from
- // PrefHashStoreTransaction::IsSuperMACValid().
- void set_is_super_mac_valid_result(bool result) {
- is_super_mac_valid_result_ = result;
- }
- // Returns the number of transactions that were performed.
- size_t transactions_performed() { return transactions_performed_; }
- // Returns the number of paths checked.
- size_t checked_paths_count() const { return checked_values_.size(); }
- // Returns the number of paths stored.
- size_t stored_paths_count() const { return stored_values_.size(); }
- // Returns the pointer value and strategy that was passed to
- // |CheckHash/CheckSplitHash| for |path|. The returned pointer could since
- // have been freed and is thus not safe to dereference.
- ValuePtrStrategyPair checked_value(const std::string& path) const {
- std::map<std::string, ValuePtrStrategyPair>::const_iterator value =
- checked_values_.find(path);
- if (value != checked_values_.end())
- return value->second;
- return std::make_pair(reinterpret_cast<void*>(0xBAD),
- static_cast<PrefTrackingStrategy>(-1));
- }
- // Returns the pointer value that was passed to |StoreHash/StoreSplitHash| for
- // |path|. The returned pointer could since have been freed and is thus not
- // safe to dereference.
- ValuePtrStrategyPair stored_value(const std::string& path) const {
- std::map<std::string, ValuePtrStrategyPair>::const_iterator value =
- stored_values_.find(path);
- if (value != stored_values_.end())
- return value->second;
- return std::make_pair(reinterpret_cast<void*>(0xBAD),
- static_cast<PrefTrackingStrategy>(-1));
- }
- // PrefHashStore implementation.
- std::unique_ptr<PrefHashStoreTransaction> BeginTransaction(
- HashStoreContents* storage) override;
- std::string ComputeMac(const std::string& path,
- const base::Value* new_value) override;
- std::unique_ptr<base::DictionaryValue> ComputeSplitMacs(
- const std::string& path,
- const base::DictionaryValue* split_values) override;
- private:
- // A MockPrefHashStoreTransaction is handed to the caller on
- // MockPrefHashStore::BeginTransaction(). It then stores state in its
- // underlying MockPrefHashStore about calls it receives from that same caller
- // which can later be verified in tests.
- class MockPrefHashStoreTransaction : public PrefHashStoreTransaction {
- public:
- explicit MockPrefHashStoreTransaction(MockPrefHashStore* outer)
- : outer_(outer) {}
- MockPrefHashStoreTransaction(const MockPrefHashStoreTransaction&) = delete;
- MockPrefHashStoreTransaction& operator=(
- const MockPrefHashStoreTransaction&) = delete;
- ~MockPrefHashStoreTransaction() override {
- outer_->transaction_active_ = false;
- ++outer_->transactions_performed_;
- }
- // PrefHashStoreTransaction implementation.
- base::StringPiece GetStoreUMASuffix() const override;
- ValueState CheckValue(const std::string& path,
- const base::Value* value) const override;
- void StoreHash(const std::string& path,
- const base::Value* new_value) override;
- ValueState CheckSplitValue(
- const std::string& path,
- const base::DictionaryValue* initial_split_value,
- std::vector<std::string>* invalid_keys) const override;
- void StoreSplitHash(const std::string& path,
- const base::DictionaryValue* split_value) override;
- bool HasHash(const std::string& path) const override;
- void ImportHash(const std::string& path, const base::Value* hash) override;
- void ClearHash(const std::string& path) override;
- bool IsSuperMACValid() const override;
- bool StampSuperMac() override;
- private:
- raw_ptr<MockPrefHashStore> outer_;
- };
- // Records a call to this mock's CheckValue/CheckSplitValue methods.
- ValueState RecordCheckValue(const std::string& path,
- const base::Value* value,
- PrefTrackingStrategy strategy);
- // Records a call to this mock's StoreHash/StoreSplitHash methods.
- void RecordStoreHash(const std::string& path,
- const base::Value* new_value,
- PrefTrackingStrategy strategy);
- std::map<std::string, ValueState> check_results_;
- std::map<std::string, std::vector<std::string>> invalid_keys_results_;
- bool stamp_super_mac_result_;
- bool is_super_mac_valid_result_;
- std::map<std::string, ValuePtrStrategyPair> checked_values_;
- std::map<std::string, ValuePtrStrategyPair> stored_values_;
- // Number of transactions that were performed via this MockPrefHashStore.
- size_t transactions_performed_;
- // Whether a transaction is currently active (only one transaction should be
- // active at a time).
- bool transaction_active_;
- };
- void MockPrefHashStore::SetCheckResult(const std::string& path,
- ValueState result) {
- check_results_.insert(std::make_pair(path, result));
- }
- void MockPrefHashStore::SetInvalidKeysResult(
- const std::string& path,
- const std::vector<std::string>& invalid_keys_result) {
- // Ensure |check_results_| has a CHANGED entry for |path|.
- std::map<std::string, ValueState>::const_iterator result =
- check_results_.find(path);
- ASSERT_TRUE(result != check_results_.end());
- ASSERT_EQ(ValueState::CHANGED, result->second);
- invalid_keys_results_.insert(std::make_pair(path, invalid_keys_result));
- }
- std::unique_ptr<PrefHashStoreTransaction> MockPrefHashStore::BeginTransaction(
- HashStoreContents* storage) {
- EXPECT_FALSE(transaction_active_);
- return std::unique_ptr<PrefHashStoreTransaction>(
- new MockPrefHashStoreTransaction(this));
- }
- std::string MockPrefHashStore::ComputeMac(const std::string& path,
- const base::Value* new_value) {
- return "atomic mac for: " + path;
- }
- std::unique_ptr<base::DictionaryValue> MockPrefHashStore::ComputeSplitMacs(
- const std::string& path,
- const base::DictionaryValue* split_values) {
- std::unique_ptr<base::DictionaryValue> macs_dict(new base::DictionaryValue);
- if (!split_values)
- return macs_dict;
- for (base::DictionaryValue::Iterator it(*split_values); !it.IsAtEnd();
- it.Advance()) {
- macs_dict->SetKey(it.key(),
- base::Value("split mac for: " + path + "/" + it.key()));
- }
- return macs_dict;
- }
- ValueState MockPrefHashStore::RecordCheckValue(const std::string& path,
- const base::Value* value,
- PrefTrackingStrategy strategy) {
- // Record that |path| was checked and validate that it wasn't previously
- // checked.
- EXPECT_TRUE(checked_values_
- .insert(std::make_pair(path, std::make_pair(value, strategy)))
- .second);
- std::map<std::string, ValueState>::const_iterator result =
- check_results_.find(path);
- if (result != check_results_.end())
- return result->second;
- return ValueState::UNCHANGED;
- }
- void MockPrefHashStore::RecordStoreHash(const std::string& path,
- const base::Value* new_value,
- PrefTrackingStrategy strategy) {
- EXPECT_TRUE(
- stored_values_
- .insert(std::make_pair(path, std::make_pair(new_value, strategy)))
- .second);
- }
- base::StringPiece
- MockPrefHashStore::MockPrefHashStoreTransaction ::GetStoreUMASuffix() const {
- return "unused";
- }
- ValueState MockPrefHashStore::MockPrefHashStoreTransaction::CheckValue(
- const std::string& path,
- const base::Value* value) const {
- return outer_->RecordCheckValue(path, value, PrefTrackingStrategy::ATOMIC);
- }
- void MockPrefHashStore::MockPrefHashStoreTransaction::StoreHash(
- const std::string& path,
- const base::Value* new_value) {
- outer_->RecordStoreHash(path, new_value, PrefTrackingStrategy::ATOMIC);
- }
- ValueState MockPrefHashStore::MockPrefHashStoreTransaction::CheckSplitValue(
- const std::string& path,
- const base::DictionaryValue* initial_split_value,
- std::vector<std::string>* invalid_keys) const {
- EXPECT_TRUE(invalid_keys && invalid_keys->empty());
- std::map<std::string, std::vector<std::string>>::const_iterator
- invalid_keys_result = outer_->invalid_keys_results_.find(path);
- if (invalid_keys_result != outer_->invalid_keys_results_.end()) {
- invalid_keys->insert(invalid_keys->begin(),
- invalid_keys_result->second.begin(),
- invalid_keys_result->second.end());
- }
- return outer_->RecordCheckValue(path, initial_split_value,
- PrefTrackingStrategy::SPLIT);
- }
- void MockPrefHashStore::MockPrefHashStoreTransaction::StoreSplitHash(
- const std::string& path,
- const base::DictionaryValue* new_value) {
- outer_->RecordStoreHash(path, new_value, PrefTrackingStrategy::SPLIT);
- }
- bool MockPrefHashStore::MockPrefHashStoreTransaction::HasHash(
- const std::string& path) const {
- ADD_FAILURE() << "Unexpected call.";
- return false;
- }
- void MockPrefHashStore::MockPrefHashStoreTransaction::ImportHash(
- const std::string& path,
- const base::Value* hash) {
- ADD_FAILURE() << "Unexpected call.";
- }
- void MockPrefHashStore::MockPrefHashStoreTransaction::ClearHash(
- const std::string& path) {
- // Allow this to be called by PrefHashFilter's deprecated tracked prefs
- // cleanup tasks.
- }
- bool MockPrefHashStore::MockPrefHashStoreTransaction::IsSuperMACValid() const {
- return outer_->is_super_mac_valid_result_;
- }
- bool MockPrefHashStore::MockPrefHashStoreTransaction::StampSuperMac() {
- return outer_->stamp_super_mac_result_;
- }
- std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> GetConfiguration(
- EnforcementLevel max_enforcement_level) {
- auto configuration = prefs::ConstructTrackedConfiguration(kTestTrackedPrefs);
- for (const auto& metadata : configuration) {
- if (metadata->enforcement_level > max_enforcement_level)
- metadata->enforcement_level = max_enforcement_level;
- }
- return configuration;
- }
- class MockHashStoreContents : public HashStoreContents {
- public:
- MockHashStoreContents() {}
- MockHashStoreContents(const MockHashStoreContents&) = delete;
- MockHashStoreContents& operator=(const MockHashStoreContents&) = delete;
- // Returns the number of hashes stored.
- size_t stored_hashes_count() const { return dictionary_.DictSize(); }
- // Returns the number of paths cleared.
- size_t cleared_paths_count() const { return removed_entries_.size(); }
- // Returns the stored MAC for an Atomic preference.
- std::string GetStoredMac(const std::string& path) const;
- // Returns the stored MAC for a Split preference.
- std::string GetStoredSplitMac(const std::string& path,
- const std::string& split_path) const;
- // HashStoreContents implementation.
- bool IsCopyable() const override;
- std::unique_ptr<HashStoreContents> MakeCopy() const override;
- base::StringPiece GetUMASuffix() const override;
- void Reset() override;
- bool GetMac(const std::string& path, std::string* out_value) override;
- bool GetSplitMacs(const std::string& path,
- std::map<std::string, std::string>* split_macs) override;
- void SetMac(const std::string& path, const std::string& value) override;
- void SetSplitMac(const std::string& path,
- const std::string& split_path,
- const std::string& value) override;
- void ImportEntry(const std::string& path,
- const base::Value* in_value) override;
- bool RemoveEntry(const std::string& path) override;
- const base::DictionaryValue* GetContents() const override;
- std::string GetSuperMac() const override;
- void SetSuperMac(const std::string& super_mac) override;
- private:
- MockHashStoreContents(MockHashStoreContents* origin_mock);
- // Records calls to this mock's SetMac/SetSplitMac methods.
- void RecordSetMac(const std::string& path, const std::string& mac) {
- dictionary_.SetKey(path, base::Value(mac));
- }
- void RecordSetSplitMac(const std::string& path,
- const std::string& split_path,
- const std::string& mac) {
- dictionary_.SetPath({path, split_path}, base::Value(mac));
- }
- // Records a call to this mock's RemoveEntry method.
- void RecordRemoveEntry(const std::string& path) {
- // Don't expect the same pref to be cleared more than once.
- EXPECT_EQ(removed_entries_.end(), removed_entries_.find(path));
- removed_entries_.insert(path);
- }
- base::DictionaryValue dictionary_;
- std::set<std::string> removed_entries_;
- // The code being tested copies its HashStoreContents for use in a callback
- // which can be executed during shutdown. To be able to capture the behavior
- // of the copy, we make it forward calls to the mock it was created from.
- // Once set, |origin_mock_| must outlive this instance.
- raw_ptr<MockHashStoreContents> origin_mock_;
- };
- std::string MockHashStoreContents::GetStoredMac(const std::string& path) const {
- const base::Value* out_value = dictionary_.FindKey(path);
- if (out_value) {
- EXPECT_TRUE(out_value->is_string());
- return out_value->GetString();
- }
- return std::string();
- }
- std::string MockHashStoreContents::GetStoredSplitMac(
- const std::string& path,
- const std::string& split_path) const {
- const base::Value* out_value = dictionary_.FindKey(path);
- if (out_value) {
- const base::DictionaryValue* value_as_dict;
- EXPECT_TRUE(out_value->GetAsDictionary(&value_as_dict));
- out_value = dictionary_.FindKey(split_path);
- if (out_value) {
- EXPECT_TRUE(out_value->is_string());
- return out_value->GetString();
- }
- }
- return std::string();
- }
- MockHashStoreContents::MockHashStoreContents(MockHashStoreContents* origin_mock)
- : origin_mock_(origin_mock) {}
- bool MockHashStoreContents::IsCopyable() const {
- return true;
- }
- std::unique_ptr<HashStoreContents> MockHashStoreContents::MakeCopy() const {
- // Return a new MockHashStoreContents which forwards all requests to this
- // mock instance.
- return std::unique_ptr<HashStoreContents>(
- new MockHashStoreContents(const_cast<MockHashStoreContents*>(this)));
- }
- base::StringPiece MockHashStoreContents::GetUMASuffix() const {
- return "Unused";
- }
- void MockHashStoreContents::Reset() {
- ADD_FAILURE() << "Unexpected call.";
- }
- bool MockHashStoreContents::GetMac(const std::string& path,
- std::string* out_value) {
- ADD_FAILURE() << "Unexpected call.";
- return false;
- }
- bool MockHashStoreContents::GetSplitMacs(
- const std::string& path,
- std::map<std::string, std::string>* split_macs) {
- ADD_FAILURE() << "Unexpected call.";
- return false;
- }
- void MockHashStoreContents::SetMac(const std::string& path,
- const std::string& value) {
- if (origin_mock_)
- origin_mock_->RecordSetMac(path, value);
- else
- RecordSetMac(path, value);
- }
- void MockHashStoreContents::SetSplitMac(const std::string& path,
- const std::string& split_path,
- const std::string& value) {
- if (origin_mock_)
- origin_mock_->RecordSetSplitMac(path, split_path, value);
- else
- RecordSetSplitMac(path, split_path, value);
- }
- void MockHashStoreContents::ImportEntry(const std::string& path,
- const base::Value* in_value) {
- ADD_FAILURE() << "Unexpected call.";
- }
- bool MockHashStoreContents::RemoveEntry(const std::string& path) {
- if (origin_mock_)
- origin_mock_->RecordRemoveEntry(path);
- else
- RecordRemoveEntry(path);
- return true;
- }
- const base::DictionaryValue* MockHashStoreContents::GetContents() const {
- ADD_FAILURE() << "Unexpected call.";
- return nullptr;
- }
- std::string MockHashStoreContents::GetSuperMac() const {
- ADD_FAILURE() << "Unexpected call.";
- return std::string();
- }
- void MockHashStoreContents::SetSuperMac(const std::string& super_mac) {
- ADD_FAILURE() << "Unexpected call.";
- }
- class PrefHashFilterTest : public testing::TestWithParam<EnforcementLevel>,
- public prefs::mojom::ResetOnLoadObserver {
- public:
- PrefHashFilterTest()
- : mock_pref_hash_store_(nullptr),
- pref_store_contents_(new base::DictionaryValue),
- mock_validation_delegate_record_(new MockValidationDelegateRecord),
- mock_validation_delegate_(mock_validation_delegate_record_),
- validation_delegate_receiver_(&mock_validation_delegate_),
- reset_recorded_(false) {}
- PrefHashFilterTest(const PrefHashFilterTest&) = delete;
- PrefHashFilterTest& operator=(const PrefHashFilterTest&) = delete;
- void SetUp() override {
- Reset();
- }
- protected:
- // Reset the PrefHashFilter instance.
- void Reset() {
- // Construct a PrefHashFilter and MockPrefHashStore for the test.
- InitializePrefHashFilter(GetConfiguration(GetParam()));
- }
- // Initializes |pref_hash_filter_| with a PrefHashFilter that uses a
- // MockPrefHashStore. The raw pointer to the MockPrefHashStore (owned by the
- // PrefHashFilter) is stored in |mock_pref_hash_store_|.
- void InitializePrefHashFilter(
- std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> configuration) {
- std::unique_ptr<MockPrefHashStore> temp_mock_pref_hash_store(
- new MockPrefHashStore);
- std::unique_ptr<MockPrefHashStore>
- temp_mock_external_validation_pref_hash_store(new MockPrefHashStore);
- std::unique_ptr<MockHashStoreContents>
- temp_mock_external_validation_hash_store_contents(
- new MockHashStoreContents);
- mock_pref_hash_store_ = temp_mock_pref_hash_store.get();
- mock_external_validation_pref_hash_store_ =
- temp_mock_external_validation_pref_hash_store.get();
- mock_external_validation_hash_store_contents_ =
- temp_mock_external_validation_hash_store_contents.get();
- mojo::PendingRemote<prefs::mojom::ResetOnLoadObserver>
- reset_on_load_observer;
- reset_on_load_observer_receivers_.Add(
- this, reset_on_load_observer.InitWithNewPipeAndPassReceiver());
- mojo::Remote<prefs::mojom::TrackedPreferenceValidationDelegate>
- validation_delegate_remote(
- validation_delegate_receiver_.BindNewPipeAndPassRemote());
- auto validation_delegate_remote_ref =
- base::MakeRefCounted<base::RefCountedData<
- mojo::Remote<prefs::mojom::TrackedPreferenceValidationDelegate>>>(
- std::move(validation_delegate_remote));
- pref_hash_filter_ = std::make_unique<PrefHashFilter>(
- std::move(temp_mock_pref_hash_store),
- PrefHashFilter::StoreContentsPair(
- std::move(temp_mock_external_validation_pref_hash_store),
- std::move(temp_mock_external_validation_hash_store_contents)),
- std::move(configuration), std::move(reset_on_load_observer),
- std::move(validation_delegate_remote_ref),
- std::size(kTestTrackedPrefs));
- }
- // Verifies whether a reset was reported by the PrefHashFiler. Also verifies
- // that kPreferenceResetTime was set (or not) accordingly.
- void VerifyRecordedReset(bool reset_expected) {
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(reset_expected, reset_recorded_);
- EXPECT_EQ(reset_expected, pref_store_contents_->Get(
- user_prefs::kPreferenceResetTime, NULL));
- }
- // Calls FilterOnLoad() on |pref_hash_Filter_|. |pref_store_contents_| is
- // handed off, but should be given back to us synchronously through
- // GetPrefsBack() as there is no FilterOnLoadInterceptor installed on
- // |pref_hash_filter_|.
- void DoFilterOnLoad(bool expect_prefs_modifications) {
- pref_hash_filter_->FilterOnLoad(
- base::BindOnce(&PrefHashFilterTest::GetPrefsBack,
- base::Unretained(this), expect_prefs_modifications),
- std::move(pref_store_contents_));
- }
- raw_ptr<MockPrefHashStore> mock_pref_hash_store_;
- raw_ptr<MockPrefHashStore> mock_external_validation_pref_hash_store_;
- raw_ptr<MockHashStoreContents> mock_external_validation_hash_store_contents_;
- std::unique_ptr<base::DictionaryValue> pref_store_contents_;
- scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_;
- std::unique_ptr<PrefHashFilter> pref_hash_filter_;
- private:
- // Stores |prefs| back in |pref_store_contents| and ensure
- // |expected_schedule_write| matches the reported |schedule_write|.
- void GetPrefsBack(bool expected_schedule_write,
- std::unique_ptr<base::DictionaryValue> prefs,
- bool schedule_write) {
- pref_store_contents_ = std::move(prefs);
- EXPECT_TRUE(pref_store_contents_);
- EXPECT_EQ(expected_schedule_write, schedule_write);
- }
- void OnResetOnLoad() override {
- // As-is |reset_recorded_| is only designed to remember a single reset, make
- // sure none was previously recorded.
- EXPECT_FALSE(reset_recorded_);
- reset_recorded_ = true;
- }
- base::test::SingleThreadTaskEnvironment task_environment_;
- MockValidationDelegate mock_validation_delegate_;
- mojo::Receiver<prefs::mojom::TrackedPreferenceValidationDelegate>
- validation_delegate_receiver_;
- mojo::ReceiverSet<prefs::mojom::ResetOnLoadObserver>
- reset_on_load_observer_receivers_;
- bool reset_recorded_;
- };
- TEST_P(PrefHashFilterTest, EmptyAndUnchanged) {
- DoFilterOnLoad(false);
- // All paths checked.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_pref_hash_store_->checked_paths_count());
- // No paths stored, since they all return |UNCHANGED|.
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // Since there was nothing in |pref_store_contents_| the checked value should
- // have been NULL for all tracked preferences.
- for (size_t i = 0; i < std::size(kTestTrackedPrefs); ++i) {
- ASSERT_EQ(
- NULL,
- mock_pref_hash_store_->checked_value(kTestTrackedPrefs[i].name).first);
- }
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- VerifyRecordedReset(false);
- // Delegate saw all paths, and all unchanged.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->recorded_validations_count());
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNCHANGED));
- }
- TEST_P(PrefHashFilterTest, StampSuperMACAltersStore) {
- mock_pref_hash_store_->set_stamp_super_mac_result(true);
- DoFilterOnLoad(true);
- // No paths stored, since they all return |UNCHANGED|. The StampSuperMAC
- // result is the only reason the prefs were considered altered.
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- }
- TEST_P(PrefHashFilterTest, FilterTrackedPrefUpdate) {
- base::DictionaryValue root_dict;
- base::Value* string_value = root_dict.SetString(kAtomicPref, "string value");
- // No path should be stored on FilterUpdate.
- pref_hash_filter_->FilterUpdate(kAtomicPref);
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // One path should be stored on FilterSerializeData.
- pref_hash_filter_->FilterSerializeData(&root_dict);
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths_count());
- MockPrefHashStore::ValuePtrStrategyPair stored_value =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- ASSERT_EQ(string_value, stored_value.first);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_value.second);
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- VerifyRecordedReset(false);
- }
- TEST_P(PrefHashFilterTest, FilterTrackedPrefClearing) {
- base::DictionaryValue root_dict;
- // We don't actually add the pref's value to root_dict to simulate that
- // it was just cleared in the PrefStore.
- // No path should be stored on FilterUpdate.
- pref_hash_filter_->FilterUpdate(kAtomicPref);
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // One path should be stored on FilterSerializeData, with no value.
- pref_hash_filter_->FilterSerializeData(&root_dict);
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths_count());
- MockPrefHashStore::ValuePtrStrategyPair stored_value =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- ASSERT_EQ(nullptr, stored_value.first);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_value.second);
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- VerifyRecordedReset(false);
- }
- TEST_P(PrefHashFilterTest, FilterSplitPrefUpdate) {
- base::DictionaryValue root_dict;
- base::Value* dict_value =
- root_dict.SetKey(kSplitPref, base::Value(base::Value::Type::DICTIONARY));
- dict_value->SetStringKey("a", "foo");
- dict_value->SetIntKey("b", 1234);
- // No path should be stored on FilterUpdate.
- pref_hash_filter_->FilterUpdate(kSplitPref);
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // One path should be stored on FilterSerializeData.
- pref_hash_filter_->FilterSerializeData(&root_dict);
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths_count());
- MockPrefHashStore::ValuePtrStrategyPair stored_value =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(dict_value, stored_value.first);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_value.second);
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- VerifyRecordedReset(false);
- }
- TEST_P(PrefHashFilterTest, FilterTrackedSplitPrefClearing) {
- base::DictionaryValue root_dict;
- // We don't actually add the pref's value to root_dict to simulate that
- // it was just cleared in the PrefStore.
- // No path should be stored on FilterUpdate.
- pref_hash_filter_->FilterUpdate(kSplitPref);
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // One path should be stored on FilterSerializeData, with no value.
- pref_hash_filter_->FilterSerializeData(&root_dict);
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths_count());
- MockPrefHashStore::ValuePtrStrategyPair stored_value =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(nullptr, stored_value.first);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_value.second);
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- VerifyRecordedReset(false);
- }
- TEST_P(PrefHashFilterTest, FilterUntrackedPrefUpdate) {
- base::DictionaryValue root_dict;
- root_dict.SetString("untracked", "some value");
- pref_hash_filter_->FilterUpdate("untracked");
- // No paths should be stored on FilterUpdate.
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // Nor on FilterSerializeData.
- pref_hash_filter_->FilterSerializeData(&root_dict);
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // No transaction should even be started on FilterSerializeData() if there are
- // no updates to perform.
- ASSERT_EQ(0u, mock_pref_hash_store_->transactions_performed());
- }
- TEST_P(PrefHashFilterTest, MultiplePrefsFilterSerializeData) {
- base::DictionaryValue root_dict;
- base::Value* int_value1 = root_dict.GetDict().Set(kAtomicPref, 1);
- root_dict.GetDict().Set(kAtomicPref2, 2);
- root_dict.GetDict().Set(kAtomicPref3, 3);
- root_dict.GetDict().Set("untracked", 4);
- base::Value* dict_value =
- root_dict.SetKey(kSplitPref, base::Value(base::Value::Type::DICTIONARY));
- dict_value->SetBoolKey("a", true);
- // Only update kAtomicPref, kAtomicPref3, and kSplitPref.
- pref_hash_filter_->FilterUpdate(kAtomicPref);
- pref_hash_filter_->FilterUpdate(kAtomicPref3);
- pref_hash_filter_->FilterUpdate(kSplitPref);
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // Update kAtomicPref3 again, nothing should be stored still.
- base::Value* int_value5 = root_dict.GetDict().Set(kAtomicPref3, 5);
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
- // On FilterSerializeData, only kAtomicPref, kAtomicPref3, and kSplitPref
- // should get a new hash.
- pref_hash_filter_->FilterSerializeData(&root_dict);
- ASSERT_EQ(3u, mock_pref_hash_store_->stored_paths_count());
- MockPrefHashStore::ValuePtrStrategyPair stored_value_atomic1 =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- ASSERT_EQ(int_value1, stored_value_atomic1.first);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_value_atomic1.second);
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- MockPrefHashStore::ValuePtrStrategyPair stored_value_atomic3 =
- mock_pref_hash_store_->stored_value(kAtomicPref3);
- ASSERT_EQ(int_value5, stored_value_atomic3.first);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_value_atomic3.second);
- MockPrefHashStore::ValuePtrStrategyPair stored_value_split =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(dict_value, stored_value_split.first);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_value_split.second);
- }
- TEST_P(PrefHashFilterTest, UnknownNullValue) {
- ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL));
- // NULL values are always trusted by the PrefHashStore.
- mock_pref_hash_store_->SetCheckResult(kAtomicPref,
- ValueState::TRUSTED_NULL_VALUE);
- mock_pref_hash_store_->SetCheckResult(kSplitPref,
- ValueState::TRUSTED_NULL_VALUE);
- DoFilterOnLoad(false);
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_pref_hash_store_->checked_paths_count());
- ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- ASSERT_EQ(NULL, stored_atomic_value.first);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_atomic_value.second);
- MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(NULL, stored_split_value.first);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_split_value.second);
- // Delegate saw all prefs, two of which had the expected value_state.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->recorded_validations_count());
- ASSERT_EQ(2u, mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::TRUSTED_NULL_VALUE));
- ASSERT_EQ(std::size(kTestTrackedPrefs) - 2u,
- mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNCHANGED));
- const MockValidationDelegateRecord::ValidationEvent* validated_split_pref =
- mock_validation_delegate_record_->GetEventForPath(kSplitPref);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, validated_split_pref->strategy);
- ASSERT_FALSE(validated_split_pref->is_personal);
- const MockValidationDelegateRecord::ValidationEvent* validated_atomic_pref =
- mock_validation_delegate_record_->GetEventForPath(kAtomicPref);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, validated_atomic_pref->strategy);
- ASSERT_TRUE(validated_atomic_pref->is_personal);
- }
- TEST_P(PrefHashFilterTest, InitialValueUnknown) {
- base::Value* string_value =
- pref_store_contents_->SetString(kAtomicPref, "string value");
- base::Value* dict_value = pref_store_contents_->SetKey(
- kSplitPref, base::Value(base::Value::Type::DICTIONARY));
- dict_value->SetStringKey("a", "foo");
- dict_value->SetIntKey("b", 1234);
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, NULL));
- mock_pref_hash_store_->SetCheckResult(kAtomicPref,
- ValueState::UNTRUSTED_UNKNOWN_VALUE);
- mock_pref_hash_store_->SetCheckResult(kSplitPref,
- ValueState::UNTRUSTED_UNKNOWN_VALUE);
- // If we are enforcing, expect this to report changes.
- DoFilterOnLoad(GetParam() >= EnforcementLevel::ENFORCE_ON_LOAD);
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_pref_hash_store_->checked_paths_count());
- ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- // Delegate saw all prefs, two of which had the expected value_state.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->recorded_validations_count());
- ASSERT_EQ(2u, mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNTRUSTED_UNKNOWN_VALUE));
- ASSERT_EQ(std::size(kTestTrackedPrefs) - 2u,
- mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNCHANGED));
- MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_atomic_value.second);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_split_value.second);
- if (GetParam() == EnforcementLevel::ENFORCE_ON_LOAD) {
- // Ensure the prefs were cleared and the hashes for NULL were restored if
- // the current enforcement level denies seeding.
- ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_EQ(NULL, stored_atomic_value.first);
- ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL));
- ASSERT_EQ(NULL, stored_split_value.first);
- VerifyRecordedReset(true);
- } else {
- // Otherwise the values should have remained intact and the hashes should
- // have been updated to match them.
- const base::Value* atomic_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &atomic_value_in_store));
- ASSERT_EQ(string_value, atomic_value_in_store);
- ASSERT_EQ(string_value, stored_atomic_value.first);
- const base::Value* split_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store));
- ASSERT_EQ(dict_value, split_value_in_store);
- ASSERT_EQ(dict_value, stored_split_value.first);
- VerifyRecordedReset(false);
- }
- }
- TEST_P(PrefHashFilterTest, InitialValueTrustedUnknown) {
- base::Value* string_value =
- pref_store_contents_->SetString(kAtomicPref, "test");
- auto* dict_value = pref_store_contents_->SetKey(
- kSplitPref, base::Value(base::Value::Type::DICTIONARY));
- dict_value->SetStringKey("a", "foo");
- dict_value->SetIntKey("b", 1234);
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, NULL));
- mock_pref_hash_store_->SetCheckResult(kAtomicPref,
- ValueState::TRUSTED_UNKNOWN_VALUE);
- mock_pref_hash_store_->SetCheckResult(kSplitPref,
- ValueState::TRUSTED_UNKNOWN_VALUE);
- DoFilterOnLoad(false);
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_pref_hash_store_->checked_paths_count());
- ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- // Delegate saw all prefs, two of which had the expected value_state.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->recorded_validations_count());
- ASSERT_EQ(2u, mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::TRUSTED_UNKNOWN_VALUE));
- ASSERT_EQ(std::size(kTestTrackedPrefs) - 2u,
- mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNCHANGED));
- // Seeding is always allowed for trusted unknown values.
- const base::Value* atomic_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &atomic_value_in_store));
- ASSERT_EQ(string_value, atomic_value_in_store);
- MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- ASSERT_EQ(string_value, stored_atomic_value.first);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_atomic_value.second);
- const base::Value* split_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store));
- ASSERT_EQ(dict_value, split_value_in_store);
- MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(dict_value, stored_split_value.first);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_split_value.second);
- }
- TEST_P(PrefHashFilterTest, InitialValueChanged) {
- base::Value* int_value =
- pref_store_contents_->GetDict().Set(kAtomicPref, 1234);
- base::Value* dict_value = pref_store_contents_->SetKey(
- kSplitPref, base::Value(base::Value::Type::DICTIONARY));
- dict_value->SetStringKey("a", "foo");
- dict_value->SetIntKey("b", 1234);
- dict_value->SetIntKey("c", 56);
- dict_value->SetBoolKey("d", false);
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, NULL));
- mock_pref_hash_store_->SetCheckResult(kAtomicPref, ValueState::CHANGED);
- mock_pref_hash_store_->SetCheckResult(kSplitPref, ValueState::CHANGED);
- std::vector<std::string> mock_invalid_keys;
- mock_invalid_keys.push_back("a");
- mock_invalid_keys.push_back("c");
- mock_pref_hash_store_->SetInvalidKeysResult(kSplitPref, mock_invalid_keys);
- DoFilterOnLoad(GetParam() >= EnforcementLevel::ENFORCE_ON_LOAD);
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_pref_hash_store_->checked_paths_count());
- ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_atomic_value.second);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_split_value.second);
- if (GetParam() == EnforcementLevel::ENFORCE_ON_LOAD) {
- // Ensure the atomic pref was cleared and the hash for NULL was restored if
- // the current enforcement level prevents changes.
- ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_EQ(NULL, stored_atomic_value.first);
- // The split pref on the other hand should only have been stripped of its
- // invalid keys.
- const base::Value* split_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store));
- ASSERT_EQ(2U, dict_value->DictSize());
- ASSERT_EQ(dict_value->FindKey("a"), nullptr);
- ASSERT_NE(dict_value->FindKey("b"), nullptr);
- ASSERT_EQ(dict_value->FindKey("c"), nullptr);
- ASSERT_NE(dict_value->FindKey("d"), nullptr);
- ASSERT_EQ(dict_value, stored_split_value.first);
- VerifyRecordedReset(true);
- } else {
- // Otherwise the value should have remained intact and the hash should have
- // been updated to match it.
- const base::Value* atomic_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &atomic_value_in_store));
- ASSERT_EQ(int_value, atomic_value_in_store);
- ASSERT_EQ(int_value, stored_atomic_value.first);
- const base::Value* split_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store));
- ASSERT_EQ(dict_value, split_value_in_store);
- ASSERT_EQ(4U, dict_value->DictSize());
- ASSERT_NE(dict_value->FindKey("a"), nullptr);
- ASSERT_NE(dict_value->FindKey("b"), nullptr);
- ASSERT_NE(dict_value->FindKey("c"), nullptr);
- ASSERT_NE(dict_value->FindKey("d"), nullptr);
- ASSERT_EQ(dict_value, stored_split_value.first);
- VerifyRecordedReset(false);
- }
- }
- TEST_P(PrefHashFilterTest, EmptyCleared) {
- ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL));
- mock_pref_hash_store_->SetCheckResult(kAtomicPref, ValueState::CLEARED);
- mock_pref_hash_store_->SetCheckResult(kSplitPref, ValueState::CLEARED);
- DoFilterOnLoad(false);
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_pref_hash_store_->checked_paths_count());
- ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- // Delegate saw all prefs, two of which had the expected value_state.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->recorded_validations_count());
- ASSERT_EQ(2u, mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::CLEARED));
- ASSERT_EQ(std::size(kTestTrackedPrefs) - 2u,
- mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNCHANGED));
- // Regardless of the enforcement level, the only thing that should be done is
- // to restore the hash for NULL. The value itself should still be NULL.
- ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
- MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- ASSERT_EQ(NULL, stored_atomic_value.first);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_atomic_value.second);
- ASSERT_FALSE(pref_store_contents_->Get(kSplitPref, NULL));
- MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(NULL, stored_split_value.first);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_split_value.second);
- }
- TEST_P(PrefHashFilterTest, InitialValueUnchangedLegacyId) {
- base::Value* string_value =
- pref_store_contents_->SetString(kAtomicPref, "string value");
- base::Value* dict_value = pref_store_contents_->SetKey(
- kSplitPref, base::Value(base::Value::Type::DICTIONARY));
- dict_value->SetStringKey("a", "foo");
- dict_value->SetIntKey("b", 1234);
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, NULL));
- mock_pref_hash_store_->SetCheckResult(kAtomicPref, ValueState::SECURE_LEGACY);
- mock_pref_hash_store_->SetCheckResult(kSplitPref, ValueState::SECURE_LEGACY);
- DoFilterOnLoad(false);
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_pref_hash_store_->checked_paths_count());
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- // Delegate saw all prefs, two of which had the expected value_state.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->recorded_validations_count());
- ASSERT_EQ(2u, mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::SECURE_LEGACY));
- ASSERT_EQ(std::size(kTestTrackedPrefs) - 2u,
- mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNCHANGED));
- // Ensure that both the atomic and split hashes were restored.
- ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
- // In all cases, the values should have remained intact and the hashes should
- // have been updated to match them.
- MockPrefHashStore::ValuePtrStrategyPair stored_atomic_value =
- mock_pref_hash_store_->stored_value(kAtomicPref);
- ASSERT_EQ(PrefTrackingStrategy::ATOMIC, stored_atomic_value.second);
- const base::Value* atomic_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &atomic_value_in_store));
- ASSERT_EQ(string_value, atomic_value_in_store);
- ASSERT_EQ(string_value, stored_atomic_value.first);
- MockPrefHashStore::ValuePtrStrategyPair stored_split_value =
- mock_pref_hash_store_->stored_value(kSplitPref);
- ASSERT_EQ(PrefTrackingStrategy::SPLIT, stored_split_value.second);
- const base::Value* split_value_in_store;
- ASSERT_TRUE(pref_store_contents_->Get(kSplitPref, &split_value_in_store));
- ASSERT_EQ(dict_value, split_value_in_store);
- ASSERT_EQ(dict_value, stored_split_value.first);
- VerifyRecordedReset(false);
- }
- TEST_P(PrefHashFilterTest, DontResetReportOnly) {
- base::Value* int_value1 = pref_store_contents_->GetDict().Set(kAtomicPref, 1);
- base::Value* int_value2 =
- pref_store_contents_->GetDict().Set(kAtomicPref2, 2);
- base::Value* report_only_val =
- pref_store_contents_->GetDict().Set(kReportOnlyPref, 3);
- base::Value* report_only_split_val = pref_store_contents_->SetKey(
- kReportOnlySplitPref, base::Value(base::Value::Type::DICTIONARY));
- report_only_split_val->SetIntKey("a", 1234);
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref2, NULL));
- ASSERT_TRUE(pref_store_contents_->Get(kReportOnlyPref, NULL));
- ASSERT_TRUE(pref_store_contents_->Get(kReportOnlySplitPref, NULL));
- mock_pref_hash_store_->SetCheckResult(kAtomicPref, ValueState::CHANGED);
- mock_pref_hash_store_->SetCheckResult(kAtomicPref2, ValueState::CHANGED);
- mock_pref_hash_store_->SetCheckResult(kReportOnlyPref, ValueState::CHANGED);
- mock_pref_hash_store_->SetCheckResult(kReportOnlySplitPref,
- ValueState::CHANGED);
- DoFilterOnLoad(GetParam() >= EnforcementLevel::ENFORCE_ON_LOAD);
- // All prefs should be checked and a new hash should be stored for each tested
- // pref.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_pref_hash_store_->checked_paths_count());
- ASSERT_EQ(4u, mock_pref_hash_store_->stored_paths_count());
- ASSERT_EQ(1u, mock_pref_hash_store_->transactions_performed());
- // Delegate saw all prefs, four of which had the expected value_state.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->recorded_validations_count());
- ASSERT_EQ(4u, mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::CHANGED));
- ASSERT_EQ(std::size(kTestTrackedPrefs) - 4u,
- mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNCHANGED));
- // No matter what the enforcement level is, the report only pref should never
- // be reset.
- ASSERT_TRUE(pref_store_contents_->Get(kReportOnlyPref, NULL));
- ASSERT_TRUE(pref_store_contents_->Get(kReportOnlySplitPref, NULL));
- ASSERT_EQ(report_only_val,
- mock_pref_hash_store_->stored_value(kReportOnlyPref).first);
- ASSERT_EQ(report_only_split_val,
- mock_pref_hash_store_->stored_value(kReportOnlySplitPref).first);
- // All other prefs should have been reset if the enforcement level allows it.
- if (GetParam() == EnforcementLevel::ENFORCE_ON_LOAD) {
- ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref, NULL));
- ASSERT_FALSE(pref_store_contents_->Get(kAtomicPref2, NULL));
- ASSERT_EQ(NULL, mock_pref_hash_store_->stored_value(kAtomicPref).first);
- ASSERT_EQ(NULL, mock_pref_hash_store_->stored_value(kAtomicPref2).first);
- VerifyRecordedReset(true);
- } else {
- const base::Value* value_in_store;
- const base::Value* value_in_store2;
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref, &value_in_store));
- ASSERT_TRUE(pref_store_contents_->Get(kAtomicPref2, &value_in_store2));
- ASSERT_EQ(int_value1, value_in_store);
- ASSERT_EQ(int_value1,
- mock_pref_hash_store_->stored_value(kAtomicPref).first);
- ASSERT_EQ(int_value2, value_in_store2);
- ASSERT_EQ(int_value2,
- mock_pref_hash_store_->stored_value(kAtomicPref2).first);
- VerifyRecordedReset(false);
- }
- }
- TEST_P(PrefHashFilterTest, CallFilterSerializeDataCallbacks) {
- base::DictionaryValue root_dict;
- base::DictionaryValue dict_value;
- dict_value.SetBoolean("a", true);
- root_dict.GetDict().Set(kAtomicPref, 1);
- root_dict.GetDict().Set(kAtomicPref2, 2);
- root_dict.SetKey(kSplitPref, std::move(dict_value));
- // Skip updating kAtomicPref2.
- pref_hash_filter_->FilterUpdate(kAtomicPref);
- pref_hash_filter_->FilterUpdate(kSplitPref);
- PrefHashFilter::OnWriteCallbackPair callbacks =
- pref_hash_filter_->FilterSerializeData(&root_dict);
- ASSERT_FALSE(callbacks.first.is_null());
- // Prefs should be cleared from the external validation store only once the
- // before-write callback is run.
- ASSERT_EQ(
- 0u, mock_external_validation_hash_store_contents_->cleared_paths_count());
- std::move(callbacks.first).Run();
- ASSERT_EQ(
- 2u, mock_external_validation_hash_store_contents_->cleared_paths_count());
- // No pref write should occur before the after-write callback is run.
- ASSERT_EQ(
- 0u, mock_external_validation_hash_store_contents_->stored_hashes_count());
- std::move(callbacks.second).Run(true);
- ASSERT_EQ(
- 2u, mock_external_validation_hash_store_contents_->stored_hashes_count());
- ASSERT_EQ(
- "atomic mac for: atomic_pref",
- mock_external_validation_hash_store_contents_->GetStoredMac(kAtomicPref));
- ASSERT_EQ("split mac for: split_pref/a",
- mock_external_validation_hash_store_contents_->GetStoredSplitMac(
- kSplitPref, "a"));
- // The callbacks should write directly to the contents without going through
- // a pref hash store.
- ASSERT_EQ(0u,
- mock_external_validation_pref_hash_store_->stored_paths_count());
- }
- TEST_P(PrefHashFilterTest, CallFilterSerializeDataCallbacksWithFailure) {
- base::DictionaryValue root_dict;
- root_dict.GetDict().Set(kAtomicPref, 1);
- // Only update kAtomicPref.
- pref_hash_filter_->FilterUpdate(kAtomicPref);
- PrefHashFilter::OnWriteCallbackPair callbacks =
- pref_hash_filter_->FilterSerializeData(&root_dict);
- ASSERT_FALSE(callbacks.first.is_null());
- std::move(callbacks.first).Run();
- // The pref should have been cleared from the external validation store.
- ASSERT_EQ(
- 1u, mock_external_validation_hash_store_contents_->cleared_paths_count());
- std::move(callbacks.second).Run(false);
- // Expect no writes to the external validation hash store contents.
- ASSERT_EQ(0u,
- mock_external_validation_pref_hash_store_->stored_paths_count());
- ASSERT_EQ(
- 0u, mock_external_validation_hash_store_contents_->stored_hashes_count());
- }
- TEST_P(PrefHashFilterTest, ExternalValidationValueChanged) {
- pref_store_contents_->GetDict().Set(kAtomicPref, 1234);
- base::DictionaryValue dict_value;
- dict_value.SetString("a", "foo");
- dict_value.GetDict().Set("b", 1234);
- dict_value.GetDict().Set("c", 56);
- dict_value.SetBoolean("d", false);
- pref_store_contents_->SetKey(kSplitPref, std::move(dict_value));
- mock_external_validation_pref_hash_store_->SetCheckResult(
- kAtomicPref, ValueState::CHANGED);
- mock_external_validation_pref_hash_store_->SetCheckResult(
- kSplitPref, ValueState::CHANGED);
- std::vector<std::string> mock_invalid_keys;
- mock_invalid_keys.push_back("a");
- mock_invalid_keys.push_back("c");
- mock_external_validation_pref_hash_store_->SetInvalidKeysResult(
- kSplitPref, mock_invalid_keys);
- DoFilterOnLoad(false);
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_external_validation_pref_hash_store_->checked_paths_count());
- ASSERT_EQ(2u,
- mock_external_validation_pref_hash_store_->stored_paths_count());
- ASSERT_EQ(
- 1u, mock_external_validation_pref_hash_store_->transactions_performed());
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->recorded_validations_count());
- // Regular validation should not have any CHANGED prefs.
- ASSERT_EQ(std::size(kTestTrackedPrefs),
- mock_validation_delegate_record_->CountValidationsOfState(
- ValueState::UNCHANGED));
- // External validation should have two CHANGED prefs (kAtomic and kSplit).
- ASSERT_EQ(2u,
- mock_validation_delegate_record_->CountExternalValidationsOfState(
- ValueState::CHANGED));
- ASSERT_EQ(std::size(kTestTrackedPrefs) - 2u,
- mock_validation_delegate_record_->CountExternalValidationsOfState(
- ValueState::UNCHANGED));
- }
- INSTANTIATE_TEST_SUITE_P(PrefHashFilterTestInstance,
- PrefHashFilterTest,
- testing::Values(EnforcementLevel::NO_ENFORCEMENT,
- EnforcementLevel::ENFORCE_ON_LOAD));
|