1 // Copyright (c) 2012 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 "chrome/browser/extensions/management_policy.h"
7 #include "chrome/browser/extensions/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 restrict_all_
.SetProhibitedActions(TestProvider::PROHIBIT_MODIFY_STATUS
|
23 TestProvider::PROHIBIT_LOAD
|
24 TestProvider::MUST_REMAIN_ENABLED
);
28 extensions::ManagementPolicy policy_
;
30 TestProvider allow_all_
;
31 TestProvider no_modify_status_
;
32 TestProvider no_load_
;
33 TestProvider must_remain_enabled_
;
34 TestProvider restrict_all_
;
37 TEST_F(ManagementPolicyTest
, RegisterAndUnregister
) {
38 EXPECT_EQ(0, policy_
.GetNumProviders());
39 policy_
.RegisterProvider(&allow_all_
);
40 EXPECT_EQ(1, policy_
.GetNumProviders());
41 policy_
.RegisterProvider(&allow_all_
);
42 EXPECT_EQ(1, policy_
.GetNumProviders());
44 policy_
.RegisterProvider(&no_modify_status_
);
45 EXPECT_EQ(2, policy_
.GetNumProviders());
46 policy_
.UnregisterProvider(&allow_all_
);
47 EXPECT_EQ(1, policy_
.GetNumProviders());
48 policy_
.UnregisterProvider(&allow_all_
);
49 EXPECT_EQ(1, policy_
.GetNumProviders());
50 policy_
.UnregisterProvider(&no_modify_status_
);
51 EXPECT_EQ(0, policy_
.GetNumProviders());
53 policy_
.RegisterProvider(&allow_all_
);
54 policy_
.RegisterProvider(&no_modify_status_
);
55 EXPECT_EQ(2, policy_
.GetNumProviders());
56 policy_
.UnregisterAllProviders();
57 EXPECT_EQ(0, policy_
.GetNumProviders());
60 TEST_F(ManagementPolicyTest
, UserMayLoad
) {
61 // No providers registered.
63 // The extension and location are irrelevant to the
64 // TestManagementPolicyProviders.
65 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
66 EXPECT_TRUE(error
.empty());
68 // One provider, no relevant restriction.
69 policy_
.RegisterProvider(&no_modify_status_
);
70 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
71 EXPECT_TRUE(error
.empty());
73 // Two providers, no relevant restrictions.
74 policy_
.RegisterProvider(&must_remain_enabled_
);
75 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
76 EXPECT_TRUE(error
.empty());
78 // Three providers, one with a relevant restriction.
79 policy_
.RegisterProvider(&no_load_
);
80 EXPECT_FALSE(policy_
.UserMayLoad(NULL
, &error
));
81 EXPECT_FALSE(error
.empty());
83 // Remove the restriction.
84 policy_
.UnregisterProvider(&no_load_
);
86 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
87 EXPECT_TRUE(error
.empty());
89 TEST_F(ManagementPolicyTest
, UserMayModifySettings
) {
90 // No providers registered.
92 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
93 EXPECT_TRUE(error
.empty());
95 // One provider, no relevant restriction.
96 policy_
.RegisterProvider(&allow_all_
);
97 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
98 EXPECT_TRUE(error
.empty());
100 // Two providers, no relevant restrictions.
101 policy_
.RegisterProvider(&no_load_
);
102 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
103 EXPECT_TRUE(error
.empty());
105 // Three providers, one with a relevant restriction.
106 policy_
.RegisterProvider(&no_modify_status_
);
107 EXPECT_FALSE(policy_
.UserMayModifySettings(NULL
, &error
));
108 EXPECT_FALSE(error
.empty());
110 // Remove the restriction.
111 policy_
.UnregisterProvider(&no_modify_status_
);
113 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
114 EXPECT_TRUE(error
.empty());
117 TEST_F(ManagementPolicyTest
, MustRemainEnabled
) {
118 // No providers registered.
120 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
121 EXPECT_TRUE(error
.empty());
123 // One provider, no relevant restriction.
124 policy_
.RegisterProvider(&allow_all_
);
125 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
126 EXPECT_TRUE(error
.empty());
128 // Two providers, no relevant restrictions.
129 policy_
.RegisterProvider(&no_modify_status_
);
130 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
131 EXPECT_TRUE(error
.empty());
133 // Three providers, one with a relevant restriction.
134 policy_
.RegisterProvider(&must_remain_enabled_
);
135 EXPECT_TRUE(policy_
.MustRemainEnabled(NULL
, &error
));
136 EXPECT_FALSE(error
.empty());
138 // Remove the restriction.
139 policy_
.UnregisterProvider(&must_remain_enabled_
);
141 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
142 EXPECT_TRUE(error
.empty());
145 // Tests error handling in the ManagementPolicy.
146 TEST_F(ManagementPolicyTest
, ErrorHandling
) {
147 // The error parameter should be unchanged if no restriction was found.
148 std::string original_error
= "Ceci est en effet une erreur.";
149 string16 original_error16
= UTF8ToUTF16(original_error
);
150 string16 error
= original_error16
;
151 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, &error
));
152 EXPECT_EQ(original_error
, UTF16ToUTF8(error
));
153 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, &error
));
154 EXPECT_EQ(original_error
, UTF16ToUTF8(error
));
155 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, &error
));
156 EXPECT_EQ(original_error
, UTF16ToUTF8(error
));
158 // Ensure no crashes if no error message was requested.
159 EXPECT_TRUE(policy_
.UserMayLoad(NULL
, NULL
));
160 EXPECT_TRUE(policy_
.UserMayModifySettings(NULL
, NULL
));
161 EXPECT_FALSE(policy_
.MustRemainEnabled(NULL
, NULL
));
162 policy_
.RegisterProvider(&restrict_all_
);
163 EXPECT_FALSE(policy_
.UserMayLoad(NULL
, NULL
));
164 EXPECT_FALSE(policy_
.UserMayModifySettings(NULL
, NULL
));
165 EXPECT_TRUE(policy_
.MustRemainEnabled(NULL
, NULL
));
167 // Make sure returned error is correct.
168 error
= original_error16
;
169 EXPECT_FALSE(policy_
.UserMayLoad(NULL
, &error
));
170 EXPECT_EQ(UTF8ToUTF16(TestProvider::expected_error()), error
);
171 error
= original_error16
;
172 EXPECT_FALSE(policy_
.UserMayModifySettings(NULL
, &error
));
173 EXPECT_EQ(UTF8ToUTF16(TestProvider::expected_error()), error
);
174 error
= original_error16
;
175 EXPECT_TRUE(policy_
.MustRemainEnabled(NULL
, &error
));
176 EXPECT_EQ(UTF8ToUTF16(TestProvider::expected_error()), error
);