PolicyManager: UpdateCheckAllowed policy initial implementation.

This patch implements the UpdateCheckAllowed policy for ChromeOS
using the same logic we had on update_check_scheduler.cc. It checks
for updates onces every 45 minutes and does an exponential backoff
up to 4 hours when the update check fails. Some other parts of the
policy are not implemented, such as retry an update check with a
short delay on certain failures.

BUG=chromium:358269
TEST=Unittests added to the policy.

Change-Id: Ief8deff47fd6490bd70a22ba20abed05fcc37ab4
Reviewed-on: https://chromium-review.googlesource.com/197595
Reviewed-by: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/policy_manager_unittest.cc b/policy_manager/policy_manager_unittest.cc
index 15553a5..5272bcd 100644
--- a/policy_manager/policy_manager_unittest.cc
+++ b/policy_manager/policy_manager_unittest.cc
@@ -50,7 +50,7 @@
 class FailingPolicy : public DefaultPolicy {
   virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
                                         string* error,
-                                        bool* result) const {
+                                        UpdateCheckParams* result) const {
     *error = "FailingPolicy failed.";
     return EvalStatus::kFailed;
   }
@@ -60,7 +60,7 @@
 class LazyPolicy : public DefaultPolicy {
   virtual EvalStatus UpdateCheckAllowed(EvaluationContext* ec, State* state,
                                         string* error,
-                                        bool* result) const {
+                                        UpdateCheckParams* result) const {
     return EvalStatus::kAskMeAgainLater;
   }
 };
@@ -75,41 +75,19 @@
   acc->push_back(std::make_pair(status, result));
 }
 
-TEST_F(PmPolicyManagerTest, PolicyRequestCallReturnsSuccess) {
+// Tests that policy requests are completed successfully. It is important that
+// this tests cover all policy requests as defined in Policy.
+TEST_F(PmPolicyManagerTest, PolicyRequestCallUpdateDownloadAndApplyAllowed) {
   bool result;
-  EvalStatus status;
-
-  // Tests that policy requests are completed successfully. It is important that
-  // this test covers all policy requests as defined in Policy.
-  //
-  // TODO(garnold) We may need to adapt this test as the Chrome OS policy grows
-  // beyond the stub implementation.
-  status = pmut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result);
-  EXPECT_EQ(EvalStatus::kSucceeded, status);
-  status = pmut_->PolicyRequest(&Policy::UpdateDownloadAndApplyAllowed,
-                                &result);
-  EXPECT_EQ(EvalStatus::kSucceeded, status);
+  EXPECT_EQ(EvalStatus::kSucceeded,
+            pmut_->PolicyRequest(&Policy::UpdateDownloadAndApplyAllowed,
+                                 &result));
 }
 
-TEST_F(PmPolicyManagerTest, PolicyRequestCallsPolicy) {
-  StrictMock<MockPolicy>* policy = new StrictMock<MockPolicy>();
-  pmut_->set_policy(policy);
-  bool result;
-  EvalStatus status;
-
-  // Tests that the policy methods are actually called on the policy instance.
-  // It is important that this test covers all policy requests as defined in
-  // Policy.
-  EXPECT_CALL(*policy, UpdateCheckAllowed(_, _, _, _))
-      .WillOnce(Return(EvalStatus::kSucceeded));
-  status = pmut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result);
-  EXPECT_EQ(EvalStatus::kSucceeded, status);
-
-  EXPECT_CALL(*policy, UpdateDownloadAndApplyAllowed(_, _, _, _))
-      .WillOnce(Return(EvalStatus::kSucceeded));
-  status = pmut_->PolicyRequest(&Policy::UpdateDownloadAndApplyAllowed,
-                                &result);
-  EXPECT_EQ(EvalStatus::kSucceeded, status);
+TEST_F(PmPolicyManagerTest, PolicyRequestCallUpdateCheckAllowed) {
+  UpdateCheckParams result;
+  EXPECT_EQ(EvalStatus::kSucceeded, pmut_->PolicyRequest(
+      &Policy::UpdateCheckAllowed, &result));
 }
 
 TEST_F(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError) {
@@ -117,16 +95,17 @@
 
   // Tests that the DefaultPolicy instance is called when the method fails,
   // which will set this as true.
-  bool result = false;
+  UpdateCheckParams result;
+  result.updates_enabled = false;
   EvalStatus status = pmut_->PolicyRequest(
       &Policy::UpdateCheckAllowed, &result);
   EXPECT_EQ(EvalStatus::kSucceeded, status);
-  EXPECT_TRUE(result);
+  EXPECT_TRUE(result.updates_enabled);
 }
 
 TEST_F(PmPolicyManagerTest, PolicyRequestDoesntBlock) {
+  UpdateCheckParams result;
   pmut_->set_policy(new LazyPolicy());
-  bool result;
 
   EvalStatus status = pmut_->PolicyRequest(
       &Policy::UpdateCheckAllowed, &result);
@@ -140,9 +119,9 @@
   // the main loop in both cases even when we could evaluate it right now.
   pmut_->set_policy(new FailingPolicy());
 
-  vector<pair<EvalStatus, bool>> calls;
-  Callback<void(EvalStatus, const bool& result)> callback =
-      Bind(AccumulateCallsCallback<bool>, &calls);
+  vector<pair<EvalStatus, UpdateCheckParams>> calls;
+  Callback<void(EvalStatus, const UpdateCheckParams& result)> callback =
+      Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
 
   pmut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
   // The callback should wait until we run the main loop for it to be executed.