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