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, |
| 81 const RequestCallback& callback) OVERRIDE { |
| 82 request_count_++; |
| 83 |
| 84 BlacklistState result = NOT_BLACKLISTED; |
| 85 if (ContainsKey(states_, id)) |
| 86 result = states_[id]; |
| 87 |
| 88 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 89 base::Bind(callback, result)); |
| 90 } |
| 91 |
| 92 void SetState(const std::string& id, BlacklistState state) { |
| 93 states_[id] = state; |
| 94 } |
| 95 |
| 96 int request_count() const { |
| 97 return request_count_; |
| 98 } |
| 99 |
| 100 private: |
| 101 std::map<std::string, BlacklistState> states_; |
| 102 int request_count_; |
| 103 }; |
| 104 |
| 105 template<typename T> |
| 106 void Assign(T *to, const T& from) { |
76 *to = from; | 107 *to = from; |
77 } | 108 } |
78 | 109 |
| 110 } // namespace |
| 111 |
79 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { | 112 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { |
80 std::string a = AddExtension("a"); | 113 std::string a = AddExtension("a"); |
81 std::string b = AddExtension("b"); | 114 std::string b = AddExtension("b"); |
82 std::string c = AddExtension("c"); | 115 std::string c = AddExtension("c"); |
83 | 116 |
84 Blacklist blacklist(prefs()); | 117 Blacklist blacklist(prefs()); |
85 TestBlacklist tester(&blacklist); | 118 TestBlacklist tester(&blacklist); |
| 119 BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock(); |
| 120 fetcher->SetState(a, BLACKLISTED_MALWARE); |
| 121 fetcher->SetState(b, BLACKLISTED_MALWARE); |
| 122 blacklist.SetBlacklistStateFetcherForTest(fetcher); |
86 | 123 |
87 blacklist_db()->Enable(); | 124 blacklist_db()->Enable(); |
88 blacklist_db()->SetUnsafe(a, b); | 125 blacklist_db()->SetUnsafe(a, b); |
89 | 126 |
90 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); | 127 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); |
91 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(b)); | 128 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(b)); |
92 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(c)); | 129 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(c)); |
93 | 130 |
94 std::set<std::string> blacklisted_ids; | 131 std::set<std::string> blacklisted_ids; |
95 blacklist.GetMalwareIDs(Set(a, c), base::Bind(&Assign, &blacklisted_ids)); | 132 blacklist.GetMalwareIDs( |
| 133 Set(a, c), base::Bind(&Assign<std::set<std::string> >, &blacklisted_ids)); |
96 base::RunLoop().RunUntilIdle(); | 134 base::RunLoop().RunUntilIdle(); |
97 | 135 |
98 EXPECT_EQ(Set(a), blacklisted_ids); | 136 EXPECT_EQ(Set(a), blacklisted_ids); |
99 } | 137 } |
100 | 138 |
101 TEST_F(BlacklistTest, SafeBrowsing) { | 139 TEST_F(BlacklistTest, SafeBrowsing) { |
102 std::string a = AddExtension("a"); | 140 std::string a = AddExtension("a"); |
103 | 141 |
104 Blacklist blacklist(prefs()); | 142 Blacklist blacklist(prefs()); |
105 TestBlacklist tester(&blacklist); | 143 TestBlacklist tester(&blacklist); |
| 144 BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock(); |
| 145 fetcher->SetState(a, BLACKLISTED_MALWARE); |
| 146 blacklist.SetBlacklistStateFetcherForTest(fetcher); |
106 | 147 |
107 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); | 148 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); |
108 | 149 |
109 blacklist_db()->SetUnsafe(a); | 150 blacklist_db()->SetUnsafe(a); |
110 // The manager is still disabled at this point, so it won't be blacklisted. | 151 // The manager is still disabled at this point, so it won't be blacklisted. |
111 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); | 152 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); |
112 | 153 |
113 blacklist_db()->Enable().NotifyUpdate(); | 154 blacklist_db()->Enable().NotifyUpdate(); |
114 base::RunLoop().RunUntilIdle(); | 155 base::RunLoop().RunUntilIdle(); |
115 // Now it should be. | 156 // Now it should be. |
116 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); | 157 EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); |
117 | 158 |
118 blacklist_db()->ClearUnsafe().NotifyUpdate(); | 159 blacklist_db()->ClearUnsafe().NotifyUpdate(); |
119 // Safe browsing blacklist empty, now enabled. | 160 // Safe browsing blacklist empty, now enabled. |
120 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); | 161 EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); |
121 } | 162 } |
122 | 163 |
123 // Tests that Blacklist clears the old prefs blacklist on startup. | 164 // Tests that Blacklist clears the old prefs blacklist on startup. |
124 TEST_F(BlacklistTest, ClearsPreferencesBlacklist) { | 165 TEST_F(BlacklistTest, ClearsPreferencesBlacklist) { |
125 std::string a = AddExtension("a"); | 166 std::string a = AddExtension("a"); |
126 std::string b = AddExtension("b"); | 167 std::string b = AddExtension("b"); |
127 | 168 |
128 // Blacklist an installed extension. | 169 // Blacklist an installed extension. |
129 prefs()->SetExtensionBlacklisted(a, true); | 170 prefs()->SetExtensionBlacklisted(a, true); |
130 | 171 |
131 // Blacklist some non-installed extensions. This is what the old preferences | 172 // Blacklist some non-installed extensions. This is what the old preferences |
132 // blacklist looked like. | 173 // blacklist looked like. |
133 std::string c = "cccccccccccccccccccccccccccccccc"; | 174 std::string c = "cccccccccccccccccccccccccccccccc"; |
134 std::string d = "dddddddddddddddddddddddddddddddd"; | 175 std::string d = "dddddddddddddddddddddddddddddddd"; |
135 prefs()->SetExtensionBlacklisted(c, true); | 176 prefs()->SetExtensionBlacklisted(c, true); |
136 prefs()->SetExtensionBlacklisted(d, true); | 177 prefs()->SetExtensionBlacklisted(d, true); |
137 | 178 |
138 EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions()); | 179 EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions()); |
139 | 180 |
140 Blacklist blacklist(prefs()); | 181 Blacklist blacklist(prefs()); |
141 TestBlacklist tester(&blacklist); | 182 blacklist.SetBlacklistStateFetcherForTest(new BlacklistStateFetcherMock()); |
142 | 183 |
143 // Blacklist has been cleared. Only the installed extension "a" left. | 184 // Blacklist has been cleared. Only the installed extension "a" left. |
144 EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions()); | 185 EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions()); |
145 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(a).get()); | 186 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(a).get()); |
146 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(b).get()); | 187 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(b).get()); |
147 | 188 |
148 // "a" won't actually be *blacklisted* since it doesn't appear in | 189 // "a" won't actually be *blacklisted* since it doesn't appear in |
149 // safebrowsing. Blacklist no longer reads from prefs. This is purely a | 190 // safebrowsing. Blacklist no longer reads from prefs. This is purely a |
150 // concern of somebody else (currently, ExtensionService). | 191 // concern of somebody else (currently, ExtensionService). |
151 std::set<std::string> blacklisted_ids; | 192 std::set<std::string> blacklisted_ids; |
152 blacklist.GetMalwareIDs(Set(a, b, c, d), | 193 blacklist.GetMalwareIDs(Set(a, b, c, d), |
153 base::Bind(&Assign, &blacklisted_ids)); | 194 base::Bind(&Assign<std::set<std::string> >, |
| 195 &blacklisted_ids)); |
154 base::RunLoop().RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
155 EXPECT_EQ(std::set<std::string>(), blacklisted_ids); | 197 EXPECT_EQ(std::set<std::string>(), blacklisted_ids); |
156 | 198 |
157 // Prefs are still unaffected for installed extensions, though. | 199 // Prefs are still unaffected for installed extensions, though. |
158 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(a)); | 200 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(a)); |
159 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(b)); | 201 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(b)); |
160 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(c)); | 202 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(c)); |
161 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(d)); | 203 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(d)); |
162 } | 204 } |
163 | 205 |
164 } // namespace | 206 // Test getting different blacklist states from Blacklist. |
| 207 TEST_F(BlacklistTest, GetBlacklistStates) { |
| 208 Blacklist blacklist(prefs()); |
| 209 |
| 210 std::string a = AddExtension("a"); |
| 211 std::string b = AddExtension("b"); |
| 212 std::string c = AddExtension("c"); |
| 213 std::string d = AddExtension("d"); |
| 214 std::string e = AddExtension("e"); |
| 215 |
| 216 blacklist_db()->Enable(); |
| 217 blacklist_db()->SetUnsafe(a, b, c, d); |
| 218 |
| 219 // Will be deleted by blacklist destructor. |
| 220 BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock(); |
| 221 fetcher->SetState(a, BLACKLISTED_MALWARE); |
| 222 fetcher->SetState(b, BLACKLISTED_SECURITY_VULNERABILITY); |
| 223 fetcher->SetState(c, BLACKLISTED_CWS_POLICY_VIOLATION); |
| 224 fetcher->SetState(d, BLACKLISTED_POTENTIALLY_UNWANTED); |
| 225 blacklist.SetBlacklistStateFetcherForTest(fetcher); |
| 226 |
| 227 Blacklist::BlacklistStateMap states_abc; |
| 228 Blacklist::BlacklistStateMap states_bcd; |
| 229 blacklist.GetBlacklistedIDs(Set(a, b, c, e), |
| 230 base::Bind(&Assign<Blacklist::BlacklistStateMap>, |
| 231 &states_abc)); |
| 232 blacklist.GetBlacklistedIDs(Set(b, c, d, e), |
| 233 base::Bind(&Assign<Blacklist::BlacklistStateMap>, |
| 234 &states_bcd)); |
| 235 base::RunLoop().RunUntilIdle(); |
| 236 |
| 237 EXPECT_EQ(BLACKLISTED_MALWARE, states_abc[a]); |
| 238 EXPECT_EQ(BLACKLISTED_SECURITY_VULNERABILITY, states_abc[b]); |
| 239 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, states_abc[c]); |
| 240 EXPECT_EQ(BLACKLISTED_SECURITY_VULNERABILITY, states_bcd[b]); |
| 241 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, states_bcd[c]); |
| 242 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states_bcd[d]); |
| 243 EXPECT_EQ(states_abc.end(), states_abc.find(e)); |
| 244 EXPECT_EQ(states_bcd.end(), states_bcd.find(e)); |
| 245 |
| 246 int old_request_count = fetcher->request_count(); |
| 247 Blacklist::BlacklistStateMap states_ad; |
| 248 blacklist.GetBlacklistedIDs(Set(a, d, e), |
| 249 base::Bind(&Assign<Blacklist::BlacklistStateMap>, |
| 250 &states_ad)); |
| 251 base::RunLoop().RunUntilIdle(); |
| 252 EXPECT_EQ(BLACKLISTED_MALWARE, states_ad[a]); |
| 253 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states_ad[d]); |
| 254 EXPECT_EQ(states_ad.end(), states_ad.find(e)); |
| 255 EXPECT_EQ(old_request_count, fetcher->request_count()); |
| 256 } |
| 257 |
| 258 // Test both Blacklist and BlacklistStateFetcher by requesting the blacklist |
| 259 // states, sending fake requests and parsing the responses. |
| 260 TEST_F(BlacklistTest, FetchBlacklistStates) { |
| 261 Blacklist blacklist(prefs()); |
| 262 |
| 263 std::string a = AddExtension("a"); |
| 264 std::string b = AddExtension("b"); |
| 265 std::string c = AddExtension("c"); |
| 266 |
| 267 blacklist_db()->Enable(); |
| 268 blacklist_db()->SetUnsafe(a, b); |
| 269 |
| 270 // Prepare real fetcher. |
| 271 BlacklistStateFetcher* fetcher = new BlacklistStateFetcher(); |
| 272 TestBlacklistStateFetcher fetcher_tester(fetcher); |
| 273 blacklist.SetBlacklistStateFetcherForTest(fetcher); |
| 274 |
| 275 fetcher_tester.SetBlacklistVerdict( |
| 276 a, ClientCRXListInfoResponse_Verdict_CWS_POLICY_VIOLATION); |
| 277 fetcher_tester.SetBlacklistVerdict( |
| 278 b, ClientCRXListInfoResponse_Verdict_POTENTIALLY_UNWANTED); |
| 279 |
| 280 Blacklist::BlacklistStateMap states; |
| 281 blacklist.GetBlacklistedIDs( |
| 282 Set(a, b, c), base::Bind(&Assign<Blacklist::BlacklistStateMap>, &states)); |
| 283 base::RunLoop().RunUntilIdle(); |
| 284 |
| 285 // Two fetchers should be created. |
| 286 EXPECT_TRUE(fetcher_tester.HandleFetcher(0)); |
| 287 EXPECT_TRUE(fetcher_tester.HandleFetcher(1)); |
| 288 |
| 289 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, states[a]); |
| 290 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states[b]); |
| 291 EXPECT_EQ(states.end(), states.find(c)); |
| 292 |
| 293 Blacklist::BlacklistStateMap cached_states; |
| 294 |
| 295 blacklist.GetBlacklistedIDs( |
| 296 Set(a, b, c), base::Bind(&Assign<Blacklist::BlacklistStateMap>, |
| 297 &cached_states)); |
| 298 base::RunLoop().RunUntilIdle(); |
| 299 |
| 300 // No new fetchers. |
| 301 EXPECT_FALSE(fetcher_tester.HandleFetcher(2)); |
| 302 EXPECT_EQ(BLACKLISTED_CWS_POLICY_VIOLATION, cached_states[a]); |
| 303 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, cached_states[b]); |
| 304 EXPECT_EQ(cached_states.end(), cached_states.find(c)); |
| 305 } |
| 306 |
165 } // namespace extensions | 307 } // namespace extensions |
OLD | NEW |