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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « extensions/browser/policy_check.cc ('k') | extensions/browser/preload_check.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "extensions/browser/policy_check.h"
6
7 #include <vector>
8
9 #include "base/memory/ref_counted.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/public/test/test_browser_context.h"
13 #include "extensions/browser/extension_system.h"
14 #include "extensions/browser/extensions_test.h"
15 #include "extensions/browser/management_policy.h"
16 #include "extensions/browser/mock_extension_system.h"
17 #include "extensions/browser/preload_check.h"
18 #include "extensions/browser/preload_check_test_util.h"
19 #include "extensions/browser/test_extensions_browser_client.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/extension.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace extensions {
26
27 namespace {
28
29 const char kDummyPolicyError[] = "Cannot install extension";
30
31 class ManagementPolicyMock : public ManagementPolicy::Provider {
32 public:
33 ManagementPolicyMock(const Extension* extension, bool may_load)
34 : extension_(extension), may_load_(may_load) {}
35
36 std::string GetDebugPolicyProviderName() const override {
37 return "ManagementPolicyMock";
38 }
39
40 bool UserMayLoad(const Extension* extension,
41 base::string16* error) const override {
42 EXPECT_EQ(extension_, extension);
43 if (!may_load_)
44 *error = base::ASCIIToUTF16(kDummyPolicyError);
45 return may_load_;
46 }
47
48 private:
49 const Extension* extension_;
50 bool may_load_;
51 };
52
53 class TestExtensionSystem : public MockExtensionSystem {
54 public:
55 explicit TestExtensionSystem(content::BrowserContext* context)
56 : MockExtensionSystem(context) {}
57 ~TestExtensionSystem() override {}
58
59 ManagementPolicy* management_policy() override { return &management_policy_; }
60
61 private:
62 ManagementPolicy management_policy_;
63 };
64
65 } // namespace
66
67 class PolicyCheckTest : public ExtensionsTest {
68 public:
69 PolicyCheckTest() {
70 // Replace the MockExtensionSystemFactory set by ExtensionsTest.
71 extensions_browser_client()->set_extension_system_factory(&factory_);
72 }
73
74 ~PolicyCheckTest() override {}
75
76 void SetUp() override {
77 ExtensionsTest::SetUp();
78
79 base::DictionaryValue manifest_dict;
80 manifest_dict.SetString("name", "dummy name");
81 manifest_dict.SetString("version", "1");
82 std::string error;
83
84 extension_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
85 manifest_dict, Extension::NO_FLAGS, &error);
86 EXPECT_TRUE(extension_.get()) << error;
87 }
88
89 protected:
90 scoped_refptr<Extension> extension_;
91 PreloadCheckRunner runner_;
92
93 private:
94 MockExtensionSystemFactory<TestExtensionSystem> factory_;
95 };
96
97 // Tests an allowed extension.
98 TEST_F(PolicyCheckTest, PolicySuccess) {
99 PolicyCheck policy_check(browser_context(), extension_);
100 runner_.Run(&policy_check);
101 EXPECT_TRUE(runner_.called());
102 EXPECT_EQ(0u, runner_.errors().size());
103 EXPECT_TRUE(policy_check.GetErrorMessage().empty());
104 }
105
106 // Tests a disallowed extension.
107 TEST_F(PolicyCheckTest, PolicyFailure) {
108 ManagementPolicyMock policy(extension_.get(), false);
109 ExtensionSystem::Get(browser_context())
110 ->management_policy()
111 ->RegisterProvider(&policy);
112
113 PolicyCheck policy_check(browser_context(), extension_);
114 runner_.Run(&policy_check);
115 EXPECT_TRUE(runner_.called());
116 EXPECT_THAT(runner_.errors(), testing::UnorderedElementsAre(
117 PreloadCheck::DISALLOWED_BY_POLICY));
118 EXPECT_EQ(base::ASCIIToUTF16(kDummyPolicyError),
119 policy_check.GetErrorMessage());
120 }
121
122 } // namespace extensions
OLDNEW
« 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