Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: chrome/browser/privacy_blacklist/blacklist_manager_unittest.cc

Issue 164535: Create BlacklistManager, which will aggregate individual blacklists into one compiled one (Closed)
Patch Set: sync with trunk, fix little things Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/privacy_blacklist/blacklist_manager.h"
6
7 #include "base/message_loop.h"
8 #include "base/path_service.h"
9 #include "base/scoped_temp_dir.h"
10 #include "base/thread.h"
11 #include "chrome/browser/privacy_blacklist/blacklist.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/notification_service.h"
14 #include "chrome/test/testing_profile.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace {
18
19 class MyTestingProfile : public TestingProfile {
20 public:
21 MyTestingProfile() {
22 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
23 path_ = temp_dir_.path();
24 }
25
26 private:
27 ScopedTempDir temp_dir_;
28
29 DISALLOW_COPY_AND_ASSIGN(MyTestingProfile);
30 };
31
32 class TestBlacklistPathProvider : public BlacklistPathProvider {
33 public:
34 explicit TestBlacklistPathProvider(Profile* profile) : profile_(profile) {
35 }
36
37 virtual std::vector<FilePath> GetBlacklistPaths() {
38 return paths_;
39 }
40
41 void AddPath(const FilePath& path) {
42 paths_.push_back(path);
43 NotificationService::current()->Notify(
44 NotificationType::PRIVACY_BLACKLIST_PATH_PROVIDER_UPDATED,
45 Source<Profile>(profile_),
46 Details<BlacklistPathProvider>(this));
47 }
48
49 private:
50 Profile* profile_;
51
52 std::vector<FilePath> paths_;
53
54 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider);
55 };
56
57 class BlacklistManagerTest : public testing::Test {
58 public:
59 virtual void SetUp() {
60 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
61 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples");
62 }
63
64 virtual void TearDown() {
65 loop_.RunAllPending();
66 }
67
68 protected:
69 FilePath test_data_dir_;
70
71 MyTestingProfile profile_;
72
73 private:
74 MessageLoop loop_;
75 };
76
77 TEST_F(BlacklistManagerTest, Basic) {
78 scoped_refptr<BlacklistManager> manager(
79 new BlacklistManager(&profile_, NULL));
80
81 const Blacklist* blacklist = manager->GetCompiledBlacklist();
82
83 // We should get an empty, but valid object.
84 ASSERT_TRUE(blacklist);
85 EXPECT_TRUE(blacklist->is_good());
86
87 // Repeated invocations of GetCompiledBlacklist should return the same object.
88 EXPECT_EQ(blacklist, manager->GetCompiledBlacklist());
89 }
90
91 TEST_F(BlacklistManagerTest, BlacklistPathProvider) {
92 scoped_refptr<BlacklistManager> manager(
93 new BlacklistManager(&profile_, NULL));
94
95 const Blacklist* blacklist1 = manager->GetCompiledBlacklist();
96 EXPECT_FALSE(blacklist1->findMatch(GURL("http://host/annoying_ads/ad.jpg")));
97
98 TestBlacklistPathProvider provider(&profile_);
99 manager->RegisterBlacklistPathProvider(&provider);
100
101 // Blacklist should not get recompiled.
102 EXPECT_EQ(blacklist1, manager->GetCompiledBlacklist());
103
104 provider.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl"));
105
106 const Blacklist* blacklist2 = manager->GetCompiledBlacklist();
107
108 // Added a real blacklist, the manager should recompile.
109 EXPECT_NE(blacklist1, blacklist2);
110 EXPECT_TRUE(blacklist2->findMatch(GURL("http://host/annoying_ads/ad.jpg")));
111
112 manager->UnregisterBlacklistPathProvider(&provider);
113
114 // Just unregistering the provider doesn't remove the blacklist paths
115 // from the manager.
116 EXPECT_EQ(blacklist2, manager->GetCompiledBlacklist());
117 }
118
119 TEST_F(BlacklistManagerTest, RealThread) {
120 base::Thread backend_thread("backend_thread");
121 backend_thread.Start();
122
123 scoped_refptr<BlacklistManager> manager(
124 new BlacklistManager(&profile_, &backend_thread));
125
126 // Make sure all pending tasks run.
127 backend_thread.Stop();
128 backend_thread.Start();
129
130 const Blacklist* blacklist1 = manager->GetCompiledBlacklist();
131 EXPECT_FALSE(blacklist1->findMatch(GURL("http://host/annoying_ads/ad.jpg")));
132
133 TestBlacklistPathProvider provider(&profile_);
134 manager->RegisterBlacklistPathProvider(&provider);
135
136 // Make sure all pending tasks run.
137 backend_thread.Stop();
138 backend_thread.Start();
139
140 // Blacklist should not get recompiled.
141 EXPECT_EQ(blacklist1, manager->GetCompiledBlacklist());
142
143 provider.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl"));
144
145 // Make sure all pending tasks run.
146 backend_thread.Stop();
147 backend_thread.Start();
148
149 const Blacklist* blacklist2 = manager->GetCompiledBlacklist();
150
151 // Added a real blacklist, the manager should recompile.
152 EXPECT_NE(blacklist1, blacklist2);
153 EXPECT_TRUE(blacklist2->findMatch(GURL("http://host/annoying_ads/ad.jpg")));
154
155 manager->UnregisterBlacklistPathProvider(&provider);
156
157 // Make sure all pending tasks run.
158 backend_thread.Stop();
159 backend_thread.Start();
160
161 // Just unregistering the provider doesn't remove the blacklist paths
162 // from the manager.
163 EXPECT_EQ(blacklist2, manager->GetCompiledBlacklist());
164 }
165
166 TEST_F(BlacklistManagerTest, CompiledBlacklistStaysOnDisk) {
167 {
168 scoped_refptr<BlacklistManager> manager(
169 new BlacklistManager(&profile_, NULL));
170
171 TestBlacklistPathProvider provider(&profile_);
172 manager->RegisterBlacklistPathProvider(&provider);
173 provider.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl"));
174 const Blacklist* blacklist = manager->GetCompiledBlacklist();
175 EXPECT_TRUE(blacklist->findMatch(GURL("http://host/annoying_ads/ad.jpg")));
176 manager->UnregisterBlacklistPathProvider(&provider);
177 }
178
179 {
180 scoped_refptr<BlacklistManager> manager(
181 new BlacklistManager(&profile_, NULL));
182
183 // Make sure we read the compiled blacklist from disk and don't even touch
184 // the paths providers.
185 const Blacklist* blacklist = manager->GetCompiledBlacklist();
186 EXPECT_TRUE(blacklist->findMatch(GURL("http://host/annoying_ads/ad.jpg")));
187 }
188 }
189
190 TEST_F(BlacklistManagerTest, BlacklistPathReadError) {
191 scoped_refptr<BlacklistManager> manager(
192 new BlacklistManager(&profile_, NULL));
193
194 TestBlacklistPathProvider provider(&profile_);
195 manager->RegisterBlacklistPathProvider(&provider);
196
197 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness"));
198 ASSERT_FALSE(file_util::PathExists(bogus_path));
199 provider.AddPath(bogus_path);
200
201 const Blacklist* blacklist = manager->GetCompiledBlacklist();
202
203 // We should get an empty, but valid object.
204 ASSERT_TRUE(blacklist);
205 EXPECT_TRUE(blacklist->is_good());
206
207 manager->UnregisterBlacklistPathProvider(&provider);
208 }
209
210 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) {
211 FilePath compiled_blacklist_path;
212
213 {
214 scoped_refptr<BlacklistManager> manager(
215 new BlacklistManager(&profile_, NULL));
216
217 TestBlacklistPathProvider provider(&profile_);
218 manager->RegisterBlacklistPathProvider(&provider);
219 provider.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl"));
220 const Blacklist* blacklist = manager->GetCompiledBlacklist();
221 EXPECT_TRUE(blacklist->findMatch(GURL("http://host/annoying_ads/ad.jpg")));
222 manager->UnregisterBlacklistPathProvider(&provider);
223
224 compiled_blacklist_path = manager->compiled_blacklist_path();
225 }
226
227 ASSERT_TRUE(file_util::PathExists(compiled_blacklist_path));
228 ASSERT_TRUE(file_util::Delete(compiled_blacklist_path, false));
229
230 {
231 scoped_refptr<BlacklistManager> manager(
232 new BlacklistManager(&profile_, NULL));
233
234 // Now we don't have any providers, and no compiled blacklist file. We
235 // shouldn't match any URLs.
236 const Blacklist* blacklist = manager->GetCompiledBlacklist();
237 EXPECT_FALSE(blacklist->findMatch(GURL("http://host/annoying_ads/ad.jpg")));
238 }
239 }
240
241 TEST_F(BlacklistManagerTest, MultipleProviders) {
242 scoped_refptr<BlacklistManager> manager(
243 new BlacklistManager(&profile_, NULL));
244
245 TestBlacklistPathProvider provider1(&profile_);
246 TestBlacklistPathProvider provider2(&profile_);
247 manager->RegisterBlacklistPathProvider(&provider1);
248 manager->RegisterBlacklistPathProvider(&provider2);
249
250 const Blacklist* blacklist1 = manager->GetCompiledBlacklist();
251 EXPECT_FALSE(blacklist1->findMatch(GURL("http://sample/annoying_ads/a.jpg")));
252 EXPECT_FALSE(blacklist1->findMatch(GURL("http://sample/other_ads/a.jpg")));
253 EXPECT_FALSE(blacklist1->findMatch(GURL("http://host/something.doc")));
254
255 provider1.AddPath(test_data_dir_.AppendASCII("annoying_ads.pbl"));
256 const Blacklist* blacklist2 = manager->GetCompiledBlacklist();
257 EXPECT_NE(blacklist1, blacklist2);
258
259 provider2.AddPath(test_data_dir_.AppendASCII("host.pbl"));
260 const Blacklist* blacklist3 = manager->GetCompiledBlacklist();
261 EXPECT_NE(blacklist2, blacklist3);
262
263 EXPECT_TRUE(blacklist3->findMatch(GURL("http://sample/annoying_ads/a.jpg")));
264 EXPECT_FALSE(blacklist3->findMatch(GURL("http://sample/other_ads/a.jpg")));
265 EXPECT_TRUE(blacklist3->findMatch(GURL("http://host/something.doc")));
266
267 provider1.AddPath(test_data_dir_.AppendASCII("other_ads.pbl"));
268
269 const Blacklist* blacklist4 = manager->GetCompiledBlacklist();
270
271 EXPECT_NE(blacklist3, blacklist4);
272 EXPECT_TRUE(blacklist4->findMatch(GURL("http://sample/annoying_ads/a.jpg")));
273 EXPECT_TRUE(blacklist4->findMatch(GURL("http://sample/other_ads/a.jpg")));
274 EXPECT_TRUE(blacklist4->findMatch(GURL("http://host/something.doc")));
275
276 manager->UnregisterBlacklistPathProvider(&provider1);
277 manager->UnregisterBlacklistPathProvider(&provider2);
278 }
279
280 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698