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