| 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" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/thread.h" | 10 #include "base/thread.h" |
| 11 #include "base/values.h" |
| 11 #include "chrome/browser/privacy_blacklist/blacklist.h" | 12 #include "chrome/browser/privacy_blacklist/blacklist.h" |
| 12 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_constants.h" |
| 14 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
| 15 #include "chrome/test/testing_profile.h" | 17 #include "chrome/test/testing_profile.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 class MyTestingProfile : public TestingProfile { | 22 class MyTestingProfile : public TestingProfile { |
| 21 public: | 23 public: |
| 22 MyTestingProfile() { | 24 MyTestingProfile() { |
| 23 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 25 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 24 path_ = temp_dir_.path(); | 26 path_ = temp_dir_.path(); |
| 25 } | 27 } |
| 26 | 28 |
| 27 private: | 29 private: |
| 28 ScopedTempDir temp_dir_; | 30 ScopedTempDir temp_dir_; |
| 29 | 31 |
| 30 DISALLOW_COPY_AND_ASSIGN(MyTestingProfile); | 32 DISALLOW_COPY_AND_ASSIGN(MyTestingProfile); |
| 31 }; | 33 }; |
| 32 | 34 |
| 33 class TestBlacklistPathProvider : public BlacklistPathProvider { | 35 class TestBlacklistPathProvider : public BlacklistPathProvider { |
| 34 public: | 36 public: |
| 35 explicit TestBlacklistPathProvider(Profile* profile) : profile_(profile) { | 37 explicit TestBlacklistPathProvider(Profile* profile) : profile_(profile) { |
| 36 } | 38 } |
| 37 | 39 |
| 40 virtual bool AreBlacklistPathsReady() const { |
| 41 return true; |
| 42 } |
| 43 |
| 38 virtual std::vector<FilePath> GetPersistentBlacklistPaths() { | 44 virtual std::vector<FilePath> GetPersistentBlacklistPaths() { |
| 39 return persistent_paths_; | 45 return persistent_paths_; |
| 40 } | 46 } |
| 41 | 47 |
| 42 virtual std::vector<FilePath> GetTransientBlacklistPaths() { | 48 virtual std::vector<FilePath> GetTransientBlacklistPaths() { |
| 43 return transient_paths_; | 49 return transient_paths_; |
| 44 } | 50 } |
| 45 | 51 |
| 46 void AddPersistentPath(const FilePath& path) { | 52 void AddPersistentPath(const FilePath& path) { |
| 47 persistent_paths_.push_back(path); | 53 persistent_paths_.push_back(path); |
| 48 SendUpdateNotification(); | 54 SendUpdateNotification(); |
| 49 } | 55 } |
| 50 | 56 |
| 51 void AddTransientPath(const FilePath& path) { | 57 void AddTransientPath(const FilePath& path) { |
| 52 transient_paths_.push_back(path); | 58 transient_paths_.push_back(path); |
| 53 SendUpdateNotification(); | 59 SendUpdateNotification(); |
| 54 } | 60 } |
| 55 | 61 |
| 56 void clear() { | 62 void clear() { |
| 57 persistent_paths_.clear(); | 63 persistent_paths_.clear(); |
| 58 transient_paths_.clear(); | 64 transient_paths_.clear(); |
| 59 SendUpdateNotification(); | 65 SendUpdateNotification(); |
| 60 } | 66 } |
| 61 | 67 |
| 62 private: | 68 private: |
| 63 void SendUpdateNotification() { | 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 |
| 64 #if defined(OS_WIN) | 79 #if defined(OS_WIN) |
| 65 FilePath path(FILE_PATH_LITERAL("c:\\foo")); | 80 FilePath path(FILE_PATH_LITERAL("c:\\foo")); |
| 66 #elif defined(OS_POSIX) | 81 #elif defined(OS_POSIX) |
| 67 FilePath path(FILE_PATH_LITERAL("/foo")); | 82 FilePath path(FILE_PATH_LITERAL("/foo")); |
| 68 #endif | 83 #endif |
| 69 Extension extension(path); | 84 Extension extension(path); |
| 85 std::string error; |
| 86 ASSERT_TRUE(extension.InitFromValue(manifest, false, &error)); |
| 87 ASSERT_TRUE(error.empty()); |
| 88 |
| 70 NotificationService::current()->Notify( | 89 NotificationService::current()->Notify( |
| 71 NotificationType::EXTENSION_LOADED, | 90 NotificationType::EXTENSION_LOADED, |
| 72 Source<Profile>(profile_), | 91 Source<Profile>(profile_), |
| 73 Details<Extension>(&extension)); | 92 Details<Extension>(&extension)); |
| 74 } | 93 } |
| 75 | 94 |
| 76 Profile* profile_; | 95 Profile* profile_; |
| 77 | 96 |
| 78 std::vector<FilePath> persistent_paths_; | 97 std::vector<FilePath> persistent_paths_; |
| 79 std::vector<FilePath> transient_paths_; | 98 std::vector<FilePath> transient_paths_; |
| 80 | 99 |
| 81 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); | 100 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); |
| 82 }; | 101 }; |
| 83 | 102 |
| 84 class BlacklistManagerTest : public testing::Test, public NotificationObserver { | 103 class BlacklistManagerTest : public testing::Test, public NotificationObserver { |
| 85 public: | 104 public: |
| 86 BlacklistManagerTest() | 105 BlacklistManagerTest() |
| 87 : path_provider_(&profile_), | 106 : path_provider_(&profile_), |
| 88 mock_ui_thread_(ChromeThread::UI, MessageLoop::current()), | 107 mock_ui_thread_(ChromeThread::UI, MessageLoop::current()), |
| 89 mock_file_thread_(ChromeThread::FILE) { | 108 mock_file_thread_(ChromeThread::FILE), |
| 109 mock_io_thread_(ChromeThread::IO, MessageLoop::current()) { |
| 90 } | 110 } |
| 91 | 111 |
| 92 virtual void SetUp() { | 112 virtual void SetUp() { |
| 93 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); | 113 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); |
| 94 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); | 114 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); |
| 95 ASSERT_TRUE(mock_file_thread_.Start()); | 115 ASSERT_TRUE(mock_file_thread_.Start()); |
| 96 } | 116 } |
| 97 | 117 |
| 98 virtual void TearDown() { | 118 virtual void TearDown() { |
| 99 mock_file_thread_.Stop(); | 119 mock_file_thread_.Stop(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 128 | 148 |
| 129 MyTestingProfile profile_; | 149 MyTestingProfile profile_; |
| 130 | 150 |
| 131 TestBlacklistPathProvider path_provider_; | 151 TestBlacklistPathProvider path_provider_; |
| 132 | 152 |
| 133 private: | 153 private: |
| 134 MessageLoop loop_; | 154 MessageLoop loop_; |
| 135 | 155 |
| 136 ChromeThread mock_ui_thread_; | 156 ChromeThread mock_ui_thread_; |
| 137 ChromeThread mock_file_thread_; | 157 ChromeThread mock_file_thread_; |
| 158 ChromeThread mock_io_thread_; |
| 138 }; | 159 }; |
| 139 | 160 |
| 140 // Returns true if |blacklist| contains a match for |url|. | 161 // Returns true if |blacklist| contains a match for |url|. |
| 141 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { | 162 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { |
| 142 Blacklist::Match* match = blacklist->findMatch(GURL(url)); | 163 Blacklist::Match* match = blacklist->findMatch(GURL(url)); |
| 143 | 164 |
| 144 if (!match) | 165 if (!match) |
| 145 return false; | 166 return false; |
| 146 | 167 |
| 147 delete match; | 168 delete match; |
| 148 return true; | 169 return true; |
| 149 } | 170 } |
| 150 | 171 |
| 151 TEST_F(BlacklistManagerTest, Basic) { | 172 TEST_F(BlacklistManagerTest, Basic) { |
| 152 scoped_refptr<BlacklistManager> manager(new BlacklistManager()); | 173 scoped_refptr<BlacklistManager> manager( |
| 153 manager->Initialize(&profile_, &path_provider_); | 174 new BlacklistManager(&profile_, &path_provider_)); |
| 175 manager->Initialize(); |
| 154 WaitForBlacklistUpdate(); | 176 WaitForBlacklistUpdate(); |
| 155 | 177 |
| 156 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 178 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 157 EXPECT_TRUE(blacklist); | 179 EXPECT_TRUE(blacklist); |
| 158 | 180 |
| 159 // Repeated invocations of GetCompiledBlacklist should return the same object. | 181 // Repeated invocations of GetCompiledBlacklist should return the same object. |
| 160 EXPECT_EQ(blacklist, manager->GetCompiledBlacklist()); | 182 EXPECT_EQ(blacklist, manager->GetCompiledBlacklist()); |
| 161 } | 183 } |
| 162 | 184 |
| 163 TEST_F(BlacklistManagerTest, BlacklistPathProvider) { | 185 TEST_F(BlacklistManagerTest, BlacklistPathProvider) { |
| 164 scoped_refptr<BlacklistManager> manager(new BlacklistManager()); | 186 scoped_refptr<BlacklistManager> manager( |
| 165 manager->Initialize(&profile_, &path_provider_); | 187 new BlacklistManager(&profile_, &path_provider_)); |
| 188 manager->Initialize(); |
| 166 WaitForBlacklistUpdate(); | 189 WaitForBlacklistUpdate(); |
| 167 | 190 |
| 168 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); | 191 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); |
| 169 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | 192 EXPECT_FALSE(BlacklistHasMatch(blacklist1, |
| 170 "http://host/annoying_ads/ad.jpg")); | 193 "http://host/annoying_ads/ad.jpg")); |
| 171 | 194 |
| 172 path_provider_.AddPersistentPath( | 195 path_provider_.AddPersistentPath( |
| 173 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 196 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
| 174 WaitForBlacklistUpdate(); | 197 WaitForBlacklistUpdate(); |
| 175 | 198 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 189 // not checking for inequality. | 212 // not checking for inequality. |
| 190 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/annoying_ads/ad.jpg")); | 213 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/annoying_ads/ad.jpg")); |
| 191 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/other_ads/ad.jpg")); | 214 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/other_ads/ad.jpg")); |
| 192 | 215 |
| 193 // Now make sure that transient blacklists don't survive after re-creating | 216 // Now make sure that transient blacklists don't survive after re-creating |
| 194 // the BlacklistManager. | 217 // the BlacklistManager. |
| 195 manager = NULL; | 218 manager = NULL; |
| 196 path_provider_.clear(); | 219 path_provider_.clear(); |
| 197 path_provider_.AddPersistentPath( | 220 path_provider_.AddPersistentPath( |
| 198 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 221 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
| 199 manager = new BlacklistManager(); | 222 manager = new BlacklistManager(&profile_, &path_provider_); |
| 200 manager->Initialize(&profile_, &path_provider_); | 223 manager->Initialize(); |
| 201 WaitForBlacklistUpdate(); | 224 WaitForBlacklistUpdate(); |
| 202 | 225 |
| 203 const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); | 226 const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); |
| 204 | 227 |
| 205 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/annoying_ads/ad.jpg")); | 228 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/annoying_ads/ad.jpg")); |
| 206 EXPECT_FALSE(BlacklistHasMatch(blacklist4, "http://host/other_ads/ad.jpg")); | 229 EXPECT_FALSE(BlacklistHasMatch(blacklist4, "http://host/other_ads/ad.jpg")); |
| 207 } | 230 } |
| 208 | 231 |
| 209 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { | 232 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { |
| 210 scoped_refptr<BlacklistManager> manager(new BlacklistManager()); | 233 scoped_refptr<BlacklistManager> manager( |
| 211 manager->Initialize(&profile_, &path_provider_); | 234 new BlacklistManager(&profile_, &path_provider_)); |
| 235 manager->Initialize(); |
| 212 WaitForBlacklistUpdate(); | 236 WaitForBlacklistUpdate(); |
| 213 | 237 |
| 214 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); | 238 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); |
| 215 ASSERT_FALSE(file_util::PathExists(bogus_path)); | 239 ASSERT_FALSE(file_util::PathExists(bogus_path)); |
| 216 path_provider_.AddPersistentPath(bogus_path); | 240 path_provider_.AddPersistentPath(bogus_path); |
| 217 WaitForBlacklistError(); | 241 WaitForBlacklistError(); |
| 218 | 242 |
| 219 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 243 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 220 EXPECT_TRUE(blacklist); | 244 EXPECT_TRUE(blacklist); |
| 221 } | 245 } |
| 222 | 246 |
| 223 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { | 247 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { |
| 224 FilePath compiled_blacklist_path; | 248 FilePath compiled_blacklist_path; |
| 225 | 249 |
| 226 { | 250 { |
| 227 scoped_refptr<BlacklistManager> manager(new BlacklistManager()); | 251 scoped_refptr<BlacklistManager> manager( |
| 228 manager->Initialize(&profile_, &path_provider_); | 252 new BlacklistManager(&profile_, &path_provider_)); |
| 253 manager->Initialize(); |
| 229 WaitForBlacklistUpdate(); | 254 WaitForBlacklistUpdate(); |
| 230 | 255 |
| 231 path_provider_.AddPersistentPath( | 256 path_provider_.AddPersistentPath( |
| 232 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 257 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
| 233 WaitForBlacklistUpdate(); | 258 WaitForBlacklistUpdate(); |
| 234 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 259 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 235 EXPECT_TRUE(BlacklistHasMatch(blacklist, | 260 EXPECT_TRUE(BlacklistHasMatch(blacklist, |
| 236 "http://host/annoying_ads/ad.jpg")); | 261 "http://host/annoying_ads/ad.jpg")); |
| 237 | 262 |
| 238 compiled_blacklist_path = manager->compiled_blacklist_path(); | 263 compiled_blacklist_path = manager->compiled_blacklist_path(); |
| 239 } | 264 } |
| 240 | 265 |
| 241 ASSERT_TRUE(file_util::PathExists(compiled_blacklist_path)); | 266 ASSERT_TRUE(file_util::PathExists(compiled_blacklist_path)); |
| 242 ASSERT_TRUE(file_util::Delete(compiled_blacklist_path, false)); | 267 ASSERT_TRUE(file_util::Delete(compiled_blacklist_path, false)); |
| 243 | 268 |
| 244 { | 269 { |
| 245 scoped_refptr<BlacklistManager> manager(new BlacklistManager()); | 270 scoped_refptr<BlacklistManager> manager( |
| 246 manager->Initialize(&profile_, &path_provider_); | 271 new BlacklistManager(&profile_, &path_provider_)); |
| 272 manager->Initialize(); |
| 247 WaitForBlacklistUpdate(); | 273 WaitForBlacklistUpdate(); |
| 248 | 274 |
| 249 // The manager should recompile the blacklist. | 275 // The manager should recompile the blacklist. |
| 250 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 276 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 251 EXPECT_TRUE(BlacklistHasMatch(blacklist, | 277 EXPECT_TRUE(BlacklistHasMatch(blacklist, |
| 252 "http://host/annoying_ads/ad.jpg")); | 278 "http://host/annoying_ads/ad.jpg")); |
| 253 } | 279 } |
| 254 } | 280 } |
| 255 | 281 |
| 256 } // namespace | 282 } // namespace |
| OLD | NEW |