1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_error_controller.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_error_ui.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_service_test_base.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "extensions/browser/blocklist_extension_prefs.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
namespace extensions {
namespace {
// Create a mock for the UI component of the error alert that is shown for
// blocklisted extensions. This allows us to test which extensions the alert
// is showing, and also eliminates the UI component (since this is a unit
// test).
class MockExtensionErrorUI : public ExtensionErrorUI {
public:
explicit MockExtensionErrorUI(ExtensionErrorUI::Delegate* delegate);
~MockExtensionErrorUI() override;
// Wrappers around the similar methods in ExtensionErrorUI.
void CloseUI();
void Accept();
void Details();
ExtensionErrorUI::Delegate* delegate() { return delegate_; }
private:
// ExtensionErrorUI implementation.
bool ShowErrorInBubbleView() override;
void ShowExtensions() override;
void Close() override;
// Keep a copy of the delegate around for ourselves.
raw_ptr<ExtensionErrorUI::Delegate> delegate_;
};
// We use this as a slight hack to get the created Error UI, if any. We should
// only ever have one (since this is a single-profile test), and this avoids
// the need for any kind of accessor to the ErrorController from
// ExtensionService.
MockExtensionErrorUI* g_error_ui = nullptr;
MockExtensionErrorUI::MockExtensionErrorUI(ExtensionErrorUI::Delegate* delegate)
: delegate_(delegate) {
// We should never make more than one of these in a test.
DCHECK(!g_error_ui);
g_error_ui = this;
}
MockExtensionErrorUI::~MockExtensionErrorUI() {
g_error_ui = nullptr;
}
void MockExtensionErrorUI::CloseUI() {
delegate_->OnAlertClosed();
}
void MockExtensionErrorUI::Accept() {
delegate_->OnAlertAccept();
}
void MockExtensionErrorUI::Details() {
delegate_->OnAlertDetails();
}
bool MockExtensionErrorUI::ShowErrorInBubbleView() {
return true;
}
void MockExtensionErrorUI::ShowExtensions() {}
void MockExtensionErrorUI::Close() {
CloseUI();
}
ExtensionErrorUI* CreateMockUI(ExtensionErrorUI::Delegate* delegate) {
return new MockExtensionErrorUI(delegate);
}
// Builds and returns a simple extension.
scoped_refptr<const Extension> BuildExtension() {
return ExtensionBuilder()
.SetManifest(base::Value::Dict()
.Set("name", "My Wonderful Extension")
.Set("version", "0.1.1.0")
.Set("manifest_version", 2))
.Build();
}
} // namespace
class ExtensionErrorControllerUnitTest : public ExtensionServiceTestBase {
protected:
void SetUp() override;
// Add an extension to chrome, and mark it as blocklisted in the prefs.
testing::AssertionResult AddBlocklistedExtension(const Extension* extension);
// Set enterprise policy to block `extension`. Use nullptr to not block any
// extension.
void SetBlockExtensionPolicy(const Extension* extension);
// Return the ExtensionPrefs associated with the test.
ExtensionPrefs* GetPrefs();
};
void ExtensionErrorControllerUnitTest::SetUp() {
ExtensionServiceTestBase::SetUp();
// Make sure we use the mock UI instead of the real UI.
ExtensionErrorController::SetUICreateMethodForTesting(CreateMockUI);
// We don't want a first-run ExtensionService, since we ignore warnings
// for new profiles.
ExtensionServiceInitParams params;
params.is_first_run = false;
InitializeExtensionService(std::move(params));
}
testing::AssertionResult
ExtensionErrorControllerUnitTest::AddBlocklistedExtension(
const Extension* extension) {
blocklist_prefs::SetSafeBrowsingExtensionBlocklistState(
extension->id(), BitMapBlocklistState::BLOCKLISTED_MALWARE, GetPrefs());
registrar()->AddExtension(extension);
// Make sure the extension is added to the blocklisted set.
if (!ExtensionRegistry::Get(profile())->blocklisted_extensions().Contains(
extension->id())) {
return testing::AssertionFailure()
<< "Failed to add blocklisted extension.";
}
return testing::AssertionSuccess();
}
void ExtensionErrorControllerUnitTest::SetBlockExtensionPolicy(
const Extension* extension) {
base::Value::List block_list;
if (extension) {
block_list.Append(extension->id());
}
testing_pref_service()->SetManagedPref(pref_names::kInstallDenyList,
std::move(block_list));
}
ExtensionPrefs* ExtensionErrorControllerUnitTest::GetPrefs() {
return ExtensionPrefs::Get(profile());
}
// Test that closing the extension alert for blocklisted extensions counts
// as acknowledging them in the prefs.
TEST_F(ExtensionErrorControllerUnitTest, ClosingAcknowledgesBlocklisted) {
// Add a blocklisted extension.
scoped_refptr<const Extension> extension = BuildExtension();
ASSERT_TRUE(AddBlocklistedExtension(extension.get()));
service_->Init();
// Make sure that we created an error "ui" to warn about the blocklisted
// extension.
ASSERT_TRUE(g_error_ui);
ExtensionErrorUI::Delegate* delegate = g_error_ui->delegate();
ASSERT_TRUE(delegate);
// Make sure that the blocklisted extension is reported (and that no other
// extensions are).
const ExtensionSet& delegate_blocklisted_extensions =
delegate->GetBlocklistedExtensions();
EXPECT_EQ(1u, delegate_blocklisted_extensions.size());
EXPECT_TRUE(delegate_blocklisted_extensions.Contains(extension->id()));
// Close, and verify that the extension ids now acknowledged.
g_error_ui->CloseUI();
EXPECT_TRUE(GetPrefs()->IsBlocklistedExtensionAcknowledged(extension->id()));
// Verify we cleaned up after ourselves.
EXPECT_FALSE(g_error_ui);
}
// Test that clicking "accept" on the extension alert counts as acknowledging
// blocklisted extensions.
TEST_F(ExtensionErrorControllerUnitTest, AcceptingAcknowledgesBlocklisted) {
// Add a blocklisted extension.
scoped_refptr<const Extension> extension = BuildExtension();
ASSERT_TRUE(AddBlocklistedExtension(extension.get()));
service_->Init();
// Make sure that we created an error "ui" to warn about the blocklisted
// extension.
ASSERT_TRUE(g_error_ui);
// Accept, and verify that the extension ids now acknowledged.
g_error_ui->Accept();
EXPECT_TRUE(GetPrefs()->IsBlocklistedExtensionAcknowledged(extension->id()));
// Verify we cleaned up after ourselves.
EXPECT_FALSE(g_error_ui);
}
// Test that we don't warn for extensions which are blocklisted, but have
// already been acknowledged.
TEST_F(ExtensionErrorControllerUnitTest, DontWarnForAcknowledgedBlocklisted) {
scoped_refptr<const Extension> extension = BuildExtension();
ASSERT_TRUE(AddBlocklistedExtension(extension.get()));
GetPrefs()->AcknowledgeBlocklistedExtension(extension->id());
service_->Init();
// We should never have made an alert, because the extension should already
// be acknowledged.
ASSERT_FALSE(g_error_ui);
}
// Test there is no error ui if no extension is blocked by policy.
TEST_F(ExtensionErrorControllerUnitTest,
ExtensionIsNotBlockedByEnterprisePolicy) {
scoped_refptr<const Extension> extension = BuildExtension();
service_->Init();
registrar()->AddExtension(extension);
EXPECT_FALSE(g_error_ui);
}
// Test error ui is presented and acknowledged when an extension is blocked by
// policy.
TEST_F(ExtensionErrorControllerUnitTest, ExtensionIsBlockedByEnterprisePolicy) {
scoped_refptr<const Extension> extension = BuildExtension();
service_->Init();
registrar()->AddExtension(extension);
SetBlockExtensionPolicy(extension.get());
ASSERT_TRUE(g_error_ui);
g_error_ui->Accept();
EXPECT_TRUE(GetPrefs()->IsBlocklistedExtensionAcknowledged(extension->id()));
EXPECT_FALSE(g_error_ui);
}
// Test the case that the error UI is accepted when we no longer need to show
// error for a blocked extension. It includes the case that the policy is
// updated or the extension is moved to the disabled list.
TEST_F(ExtensionErrorControllerUnitTest, ExtensionIsUnblockedBeforeUIAccepted) {
scoped_refptr<const Extension> extension = BuildExtension();
service_->Init();
registrar()->AddExtension(extension);
SetBlockExtensionPolicy(extension.get());
ASSERT_TRUE(g_error_ui);
// Reset extension policy
SetBlockExtensionPolicy(nullptr);
g_error_ui->Accept();
EXPECT_TRUE(GetPrefs()->IsBlocklistedExtensionAcknowledged(extension->id()));
EXPECT_FALSE(g_error_ui);
}
} // namespace extensions
|