• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "update_engine/update_manager/chromeos_policy.h"
18 
19 #include <memory>
20 #include <set>
21 
22 #include "update_engine/update_manager/next_update_check_policy_impl.h"
23 #include "update_engine/update_manager/policy_test_utils.h"
24 #include "update_engine/update_manager/weekly_time.h"
25 
26 using base::Time;
27 using base::TimeDelta;
28 using chromeos_update_engine::ConnectionTethering;
29 using chromeos_update_engine::ConnectionType;
30 using chromeos_update_engine::ErrorCode;
31 using chromeos_update_engine::InstallPlan;
32 using std::set;
33 using std::string;
34 
35 namespace chromeos_update_manager {
36 
37 class UmChromeOSPolicyTest : public UmPolicyTestBase {
38  protected:
UmChromeOSPolicyTest()39   UmChromeOSPolicyTest() : UmPolicyTestBase() {
40     policy_ = std::make_unique<ChromeOSPolicy>();
41   }
42 
SetUp()43   void SetUp() override {
44     UmPolicyTestBase::SetUp();
45     SetUpDefaultDevicePolicy();
46   }
47 
SetUpDefaultState()48   void SetUpDefaultState() override {
49     UmPolicyTestBase::SetUpDefaultState();
50 
51     // OOBE is enabled by default.
52     fake_state_.config_provider()->var_is_oobe_enabled()->reset(new bool(true));
53 
54     // For the purpose of the tests, this is an official build and OOBE was
55     // completed.
56     fake_state_.system_provider()->var_is_official_build()->reset(
57         new bool(true));
58     fake_state_.system_provider()->var_is_oobe_complete()->reset(
59         new bool(true));
60     // NOLINTNEXTLINE(readability/casting)
61     fake_state_.system_provider()->var_num_slots()->reset(new unsigned int(2));
62 
63     // Connection is wifi, untethered.
64     fake_state_.shill_provider()->var_conn_type()->reset(
65         new ConnectionType(ConnectionType::kWifi));
66     fake_state_.shill_provider()->var_conn_tethering()->reset(
67         new ConnectionTethering(ConnectionTethering::kNotDetected));
68   }
69 
70   // Sets up a default device policy that does not impose any restrictions
71   // (HTTP) nor enables any features (P2P).
SetUpDefaultDevicePolicy()72   void SetUpDefaultDevicePolicy() {
73     fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
74         new bool(true));
75     fake_state_.device_policy_provider()->var_update_disabled()->reset(
76         new bool(false));
77     fake_state_.device_policy_provider()
78         ->var_allowed_connection_types_for_update()
79         ->reset(nullptr);
80     fake_state_.device_policy_provider()->var_scatter_factor()->reset(
81         new TimeDelta());
82     fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
83         new bool(true));
84     fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
85         new bool(false));
86     fake_state_.device_policy_provider()
87         ->var_release_channel_delegated()
88         ->reset(new bool(true));
89     fake_state_.device_policy_provider()
90         ->var_disallowed_time_intervals()
91         ->reset(new WeeklyTimeIntervalVector());
92   }
93 
94   // Configures the policy to return a desired value from UpdateCheckAllowed by
95   // faking the current wall clock time as needed. Restores the default state.
96   // This is used when testing policies that depend on this one.
97   //
98   // Note that the default implementation relies on NextUpdateCheckPolicyImpl to
99   // set the FakeClock to the appropriate time.
SetUpdateCheckAllowed(bool allow_check)100   virtual void SetUpdateCheckAllowed(bool allow_check) {
101     Time next_update_check;
102     CallMethodWithContext(&NextUpdateCheckTimePolicyImpl::NextUpdateCheckTime,
103                           &next_update_check,
104                           ChromeOSPolicy::kNextUpdateCheckPolicyConstants);
105     SetUpDefaultState();
106     SetUpDefaultDevicePolicy();
107     Time curr_time = next_update_check;
108     if (allow_check)
109       curr_time += TimeDelta::FromSeconds(1);
110     else
111       curr_time -= TimeDelta::FromSeconds(1);
112     fake_clock_->SetWallclockTime(curr_time);
113   }
114 
115   // Sets the policies required for a kiosk app to control Chrome OS version:
116   // - AllowKioskAppControlChromeVersion = True
117   // - UpdateDisabled = True
118   // In the kiosk app manifest:
119   // - RequiredPlatformVersion = 1234.
SetKioskAppControlsChromeOsVersion()120   void SetKioskAppControlsChromeOsVersion() {
121     fake_state_.device_policy_provider()
122         ->var_allow_kiosk_app_control_chrome_version()
123         ->reset(new bool(true));
124     fake_state_.device_policy_provider()->var_update_disabled()->reset(
125         new bool(true));
126     fake_state_.system_provider()->var_kiosk_required_platform_version()->reset(
127         new string("1234."));
128   }
129 
130   // Sets up a test with the value of RollbackToTargetVersion policy (and
131   // whether it's set), and returns the value of
132   // UpdateCheckParams.rollback_allowed.
TestRollbackAllowed(bool set_policy,RollbackToTargetVersion rollback_to_target_version)133   bool TestRollbackAllowed(bool set_policy,
134                            RollbackToTargetVersion rollback_to_target_version) {
135     // Update check is allowed, response includes attributes for use in the
136     // request.
137     SetUpdateCheckAllowed(true);
138 
139     if (set_policy) {
140       // Override RollbackToTargetVersion device policy attribute.
141       fake_state_.device_policy_provider()
142           ->var_rollback_to_target_version()
143           ->reset(new RollbackToTargetVersion(rollback_to_target_version));
144     }
145 
146     UpdateCheckParams result;
147     ExpectPolicyStatus(
148         EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
149     return result.rollback_allowed;
150   }
151 
152   // Sets up a test with the given intervals and the current fake wallclock
153   // time.
TestDisallowedTimeIntervals(const WeeklyTimeIntervalVector & intervals,const ErrorCode & expected_error_code,bool kiosk)154   void TestDisallowedTimeIntervals(const WeeklyTimeIntervalVector& intervals,
155                                    const ErrorCode& expected_error_code,
156                                    bool kiosk) {
157     SetUpDefaultTimeProvider();
158     if (kiosk)
159       fake_state_.device_policy_provider()
160           ->var_auto_launched_kiosk_app_id()
161           ->reset(new string("myapp"));
162     fake_state_.device_policy_provider()
163         ->var_disallowed_time_intervals()
164         ->reset(new WeeklyTimeIntervalVector(intervals));
165 
166     // Check that |expected_status| matches the value of UpdateCheckAllowed
167     ErrorCode result;
168     InstallPlan install_plan;
169     ExpectPolicyStatus(EvalStatus::kSucceeded,
170                        &Policy::UpdateCanBeApplied,
171                        &result,
172                        &install_plan);
173     EXPECT_EQ(result, expected_error_code);
174   }
175 };
176 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedWaitsForTheTimeout)177 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
178   // We get the next update_check timestamp from the policy's private method
179   // and then we check the public method respects that value on the normal
180   // case.
181   Time next_update_check;
182   Time last_checked_time =
183       fake_clock_->GetWallclockTime() + TimeDelta::FromMinutes(1234);
184 
185   fake_state_.updater_provider()->var_last_checked_time()->reset(
186       new Time(last_checked_time));
187   CallMethodWithContext(&NextUpdateCheckTimePolicyImpl::NextUpdateCheckTime,
188                         &next_update_check,
189                         ChromeOSPolicy::kNextUpdateCheckPolicyConstants);
190 
191   UpdateCheckParams result;
192 
193   // Check that the policy blocks until the next_update_check is reached.
194   SetUpDefaultClock();
195   SetUpDefaultState();
196   fake_state_.updater_provider()->var_last_checked_time()->reset(
197       new Time(last_checked_time));
198   fake_clock_->SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
199   ExpectPolicyStatus(
200       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
201 
202   SetUpDefaultClock();
203   SetUpDefaultState();
204   fake_state_.updater_provider()->var_last_checked_time()->reset(
205       new Time(last_checked_time));
206   fake_clock_->SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
207   ExpectPolicyStatus(
208       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
209   EXPECT_TRUE(result.updates_enabled);
210   EXPECT_FALSE(result.interactive);
211 }
212 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedWaitsForOOBE)213 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE) {
214   // Update checks are deferred until OOBE is completed.
215 
216   // Ensure that update is not allowed even if wait period is satisfied.
217   Time next_update_check;
218   Time last_checked_time =
219       fake_clock_->GetWallclockTime() + TimeDelta::FromMinutes(1234);
220 
221   fake_state_.updater_provider()->var_last_checked_time()->reset(
222       new Time(last_checked_time));
223   CallMethodWithContext(&NextUpdateCheckTimePolicyImpl::NextUpdateCheckTime,
224                         &next_update_check,
225                         ChromeOSPolicy::kNextUpdateCheckPolicyConstants);
226 
227   SetUpDefaultClock();
228   SetUpDefaultState();
229   fake_state_.updater_provider()->var_last_checked_time()->reset(
230       new Time(last_checked_time));
231   fake_clock_->SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
232   fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
233 
234   UpdateCheckParams result;
235   ExpectPolicyStatus(
236       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
237 
238   // Now check that it is allowed if OOBE is completed.
239   SetUpDefaultClock();
240   SetUpDefaultState();
241   fake_state_.updater_provider()->var_last_checked_time()->reset(
242       new Time(last_checked_time));
243   fake_clock_->SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
244   ExpectPolicyStatus(
245       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
246   EXPECT_TRUE(result.updates_enabled);
247   EXPECT_FALSE(result.interactive);
248 }
249 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedWithAttributes)250 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
251   // Update check is allowed, response includes attributes for use in the
252   // request.
253   SetUpdateCheckAllowed(true);
254 
255   // Override specific device policy attributes.
256   fake_state_.device_policy_provider()->var_target_version_prefix()->reset(
257       new string("1.2"));
258   fake_state_.device_policy_provider()
259       ->var_rollback_allowed_milestones()
260       ->reset(new int(5));
261   fake_state_.device_policy_provider()->var_release_channel_delegated()->reset(
262       new bool(false));
263   fake_state_.device_policy_provider()->var_release_channel()->reset(
264       new string("foo-channel"));
265   fake_state_.device_policy_provider()->var_release_lts_tag()->reset(
266       new string("foo-hint"));
267   fake_state_.device_policy_provider()->var_quick_fix_build_token()->reset(
268       new string("foo-token"));
269 
270   UpdateCheckParams result;
271   ExpectPolicyStatus(
272       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
273   EXPECT_TRUE(result.updates_enabled);
274   EXPECT_EQ("1.2", result.target_version_prefix);
275   EXPECT_EQ(5, result.rollback_allowed_milestones);
276   EXPECT_EQ("foo-channel", result.target_channel);
277   EXPECT_EQ("foo-hint", result.lts_tag);
278   EXPECT_EQ("foo-token", result.quick_fix_build_token);
279   EXPECT_FALSE(result.interactive);
280 }
281 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackAndPowerwash)282 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackAndPowerwash) {
283   EXPECT_TRUE(TestRollbackAllowed(
284       true, RollbackToTargetVersion::kRollbackAndPowerwash));
285 }
286 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackAndRestoreIfPossible)287 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackAndRestoreIfPossible) {
288   // We're doing rollback even if we don't support data save and restore.
289   EXPECT_TRUE(TestRollbackAllowed(
290       true, RollbackToTargetVersion::kRollbackAndRestoreIfPossible));
291 }
292 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackDisabled)293 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackDisabled) {
294   EXPECT_FALSE(TestRollbackAllowed(true, RollbackToTargetVersion::kDisabled));
295 }
296 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackUnspecified)297 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackUnspecified) {
298   EXPECT_FALSE(
299       TestRollbackAllowed(true, RollbackToTargetVersion::kUnspecified));
300 }
301 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackNotSet)302 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackNotSet) {
303   EXPECT_FALSE(
304       TestRollbackAllowed(false, RollbackToTargetVersion::kUnspecified));
305 }
306 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskRollbackAllowed)307 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskRollbackAllowed) {
308   SetKioskAppControlsChromeOsVersion();
309 
310   EXPECT_TRUE(TestRollbackAllowed(
311       true, RollbackToTargetVersion::kRollbackAndPowerwash));
312 }
313 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskRollbackDisabled)314 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskRollbackDisabled) {
315   SetKioskAppControlsChromeOsVersion();
316 
317   EXPECT_FALSE(TestRollbackAllowed(true, RollbackToTargetVersion::kDisabled));
318 }
319 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskRollbackUnspecified)320 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskRollbackUnspecified) {
321   SetKioskAppControlsChromeOsVersion();
322 
323   EXPECT_FALSE(
324       TestRollbackAllowed(true, RollbackToTargetVersion::kUnspecified));
325 }
326 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskRollbackNotSet)327 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskRollbackNotSet) {
328   SetKioskAppControlsChromeOsVersion();
329 
330   EXPECT_FALSE(
331       TestRollbackAllowed(false, RollbackToTargetVersion::kUnspecified));
332 }
333 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds)334 TEST_F(UmChromeOSPolicyTest,
335        UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
336   // UpdateCheckAllowed should return kAskMeAgainLater if this is an unofficial
337   // build; we don't want periodic update checks on developer images.
338 
339   fake_state_.system_provider()->var_is_official_build()->reset(
340       new bool(false));
341 
342   UpdateCheckParams result;
343   ExpectPolicyStatus(
344       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
345 }
346 
TEST_F(UmChromeOSPolicyTest,TestUpdateCheckIntervalTimeout)347 TEST_F(UmChromeOSPolicyTest, TestUpdateCheckIntervalTimeout) {
348   fake_state_.updater_provider()
349       ->var_test_update_check_interval_timeout()
350       ->reset(new int64_t(10));
351   fake_state_.system_provider()->var_is_official_build()->reset(
352       new bool(false));
353 
354   // The first time, update should not be allowed.
355   UpdateCheckParams result;
356   ExpectPolicyStatus(
357       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
358 
359   // After moving the time forward more than the update check interval, it
360   // should now allow for update.
361   fake_clock_->SetWallclockTime(fake_clock_->GetWallclockTime() +
362                                 TimeDelta::FromSeconds(11));
363   ExpectPolicyStatus(
364       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
365 }
366 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedUpdatesDisabledWhenNotEnoughSlotsAbUpdates)367 TEST_F(UmChromeOSPolicyTest,
368        UpdateCheckAllowedUpdatesDisabledWhenNotEnoughSlotsAbUpdates) {
369   // UpdateCheckAllowed should return false (kSucceeded) if the image booted
370   // without enough slots to do A/B updates.
371 
372   // NOLINTNEXTLINE(readability/casting)
373   fake_state_.system_provider()->var_num_slots()->reset(new unsigned int(1));
374 
375   UpdateCheckParams result;
376   ExpectPolicyStatus(
377       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
378   EXPECT_FALSE(result.updates_enabled);
379 }
380 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedUpdatesDisabledByPolicy)381 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
382   // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
383   // is loaded and prohibits updates.
384 
385   SetUpdateCheckAllowed(false);
386   fake_state_.device_policy_provider()->var_update_disabled()->reset(
387       new bool(true));
388 
389   UpdateCheckParams result;
390   ExpectPolicyStatus(
391       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
392 }
393 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedForcedUpdateRequestedInteractive)394 TEST_F(UmChromeOSPolicyTest,
395        UpdateCheckAllowedForcedUpdateRequestedInteractive) {
396   // UpdateCheckAllowed should return true because a forced update request was
397   // signaled for an interactive update.
398 
399   SetUpdateCheckAllowed(true);
400   fake_state_.updater_provider()->var_forced_update_requested()->reset(
401       new UpdateRequestStatus(UpdateRequestStatus::kInteractive));
402 
403   UpdateCheckParams result;
404   ExpectPolicyStatus(
405       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
406   EXPECT_TRUE(result.updates_enabled);
407   EXPECT_TRUE(result.interactive);
408 }
409 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedForcedUpdateRequestedPeriodic)410 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedForcedUpdateRequestedPeriodic) {
411   // UpdateCheckAllowed should return true because a forced update request was
412   // signaled for a periodic check.
413 
414   SetUpdateCheckAllowed(true);
415   fake_state_.updater_provider()->var_forced_update_requested()->reset(
416       new UpdateRequestStatus(UpdateRequestStatus::kPeriodic));
417 
418   UpdateCheckParams result;
419   ExpectPolicyStatus(
420       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
421   EXPECT_TRUE(result.updates_enabled);
422   EXPECT_FALSE(result.interactive);
423 }
424 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskPin)425 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskPin) {
426   // Update check is allowed.
427   SetUpdateCheckAllowed(true);
428 
429   SetKioskAppControlsChromeOsVersion();
430 
431   UpdateCheckParams result;
432   ExpectPolicyStatus(
433       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
434   EXPECT_TRUE(result.updates_enabled);
435   EXPECT_EQ("1234.", result.target_version_prefix);
436   EXPECT_FALSE(result.interactive);
437 }
438 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedDisabledWhenNoKioskPin)439 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedDisabledWhenNoKioskPin) {
440   // Update check is allowed.
441   SetUpdateCheckAllowed(true);
442 
443   // Disable AU policy is set but kiosk pin policy is set to false. Update is
444   // disabled in such case.
445   fake_state_.device_policy_provider()->var_update_disabled()->reset(
446       new bool(true));
447   fake_state_.device_policy_provider()
448       ->var_allow_kiosk_app_control_chrome_version()
449       ->reset(new bool(false));
450 
451   UpdateCheckParams result;
452   ExpectPolicyStatus(
453       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
454 }
455 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskPinWithNoRequiredVersion)456 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskPinWithNoRequiredVersion) {
457   // Update check is allowed.
458   SetUpdateCheckAllowed(true);
459 
460   // AU disabled, allow kiosk to pin but there is no kiosk required platform
461   // version (i.e. app does not provide the info). Update to latest in such
462   // case.
463   fake_state_.device_policy_provider()->var_update_disabled()->reset(
464       new bool(true));
465   fake_state_.device_policy_provider()
466       ->var_allow_kiosk_app_control_chrome_version()
467       ->reset(new bool(true));
468   fake_state_.system_provider()->var_kiosk_required_platform_version()->reset(
469       new string());
470 
471   UpdateCheckParams result;
472   ExpectPolicyStatus(
473       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
474   EXPECT_TRUE(result.updates_enabled);
475   EXPECT_TRUE(result.target_version_prefix.empty());
476   EXPECT_FALSE(result.interactive);
477 }
478 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskPinWithFailedGetRequiredVersionCall)479 TEST_F(UmChromeOSPolicyTest,
480        UpdateCheckAllowedKioskPinWithFailedGetRequiredVersionCall) {
481   // AU disabled, allow kiosk to pin but D-Bus call to get required platform
482   // version failed. Defer update check in this case.
483   fake_state_.device_policy_provider()->var_update_disabled()->reset(
484       new bool(true));
485   fake_state_.device_policy_provider()
486       ->var_allow_kiosk_app_control_chrome_version()
487       ->reset(new bool(true));
488   fake_state_.system_provider()->var_kiosk_required_platform_version()->reset(
489       nullptr);
490 
491   UpdateCheckParams result;
492   ExpectPolicyStatus(
493       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
494 }
495 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartFailsCheckAllowedError)496 TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
497   // The UpdateCanStart policy fails, not being able to query
498   // UpdateCheckAllowed.
499 
500   // Configure the UpdateCheckAllowed policy to fail.
501   fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
502 
503   // Check that the UpdateCanStart fails.
504   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
505   UpdateDownloadParams result;
506   ExpectPolicyStatus(
507       EvalStatus::kFailed, &Policy::UpdateCanStart, &result, update_state);
508 }
509 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedCheckDue)510 TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
511   // The UpdateCanStart policy returns false because we are due for another
512   // update check. Ensure that download related values are still returned.
513 
514   SetUpdateCheckAllowed(true);
515 
516   // Check that the UpdateCanStart returns false.
517   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
518   UpdateDownloadParams result;
519   ExpectPolicyStatus(
520       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
521   EXPECT_FALSE(result.update_can_start);
522   EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
523   EXPECT_EQ(0, result.download_url_idx);
524   EXPECT_EQ(0, result.download_url_num_errors);
525 }
526 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoDevicePolicy)527 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
528   // The UpdateCanStart policy returns true; no device policy is loaded.
529 
530   SetUpdateCheckAllowed(false);
531   fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
532       new bool(false));
533 
534   // Check that the UpdateCanStart returns true with no further attributes.
535   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
536   UpdateDownloadParams result;
537   ExpectPolicyStatus(
538       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
539   EXPECT_TRUE(result.update_can_start);
540   EXPECT_FALSE(result.p2p_downloading_allowed);
541   EXPECT_FALSE(result.p2p_sharing_allowed);
542   EXPECT_EQ(0, result.download_url_idx);
543   EXPECT_TRUE(result.download_url_allowed);
544   EXPECT_EQ(0, result.download_url_num_errors);
545   EXPECT_FALSE(result.do_increment_failures);
546 }
547 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedBlankPolicy)548 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
549   // The UpdateCanStart policy returns true; device policy is loaded but imposes
550   // no restrictions on updating.
551 
552   SetUpdateCheckAllowed(false);
553 
554   // Check that the UpdateCanStart returns true.
555   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
556   UpdateDownloadParams result;
557   ExpectPolicyStatus(
558       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
559   EXPECT_TRUE(result.update_can_start);
560   EXPECT_FALSE(result.p2p_downloading_allowed);
561   EXPECT_FALSE(result.p2p_sharing_allowed);
562   EXPECT_EQ(0, result.download_url_idx);
563   EXPECT_TRUE(result.download_url_allowed);
564   EXPECT_EQ(0, result.download_url_num_errors);
565   EXPECT_FALSE(result.do_increment_failures);
566 }
567 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedBackoffNewWaitPeriodApplies)568 TEST_F(UmChromeOSPolicyTest,
569        UpdateCanStartNotAllowedBackoffNewWaitPeriodApplies) {
570   // The UpdateCanStart policy returns false; failures are reported and a new
571   // backoff period is enacted.
572 
573   SetUpdateCheckAllowed(false);
574 
575   const Time curr_time = fake_clock_->GetWallclockTime();
576   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
577   update_state.download_errors_max = 1;
578   update_state.download_errors.emplace_back(
579       0,
580       ErrorCode::kDownloadTransferError,
581       curr_time - TimeDelta::FromSeconds(8));
582   update_state.download_errors.emplace_back(
583       0,
584       ErrorCode::kDownloadTransferError,
585       curr_time - TimeDelta::FromSeconds(2));
586 
587   // Check that UpdateCanStart returns false and a new backoff expiry is
588   // generated.
589   UpdateDownloadParams result;
590   ExpectPolicyStatus(
591       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
592   EXPECT_FALSE(result.update_can_start);
593   EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
594   EXPECT_TRUE(result.do_increment_failures);
595   EXPECT_LT(curr_time, result.backoff_expiry);
596 }
597 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedBackoffPrevWaitPeriodStillApplies)598 TEST_F(UmChromeOSPolicyTest,
599        UpdateCanStartNotAllowedBackoffPrevWaitPeriodStillApplies) {
600   // The UpdateCanStart policy returns false; a previously enacted backoff
601   // period still applies.
602 
603   SetUpdateCheckAllowed(false);
604 
605   const Time curr_time = fake_clock_->GetWallclockTime();
606   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
607   update_state.download_errors_max = 1;
608   update_state.download_errors.emplace_back(
609       0,
610       ErrorCode::kDownloadTransferError,
611       curr_time - TimeDelta::FromSeconds(8));
612   update_state.download_errors.emplace_back(
613       0,
614       ErrorCode::kDownloadTransferError,
615       curr_time - TimeDelta::FromSeconds(2));
616   update_state.failures_last_updated = curr_time;
617   update_state.backoff_expiry = curr_time + TimeDelta::FromMinutes(3);
618 
619   // Check that UpdateCanStart returns false and a new backoff expiry is
620   // generated.
621   UpdateDownloadParams result;
622   ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
623                      &Policy::UpdateCanStart,
624                      &result,
625                      update_state);
626   EXPECT_FALSE(result.update_can_start);
627   EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
628   EXPECT_FALSE(result.do_increment_failures);
629   EXPECT_LT(curr_time, result.backoff_expiry);
630 }
631 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedBackoffSatisfied)632 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffSatisfied) {
633   // The UpdateCanStart policy returns true; a previously enacted backoff period
634   // has elapsed, we're good to go.
635 
636   SetUpdateCheckAllowed(false);
637 
638   const Time curr_time = fake_clock_->GetWallclockTime();
639   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
640   update_state.download_errors_max = 1;
641   update_state.download_errors.emplace_back(
642       0,
643       ErrorCode::kDownloadTransferError,
644       curr_time - TimeDelta::FromSeconds(8));
645   update_state.download_errors.emplace_back(
646       0,
647       ErrorCode::kDownloadTransferError,
648       curr_time - TimeDelta::FromSeconds(2));
649   update_state.failures_last_updated = curr_time - TimeDelta::FromSeconds(1);
650   update_state.backoff_expiry = curr_time - TimeDelta::FromSeconds(1);
651 
652   // Check that UpdateCanStart returns false and a new backoff expiry is
653   // generated.
654   UpdateDownloadParams result;
655   ExpectPolicyStatus(
656       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
657   EXPECT_TRUE(result.update_can_start);
658   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
659   EXPECT_EQ(0, result.download_url_idx);
660   EXPECT_TRUE(result.download_url_allowed);
661   EXPECT_EQ(0, result.download_url_num_errors);
662   EXPECT_FALSE(result.do_increment_failures);
663   EXPECT_EQ(Time(), result.backoff_expiry);
664 }
665 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedBackoffDisabled)666 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffDisabled) {
667   // The UpdateCanStart policy returns false; failures are reported but backoff
668   // is disabled.
669 
670   SetUpdateCheckAllowed(false);
671 
672   const Time curr_time = fake_clock_->GetWallclockTime();
673   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
674   update_state.download_errors_max = 1;
675   update_state.download_errors.emplace_back(
676       0,
677       ErrorCode::kDownloadTransferError,
678       curr_time - TimeDelta::FromSeconds(8));
679   update_state.download_errors.emplace_back(
680       0,
681       ErrorCode::kDownloadTransferError,
682       curr_time - TimeDelta::FromSeconds(2));
683   update_state.is_backoff_disabled = true;
684 
685   // Check that UpdateCanStart returns false and a new backoff expiry is
686   // generated.
687   UpdateDownloadParams result;
688   ExpectPolicyStatus(
689       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
690   EXPECT_TRUE(result.update_can_start);
691   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
692   EXPECT_EQ(0, result.download_url_idx);
693   EXPECT_TRUE(result.download_url_allowed);
694   EXPECT_EQ(0, result.download_url_num_errors);
695   EXPECT_TRUE(result.do_increment_failures);
696   EXPECT_EQ(Time(), result.backoff_expiry);
697 }
698 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoBackoffInteractive)699 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffInteractive) {
700   // The UpdateCanStart policy returns false; failures are reported but this is
701   // an interactive update check.
702 
703   SetUpdateCheckAllowed(false);
704 
705   const Time curr_time = fake_clock_->GetWallclockTime();
706   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
707   update_state.download_errors_max = 1;
708   update_state.download_errors.emplace_back(
709       0,
710       ErrorCode::kDownloadTransferError,
711       curr_time - TimeDelta::FromSeconds(8));
712   update_state.download_errors.emplace_back(
713       0,
714       ErrorCode::kDownloadTransferError,
715       curr_time - TimeDelta::FromSeconds(2));
716   update_state.interactive = true;
717 
718   // Check that UpdateCanStart returns false and a new backoff expiry is
719   // generated.
720   UpdateDownloadParams result;
721   ExpectPolicyStatus(
722       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
723   EXPECT_TRUE(result.update_can_start);
724   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
725   EXPECT_EQ(0, result.download_url_idx);
726   EXPECT_TRUE(result.download_url_allowed);
727   EXPECT_EQ(0, result.download_url_num_errors);
728   EXPECT_TRUE(result.do_increment_failures);
729   EXPECT_EQ(Time(), result.backoff_expiry);
730 }
731 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoBackoffDelta)732 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffDelta) {
733   // The UpdateCanStart policy returns false; failures are reported but this is
734   // a delta payload.
735 
736   SetUpdateCheckAllowed(false);
737 
738   const Time curr_time = fake_clock_->GetWallclockTime();
739   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
740   update_state.download_errors_max = 1;
741   update_state.download_errors.emplace_back(
742       0,
743       ErrorCode::kDownloadTransferError,
744       curr_time - TimeDelta::FromSeconds(8));
745   update_state.download_errors.emplace_back(
746       0,
747       ErrorCode::kDownloadTransferError,
748       curr_time - TimeDelta::FromSeconds(2));
749   update_state.is_delta_payload = true;
750 
751   // Check that UpdateCanStart returns false and a new backoff expiry is
752   // generated.
753   UpdateDownloadParams result;
754   ExpectPolicyStatus(
755       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
756   EXPECT_TRUE(result.update_can_start);
757   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
758   EXPECT_EQ(0, result.download_url_idx);
759   EXPECT_TRUE(result.download_url_allowed);
760   EXPECT_EQ(0, result.download_url_num_errors);
761   EXPECT_TRUE(result.do_increment_failures);
762   EXPECT_EQ(Time(), result.backoff_expiry);
763 }
764 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoBackoffUnofficialBuild)765 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffUnofficialBuild) {
766   // The UpdateCanStart policy returns false; failures are reported but this is
767   // an unofficial build.
768 
769   SetUpdateCheckAllowed(false);
770 
771   const Time curr_time = fake_clock_->GetWallclockTime();
772   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
773   update_state.download_errors_max = 1;
774   update_state.download_errors.emplace_back(
775       0,
776       ErrorCode::kDownloadTransferError,
777       curr_time - TimeDelta::FromSeconds(8));
778   update_state.download_errors.emplace_back(
779       0,
780       ErrorCode::kDownloadTransferError,
781       curr_time - TimeDelta::FromSeconds(2));
782 
783   fake_state_.system_provider()->var_is_official_build()->reset(
784       new bool(false));
785 
786   // Check that UpdateCanStart returns false and a new backoff expiry is
787   // generated.
788   UpdateDownloadParams result;
789   ExpectPolicyStatus(
790       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
791   EXPECT_TRUE(result.update_can_start);
792   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
793   EXPECT_EQ(0, result.download_url_idx);
794   EXPECT_TRUE(result.download_url_allowed);
795   EXPECT_EQ(0, result.download_url_num_errors);
796   EXPECT_TRUE(result.do_increment_failures);
797   EXPECT_EQ(Time(), result.backoff_expiry);
798 }
799 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartFailsScatteringFailed)800 TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
801   // The UpdateCanStart policy fails because the UpdateScattering policy it
802   // depends on fails (unset variable).
803 
804   SetUpdateCheckAllowed(false);
805 
806   // Override the default seed variable with a null value so that the policy
807   // request would fail.
808   // TODO(garnold) This failure may or may not fail a number
809   // sub-policies/decisions, like scattering and backoff. We'll need a more
810   // deliberate setup to ensure that we're failing what we want to be failing.
811   fake_state_.random_provider()->var_seed()->reset(nullptr);
812 
813   // Check that the UpdateCanStart fails.
814   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
815   UpdateDownloadParams result;
816   ExpectPolicyStatus(
817       EvalStatus::kFailed, &Policy::UpdateCanStart, &result, update_state);
818 }
819 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies)820 TEST_F(UmChromeOSPolicyTest,
821        UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
822   // The UpdateCanStart policy returns false; device policy is loaded and
823   // scattering applies due to an unsatisfied wait period, which was newly
824   // generated.
825 
826   SetUpdateCheckAllowed(false);
827   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
828       new TimeDelta(TimeDelta::FromMinutes(2)));
829 
830   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
831 
832   // Check that the UpdateCanStart returns false and a new wait period
833   // generated.
834   UpdateDownloadParams result;
835   ExpectPolicyStatus(
836       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
837   EXPECT_FALSE(result.update_can_start);
838   EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
839   EXPECT_LT(TimeDelta(), result.scatter_wait_period);
840   EXPECT_EQ(0, result.scatter_check_threshold);
841 }
842 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies)843 TEST_F(UmChromeOSPolicyTest,
844        UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
845   // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
846   // is loaded and a previously generated scattering period still applies, none
847   // of the scattering values has changed.
848 
849   SetUpdateCheckAllowed(false);
850   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
851       new TimeDelta(TimeDelta::FromMinutes(2)));
852 
853   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
854   update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
855 
856   // Check that the UpdateCanStart returns false and a new wait period
857   // generated.
858   UpdateDownloadParams result;
859   ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
860                      &Policy::UpdateCanStart,
861                      &result,
862                      update_state);
863   EXPECT_FALSE(result.update_can_start);
864   EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
865   EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
866   EXPECT_EQ(0, result.scatter_check_threshold);
867 }
868 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedScatteringNewCountThresholdApplies)869 TEST_F(UmChromeOSPolicyTest,
870        UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
871   // The UpdateCanStart policy returns false; device policy is loaded and
872   // scattering applies due to an unsatisfied update check count threshold.
873   //
874   // This ensures a non-zero check threshold, which may or may not be combined
875   // with a non-zero wait period (for which we cannot reliably control).
876 
877   SetUpdateCheckAllowed(false);
878   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
879       new TimeDelta(TimeDelta::FromSeconds(1)));
880 
881   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
882   update_state.scatter_check_threshold_min = 2;
883   update_state.scatter_check_threshold_max = 5;
884 
885   // Check that the UpdateCanStart returns false.
886   UpdateDownloadParams result;
887   ExpectPolicyStatus(
888       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
889   EXPECT_FALSE(result.update_can_start);
890   EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
891   EXPECT_LE(2, result.scatter_check_threshold);
892   EXPECT_GE(5, result.scatter_check_threshold);
893 }
894 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies)895 TEST_F(UmChromeOSPolicyTest,
896        UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
897   // The UpdateCanStart policy returns false; device policy is loaded and
898   // scattering due to a previously generated count threshold still applies.
899 
900   SetUpdateCheckAllowed(false);
901   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
902       new TimeDelta(TimeDelta::FromSeconds(1)));
903 
904   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
905   update_state.scatter_check_threshold = 3;
906   update_state.scatter_check_threshold_min = 2;
907   update_state.scatter_check_threshold_max = 5;
908 
909   // Check that the UpdateCanStart returns false.
910   UpdateDownloadParams result;
911   ExpectPolicyStatus(
912       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
913   EXPECT_FALSE(result.update_can_start);
914   EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
915   EXPECT_EQ(3, result.scatter_check_threshold);
916 }
917 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedScatteringSatisfied)918 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
919   // The UpdateCanStart policy returns true; device policy is loaded and
920   // scattering is enabled, but both wait period and check threshold are
921   // satisfied.
922 
923   SetUpdateCheckAllowed(false);
924   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
925       new TimeDelta(TimeDelta::FromSeconds(120)));
926 
927   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
928   update_state.num_checks = 4;
929   update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
930   update_state.scatter_check_threshold = 3;
931   update_state.scatter_check_threshold_min = 2;
932   update_state.scatter_check_threshold_max = 5;
933 
934   // Check that the UpdateCanStart returns true.
935   UpdateDownloadParams result;
936   ExpectPolicyStatus(
937       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
938   EXPECT_TRUE(result.update_can_start);
939   EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
940   EXPECT_EQ(0, result.scatter_check_threshold);
941   EXPECT_EQ(0, result.download_url_idx);
942   EXPECT_TRUE(result.download_url_allowed);
943   EXPECT_EQ(0, result.download_url_num_errors);
944   EXPECT_FALSE(result.do_increment_failures);
945 }
946 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedInteractivePreventsScattering)947 TEST_F(UmChromeOSPolicyTest,
948        UpdateCanStartAllowedInteractivePreventsScattering) {
949   // The UpdateCanStart policy returns true; device policy is loaded and
950   // scattering would have applied, except that the update check is interactive
951   // and so it is suppressed.
952 
953   SetUpdateCheckAllowed(false);
954   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
955       new TimeDelta(TimeDelta::FromSeconds(1)));
956 
957   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
958   update_state.interactive = true;
959   update_state.scatter_check_threshold = 0;
960   update_state.scatter_check_threshold_min = 2;
961   update_state.scatter_check_threshold_max = 5;
962 
963   // Check that the UpdateCanStart returns true.
964   UpdateDownloadParams result;
965   ExpectPolicyStatus(
966       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
967   EXPECT_TRUE(result.update_can_start);
968   EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
969   EXPECT_EQ(0, result.scatter_check_threshold);
970   EXPECT_EQ(0, result.download_url_idx);
971   EXPECT_TRUE(result.download_url_allowed);
972   EXPECT_EQ(0, result.download_url_num_errors);
973   EXPECT_FALSE(result.do_increment_failures);
974 }
975 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedOobePreventsScattering)976 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedOobePreventsScattering) {
977   // The UpdateCanStart policy returns true; device policy is loaded and
978   // scattering would have applied, except that OOBE was not completed and so it
979   // is suppressed.
980 
981   SetUpdateCheckAllowed(false);
982   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
983       new TimeDelta(TimeDelta::FromSeconds(1)));
984   fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
985 
986   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
987   update_state.interactive = true;
988   update_state.scatter_check_threshold = 0;
989   update_state.scatter_check_threshold_min = 2;
990   update_state.scatter_check_threshold_max = 5;
991 
992   // Check that the UpdateCanStart returns true.
993   UpdateDownloadParams result;
994   ExpectPolicyStatus(
995       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
996   EXPECT_TRUE(result.update_can_start);
997   EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
998   EXPECT_EQ(0, result.scatter_check_threshold);
999   EXPECT_EQ(0, result.download_url_idx);
1000   EXPECT_TRUE(result.download_url_allowed);
1001   EXPECT_EQ(0, result.download_url_num_errors);
1002   EXPECT_FALSE(result.do_increment_failures);
1003 }
1004 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithAttributes)1005 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
1006   // The UpdateCanStart policy returns true; device policy permits both HTTP and
1007   // P2P updates, as well as a non-empty target channel string.
1008 
1009   SetUpdateCheckAllowed(false);
1010 
1011   // Override specific device policy attributes.
1012   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1013       new bool(true));
1014   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1015       new bool(true));
1016 
1017   // Check that the UpdateCanStart returns true.
1018   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1019   UpdateDownloadParams result;
1020   ExpectPolicyStatus(
1021       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1022   EXPECT_TRUE(result.update_can_start);
1023   EXPECT_TRUE(result.p2p_downloading_allowed);
1024   EXPECT_TRUE(result.p2p_sharing_allowed);
1025   EXPECT_EQ(0, result.download_url_idx);
1026   EXPECT_TRUE(result.download_url_allowed);
1027   EXPECT_EQ(0, result.download_url_num_errors);
1028   EXPECT_FALSE(result.do_increment_failures);
1029 }
1030 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithP2PFromUpdater)1031 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
1032   // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1033   // P2P updates, but the updater is configured to allow P2P and overrules the
1034   // setting.
1035 
1036   SetUpdateCheckAllowed(false);
1037 
1038   // Override specific device policy attributes.
1039   fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
1040 
1041   // Check that the UpdateCanStart returns true.
1042   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1043   UpdateDownloadParams result;
1044   ExpectPolicyStatus(
1045       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1046   EXPECT_TRUE(result.update_can_start);
1047   EXPECT_TRUE(result.p2p_downloading_allowed);
1048   EXPECT_TRUE(result.p2p_sharing_allowed);
1049   EXPECT_EQ(0, result.download_url_idx);
1050   EXPECT_TRUE(result.download_url_allowed);
1051   EXPECT_EQ(0, result.download_url_num_errors);
1052   EXPECT_FALSE(result.do_increment_failures);
1053 }
1054 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedP2PDownloadingBlockedDueToOmaha)1055 TEST_F(UmChromeOSPolicyTest,
1056        UpdateCanStartAllowedP2PDownloadingBlockedDueToOmaha) {
1057   // The UpdateCanStart policy returns true; device policy permits HTTP, but
1058   // policy blocks P2P downloading because Omaha forbids it.  P2P sharing is
1059   // still permitted.
1060 
1061   SetUpdateCheckAllowed(false);
1062 
1063   // Override specific device policy attributes.
1064   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1065       new bool(true));
1066   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1067       new bool(true));
1068 
1069   // Check that the UpdateCanStart returns true.
1070   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1071   update_state.p2p_downloading_disabled = true;
1072   UpdateDownloadParams result;
1073   ExpectPolicyStatus(
1074       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1075   EXPECT_TRUE(result.update_can_start);
1076   EXPECT_FALSE(result.p2p_downloading_allowed);
1077   EXPECT_TRUE(result.p2p_sharing_allowed);
1078 }
1079 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedP2PSharingBlockedDueToOmaha)1080 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedP2PSharingBlockedDueToOmaha) {
1081   // The UpdateCanStart policy returns true; device policy permits HTTP, but
1082   // policy blocks P2P sharing because Omaha forbids it.  P2P downloading is
1083   // still permitted.
1084 
1085   SetUpdateCheckAllowed(false);
1086 
1087   // Override specific device policy attributes.
1088   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1089       new bool(true));
1090   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1091       new bool(true));
1092 
1093   // Check that the UpdateCanStart returns true.
1094   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1095   update_state.p2p_sharing_disabled = true;
1096   UpdateDownloadParams result;
1097   ExpectPolicyStatus(
1098       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1099   EXPECT_TRUE(result.update_can_start);
1100   EXPECT_TRUE(result.p2p_downloading_allowed);
1101   EXPECT_FALSE(result.p2p_sharing_allowed);
1102 }
1103 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts)1104 TEST_F(UmChromeOSPolicyTest,
1105        UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts) {
1106   // The UpdateCanStart policy returns true; device policy permits HTTP but
1107   // blocks P2P download, because the max number of P2P downloads have been
1108   // attempted. P2P sharing is still permitted.
1109 
1110   SetUpdateCheckAllowed(false);
1111 
1112   // Override specific device policy attributes.
1113   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1114       new bool(true));
1115   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1116       new bool(true));
1117 
1118   // Check that the UpdateCanStart returns true.
1119   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1120   update_state.p2p_num_attempts = ChromeOSPolicy::kMaxP2PAttempts;
1121   UpdateDownloadParams result;
1122   ExpectPolicyStatus(
1123       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1124   EXPECT_TRUE(result.update_can_start);
1125   EXPECT_FALSE(result.p2p_downloading_allowed);
1126   EXPECT_TRUE(result.p2p_sharing_allowed);
1127 }
1128 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod)1129 TEST_F(UmChromeOSPolicyTest,
1130        UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod) {
1131   // The UpdateCanStart policy returns true; device policy permits HTTP but
1132   // blocks P2P download, because the max period for attempt to download via P2P
1133   // has elapsed. P2P sharing is still permitted.
1134 
1135   SetUpdateCheckAllowed(false);
1136 
1137   // Override specific device policy attributes.
1138   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1139       new bool(true));
1140   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1141       new bool(true));
1142 
1143   // Check that the UpdateCanStart returns true.
1144   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1145   update_state.p2p_num_attempts = 1;
1146   update_state.p2p_first_attempted =
1147       fake_clock_->GetWallclockTime() -
1148       TimeDelta::FromSeconds(ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds +
1149                              1);
1150   UpdateDownloadParams result;
1151   ExpectPolicyStatus(
1152       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1153   EXPECT_TRUE(result.update_can_start);
1154   EXPECT_FALSE(result.p2p_downloading_allowed);
1155   EXPECT_TRUE(result.p2p_sharing_allowed);
1156 }
1157 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithHttpUrlForUnofficialBuild)1158 TEST_F(UmChromeOSPolicyTest,
1159        UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
1160   // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1161   // P2P updates, but marking this an unofficial build overrules the HTTP
1162   // setting.
1163 
1164   SetUpdateCheckAllowed(false);
1165 
1166   // Override specific device policy attributes.
1167   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1168       new bool(false));
1169   fake_state_.system_provider()->var_is_official_build()->reset(
1170       new bool(false));
1171 
1172   // Check that the UpdateCanStart returns true.
1173   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1174   UpdateDownloadParams result;
1175   ExpectPolicyStatus(
1176       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1177   EXPECT_TRUE(result.update_can_start);
1178   EXPECT_EQ(0, result.download_url_idx);
1179   EXPECT_TRUE(result.download_url_allowed);
1180   EXPECT_EQ(0, result.download_url_num_errors);
1181   EXPECT_FALSE(result.do_increment_failures);
1182 }
1183 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithHttpsUrl)1184 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
1185   // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1186   // P2P updates, but an HTTPS URL is provided and selected for download.
1187 
1188   SetUpdateCheckAllowed(false);
1189 
1190   // Override specific device policy attributes.
1191   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1192       new bool(false));
1193 
1194   // Add an HTTPS URL.
1195   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1196   update_state.download_urls.emplace_back("https://secure/url/");
1197 
1198   // Check that the UpdateCanStart returns true.
1199   UpdateDownloadParams result;
1200   ExpectPolicyStatus(
1201       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1202   EXPECT_TRUE(result.update_can_start);
1203   EXPECT_EQ(1, result.download_url_idx);
1204   EXPECT_TRUE(result.download_url_allowed);
1205   EXPECT_EQ(0, result.download_url_num_errors);
1206   EXPECT_FALSE(result.do_increment_failures);
1207 }
1208 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedMaxErrorsNotExceeded)1209 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedMaxErrorsNotExceeded) {
1210   // The UpdateCanStart policy returns true; the first URL has download errors
1211   // but does not exceed the maximum allowed number of failures, so it is stilli
1212   // usable.
1213 
1214   SetUpdateCheckAllowed(false);
1215 
1216   // Add a second URL; update with this URL attempted and failed enough times to
1217   // disqualify the current (first) URL.
1218   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1219   update_state.num_checks = 5;
1220   update_state.download_urls.emplace_back("http://another/fake/url/");
1221   Time t = fake_clock_->GetWallclockTime() - TimeDelta::FromSeconds(12);
1222   for (int i = 0; i < 5; i++) {
1223     update_state.download_errors.emplace_back(
1224         0, ErrorCode::kDownloadTransferError, t);
1225     t += TimeDelta::FromSeconds(1);
1226   }
1227 
1228   // Check that the UpdateCanStart returns true.
1229   UpdateDownloadParams result;
1230   ExpectPolicyStatus(
1231       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1232   EXPECT_TRUE(result.update_can_start);
1233   EXPECT_EQ(0, result.download_url_idx);
1234   EXPECT_TRUE(result.download_url_allowed);
1235   EXPECT_EQ(5, result.download_url_num_errors);
1236   EXPECT_FALSE(result.do_increment_failures);
1237 }
1238 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithSecondUrlMaxExceeded)1239 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
1240   // The UpdateCanStart policy returns true; the first URL exceeded the maximum
1241   // allowed number of failures, but a second URL is available.
1242 
1243   SetUpdateCheckAllowed(false);
1244 
1245   // Add a second URL; update with this URL attempted and failed enough times to
1246   // disqualify the current (first) URL.
1247   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1248   update_state.num_checks = 10;
1249   update_state.download_urls.emplace_back("http://another/fake/url/");
1250   Time t = fake_clock_->GetWallclockTime() - TimeDelta::FromSeconds(12);
1251   for (int i = 0; i < 11; i++) {
1252     update_state.download_errors.emplace_back(
1253         0, ErrorCode::kDownloadTransferError, t);
1254     t += TimeDelta::FromSeconds(1);
1255   }
1256 
1257   // Check that the UpdateCanStart returns true.
1258   UpdateDownloadParams result;
1259   ExpectPolicyStatus(
1260       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1261   EXPECT_TRUE(result.update_can_start);
1262   EXPECT_EQ(1, result.download_url_idx);
1263   EXPECT_TRUE(result.download_url_allowed);
1264   EXPECT_EQ(0, result.download_url_num_errors);
1265   EXPECT_FALSE(result.do_increment_failures);
1266 }
1267 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithSecondUrlHardError)1268 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
1269   // The UpdateCanStart policy returns true; the first URL fails with a hard
1270   // error, but a second URL is available.
1271 
1272   SetUpdateCheckAllowed(false);
1273 
1274   // Add a second URL; update with this URL attempted and failed in a way that
1275   // causes it to switch directly to the next URL.
1276   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1277   update_state.num_checks = 10;
1278   update_state.download_urls.emplace_back("http://another/fake/url/");
1279   update_state.download_errors.emplace_back(
1280       0,
1281       ErrorCode::kPayloadHashMismatchError,
1282       fake_clock_->GetWallclockTime() - TimeDelta::FromSeconds(1));
1283 
1284   // Check that the UpdateCanStart returns true.
1285   UpdateDownloadParams result;
1286   ExpectPolicyStatus(
1287       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1288   EXPECT_TRUE(result.update_can_start);
1289   EXPECT_EQ(1, result.download_url_idx);
1290   EXPECT_TRUE(result.download_url_allowed);
1291   EXPECT_EQ(0, result.download_url_num_errors);
1292   EXPECT_FALSE(result.do_increment_failures);
1293 }
1294 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedUrlWrapsAround)1295 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
1296   // The UpdateCanStart policy returns true; URL search properly wraps around
1297   // the last one on the list.
1298 
1299   SetUpdateCheckAllowed(false);
1300 
1301   // Add a second URL; update with this URL attempted and failed in a way that
1302   // causes it to switch directly to the next URL. We must disable backoff in
1303   // order for it not to interfere.
1304   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1305   update_state.num_checks = 1;
1306   update_state.is_backoff_disabled = true;
1307   update_state.download_urls.emplace_back("http://another/fake/url/");
1308   update_state.download_errors.emplace_back(
1309       1,
1310       ErrorCode::kPayloadHashMismatchError,
1311       fake_clock_->GetWallclockTime() - TimeDelta::FromSeconds(1));
1312 
1313   // Check that the UpdateCanStart returns true.
1314   UpdateDownloadParams result;
1315   ExpectPolicyStatus(
1316       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1317   EXPECT_TRUE(result.update_can_start);
1318   EXPECT_EQ(0, result.download_url_idx);
1319   EXPECT_TRUE(result.download_url_allowed);
1320   EXPECT_EQ(0, result.download_url_num_errors);
1321   EXPECT_TRUE(result.do_increment_failures);
1322 }
1323 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedNoUsableUrls)1324 TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
1325   // The UpdateCanStart policy returns false; there's a single HTTP URL but its
1326   // use is forbidden by policy.
1327   //
1328   // Note: In the case where no usable URLs are found, the policy should not
1329   // increment the number of failed attempts! Doing so would result in a
1330   // non-idempotent semantics, and does not fall within the intended purpose of
1331   // the backoff mechanism anyway.
1332 
1333   SetUpdateCheckAllowed(false);
1334 
1335   // Override specific device policy attributes.
1336   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1337       new bool(false));
1338 
1339   // Check that the UpdateCanStart returns false.
1340   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1341   UpdateDownloadParams result;
1342   ExpectPolicyStatus(
1343       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1344   EXPECT_FALSE(result.update_can_start);
1345   EXPECT_EQ(UpdateCannotStartReason::kCannotDownload,
1346             result.cannot_start_reason);
1347   EXPECT_FALSE(result.do_increment_failures);
1348 }
1349 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoUsableUrlsButP2PEnabled)1350 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
1351   // The UpdateCanStart policy returns true; there's a single HTTP URL but its
1352   // use is forbidden by policy, however P2P is enabled. The result indicates
1353   // that no URL can be used.
1354   //
1355   // Note: The number of failed attempts should not increase in this case (see
1356   // above test).
1357 
1358   SetUpdateCheckAllowed(false);
1359 
1360   // Override specific device policy attributes.
1361   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1362       new bool(true));
1363   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1364       new bool(false));
1365 
1366   // Check that the UpdateCanStart returns true.
1367   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1368   UpdateDownloadParams result;
1369   ExpectPolicyStatus(
1370       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1371   EXPECT_TRUE(result.update_can_start);
1372   EXPECT_TRUE(result.p2p_downloading_allowed);
1373   EXPECT_TRUE(result.p2p_sharing_allowed);
1374   EXPECT_GT(0, result.download_url_idx);
1375   EXPECT_TRUE(result.download_url_allowed);
1376   EXPECT_EQ(0, result.download_url_num_errors);
1377   EXPECT_FALSE(result.do_increment_failures);
1378 }
1379 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoUsableUrlsButEnterpriseEnrolled)1380 TEST_F(UmChromeOSPolicyTest,
1381        UpdateCanStartAllowedNoUsableUrlsButEnterpriseEnrolled) {
1382   // The UpdateCanStart policy returns true; there's a single HTTP URL but its
1383   // use is forbidden by policy, and P2P is unset on the policy, however the
1384   // device is enterprise-enrolled so P2P is allowed. The result indicates that
1385   // no URL can be used.
1386   //
1387   // Note: The number of failed attempts should not increase in this case (see
1388   // above test).
1389 
1390   SetUpdateCheckAllowed(false);
1391 
1392   // Override specific device policy attributes.
1393   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(nullptr);
1394   fake_state_.device_policy_provider()->var_has_owner()->reset(new bool(false));
1395   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1396       new bool(false));
1397 
1398   // Check that the UpdateCanStart returns true.
1399   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1400   UpdateDownloadParams result;
1401   ExpectPolicyStatus(
1402       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1403   EXPECT_TRUE(result.update_can_start);
1404   EXPECT_TRUE(result.p2p_downloading_allowed);
1405   EXPECT_TRUE(result.p2p_sharing_allowed);
1406   EXPECT_GT(0, result.download_url_idx);
1407   EXPECT_TRUE(result.download_url_allowed);
1408   EXPECT_EQ(0, result.download_url_num_errors);
1409   EXPECT_FALSE(result.do_increment_failures);
1410 }
1411 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedScatteringSupressedDueToP2P)1412 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSupressedDueToP2P) {
1413   // The UpdateCanStart policy returns true; scattering should have applied, but
1414   // P2P download is allowed. Scattering values are nonetheless returned, and so
1415   // are download URL values, albeit the latter are not allowed to be used.
1416 
1417   SetUpdateCheckAllowed(false);
1418   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
1419       new TimeDelta(TimeDelta::FromMinutes(2)));
1420   fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
1421 
1422   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
1423   update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
1424 
1425   UpdateDownloadParams result;
1426   ExpectPolicyStatus(
1427       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1428   EXPECT_TRUE(result.update_can_start);
1429   EXPECT_EQ(0, result.download_url_idx);
1430   EXPECT_FALSE(result.download_url_allowed);
1431   EXPECT_EQ(0, result.download_url_num_errors);
1432   EXPECT_TRUE(result.p2p_downloading_allowed);
1433   EXPECT_TRUE(result.p2p_sharing_allowed);
1434   EXPECT_FALSE(result.do_increment_failures);
1435   EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
1436   EXPECT_EQ(0, result.scatter_check_threshold);
1437 }
1438 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedBackoffSupressedDueToP2P)1439 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffSupressedDueToP2P) {
1440   // The UpdateCanStart policy returns true; backoff should have applied, but
1441   // P2P download is allowed. Backoff values are nonetheless returned, and so
1442   // are download URL values, albeit the latter are not allowed to be used.
1443 
1444   SetUpdateCheckAllowed(false);
1445 
1446   const Time curr_time = fake_clock_->GetWallclockTime();
1447   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
1448   update_state.download_errors_max = 1;
1449   update_state.download_errors.emplace_back(
1450       0,
1451       ErrorCode::kDownloadTransferError,
1452       curr_time - TimeDelta::FromSeconds(8));
1453   update_state.download_errors.emplace_back(
1454       0,
1455       ErrorCode::kDownloadTransferError,
1456       curr_time - TimeDelta::FromSeconds(2));
1457   fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
1458 
1459   UpdateDownloadParams result;
1460   ExpectPolicyStatus(
1461       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1462   EXPECT_TRUE(result.update_can_start);
1463   EXPECT_EQ(0, result.download_url_idx);
1464   EXPECT_FALSE(result.download_url_allowed);
1465   EXPECT_EQ(0, result.download_url_num_errors);
1466   EXPECT_TRUE(result.p2p_downloading_allowed);
1467   EXPECT_TRUE(result.p2p_sharing_allowed);
1468   EXPECT_TRUE(result.do_increment_failures);
1469   EXPECT_LT(curr_time, result.backoff_expiry);
1470 }
1471 
TEST_F(UmChromeOSPolicyTest,P2PEnabledNotAllowed)1472 TEST_F(UmChromeOSPolicyTest, P2PEnabledNotAllowed) {
1473   bool result;
1474   ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::P2PEnabled, &result);
1475   EXPECT_FALSE(result);
1476 }
1477 
TEST_F(UmChromeOSPolicyTest,P2PEnabledAllowedByDevicePolicy)1478 TEST_F(UmChromeOSPolicyTest, P2PEnabledAllowedByDevicePolicy) {
1479   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1480       new bool(true));
1481 
1482   bool result;
1483   ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::P2PEnabled, &result);
1484   EXPECT_TRUE(result);
1485 }
1486 
TEST_F(UmChromeOSPolicyTest,P2PEnabledAllowedByUpdater)1487 TEST_F(UmChromeOSPolicyTest, P2PEnabledAllowedByUpdater) {
1488   fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
1489 
1490   bool result;
1491   ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::P2PEnabled, &result);
1492   EXPECT_TRUE(result);
1493 }
1494 
TEST_F(UmChromeOSPolicyTest,P2PEnabledAllowedDeviceEnterpriseEnrolled)1495 TEST_F(UmChromeOSPolicyTest, P2PEnabledAllowedDeviceEnterpriseEnrolled) {
1496   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(nullptr);
1497   fake_state_.device_policy_provider()->var_has_owner()->reset(new bool(false));
1498 
1499   bool result;
1500   ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::P2PEnabled, &result);
1501   EXPECT_TRUE(result);
1502 }
1503 
TEST_F(UmChromeOSPolicyTest,P2PEnabledChangedBlocks)1504 TEST_F(UmChromeOSPolicyTest, P2PEnabledChangedBlocks) {
1505   bool result;
1506   ExpectPolicyStatus(
1507       EvalStatus::kAskMeAgainLater, &Policy::P2PEnabledChanged, &result, false);
1508 }
1509 
TEST_F(UmChromeOSPolicyTest,UpdateCanBeAppliedForcedUpdatesDisablesTimeRestrictions)1510 TEST_F(UmChromeOSPolicyTest,
1511        UpdateCanBeAppliedForcedUpdatesDisablesTimeRestrictions) {
1512   Time curr_time = fake_clock_->GetWallclockTime();
1513   fake_state_.updater_provider()->var_forced_update_requested()->reset(
1514       new UpdateRequestStatus(UpdateRequestStatus::kInteractive));
1515   // Should return kAskMeAgainLater when updated are not forced.
1516   TestDisallowedTimeIntervals(
1517       {WeeklyTimeInterval(
1518           WeeklyTime::FromTime(curr_time),
1519           WeeklyTime::FromTime(curr_time + TimeDelta::FromMinutes(1)))},
1520       ErrorCode::kSuccess,
1521       /* kiosk = */ true);
1522 }
1523 
TEST_F(UmChromeOSPolicyTest,UpdateCanBeAppliedFailsInDisallowedTime)1524 TEST_F(UmChromeOSPolicyTest, UpdateCanBeAppliedFailsInDisallowedTime) {
1525   Time curr_time = fake_clock_->GetWallclockTime();
1526   TestDisallowedTimeIntervals(
1527       {WeeklyTimeInterval(
1528           WeeklyTime::FromTime(curr_time),
1529           WeeklyTime::FromTime(curr_time + TimeDelta::FromMinutes(1)))},
1530       ErrorCode::kOmahaUpdateDeferredPerPolicy,
1531       /* kiosk = */ true);
1532 }
1533 
TEST_F(UmChromeOSPolicyTest,UpdateCanBeAppliedOutsideDisallowedTime)1534 TEST_F(UmChromeOSPolicyTest, UpdateCanBeAppliedOutsideDisallowedTime) {
1535   Time curr_time = fake_clock_->GetWallclockTime();
1536   TestDisallowedTimeIntervals(
1537       {WeeklyTimeInterval(
1538           WeeklyTime::FromTime(curr_time - TimeDelta::FromHours(3)),
1539           WeeklyTime::FromTime(curr_time))},
1540       ErrorCode::kSuccess,
1541       /* kiosk = */ true);
1542 }
1543 
TEST_F(UmChromeOSPolicyTest,UpdateCanBeAppliedPassesOnNonKiosk)1544 TEST_F(UmChromeOSPolicyTest, UpdateCanBeAppliedPassesOnNonKiosk) {
1545   Time curr_time = fake_clock_->GetWallclockTime();
1546   TestDisallowedTimeIntervals(
1547       {WeeklyTimeInterval(
1548           WeeklyTime::FromTime(curr_time),
1549           WeeklyTime::FromTime(curr_time + TimeDelta::FromMinutes(1)))},
1550       ErrorCode::kSuccess,
1551       /* kiosk = */ false);
1552 }
1553 
1554 }  // namespace chromeos_update_manager
1555