UpdateManager: Fix update scheduling interval/fuzz inference.

This addresses two differences wrt current logic:

* In the case of recurring periodic check, we should use
  kTimeoutPeriodicInterval as the base check interval (and not
  kTimeoutInitialInterval).

* When doing exponential backoff, we should be using the interval / 2 as
  the fuzz factor (and not the fixed kTimeoutRegularFuzz).

Added two new tests (RecurringCheckBaseIntervalAndFuzz and
RecurringCheckBackoffIntervalAndFuzz) to ensure the correct values are
used.

Also fixed existing unit tests to properly distinguish a first update
check from recurring ones (FirstCheckIsAtMostInitialIntervalAfterStart)
and to properly verify that deferred update check times are within the
expected buzz boundaries (FirstCheckIsAtMostInitialIntervalAfterStart
and ExponentialBackoffIsCapped).

BUG=chromium:392582
TEST=Unit tests.

Change-Id: I3085502c57616cba2eff4cb0372ca5699427ae20
Reviewed-on: https://chromium-review.googlesource.com/207192
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/update_manager/chromeos_policy.cc b/update_manager/chromeos_policy.cc
index 8a2e2c2..86f53ce 100644
--- a/update_manager/chromeos_policy.cc
+++ b/update_manager/chromeos_policy.cc
@@ -406,28 +406,35 @@
 
   PRNG prng(*seed);
 
+  // If this is the first attempt, compute and return an initial value.
   if (!last_checked_time || *last_checked_time < *updater_started_time) {
-    // First attempt.
     *next_update_check = *updater_started_time + FuzzedInterval(
         &prng, kTimeoutInitialInterval, kTimeoutRegularFuzz);
     return EvalStatus::kSucceeded;
   }
-  // Check for previous failed attempts to implement the exponential backoff.
+
+  // Check for previous failed attempts to implement an exponential backoff.
   const unsigned int* consecutive_failed_update_checks = ec->GetValue(
       state->updater_provider()->var_consecutive_failed_update_checks());
   POLICY_CHECK_VALUE_AND_FAIL(consecutive_failed_update_checks, error);
 
-  int interval = kTimeoutInitialInterval;
+  int interval = kTimeoutPeriodicInterval;
+  int fuzz = kTimeoutRegularFuzz;
   for (unsigned int i = 0; i < *consecutive_failed_update_checks; ++i) {
     interval *= 2;
+    fuzz = 0;  // In case of backoff, fuzz is different (see below).
     if (interval > kTimeoutMaxBackoffInterval) {
       interval = kTimeoutMaxBackoffInterval;
       break;
     }
   }
 
+  // Defer to a fuzz of +/-(interval / 2) in case of backoff.
+  if (fuzz == 0)
+    fuzz = interval;
+
   *next_update_check = *last_checked_time + FuzzedInterval(
-      &prng, interval, kTimeoutRegularFuzz);
+      &prng, interval, fuzz);
   return EvalStatus::kSucceeded;
 }