blob: b99074c3421f572991aaee04266af46b0b6edf25 [file] [log] [blame]
Alex Deymo0d11c602014-04-23 20:12:20 -07001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo63784a52014-05-28 10:46:14 -07005#include "update_engine/update_manager/chromeos_policy.h"
Alex Deymo0d11c602014-04-23 20:12:20 -07006
Gilad Arnold0adbc942014-05-12 10:35:43 -07007#include <set>
Alex Deymo0d11c602014-04-23 20:12:20 -07008#include <string>
Gilad Arnolddc4bb262014-07-23 10:45:19 -07009#include <tuple>
Gilad Arnoldb3b05442014-05-30 14:25:05 -070010#include <vector>
Alex Deymo0d11c602014-04-23 20:12:20 -070011
12#include <base/time/time.h>
13#include <gtest/gtest.h>
14
15#include "update_engine/fake_clock.h"
Alex Deymo63784a52014-05-28 10:46:14 -070016#include "update_engine/update_manager/evaluation_context.h"
17#include "update_engine/update_manager/fake_state.h"
18#include "update_engine/update_manager/umtest_utils.h"
Alex Deymo0d11c602014-04-23 20:12:20 -070019
20using base::Time;
21using base::TimeDelta;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070022using chromeos_update_engine::ErrorCode;
Alex Deymo0d11c602014-04-23 20:12:20 -070023using chromeos_update_engine::FakeClock;
Gilad Arnold0adbc942014-05-12 10:35:43 -070024using std::set;
Alex Deymo0d11c602014-04-23 20:12:20 -070025using std::string;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070026using std::tuple;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070027using std::vector;
Alex Deymo0d11c602014-04-23 20:12:20 -070028
Alex Deymo63784a52014-05-28 10:46:14 -070029namespace chromeos_update_manager {
Alex Deymo0d11c602014-04-23 20:12:20 -070030
Alex Deymo63784a52014-05-28 10:46:14 -070031class UmChromeOSPolicyTest : public ::testing::Test {
Alex Deymo0d11c602014-04-23 20:12:20 -070032 protected:
33 virtual void SetUp() {
34 SetUpDefaultClock();
Gilad Arnoldb2271992014-06-19 12:35:24 -070035 eval_ctx_ = new EvaluationContext(&fake_clock_, TimeDelta::FromSeconds(5));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070036 SetUpDefaultState();
37 SetUpDefaultDevicePolicy();
Alex Deymo0d11c602014-04-23 20:12:20 -070038 }
39
40 // Sets the clock to fixed values.
41 void SetUpDefaultClock() {
42 fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
43 fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L));
44 }
45
46 void SetUpDefaultState() {
47 fake_state_.updater_provider()->var_updater_started_time()->reset(
48 new Time(fake_clock_.GetWallclockTime()));
49 fake_state_.updater_provider()->var_last_checked_time()->reset(
50 new Time(fake_clock_.GetWallclockTime()));
51 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -070052 reset(new unsigned int{0});
Gilad Arnolda0258a52014-07-10 16:21:19 -070053 fake_state_.updater_provider()->var_server_dictated_poll_interval()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -070054 reset(new unsigned int{0});
55 fake_state_.updater_provider()->var_forced_update_requested()->
56 reset(new UpdateRequestStatus{UpdateRequestStatus::kNone});
Alex Deymo0d11c602014-04-23 20:12:20 -070057
58 fake_state_.random_provider()->var_seed()->reset(
59 new uint64_t(4)); // chosen by fair dice roll.
60 // guaranteed to be random.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070061
62 // No device policy loaded by default.
63 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
64 new bool(false));
65
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070066 // OOBE is enabled by default.
67 fake_state_.config_provider()->var_is_oobe_enabled()->reset(
68 new bool(true));
69
Gilad Arnold76a11f62014-05-20 09:02:12 -070070 // For the purpose of the tests, this is an official build and OOBE was
71 // completed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070072 fake_state_.system_provider()->var_is_official_build()->reset(
73 new bool(true));
Gilad Arnold76a11f62014-05-20 09:02:12 -070074 fake_state_.system_provider()->var_is_oobe_complete()->reset(
75 new bool(true));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -070076 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
77 new bool(false));
Gilad Arnold0adbc942014-05-12 10:35:43 -070078
79 // Connection is wifi, untethered.
80 fake_state_.shill_provider()->var_conn_type()->
81 reset(new ConnectionType(ConnectionType::kWifi));
82 fake_state_.shill_provider()->var_conn_tethering()->
83 reset(new ConnectionTethering(ConnectionTethering::kNotDetected));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070084 }
85
Gilad Arnoldb3b05442014-05-30 14:25:05 -070086 // Sets up a default device policy that does not impose any restrictions
87 // (HTTP) nor enables any features (P2P).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070088 void SetUpDefaultDevicePolicy() {
89 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
90 new bool(true));
91 fake_state_.device_policy_provider()->var_update_disabled()->reset(
92 new bool(false));
93 fake_state_.device_policy_provider()->
94 var_allowed_connection_types_for_update()->reset(nullptr);
95 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
96 new TimeDelta());
97 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
Gilad Arnoldb3b05442014-05-30 14:25:05 -070098 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070099 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
100 new bool(false));
101 fake_state_.device_policy_provider()->var_release_channel_delegated()->
102 reset(new bool(true));
103 }
104
105 // Configures the UpdateCheckAllowed policy to return a desired value by
106 // faking the current wall clock time as needed. Restores the default state.
107 // This is used when testing policies that depend on this one.
108 void SetUpdateCheckAllowed(bool allow_check) {
109 Time next_update_check;
110 ExpectPolicyStatus(EvalStatus::kSucceeded,
111 &ChromeOSPolicy::NextUpdateCheckTime,
112 &next_update_check);
113 SetUpDefaultState();
114 SetUpDefaultDevicePolicy();
115 Time curr_time = next_update_check;
116 if (allow_check)
117 curr_time += TimeDelta::FromSeconds(1);
118 else
119 curr_time -= TimeDelta::FromSeconds(1);
120 fake_clock_.SetWallclockTime(curr_time);
121 }
122
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700123 // Returns a default UpdateState structure:
124 UpdateState GetDefaultUpdateState(TimeDelta first_seen_period) {
125 Time first_seen_time = fake_clock_.GetWallclockTime() - first_seen_period;
126 UpdateState update_state = UpdateState();
127
128 // This is a non-interactive check returning a delta payload, seen for the
129 // first time (|first_seen_period| ago). Clearly, there were no failed
130 // attempts so far.
131 update_state.is_interactive = false;
132 update_state.is_delta_payload = false;
133 update_state.first_seen = first_seen_time;
134 update_state.num_checks = 1;
135 update_state.num_failures = 0;
136 update_state.failures_last_updated = Time(); // Needs to be zero.
137 // There's a single HTTP download URL with a maximum of 10 retries.
138 update_state.download_urls = vector<string>{"http://fake/url/"};
139 update_state.download_errors_max = 10;
140 // Download was never attempted.
141 update_state.last_download_url_idx = -1;
142 update_state.last_download_url_num_errors = 0;
143 // There were no download errors.
144 update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
Gilad Arnold349ac832014-10-06 14:20:28 -0700145 // P2P was not attempted.
146 update_state.p2p_num_attempts = 0;
147 update_state.p2p_first_attempted = Time();
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700148 // No active backoff period, backoff is not disabled by Omaha.
149 update_state.backoff_expiry = Time();
150 update_state.is_backoff_disabled = false;
151 // There is no active scattering wait period (max 7 days allowed) nor check
152 // threshold (none allowed).
153 update_state.scatter_wait_period = TimeDelta();
154 update_state.scatter_check_threshold = 0;
155 update_state.scatter_wait_period_max = TimeDelta::FromDays(7);
156 update_state.scatter_check_threshold_min = 0;
157 update_state.scatter_check_threshold_max = 0;
158
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700159 return update_state;
Alex Deymo0d11c602014-04-23 20:12:20 -0700160 }
161
162 // Runs the passed |policy_method| policy and expects it to return the
163 // |expected| return value.
164 template<typename T, typename R, typename... Args>
165 void ExpectPolicyStatus(
166 EvalStatus expected,
167 T policy_method,
168 R* result, Args... args) {
169 string error = "<None>";
170 eval_ctx_->ResetEvaluation();
171 EXPECT_EQ(expected,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700172 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result,
173 args...))
Alex Deymoc850b4f2014-05-19 11:06:41 -0700174 << "Returned error: " << error
175 << "\nEvaluation context: " << eval_ctx_->DumpContext();
Alex Deymo0d11c602014-04-23 20:12:20 -0700176 }
177
178 FakeClock fake_clock_;
179 FakeState fake_state_;
180 scoped_refptr<EvaluationContext> eval_ctx_;
181 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
182};
183
Alex Deymo63784a52014-05-28 10:46:14 -0700184TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700185 Time next_update_check;
186
Gilad Arnold38b14022014-07-09 12:45:56 -0700187 // Set the last update time so it'll appear as if this is a first update check
188 // in the lifetime of the current updater.
189 fake_state_.updater_provider()->var_last_checked_time()->reset(
190 new Time(fake_clock_.GetWallclockTime() - TimeDelta::FromMinutes(10)));
191
Alex Deymo0d11c602014-04-23 20:12:20 -0700192 ExpectPolicyStatus(EvalStatus::kSucceeded,
193 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
194
195 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
Gilad Arnold38b14022014-07-09 12:45:56 -0700196 EXPECT_GE(
197 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
198 ChromeOSPolicy::kTimeoutInitialInterval +
199 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
200 next_update_check);
201}
202
203TEST_F(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz) {
204 // Ensure that we're using the correct interval (kPeriodicInterval) and fuzz
205 // (kTimeoutRegularFuzz) as base values for period updates.
206 Time next_update_check;
207
208 ExpectPolicyStatus(EvalStatus::kSucceeded,
209 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
210
211 EXPECT_LE(
212 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
213 ChromeOSPolicy::kTimeoutPeriodicInterval -
214 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
215 next_update_check);
216 EXPECT_GE(
217 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
218 ChromeOSPolicy::kTimeoutPeriodicInterval +
219 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
220 next_update_check);
221}
222
223TEST_F(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz) {
224 // Ensure that we're properly backing off and fuzzing in the presence of
225 // failed updates attempts.
226 Time next_update_check;
227
228 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700229 reset(new unsigned int{2});
Gilad Arnold38b14022014-07-09 12:45:56 -0700230
231 ExpectPolicyStatus(EvalStatus::kSucceeded,
232 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
233
234 int expected_interval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
235 EXPECT_LE(
236 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
237 expected_interval - expected_interval / 2),
238 next_update_check);
239 EXPECT_GE(
240 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
241 expected_interval + expected_interval / 2),
242 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700243}
244
Gilad Arnolda0258a52014-07-10 16:21:19 -0700245TEST_F(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval) {
246 // Policy honors the server provided check poll interval.
247 Time next_update_check;
248
249 const unsigned int kInterval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
250 fake_state_.updater_provider()->var_server_dictated_poll_interval()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700251 reset(new unsigned int{kInterval});
Gilad Arnolda0258a52014-07-10 16:21:19 -0700252 // We should not be backing off in this case.
253 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700254 reset(new unsigned int{2});
Gilad Arnolda0258a52014-07-10 16:21:19 -0700255
256 ExpectPolicyStatus(EvalStatus::kSucceeded,
257 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
258
259 EXPECT_LE(
260 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
261 kInterval - kInterval / 2),
262 next_update_check);
263 EXPECT_GE(
264 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
265 kInterval + kInterval / 2),
266 next_update_check);
267}
268
Alex Deymo63784a52014-05-28 10:46:14 -0700269TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700270 Time next_update_check;
271
Alex Deymo0d11c602014-04-23 20:12:20 -0700272 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700273 reset(new unsigned int{100});
Gilad Arnold38b14022014-07-09 12:45:56 -0700274
Alex Deymo0d11c602014-04-23 20:12:20 -0700275 ExpectPolicyStatus(EvalStatus::kSucceeded,
276 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
277
Gilad Arnold38b14022014-07-09 12:45:56 -0700278 EXPECT_LE(
279 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
280 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
281 ChromeOSPolicy::kTimeoutMaxBackoffInterval / 2),
282 next_update_check);
283 EXPECT_GE(
284 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
285 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
286 ChromeOSPolicy::kTimeoutMaxBackoffInterval /2),
287 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700288}
289
Alex Deymo63784a52014-05-28 10:46:14 -0700290TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700291 // We get the next update_check timestamp from the policy's private method
292 // and then we check the public method respects that value on the normal
293 // case.
294 Time next_update_check;
295 Time last_checked_time =
296 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
297
Alex Deymo0d11c602014-04-23 20:12:20 -0700298 fake_state_.updater_provider()->var_last_checked_time()->reset(
299 new Time(last_checked_time));
300 ExpectPolicyStatus(EvalStatus::kSucceeded,
301 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
302
303 UpdateCheckParams result;
304
305 // Check that the policy blocks until the next_update_check is reached.
306 SetUpDefaultClock();
307 SetUpDefaultState();
308 fake_state_.updater_provider()->var_last_checked_time()->reset(
309 new Time(last_checked_time));
310 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
311 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
312 &Policy::UpdateCheckAllowed, &result);
313
314 SetUpDefaultClock();
315 SetUpDefaultState();
316 fake_state_.updater_provider()->var_last_checked_time()->reset(
317 new Time(last_checked_time));
318 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
319 ExpectPolicyStatus(EvalStatus::kSucceeded,
320 &Policy::UpdateCheckAllowed, &result);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700321 EXPECT_TRUE(result.updates_enabled);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700322 EXPECT_FALSE(result.is_interactive);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700323}
324
325TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700326 // Update checks are deferred until OOBE is completed.
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700327
328 // Ensure that update is not allowed even if wait period is satisfied.
329 Time next_update_check;
330 Time last_checked_time =
331 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
332
333 fake_state_.updater_provider()->var_last_checked_time()->reset(
334 new Time(last_checked_time));
335 ExpectPolicyStatus(EvalStatus::kSucceeded,
336 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
337
338 SetUpDefaultClock();
339 SetUpDefaultState();
340 fake_state_.updater_provider()->var_last_checked_time()->reset(
341 new Time(last_checked_time));
342 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
343 fake_state_.system_provider()->var_is_oobe_complete()->reset(
344 new bool(false));
345
346 UpdateCheckParams result;
347 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
348 &Policy::UpdateCheckAllowed, &result);
349
350 // Now check that it is allowed if OOBE is completed.
351 SetUpDefaultClock();
352 SetUpDefaultState();
353 fake_state_.updater_provider()->var_last_checked_time()->reset(
354 new Time(last_checked_time));
355 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
356 ExpectPolicyStatus(EvalStatus::kSucceeded,
357 &Policy::UpdateCheckAllowed, &result);
358 EXPECT_TRUE(result.updates_enabled);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700359 EXPECT_FALSE(result.is_interactive);
Alex Deymo0d11c602014-04-23 20:12:20 -0700360}
361
Gilad Arnold42f253b2014-06-25 12:39:17 -0700362TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700363 // Update check is allowed, response includes attributes for use in the
Gilad Arnold42f253b2014-06-25 12:39:17 -0700364 // request.
365 SetUpdateCheckAllowed(true);
366
367 // Override specific device policy attributes.
Gilad Arnoldd4b30322014-07-21 15:35:27 -0700368 fake_state_.device_policy_provider()->var_target_version_prefix()->
369 reset(new string("1.2"));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700370 fake_state_.device_policy_provider()->var_release_channel_delegated()->
371 reset(new bool(false));
372 fake_state_.device_policy_provider()->var_release_channel()->
373 reset(new string("foo-channel"));
374
375 UpdateCheckParams result;
376 ExpectPolicyStatus(EvalStatus::kSucceeded,
377 &Policy::UpdateCheckAllowed, &result);
378 EXPECT_TRUE(result.updates_enabled);
Gilad Arnoldd4b30322014-07-21 15:35:27 -0700379 EXPECT_EQ("1.2", result.target_version_prefix);
Gilad Arnold42f253b2014-06-25 12:39:17 -0700380 EXPECT_EQ("foo-channel", result.target_channel);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700381 EXPECT_FALSE(result.is_interactive);
Gilad Arnold42f253b2014-06-25 12:39:17 -0700382}
383
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700384TEST_F(UmChromeOSPolicyTest,
385 UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700386 // UpdateCheckAllowed should return kAskMeAgainLater if this is an unofficial
387 // build; we don't want periodic update checks on developer images.
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700388
389 fake_state_.system_provider()->var_is_official_build()->reset(
390 new bool(false));
391
392 UpdateCheckParams result;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700393 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700394 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700395}
396
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700397TEST_F(UmChromeOSPolicyTest,
398 UpdateCheckAllowedUpdatesDisabledForRemovableBootDevice) {
399 // UpdateCheckAllowed should return false (kSucceeded) if the image booted
400 // from a removable device.
401
402 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
403 new bool(true));
404
405 UpdateCheckParams result;
406 ExpectPolicyStatus(EvalStatus::kSucceeded,
407 &Policy::UpdateCheckAllowed, &result);
408 EXPECT_FALSE(result.updates_enabled);
409}
410
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700411TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700412 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
413 // is loaded and prohibits updates.
414
415 SetUpdateCheckAllowed(false);
416 fake_state_.device_policy_provider()->var_update_disabled()->reset(
417 new bool(true));
418
Gilad Arnold42f253b2014-06-25 12:39:17 -0700419 UpdateCheckParams result;
420 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
421 &Policy::UpdateCheckAllowed, &result);
422}
423
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700424TEST_F(UmChromeOSPolicyTest,
425 UpdateCheckAllowedForcedUpdateRequestedInteractive) {
426 // UpdateCheckAllowed should return true because a forced update request was
427 // signaled for an interactive update.
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700428
429 SetUpdateCheckAllowed(true);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700430 fake_state_.updater_provider()->var_forced_update_requested()->reset(
431 new UpdateRequestStatus(UpdateRequestStatus::kInteractive));
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700432
433 UpdateCheckParams result;
434 ExpectPolicyStatus(EvalStatus::kSucceeded,
435 &Policy::UpdateCheckAllowed, &result);
436 EXPECT_TRUE(result.updates_enabled);
437 EXPECT_TRUE(result.is_interactive);
438}
439
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700440TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedForcedUpdateRequestedPeriodic) {
441 // UpdateCheckAllowed should return true because a forced update request was
442 // signaled for a periodic check.
443
444 SetUpdateCheckAllowed(true);
445 fake_state_.updater_provider()->var_forced_update_requested()->reset(
446 new UpdateRequestStatus(UpdateRequestStatus::kPeriodic));
447
448 UpdateCheckParams result;
449 ExpectPolicyStatus(EvalStatus::kSucceeded,
450 &Policy::UpdateCheckAllowed, &result);
451 EXPECT_TRUE(result.updates_enabled);
452 EXPECT_FALSE(result.is_interactive);
453}
454
Alex Deymo63784a52014-05-28 10:46:14 -0700455TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700456 // The UpdateCanStart policy fails, not being able to query
457 // UpdateCheckAllowed.
458
459 // Configure the UpdateCheckAllowed policy to fail.
460 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
461
462 // Check that the UpdateCanStart fails.
463 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700464 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700465 ExpectPolicyStatus(EvalStatus::kFailed,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700466 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700467}
468
Alex Deymo63784a52014-05-28 10:46:14 -0700469TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700470 // The UpdateCanStart policy returns false because we are due for another
471 // update check.
472
473 SetUpdateCheckAllowed(true);
474
475 // Check that the UpdateCanStart returns false.
476 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700477 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700478 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700479 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700480 EXPECT_FALSE(result.update_can_start);
481 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
482}
483
Alex Deymo63784a52014-05-28 10:46:14 -0700484TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700485 // The UpdateCanStart policy returns true; no device policy is loaded.
486
487 SetUpdateCheckAllowed(false);
488 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
489 new bool(false));
490
491 // Check that the UpdateCanStart returns true with no further attributes.
492 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700493 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700494 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700495 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700496 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700497 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700498 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700499 EXPECT_EQ(0, result.download_url_num_errors);
500 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700501}
502
Alex Deymo63784a52014-05-28 10:46:14 -0700503TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700504 // The UpdateCanStart policy returns true; device policy is loaded but imposes
505 // no restrictions on updating.
506
507 SetUpdateCheckAllowed(false);
508
509 // Check that the UpdateCanStart returns true.
510 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700511 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700512 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700513 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700514 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700515 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700516 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700517 EXPECT_EQ(0, result.download_url_num_errors);
518 EXPECT_FALSE(result.do_increment_failures);
519}
520
521TEST_F(UmChromeOSPolicyTest,
522 UpdateCanStartNotAllowedBackoffNewWaitPeriodApplies) {
523 // The UpdateCanStart policy returns false; failures are reported and a new
524 // backoff period is enacted.
525
526 SetUpdateCheckAllowed(false);
527
528 const Time curr_time = fake_clock_.GetWallclockTime();
529 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
530 update_state.download_errors_max = 1;
531 update_state.download_errors.emplace_back(
532 0, ErrorCode::kDownloadTransferError,
533 curr_time - TimeDelta::FromSeconds(8));
534 update_state.download_errors.emplace_back(
535 0, ErrorCode::kDownloadTransferError,
536 curr_time - TimeDelta::FromSeconds(2));
537
538 // Check that UpdateCanStart returns false and a new backoff expiry is
539 // generated.
540 UpdateDownloadParams result;
541 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
542 update_state);
543 EXPECT_FALSE(result.update_can_start);
544 EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
545 EXPECT_TRUE(result.do_increment_failures);
546 EXPECT_LT(curr_time, result.backoff_expiry);
547}
548
549TEST_F(UmChromeOSPolicyTest,
550 UpdateCanStartNotAllowedBackoffPrevWaitPeriodStillApplies) {
551 // The UpdateCanStart policy returns false; a previously enacted backoff
552 // period still applies.
553
554 SetUpdateCheckAllowed(false);
555
556 const Time curr_time = fake_clock_.GetWallclockTime();
557 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
558 update_state.download_errors_max = 1;
559 update_state.download_errors.emplace_back(
560 0, ErrorCode::kDownloadTransferError,
561 curr_time - TimeDelta::FromSeconds(8));
562 update_state.download_errors.emplace_back(
563 0, ErrorCode::kDownloadTransferError,
564 curr_time - TimeDelta::FromSeconds(2));
565 update_state.failures_last_updated = curr_time;
566 update_state.backoff_expiry = curr_time + TimeDelta::FromMinutes(3);
567
568 // Check that UpdateCanStart returns false and a new backoff expiry is
569 // generated.
570 UpdateDownloadParams result;
571 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
572 &result, update_state);
573 EXPECT_FALSE(result.update_can_start);
574 EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
575 EXPECT_FALSE(result.do_increment_failures);
576 EXPECT_LT(curr_time, result.backoff_expiry);
577}
578
579TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffSatisfied) {
580 // The UpdateCanStart policy returns true; a previously enacted backoff period
581 // has elapsed, we're good to go.
582
583 SetUpdateCheckAllowed(false);
584
585 const Time curr_time = fake_clock_.GetWallclockTime();
586 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
587 update_state.download_errors_max = 1;
588 update_state.download_errors.emplace_back(
589 0, ErrorCode::kDownloadTransferError,
590 curr_time - TimeDelta::FromSeconds(8));
591 update_state.download_errors.emplace_back(
592 0, ErrorCode::kDownloadTransferError,
593 curr_time - TimeDelta::FromSeconds(2));
594 update_state.failures_last_updated = curr_time - TimeDelta::FromSeconds(1);
595 update_state.backoff_expiry = curr_time - TimeDelta::FromSeconds(1);
596
597 // Check that UpdateCanStart returns false and a new backoff expiry is
598 // generated.
599 UpdateDownloadParams result;
600 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart,
601 &result, update_state);
602 EXPECT_TRUE(result.update_can_start);
603 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
604 EXPECT_EQ(0, result.download_url_idx);
605 EXPECT_EQ(0, result.download_url_num_errors);
606 EXPECT_FALSE(result.do_increment_failures);
607 EXPECT_EQ(Time(), result.backoff_expiry);
608}
609
610TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffDisabled) {
611 // The UpdateCanStart policy returns false; failures are reported but backoff
612 // is disabled.
613
614 SetUpdateCheckAllowed(false);
615
616 const Time curr_time = fake_clock_.GetWallclockTime();
617 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
618 update_state.download_errors_max = 1;
619 update_state.download_errors.emplace_back(
620 0, ErrorCode::kDownloadTransferError,
621 curr_time - TimeDelta::FromSeconds(8));
622 update_state.download_errors.emplace_back(
623 0, ErrorCode::kDownloadTransferError,
624 curr_time - TimeDelta::FromSeconds(2));
625 update_state.is_backoff_disabled = true;
626
627 // Check that UpdateCanStart returns false and a new backoff expiry is
628 // generated.
629 UpdateDownloadParams result;
630 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
631 update_state);
632 EXPECT_TRUE(result.update_can_start);
633 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
634 EXPECT_EQ(0, result.download_url_idx);
635 EXPECT_EQ(0, result.download_url_num_errors);
636 EXPECT_TRUE(result.do_increment_failures);
637 EXPECT_EQ(Time(), result.backoff_expiry);
638}
639
640TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffInteractive) {
641 // The UpdateCanStart policy returns false; failures are reported but this is
642 // an interactive update check.
643
644 SetUpdateCheckAllowed(false);
645
646 const Time curr_time = fake_clock_.GetWallclockTime();
647 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
648 update_state.download_errors_max = 1;
649 update_state.download_errors.emplace_back(
650 0, ErrorCode::kDownloadTransferError,
651 curr_time - TimeDelta::FromSeconds(8));
652 update_state.download_errors.emplace_back(
653 0, ErrorCode::kDownloadTransferError,
654 curr_time - TimeDelta::FromSeconds(2));
655 update_state.is_interactive = true;
656
657 // Check that UpdateCanStart returns false and a new backoff expiry is
658 // generated.
659 UpdateDownloadParams result;
660 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
661 update_state);
662 EXPECT_TRUE(result.update_can_start);
663 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
664 EXPECT_EQ(0, result.download_url_idx);
665 EXPECT_EQ(0, result.download_url_num_errors);
666 EXPECT_TRUE(result.do_increment_failures);
667 EXPECT_EQ(Time(), result.backoff_expiry);
668}
669
670TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffDelta) {
671 // The UpdateCanStart policy returns false; failures are reported but this is
672 // a delta payload.
673
674 SetUpdateCheckAllowed(false);
675
676 const Time curr_time = fake_clock_.GetWallclockTime();
677 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
678 update_state.download_errors_max = 1;
679 update_state.download_errors.emplace_back(
680 0, ErrorCode::kDownloadTransferError,
681 curr_time - TimeDelta::FromSeconds(8));
682 update_state.download_errors.emplace_back(
683 0, ErrorCode::kDownloadTransferError,
684 curr_time - TimeDelta::FromSeconds(2));
685 update_state.is_delta_payload = true;
686
687 // Check that UpdateCanStart returns false and a new backoff expiry is
688 // generated.
689 UpdateDownloadParams result;
690 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
691 update_state);
692 EXPECT_TRUE(result.update_can_start);
693 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
694 EXPECT_EQ(0, result.download_url_idx);
695 EXPECT_EQ(0, result.download_url_num_errors);
696 EXPECT_TRUE(result.do_increment_failures);
697 EXPECT_EQ(Time(), result.backoff_expiry);
698}
699
700TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffUnofficialBuild) {
701 // The UpdateCanStart policy returns false; failures are reported but this is
702 // an unofficial build.
703
704 SetUpdateCheckAllowed(false);
705
706 const Time curr_time = fake_clock_.GetWallclockTime();
707 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
708 update_state.download_errors_max = 1;
709 update_state.download_errors.emplace_back(
710 0, ErrorCode::kDownloadTransferError,
711 curr_time - TimeDelta::FromSeconds(8));
712 update_state.download_errors.emplace_back(
713 0, ErrorCode::kDownloadTransferError,
714 curr_time - TimeDelta::FromSeconds(2));
715
716 fake_state_.system_provider()->var_is_official_build()->
717 reset(new bool(false));
718
719 // Check that UpdateCanStart returns false and a new backoff expiry is
720 // generated.
721 UpdateDownloadParams result;
722 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
723 update_state);
724 EXPECT_TRUE(result.update_can_start);
725 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
726 EXPECT_EQ(0, result.download_url_idx);
727 EXPECT_EQ(0, result.download_url_num_errors);
728 EXPECT_TRUE(result.do_increment_failures);
729 EXPECT_EQ(Time(), result.backoff_expiry);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700730}
731
Alex Deymo63784a52014-05-28 10:46:14 -0700732TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700733 // The UpdateCanStart policy fails because the UpdateScattering policy it
734 // depends on fails (unset variable).
735
736 SetUpdateCheckAllowed(false);
737
738 // Override the default seed variable with a null value so that the policy
739 // request would fail.
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700740 // TODO(garnold) This failure may or may not fail a number
741 // sub-policies/decisions, like scattering and backoff. We'll need a more
742 // deliberate setup to ensure that we're failing what we want to be failing.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700743 fake_state_.random_provider()->var_seed()->reset(nullptr);
744
745 // Check that the UpdateCanStart fails.
746 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700747 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700748 ExpectPolicyStatus(EvalStatus::kFailed,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700749 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700750}
751
Alex Deymo63784a52014-05-28 10:46:14 -0700752TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700753 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
754 // The UpdateCanStart policy returns false; device policy is loaded and
755 // scattering applies due to an unsatisfied wait period, which was newly
756 // generated.
757
758 SetUpdateCheckAllowed(false);
759 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
760 new TimeDelta(TimeDelta::FromMinutes(2)));
761
762
763 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
764
765 // Check that the UpdateCanStart returns false and a new wait period
766 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700767 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700768 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700769 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700770 EXPECT_FALSE(result.update_can_start);
771 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
772 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
773 EXPECT_EQ(0, result.scatter_check_threshold);
774}
775
Alex Deymo63784a52014-05-28 10:46:14 -0700776TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700777 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
778 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
779 // is loaded and a previously generated scattering period still applies, none
780 // of the scattering values has changed.
781
782 SetUpdateCheckAllowed(false);
783 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
784 new TimeDelta(TimeDelta::FromMinutes(2)));
785
786 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
787 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
788
789 // Check that the UpdateCanStart returns false and a new wait period
790 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700791 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700792 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700793 &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700794 EXPECT_FALSE(result.update_can_start);
795 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
796 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
797 EXPECT_EQ(0, result.scatter_check_threshold);
798}
799
Alex Deymo63784a52014-05-28 10:46:14 -0700800TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700801 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
802 // The UpdateCanStart policy returns false; device policy is loaded and
803 // scattering applies due to an unsatisfied update check count threshold.
804 //
805 // This ensures a non-zero check threshold, which may or may not be combined
806 // with a non-zero wait period (for which we cannot reliably control).
807
808 SetUpdateCheckAllowed(false);
809 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
810 new TimeDelta(TimeDelta::FromSeconds(1)));
811
812 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
813 update_state.scatter_check_threshold_min = 2;
814 update_state.scatter_check_threshold_max = 5;
815
816 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700817 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700818 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700819 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700820 EXPECT_FALSE(result.update_can_start);
821 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
822 EXPECT_LE(2, result.scatter_check_threshold);
823 EXPECT_GE(5, result.scatter_check_threshold);
824}
825
Alex Deymo63784a52014-05-28 10:46:14 -0700826TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700827 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
828 // The UpdateCanStart policy returns false; device policy is loaded and
829 // scattering due to a previously generated count threshold still applies.
830
831 SetUpdateCheckAllowed(false);
832 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
833 new TimeDelta(TimeDelta::FromSeconds(1)));
834
835 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
836 update_state.scatter_check_threshold = 3;
837 update_state.scatter_check_threshold_min = 2;
838 update_state.scatter_check_threshold_max = 5;
839
840 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700841 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700842 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700843 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700844 EXPECT_FALSE(result.update_can_start);
845 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
846 EXPECT_EQ(3, result.scatter_check_threshold);
847}
848
Alex Deymo63784a52014-05-28 10:46:14 -0700849TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700850 // The UpdateCanStart policy returns true; device policy is loaded and
851 // scattering is enabled, but both wait period and check threshold are
852 // satisfied.
853
854 SetUpdateCheckAllowed(false);
855 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
856 new TimeDelta(TimeDelta::FromSeconds(120)));
857
858 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
859 update_state.num_checks = 4;
860 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
861 update_state.scatter_check_threshold = 3;
862 update_state.scatter_check_threshold_min = 2;
863 update_state.scatter_check_threshold_max = 5;
864
865 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700866 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700867 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700868 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700869 EXPECT_TRUE(result.update_can_start);
870 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
871 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700872 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700873 EXPECT_EQ(0, result.download_url_num_errors);
874 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700875}
876
Alex Deymo63784a52014-05-28 10:46:14 -0700877TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700878 UpdateCanStartAllowedInteractivePreventsScattering) {
879 // The UpdateCanStart policy returns true; device policy is loaded and
880 // scattering would have applied, except that the update check is interactive
881 // and so it is suppressed.
882
883 SetUpdateCheckAllowed(false);
884 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
885 new TimeDelta(TimeDelta::FromSeconds(1)));
886
887 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700888 update_state.is_interactive = true;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700889 update_state.scatter_check_threshold = 0;
890 update_state.scatter_check_threshold_min = 2;
891 update_state.scatter_check_threshold_max = 5;
892
893 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700894 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700895 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700896 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700897 EXPECT_TRUE(result.update_can_start);
898 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
899 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700900 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700901 EXPECT_EQ(0, result.download_url_num_errors);
902 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700903}
904
Alex Deymo63784a52014-05-28 10:46:14 -0700905TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700906 UpdateCanStartAllowedOobePreventsScattering) {
907 // The UpdateCanStart policy returns true; device policy is loaded and
908 // scattering would have applied, except that OOBE was not completed and so it
909 // is suppressed.
910
911 SetUpdateCheckAllowed(false);
912 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
913 new TimeDelta(TimeDelta::FromSeconds(1)));
914 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
915
916 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700917 update_state.is_interactive = true;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700918 update_state.scatter_check_threshold = 0;
919 update_state.scatter_check_threshold_min = 2;
920 update_state.scatter_check_threshold_max = 5;
921
922 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700923 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700924 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700925 update_state);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700926 EXPECT_TRUE(result.update_can_start);
927 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
928 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700929 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700930 EXPECT_EQ(0, result.download_url_num_errors);
931 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700932}
933
Alex Deymo63784a52014-05-28 10:46:14 -0700934TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700935 // The UpdateCanStart policy returns true; device policy permits both HTTP and
936 // P2P updates, as well as a non-empty target channel string.
937
938 SetUpdateCheckAllowed(false);
939
940 // Override specific device policy attributes.
941 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
942 new bool(true));
943 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
944 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700945
946 // Check that the UpdateCanStart returns true.
947 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700948 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700949 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700950 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700951 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700952 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700953 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700954 EXPECT_EQ(0, result.download_url_num_errors);
955 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700956}
957
Alex Deymo63784a52014-05-28 10:46:14 -0700958TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700959 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
960 // P2P updates, but the updater is configured to allow P2P and overrules the
961 // setting.
962
963 SetUpdateCheckAllowed(false);
964
965 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700966 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
967
968 // Check that the UpdateCanStart returns true.
969 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700970 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700971 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700972 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700973 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700974 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700975 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700976 EXPECT_EQ(0, result.download_url_num_errors);
977 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700978}
979
Gilad Arnold349ac832014-10-06 14:20:28 -0700980TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedP2PBlockedDueToNumAttempts) {
981 // The UpdateCanStart policy returns true; device policy permits HTTP but
982 // blocks P2P, because the max number of P2P downloads have been attempted.
983
984 SetUpdateCheckAllowed(false);
985
986 // Override specific device policy attributes.
987 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
988 new bool(true));
989 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
990 new bool(true));
991
992 // Check that the UpdateCanStart returns true.
993 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
994 update_state.p2p_num_attempts = ChromeOSPolicy::kMaxP2PAttempts;
995 UpdateDownloadParams result;
996 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
997 update_state);
998 EXPECT_TRUE(result.update_can_start);
999 EXPECT_FALSE(result.p2p_allowed);
1000}
1001
1002TEST_F(UmChromeOSPolicyTest,
1003 UpdateCanStartAllowedP2PBlockedDueToAttemptsPeriod) {
1004 // The UpdateCanStart policy returns true; device policy permits HTTP but
1005 // blocks P2P, because the max period for attempt to download via P2P has
1006 // elapsed.
1007
1008 SetUpdateCheckAllowed(false);
1009
1010 // Override specific device policy attributes.
1011 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1012 new bool(true));
1013 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1014 new bool(true));
1015
1016 // Check that the UpdateCanStart returns true.
1017 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1018 update_state.p2p_num_attempts = 1;
1019 update_state.p2p_first_attempted =
1020 fake_clock_.GetWallclockTime() -
Alex Deymof329b932014-10-30 01:37:48 -07001021 TimeDelta::FromSeconds(
1022 ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds + 1);
Gilad Arnold349ac832014-10-06 14:20:28 -07001023 UpdateDownloadParams result;
1024 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1025 update_state);
1026 EXPECT_TRUE(result.update_can_start);
1027 EXPECT_FALSE(result.p2p_allowed);
1028}
1029
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001030TEST_F(UmChromeOSPolicyTest,
1031 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001032 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1033 // P2P updates, but marking this an unofficial build overrules the HTTP
1034 // setting.
1035
1036 SetUpdateCheckAllowed(false);
1037
1038 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001039 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1040 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001041 fake_state_.system_provider()->var_is_official_build()->
1042 reset(new bool(false));
1043
1044 // Check that the UpdateCanStart returns true.
1045 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -07001046 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001047 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001048 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001049 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001050 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001051 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001052 EXPECT_EQ(0, result.download_url_num_errors);
1053 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001054}
1055
1056TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
1057 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1058 // P2P updates, but an HTTPS URL is provided and selected for download.
1059
1060 SetUpdateCheckAllowed(false);
1061
1062 // Override specific device policy attributes.
1063 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1064 new bool(false));
1065
1066 // Add an HTTPS URL.
1067 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001068 update_state.download_urls.emplace_back("https://secure/url/");
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001069
1070 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -07001071 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001072 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001073 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001074 EXPECT_TRUE(result.update_can_start);
1075 EXPECT_FALSE(result.p2p_allowed);
1076 EXPECT_EQ(1, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001077 EXPECT_EQ(0, result.download_url_num_errors);
1078 EXPECT_FALSE(result.do_increment_failures);
1079}
1080
1081TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedMaxErrorsNotExceeded) {
1082 // The UpdateCanStart policy returns true; the first URL has download errors
1083 // but does not exceed the maximum allowed number of failures, so it is stilli
1084 // usable.
1085
1086 SetUpdateCheckAllowed(false);
1087
1088 // Add a second URL; update with this URL attempted and failed enough times to
1089 // disqualify the current (first) URL.
1090 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1091 update_state.num_checks = 5;
1092 update_state.download_urls.emplace_back("http://another/fake/url/");
1093 Time t = fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(12);
1094 for (int i = 0; i < 5; i++) {
1095 update_state.download_errors.emplace_back(
1096 0, ErrorCode::kDownloadTransferError, t);
1097 t += TimeDelta::FromSeconds(1);
1098 }
1099
1100 // Check that the UpdateCanStart returns true.
1101 UpdateDownloadParams result;
1102 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1103 update_state);
1104 EXPECT_TRUE(result.update_can_start);
1105 EXPECT_FALSE(result.p2p_allowed);
1106 EXPECT_EQ(0, result.download_url_idx);
1107 EXPECT_EQ(5, result.download_url_num_errors);
1108 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001109}
1110
1111TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
1112 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
1113 // allowed number of failures, but a second URL is available.
1114
1115 SetUpdateCheckAllowed(false);
1116
1117 // Add a second URL; update with this URL attempted and failed enough times to
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001118 // disqualify the current (first) URL.
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001119 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1120 update_state.num_checks = 10;
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001121 update_state.download_urls.emplace_back("http://another/fake/url/");
1122 Time t = fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(12);
1123 for (int i = 0; i < 11; i++) {
1124 update_state.download_errors.emplace_back(
1125 0, ErrorCode::kDownloadTransferError, t);
1126 t += TimeDelta::FromSeconds(1);
1127 }
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001128
1129 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -07001130 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001131 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001132 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001133 EXPECT_TRUE(result.update_can_start);
1134 EXPECT_FALSE(result.p2p_allowed);
1135 EXPECT_EQ(1, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001136 EXPECT_EQ(0, result.download_url_num_errors);
1137 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001138}
1139
1140TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
1141 // The UpdateCanStart policy returns true; the first URL fails with a hard
1142 // error, but a second URL is available.
1143
1144 SetUpdateCheckAllowed(false);
1145
1146 // Add a second URL; update with this URL attempted and failed in a way that
1147 // causes it to switch directly to the next URL.
1148 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1149 update_state.num_checks = 10;
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001150 update_state.download_urls.emplace_back("http://another/fake/url/");
1151 update_state.download_errors.emplace_back(
1152 0, ErrorCode::kPayloadHashMismatchError,
1153 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1));
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001154
1155 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -07001156 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001157 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001158 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001159 EXPECT_TRUE(result.update_can_start);
1160 EXPECT_FALSE(result.p2p_allowed);
1161 EXPECT_EQ(1, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001162 EXPECT_EQ(0, result.download_url_num_errors);
1163 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001164}
1165
1166TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
1167 // The UpdateCanStart policy returns true; URL search properly wraps around
1168 // the last one on the list.
1169
1170 SetUpdateCheckAllowed(false);
1171
1172 // Add a second URL; update with this URL attempted and failed in a way that
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001173 // causes it to switch directly to the next URL. We must disable backoff in
1174 // order for it not to interfere.
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001175 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001176 update_state.num_checks = 1;
1177 update_state.is_backoff_disabled = true;
1178 update_state.download_urls.emplace_back("http://another/fake/url/");
1179 update_state.download_errors.emplace_back(
1180 1, ErrorCode::kPayloadHashMismatchError,
1181 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1));
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001182
1183 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -07001184 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001185 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001186 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001187 EXPECT_TRUE(result.update_can_start);
1188 EXPECT_FALSE(result.p2p_allowed);
1189 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001190 EXPECT_EQ(0, result.download_url_num_errors);
1191 EXPECT_TRUE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001192}
1193
1194TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
1195 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
1196 // use is forbidden by policy.
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001197 //
1198 // Note: In the case where no usable URLs are found, the policy should not
1199 // increment the number of failed attempts! Doing so would result in a
1200 // non-idempotent semantics, and does not fall within the intended purpose of
1201 // the backoff mechanism anyway.
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001202
1203 SetUpdateCheckAllowed(false);
1204
1205 // Override specific device policy attributes.
1206 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1207 new bool(false));
1208
1209 // Check that the UpdateCanStart returns false.
1210 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -07001211 UpdateDownloadParams result;
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001212 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1213 update_state);
1214 EXPECT_FALSE(result.update_can_start);
1215 EXPECT_EQ(UpdateCannotStartReason::kCannotDownload,
1216 result.cannot_start_reason);
1217 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001218}
1219
1220TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
1221 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
1222 // use is forbidden by policy, however P2P is enabled. The result indicates
1223 // that no URL can be used.
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001224 //
1225 // Note: The number of failed attempts should not increase in this case (see
1226 // above test).
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001227
1228 SetUpdateCheckAllowed(false);
1229
1230 // Override specific device policy attributes.
1231 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1232 new bool(true));
1233 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1234 new bool(false));
1235
1236 // Check that the UpdateCanStart returns true.
1237 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -07001238 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001239 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001240 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001241 EXPECT_TRUE(result.update_can_start);
1242 EXPECT_TRUE(result.p2p_allowed);
1243 EXPECT_GT(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001244 EXPECT_EQ(0, result.download_url_num_errors);
1245 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001246}
1247
Gilad Arnoldef8d0872014-10-03 14:14:06 -07001248TEST_F(UmChromeOSPolicyTest,
1249 UpdateCanStartAllowedNoUsableUrlsButEnterpriseEnrolled) {
1250 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
1251 // use is forbidden by policy, and P2P is unset on the policy, however the
1252 // device is enterprise-enrolled so P2P is allowed. The result indicates that
1253 // no URL can be used.
1254 //
1255 // Note: The number of failed attempts should not increase in this case (see
1256 // above test).
1257
1258 SetUpdateCheckAllowed(false);
1259
1260 // Override specific device policy attributes.
1261 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(nullptr);
1262 fake_state_.device_policy_provider()->var_owner()->reset(nullptr);
1263 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1264 new bool(false));
1265
1266 // Check that the UpdateCanStart returns true.
1267 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1268 UpdateDownloadParams result;
1269 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1270 update_state);
1271 EXPECT_TRUE(result.update_can_start);
1272 EXPECT_TRUE(result.p2p_allowed);
1273 EXPECT_GT(0, result.download_url_idx);
1274 EXPECT_EQ(0, result.download_url_num_errors);
1275 EXPECT_FALSE(result.do_increment_failures);
1276}
1277
Gilad Arnold684219d2014-07-07 14:54:57 -07001278TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001279 // Ethernet is always allowed.
1280
1281 fake_state_.shill_provider()->var_conn_type()->
1282 reset(new ConnectionType(ConnectionType::kEthernet));
1283
1284 bool result;
1285 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001286 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001287 EXPECT_TRUE(result);
1288}
1289
Gilad Arnold684219d2014-07-07 14:54:57 -07001290TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001291 // Wifi is allowed if not tethered.
1292
1293 fake_state_.shill_provider()->var_conn_type()->
1294 reset(new ConnectionType(ConnectionType::kWifi));
1295
1296 bool result;
1297 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001298 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001299 EXPECT_TRUE(result);
1300}
1301
Alex Deymo63784a52014-05-28 10:46:14 -07001302TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -07001303 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
1304 // Tethered wifi is not allowed by default.
1305
1306 fake_state_.shill_provider()->var_conn_type()->
1307 reset(new ConnectionType(ConnectionType::kWifi));
1308 fake_state_.shill_provider()->var_conn_tethering()->
1309 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
1310
1311 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001312 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001313 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001314}
1315
Alex Deymo63784a52014-05-28 10:46:14 -07001316TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001317 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001318 // Tethered wifi can be allowed by policy.
1319
1320 fake_state_.shill_provider()->var_conn_type()->
1321 reset(new ConnectionType(ConnectionType::kWifi));
1322 fake_state_.shill_provider()->var_conn_tethering()->
1323 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
1324 set<ConnectionType> allowed_connections;
1325 allowed_connections.insert(ConnectionType::kCellular);
1326 fake_state_.device_policy_provider()->
1327 var_allowed_connection_types_for_update()->
1328 reset(new set<ConnectionType>(allowed_connections));
1329
1330 bool result;
1331 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001332 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001333 EXPECT_TRUE(result);
1334}
1335
Gilad Arnold684219d2014-07-07 14:54:57 -07001336TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001337 // Wimax is always allowed.
1338
1339 fake_state_.shill_provider()->var_conn_type()->
1340 reset(new ConnectionType(ConnectionType::kWifi));
1341
1342 bool result;
1343 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001344 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001345 EXPECT_TRUE(result);
1346}
1347
Alex Deymo63784a52014-05-28 10:46:14 -07001348TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -07001349 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
1350 // Bluetooth is never allowed.
1351
1352 fake_state_.shill_provider()->var_conn_type()->
1353 reset(new ConnectionType(ConnectionType::kBluetooth));
1354
1355 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001356 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001357 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001358}
1359
Alex Deymo63784a52014-05-28 10:46:14 -07001360TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -07001361 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
1362 // Bluetooth cannot be allowed even by policy.
1363
1364 fake_state_.shill_provider()->var_conn_type()->
1365 reset(new ConnectionType(ConnectionType::kBluetooth));
1366 set<ConnectionType> allowed_connections;
1367 allowed_connections.insert(ConnectionType::kBluetooth);
1368 fake_state_.device_policy_provider()->
1369 var_allowed_connection_types_for_update()->
1370 reset(new set<ConnectionType>(allowed_connections));
1371
1372 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001373 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001374 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001375}
1376
Alex Deymo63784a52014-05-28 10:46:14 -07001377TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001378 // Cellular is not allowed by default.
1379
1380 fake_state_.shill_provider()->var_conn_type()->
1381 reset(new ConnectionType(ConnectionType::kCellular));
1382
1383 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001384 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001385 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001386}
1387
Alex Deymo63784a52014-05-28 10:46:14 -07001388TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001389 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001390 // Update over cellular can be enabled by policy.
1391
1392 fake_state_.shill_provider()->var_conn_type()->
1393 reset(new ConnectionType(ConnectionType::kCellular));
1394 set<ConnectionType> allowed_connections;
1395 allowed_connections.insert(ConnectionType::kCellular);
1396 fake_state_.device_policy_provider()->
1397 var_allowed_connection_types_for_update()->
1398 reset(new set<ConnectionType>(allowed_connections));
1399
1400 bool result;
1401 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001402 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001403 EXPECT_TRUE(result);
1404}
1405
Alex Deymo63784a52014-05-28 10:46:14 -07001406TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001407 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001408 // Update over cellular can be enabled by user settings, but only if policy
1409 // is present and does not determine allowed connections.
1410
1411 fake_state_.shill_provider()->var_conn_type()->
1412 reset(new ConnectionType(ConnectionType::kCellular));
1413 set<ConnectionType> allowed_connections;
1414 allowed_connections.insert(ConnectionType::kCellular);
1415 fake_state_.updater_provider()->var_cellular_enabled()->
1416 reset(new bool(true));
1417
1418 bool result;
1419 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001420 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001421 EXPECT_TRUE(result);
1422}
1423
Alex Deymo63784a52014-05-28 10:46:14 -07001424} // namespace chromeos_update_manager