Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(959)

Unified Diff: extensions/browser/policy_check_unittest.cc

Issue 2693373003: PreloadCheck class for extension pre-install checks (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: extensions/browser/policy_check_unittest.cc
diff --git a/extensions/browser/policy_check_unittest.cc b/extensions/browser/policy_check_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0df68c5a3a959a0da254032e03d33a0b6e84c730
--- /dev/null
+++ b/extensions/browser/policy_check_unittest.cc
@@ -0,0 +1,123 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <vector>
+
+#include "base/bind.h"
+#include "base/memory/ref_counted.h"
+#include "base/strings/string16.h"
+#include "base/strings/utf_string_conversions.h"
+#include "content/public/test/test_browser_context.h"
+#include "extensions/browser/extension_system.h"
+#include "extensions/browser/management_policy.h"
+#include "extensions/browser/mock_extension_system.h"
+#include "extensions/browser/policy_check.h"
+#include "extensions/browser/preload_check.h"
+#include "extensions/browser/preload_check_test_util.h"
+#include "extensions/browser/test_extensions_browser_client.h"
+#include "extensions/common/constants.h"
+#include "extensions/common/extension.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace extensions {
+
+namespace {
+
+const base::string16 kDummyPolicyError =
+ base::ASCIIToUTF16("Cannot install extension");
+
+class ManagementPolicyMock : public ManagementPolicy::Provider {
+ public:
+ ManagementPolicyMock(const Extension* extension, bool may_load)
+ : extension_(extension), may_load_(may_load) {}
+
+ std::string GetDebugPolicyProviderName() const override {
+ return "ManagementPolicyMock";
+ }
+
+ bool UserMayLoad(const Extension* extension,
+ base::string16* error) const override {
+ EXPECT_EQ(extension_, extension);
+ if (!may_load_)
+ *error = kDummyPolicyError;
+ return may_load_;
+ }
+
+ private:
+ const Extension* extension_;
+ bool may_load_;
+};
+
+class TestExtensionSystem : public MockExtensionSystem {
Devlin 2017/03/14 01:44:34 I'd slightly prefer that we expose a SetManagement
michaelpg 2017/03/14 21:58:30 Not sure what you're referring to here. I see a Cr
Devlin 2017/03/16 01:42:44 Nevermind. I forgot we had MockExtensionSystem *a
michaelpg 2017/03/17 02:34:26 Acknowledged.
+ public:
+ explicit TestExtensionSystem(content::BrowserContext* context)
+ : MockExtensionSystem(context) {}
+ ~TestExtensionSystem() override {}
+
+ ManagementPolicy* management_policy() override { return &management_policy_; }
+
+ private:
+ ManagementPolicy management_policy_;
+};
+
+} // namespace
+
+class PolicyCheckTest : public testing::Test {
+ public:
+ PolicyCheckTest() : extensions_browser_client_(&context_) {}
+ ~PolicyCheckTest() override {}
+
+ void SetUp() override {
+ ExtensionsBrowserClient::Set(&extensions_browser_client_);
+ extensions_browser_client_.set_extension_system_factory(&factory_);
+
+ base::DictionaryValue manifest_dict;
+ manifest_dict.SetString("name", "dummy name");
+ manifest_dict.SetString("version", "1");
+ std::string error;
+
+ extension_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
+ manifest_dict, Extension::NO_FLAGS, &error);
+ EXPECT_TRUE(extension_.get()) << error;
+ }
+
+ protected:
+ content::TestBrowserContext context_;
+ TestExtensionsBrowserClient extensions_browser_client_;
+ MockExtensionSystemFactory<TestExtensionSystem> factory_;
+ scoped_refptr<Extension> extension_;
+};
+
+// Test a valid extension.
+TEST_F(PolicyCheckTest, PolicySuccess) {
+ ManagementPolicyMock policy(extension_.get(), true);
+ ExtensionSystem::Get(&context_)->management_policy()->RegisterProvider(
+ &policy);
+
+ PreloadCheckObserver observer;
+ PolicyCheck policy_check(&context_, extension_);
+ policy_check.Start(base::Bind(&PreloadCheckObserver::OnCheckComplete,
+ base::Unretained(&observer)));
+ EXPECT_TRUE(observer.called());
+ EXPECT_EQ(0u, observer.errors().size());
+ EXPECT_TRUE(policy_check.GetErrorMessage().empty());
+}
+
+// Test an invalid extension.
+TEST_F(PolicyCheckTest, PolicyFailure) {
+ ManagementPolicyMock policy(extension_.get(), false);
+ ExtensionSystem::Get(&context_)->management_policy()->RegisterProvider(
+ &policy);
+
+ PreloadCheckObserver observer;
+ PolicyCheck policy_check(&context_, extension_);
+ policy_check.Start(base::Bind(&PreloadCheckObserver::OnCheckComplete,
+ base::Unretained(&observer)));
+ EXPECT_TRUE(observer.called());
+ EXPECT_EQ(1u, observer.errors().size());
+ EXPECT_EQ(1u, observer.errors().count(PreloadCheck::DISALLOWED_BY_POLICY));
+ EXPECT_EQ(kDummyPolicyError, policy_check.GetErrorMessage());
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698