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