blob: 10e63244104707dd9d51ec055f3fa043db6ee8af [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 Arnolddc4bb262014-07-23 10:45:19 -070024using std::make_tuple;
Gilad Arnold0adbc942014-05-12 10:35:43 -070025using std::set;
Alex Deymo0d11c602014-04-23 20:12:20 -070026using std::string;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070027using std::tuple;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070028using std::vector;
Alex Deymo0d11c602014-04-23 20:12:20 -070029
Alex Deymo63784a52014-05-28 10:46:14 -070030namespace chromeos_update_manager {
Alex Deymo0d11c602014-04-23 20:12:20 -070031
Alex Deymo63784a52014-05-28 10:46:14 -070032class UmChromeOSPolicyTest : public ::testing::Test {
Alex Deymo0d11c602014-04-23 20:12:20 -070033 protected:
34 virtual void SetUp() {
35 SetUpDefaultClock();
Gilad Arnoldb2271992014-06-19 12:35:24 -070036 eval_ctx_ = new EvaluationContext(&fake_clock_, TimeDelta::FromSeconds(5));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070037 SetUpDefaultState();
38 SetUpDefaultDevicePolicy();
Alex Deymo0d11c602014-04-23 20:12:20 -070039 }
40
41 // Sets the clock to fixed values.
42 void SetUpDefaultClock() {
43 fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
44 fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L));
45 }
46
47 void SetUpDefaultState() {
48 fake_state_.updater_provider()->var_updater_started_time()->reset(
49 new Time(fake_clock_.GetWallclockTime()));
50 fake_state_.updater_provider()->var_last_checked_time()->reset(
51 new Time(fake_clock_.GetWallclockTime()));
52 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -070053 reset(new unsigned int{0});
Gilad Arnolda0258a52014-07-10 16:21:19 -070054 fake_state_.updater_provider()->var_server_dictated_poll_interval()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -070055 reset(new unsigned int{0});
56 fake_state_.updater_provider()->var_forced_update_requested()->
57 reset(new UpdateRequestStatus{UpdateRequestStatus::kNone});
Alex Deymo0d11c602014-04-23 20:12:20 -070058
59 fake_state_.random_provider()->var_seed()->reset(
60 new uint64_t(4)); // chosen by fair dice roll.
61 // guaranteed to be random.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070062
63 // No device policy loaded by default.
64 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
65 new bool(false));
66
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070067 // OOBE is enabled by default.
68 fake_state_.config_provider()->var_is_oobe_enabled()->reset(
69 new bool(true));
70
Gilad Arnold76a11f62014-05-20 09:02:12 -070071 // For the purpose of the tests, this is an official build and OOBE was
72 // completed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070073 fake_state_.system_provider()->var_is_official_build()->reset(
74 new bool(true));
Gilad Arnold76a11f62014-05-20 09:02:12 -070075 fake_state_.system_provider()->var_is_oobe_complete()->reset(
76 new bool(true));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -070077 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
78 new bool(false));
Gilad Arnold0adbc942014-05-12 10:35:43 -070079
80 // Connection is wifi, untethered.
81 fake_state_.shill_provider()->var_conn_type()->
82 reset(new ConnectionType(ConnectionType::kWifi));
83 fake_state_.shill_provider()->var_conn_tethering()->
84 reset(new ConnectionTethering(ConnectionTethering::kNotDetected));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070085 }
86
Gilad Arnoldb3b05442014-05-30 14:25:05 -070087 // Sets up a default device policy that does not impose any restrictions
88 // (HTTP) nor enables any features (P2P).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070089 void SetUpDefaultDevicePolicy() {
90 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
91 new bool(true));
92 fake_state_.device_policy_provider()->var_update_disabled()->reset(
93 new bool(false));
94 fake_state_.device_policy_provider()->
95 var_allowed_connection_types_for_update()->reset(nullptr);
96 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
97 new TimeDelta());
98 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
Gilad Arnoldb3b05442014-05-30 14:25:05 -070099 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700100 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
101 new bool(false));
102 fake_state_.device_policy_provider()->var_release_channel_delegated()->
103 reset(new bool(true));
104 }
105
106 // Configures the UpdateCheckAllowed policy to return a desired value by
107 // faking the current wall clock time as needed. Restores the default state.
108 // This is used when testing policies that depend on this one.
109 void SetUpdateCheckAllowed(bool allow_check) {
110 Time next_update_check;
111 ExpectPolicyStatus(EvalStatus::kSucceeded,
112 &ChromeOSPolicy::NextUpdateCheckTime,
113 &next_update_check);
114 SetUpDefaultState();
115 SetUpDefaultDevicePolicy();
116 Time curr_time = next_update_check;
117 if (allow_check)
118 curr_time += TimeDelta::FromSeconds(1);
119 else
120 curr_time -= TimeDelta::FromSeconds(1);
121 fake_clock_.SetWallclockTime(curr_time);
122 }
123
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700124 // Returns a default UpdateState structure:
125 UpdateState GetDefaultUpdateState(TimeDelta first_seen_period) {
126 Time first_seen_time = fake_clock_.GetWallclockTime() - first_seen_period;
127 UpdateState update_state = UpdateState();
128
129 // This is a non-interactive check returning a delta payload, seen for the
130 // first time (|first_seen_period| ago). Clearly, there were no failed
131 // attempts so far.
132 update_state.is_interactive = false;
133 update_state.is_delta_payload = false;
134 update_state.first_seen = first_seen_time;
135 update_state.num_checks = 1;
136 update_state.num_failures = 0;
137 update_state.failures_last_updated = Time(); // Needs to be zero.
138 // There's a single HTTP download URL with a maximum of 10 retries.
139 update_state.download_urls = vector<string>{"http://fake/url/"};
140 update_state.download_errors_max = 10;
141 // Download was never attempted.
142 update_state.last_download_url_idx = -1;
143 update_state.last_download_url_num_errors = 0;
144 // There were no download errors.
145 update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
Gilad Arnold349ac832014-10-06 14:20:28 -0700146 // P2P was not attempted.
147 update_state.p2p_num_attempts = 0;
148 update_state.p2p_first_attempted = Time();
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700149 // No active backoff period, backoff is not disabled by Omaha.
150 update_state.backoff_expiry = Time();
151 update_state.is_backoff_disabled = false;
152 // There is no active scattering wait period (max 7 days allowed) nor check
153 // threshold (none allowed).
154 update_state.scatter_wait_period = TimeDelta();
155 update_state.scatter_check_threshold = 0;
156 update_state.scatter_wait_period_max = TimeDelta::FromDays(7);
157 update_state.scatter_check_threshold_min = 0;
158 update_state.scatter_check_threshold_max = 0;
159
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700160 return update_state;
Alex Deymo0d11c602014-04-23 20:12:20 -0700161 }
162
163 // Runs the passed |policy_method| policy and expects it to return the
164 // |expected| return value.
165 template<typename T, typename R, typename... Args>
166 void ExpectPolicyStatus(
167 EvalStatus expected,
168 T policy_method,
169 R* result, Args... args) {
170 string error = "<None>";
171 eval_ctx_->ResetEvaluation();
172 EXPECT_EQ(expected,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700173 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result,
174 args...))
Alex Deymoc850b4f2014-05-19 11:06:41 -0700175 << "Returned error: " << error
176 << "\nEvaluation context: " << eval_ctx_->DumpContext();
Alex Deymo0d11c602014-04-23 20:12:20 -0700177 }
178
179 FakeClock fake_clock_;
180 FakeState fake_state_;
181 scoped_refptr<EvaluationContext> eval_ctx_;
182 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
183};
184
Alex Deymo63784a52014-05-28 10:46:14 -0700185TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700186 Time next_update_check;
187
Gilad Arnold38b14022014-07-09 12:45:56 -0700188 // Set the last update time so it'll appear as if this is a first update check
189 // in the lifetime of the current updater.
190 fake_state_.updater_provider()->var_last_checked_time()->reset(
191 new Time(fake_clock_.GetWallclockTime() - TimeDelta::FromMinutes(10)));
192
Alex Deymo0d11c602014-04-23 20:12:20 -0700193 ExpectPolicyStatus(EvalStatus::kSucceeded,
194 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
195
196 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
Gilad Arnold38b14022014-07-09 12:45:56 -0700197 EXPECT_GE(
198 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
199 ChromeOSPolicy::kTimeoutInitialInterval +
200 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
201 next_update_check);
202}
203
204TEST_F(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz) {
205 // Ensure that we're using the correct interval (kPeriodicInterval) and fuzz
206 // (kTimeoutRegularFuzz) as base values for period updates.
207 Time next_update_check;
208
209 ExpectPolicyStatus(EvalStatus::kSucceeded,
210 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
211
212 EXPECT_LE(
213 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
214 ChromeOSPolicy::kTimeoutPeriodicInterval -
215 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
216 next_update_check);
217 EXPECT_GE(
218 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
219 ChromeOSPolicy::kTimeoutPeriodicInterval +
220 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
221 next_update_check);
222}
223
224TEST_F(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz) {
225 // Ensure that we're properly backing off and fuzzing in the presence of
226 // failed updates attempts.
227 Time next_update_check;
228
229 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700230 reset(new unsigned int{2});
Gilad Arnold38b14022014-07-09 12:45:56 -0700231
232 ExpectPolicyStatus(EvalStatus::kSucceeded,
233 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
234
235 int expected_interval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
236 EXPECT_LE(
237 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
238 expected_interval - expected_interval / 2),
239 next_update_check);
240 EXPECT_GE(
241 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
242 expected_interval + expected_interval / 2),
243 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700244}
245
Gilad Arnolda0258a52014-07-10 16:21:19 -0700246TEST_F(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval) {
247 // Policy honors the server provided check poll interval.
248 Time next_update_check;
249
250 const unsigned int kInterval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
251 fake_state_.updater_provider()->var_server_dictated_poll_interval()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700252 reset(new unsigned int{kInterval});
Gilad Arnolda0258a52014-07-10 16:21:19 -0700253 // We should not be backing off in this case.
254 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700255 reset(new unsigned int{2});
Gilad Arnolda0258a52014-07-10 16:21:19 -0700256
257 ExpectPolicyStatus(EvalStatus::kSucceeded,
258 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
259
260 EXPECT_LE(
261 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
262 kInterval - kInterval / 2),
263 next_update_check);
264 EXPECT_GE(
265 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
266 kInterval + kInterval / 2),
267 next_update_check);
268}
269
Alex Deymo63784a52014-05-28 10:46:14 -0700270TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700271 Time next_update_check;
272
Alex Deymo0d11c602014-04-23 20:12:20 -0700273 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700274 reset(new unsigned int{100});
Gilad Arnold38b14022014-07-09 12:45:56 -0700275
Alex Deymo0d11c602014-04-23 20:12:20 -0700276 ExpectPolicyStatus(EvalStatus::kSucceeded,
277 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
278
Gilad Arnold38b14022014-07-09 12:45:56 -0700279 EXPECT_LE(
280 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
281 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
282 ChromeOSPolicy::kTimeoutMaxBackoffInterval / 2),
283 next_update_check);
284 EXPECT_GE(
285 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
286 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
287 ChromeOSPolicy::kTimeoutMaxBackoffInterval /2),
288 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700289}
290
Alex Deymo63784a52014-05-28 10:46:14 -0700291TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700292 // We get the next update_check timestamp from the policy's private method
293 // and then we check the public method respects that value on the normal
294 // case.
295 Time next_update_check;
296 Time last_checked_time =
297 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
298
Alex Deymo0d11c602014-04-23 20:12:20 -0700299 fake_state_.updater_provider()->var_last_checked_time()->reset(
300 new Time(last_checked_time));
301 ExpectPolicyStatus(EvalStatus::kSucceeded,
302 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
303
304 UpdateCheckParams result;
305
306 // Check that the policy blocks until the next_update_check is reached.
307 SetUpDefaultClock();
308 SetUpDefaultState();
309 fake_state_.updater_provider()->var_last_checked_time()->reset(
310 new Time(last_checked_time));
311 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
312 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
313 &Policy::UpdateCheckAllowed, &result);
314
315 SetUpDefaultClock();
316 SetUpDefaultState();
317 fake_state_.updater_provider()->var_last_checked_time()->reset(
318 new Time(last_checked_time));
319 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
320 ExpectPolicyStatus(EvalStatus::kSucceeded,
321 &Policy::UpdateCheckAllowed, &result);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700322 EXPECT_TRUE(result.updates_enabled);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700323 EXPECT_FALSE(result.is_interactive);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700324}
325
326TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700327 // Update checks are deferred until OOBE is completed.
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700328
329 // Ensure that update is not allowed even if wait period is satisfied.
330 Time next_update_check;
331 Time last_checked_time =
332 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
333
334 fake_state_.updater_provider()->var_last_checked_time()->reset(
335 new Time(last_checked_time));
336 ExpectPolicyStatus(EvalStatus::kSucceeded,
337 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
338
339 SetUpDefaultClock();
340 SetUpDefaultState();
341 fake_state_.updater_provider()->var_last_checked_time()->reset(
342 new Time(last_checked_time));
343 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
344 fake_state_.system_provider()->var_is_oobe_complete()->reset(
345 new bool(false));
346
347 UpdateCheckParams result;
348 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
349 &Policy::UpdateCheckAllowed, &result);
350
351 // Now check that it is allowed if OOBE is completed.
352 SetUpDefaultClock();
353 SetUpDefaultState();
354 fake_state_.updater_provider()->var_last_checked_time()->reset(
355 new Time(last_checked_time));
356 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
357 ExpectPolicyStatus(EvalStatus::kSucceeded,
358 &Policy::UpdateCheckAllowed, &result);
359 EXPECT_TRUE(result.updates_enabled);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700360 EXPECT_FALSE(result.is_interactive);
Alex Deymo0d11c602014-04-23 20:12:20 -0700361}
362
Gilad Arnold42f253b2014-06-25 12:39:17 -0700363TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700364 // Update check is allowed, response includes attributes for use in the
Gilad Arnold42f253b2014-06-25 12:39:17 -0700365 // request.
366 SetUpdateCheckAllowed(true);
367
368 // Override specific device policy attributes.
Gilad Arnoldd4b30322014-07-21 15:35:27 -0700369 fake_state_.device_policy_provider()->var_target_version_prefix()->
370 reset(new string("1.2"));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700371 fake_state_.device_policy_provider()->var_release_channel_delegated()->
372 reset(new bool(false));
373 fake_state_.device_policy_provider()->var_release_channel()->
374 reset(new string("foo-channel"));
375
376 UpdateCheckParams result;
377 ExpectPolicyStatus(EvalStatus::kSucceeded,
378 &Policy::UpdateCheckAllowed, &result);
379 EXPECT_TRUE(result.updates_enabled);
Gilad Arnoldd4b30322014-07-21 15:35:27 -0700380 EXPECT_EQ("1.2", result.target_version_prefix);
Gilad Arnold42f253b2014-06-25 12:39:17 -0700381 EXPECT_EQ("foo-channel", result.target_channel);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700382 EXPECT_FALSE(result.is_interactive);
Gilad Arnold42f253b2014-06-25 12:39:17 -0700383}
384
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700385TEST_F(UmChromeOSPolicyTest,
386 UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700387 // UpdateCheckAllowed should return kAskMeAgainLater if this is an unofficial
388 // build; we don't want periodic update checks on developer images.
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700389
390 fake_state_.system_provider()->var_is_official_build()->reset(
391 new bool(false));
392
393 UpdateCheckParams result;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700394 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700395 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700396}
397
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700398TEST_F(UmChromeOSPolicyTest,
399 UpdateCheckAllowedUpdatesDisabledForRemovableBootDevice) {
400 // UpdateCheckAllowed should return false (kSucceeded) if the image booted
401 // from a removable device.
402
403 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
404 new bool(true));
405
406 UpdateCheckParams result;
407 ExpectPolicyStatus(EvalStatus::kSucceeded,
408 &Policy::UpdateCheckAllowed, &result);
409 EXPECT_FALSE(result.updates_enabled);
410}
411
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700412TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700413 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
414 // is loaded and prohibits updates.
415
416 SetUpdateCheckAllowed(false);
417 fake_state_.device_policy_provider()->var_update_disabled()->reset(
418 new bool(true));
419
Gilad Arnold42f253b2014-06-25 12:39:17 -0700420 UpdateCheckParams result;
421 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
422 &Policy::UpdateCheckAllowed, &result);
423}
424
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700425TEST_F(UmChromeOSPolicyTest,
426 UpdateCheckAllowedForcedUpdateRequestedInteractive) {
427 // UpdateCheckAllowed should return true because a forced update request was
428 // signaled for an interactive update.
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700429
430 SetUpdateCheckAllowed(true);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700431 fake_state_.updater_provider()->var_forced_update_requested()->reset(
432 new UpdateRequestStatus(UpdateRequestStatus::kInteractive));
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700433
434 UpdateCheckParams result;
435 ExpectPolicyStatus(EvalStatus::kSucceeded,
436 &Policy::UpdateCheckAllowed, &result);
437 EXPECT_TRUE(result.updates_enabled);
438 EXPECT_TRUE(result.is_interactive);
439}
440
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700441TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedForcedUpdateRequestedPeriodic) {
442 // UpdateCheckAllowed should return true because a forced update request was
443 // signaled for a periodic check.
444
445 SetUpdateCheckAllowed(true);
446 fake_state_.updater_provider()->var_forced_update_requested()->reset(
447 new UpdateRequestStatus(UpdateRequestStatus::kPeriodic));
448
449 UpdateCheckParams result;
450 ExpectPolicyStatus(EvalStatus::kSucceeded,
451 &Policy::UpdateCheckAllowed, &result);
452 EXPECT_TRUE(result.updates_enabled);
453 EXPECT_FALSE(result.is_interactive);
454}
455
Alex Deymo63784a52014-05-28 10:46:14 -0700456TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700457 // The UpdateCanStart policy fails, not being able to query
458 // UpdateCheckAllowed.
459
460 // Configure the UpdateCheckAllowed policy to fail.
461 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
462
463 // Check that the UpdateCanStart fails.
464 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700465 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700466 ExpectPolicyStatus(EvalStatus::kFailed,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700467 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700468}
469
Alex Deymo63784a52014-05-28 10:46:14 -0700470TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700471 // The UpdateCanStart policy returns false because we are due for another
472 // update check.
473
474 SetUpdateCheckAllowed(true);
475
476 // Check that the UpdateCanStart returns false.
477 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700478 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700479 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700480 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700481 EXPECT_FALSE(result.update_can_start);
482 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
483}
484
Alex Deymo63784a52014-05-28 10:46:14 -0700485TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700486 // The UpdateCanStart policy returns true; no device policy is loaded.
487
488 SetUpdateCheckAllowed(false);
489 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
490 new bool(false));
491
492 // Check that the UpdateCanStart returns true with no further attributes.
493 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700494 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700495 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700496 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700497 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700498 EXPECT_FALSE(result.p2p_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 Arnoldf62a4b82014-05-01 07:41:07 -0700516 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700517 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700518 EXPECT_EQ(0, result.download_url_num_errors);
519 EXPECT_FALSE(result.do_increment_failures);
520}
521
522TEST_F(UmChromeOSPolicyTest,
523 UpdateCanStartNotAllowedBackoffNewWaitPeriodApplies) {
524 // The UpdateCanStart policy returns false; failures are reported and a new
525 // backoff period is enacted.
526
527 SetUpdateCheckAllowed(false);
528
529 const Time curr_time = fake_clock_.GetWallclockTime();
530 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
531 update_state.download_errors_max = 1;
532 update_state.download_errors.emplace_back(
533 0, ErrorCode::kDownloadTransferError,
534 curr_time - TimeDelta::FromSeconds(8));
535 update_state.download_errors.emplace_back(
536 0, ErrorCode::kDownloadTransferError,
537 curr_time - TimeDelta::FromSeconds(2));
538
539 // Check that UpdateCanStart returns false and a new backoff expiry is
540 // generated.
541 UpdateDownloadParams result;
542 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
543 update_state);
544 EXPECT_FALSE(result.update_can_start);
545 EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
546 EXPECT_TRUE(result.do_increment_failures);
547 EXPECT_LT(curr_time, result.backoff_expiry);
548}
549
550TEST_F(UmChromeOSPolicyTest,
551 UpdateCanStartNotAllowedBackoffPrevWaitPeriodStillApplies) {
552 // The UpdateCanStart policy returns false; a previously enacted backoff
553 // period still applies.
554
555 SetUpdateCheckAllowed(false);
556
557 const Time curr_time = fake_clock_.GetWallclockTime();
558 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
559 update_state.download_errors_max = 1;
560 update_state.download_errors.emplace_back(
561 0, ErrorCode::kDownloadTransferError,
562 curr_time - TimeDelta::FromSeconds(8));
563 update_state.download_errors.emplace_back(
564 0, ErrorCode::kDownloadTransferError,
565 curr_time - TimeDelta::FromSeconds(2));
566 update_state.failures_last_updated = curr_time;
567 update_state.backoff_expiry = curr_time + TimeDelta::FromMinutes(3);
568
569 // Check that UpdateCanStart returns false and a new backoff expiry is
570 // generated.
571 UpdateDownloadParams result;
572 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
573 &result, update_state);
574 EXPECT_FALSE(result.update_can_start);
575 EXPECT_EQ(UpdateCannotStartReason::kBackoff, result.cannot_start_reason);
576 EXPECT_FALSE(result.do_increment_failures);
577 EXPECT_LT(curr_time, result.backoff_expiry);
578}
579
580TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffSatisfied) {
581 // The UpdateCanStart policy returns true; a previously enacted backoff period
582 // has elapsed, we're good to go.
583
584 SetUpdateCheckAllowed(false);
585
586 const Time curr_time = fake_clock_.GetWallclockTime();
587 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
588 update_state.download_errors_max = 1;
589 update_state.download_errors.emplace_back(
590 0, ErrorCode::kDownloadTransferError,
591 curr_time - TimeDelta::FromSeconds(8));
592 update_state.download_errors.emplace_back(
593 0, ErrorCode::kDownloadTransferError,
594 curr_time - TimeDelta::FromSeconds(2));
595 update_state.failures_last_updated = curr_time - TimeDelta::FromSeconds(1);
596 update_state.backoff_expiry = curr_time - TimeDelta::FromSeconds(1);
597
598 // Check that UpdateCanStart returns false and a new backoff expiry is
599 // generated.
600 UpdateDownloadParams result;
601 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart,
602 &result, update_state);
603 EXPECT_TRUE(result.update_can_start);
604 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
605 EXPECT_EQ(0, result.download_url_idx);
606 EXPECT_EQ(0, result.download_url_num_errors);
607 EXPECT_FALSE(result.do_increment_failures);
608 EXPECT_EQ(Time(), result.backoff_expiry);
609}
610
611TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBackoffDisabled) {
612 // The UpdateCanStart policy returns false; failures are reported but backoff
613 // is disabled.
614
615 SetUpdateCheckAllowed(false);
616
617 const Time curr_time = fake_clock_.GetWallclockTime();
618 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
619 update_state.download_errors_max = 1;
620 update_state.download_errors.emplace_back(
621 0, ErrorCode::kDownloadTransferError,
622 curr_time - TimeDelta::FromSeconds(8));
623 update_state.download_errors.emplace_back(
624 0, ErrorCode::kDownloadTransferError,
625 curr_time - TimeDelta::FromSeconds(2));
626 update_state.is_backoff_disabled = true;
627
628 // Check that UpdateCanStart returns false and a new backoff expiry is
629 // generated.
630 UpdateDownloadParams result;
631 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
632 update_state);
633 EXPECT_TRUE(result.update_can_start);
634 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
635 EXPECT_EQ(0, result.download_url_idx);
636 EXPECT_EQ(0, result.download_url_num_errors);
637 EXPECT_TRUE(result.do_increment_failures);
638 EXPECT_EQ(Time(), result.backoff_expiry);
639}
640
641TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffInteractive) {
642 // The UpdateCanStart policy returns false; failures are reported but this is
643 // an interactive update check.
644
645 SetUpdateCheckAllowed(false);
646
647 const Time curr_time = fake_clock_.GetWallclockTime();
648 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
649 update_state.download_errors_max = 1;
650 update_state.download_errors.emplace_back(
651 0, ErrorCode::kDownloadTransferError,
652 curr_time - TimeDelta::FromSeconds(8));
653 update_state.download_errors.emplace_back(
654 0, ErrorCode::kDownloadTransferError,
655 curr_time - TimeDelta::FromSeconds(2));
656 update_state.is_interactive = true;
657
658 // Check that UpdateCanStart returns false and a new backoff expiry is
659 // generated.
660 UpdateDownloadParams result;
661 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
662 update_state);
663 EXPECT_TRUE(result.update_can_start);
664 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
665 EXPECT_EQ(0, result.download_url_idx);
666 EXPECT_EQ(0, result.download_url_num_errors);
667 EXPECT_TRUE(result.do_increment_failures);
668 EXPECT_EQ(Time(), result.backoff_expiry);
669}
670
671TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffDelta) {
672 // The UpdateCanStart policy returns false; failures are reported but this is
673 // a delta payload.
674
675 SetUpdateCheckAllowed(false);
676
677 const Time curr_time = fake_clock_.GetWallclockTime();
678 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
679 update_state.download_errors_max = 1;
680 update_state.download_errors.emplace_back(
681 0, ErrorCode::kDownloadTransferError,
682 curr_time - TimeDelta::FromSeconds(8));
683 update_state.download_errors.emplace_back(
684 0, ErrorCode::kDownloadTransferError,
685 curr_time - TimeDelta::FromSeconds(2));
686 update_state.is_delta_payload = true;
687
688 // Check that UpdateCanStart returns false and a new backoff expiry is
689 // generated.
690 UpdateDownloadParams result;
691 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
692 update_state);
693 EXPECT_TRUE(result.update_can_start);
694 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
695 EXPECT_EQ(0, result.download_url_idx);
696 EXPECT_EQ(0, result.download_url_num_errors);
697 EXPECT_TRUE(result.do_increment_failures);
698 EXPECT_EQ(Time(), result.backoff_expiry);
699}
700
701TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoBackoffUnofficialBuild) {
702 // The UpdateCanStart policy returns false; failures are reported but this is
703 // an unofficial build.
704
705 SetUpdateCheckAllowed(false);
706
707 const Time curr_time = fake_clock_.GetWallclockTime();
708 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(10));
709 update_state.download_errors_max = 1;
710 update_state.download_errors.emplace_back(
711 0, ErrorCode::kDownloadTransferError,
712 curr_time - TimeDelta::FromSeconds(8));
713 update_state.download_errors.emplace_back(
714 0, ErrorCode::kDownloadTransferError,
715 curr_time - TimeDelta::FromSeconds(2));
716
717 fake_state_.system_provider()->var_is_official_build()->
718 reset(new bool(false));
719
720 // Check that UpdateCanStart returns false and a new backoff expiry is
721 // generated.
722 UpdateDownloadParams result;
723 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
724 update_state);
725 EXPECT_TRUE(result.update_can_start);
726 EXPECT_EQ(UpdateCannotStartReason::kUndefined, result.cannot_start_reason);
727 EXPECT_EQ(0, result.download_url_idx);
728 EXPECT_EQ(0, result.download_url_num_errors);
729 EXPECT_TRUE(result.do_increment_failures);
730 EXPECT_EQ(Time(), result.backoff_expiry);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700731}
732
Alex Deymo63784a52014-05-28 10:46:14 -0700733TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700734 // The UpdateCanStart policy fails because the UpdateScattering policy it
735 // depends on fails (unset variable).
736
737 SetUpdateCheckAllowed(false);
738
739 // Override the default seed variable with a null value so that the policy
740 // request would fail.
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700741 // TODO(garnold) This failure may or may not fail a number
742 // sub-policies/decisions, like scattering and backoff. We'll need a more
743 // deliberate setup to ensure that we're failing what we want to be failing.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700744 fake_state_.random_provider()->var_seed()->reset(nullptr);
745
746 // Check that the UpdateCanStart fails.
747 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700748 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700749 ExpectPolicyStatus(EvalStatus::kFailed,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700750 &Policy::UpdateCanStart, &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700751}
752
Alex Deymo63784a52014-05-28 10:46:14 -0700753TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700754 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
755 // The UpdateCanStart policy returns false; device policy is loaded and
756 // scattering applies due to an unsatisfied wait period, which was newly
757 // generated.
758
759 SetUpdateCheckAllowed(false);
760 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
761 new TimeDelta(TimeDelta::FromMinutes(2)));
762
763
764 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
765
766 // Check that the UpdateCanStart returns false and a new wait period
767 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700768 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700769 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700770 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700771 EXPECT_FALSE(result.update_can_start);
772 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
773 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
774 EXPECT_EQ(0, result.scatter_check_threshold);
775}
776
Alex Deymo63784a52014-05-28 10:46:14 -0700777TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700778 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
779 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
780 // is loaded and a previously generated scattering period still applies, none
781 // of the scattering values has changed.
782
783 SetUpdateCheckAllowed(false);
784 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
785 new TimeDelta(TimeDelta::FromMinutes(2)));
786
787 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
788 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
789
790 // Check that the UpdateCanStart returns false and a new wait period
791 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700792 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700793 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700794 &result, update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700795 EXPECT_FALSE(result.update_can_start);
796 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
797 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
798 EXPECT_EQ(0, result.scatter_check_threshold);
799}
800
Alex Deymo63784a52014-05-28 10:46:14 -0700801TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700802 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
803 // The UpdateCanStart policy returns false; device policy is loaded and
804 // scattering applies due to an unsatisfied update check count threshold.
805 //
806 // This ensures a non-zero check threshold, which may or may not be combined
807 // with a non-zero wait period (for which we cannot reliably control).
808
809 SetUpdateCheckAllowed(false);
810 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
811 new TimeDelta(TimeDelta::FromSeconds(1)));
812
813 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
814 update_state.scatter_check_threshold_min = 2;
815 update_state.scatter_check_threshold_max = 5;
816
817 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700818 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700819 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700820 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700821 EXPECT_FALSE(result.update_can_start);
822 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
823 EXPECT_LE(2, result.scatter_check_threshold);
824 EXPECT_GE(5, result.scatter_check_threshold);
825}
826
Alex Deymo63784a52014-05-28 10:46:14 -0700827TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700828 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
829 // The UpdateCanStart policy returns false; device policy is loaded and
830 // scattering due to a previously generated count threshold still applies.
831
832 SetUpdateCheckAllowed(false);
833 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
834 new TimeDelta(TimeDelta::FromSeconds(1)));
835
836 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
837 update_state.scatter_check_threshold = 3;
838 update_state.scatter_check_threshold_min = 2;
839 update_state.scatter_check_threshold_max = 5;
840
841 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700842 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700843 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700844 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700845 EXPECT_FALSE(result.update_can_start);
846 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
847 EXPECT_EQ(3, result.scatter_check_threshold);
848}
849
Alex Deymo63784a52014-05-28 10:46:14 -0700850TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700851 // The UpdateCanStart policy returns true; device policy is loaded and
852 // scattering is enabled, but both wait period and check threshold are
853 // satisfied.
854
855 SetUpdateCheckAllowed(false);
856 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
857 new TimeDelta(TimeDelta::FromSeconds(120)));
858
859 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
860 update_state.num_checks = 4;
861 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
862 update_state.scatter_check_threshold = 3;
863 update_state.scatter_check_threshold_min = 2;
864 update_state.scatter_check_threshold_max = 5;
865
866 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700867 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700868 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700869 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700870 EXPECT_TRUE(result.update_can_start);
871 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
872 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700873 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700874 EXPECT_EQ(0, result.download_url_num_errors);
875 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700876}
877
Alex Deymo63784a52014-05-28 10:46:14 -0700878TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700879 UpdateCanStartAllowedInteractivePreventsScattering) {
880 // The UpdateCanStart policy returns true; device policy is loaded and
881 // scattering would have applied, except that the update check is interactive
882 // and so it is suppressed.
883
884 SetUpdateCheckAllowed(false);
885 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
886 new TimeDelta(TimeDelta::FromSeconds(1)));
887
888 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700889 update_state.is_interactive = true;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700890 update_state.scatter_check_threshold = 0;
891 update_state.scatter_check_threshold_min = 2;
892 update_state.scatter_check_threshold_max = 5;
893
894 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700895 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700896 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700897 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700898 EXPECT_TRUE(result.update_can_start);
899 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
900 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700901 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700902 EXPECT_EQ(0, result.download_url_num_errors);
903 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700904}
905
Alex Deymo63784a52014-05-28 10:46:14 -0700906TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700907 UpdateCanStartAllowedOobePreventsScattering) {
908 // The UpdateCanStart policy returns true; device policy is loaded and
909 // scattering would have applied, except that OOBE was not completed and so it
910 // is suppressed.
911
912 SetUpdateCheckAllowed(false);
913 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
914 new TimeDelta(TimeDelta::FromSeconds(1)));
915 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
916
917 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700918 update_state.is_interactive = true;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700919 update_state.scatter_check_threshold = 0;
920 update_state.scatter_check_threshold_min = 2;
921 update_state.scatter_check_threshold_max = 5;
922
923 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700924 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700925 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700926 update_state);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700927 EXPECT_TRUE(result.update_can_start);
928 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
929 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700930 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700931 EXPECT_EQ(0, result.download_url_num_errors);
932 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700933}
934
Alex Deymo63784a52014-05-28 10:46:14 -0700935TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700936 // The UpdateCanStart policy returns true; device policy permits both HTTP and
937 // P2P updates, as well as a non-empty target channel string.
938
939 SetUpdateCheckAllowed(false);
940
941 // Override specific device policy attributes.
942 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
943 new bool(true));
944 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
945 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700946
947 // Check that the UpdateCanStart returns true.
948 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700949 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700950 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700951 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700952 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700953 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700954 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700955 EXPECT_EQ(0, result.download_url_num_errors);
956 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700957}
958
Alex Deymo63784a52014-05-28 10:46:14 -0700959TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700960 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
961 // P2P updates, but the updater is configured to allow P2P and overrules the
962 // setting.
963
964 SetUpdateCheckAllowed(false);
965
966 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700967 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
968
969 // Check that the UpdateCanStart returns true.
970 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700971 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700972 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700973 update_state);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700974 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700975 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700976 EXPECT_EQ(0, result.download_url_idx);
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700977 EXPECT_EQ(0, result.download_url_num_errors);
978 EXPECT_FALSE(result.do_increment_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700979}
980
Gilad Arnold349ac832014-10-06 14:20:28 -0700981TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedP2PBlockedDueToNumAttempts) {
982 // The UpdateCanStart policy returns true; device policy permits HTTP but
983 // blocks P2P, because the max number of P2P downloads have been attempted.
984
985 SetUpdateCheckAllowed(false);
986
987 // Override specific device policy attributes.
988 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
989 new bool(true));
990 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
991 new bool(true));
992
993 // Check that the UpdateCanStart returns true.
994 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
995 update_state.p2p_num_attempts = ChromeOSPolicy::kMaxP2PAttempts;
996 UpdateDownloadParams result;
997 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
998 update_state);
999 EXPECT_TRUE(result.update_can_start);
1000 EXPECT_FALSE(result.p2p_allowed);
1001}
1002
1003TEST_F(UmChromeOSPolicyTest,
1004 UpdateCanStartAllowedP2PBlockedDueToAttemptsPeriod) {
1005 // The UpdateCanStart policy returns true; device policy permits HTTP but
1006 // blocks P2P, because the max period for attempt to download via P2P has
1007 // elapsed.
1008
1009 SetUpdateCheckAllowed(false);
1010
1011 // Override specific device policy attributes.
1012 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
1013 new bool(true));
1014 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
1015 new bool(true));
1016
1017 // Check that the UpdateCanStart returns true.
1018 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
1019 update_state.p2p_num_attempts = 1;
1020 update_state.p2p_first_attempted =
1021 fake_clock_.GetWallclockTime() -
1022 TimeDelta::FromSeconds(ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds + 1);
1023 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