blob: ef609c14783ed1e969f559a2516a17678936cdc9 [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)
Alex Deymo0d11c602014-04-23 20:12:20 -070053
54 fake_state_.random_provider()->var_seed()->reset(
55 new uint64_t(4)); // chosen by fair dice roll.
56 // guaranteed to be random.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070057
58 // No device policy loaded by default.
59 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
60 new bool(false));
61
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070062 // OOBE is enabled by default.
63 fake_state_.config_provider()->var_is_oobe_enabled()->reset(
64 new bool(true));
65
Gilad Arnold76a11f62014-05-20 09:02:12 -070066 // For the purpose of the tests, this is an official build and OOBE was
67 // completed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070068 fake_state_.system_provider()->var_is_official_build()->reset(
69 new bool(true));
Gilad Arnold76a11f62014-05-20 09:02:12 -070070 fake_state_.system_provider()->var_is_oobe_complete()->reset(
71 new bool(true));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -070072 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
73 new bool(false));
Gilad Arnold0adbc942014-05-12 10:35:43 -070074
75 // Connection is wifi, untethered.
76 fake_state_.shill_provider()->var_conn_type()->
77 reset(new ConnectionType(ConnectionType::kWifi));
78 fake_state_.shill_provider()->var_conn_tethering()->
79 reset(new ConnectionTethering(ConnectionTethering::kNotDetected));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070080 }
81
Gilad Arnoldb3b05442014-05-30 14:25:05 -070082 // Sets up a default device policy that does not impose any restrictions
83 // (HTTP) nor enables any features (P2P).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070084 void SetUpDefaultDevicePolicy() {
85 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
86 new bool(true));
87 fake_state_.device_policy_provider()->var_update_disabled()->reset(
88 new bool(false));
89 fake_state_.device_policy_provider()->
90 var_allowed_connection_types_for_update()->reset(nullptr);
91 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
92 new TimeDelta());
93 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
Gilad Arnoldb3b05442014-05-30 14:25:05 -070094 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070095 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
96 new bool(false));
97 fake_state_.device_policy_provider()->var_release_channel_delegated()->
98 reset(new bool(true));
99 }
100
101 // Configures the UpdateCheckAllowed policy to return a desired value by
102 // faking the current wall clock time as needed. Restores the default state.
103 // This is used when testing policies that depend on this one.
104 void SetUpdateCheckAllowed(bool allow_check) {
105 Time next_update_check;
106 ExpectPolicyStatus(EvalStatus::kSucceeded,
107 &ChromeOSPolicy::NextUpdateCheckTime,
108 &next_update_check);
109 SetUpDefaultState();
110 SetUpDefaultDevicePolicy();
111 Time curr_time = next_update_check;
112 if (allow_check)
113 curr_time += TimeDelta::FromSeconds(1);
114 else
115 curr_time -= TimeDelta::FromSeconds(1);
116 fake_clock_.SetWallclockTime(curr_time);
117 }
118
119 // Returns a default UpdateState structure: first seen time is calculated
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700120 // backward from the current wall clock time, update was seen just once;
121 // there's a single HTTP download URL with a maximum of 10 allowed failures;
122 // there is no scattering wait period and the max allowed is 7 days, there is
123 // no check threshold and none is allowed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700124 UpdateState GetDefaultUpdateState(TimeDelta update_first_seen_period) {
125 UpdateState update_state = {
126 fake_clock_.GetWallclockTime() - update_first_seen_period, 1,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700127 vector<string>(1, "http://fake/url/"), 10, 0, 0, vector<ErrorCode>(),
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700128 TimeDelta(), TimeDelta::FromDays(7), 0, 0, 0
129 };
130 return update_state;
Alex Deymo0d11c602014-04-23 20:12:20 -0700131 }
132
133 // Runs the passed |policy_method| policy and expects it to return the
134 // |expected| return value.
135 template<typename T, typename R, typename... Args>
136 void ExpectPolicyStatus(
137 EvalStatus expected,
138 T policy_method,
139 R* result, Args... args) {
140 string error = "<None>";
141 eval_ctx_->ResetEvaluation();
142 EXPECT_EQ(expected,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700143 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result,
144 args...))
Alex Deymoc850b4f2014-05-19 11:06:41 -0700145 << "Returned error: " << error
146 << "\nEvaluation context: " << eval_ctx_->DumpContext();
Alex Deymo0d11c602014-04-23 20:12:20 -0700147 }
148
149 FakeClock fake_clock_;
150 FakeState fake_state_;
151 scoped_refptr<EvaluationContext> eval_ctx_;
152 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
153};
154
Alex Deymo63784a52014-05-28 10:46:14 -0700155TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700156 Time next_update_check;
157
Gilad Arnold38b14022014-07-09 12:45:56 -0700158 // Set the last update time so it'll appear as if this is a first update check
159 // in the lifetime of the current updater.
160 fake_state_.updater_provider()->var_last_checked_time()->reset(
161 new Time(fake_clock_.GetWallclockTime() - TimeDelta::FromMinutes(10)));
162
Alex Deymo0d11c602014-04-23 20:12:20 -0700163 ExpectPolicyStatus(EvalStatus::kSucceeded,
164 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
165
166 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
Gilad Arnold38b14022014-07-09 12:45:56 -0700167 EXPECT_GE(
168 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
169 ChromeOSPolicy::kTimeoutInitialInterval +
170 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
171 next_update_check);
172}
173
174TEST_F(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz) {
175 // Ensure that we're using the correct interval (kPeriodicInterval) and fuzz
176 // (kTimeoutRegularFuzz) as base values for period updates.
177 Time next_update_check;
178
179 ExpectPolicyStatus(EvalStatus::kSucceeded,
180 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
181
182 EXPECT_LE(
183 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
184 ChromeOSPolicy::kTimeoutPeriodicInterval -
185 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
186 next_update_check);
187 EXPECT_GE(
188 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
189 ChromeOSPolicy::kTimeoutPeriodicInterval +
190 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
191 next_update_check);
192}
193
194TEST_F(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz) {
195 // Ensure that we're properly backing off and fuzzing in the presence of
196 // failed updates attempts.
197 Time next_update_check;
198
199 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
200 reset(new unsigned int(2)); // NOLINT(readability/casting)
201
202 ExpectPolicyStatus(EvalStatus::kSucceeded,
203 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
204
205 int expected_interval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
206 EXPECT_LE(
207 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
208 expected_interval - expected_interval / 2),
209 next_update_check);
210 EXPECT_GE(
211 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
212 expected_interval + expected_interval / 2),
213 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700214}
215
Gilad Arnolda0258a52014-07-10 16:21:19 -0700216TEST_F(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval) {
217 // Policy honors the server provided check poll interval.
218 Time next_update_check;
219
220 const unsigned int kInterval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
221 fake_state_.updater_provider()->var_server_dictated_poll_interval()->
222 reset(new unsigned int(kInterval)); // NOLINT(readability/casting)
223 // We should not be backing off in this case.
224 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
225 reset(new unsigned int(2)); // NOLINT(readability/casting)
226
227 ExpectPolicyStatus(EvalStatus::kSucceeded,
228 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
229
230 EXPECT_LE(
231 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
232 kInterval - kInterval / 2),
233 next_update_check);
234 EXPECT_GE(
235 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
236 kInterval + kInterval / 2),
237 next_update_check);
238}
239
Alex Deymo63784a52014-05-28 10:46:14 -0700240TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700241 Time next_update_check;
242
Alex Deymo0d11c602014-04-23 20:12:20 -0700243 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnold684219d2014-07-07 14:54:57 -0700244 reset(new unsigned int(100)); // NOLINT(readability/casting)
Gilad Arnold38b14022014-07-09 12:45:56 -0700245
Alex Deymo0d11c602014-04-23 20:12:20 -0700246 ExpectPolicyStatus(EvalStatus::kSucceeded,
247 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
248
Gilad Arnold38b14022014-07-09 12:45:56 -0700249 EXPECT_LE(
250 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
251 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
252 ChromeOSPolicy::kTimeoutMaxBackoffInterval / 2),
253 next_update_check);
254 EXPECT_GE(
255 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
256 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
257 ChromeOSPolicy::kTimeoutMaxBackoffInterval /2),
258 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700259}
260
Alex Deymo63784a52014-05-28 10:46:14 -0700261TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700262 // We get the next update_check timestamp from the policy's private method
263 // and then we check the public method respects that value on the normal
264 // case.
265 Time next_update_check;
266 Time last_checked_time =
267 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
268
Alex Deymo0d11c602014-04-23 20:12:20 -0700269 fake_state_.updater_provider()->var_last_checked_time()->reset(
270 new Time(last_checked_time));
271 ExpectPolicyStatus(EvalStatus::kSucceeded,
272 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
273
274 UpdateCheckParams result;
275
276 // Check that the policy blocks until the next_update_check is reached.
277 SetUpDefaultClock();
278 SetUpDefaultState();
279 fake_state_.updater_provider()->var_last_checked_time()->reset(
280 new Time(last_checked_time));
281 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
282 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
283 &Policy::UpdateCheckAllowed, &result);
284
285 SetUpDefaultClock();
286 SetUpDefaultState();
287 fake_state_.updater_provider()->var_last_checked_time()->reset(
288 new Time(last_checked_time));
289 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
290 ExpectPolicyStatus(EvalStatus::kSucceeded,
291 &Policy::UpdateCheckAllowed, &result);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700292 EXPECT_TRUE(result.updates_enabled);
293}
294
295TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700296 // Update checks are deferred until OOBE is completed.
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700297
298 // Ensure that update is not allowed even if wait period is satisfied.
299 Time next_update_check;
300 Time last_checked_time =
301 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
302
303 fake_state_.updater_provider()->var_last_checked_time()->reset(
304 new Time(last_checked_time));
305 ExpectPolicyStatus(EvalStatus::kSucceeded,
306 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
307
308 SetUpDefaultClock();
309 SetUpDefaultState();
310 fake_state_.updater_provider()->var_last_checked_time()->reset(
311 new Time(last_checked_time));
312 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
313 fake_state_.system_provider()->var_is_oobe_complete()->reset(
314 new bool(false));
315
316 UpdateCheckParams result;
317 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
318 &Policy::UpdateCheckAllowed, &result);
319
320 // Now check that it is allowed if OOBE is completed.
321 SetUpDefaultClock();
322 SetUpDefaultState();
323 fake_state_.updater_provider()->var_last_checked_time()->reset(
324 new Time(last_checked_time));
325 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
326 ExpectPolicyStatus(EvalStatus::kSucceeded,
327 &Policy::UpdateCheckAllowed, &result);
328 EXPECT_TRUE(result.updates_enabled);
Alex Deymo0d11c602014-04-23 20:12:20 -0700329}
330
Gilad Arnold42f253b2014-06-25 12:39:17 -0700331TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700332 // Update check is allowed, response includes attributes for use in the
Gilad Arnold42f253b2014-06-25 12:39:17 -0700333 // request.
334 SetUpdateCheckAllowed(true);
335
336 // Override specific device policy attributes.
337 fake_state_.device_policy_provider()->var_release_channel_delegated()->
338 reset(new bool(false));
339 fake_state_.device_policy_provider()->var_release_channel()->
340 reset(new string("foo-channel"));
341
342 UpdateCheckParams result;
343 ExpectPolicyStatus(EvalStatus::kSucceeded,
344 &Policy::UpdateCheckAllowed, &result);
345 EXPECT_TRUE(result.updates_enabled);
346 EXPECT_EQ("foo-channel", result.target_channel);
347}
348
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700349TEST_F(UmChromeOSPolicyTest,
350 UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
351 // UpdateCheckAllowed should return false (kSucceeded) if this is an
352 // unofficial build; we don't want periodic update checks on developer images.
353
354 fake_state_.system_provider()->var_is_official_build()->reset(
355 new bool(false));
356
357 UpdateCheckParams result;
358 ExpectPolicyStatus(EvalStatus::kSucceeded,
359 &Policy::UpdateCheckAllowed, &result);
360 EXPECT_FALSE(result.updates_enabled);
361}
362
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700363TEST_F(UmChromeOSPolicyTest,
364 UpdateCheckAllowedUpdatesDisabledForRemovableBootDevice) {
365 // UpdateCheckAllowed should return false (kSucceeded) if the image booted
366 // from a removable device.
367
368 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
369 new bool(true));
370
371 UpdateCheckParams result;
372 ExpectPolicyStatus(EvalStatus::kSucceeded,
373 &Policy::UpdateCheckAllowed, &result);
374 EXPECT_FALSE(result.updates_enabled);
375}
376
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700377TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700378 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
379 // is loaded and prohibits updates.
380
381 SetUpdateCheckAllowed(false);
382 fake_state_.device_policy_provider()->var_update_disabled()->reset(
383 new bool(true));
384
Gilad Arnold42f253b2014-06-25 12:39:17 -0700385 UpdateCheckParams result;
386 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
387 &Policy::UpdateCheckAllowed, &result);
388}
389
Alex Deymo63784a52014-05-28 10:46:14 -0700390TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700391 // The UpdateCanStart policy fails, not being able to query
392 // UpdateCheckAllowed.
393
394 // Configure the UpdateCheckAllowed policy to fail.
395 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
396
397 // Check that the UpdateCanStart fails.
398 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700399 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700400 ExpectPolicyStatus(EvalStatus::kFailed,
401 &Policy::UpdateCanStart, &result, false, update_state);
402}
403
Alex Deymo63784a52014-05-28 10:46:14 -0700404TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700405 // The UpdateCanStart policy returns false because we are due for another
406 // update check.
407
408 SetUpdateCheckAllowed(true);
409
410 // Check that the UpdateCanStart returns false.
411 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700412 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700413 ExpectPolicyStatus(EvalStatus::kSucceeded,
414 &Policy::UpdateCanStart, &result, false, update_state);
415 EXPECT_FALSE(result.update_can_start);
416 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
417}
418
Alex Deymo63784a52014-05-28 10:46:14 -0700419TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700420 // The UpdateCanStart policy returns true; no device policy is loaded.
421
422 SetUpdateCheckAllowed(false);
423 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
424 new bool(false));
425
426 // Check that the UpdateCanStart returns true with no further attributes.
427 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700428 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700429 ExpectPolicyStatus(EvalStatus::kSucceeded,
430 &Policy::UpdateCanStart, &result, false, update_state);
431 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700432 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700433 EXPECT_EQ(0, result.download_url_idx);
434 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700435}
436
Alex Deymo63784a52014-05-28 10:46:14 -0700437TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700438 // The UpdateCanStart policy returns true; device policy is loaded but imposes
439 // no restrictions on updating.
440
441 SetUpdateCheckAllowed(false);
442
443 // Check that the UpdateCanStart returns true.
444 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700445 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700446 ExpectPolicyStatus(EvalStatus::kSucceeded,
447 &Policy::UpdateCanStart, &result, false, update_state);
448 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700449 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700450 EXPECT_EQ(0, result.download_url_idx);
451 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700452}
453
Alex Deymo63784a52014-05-28 10:46:14 -0700454TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700455 // The UpdateCanStart policy fails because the UpdateScattering policy it
456 // depends on fails (unset variable).
457
458 SetUpdateCheckAllowed(false);
459
460 // Override the default seed variable with a null value so that the policy
461 // request would fail.
462 fake_state_.random_provider()->var_seed()->reset(nullptr);
463
464 // Check that the UpdateCanStart fails.
465 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700466 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700467 ExpectPolicyStatus(EvalStatus::kFailed,
468 &Policy::UpdateCanStart, &result, false, update_state);
469}
470
Alex Deymo63784a52014-05-28 10:46:14 -0700471TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700472 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
473 // The UpdateCanStart policy returns false; device policy is loaded and
474 // scattering applies due to an unsatisfied wait period, which was newly
475 // generated.
476
477 SetUpdateCheckAllowed(false);
478 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
479 new TimeDelta(TimeDelta::FromMinutes(2)));
480
481
482 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
483
484 // Check that the UpdateCanStart returns false and a new wait period
485 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700486 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700487 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
488 false, update_state);
489 EXPECT_FALSE(result.update_can_start);
490 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
491 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
492 EXPECT_EQ(0, result.scatter_check_threshold);
493}
494
Alex Deymo63784a52014-05-28 10:46:14 -0700495TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700496 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
497 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
498 // is loaded and a previously generated scattering period still applies, none
499 // of the scattering values has changed.
500
501 SetUpdateCheckAllowed(false);
502 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
503 new TimeDelta(TimeDelta::FromMinutes(2)));
504
505 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
506 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
507
508 // Check that the UpdateCanStart returns false and a new wait period
509 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700510 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700511 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
512 &result, false, update_state);
513 EXPECT_FALSE(result.update_can_start);
514 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
515 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
516 EXPECT_EQ(0, result.scatter_check_threshold);
517}
518
Alex Deymo63784a52014-05-28 10:46:14 -0700519TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700520 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
521 // The UpdateCanStart policy returns false; device policy is loaded and
522 // scattering applies due to an unsatisfied update check count threshold.
523 //
524 // This ensures a non-zero check threshold, which may or may not be combined
525 // with a non-zero wait period (for which we cannot reliably control).
526
527 SetUpdateCheckAllowed(false);
528 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
529 new TimeDelta(TimeDelta::FromSeconds(1)));
530
531 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
532 update_state.scatter_check_threshold_min = 2;
533 update_state.scatter_check_threshold_max = 5;
534
535 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700536 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700537 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
538 false, update_state);
539 EXPECT_FALSE(result.update_can_start);
540 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
541 EXPECT_LE(2, result.scatter_check_threshold);
542 EXPECT_GE(5, result.scatter_check_threshold);
543}
544
Alex Deymo63784a52014-05-28 10:46:14 -0700545TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700546 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
547 // The UpdateCanStart policy returns false; device policy is loaded and
548 // scattering due to a previously generated count threshold still applies.
549
550 SetUpdateCheckAllowed(false);
551 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
552 new TimeDelta(TimeDelta::FromSeconds(1)));
553
554 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
555 update_state.scatter_check_threshold = 3;
556 update_state.scatter_check_threshold_min = 2;
557 update_state.scatter_check_threshold_max = 5;
558
559 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700560 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700561 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
562 false, update_state);
563 EXPECT_FALSE(result.update_can_start);
564 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
565 EXPECT_EQ(3, result.scatter_check_threshold);
566}
567
Alex Deymo63784a52014-05-28 10:46:14 -0700568TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700569 // The UpdateCanStart policy returns true; device policy is loaded and
570 // scattering is enabled, but both wait period and check threshold are
571 // satisfied.
572
573 SetUpdateCheckAllowed(false);
574 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
575 new TimeDelta(TimeDelta::FromSeconds(120)));
576
577 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
578 update_state.num_checks = 4;
579 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
580 update_state.scatter_check_threshold = 3;
581 update_state.scatter_check_threshold_min = 2;
582 update_state.scatter_check_threshold_max = 5;
583
584 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700585 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700586 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
587 false, update_state);
588 EXPECT_TRUE(result.update_can_start);
589 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
590 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700591 EXPECT_EQ(0, result.download_url_idx);
592 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700593}
594
Alex Deymo63784a52014-05-28 10:46:14 -0700595TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700596 UpdateCanStartAllowedInteractivePreventsScattering) {
597 // The UpdateCanStart policy returns true; device policy is loaded and
598 // scattering would have applied, except that the update check is interactive
599 // and so it is suppressed.
600
601 SetUpdateCheckAllowed(false);
602 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
603 new TimeDelta(TimeDelta::FromSeconds(1)));
604
605 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
606 update_state.scatter_check_threshold = 0;
607 update_state.scatter_check_threshold_min = 2;
608 update_state.scatter_check_threshold_max = 5;
609
610 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700611 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700612 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
613 true, update_state);
614 EXPECT_TRUE(result.update_can_start);
615 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
616 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700617 EXPECT_EQ(0, result.download_url_idx);
618 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700619}
620
Alex Deymo63784a52014-05-28 10:46:14 -0700621TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700622 UpdateCanStartAllowedOobePreventsScattering) {
623 // The UpdateCanStart policy returns true; device policy is loaded and
624 // scattering would have applied, except that OOBE was not completed and so it
625 // is suppressed.
626
627 SetUpdateCheckAllowed(false);
628 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
629 new TimeDelta(TimeDelta::FromSeconds(1)));
630 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
631
632 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
633 update_state.scatter_check_threshold = 0;
634 update_state.scatter_check_threshold_min = 2;
635 update_state.scatter_check_threshold_max = 5;
636
637 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700638 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700639 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
640 true, update_state);
641 EXPECT_TRUE(result.update_can_start);
642 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
643 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700644 EXPECT_EQ(0, result.download_url_idx);
645 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700646}
647
Alex Deymo63784a52014-05-28 10:46:14 -0700648TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700649 // The UpdateCanStart policy returns true; device policy permits both HTTP and
650 // P2P updates, as well as a non-empty target channel string.
651
652 SetUpdateCheckAllowed(false);
653
654 // Override specific device policy attributes.
655 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
656 new bool(true));
657 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
658 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700659
660 // Check that the UpdateCanStart returns true.
661 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700662 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700663 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
664 false, update_state);
665 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700666 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700667 EXPECT_EQ(0, result.download_url_idx);
668 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700669}
670
Alex Deymo63784a52014-05-28 10:46:14 -0700671TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700672 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
673 // P2P updates, but the updater is configured to allow P2P and overrules the
674 // setting.
675
676 SetUpdateCheckAllowed(false);
677
678 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700679 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
680
681 // Check that the UpdateCanStart returns true.
682 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700683 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700684 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
685 false, update_state);
686 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700687 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700688 EXPECT_EQ(0, result.download_url_idx);
689 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700690}
691
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700692TEST_F(UmChromeOSPolicyTest,
693 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700694 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
695 // P2P updates, but marking this an unofficial build overrules the HTTP
696 // setting.
697
698 SetUpdateCheckAllowed(false);
699
700 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700701 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
702 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700703 fake_state_.system_provider()->var_is_official_build()->
704 reset(new bool(false));
705
706 // Check that the UpdateCanStart returns true.
707 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700708 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700709 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
710 false, update_state);
711 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700712 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700713 EXPECT_EQ(0, result.download_url_idx);
714 EXPECT_EQ(0, result.download_url_num_failures);
715}
716
717TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
718 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
719 // P2P updates, but an HTTPS URL is provided and selected for download.
720
721 SetUpdateCheckAllowed(false);
722
723 // Override specific device policy attributes.
724 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
725 new bool(false));
726
727 // Add an HTTPS URL.
728 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
729 update_state.download_urls.push_back("https://secure/url/");
730
731 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700732 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700733 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
734 false, update_state);
735 EXPECT_TRUE(result.update_can_start);
736 EXPECT_FALSE(result.p2p_allowed);
737 EXPECT_EQ(1, result.download_url_idx);
738 EXPECT_EQ(0, result.download_url_num_failures);
739}
740
741TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
742 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
743 // allowed number of failures, but a second URL is available.
744
745 SetUpdateCheckAllowed(false);
746
747 // Add a second URL; update with this URL attempted and failed enough times to
748 // disqualify the current (first) URL. This tests both the previously
749 // accounted failures (download_url_num_failures) as well as those occurring
750 // since the last call (download_url_error_codes).
751 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
752 update_state.num_checks = 10;
753 update_state.download_urls.push_back("http://another/fake/url/");
754 update_state.download_url_num_failures = 9;
755 update_state.download_url_error_codes.push_back(
756 ErrorCode::kDownloadTransferError);
757 update_state.download_url_error_codes.push_back(
758 ErrorCode::kDownloadWriteError);
759
760 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700761 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700762 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
763 false, update_state);
764 EXPECT_TRUE(result.update_can_start);
765 EXPECT_FALSE(result.p2p_allowed);
766 EXPECT_EQ(1, result.download_url_idx);
767 EXPECT_EQ(0, result.download_url_num_failures);
768}
769
770TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
771 // The UpdateCanStart policy returns true; the first URL fails with a hard
772 // error, but a second URL is available.
773
774 SetUpdateCheckAllowed(false);
775
776 // Add a second URL; update with this URL attempted and failed in a way that
777 // causes it to switch directly to the next URL.
778 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
779 update_state.num_checks = 10;
780 update_state.download_urls.push_back("http://another/fake/url/");
781 update_state.download_url_error_codes.push_back(
782 ErrorCode::kPayloadHashMismatchError);
783
784 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700785 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700786 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
787 false, update_state);
788 EXPECT_TRUE(result.update_can_start);
789 EXPECT_FALSE(result.p2p_allowed);
790 EXPECT_EQ(1, result.download_url_idx);
791 EXPECT_EQ(0, result.download_url_num_failures);
792}
793
794TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
795 // The UpdateCanStart policy returns true; URL search properly wraps around
796 // the last one on the list.
797
798 SetUpdateCheckAllowed(false);
799
800 // Add a second URL; update with this URL attempted and failed in a way that
801 // causes it to switch directly to the next URL.
802 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
803 update_state.num_checks = 10;
804 update_state.download_urls.push_back("http://another/fake/url/");
805 update_state.download_url_idx = 1;
806 update_state.download_url_error_codes.push_back(
807 ErrorCode::kPayloadHashMismatchError);
808
809 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700810 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700811 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
812 false, update_state);
813 EXPECT_TRUE(result.update_can_start);
814 EXPECT_FALSE(result.p2p_allowed);
815 EXPECT_EQ(0, result.download_url_idx);
816 EXPECT_EQ(0, result.download_url_num_failures);
817}
818
819TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
820 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
821 // use is forbidden by policy.
822
823 SetUpdateCheckAllowed(false);
824
825 // Override specific device policy attributes.
826 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
827 new bool(false));
828
829 // Check that the UpdateCanStart returns false.
830 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700831 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700832 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
833 false, update_state);
834}
835
836TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
837 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
838 // use is forbidden by policy, however P2P is enabled. The result indicates
839 // that no URL can be used.
840
841 SetUpdateCheckAllowed(false);
842
843 // Override specific device policy attributes.
844 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
845 new bool(true));
846 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
847 new bool(false));
848
849 // Check that the UpdateCanStart returns true.
850 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700851 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700852 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
853 false, update_state);
854 EXPECT_TRUE(result.update_can_start);
855 EXPECT_TRUE(result.p2p_allowed);
856 EXPECT_GT(0, result.download_url_idx);
857 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700858}
859
Gilad Arnold684219d2014-07-07 14:54:57 -0700860TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700861 // Ethernet is always allowed.
862
863 fake_state_.shill_provider()->var_conn_type()->
864 reset(new ConnectionType(ConnectionType::kEthernet));
865
866 bool result;
867 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700868 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700869 EXPECT_TRUE(result);
870}
871
Gilad Arnold684219d2014-07-07 14:54:57 -0700872TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700873 // Wifi is allowed if not tethered.
874
875 fake_state_.shill_provider()->var_conn_type()->
876 reset(new ConnectionType(ConnectionType::kWifi));
877
878 bool result;
879 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700880 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700881 EXPECT_TRUE(result);
882}
883
Alex Deymo63784a52014-05-28 10:46:14 -0700884TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700885 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
886 // Tethered wifi is not allowed by default.
887
888 fake_state_.shill_provider()->var_conn_type()->
889 reset(new ConnectionType(ConnectionType::kWifi));
890 fake_state_.shill_provider()->var_conn_tethering()->
891 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
892
893 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700894 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700895 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700896}
897
Alex Deymo63784a52014-05-28 10:46:14 -0700898TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700899 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700900 // Tethered wifi can be allowed by policy.
901
902 fake_state_.shill_provider()->var_conn_type()->
903 reset(new ConnectionType(ConnectionType::kWifi));
904 fake_state_.shill_provider()->var_conn_tethering()->
905 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
906 set<ConnectionType> allowed_connections;
907 allowed_connections.insert(ConnectionType::kCellular);
908 fake_state_.device_policy_provider()->
909 var_allowed_connection_types_for_update()->
910 reset(new set<ConnectionType>(allowed_connections));
911
912 bool result;
913 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700914 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700915 EXPECT_TRUE(result);
916}
917
Gilad Arnold684219d2014-07-07 14:54:57 -0700918TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700919 // Wimax is always allowed.
920
921 fake_state_.shill_provider()->var_conn_type()->
922 reset(new ConnectionType(ConnectionType::kWifi));
923
924 bool result;
925 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700926 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700927 EXPECT_TRUE(result);
928}
929
Alex Deymo63784a52014-05-28 10:46:14 -0700930TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700931 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
932 // Bluetooth is never allowed.
933
934 fake_state_.shill_provider()->var_conn_type()->
935 reset(new ConnectionType(ConnectionType::kBluetooth));
936
937 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700938 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700939 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700940}
941
Alex Deymo63784a52014-05-28 10:46:14 -0700942TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700943 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
944 // Bluetooth cannot be allowed even by policy.
945
946 fake_state_.shill_provider()->var_conn_type()->
947 reset(new ConnectionType(ConnectionType::kBluetooth));
948 set<ConnectionType> allowed_connections;
949 allowed_connections.insert(ConnectionType::kBluetooth);
950 fake_state_.device_policy_provider()->
951 var_allowed_connection_types_for_update()->
952 reset(new set<ConnectionType>(allowed_connections));
953
954 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700955 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700956 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700957}
958
Alex Deymo63784a52014-05-28 10:46:14 -0700959TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700960 // Cellular is not allowed by default.
961
962 fake_state_.shill_provider()->var_conn_type()->
963 reset(new ConnectionType(ConnectionType::kCellular));
964
965 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700966 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700967 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700968}
969
Alex Deymo63784a52014-05-28 10:46:14 -0700970TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700971 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700972 // Update over cellular can be enabled by policy.
973
974 fake_state_.shill_provider()->var_conn_type()->
975 reset(new ConnectionType(ConnectionType::kCellular));
976 set<ConnectionType> allowed_connections;
977 allowed_connections.insert(ConnectionType::kCellular);
978 fake_state_.device_policy_provider()->
979 var_allowed_connection_types_for_update()->
980 reset(new set<ConnectionType>(allowed_connections));
981
982 bool result;
983 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700984 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700985 EXPECT_TRUE(result);
986}
987
Alex Deymo63784a52014-05-28 10:46:14 -0700988TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700989 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700990 // Update over cellular can be enabled by user settings, but only if policy
991 // is present and does not determine allowed connections.
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_.updater_provider()->var_cellular_enabled()->
998 reset(new bool(true));
999
1000 bool result;
1001 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -07001002 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -07001003 EXPECT_TRUE(result);
1004}
1005
Alex Deymo63784a52014-05-28 10:46:14 -07001006} // namespace chromeos_update_manager