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 16 matching lines...) Expand all Loading... |
27 ScopedTempDir temp_dir_; | 27 ScopedTempDir temp_dir_; |
28 | 28 |
29 DISALLOW_COPY_AND_ASSIGN(MyTestingProfile); | 29 DISALLOW_COPY_AND_ASSIGN(MyTestingProfile); |
30 }; | 30 }; |
31 | 31 |
32 class TestBlacklistPathProvider : public BlacklistPathProvider { | 32 class TestBlacklistPathProvider : public BlacklistPathProvider { |
33 public: | 33 public: |
34 explicit TestBlacklistPathProvider(Profile* profile) : profile_(profile) { | 34 explicit TestBlacklistPathProvider(Profile* profile) : profile_(profile) { |
35 } | 35 } |
36 | 36 |
37 virtual std::vector<FilePath> GetBlacklistPaths() { | 37 virtual std::vector<FilePath> GetPersistentBlacklistPaths() { |
38 return paths_; | 38 return persistent_paths_; |
| 39 } |
| 40 |
| 41 virtual std::vector<FilePath> GetTransientBlacklistPaths() { |
| 42 return transient_paths_; |
39 } | 43 } |
40 | 44 |
41 void AddPath(const FilePath& path) { | 45 void AddPersistentPath(const FilePath& path) { |
42 paths_.push_back(path); | 46 persistent_paths_.push_back(path); |
| 47 SendUpdateNotification(); |
| 48 } |
| 49 |
| 50 void AddTransientPath(const FilePath& path) { |
| 51 transient_paths_.push_back(path); |
| 52 SendUpdateNotification(); |
| 53 } |
| 54 |
| 55 void clear() { |
| 56 persistent_paths_.clear(); |
| 57 transient_paths_.clear(); |
| 58 SendUpdateNotification(); |
| 59 } |
| 60 |
| 61 private: |
| 62 void SendUpdateNotification() { |
43 NotificationService::current()->Notify( | 63 NotificationService::current()->Notify( |
44 NotificationType::PRIVACY_BLACKLIST_PATH_PROVIDER_UPDATED, | 64 NotificationType::BLACKLIST_PATH_PROVIDER_UPDATED, |
45 Source<Profile>(profile_), | 65 Source<Profile>(profile_), |
46 Details<BlacklistPathProvider>(this)); | 66 Details<BlacklistPathProvider>(this)); |
47 } | 67 } |
48 | 68 |
49 private: | |
50 Profile* profile_; | 69 Profile* profile_; |
51 | 70 |
52 std::vector<FilePath> paths_; | 71 std::vector<FilePath> persistent_paths_; |
| 72 std::vector<FilePath> transient_paths_; |
53 | 73 |
54 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); | 74 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); |
55 }; | 75 }; |
56 | 76 |
57 class BlacklistManagerTest : public testing::Test { | 77 class BlacklistManagerTest : public testing::Test, public NotificationObserver { |
58 public: | 78 public: |
| 79 BlacklistManagerTest() : path_provider_(&profile_) { |
| 80 } |
| 81 |
59 virtual void SetUp() { | 82 virtual void SetUp() { |
60 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); | 83 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); |
61 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); | 84 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); |
62 } | 85 } |
63 | 86 |
64 virtual void TearDown() { | 87 virtual void TearDown() { |
65 loop_.RunAllPending(); | 88 loop_.RunAllPending(); |
66 } | 89 } |
67 | 90 |
| 91 // NotificationObserver |
| 92 virtual void Observe(NotificationType type, |
| 93 const NotificationSource& source, |
| 94 const NotificationDetails& details) { |
| 95 MessageLoop::current()->Quit(); |
| 96 } |
| 97 |
68 protected: | 98 protected: |
| 99 void WaitForBlacklistError() { |
| 100 NotificationRegistrar registrar; |
| 101 registrar.Add(this, |
| 102 NotificationType::BLACKLIST_MANAGER_ERROR, |
| 103 Source<Profile>(&profile_)); |
| 104 MessageLoop::current()->Run(); |
| 105 } |
| 106 |
| 107 void WaitForBlacklistUpdate() { |
| 108 NotificationRegistrar registrar; |
| 109 registrar.Add(this, |
| 110 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, |
| 111 Source<Profile>(&profile_)); |
| 112 MessageLoop::current()->Run(); |
| 113 } |
| 114 |
69 FilePath test_data_dir_; | 115 FilePath test_data_dir_; |
70 | 116 |
71 MyTestingProfile profile_; | 117 MyTestingProfile profile_; |
| 118 |
| 119 TestBlacklistPathProvider path_provider_; |
72 | 120 |
73 private: | 121 private: |
74 MessageLoop loop_; | 122 MessageLoop loop_; |
75 }; | 123 }; |
76 | 124 |
77 // Returns true if |blacklist| contains a match for |url|. | 125 // Returns true if |blacklist| contains a match for |url|. |
78 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { | 126 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { |
79 Blacklist::Match* match = blacklist->findMatch(GURL(url)); | 127 Blacklist::Match* match = blacklist->findMatch(GURL(url)); |
80 | 128 |
81 if (!match) | 129 if (!match) |
82 return false; | 130 return false; |
83 | 131 |
84 delete match; | 132 delete match; |
85 return true; | 133 return true; |
86 } | 134 } |
87 | 135 |
88 TEST_F(BlacklistManagerTest, Basic) { | 136 TEST_F(BlacklistManagerTest, Basic) { |
89 scoped_refptr<BlacklistManager> manager( | 137 scoped_refptr<BlacklistManager> manager( |
90 new BlacklistManager(&profile_, NULL)); | 138 new BlacklistManager(&profile_, &path_provider_, NULL)); |
| 139 WaitForBlacklistUpdate(); |
91 | 140 |
92 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 141 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
93 | 142 EXPECT_TRUE(blacklist); |
94 // We should get an empty, but valid object. | |
95 ASSERT_TRUE(blacklist); | |
96 EXPECT_TRUE(blacklist->is_good()); | |
97 | 143 |
98 // Repeated invocations of GetCompiledBlacklist should return the same object. | 144 // Repeated invocations of GetCompiledBlacklist should return the same object. |
99 EXPECT_EQ(blacklist, manager->GetCompiledBlacklist()); | 145 EXPECT_EQ(blacklist, manager->GetCompiledBlacklist()); |
100 } | 146 } |
101 | 147 |
102 TEST_F(BlacklistManagerTest, BlacklistPathProvider) { | 148 TEST_F(BlacklistManagerTest, BlacklistPathProvider) { |
103 scoped_refptr<BlacklistManager> manager( | 149 scoped_refptr<BlacklistManager> manager( |
104 new BlacklistManager(&profile_, NULL)); | 150 new BlacklistManager(&profile_, &path_provider_, NULL)); |
| 151 WaitForBlacklistUpdate(); |
105 | 152 |
106 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); | 153 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); |
107 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | 154 EXPECT_FALSE(BlacklistHasMatch(blacklist1, |
108 "http://host/annoying_ads/ad.jpg")); | 155 "http://host/annoying_ads/ad.jpg")); |
109 | 156 |
110 TestBlacklistPathProvider provider(&profile_); | 157 path_provider_.AddPersistentPath( |
111 manager->RegisterBlacklistPathProvider(&provider); | 158 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
112 | 159 WaitForBlacklistUpdate(); |
113 // Blacklist should not get recompiled. | 160 |
114 EXPECT_EQ(blacklist1, manager->GetCompiledBlacklist()); | |
115 | |
116 provider.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl")); | |
117 | |
118 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); | 161 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); |
119 | 162 |
120 // Added a real blacklist, the manager should recompile. | 163 // Added a real blacklist, the manager should recompile. |
121 EXPECT_NE(blacklist1, blacklist2); | 164 EXPECT_NE(blacklist1, blacklist2); |
122 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); | 165 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); |
123 | 166 EXPECT_FALSE(BlacklistHasMatch(blacklist2, "http://host/other_ads/ad.jpg")); |
124 manager->UnregisterBlacklistPathProvider(&provider); | 167 |
125 | 168 path_provider_.AddTransientPath(test_data_dir_.AppendASCII("other_ads.pbl")); |
126 // Just unregistering the provider doesn't remove the blacklist paths | 169 WaitForBlacklistUpdate(); |
127 // from the manager. | 170 |
128 EXPECT_EQ(blacklist2, manager->GetCompiledBlacklist()); | 171 const Blacklist* blacklist3 = manager->GetCompiledBlacklist(); |
| 172 |
| 173 // In theory blacklist2 and blacklist3 could be the same object, so we're |
| 174 // not checking for inequality. |
| 175 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/annoying_ads/ad.jpg")); |
| 176 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/other_ads/ad.jpg")); |
| 177 |
| 178 // Now make sure that transient blacklists don't survive after re-creating |
| 179 // the BlacklistManager. |
| 180 manager = NULL; |
| 181 path_provider_.clear(); |
| 182 path_provider_.AddPersistentPath( |
| 183 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
| 184 manager = new BlacklistManager(&profile_, &path_provider_, NULL); |
| 185 WaitForBlacklistUpdate(); |
| 186 |
| 187 const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); |
| 188 |
| 189 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/annoying_ads/ad.jpg")); |
| 190 EXPECT_FALSE(BlacklistHasMatch(blacklist4, "http://host/other_ads/ad.jpg")); |
129 } | 191 } |
130 | 192 |
131 TEST_F(BlacklistManagerTest, RealThread) { | 193 TEST_F(BlacklistManagerTest, RealThread) { |
132 base::Thread backend_thread("backend_thread"); | 194 base::Thread backend_thread("backend_thread"); |
133 backend_thread.Start(); | 195 backend_thread.Start(); |
134 | 196 |
135 scoped_refptr<BlacklistManager> manager( | 197 scoped_refptr<BlacklistManager> manager( |
136 new BlacklistManager(&profile_, &backend_thread)); | 198 new BlacklistManager(&profile_, &path_provider_, &backend_thread)); |
137 | 199 WaitForBlacklistUpdate(); |
138 // Make sure all pending tasks run. | |
139 backend_thread.Stop(); | |
140 backend_thread.Start(); | |
141 | 200 |
142 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); | 201 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); |
143 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | 202 EXPECT_FALSE(BlacklistHasMatch(blacklist1, |
144 "http://host/annoying_ads/ad.jpg")); | 203 "http://host/annoying_ads/ad.jpg")); |
145 | 204 |
146 TestBlacklistPathProvider provider(&profile_); | 205 path_provider_.AddPersistentPath( |
147 manager->RegisterBlacklistPathProvider(&provider); | 206 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
148 | 207 WaitForBlacklistUpdate(); |
149 // Make sure all pending tasks run. | |
150 backend_thread.Stop(); | |
151 backend_thread.Start(); | |
152 | |
153 // Blacklist should not get recompiled. | |
154 EXPECT_EQ(blacklist1, manager->GetCompiledBlacklist()); | |
155 | |
156 provider.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl")); | |
157 | |
158 // Make sure all pending tasks run. | |
159 backend_thread.Stop(); | |
160 backend_thread.Start(); | |
161 | 208 |
162 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); | 209 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); |
163 | 210 |
164 // Added a real blacklist, the manager should recompile. | 211 // Added a real blacklist, the manager should recompile. |
165 EXPECT_NE(blacklist1, blacklist2); | 212 EXPECT_NE(blacklist1, blacklist2); |
166 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); | 213 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); |
167 | |
168 manager->UnregisterBlacklistPathProvider(&provider); | |
169 | |
170 // Make sure all pending tasks run. | |
171 backend_thread.Stop(); | |
172 backend_thread.Start(); | |
173 | |
174 // Just unregistering the provider doesn't remove the blacklist paths | |
175 // from the manager. | |
176 EXPECT_EQ(blacklist2, manager->GetCompiledBlacklist()); | |
177 } | |
178 | |
179 TEST_F(BlacklistManagerTest, CompiledBlacklistStaysOnDisk) { | |
180 { | |
181 scoped_refptr<BlacklistManager> manager( | |
182 new BlacklistManager(&profile_, NULL)); | |
183 | |
184 TestBlacklistPathProvider provider(&profile_); | |
185 manager->RegisterBlacklistPathProvider(&provider); | |
186 provider.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl")); | |
187 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | |
188 EXPECT_TRUE(BlacklistHasMatch(blacklist, | |
189 "http://host/annoying_ads/ad.jpg")); | |
190 manager->UnregisterBlacklistPathProvider(&provider); | |
191 } | |
192 | |
193 { | |
194 scoped_refptr<BlacklistManager> manager( | |
195 new BlacklistManager(&profile_, NULL)); | |
196 | |
197 // Make sure we read the compiled blacklist from disk and don't even touch | |
198 // the paths providers. | |
199 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | |
200 EXPECT_TRUE(BlacklistHasMatch(blacklist, | |
201 "http://host/annoying_ads/ad.jpg")); | |
202 } | |
203 } | 214 } |
204 | 215 |
205 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { | 216 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { |
206 scoped_refptr<BlacklistManager> manager( | 217 scoped_refptr<BlacklistManager> manager( |
207 new BlacklistManager(&profile_, NULL)); | 218 new BlacklistManager(&profile_, &path_provider_, NULL)); |
208 | 219 WaitForBlacklistUpdate(); |
209 TestBlacklistPathProvider provider(&profile_); | |
210 manager->RegisterBlacklistPathProvider(&provider); | |
211 | 220 |
212 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); | 221 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); |
213 ASSERT_FALSE(file_util::PathExists(bogus_path)); | 222 ASSERT_FALSE(file_util::PathExists(bogus_path)); |
214 provider.AddPath(bogus_path); | 223 path_provider_.AddPersistentPath(bogus_path); |
215 | 224 WaitForBlacklistError(); |
| 225 |
216 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 226 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
217 | 227 EXPECT_TRUE(blacklist); |
218 // We should get an empty, but valid object. | |
219 ASSERT_TRUE(blacklist); | |
220 EXPECT_TRUE(blacklist->is_good()); | |
221 | |
222 manager->UnregisterBlacklistPathProvider(&provider); | |
223 } | 228 } |
224 | 229 |
225 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { | 230 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { |
226 FilePath compiled_blacklist_path; | 231 FilePath compiled_blacklist_path; |
227 | 232 |
228 { | 233 { |
229 scoped_refptr<BlacklistManager> manager( | 234 scoped_refptr<BlacklistManager> manager( |
230 new BlacklistManager(&profile_, NULL)); | 235 new BlacklistManager(&profile_, &path_provider_, NULL)); |
| 236 WaitForBlacklistUpdate(); |
231 | 237 |
232 TestBlacklistPathProvider provider(&profile_); | 238 path_provider_.AddPersistentPath( |
233 manager->RegisterBlacklistPathProvider(&provider); | 239 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
234 provider.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl")); | 240 WaitForBlacklistUpdate(); |
235 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 241 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
236 EXPECT_TRUE(BlacklistHasMatch(blacklist, | 242 EXPECT_TRUE(BlacklistHasMatch(blacklist, |
237 "http://host/annoying_ads/ad.jpg")); | 243 "http://host/annoying_ads/ad.jpg")); |
238 manager->UnregisterBlacklistPathProvider(&provider); | |
239 | 244 |
240 compiled_blacklist_path = manager->compiled_blacklist_path(); | 245 compiled_blacklist_path = manager->compiled_blacklist_path(); |
241 } | 246 } |
242 | 247 |
243 ASSERT_TRUE(file_util::PathExists(compiled_blacklist_path)); | 248 ASSERT_TRUE(file_util::PathExists(compiled_blacklist_path)); |
244 ASSERT_TRUE(file_util::Delete(compiled_blacklist_path, false)); | 249 ASSERT_TRUE(file_util::Delete(compiled_blacklist_path, false)); |
245 | 250 |
246 { | 251 { |
247 scoped_refptr<BlacklistManager> manager( | 252 scoped_refptr<BlacklistManager> manager( |
248 new BlacklistManager(&profile_, NULL)); | 253 new BlacklistManager(&profile_, &path_provider_, NULL)); |
| 254 WaitForBlacklistUpdate(); |
249 | 255 |
250 // Now we don't have any providers, and no compiled blacklist file. We | 256 // The manager should recompile the blacklist. |
251 // shouldn't match any URLs. | |
252 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 257 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
253 EXPECT_FALSE(BlacklistHasMatch(blacklist, | 258 EXPECT_TRUE(BlacklistHasMatch(blacklist, |
254 "http://host/annoying_ads/ad.jpg")); | 259 "http://host/annoying_ads/ad.jpg")); |
255 } | 260 } |
256 } | 261 } |
257 | 262 |
258 TEST_F(BlacklistManagerTest, MultipleProviders) { | |
259 scoped_refptr<BlacklistManager> manager( | |
260 new BlacklistManager(&profile_, NULL)); | |
261 | |
262 TestBlacklistPathProvider provider1(&profile_); | |
263 TestBlacklistPathProvider provider2(&profile_); | |
264 manager->RegisterBlacklistPathProvider(&provider1); | |
265 manager->RegisterBlacklistPathProvider(&provider2); | |
266 | |
267 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); | |
268 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | |
269 "http://sample/annoying_ads/a.jpg")); | |
270 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | |
271 "http://sample/other_ads/a.jpg")); | |
272 EXPECT_FALSE(BlacklistHasMatch(blacklist1, "http://host/something.doc")); | |
273 | |
274 provider1.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl")); | |
275 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); | |
276 EXPECT_NE(blacklist1, blacklist2); | |
277 | |
278 provider2.AddPath(test_data_dir_.AppendASCII("host.pbl")); | |
279 const Blacklist* blacklist3 = manager->GetCompiledBlacklist(); | |
280 EXPECT_NE(blacklist2, blacklist3); | |
281 | |
282 EXPECT_TRUE(BlacklistHasMatch(blacklist3, | |
283 "http://sample/annoying_ads/a.jpg")); | |
284 EXPECT_FALSE(BlacklistHasMatch(blacklist3, "http://sample/other_ads/a.jpg")); | |
285 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/something.doc")); | |
286 | |
287 provider1.AddPath(test_data_dir_.AppendASCII("other_ads.pbl")); | |
288 | |
289 const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); | |
290 | |
291 EXPECT_NE(blacklist3, blacklist4); | |
292 EXPECT_TRUE(BlacklistHasMatch(blacklist4, | |
293 "http://sample/annoying_ads/a.jpg")); | |
294 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://sample/other_ads/a.jpg")); | |
295 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/something.doc")); | |
296 | |
297 manager->UnregisterBlacklistPathProvider(&provider1); | |
298 manager->UnregisterBlacklistPathProvider(&provider2); | |
299 } | |
300 | |
301 } // namespace | 263 } // namespace |
OLD | NEW |