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 user_pref_store->set_read_error( |
| 56 new_profile ? PersistentPrefStore::PREF_READ_ERROR_NO_FILE |
| 57 : PersistentPrefStore::PREF_READ_ERROR_NONE); |
| 58 // Ownership of |user_pref_store| is passed to the service. |
| 59 scoped_ptr<TestingPrefServiceSyncable> prefs(new TestingPrefServiceSyncable( |
| 60 new TestingPrefStore(), user_pref_store, new TestingPrefStore(), |
| 61 new user_prefs::PrefRegistrySyncable(), new PrefNotifierImpl())); |
| 62 chrome::RegisterUserProfilePrefs(prefs->registry()); |
| 63 profile_ = profile_manager_.CreateTestingProfile( |
| 64 kProfileName_, prefs.Pass(), base::UTF8ToUTF16(kProfileName_), 0, |
| 65 std::string(), TestingProfile::TestingFactories()); |
| 66 } |
| 67 |
| 68 void WriteTestData() { |
| 69 base::win::RegKey key; |
| 70 ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, kStoreKeyName_, |
| 71 KEY_SET_VALUE | KEY_WOW64_32KEY)); |
| 72 ASSERT_EQ(ERROR_SUCCESS, |
| 73 key.WriteValue(base::UTF8ToUTF16(kProfileName_).c_str(), |
| 74 &kTestData_[0], kTestDataSize_, REG_BINARY)); |
| 75 } |
| 76 |
| 77 void AssertTestDataIsAbsent() { |
| 78 base::win::RegKey key; |
| 79 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kStoreKeyName_, |
| 80 KEY_QUERY_VALUE | KEY_WOW64_32KEY)); |
| 81 ASSERT_FALSE(key.HasValue(base::UTF8ToUTF16(kProfileName_).c_str())); |
| 82 } |
| 83 |
| 84 void AssertTestDataIsPresent() { |
| 85 char buffer[kTestDataSize_] = {}; |
| 86 base::win::RegKey key; |
| 87 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kStoreKeyName_, |
| 88 KEY_QUERY_VALUE | KEY_WOW64_32KEY)); |
| 89 DWORD data_size = kTestDataSize_; |
| 90 DWORD data_type = REG_NONE; |
| 91 ASSERT_EQ(ERROR_SUCCESS, |
| 92 key.ReadValue(base::UTF8ToUTF16(kProfileName_).c_str(), |
| 93 &buffer[0], &data_size, &data_type)); |
| 94 EXPECT_EQ(REG_BINARY, data_type); |
| 95 ASSERT_EQ(kTestDataSize_, data_size); |
| 96 EXPECT_EQ(std::string(&buffer[0], data_size), |
| 97 std::string(&kTestData_[0], kTestDataSize_)); |
| 98 } |
| 99 |
| 100 static const char kProfileName_[]; |
| 101 static const base::char16 kStoreKeyName_[]; |
| 102 TestingProfile* profile_; |
| 103 |
| 104 private: |
| 105 registry_util::RegistryOverrideManager registry_override_manager_; |
| 106 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 107 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
| 108 TestingProfileManager profile_manager_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(PlatformStateStoreWinTest); |
| 111 }; |
| 112 |
| 113 // static |
| 114 const char PlatformStateStoreWinTest::kProfileName_[] = "test_profile"; |
| 115 #if defined(GOOGLE_CHROME_BUILD) |
| 116 const base::char16 PlatformStateStoreWinTest::kStoreKeyName_[] = |
| 117 L"Software\\Google\\Chrome\\IncidentsSent"; |
| 118 #else |
| 119 const base::char16 PlatformStateStoreWinTest::kStoreKeyName_[] = |
| 120 L"Software\\Chromium\\IncidentsSent"; |
| 121 #endif |
| 122 |
| 123 TEST_F(PlatformStateStoreWinTest, WriteStoreData) { |
| 124 ResetProfile(false); |
| 125 |
| 126 ASSERT_FALSE(base::win::RegKey(HKEY_CURRENT_USER, kStoreKeyName_, |
| 127 KEY_QUERY_VALUE | KEY_WOW64_32KEY) |
| 128 .HasValue(base::UTF8ToUTF16(kProfileName_).c_str())); |
| 129 WriteStoreData(profile_, kTestData_); |
| 130 AssertTestDataIsPresent(); |
| 131 } |
| 132 |
| 133 TEST_F(PlatformStateStoreWinTest, ReadNewProfileClearData) { |
| 134 ResetProfile(true); |
| 135 ASSERT_TRUE(profile_->IsNewProfile()); |
| 136 |
| 137 // Put some data in the registry. |
| 138 WriteTestData(); |
| 139 |
| 140 std::string data; |
| 141 PlatformStateStoreLoadResult result = ReadStoreData(profile_, &data); |
| 142 EXPECT_EQ(PlatformStateStoreLoadResult::CLEARED_DATA, result); |
| 143 EXPECT_EQ(std::string(), data); |
| 144 AssertTestDataIsAbsent(); |
| 145 } |
| 146 |
| 147 } // namespace platform_state_store |
| 148 } // namespace safe_browsing |
OLD | NEW |