blob: 140b6ba778ae866d0fc9a97595d12157ef9e8d71 [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 Arnoldb3b05442014-05-30 14:25:05 -07009#include <vector>
Alex Deymo0d11c602014-04-23 20:12:20 -070010
11#include <base/time/time.h>
12#include <gtest/gtest.h>
13
14#include "update_engine/fake_clock.h"
Alex Deymo63784a52014-05-28 10:46:14 -070015#include "update_engine/update_manager/evaluation_context.h"
16#include "update_engine/update_manager/fake_state.h"
17#include "update_engine/update_manager/umtest_utils.h"
Alex Deymo0d11c602014-04-23 20:12:20 -070018
19using base::Time;
20using base::TimeDelta;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070021using chromeos_update_engine::ErrorCode;
Alex Deymo0d11c602014-04-23 20:12:20 -070022using chromeos_update_engine::FakeClock;
Gilad Arnold0adbc942014-05-12 10:35:43 -070023using std::set;
Alex Deymo0d11c602014-04-23 20:12:20 -070024using std::string;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070025using std::vector;
Alex Deymo0d11c602014-04-23 20:12:20 -070026
Alex Deymo63784a52014-05-28 10:46:14 -070027namespace chromeos_update_manager {
Alex Deymo0d11c602014-04-23 20:12:20 -070028
Alex Deymo63784a52014-05-28 10:46:14 -070029class UmChromeOSPolicyTest : public ::testing::Test {
Alex Deymo0d11c602014-04-23 20:12:20 -070030 protected:
31 virtual void SetUp() {
32 SetUpDefaultClock();
33 eval_ctx_ = new EvaluationContext(&fake_clock_);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070034 SetUpDefaultState();
35 SetUpDefaultDevicePolicy();
Alex Deymo0d11c602014-04-23 20:12:20 -070036 }
37
38 // Sets the clock to fixed values.
39 void SetUpDefaultClock() {
40 fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
41 fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L));
42 }
43
44 void SetUpDefaultState() {
45 fake_state_.updater_provider()->var_updater_started_time()->reset(
46 new Time(fake_clock_.GetWallclockTime()));
47 fake_state_.updater_provider()->var_last_checked_time()->reset(
48 new Time(fake_clock_.GetWallclockTime()));
49 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
50 reset(new unsigned int(0));
51
52 fake_state_.random_provider()->var_seed()->reset(
53 new uint64_t(4)); // chosen by fair dice roll.
54 // guaranteed to be random.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070055
56 // No device policy loaded by default.
57 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
58 new bool(false));
59
Gilad Arnold76a11f62014-05-20 09:02:12 -070060 // For the purpose of the tests, this is an official build and OOBE was
61 // completed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070062 fake_state_.system_provider()->var_is_official_build()->reset(
63 new bool(true));
Gilad Arnold76a11f62014-05-20 09:02:12 -070064 fake_state_.system_provider()->var_is_oobe_complete()->reset(
65 new bool(true));
Gilad Arnold0adbc942014-05-12 10:35:43 -070066
67 // Connection is wifi, untethered.
68 fake_state_.shill_provider()->var_conn_type()->
69 reset(new ConnectionType(ConnectionType::kWifi));
70 fake_state_.shill_provider()->var_conn_tethering()->
71 reset(new ConnectionTethering(ConnectionTethering::kNotDetected));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070072 }
73
Gilad Arnoldb3b05442014-05-30 14:25:05 -070074 // Sets up a default device policy that does not impose any restrictions
75 // (HTTP) nor enables any features (P2P).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070076 void SetUpDefaultDevicePolicy() {
77 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
78 new bool(true));
79 fake_state_.device_policy_provider()->var_update_disabled()->reset(
80 new bool(false));
81 fake_state_.device_policy_provider()->
82 var_allowed_connection_types_for_update()->reset(nullptr);
83 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
84 new TimeDelta());
85 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
Gilad Arnoldb3b05442014-05-30 14:25:05 -070086 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070087 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
88 new bool(false));
89 fake_state_.device_policy_provider()->var_release_channel_delegated()->
90 reset(new bool(true));
91 }
92
93 // Configures the UpdateCheckAllowed policy to return a desired value by
94 // faking the current wall clock time as needed. Restores the default state.
95 // This is used when testing policies that depend on this one.
96 void SetUpdateCheckAllowed(bool allow_check) {
97 Time next_update_check;
98 ExpectPolicyStatus(EvalStatus::kSucceeded,
99 &ChromeOSPolicy::NextUpdateCheckTime,
100 &next_update_check);
101 SetUpDefaultState();
102 SetUpDefaultDevicePolicy();
103 Time curr_time = next_update_check;
104 if (allow_check)
105 curr_time += TimeDelta::FromSeconds(1);
106 else
107 curr_time -= TimeDelta::FromSeconds(1);
108 fake_clock_.SetWallclockTime(curr_time);
109 }
110
111 // Returns a default UpdateState structure: first seen time is calculated
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700112 // backward from the current wall clock time, update was seen just once;
113 // there's a single HTTP download URL with a maximum of 10 allowed failures;
114 // there is no scattering wait period and the max allowed is 7 days, there is
115 // no check threshold and none is allowed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700116 UpdateState GetDefaultUpdateState(TimeDelta update_first_seen_period) {
117 UpdateState update_state = {
118 fake_clock_.GetWallclockTime() - update_first_seen_period, 1,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700119 vector<string>(1, "http://fake/url/"), 10, 0, 0, vector<ErrorCode>(),
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700120 TimeDelta(), TimeDelta::FromDays(7), 0, 0, 0
121 };
122 return update_state;
Alex Deymo0d11c602014-04-23 20:12:20 -0700123 }
124
125 // Runs the passed |policy_method| policy and expects it to return the
126 // |expected| return value.
127 template<typename T, typename R, typename... Args>
128 void ExpectPolicyStatus(
129 EvalStatus expected,
130 T policy_method,
131 R* result, Args... args) {
132 string error = "<None>";
133 eval_ctx_->ResetEvaluation();
134 EXPECT_EQ(expected,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700135 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result,
136 args...))
Alex Deymoc850b4f2014-05-19 11:06:41 -0700137 << "Returned error: " << error
138 << "\nEvaluation context: " << eval_ctx_->DumpContext();
Alex Deymo0d11c602014-04-23 20:12:20 -0700139 }
140
141 FakeClock fake_clock_;
142 FakeState fake_state_;
143 scoped_refptr<EvaluationContext> eval_ctx_;
144 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
145};
146
Alex Deymo63784a52014-05-28 10:46:14 -0700147TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700148 Time next_update_check;
149
Alex Deymo0d11c602014-04-23 20:12:20 -0700150 ExpectPolicyStatus(EvalStatus::kSucceeded,
151 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
152
153 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
154 EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
155 ChromeOSPolicy::kTimeoutInitialInterval +
156 ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check);
157}
158
Alex Deymo63784a52014-05-28 10:46:14 -0700159TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700160 Time next_update_check;
161
Alex Deymo0d11c602014-04-23 20:12:20 -0700162 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
163 reset(new unsigned int(100));
164 ExpectPolicyStatus(EvalStatus::kSucceeded,
165 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
166
167 EXPECT_LE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
168 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
169 ChromeOSPolicy::kTimeoutRegularFuzz - 1), next_update_check);
170 EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
171 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
172 ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check);
173}
174
Alex Deymo63784a52014-05-28 10:46:14 -0700175TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700176 // We get the next update_check timestamp from the policy's private method
177 // and then we check the public method respects that value on the normal
178 // case.
179 Time next_update_check;
180 Time last_checked_time =
181 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
182
Alex Deymo0d11c602014-04-23 20:12:20 -0700183 fake_state_.updater_provider()->var_last_checked_time()->reset(
184 new Time(last_checked_time));
185 ExpectPolicyStatus(EvalStatus::kSucceeded,
186 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
187
188 UpdateCheckParams result;
189
190 // Check that the policy blocks until the next_update_check is reached.
191 SetUpDefaultClock();
192 SetUpDefaultState();
193 fake_state_.updater_provider()->var_last_checked_time()->reset(
194 new Time(last_checked_time));
195 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
196 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
197 &Policy::UpdateCheckAllowed, &result);
198
199 SetUpDefaultClock();
200 SetUpDefaultState();
201 fake_state_.updater_provider()->var_last_checked_time()->reset(
202 new Time(last_checked_time));
203 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
204 ExpectPolicyStatus(EvalStatus::kSucceeded,
205 &Policy::UpdateCheckAllowed, &result);
206}
207
Alex Deymo63784a52014-05-28 10:46:14 -0700208TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700209 // The UpdateCanStart policy fails, not being able to query
210 // UpdateCheckAllowed.
211
212 // Configure the UpdateCheckAllowed policy to fail.
213 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
214
215 // Check that the UpdateCanStart fails.
216 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
217 UpdateCanStartResult result;
218 ExpectPolicyStatus(EvalStatus::kFailed,
219 &Policy::UpdateCanStart, &result, false, update_state);
220}
221
Alex Deymo63784a52014-05-28 10:46:14 -0700222TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700223 // The UpdateCanStart policy returns false because we are due for another
224 // update check.
225
226 SetUpdateCheckAllowed(true);
227
228 // Check that the UpdateCanStart returns false.
229 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
230 UpdateCanStartResult result;
231 ExpectPolicyStatus(EvalStatus::kSucceeded,
232 &Policy::UpdateCanStart, &result, false, update_state);
233 EXPECT_FALSE(result.update_can_start);
234 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
235}
236
Alex Deymo63784a52014-05-28 10:46:14 -0700237TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700238 // The UpdateCanStart policy returns true; no device policy is loaded.
239
240 SetUpdateCheckAllowed(false);
241 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
242 new bool(false));
243
244 // Check that the UpdateCanStart returns true with no further attributes.
245 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
246 UpdateCanStartResult result;
247 ExpectPolicyStatus(EvalStatus::kSucceeded,
248 &Policy::UpdateCanStart, &result, false, update_state);
249 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700250 EXPECT_FALSE(result.p2p_allowed);
251 EXPECT_TRUE(result.target_channel.empty());
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700252 EXPECT_EQ(0, result.download_url_idx);
253 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700254}
255
Alex Deymo63784a52014-05-28 10:46:14 -0700256TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700257 // The UpdateCanStart policy returns true; device policy is loaded but imposes
258 // no restrictions on updating.
259
260 SetUpdateCheckAllowed(false);
261
262 // Check that the UpdateCanStart returns true.
263 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
264 UpdateCanStartResult result;
265 ExpectPolicyStatus(EvalStatus::kSucceeded,
266 &Policy::UpdateCanStart, &result, false, update_state);
267 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700268 EXPECT_FALSE(result.p2p_allowed);
269 EXPECT_TRUE(result.target_channel.empty());
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700270 EXPECT_EQ(0, result.download_url_idx);
271 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700272}
273
Alex Deymo63784a52014-05-28 10:46:14 -0700274TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedUpdatesDisabled) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700275 // The UpdateCanStart should return false (kAskMeAgainlater) because a device
276 // policy is loaded and prohibits updates.
277
278 SetUpdateCheckAllowed(false);
279 fake_state_.device_policy_provider()->var_update_disabled()->reset(
280 new bool(true));
281
282 // Check that the UpdateCanStart returns false.
283 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
284 UpdateCanStartResult result;
285 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
286 &Policy::UpdateCanStart, &result, false, update_state);
287 EXPECT_FALSE(result.update_can_start);
288 EXPECT_EQ(UpdateCannotStartReason::kDisabledByPolicy,
289 result.cannot_start_reason);
290}
291
Alex Deymo63784a52014-05-28 10:46:14 -0700292TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700293 // The UpdateCanStart policy fails because the UpdateScattering policy it
294 // depends on fails (unset variable).
295
296 SetUpdateCheckAllowed(false);
297
298 // Override the default seed variable with a null value so that the policy
299 // request would fail.
300 fake_state_.random_provider()->var_seed()->reset(nullptr);
301
302 // Check that the UpdateCanStart fails.
303 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
304 UpdateCanStartResult result;
305 ExpectPolicyStatus(EvalStatus::kFailed,
306 &Policy::UpdateCanStart, &result, false, update_state);
307}
308
Alex Deymo63784a52014-05-28 10:46:14 -0700309TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700310 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
311 // The UpdateCanStart policy returns false; device policy is loaded and
312 // scattering applies due to an unsatisfied wait period, which was newly
313 // generated.
314
315 SetUpdateCheckAllowed(false);
316 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
317 new TimeDelta(TimeDelta::FromMinutes(2)));
318
319
320 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
321
322 // Check that the UpdateCanStart returns false and a new wait period
323 // generated.
324 UpdateCanStartResult result;
325 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
326 false, update_state);
327 EXPECT_FALSE(result.update_can_start);
328 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
329 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
330 EXPECT_EQ(0, result.scatter_check_threshold);
331}
332
Alex Deymo63784a52014-05-28 10:46:14 -0700333TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700334 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
335 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
336 // is loaded and a previously generated scattering period still applies, none
337 // of the scattering values has changed.
338
339 SetUpdateCheckAllowed(false);
340 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
341 new TimeDelta(TimeDelta::FromMinutes(2)));
342
343 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
344 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
345
346 // Check that the UpdateCanStart returns false and a new wait period
347 // generated.
348 UpdateCanStartResult result;
349 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
350 &result, false, update_state);
351 EXPECT_FALSE(result.update_can_start);
352 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
353 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
354 EXPECT_EQ(0, result.scatter_check_threshold);
355}
356
Alex Deymo63784a52014-05-28 10:46:14 -0700357TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700358 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
359 // The UpdateCanStart policy returns false; device policy is loaded and
360 // scattering applies due to an unsatisfied update check count threshold.
361 //
362 // This ensures a non-zero check threshold, which may or may not be combined
363 // with a non-zero wait period (for which we cannot reliably control).
364
365 SetUpdateCheckAllowed(false);
366 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
367 new TimeDelta(TimeDelta::FromSeconds(1)));
368
369 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
370 update_state.scatter_check_threshold_min = 2;
371 update_state.scatter_check_threshold_max = 5;
372
373 // Check that the UpdateCanStart returns false.
374 UpdateCanStartResult result;
375 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
376 false, update_state);
377 EXPECT_FALSE(result.update_can_start);
378 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
379 EXPECT_LE(2, result.scatter_check_threshold);
380 EXPECT_GE(5, result.scatter_check_threshold);
381}
382
Alex Deymo63784a52014-05-28 10:46:14 -0700383TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700384 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
385 // The UpdateCanStart policy returns false; device policy is loaded and
386 // scattering due to a previously generated count threshold still applies.
387
388 SetUpdateCheckAllowed(false);
389 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
390 new TimeDelta(TimeDelta::FromSeconds(1)));
391
392 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
393 update_state.scatter_check_threshold = 3;
394 update_state.scatter_check_threshold_min = 2;
395 update_state.scatter_check_threshold_max = 5;
396
397 // Check that the UpdateCanStart returns false.
398 UpdateCanStartResult result;
399 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
400 false, update_state);
401 EXPECT_FALSE(result.update_can_start);
402 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
403 EXPECT_EQ(3, result.scatter_check_threshold);
404}
405
Alex Deymo63784a52014-05-28 10:46:14 -0700406TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700407 // The UpdateCanStart policy returns true; device policy is loaded and
408 // scattering is enabled, but both wait period and check threshold are
409 // satisfied.
410
411 SetUpdateCheckAllowed(false);
412 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
413 new TimeDelta(TimeDelta::FromSeconds(120)));
414
415 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
416 update_state.num_checks = 4;
417 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
418 update_state.scatter_check_threshold = 3;
419 update_state.scatter_check_threshold_min = 2;
420 update_state.scatter_check_threshold_max = 5;
421
422 // Check that the UpdateCanStart returns true.
423 UpdateCanStartResult result;
424 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
425 false, update_state);
426 EXPECT_TRUE(result.update_can_start);
427 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
428 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700429 EXPECT_EQ(0, result.download_url_idx);
430 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700431}
432
Alex Deymo63784a52014-05-28 10:46:14 -0700433TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700434 UpdateCanStartAllowedInteractivePreventsScattering) {
435 // The UpdateCanStart policy returns true; device policy is loaded and
436 // scattering would have applied, except that the update check is interactive
437 // and so it is suppressed.
438
439 SetUpdateCheckAllowed(false);
440 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
441 new TimeDelta(TimeDelta::FromSeconds(1)));
442
443 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
444 update_state.scatter_check_threshold = 0;
445 update_state.scatter_check_threshold_min = 2;
446 update_state.scatter_check_threshold_max = 5;
447
448 // Check that the UpdateCanStart returns true.
449 UpdateCanStartResult result;
450 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
451 true, update_state);
452 EXPECT_TRUE(result.update_can_start);
453 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
454 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700455 EXPECT_EQ(0, result.download_url_idx);
456 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700457}
458
Alex Deymo63784a52014-05-28 10:46:14 -0700459TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700460 UpdateCanStartAllowedOobePreventsScattering) {
461 // The UpdateCanStart policy returns true; device policy is loaded and
462 // scattering would have applied, except that OOBE was not completed and so it
463 // is suppressed.
464
465 SetUpdateCheckAllowed(false);
466 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
467 new TimeDelta(TimeDelta::FromSeconds(1)));
468 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
469
470 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
471 update_state.scatter_check_threshold = 0;
472 update_state.scatter_check_threshold_min = 2;
473 update_state.scatter_check_threshold_max = 5;
474
475 // Check that the UpdateCanStart returns true.
476 UpdateCanStartResult result;
477 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
478 true, update_state);
479 EXPECT_TRUE(result.update_can_start);
480 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
481 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700482 EXPECT_EQ(0, result.download_url_idx);
483 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700484}
485
Alex Deymo63784a52014-05-28 10:46:14 -0700486TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700487 // The UpdateCanStart policy returns true; device policy permits both HTTP and
488 // P2P updates, as well as a non-empty target channel string.
489
490 SetUpdateCheckAllowed(false);
491
492 // Override specific device policy attributes.
493 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
494 new bool(true));
495 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
496 new bool(true));
497 fake_state_.device_policy_provider()->var_release_channel_delegated()->
498 reset(new bool(false));
499 fake_state_.device_policy_provider()->var_release_channel()->
500 reset(new string("foo-channel"));
501
502 // Check that the UpdateCanStart returns true.
503 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
504 UpdateCanStartResult result;
505 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
506 false, update_state);
507 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700508 EXPECT_TRUE(result.p2p_allowed);
509 EXPECT_EQ("foo-channel", result.target_channel);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700510 EXPECT_EQ(0, result.download_url_idx);
511 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700512}
513
Alex Deymo63784a52014-05-28 10:46:14 -0700514TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700515 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
516 // P2P updates, but the updater is configured to allow P2P and overrules the
517 // setting.
518
519 SetUpdateCheckAllowed(false);
520
521 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700522 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
523
524 // Check that the UpdateCanStart returns true.
525 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
526 UpdateCanStartResult result;
527 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
528 false, update_state);
529 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700530 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700531 EXPECT_EQ(0, result.download_url_idx);
532 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700533}
534
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700535TEST_F(UmChromeOSPolicyTest,
536 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700537 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
538 // P2P updates, but marking this an unofficial build overrules the HTTP
539 // setting.
540
541 SetUpdateCheckAllowed(false);
542
543 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700544 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
545 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700546 fake_state_.system_provider()->var_is_official_build()->
547 reset(new bool(false));
548
549 // Check that the UpdateCanStart returns true.
550 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
551 UpdateCanStartResult result;
552 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
553 false, update_state);
554 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700555 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700556 EXPECT_EQ(0, result.download_url_idx);
557 EXPECT_EQ(0, result.download_url_num_failures);
558}
559
560TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
561 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
562 // P2P updates, but an HTTPS URL is provided and selected for download.
563
564 SetUpdateCheckAllowed(false);
565
566 // Override specific device policy attributes.
567 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
568 new bool(false));
569
570 // Add an HTTPS URL.
571 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
572 update_state.download_urls.push_back("https://secure/url/");
573
574 // Check that the UpdateCanStart returns true.
575 UpdateCanStartResult result;
576 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
577 false, update_state);
578 EXPECT_TRUE(result.update_can_start);
579 EXPECT_FALSE(result.p2p_allowed);
580 EXPECT_EQ(1, result.download_url_idx);
581 EXPECT_EQ(0, result.download_url_num_failures);
582}
583
584TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
585 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
586 // allowed number of failures, but a second URL is available.
587
588 SetUpdateCheckAllowed(false);
589
590 // Add a second URL; update with this URL attempted and failed enough times to
591 // disqualify the current (first) URL. This tests both the previously
592 // accounted failures (download_url_num_failures) as well as those occurring
593 // since the last call (download_url_error_codes).
594 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
595 update_state.num_checks = 10;
596 update_state.download_urls.push_back("http://another/fake/url/");
597 update_state.download_url_num_failures = 9;
598 update_state.download_url_error_codes.push_back(
599 ErrorCode::kDownloadTransferError);
600 update_state.download_url_error_codes.push_back(
601 ErrorCode::kDownloadWriteError);
602
603 // Check that the UpdateCanStart returns true.
604 UpdateCanStartResult result;
605 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
606 false, update_state);
607 EXPECT_TRUE(result.update_can_start);
608 EXPECT_FALSE(result.p2p_allowed);
609 EXPECT_EQ(1, result.download_url_idx);
610 EXPECT_EQ(0, result.download_url_num_failures);
611}
612
613TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
614 // The UpdateCanStart policy returns true; the first URL fails with a hard
615 // error, but a second URL is available.
616
617 SetUpdateCheckAllowed(false);
618
619 // Add a second URL; update with this URL attempted and failed in a way that
620 // causes it to switch directly to the next URL.
621 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
622 update_state.num_checks = 10;
623 update_state.download_urls.push_back("http://another/fake/url/");
624 update_state.download_url_error_codes.push_back(
625 ErrorCode::kPayloadHashMismatchError);
626
627 // Check that the UpdateCanStart returns true.
628 UpdateCanStartResult result;
629 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
630 false, update_state);
631 EXPECT_TRUE(result.update_can_start);
632 EXPECT_FALSE(result.p2p_allowed);
633 EXPECT_EQ(1, result.download_url_idx);
634 EXPECT_EQ(0, result.download_url_num_failures);
635}
636
637TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
638 // The UpdateCanStart policy returns true; URL search properly wraps around
639 // the last one on the list.
640
641 SetUpdateCheckAllowed(false);
642
643 // Add a second URL; update with this URL attempted and failed in a way that
644 // causes it to switch directly to the next URL.
645 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
646 update_state.num_checks = 10;
647 update_state.download_urls.push_back("http://another/fake/url/");
648 update_state.download_url_idx = 1;
649 update_state.download_url_error_codes.push_back(
650 ErrorCode::kPayloadHashMismatchError);
651
652 // Check that the UpdateCanStart returns true.
653 UpdateCanStartResult result;
654 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
655 false, update_state);
656 EXPECT_TRUE(result.update_can_start);
657 EXPECT_FALSE(result.p2p_allowed);
658 EXPECT_EQ(0, result.download_url_idx);
659 EXPECT_EQ(0, result.download_url_num_failures);
660}
661
662TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
663 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
664 // use is forbidden by policy.
665
666 SetUpdateCheckAllowed(false);
667
668 // Override specific device policy attributes.
669 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
670 new bool(false));
671
672 // Check that the UpdateCanStart returns false.
673 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
674 UpdateCanStartResult result;
675 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
676 false, update_state);
677}
678
679TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
680 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
681 // use is forbidden by policy, however P2P is enabled. The result indicates
682 // that no URL can be used.
683
684 SetUpdateCheckAllowed(false);
685
686 // Override specific device policy attributes.
687 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
688 new bool(true));
689 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
690 new bool(false));
691
692 // Check that the UpdateCanStart returns true.
693 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
694 UpdateCanStartResult result;
695 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
696 false, update_state);
697 EXPECT_TRUE(result.update_can_start);
698 EXPECT_TRUE(result.p2p_allowed);
699 EXPECT_GT(0, result.download_url_idx);
700 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700701}
702
Alex Deymo63784a52014-05-28 10:46:14 -0700703TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700704 // Ethernet is always allowed.
705
706 fake_state_.shill_provider()->var_conn_type()->
707 reset(new ConnectionType(ConnectionType::kEthernet));
708
709 bool result;
710 ExpectPolicyStatus(EvalStatus::kSucceeded,
711 &Policy::UpdateCurrentConnectionAllowed, &result);
712 EXPECT_TRUE(result);
713}
714
Alex Deymo63784a52014-05-28 10:46:14 -0700715TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700716 // Wifi is allowed if not tethered.
717
718 fake_state_.shill_provider()->var_conn_type()->
719 reset(new ConnectionType(ConnectionType::kWifi));
720
721 bool result;
722 ExpectPolicyStatus(EvalStatus::kSucceeded,
723 &Policy::UpdateCurrentConnectionAllowed, &result);
724 EXPECT_TRUE(result);
725}
726
Alex Deymo63784a52014-05-28 10:46:14 -0700727TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700728 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
729 // Tethered wifi is not allowed by default.
730
731 fake_state_.shill_provider()->var_conn_type()->
732 reset(new ConnectionType(ConnectionType::kWifi));
733 fake_state_.shill_provider()->var_conn_tethering()->
734 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
735
736 bool result;
737 ExpectPolicyStatus(EvalStatus::kSucceeded,
738 &Policy::UpdateCurrentConnectionAllowed, &result);
739 EXPECT_FALSE(result);
740}
741
Alex Deymo63784a52014-05-28 10:46:14 -0700742TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700743 UpdateCurrentConnectionAllowedWifiTetheredPolicyOverride) {
744 // Tethered wifi can be allowed by policy.
745
746 fake_state_.shill_provider()->var_conn_type()->
747 reset(new ConnectionType(ConnectionType::kWifi));
748 fake_state_.shill_provider()->var_conn_tethering()->
749 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
750 set<ConnectionType> allowed_connections;
751 allowed_connections.insert(ConnectionType::kCellular);
752 fake_state_.device_policy_provider()->
753 var_allowed_connection_types_for_update()->
754 reset(new set<ConnectionType>(allowed_connections));
755
756 bool result;
757 ExpectPolicyStatus(EvalStatus::kSucceeded,
758 &Policy::UpdateCurrentConnectionAllowed, &result);
759 EXPECT_TRUE(result);
760}
761
Alex Deymo63784a52014-05-28 10:46:14 -0700762TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700763 // Wimax is always allowed.
764
765 fake_state_.shill_provider()->var_conn_type()->
766 reset(new ConnectionType(ConnectionType::kWifi));
767
768 bool result;
769 ExpectPolicyStatus(EvalStatus::kSucceeded,
770 &Policy::UpdateCurrentConnectionAllowed, &result);
771 EXPECT_TRUE(result);
772}
773
Alex Deymo63784a52014-05-28 10:46:14 -0700774TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700775 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
776 // Bluetooth is never allowed.
777
778 fake_state_.shill_provider()->var_conn_type()->
779 reset(new ConnectionType(ConnectionType::kBluetooth));
780
781 bool result;
782 ExpectPolicyStatus(EvalStatus::kSucceeded,
783 &Policy::UpdateCurrentConnectionAllowed, &result);
784 EXPECT_FALSE(result);
785}
786
Alex Deymo63784a52014-05-28 10:46:14 -0700787TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700788 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
789 // Bluetooth cannot be allowed even by policy.
790
791 fake_state_.shill_provider()->var_conn_type()->
792 reset(new ConnectionType(ConnectionType::kBluetooth));
793 set<ConnectionType> allowed_connections;
794 allowed_connections.insert(ConnectionType::kBluetooth);
795 fake_state_.device_policy_provider()->
796 var_allowed_connection_types_for_update()->
797 reset(new set<ConnectionType>(allowed_connections));
798
799 bool result;
800 ExpectPolicyStatus(EvalStatus::kSucceeded,
801 &Policy::UpdateCurrentConnectionAllowed, &result);
802 EXPECT_FALSE(result);
803}
804
Alex Deymo63784a52014-05-28 10:46:14 -0700805TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700806 // Cellular is not allowed by default.
807
808 fake_state_.shill_provider()->var_conn_type()->
809 reset(new ConnectionType(ConnectionType::kCellular));
810
811 bool result;
812 ExpectPolicyStatus(EvalStatus::kSucceeded,
813 &Policy::UpdateCurrentConnectionAllowed, &result);
814 EXPECT_FALSE(result);
815}
816
Alex Deymo63784a52014-05-28 10:46:14 -0700817TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700818 UpdateCurrentConnectionAllowedCellularPolicyOverride) {
819 // Update over cellular can be enabled by policy.
820
821 fake_state_.shill_provider()->var_conn_type()->
822 reset(new ConnectionType(ConnectionType::kCellular));
823 set<ConnectionType> allowed_connections;
824 allowed_connections.insert(ConnectionType::kCellular);
825 fake_state_.device_policy_provider()->
826 var_allowed_connection_types_for_update()->
827 reset(new set<ConnectionType>(allowed_connections));
828
829 bool result;
830 ExpectPolicyStatus(EvalStatus::kSucceeded,
831 &Policy::UpdateCurrentConnectionAllowed, &result);
832 EXPECT_TRUE(result);
833}
834
Alex Deymo63784a52014-05-28 10:46:14 -0700835TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700836 UpdateCurrentConnectionAllowedCellularUserOverride) {
837 // Update over cellular can be enabled by user settings, but only if policy
838 // is present and does not determine allowed connections.
839
840 fake_state_.shill_provider()->var_conn_type()->
841 reset(new ConnectionType(ConnectionType::kCellular));
842 set<ConnectionType> allowed_connections;
843 allowed_connections.insert(ConnectionType::kCellular);
844 fake_state_.updater_provider()->var_cellular_enabled()->
845 reset(new bool(true));
846
847 bool result;
848 ExpectPolicyStatus(EvalStatus::kSucceeded,
849 &Policy::UpdateCurrentConnectionAllowed, &result);
850 EXPECT_TRUE(result);
851}
852
Alex Deymo63784a52014-05-28 10:46:14 -0700853} // namespace chromeos_update_manager