1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/utf_string_conversions.h"
#include "extensions/browser/management_policy.h"
#include "extensions/browser/test_management_policy.h"
#include "testing/gtest/include/gtest/gtest.h"
using TestProvider = extensions::TestManagementPolicyProvider;
using extensions::Extension;
class ManagementPolicyTest : public testing::Test {
public:
void SetUp() override {
allow_all_.SetProhibitedActions(TestProvider::ALLOW_ALL);
no_modify_status_.SetProhibitedActions(
TestProvider::PROHIBIT_MODIFY_STATUS);
no_load_.SetProhibitedActions(TestProvider::PROHIBIT_LOAD);
must_remain_enabled_.SetProhibitedActions(
TestProvider::MUST_REMAIN_ENABLED);
must_remain_disabled_.SetProhibitedActions(
TestProvider::MUST_REMAIN_DISABLED);
must_remain_disabled_.SetDisableReason(
extensions::disable_reason::DISABLE_SIDELOAD_WIPEOUT);
must_remain_installed_.SetProhibitedActions(
TestProvider::MUST_REMAIN_INSTALLED);
restrict_all_.SetProhibitedActions(TestProvider::PROHIBIT_MODIFY_STATUS |
TestProvider::PROHIBIT_LOAD |
TestProvider::MUST_REMAIN_ENABLED);
}
protected:
extensions::ManagementPolicy policy_;
TestProvider allow_all_;
TestProvider no_modify_status_;
TestProvider no_load_;
TestProvider must_remain_enabled_;
TestProvider must_remain_disabled_;
TestProvider must_remain_installed_;
TestProvider restrict_all_;
};
TEST_F(ManagementPolicyTest, RegisterAndUnregister) {
EXPECT_EQ(0, policy_.GetNumProviders());
policy_.RegisterProvider(&allow_all_);
EXPECT_EQ(1, policy_.GetNumProviders());
policy_.RegisterProvider(&allow_all_);
EXPECT_EQ(1, policy_.GetNumProviders());
policy_.RegisterProvider(&no_modify_status_);
EXPECT_EQ(2, policy_.GetNumProviders());
policy_.UnregisterProvider(&allow_all_);
EXPECT_EQ(1, policy_.GetNumProviders());
policy_.UnregisterProvider(&allow_all_);
EXPECT_EQ(1, policy_.GetNumProviders());
policy_.UnregisterProvider(&no_modify_status_);
EXPECT_EQ(0, policy_.GetNumProviders());
policy_.RegisterProvider(&allow_all_);
policy_.RegisterProvider(&no_modify_status_);
EXPECT_EQ(2, policy_.GetNumProviders());
policy_.UnregisterAllProviders();
EXPECT_EQ(0, policy_.GetNumProviders());
}
TEST_F(ManagementPolicyTest, UserMayLoad) {
// No providers registered.
// The extension and location are irrelevant to the
// TestManagementPolicyProviders.
EXPECT_TRUE(policy_.UserMayLoad(nullptr));
// One provider, no relevant restriction.
policy_.RegisterProvider(&no_modify_status_);
EXPECT_TRUE(policy_.UserMayLoad(nullptr));
// Two providers, no relevant restrictions.
policy_.RegisterProvider(&must_remain_enabled_);
EXPECT_TRUE(policy_.UserMayLoad(nullptr));
// Three providers, one with a relevant restriction.
policy_.RegisterProvider(&no_load_);
EXPECT_FALSE(policy_.UserMayLoad(nullptr));
// Remove the restriction.
policy_.UnregisterProvider(&no_load_);
EXPECT_TRUE(policy_.UserMayLoad(nullptr));
}
TEST_F(ManagementPolicyTest, UserMayModifySettings) {
// No providers registered.
std::u16string error;
EXPECT_TRUE(policy_.UserMayModifySettings(nullptr, &error));
EXPECT_TRUE(error.empty());
// One provider, no relevant restriction.
policy_.RegisterProvider(&allow_all_);
EXPECT_TRUE(policy_.UserMayModifySettings(nullptr, &error));
EXPECT_TRUE(error.empty());
// Two providers, no relevant restrictions.
policy_.RegisterProvider(&no_load_);
EXPECT_TRUE(policy_.UserMayModifySettings(nullptr, &error));
EXPECT_TRUE(error.empty());
// Three providers, one with a relevant restriction.
policy_.RegisterProvider(&no_modify_status_);
EXPECT_FALSE(policy_.UserMayModifySettings(nullptr, &error));
EXPECT_FALSE(error.empty());
// Remove the restriction.
policy_.UnregisterProvider(&no_modify_status_);
error.clear();
EXPECT_TRUE(policy_.UserMayModifySettings(nullptr, &error));
EXPECT_TRUE(error.empty());
}
TEST_F(ManagementPolicyTest, MustRemainEnabled) {
// No providers registered.
std::u16string error;
EXPECT_FALSE(policy_.MustRemainEnabled(nullptr, &error));
EXPECT_TRUE(error.empty());
// One provider, no relevant restriction.
policy_.RegisterProvider(&allow_all_);
EXPECT_FALSE(policy_.MustRemainEnabled(nullptr, &error));
EXPECT_TRUE(error.empty());
// Two providers, no relevant restrictions.
policy_.RegisterProvider(&no_modify_status_);
EXPECT_FALSE(policy_.MustRemainEnabled(nullptr, &error));
EXPECT_TRUE(error.empty());
// Three providers, one with a relevant restriction.
policy_.RegisterProvider(&must_remain_enabled_);
EXPECT_TRUE(policy_.MustRemainEnabled(nullptr, &error));
EXPECT_FALSE(error.empty());
// Remove the restriction.
policy_.UnregisterProvider(&must_remain_enabled_);
error.clear();
EXPECT_FALSE(policy_.MustRemainEnabled(nullptr, &error));
EXPECT_TRUE(error.empty());
}
TEST_F(ManagementPolicyTest, MustRemainDisabled) {
// No providers registered.
extensions::disable_reason::DisableReason reason =
extensions::disable_reason::DISABLE_NONE;
EXPECT_FALSE(policy_.MustRemainDisabled(nullptr, &reason));
EXPECT_EQ(extensions::disable_reason::DISABLE_NONE, reason);
// One provider, no relevant restriction.
policy_.RegisterProvider(&allow_all_);
EXPECT_FALSE(policy_.MustRemainDisabled(nullptr, &reason));
EXPECT_EQ(extensions::disable_reason::DISABLE_NONE, reason);
// Two providers, no relevant restrictions.
policy_.RegisterProvider(&no_modify_status_);
EXPECT_FALSE(policy_.MustRemainDisabled(nullptr, &reason));
EXPECT_EQ(extensions::disable_reason::DISABLE_NONE, reason);
// Three providers, one with a relevant restriction.
policy_.RegisterProvider(&must_remain_disabled_);
EXPECT_TRUE(policy_.MustRemainDisabled(nullptr, &reason));
EXPECT_EQ(extensions::disable_reason::DISABLE_SIDELOAD_WIPEOUT, reason);
// Remove the restriction.
policy_.UnregisterProvider(&must_remain_disabled_);
reason = extensions::disable_reason::DISABLE_NONE;
EXPECT_FALSE(policy_.MustRemainDisabled(nullptr, &reason));
EXPECT_EQ(extensions::disable_reason::DISABLE_NONE, reason);
}
TEST_F(ManagementPolicyTest, MustRemainInstalled) {
// No providers registered.
std::u16string error;
EXPECT_FALSE(policy_.MustRemainInstalled(nullptr, &error));
EXPECT_TRUE(error.empty());
// One provider, no relevant restriction.
policy_.RegisterProvider(&allow_all_);
EXPECT_FALSE(policy_.MustRemainInstalled(nullptr, &error));
EXPECT_TRUE(error.empty());
// Two providers, no relevant restrictions.
policy_.RegisterProvider(&no_modify_status_);
EXPECT_FALSE(policy_.MustRemainInstalled(nullptr, &error));
EXPECT_TRUE(error.empty());
// Three providers, one with a relevant restriction.
policy_.RegisterProvider(&must_remain_installed_);
EXPECT_TRUE(policy_.MustRemainInstalled(nullptr, &error));
EXPECT_FALSE(error.empty());
// Remove the restriction.
policy_.UnregisterProvider(&must_remain_installed_);
error.clear();
EXPECT_FALSE(policy_.MustRemainInstalled(nullptr, &error));
EXPECT_TRUE(error.empty());
}
// Tests error handling in the ManagementPolicy.
TEST_F(ManagementPolicyTest, ErrorHandling) {
// The error parameter should be unchanged if no restriction was found.
std::string original_error = "Ceci est en effet une erreur.";
std::u16string original_error16 = base::UTF8ToUTF16(original_error);
std::u16string error = original_error16;
EXPECT_TRUE(policy_.UserMayModifySettings(nullptr, &error));
EXPECT_EQ(original_error, base::UTF16ToUTF8(error));
EXPECT_FALSE(policy_.MustRemainEnabled(nullptr, &error));
EXPECT_EQ(original_error, base::UTF16ToUTF8(error));
// Ensure no crashes if no error message was requested.
EXPECT_TRUE(policy_.UserMayModifySettings(nullptr, nullptr));
EXPECT_FALSE(policy_.MustRemainEnabled(nullptr, nullptr));
policy_.RegisterProvider(&restrict_all_);
EXPECT_FALSE(policy_.UserMayModifySettings(nullptr, nullptr));
EXPECT_TRUE(policy_.MustRemainEnabled(nullptr, nullptr));
// Make sure returned error is correct.
EXPECT_FALSE(policy_.UserMayModifySettings(nullptr, &error));
EXPECT_EQ(base::UTF8ToUTF16(TestProvider::expected_error()), error);
error = original_error16;
EXPECT_TRUE(policy_.MustRemainEnabled(nullptr, &error));
EXPECT_EQ(base::UTF8ToUTF16(TestProvider::expected_error()), error);
}
|