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
{
15 virtual void SetUp() {
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 restrict_all_
.SetProhibitedActions(TestProvider::PROHIBIT_MODIFY_STATUS
|
26 TestProvider::PROHIBIT_LOAD
|
27 TestProvider::MUST_REMAIN_ENABLED
);
31 extensions::ManagementPolicy policy_
;
33 TestProvider allow_all_
;
34 TestProvider no_modify_status_
;
35 TestProvider no_load_
;
36 TestProvider must_remain_enabled_
;
37 TestProvider must_remain_disabled_
;
38 TestProvider restrict_all_
;
41 TEST_F(ManagementPolicyTest
, RegisterAndUnregister
) {
42 EXPECT_EQ(0, policy_
.GetNumProviders());
43 policy_
.RegisterProvider(&allow_all_
);
44 EXPECT_EQ(1, policy_
.GetNumProviders());
45 policy_
.RegisterProvider(&allow_all_
);
46 EXPECT_EQ(1, policy_
.GetNumProviders());
48 policy_
.RegisterProvider(&no_modify_status_
);
49 EXPECT_EQ(2, policy_
.GetNumProviders());
50 policy_
.UnregisterProvider(&allow_all_
);
51 EXPECT_EQ(1, policy_
.GetNumProviders());
52 policy_
.UnregisterProvider(&allow_all_
);
53 EXPECT_EQ(1, policy_
.GetNumProviders());
54 policy_
.UnregisterProvider(&no_modify_status_
);
55 EXPECT_EQ(0, policy_
.GetNumProviders());
57 policy_
.RegisterProvider(&allow_all_
);
58 policy_
.RegisterProvider(&no_modify_status_
);
59 EXPECT_EQ(2, policy_
.GetNumProviders());
60 policy_
.UnregisterAllProviders();
61 EXPECT_EQ(0, policy_
.GetNumProviders());
64 TEST_F(ManagementPolicyTest
, UserMayLoad
) {
65 // No providers registered.
67 // The extension and location are irrelevant to the
68 // TestManagementPolicyProviders.
69 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
70 EXPECT_TRUE(error
.empty());
72 // One provider, no relevant restriction.
73 policy_
.RegisterProvider(&no_modify_status_
);
74 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
75 EXPECT_TRUE(error
.empty());
77 // Two providers, no relevant restrictions.
78 policy_
.RegisterProvider(&must_remain_enabled_
);
79 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
80 EXPECT_TRUE(error
.empty());
82 // Three providers, one with a relevant restriction.
83 policy_
.RegisterProvider(&no_load_
);
84 EXPECT_FALSE(policy_
.UserMayLoad(NULL
, &error
));
85 EXPECT_FALSE(error
.empty());
87 // Remove the restriction.
88 policy_
.UnregisterProvider(&no_load_
);
90 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
91 EXPECT_TRUE(error
.empty());
93 TEST_F(ManagementPolicyTest
, UserMayModifySettings
) {
94 // No providers registered.
96 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
97 EXPECT_TRUE(error
.empty());
99 // One provider, no relevant restriction.
100 policy_
.RegisterProvider(&allow_all_
);
101 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
102 EXPECT_TRUE(error
.empty());
104 // Two providers, no relevant restrictions.
105 policy_
.RegisterProvider(&no_load_
);
106 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
107 EXPECT_TRUE(error
.empty());
109 // Three providers, one with a relevant restriction.
110 policy_
.RegisterProvider(&no_modify_status_
);
111 EXPECT_FALSE(policy_
.UserMayModifySettings(NULL
, &error
));
112 EXPECT_FALSE(error
.empty());
114 // Remove the restriction.
115 policy_
.UnregisterProvider(&no_modify_status_
);
117 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
118 EXPECT_TRUE(error
.empty());
121 TEST_F(ManagementPolicyTest
, MustRemainEnabled
) {
122 // No providers registered.
123 base::string16 error
;
124 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
125 EXPECT_TRUE(error
.empty());
127 // One provider, no relevant restriction.
128 policy_
.RegisterProvider(&allow_all_
);
129 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
130 EXPECT_TRUE(error
.empty());
132 // Two providers, no relevant restrictions.
133 policy_
.RegisterProvider(&no_modify_status_
);
134 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
135 EXPECT_TRUE(error
.empty());
137 // Three providers, one with a relevant restriction.
138 policy_
.RegisterProvider(&must_remain_enabled_
);
139 EXPECT_TRUE(policy_
.MustRemainEnabled(NULL
, &error
));
140 EXPECT_FALSE(error
.empty());
142 // Remove the restriction.
143 policy_
.UnregisterProvider(&must_remain_enabled_
);
145 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
146 EXPECT_TRUE(error
.empty());
149 TEST_F(ManagementPolicyTest
, MustRemainDisabled
) {
150 // No providers registered.
151 base::string16 error
;
152 EXPECT_FALSE(policy_
.MustRemainDisabled(NULL
, NULL
, &error
));
153 EXPECT_TRUE(error
.empty());
155 // One provider, no relevant restriction.
156 policy_
.RegisterProvider(&allow_all_
);
157 EXPECT_FALSE(policy_
.MustRemainDisabled(NULL
, NULL
, &error
));
158 EXPECT_TRUE(error
.empty());
160 // Two providers, no relevant restrictions.
161 policy_
.RegisterProvider(&no_modify_status_
);
162 EXPECT_FALSE(policy_
.MustRemainDisabled(NULL
, NULL
, &error
));
163 EXPECT_TRUE(error
.empty());
165 // Three providers, one with a relevant restriction.
166 Extension::DisableReason reason
= Extension::DISABLE_NONE
;
167 policy_
.RegisterProvider(&must_remain_disabled_
);
168 EXPECT_TRUE(policy_
.MustRemainDisabled(NULL
, &reason
, &error
));
169 EXPECT_FALSE(error
.empty());
170 EXPECT_EQ(Extension::DISABLE_SIDELOAD_WIPEOUT
, reason
);
172 // Remove the restriction.
173 policy_
.UnregisterProvider(&must_remain_disabled_
);
175 EXPECT_FALSE(policy_
.MustRemainDisabled(NULL
, NULL
, &error
));
176 EXPECT_TRUE(error
.empty());
179 // Tests error handling in the ManagementPolicy.
180 TEST_F(ManagementPolicyTest
, ErrorHandling
) {
181 // The error parameter should be unchanged if no restriction was found.
182 std::string original_error
= "Ceci est en effet une erreur.";
183 base::string16 original_error16
= base::UTF8ToUTF16(original_error
);
184 base::string16 error
= original_error16
;
185 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
186 EXPECT_EQ(original_error
, base::UTF16ToUTF8(error
));
187 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
188 EXPECT_EQ(original_error
, base::UTF16ToUTF8(error
));
189 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
190 EXPECT_EQ(original_error
, base::UTF16ToUTF8(error
));
192 // Ensure no crashes if no error message was requested.
193 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, NULL
));
194 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, NULL
));
195 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, NULL
));
196 policy_
.RegisterProvider(&restrict_all_
);
197 EXPECT_FALSE(policy_
.UserMayLoad(NULL
, NULL
));
198 EXPECT_FALSE(policy_
.UserMayModifySettings(NULL
, NULL
));
199 EXPECT_TRUE(policy_
.MustRemainEnabled(NULL
, NULL
));
201 // Make sure returned error is correct.
202 error
= original_error16
;
203 EXPECT_FALSE(policy_
.UserMayLoad(NULL
, &error
));
204 EXPECT_EQ(base::UTF8ToUTF16(TestProvider::expected_error()), error
);
205 error
= original_error16
;
206 EXPECT_FALSE(policy_
.UserMayModifySettings(NULL
, &error
));
207 EXPECT_EQ(base::UTF8ToUTF16(TestProvider::expected_error()), error
);
208 error
= original_error16
;
209 EXPECT_TRUE(policy_
.MustRemainEnabled(NULL
, &error
));
210 EXPECT_EQ(base::UTF8ToUTF16(TestProvider::expected_error()), error
);