OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/extensions/blacklist.h" | 9 #include "chrome/browser/extensions/blacklist.h" |
10 #include "chrome/browser/extensions/blacklist_state_fetcher.h" | 10 #include "chrome/browser/extensions/blacklist_state_fetcher.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 const std::string& c, | 41 const std::string& c, |
42 const std::string& d) { | 42 const std::string& d) { |
43 std::set<std::string> set = Set(a, b, c); | 43 std::set<std::string> set = Set(a, b, c); |
44 set.insert(d); | 44 set.insert(d); |
45 return set; | 45 return set; |
46 } | 46 } |
47 | 47 |
48 class BlacklistTest : public testing::Test { | 48 class BlacklistTest : public testing::Test { |
49 public: | 49 public: |
50 BlacklistTest() | 50 BlacklistTest() |
51 : test_prefs_(base::MessageLoopProxy::current()), | 51 : test_prefs_(base::MessageLoopProxy::current()) {} |
52 blacklist_db_(new FakeSafeBrowsingDatabaseManager(false)), | |
53 scoped_blacklist_db_(blacklist_db_) {} | |
54 | 52 |
55 protected: | 53 protected: |
56 ExtensionPrefs* prefs() { | 54 ExtensionPrefs* prefs() { |
57 return test_prefs_.prefs(); | 55 return test_prefs_.prefs(); |
58 } | 56 } |
59 | 57 |
60 FakeSafeBrowsingDatabaseManager* blacklist_db() { | |
61 return blacklist_db_.get(); | |
62 } | |
63 | |
64 std::string AddExtension(const std::string& id) { | 58 std::string AddExtension(const std::string& id) { |
65 return test_prefs_.AddExtension(id)->id(); | 59 return test_prefs_.AddExtension(id)->id(); |
66 } | 60 } |
67 | 61 |
68 private: | 62 private: |
69 content::TestBrowserThreadBundle browser_thread_bundle_; | 63 content::TestBrowserThreadBundle browser_thread_bundle_; |
70 | 64 |
71 TestExtensionPrefs test_prefs_; | 65 TestExtensionPrefs test_prefs_; |
72 | |
73 scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db_; | |
74 | |
75 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db_; | |
76 }; | |
77 | |
78 class BlacklistStateFetcherMock : public BlacklistStateFetcher { | |
79 public: | |
80 BlacklistStateFetcherMock() : request_count_(0) { | |
81 } | |
82 | |
83 virtual void Request(const std::string& id, | |
84 const RequestCallback& callback) OVERRIDE { | |
85 request_count_++; | |
86 | |
87 BlacklistState result = NOT_BLACKLISTED; | |
88 if (ContainsKey(states_, id)) | |
89 result = states_[id]; | |
90 | |
91 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | |
92 base::Bind(callback, result)); | |
93 } | |
94 | |
95 void SetState(const std::string& id, BlacklistState state) { | |
96 states_[id] = state; | |
97 } | |
98 | |
99 int request_count() const { | |
100 return request_count_; | |
101 } | |
102 | |
103 private: | |
104 std::map<std::string, BlacklistState> states_; | |
105 int request_count_; | |
106 }; | 66 }; |
107 | 67 |
108 template<typename T> | 68 template<typename T> |
109 void Assign(T *to, const T& from) { | 69 void Assign(T *to, const T& from) { |
110 *to = from; | 70 *to = from; |
111 } | 71 } |
112 | 72 |
113 } // namespace | 73 } // namespace |
114 | 74 |
115 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { | 75 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { |
116 std::string a = AddExtension("a"); | 76 std::string a = AddExtension("a"); |
117 std::string b = AddExtension("b"); | 77 std::string b = AddExtension("b"); |
118 std::string c = AddExtension("c"); | 78 std::string c = AddExtension("c"); |
119 | 79 |
120 Blacklist blacklist(prefs()); | 80 Blacklist blacklist(prefs()); |
121 TestBlacklist tester(&blacklist); | 81 TestBlacklist tester(&blacklist); |
122 BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock(); | 82 tester.SetBlacklistState(a, BLACKLISTED_MALWARE, false); |
123 fetcher->SetState(a, BLACKLISTED_MALWARE); | 83 tester.SetBlacklistState(b, BLACKLISTED_MALWARE, false); |
124 fetcher->SetState(b, BLACKLISTED_MALWARE); | |
125 blacklist.SetBlacklistStateFetcherForTest(fetcher); | |
126 | |
127 blacklist_db()->Enable(); | |
128 blacklist_db()->SetUnsafe(a, b); | |
129 | 84 |
130 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); | 85 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); |
131 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(b)); | 86 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(b)); |
132 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(c)); | 87 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(c)); |
133 | 88 |
134 std::set<std::string> blacklisted_ids; | 89 std::set<std::string> blacklisted_ids; |
135 blacklist.GetMalwareIDs( | 90 blacklist.GetMalwareIDs( |
136 Set(a, c), base::Bind(&Assign<std::set<std::string> >, &blacklisted_ids)); | 91 Set(a, c), base::Bind(&Assign<std::set<std::string> >, &blacklisted_ids)); |
137 base::RunLoop().RunUntilIdle(); | 92 base::RunLoop().RunUntilIdle(); |
138 | 93 |
139 EXPECT_EQ(Set(a), blacklisted_ids); | 94 EXPECT_EQ(Set(a), blacklisted_ids); |
140 } | 95 } |
141 | 96 |
142 TEST_F(BlacklistTest, SafeBrowsing) { | 97 TEST_F(BlacklistTest, SafeBrowsing) { |
143 std::string a = AddExtension("a"); | 98 std::string a = AddExtension("a"); |
144 | 99 |
145 Blacklist blacklist(prefs()); | 100 Blacklist blacklist(prefs()); |
146 TestBlacklist tester(&blacklist); | 101 TestBlacklist tester(&blacklist); |
147 BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock(); | 102 tester.DisableSafeBrowsing(); |
148 fetcher->SetState(a, BLACKLISTED_MALWARE); | |
149 blacklist.SetBlacklistStateFetcherForTest(fetcher); | |
150 | 103 |
151 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); | 104 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); |
152 | 105 |
153 blacklist_db()->SetUnsafe(a); | 106 tester.SetBlacklistState(a, BLACKLISTED_MALWARE, false); |
154 // The manager is still disabled at this point, so it won't be blacklisted. | 107 // The manager is still disabled at this point, so it won't be blacklisted. |
155 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); | 108 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); |
156 | 109 |
157 blacklist_db()->Enable().NotifyUpdate(); | 110 tester.EnableSafeBrowsing(); |
| 111 tester.NotifyUpdate(); |
158 base::RunLoop().RunUntilIdle(); | 112 base::RunLoop().RunUntilIdle(); |
159 // Now it should be. | 113 // Now it should be. |
160 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); | 114 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); |
161 | 115 |
162 blacklist_db()->ClearUnsafe().NotifyUpdate(); | 116 tester.Clear(true); |
163 // Safe browsing blacklist empty, now enabled. | 117 // Safe browsing blacklist empty, now enabled. |
164 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); | 118 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); |
165 } | 119 } |
166 | 120 |
167 // Tests that Blacklist clears the old prefs blacklist on startup. | 121 // Tests that Blacklist clears the old prefs blacklist on startup. |
168 TEST_F(BlacklistTest, ClearsPreferencesBlacklist) { | 122 TEST_F(BlacklistTest, ClearsPreferencesBlacklist) { |
169 std::string a = AddExtension("a"); | 123 std::string a = AddExtension("a"); |
170 std::string b = AddExtension("b"); | 124 std::string b = AddExtension("b"); |
171 | 125 |
172 // Blacklist an installed extension. | 126 // Blacklist an installed extension. |
173 prefs()->SetExtensionBlacklisted(a, true); | 127 prefs()->SetExtensionBlacklisted(a, true); |
174 | 128 |
175 // Blacklist some non-installed extensions. This is what the old preferences | 129 // Blacklist some non-installed extensions. This is what the old preferences |
176 // blacklist looked like. | 130 // blacklist looked like. |
177 std::string c = "cccccccccccccccccccccccccccccccc"; | 131 std::string c = "cccccccccccccccccccccccccccccccc"; |
178 std::string d = "dddddddddddddddddddddddddddddddd"; | 132 std::string d = "dddddddddddddddddddddddddddddddd"; |
179 prefs()->SetExtensionBlacklisted(c, true); | 133 prefs()->SetExtensionBlacklisted(c, true); |
180 prefs()->SetExtensionBlacklisted(d, true); | 134 prefs()->SetExtensionBlacklisted(d, true); |
181 | 135 |
182 EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions()); | 136 EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions()); |
183 | 137 |
184 Blacklist blacklist(prefs()); | 138 Blacklist blacklist(prefs()); |
185 blacklist.SetBlacklistStateFetcherForTest(new BlacklistStateFetcherMock()); | 139 TestBlacklist tester(&blacklist); |
186 | 140 |
187 // Blacklist has been cleared. Only the installed extension "a" left. | 141 // Blacklist has been cleared. Only the installed extension "a" left. |
188 EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions()); | 142 EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions()); |
189 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(a).get()); | 143 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(a).get()); |
190 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(b).get()); | 144 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(b).get()); |
191 | 145 |
192 // "a" won't actually be *blacklisted* since it doesn't appear in | 146 // "a" won't actually be *blacklisted* since it doesn't appear in |
193 // safebrowsing. Blacklist no longer reads from prefs. This is purely a | 147 // safebrowsing. Blacklist no longer reads from prefs. This is purely a |
194 // concern of somebody else (currently, ExtensionService). | 148 // concern of somebody else (currently, ExtensionService). |
195 std::set<std::string> blacklisted_ids; | 149 std::set<std::string> blacklisted_ids; |
196 blacklist.GetMalwareIDs(Set(a, b, c, d), | 150 blacklist.GetMalwareIDs(Set(a, b, c, d), |
197 base::Bind(&Assign<std::set<std::string> >, | 151 base::Bind(&Assign<std::set<std::string> >, |
198 &blacklisted_ids)); | 152 &blacklisted_ids)); |
199 base::RunLoop().RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
200 EXPECT_EQ(std::set<std::string>(), blacklisted_ids); | 154 EXPECT_EQ(std::set<std::string>(), blacklisted_ids); |
201 | 155 |
202 // Prefs are still unaffected for installed extensions, though. | 156 // Prefs are still unaffected for installed extensions, though. |
203 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(a)); | 157 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(a)); |
204 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(b)); | 158 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(b)); |
205 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(c)); | 159 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(c)); |
206 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(d)); | 160 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(d)); |
207 } | 161 } |
208 | 162 |
209 // Test getting different blacklist states from Blacklist. | 163 // Test getting different blacklist states from Blacklist. |
210 TEST_F(BlacklistTest, GetBlacklistStates) { | 164 TEST_F(BlacklistTest, GetBlacklistStates) { |
211 Blacklist blacklist(prefs()); | 165 Blacklist blacklist(prefs()); |
| 166 TestBlacklist tester(&blacklist); |
212 | 167 |
213 std::string a = AddExtension("a"); | 168 std::string a = AddExtension("a"); |
214 std::string b = AddExtension("b"); | 169 std::string b = AddExtension("b"); |
215 std::string c = AddExtension("c"); | 170 std::string c = AddExtension("c"); |
216 std::string d = AddExtension("d"); | 171 std::string d = AddExtension("d"); |
217 std::string e = AddExtension("e"); | 172 std::string e = AddExtension("e"); |
218 | 173 |
219 blacklist_db()->Enable(); | 174 tester.SetBlacklistState(a, BLACKLISTED_MALWARE, false); |
220 blacklist_db()->SetUnsafe(a, b, c, d); | 175 tester.SetBlacklistState(b, BLACKLISTED_SECURITY_VULNERABILITY, false); |
221 | 176 tester.SetBlacklistState(c, BLACKLISTED_CWS_POLICY_VIOLATION, false); |
222 // Will be deleted by blacklist destructor. | 177 tester.SetBlacklistState(d, BLACKLISTED_POTENTIALLY_UNWANTED, false); |
223 BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock(); | |
224 fetcher->SetState(a, BLACKLISTED_MALWARE); | |
225 fetcher->SetState(b, BLACKLISTED_SECURITY_VULNERABILITY); | |
226 fetcher->SetState(c, BLACKLISTED_CWS_POLICY_VIOLATION); | |
227 fetcher->SetState(d, BLACKLISTED_POTENTIALLY_UNWANTED); | |
228 blacklist.SetBlacklistStateFetcherForTest(fetcher); | |
229 | 178 |
230 Blacklist::BlacklistStateMap states_abc; | 179 Blacklist::BlacklistStateMap states_abc; |
231 Blacklist::BlacklistStateMap states_bcd; | 180 Blacklist::BlacklistStateMap states_bcd; |
232 blacklist.GetBlacklistedIDs(Set(a, b, c, e), | 181 blacklist.GetBlacklistedIDs(Set(a, b, c, e), |
233 base::Bind(&Assign<Blacklist::BlacklistStateMap>, | 182 base::Bind(&Assign<Blacklist::BlacklistStateMap>, |
234 &states_abc)); | 183 &states_abc)); |
235 blacklist.GetBlacklistedIDs(Set(b, c, d, e), | 184 blacklist.GetBlacklistedIDs(Set(b, c, d, e), |
236 base::Bind(&Assign<Blacklist::BlacklistStateMap>, | 185 base::Bind(&Assign<Blacklist::BlacklistStateMap>, |
237 &states_bcd)); | 186 &states_bcd)); |
238 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
239 | 188 |
240 EXPECT_EQ(BLACKLISTED_MALWARE, states_abc[a]); | 189 EXPECT_EQ(BLACKLISTED_MALWARE, states_abc[a]); |
241 EXPECT_EQ(BLACKLISTED_SECURITY_VULNERABILITY, states_abc[b]); | 190 EXPECT_EQ(BLACKLISTED_SECURITY_VULNERABILITY, states_abc[b]); |
242 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, states_abc[c]); | 191 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, states_abc[c]); |
243 EXPECT_EQ(BLACKLISTED_SECURITY_VULNERABILITY, states_bcd[b]); | 192 EXPECT_EQ(BLACKLISTED_SECURITY_VULNERABILITY, states_bcd[b]); |
244 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, states_bcd[c]); | 193 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, states_bcd[c]); |
245 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states_bcd[d]); | 194 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states_bcd[d]); |
246 EXPECT_EQ(states_abc.end(), states_abc.find(e)); | 195 EXPECT_EQ(states_abc.end(), states_abc.find(e)); |
247 EXPECT_EQ(states_bcd.end(), states_bcd.find(e)); | 196 EXPECT_EQ(states_bcd.end(), states_bcd.find(e)); |
248 | 197 |
249 int old_request_count = fetcher->request_count(); | 198 int old_request_count = tester.fetcher()->request_count(); |
250 Blacklist::BlacklistStateMap states_ad; | 199 Blacklist::BlacklistStateMap states_ad; |
251 blacklist.GetBlacklistedIDs(Set(a, d, e), | 200 blacklist.GetBlacklistedIDs(Set(a, d, e), |
252 base::Bind(&Assign<Blacklist::BlacklistStateMap>, | 201 base::Bind(&Assign<Blacklist::BlacklistStateMap>, |
253 &states_ad)); | 202 &states_ad)); |
254 base::RunLoop().RunUntilIdle(); | 203 base::RunLoop().RunUntilIdle(); |
255 EXPECT_EQ(BLACKLISTED_MALWARE, states_ad[a]); | 204 EXPECT_EQ(BLACKLISTED_MALWARE, states_ad[a]); |
256 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states_ad[d]); | 205 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states_ad[d]); |
257 EXPECT_EQ(states_ad.end(), states_ad.find(e)); | 206 EXPECT_EQ(states_ad.end(), states_ad.find(e)); |
258 EXPECT_EQ(old_request_count, fetcher->request_count()); | 207 EXPECT_EQ(old_request_count, tester.fetcher()->request_count()); |
259 } | 208 } |
260 | 209 |
261 // Test both Blacklist and BlacklistStateFetcher by requesting the blacklist | 210 // Test both Blacklist and BlacklistStateFetcher by requesting the blacklist |
262 // states, sending fake requests and parsing the responses. | 211 // states, sending fake requests and parsing the responses. |
263 TEST_F(BlacklistTest, FetchBlacklistStates) { | 212 TEST_F(BlacklistTest, FetchBlacklistStates) { |
264 Blacklist blacklist(prefs()); | 213 Blacklist blacklist(prefs()); |
| 214 scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db( |
| 215 new FakeSafeBrowsingDatabaseManager(true)); |
| 216 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db); |
265 | 217 |
266 std::string a = AddExtension("a"); | 218 std::string a = AddExtension("a"); |
267 std::string b = AddExtension("b"); | 219 std::string b = AddExtension("b"); |
268 std::string c = AddExtension("c"); | 220 std::string c = AddExtension("c"); |
269 | 221 |
270 blacklist_db()->Enable(); | 222 blacklist_db->Enable(); |
271 blacklist_db()->SetUnsafe(a, b); | 223 blacklist_db->SetUnsafe(a, b); |
272 | 224 |
273 // Prepare real fetcher. | 225 // Prepare real fetcher. |
274 BlacklistStateFetcher* fetcher = new BlacklistStateFetcher(); | 226 BlacklistStateFetcher* fetcher = new BlacklistStateFetcher(); |
275 TestBlacklistStateFetcher fetcher_tester(fetcher); | 227 TestBlacklistStateFetcher fetcher_tester(fetcher); |
276 blacklist.SetBlacklistStateFetcherForTest(fetcher); | 228 blacklist.SetBlacklistStateFetcherForTest(fetcher); |
277 | 229 |
278 fetcher_tester.SetBlacklistVerdict( | 230 fetcher_tester.SetBlacklistVerdict( |
279 a, ClientCRXListInfoResponse_Verdict_CWS_POLICY_VIOLATION); | 231 a, ClientCRXListInfoResponse_Verdict_CWS_POLICY_VIOLATION); |
280 fetcher_tester.SetBlacklistVerdict( | 232 fetcher_tester.SetBlacklistVerdict( |
281 b, ClientCRXListInfoResponse_Verdict_POTENTIALLY_UNWANTED); | 233 b, ClientCRXListInfoResponse_Verdict_POTENTIALLY_UNWANTED); |
(...skipping 19 matching lines...) Expand all Loading... |
301 base::RunLoop().RunUntilIdle(); | 253 base::RunLoop().RunUntilIdle(); |
302 | 254 |
303 // No new fetchers. | 255 // No new fetchers. |
304 EXPECT_FALSE(fetcher_tester.HandleFetcher(2)); | 256 EXPECT_FALSE(fetcher_tester.HandleFetcher(2)); |
305 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, cached_states[a]); | 257 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, cached_states[a]); |
306 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, cached_states[b]); | 258 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, cached_states[b]); |
307 EXPECT_EQ(cached_states.end(), cached_states.find(c)); | 259 EXPECT_EQ(cached_states.end(), cached_states.find(c)); |
308 } | 260 } |
309 | 261 |
310 } // namespace extensions | 262 } // namespace extensions |
OLD | NEW |