| 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/extension_prefs.h" | 9 #include "chrome/browser/extensions/extension_prefs.h" |
| 10 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" | 10 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" |
| 11 #include "chrome/browser/extensions/test_blacklist.h" | 11 #include "chrome/browser/extensions/test_blacklist.h" |
| 12 #include "chrome/browser/extensions/test_extension_prefs.h" | 12 #include "chrome/browser/extensions/test_extension_prefs.h" |
| 13 #include "content/public/test/test_browser_thread.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace extensions { | 16 namespace extensions { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 std::set<std::string> Set(const std::string& a) { |
| 20 std::set<std::string> set; |
| 21 set.insert(a); |
| 22 return set; |
| 23 } |
| 24 std::set<std::string> Set(const std::string& a, const std::string& b) { |
| 25 std::set<std::string> set = Set(a); |
| 26 set.insert(b); |
| 27 return set; |
| 28 } |
| 29 std::set<std::string> Set(const std::string& a, |
| 30 const std::string& b, |
| 31 const std::string& c) { |
| 32 std::set<std::string> set = Set(a, b); |
| 33 set.insert(c); |
| 34 return set; |
| 35 } |
| 36 std::set<std::string> Set(const std::string& a, |
| 37 const std::string& b, |
| 38 const std::string& d, |
| 39 const std::string& c) { |
| 40 std::set<std::string> set = Set(a, b, c); |
| 41 set.insert(d); |
| 42 return set; |
| 43 } |
| 44 |
| 19 class BlacklistTest : public testing::Test { | 45 class BlacklistTest : public testing::Test { |
| 20 public: | 46 public: |
| 21 BlacklistTest() | 47 BlacklistTest() |
| 22 : prefs_(message_loop_.message_loop_proxy().get()), | 48 : test_prefs_(base::MessageLoopProxy::current()), |
| 23 ui_thread_(content::BrowserThread::UI, &message_loop_), | 49 blacklist_db_(new FakeSafeBrowsingDatabaseManager(false)), |
| 24 io_thread_(content::BrowserThread::IO, &message_loop_), | 50 scoped_blacklist_db_(blacklist_db_) {} |
| 25 safe_browsing_database_manager_(new FakeSafeBrowsingDatabaseManager()), | |
| 26 scoped_blacklist_database_manager_(safe_browsing_database_manager_), | |
| 27 blacklist_(prefs_.prefs()) {} | |
| 28 | 51 |
| 29 bool IsBlacklisted(const Extension* extension) { | 52 protected: |
| 30 return TestBlacklist(&blacklist_).IsBlacklisted(extension->id()); | 53 ExtensionPrefs* prefs() { |
| 54 return test_prefs_.prefs(); |
| 31 } | 55 } |
| 32 | 56 |
| 33 protected: | 57 FakeSafeBrowsingDatabaseManager* blacklist_db() { |
| 34 base::MessageLoop message_loop_; | 58 return blacklist_db_.get(); |
| 59 } |
| 35 | 60 |
| 36 TestExtensionPrefs prefs_; | 61 std::string AddExtension(const std::string& id) { |
| 62 return test_prefs_.AddExtension(id)->id(); |
| 63 } |
| 37 | 64 |
| 38 content::TestBrowserThread ui_thread_; | 65 private: |
| 39 content::TestBrowserThread io_thread_; | 66 content::TestBrowserThreadBundle browser_thread_bundle_; |
| 40 | 67 |
| 41 scoped_refptr<FakeSafeBrowsingDatabaseManager> | 68 TestExtensionPrefs test_prefs_; |
| 42 safe_browsing_database_manager_; | |
| 43 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_database_manager_; | |
| 44 | 69 |
| 45 Blacklist blacklist_; | 70 scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db_; |
| 71 |
| 72 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db_; |
| 46 }; | 73 }; |
| 47 | 74 |
| 48 TEST_F(BlacklistTest, SetFromUpdater) { | |
| 49 scoped_refptr<const Extension> extension_a = prefs_.AddExtension("a"); | |
| 50 scoped_refptr<const Extension> extension_b = prefs_.AddExtension("b"); | |
| 51 scoped_refptr<const Extension> extension_c = prefs_.AddExtension("c"); | |
| 52 scoped_refptr<const Extension> extension_d = prefs_.AddExtension("d"); | |
| 53 | |
| 54 // c, d, start blacklisted. | |
| 55 prefs_.prefs()->SetExtensionBlacklisted(extension_c->id(), true); | |
| 56 prefs_.prefs()->SetExtensionBlacklisted(extension_d->id(), true); | |
| 57 | |
| 58 EXPECT_FALSE(IsBlacklisted(extension_a.get())); | |
| 59 EXPECT_FALSE(IsBlacklisted(extension_b.get())); | |
| 60 EXPECT_TRUE(IsBlacklisted(extension_c.get())); | |
| 61 EXPECT_TRUE(IsBlacklisted(extension_d.get())); | |
| 62 | |
| 63 // Mix up the blacklist. | |
| 64 { | |
| 65 std::vector<std::string> blacklist; | |
| 66 blacklist.push_back(extension_b->id()); | |
| 67 blacklist.push_back(extension_c->id()); | |
| 68 blacklist_.SetFromUpdater(blacklist, "1"); | |
| 69 } | |
| 70 EXPECT_FALSE(IsBlacklisted(extension_a.get())); | |
| 71 EXPECT_TRUE(IsBlacklisted(extension_b.get())); | |
| 72 EXPECT_TRUE(IsBlacklisted(extension_c.get())); | |
| 73 EXPECT_FALSE(IsBlacklisted(extension_d.get())); | |
| 74 | |
| 75 // No-op, just in case. | |
| 76 { | |
| 77 std::vector<std::string> blacklist; | |
| 78 blacklist.push_back(extension_b->id()); | |
| 79 blacklist.push_back(extension_c->id()); | |
| 80 blacklist_.SetFromUpdater(blacklist, "2"); | |
| 81 } | |
| 82 EXPECT_FALSE(IsBlacklisted(extension_a.get())); | |
| 83 EXPECT_TRUE(IsBlacklisted(extension_b.get())); | |
| 84 EXPECT_TRUE(IsBlacklisted(extension_c.get())); | |
| 85 EXPECT_FALSE(IsBlacklisted(extension_d.get())); | |
| 86 | |
| 87 // Strictly increase the blacklist. | |
| 88 { | |
| 89 std::vector<std::string> blacklist; | |
| 90 blacklist.push_back(extension_a->id()); | |
| 91 blacklist.push_back(extension_b->id()); | |
| 92 blacklist.push_back(extension_c->id()); | |
| 93 blacklist.push_back(extension_d->id()); | |
| 94 blacklist_.SetFromUpdater(blacklist, "3"); | |
| 95 } | |
| 96 EXPECT_TRUE(IsBlacklisted(extension_a.get())); | |
| 97 EXPECT_TRUE(IsBlacklisted(extension_b.get())); | |
| 98 EXPECT_TRUE(IsBlacklisted(extension_c.get())); | |
| 99 EXPECT_TRUE(IsBlacklisted(extension_d.get())); | |
| 100 | |
| 101 // Strictly decrease the blacklist. | |
| 102 { | |
| 103 std::vector<std::string> blacklist; | |
| 104 blacklist.push_back(extension_a->id()); | |
| 105 blacklist.push_back(extension_b->id()); | |
| 106 blacklist_.SetFromUpdater(blacklist, "4"); | |
| 107 } | |
| 108 EXPECT_TRUE(IsBlacklisted(extension_a.get())); | |
| 109 EXPECT_TRUE(IsBlacklisted(extension_b.get())); | |
| 110 EXPECT_FALSE(IsBlacklisted(extension_c.get())); | |
| 111 EXPECT_FALSE(IsBlacklisted(extension_d.get())); | |
| 112 | |
| 113 // Clear the blacklist. | |
| 114 { | |
| 115 std::vector<std::string> blacklist; | |
| 116 blacklist_.SetFromUpdater(blacklist, "5"); | |
| 117 } | |
| 118 EXPECT_FALSE(IsBlacklisted(extension_a.get())); | |
| 119 EXPECT_FALSE(IsBlacklisted(extension_b.get())); | |
| 120 EXPECT_FALSE(IsBlacklisted(extension_c.get())); | |
| 121 EXPECT_FALSE(IsBlacklisted(extension_d.get())); | |
| 122 } | |
| 123 | |
| 124 void Assign(std::set<std::string> *to, const std::set<std::string>& from) { | 75 void Assign(std::set<std::string> *to, const std::set<std::string>& from) { |
| 125 *to = from; | 76 *to = from; |
| 126 } | 77 } |
| 127 | 78 |
| 128 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { | 79 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { |
| 129 scoped_refptr<const Extension> extension_a = prefs_.AddExtension("a"); | 80 std::string a = AddExtension("a"); |
| 130 scoped_refptr<const Extension> extension_b = prefs_.AddExtension("b"); | 81 std::string b = AddExtension("b"); |
| 131 scoped_refptr<const Extension> extension_c = prefs_.AddExtension("c"); | 82 std::string c = AddExtension("c"); |
| 132 | 83 |
| 133 { | 84 Blacklist blacklist(prefs()); |
| 134 std::vector<std::string> blacklist; | 85 TestBlacklist tester(&blacklist); |
| 135 blacklist.push_back(extension_a->id()); | |
| 136 blacklist.push_back(extension_b->id()); | |
| 137 blacklist_.SetFromUpdater(blacklist, "1"); | |
| 138 base::RunLoop().RunUntilIdle(); | |
| 139 } | |
| 140 | 86 |
| 141 std::set<std::string> blacklist_actual; | 87 blacklist_db()->Enable(); |
| 142 { | 88 blacklist_db()->SetUnsafe(a, b); |
| 143 std::set<std::string> blacklist_query; | |
| 144 blacklist_query.insert(extension_a->id()); | |
| 145 blacklist_query.insert(extension_c->id()); | |
| 146 blacklist_.GetBlacklistedIDs(blacklist_query, | |
| 147 base::Bind(&Assign, &blacklist_actual)); | |
| 148 base::RunLoop().RunUntilIdle(); | |
| 149 } | |
| 150 | 89 |
| 151 std::set<std::string> blacklist_expected; | 90 EXPECT_TRUE(tester.IsBlacklisted(a)); |
| 152 blacklist_expected.insert(extension_a->id()); | 91 EXPECT_TRUE(tester.IsBlacklisted(b)); |
| 153 EXPECT_EQ(blacklist_expected, blacklist_actual); | 92 EXPECT_FALSE(tester.IsBlacklisted(c)); |
| 93 |
| 94 std::set<std::string> blacklisted_ids; |
| 95 blacklist.GetBlacklistedIDs(Set(a, c), base::Bind(&Assign, &blacklisted_ids)); |
| 96 base::RunLoop().RunUntilIdle(); |
| 97 |
| 98 EXPECT_EQ(Set(a), blacklisted_ids); |
| 154 } | 99 } |
| 155 | 100 |
| 156 TEST_F(BlacklistTest, PrefsVsSafeBrowsing) { | 101 TEST_F(BlacklistTest, SafeBrowsing) { |
| 157 scoped_refptr<const Extension> extension_a = prefs_.AddExtension("a"); | 102 std::string a = AddExtension("a"); |
| 158 scoped_refptr<const Extension> extension_b = prefs_.AddExtension("b"); | |
| 159 scoped_refptr<const Extension> extension_c = prefs_.AddExtension("c"); | |
| 160 | 103 |
| 161 // Prefs have a and b blacklisted, safebrowsing has b and c. | 104 Blacklist blacklist(prefs()); |
| 162 prefs_.prefs()->SetExtensionBlacklisted(extension_a->id(), true); | 105 TestBlacklist tester(&blacklist); |
| 163 prefs_.prefs()->SetExtensionBlacklisted(extension_b->id(), true); | |
| 164 { | |
| 165 std::set<std::string> bc; | |
| 166 bc.insert(extension_b->id()); | |
| 167 bc.insert(extension_c->id()); | |
| 168 safe_browsing_database_manager_->set_unsafe_ids(bc); | |
| 169 } | |
| 170 | 106 |
| 171 // The manager is still disabled at this point, so c won't be blacklisted. | 107 EXPECT_FALSE(tester.IsBlacklisted(a)); |
| 172 EXPECT_TRUE(IsBlacklisted(extension_a.get())); | |
| 173 EXPECT_TRUE(IsBlacklisted(extension_b.get())); | |
| 174 EXPECT_FALSE(IsBlacklisted(extension_c.get())); | |
| 175 | 108 |
| 109 blacklist_db()->SetUnsafe(a); |
| 110 // The manager is still disabled at this point, so it won't be blacklisted. |
| 111 EXPECT_FALSE(tester.IsBlacklisted(a)); |
| 112 |
| 113 blacklist_db()->Enable().NotifyUpdate(); |
| 114 base::RunLoop().RunUntilIdle(); |
| 176 // Now it should be. | 115 // Now it should be. |
| 177 safe_browsing_database_manager_->set_enabled(true); | 116 EXPECT_TRUE(tester.IsBlacklisted(a)); |
| 178 EXPECT_TRUE(IsBlacklisted(extension_a.get())); | |
| 179 EXPECT_TRUE(IsBlacklisted(extension_b.get())); | |
| 180 EXPECT_TRUE(IsBlacklisted(extension_c.get())); | |
| 181 | 117 |
| 182 // Corner case: nothing in safebrowsing (but still enabled). | 118 blacklist_db()->ClearUnsafe().NotifyUpdate(); |
| 183 safe_browsing_database_manager_->set_unsafe_ids(std::set<std::string>()); | 119 // Safe browsing blacklist empty, now enabled. |
| 184 EXPECT_TRUE(IsBlacklisted(extension_a.get())); | 120 EXPECT_FALSE(tester.IsBlacklisted(a)); |
| 185 EXPECT_TRUE(IsBlacklisted(extension_b.get())); | |
| 186 EXPECT_FALSE(IsBlacklisted(extension_c.get())); | |
| 187 | |
| 188 // Corner case: nothing in prefs. | |
| 189 prefs_.prefs()->SetExtensionBlacklisted(extension_a->id(), false); | |
| 190 prefs_.prefs()->SetExtensionBlacklisted(extension_b->id(), false); | |
| 191 { | |
| 192 std::set<std::string> bc; | |
| 193 bc.insert(extension_b->id()); | |
| 194 bc.insert(extension_c->id()); | |
| 195 safe_browsing_database_manager_->set_unsafe_ids(bc); | |
| 196 } | |
| 197 EXPECT_FALSE(IsBlacklisted(extension_a.get())); | |
| 198 EXPECT_TRUE(IsBlacklisted(extension_b.get())); | |
| 199 EXPECT_TRUE(IsBlacklisted(extension_c.get())); | |
| 200 } | 121 } |
| 201 | 122 |
| 123 // Tests that Blacklist clears the old prefs blacklist on startup. |
| 124 TEST_F(BlacklistTest, ClearsPreferencesBlacklist) { |
| 125 std::string a = AddExtension("a"); |
| 126 std::string b = AddExtension("b"); |
| 127 |
| 128 // Blacklist an installed extension. |
| 129 prefs()->SetExtensionBlacklisted(a, true); |
| 130 |
| 131 // Blacklist some non-installed extensions. This is what the old preferences |
| 132 // blacklist looked like. |
| 133 std::string c = "cccccccccccccccccccccccccccccccc"; |
| 134 std::string d = "dddddddddddddddddddddddddddddddd"; |
| 135 prefs()->SetExtensionBlacklisted(c, true); |
| 136 prefs()->SetExtensionBlacklisted(d, true); |
| 137 |
| 138 EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions()); |
| 139 |
| 140 Blacklist blacklist(prefs()); |
| 141 TestBlacklist tester(&blacklist); |
| 142 |
| 143 // Blacklist has been cleared. Only the installed extension "a" left. |
| 144 EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions()); |
| 145 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(a).get()); |
| 146 EXPECT_TRUE(prefs()->GetInstalledExtensionInfo(b).get()); |
| 147 |
| 148 // "a" won't actually be *blacklisted* since it doesn't appear in |
| 149 // safebrowsing. Blacklist no longer reads from prefs. This is purely a |
| 150 // concern of somebody else (currently, ExtensionService). |
| 151 std::set<std::string> blacklisted_ids; |
| 152 blacklist.GetBlacklistedIDs(Set(a, b, c, d), |
| 153 base::Bind(&Assign, &blacklisted_ids)); |
| 154 base::RunLoop().RunUntilIdle(); |
| 155 EXPECT_EQ(std::set<std::string>(), blacklisted_ids); |
| 156 |
| 157 // Prefs are still unaffected for installed extensions, though. |
| 158 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(a)); |
| 159 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(b)); |
| 160 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(c)); |
| 161 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(d)); |
| 162 } |
| 163 |
| 164 } // namespace |
| 202 } // namespace extensions | 165 } // namespace extensions |
| 203 } // namespace | |
| OLD | NEW |