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 "chrome/browser/privacy_blacklist/blacklist.h" | 11 #include "chrome/browser/privacy_blacklist/blacklist.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/extensions/extension.h" |
13 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
14 #include "chrome/test/testing_profile.h" | 15 #include "chrome/test/testing_profile.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 class MyTestingProfile : public TestingProfile { | 20 class MyTestingProfile : public TestingProfile { |
20 public: | 21 public: |
21 MyTestingProfile() { | 22 MyTestingProfile() { |
22 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 23 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
23 path_ = temp_dir_.path(); | 24 path_ = temp_dir_.path(); |
24 } | 25 } |
25 | 26 |
26 private: | 27 private: |
27 ScopedTempDir temp_dir_; | 28 ScopedTempDir temp_dir_; |
28 | 29 |
29 DISALLOW_COPY_AND_ASSIGN(MyTestingProfile); | 30 DISALLOW_COPY_AND_ASSIGN(MyTestingProfile); |
30 }; | 31 }; |
31 | 32 |
32 class TestBlacklistPathProvider : public BlacklistPathProvider { | 33 class TestBlacklistPathProvider : public BlacklistPathProvider { |
33 public: | 34 public: |
34 explicit TestBlacklistPathProvider(Profile* profile) : profile_(profile) { | 35 explicit TestBlacklistPathProvider(Profile* profile) : profile_(profile) { |
35 } | 36 } |
36 | 37 |
37 virtual std::vector<FilePath> GetPersistentBlacklistPaths() { | 38 virtual std::vector<FilePath> GetPersistentBlacklistPaths() { |
38 return persistent_paths_; | 39 return persistent_paths_; |
39 } | 40 } |
40 | 41 |
41 virtual std::vector<FilePath> GetTransientBlacklistPaths() { | 42 virtual std::vector<FilePath> GetTransientBlacklistPaths() { |
42 return transient_paths_; | 43 return transient_paths_; |
43 } | 44 } |
44 | 45 |
45 void AddPersistentPath(const FilePath& path) { | 46 void AddPersistentPath(const FilePath& path) { |
46 persistent_paths_.push_back(path); | 47 persistent_paths_.push_back(path); |
47 SendUpdateNotification(); | 48 SendUpdateNotification(); |
48 } | 49 } |
49 | 50 |
50 void AddTransientPath(const FilePath& path) { | 51 void AddTransientPath(const FilePath& path) { |
51 transient_paths_.push_back(path); | 52 transient_paths_.push_back(path); |
52 SendUpdateNotification(); | 53 SendUpdateNotification(); |
53 } | 54 } |
54 | 55 |
55 void clear() { | 56 void clear() { |
56 persistent_paths_.clear(); | 57 persistent_paths_.clear(); |
57 transient_paths_.clear(); | 58 transient_paths_.clear(); |
58 SendUpdateNotification(); | 59 SendUpdateNotification(); |
59 } | 60 } |
60 | 61 |
61 private: | 62 private: |
62 void SendUpdateNotification() { | 63 void SendUpdateNotification() { |
| 64 #if defined(OS_WIN) |
| 65 FilePath path(FILE_PATH_LITERAL("c:\\foo")); |
| 66 #elif defined(OS_POSIX) |
| 67 FilePath path(FILE_PATH_LITERAL("/foo")); |
| 68 #endif |
| 69 Extension extension(path); |
63 NotificationService::current()->Notify( | 70 NotificationService::current()->Notify( |
64 NotificationType::BLACKLIST_PATH_PROVIDER_UPDATED, | 71 NotificationType::EXTENSION_LOADED, |
65 Source<Profile>(profile_), | 72 Source<Profile>(profile_), |
66 Details<BlacklistPathProvider>(this)); | 73 Details<Extension>(&extension)); |
67 } | 74 } |
68 | 75 |
69 Profile* profile_; | 76 Profile* profile_; |
70 | 77 |
71 std::vector<FilePath> persistent_paths_; | 78 std::vector<FilePath> persistent_paths_; |
72 std::vector<FilePath> transient_paths_; | 79 std::vector<FilePath> transient_paths_; |
73 | 80 |
74 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); | 81 DISALLOW_COPY_AND_ASSIGN(TestBlacklistPathProvider); |
75 }; | 82 }; |
76 | 83 |
77 class BlacklistManagerTest : public testing::Test, public NotificationObserver { | 84 class BlacklistManagerTest : public testing::Test, public NotificationObserver { |
78 public: | 85 public: |
79 BlacklistManagerTest() : path_provider_(&profile_) { | 86 BlacklistManagerTest() : path_provider_(&profile_) { |
80 } | 87 } |
81 | 88 |
82 virtual void SetUp() { | 89 virtual void SetUp() { |
83 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); | 90 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); |
84 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); | 91 test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); |
85 } | 92 } |
86 | 93 |
87 virtual void TearDown() { | 94 virtual void TearDown() { |
88 loop_.RunAllPending(); | 95 loop_.RunAllPending(); |
89 } | 96 } |
90 | 97 |
91 // NotificationObserver | 98 // NotificationObserver |
92 virtual void Observe(NotificationType type, | 99 virtual void Observe(NotificationType type, |
93 const NotificationSource& source, | 100 const NotificationSource& source, |
94 const NotificationDetails& details) { | 101 const NotificationDetails& details) { |
95 MessageLoop::current()->Quit(); | 102 MessageLoop::current()->Quit(); |
96 } | 103 } |
97 | 104 |
98 protected: | 105 protected: |
99 void WaitForBlacklistError() { | 106 void WaitForBlacklistError() { |
100 NotificationRegistrar registrar; | 107 NotificationRegistrar registrar; |
101 registrar.Add(this, | 108 registrar.Add(this, |
102 NotificationType::BLACKLIST_MANAGER_ERROR, | 109 NotificationType::BLACKLIST_MANAGER_ERROR, |
103 Source<Profile>(&profile_)); | 110 Source<Profile>(&profile_)); |
104 MessageLoop::current()->Run(); | 111 MessageLoop::current()->Run(); |
105 } | 112 } |
106 | 113 |
107 void WaitForBlacklistUpdate() { | 114 void WaitForBlacklistUpdate() { |
108 NotificationRegistrar registrar; | 115 NotificationRegistrar registrar; |
109 registrar.Add(this, | 116 registrar.Add(this, |
110 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, | 117 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, |
111 Source<Profile>(&profile_)); | 118 Source<Profile>(&profile_)); |
112 MessageLoop::current()->Run(); | 119 MessageLoop::current()->Run(); |
113 } | 120 } |
114 | 121 |
115 FilePath test_data_dir_; | 122 FilePath test_data_dir_; |
116 | 123 |
117 MyTestingProfile profile_; | 124 MyTestingProfile profile_; |
118 | 125 |
119 TestBlacklistPathProvider path_provider_; | 126 TestBlacklistPathProvider path_provider_; |
120 | 127 |
121 private: | 128 private: |
122 MessageLoop loop_; | 129 MessageLoop loop_; |
123 }; | 130 }; |
124 | 131 |
125 // Returns true if |blacklist| contains a match for |url|. | 132 // Returns true if |blacklist| contains a match for |url|. |
126 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { | 133 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { |
127 Blacklist::Match* match = blacklist->findMatch(GURL(url)); | 134 Blacklist::Match* match = blacklist->findMatch(GURL(url)); |
128 | 135 |
(...skipping 21 matching lines...) Expand all Loading... |
150 new BlacklistManager(&profile_, &path_provider_, NULL)); | 157 new BlacklistManager(&profile_, &path_provider_, NULL)); |
151 WaitForBlacklistUpdate(); | 158 WaitForBlacklistUpdate(); |
152 | 159 |
153 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); | 160 const Blacklist* blacklist1 = manager->GetCompiledBlacklist(); |
154 EXPECT_FALSE(BlacklistHasMatch(blacklist1, | 161 EXPECT_FALSE(BlacklistHasMatch(blacklist1, |
155 "http://host/annoying_ads/ad.jpg")); | 162 "http://host/annoying_ads/ad.jpg")); |
156 | 163 |
157 path_provider_.AddPersistentPath( | 164 path_provider_.AddPersistentPath( |
158 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 165 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
159 WaitForBlacklistUpdate(); | 166 WaitForBlacklistUpdate(); |
160 | 167 |
161 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); | 168 const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); |
162 | 169 |
163 // Added a real blacklist, the manager should recompile. | 170 // Added a real blacklist, the manager should recompile. |
164 EXPECT_NE(blacklist1, blacklist2); | 171 EXPECT_NE(blacklist1, blacklist2); |
165 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); | 172 EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); |
166 EXPECT_FALSE(BlacklistHasMatch(blacklist2, "http://host/other_ads/ad.jpg")); | 173 EXPECT_FALSE(BlacklistHasMatch(blacklist2, "http://host/other_ads/ad.jpg")); |
167 | 174 |
168 path_provider_.AddTransientPath(test_data_dir_.AppendASCII("other_ads.pbl")); | 175 path_provider_.AddTransientPath(test_data_dir_.AppendASCII("other_ads.pbl")); |
169 WaitForBlacklistUpdate(); | 176 WaitForBlacklistUpdate(); |
170 | 177 |
171 const Blacklist* blacklist3 = manager->GetCompiledBlacklist(); | 178 const Blacklist* blacklist3 = manager->GetCompiledBlacklist(); |
172 | 179 |
173 // In theory blacklist2 and blacklist3 could be the same object, so we're | 180 // In theory blacklist2 and blacklist3 could be the same object, so we're |
174 // not checking for inequality. | 181 // not checking for inequality. |
175 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/annoying_ads/ad.jpg")); | 182 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/annoying_ads/ad.jpg")); |
176 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/other_ads/ad.jpg")); | 183 EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/other_ads/ad.jpg")); |
177 | 184 |
178 // Now make sure that transient blacklists don't survive after re-creating | 185 // Now make sure that transient blacklists don't survive after re-creating |
179 // the BlacklistManager. | 186 // the BlacklistManager. |
180 manager = NULL; | 187 manager = NULL; |
181 path_provider_.clear(); | 188 path_provider_.clear(); |
182 path_provider_.AddPersistentPath( | 189 path_provider_.AddPersistentPath( |
183 test_data_dir_.AppendASCII("annoying_ads.pbl")); | 190 test_data_dir_.AppendASCII("annoying_ads.pbl")); |
184 manager = new BlacklistManager(&profile_, &path_provider_, NULL); | 191 manager = new BlacklistManager(&profile_, &path_provider_, NULL); |
185 WaitForBlacklistUpdate(); | 192 WaitForBlacklistUpdate(); |
186 | 193 |
187 const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); | 194 const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); |
188 | 195 |
189 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/annoying_ads/ad.jpg")); | 196 EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/annoying_ads/ad.jpg")); |
190 EXPECT_FALSE(BlacklistHasMatch(blacklist4, "http://host/other_ads/ad.jpg")); | 197 EXPECT_FALSE(BlacklistHasMatch(blacklist4, "http://host/other_ads/ad.jpg")); |
191 } | 198 } |
192 | 199 |
193 TEST_F(BlacklistManagerTest, RealThread) { | 200 TEST_F(BlacklistManagerTest, RealThread) { |
194 base::Thread backend_thread("backend_thread"); | 201 base::Thread backend_thread("backend_thread"); |
195 backend_thread.Start(); | 202 backend_thread.Start(); |
196 | 203 |
197 scoped_refptr<BlacklistManager> manager( | 204 scoped_refptr<BlacklistManager> manager( |
198 new BlacklistManager(&profile_, &path_provider_, &backend_thread)); | 205 new BlacklistManager(&profile_, &path_provider_, &backend_thread)); |
(...skipping 16 matching lines...) Expand all Loading... |
215 | 222 |
216 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { | 223 TEST_F(BlacklistManagerTest, BlacklistPathReadError) { |
217 scoped_refptr<BlacklistManager> manager( | 224 scoped_refptr<BlacklistManager> manager( |
218 new BlacklistManager(&profile_, &path_provider_, NULL)); | 225 new BlacklistManager(&profile_, &path_provider_, NULL)); |
219 WaitForBlacklistUpdate(); | 226 WaitForBlacklistUpdate(); |
220 | 227 |
221 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); | 228 FilePath bogus_path(test_data_dir_.AppendASCII("does_not_exist_randomness")); |
222 ASSERT_FALSE(file_util::PathExists(bogus_path)); | 229 ASSERT_FALSE(file_util::PathExists(bogus_path)); |
223 path_provider_.AddPersistentPath(bogus_path); | 230 path_provider_.AddPersistentPath(bogus_path); |
224 WaitForBlacklistError(); | 231 WaitForBlacklistError(); |
225 | 232 |
226 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 233 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
227 EXPECT_TRUE(blacklist); | 234 EXPECT_TRUE(blacklist); |
228 } | 235 } |
229 | 236 |
230 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { | 237 TEST_F(BlacklistManagerTest, CompiledBlacklistReadError) { |
231 FilePath compiled_blacklist_path; | 238 FilePath compiled_blacklist_path; |
232 | 239 |
233 { | 240 { |
234 scoped_refptr<BlacklistManager> manager( | 241 scoped_refptr<BlacklistManager> manager( |
235 new BlacklistManager(&profile_, &path_provider_, NULL)); | 242 new BlacklistManager(&profile_, &path_provider_, NULL)); |
(...skipping 18 matching lines...) Expand all Loading... |
254 WaitForBlacklistUpdate(); | 261 WaitForBlacklistUpdate(); |
255 | 262 |
256 // The manager should recompile the blacklist. | 263 // The manager should recompile the blacklist. |
257 const Blacklist* blacklist = manager->GetCompiledBlacklist(); | 264 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
258 EXPECT_TRUE(BlacklistHasMatch(blacklist, | 265 EXPECT_TRUE(BlacklistHasMatch(blacklist, |
259 "http://host/annoying_ads/ad.jpg")); | 266 "http://host/annoying_ads/ad.jpg")); |
260 } | 267 } |
261 } | 268 } |
262 | 269 |
263 } // namespace | 270 } // namespace |
OLD | NEW |