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 
266   UpdateCheckParams result;
267   ExpectPolicyStatus(
268       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
269   EXPECT_TRUE(result.updates_enabled);
270   EXPECT_EQ("1.2", result.target_version_prefix);
271   EXPECT_EQ(5, result.rollback_allowed_milestones);
272   EXPECT_EQ("foo-channel", result.target_channel);
273   EXPECT_FALSE(result.interactive);
274 }
275 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackAndPowerwash)276 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackAndPowerwash) {
277   EXPECT_TRUE(TestRollbackAllowed(
278       true, RollbackToTargetVersion::kRollbackAndPowerwash));
279 }
280 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackAndRestoreIfPossible)281 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackAndRestoreIfPossible) {
282   // We're doing rollback even if we don't support data save and restore.
283   EXPECT_TRUE(TestRollbackAllowed(
284       true, RollbackToTargetVersion::kRollbackAndRestoreIfPossible));
285 }
286 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackDisabled)287 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackDisabled) {
288   EXPECT_FALSE(TestRollbackAllowed(true, RollbackToTargetVersion::kDisabled));
289 }
290 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackUnspecified)291 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackUnspecified) {
292   EXPECT_FALSE(
293       TestRollbackAllowed(true, RollbackToTargetVersion::kUnspecified));
294 }
295 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedRollbackNotSet)296 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedRollbackNotSet) {
297   EXPECT_FALSE(
298       TestRollbackAllowed(false, RollbackToTargetVersion::kUnspecified));
299 }
300 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskRollbackAllowed)301 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskRollbackAllowed) {
302   SetKioskAppControlsChromeOsVersion();
303 
304   EXPECT_TRUE(TestRollbackAllowed(
305       true, RollbackToTargetVersion::kRollbackAndPowerwash));
306 }
307 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskRollbackDisabled)308 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskRollbackDisabled) {
309   SetKioskAppControlsChromeOsVersion();
310 
311   EXPECT_FALSE(TestRollbackAllowed(true, RollbackToTargetVersion::kDisabled));
312 }
313 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskRollbackUnspecified)314 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskRollbackUnspecified) {
315   SetKioskAppControlsChromeOsVersion();
316 
317   EXPECT_FALSE(
318       TestRollbackAllowed(true, RollbackToTargetVersion::kUnspecified));
319 }
320 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskRollbackNotSet)321 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskRollbackNotSet) {
322   SetKioskAppControlsChromeOsVersion();
323 
324   EXPECT_FALSE(
325       TestRollbackAllowed(false, RollbackToTargetVersion::kUnspecified));
326 }
327 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds)328 TEST_F(UmChromeOSPolicyTest,
329        UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
330   // UpdateCheckAllowed should return kAskMeAgainLater if this is an unofficial
331   // build; we don't want periodic update checks on developer images.
332 
333   fake_state_.system_provider()->var_is_official_build()->reset(
334       new bool(false));
335 
336   UpdateCheckParams result;
337   ExpectPolicyStatus(
338       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
339 }
340 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedUpdatesDisabledWhenNotEnoughSlotsAbUpdates)341 TEST_F(UmChromeOSPolicyTest,
342        UpdateCheckAllowedUpdatesDisabledWhenNotEnoughSlotsAbUpdates) {
343   // UpdateCheckAllowed should return false (kSucceeded) if the image booted
344   // without enough slots to do A/B updates.
345 
346   // NOLINTNEXTLINE(readability/casting)
347   fake_state_.system_provider()->var_num_slots()->reset(new unsigned int(1));
348 
349   UpdateCheckParams result;
350   ExpectPolicyStatus(
351       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
352   EXPECT_FALSE(result.updates_enabled);
353 }
354 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedUpdatesDisabledByPolicy)355 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
356   // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
357   // is loaded and prohibits updates.
358 
359   SetUpdateCheckAllowed(false);
360   fake_state_.device_policy_provider()->var_update_disabled()->reset(
361       new bool(true));
362 
363   UpdateCheckParams result;
364   ExpectPolicyStatus(
365       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
366 }
367 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedForcedUpdateRequestedInteractive)368 TEST_F(UmChromeOSPolicyTest,
369        UpdateCheckAllowedForcedUpdateRequestedInteractive) {
370   // UpdateCheckAllowed should return true because a forced update request was
371   // signaled for an interactive update.
372 
373   SetUpdateCheckAllowed(true);
374   fake_state_.updater_provider()->var_forced_update_requested()->reset(
375       new UpdateRequestStatus(UpdateRequestStatus::kInteractive));
376 
377   UpdateCheckParams result;
378   ExpectPolicyStatus(
379       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
380   EXPECT_TRUE(result.updates_enabled);
381   EXPECT_TRUE(result.interactive);
382 }
383 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedForcedUpdateRequestedPeriodic)384 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedForcedUpdateRequestedPeriodic) {
385   // UpdateCheckAllowed should return true because a forced update request was
386   // signaled for a periodic check.
387 
388   SetUpdateCheckAllowed(true);
389   fake_state_.updater_provider()->var_forced_update_requested()->reset(
390       new UpdateRequestStatus(UpdateRequestStatus::kPeriodic));
391 
392   UpdateCheckParams result;
393   ExpectPolicyStatus(
394       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
395   EXPECT_TRUE(result.updates_enabled);
396   EXPECT_FALSE(result.interactive);
397 }
398 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskPin)399 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskPin) {
400   // Update check is allowed.
401   SetUpdateCheckAllowed(true);
402 
403   SetKioskAppControlsChromeOsVersion();
404 
405   UpdateCheckParams result;
406   ExpectPolicyStatus(
407       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
408   EXPECT_TRUE(result.updates_enabled);
409   EXPECT_EQ("1234.", result.target_version_prefix);
410   EXPECT_FALSE(result.interactive);
411 }
412 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedDisabledWhenNoKioskPin)413 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedDisabledWhenNoKioskPin) {
414   // Update check is allowed.
415   SetUpdateCheckAllowed(true);
416 
417   // Disable AU policy is set but kiosk pin policy is set to false. Update is
418   // disabled in such case.
419   fake_state_.device_policy_provider()->var_update_disabled()->reset(
420       new bool(true));
421   fake_state_.device_policy_provider()
422       ->var_allow_kiosk_app_control_chrome_version()
423       ->reset(new bool(false));
424 
425   UpdateCheckParams result;
426   ExpectPolicyStatus(
427       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
428 }
429 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskPinWithNoRequiredVersion)430 TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedKioskPinWithNoRequiredVersion) {
431   // Update check is allowed.
432   SetUpdateCheckAllowed(true);
433 
434   // AU disabled, allow kiosk to pin but there is no kiosk required platform
435   // version (i.e. app does not provide the info). Update to latest in such
436   // case.
437   fake_state_.device_policy_provider()->var_update_disabled()->reset(
438       new bool(true));
439   fake_state_.device_policy_provider()
440       ->var_allow_kiosk_app_control_chrome_version()
441       ->reset(new bool(true));
442   fake_state_.system_provider()->var_kiosk_required_platform_version()->reset(
443       new string());
444 
445   UpdateCheckParams result;
446   ExpectPolicyStatus(
447       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
448   EXPECT_TRUE(result.updates_enabled);
449   EXPECT_TRUE(result.target_version_prefix.empty());
450   EXPECT_FALSE(result.interactive);
451 }
452 
TEST_F(UmChromeOSPolicyTest,UpdateCheckAllowedKioskPinWithFailedGetRequiredVersionCall)453 TEST_F(UmChromeOSPolicyTest,
454        UpdateCheckAllowedKioskPinWithFailedGetRequiredVersionCall) {
455   // AU disabled, allow kiosk to pin but D-Bus call to get required platform
456   // version failed. Defer update check in this case.
457   fake_state_.device_policy_provider()->var_update_disabled()->reset(
458       new bool(true));
459   fake_state_.device_policy_provider()
460       ->var_allow_kiosk_app_control_chrome_version()
461       ->reset(new bool(true));
462   fake_state_.system_provider()->var_kiosk_required_platform_version()->reset(
463       nullptr);
464 
465   UpdateCheckParams result;
466   ExpectPolicyStatus(
467       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
468 }
469 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartFailsCheckAllowedError)470 TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
471   // The UpdateCanStart policy fails, not being able to query
472   // UpdateCheckAllowed.
473 
474   // Configure the UpdateCheckAllowed policy to fail.
475   fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
476 
477   // Check that the UpdateCanStart fails.
478   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
479   UpdateDownloadParams result;
480   ExpectPolicyStatus(
481       EvalStatus::kFailed, &Policy::UpdateCanStart, &result, update_state);
482 }
483 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedCheckDue)484 TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
485   // The UpdateCanStart policy returns false because we are due for another
486   // update check. Ensure that download related values are still returned.
487 
488   SetUpdateCheckAllowed(true);
489 
490   // Check that the UpdateCanStart returns false.
491   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
492   UpdateDownloadParams result;
493   ExpectPolicyStatus(
494       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
495   EXPECT_FALSE(result.update_can_start);
496   EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
497   EXPECT_EQ(0, result.download_url_idx);
498   EXPECT_EQ(0, result.download_url_num_errors);
499 }
500 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoDevicePolicy)501 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
502   // The UpdateCanStart policy returns true; no device policy is loaded.
503 
504   SetUpdateCheckAllowed(false);
505   fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
506       new bool(false));
507 
508   // Check that the UpdateCanStart returns true with no further attributes.
509   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
510   UpdateDownloadParams result;
511   ExpectPolicyStatus(
512       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
513   EXPECT_TRUE(result.update_can_start);
514   EXPECT_FALSE(result.p2p_downloading_allowed);
515   EXPECT_FALSE(result.p2p_sharing_allowed);
516   EXPECT_EQ(0, result.download_url_idx);
517   EXPECT_TRUE(result.download_url_allowed);
518   EXPECT_EQ(0, result.download_url_num_errors);
519   EXPECT_FALSE(result.do_increment_failures);
520 }
521 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedBlankPolicy)522 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
523   // The UpdateCanStart policy returns true; device policy is loaded but imposes
524   // no restrictions on updating.
525 
526   SetUpdateCheckAllowed(false);
527 
528   // Check that the UpdateCanStart returns true.
529   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
530   UpdateDownloadParams result;
531   ExpectPolicyStatus(
532       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
533   EXPECT_TRUE(result.update_can_start);
534   EXPECT_FALSE(result.p2p_downloading_allowed);
535   EXPECT_FALSE(result.p2p_sharing_allowed);
536   EXPECT_EQ(0, result.download_url_idx);
537   EXPECT_TRUE(result.download_url_allowed);
538   EXPECT_EQ(0, result.download_url_num_errors);
539   EXPECT_FALSE(result.do_increment_failures);
540 }
541 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedBackoffNewWaitPeriodApplies)542 TEST_F(UmChromeOSPolicyTest,
543        UpdateCanStartNotAllowedBackoffNewWaitPeriodApplies) {
544   // The UpdateCanStart policy returns false; failures are reported and a new
545   // backoff period is enacted.
546 
547   SetUpdateCheckAllowed(false);
548 
549   const Time curr_time = fake_clock_.GetWallclockTime();
550   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
551   update_state.download_errors_max = 1;
552   update_state.download_errors.emplace_back(
553       0,
554       ErrorCode::kDownloadTransferError,
555       curr_time - TimeDelta::FromSeconds(8));
556   update_state.download_errors.emplace_back(
557       0,
558       ErrorCode::kDownloadTransferError,
559       curr_time - TimeDelta::FromSeconds(2));
560 
561   // Check that UpdateCanStart returns false and a new backoff expiry is
562   // generated.
563   UpdateDownloadParams result;
564   ExpectPolicyStatus(
565       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
566   EXPECT_FALSE(result.update_can_start);
567   EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
568   EXPECT_TRUE(result.do_increment_failures);
569   EXPECT_LT(curr_time, result.backoff_expiry);
570 }
571 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedBackoffPrevWaitPeriodStillApplies)572 TEST_F(UmChromeOSPolicyTest,
573        UpdateCanStartNotAllowedBackoffPrevWaitPeriodStillApplies) {
574   // The UpdateCanStart policy returns false; a previously enacted backoff
575   // period still applies.
576 
577   SetUpdateCheckAllowed(false);
578 
579   const Time curr_time = fake_clock_.GetWallclockTime();
580   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
581   update_state.download_errors_max = 1;
582   update_state.download_errors.emplace_back(
583       0,
584       ErrorCode::kDownloadTransferError,
585       curr_time - TimeDelta::FromSeconds(8));
586   update_state.download_errors.emplace_back(
587       0,
588       ErrorCode::kDownloadTransferError,
589       curr_time - TimeDelta::FromSeconds(2));
590   update_state.failures_last_updated = curr_time;
591   update_state.backoff_expiry = curr_time + TimeDelta::FromMinutes(3);
592 
593   // Check that UpdateCanStart returns false and a new backoff expiry is
594   // generated.
595   UpdateDownloadParams result;
596   ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
597                      &Policy::UpdateCanStart,
598                      &result,
599                      update_state);
600   EXPECT_FALSE(result.update_can_start);
601   EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
602   EXPECT_FALSE(result.do_increment_failures);
603   EXPECT_LT(curr_time, result.backoff_expiry);
604 }
605 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedBackoffSatisfied)606 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffSatisfied) {
607   // The UpdateCanStart policy returns true; a previously enacted backoff period
608   // has elapsed, we're good to go.
609 
610   SetUpdateCheckAllowed(false);
611 
612   const Time curr_time = fake_clock_.GetWallclockTime();
613   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
614   update_state.download_errors_max = 1;
615   update_state.download_errors.emplace_back(
616       0,
617       ErrorCode::kDownloadTransferError,
618       curr_time - TimeDelta::FromSeconds(8));
619   update_state.download_errors.emplace_back(
620       0,
621       ErrorCode::kDownloadTransferError,
622       curr_time - TimeDelta::FromSeconds(2));
623   update_state.failures_last_updated = curr_time - TimeDelta::FromSeconds(1);
624   update_state.backoff_expiry = curr_time - TimeDelta::FromSeconds(1);
625 
626   // Check that UpdateCanStart returns false and a new backoff expiry is
627   // generated.
628   UpdateDownloadParams result;
629   ExpectPolicyStatus(
630       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
631   EXPECT_TRUE(result.update_can_start);
632   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
633   EXPECT_EQ(0, result.download_url_idx);
634   EXPECT_TRUE(result.download_url_allowed);
635   EXPECT_EQ(0, result.download_url_num_errors);
636   EXPECT_FALSE(result.do_increment_failures);
637   EXPECT_EQ(Time(), result.backoff_expiry);
638 }
639 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedBackoffDisabled)640 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffDisabled) {
641   // The UpdateCanStart policy returns false; failures are reported but backoff
642   // is disabled.
643 
644   SetUpdateCheckAllowed(false);
645 
646   const Time curr_time = fake_clock_.GetWallclockTime();
647   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
648   update_state.download_errors_max = 1;
649   update_state.download_errors.emplace_back(
650       0,
651       ErrorCode::kDownloadTransferError,
652       curr_time - TimeDelta::FromSeconds(8));
653   update_state.download_errors.emplace_back(
654       0,
655       ErrorCode::kDownloadTransferError,
656       curr_time - TimeDelta::FromSeconds(2));
657   update_state.is_backoff_disabled = true;
658 
659   // Check that UpdateCanStart returns false and a new backoff expiry is
660   // generated.
661   UpdateDownloadParams result;
662   ExpectPolicyStatus(
663       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
664   EXPECT_TRUE(result.update_can_start);
665   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
666   EXPECT_EQ(0, result.download_url_idx);
667   EXPECT_TRUE(result.download_url_allowed);
668   EXPECT_EQ(0, result.download_url_num_errors);
669   EXPECT_TRUE(result.do_increment_failures);
670   EXPECT_EQ(Time(), result.backoff_expiry);
671 }
672 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoBackoffInteractive)673 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffInteractive) {
674   // The UpdateCanStart policy returns false; failures are reported but this is
675   // an interactive update check.
676 
677   SetUpdateCheckAllowed(false);
678 
679   const Time curr_time = fake_clock_.GetWallclockTime();
680   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
681   update_state.download_errors_max = 1;
682   update_state.download_errors.emplace_back(
683       0,
684       ErrorCode::kDownloadTransferError,
685       curr_time - TimeDelta::FromSeconds(8));
686   update_state.download_errors.emplace_back(
687       0,
688       ErrorCode::kDownloadTransferError,
689       curr_time - TimeDelta::FromSeconds(2));
690   update_state.interactive = true;
691 
692   // Check that UpdateCanStart returns false and a new backoff expiry is
693   // generated.
694   UpdateDownloadParams result;
695   ExpectPolicyStatus(
696       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
697   EXPECT_TRUE(result.update_can_start);
698   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
699   EXPECT_EQ(0, result.download_url_idx);
700   EXPECT_TRUE(result.download_url_allowed);
701   EXPECT_EQ(0, result.download_url_num_errors);
702   EXPECT_TRUE(result.do_increment_failures);
703   EXPECT_EQ(Time(), result.backoff_expiry);
704 }
705 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoBackoffDelta)706 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffDelta) {
707   // The UpdateCanStart policy returns false; failures are reported but this is
708   // a delta payload.
709 
710   SetUpdateCheckAllowed(false);
711 
712   const Time curr_time = fake_clock_.GetWallclockTime();
713   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
714   update_state.download_errors_max = 1;
715   update_state.download_errors.emplace_back(
716       0,
717       ErrorCode::kDownloadTransferError,
718       curr_time - TimeDelta::FromSeconds(8));
719   update_state.download_errors.emplace_back(
720       0,
721       ErrorCode::kDownloadTransferError,
722       curr_time - TimeDelta::FromSeconds(2));
723   update_state.is_delta_payload = true;
724 
725   // Check that UpdateCanStart returns false and a new backoff expiry is
726   // generated.
727   UpdateDownloadParams result;
728   ExpectPolicyStatus(
729       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
730   EXPECT_TRUE(result.update_can_start);
731   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
732   EXPECT_EQ(0, result.download_url_idx);
733   EXPECT_TRUE(result.download_url_allowed);
734   EXPECT_EQ(0, result.download_url_num_errors);
735   EXPECT_TRUE(result.do_increment_failures);
736   EXPECT_EQ(Time(), result.backoff_expiry);
737 }
738 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoBackoffUnofficialBuild)739 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffUnofficialBuild) {
740   // The UpdateCanStart policy returns false; failures are reported but this is
741   // an unofficial build.
742 
743   SetUpdateCheckAllowed(false);
744 
745   const Time curr_time = fake_clock_.GetWallclockTime();
746   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
747   update_state.download_errors_max = 1;
748   update_state.download_errors.emplace_back(
749       0,
750       ErrorCode::kDownloadTransferError,
751       curr_time - TimeDelta::FromSeconds(8));
752   update_state.download_errors.emplace_back(
753       0,
754       ErrorCode::kDownloadTransferError,
755       curr_time - TimeDelta::FromSeconds(2));
756 
757   fake_state_.system_provider()->var_is_official_build()->reset(
758       new bool(false));
759 
760   // Check that UpdateCanStart returns false and a new backoff expiry is
761   // generated.
762   UpdateDownloadParams result;
763   ExpectPolicyStatus(
764       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
765   EXPECT_TRUE(result.update_can_start);
766   EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
767   EXPECT_EQ(0, result.download_url_idx);
768   EXPECT_TRUE(result.download_url_allowed);
769   EXPECT_EQ(0, result.download_url_num_errors);
770   EXPECT_TRUE(result.do_increment_failures);
771   EXPECT_EQ(Time(), result.backoff_expiry);
772 }
773 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartFailsScatteringFailed)774 TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
775   // The UpdateCanStart policy fails because the UpdateScattering policy it
776   // depends on fails (unset variable).
777 
778   SetUpdateCheckAllowed(false);
779 
780   // Override the default seed variable with a null value so that the policy
781   // request would fail.
782   // TODO(garnold) This failure may or may not fail a number
783   // sub-policies/decisions, like scattering and backoff. We'll need a more
784   // deliberate setup to ensure that we're failing what we want to be failing.
785   fake_state_.random_provider()->var_seed()->reset(nullptr);
786 
787   // Check that the UpdateCanStart fails.
788   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
789   UpdateDownloadParams result;
790   ExpectPolicyStatus(
791       EvalStatus::kFailed, &Policy::UpdateCanStart, &result, update_state);
792 }
793 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies)794 TEST_F(UmChromeOSPolicyTest,
795        UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
796   // The UpdateCanStart policy returns false; device policy is loaded and
797   // scattering applies due to an unsatisfied wait period, which was newly
798   // generated.
799 
800   SetUpdateCheckAllowed(false);
801   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
802       new TimeDelta(TimeDelta::FromMinutes(2)));
803 
804   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
805 
806   // Check that the UpdateCanStart returns false and a new wait period
807   // generated.
808   UpdateDownloadParams result;
809   ExpectPolicyStatus(
810       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
811   EXPECT_FALSE(result.update_can_start);
812   EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
813   EXPECT_LT(TimeDelta(), result.scatter_wait_period);
814   EXPECT_EQ(0, result.scatter_check_threshold);
815 }
816 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies)817 TEST_F(UmChromeOSPolicyTest,
818        UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
819   // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
820   // is loaded and a previously generated scattering period still applies, none
821   // of the scattering values has changed.
822 
823   SetUpdateCheckAllowed(false);
824   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
825       new TimeDelta(TimeDelta::FromMinutes(2)));
826 
827   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
828   update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
829 
830   // Check that the UpdateCanStart returns false and a new wait period
831   // generated.
832   UpdateDownloadParams result;
833   ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
834                      &Policy::UpdateCanStart,
835                      &result,
836                      update_state);
837   EXPECT_FALSE(result.update_can_start);
838   EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
839   EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
840   EXPECT_EQ(0, result.scatter_check_threshold);
841 }
842 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedScatteringNewCountThresholdApplies)843 TEST_F(UmChromeOSPolicyTest,
844        UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
845   // The UpdateCanStart policy returns false; device policy is loaded and
846   // scattering applies due to an unsatisfied update check count threshold.
847   //
848   // This ensures a non-zero check threshold, which may or may not be combined
849   // with a non-zero wait period (for which we cannot reliably control).
850 
851   SetUpdateCheckAllowed(false);
852   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
853       new TimeDelta(TimeDelta::FromSeconds(1)));
854 
855   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
856   update_state.scatter_check_threshold_min = 2;
857   update_state.scatter_check_threshold_max = 5;
858 
859   // Check that the UpdateCanStart returns false.
860   UpdateDownloadParams result;
861   ExpectPolicyStatus(
862       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
863   EXPECT_FALSE(result.update_can_start);
864   EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
865   EXPECT_LE(2, result.scatter_check_threshold);
866   EXPECT_GE(5, result.scatter_check_threshold);
867 }
868 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies)869 TEST_F(UmChromeOSPolicyTest,
870        UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
871   // The UpdateCanStart policy returns false; device policy is loaded and
872   // scattering due to a previously generated count threshold still applies.
873 
874   SetUpdateCheckAllowed(false);
875   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
876       new TimeDelta(TimeDelta::FromSeconds(1)));
877 
878   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
879   update_state.scatter_check_threshold = 3;
880   update_state.scatter_check_threshold_min = 2;
881   update_state.scatter_check_threshold_max = 5;
882 
883   // Check that the UpdateCanStart returns false.
884   UpdateDownloadParams result;
885   ExpectPolicyStatus(
886       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
887   EXPECT_FALSE(result.update_can_start);
888   EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
889   EXPECT_EQ(3, result.scatter_check_threshold);
890 }
891 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedScatteringSatisfied)892 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
893   // The UpdateCanStart policy returns true; device policy is loaded and
894   // scattering is enabled, but both wait period and check threshold are
895   // satisfied.
896 
897   SetUpdateCheckAllowed(false);
898   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
899       new TimeDelta(TimeDelta::FromSeconds(120)));
900 
901   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
902   update_state.num_checks = 4;
903   update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
904   update_state.scatter_check_threshold = 3;
905   update_state.scatter_check_threshold_min = 2;
906   update_state.scatter_check_threshold_max = 5;
907 
908   // Check that the UpdateCanStart returns true.
909   UpdateDownloadParams result;
910   ExpectPolicyStatus(
911       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
912   EXPECT_TRUE(result.update_can_start);
913   EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
914   EXPECT_EQ(0, result.scatter_check_threshold);
915   EXPECT_EQ(0, result.download_url_idx);
916   EXPECT_TRUE(result.download_url_allowed);
917   EXPECT_EQ(0, result.download_url_num_errors);
918   EXPECT_FALSE(result.do_increment_failures);
919 }
920 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedInteractivePreventsScattering)921 TEST_F(UmChromeOSPolicyTest,
922        UpdateCanStartAllowedInteractivePreventsScattering) {
923   // The UpdateCanStart policy returns true; device policy is loaded and
924   // scattering would have applied, except that the update check is interactive
925   // and so it is suppressed.
926 
927   SetUpdateCheckAllowed(false);
928   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
929       new TimeDelta(TimeDelta::FromSeconds(1)));
930 
931   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
932   update_state.interactive = true;
933   update_state.scatter_check_threshold = 0;
934   update_state.scatter_check_threshold_min = 2;
935   update_state.scatter_check_threshold_max = 5;
936 
937   // Check that the UpdateCanStart returns true.
938   UpdateDownloadParams result;
939   ExpectPolicyStatus(
940       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
941   EXPECT_TRUE(result.update_can_start);
942   EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
943   EXPECT_EQ(0, result.scatter_check_threshold);
944   EXPECT_EQ(0, result.download_url_idx);
945   EXPECT_TRUE(result.download_url_allowed);
946   EXPECT_EQ(0, result.download_url_num_errors);
947   EXPECT_FALSE(result.do_increment_failures);
948 }
949 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedOobePreventsScattering)950 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedOobePreventsScattering) {
951   // The UpdateCanStart policy returns true; device policy is loaded and
952   // scattering would have applied, except that OOBE was not completed and so it
953   // is suppressed.
954 
955   SetUpdateCheckAllowed(false);
956   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
957       new TimeDelta(TimeDelta::FromSeconds(1)));
958   fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
959 
960   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
961   update_state.interactive = true;
962   update_state.scatter_check_threshold = 0;
963   update_state.scatter_check_threshold_min = 2;
964   update_state.scatter_check_threshold_max = 5;
965 
966   // Check that the UpdateCanStart returns true.
967   UpdateDownloadParams result;
968   ExpectPolicyStatus(
969       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
970   EXPECT_TRUE(result.update_can_start);
971   EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
972   EXPECT_EQ(0, result.scatter_check_threshold);
973   EXPECT_EQ(0, result.download_url_idx);
974   EXPECT_TRUE(result.download_url_allowed);
975   EXPECT_EQ(0, result.download_url_num_errors);
976   EXPECT_FALSE(result.do_increment_failures);
977 }
978 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithAttributes)979 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
980   // The UpdateCanStart policy returns true; device policy permits both HTTP and
981   // P2P updates, as well as a non-empty target channel string.
982 
983   SetUpdateCheckAllowed(false);
984 
985   // Override specific device policy attributes.
986   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
987       new bool(true));
988   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
989       new bool(true));
990 
991   // Check that the UpdateCanStart returns true.
992   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
993   UpdateDownloadParams result;
994   ExpectPolicyStatus(
995       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
996   EXPECT_TRUE(result.update_can_start);
997   EXPECT_TRUE(result.p2p_downloading_allowed);
998   EXPECT_TRUE(result.p2p_sharing_allowed);
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,UpdateCanStartAllowedWithP2PFromUpdater)1005 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
1006   // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1007   // P2P updates, but the updater is configured to allow P2P and overrules the
1008   // setting.
1009 
1010   SetUpdateCheckAllowed(false);
1011 
1012   // Override specific device policy attributes.
1013   fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
1014 
1015   // Check that the UpdateCanStart returns true.
1016   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1017   UpdateDownloadParams result;
1018   ExpectPolicyStatus(
1019       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1020   EXPECT_TRUE(result.update_can_start);
1021   EXPECT_TRUE(result.p2p_downloading_allowed);
1022   EXPECT_TRUE(result.p2p_sharing_allowed);
1023   EXPECT_EQ(0, result.download_url_idx);
1024   EXPECT_TRUE(result.download_url_allowed);
1025   EXPECT_EQ(0, result.download_url_num_errors);
1026   EXPECT_FALSE(result.do_increment_failures);
1027 }
1028 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedP2PDownloadingBlockedDueToOmaha)1029 TEST_F(UmChromeOSPolicyTest,
1030        UpdateCanStartAllowedP2PDownloadingBlockedDueToOmaha) {
1031   // The UpdateCanStart policy returns true; device policy permits HTTP, but
1032   // policy blocks P2P downloading because Omaha forbids it.  P2P sharing is
1033   // still permitted.
1034 
1035   SetUpdateCheckAllowed(false);
1036 
1037   // Override specific device policy attributes.
1038   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1039       new bool(true));
1040   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1041       new bool(true));
1042 
1043   // Check that the UpdateCanStart returns true.
1044   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1045   update_state.p2p_downloading_disabled = true;
1046   UpdateDownloadParams result;
1047   ExpectPolicyStatus(
1048       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1049   EXPECT_TRUE(result.update_can_start);
1050   EXPECT_FALSE(result.p2p_downloading_allowed);
1051   EXPECT_TRUE(result.p2p_sharing_allowed);
1052 }
1053 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedP2PSharingBlockedDueToOmaha)1054 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedP2PSharingBlockedDueToOmaha) {
1055   // The UpdateCanStart policy returns true; device policy permits HTTP, but
1056   // policy blocks P2P sharing because Omaha forbids it.  P2P downloading is
1057   // still permitted.
1058 
1059   SetUpdateCheckAllowed(false);
1060 
1061   // Override specific device policy attributes.
1062   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1063       new bool(true));
1064   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1065       new bool(true));
1066 
1067   // Check that the UpdateCanStart returns true.
1068   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1069   update_state.p2p_sharing_disabled = true;
1070   UpdateDownloadParams result;
1071   ExpectPolicyStatus(
1072       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1073   EXPECT_TRUE(result.update_can_start);
1074   EXPECT_TRUE(result.p2p_downloading_allowed);
1075   EXPECT_FALSE(result.p2p_sharing_allowed);
1076 }
1077 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts)1078 TEST_F(UmChromeOSPolicyTest,
1079        UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts) {
1080   // The UpdateCanStart policy returns true; device policy permits HTTP but
1081   // blocks P2P download, because the max number of P2P downloads have been
1082   // attempted. P2P sharing is still permitted.
1083 
1084   SetUpdateCheckAllowed(false);
1085 
1086   // Override specific device policy attributes.
1087   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1088       new bool(true));
1089   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1090       new bool(true));
1091 
1092   // Check that the UpdateCanStart returns true.
1093   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1094   update_state.p2p_num_attempts = ChromeOSPolicy::kMaxP2PAttempts;
1095   UpdateDownloadParams result;
1096   ExpectPolicyStatus(
1097       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1098   EXPECT_TRUE(result.update_can_start);
1099   EXPECT_FALSE(result.p2p_downloading_allowed);
1100   EXPECT_TRUE(result.p2p_sharing_allowed);
1101 }
1102 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod)1103 TEST_F(UmChromeOSPolicyTest,
1104        UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod) {
1105   // The UpdateCanStart policy returns true; device policy permits HTTP but
1106   // blocks P2P download, because the max period for attempt to download via P2P
1107   // has elapsed. P2P sharing is still permitted.
1108 
1109   SetUpdateCheckAllowed(false);
1110 
1111   // Override specific device policy attributes.
1112   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1113       new bool(true));
1114   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1115       new bool(true));
1116 
1117   // Check that the UpdateCanStart returns true.
1118   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1119   update_state.p2p_num_attempts = 1;
1120   update_state.p2p_first_attempted =
1121       fake_clock_.GetWallclockTime() -
1122       TimeDelta::FromSeconds(ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds +
1123                              1);
1124   UpdateDownloadParams result;
1125   ExpectPolicyStatus(
1126       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1127   EXPECT_TRUE(result.update_can_start);
1128   EXPECT_FALSE(result.p2p_downloading_allowed);
1129   EXPECT_TRUE(result.p2p_sharing_allowed);
1130 }
1131 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithHttpUrlForUnofficialBuild)1132 TEST_F(UmChromeOSPolicyTest,
1133        UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
1134   // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1135   // P2P updates, but marking this an unofficial build overrules the HTTP
1136   // setting.
1137 
1138   SetUpdateCheckAllowed(false);
1139 
1140   // Override specific device policy attributes.
1141   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1142       new bool(false));
1143   fake_state_.system_provider()->var_is_official_build()->reset(
1144       new bool(false));
1145 
1146   // Check that the UpdateCanStart returns true.
1147   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1148   UpdateDownloadParams result;
1149   ExpectPolicyStatus(
1150       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1151   EXPECT_TRUE(result.update_can_start);
1152   EXPECT_EQ(0, result.download_url_idx);
1153   EXPECT_TRUE(result.download_url_allowed);
1154   EXPECT_EQ(0, result.download_url_num_errors);
1155   EXPECT_FALSE(result.do_increment_failures);
1156 }
1157 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithHttpsUrl)1158 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
1159   // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1160   // P2P updates, but an HTTPS URL is provided and selected for download.
1161 
1162   SetUpdateCheckAllowed(false);
1163 
1164   // Override specific device policy attributes.
1165   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1166       new bool(false));
1167 
1168   // Add an HTTPS URL.
1169   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1170   update_state.download_urls.emplace_back("https://secure/url/");
1171 
1172   // Check that the UpdateCanStart returns true.
1173   UpdateDownloadParams result;
1174   ExpectPolicyStatus(
1175       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1176   EXPECT_TRUE(result.update_can_start);
1177   EXPECT_EQ(1, result.download_url_idx);
1178   EXPECT_TRUE(result.download_url_allowed);
1179   EXPECT_EQ(0, result.download_url_num_errors);
1180   EXPECT_FALSE(result.do_increment_failures);
1181 }
1182 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedMaxErrorsNotExceeded)1183 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedMaxErrorsNotExceeded) {
1184   // The UpdateCanStart policy returns true; the first URL has download errors
1185   // but does not exceed the maximum allowed number of failures, so it is stilli
1186   // usable.
1187 
1188   SetUpdateCheckAllowed(false);
1189 
1190   // Add a second URL; update with this URL attempted and failed enough times to
1191   // disqualify the current (first) URL.
1192   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1193   update_state.num_checks = 5;
1194   update_state.download_urls.emplace_back("http://another/fake/url/");
1195   Time t = fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(12);
1196   for (int i = 0; i < 5; i++) {
1197     update_state.download_errors.emplace_back(
1198         0, ErrorCode::kDownloadTransferError, t);
1199     t += TimeDelta::FromSeconds(1);
1200   }
1201 
1202   // Check that the UpdateCanStart returns true.
1203   UpdateDownloadParams result;
1204   ExpectPolicyStatus(
1205       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1206   EXPECT_TRUE(result.update_can_start);
1207   EXPECT_EQ(0, result.download_url_idx);
1208   EXPECT_TRUE(result.download_url_allowed);
1209   EXPECT_EQ(5, result.download_url_num_errors);
1210   EXPECT_FALSE(result.do_increment_failures);
1211 }
1212 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithSecondUrlMaxExceeded)1213 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
1214   // The UpdateCanStart policy returns true; the first URL exceeded the maximum
1215   // allowed number of failures, but a second URL is available.
1216 
1217   SetUpdateCheckAllowed(false);
1218 
1219   // Add a second URL; update with this URL attempted and failed enough times to
1220   // disqualify the current (first) URL.
1221   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1222   update_state.num_checks = 10;
1223   update_state.download_urls.emplace_back("http://another/fake/url/");
1224   Time t = fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(12);
1225   for (int i = 0; i < 11; i++) {
1226     update_state.download_errors.emplace_back(
1227         0, ErrorCode::kDownloadTransferError, t);
1228     t += TimeDelta::FromSeconds(1);
1229   }
1230 
1231   // Check that the UpdateCanStart returns true.
1232   UpdateDownloadParams result;
1233   ExpectPolicyStatus(
1234       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1235   EXPECT_TRUE(result.update_can_start);
1236   EXPECT_EQ(1, result.download_url_idx);
1237   EXPECT_TRUE(result.download_url_allowed);
1238   EXPECT_EQ(0, result.download_url_num_errors);
1239   EXPECT_FALSE(result.do_increment_failures);
1240 }
1241 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedWithSecondUrlHardError)1242 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
1243   // The UpdateCanStart policy returns true; the first URL fails with a hard
1244   // error, but a second URL is available.
1245 
1246   SetUpdateCheckAllowed(false);
1247 
1248   // Add a second URL; update with this URL attempted and failed in a way that
1249   // causes it to switch directly to the next URL.
1250   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1251   update_state.num_checks = 10;
1252   update_state.download_urls.emplace_back("http://another/fake/url/");
1253   update_state.download_errors.emplace_back(
1254       0,
1255       ErrorCode::kPayloadHashMismatchError,
1256       fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1));
1257 
1258   // Check that the UpdateCanStart returns true.
1259   UpdateDownloadParams result;
1260   ExpectPolicyStatus(
1261       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1262   EXPECT_TRUE(result.update_can_start);
1263   EXPECT_EQ(1, result.download_url_idx);
1264   EXPECT_TRUE(result.download_url_allowed);
1265   EXPECT_EQ(0, result.download_url_num_errors);
1266   EXPECT_FALSE(result.do_increment_failures);
1267 }
1268 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedUrlWrapsAround)1269 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
1270   // The UpdateCanStart policy returns true; URL search properly wraps around
1271   // the last one on the list.
1272 
1273   SetUpdateCheckAllowed(false);
1274 
1275   // Add a second URL; update with this URL attempted and failed in a way that
1276   // causes it to switch directly to the next URL. We must disable backoff in
1277   // order for it not to interfere.
1278   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1279   update_state.num_checks = 1;
1280   update_state.is_backoff_disabled = true;
1281   update_state.download_urls.emplace_back("http://another/fake/url/");
1282   update_state.download_errors.emplace_back(
1283       1,
1284       ErrorCode::kPayloadHashMismatchError,
1285       fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1));
1286 
1287   // Check that the UpdateCanStart returns true.
1288   UpdateDownloadParams result;
1289   ExpectPolicyStatus(
1290       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1291   EXPECT_TRUE(result.update_can_start);
1292   EXPECT_EQ(0, result.download_url_idx);
1293   EXPECT_TRUE(result.download_url_allowed);
1294   EXPECT_EQ(0, result.download_url_num_errors);
1295   EXPECT_TRUE(result.do_increment_failures);
1296 }
1297 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartNotAllowedNoUsableUrls)1298 TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
1299   // The UpdateCanStart policy returns false; there's a single HTTP URL but its
1300   // use is forbidden by policy.
1301   //
1302   // Note: In the case where no usable URLs are found, the policy should not
1303   // increment the number of failed attempts! Doing so would result in a
1304   // non-idempotent semantics, and does not fall within the intended purpose of
1305   // the backoff mechanism anyway.
1306 
1307   SetUpdateCheckAllowed(false);
1308 
1309   // Override specific device policy attributes.
1310   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1311       new bool(false));
1312 
1313   // Check that the UpdateCanStart returns false.
1314   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1315   UpdateDownloadParams result;
1316   ExpectPolicyStatus(
1317       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1318   EXPECT_FALSE(result.update_can_start);
1319   EXPECT_EQ(UpdateCannotStartReason::kCannotDownload,
1320             result.cannot_start_reason);
1321   EXPECT_FALSE(result.do_increment_failures);
1322 }
1323 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoUsableUrlsButP2PEnabled)1324 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
1325   // The UpdateCanStart policy returns true; there's a single HTTP URL but its
1326   // use is forbidden by policy, however P2P is enabled. The result indicates
1327   // that no URL can be used.
1328   //
1329   // Note: The number of failed attempts should not increase in this case (see
1330   // above test).
1331 
1332   SetUpdateCheckAllowed(false);
1333 
1334   // Override specific device policy attributes.
1335   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1336       new bool(true));
1337   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1338       new bool(false));
1339 
1340   // Check that the UpdateCanStart returns true.
1341   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1342   UpdateDownloadParams result;
1343   ExpectPolicyStatus(
1344       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1345   EXPECT_TRUE(result.update_can_start);
1346   EXPECT_TRUE(result.p2p_downloading_allowed);
1347   EXPECT_TRUE(result.p2p_sharing_allowed);
1348   EXPECT_GT(0, result.download_url_idx);
1349   EXPECT_TRUE(result.download_url_allowed);
1350   EXPECT_EQ(0, result.download_url_num_errors);
1351   EXPECT_FALSE(result.do_increment_failures);
1352 }
1353 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedNoUsableUrlsButEnterpriseEnrolled)1354 TEST_F(UmChromeOSPolicyTest,
1355        UpdateCanStartAllowedNoUsableUrlsButEnterpriseEnrolled) {
1356   // The UpdateCanStart policy returns true; there's a single HTTP URL but its
1357   // use is forbidden by policy, and P2P is unset on the policy, however the
1358   // device is enterprise-enrolled so P2P is allowed. The result indicates that
1359   // no URL can be used.
1360   //
1361   // Note: The number of failed attempts should not increase in this case (see
1362   // above test).
1363 
1364   SetUpdateCheckAllowed(false);
1365 
1366   // Override specific device policy attributes.
1367   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(nullptr);
1368   fake_state_.device_policy_provider()->var_has_owner()->reset(new bool(false));
1369   fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1370       new bool(false));
1371 
1372   // Check that the UpdateCanStart returns true.
1373   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1374   UpdateDownloadParams result;
1375   ExpectPolicyStatus(
1376       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1377   EXPECT_TRUE(result.update_can_start);
1378   EXPECT_TRUE(result.p2p_downloading_allowed);
1379   EXPECT_TRUE(result.p2p_sharing_allowed);
1380   EXPECT_GT(0, result.download_url_idx);
1381   EXPECT_TRUE(result.download_url_allowed);
1382   EXPECT_EQ(0, result.download_url_num_errors);
1383   EXPECT_FALSE(result.do_increment_failures);
1384 }
1385 
TEST_F(UmChromeOSPolicyTest,UpdateDownloadAllowedEthernetDefault)1386 TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
1387   // Ethernet is always allowed.
1388 
1389   fake_state_.shill_provider()->var_conn_type()->reset(
1390       new ConnectionType(ConnectionType::kEthernet));
1391 
1392   bool result;
1393   ExpectPolicyStatus(
1394       EvalStatus::kSucceeded, &Policy::UpdateDownloadAllowed, &result);
1395   EXPECT_TRUE(result);
1396 }
1397 
TEST_F(UmChromeOSPolicyTest,UpdateDownloadAllowedWifiDefault)1398 TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
1399   // Wifi is allowed if not tethered.
1400 
1401   fake_state_.shill_provider()->var_conn_type()->reset(
1402       new ConnectionType(ConnectionType::kWifi));
1403 
1404   bool result;
1405   ExpectPolicyStatus(
1406       EvalStatus::kSucceeded, &Policy::UpdateDownloadAllowed, &result);
1407   EXPECT_TRUE(result);
1408 }
1409 
TEST_F(UmChromeOSPolicyTest,UpdateCurrentConnectionNotAllowedWifiTetheredDefault)1410 TEST_F(UmChromeOSPolicyTest,
1411        UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
1412   // Tethered wifi is not allowed by default.
1413 
1414   fake_state_.shill_provider()->var_conn_type()->reset(
1415       new ConnectionType(ConnectionType::kWifi));
1416   fake_state_.shill_provider()->var_conn_tethering()->reset(
1417       new ConnectionTethering(ConnectionTethering::kConfirmed));
1418 
1419   bool result;
1420   ExpectPolicyStatus(
1421       EvalStatus::kAskMeAgainLater, &Policy::UpdateDownloadAllowed, &result);
1422 }
1423 
TEST_F(UmChromeOSPolicyTest,UpdateDownloadAllowedWifiTetheredPolicyOverride)1424 TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiTetheredPolicyOverride) {
1425   // Tethered wifi can be allowed by policy.
1426 
1427   fake_state_.shill_provider()->var_conn_type()->reset(
1428       new ConnectionType(ConnectionType::kWifi));
1429   fake_state_.shill_provider()->var_conn_tethering()->reset(
1430       new ConnectionTethering(ConnectionTethering::kConfirmed));
1431   set<ConnectionType> allowed_connections;
1432   allowed_connections.insert(ConnectionType::kCellular);
1433   fake_state_.device_policy_provider()
1434       ->var_allowed_connection_types_for_update()
1435       ->reset(new set<ConnectionType>(allowed_connections));
1436 
1437   bool result;
1438   ExpectPolicyStatus(
1439       EvalStatus::kSucceeded, &Policy::UpdateDownloadAllowed, &result);
1440   EXPECT_TRUE(result);
1441 }
1442 
TEST_F(UmChromeOSPolicyTest,UpdateCurrentConnectionNotAllowedCellularDefault)1443 TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
1444   // Cellular is not allowed by default.
1445 
1446   fake_state_.shill_provider()->var_conn_type()->reset(
1447       new ConnectionType(ConnectionType::kCellular));
1448 
1449   bool result;
1450   ExpectPolicyStatus(
1451       EvalStatus::kAskMeAgainLater, &Policy::UpdateDownloadAllowed, &result);
1452 }
1453 
TEST_F(UmChromeOSPolicyTest,UpdateDownloadAllowedCellularPolicyOverride)1454 TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedCellularPolicyOverride) {
1455   // Update over cellular can be enabled by policy.
1456 
1457   fake_state_.shill_provider()->var_conn_type()->reset(
1458       new ConnectionType(ConnectionType::kCellular));
1459   set<ConnectionType> allowed_connections;
1460   allowed_connections.insert(ConnectionType::kCellular);
1461   fake_state_.device_policy_provider()
1462       ->var_allowed_connection_types_for_update()
1463       ->reset(new set<ConnectionType>(allowed_connections));
1464 
1465   bool result;
1466   ExpectPolicyStatus(
1467       EvalStatus::kSucceeded, &Policy::UpdateDownloadAllowed, &result);
1468   EXPECT_TRUE(result);
1469 }
1470 
TEST_F(UmChromeOSPolicyTest,UpdateDownloadAllowedCellularUserOverride)1471 TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedCellularUserOverride) {
1472   // Update over cellular can be enabled by user settings, but only if policy
1473   // is present and does not determine allowed connections.
1474 
1475   fake_state_.shill_provider()->var_conn_type()->reset(
1476       new ConnectionType(ConnectionType::kCellular));
1477   set<ConnectionType> allowed_connections;
1478   allowed_connections.insert(ConnectionType::kCellular);
1479   fake_state_.updater_provider()->var_cellular_enabled()->reset(new bool(true));
1480 
1481   bool result;
1482   ExpectPolicyStatus(
1483       EvalStatus::kSucceeded, &Policy::UpdateDownloadAllowed, &result);
1484   EXPECT_TRUE(result);
1485 }
1486 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedScatteringSupressedDueToP2P)1487 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSupressedDueToP2P) {
1488   // The UpdateCanStart policy returns true; scattering should have applied, but
1489   // P2P download is allowed. Scattering values are nonetheless returned, and so
1490   // are download URL values, albeit the latter are not allowed to be used.
1491 
1492   SetUpdateCheckAllowed(false);
1493   fake_state_.device_policy_provider()->var_scatter_factor()->reset(
1494       new TimeDelta(TimeDelta::FromMinutes(2)));
1495   fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
1496 
1497   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
1498   update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
1499 
1500   UpdateDownloadParams result;
1501   ExpectPolicyStatus(
1502       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1503   EXPECT_TRUE(result.update_can_start);
1504   EXPECT_EQ(0, result.download_url_idx);
1505   EXPECT_FALSE(result.download_url_allowed);
1506   EXPECT_EQ(0, result.download_url_num_errors);
1507   EXPECT_TRUE(result.p2p_downloading_allowed);
1508   EXPECT_TRUE(result.p2p_sharing_allowed);
1509   EXPECT_FALSE(result.do_increment_failures);
1510   EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
1511   EXPECT_EQ(0, result.scatter_check_threshold);
1512 }
1513 
TEST_F(UmChromeOSPolicyTest,UpdateCanStartAllowedBackoffSupressedDueToP2P)1514 TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffSupressedDueToP2P) {
1515   // The UpdateCanStart policy returns true; backoff should have applied, but
1516   // P2P download is allowed. Backoff values are nonetheless returned, and so
1517   // are download URL values, albeit the latter are not allowed to be used.
1518 
1519   SetUpdateCheckAllowed(false);
1520 
1521   const Time curr_time = fake_clock_.GetWallclockTime();
1522   UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
1523   update_state.download_errors_max = 1;
1524   update_state.download_errors.emplace_back(
1525       0,
1526       ErrorCode::kDownloadTransferError,
1527       curr_time - TimeDelta::FromSeconds(8));
1528   update_state.download_errors.emplace_back(
1529       0,
1530       ErrorCode::kDownloadTransferError,
1531       curr_time - TimeDelta::FromSeconds(2));
1532   fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
1533 
1534   UpdateDownloadParams result;
1535   ExpectPolicyStatus(
1536       EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, update_state);
1537   EXPECT_TRUE(result.update_can_start);
1538   EXPECT_EQ(0, result.download_url_idx);
1539   EXPECT_FALSE(result.download_url_allowed);
1540   EXPECT_EQ(0, result.download_url_num_errors);
1541   EXPECT_TRUE(result.p2p_downloading_allowed);
1542   EXPECT_TRUE(result.p2p_sharing_allowed);
1543   EXPECT_TRUE(result.do_increment_failures);
1544   EXPECT_LT(curr_time, result.backoff_expiry);
1545 }
1546 
TEST_F(UmChromeOSPolicyTest,P2PEnabledNotAllowed)1547 TEST_F(UmChromeOSPolicyTest, P2PEnabledNotAllowed) {
1548   bool result;
1549   ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::P2PEnabled, &result);
1550   EXPECT_FALSE(result);
1551 }
1552 
TEST_F(UmChromeOSPolicyTest,P2PEnabledAllowedByDevicePolicy)1553 TEST_F(UmChromeOSPolicyTest, P2PEnabledAllowedByDevicePolicy) {
1554   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1555       new bool(true));
1556 
1557   bool result;
1558   ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::P2PEnabled, &result);
1559   EXPECT_TRUE(result);
1560 }
1561 
TEST_F(UmChromeOSPolicyTest,P2PEnabledAllowedByUpdater)1562 TEST_F(UmChromeOSPolicyTest, P2PEnabledAllowedByUpdater) {
1563   fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
1564 
1565   bool result;
1566   ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::P2PEnabled, &result);
1567   EXPECT_TRUE(result);
1568 }
1569 
TEST_F(UmChromeOSPolicyTest,P2PEnabledAllowedDeviceEnterpriseEnrolled)1570 TEST_F(UmChromeOSPolicyTest, P2PEnabledAllowedDeviceEnterpriseEnrolled) {
1571   fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(nullptr);
1572   fake_state_.device_policy_provider()->var_has_owner()->reset(new bool(false));
1573 
1574   bool result;
1575   ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::P2PEnabled, &result);
1576   EXPECT_TRUE(result);
1577 }
1578 
TEST_F(UmChromeOSPolicyTest,P2PEnabledChangedBlocks)1579 TEST_F(UmChromeOSPolicyTest, P2PEnabledChangedBlocks) {
1580   bool result;
1581   ExpectPolicyStatus(
1582       EvalStatus::kAskMeAgainLater, &Policy::P2PEnabledChanged, &result, false);
1583 }
1584 
TEST_F(UmChromeOSPolicyTest,UpdateCanBeAppliedForcedUpdatesDisablesTimeRestrictions)1585 TEST_F(UmChromeOSPolicyTest,
1586        UpdateCanBeAppliedForcedUpdatesDisablesTimeRestrictions) {
1587   Time curr_time = fake_clock_.GetWallclockTime();
1588   fake_state_.updater_provider()->var_forced_update_requested()->reset(
1589       new UpdateRequestStatus(UpdateRequestStatus::kInteractive));
1590   // Should return kAskMeAgainLater when updated are not forced.
1591   TestDisallowedTimeIntervals(
1592       {WeeklyTimeInterval(
1593           WeeklyTime::FromTime(curr_time),
1594           WeeklyTime::FromTime(curr_time + TimeDelta::FromMinutes(1)))},
1595       ErrorCode::kSuccess,
1596       /* kiosk = */ true);
1597 }
1598 
TEST_F(UmChromeOSPolicyTest,UpdateCanBeAppliedFailsInDisallowedTime)1599 TEST_F(UmChromeOSPolicyTest, UpdateCanBeAppliedFailsInDisallowedTime) {
1600   Time curr_time = fake_clock_.GetWallclockTime();
1601   TestDisallowedTimeIntervals(
1602       {WeeklyTimeInterval(
1603           WeeklyTime::FromTime(curr_time),
1604           WeeklyTime::FromTime(curr_time + TimeDelta::FromMinutes(1)))},
1605       ErrorCode::kOmahaUpdateDeferredPerPolicy,
1606       /* kiosk = */ true);
1607 }
1608 
TEST_F(UmChromeOSPolicyTest,UpdateCanBeAppliedOutsideDisallowedTime)1609 TEST_F(UmChromeOSPolicyTest, UpdateCanBeAppliedOutsideDisallowedTime) {
1610   Time curr_time = fake_clock_.GetWallclockTime();
1611   TestDisallowedTimeIntervals(
1612       {WeeklyTimeInterval(
1613           WeeklyTime::FromTime(curr_time - TimeDelta::FromHours(3)),
1614           WeeklyTime::FromTime(curr_time))},
1615       ErrorCode::kSuccess,
1616       /* kiosk = */ true);
1617 }
1618 
TEST_F(UmChromeOSPolicyTest,UpdateCanBeAppliedPassesOnNonKiosk)1619 TEST_F(UmChromeOSPolicyTest, UpdateCanBeAppliedPassesOnNonKiosk) {
1620   Time curr_time = fake_clock_.GetWallclockTime();
1621   TestDisallowedTimeIntervals(
1622       {WeeklyTimeInterval(
1623           WeeklyTime::FromTime(curr_time),
1624           WeeklyTime::FromTime(curr_time + TimeDelta::FromMinutes(1)))},
1625       ErrorCode::kSuccess,
1626       /* kiosk = */ false);
1627 }
1628 
1629 }  // namespace chromeos_update_manager
1630