| 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" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 Profile* profile_; | 76 Profile* profile_; |
| 77 | 77 |
| 78 std::vector<FilePath> persistent_paths_; | 78 std::vector<FilePath> persistent_paths_; |
| 79 std::vector<FilePath> transient_paths_; | 79 std::vector<FilePath> transient_paths_; |
| 80 | 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); | 81 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 class BlacklistManagerTest : public testing::Test, public NotificationObserver { | 84 class BlacklistManagerTest : public testing::Test, public NotificationObserver { |
| 85 public: | 85 public: |
| 86 BlacklistManagerTest() : path_provider_(&profile_) { | 86 BlacklistManagerTest() |
| 87 : path_provider_(&profile_), |
| 88 mock_ui_thread_(ChromeThread::UI, MessageLoop::current()), |
| 89 mock_file_thread_(ChromeThread::FILE) { |
| 87 } | 90 } |
| 88 | 91 |
| 89 virtual void SetUp() { | 92 virtual void SetUp() { |
| 90 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); | 93 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); |
| 91 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); | 94 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); |
| 95 ASSERT_TRUE(mock_file_thread_.Start()); |
| 92 } | 96 } |
| 93 | 97 |
| 94 virtual void TearDown() { | 98 virtual void TearDown() { |
| 95 loop_.RunAllPending(); | 99 loop_.RunAllPending(); |
| 96 } | 100 } |
| 97 | 101 |
| 98 // NotificationObserver | 102 // NotificationObserver |
| 99 virtual void Observe(NotificationType type, | 103 virtual void Observe(NotificationType type, |
| 100 const NotificationSource& source, | 104 const NotificationSource& source, |
| 101 const NotificationDetails& details) { | 105 const NotificationDetails& details) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 120 } | 124 } |
| 121 | 125 |
| 122 FilePath test_data_dir_; | 126 FilePath test_data_dir_; |
| 123 | 127 |
| 124 MyTestingProfile profile_; | 128 MyTestingProfile profile_; |
| 125 | 129 |
| 126 TestBlacklistPathProvider path_provider_; | 130 TestBlacklistPathProvider path_provider_; |
| 127 | 131 |
| 128 private: | 132 private: |
| 129 MessageLoop loop_; | 133 MessageLoop loop_; |
| 134 |
| 135 ChromeThread mock_ui_thread_; |
| 136 ChromeThread mock_file_thread_; |
| 130 }; | 137 }; |
| 131 | 138 |
| 132 // Returns true if |blacklist| contains a match for |url|. | 139 // Returns true if |blacklist| contains a match for |url|. |
| 133 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { | 140 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { |
| 134 Blacklist::Match* match = blacklist->findMatch(GURL(url)); | 141 Blacklist::Match* match = blacklist->findMatch(GURL(url)); |
| 135 | 142 |
| 136 if (!match) | 143 if (!match) |
| 137 return false; | 144 return false; |
| 138 | 145 |
| 139 delete match; | 146 delete match; |
| 140 return true; | 147 return true; |
| 141 } | 148 } |
| 142 | 149 |
| 143 TEST_F(BlacklistManagerTest, Basic) { | 150 TEST_F(BlacklistManagerTest, Basic) { |
| 144 scoped_refptr<BlacklistManager> manager( | 151 scoped_refptr<BlacklistManager> manager( |
| 145 new BlacklistManager(&profile_, &path_provider_, NULL)); | 152 new BlacklistManager(&profile_, &path_provider_)); |
| 146 WaitForBlacklistUpdate(); | 153 WaitForBlacklistUpdate(); |
| 147 | 154 |
| 148 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 155 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 149 EXPECT_TRUE(blacklist); | 156 EXPECT_TRUE(blacklist); |
| 150 | 157 |
| 151 // Repeated invocations of GetCompiledBlacklist should return the same object. | 158 // Repeated invocations of GetCompiledBlacklist should return the same object. |
| 152 EXPECT_EQ(blacklist, manager->GetCompiledBlacklist()); | 159 EXPECT_EQ(blacklist, manager->GetCompiledBlacklist()); |
| 153 } | 160 } |
| 154 | 161 |
| 155 TEST_F(BlacklistManagerTest, BlacklistPathProvider) { | 162 TEST_F(BlacklistManagerTest, BlacklistPathProvider) { |
| 156 scoped_refptr<BlacklistManager> manager( | 163 scoped_refptr<BlacklistManager> manager( |
| 157 new BlacklistManager(&profile_, &path_provider_, NULL)); | 164 new BlacklistManager(&profile_, &path_provider_)); |
| 158 WaitForBlacklistUpdate(); | 165 WaitForBlacklistUpdate(); |
| 159 | 166 |
| 160 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); | 167 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); |
| 161 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | 168 EXPECT_FALSE(BlacklistHasMatch(blacklist1, |
| 162 "http://host/annoying_ads/ad.jpg")); | 169 "http://host/annoying_ads/ad.jpg")); |
| 163 | 170 |
| 164 path_provider_.AddPersistentPath( | 171 path_provider_.AddPersistentPath( |
| 165 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 172 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
| 166 WaitForBlacklistUpdate(); | 173 WaitForBlacklistUpdate(); |
| 167 | 174 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 181 // not checking for inequality. | 188 // not checking for inequality. |
| 182 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/annoying_ads/ad.jpg")); | 189 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/annoying_ads/ad.jpg")); |
| 183 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/other_ads/ad.jpg")); | 190 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/other_ads/ad.jpg")); |
| 184 | 191 |
| 185 // Now make sure that transient blacklists don't survive after re-creating | 192 // Now make sure that transient blacklists don't survive after re-creating |
| 186 // the BlacklistManager. | 193 // the BlacklistManager. |
| 187 manager = NULL; | 194 manager = NULL; |
| 188 path_provider_.clear(); | 195 path_provider_.clear(); |
| 189 path_provider_.AddPersistentPath( | 196 path_provider_.AddPersistentPath( |
| 190 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 197 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
| 191 manager = new BlacklistManager(&profile_, &path_provider_, NULL); | 198 manager = new BlacklistManager(&profile_, &path_provider_); |
| 192 WaitForBlacklistUpdate(); | 199 WaitForBlacklistUpdate(); |
| 193 | 200 |
| 194 const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); | 201 const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); |
| 195 | 202 |
| 196 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/annoying_ads/ad.jpg")); | 203 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/annoying_ads/ad.jpg")); |
| 197 EXPECT_FALSE(BlacklistHasMatch(blacklist4, "http://host/other_ads/ad.jpg")); | 204 EXPECT_FALSE(BlacklistHasMatch(blacklist4, "http://host/other_ads/ad.jpg")); |
| 198 } | 205 } |
| 199 | 206 |
| 200 TEST_F(BlacklistManagerTest, RealThread) { | |
| 201 base::Thread backend_thread("backend_thread"); | |
| 202 backend_thread.Start(); | |
| 203 | |
| 204 scoped_refptr<BlacklistManager> manager( | |
| 205 new BlacklistManager(&profile_, &path_provider_, &backend_thread)); | |
| 206 WaitForBlacklistUpdate(); | |
| 207 | |
| 208 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); | |
| 209 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | |
| 210 "http://host/annoying_ads/ad.jpg")); | |
| 211 | |
| 212 path_provider_.AddPersistentPath( | |
| 213 test_data_dir_.AppendASCII("annoying_ads.pbl")); | |
| 214 WaitForBlacklistUpdate(); | |
| 215 | |
| 216 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); | |
| 217 | |
| 218 // Added a real blacklist, the manager should recompile. | |
| 219 EXPECT_NE(blacklist1, blacklist2); | |
| 220 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); | |
| 221 } | |
| 222 | |
| 223 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { | 207 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { |
| 224 scoped_refptr<BlacklistManager> manager( | 208 scoped_refptr<BlacklistManager> manager( |
| 225 new BlacklistManager(&profile_, &path_provider_, NULL)); | 209 new BlacklistManager(&profile_, &path_provider_)); |
| 226 WaitForBlacklistUpdate(); | 210 WaitForBlacklistUpdate(); |
| 227 | 211 |
| 228 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); | 212 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); |
| 229 ASSERT_FALSE(file_util::PathExists(bogus_path)); | 213 ASSERT_FALSE(file_util::PathExists(bogus_path)); |
| 230 path_provider_.AddPersistentPath(bogus_path); | 214 path_provider_.AddPersistentPath(bogus_path); |
| 231 WaitForBlacklistError(); | 215 WaitForBlacklistError(); |
| 232 | 216 |
| 233 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 217 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 234 EXPECT_TRUE(blacklist); | 218 EXPECT_TRUE(blacklist); |
| 235 } | 219 } |
| 236 | 220 |
| 237 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { | 221 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { |
| 238 FilePath compiled_blacklist_path; | 222 FilePath compiled_blacklist_path; |
| 239 | 223 |
| 240 { | 224 { |
| 241 scoped_refptr<BlacklistManager> manager( | 225 scoped_refptr<BlacklistManager> manager( |
| 242 new BlacklistManager(&profile_, &path_provider_, NULL)); | 226 new BlacklistManager(&profile_, &path_provider_)); |
| 243 WaitForBlacklistUpdate(); | 227 WaitForBlacklistUpdate(); |
| 244 | 228 |
| 245 path_provider_.AddPersistentPath( | 229 path_provider_.AddPersistentPath( |
| 246 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 230 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
| 247 WaitForBlacklistUpdate(); | 231 WaitForBlacklistUpdate(); |
| 248 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 232 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 249 EXPECT_TRUE(BlacklistHasMatch(blacklist, | 233 EXPECT_TRUE(BlacklistHasMatch(blacklist, |
| 250 "http://host/annoying_ads/ad.jpg")); | 234 "http://host/annoying_ads/ad.jpg")); |
| 251 | 235 |
| 252 compiled_blacklist_path = manager->compiled_blacklist_path(); | 236 compiled_blacklist_path = manager->compiled_blacklist_path(); |
| 253 } | 237 } |
| 254 | 238 |
| 255 ASSERT_TRUE(file_util::PathExists(compiled_blacklist_path)); | 239 ASSERT_TRUE(file_util::PathExists(compiled_blacklist_path)); |
| 256 ASSERT_TRUE(file_util::Delete(compiled_blacklist_path, false)); | 240 ASSERT_TRUE(file_util::Delete(compiled_blacklist_path, false)); |
| 257 | 241 |
| 258 { | 242 { |
| 259 scoped_refptr<BlacklistManager> manager( | 243 scoped_refptr<BlacklistManager> manager( |
| 260 new BlacklistManager(&profile_, &path_provider_, NULL)); | 244 new BlacklistManager(&profile_, &path_provider_)); |
| 261 WaitForBlacklistUpdate(); | 245 WaitForBlacklistUpdate(); |
| 262 | 246 |
| 263 // The manager should recompile the blacklist. | 247 // The manager should recompile the blacklist. |
| 264 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 248 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 265 EXPECT_TRUE(BlacklistHasMatch(blacklist, | 249 EXPECT_TRUE(BlacklistHasMatch(blacklist, |
| 266 "http://host/annoying_ads/ad.jpg")); | 250 "http://host/annoying_ads/ad.jpg")); |
| 267 } | 251 } |
| 268 } | 252 } |
| 269 | 253 |
| 270 } // namespace | 254 } // namespace |
| OLD | NEW |