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 "chrome/browser/extensions/blacklist.h" | 8 #include "chrome/browser/extensions/blacklist.h" |
9 #include "chrome/browser/extensions/blacklist_fetcher.h" | |
9 #include "chrome/browser/extensions/extension_prefs.h" | 10 #include "chrome/browser/extensions/extension_prefs.h" |
10 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" | 11 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" |
11 #include "chrome/browser/extensions/test_blacklist.h" | 12 #include "chrome/browser/extensions/test_blacklist.h" |
13 #include "chrome/browser/extensions/test_blacklist_fetcher.h" | |
12 #include "chrome/browser/extensions/test_extension_prefs.h" | 14 #include "chrome/browser/extensions/test_extension_prefs.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 namespace extensions { | 18 namespace extensions { |
17 namespace { | 19 namespace { |
18 | 20 |
19 std::set<std::string> Set(const std::string& a) { | 21 std::set<std::string> Set(const std::string& a) { |
20 std::set<std::string> set; | 22 std::set<std::string> set; |
21 set.insert(a); | 23 set.insert(a); |
22 return set; | 24 return set; |
23 } | 25 } |
24 std::set<std::string> Set(const std::string& a, const std::string& b) { | 26 std::set<std::string> Set(const std::string& a, const std::string& b) { |
25 std::set<std::string> set = Set(a); | 27 std::set<std::string> set = Set(a); |
26 set.insert(b); | 28 set.insert(b); |
27 return set; | 29 return set; |
28 } | 30 } |
29 std::set<std::string> Set(const std::string& a, | 31 std::set<std::string> Set(const std::string& a, |
30 const std::string& b, | 32 const std::string& b, |
31 const std::string& c) { | 33 const std::string& c) { |
32 std::set<std::string> set = Set(a, b); | 34 std::set<std::string> set = Set(a, b); |
33 set.insert(c); | 35 set.insert(c); |
34 return set; | 36 return set; |
35 } | 37 } |
36 std::set<std::string> Set(const std::string& a, | 38 std::set<std::string> Set(const std::string& a, |
37 const std::string& b, | 39 const std::string& b, |
38 const std::string& d, | 40 const std::string& c, |
39 const std::string& c) { | 41 const std::string& d) { |
40 std::set<std::string> set = Set(a, b, c); | 42 std::set<std::string> set = Set(a, b, c); |
41 set.insert(d); | 43 set.insert(d); |
42 return set; | 44 return set; |
43 } | 45 } |
44 | 46 |
45 class BlacklistTest : public testing::Test { | 47 class BlacklistTest : public testing::Test { |
46 public: | 48 public: |
47 BlacklistTest() | 49 BlacklistTest() |
48 : test_prefs_(base::MessageLoopProxy::current()), | 50 : test_prefs_(base::MessageLoopProxy::current()), |
49 blacklist_db_(new FakeSafeBrowsingDatabaseManager(false)), | 51 blacklist_db_(new FakeSafeBrowsingDatabaseManager(false)), |
(...skipping 15 matching lines...) Expand all Loading... | |
65 private: | 67 private: |
66 content::TestBrowserThreadBundle browser_thread_bundle_; | 68 content::TestBrowserThreadBundle browser_thread_bundle_; |
67 | 69 |
68 TestExtensionPrefs test_prefs_; | 70 TestExtensionPrefs test_prefs_; |
69 | 71 |
70 scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db_; | 72 scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db_; |
71 | 73 |
72 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db_; | 74 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db_; |
73 }; | 75 }; |
74 | 76 |
75 void Assign(std::set<std::string> *to, const std::set<std::string>& from) { | 77 class BlacklistFetcherMock : public BlacklistFetcher { |
78 public: | |
79 virtual void Request(const std::string& id, const RequestCallback& callback) { | |
80 Blacklist::BlacklistState result = Blacklist::NOT_BLACKLISTED; | |
81 if (states_.find(id) != states_.end()) { | |
not at google - send to devlin
2013/11/18 17:03:49
likewise you could use count here. but since you d
Oleg Eterevsky
2013/11/20 12:58:45
If we were using C++11, I'd write
(auto it = sta
| |
82 result = states_[id]; | |
83 } | |
84 | |
85 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | |
86 base::Bind(callback, result)); | |
87 } | |
88 | |
89 void SetState(const std::string& id, Blacklist::BlacklistState state) { | |
90 states_[id] = state; | |
91 } | |
92 | |
93 private: | |
94 std::map<std::string, Blacklist::BlacklistState> states_; | |
95 | |
96 bool delayed_; | |
not at google - send to devlin
2013/11/18 17:03:49
not used
Oleg Eterevsky
2013/11/20 12:58:45
Done.
| |
97 }; | |
98 | |
99 void AssignSet(std::set<std::string> *to, const std::set<std::string>& from) { | |
76 *to = from; | 100 *to = from; |
77 } | 101 } |
78 | 102 |
103 void AssignMap(Blacklist::BlacklistStateMap *to, | |
104 const Blacklist::BlacklistStateMap& from) { | |
not at google - send to devlin
2013/11/18 17:03:49
nit: indent
though could you templatise Assign ra
Oleg Eterevsky
2013/11/20 12:58:45
Done.
| |
105 *to = from; | |
106 } | |
107 | |
108 } // namespace | |
109 | |
79 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { | 110 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { |
80 std::string a = AddExtension("a"); | 111 std::string a = AddExtension("a"); |
81 std::string b = AddExtension("b"); | 112 std::string b = AddExtension("b"); |
82 std::string c = AddExtension("c"); | 113 std::string c = AddExtension("c"); |
83 | 114 |
84 Blacklist blacklist(prefs()); | 115 Blacklist blacklist(prefs()); |
85 TestBlacklist tester(&blacklist); | 116 TestBlacklist tester(&blacklist); |
117 BlacklistFetcherMock* fetcher = new BlacklistFetcherMock(); | |
118 fetcher->SetState(a, Blacklist::BLACKLISTED_MALWARE); | |
119 fetcher->SetState(b, Blacklist::BLACKLISTED_MALWARE); | |
120 blacklist.SetBlacklistFetcherForTest(fetcher); | |
86 | 121 |
87 blacklist_db()->Enable(); | 122 blacklist_db()->Enable(); |
88 blacklist_db()->SetUnsafe(a, b); | 123 blacklist_db()->SetUnsafe(a, b); |
89 | 124 |
90 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); | 125 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); |
91 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(b)); | 126 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(b)); |
92 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(c)); | 127 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(c)); |
93 | 128 |
94 std::set<std::string> blacklisted_ids; | 129 std::set<std::string> blacklisted_ids; |
95 blacklist.GetMalwareIDs(Set(a, c), base::Bind(&Assign, &blacklisted_ids)); | 130 blacklist.GetMalwareIDs(Set(a, c), base::Bind(&AssignSet, &blacklisted_ids)); |
96 base::RunLoop().RunUntilIdle(); | 131 base::RunLoop().RunUntilIdle(); |
97 | 132 |
98 EXPECT_EQ(Set(a), blacklisted_ids); | 133 EXPECT_EQ(Set(a), blacklisted_ids); |
99 } | 134 } |
100 | 135 |
101 TEST_F(BlacklistTest, SafeBrowsing) { | 136 TEST_F(BlacklistTest, SafeBrowsing) { |
102 std::string a = AddExtension("a"); | 137 std::string a = AddExtension("a"); |
103 | 138 |
104 Blacklist blacklist(prefs()); | 139 Blacklist blacklist(prefs()); |
105 TestBlacklist tester(&blacklist); | 140 TestBlacklist tester(&blacklist); |
141 BlacklistFetcherMock* fetcher = new BlacklistFetcherMock(); | |
142 fetcher->SetState(a, Blacklist::BLACKLISTED_MALWARE); | |
143 blacklist.SetBlacklistFetcherForTest(fetcher); | |
106 | 144 |
107 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); | 145 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); |
108 | 146 |
109 blacklist_db()->SetUnsafe(a); | 147 blacklist_db()->SetUnsafe(a); |
110 // The manager is still disabled at this point, so it won't be blacklisted. | 148 // The manager is still disabled at this point, so it won't be blacklisted. |
111 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); | 149 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); |
112 | 150 |
113 blacklist_db()->Enable().NotifyUpdate(); | 151 blacklist_db()->Enable().NotifyUpdate(); |
114 base::RunLoop().RunUntilIdle(); | 152 base::RunLoop().RunUntilIdle(); |
115 // Now it should be. | 153 // Now it should be. |
(...skipping 15 matching lines...) Expand all Loading... | |
131 // Blacklist some non-installed extensions. This is what the old preferences | 169 // Blacklist some non-installed extensions. This is what the old preferences |
132 // blacklist looked like. | 170 // blacklist looked like. |
133 std::string c = "cccccccccccccccccccccccccccccccc"; | 171 std::string c = "cccccccccccccccccccccccccccccccc"; |
134 std::string d = "dddddddddddddddddddddddddddddddd"; | 172 std::string d = "dddddddddddddddddddddddddddddddd"; |
135 prefs()->SetExtensionBlacklisted(c, true); | 173 prefs()->SetExtensionBlacklisted(c, true); |
136 prefs()->SetExtensionBlacklisted(d, true); | 174 prefs()->SetExtensionBlacklisted(d, true); |
137 | 175 |
138 EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions()); | 176 EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions()); |
139 | 177 |
140 Blacklist blacklist(prefs()); | 178 Blacklist blacklist(prefs()); |
141 TestBlacklist tester(&blacklist); | 179 blacklist.SetBlacklistFetcherForTest(new BlacklistFetcherMock()); |
142 | 180 |
143 // Blacklist has been cleared. Only the installed extension "a" left. | 181 // Blacklist has been cleared. Only the installed extension "a" left. |
144 EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions()); | 182 EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions()); |
145 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(a).get()); | 183 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(a).get()); |
146 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(b).get()); | 184 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(b).get()); |
147 | 185 |
148 // "a" won't actually be *blacklisted* since it doesn't appear in | 186 // "a" won't actually be *blacklisted* since it doesn't appear in |
149 // safebrowsing. Blacklist no longer reads from prefs. This is purely a | 187 // safebrowsing. Blacklist no longer reads from prefs. This is purely a |
150 // concern of somebody else (currently, ExtensionService). | 188 // concern of somebody else (currently, ExtensionService). |
151 std::set<std::string> blacklisted_ids; | 189 std::set<std::string> blacklisted_ids; |
152 blacklist.GetMalwareIDs(Set(a, b, c, d), | 190 blacklist.GetMalwareIDs(Set(a, b, c, d), |
153 base::Bind(&Assign, &blacklisted_ids)); | 191 base::Bind(&AssignSet, &blacklisted_ids)); |
154 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
155 EXPECT_EQ(std::set<std::string>(), blacklisted_ids); | 193 EXPECT_EQ(std::set<std::string>(), blacklisted_ids); |
156 | 194 |
157 // Prefs are still unaffected for installed extensions, though. | 195 // Prefs are still unaffected for installed extensions, though. |
158 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(a)); | 196 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(a)); |
159 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(b)); | 197 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(b)); |
160 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(c)); | 198 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(c)); |
161 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(d)); | 199 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(d)); |
162 } | 200 } |
163 | 201 |
164 } // namespace | 202 // Test getting different blacklist states from Blacklist. |
203 TEST_F(BlacklistTest, GetBlacklistStates) { | |
204 Blacklist blacklist(prefs()); | |
205 | |
206 std::string a = AddExtension("a"); | |
207 std::string b = AddExtension("b"); | |
208 std::string c = AddExtension("c"); | |
209 std::string d = AddExtension("d"); | |
210 std::string e = AddExtension("e"); | |
211 | |
212 blacklist_db()->Enable(); | |
213 blacklist_db()->SetUnsafe(a, b, c, d); | |
214 | |
215 BlacklistFetcherMock* fetcher = new BlacklistFetcherMock(); | |
216 fetcher->SetState(a, Blacklist::BLACKLISTED_MALWARE); | |
217 fetcher->SetState(b, Blacklist::BLACKLISTED_SECURITY_VULNERABILITY); | |
218 fetcher->SetState(c, Blacklist::BLACKLISTED_CWS_POLICY_VIOLATION); | |
219 fetcher->SetState(d, Blacklist::BLACKLISTED_POTENTIALLY_UNWANTED); | |
220 blacklist.SetBlacklistFetcherForTest(fetcher); | |
221 | |
222 Blacklist::BlacklistStateMap states_abc; | |
223 Blacklist::BlacklistStateMap states_bcd; | |
224 blacklist.GetBlacklistedIDs(Set(a, b, c, e), | |
225 base::Bind(&AssignMap, &states_abc)); | |
226 blacklist.GetBlacklistedIDs(Set(b, c, d, e), | |
227 base::Bind(&AssignMap, &states_bcd)); | |
228 base::RunLoop().RunUntilIdle(); | |
229 | |
230 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, states_abc[a]); | |
231 EXPECT_EQ(Blacklist::BLACKLISTED_SECURITY_VULNERABILITY, states_abc[b]); | |
232 EXPECT_EQ(Blacklist::BLACKLISTED_CWS_POLICY_VIOLATION, states_abc[c]); | |
233 EXPECT_EQ(Blacklist::BLACKLISTED_SECURITY_VULNERABILITY, states_bcd[b]); | |
234 EXPECT_EQ(Blacklist::BLACKLISTED_CWS_POLICY_VIOLATION, states_bcd[c]); | |
235 EXPECT_EQ(Blacklist::BLACKLISTED_POTENTIALLY_UNWANTED, states_bcd[d]); | |
236 EXPECT_EQ(states_abc.end(), states_abc.find(e)); | |
not at google - send to devlin
2013/11/18 17:03:49
colud use EXPECT_EQ(0, states_abc.count(e)) here.
Oleg Eterevsky
2013/11/20 12:58:45
Same find vs count as before.
| |
237 EXPECT_EQ(states_bcd.end(), states_bcd.find(e)); | |
not at google - send to devlin
2013/11/18 17:03:49
could you also test a purely cached response?
Oleg Eterevsky
2013/11/20 12:58:45
Done.
| |
238 } | |
239 | |
240 // Test both Blacklist and BlacklistFetcher by requesting the blacklist states, | |
241 // sending fake requests and parsing the responses. | |
242 TEST_F(BlacklistTest, FetchBlacklistStates) { | |
243 Blacklist blacklist(prefs()); | |
244 | |
245 std::string a = AddExtension("a"); | |
246 std::string b = AddExtension("b"); | |
247 std::string c = AddExtension("c"); | |
248 | |
249 blacklist_db()->Enable(); | |
250 blacklist_db()->SetUnsafe(a, b); | |
251 | |
252 // Prepare real fetcher. | |
253 BlacklistFetcher* fetcher = new BlacklistFetcher(); | |
254 TestBlacklistFetcher fetcher_tester(fetcher); | |
255 blacklist.SetBlacklistFetcherForTest(fetcher); | |
256 | |
257 fetcher_tester.SetBlacklistVerdict( | |
258 a, ClientCRXListInfoResponse_Verdict_CWS_POLICY_VIOLATION); | |
259 fetcher_tester.SetBlacklistVerdict( | |
260 b, ClientCRXListInfoResponse_Verdict_POTENTIALLY_UNWANTED); | |
261 | |
262 Blacklist::BlacklistStateMap states; | |
263 blacklist.GetBlacklistedIDs(Set(a, b, c), base::Bind(&AssignMap, &states)); | |
264 base::RunLoop().RunUntilIdle(); | |
265 | |
266 // Two fetchers should be created. | |
not at google - send to devlin
2013/11/18 17:03:49
indent
Oleg Eterevsky
2013/11/20 12:58:45
Done.
| |
267 EXPECT_TRUE(fetcher_tester.HandleFetcher(0)); | |
268 EXPECT_TRUE(fetcher_tester.HandleFetcher(1)); | |
269 | |
270 EXPECT_EQ(Blacklist::BLACKLISTED_CWS_POLICY_VIOLATION, states[a]); | |
271 EXPECT_EQ(Blacklist::BLACKLISTED_POTENTIALLY_UNWANTED, states[b]); | |
272 EXPECT_EQ(states.end(), states.find(c)); | |
not at google - send to devlin
2013/11/18 17:03:49
likewise caching
Oleg Eterevsky
2013/11/20 12:58:45
Done.
| |
273 } | |
274 | |
165 } // namespace extensions | 275 } // namespace extensions |
OLD | NEW |