| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/privacy_blacklist/blacklist_manager.h" | 5 #include "chrome/browser/privacy_blacklist/blacklist_manager.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/scoped_temp_dir.h" | |
| 10 #include "base/thread.h" | 9 #include "base/thread.h" |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/privacy_blacklist/blacklist.h" | 10 #include "chrome/browser/privacy_blacklist/blacklist.h" |
| 11 #include "chrome/browser/privacy_blacklist/blacklist_test_util.h" |
| 13 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/notification_observer.h" |
| 15 #include "chrome/common/extensions/extension_constants.h" | 14 #include "chrome/common/notification_registrar.h" |
| 16 #include "chrome/common/notification_service.h" | 15 #include "chrome/common/notification_source.h" |
| 17 #include "chrome/test/testing_profile.h" | 16 #include "chrome/common/notification_type.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 class MyTestingProfile : public TestingProfile { | |
| 23 public: | |
| 24 MyTestingProfile() { | |
| 25 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 26 path_ = temp_dir_.path(); | |
| 27 } | |
| 28 | |
| 29 private: | |
| 30 ScopedTempDir temp_dir_; | |
| 31 | |
| 32 DISALLOW_COPY_AND_ASSIGN(MyTestingProfile); | |
| 33 }; | |
| 34 | |
| 35 class TestBlacklistPathProvider : public BlacklistPathProvider { | |
| 36 public: | |
| 37 explicit TestBlacklistPathProvider(Profile* profile) : profile_(profile) { | |
| 38 } | |
| 39 | |
| 40 virtual bool AreBlacklistPathsReady() const { | |
| 41 return true; | |
| 42 } | |
| 43 | |
| 44 virtual std::vector<FilePath> GetPersistentBlacklistPaths() { | |
| 45 return persistent_paths_; | |
| 46 } | |
| 47 | |
| 48 virtual std::vector<FilePath> GetTransientBlacklistPaths() { | |
| 49 return transient_paths_; | |
| 50 } | |
| 51 | |
| 52 void AddPersistentPath(const FilePath& path) { | |
| 53 persistent_paths_.push_back(path); | |
| 54 SendUpdateNotification(); | |
| 55 } | |
| 56 | |
| 57 void AddTransientPath(const FilePath& path) { | |
| 58 transient_paths_.push_back(path); | |
| 59 SendUpdateNotification(); | |
| 60 } | |
| 61 | |
| 62 void clear() { | |
| 63 persistent_paths_.clear(); | |
| 64 transient_paths_.clear(); | |
| 65 SendUpdateNotification(); | |
| 66 } | |
| 67 | |
| 68 private: | |
| 69 void SendUpdateNotification() { | |
| 70 ListValue* privacy_blacklists = new ListValue; | |
| 71 privacy_blacklists->Append(new StringValue("dummy.pbl")); | |
| 72 | |
| 73 DictionaryValue manifest; | |
| 74 manifest.SetString(extension_manifest_keys::kVersion, "1.0"); | |
| 75 manifest.SetString(extension_manifest_keys::kName, "test"); | |
| 76 manifest.Set(extension_manifest_keys::kPrivacyBlacklists, | |
| 77 privacy_blacklists); | |
| 78 | |
| 79 #if defined(OS_WIN) | |
| 80 FilePath path(FILE_PATH_LITERAL("c:\\foo")); | |
| 81 #elif defined(OS_POSIX) | |
| 82 FilePath path(FILE_PATH_LITERAL("/foo")); | |
| 83 #endif | |
| 84 Extension extension(path); | |
| 85 std::string error; | |
| 86 ASSERT_TRUE(extension.InitFromValue(manifest, false, &error)); | |
| 87 ASSERT_TRUE(error.empty()); | |
| 88 | |
| 89 NotificationService::current()->Notify( | |
| 90 NotificationType::EXTENSION_LOADED, | |
| 91 Source<Profile>(profile_), | |
| 92 Details<Extension>(&extension)); | |
| 93 } | |
| 94 | |
| 95 Profile* profile_; | |
| 96 | |
| 97 std::vector<FilePath> persistent_paths_; | |
| 98 std::vector<FilePath> transient_paths_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); | |
| 101 }; | |
| 102 | |
| 103 class BlacklistManagerTest : public testing::Test, public NotificationObserver { | 21 class BlacklistManagerTest : public testing::Test, public NotificationObserver { |
| 104 public: | 22 public: |
| 105 BlacklistManagerTest() | 23 BlacklistManagerTest() |
| 106 : path_provider_(&profile_), | 24 : path_provider_(&profile_), |
| 107 mock_ui_thread_(ChromeThread::UI, MessageLoop::current()), | 25 mock_ui_thread_(ChromeThread::UI, MessageLoop::current()), |
| 108 mock_file_thread_(ChromeThread::FILE), | 26 mock_file_thread_(ChromeThread::FILE), |
| 109 mock_io_thread_(ChromeThread::IO, MessageLoop::current()) { | 27 mock_io_thread_(ChromeThread::IO, MessageLoop::current()) { |
| 110 } | 28 } |
| 111 | 29 |
| 112 virtual void SetUp() { | 30 virtual void SetUp() { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 139 void WaitForBlacklistUpdate() { | 57 void WaitForBlacklistUpdate() { |
| 140 NotificationRegistrar registrar; | 58 NotificationRegistrar registrar; |
| 141 registrar.Add(this, | 59 registrar.Add(this, |
| 142 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, | 60 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, |
| 143 Source<Profile>(&profile_)); | 61 Source<Profile>(&profile_)); |
| 144 MessageLoop::current()->Run(); | 62 MessageLoop::current()->Run(); |
| 145 } | 63 } |
| 146 | 64 |
| 147 FilePath test_data_dir_; | 65 FilePath test_data_dir_; |
| 148 | 66 |
| 149 MyTestingProfile profile_; | 67 BlacklistTestingProfile profile_; |
| 150 | 68 |
| 151 TestBlacklistPathProvider path_provider_; | 69 TestBlacklistPathProvider path_provider_; |
| 152 | 70 |
| 153 private: | 71 private: |
| 154 MessageLoop loop_; | 72 MessageLoop loop_; |
| 155 | 73 |
| 156 ChromeThread mock_ui_thread_; | 74 ChromeThread mock_ui_thread_; |
| 157 ChromeThread mock_file_thread_; | 75 ChromeThread mock_file_thread_; |
| 158 ChromeThread mock_io_thread_; | 76 ChromeThread mock_io_thread_; |
| 159 }; | 77 }; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | 110 EXPECT_FALSE(BlacklistHasMatch(blacklist1, |
| 193 "http://host/annoying_ads/ad.jpg")); | 111 "http://host/annoying_ads/ad.jpg")); |
| 194 | 112 |
| 195 path_provider_.AddPersistentPath( | 113 path_provider_.AddPersistentPath( |
| 196 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 114 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
| 197 WaitForBlacklistUpdate(); | 115 WaitForBlacklistUpdate(); |
| 198 | 116 |
| 199 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); | 117 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); |
| 200 | 118 |
| 201 // Added a real blacklist, the manager should recompile. | 119 // Added a real blacklist, the manager should recompile. |
| 202 EXPECT_NE(blacklist1, blacklist2); | |
| 203 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); | 120 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); |
| 204 EXPECT_FALSE(BlacklistHasMatch(blacklist2, "http://host/other_ads/ad.jpg")); | 121 EXPECT_FALSE(BlacklistHasMatch(blacklist2, "http://host/other_ads/ad.jpg")); |
| 205 | 122 |
| 206 path_provider_.AddTransientPath(test_data_dir_.AppendASCII("other_ads.pbl")); | 123 path_provider_.AddTransientPath(test_data_dir_.AppendASCII("other_ads.pbl")); |
| 207 WaitForBlacklistUpdate(); | 124 WaitForBlacklistUpdate(); |
| 208 | 125 |
| 209 const Blacklist* blacklist3 = manager->GetCompiledBlacklist(); | 126 const Blacklist* blacklist3 = manager->GetCompiledBlacklist(); |
| 210 | 127 |
| 211 // In theory blacklist2 and blacklist3 could be the same object, so we're | 128 // In theory blacklist2 and blacklist3 could be the same object, so we're |
| 212 // not checking for inequality. | 129 // not checking for inequality. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 231 | 148 |
| 232 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { | 149 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { |
| 233 scoped_refptr<BlacklistManager> manager( | 150 scoped_refptr<BlacklistManager> manager( |
| 234 new BlacklistManager(&profile_, &path_provider_)); | 151 new BlacklistManager(&profile_, &path_provider_)); |
| 235 manager->Initialize(); | 152 manager->Initialize(); |
| 236 WaitForBlacklistUpdate(); | 153 WaitForBlacklistUpdate(); |
| 237 | 154 |
| 238 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); | 155 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); |
| 239 ASSERT_FALSE(file_util::PathExists(bogus_path)); | 156 ASSERT_FALSE(file_util::PathExists(bogus_path)); |
| 240 path_provider_.AddPersistentPath(bogus_path); | 157 path_provider_.AddPersistentPath(bogus_path); |
| 241 WaitForBlacklistError(); | 158 WaitForBlacklistUpdate(); |
| 242 | 159 |
| 243 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 160 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 244 EXPECT_TRUE(blacklist); | 161 EXPECT_TRUE(blacklist); |
| 245 } | 162 } |
| 246 | 163 |
| 247 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { | 164 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { |
| 248 FilePath compiled_blacklist_path; | 165 FilePath compiled_blacklist_path; |
| 249 | 166 |
| 250 { | 167 { |
| 251 scoped_refptr<BlacklistManager> manager( | 168 scoped_refptr<BlacklistManager> manager( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 273 WaitForBlacklistUpdate(); | 190 WaitForBlacklistUpdate(); |
| 274 | 191 |
| 275 // The manager should recompile the blacklist. | 192 // The manager should recompile the blacklist. |
| 276 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 193 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 277 EXPECT_TRUE(BlacklistHasMatch(blacklist, | 194 EXPECT_TRUE(BlacklistHasMatch(blacklist, |
| 278 "http://host/annoying_ads/ad.jpg")); | 195 "http://host/annoying_ads/ad.jpg")); |
| 279 } | 196 } |
| 280 } | 197 } |
| 281 | 198 |
| 282 } // namespace | 199 } // namespace |
| OLD | NEW |