Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/prefs/tracked/protected_pref_store.cc" | |
|
erikwright (departed)
2014/03/26 21:08:12
Ha. Can't believe I wrote it, not too surprised y'
| |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/prefs/persistent_pref_store.h" | |
| 15 #include "base/prefs/pref_store_observer_mock.h" | |
| 16 #include "base/prefs/testing_pref_store.h" | |
| 17 #include "base/values.h" | |
| 18 #include "chrome/browser/prefs/tracked/protected_pref_store.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 const char kProtectedPref[] = "protected_pref"; | |
| 25 const char kUnprotectedPref[] = "unprotected_pref"; | |
| 26 | |
| 27 const char kValue1[] = "value1"; | |
| 28 const char kValue2[] = "value2"; | |
| 29 | |
| 30 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | |
| 31 public: | |
| 32 struct Data { | |
| 33 bool invoked; | |
| 34 PersistentPrefStore::PrefReadError read_error; | |
| 35 }; | |
| 36 | |
| 37 explicit MockReadErrorDelegate(Data* data) : data_(data) { | |
| 38 DCHECK(data_); | |
| 39 EXPECT_FALSE(data_->invoked); | |
| 40 } | |
| 41 | |
| 42 // PersistentPrefStore::ReadErrorDelegate implementation | |
| 43 virtual void OnError(PersistentPrefStore::PrefReadError read_error) OVERRIDE { | |
| 44 EXPECT_FALSE(data_->invoked); | |
| 45 data_->invoked = true; | |
| 46 data_->read_error = read_error; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 Data* data_; | |
| 51 }; | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 class ProtectedPrefStoreTest : public testing::Test { | |
| 56 public: | |
| 57 ProtectedPrefStoreTest() | |
| 58 : initialization_callback_invoked_(false), | |
| 59 read_error_delegate_data_( | |
| 60 {false, PersistentPrefStore::PREF_READ_ERROR_NONE}), | |
| 61 read_error_delegate_( | |
| 62 new MockReadErrorDelegate(&read_error_delegate_data_)) {} | |
| 63 | |
| 64 virtual void SetUp() OVERRIDE { | |
| 65 protected_store_ = new TestingPrefStore; | |
| 66 unprotected_store_ = new TestingPrefStore; | |
| 67 | |
| 68 std::set<std::string> protected_pref_names; | |
| 69 protected_pref_names.insert(kProtectedPref); | |
| 70 | |
| 71 combined_store_ = new ProtectedPrefStore( | |
| 72 unprotected_store_, | |
| 73 protected_store_, | |
| 74 protected_pref_names, | |
| 75 base::Bind(&ProtectedPrefStoreTest::InitializationCallback, | |
| 76 base::Unretained(this))); | |
| 77 | |
| 78 combined_store_->AddObserver(&observer_); | |
| 79 } | |
| 80 | |
| 81 virtual void TearDown() OVERRIDE { | |
| 82 combined_store_->RemoveObserver(&observer_); | |
| 83 } | |
| 84 | |
| 85 protected: | |
| 86 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> GetReadErrorDelegate() { | |
| 87 EXPECT_TRUE(read_error_delegate_); | |
| 88 return read_error_delegate_ | |
| 89 .PassAs<PersistentPrefStore::ReadErrorDelegate>(); | |
| 90 } | |
| 91 | |
| 92 PrefStoreObserverMock observer_; | |
| 93 bool initialization_callback_invoked_; | |
| 94 | |
| 95 scoped_refptr<TestingPrefStore> unprotected_store_; | |
| 96 scoped_refptr<TestingPrefStore> protected_store_; | |
| 97 scoped_refptr<ProtectedPrefStore> combined_store_; | |
| 98 | |
| 99 MockReadErrorDelegate::Data read_error_delegate_data_; | |
| 100 | |
| 101 private: | |
| 102 void InitializationCallback() { | |
| 103 EXPECT_FALSE(observer_.initialized); | |
| 104 EXPECT_FALSE(initialization_callback_invoked_); | |
| 105 initialization_callback_invoked_ = true; | |
| 106 } | |
| 107 | |
| 108 scoped_ptr<MockReadErrorDelegate> read_error_delegate_; | |
| 109 }; | |
| 110 | |
| 111 TEST_F(ProtectedPrefStoreTest, StoreValues) { | |
| 112 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | |
| 113 combined_store_->ReadPrefs()); | |
| 114 | |
| 115 // Properly stores new values. | |
| 116 combined_store_->SetValue(kProtectedPref, new base::StringValue(kValue1)); | |
| 117 combined_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue2)); | |
| 118 | |
| 119 ASSERT_TRUE(protected_store_->GetValue(kProtectedPref, NULL)); | |
| 120 ASSERT_FALSE(protected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 121 ASSERT_FALSE(unprotected_store_->GetValue(kProtectedPref, NULL)); | |
| 122 ASSERT_TRUE(unprotected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 123 | |
| 124 ASSERT_TRUE(combined_store_->GetValue(kProtectedPref, NULL)); | |
| 125 ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL)); | |
| 126 | |
| 127 ASSERT_FALSE(protected_store_->committed()); | |
| 128 ASSERT_FALSE(unprotected_store_->committed()); | |
| 129 | |
| 130 combined_store_->CommitPendingWrite(); | |
| 131 | |
| 132 ASSERT_TRUE(protected_store_->committed()); | |
| 133 ASSERT_TRUE(unprotected_store_->committed()); | |
| 134 } | |
| 135 | |
| 136 TEST_F(ProtectedPrefStoreTest, ReadValues) { | |
| 137 protected_store_->SetValue(kProtectedPref, new base::StringValue(kValue1)); | |
| 138 unprotected_store_->SetValue(kUnprotectedPref, | |
| 139 new base::StringValue(kValue2)); | |
| 140 | |
| 141 // Works properly with values that are already there. | |
| 142 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | |
| 143 combined_store_->ReadPrefs()); | |
| 144 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | |
| 145 combined_store_->GetReadError()); | |
| 146 | |
| 147 ASSERT_TRUE(protected_store_->GetValue(kProtectedPref, NULL)); | |
| 148 ASSERT_FALSE(protected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 149 ASSERT_FALSE(unprotected_store_->GetValue(kProtectedPref, NULL)); | |
| 150 ASSERT_TRUE(unprotected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 151 | |
| 152 ASSERT_TRUE(combined_store_->GetValue(kProtectedPref, NULL)); | |
| 153 ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL)); | |
| 154 } | |
| 155 | |
| 156 TEST_F(ProtectedPrefStoreTest, PreviouslyProtected) { | |
| 157 protected_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue1)); | |
| 158 combined_store_->ReadPrefs(); | |
| 159 // It will read from the protected store. | |
| 160 ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL)); | |
| 161 ASSERT_TRUE(protected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 162 ASSERT_FALSE(unprotected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 163 | |
| 164 // But when we update the value... | |
| 165 combined_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue2)); | |
| 166 // ...it will be migrated. | |
| 167 ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL)); | |
| 168 ASSERT_FALSE(protected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 169 ASSERT_TRUE(unprotected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 170 } | |
| 171 | |
| 172 TEST_F(ProtectedPrefStoreTest, Observer) { | |
| 173 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | |
| 174 combined_store_->ReadPrefs()); | |
| 175 EXPECT_TRUE(initialization_callback_invoked_); | |
| 176 EXPECT_TRUE(observer_.initialized); | |
| 177 EXPECT_TRUE(observer_.initialization_success); | |
| 178 EXPECT_TRUE(observer_.changed_keys.empty()); | |
| 179 combined_store_->SetValue(kProtectedPref, new base::StringValue(kValue1)); | |
| 180 EXPECT_THAT(observer_.changed_keys, testing::ElementsAre(kProtectedPref)); | |
| 181 observer_.changed_keys.clear(); | |
| 182 combined_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue2)); | |
| 183 EXPECT_THAT(observer_.changed_keys, testing::ElementsAre(kUnprotectedPref)); | |
| 184 } | |
| 185 | |
| 186 TEST_F(ProtectedPrefStoreTest, ProtectedPrefReadError) { | |
| 187 protected_store_->set_read_error( | |
| 188 PersistentPrefStore::PREF_READ_ERROR_NO_FILE); | |
| 189 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 190 combined_store_->ReadPrefs()); | |
| 191 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 192 combined_store_->GetReadError()); | |
| 193 } | |
| 194 | |
| 195 TEST_F(ProtectedPrefStoreTest, UnprotectedPrefReadError) { | |
| 196 unprotected_store_->set_read_error( | |
| 197 PersistentPrefStore::PREF_READ_ERROR_NO_FILE); | |
| 198 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 199 combined_store_->ReadPrefs()); | |
| 200 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 201 combined_store_->GetReadError()); | |
| 202 } | |
| 203 | |
| 204 TEST_F(ProtectedPrefStoreTest, BothPrefReadError) { | |
| 205 unprotected_store_->set_read_error( | |
| 206 PersistentPrefStore::PREF_READ_ERROR_NO_FILE); | |
| 207 protected_store_->set_read_error( | |
| 208 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED); | |
| 209 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 210 combined_store_->ReadPrefs()); | |
| 211 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 212 combined_store_->GetReadError()); | |
| 213 } | |
| 214 | |
| 215 TEST_F(ProtectedPrefStoreTest, BothPrefReadErrorAsync) { | |
| 216 unprotected_store_->set_read_error( | |
| 217 PersistentPrefStore::PREF_READ_ERROR_NO_FILE); | |
| 218 protected_store_->set_read_error( | |
| 219 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED); | |
| 220 | |
| 221 protected_store_->SetBlockAsyncRead(true); | |
| 222 | |
| 223 EXPECT_FALSE(read_error_delegate_data_.invoked); | |
| 224 | |
| 225 combined_store_->ReadPrefsAsync(GetReadErrorDelegate().release()); | |
| 226 | |
| 227 EXPECT_FALSE(read_error_delegate_data_.invoked); | |
| 228 | |
| 229 protected_store_->SetBlockAsyncRead(false); | |
| 230 | |
| 231 EXPECT_TRUE(read_error_delegate_data_.invoked); | |
| 232 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 233 combined_store_->GetReadError()); | |
| 234 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 235 combined_store_->GetReadError()); | |
| 236 } | |
| 237 | |
| 238 TEST_F(ProtectedPrefStoreTest, IsInitializationComplete) { | |
| 239 EXPECT_FALSE(combined_store_->IsInitializationComplete()); | |
| 240 combined_store_->ReadPrefs(); | |
| 241 EXPECT_TRUE(combined_store_->IsInitializationComplete()); | |
| 242 } | |
| 243 | |
| 244 TEST_F(ProtectedPrefStoreTest, IsInitializationCompleteAsync) { | |
| 245 protected_store_->SetBlockAsyncRead(true); | |
| 246 unprotected_store_->SetBlockAsyncRead(true); | |
| 247 EXPECT_FALSE(combined_store_->IsInitializationComplete()); | |
| 248 combined_store_->ReadPrefsAsync(NULL); | |
| 249 EXPECT_FALSE(combined_store_->IsInitializationComplete()); | |
| 250 protected_store_->SetBlockAsyncRead(false); | |
| 251 EXPECT_FALSE(combined_store_->IsInitializationComplete()); | |
| 252 unprotected_store_->SetBlockAsyncRead(false); | |
| 253 EXPECT_TRUE(combined_store_->IsInitializationComplete()); | |
| 254 } | |
| OLD | NEW |