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