blob: d231a7c8e1ba9bf17e53c4094d782ab5e3ce9eb5 [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();
Gilad Arnoldb2271992014-06-19 12:35:24 -070033 eval_ctx_ = new EvaluationContext(&fake_clock_, TimeDelta::FromSeconds(5));
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()->
Gilad Arnold684219d2014-07-07 14:54:57 -070050 reset(new unsigned int(0)); // NOLINT(readability/casting)
Gilad Arnolda0258a52014-07-10 16:21:19 -070051 fake_state_.updater_provider()->var_server_dictated_poll_interval()->
52 reset(new unsigned int(0)); // NOLINT(readability/casting)
Gilad Arnold44dc3bf2014-07-18 23:39:38 -070053 fake_state_.updater_provider()->var_interactive_update_requested()->
54 reset(new bool(false)); // NOLINT(readability/casting)
Alex Deymo0d11c602014-04-23 20:12:20 -070055
56 fake_state_.random_provider()->var_seed()->reset(
57 new uint64_t(4)); // chosen by fair dice roll.
58 // guaranteed to be random.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070059
60 // No device policy loaded by default.
61 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
62 new bool(false));
63
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070064 // OOBE is enabled by default.
65 fake_state_.config_provider()->var_is_oobe_enabled()->reset(
66 new bool(true));
67
Gilad Arnold76a11f62014-05-20 09:02:12 -070068 // For the purpose of the tests, this is an official build and OOBE was
69 // completed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070070 fake_state_.system_provider()->var_is_official_build()->reset(
71 new bool(true));
Gilad Arnold76a11f62014-05-20 09:02:12 -070072 fake_state_.system_provider()->var_is_oobe_complete()->reset(
73 new bool(true));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -070074 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
75 new bool(false));
Gilad Arnold0adbc942014-05-12 10:35:43 -070076
77 // Connection is wifi, untethered.
78 fake_state_.shill_provider()->var_conn_type()->
79 reset(new ConnectionType(ConnectionType::kWifi));
80 fake_state_.shill_provider()->var_conn_tethering()->
81 reset(new ConnectionTethering(ConnectionTethering::kNotDetected));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070082 }
83
Gilad Arnoldb3b05442014-05-30 14:25:05 -070084 // Sets up a default device policy that does not impose any restrictions
85 // (HTTP) nor enables any features (P2P).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070086 void SetUpDefaultDevicePolicy() {
87 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
88 new bool(true));
89 fake_state_.device_policy_provider()->var_update_disabled()->reset(
90 new bool(false));
91 fake_state_.device_policy_provider()->
92 var_allowed_connection_types_for_update()->reset(nullptr);
93 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
94 new TimeDelta());
95 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
Gilad Arnoldb3b05442014-05-30 14:25:05 -070096 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070097 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
98 new bool(false));
99 fake_state_.device_policy_provider()->var_release_channel_delegated()->
100 reset(new bool(true));
101 }
102
103 // Configures the UpdateCheckAllowed policy to return a desired value by
104 // faking the current wall clock time as needed. Restores the default state.
105 // This is used when testing policies that depend on this one.
106 void SetUpdateCheckAllowed(bool allow_check) {
107 Time next_update_check;
108 ExpectPolicyStatus(EvalStatus::kSucceeded,
109 &ChromeOSPolicy::NextUpdateCheckTime,
110 &next_update_check);
111 SetUpDefaultState();
112 SetUpDefaultDevicePolicy();
113 Time curr_time = next_update_check;
114 if (allow_check)
115 curr_time += TimeDelta::FromSeconds(1);
116 else
117 curr_time -= TimeDelta::FromSeconds(1);
118 fake_clock_.SetWallclockTime(curr_time);
119 }
120
121 // Returns a default UpdateState structure: first seen time is calculated
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700122 // backward from the current wall clock time, update was seen just once;
123 // there's a single HTTP download URL with a maximum of 10 allowed failures;
124 // there is no scattering wait period and the max allowed is 7 days, there is
125 // no check threshold and none is allowed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700126 UpdateState GetDefaultUpdateState(TimeDelta update_first_seen_period) {
127 UpdateState update_state = {
128 fake_clock_.GetWallclockTime() - update_first_seen_period, 1,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700129 vector<string>(1, "http://fake/url/"), 10, 0, 0, vector<ErrorCode>(),
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700130 TimeDelta(), TimeDelta::FromDays(7), 0, 0, 0
131 };
132 return update_state;
Alex Deymo0d11c602014-04-23 20:12:20 -0700133 }
134
135 // Runs the passed |policy_method| policy and expects it to return the
136 // |expected| return value.
137 template<typename T, typename R, typename... Args>
138 void ExpectPolicyStatus(
139 EvalStatus expected,
140 T policy_method,
141 R* result, Args... args) {
142 string error = "<None>";
143 eval_ctx_->ResetEvaluation();
144 EXPECT_EQ(expected,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700145 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result,
146 args...))
Alex Deymoc850b4f2014-05-19 11:06:41 -0700147 << "Returned error: " << error
148 << "\nEvaluation context: " << eval_ctx_->DumpContext();
Alex Deymo0d11c602014-04-23 20:12:20 -0700149 }
150
151 FakeClock fake_clock_;
152 FakeState fake_state_;
153 scoped_refptr<EvaluationContext> eval_ctx_;
154 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
155};
156
Alex Deymo63784a52014-05-28 10:46:14 -0700157TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700158 Time next_update_check;
159
Gilad Arnold38b14022014-07-09 12:45:56 -0700160 // Set the last update time so it'll appear as if this is a first update check
161 // in the lifetime of the current updater.
162 fake_state_.updater_provider()->var_last_checked_time()->reset(
163 new Time(fake_clock_.GetWallclockTime() - TimeDelta::FromMinutes(10)));
164
Alex Deymo0d11c602014-04-23 20:12:20 -0700165 ExpectPolicyStatus(EvalStatus::kSucceeded,
166 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
167
168 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
Gilad Arnold38b14022014-07-09 12:45:56 -0700169 EXPECT_GE(
170 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
171 ChromeOSPolicy::kTimeoutInitialInterval +
172 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
173 next_update_check);
174}
175
176TEST_F(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz) {
177 // Ensure that we're using the correct interval (kPeriodicInterval) and fuzz
178 // (kTimeoutRegularFuzz) as base values for period updates.
179 Time next_update_check;
180
181 ExpectPolicyStatus(EvalStatus::kSucceeded,
182 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
183
184 EXPECT_LE(
185 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
186 ChromeOSPolicy::kTimeoutPeriodicInterval -
187 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
188 next_update_check);
189 EXPECT_GE(
190 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
191 ChromeOSPolicy::kTimeoutPeriodicInterval +
192 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
193 next_update_check);
194}
195
196TEST_F(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz) {
197 // Ensure that we're properly backing off and fuzzing in the presence of
198 // failed updates attempts.
199 Time next_update_check;
200
201 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
202 reset(new unsigned int(2)); // NOLINT(readability/casting)
203
204 ExpectPolicyStatus(EvalStatus::kSucceeded,
205 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
206
207 int expected_interval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
208 EXPECT_LE(
209 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
210 expected_interval - expected_interval / 2),
211 next_update_check);
212 EXPECT_GE(
213 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
214 expected_interval + expected_interval / 2),
215 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700216}
217
Gilad Arnolda0258a52014-07-10 16:21:19 -0700218TEST_F(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval) {
219 // Policy honors the server provided check poll interval.
220 Time next_update_check;
221
222 const unsigned int kInterval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
223 fake_state_.updater_provider()->var_server_dictated_poll_interval()->
224 reset(new unsigned int(kInterval)); // NOLINT(readability/casting)
225 // We should not be backing off in this case.
226 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
227 reset(new unsigned int(2)); // NOLINT(readability/casting)
228
229 ExpectPolicyStatus(EvalStatus::kSucceeded,
230 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
231
232 EXPECT_LE(
233 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
234 kInterval - kInterval / 2),
235 next_update_check);
236 EXPECT_GE(
237 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
238 kInterval + kInterval / 2),
239 next_update_check);
240}
241
Alex Deymo63784a52014-05-28 10:46:14 -0700242TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700243 Time next_update_check;
244
Alex Deymo0d11c602014-04-23 20:12:20 -0700245 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnold684219d2014-07-07 14:54:57 -0700246 reset(new unsigned int(100)); // NOLINT(readability/casting)
Gilad Arnold38b14022014-07-09 12:45:56 -0700247
Alex Deymo0d11c602014-04-23 20:12:20 -0700248 ExpectPolicyStatus(EvalStatus::kSucceeded,
249 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
250
Gilad Arnold38b14022014-07-09 12:45:56 -0700251 EXPECT_LE(
252 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
253 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
254 ChromeOSPolicy::kTimeoutMaxBackoffInterval / 2),
255 next_update_check);
256 EXPECT_GE(
257 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
258 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
259 ChromeOSPolicy::kTimeoutMaxBackoffInterval /2),
260 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700261}
262
Alex Deymo63784a52014-05-28 10:46:14 -0700263TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700264 // We get the next update_check timestamp from the policy's private method
265 // and then we check the public method respects that value on the normal
266 // case.
267 Time next_update_check;
268 Time last_checked_time =
269 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
270
Alex Deymo0d11c602014-04-23 20:12:20 -0700271 fake_state_.updater_provider()->var_last_checked_time()->reset(
272 new Time(last_checked_time));
273 ExpectPolicyStatus(EvalStatus::kSucceeded,
274 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
275
276 UpdateCheckParams result;
277
278 // Check that the policy blocks until the next_update_check is reached.
279 SetUpDefaultClock();
280 SetUpDefaultState();
281 fake_state_.updater_provider()->var_last_checked_time()->reset(
282 new Time(last_checked_time));
283 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
284 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
285 &Policy::UpdateCheckAllowed, &result);
286
287 SetUpDefaultClock();
288 SetUpDefaultState();
289 fake_state_.updater_provider()->var_last_checked_time()->reset(
290 new Time(last_checked_time));
291 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
292 ExpectPolicyStatus(EvalStatus::kSucceeded,
293 &Policy::UpdateCheckAllowed, &result);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700294 EXPECT_TRUE(result.updates_enabled);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700295 EXPECT_FALSE(result.is_interactive);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700296}
297
298TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700299 // Update checks are deferred until OOBE is completed.
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700300
301 // Ensure that update is not allowed even if wait period is satisfied.
302 Time next_update_check;
303 Time last_checked_time =
304 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
305
306 fake_state_.updater_provider()->var_last_checked_time()->reset(
307 new Time(last_checked_time));
308 ExpectPolicyStatus(EvalStatus::kSucceeded,
309 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
310
311 SetUpDefaultClock();
312 SetUpDefaultState();
313 fake_state_.updater_provider()->var_last_checked_time()->reset(
314 new Time(last_checked_time));
315 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
316 fake_state_.system_provider()->var_is_oobe_complete()->reset(
317 new bool(false));
318
319 UpdateCheckParams result;
320 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
321 &Policy::UpdateCheckAllowed, &result);
322
323 // Now check that it is allowed if OOBE is completed.
324 SetUpDefaultClock();
325 SetUpDefaultState();
326 fake_state_.updater_provider()->var_last_checked_time()->reset(
327 new Time(last_checked_time));
328 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
329 ExpectPolicyStatus(EvalStatus::kSucceeded,
330 &Policy::UpdateCheckAllowed, &result);
331 EXPECT_TRUE(result.updates_enabled);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700332 EXPECT_FALSE(result.is_interactive);
Alex Deymo0d11c602014-04-23 20:12:20 -0700333}
334
Gilad Arnold42f253b2014-06-25 12:39:17 -0700335TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700336 // Update check is allowed, response includes attributes for use in the
Gilad Arnold42f253b2014-06-25 12:39:17 -0700337 // request.
338 SetUpdateCheckAllowed(true);
339
340 // Override specific device policy attributes.
Gilad Arnoldd4b30322014-07-21 15:35:27 -0700341 fake_state_.device_policy_provider()->var_target_version_prefix()->
342 reset(new string("1.2"));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700343 fake_state_.device_policy_provider()->var_release_channel_delegated()->
344 reset(new bool(false));
345 fake_state_.device_policy_provider()->var_release_channel()->
346 reset(new string("foo-channel"));
347
348 UpdateCheckParams result;
349 ExpectPolicyStatus(EvalStatus::kSucceeded,
350 &Policy::UpdateCheckAllowed, &result);
351 EXPECT_TRUE(result.updates_enabled);
Gilad Arnoldd4b30322014-07-21 15:35:27 -0700352 EXPECT_EQ("1.2", result.target_version_prefix);
Gilad Arnold42f253b2014-06-25 12:39:17 -0700353 EXPECT_EQ("foo-channel", result.target_channel);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700354 EXPECT_FALSE(result.is_interactive);
Gilad Arnold42f253b2014-06-25 12:39:17 -0700355}
356
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700357TEST_F(UmChromeOSPolicyTest,
358 UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700359 // UpdateCheckAllowed should return kAskMeAgainLater if this is an unofficial
360 // build; we don't want periodic update checks on developer images.
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700361
362 fake_state_.system_provider()->var_is_official_build()->reset(
363 new bool(false));
364
365 UpdateCheckParams result;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700366 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700367 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700368}
369
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700370TEST_F(UmChromeOSPolicyTest,
371 UpdateCheckAllowedUpdatesDisabledForRemovableBootDevice) {
372 // UpdateCheckAllowed should return false (kSucceeded) if the image booted
373 // from a removable device.
374
375 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
376 new bool(true));
377
378 UpdateCheckParams result;
379 ExpectPolicyStatus(EvalStatus::kSucceeded,
380 &Policy::UpdateCheckAllowed, &result);
381 EXPECT_FALSE(result.updates_enabled);
382}
383
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700384TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700385 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
386 // is loaded and prohibits updates.
387
388 SetUpdateCheckAllowed(false);
389 fake_state_.device_policy_provider()->var_update_disabled()->reset(
390 new bool(true));
391
Gilad Arnold42f253b2014-06-25 12:39:17 -0700392 UpdateCheckParams result;
393 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
394 &Policy::UpdateCheckAllowed, &result);
395}
396
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700397TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedInteractiveUpdateRequested) {
398 // UpdateCheckAllowed should return true because an interactive update request
399 // was signaled.
400
401 SetUpdateCheckAllowed(true);
402 fake_state_.updater_provider()->var_interactive_update_requested()->reset(
403 new bool(true));
404
405 UpdateCheckParams result;
406 ExpectPolicyStatus(EvalStatus::kSucceeded,
407 &Policy::UpdateCheckAllowed, &result);
408 EXPECT_TRUE(result.updates_enabled);
409 EXPECT_TRUE(result.is_interactive);
410}
411
Alex Deymo63784a52014-05-28 10:46:14 -0700412TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700413 // The UpdateCanStart policy fails, not being able to query
414 // UpdateCheckAllowed.
415
416 // Configure the UpdateCheckAllowed policy to fail.
417 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
418
419 // Check that the UpdateCanStart fails.
420 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700421 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700422 ExpectPolicyStatus(EvalStatus::kFailed,
423 &Policy::UpdateCanStart, &result, false, update_state);
424}
425
Alex Deymo63784a52014-05-28 10:46:14 -0700426TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700427 // The UpdateCanStart policy returns false because we are due for another
428 // update check.
429
430 SetUpdateCheckAllowed(true);
431
432 // Check that the UpdateCanStart returns false.
433 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700434 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700435 ExpectPolicyStatus(EvalStatus::kSucceeded,
436 &Policy::UpdateCanStart, &result, false, update_state);
437 EXPECT_FALSE(result.update_can_start);
438 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
439}
440
Alex Deymo63784a52014-05-28 10:46:14 -0700441TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700442 // The UpdateCanStart policy returns true; no device policy is loaded.
443
444 SetUpdateCheckAllowed(false);
445 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
446 new bool(false));
447
448 // Check that the UpdateCanStart returns true with no further attributes.
449 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700450 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700451 ExpectPolicyStatus(EvalStatus::kSucceeded,
452 &Policy::UpdateCanStart, &result, false, update_state);
453 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700454 EXPECT_FALSE(result.p2p_allowed);
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, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700460 // The UpdateCanStart policy returns true; device policy is loaded but imposes
461 // no restrictions on updating.
462
463 SetUpdateCheckAllowed(false);
464
465 // Check that the UpdateCanStart returns true.
466 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700467 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700468 ExpectPolicyStatus(EvalStatus::kSucceeded,
469 &Policy::UpdateCanStart, &result, false, update_state);
470 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700471 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700472 EXPECT_EQ(0, result.download_url_idx);
473 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700474}
475
Alex Deymo63784a52014-05-28 10:46:14 -0700476TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700477 // The UpdateCanStart policy fails because the UpdateScattering policy it
478 // depends on fails (unset variable).
479
480 SetUpdateCheckAllowed(false);
481
482 // Override the default seed variable with a null value so that the policy
483 // request would fail.
484 fake_state_.random_provider()->var_seed()->reset(nullptr);
485
486 // Check that the UpdateCanStart fails.
487 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700488 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700489 ExpectPolicyStatus(EvalStatus::kFailed,
490 &Policy::UpdateCanStart, &result, false, update_state);
491}
492
Alex Deymo63784a52014-05-28 10:46:14 -0700493TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700494 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
495 // The UpdateCanStart policy returns false; device policy is loaded and
496 // scattering applies due to an unsatisfied wait period, which was newly
497 // generated.
498
499 SetUpdateCheckAllowed(false);
500 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
501 new TimeDelta(TimeDelta::FromMinutes(2)));
502
503
504 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
505
506 // Check that the UpdateCanStart returns false and a new wait period
507 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700508 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700509 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
510 false, update_state);
511 EXPECT_FALSE(result.update_can_start);
512 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
513 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
514 EXPECT_EQ(0, result.scatter_check_threshold);
515}
516
Alex Deymo63784a52014-05-28 10:46:14 -0700517TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700518 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
519 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
520 // is loaded and a previously generated scattering period still applies, none
521 // of the scattering values has changed.
522
523 SetUpdateCheckAllowed(false);
524 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
525 new TimeDelta(TimeDelta::FromMinutes(2)));
526
527 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
528 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
529
530 // Check that the UpdateCanStart returns false and a new wait period
531 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700532 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700533 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
534 &result, false, update_state);
535 EXPECT_FALSE(result.update_can_start);
536 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
537 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
538 EXPECT_EQ(0, result.scatter_check_threshold);
539}
540
Alex Deymo63784a52014-05-28 10:46:14 -0700541TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700542 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
543 // The UpdateCanStart policy returns false; device policy is loaded and
544 // scattering applies due to an unsatisfied update check count threshold.
545 //
546 // This ensures a non-zero check threshold, which may or may not be combined
547 // with a non-zero wait period (for which we cannot reliably control).
548
549 SetUpdateCheckAllowed(false);
550 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
551 new TimeDelta(TimeDelta::FromSeconds(1)));
552
553 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
554 update_state.scatter_check_threshold_min = 2;
555 update_state.scatter_check_threshold_max = 5;
556
557 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700558 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700559 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
560 false, update_state);
561 EXPECT_FALSE(result.update_can_start);
562 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
563 EXPECT_LE(2, result.scatter_check_threshold);
564 EXPECT_GE(5, result.scatter_check_threshold);
565}
566
Alex Deymo63784a52014-05-28 10:46:14 -0700567TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700568 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
569 // The UpdateCanStart policy returns false; device policy is loaded and
570 // scattering due to a previously generated count threshold still applies.
571
572 SetUpdateCheckAllowed(false);
573 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
574 new TimeDelta(TimeDelta::FromSeconds(1)));
575
576 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
577 update_state.scatter_check_threshold = 3;
578 update_state.scatter_check_threshold_min = 2;
579 update_state.scatter_check_threshold_max = 5;
580
581 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700582 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700583 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
584 false, update_state);
585 EXPECT_FALSE(result.update_can_start);
586 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
587 EXPECT_EQ(3, result.scatter_check_threshold);
588}
589
Alex Deymo63784a52014-05-28 10:46:14 -0700590TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700591 // The UpdateCanStart policy returns true; device policy is loaded and
592 // scattering is enabled, but both wait period and check threshold are
593 // satisfied.
594
595 SetUpdateCheckAllowed(false);
596 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
597 new TimeDelta(TimeDelta::FromSeconds(120)));
598
599 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
600 update_state.num_checks = 4;
601 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
602 update_state.scatter_check_threshold = 3;
603 update_state.scatter_check_threshold_min = 2;
604 update_state.scatter_check_threshold_max = 5;
605
606 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700607 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700608 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
609 false, update_state);
610 EXPECT_TRUE(result.update_can_start);
611 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
612 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700613 EXPECT_EQ(0, result.download_url_idx);
614 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700615}
616
Alex Deymo63784a52014-05-28 10:46:14 -0700617TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700618 UpdateCanStartAllowedInteractivePreventsScattering) {
619 // The UpdateCanStart policy returns true; device policy is loaded and
620 // scattering would have applied, except that the update check is interactive
621 // and so it is suppressed.
622
623 SetUpdateCheckAllowed(false);
624 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
625 new TimeDelta(TimeDelta::FromSeconds(1)));
626
627 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
628 update_state.scatter_check_threshold = 0;
629 update_state.scatter_check_threshold_min = 2;
630 update_state.scatter_check_threshold_max = 5;
631
632 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700633 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700634 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
635 true, update_state);
636 EXPECT_TRUE(result.update_can_start);
637 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
638 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700639 EXPECT_EQ(0, result.download_url_idx);
640 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700641}
642
Alex Deymo63784a52014-05-28 10:46:14 -0700643TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700644 UpdateCanStartAllowedOobePreventsScattering) {
645 // The UpdateCanStart policy returns true; device policy is loaded and
646 // scattering would have applied, except that OOBE was not completed and so it
647 // is suppressed.
648
649 SetUpdateCheckAllowed(false);
650 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
651 new TimeDelta(TimeDelta::FromSeconds(1)));
652 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
653
654 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
655 update_state.scatter_check_threshold = 0;
656 update_state.scatter_check_threshold_min = 2;
657 update_state.scatter_check_threshold_max = 5;
658
659 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700660 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700661 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
662 true, update_state);
663 EXPECT_TRUE(result.update_can_start);
664 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
665 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700666 EXPECT_EQ(0, result.download_url_idx);
667 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700668}
669
Alex Deymo63784a52014-05-28 10:46:14 -0700670TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700671 // The UpdateCanStart policy returns true; device policy permits both HTTP and
672 // P2P updates, as well as a non-empty target channel string.
673
674 SetUpdateCheckAllowed(false);
675
676 // Override specific device policy attributes.
677 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
678 new bool(true));
679 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
680 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700681
682 // Check that the UpdateCanStart returns true.
683 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700684 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700685 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
686 false, update_state);
687 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700688 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700689 EXPECT_EQ(0, result.download_url_idx);
690 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700691}
692
Alex Deymo63784a52014-05-28 10:46:14 -0700693TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700694 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
695 // P2P updates, but the updater is configured to allow P2P and overrules the
696 // setting.
697
698 SetUpdateCheckAllowed(false);
699
700 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700701 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
702
703 // Check that the UpdateCanStart returns true.
704 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700705 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700706 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
707 false, update_state);
708 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700709 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700710 EXPECT_EQ(0, result.download_url_idx);
711 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700712}
713
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700714TEST_F(UmChromeOSPolicyTest,
715 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700716 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
717 // P2P updates, but marking this an unofficial build overrules the HTTP
718 // setting.
719
720 SetUpdateCheckAllowed(false);
721
722 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700723 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
724 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700725 fake_state_.system_provider()->var_is_official_build()->
726 reset(new bool(false));
727
728 // Check that the UpdateCanStart returns true.
729 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700730 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700731 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
732 false, update_state);
733 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700734 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700735 EXPECT_EQ(0, result.download_url_idx);
736 EXPECT_EQ(0, result.download_url_num_failures);
737}
738
739TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
740 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
741 // P2P updates, but an HTTPS URL is provided and selected for download.
742
743 SetUpdateCheckAllowed(false);
744
745 // Override specific device policy attributes.
746 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
747 new bool(false));
748
749 // Add an HTTPS URL.
750 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
751 update_state.download_urls.push_back("https://secure/url/");
752
753 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700754 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700755 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
756 false, update_state);
757 EXPECT_TRUE(result.update_can_start);
758 EXPECT_FALSE(result.p2p_allowed);
759 EXPECT_EQ(1, result.download_url_idx);
760 EXPECT_EQ(0, result.download_url_num_failures);
761}
762
763TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
764 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
765 // allowed number of failures, but a second URL is available.
766
767 SetUpdateCheckAllowed(false);
768
769 // Add a second URL; update with this URL attempted and failed enough times to
770 // disqualify the current (first) URL. This tests both the previously
771 // accounted failures (download_url_num_failures) as well as those occurring
772 // since the last call (download_url_error_codes).
773 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
774 update_state.num_checks = 10;
775 update_state.download_urls.push_back("http://another/fake/url/");
776 update_state.download_url_num_failures = 9;
777 update_state.download_url_error_codes.push_back(
778 ErrorCode::kDownloadTransferError);
779 update_state.download_url_error_codes.push_back(
780 ErrorCode::kDownloadWriteError);
781
782 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700783 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700784 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
785 false, update_state);
786 EXPECT_TRUE(result.update_can_start);
787 EXPECT_FALSE(result.p2p_allowed);
788 EXPECT_EQ(1, result.download_url_idx);
789 EXPECT_EQ(0, result.download_url_num_failures);
790}
791
792TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
793 // The UpdateCanStart policy returns true; the first URL fails with a hard
794 // error, but a second URL is available.
795
796 SetUpdateCheckAllowed(false);
797
798 // Add a second URL; update with this URL attempted and failed in a way that
799 // causes it to switch directly to the next URL.
800 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
801 update_state.num_checks = 10;
802 update_state.download_urls.push_back("http://another/fake/url/");
803 update_state.download_url_error_codes.push_back(
804 ErrorCode::kPayloadHashMismatchError);
805
806 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700807 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700808 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
809 false, update_state);
810 EXPECT_TRUE(result.update_can_start);
811 EXPECT_FALSE(result.p2p_allowed);
812 EXPECT_EQ(1, result.download_url_idx);
813 EXPECT_EQ(0, result.download_url_num_failures);
814}
815
816TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
817 // The UpdateCanStart policy returns true; URL search properly wraps around
818 // the last one on the list.
819
820 SetUpdateCheckAllowed(false);
821
822 // Add a second URL; update with this URL attempted and failed in a way that
823 // causes it to switch directly to the next URL.
824 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
825 update_state.num_checks = 10;
826 update_state.download_urls.push_back("http://another/fake/url/");
827 update_state.download_url_idx = 1;
828 update_state.download_url_error_codes.push_back(
829 ErrorCode::kPayloadHashMismatchError);
830
831 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700832 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700833 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
834 false, update_state);
835 EXPECT_TRUE(result.update_can_start);
836 EXPECT_FALSE(result.p2p_allowed);
837 EXPECT_EQ(0, result.download_url_idx);
838 EXPECT_EQ(0, result.download_url_num_failures);
839}
840
841TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
842 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
843 // use is forbidden by policy.
844
845 SetUpdateCheckAllowed(false);
846
847 // Override specific device policy attributes.
848 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
849 new bool(false));
850
851 // Check that the UpdateCanStart returns false.
852 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700853 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700854 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
855 false, update_state);
856}
857
858TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
859 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
860 // use is forbidden by policy, however P2P is enabled. The result indicates
861 // that no URL can be used.
862
863 SetUpdateCheckAllowed(false);
864
865 // Override specific device policy attributes.
866 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
867 new bool(true));
868 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
869 new bool(false));
870
871 // Check that the UpdateCanStart returns true.
872 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700873 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700874 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
875 false, update_state);
876 EXPECT_TRUE(result.update_can_start);
877 EXPECT_TRUE(result.p2p_allowed);
878 EXPECT_GT(0, result.download_url_idx);
879 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700880}
881
Gilad Arnold684219d2014-07-07 14:54:57 -0700882TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700883 // Ethernet is always allowed.
884
885 fake_state_.shill_provider()->var_conn_type()->
886 reset(new ConnectionType(ConnectionType::kEthernet));
887
888 bool result;
889 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700890 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700891 EXPECT_TRUE(result);
892}
893
Gilad Arnold684219d2014-07-07 14:54:57 -0700894TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700895 // Wifi is allowed if not tethered.
896
897 fake_state_.shill_provider()->var_conn_type()->
898 reset(new ConnectionType(ConnectionType::kWifi));
899
900 bool result;
901 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700902 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700903 EXPECT_TRUE(result);
904}
905
Alex Deymo63784a52014-05-28 10:46:14 -0700906TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700907 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
908 // Tethered wifi is not allowed by default.
909
910 fake_state_.shill_provider()->var_conn_type()->
911 reset(new ConnectionType(ConnectionType::kWifi));
912 fake_state_.shill_provider()->var_conn_tethering()->
913 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
914
915 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700916 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700917 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700918}
919
Alex Deymo63784a52014-05-28 10:46:14 -0700920TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700921 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700922 // Tethered wifi can be allowed by policy.
923
924 fake_state_.shill_provider()->var_conn_type()->
925 reset(new ConnectionType(ConnectionType::kWifi));
926 fake_state_.shill_provider()->var_conn_tethering()->
927 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
928 set<ConnectionType> allowed_connections;
929 allowed_connections.insert(ConnectionType::kCellular);
930 fake_state_.device_policy_provider()->
931 var_allowed_connection_types_for_update()->
932 reset(new set<ConnectionType>(allowed_connections));
933
934 bool result;
935 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700936 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700937 EXPECT_TRUE(result);
938}
939
Gilad Arnold684219d2014-07-07 14:54:57 -0700940TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700941 // Wimax is always allowed.
942
943 fake_state_.shill_provider()->var_conn_type()->
944 reset(new ConnectionType(ConnectionType::kWifi));
945
946 bool result;
947 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700948 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700949 EXPECT_TRUE(result);
950}
951
Alex Deymo63784a52014-05-28 10:46:14 -0700952TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700953 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
954 // Bluetooth is never allowed.
955
956 fake_state_.shill_provider()->var_conn_type()->
957 reset(new ConnectionType(ConnectionType::kBluetooth));
958
959 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700960 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700961 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700962}
963
Alex Deymo63784a52014-05-28 10:46:14 -0700964TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700965 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
966 // Bluetooth cannot be allowed even by policy.
967
968 fake_state_.shill_provider()->var_conn_type()->
969 reset(new ConnectionType(ConnectionType::kBluetooth));
970 set<ConnectionType> allowed_connections;
971 allowed_connections.insert(ConnectionType::kBluetooth);
972 fake_state_.device_policy_provider()->
973 var_allowed_connection_types_for_update()->
974 reset(new set<ConnectionType>(allowed_connections));
975
976 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700977 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700978 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700979}
980
Alex Deymo63784a52014-05-28 10:46:14 -0700981TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700982 // Cellular is not allowed by default.
983
984 fake_state_.shill_provider()->var_conn_type()->
985 reset(new ConnectionType(ConnectionType::kCellular));
986
987 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700988 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700989 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700990}
991
Alex Deymo63784a52014-05-28 10:46:14 -0700992TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700993 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700994 // Update over cellular can be enabled by policy.
995
996 fake_state_.shill_provider()->var_conn_type()->
997 reset(new ConnectionType(ConnectionType::kCellular));
998 set<ConnectionType> allowed_connections;
999 allowed_connections.insert(ConnectionType::kCellular);
1000 fake_state_.device_policy_provider()->
1001 var_allowed_connection_types_for_update()->
1002 reset(new set<ConnectionType>(allowed_connections));
1003
1004 bool result;
1005 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001006 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001007 EXPECT_TRUE(result);
1008}
1009
Alex Deymo63784a52014-05-28 10:46:14 -07001010TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001011 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001012 // Update over cellular can be enabled by user settings, but only if policy
1013 // is present and does not determine allowed connections.
1014
1015 fake_state_.shill_provider()->var_conn_type()->
1016 reset(new ConnectionType(ConnectionType::kCellular));
1017 set<ConnectionType> allowed_connections;
1018 allowed_connections.insert(ConnectionType::kCellular);
1019 fake_state_.updater_provider()->var_cellular_enabled()->
1020 reset(new bool(true));
1021
1022 bool result;
1023 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001024 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001025 EXPECT_TRUE(result);
1026}
1027
Alex Deymo63784a52014-05-28 10:46:14 -07001028} // namespace chromeos_update_manager