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

Unified Diff: extensions/browser/policy_check_unittest.cc

Issue 2693373003: PreloadCheck class for extension pre-install checks (Closed)
Patch Set: review ready 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..1e349d76e5113f7836c7e84d8531dc9a0dfff52f
--- /dev/null
+++ b/extensions/browser/policy_check_unittest.cc
@@ -0,0 +1,128 @@
+// 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 char kDummyPolicyError[] = "Cannot install extension";
+
+class ManagementPolicyMock : public extensions::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::UTF8ToUTF16(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 testing::Test {
+ public:
+ PolicyCheckTest() : extensions_browser_client_(&context_) {}
+ ~PolicyCheckTest() override {}
+
+ void SetUp() override {
+ ExtensionsBrowserClient::Set(&extensions_browser_client_);
Devlin 2017/03/08 03:01:46 This not being torn down causes badness. Addition
michaelpg 2017/03/09 01:53:30 I'm not quite sure what you mean/how the unit test
michaelpg 2017/03/13 19:18:30 Here's an example from //extensions/browser that i
michaelpg 2017/03/13 20:11:49 Looks like ExtensionsTest itself calls ExtensionsB
+ 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(), extensions::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);
+ extensions::ExtensionSystem::Get(&context_)
Devlin 2017/03/08 03:01:46 no need for extensions:: namespace
michaelpg 2017/03/09 01:53:30 Done.
+ ->management_policy()
+ ->RegisterProvider(&policy);
+
+ PreloadCheckObserver observer;
+ PolicyCheck policy_check(&context_, extension_.get());
+ policy_check.Start(base::Bind(&PreloadCheckObserver::OnCheckComplete,
+ base::Unretained(&observer)));
+ EXPECT_EQ(1, observer.call_count());
+ EXPECT_EQ(0u, observer.errors().size());
+ base::string16 message;
+ EXPECT_FALSE(policy_check.GetErrorMessage(&message));
+}
+
+// Test an invalid extension.
+TEST_F(PolicyCheckTest, PolicyFailure) {
+ ManagementPolicyMock policy(extension_.get(), false);
+ extensions::ExtensionSystem::Get(&context_)
+ ->management_policy()
+ ->RegisterProvider(&policy);
+
+ PreloadCheckObserver observer;
+ PolicyCheck policy_check(&context_, extension_.get());
+ policy_check.Start(base::Bind(&PreloadCheckObserver::OnCheckComplete,
+ base::Unretained(&observer)));
+ EXPECT_EQ(1, observer.call_count());
+ EXPECT_EQ(1u, observer.errors().size());
+ EXPECT_EQ(1u, observer.errors().count(PreloadCheck::DISALLOWED_BY_POLICY));
+ base::string16 message;
+ EXPECT_TRUE(policy_check.GetErrorMessage(&message));
+ EXPECT_EQ(base::UTF8ToUTF16(kDummyPolicyError), message);
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698