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( | |
Cait (Slow)
2015/08/31 17:55:29
Is setting the read_error really the best way to t
grt (UTC plus 2)
2015/09/01 13:55:23
As far as I can tell, it's the only way. Comment a
| |
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 if (new_profile) | |
67 ASSERT_TRUE(profile_->IsNewProfile()); | |
68 else | |
69 ASSERT_FALSE(profile_->IsNewProfile()); | |
70 } | |
71 | |
72 void WriteTestData() { | |
73 base::win::RegKey key; | |
74 ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, kStoreKeyName_, | |
75 KEY_SET_VALUE | KEY_WOW64_32KEY)); | |
76 ASSERT_EQ(ERROR_SUCCESS, | |
77 key.WriteValue(base::UTF8ToUTF16(kProfileName_).c_str(), | |
78 &kTestData_[0], kTestDataSize_, REG_BINARY)); | |
79 } | |
80 | |
81 void AssertTestDataIsAbsent() { | |
82 base::win::RegKey key; | |
83 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kStoreKeyName_, | |
84 KEY_QUERY_VALUE | KEY_WOW64_32KEY)); | |
85 ASSERT_FALSE(key.HasValue(base::UTF8ToUTF16(kProfileName_).c_str())); | |
86 } | |
87 | |
88 void AssertTestDataIsPresent() { | |
89 char buffer[kTestDataSize_] = {}; | |
90 base::win::RegKey key; | |
91 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kStoreKeyName_, | |
92 KEY_QUERY_VALUE | KEY_WOW64_32KEY)); | |
93 DWORD data_size = kTestDataSize_; | |
94 DWORD data_type = REG_NONE; | |
95 ASSERT_EQ(ERROR_SUCCESS, | |
96 key.ReadValue(base::UTF8ToUTF16(kProfileName_).c_str(), | |
97 &buffer[0], &data_size, &data_type)); | |
98 EXPECT_EQ(REG_BINARY, data_type); | |
99 ASSERT_EQ(kTestDataSize_, data_size); | |
100 EXPECT_EQ(std::string(&buffer[0], data_size), | |
101 std::string(&kTestData_[0], kTestDataSize_)); | |
102 } | |
103 | |
104 static const char kProfileName_[]; | |
105 static const base::char16 kStoreKeyName_[]; | |
106 TestingProfile* profile_; | |
107 | |
108 private: | |
109 registry_util::RegistryOverrideManager registry_override_manager_; | |
110 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
111 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | |
112 TestingProfileManager profile_manager_; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(PlatformStateStoreWinTest); | |
115 }; | |
116 | |
117 // static | |
118 const char PlatformStateStoreWinTest::kProfileName_[] = "test_profile"; | |
119 #if defined(GOOGLE_CHROME_BUILD) | |
120 const base::char16 PlatformStateStoreWinTest::kStoreKeyName_[] = | |
121 L"Software\\Google\\Chrome\\IncidentsSent"; | |
122 #else | |
123 const base::char16 PlatformStateStoreWinTest::kStoreKeyName_[] = | |
124 L"Software\\Chromium\\IncidentsSent"; | |
125 #endif | |
126 | |
127 // Tests that store data is written correctly to the proper location. | |
128 TEST_F(PlatformStateStoreWinTest, WriteStoreData) { | |
129 ResetProfile(false /* !new_profile */); | |
130 | |
131 ASSERT_FALSE(base::win::RegKey(HKEY_CURRENT_USER, kStoreKeyName_, | |
132 KEY_QUERY_VALUE | KEY_WOW64_32KEY) | |
133 .HasValue(base::UTF8ToUTF16(kProfileName_).c_str())); | |
134 WriteStoreData(profile_, kTestData_); | |
135 AssertTestDataIsPresent(); | |
136 } | |
137 | |
138 // Tests that store data is read from the proper location. | |
139 TEST_F(PlatformStateStoreWinTest, ReadStoreData) { | |
140 // Put some data in the registry. | |
141 WriteTestData(); | |
142 | |
143 ResetProfile(false /* !new_profile */); | |
144 std::string data; | |
145 PlatformStateStoreLoadResult result = ReadStoreData(profile_, &data); | |
146 EXPECT_EQ(PlatformStateStoreLoadResult::SUCCESS, result); | |
147 EXPECT_EQ(std::string(&kTestData_[0], kTestDataSize_), data); | |
148 } | |
149 | |
150 // Tests that an empty write clears the stored data. | |
151 TEST_F(PlatformStateStoreWinTest, WriteEmptyStoreData) { | |
152 // Put some data in the registry. | |
153 WriteTestData(); | |
154 | |
155 ResetProfile(false /* !new_profile */); | |
156 | |
157 WriteStoreData(profile_, std::string()); | |
158 AssertTestDataIsAbsent(); | |
159 } | |
160 | |
161 // Tests that data in the registry is ignored if the profile is new. | |
162 TEST_F(PlatformStateStoreWinTest, ReadNewProfileClearData) { | |
163 ResetProfile(true /* new_profile */); | |
164 | |
165 // Put some data in the registry. | |
166 WriteTestData(); | |
167 | |
168 std::string data; | |
169 PlatformStateStoreLoadResult result = ReadStoreData(profile_, &data); | |
170 EXPECT_EQ(PlatformStateStoreLoadResult::CLEARED_DATA, result); | |
171 EXPECT_EQ(std::string(), data); | |
172 AssertTestDataIsAbsent(); | |
173 } | |
174 | |
175 } // namespace platform_state_store | |
176 } // namespace safe_browsing | |
OLD | NEW |