blob: 443a298a1109619f009a5d6f0bb968b00e6a121c [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 Arnoldb2f99192014-10-07 13:01:52 -0700497 EXPECT_FALSE(result.p2p_downloading_allowed);
498 EXPECT_FALSE(result.p2p_sharing_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700499 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700500 EXPECT_EQ(0, result.download_url_num_errors);
501 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700502}
503
Alex Deymo63784a52014-05-28 10:46:14 -0700504TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700505 // The UpdateCanStart policy returns true; device policy is loaded but imposes
506 // no restrictions on updating.
507
508 SetUpdateCheckAllowed(false);
509
510 // Check that the UpdateCanStart returns true.
511 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700512 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700513 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700514 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700515 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700516 EXPECT_FALSE(result.p2p_downloading_allowed);
517 EXPECT_FALSE(result.p2p_sharing_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700518 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700519 EXPECT_EQ(0, result.download_url_num_errors);
520 EXPECT_FALSE(result.do_increment_failures);
521}
522
523TEST_F(UmChromeOSPolicyTest,
524 UpdateCanStartNotAllowedBackoffNewWaitPeriodApplies) {
525 // The UpdateCanStart policy returns false; failures are reported and a new
526 // backoff period is enacted.
527
528 SetUpdateCheckAllowed(false);
529
530 const Time curr_time = fake_clock_.GetWallclockTime();
531 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
532 update_state.download_errors_max = 1;
533 update_state.download_errors.emplace_back(
534 0, ErrorCode::kDownloadTransferError,
535 curr_time - TimeDelta::FromSeconds(8));
536 update_state.download_errors.emplace_back(
537 0, ErrorCode::kDownloadTransferError,
538 curr_time - TimeDelta::FromSeconds(2));
539
540 // Check that UpdateCanStart returns false and a new backoff expiry is
541 // generated.
542 UpdateDownloadParams result;
543 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
544 update_state);
545 EXPECT_FALSE(result.update_can_start);
546 EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
547 EXPECT_TRUE(result.do_increment_failures);
548 EXPECT_LT(curr_time, result.backoff_expiry);
549}
550
551TEST_F(UmChromeOSPolicyTest,
552 UpdateCanStartNotAllowedBackoffPrevWaitPeriodStillApplies) {
553 // The UpdateCanStart policy returns false; a previously enacted backoff
554 // period still applies.
555
556 SetUpdateCheckAllowed(false);
557
558 const Time curr_time = fake_clock_.GetWallclockTime();
559 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
560 update_state.download_errors_max = 1;
561 update_state.download_errors.emplace_back(
562 0, ErrorCode::kDownloadTransferError,
563 curr_time - TimeDelta::FromSeconds(8));
564 update_state.download_errors.emplace_back(
565 0, ErrorCode::kDownloadTransferError,
566 curr_time - TimeDelta::FromSeconds(2));
567 update_state.failures_last_updated = curr_time;
568 update_state.backoff_expiry = curr_time + TimeDelta::FromMinutes(3);
569
570 // Check that UpdateCanStart returns false and a new backoff expiry is
571 // generated.
572 UpdateDownloadParams result;
573 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
574 &result, update_state);
575 EXPECT_FALSE(result.update_can_start);
576 EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
577 EXPECT_FALSE(result.do_increment_failures);
578 EXPECT_LT(curr_time, result.backoff_expiry);
579}
580
581TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffSatisfied) {
582 // The UpdateCanStart policy returns true; a previously enacted backoff period
583 // has elapsed, we're good to go.
584
585 SetUpdateCheckAllowed(false);
586
587 const Time curr_time = fake_clock_.GetWallclockTime();
588 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
589 update_state.download_errors_max = 1;
590 update_state.download_errors.emplace_back(
591 0, ErrorCode::kDownloadTransferError,
592 curr_time - TimeDelta::FromSeconds(8));
593 update_state.download_errors.emplace_back(
594 0, ErrorCode::kDownloadTransferError,
595 curr_time - TimeDelta::FromSeconds(2));
596 update_state.failures_last_updated = curr_time - TimeDelta::FromSeconds(1);
597 update_state.backoff_expiry = curr_time - TimeDelta::FromSeconds(1);
598
599 // Check that UpdateCanStart returns false and a new backoff expiry is
600 // generated.
601 UpdateDownloadParams result;
602 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart,
603 &result, update_state);
604 EXPECT_TRUE(result.update_can_start);
605 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
606 EXPECT_EQ(0, result.download_url_idx);
607 EXPECT_EQ(0, result.download_url_num_errors);
608 EXPECT_FALSE(result.do_increment_failures);
609 EXPECT_EQ(Time(), result.backoff_expiry);
610}
611
612TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffDisabled) {
613 // The UpdateCanStart policy returns false; failures are reported but backoff
614 // is disabled.
615
616 SetUpdateCheckAllowed(false);
617
618 const Time curr_time = fake_clock_.GetWallclockTime();
619 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
620 update_state.download_errors_max = 1;
621 update_state.download_errors.emplace_back(
622 0, ErrorCode::kDownloadTransferError,
623 curr_time - TimeDelta::FromSeconds(8));
624 update_state.download_errors.emplace_back(
625 0, ErrorCode::kDownloadTransferError,
626 curr_time - TimeDelta::FromSeconds(2));
627 update_state.is_backoff_disabled = true;
628
629 // Check that UpdateCanStart returns false and a new backoff expiry is
630 // generated.
631 UpdateDownloadParams result;
632 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
633 update_state);
634 EXPECT_TRUE(result.update_can_start);
635 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
636 EXPECT_EQ(0, result.download_url_idx);
637 EXPECT_EQ(0, result.download_url_num_errors);
638 EXPECT_TRUE(result.do_increment_failures);
639 EXPECT_EQ(Time(), result.backoff_expiry);
640}
641
642TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffInteractive) {
643 // The UpdateCanStart policy returns false; failures are reported but this is
644 // an interactive update check.
645
646 SetUpdateCheckAllowed(false);
647
648 const Time curr_time = fake_clock_.GetWallclockTime();
649 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
650 update_state.download_errors_max = 1;
651 update_state.download_errors.emplace_back(
652 0, ErrorCode::kDownloadTransferError,
653 curr_time - TimeDelta::FromSeconds(8));
654 update_state.download_errors.emplace_back(
655 0, ErrorCode::kDownloadTransferError,
656 curr_time - TimeDelta::FromSeconds(2));
657 update_state.is_interactive = true;
658
659 // Check that UpdateCanStart returns false and a new backoff expiry is
660 // generated.
661 UpdateDownloadParams result;
662 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
663 update_state);
664 EXPECT_TRUE(result.update_can_start);
665 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
666 EXPECT_EQ(0, result.download_url_idx);
667 EXPECT_EQ(0, result.download_url_num_errors);
668 EXPECT_TRUE(result.do_increment_failures);
669 EXPECT_EQ(Time(), result.backoff_expiry);
670}
671
672TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffDelta) {
673 // The UpdateCanStart policy returns false; failures are reported but this is
674 // a delta payload.
675
676 SetUpdateCheckAllowed(false);
677
678 const Time curr_time = fake_clock_.GetWallclockTime();
679 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
680 update_state.download_errors_max = 1;
681 update_state.download_errors.emplace_back(
682 0, ErrorCode::kDownloadTransferError,
683 curr_time - TimeDelta::FromSeconds(8));
684 update_state.download_errors.emplace_back(
685 0, ErrorCode::kDownloadTransferError,
686 curr_time - TimeDelta::FromSeconds(2));
687 update_state.is_delta_payload = true;
688
689 // Check that UpdateCanStart returns false and a new backoff expiry is
690 // generated.
691 UpdateDownloadParams result;
692 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
693 update_state);
694 EXPECT_TRUE(result.update_can_start);
695 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
696 EXPECT_EQ(0, result.download_url_idx);
697 EXPECT_EQ(0, result.download_url_num_errors);
698 EXPECT_TRUE(result.do_increment_failures);
699 EXPECT_EQ(Time(), result.backoff_expiry);
700}
701
702TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffUnofficialBuild) {
703 // The UpdateCanStart policy returns false; failures are reported but this is
704 // an unofficial build.
705
706 SetUpdateCheckAllowed(false);
707
708 const Time curr_time = fake_clock_.GetWallclockTime();
709 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
710 update_state.download_errors_max = 1;
711 update_state.download_errors.emplace_back(
712 0, ErrorCode::kDownloadTransferError,
713 curr_time - TimeDelta::FromSeconds(8));
714 update_state.download_errors.emplace_back(
715 0, ErrorCode::kDownloadTransferError,
716 curr_time - TimeDelta::FromSeconds(2));
717
718 fake_state_.system_provider()->var_is_official_build()->
719 reset(new bool(false));
720
721 // Check that UpdateCanStart returns false and a new backoff expiry is
722 // generated.
723 UpdateDownloadParams result;
724 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
725 update_state);
726 EXPECT_TRUE(result.update_can_start);
727 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
728 EXPECT_EQ(0, result.download_url_idx);
729 EXPECT_EQ(0, result.download_url_num_errors);
730 EXPECT_TRUE(result.do_increment_failures);
731 EXPECT_EQ(Time(), result.backoff_expiry);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700732}
733
Alex Deymo63784a52014-05-28 10:46:14 -0700734TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700735 // The UpdateCanStart policy fails because the UpdateScattering policy it
736 // depends on fails (unset variable).
737
738 SetUpdateCheckAllowed(false);
739
740 // Override the default seed variable with a null value so that the policy
741 // request would fail.
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700742 // TODO(garnold) This failure may or may not fail a number
743 // sub-policies/decisions, like scattering and backoff. We'll need a more
744 // deliberate setup to ensure that we're failing what we want to be failing.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700745 fake_state_.random_provider()->var_seed()->reset(nullptr);
746
747 // Check that the UpdateCanStart fails.
748 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700749 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700750 ExpectPolicyStatus(EvalStatus::kFailed,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700751 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700752}
753
Alex Deymo63784a52014-05-28 10:46:14 -0700754TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700755 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
756 // The UpdateCanStart policy returns false; device policy is loaded and
757 // scattering applies due to an unsatisfied wait period, which was newly
758 // generated.
759
760 SetUpdateCheckAllowed(false);
761 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
762 new TimeDelta(TimeDelta::FromMinutes(2)));
763
764
765 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
766
767 // Check that the UpdateCanStart returns false and a new wait period
768 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700769 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700770 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700771 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700772 EXPECT_FALSE(result.update_can_start);
773 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
774 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
775 EXPECT_EQ(0, result.scatter_check_threshold);
776}
777
Alex Deymo63784a52014-05-28 10:46:14 -0700778TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700779 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
780 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
781 // is loaded and a previously generated scattering period still applies, none
782 // of the scattering values has changed.
783
784 SetUpdateCheckAllowed(false);
785 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
786 new TimeDelta(TimeDelta::FromMinutes(2)));
787
788 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
789 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
790
791 // Check that the UpdateCanStart returns false and a new wait period
792 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700793 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700794 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700795 &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700796 EXPECT_FALSE(result.update_can_start);
797 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
798 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
799 EXPECT_EQ(0, result.scatter_check_threshold);
800}
801
Alex Deymo63784a52014-05-28 10:46:14 -0700802TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700803 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
804 // The UpdateCanStart policy returns false; device policy is loaded and
805 // scattering applies due to an unsatisfied update check count threshold.
806 //
807 // This ensures a non-zero check threshold, which may or may not be combined
808 // with a non-zero wait period (for which we cannot reliably control).
809
810 SetUpdateCheckAllowed(false);
811 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
812 new TimeDelta(TimeDelta::FromSeconds(1)));
813
814 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
815 update_state.scatter_check_threshold_min = 2;
816 update_state.scatter_check_threshold_max = 5;
817
818 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700819 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700820 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700821 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700822 EXPECT_FALSE(result.update_can_start);
823 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
824 EXPECT_LE(2, result.scatter_check_threshold);
825 EXPECT_GE(5, result.scatter_check_threshold);
826}
827
Alex Deymo63784a52014-05-28 10:46:14 -0700828TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700829 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
830 // The UpdateCanStart policy returns false; device policy is loaded and
831 // scattering due to a previously generated count threshold still applies.
832
833 SetUpdateCheckAllowed(false);
834 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
835 new TimeDelta(TimeDelta::FromSeconds(1)));
836
837 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
838 update_state.scatter_check_threshold = 3;
839 update_state.scatter_check_threshold_min = 2;
840 update_state.scatter_check_threshold_max = 5;
841
842 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700843 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700844 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700845 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700846 EXPECT_FALSE(result.update_can_start);
847 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
848 EXPECT_EQ(3, result.scatter_check_threshold);
849}
850
Alex Deymo63784a52014-05-28 10:46:14 -0700851TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700852 // The UpdateCanStart policy returns true; device policy is loaded and
853 // scattering is enabled, but both wait period and check threshold are
854 // satisfied.
855
856 SetUpdateCheckAllowed(false);
857 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
858 new TimeDelta(TimeDelta::FromSeconds(120)));
859
860 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
861 update_state.num_checks = 4;
862 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
863 update_state.scatter_check_threshold = 3;
864 update_state.scatter_check_threshold_min = 2;
865 update_state.scatter_check_threshold_max = 5;
866
867 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700868 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700869 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700870 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700871 EXPECT_TRUE(result.update_can_start);
872 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
873 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700874 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700875 EXPECT_EQ(0, result.download_url_num_errors);
876 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700877}
878
Alex Deymo63784a52014-05-28 10:46:14 -0700879TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700880 UpdateCanStartAllowedInteractivePreventsScattering) {
881 // The UpdateCanStart policy returns true; device policy is loaded and
882 // scattering would have applied, except that the update check is interactive
883 // and so it is suppressed.
884
885 SetUpdateCheckAllowed(false);
886 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
887 new TimeDelta(TimeDelta::FromSeconds(1)));
888
889 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700890 update_state.is_interactive = true;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700891 update_state.scatter_check_threshold = 0;
892 update_state.scatter_check_threshold_min = 2;
893 update_state.scatter_check_threshold_max = 5;
894
895 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700896 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700897 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700898 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700899 EXPECT_TRUE(result.update_can_start);
900 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
901 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700902 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700903 EXPECT_EQ(0, result.download_url_num_errors);
904 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700905}
906
Alex Deymo63784a52014-05-28 10:46:14 -0700907TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700908 UpdateCanStartAllowedOobePreventsScattering) {
909 // The UpdateCanStart policy returns true; device policy is loaded and
910 // scattering would have applied, except that OOBE was not completed and so it
911 // is suppressed.
912
913 SetUpdateCheckAllowed(false);
914 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
915 new TimeDelta(TimeDelta::FromSeconds(1)));
916 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
917
918 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700919 update_state.is_interactive = true;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700920 update_state.scatter_check_threshold = 0;
921 update_state.scatter_check_threshold_min = 2;
922 update_state.scatter_check_threshold_max = 5;
923
924 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700925 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700926 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700927 update_state);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700928 EXPECT_TRUE(result.update_can_start);
929 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
930 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700931 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700932 EXPECT_EQ(0, result.download_url_num_errors);
933 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700934}
935
Alex Deymo63784a52014-05-28 10:46:14 -0700936TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700937 // The UpdateCanStart policy returns true; device policy permits both HTTP and
938 // P2P updates, as well as a non-empty target channel string.
939
940 SetUpdateCheckAllowed(false);
941
942 // Override specific device policy attributes.
943 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
944 new bool(true));
945 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
946 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700947
948 // Check that the UpdateCanStart returns true.
949 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700950 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700951 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700952 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700953 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700954 EXPECT_TRUE(result.p2p_downloading_allowed);
955 EXPECT_TRUE(result.p2p_sharing_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700956 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700957 EXPECT_EQ(0, result.download_url_num_errors);
958 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700959}
960
Alex Deymo63784a52014-05-28 10:46:14 -0700961TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700962 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
963 // P2P updates, but the updater is configured to allow P2P and overrules the
964 // setting.
965
966 SetUpdateCheckAllowed(false);
967
968 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700969 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
970
971 // Check that the UpdateCanStart returns true.
972 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700973 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700974 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700975 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700976 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700977 EXPECT_TRUE(result.p2p_downloading_allowed);
978 EXPECT_TRUE(result.p2p_sharing_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700979 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700980 EXPECT_EQ(0, result.download_url_num_errors);
981 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700982}
983
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700984TEST_F(UmChromeOSPolicyTest,
985 UpdateCanStartAllowedP2PDownloadBlockedDueToNumAttempts) {
Gilad Arnold349ac832014-10-06 14:20:28 -0700986 // The UpdateCanStart policy returns true; device policy permits HTTP but
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700987 // blocks P2P download, because the max number of P2P downloads have been
988 // attempted. P2P sharing is still permitted.
Gilad Arnold349ac832014-10-06 14:20:28 -0700989
990 SetUpdateCheckAllowed(false);
991
992 // Override specific device policy attributes.
993 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
994 new bool(true));
995 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
996 new bool(true));
997
998 // Check that the UpdateCanStart returns true.
999 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1000 update_state.p2p_num_attempts = ChromeOSPolicy::kMaxP2PAttempts;
1001 UpdateDownloadParams result;
1002 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1003 update_state);
1004 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb2f99192014-10-07 13:01:52 -07001005 EXPECT_FALSE(result.p2p_downloading_allowed);
1006 EXPECT_TRUE(result.p2p_sharing_allowed);
Gilad Arnold349ac832014-10-06 14:20:28 -07001007}
1008
1009TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldb2f99192014-10-07 13:01:52 -07001010 UpdateCanStartAllowedP2PDownloadBlockedDueToAttemptsPeriod) {
Gilad Arnold349ac832014-10-06 14:20:28 -07001011 // The UpdateCanStart policy returns true; device policy permits HTTP but
Gilad Arnoldb2f99192014-10-07 13:01:52 -07001012 // blocks P2P download, because the max period for attempt to download via P2P
1013 // has elapsed. P2P sharing is still permitted.
Gilad Arnold349ac832014-10-06 14:20:28 -07001014
1015 SetUpdateCheckAllowed(false);
1016
1017 // Override specific device policy attributes.
1018 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1019 new bool(true));
1020 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1021 new bool(true));
1022
1023 // Check that the UpdateCanStart returns true.
1024 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1025 update_state.p2p_num_attempts = 1;
1026 update_state.p2p_first_attempted =
1027 fake_clock_.GetWallclockTime() -
Alex Deymof329b932014-10-30 01:37:48 -07001028 TimeDelta::FromSeconds(
1029 ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds + 1);
Gilad Arnold349ac832014-10-06 14:20:28 -07001030 UpdateDownloadParams result;
1031 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1032 update_state);
1033 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb2f99192014-10-07 13:01:52 -07001034 EXPECT_FALSE(result.p2p_downloading_allowed);
1035 EXPECT_TRUE(result.p2p_sharing_allowed);
Gilad Arnold349ac832014-10-06 14:20:28 -07001036}
1037
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001038TEST_F(UmChromeOSPolicyTest,
1039 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001040 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1041 // P2P updates, but marking this an unofficial build overrules the HTTP
1042 // setting.
1043
1044 SetUpdateCheckAllowed(false);
1045
1046 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001047 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1048 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001049 fake_state_.system_provider()->var_is_official_build()->
1050 reset(new bool(false));
1051
1052 // Check that the UpdateCanStart returns true.
1053 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -07001054 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001055 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001056 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001057 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001058 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001059 EXPECT_EQ(0, result.download_url_num_errors);
1060 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001061}
1062
1063TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
1064 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
1065 // P2P updates, but an HTTPS URL is provided and selected for download.
1066
1067 SetUpdateCheckAllowed(false);
1068
1069 // Override specific device policy attributes.
1070 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1071 new bool(false));
1072
1073 // Add an HTTPS URL.
1074 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001075 update_state.download_urls.emplace_back("https://secure/url/");
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001076
1077 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -07001078 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001079 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001080 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001081 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001082 EXPECT_EQ(1, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001083 EXPECT_EQ(0, result.download_url_num_errors);
1084 EXPECT_FALSE(result.do_increment_failures);
1085}
1086
1087TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedMaxErrorsNotExceeded) {
1088 // The UpdateCanStart policy returns true; the first URL has download errors
1089 // but does not exceed the maximum allowed number of failures, so it is stilli
1090 // usable.
1091
1092 SetUpdateCheckAllowed(false);
1093
1094 // Add a second URL; update with this URL attempted and failed enough times to
1095 // disqualify the current (first) URL.
1096 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1097 update_state.num_checks = 5;
1098 update_state.download_urls.emplace_back("http://another/fake/url/");
1099 Time t = fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(12);
1100 for (int i = 0; i < 5; i++) {
1101 update_state.download_errors.emplace_back(
1102 0, ErrorCode::kDownloadTransferError, t);
1103 t += TimeDelta::FromSeconds(1);
1104 }
1105
1106 // Check that the UpdateCanStart returns true.
1107 UpdateDownloadParams result;
1108 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1109 update_state);
1110 EXPECT_TRUE(result.update_can_start);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001111 EXPECT_EQ(0, result.download_url_idx);
1112 EXPECT_EQ(5, result.download_url_num_errors);
1113 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001114}
1115
1116TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
1117 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
1118 // allowed number of failures, but a second URL is available.
1119
1120 SetUpdateCheckAllowed(false);
1121
1122 // Add a second URL; update with this URL attempted and failed enough times to
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001123 // disqualify the current (first) URL.
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001124 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1125 update_state.num_checks = 10;
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001126 update_state.download_urls.emplace_back("http://another/fake/url/");
1127 Time t = fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(12);
1128 for (int i = 0; i < 11; i++) {
1129 update_state.download_errors.emplace_back(
1130 0, ErrorCode::kDownloadTransferError, t);
1131 t += TimeDelta::FromSeconds(1);
1132 }
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001133
1134 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -07001135 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001136 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001137 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001138 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001139 EXPECT_EQ(1, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001140 EXPECT_EQ(0, result.download_url_num_errors);
1141 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001142}
1143
1144TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
1145 // The UpdateCanStart policy returns true; the first URL fails with a hard
1146 // error, but a second URL is available.
1147
1148 SetUpdateCheckAllowed(false);
1149
1150 // Add a second URL; update with this URL attempted and failed in a way that
1151 // causes it to switch directly to the next URL.
1152 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1153 update_state.num_checks = 10;
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001154 update_state.download_urls.emplace_back("http://another/fake/url/");
1155 update_state.download_errors.emplace_back(
1156 0, ErrorCode::kPayloadHashMismatchError,
1157 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1));
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001158
1159 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -07001160 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001161 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001162 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001163 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001164 EXPECT_EQ(1, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001165 EXPECT_EQ(0, result.download_url_num_errors);
1166 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001167}
1168
1169TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
1170 // The UpdateCanStart policy returns true; URL search properly wraps around
1171 // the last one on the list.
1172
1173 SetUpdateCheckAllowed(false);
1174
1175 // Add a second URL; update with this URL attempted and failed in a way that
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001176 // causes it to switch directly to the next URL. We must disable backoff in
1177 // order for it not to interfere.
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001178 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001179 update_state.num_checks = 1;
1180 update_state.is_backoff_disabled = true;
1181 update_state.download_urls.emplace_back("http://another/fake/url/");
1182 update_state.download_errors.emplace_back(
1183 1, ErrorCode::kPayloadHashMismatchError,
1184 fake_clock_.GetWallclockTime() - TimeDelta::FromSeconds(1));
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001185
1186 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -07001187 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001188 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001189 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001190 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001191 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001192 EXPECT_EQ(0, result.download_url_num_errors);
1193 EXPECT_TRUE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001194}
1195
1196TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
1197 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
1198 // use is forbidden by policy.
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001199 //
1200 // Note: In the case where no usable URLs are found, the policy should not
1201 // increment the number of failed attempts! Doing so would result in a
1202 // non-idempotent semantics, and does not fall within the intended purpose of
1203 // the backoff mechanism anyway.
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001204
1205 SetUpdateCheckAllowed(false);
1206
1207 // Override specific device policy attributes.
1208 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1209 new bool(false));
1210
1211 // Check that the UpdateCanStart returns false.
1212 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -07001213 UpdateDownloadParams result;
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001214 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1215 update_state);
1216 EXPECT_FALSE(result.update_can_start);
1217 EXPECT_EQ(UpdateCannotStartReason::kCannotDownload,
1218 result.cannot_start_reason);
1219 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001220}
1221
1222TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
1223 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
1224 // use is forbidden by policy, however P2P is enabled. The result indicates
1225 // that no URL can be used.
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001226 //
1227 // Note: The number of failed attempts should not increase in this case (see
1228 // above test).
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001229
1230 SetUpdateCheckAllowed(false);
1231
1232 // Override specific device policy attributes.
1233 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1234 new bool(true));
1235 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1236 new bool(false));
1237
1238 // Check that the UpdateCanStart returns true.
1239 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -07001240 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001241 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001242 update_state);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001243 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb2f99192014-10-07 13:01:52 -07001244 EXPECT_TRUE(result.p2p_downloading_allowed);
1245 EXPECT_TRUE(result.p2p_sharing_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -07001246 EXPECT_GT(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -07001247 EXPECT_EQ(0, result.download_url_num_errors);
1248 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07001249}
1250
Gilad Arnoldef8d0872014-10-03 14:14:06 -07001251TEST_F(UmChromeOSPolicyTest,
1252 UpdateCanStartAllowedNoUsableUrlsButEnterpriseEnrolled) {
1253 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
1254 // use is forbidden by policy, and P2P is unset on the policy, however the
1255 // device is enterprise-enrolled so P2P is allowed. The result indicates that
1256 // no URL can be used.
1257 //
1258 // Note: The number of failed attempts should not increase in this case (see
1259 // above test).
1260
1261 SetUpdateCheckAllowed(false);
1262
1263 // Override specific device policy attributes.
1264 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(nullptr);
1265 fake_state_.device_policy_provider()->var_owner()->reset(nullptr);
1266 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1267 new bool(false));
1268
1269 // Check that the UpdateCanStart returns true.
1270 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1271 UpdateDownloadParams result;
1272 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
1273 update_state);
1274 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldb2f99192014-10-07 13:01:52 -07001275 EXPECT_TRUE(result.p2p_downloading_allowed);
1276 EXPECT_TRUE(result.p2p_sharing_allowed);
Gilad Arnoldef8d0872014-10-03 14:14:06 -07001277 EXPECT_GT(0, result.download_url_idx);
1278 EXPECT_EQ(0, result.download_url_num_errors);
1279 EXPECT_FALSE(result.do_increment_failures);
1280}
1281
Gilad Arnold684219d2014-07-07 14:54:57 -07001282TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001283 // Ethernet is always allowed.
1284
1285 fake_state_.shill_provider()->var_conn_type()->
1286 reset(new ConnectionType(ConnectionType::kEthernet));
1287
1288 bool result;
1289 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001290 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001291 EXPECT_TRUE(result);
1292}
1293
Gilad Arnold684219d2014-07-07 14:54:57 -07001294TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001295 // Wifi is allowed if not tethered.
1296
1297 fake_state_.shill_provider()->var_conn_type()->
1298 reset(new ConnectionType(ConnectionType::kWifi));
1299
1300 bool result;
1301 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001302 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001303 EXPECT_TRUE(result);
1304}
1305
Alex Deymo63784a52014-05-28 10:46:14 -07001306TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -07001307 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
1308 // Tethered wifi is not allowed by default.
1309
1310 fake_state_.shill_provider()->var_conn_type()->
1311 reset(new ConnectionType(ConnectionType::kWifi));
1312 fake_state_.shill_provider()->var_conn_tethering()->
1313 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
1314
1315 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001316 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001317 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001318}
1319
Alex Deymo63784a52014-05-28 10:46:14 -07001320TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001321 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001322 // Tethered wifi can be allowed by policy.
1323
1324 fake_state_.shill_provider()->var_conn_type()->
1325 reset(new ConnectionType(ConnectionType::kWifi));
1326 fake_state_.shill_provider()->var_conn_tethering()->
1327 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
1328 set<ConnectionType> allowed_connections;
1329 allowed_connections.insert(ConnectionType::kCellular);
1330 fake_state_.device_policy_provider()->
1331 var_allowed_connection_types_for_update()->
1332 reset(new set<ConnectionType>(allowed_connections));
1333
1334 bool result;
1335 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001336 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001337 EXPECT_TRUE(result);
1338}
1339
Gilad Arnold684219d2014-07-07 14:54:57 -07001340TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001341 // Wimax is always allowed.
1342
1343 fake_state_.shill_provider()->var_conn_type()->
1344 reset(new ConnectionType(ConnectionType::kWifi));
1345
1346 bool result;
1347 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001348 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001349 EXPECT_TRUE(result);
1350}
1351
Alex Deymo63784a52014-05-28 10:46:14 -07001352TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -07001353 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
1354 // Bluetooth is never allowed.
1355
1356 fake_state_.shill_provider()->var_conn_type()->
1357 reset(new ConnectionType(ConnectionType::kBluetooth));
1358
1359 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001360 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001361 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001362}
1363
Alex Deymo63784a52014-05-28 10:46:14 -07001364TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -07001365 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
1366 // Bluetooth cannot be allowed even by policy.
1367
1368 fake_state_.shill_provider()->var_conn_type()->
1369 reset(new ConnectionType(ConnectionType::kBluetooth));
1370 set<ConnectionType> allowed_connections;
1371 allowed_connections.insert(ConnectionType::kBluetooth);
1372 fake_state_.device_policy_provider()->
1373 var_allowed_connection_types_for_update()->
1374 reset(new set<ConnectionType>(allowed_connections));
1375
1376 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001377 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001378 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001379}
1380
Alex Deymo63784a52014-05-28 10:46:14 -07001381TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001382 // Cellular is not allowed by default.
1383
1384 fake_state_.shill_provider()->var_conn_type()->
1385 reset(new ConnectionType(ConnectionType::kCellular));
1386
1387 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001388 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001389 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001390}
1391
Alex Deymo63784a52014-05-28 10:46:14 -07001392TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001393 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001394 // Update over cellular can be enabled by policy.
1395
1396 fake_state_.shill_provider()->var_conn_type()->
1397 reset(new ConnectionType(ConnectionType::kCellular));
1398 set<ConnectionType> allowed_connections;
1399 allowed_connections.insert(ConnectionType::kCellular);
1400 fake_state_.device_policy_provider()->
1401 var_allowed_connection_types_for_update()->
1402 reset(new set<ConnectionType>(allowed_connections));
1403
1404 bool result;
1405 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001406 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001407 EXPECT_TRUE(result);
1408}
1409
Alex Deymo63784a52014-05-28 10:46:14 -07001410TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001411 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001412 // Update over cellular can be enabled by user settings, but only if policy
1413 // is present and does not determine allowed connections.
1414
1415 fake_state_.shill_provider()->var_conn_type()->
1416 reset(new ConnectionType(ConnectionType::kCellular));
1417 set<ConnectionType> allowed_connections;
1418 allowed_connections.insert(ConnectionType::kCellular);
1419 fake_state_.updater_provider()->var_cellular_enabled()->
1420 reset(new bool(true));
1421
1422 bool result;
1423 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001424 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001425 EXPECT_TRUE(result);
1426}
1427
Alex Deymo63784a52014-05-28 10:46:14 -07001428} // namespace chromeos_update_manager