OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/safe_browsing/incident_reporting/platform_state_store.h
" |
| 6 |
| 7 #include "base/prefs/pref_notifier_impl.h" |
| 8 #include "base/prefs/testing_pref_store.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/test/test_reg_util_win.h" |
| 11 #include "base/test/test_simple_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/win/registry.h" |
| 14 #include "chrome/browser/prefs/browser_prefs.h" |
| 15 #include "chrome/test/base/testing_browser_process.h" |
| 16 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "chrome/test/base/testing_profile_manager.h" |
| 19 #include "components/pref_registry/pref_registry_syncable.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace safe_browsing { |
| 23 namespace platform_state_store { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const char kTestData_[] = "comme un poisson"; |
| 28 const DWORD kTestDataSize_ = sizeof(kTestData_) - 1; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 class PlatformStateStoreWinTest : public ::testing::Test { |
| 33 protected: |
| 34 PlatformStateStoreWinTest() |
| 35 : profile_(nullptr), |
| 36 task_runner_(new base::TestSimpleTaskRunner()), |
| 37 thread_task_runner_handle_(task_runner_), |
| 38 profile_manager_(TestingBrowserProcess::GetGlobal()) {} |
| 39 |
| 40 void SetUp() override { |
| 41 ::testing::Test::SetUp(); |
| 42 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER); |
| 43 ASSERT_TRUE(profile_manager_.SetUp()); |
| 44 } |
| 45 |
| 46 // Creates/resets |profile_|. If |new_profile| is true, the profile will |
| 47 // believe that it is new (Profile::IsNewProfile() will return true). |
| 48 void ResetProfile(bool new_profile) { |
| 49 if (profile_) { |
| 50 profile_manager_.DeleteTestingProfile(kProfileName_); |
| 51 profile_ = nullptr; |
| 52 } |
| 53 // Create a profile with a user PrefStore that can be manipulated. |
| 54 TestingPrefStore* user_pref_store = new TestingPrefStore(); |
| 55 // Profile::IsNewProfile() returns true/false on the basis of the pref |
| 56 // store's read_error property. A profile is considered "New" if it didn't |
| 57 // have a user prefs file. |
| 58 user_pref_store->set_read_error( |
| 59 new_profile ? PersistentPrefStore::PREF_READ_ERROR_NO_FILE |
| 60 : PersistentPrefStore::PREF_READ_ERROR_NONE); |
| 61 // Ownership of |user_pref_store| is passed to the service. |
| 62 scoped_ptr<TestingPrefServiceSyncable> prefs(new TestingPrefServiceSyncable( |
| 63 new TestingPrefStore(), user_pref_store, new TestingPrefStore(), |
| 64 new user_prefs::PrefRegistrySyncable(), new PrefNotifierImpl())); |
| 65 chrome::RegisterUserProfilePrefs(prefs->registry()); |
| 66 profile_ = profile_manager_.CreateTestingProfile( |
| 67 kProfileName_, prefs.Pass(), base::UTF8ToUTF16(kProfileName_), 0, |
| 68 std::string(), TestingProfile::TestingFactories()); |
| 69 if (new_profile) |
| 70 ASSERT_TRUE(profile_->IsNewProfile()); |
| 71 else |
| 72 ASSERT_FALSE(profile_->IsNewProfile()); |
| 73 } |
| 74 |
| 75 void WriteTestData() { |
| 76 base::win::RegKey key; |
| 77 ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, kStoreKeyName_, |
| 78 KEY_SET_VALUE | KEY_WOW64_32KEY)); |
| 79 ASSERT_EQ(ERROR_SUCCESS, |
| 80 key.WriteValue(base::UTF8ToUTF16(kProfileName_).c_str(), |
| 81 &kTestData_[0], kTestDataSize_, REG_BINARY)); |
| 82 } |
| 83 |
| 84 void AssertTestDataIsAbsent() { |
| 85 base::win::RegKey key; |
| 86 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kStoreKeyName_, |
| 87 KEY_QUERY_VALUE | KEY_WOW64_32KEY)); |
| 88 ASSERT_FALSE(key.HasValue(base::UTF8ToUTF16(kProfileName_).c_str())); |
| 89 } |
| 90 |
| 91 void AssertTestDataIsPresent() { |
| 92 char buffer[kTestDataSize_] = {}; |
| 93 base::win::RegKey key; |
| 94 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kStoreKeyName_, |
| 95 KEY_QUERY_VALUE | KEY_WOW64_32KEY)); |
| 96 DWORD data_size = kTestDataSize_; |
| 97 DWORD data_type = REG_NONE; |
| 98 ASSERT_EQ(ERROR_SUCCESS, |
| 99 key.ReadValue(base::UTF8ToUTF16(kProfileName_).c_str(), |
| 100 &buffer[0], &data_size, &data_type)); |
| 101 EXPECT_EQ(REG_BINARY, data_type); |
| 102 ASSERT_EQ(kTestDataSize_, data_size); |
| 103 EXPECT_EQ(std::string(&buffer[0], data_size), |
| 104 std::string(&kTestData_[0], kTestDataSize_)); |
| 105 } |
| 106 |
| 107 static const char kProfileName_[]; |
| 108 static const base::char16 kStoreKeyName_[]; |
| 109 TestingProfile* profile_; |
| 110 |
| 111 private: |
| 112 registry_util::RegistryOverrideManager registry_override_manager_; |
| 113 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 114 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
| 115 TestingProfileManager profile_manager_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(PlatformStateStoreWinTest); |
| 118 }; |
| 119 |
| 120 // static |
| 121 const char PlatformStateStoreWinTest::kProfileName_[] = "test_profile"; |
| 122 #if defined(GOOGLE_CHROME_BUILD) |
| 123 const base::char16 PlatformStateStoreWinTest::kStoreKeyName_[] = |
| 124 L"Software\\Google\\Chrome\\IncidentsSent"; |
| 125 #else |
| 126 const base::char16 PlatformStateStoreWinTest::kStoreKeyName_[] = |
| 127 L"Software\\Chromium\\IncidentsSent"; |
| 128 #endif |
| 129 |
| 130 // Tests that store data is written correctly to the proper location. |
| 131 TEST_F(PlatformStateStoreWinTest, WriteStoreData) { |
| 132 ResetProfile(false /* !new_profile */); |
| 133 |
| 134 ASSERT_FALSE(base::win::RegKey(HKEY_CURRENT_USER, kStoreKeyName_, |
| 135 KEY_QUERY_VALUE | KEY_WOW64_32KEY) |
| 136 .HasValue(base::UTF8ToUTF16(kProfileName_).c_str())); |
| 137 WriteStoreData(profile_, kTestData_); |
| 138 AssertTestDataIsPresent(); |
| 139 } |
| 140 |
| 141 // Tests that store data is read from the proper location. |
| 142 TEST_F(PlatformStateStoreWinTest, ReadStoreData) { |
| 143 // Put some data in the registry. |
| 144 WriteTestData(); |
| 145 |
| 146 ResetProfile(false /* !new_profile */); |
| 147 std::string data; |
| 148 PlatformStateStoreLoadResult result = ReadStoreData(profile_, &data); |
| 149 EXPECT_EQ(PlatformStateStoreLoadResult::SUCCESS, result); |
| 150 EXPECT_EQ(std::string(&kTestData_[0], kTestDataSize_), data); |
| 151 } |
| 152 |
| 153 // Tests that an empty write clears the stored data. |
| 154 TEST_F(PlatformStateStoreWinTest, WriteEmptyStoreData) { |
| 155 // Put some data in the registry. |
| 156 WriteTestData(); |
| 157 |
| 158 ResetProfile(false /* !new_profile */); |
| 159 |
| 160 WriteStoreData(profile_, std::string()); |
| 161 AssertTestDataIsAbsent(); |
| 162 } |
| 163 |
| 164 // Tests that data in the registry is ignored if the profile is new. |
| 165 TEST_F(PlatformStateStoreWinTest, ReadNewProfileClearData) { |
| 166 ResetProfile(true /* new_profile */); |
| 167 |
| 168 // Put some data in the registry. |
| 169 WriteTestData(); |
| 170 |
| 171 std::string data; |
| 172 PlatformStateStoreLoadResult result = ReadStoreData(profile_, &data); |
| 173 EXPECT_EQ(PlatformStateStoreLoadResult::CLEARED_DATA, result); |
| 174 EXPECT_EQ(std::string(), data); |
| 175 AssertTestDataIsAbsent(); |
| 176 } |
| 177 |
| 178 } // namespace platform_state_store |
| 179 } // namespace safe_browsing |
OLD | NEW |