blob: f79c4fc53f75223428fdc84b21e151792dd4d654 [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.
341 fake_state_.device_policy_provider()->var_release_channel_delegated()->
342 reset(new bool(false));
343 fake_state_.device_policy_provider()->var_release_channel()->
344 reset(new string("foo-channel"));
345
346 UpdateCheckParams result;
347 ExpectPolicyStatus(EvalStatus::kSucceeded,
348 &Policy::UpdateCheckAllowed, &result);
349 EXPECT_TRUE(result.updates_enabled);
350 EXPECT_EQ("foo-channel", result.target_channel);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700351 EXPECT_FALSE(result.is_interactive);
Gilad Arnold42f253b2014-06-25 12:39:17 -0700352}
353
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700354TEST_F(UmChromeOSPolicyTest,
355 UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700356 // UpdateCheckAllowed should return kAskMeAgainLater if this is an unofficial
357 // build; we don't want periodic update checks on developer images.
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700358
359 fake_state_.system_provider()->var_is_official_build()->reset(
360 new bool(false));
361
362 UpdateCheckParams result;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700363 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700364 &Policy::UpdateCheckAllowed, &result);
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700365}
366
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700367TEST_F(UmChromeOSPolicyTest,
368 UpdateCheckAllowedUpdatesDisabledForRemovableBootDevice) {
369 // UpdateCheckAllowed should return false (kSucceeded) if the image booted
370 // from a removable device.
371
372 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
373 new bool(true));
374
375 UpdateCheckParams result;
376 ExpectPolicyStatus(EvalStatus::kSucceeded,
377 &Policy::UpdateCheckAllowed, &result);
378 EXPECT_FALSE(result.updates_enabled);
379}
380
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700381TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700382 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
383 // is loaded and prohibits updates.
384
385 SetUpdateCheckAllowed(false);
386 fake_state_.device_policy_provider()->var_update_disabled()->reset(
387 new bool(true));
388
Gilad Arnold42f253b2014-06-25 12:39:17 -0700389 UpdateCheckParams result;
390 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
391 &Policy::UpdateCheckAllowed, &result);
392}
393
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700394TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedInteractiveUpdateRequested) {
395 // UpdateCheckAllowed should return true because an interactive update request
396 // was signaled.
397
398 SetUpdateCheckAllowed(true);
399 fake_state_.updater_provider()->var_interactive_update_requested()->reset(
400 new bool(true));
401
402 UpdateCheckParams result;
403 ExpectPolicyStatus(EvalStatus::kSucceeded,
404 &Policy::UpdateCheckAllowed, &result);
405 EXPECT_TRUE(result.updates_enabled);
406 EXPECT_TRUE(result.is_interactive);
407}
408
Alex Deymo63784a52014-05-28 10:46:14 -0700409TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700410 // The UpdateCanStart policy fails, not being able to query
411 // UpdateCheckAllowed.
412
413 // Configure the UpdateCheckAllowed policy to fail.
414 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
415
416 // Check that the UpdateCanStart fails.
417 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700418 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700419 ExpectPolicyStatus(EvalStatus::kFailed,
420 &Policy::UpdateCanStart, &result, false, update_state);
421}
422
Alex Deymo63784a52014-05-28 10:46:14 -0700423TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700424 // The UpdateCanStart policy returns false because we are due for another
425 // update check.
426
427 SetUpdateCheckAllowed(true);
428
429 // Check that the UpdateCanStart returns false.
430 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700431 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700432 ExpectPolicyStatus(EvalStatus::kSucceeded,
433 &Policy::UpdateCanStart, &result, false, update_state);
434 EXPECT_FALSE(result.update_can_start);
435 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
436}
437
Alex Deymo63784a52014-05-28 10:46:14 -0700438TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700439 // The UpdateCanStart policy returns true; no device policy is loaded.
440
441 SetUpdateCheckAllowed(false);
442 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
443 new bool(false));
444
445 // Check that the UpdateCanStart returns true with no further attributes.
446 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700447 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700448 ExpectPolicyStatus(EvalStatus::kSucceeded,
449 &Policy::UpdateCanStart, &result, false, update_state);
450 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700451 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700452 EXPECT_EQ(0, result.download_url_idx);
453 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700454}
455
Alex Deymo63784a52014-05-28 10:46:14 -0700456TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700457 // The UpdateCanStart policy returns true; device policy is loaded but imposes
458 // no restrictions on updating.
459
460 SetUpdateCheckAllowed(false);
461
462 // Check that the UpdateCanStart returns true.
463 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700464 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700465 ExpectPolicyStatus(EvalStatus::kSucceeded,
466 &Policy::UpdateCanStart, &result, false, update_state);
467 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700468 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700469 EXPECT_EQ(0, result.download_url_idx);
470 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700471}
472
Alex Deymo63784a52014-05-28 10:46:14 -0700473TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700474 // The UpdateCanStart policy fails because the UpdateScattering policy it
475 // depends on fails (unset variable).
476
477 SetUpdateCheckAllowed(false);
478
479 // Override the default seed variable with a null value so that the policy
480 // request would fail.
481 fake_state_.random_provider()->var_seed()->reset(nullptr);
482
483 // Check that the UpdateCanStart fails.
484 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700485 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700486 ExpectPolicyStatus(EvalStatus::kFailed,
487 &Policy::UpdateCanStart, &result, false, update_state);
488}
489
Alex Deymo63784a52014-05-28 10:46:14 -0700490TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700491 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
492 // The UpdateCanStart policy returns false; device policy is loaded and
493 // scattering applies due to an unsatisfied wait period, which was newly
494 // generated.
495
496 SetUpdateCheckAllowed(false);
497 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
498 new TimeDelta(TimeDelta::FromMinutes(2)));
499
500
501 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
502
503 // Check that the UpdateCanStart returns false and a new wait period
504 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700505 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700506 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
507 false, update_state);
508 EXPECT_FALSE(result.update_can_start);
509 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
510 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
511 EXPECT_EQ(0, result.scatter_check_threshold);
512}
513
Alex Deymo63784a52014-05-28 10:46:14 -0700514TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700515 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
516 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
517 // is loaded and a previously generated scattering period still applies, none
518 // of the scattering values has changed.
519
520 SetUpdateCheckAllowed(false);
521 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
522 new TimeDelta(TimeDelta::FromMinutes(2)));
523
524 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
525 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
526
527 // Check that the UpdateCanStart returns false and a new wait period
528 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700529 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700530 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
531 &result, false, update_state);
532 EXPECT_FALSE(result.update_can_start);
533 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
534 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
535 EXPECT_EQ(0, result.scatter_check_threshold);
536}
537
Alex Deymo63784a52014-05-28 10:46:14 -0700538TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700539 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
540 // The UpdateCanStart policy returns false; device policy is loaded and
541 // scattering applies due to an unsatisfied update check count threshold.
542 //
543 // This ensures a non-zero check threshold, which may or may not be combined
544 // with a non-zero wait period (for which we cannot reliably control).
545
546 SetUpdateCheckAllowed(false);
547 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
548 new TimeDelta(TimeDelta::FromSeconds(1)));
549
550 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
551 update_state.scatter_check_threshold_min = 2;
552 update_state.scatter_check_threshold_max = 5;
553
554 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700555 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700556 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
557 false, update_state);
558 EXPECT_FALSE(result.update_can_start);
559 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
560 EXPECT_LE(2, result.scatter_check_threshold);
561 EXPECT_GE(5, result.scatter_check_threshold);
562}
563
Alex Deymo63784a52014-05-28 10:46:14 -0700564TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700565 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
566 // The UpdateCanStart policy returns false; device policy is loaded and
567 // scattering due to a previously generated count threshold still applies.
568
569 SetUpdateCheckAllowed(false);
570 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
571 new TimeDelta(TimeDelta::FromSeconds(1)));
572
573 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
574 update_state.scatter_check_threshold = 3;
575 update_state.scatter_check_threshold_min = 2;
576 update_state.scatter_check_threshold_max = 5;
577
578 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700579 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700580 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
581 false, update_state);
582 EXPECT_FALSE(result.update_can_start);
583 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
584 EXPECT_EQ(3, result.scatter_check_threshold);
585}
586
Alex Deymo63784a52014-05-28 10:46:14 -0700587TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700588 // The UpdateCanStart policy returns true; device policy is loaded and
589 // scattering is enabled, but both wait period and check threshold are
590 // satisfied.
591
592 SetUpdateCheckAllowed(false);
593 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
594 new TimeDelta(TimeDelta::FromSeconds(120)));
595
596 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
597 update_state.num_checks = 4;
598 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
599 update_state.scatter_check_threshold = 3;
600 update_state.scatter_check_threshold_min = 2;
601 update_state.scatter_check_threshold_max = 5;
602
603 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700604 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700605 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
606 false, update_state);
607 EXPECT_TRUE(result.update_can_start);
608 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
609 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700610 EXPECT_EQ(0, result.download_url_idx);
611 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700612}
613
Alex Deymo63784a52014-05-28 10:46:14 -0700614TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700615 UpdateCanStartAllowedInteractivePreventsScattering) {
616 // The UpdateCanStart policy returns true; device policy is loaded and
617 // scattering would have applied, except that the update check is interactive
618 // and so it is suppressed.
619
620 SetUpdateCheckAllowed(false);
621 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
622 new TimeDelta(TimeDelta::FromSeconds(1)));
623
624 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
625 update_state.scatter_check_threshold = 0;
626 update_state.scatter_check_threshold_min = 2;
627 update_state.scatter_check_threshold_max = 5;
628
629 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700630 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700631 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
632 true, update_state);
633 EXPECT_TRUE(result.update_can_start);
634 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
635 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700636 EXPECT_EQ(0, result.download_url_idx);
637 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700638}
639
Alex Deymo63784a52014-05-28 10:46:14 -0700640TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700641 UpdateCanStartAllowedOobePreventsScattering) {
642 // The UpdateCanStart policy returns true; device policy is loaded and
643 // scattering would have applied, except that OOBE was not completed and so it
644 // is suppressed.
645
646 SetUpdateCheckAllowed(false);
647 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
648 new TimeDelta(TimeDelta::FromSeconds(1)));
649 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
650
651 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
652 update_state.scatter_check_threshold = 0;
653 update_state.scatter_check_threshold_min = 2;
654 update_state.scatter_check_threshold_max = 5;
655
656 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700657 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700658 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
659 true, update_state);
660 EXPECT_TRUE(result.update_can_start);
661 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
662 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700663 EXPECT_EQ(0, result.download_url_idx);
664 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700665}
666
Alex Deymo63784a52014-05-28 10:46:14 -0700667TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700668 // The UpdateCanStart policy returns true; device policy permits both HTTP and
669 // P2P updates, as well as a non-empty target channel string.
670
671 SetUpdateCheckAllowed(false);
672
673 // Override specific device policy attributes.
674 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
675 new bool(true));
676 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
677 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700678
679 // Check that the UpdateCanStart returns true.
680 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700681 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700682 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
683 false, update_state);
684 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700685 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700686 EXPECT_EQ(0, result.download_url_idx);
687 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700688}
689
Alex Deymo63784a52014-05-28 10:46:14 -0700690TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700691 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
692 // P2P updates, but the updater is configured to allow P2P and overrules the
693 // setting.
694
695 SetUpdateCheckAllowed(false);
696
697 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700698 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
699
700 // Check that the UpdateCanStart returns true.
701 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700702 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700703 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
704 false, update_state);
705 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700706 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700707 EXPECT_EQ(0, result.download_url_idx);
708 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700709}
710
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700711TEST_F(UmChromeOSPolicyTest,
712 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700713 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
714 // P2P updates, but marking this an unofficial build overrules the HTTP
715 // setting.
716
717 SetUpdateCheckAllowed(false);
718
719 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700720 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
721 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700722 fake_state_.system_provider()->var_is_official_build()->
723 reset(new bool(false));
724
725 // Check that the UpdateCanStart returns true.
726 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700727 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700728 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
729 false, update_state);
730 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700731 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700732 EXPECT_EQ(0, result.download_url_idx);
733 EXPECT_EQ(0, result.download_url_num_failures);
734}
735
736TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
737 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
738 // P2P updates, but an HTTPS URL is provided and selected for download.
739
740 SetUpdateCheckAllowed(false);
741
742 // Override specific device policy attributes.
743 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
744 new bool(false));
745
746 // Add an HTTPS URL.
747 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
748 update_state.download_urls.push_back("https://secure/url/");
749
750 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700751 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700752 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
753 false, update_state);
754 EXPECT_TRUE(result.update_can_start);
755 EXPECT_FALSE(result.p2p_allowed);
756 EXPECT_EQ(1, result.download_url_idx);
757 EXPECT_EQ(0, result.download_url_num_failures);
758}
759
760TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
761 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
762 // allowed number of failures, but a second URL is available.
763
764 SetUpdateCheckAllowed(false);
765
766 // Add a second URL; update with this URL attempted and failed enough times to
767 // disqualify the current (first) URL. This tests both the previously
768 // accounted failures (download_url_num_failures) as well as those occurring
769 // since the last call (download_url_error_codes).
770 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
771 update_state.num_checks = 10;
772 update_state.download_urls.push_back("http://another/fake/url/");
773 update_state.download_url_num_failures = 9;
774 update_state.download_url_error_codes.push_back(
775 ErrorCode::kDownloadTransferError);
776 update_state.download_url_error_codes.push_back(
777 ErrorCode::kDownloadWriteError);
778
779 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700780 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700781 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
782 false, update_state);
783 EXPECT_TRUE(result.update_can_start);
784 EXPECT_FALSE(result.p2p_allowed);
785 EXPECT_EQ(1, result.download_url_idx);
786 EXPECT_EQ(0, result.download_url_num_failures);
787}
788
789TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
790 // The UpdateCanStart policy returns true; the first URL fails with a hard
791 // error, but a second URL is available.
792
793 SetUpdateCheckAllowed(false);
794
795 // Add a second URL; update with this URL attempted and failed in a way that
796 // causes it to switch directly to the next URL.
797 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
798 update_state.num_checks = 10;
799 update_state.download_urls.push_back("http://another/fake/url/");
800 update_state.download_url_error_codes.push_back(
801 ErrorCode::kPayloadHashMismatchError);
802
803 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700804 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700805 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
806 false, update_state);
807 EXPECT_TRUE(result.update_can_start);
808 EXPECT_FALSE(result.p2p_allowed);
809 EXPECT_EQ(1, result.download_url_idx);
810 EXPECT_EQ(0, result.download_url_num_failures);
811}
812
813TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
814 // The UpdateCanStart policy returns true; URL search properly wraps around
815 // the last one on the list.
816
817 SetUpdateCheckAllowed(false);
818
819 // Add a second URL; update with this URL attempted and failed in a way that
820 // causes it to switch directly to the next URL.
821 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
822 update_state.num_checks = 10;
823 update_state.download_urls.push_back("http://another/fake/url/");
824 update_state.download_url_idx = 1;
825 update_state.download_url_error_codes.push_back(
826 ErrorCode::kPayloadHashMismatchError);
827
828 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700829 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700830 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
831 false, update_state);
832 EXPECT_TRUE(result.update_can_start);
833 EXPECT_FALSE(result.p2p_allowed);
834 EXPECT_EQ(0, result.download_url_idx);
835 EXPECT_EQ(0, result.download_url_num_failures);
836}
837
838TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
839 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
840 // use is forbidden by policy.
841
842 SetUpdateCheckAllowed(false);
843
844 // Override specific device policy attributes.
845 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
846 new bool(false));
847
848 // Check that the UpdateCanStart returns false.
849 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700850 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700851 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
852 false, update_state);
853}
854
855TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
856 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
857 // use is forbidden by policy, however P2P is enabled. The result indicates
858 // that no URL can be used.
859
860 SetUpdateCheckAllowed(false);
861
862 // Override specific device policy attributes.
863 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
864 new bool(true));
865 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
866 new bool(false));
867
868 // Check that the UpdateCanStart returns true.
869 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700870 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700871 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
872 false, update_state);
873 EXPECT_TRUE(result.update_can_start);
874 EXPECT_TRUE(result.p2p_allowed);
875 EXPECT_GT(0, result.download_url_idx);
876 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700877}
878
Gilad Arnold684219d2014-07-07 14:54:57 -0700879TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700880 // Ethernet is always allowed.
881
882 fake_state_.shill_provider()->var_conn_type()->
883 reset(new ConnectionType(ConnectionType::kEthernet));
884
885 bool result;
886 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700887 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700888 EXPECT_TRUE(result);
889}
890
Gilad Arnold684219d2014-07-07 14:54:57 -0700891TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700892 // Wifi is allowed if not tethered.
893
894 fake_state_.shill_provider()->var_conn_type()->
895 reset(new ConnectionType(ConnectionType::kWifi));
896
897 bool result;
898 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700899 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700900 EXPECT_TRUE(result);
901}
902
Alex Deymo63784a52014-05-28 10:46:14 -0700903TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700904 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
905 // Tethered wifi is not allowed by default.
906
907 fake_state_.shill_provider()->var_conn_type()->
908 reset(new ConnectionType(ConnectionType::kWifi));
909 fake_state_.shill_provider()->var_conn_tethering()->
910 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
911
912 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700913 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700914 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700915}
916
Alex Deymo63784a52014-05-28 10:46:14 -0700917TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700918 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700919 // Tethered wifi can be allowed by policy.
920
921 fake_state_.shill_provider()->var_conn_type()->
922 reset(new ConnectionType(ConnectionType::kWifi));
923 fake_state_.shill_provider()->var_conn_tethering()->
924 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
925 set<ConnectionType> allowed_connections;
926 allowed_connections.insert(ConnectionType::kCellular);
927 fake_state_.device_policy_provider()->
928 var_allowed_connection_types_for_update()->
929 reset(new set<ConnectionType>(allowed_connections));
930
931 bool result;
932 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700933 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700934 EXPECT_TRUE(result);
935}
936
Gilad Arnold684219d2014-07-07 14:54:57 -0700937TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700938 // Wimax is always allowed.
939
940 fake_state_.shill_provider()->var_conn_type()->
941 reset(new ConnectionType(ConnectionType::kWifi));
942
943 bool result;
944 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700945 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700946 EXPECT_TRUE(result);
947}
948
Alex Deymo63784a52014-05-28 10:46:14 -0700949TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700950 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
951 // Bluetooth is never allowed.
952
953 fake_state_.shill_provider()->var_conn_type()->
954 reset(new ConnectionType(ConnectionType::kBluetooth));
955
956 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700957 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700958 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700959}
960
Alex Deymo63784a52014-05-28 10:46:14 -0700961TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700962 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
963 // Bluetooth cannot be allowed even by policy.
964
965 fake_state_.shill_provider()->var_conn_type()->
966 reset(new ConnectionType(ConnectionType::kBluetooth));
967 set<ConnectionType> allowed_connections;
968 allowed_connections.insert(ConnectionType::kBluetooth);
969 fake_state_.device_policy_provider()->
970 var_allowed_connection_types_for_update()->
971 reset(new set<ConnectionType>(allowed_connections));
972
973 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700974 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700975 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700976}
977
Alex Deymo63784a52014-05-28 10:46:14 -0700978TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700979 // Cellular is not allowed by default.
980
981 fake_state_.shill_provider()->var_conn_type()->
982 reset(new ConnectionType(ConnectionType::kCellular));
983
984 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700985 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700986 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700987}
988
Alex Deymo63784a52014-05-28 10:46:14 -0700989TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700990 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700991 // Update over cellular can be enabled by policy.
992
993 fake_state_.shill_provider()->var_conn_type()->
994 reset(new ConnectionType(ConnectionType::kCellular));
995 set<ConnectionType> allowed_connections;
996 allowed_connections.insert(ConnectionType::kCellular);
997 fake_state_.device_policy_provider()->
998 var_allowed_connection_types_for_update()->
999 reset(new set<ConnectionType>(allowed_connections));
1000
1001 bool result;
1002 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001003 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001004 EXPECT_TRUE(result);
1005}
1006
Alex Deymo63784a52014-05-28 10:46:14 -07001007TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001008 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001009 // Update over cellular can be enabled by user settings, but only if policy
1010 // is present and does not determine allowed connections.
1011
1012 fake_state_.shill_provider()->var_conn_type()->
1013 reset(new ConnectionType(ConnectionType::kCellular));
1014 set<ConnectionType> allowed_connections;
1015 allowed_connections.insert(ConnectionType::kCellular);
1016 fake_state_.updater_provider()->var_cellular_enabled()->
1017 reset(new bool(true));
1018
1019 bool result;
1020 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001021 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001022 EXPECT_TRUE(result);
1023}
1024
Alex Deymo63784a52014-05-28 10:46:14 -07001025} // namespace chromeos_update_manager