Commit f71b582c authored by oleg@chromium.org's avatar oleg@chromium.org

Manage all the testing classes for Blacklist in TestBlacklist.

BUG=267514

Review URL: https://codereview.chromium.org/119963004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@244178 0039d316-1c4b-4281-b951-d872f2087c98
parent 70d04c31
...@@ -317,6 +317,10 @@ void Blacklist::SetBlacklistStateFetcherForTest( ...@@ -317,6 +317,10 @@ void Blacklist::SetBlacklistStateFetcherForTest(
state_fetcher_.reset(fetcher); state_fetcher_.reset(fetcher);
} }
BlacklistStateFetcher* Blacklist::ResetBlacklistStateFetcherForTest() {
return state_fetcher_.release();
}
void Blacklist::AddObserver(Observer* observer) { void Blacklist::AddObserver(Observer* observer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
observers_.AddObserver(observer); observers_.AddObserver(observer);
......
...@@ -93,9 +93,14 @@ class Blacklist : public content::NotificationObserver, ...@@ -93,9 +93,14 @@ class Blacklist : public content::NotificationObserver,
void IsBlacklisted(const std::string& extension_id, void IsBlacklisted(const std::string& extension_id,
const IsBlacklistedCallback& callback); const IsBlacklistedCallback& callback);
// Used to mock BlacklistStateFetcher in unit tests. // Used to mock BlacklistStateFetcher in unit tests. Blacklist owns the
// |fetcher|.
void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher); void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher);
// Reset the owned BlacklistStateFetcher to null and return the current
// BlacklistStateFetcher.
BlacklistStateFetcher* ResetBlacklistStateFetcherForTest();
// Adds/removes an observer to the blacklist. // Adds/removes an observer to the blacklist.
void AddObserver(Observer* observer); void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer); void RemoveObserver(Observer* observer);
......
...@@ -48,19 +48,13 @@ std::set<std::string> Set(const std::string& a, ...@@ -48,19 +48,13 @@ std::set<std::string> Set(const std::string& a,
class BlacklistTest : public testing::Test { class BlacklistTest : public testing::Test {
public: public:
BlacklistTest() BlacklistTest()
: test_prefs_(base::MessageLoopProxy::current()), : test_prefs_(base::MessageLoopProxy::current()) {}
blacklist_db_(new FakeSafeBrowsingDatabaseManager(false)),
scoped_blacklist_db_(blacklist_db_) {}
protected: protected:
ExtensionPrefs* prefs() { ExtensionPrefs* prefs() {
return test_prefs_.prefs(); return test_prefs_.prefs();
} }
FakeSafeBrowsingDatabaseManager* blacklist_db() {
return blacklist_db_.get();
}
std::string AddExtension(const std::string& id) { std::string AddExtension(const std::string& id) {
return test_prefs_.AddExtension(id)->id(); return test_prefs_.AddExtension(id)->id();
} }
...@@ -69,40 +63,6 @@ class BlacklistTest : public testing::Test { ...@@ -69,40 +63,6 @@ class BlacklistTest : public testing::Test {
content::TestBrowserThreadBundle browser_thread_bundle_; content::TestBrowserThreadBundle browser_thread_bundle_;
TestExtensionPrefs test_prefs_; TestExtensionPrefs test_prefs_;
scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db_;
Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db_;
};
class BlacklistStateFetcherMock : public BlacklistStateFetcher {
public:
BlacklistStateFetcherMock() : request_count_(0) {
}
virtual void Request(const std::string& id,
const RequestCallback& callback) OVERRIDE {
request_count_++;
BlacklistState result = NOT_BLACKLISTED;
if (ContainsKey(states_, id))
result = states_[id];
base::MessageLoopProxy::current()->PostTask(FROM_HERE,
base::Bind(callback, result));
}
void SetState(const std::string& id, BlacklistState state) {
states_[id] = state;
}
int request_count() const {
return request_count_;
}
private:
std::map<std::string, BlacklistState> states_;
int request_count_;
}; };
template<typename T> template<typename T>
...@@ -119,13 +79,8 @@ TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) { ...@@ -119,13 +79,8 @@ TEST_F(BlacklistTest, OnlyIncludesRequestedIDs) {
Blacklist blacklist(prefs()); Blacklist blacklist(prefs());
TestBlacklist tester(&blacklist); TestBlacklist tester(&blacklist);
BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock(); tester.SetBlacklistState(a, BLACKLISTED_MALWARE, false);
fetcher->SetState(a, BLACKLISTED_MALWARE); tester.SetBlacklistState(b, BLACKLISTED_MALWARE, false);
fetcher->SetState(b, BLACKLISTED_MALWARE);
blacklist.SetBlacklistStateFetcherForTest(fetcher);
blacklist_db()->Enable();
blacklist_db()->SetUnsafe(a, b);
EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a));
EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(b)); EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(b));
...@@ -144,22 +99,21 @@ TEST_F(BlacklistTest, SafeBrowsing) { ...@@ -144,22 +99,21 @@ TEST_F(BlacklistTest, SafeBrowsing) {
Blacklist blacklist(prefs()); Blacklist blacklist(prefs());
TestBlacklist tester(&blacklist); TestBlacklist tester(&blacklist);
BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock(); tester.DisableSafeBrowsing();
fetcher->SetState(a, BLACKLISTED_MALWARE);
blacklist.SetBlacklistStateFetcherForTest(fetcher);
EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a));
blacklist_db()->SetUnsafe(a); tester.SetBlacklistState(a, BLACKLISTED_MALWARE, false);
// The manager is still disabled at this point, so it won't be blacklisted. // The manager is still disabled at this point, so it won't be blacklisted.
EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a));
blacklist_db()->Enable().NotifyUpdate(); tester.EnableSafeBrowsing();
tester.NotifyUpdate();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Now it should be. // Now it should be.
EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a)); EXPECT_EQ(BLACKLISTED_MALWARE, tester.GetBlacklistState(a));
blacklist_db()->ClearUnsafe().NotifyUpdate(); tester.Clear(true);
// Safe browsing blacklist empty, now enabled. // Safe browsing blacklist empty, now enabled.
EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a)); EXPECT_EQ(NOT_BLACKLISTED, tester.GetBlacklistState(a));
} }
...@@ -182,7 +136,7 @@ TEST_F(BlacklistTest, ClearsPreferencesBlacklist) { ...@@ -182,7 +136,7 @@ TEST_F(BlacklistTest, ClearsPreferencesBlacklist) {
EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions()); EXPECT_EQ(Set(a, c, d), prefs()->GetBlacklistedExtensions());
Blacklist blacklist(prefs()); Blacklist blacklist(prefs());
blacklist.SetBlacklistStateFetcherForTest(new BlacklistStateFetcherMock()); TestBlacklist tester(&blacklist);
// Blacklist has been cleared. Only the installed extension "a" left. // Blacklist has been cleared. Only the installed extension "a" left.
EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions()); EXPECT_EQ(Set(a), prefs()->GetBlacklistedExtensions());
...@@ -209,6 +163,7 @@ TEST_F(BlacklistTest, ClearsPreferencesBlacklist) { ...@@ -209,6 +163,7 @@ TEST_F(BlacklistTest, ClearsPreferencesBlacklist) {
// Test getting different blacklist states from Blacklist. // Test getting different blacklist states from Blacklist.
TEST_F(BlacklistTest, GetBlacklistStates) { TEST_F(BlacklistTest, GetBlacklistStates) {
Blacklist blacklist(prefs()); Blacklist blacklist(prefs());
TestBlacklist tester(&blacklist);
std::string a = AddExtension("a"); std::string a = AddExtension("a");
std::string b = AddExtension("b"); std::string b = AddExtension("b");
...@@ -216,16 +171,10 @@ TEST_F(BlacklistTest, GetBlacklistStates) { ...@@ -216,16 +171,10 @@ TEST_F(BlacklistTest, GetBlacklistStates) {
std::string d = AddExtension("d"); std::string d = AddExtension("d");
std::string e = AddExtension("e"); std::string e = AddExtension("e");
blacklist_db()->Enable(); tester.SetBlacklistState(a, BLACKLISTED_MALWARE, false);
blacklist_db()->SetUnsafe(a, b, c, d); tester.SetBlacklistState(b, BLACKLISTED_SECURITY_VULNERABILITY, false);
tester.SetBlacklistState(c, BLACKLISTED_CWS_POLICY_VIOLATION, false);
// Will be deleted by blacklist destructor. tester.SetBlacklistState(d, BLACKLISTED_POTENTIALLY_UNWANTED, false);
BlacklistStateFetcherMock* fetcher = new BlacklistStateFetcherMock();
fetcher->SetState(a, BLACKLISTED_MALWARE);
fetcher->SetState(b, BLACKLISTED_SECURITY_VULNERABILITY);
fetcher->SetState(c, BLACKLISTED_CWS_POLICY_VIOLATION);
fetcher->SetState(d, BLACKLISTED_POTENTIALLY_UNWANTED);
blacklist.SetBlacklistStateFetcherForTest(fetcher);
Blacklist::BlacklistStateMap states_abc; Blacklist::BlacklistStateMap states_abc;
Blacklist::BlacklistStateMap states_bcd; Blacklist::BlacklistStateMap states_bcd;
...@@ -246,7 +195,7 @@ TEST_F(BlacklistTest, GetBlacklistStates) { ...@@ -246,7 +195,7 @@ TEST_F(BlacklistTest, GetBlacklistStates) {
EXPECT_EQ(states_abc.end(), states_abc.find(e)); EXPECT_EQ(states_abc.end(), states_abc.find(e));
EXPECT_EQ(states_bcd.end(), states_bcd.find(e)); EXPECT_EQ(states_bcd.end(), states_bcd.find(e));
int old_request_count = fetcher->request_count(); int old_request_count = tester.fetcher()->request_count();
Blacklist::BlacklistStateMap states_ad; Blacklist::BlacklistStateMap states_ad;
blacklist.GetBlacklistedIDs(Set(a, d, e), blacklist.GetBlacklistedIDs(Set(a, d, e),
base::Bind(&Assign<Blacklist::BlacklistStateMap>, base::Bind(&Assign<Blacklist::BlacklistStateMap>,
...@@ -255,20 +204,23 @@ TEST_F(BlacklistTest, GetBlacklistStates) { ...@@ -255,20 +204,23 @@ TEST_F(BlacklistTest, GetBlacklistStates) {
EXPECT_EQ(BLACKLISTED_MALWARE, states_ad[a]); EXPECT_EQ(BLACKLISTED_MALWARE, states_ad[a]);
EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states_ad[d]); EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED, states_ad[d]);
EXPECT_EQ(states_ad.end(), states_ad.find(e)); EXPECT_EQ(states_ad.end(), states_ad.find(e));
EXPECT_EQ(old_request_count, fetcher->request_count()); EXPECT_EQ(old_request_count, tester.fetcher()->request_count());
} }
// Test both Blacklist and BlacklistStateFetcher by requesting the blacklist // Test both Blacklist and BlacklistStateFetcher by requesting the blacklist
// states, sending fake requests and parsing the responses. // states, sending fake requests and parsing the responses.
TEST_F(BlacklistTest, FetchBlacklistStates) { TEST_F(BlacklistTest, FetchBlacklistStates) {
Blacklist blacklist(prefs()); Blacklist blacklist(prefs());
scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db(
new FakeSafeBrowsingDatabaseManager(true));
Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db);
std::string a = AddExtension("a"); std::string a = AddExtension("a");
std::string b = AddExtension("b"); std::string b = AddExtension("b");
std::string c = AddExtension("c"); std::string c = AddExtension("c");
blacklist_db()->Enable(); blacklist_db->Enable();
blacklist_db()->SetUnsafe(a, b); blacklist_db->SetUnsafe(a, b);
// Prepare real fetcher. // Prepare real fetcher.
BlacklistStateFetcher* fetcher = new BlacklistStateFetcher(); BlacklistStateFetcher* fetcher = new BlacklistStateFetcher();
......
...@@ -853,6 +853,15 @@ class ExtensionService ...@@ -853,6 +853,15 @@ class ExtensionService
InstallAppsWithUnlimtedStorage); InstallAppsWithUnlimtedStorage);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest, FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
InstallAppsAndCheckStorageProtection); InstallAppsAndCheckStorageProtection);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest, SetUnsetBlacklistInPrefs);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
BlacklistedExtensionWillNotInstall);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
UnloadBlacklistedExtensionPolicy);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
WillNotLoadBlacklistedExtensionsFromDirectory);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
BlacklistedInPrefsFromStartup);
DISALLOW_COPY_AND_ASSIGN(ExtensionService); DISALLOW_COPY_AND_ASSIGN(ExtensionService);
}; };
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
#include "chrome/browser/extensions/install_tracker_factory.h" #include "chrome/browser/extensions/install_tracker_factory.h"
#include "chrome/browser/extensions/installed_loader.h" #include "chrome/browser/extensions/installed_loader.h"
#include "chrome/browser/extensions/pack_extension_job.h" #include "chrome/browser/extensions/pack_extension_job.h"
#include "chrome/browser/extensions/test_blacklist.h"
#include "chrome/browser/extensions/test_extension_system.h" #include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/extensions/unpacked_installer.h" #include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/extensions/updater/extension_updater.h" #include "chrome/browser/extensions/updater/extension_updater.h"
...@@ -3361,12 +3362,10 @@ TEST_F(ExtensionServiceTest, UpdatePendingExtensionAlreadyInstalled) { ...@@ -3361,12 +3362,10 @@ TEST_F(ExtensionServiceTest, UpdatePendingExtensionAlreadyInstalled) {
// Tests blacklisting then unblacklisting extensions after the service has been // Tests blacklisting then unblacklisting extensions after the service has been
// initialized. // initialized.
TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) { TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) {
scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db( extensions::TestBlacklist test_blacklist;
new FakeSafeBrowsingDatabaseManager(true));
Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db);
// A profile with 3 extensions installed: good0, good1, and good2. // A profile with 3 extensions installed: good0, good1, and good2.
InitializeGoodInstalledExtensionService(); InitializeGoodInstalledExtensionService();
test_blacklist.Attach(service_->blacklist_);
service_->Init(); service_->Init();
ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get()); ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
...@@ -3388,7 +3387,12 @@ TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) { ...@@ -3388,7 +3387,12 @@ TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) {
EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist")); EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
// Blacklist good0 and good1 (and an invalid extension ID). // Blacklist good0 and good1 (and an invalid extension ID).
blacklist_db->SetUnsafe(good0, good1, "invalid_id").NotifyUpdate(); test_blacklist.SetBlacklistState(
good0, extensions::BLACKLISTED_MALWARE, true);
test_blacklist.SetBlacklistState(
good1, extensions::BLACKLISTED_MALWARE, true);
test_blacklist.SetBlacklistState(
"invalid_id", extensions::BLACKLISTED_MALWARE, true);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(!enabled_extensions.Contains(good0) && EXPECT_TRUE(!enabled_extensions.Contains(good0) &&
...@@ -3404,7 +3408,13 @@ TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) { ...@@ -3404,7 +3408,13 @@ TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) {
EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist")); EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
// Un-blacklist good1 and blacklist good2. // Un-blacklist good1 and blacklist good2.
blacklist_db->SetUnsafe(good0, good2, "invalid_id").NotifyUpdate(); test_blacklist.Clear(false);
test_blacklist.SetBlacklistState(
good0, extensions::BLACKLISTED_MALWARE, true);
test_blacklist.SetBlacklistState(
good2, extensions::BLACKLISTED_MALWARE, true);
test_blacklist.SetBlacklistState(
"invalid_id", extensions::BLACKLISTED_MALWARE, true);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(!enabled_extensions.Contains(good0) && EXPECT_TRUE(!enabled_extensions.Contains(good0) &&
...@@ -3447,12 +3457,11 @@ TEST_F(ExtensionServiceTest, BlacklistedExtensionWillNotInstall) { ...@@ -3447,12 +3457,11 @@ TEST_F(ExtensionServiceTest, BlacklistedExtensionWillNotInstall) {
#if defined(ENABLE_BLACKLIST_TESTS) #if defined(ENABLE_BLACKLIST_TESTS)
// Unload blacklisted extension on policy change. // Unload blacklisted extension on policy change.
TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) { TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) {
scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db( extensions::TestBlacklist test_blacklist;
new FakeSafeBrowsingDatabaseManager(true));
Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db);
// A profile with no extensions installed. // A profile with no extensions installed.
InitializeEmptyExtensionService(); InitializeEmptyExtensionService();
test_blacklist.Attach(service_->blacklist_);
base::FilePath path = data_dir_.AppendASCII("good.crx"); base::FilePath path = data_dir_.AppendASCII("good.crx");
...@@ -3466,7 +3475,8 @@ TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) { ...@@ -3466,7 +3475,8 @@ TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) {
whitelist.Append(new base::StringValue(good_crx)); whitelist.Append(new base::StringValue(good_crx));
prefs->Set(prefs::kExtensionInstallAllowList, whitelist); prefs->Set(prefs::kExtensionInstallAllowList, whitelist);
blacklist_db->SetUnsafe(good_crx).NotifyUpdate(); test_blacklist.SetBlacklistState(
good_crx, extensions::BLACKLISTED_MALWARE, true);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// The good_crx is blacklisted and the whitelist doesn't negate it. // The good_crx is blacklisted and the whitelist doesn't negate it.
...@@ -3479,15 +3489,15 @@ TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) { ...@@ -3479,15 +3489,15 @@ TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) {
// Tests that a blacklisted extension is eventually unloaded on startup, if it // Tests that a blacklisted extension is eventually unloaded on startup, if it
// wasn't already. // wasn't already.
TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) { TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) {
scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db( extensions::TestBlacklist test_blacklist;
new FakeSafeBrowsingDatabaseManager(true));
Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db);
// A profile with 3 extensions installed: good0, good1, and good2. // A profile with 3 extensions installed: good0, good1, and good2.
InitializeGoodInstalledExtensionService(); InitializeGoodInstalledExtensionService();
test_blacklist.Attach(service_->blacklist_);
// Blacklist good1 before the service initializes. // Blacklist good1 before the service initializes.
blacklist_db->SetUnsafe(good1); test_blacklist.SetBlacklistState(
good1, extensions::BLACKLISTED_MALWARE, false);
// Load extensions. // Load extensions.
service_->Init(); service_->Init();
...@@ -3509,15 +3519,15 @@ TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) { ...@@ -3509,15 +3519,15 @@ TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) {
// Tests extensions blacklisted in prefs on startup; one still blacklisted by // Tests extensions blacklisted in prefs on startup; one still blacklisted by
// safe browsing, the other not. The not-blacklisted one should recover. // safe browsing, the other not. The not-blacklisted one should recover.
TEST_F(ExtensionServiceTest, BlacklistedInPrefsFromStartup) { TEST_F(ExtensionServiceTest, BlacklistedInPrefsFromStartup) {
scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db( extensions::TestBlacklist test_blacklist;
new FakeSafeBrowsingDatabaseManager(true));
Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db);
InitializeGoodInstalledExtensionService(); InitializeGoodInstalledExtensionService();
test_blacklist.Attach(service_->blacklist_);
service_->extension_prefs()->SetExtensionBlacklisted(good0, true); service_->extension_prefs()->SetExtensionBlacklisted(good0, true);
service_->extension_prefs()->SetExtensionBlacklisted(good1, true); service_->extension_prefs()->SetExtensionBlacklisted(good1, true);
blacklist_db->SetUnsafe(good1); test_blacklist.SetBlacklistState(
good1, extensions::BLACKLISTED_MALWARE, false);
service_->Init(); service_->Init();
......
...@@ -31,6 +31,11 @@ FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::Enable() { ...@@ -31,6 +31,11 @@ FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::Enable() {
return *this; return *this;
} }
FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::Disable() {
enabled_ = false;
return *this;
}
FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager&
FakeSafeBrowsingDatabaseManager::ClearUnsafe() { FakeSafeBrowsingDatabaseManager::ClearUnsafe() {
unsafe_ids_.clear(); unsafe_ids_.clear();
...@@ -66,6 +71,18 @@ FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe( ...@@ -66,6 +71,18 @@ FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe(
return *this; return *this;
} }
FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::AddUnsafe(
const std::string& a) {
unsafe_ids_.insert(a);
return *this;
}
FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::RemoveUnsafe(
const std::string& a) {
unsafe_ids_.erase(a);
return *this;
}
void FakeSafeBrowsingDatabaseManager::NotifyUpdate() { void FakeSafeBrowsingDatabaseManager::NotifyUpdate() {
SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished(true); SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished(true);
} }
......
...@@ -28,6 +28,7 @@ class FakeSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager { ...@@ -28,6 +28,7 @@ class FakeSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager {
// Return |this| to chain together SetUnsafe(...).NotifyUpdate() conveniently. // Return |this| to chain together SetUnsafe(...).NotifyUpdate() conveniently.
FakeSafeBrowsingDatabaseManager& Enable(); FakeSafeBrowsingDatabaseManager& Enable();
FakeSafeBrowsingDatabaseManager& Disable();
FakeSafeBrowsingDatabaseManager& ClearUnsafe(); FakeSafeBrowsingDatabaseManager& ClearUnsafe();
FakeSafeBrowsingDatabaseManager& SetUnsafe(const std::string& a); FakeSafeBrowsingDatabaseManager& SetUnsafe(const std::string& a);
FakeSafeBrowsingDatabaseManager& SetUnsafe(const std::string& a, FakeSafeBrowsingDatabaseManager& SetUnsafe(const std::string& a,
...@@ -39,6 +40,8 @@ class FakeSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager { ...@@ -39,6 +40,8 @@ class FakeSafeBrowsingDatabaseManager : public SafeBrowsingDatabaseManager {
const std::string& b, const std::string& b,
const std::string& c, const std::string& c,
const std::string& d); const std::string& d);
FakeSafeBrowsingDatabaseManager& AddUnsafe(const std::string& a);
FakeSafeBrowsingDatabaseManager& RemoveUnsafe(const std::string& a);
// Send the update notification. // Send the update notification.
void NotifyUpdate(); void NotifyUpdate();
......
...@@ -9,14 +9,13 @@ ...@@ -9,14 +9,13 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/stl_util.h"
#include "chrome/browser/extensions/blacklist.h" #include "chrome/browser/extensions/blacklist.h"
#include "chrome/browser/extensions/blacklist_state_fetcher.h"
#include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
namespace extensions { namespace extensions {
TestBlacklist::TestBlacklist(Blacklist* blacklist)
: blacklist_(blacklist) {
}
namespace { namespace {
void Assign(BlacklistState *out, BlacklistState in) { void Assign(BlacklistState *out, BlacklistState in) {
...@@ -25,6 +24,93 @@ void Assign(BlacklistState *out, BlacklistState in) { ...@@ -25,6 +24,93 @@ void Assign(BlacklistState *out, BlacklistState in) {
} // namespace } // namespace
BlacklistStateFetcherMock::BlacklistStateFetcherMock() : request_count_(0) {}
BlacklistStateFetcherMock::~BlacklistStateFetcherMock() {}
void BlacklistStateFetcherMock::Request(const std::string& id,
const RequestCallback& callback) {
++request_count_;
BlacklistState result = NOT_BLACKLISTED;
if (ContainsKey(states_, id))
result = states_[id];
base::MessageLoopProxy::current()->PostTask(FROM_HERE,
base::Bind(callback, result));
}
void BlacklistStateFetcherMock::SetState(const std::string& id,
BlacklistState state) {
states_[id] = state;
}
void BlacklistStateFetcherMock::Clear() {
states_.clear();
}
TestBlacklist::TestBlacklist()
: blacklist_(NULL),
blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)),
scoped_blacklist_db_(blacklist_db_) {
}
TestBlacklist::TestBlacklist(Blacklist* blacklist)
: blacklist_(NULL),
blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)),
scoped_blacklist_db_(blacklist_db_) {
Attach(blacklist);
}
TestBlacklist::~TestBlacklist() {
Detach();
}
void TestBlacklist::Attach(Blacklist* blacklist) {
if (blacklist_)
Detach();
blacklist_ = blacklist;
blacklist_->SetBlacklistStateFetcherForTest(&state_fetcher_mock_);
}
void TestBlacklist::Detach() {
blacklist_->ResetBlacklistStateFetcherForTest();
}
void TestBlacklist::SetBlacklistState(const std::string& extension_id,
BlacklistState state,
bool notify) {
state_fetcher_mock_.SetState(extension_id, state);
switch (state) {
case NOT_BLACKLISTED:
blacklist_db_->RemoveUnsafe(extension_id);
break;
case BLACKLISTED_MALWARE:
case BLACKLISTED_SECURITY_VULNERABILITY:
case BLACKLISTED_CWS_POLICY_VIOLATION:
case BLACKLISTED_POTENTIALLY_UNWANTED:
blacklist_db_->AddUnsafe(extension_id);
break;
default:
break;
}
if (notify)
blacklist_db_->NotifyUpdate();
}
void TestBlacklist::Clear(bool notify) {
state_fetcher_mock_.Clear();
blacklist_db_->ClearUnsafe();
if (notify)
blacklist_db_->NotifyUpdate();
}
BlacklistState TestBlacklist::GetBlacklistState( BlacklistState TestBlacklist::GetBlacklistState(
const std::string& extension_id) { const std::string& extension_id) {
BlacklistState blacklist_state; BlacklistState blacklist_state;
...@@ -34,4 +120,16 @@ BlacklistState TestBlacklist::GetBlacklistState( ...@@ -34,4 +120,16 @@ BlacklistState TestBlacklist::GetBlacklistState(
return blacklist_state; return blacklist_state;
} }
void TestBlacklist::DisableSafeBrowsing() {
blacklist_db_->Disable();
}
void TestBlacklist::EnableSafeBrowsing() {
blacklist_db_->Enable();
}
void TestBlacklist::NotifyUpdate() {
blacklist_db_->NotifyUpdate();
}
} // namespace extensions } // namespace extensions
...@@ -5,26 +5,91 @@ ...@@ -5,26 +5,91 @@
#ifndef CHROME_BROWSER_EXTENSIONS_TEST_BLACKLIST_H_ #ifndef CHROME_BROWSER_EXTENSIONS_TEST_BLACKLIST_H_
#define CHROME_BROWSER_EXTENSIONS_TEST_BLACKLIST_H_ #define CHROME_BROWSER_EXTENSIONS_TEST_BLACKLIST_H_
#include <map>
#include <string> #include <string>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "chrome/browser/extensions/blacklist.h" #include "chrome/browser/extensions/blacklist.h"
#include "chrome/browser/extensions/blacklist_state_fetcher.h"
namespace extensions { namespace extensions {
class FakeSafeBrowsingDatabaseManager;
// Replace BlacklistStateFetcher for testing of the Blacklist class.
class BlacklistStateFetcherMock : public BlacklistStateFetcher {
public:
BlacklistStateFetcherMock();
virtual ~BlacklistStateFetcherMock();
virtual void Request(const std::string& id,
const RequestCallback& callback) OVERRIDE;
void SetState(const std::string& id, BlacklistState state);
void Clear();
int request_count() const { return request_count_; }
private:
std::map<std::string, BlacklistState> states_;
int request_count_;
};
// A wrapper for an extensions::Blacklist that provides functionality for // A wrapper for an extensions::Blacklist that provides functionality for
// testing. // testing. It sets up mocks for SafeBrowsing database and BlacklistFetcher,
// that are used by blacklist to retrieve respectively the set of blacklisted
// extensions and their blacklist states.
class TestBlacklist { class TestBlacklist {
public: public:
// Use this if the SafeBrowsing and/or StateFetcher mocks should be created
// before initializing the Blacklist.
explicit TestBlacklist();
explicit TestBlacklist(Blacklist* blacklist); explicit TestBlacklist(Blacklist* blacklist);
~TestBlacklist();
void Attach(Blacklist* blacklist);
// Only call this if Blacklist is destroyed before TestBlacklist, otherwise
// it will be performed from the destructor.
void Detach();
Blacklist* blacklist() { return blacklist_; } Blacklist* blacklist() { return blacklist_; }
// Set the extension state in SafeBrowsingDatabaseManager and
// BlacklistFetcher.
void SetBlacklistState(const std::string& extension_id,
BlacklistState state,
bool notify);
BlacklistState GetBlacklistState(const std::string& extension_id); BlacklistState GetBlacklistState(const std::string& extension_id);
void Clear(bool notify);
void DisableSafeBrowsing();
void EnableSafeBrowsing();
void NotifyUpdate();
const BlacklistStateFetcherMock* fetcher() { return &state_fetcher_mock_; }
private: private:
Blacklist* blacklist_; Blacklist* blacklist_;
// The BlacklistStateFetcher object is normally managed by Blacklist. Because
// of this, we need to prevent this object from being deleted with Blacklist.
// For this, Detach() should be called before blacklist_ is deleted.
BlacklistStateFetcherMock state_fetcher_mock_;
scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db_;
Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db_;
DISALLOW_COPY_AND_ASSIGN(TestBlacklist); DISALLOW_COPY_AND_ASSIGN(TestBlacklist);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment