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 |