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

Unified Diff: extensions/browser/policy_check_unittest.cc

Issue 2693373003: PreloadCheck class for extension pre-install checks (Closed)
Patch Set: don't break enable_extensions=false 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
« no previous file with comments | « extensions/browser/policy_check.cc ('k') | extensions/browser/preload_check.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..8b8af3057d2d42c91d3265f7c6d245d8c4781fa4
--- /dev/null
+++ b/extensions/browser/policy_check_unittest.cc
@@ -0,0 +1,122 @@
+// 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 "extensions/browser/policy_check.h"
+
+#include <vector>
+
+#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/extensions_test.h"
+#include "extensions/browser/management_policy.h"
+#include "extensions/browser/mock_extension_system.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/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace extensions {
+
+namespace {
+
+const char kDummyPolicyError[] = "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 = base::ASCIIToUTF16(kDummyPolicyError);
+ return may_load_;
+ }
+
+ private:
+ const Extension* extension_;
+ bool may_load_;
+};
+
+class TestExtensionSystem : public MockExtensionSystem {
+ 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 ExtensionsTest {
+ public:
+ PolicyCheckTest() {
+ // Replace the MockExtensionSystemFactory set by ExtensionsTest.
+ extensions_browser_client()->set_extension_system_factory(&factory_);
+ }
+
+ ~PolicyCheckTest() override {}
+
+ void SetUp() override {
+ ExtensionsTest::SetUp();
+
+ 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:
+ scoped_refptr<Extension> extension_;
+ PreloadCheckRunner runner_;
+
+ private:
+ MockExtensionSystemFactory<TestExtensionSystem> factory_;
+};
+
+// Tests an allowed extension.
+TEST_F(PolicyCheckTest, PolicySuccess) {
+ PolicyCheck policy_check(browser_context(), extension_);
+ runner_.Run(&policy_check);
+ EXPECT_TRUE(runner_.called());
+ EXPECT_EQ(0u, runner_.errors().size());
+ EXPECT_TRUE(policy_check.GetErrorMessage().empty());
+}
+
+// Tests a disallowed extension.
+TEST_F(PolicyCheckTest, PolicyFailure) {
+ ManagementPolicyMock policy(extension_.get(), false);
+ ExtensionSystem::Get(browser_context())
+ ->management_policy()
+ ->RegisterProvider(&policy);
+
+ PolicyCheck policy_check(browser_context(), extension_);
+ runner_.Run(&policy_check);
+ EXPECT_TRUE(runner_.called());
+ EXPECT_THAT(runner_.errors(), testing::UnorderedElementsAre(
+ PreloadCheck::DISALLOWED_BY_POLICY));
+ EXPECT_EQ(base::ASCIIToUTF16(kDummyPolicyError),
+ policy_check.GetErrorMessage());
+}
+
+} // namespace extensions
« no previous file with comments | « extensions/browser/policy_check.cc ('k') | extensions/browser/preload_check.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698