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 3 // found in the LICENSE file. 4 5 #include "base/bind.h" 6 #include "base/message_loop/message_loop.h" 7 #include "base/run_loop.h" 8 #include "chrome/browser/extensions/blacklist.h" 9 #include "chrome/browser/extensions/extension_prefs.h" 10 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" 11 #include "chrome/browser/extensions/test_blacklist.h" 12 #include "chrome/browser/extensions/test_extension_prefs.h" 13 #include "content/public/test/test_browser_thread_bundle.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace extensions { 17 namespace { 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 45 class BlacklistTest : public testing::Test { 46 public: 47 BlacklistTest() 48 : test_prefs_(base::MessageLoopProxy::current()), 49 blacklist_db_(new FakeSafeBrowsingDatabaseManager(false)), 50 scoped_blacklist_db_(blacklist_db_) {} 51 52 protected: 53 ExtensionPrefs* prefs() { 54 return test_prefs_.prefs(); 55 } 56 57 FakeSafeBrowsingDatabaseManager* blacklist_db() { 58 return blacklist_db_.get(); 59 } 60 61 std::string AddExtension(const std::string& id) { 62 return test_prefs_.AddExtension(id)->id(); 63 } 64 65 private: 66 content::TestBrowserThreadBundle browser_thread_bundle_; 67 68 TestExtensionPrefs test_prefs_; 69 70 scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db_; 71 72 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db_; 73 }; 74 75 void Assign(std::set<std::string> *to, const std::set<std::string>& from) { 76 *to = from; 77 } 78 79 TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { 80 std::string a = AddExtension("a"); 81 std::string b = AddExtension("b"); 82 std::string c = AddExtension("c"); 83 84 Blacklist blacklist(prefs()); 85 TestBlacklist tester(&blacklist); 86 87 blacklist_db()->Enable(); 88 blacklist_db()->SetUnsafe(a, b); 89 90 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); 91 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(b)); 92 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(c)); 93 94 std::set<std::string> blacklisted_ids; 95 blacklist.GetMalwareIDs(Set(a, c), base::Bind(&Assign, &blacklisted_ids)); 96 base::RunLoop().RunUntilIdle(); 97 98 EXPECT_EQ(Set(a), blacklisted_ids); 99 } 100 101 TEST_F(BlacklistTest, SafeBrowsing) { 102 std::string a = AddExtension("a"); 103 104 Blacklist blacklist(prefs()); 105 TestBlacklist tester(&blacklist); 106 107 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); 108 109 blacklist_db()->SetUnsafe(a); 110 // The manager is still disabled at this point, so it won't be blacklisted. 111 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); 112 113 blacklist_db()->Enable().NotifyUpdate(); 114 base::RunLoop().RunUntilIdle(); 115 // Now it should be. 116 EXPECT_EQ(Blacklist::BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); 117 118 blacklist_db()->ClearUnsafe().NotifyUpdate(); 119 // Safe browsing blacklist empty, now enabled. 120 EXPECT_EQ(Blacklist::NOT_BLACKLISTED, tester.GetBlacklistState(a)); 121 } 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.GetMalwareIDs(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 165 } // namespace extensions 166