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_prefs.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/protector/protected_prefs_watcher.h" |
| 12 #include "chrome/browser/protector/protector_service.h" |
| 13 #include "chrome/browser/protector/protector_service_factory.h" |
| 14 #include "chrome/common/extensions/extension_constants.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/test/test_browser_thread.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace protector { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kNewHomePage[] = "http://example.com"; |
| 25 |
| 26 } |
| 27 |
| 28 class ProtectedPrefsWatcherTest : public testing::Test { |
| 29 public: |
| 30 virtual void SetUp() OVERRIDE { |
| 31 prefs_watcher_ = |
| 32 ProtectorServiceFactory::GetForProfile(&profile_)->GetPrefsWatcher(); |
| 33 prefs_ = profile_.GetPrefs(); |
| 34 } |
| 35 |
| 36 bool IsSignatureValid() { |
| 37 return prefs_watcher_->IsSignatureValid(); |
| 38 } |
| 39 |
| 40 bool HasBackup() { |
| 41 return prefs_watcher_->HasBackup(); |
| 42 } |
| 43 |
| 44 void RevalidateBackup() { |
| 45 prefs_watcher_->ValidateBackup(); |
| 46 } |
| 47 |
| 48 protected: |
| 49 ProtectedPrefsWatcher* prefs_watcher_; |
| 50 TestingProfile profile_; |
| 51 PrefService* prefs_; |
| 52 }; |
| 53 |
| 54 TEST_F(ProtectedPrefsWatcherTest, ValidOnCleanProfile) { |
| 55 EXPECT_TRUE(HasBackup()); |
| 56 EXPECT_TRUE(IsSignatureValid()); |
| 57 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); |
| 58 } |
| 59 |
| 60 TEST_F(ProtectedPrefsWatcherTest, ValidAfterPrefChange) { |
| 61 // Signature is still valid after a protected pref has been changed. |
| 62 base::StringValue new_homepage(kNewHomePage); |
| 63 EXPECT_NE(prefs_->GetString(prefs::kHomePage), kNewHomePage); |
| 64 EXPECT_FALSE(new_homepage.Equals( |
| 65 prefs_watcher_->GetBackupForPref(prefs::kHomePage))); |
| 66 |
| 67 prefs_->SetString(prefs::kHomePage, kNewHomePage); |
| 68 |
| 69 EXPECT_TRUE(HasBackup()); |
| 70 EXPECT_TRUE(IsSignatureValid()); |
| 71 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); |
| 72 EXPECT_EQ(prefs_->GetString(prefs::kHomePage), kNewHomePage); |
| 73 // Backup is updated accordingly. |
| 74 EXPECT_TRUE(new_homepage.Equals( |
| 75 prefs_watcher_->GetBackupForPref(prefs::kHomePage))); |
| 76 } |
| 77 |
| 78 TEST_F(ProtectedPrefsWatcherTest, InvalidSignature) { |
| 79 // Make backup invalid by changing one of its members directly. |
| 80 prefs_->SetString("backup.homepage", kNewHomePage); |
| 81 RevalidateBackup(); |
| 82 EXPECT_TRUE(HasBackup()); |
| 83 EXPECT_FALSE(IsSignatureValid()); |
| 84 EXPECT_FALSE(prefs_watcher_->is_backup_valid()); |
| 85 // No backup values available. |
| 86 EXPECT_FALSE(prefs_watcher_->GetBackupForPref(prefs::kHomePage)); |
| 87 |
| 88 // Now change the corresponding protected prefernce: backup should be signed |
| 89 // again but still invalid. |
| 90 prefs_->SetString(prefs::kHomePage, kNewHomePage); |
| 91 EXPECT_TRUE(IsSignatureValid()); |
| 92 EXPECT_FALSE(prefs_watcher_->is_backup_valid()); |
| 93 EXPECT_FALSE(prefs_watcher_->GetBackupForPref(prefs::kHomePage)); |
| 94 } |
| 95 |
| 96 TEST_F(ProtectedPrefsWatcherTest, ExtensionPrefChange) { |
| 97 // Changes to extensions data (but not to extension IDs) do not update |
| 98 // the backup and its signature. |
| 99 MessageLoopForUI message_loop; |
| 100 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 101 &message_loop); |
| 102 |
| 103 FilePath extensions_install_dir = |
| 104 profile_.GetPath().AppendASCII(ExtensionService::kInstallDirectoryName); |
| 105 scoped_ptr<ExtensionPrefValueMap> extension_pref_value_map_( |
| 106 new ExtensionPrefValueMap); |
| 107 scoped_ptr<ExtensionPrefs> extension_prefs( |
| 108 new ExtensionPrefs(profile_.GetPrefs(), |
| 109 extensions_install_dir, |
| 110 extension_pref_value_map_.get())); |
| 111 std::string sample_id = extension_misc::kWebStoreAppId; |
| 112 extension_prefs->Init(false); |
| 113 // Flip a pref value of an extension (this will actually add it to the list). |
| 114 extension_prefs->SetAppNotificationDisabled( |
| 115 sample_id, !extension_prefs->IsAppNotificationDisabled(sample_id)); |
| 116 |
| 117 // Backup is still valid. |
| 118 EXPECT_TRUE(IsSignatureValid()); |
| 119 EXPECT_TRUE(prefs_watcher_->is_backup_valid()); |
| 120 |
| 121 // Make backup invalid by changing one of its members directly. |
| 122 prefs_->SetString("backup.homepage", kNewHomePage); |
| 123 RevalidateBackup(); |
| 124 EXPECT_FALSE(IsSignatureValid()); |
| 125 |
| 126 // Flip another pref value of that extension. |
| 127 extension_prefs->SetIsIncognitoEnabled( |
| 128 sample_id, !extension_prefs->IsIncognitoEnabled(sample_id)); |
| 129 |
| 130 // No changes to the backup and signature. |
| 131 EXPECT_FALSE(IsSignatureValid()); |
| 132 |
| 133 // Blacklisting the extension does update the backup and signature. |
| 134 std::set<std::string> blacklist; |
| 135 blacklist.insert(sample_id); |
| 136 extension_prefs->UpdateBlacklist(blacklist); |
| 137 |
| 138 EXPECT_TRUE(IsSignatureValid()); |
| 139 } |
| 140 |
| 141 } // namespace protector |
OLD | NEW |