blob: 25a091ea60a375cb4997029ba9b00c26d8539208 [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 Arnoldec7f9162014-07-15 13:24:46 -070050 reset(new unsigned int{0});
Gilad Arnolda0258a52014-07-10 16:21:19 -070051 fake_state_.updater_provider()->var_server_dictated_poll_interval()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -070052 reset(new unsigned int{0});
53 fake_state_.updater_provider()->var_forced_update_requested()->
54 reset(new UpdateRequestStatus{UpdateRequestStatus::kNone});
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()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700202 reset(new unsigned int{2});
Gilad Arnold38b14022014-07-09 12:45:56 -0700203
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()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700224 reset(new unsigned int{kInterval});
Gilad Arnolda0258a52014-07-10 16:21:19 -0700225 // We should not be backing off in this case.
226 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700227 reset(new unsigned int{2});
Gilad Arnolda0258a52014-07-10 16:21:19 -0700228
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 Arnoldec7f9162014-07-15 13:24:46 -0700246 reset(new unsigned int{100});
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 Arnoldec7f9162014-07-15 13:24:46 -0700397TEST_F(UmChromeOSPolicyTest,
398 UpdateCheckAllowedForcedUpdateRequestedInteractive) {
399 // UpdateCheckAllowed should return true because a forced update request was
400 // signaled for an interactive update.
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700401
402 SetUpdateCheckAllowed(true);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700403 fake_state_.updater_provider()->var_forced_update_requested()->reset(
404 new UpdateRequestStatus(UpdateRequestStatus::kInteractive));
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700405
406 UpdateCheckParams result;
407 ExpectPolicyStatus(EvalStatus::kSucceeded,
408 &Policy::UpdateCheckAllowed, &result);
409 EXPECT_TRUE(result.updates_enabled);
410 EXPECT_TRUE(result.is_interactive);
411}
412
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700413TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedForcedUpdateRequestedPeriodic) {
414 // UpdateCheckAllowed should return true because a forced update request was
415 // signaled for a periodic check.
416
417 SetUpdateCheckAllowed(true);
418 fake_state_.updater_provider()->var_forced_update_requested()->reset(
419 new UpdateRequestStatus(UpdateRequestStatus::kPeriodic));
420
421 UpdateCheckParams result;
422 ExpectPolicyStatus(EvalStatus::kSucceeded,
423 &Policy::UpdateCheckAllowed, &result);
424 EXPECT_TRUE(result.updates_enabled);
425 EXPECT_FALSE(result.is_interactive);
426}
427
Alex Deymo63784a52014-05-28 10:46:14 -0700428TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700429 // The UpdateCanStart policy fails, not being able to query
430 // UpdateCheckAllowed.
431
432 // Configure the UpdateCheckAllowed policy to fail.
433 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
434
435 // Check that the UpdateCanStart fails.
436 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700437 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700438 ExpectPolicyStatus(EvalStatus::kFailed,
439 &Policy::UpdateCanStart, &result, false, update_state);
440}
441
Alex Deymo63784a52014-05-28 10:46:14 -0700442TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700443 // The UpdateCanStart policy returns false because we are due for another
444 // update check.
445
446 SetUpdateCheckAllowed(true);
447
448 // Check that the UpdateCanStart returns false.
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_FALSE(result.update_can_start);
454 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
455}
456
Alex Deymo63784a52014-05-28 10:46:14 -0700457TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700458 // The UpdateCanStart policy returns true; no device policy is loaded.
459
460 SetUpdateCheckAllowed(false);
461 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
462 new bool(false));
463
464 // Check that the UpdateCanStart returns true with no further attributes.
465 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700466 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700467 ExpectPolicyStatus(EvalStatus::kSucceeded,
468 &Policy::UpdateCanStart, &result, false, update_state);
469 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700470 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700471 EXPECT_EQ(0, result.download_url_idx);
472 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700473}
474
Alex Deymo63784a52014-05-28 10:46:14 -0700475TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700476 // The UpdateCanStart policy returns true; device policy is loaded but imposes
477 // no restrictions on updating.
478
479 SetUpdateCheckAllowed(false);
480
481 // Check that the UpdateCanStart returns true.
482 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700483 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700484 ExpectPolicyStatus(EvalStatus::kSucceeded,
485 &Policy::UpdateCanStart, &result, false, update_state);
486 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700487 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700488 EXPECT_EQ(0, result.download_url_idx);
489 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700490}
491
Alex Deymo63784a52014-05-28 10:46:14 -0700492TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700493 // The UpdateCanStart policy fails because the UpdateScattering policy it
494 // depends on fails (unset variable).
495
496 SetUpdateCheckAllowed(false);
497
498 // Override the default seed variable with a null value so that the policy
499 // request would fail.
500 fake_state_.random_provider()->var_seed()->reset(nullptr);
501
502 // Check that the UpdateCanStart fails.
503 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700504 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700505 ExpectPolicyStatus(EvalStatus::kFailed,
506 &Policy::UpdateCanStart, &result, false, update_state);
507}
508
Alex Deymo63784a52014-05-28 10:46:14 -0700509TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700510 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
511 // The UpdateCanStart policy returns false; device policy is loaded and
512 // scattering applies due to an unsatisfied wait period, which was newly
513 // generated.
514
515 SetUpdateCheckAllowed(false);
516 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
517 new TimeDelta(TimeDelta::FromMinutes(2)));
518
519
520 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
521
522 // Check that the UpdateCanStart returns false and a new wait period
523 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700524 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700525 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
526 false, update_state);
527 EXPECT_FALSE(result.update_can_start);
528 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
529 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
530 EXPECT_EQ(0, result.scatter_check_threshold);
531}
532
Alex Deymo63784a52014-05-28 10:46:14 -0700533TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700534 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
535 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
536 // is loaded and a previously generated scattering period still applies, none
537 // of the scattering values has changed.
538
539 SetUpdateCheckAllowed(false);
540 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
541 new TimeDelta(TimeDelta::FromMinutes(2)));
542
543 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
544 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
545
546 // Check that the UpdateCanStart returns false and a new wait period
547 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700548 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700549 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
550 &result, false, update_state);
551 EXPECT_FALSE(result.update_can_start);
552 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
553 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
554 EXPECT_EQ(0, result.scatter_check_threshold);
555}
556
Alex Deymo63784a52014-05-28 10:46:14 -0700557TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700558 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
559 // The UpdateCanStart policy returns false; device policy is loaded and
560 // scattering applies due to an unsatisfied update check count threshold.
561 //
562 // This ensures a non-zero check threshold, which may or may not be combined
563 // with a non-zero wait period (for which we cannot reliably control).
564
565 SetUpdateCheckAllowed(false);
566 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
567 new TimeDelta(TimeDelta::FromSeconds(1)));
568
569 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
570 update_state.scatter_check_threshold_min = 2;
571 update_state.scatter_check_threshold_max = 5;
572
573 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700574 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700575 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
576 false, update_state);
577 EXPECT_FALSE(result.update_can_start);
578 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
579 EXPECT_LE(2, result.scatter_check_threshold);
580 EXPECT_GE(5, result.scatter_check_threshold);
581}
582
Alex Deymo63784a52014-05-28 10:46:14 -0700583TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700584 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
585 // The UpdateCanStart policy returns false; device policy is loaded and
586 // scattering due to a previously generated count threshold still applies.
587
588 SetUpdateCheckAllowed(false);
589 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
590 new TimeDelta(TimeDelta::FromSeconds(1)));
591
592 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
593 update_state.scatter_check_threshold = 3;
594 update_state.scatter_check_threshold_min = 2;
595 update_state.scatter_check_threshold_max = 5;
596
597 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700598 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700599 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
600 false, update_state);
601 EXPECT_FALSE(result.update_can_start);
602 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
603 EXPECT_EQ(3, result.scatter_check_threshold);
604}
605
Alex Deymo63784a52014-05-28 10:46:14 -0700606TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700607 // The UpdateCanStart policy returns true; device policy is loaded and
608 // scattering is enabled, but both wait period and check threshold are
609 // satisfied.
610
611 SetUpdateCheckAllowed(false);
612 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
613 new TimeDelta(TimeDelta::FromSeconds(120)));
614
615 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
616 update_state.num_checks = 4;
617 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
618 update_state.scatter_check_threshold = 3;
619 update_state.scatter_check_threshold_min = 2;
620 update_state.scatter_check_threshold_max = 5;
621
622 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700623 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700624 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
625 false, update_state);
626 EXPECT_TRUE(result.update_can_start);
627 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
628 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700629 EXPECT_EQ(0, result.download_url_idx);
630 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700631}
632
Alex Deymo63784a52014-05-28 10:46:14 -0700633TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700634 UpdateCanStartAllowedInteractivePreventsScattering) {
635 // The UpdateCanStart policy returns true; device policy is loaded and
636 // scattering would have applied, except that the update check is interactive
637 // and so it is suppressed.
638
639 SetUpdateCheckAllowed(false);
640 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
641 new TimeDelta(TimeDelta::FromSeconds(1)));
642
643 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
644 update_state.scatter_check_threshold = 0;
645 update_state.scatter_check_threshold_min = 2;
646 update_state.scatter_check_threshold_max = 5;
647
648 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700649 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700650 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
651 true, update_state);
652 EXPECT_TRUE(result.update_can_start);
653 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
654 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700655 EXPECT_EQ(0, result.download_url_idx);
656 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700657}
658
Alex Deymo63784a52014-05-28 10:46:14 -0700659TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700660 UpdateCanStartAllowedOobePreventsScattering) {
661 // The UpdateCanStart policy returns true; device policy is loaded and
662 // scattering would have applied, except that OOBE was not completed and so it
663 // is suppressed.
664
665 SetUpdateCheckAllowed(false);
666 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
667 new TimeDelta(TimeDelta::FromSeconds(1)));
668 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
669
670 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
671 update_state.scatter_check_threshold = 0;
672 update_state.scatter_check_threshold_min = 2;
673 update_state.scatter_check_threshold_max = 5;
674
675 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700676 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700677 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
678 true, update_state);
679 EXPECT_TRUE(result.update_can_start);
680 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
681 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700682 EXPECT_EQ(0, result.download_url_idx);
683 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700684}
685
Alex Deymo63784a52014-05-28 10:46:14 -0700686TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700687 // The UpdateCanStart policy returns true; device policy permits both HTTP and
688 // P2P updates, as well as a non-empty target channel string.
689
690 SetUpdateCheckAllowed(false);
691
692 // Override specific device policy attributes.
693 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
694 new bool(true));
695 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
696 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700697
698 // Check that the UpdateCanStart returns true.
699 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700700 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700701 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
702 false, update_state);
703 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700704 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700705 EXPECT_EQ(0, result.download_url_idx);
706 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700707}
708
Alex Deymo63784a52014-05-28 10:46:14 -0700709TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700710 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
711 // P2P updates, but the updater is configured to allow P2P and overrules the
712 // setting.
713
714 SetUpdateCheckAllowed(false);
715
716 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700717 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
718
719 // Check that the UpdateCanStart returns true.
720 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700721 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700722 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
723 false, update_state);
724 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700725 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700726 EXPECT_EQ(0, result.download_url_idx);
727 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700728}
729
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700730TEST_F(UmChromeOSPolicyTest,
731 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700732 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
733 // P2P updates, but marking this an unofficial build overrules the HTTP
734 // setting.
735
736 SetUpdateCheckAllowed(false);
737
738 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700739 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
740 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700741 fake_state_.system_provider()->var_is_official_build()->
742 reset(new bool(false));
743
744 // Check that the UpdateCanStart returns true.
745 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700746 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700747 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
748 false, update_state);
749 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700750 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700751 EXPECT_EQ(0, result.download_url_idx);
752 EXPECT_EQ(0, result.download_url_num_failures);
753}
754
755TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
756 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
757 // P2P updates, but an HTTPS URL is provided and selected for download.
758
759 SetUpdateCheckAllowed(false);
760
761 // Override specific device policy attributes.
762 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
763 new bool(false));
764
765 // Add an HTTPS URL.
766 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
767 update_state.download_urls.push_back("https://secure/url/");
768
769 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700770 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700771 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
772 false, update_state);
773 EXPECT_TRUE(result.update_can_start);
774 EXPECT_FALSE(result.p2p_allowed);
775 EXPECT_EQ(1, result.download_url_idx);
776 EXPECT_EQ(0, result.download_url_num_failures);
777}
778
779TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
780 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
781 // allowed number of failures, but a second URL is available.
782
783 SetUpdateCheckAllowed(false);
784
785 // Add a second URL; update with this URL attempted and failed enough times to
786 // disqualify the current (first) URL. This tests both the previously
787 // accounted failures (download_url_num_failures) as well as those occurring
788 // since the last call (download_url_error_codes).
789 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
790 update_state.num_checks = 10;
791 update_state.download_urls.push_back("http://another/fake/url/");
792 update_state.download_url_num_failures = 9;
793 update_state.download_url_error_codes.push_back(
794 ErrorCode::kDownloadTransferError);
795 update_state.download_url_error_codes.push_back(
796 ErrorCode::kDownloadWriteError);
797
798 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700799 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700800 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
801 false, update_state);
802 EXPECT_TRUE(result.update_can_start);
803 EXPECT_FALSE(result.p2p_allowed);
804 EXPECT_EQ(1, result.download_url_idx);
805 EXPECT_EQ(0, result.download_url_num_failures);
806}
807
808TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
809 // The UpdateCanStart policy returns true; the first URL fails with a hard
810 // error, but a second URL is available.
811
812 SetUpdateCheckAllowed(false);
813
814 // Add a second URL; update with this URL attempted and failed in a way that
815 // causes it to switch directly to the next URL.
816 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
817 update_state.num_checks = 10;
818 update_state.download_urls.push_back("http://another/fake/url/");
819 update_state.download_url_error_codes.push_back(
820 ErrorCode::kPayloadHashMismatchError);
821
822 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700823 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700824 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
825 false, update_state);
826 EXPECT_TRUE(result.update_can_start);
827 EXPECT_FALSE(result.p2p_allowed);
828 EXPECT_EQ(1, result.download_url_idx);
829 EXPECT_EQ(0, result.download_url_num_failures);
830}
831
832TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
833 // The UpdateCanStart policy returns true; URL search properly wraps around
834 // the last one on the list.
835
836 SetUpdateCheckAllowed(false);
837
838 // Add a second URL; update with this URL attempted and failed in a way that
839 // causes it to switch directly to the next URL.
840 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
841 update_state.num_checks = 10;
842 update_state.download_urls.push_back("http://another/fake/url/");
843 update_state.download_url_idx = 1;
844 update_state.download_url_error_codes.push_back(
845 ErrorCode::kPayloadHashMismatchError);
846
847 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700848 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700849 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
850 false, update_state);
851 EXPECT_TRUE(result.update_can_start);
852 EXPECT_FALSE(result.p2p_allowed);
853 EXPECT_EQ(0, result.download_url_idx);
854 EXPECT_EQ(0, result.download_url_num_failures);
855}
856
857TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
858 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
859 // use is forbidden by policy.
860
861 SetUpdateCheckAllowed(false);
862
863 // Override specific device policy attributes.
864 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
865 new bool(false));
866
867 // Check that the UpdateCanStart returns false.
868 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700869 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700870 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
871 false, update_state);
872}
873
874TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
875 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
876 // use is forbidden by policy, however P2P is enabled. The result indicates
877 // that no URL can be used.
878
879 SetUpdateCheckAllowed(false);
880
881 // Override specific device policy attributes.
882 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
883 new bool(true));
884 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
885 new bool(false));
886
887 // Check that the UpdateCanStart returns true.
888 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700889 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700890 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
891 false, update_state);
892 EXPECT_TRUE(result.update_can_start);
893 EXPECT_TRUE(result.p2p_allowed);
894 EXPECT_GT(0, result.download_url_idx);
895 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700896}
897
Gilad Arnold684219d2014-07-07 14:54:57 -0700898TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700899 // Ethernet is always allowed.
900
901 fake_state_.shill_provider()->var_conn_type()->
902 reset(new ConnectionType(ConnectionType::kEthernet));
903
904 bool result;
905 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700906 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700907 EXPECT_TRUE(result);
908}
909
Gilad Arnold684219d2014-07-07 14:54:57 -0700910TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700911 // Wifi is allowed if not tethered.
912
913 fake_state_.shill_provider()->var_conn_type()->
914 reset(new ConnectionType(ConnectionType::kWifi));
915
916 bool result;
917 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700918 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700919 EXPECT_TRUE(result);
920}
921
Alex Deymo63784a52014-05-28 10:46:14 -0700922TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700923 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
924 // Tethered wifi is not allowed by default.
925
926 fake_state_.shill_provider()->var_conn_type()->
927 reset(new ConnectionType(ConnectionType::kWifi));
928 fake_state_.shill_provider()->var_conn_tethering()->
929 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
930
931 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700932 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700933 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700934}
935
Alex Deymo63784a52014-05-28 10:46:14 -0700936TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700937 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700938 // Tethered wifi can be allowed by policy.
939
940 fake_state_.shill_provider()->var_conn_type()->
941 reset(new ConnectionType(ConnectionType::kWifi));
942 fake_state_.shill_provider()->var_conn_tethering()->
943 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
944 set<ConnectionType> allowed_connections;
945 allowed_connections.insert(ConnectionType::kCellular);
946 fake_state_.device_policy_provider()->
947 var_allowed_connection_types_for_update()->
948 reset(new set<ConnectionType>(allowed_connections));
949
950 bool result;
951 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700952 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700953 EXPECT_TRUE(result);
954}
955
Gilad Arnold684219d2014-07-07 14:54:57 -0700956TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700957 // Wimax is always allowed.
958
959 fake_state_.shill_provider()->var_conn_type()->
960 reset(new ConnectionType(ConnectionType::kWifi));
961
962 bool result;
963 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700964 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700965 EXPECT_TRUE(result);
966}
967
Alex Deymo63784a52014-05-28 10:46:14 -0700968TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700969 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
970 // Bluetooth is never allowed.
971
972 fake_state_.shill_provider()->var_conn_type()->
973 reset(new ConnectionType(ConnectionType::kBluetooth));
974
975 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700976 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700977 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700978}
979
Alex Deymo63784a52014-05-28 10:46:14 -0700980TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700981 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
982 // Bluetooth cannot be allowed even by policy.
983
984 fake_state_.shill_provider()->var_conn_type()->
985 reset(new ConnectionType(ConnectionType::kBluetooth));
986 set<ConnectionType> allowed_connections;
987 allowed_connections.insert(ConnectionType::kBluetooth);
988 fake_state_.device_policy_provider()->
989 var_allowed_connection_types_for_update()->
990 reset(new set<ConnectionType>(allowed_connections));
991
992 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700993 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700994 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700995}
996
Alex Deymo63784a52014-05-28 10:46:14 -0700997TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700998 // Cellular is not allowed by default.
999
1000 fake_state_.shill_provider()->var_conn_type()->
1001 reset(new ConnectionType(ConnectionType::kCellular));
1002
1003 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -07001004 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -07001005 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001006}
1007
Alex Deymo63784a52014-05-28 10:46:14 -07001008TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001009 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001010 // Update over cellular can be enabled by policy.
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_.device_policy_provider()->
1017 var_allowed_connection_types_for_update()->
1018 reset(new set<ConnectionType>(allowed_connections));
1019
1020 bool result;
1021 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001022 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001023 EXPECT_TRUE(result);
1024}
1025
Alex Deymo63784a52014-05-28 10:46:14 -07001026TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -07001027 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -07001028 // Update over cellular can be enabled by user settings, but only if policy
1029 // is present and does not determine allowed connections.
1030
1031 fake_state_.shill_provider()->var_conn_type()->
1032 reset(new ConnectionType(ConnectionType::kCellular));
1033 set<ConnectionType> allowed_connections;
1034 allowed_connections.insert(ConnectionType::kCellular);
1035 fake_state_.updater_provider()->var_cellular_enabled()->
1036 reset(new bool(true));
1037
1038 bool result;
1039 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001040 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001041 EXPECT_TRUE(result);
1042}
1043
Alex Deymo63784a52014-05-28 10:46:14 -07001044} // namespace chromeos_update_manager