OLD | NEW |
---|---|
(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 <vector> | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ref_counted.h" | |
9 #include "base/strings/string16.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "content/public/test/test_browser_context.h" | |
12 #include "extensions/browser/extension_system.h" | |
13 #include "extensions/browser/management_policy.h" | |
14 #include "extensions/browser/mock_extension_system.h" | |
15 #include "extensions/browser/policy_check.h" | |
16 #include "extensions/browser/preload_check.h" | |
17 #include "extensions/browser/preload_check_test_util.h" | |
18 #include "extensions/browser/test_extensions_browser_client.h" | |
19 #include "extensions/common/constants.h" | |
20 #include "extensions/common/extension.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 namespace extensions { | |
24 | |
25 namespace { | |
26 | |
27 const base::string16 kDummyPolicyError = | |
28 base::ASCIIToUTF16("Cannot install extension"); | |
29 | |
30 class ManagementPolicyMock : public ManagementPolicy::Provider { | |
31 public: | |
32 ManagementPolicyMock(const Extension* extension, bool may_load) | |
33 : extension_(extension), may_load_(may_load) {} | |
34 | |
35 std::string GetDebugPolicyProviderName() const override { | |
36 return "ManagementPolicyMock"; | |
37 } | |
38 | |
39 bool UserMayLoad(const Extension* extension, | |
40 base::string16* error) const override { | |
41 EXPECT_EQ(extension_, extension); | |
42 if (!may_load_) | |
43 *error = kDummyPolicyError; | |
44 return may_load_; | |
45 } | |
46 | |
47 private: | |
48 const Extension* extension_; | |
49 bool may_load_; | |
50 }; | |
51 | |
52 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.
| |
53 public: | |
54 explicit TestExtensionSystem(content::BrowserContext* context) | |
55 : MockExtensionSystem(context) {} | |
56 ~TestExtensionSystem() override {} | |
57 | |
58 ManagementPolicy* management_policy() override { return &management_policy_; } | |
59 | |
60 private: | |
61 ManagementPolicy management_policy_; | |
62 }; | |
63 | |
64 } // namespace | |
65 | |
66 class PolicyCheckTest : public testing::Test { | |
67 public: | |
68 PolicyCheckTest() : extensions_browser_client_(&context_) {} | |
69 ~PolicyCheckTest() override {} | |
70 | |
71 void SetUp() override { | |
72 ExtensionsBrowserClient::Set(&extensions_browser_client_); | |
73 extensions_browser_client_.set_extension_system_factory(&factory_); | |
74 | |
75 base::DictionaryValue manifest_dict; | |
76 manifest_dict.SetString("name", "dummy name"); | |
77 manifest_dict.SetString("version", "1"); | |
78 std::string error; | |
79 | |
80 extension_ = Extension::Create(base::FilePath(), Manifest::UNPACKED, | |
81 manifest_dict, Extension::NO_FLAGS, &error); | |
82 EXPECT_TRUE(extension_.get()) << error; | |
83 } | |
84 | |
85 protected: | |
86 content::TestBrowserContext context_; | |
87 TestExtensionsBrowserClient extensions_browser_client_; | |
88 MockExtensionSystemFactory<TestExtensionSystem> factory_; | |
89 scoped_refptr<Extension> extension_; | |
90 }; | |
91 | |
92 // Test a valid extension. | |
93 TEST_F(PolicyCheckTest, PolicySuccess) { | |
94 ManagementPolicyMock policy(extension_.get(), true); | |
95 ExtensionSystem::Get(&context_)->management_policy()->RegisterProvider( | |
96 &policy); | |
97 | |
98 PreloadCheckObserver observer; | |
99 PolicyCheck policy_check(&context_, extension_); | |
100 policy_check.Start(base::Bind(&PreloadCheckObserver::OnCheckComplete, | |
101 base::Unretained(&observer))); | |
102 EXPECT_TRUE(observer.called()); | |
103 EXPECT_EQ(0u, observer.errors().size()); | |
104 EXPECT_TRUE(policy_check.GetErrorMessage().empty()); | |
105 } | |
106 | |
107 // Test an invalid extension. | |
108 TEST_F(PolicyCheckTest, PolicyFailure) { | |
109 ManagementPolicyMock policy(extension_.get(), false); | |
110 ExtensionSystem::Get(&context_)->management_policy()->RegisterProvider( | |
111 &policy); | |
112 | |
113 PreloadCheckObserver observer; | |
114 PolicyCheck policy_check(&context_, extension_); | |
115 policy_check.Start(base::Bind(&PreloadCheckObserver::OnCheckComplete, | |
116 base::Unretained(&observer))); | |
117 EXPECT_TRUE(observer.called()); | |
118 EXPECT_EQ(1u, observer.errors().size()); | |
119 EXPECT_EQ(1u, observer.errors().count(PreloadCheck::DISALLOWED_BY_POLICY)); | |
120 EXPECT_EQ(kDummyPolicyError, policy_check.GetErrorMessage()); | |
121 } | |
122 | |
123 } // namespace extensions | |
OLD | NEW |