Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / browser / management_policy_unittest.cc
blob7bf60a9bf44dd92924014dbd9b224f120c2c9a4e
1 // Copyright 2013 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.
5 #include "base/strings/utf_string_conversions.h"
6 #include "extensions/browser/management_policy.h"
7 #include "extensions/browser/test_management_policy.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 typedef extensions::TestManagementPolicyProvider TestProvider;
11 using extensions::Extension;
13 class ManagementPolicyTest : public testing::Test {
14 public:
15 void SetUp() override {
16 allow_all_.SetProhibitedActions(TestProvider::ALLOW_ALL);
17 no_modify_status_.SetProhibitedActions(
18 TestProvider::PROHIBIT_MODIFY_STATUS);
19 no_load_.SetProhibitedActions(TestProvider::PROHIBIT_LOAD);
20 must_remain_enabled_.SetProhibitedActions(
21 TestProvider::MUST_REMAIN_ENABLED);
22 must_remain_disabled_.SetProhibitedActions(
23 TestProvider::MUST_REMAIN_DISABLED);
24 must_remain_disabled_.SetDisableReason(Extension::DISABLE_SIDELOAD_WIPEOUT);
25 must_remain_installed_.SetProhibitedActions(
26 TestProvider::MUST_REMAIN_INSTALLED);
27 restrict_all_.SetProhibitedActions(TestProvider::PROHIBIT_MODIFY_STATUS |
28 TestProvider::PROHIBIT_LOAD |
29 TestProvider::MUST_REMAIN_ENABLED);
32 protected:
33 extensions::ManagementPolicy policy_;
35 TestProvider allow_all_;
36 TestProvider no_modify_status_;
37 TestProvider no_load_;
38 TestProvider must_remain_enabled_;
39 TestProvider must_remain_disabled_;
40 TestProvider must_remain_installed_;
41 TestProvider restrict_all_;
44 TEST_F(ManagementPolicyTest, RegisterAndUnregister) {
45 EXPECT_EQ(0, policy_.GetNumProviders());
46 policy_.RegisterProvider(&allow_all_);
47 EXPECT_EQ(1, policy_.GetNumProviders());
48 policy_.RegisterProvider(&allow_all_);
49 EXPECT_EQ(1, policy_.GetNumProviders());
51 policy_.RegisterProvider(&no_modify_status_);
52 EXPECT_EQ(2, policy_.GetNumProviders());
53 policy_.UnregisterProvider(&allow_all_);
54 EXPECT_EQ(1, policy_.GetNumProviders());
55 policy_.UnregisterProvider(&allow_all_);
56 EXPECT_EQ(1, policy_.GetNumProviders());
57 policy_.UnregisterProvider(&no_modify_status_);
58 EXPECT_EQ(0, policy_.GetNumProviders());
60 policy_.RegisterProvider(&allow_all_);
61 policy_.RegisterProvider(&no_modify_status_);
62 EXPECT_EQ(2, policy_.GetNumProviders());
63 policy_.UnregisterAllProviders();
64 EXPECT_EQ(0, policy_.GetNumProviders());
67 TEST_F(ManagementPolicyTest, UserMayLoad) {
68 // No providers registered.
69 base::string16 error;
70 // The extension and location are irrelevant to the
71 // TestManagementPolicyProviders.
72 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
73 EXPECT_TRUE(error.empty());
75 // One provider, no relevant restriction.
76 policy_.RegisterProvider(&no_modify_status_);
77 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
78 EXPECT_TRUE(error.empty());
80 // Two providers, no relevant restrictions.
81 policy_.RegisterProvider(&must_remain_enabled_);
82 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
83 EXPECT_TRUE(error.empty());
85 // Three providers, one with a relevant restriction.
86 policy_.RegisterProvider(&no_load_);
87 EXPECT_FALSE(policy_.UserMayLoad(NULL, &error));
88 EXPECT_FALSE(error.empty());
90 // Remove the restriction.
91 policy_.UnregisterProvider(&no_load_);
92 error.clear();
93 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
94 EXPECT_TRUE(error.empty());
96 TEST_F(ManagementPolicyTest, UserMayModifySettings) {
97 // No providers registered.
98 base::string16 error;
99 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
100 EXPECT_TRUE(error.empty());
102 // One provider, no relevant restriction.
103 policy_.RegisterProvider(&allow_all_);
104 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
105 EXPECT_TRUE(error.empty());
107 // Two providers, no relevant restrictions.
108 policy_.RegisterProvider(&no_load_);
109 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
110 EXPECT_TRUE(error.empty());
112 // Three providers, one with a relevant restriction.
113 policy_.RegisterProvider(&no_modify_status_);
114 EXPECT_FALSE(policy_.UserMayModifySettings(NULL, &error));
115 EXPECT_FALSE(error.empty());
117 // Remove the restriction.
118 policy_.UnregisterProvider(&no_modify_status_);
119 error.clear();
120 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
121 EXPECT_TRUE(error.empty());
124 TEST_F(ManagementPolicyTest, MustRemainEnabled) {
125 // No providers registered.
126 base::string16 error;
127 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
128 EXPECT_TRUE(error.empty());
130 // One provider, no relevant restriction.
131 policy_.RegisterProvider(&allow_all_);
132 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
133 EXPECT_TRUE(error.empty());
135 // Two providers, no relevant restrictions.
136 policy_.RegisterProvider(&no_modify_status_);
137 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
138 EXPECT_TRUE(error.empty());
140 // Three providers, one with a relevant restriction.
141 policy_.RegisterProvider(&must_remain_enabled_);
142 EXPECT_TRUE(policy_.MustRemainEnabled(NULL, &error));
143 EXPECT_FALSE(error.empty());
145 // Remove the restriction.
146 policy_.UnregisterProvider(&must_remain_enabled_);
147 error.clear();
148 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
149 EXPECT_TRUE(error.empty());
152 TEST_F(ManagementPolicyTest, MustRemainDisabled) {
153 // No providers registered.
154 base::string16 error;
155 EXPECT_FALSE(policy_.MustRemainDisabled(NULL, NULL, &error));
156 EXPECT_TRUE(error.empty());
158 // One provider, no relevant restriction.
159 policy_.RegisterProvider(&allow_all_);
160 EXPECT_FALSE(policy_.MustRemainDisabled(NULL, NULL, &error));
161 EXPECT_TRUE(error.empty());
163 // Two providers, no relevant restrictions.
164 policy_.RegisterProvider(&no_modify_status_);
165 EXPECT_FALSE(policy_.MustRemainDisabled(NULL, NULL, &error));
166 EXPECT_TRUE(error.empty());
168 // Three providers, one with a relevant restriction.
169 Extension::DisableReason reason = Extension::DISABLE_NONE;
170 policy_.RegisterProvider(&must_remain_disabled_);
171 EXPECT_TRUE(policy_.MustRemainDisabled(NULL, &reason, &error));
172 EXPECT_FALSE(error.empty());
173 EXPECT_EQ(Extension::DISABLE_SIDELOAD_WIPEOUT, reason);
175 // Remove the restriction.
176 policy_.UnregisterProvider(&must_remain_disabled_);
177 error.clear();
178 EXPECT_FALSE(policy_.MustRemainDisabled(NULL, NULL, &error));
179 EXPECT_TRUE(error.empty());
182 TEST_F(ManagementPolicyTest, MustRemainInstalled) {
183 // No providers registered.
184 base::string16 error;
185 EXPECT_FALSE(policy_.MustRemainInstalled(NULL, &error));
186 EXPECT_TRUE(error.empty());
188 // One provider, no relevant restriction.
189 policy_.RegisterProvider(&allow_all_);
190 EXPECT_FALSE(policy_.MustRemainInstalled(NULL, &error));
191 EXPECT_TRUE(error.empty());
193 // Two providers, no relevant restrictions.
194 policy_.RegisterProvider(&no_modify_status_);
195 EXPECT_FALSE(policy_.MustRemainInstalled(NULL, &error));
196 EXPECT_TRUE(error.empty());
198 // Three providers, one with a relevant restriction.
199 policy_.RegisterProvider(&must_remain_installed_);
200 EXPECT_TRUE(policy_.MustRemainInstalled(NULL, &error));
201 EXPECT_FALSE(error.empty());
203 // Remove the restriction.
204 policy_.UnregisterProvider(&must_remain_installed_);
205 error.clear();
206 EXPECT_FALSE(policy_.MustRemainInstalled(NULL, &error));
207 EXPECT_TRUE(error.empty());
210 // Tests error handling in the ManagementPolicy.
211 TEST_F(ManagementPolicyTest, ErrorHandling) {
212 // The error parameter should be unchanged if no restriction was found.
213 std::string original_error = "Ceci est en effet une erreur.";
214 base::string16 original_error16 = base::UTF8ToUTF16(original_error);
215 base::string16 error = original_error16;
216 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
217 EXPECT_EQ(original_error, base::UTF16ToUTF8(error));
218 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
219 EXPECT_EQ(original_error, base::UTF16ToUTF8(error));
220 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
221 EXPECT_EQ(original_error, base::UTF16ToUTF8(error));
223 // Ensure no crashes if no error message was requested.
224 EXPECT_TRUE(policy_.UserMayLoad(NULL, NULL));
225 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, NULL));
226 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, NULL));
227 policy_.RegisterProvider(&restrict_all_);
228 EXPECT_FALSE(policy_.UserMayLoad(NULL, NULL));
229 EXPECT_FALSE(policy_.UserMayModifySettings(NULL, NULL));
230 EXPECT_TRUE(policy_.MustRemainEnabled(NULL, NULL));
232 // Make sure returned error is correct.
233 error = original_error16;
234 EXPECT_FALSE(policy_.UserMayLoad(NULL, &error));
235 EXPECT_EQ(base::UTF8ToUTF16(TestProvider::expected_error()), error);
236 error = original_error16;
237 EXPECT_FALSE(policy_.UserMayModifySettings(NULL, &error));
238 EXPECT_EQ(base::UTF8ToUTF16(TestProvider::expected_error()), error);
239 error = original_error16;
240 EXPECT_TRUE(policy_.MustRemainEnabled(NULL, &error));
241 EXPECT_EQ(base::UTF8ToUTF16(TestProvider::expected_error()), error);