OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "base/message_loop.h" | |
6 #include "base/values.h" | |
7 #include "chrome/browser/extensions/extension_pref_value_map.h" | |
8 #include "chrome/browser/extensions/extension_pref_value_map_factory.h" | |
9 #include "chrome/browser/extensions/extension_prefs.h" | |
10 #include "chrome/browser/extensions/extension_service.h" | |
11 #include "chrome/browser/prefs/pref_service.h" | |
12 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
13 #include "chrome/browser/protector/protected_prefs_watcher.h" | |
14 #include "chrome/browser/protector/protector_service_factory.h" | |
15 #include "chrome/browser/protector/protector_service.h" | |
16 #include "chrome/common/extensions/extension_constants.h" | |
17 #include "chrome/common/pref_names.h" | |
18 #include "chrome/test/base/testing_profile.h" | |
19 #include "content/public/test/test_browser_thread.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace protector { | |
23 | |
24 namespace { | |
25 | |
26 const char kBackupSignature[] = "backup._signature"; | |
27 const char kNewHomePage[] = "http://example.com"; | |
28 | |
29 } | |
30 | |
31 class ProtectedPrefsWatcherTest : public testing::Test { | |
32 public: | |
33 virtual void SetUp() OVERRIDE { | |
34 prefs_watcher_ = | |
35 ProtectorServiceFactory::GetForProfile(&profile_)->GetPrefsWatcher(); | |
36 prefs_ = profile_.GetPrefs(); | |
37 } | |
38 | |
39 bool IsSignatureValid() { | |
40 return prefs_watcher_->IsSignatureValid(); | |
41 } | |
42 | |
43 bool HasBackup() { | |
44 return prefs_watcher_->HasBackup(); | |
45 } | |
46 | |
47 void RevalidateBackup() { | |
48 prefs_watcher_->ValidateBackup(); | |
49 } | |
50 | |
51 void ForceUpdateSignature(ProtectedPrefsWatcher* prefs_watcher) { | |
52 prefs_watcher->UpdateBackupSignature(); | |
53 } | |
54 | |
55 protected: | |
56 ProtectedPrefsWatcher* prefs_watcher_; | |
57 TestingProfile profile_; | |
58 PrefService* prefs_; | |
59 }; | |
60 | |
61 TEST_F(ProtectedPrefsWatcherTest, ValidOnCleanProfile) { | |
62 EXPECT_TRUE(HasBackup()); | |
63 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); | |
64 } | |
65 | |
66 TEST_F(ProtectedPrefsWatcherTest, ValidAfterPrefChange) { | |
67 // Signature is still valid after a protected pref has been changed. | |
68 base::StringValue new_homepage(kNewHomePage); | |
69 EXPECT_NE(prefs_->GetString(prefs::kHomePage), kNewHomePage); | |
70 EXPECT_FALSE(new_homepage.Equals( | |
71 prefs_watcher_->GetBackupForPref(prefs::kHomePage))); | |
72 | |
73 prefs_->SetString(prefs::kHomePage, kNewHomePage); | |
74 | |
75 EXPECT_TRUE(HasBackup()); | |
76 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); | |
77 EXPECT_EQ(prefs_->GetString(prefs::kHomePage), kNewHomePage); | |
78 // Backup is updated accordingly. | |
79 EXPECT_TRUE(new_homepage.Equals( | |
80 prefs_watcher_->GetBackupForPref(prefs::kHomePage))); | |
81 } | |
82 | |
83 TEST_F(ProtectedPrefsWatcherTest, InvalidSignature) { | |
84 // Make backup invalid by changing one of its members directly. | |
85 prefs_->SetString("backup.homepage", kNewHomePage); | |
86 RevalidateBackup(); | |
87 EXPECT_TRUE(HasBackup()); | |
88 EXPECT_FALSE(prefs_watcher_->is_backup_valid()); | |
89 // No backup values available. | |
90 EXPECT_FALSE(prefs_watcher_->GetBackupForPref(prefs::kHomePage)); | |
91 | |
92 // Now change the corresponding protected prefernce: backup should be signed | |
93 // again but still invalid. | |
94 prefs_->SetString(prefs::kHomePage, kNewHomePage); | |
95 EXPECT_TRUE(IsSignatureValid()); | |
96 EXPECT_FALSE(prefs_watcher_->is_backup_valid()); | |
97 EXPECT_FALSE(prefs_watcher_->GetBackupForPref(prefs::kHomePage)); | |
98 } | |
99 | |
100 TEST_F(ProtectedPrefsWatcherTest, ExtensionPrefChange) { | |
101 // Changes to extensions data (but not to extension IDs) do not update | |
102 // the backup and its signature. | |
103 MessageLoopForUI message_loop; | |
104 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
105 &message_loop); | |
106 | |
107 FilePath extensions_install_dir = | |
108 profile_.GetPath().AppendASCII(ExtensionService::kInstallDirectoryName); | |
109 scoped_ptr<extensions::ExtensionPrefs> extension_prefs = | |
110 extensions::ExtensionPrefs::Create( | |
111 profile_.GetPrefs(), | |
112 extensions_install_dir, | |
113 ExtensionPrefValueMapFactory::GetForProfile(&profile_), | |
114 false); // extensions_disabled | |
115 std::string sample_id = extension_misc::kWebStoreAppId; | |
116 // Flip a pref value of an extension (this will actually add it to the list). | |
117 extension_prefs->SetAppNotificationDisabled( | |
118 sample_id, !extension_prefs->IsAppNotificationDisabled(sample_id)); | |
119 | |
120 // Backup is still valid. | |
121 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); | |
122 | |
123 // Make signature invalid by changing it directly. | |
124 prefs_->SetString(kBackupSignature, "INVALID"); | |
125 EXPECT_FALSE(IsSignatureValid()); | |
126 | |
127 // Flip another pref value of that extension. | |
128 extension_prefs->SetExtensionRunning( | |
129 sample_id, !extension_prefs->IsExtensionRunning(sample_id)); | |
130 | |
131 // No changes to the backup and signature. | |
132 EXPECT_FALSE(IsSignatureValid()); | |
133 | |
134 // Blacklisting the extension does update the backup and signature. | |
135 extension_prefs->SetExtensionBlacklisted(sample_id, true); | |
136 | |
137 EXPECT_TRUE(IsSignatureValid()); | |
138 } | |
139 | |
140 // Verify that version bigger than 1 is included in the signature. | |
141 TEST_F(ProtectedPrefsWatcherTest, VersionIsSigned) { | |
142 // Reset version to 1. | |
143 prefs_->ClearPref("backup._version"); | |
144 // This should make the backup invalid. | |
145 EXPECT_FALSE(IsSignatureValid()); | |
146 | |
147 // "Migrate" the backup back to the latest version. | |
148 RevalidateBackup(); | |
149 | |
150 EXPECT_FALSE(prefs_watcher_->is_backup_valid()); | |
151 EXPECT_EQ(ProtectedPrefsWatcher::kCurrentVersionNumber, | |
152 prefs_->GetInteger("backup._version")); | |
153 } | |
154 | |
155 // Verify that backup for "pinned_tabs" is added during version 2 migration. | |
156 TEST_F(ProtectedPrefsWatcherTest, MigrationToVersion2) { | |
157 // Add a pinned tab. | |
158 { | |
159 ListPrefUpdate pinned_tabs_update(prefs_, prefs::kPinnedTabs); | |
160 base::ListValue* pinned_tabs = pinned_tabs_update.Get(); | |
161 pinned_tabs->Clear(); | |
162 base::DictionaryValue* tab = new base::DictionaryValue; | |
163 tab->SetString("url", "http://example.com/"); | |
164 pinned_tabs->Append(tab); | |
165 } | |
166 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); | |
167 | |
168 scoped_ptr<base::Value> pinned_tabs_copy( | |
169 prefs_->GetList(prefs::kPinnedTabs)->DeepCopy()); | |
170 | |
171 // Reset version to 1, remove "pinned_tabs" and overwrite the signature. | |
172 // Store the old signature (without "pinned_tabs"). | |
173 prefs_->ClearPref("backup._version"); | |
174 prefs_->ClearPref("backup.pinned_tabs"); | |
175 ForceUpdateSignature(prefs_watcher_); | |
176 EXPECT_TRUE(IsSignatureValid()); | |
177 | |
178 // This will migrate backup to the latest version. | |
179 RevalidateBackup(); | |
180 | |
181 // Now the backup should be valid and "pinned_tabs" is added back. | |
182 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); | |
183 EXPECT_TRUE(pinned_tabs_copy->Equals(prefs_->GetList("backup.pinned_tabs"))); | |
184 EXPECT_TRUE(pinned_tabs_copy->Equals(prefs_->GetList(prefs::kPinnedTabs))); | |
185 EXPECT_FALSE(prefs_watcher_->DidPrefChange(prefs::kPinnedTabs)); | |
186 EXPECT_EQ(ProtectedPrefsWatcher::kCurrentVersionNumber, | |
187 prefs_->GetInteger("backup._version")); | |
188 } | |
189 | |
190 // Verify that SessionStartupPref migration doesn't trigger Protector | |
191 // (version 3 migration). | |
192 TEST_F(ProtectedPrefsWatcherTest, MigrationToVersion3) { | |
193 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); | |
194 | |
195 // Bring startup prefs to an old (pre-migration) version. | |
196 prefs_->SetBoolean(prefs::kHomePageIsNewTabPage, false); | |
197 prefs_->SetString(prefs::kHomePage, kNewHomePage); | |
198 prefs_->ClearPref(prefs::kRestoreOnStartupMigrated); | |
199 | |
200 // Reset version to 2 and overwrite the signature. | |
201 prefs_->SetInteger("backup._version", 2); | |
202 ForceUpdateSignature(prefs_watcher_); | |
203 EXPECT_TRUE(IsSignatureValid()); | |
204 | |
205 // Take down the old instance and create a new ProtectedPrefsWatcher from | |
206 // current prefs. | |
207 ProtectorServiceFactory::GetForProfile(&profile_)-> | |
208 StopWatchingPrefsForTesting(); | |
209 scoped_ptr<ProtectedPrefsWatcher> prefs_watcher2( | |
210 new ProtectedPrefsWatcher(&profile_)); | |
211 EXPECT_TRUE(prefs_watcher2->is_backup_valid()); | |
212 | |
213 // Startup settings shouldn't be reported as changed. | |
214 EXPECT_FALSE(prefs_watcher2->DidPrefChange(prefs::kRestoreOnStartup)); | |
215 EXPECT_FALSE(prefs_watcher2->DidPrefChange(prefs::kURLsToRestoreOnStartup)); | |
216 EXPECT_EQ(ProtectedPrefsWatcher::kCurrentVersionNumber, | |
217 prefs_->GetInteger("backup._version")); | |
218 } | |
219 | |
220 // Verify that migration to version 4 removes backups with default values. | |
221 TEST_F(ProtectedPrefsWatcherTest, MigrationToVersion4) { | |
222 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); | |
223 | |
224 prefs_->SetString(prefs::kHomePage, kNewHomePage); | |
225 EXPECT_TRUE(prefs_->HasPrefPath("backup.homepage")); | |
226 | |
227 // Reset version to 3 and overwrite the signature. | |
228 prefs_->SetInteger("backup._version", 3); | |
229 ForceUpdateSignature(prefs_watcher_); | |
230 EXPECT_TRUE(IsSignatureValid()); | |
231 | |
232 ProtectorServiceFactory::GetForProfile(&profile_)-> | |
233 StopWatchingPrefsForTesting(); | |
234 | |
235 // Restore |kHomePage| to default value. | |
236 prefs_->ClearPref(prefs::kHomePage); | |
237 | |
238 scoped_ptr<ProtectedPrefsWatcher> prefs_watcher2( | |
239 new ProtectedPrefsWatcher(&profile_)); | |
240 EXPECT_TRUE(prefs_watcher2->is_backup_valid()); | |
241 | |
242 // Backup for |kHomePage| should now be restored to the default value, too. | |
243 EXPECT_FALSE(prefs_->HasPrefPath("backup.homepage")); | |
244 EXPECT_FALSE(prefs_watcher2->DidPrefChange(prefs::kHomePage)); | |
245 EXPECT_EQ(ProtectedPrefsWatcher::kCurrentVersionNumber, | |
246 prefs_->GetInteger("backup._version")); | |
247 } | |
248 | |
249 // Verify handling of default values of protected prefs. | |
250 TEST_F(ProtectedPrefsWatcherTest, DefaultValues) { | |
251 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); | |
252 | |
253 EXPECT_FALSE(prefs_->HasPrefPath(prefs::kHomePage)); | |
254 EXPECT_FALSE(prefs_watcher_->DidPrefChange(prefs::kHomePage)); | |
255 prefs_->SetString(prefs::kHomePage, kNewHomePage); | |
256 EXPECT_FALSE(prefs_watcher_->DidPrefChange(prefs::kHomePage)); | |
257 | |
258 ProtectorServiceFactory::GetForProfile(&profile_)-> | |
259 StopWatchingPrefsForTesting(); | |
260 | |
261 // Restore |kHomePage| to default value. | |
262 prefs_->ClearPref(prefs::kHomePage); | |
263 | |
264 scoped_ptr<ProtectedPrefsWatcher> prefs_watcher2( | |
265 new ProtectedPrefsWatcher(&profile_)); | |
266 EXPECT_TRUE(prefs_watcher2->is_backup_valid()); | |
267 EXPECT_TRUE(prefs_watcher2->DidPrefChange(prefs::kHomePage)); | |
268 | |
269 prefs_->ClearPref("backup.homepage"); | |
270 ForceUpdateSignature(prefs_watcher2.get()); | |
271 | |
272 EXPECT_TRUE(prefs_watcher2->is_backup_valid()); | |
273 EXPECT_FALSE(prefs_watcher2->DidPrefChange(prefs::kHomePage)); | |
274 } | |
275 | |
276 TEST_F(ProtectedPrefsWatcherTest, CheckPrefNames) { | |
277 // If any of these preference names change, add corresponding migration code | |
278 // to ProtectedPrefsWatcher. | |
279 // DO NOT simply fix these literals! | |
280 EXPECT_EQ("homepage", std::string(prefs::kHomePage)); | |
281 EXPECT_EQ("homepage_is_newtabpage", | |
282 std::string(prefs::kHomePageIsNewTabPage)); | |
283 EXPECT_EQ("browser.show_home_button", std::string(prefs::kShowHomeButton)); | |
284 EXPECT_EQ("session.restore_on_startup", | |
285 std::string(prefs::kRestoreOnStartup)); | |
286 EXPECT_EQ("session.urls_to_restore_on_startup", | |
287 std::string(prefs::kURLsToRestoreOnStartup)); | |
288 EXPECT_EQ("pinned_tabs", std::string(prefs::kPinnedTabs)); | |
289 } | |
290 | |
291 } // namespace protector | |
OLD | NEW |