Commit 997defcf authored by Xiaoling Bao's avatar Xiaoling Bao Committed by Commit Bot

Update policy merge algorithm.

Original algorithm: choose the active policy provider with the highest
   priority and apply all policies from it.

Updated algorithm: for each policy, find the highest priority provider
 that has defined value, and returns that policy.

Bug: 1134303
Change-Id: I3461f476e5404f97a08a6a83f57346804e84e678
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2443541Reviewed-by: default avatarSorin Jianu <sorin@chromium.org>
Reviewed-by: default avatarS. Ganesh <ganesh@chromium.org>
Commit-Queue: Xiaoling Bao <xiaolingbao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813449}
parent 7f4de763
This diff is collapsed.
...@@ -61,17 +61,9 @@ class PolicyService : public PolicyManagerInterface { ...@@ -61,17 +61,9 @@ class PolicyService : public PolicyManagerInterface {
const PolicyManagerInterface& GetActivePolicyManager(); const PolicyManagerInterface& GetActivePolicyManager();
private: private:
bool ShouldFallbackToDefaultManager() const;
// Sets the policy manager that is managed and has the highest priority as the
// active policy manager. If no manager is managed, use the default policy
// manager as the active one.
void UpdateActivePolicyManager();
// List of policy managers in descending order of priority. The first policy // List of policy managers in descending order of priority. The first policy
// manager's policies takes precedence over the following. // manager's policies takes precedence over the following.
std::vector<std::unique_ptr<PolicyManagerInterface>> policy_managers_; std::vector<std::unique_ptr<PolicyManagerInterface>> policy_managers_;
std::unique_ptr<PolicyManagerInterface> default_policy_manager_;
const PolicyManagerInterface* active_policy_manager_;
}; };
std::unique_ptr<PolicyService> GetUpdaterPolicyService(); std::unique_ptr<PolicyService> GetUpdaterPolicyService();
......
...@@ -16,10 +16,11 @@ namespace updater { ...@@ -16,10 +16,11 @@ namespace updater {
// Policy and Device Management. // Policy and Device Management.
class FakePolicyManager : public PolicyManagerInterface { class FakePolicyManager : public PolicyManagerInterface {
public: public:
explicit FakePolicyManager(const std::string& source) : source_(source) {}
~FakePolicyManager() override = default; ~FakePolicyManager() override = default;
std::string source() const override { return source_; } std::string source() const override { return source_; }
bool IsManaged() const override { return managed_; } bool IsManaged() const override { return true; }
bool GetLastCheckPeriodMinutes(int* minutes) const override { return false; } bool GetLastCheckPeriodMinutes(int* minutes) const override { return false; }
bool GetUpdatesSuppressedTimes(int* start_hour, bool GetUpdatesSuppressedTimes(int* start_hour,
int* start_min, int* start_min,
...@@ -28,7 +29,14 @@ class FakePolicyManager : public PolicyManagerInterface { ...@@ -28,7 +29,14 @@ class FakePolicyManager : public PolicyManagerInterface {
} }
bool GetDownloadPreferenceGroupPolicy( bool GetDownloadPreferenceGroupPolicy(
std::string* download_preference) const override { std::string* download_preference) const override {
return false; if (download_preference_.empty())
return false;
*download_preference = download_preference_;
return true;
}
void SetDownloadPreferenceGroupPolicy(const std::string& preference) {
download_preference_ = preference;
} }
bool GetPackageCacheSizeLimitMBytes(int* cache_size_limit) const override { bool GetPackageCacheSizeLimitMBytes(int* cache_size_limit) const override {
return false; return false;
...@@ -42,7 +50,14 @@ class FakePolicyManager : public PolicyManagerInterface { ...@@ -42,7 +50,14 @@ class FakePolicyManager : public PolicyManagerInterface {
} }
bool GetEffectivePolicyForAppUpdates(const std::string& app_id, bool GetEffectivePolicyForAppUpdates(const std::string& app_id,
int* update_policy) const override { int* update_policy) const override {
return false; auto value = update_policies_.find(app_id);
if (value == update_policies_.end())
return false;
*update_policy = value->second;
return true;
}
void SetUpdatePolicy(const std::string& app_id, int update_policy) {
update_policies_[app_id] = update_policy;
} }
bool GetTargetVersionPrefix( bool GetTargetVersionPrefix(
const std::string& app_id, const std::string& app_id,
...@@ -72,67 +87,96 @@ class FakePolicyManager : public PolicyManagerInterface { ...@@ -72,67 +87,96 @@ class FakePolicyManager : public PolicyManagerInterface {
channels_[app_id] = std::move(channel); channels_[app_id] = std::move(channel);
} }
static std::unique_ptr<FakePolicyManager> GetTestingPolicyManager(
std::string source,
bool managed) {
auto manager = std::make_unique<FakePolicyManager>();
manager->source_ = std::move(source);
manager->managed_ = managed;
return manager;
}
private: private:
std::string source_; std::string source_;
std::string download_preference_;
std::map<std::string, std::string> channels_; std::map<std::string, std::string> channels_;
bool managed_; std::map<std::string, int> update_policies_;
}; };
TEST(PolicyService, ReturnsHighestPriorityManagedPolicyManager) { TEST(PolicyService, DefaultPolicyValue) {
std::unique_ptr<PolicyService> policy_service(GetUpdaterPolicyService()); std::unique_ptr<PolicyService> policy_service(GetUpdaterPolicyService());
std::vector<std::unique_ptr<PolicyManagerInterface>> managers; std::vector<std::unique_ptr<PolicyManagerInterface>> managers;
managers.emplace_back( managers.push_back(GetPolicyManager());
FakePolicyManager::GetTestingPolicyManager("highest_unmanaged", false));
managers.emplace_back(
FakePolicyManager::GetTestingPolicyManager("highest_managed", true));
managers.emplace_back(
FakePolicyManager::GetTestingPolicyManager("managed", true));
managers.emplace_back(
FakePolicyManager::GetTestingPolicyManager("lowest_managed", true));
managers.emplace_back(
FakePolicyManager::GetTestingPolicyManager("lowest_unmanaged", false));
policy_service->SetPolicyManagersForTesting(std::move(managers)); policy_service->SetPolicyManagersForTesting(std::move(managers));
ASSERT_EQ("highest_managed", EXPECT_EQ(policy_service->source(), "");
policy_service->GetActivePolicyManager().source());
}
TEST(PolicyService, ReturnsDefaultPolicyManager) { std::string version_prefix;
std::unique_ptr<PolicyService> policy_service(GetUpdaterPolicyService()); EXPECT_FALSE(policy_service->GetTargetVersionPrefix("", &version_prefix));
policy_service->SetPolicyManagersForTesting({}); int last_check = 0;
ASSERT_EQ("default", policy_service->GetActivePolicyManager().source()); EXPECT_FALSE(policy_service->GetLastCheckPeriodMinutes(&last_check));
} }
TEST(PolicyService, TargetChannelUnmanagedSource) { TEST(PolicyService, SinglePolicyManager) {
std::unique_ptr<PolicyService> policy_service(GetUpdaterPolicyService()); std::unique_ptr<PolicyService> policy_service(GetUpdaterPolicyService());
auto manager = FakePolicyManager::GetTestingPolicyManager("unmanaged", false); auto manager = std::make_unique<FakePolicyManager>("test_source");
manager->SetChannel("", "channel"); manager->SetChannel("app1", "test_channel");
manager->SetUpdatePolicy("app2", 3);
std::vector<std::unique_ptr<PolicyManagerInterface>> managers; std::vector<std::unique_ptr<PolicyManagerInterface>> managers;
managers.emplace_back(std::move(manager)); managers.push_back(std::move(manager));
policy_service->SetPolicyManagersForTesting(std::move(managers)); policy_service->SetPolicyManagersForTesting(std::move(managers));
EXPECT_EQ(policy_service->source(), "test_source");
std::string channel; std::string channel;
policy_service->GetTargetChannel("", &channel); EXPECT_FALSE(policy_service->GetTargetChannel("app2", &channel));
ASSERT_TRUE(channel.empty()); EXPECT_TRUE(policy_service->GetTargetChannel("app1", &channel));
EXPECT_EQ(channel, "test_channel");
int update_policy = 0;
EXPECT_FALSE(
policy_service->GetEffectivePolicyForAppUpdates("app1", &update_policy));
EXPECT_TRUE(
policy_service->GetEffectivePolicyForAppUpdates("app2", &update_policy));
EXPECT_EQ(update_policy, 3);
} }
TEST(PolicyService, TargetChannelManagedSource) { TEST(PolicyService, MultiplePolicyManagers) {
std::unique_ptr<PolicyService> policy_service(GetUpdaterPolicyService()); std::unique_ptr<PolicyService> policy_service(GetUpdaterPolicyService());
auto manager = FakePolicyManager::GetTestingPolicyManager("managed", true);
manager->SetChannel("", "channel");
std::vector<std::unique_ptr<PolicyManagerInterface>> managers; std::vector<std::unique_ptr<PolicyManagerInterface>> managers;
managers.emplace_back(std::move(manager));
auto manager = std::make_unique<FakePolicyManager>("group_policy");
manager->SetChannel("app1", "channel_gp");
manager->SetUpdatePolicy("app2", 1);
managers.push_back(std::move(manager));
manager = std::make_unique<FakePolicyManager>("device_management");
manager->SetChannel("app1", "channel_dm");
manager->SetUpdatePolicy("app1", 3);
managers.push_back(std::move(manager));
manager = std::make_unique<FakePolicyManager>("imaginary");
manager->SetChannel("app1", "channel_imaginary");
manager->SetUpdatePolicy("app1", 2);
manager->SetDownloadPreferenceGroupPolicy("cacheable");
managers.push_back(std::move(manager));
// The default policy manager.
managers.push_back(GetPolicyManager());
policy_service->SetPolicyManagersForTesting(std::move(managers)); policy_service->SetPolicyManagersForTesting(std::move(managers));
EXPECT_EQ(policy_service->source(),
"group_policy;device_management;imaginary");
std::string channel; std::string channel;
policy_service->GetTargetChannel("", &channel); EXPECT_TRUE(policy_service->GetTargetChannel("app1", &channel));
ASSERT_EQ(channel, "channel"); EXPECT_EQ(channel, "channel_gp");
int update_policy = 0;
EXPECT_TRUE(
policy_service->GetEffectivePolicyForAppUpdates("app1", &update_policy));
EXPECT_EQ(update_policy, 3);
EXPECT_TRUE(
policy_service->GetEffectivePolicyForAppUpdates("app2", &update_policy));
EXPECT_EQ(update_policy, 1);
std::string download_preference;
EXPECT_TRUE(
policy_service->GetDownloadPreferenceGroupPolicy(&download_preference));
EXPECT_EQ(download_preference, "cacheable");
int cache_size_limit = 0;
EXPECT_FALSE(
policy_service->GetPackageCacheSizeLimitMBytes(&cache_size_limit));
} }
} // namespace updater } // namespace updater
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment