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.
4
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"
9
10 typedef extensions::TestManagementPolicyProvider TestProvider;
11 using extensions::Extension;
12
13 class ManagementPolicyTest : public testing::Test {
14 public:
SetUp()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);
28 }
29
30 protected:
31 extensions::ManagementPolicy policy_;
32
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_;
39 };
40
TEST_F(ManagementPolicyTest,RegisterAndUnregister)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());
47
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());
56
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());
62 }
63
TEST_F(ManagementPolicyTest,UserMayLoad)64 TEST_F(ManagementPolicyTest, UserMayLoad) {
65 // No providers registered.
66 string16 error;
67 // The extension and location are irrelevant to the
68 // TestManagementPolicyProviders.
69 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
70 EXPECT_TRUE(error.empty());
71
72 // One provider, no relevant restriction.
73 policy_.RegisterProvider(&no_modify_status_);
74 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
75 EXPECT_TRUE(error.empty());
76
77 // Two providers, no relevant restrictions.
78 policy_.RegisterProvider(&must_remain_enabled_);
79 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
80 EXPECT_TRUE(error.empty());
81
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());
86
87 // Remove the restriction.
88 policy_.UnregisterProvider(&no_load_);
89 error.clear();
90 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
91 EXPECT_TRUE(error.empty());
92 }
TEST_F(ManagementPolicyTest,UserMayModifySettings)93 TEST_F(ManagementPolicyTest, UserMayModifySettings) {
94 // No providers registered.
95 string16 error;
96 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
97 EXPECT_TRUE(error.empty());
98
99 // One provider, no relevant restriction.
100 policy_.RegisterProvider(&allow_all_);
101 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
102 EXPECT_TRUE(error.empty());
103
104 // Two providers, no relevant restrictions.
105 policy_.RegisterProvider(&no_load_);
106 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
107 EXPECT_TRUE(error.empty());
108
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());
113
114 // Remove the restriction.
115 policy_.UnregisterProvider(&no_modify_status_);
116 error.clear();
117 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
118 EXPECT_TRUE(error.empty());
119 }
120
TEST_F(ManagementPolicyTest,MustRemainEnabled)121 TEST_F(ManagementPolicyTest, MustRemainEnabled) {
122 // No providers registered.
123 string16 error;
124 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
125 EXPECT_TRUE(error.empty());
126
127 // One provider, no relevant restriction.
128 policy_.RegisterProvider(&allow_all_);
129 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
130 EXPECT_TRUE(error.empty());
131
132 // Two providers, no relevant restrictions.
133 policy_.RegisterProvider(&no_modify_status_);
134 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
135 EXPECT_TRUE(error.empty());
136
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());
141
142 // Remove the restriction.
143 policy_.UnregisterProvider(&must_remain_enabled_);
144 error.clear();
145 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
146 EXPECT_TRUE(error.empty());
147 }
148
TEST_F(ManagementPolicyTest,MustRemainDisabled)149 TEST_F(ManagementPolicyTest, MustRemainDisabled) {
150 // No providers registered.
151 string16 error;
152 EXPECT_FALSE(policy_.MustRemainDisabled(NULL, NULL, &error));
153 EXPECT_TRUE(error.empty());
154
155 // One provider, no relevant restriction.
156 policy_.RegisterProvider(&allow_all_);
157 EXPECT_FALSE(policy_.MustRemainDisabled(NULL, NULL, &error));
158 EXPECT_TRUE(error.empty());
159
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());
164
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);
171
172 // Remove the restriction.
173 policy_.UnregisterProvider(&must_remain_disabled_);
174 error.clear();
175 EXPECT_FALSE(policy_.MustRemainDisabled(NULL, NULL, &error));
176 EXPECT_TRUE(error.empty());
177 }
178
179 // Tests error handling in the ManagementPolicy.
TEST_F(ManagementPolicyTest,ErrorHandling)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 string16 original_error16 = UTF8ToUTF16(original_error);
184 string16 error = original_error16;
185 EXPECT_TRUE(policy_.UserMayLoad(NULL, &error));
186 EXPECT_EQ(original_error, UTF16ToUTF8(error));
187 EXPECT_TRUE(policy_.UserMayModifySettings(NULL, &error));
188 EXPECT_EQ(original_error, UTF16ToUTF8(error));
189 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error));
190 EXPECT_EQ(original_error, UTF16ToUTF8(error));
191
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));
200
201 // Make sure returned error is correct.
202 error = original_error16;
203 EXPECT_FALSE(policy_.UserMayLoad(NULL, &error));
204 EXPECT_EQ(UTF8ToUTF16(TestProvider::expected_error()), error);
205 error = original_error16;
206 EXPECT_FALSE(policy_.UserMayModifySettings(NULL, &error));
207 EXPECT_EQ(UTF8ToUTF16(TestProvider::expected_error()), error);
208 error = original_error16;
209 EXPECT_TRUE(policy_.MustRemainEnabled(NULL, &error));
210 EXPECT_EQ(UTF8ToUTF16(TestProvider::expected_error()), error);
211 }
212