blob: d0a2114d49e64b8f2a7970e550267102242a138e [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)
Alex Deymo0d11c602014-04-23 20:12:20 -070051
52 fake_state_.random_provider()->var_seed()->reset(
53 new uint64_t(4)); // chosen by fair dice roll.
54 // guaranteed to be random.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070055
56 // No device policy loaded by default.
57 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
58 new bool(false));
59
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070060 // OOBE is enabled by default.
61 fake_state_.config_provider()->var_is_oobe_enabled()->reset(
62 new bool(true));
63
Gilad Arnold76a11f62014-05-20 09:02:12 -070064 // For the purpose of the tests, this is an official build and OOBE was
65 // completed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070066 fake_state_.system_provider()->var_is_official_build()->reset(
67 new bool(true));
Gilad Arnold76a11f62014-05-20 09:02:12 -070068 fake_state_.system_provider()->var_is_oobe_complete()->reset(
69 new bool(true));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -070070 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
71 new bool(false));
Gilad Arnold0adbc942014-05-12 10:35:43 -070072
73 // Connection is wifi, untethered.
74 fake_state_.shill_provider()->var_conn_type()->
75 reset(new ConnectionType(ConnectionType::kWifi));
76 fake_state_.shill_provider()->var_conn_tethering()->
77 reset(new ConnectionTethering(ConnectionTethering::kNotDetected));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070078 }
79
Gilad Arnoldb3b05442014-05-30 14:25:05 -070080 // Sets up a default device policy that does not impose any restrictions
81 // (HTTP) nor enables any features (P2P).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070082 void SetUpDefaultDevicePolicy() {
83 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
84 new bool(true));
85 fake_state_.device_policy_provider()->var_update_disabled()->reset(
86 new bool(false));
87 fake_state_.device_policy_provider()->
88 var_allowed_connection_types_for_update()->reset(nullptr);
89 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
90 new TimeDelta());
91 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
Gilad Arnoldb3b05442014-05-30 14:25:05 -070092 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070093 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
94 new bool(false));
95 fake_state_.device_policy_provider()->var_release_channel_delegated()->
96 reset(new bool(true));
97 }
98
99 // Configures the UpdateCheckAllowed policy to return a desired value by
100 // faking the current wall clock time as needed. Restores the default state.
101 // This is used when testing policies that depend on this one.
102 void SetUpdateCheckAllowed(bool allow_check) {
103 Time next_update_check;
104 ExpectPolicyStatus(EvalStatus::kSucceeded,
105 &ChromeOSPolicy::NextUpdateCheckTime,
106 &next_update_check);
107 SetUpDefaultState();
108 SetUpDefaultDevicePolicy();
109 Time curr_time = next_update_check;
110 if (allow_check)
111 curr_time += TimeDelta::FromSeconds(1);
112 else
113 curr_time -= TimeDelta::FromSeconds(1);
114 fake_clock_.SetWallclockTime(curr_time);
115 }
116
117 // Returns a default UpdateState structure: first seen time is calculated
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700118 // backward from the current wall clock time, update was seen just once;
119 // there's a single HTTP download URL with a maximum of 10 allowed failures;
120 // there is no scattering wait period and the max allowed is 7 days, there is
121 // no check threshold and none is allowed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700122 UpdateState GetDefaultUpdateState(TimeDelta update_first_seen_period) {
123 UpdateState update_state = {
124 fake_clock_.GetWallclockTime() - update_first_seen_period, 1,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700125 vector<string>(1, "http://fake/url/"), 10, 0, 0, vector<ErrorCode>(),
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700126 TimeDelta(), TimeDelta::FromDays(7), 0, 0, 0
127 };
128 return update_state;
Alex Deymo0d11c602014-04-23 20:12:20 -0700129 }
130
131 // Runs the passed |policy_method| policy and expects it to return the
132 // |expected| return value.
133 template<typename T, typename R, typename... Args>
134 void ExpectPolicyStatus(
135 EvalStatus expected,
136 T policy_method,
137 R* result, Args... args) {
138 string error = "<None>";
139 eval_ctx_->ResetEvaluation();
140 EXPECT_EQ(expected,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700141 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result,
142 args...))
Alex Deymoc850b4f2014-05-19 11:06:41 -0700143 << "Returned error: " << error
144 << "\nEvaluation context: " << eval_ctx_->DumpContext();
Alex Deymo0d11c602014-04-23 20:12:20 -0700145 }
146
147 FakeClock fake_clock_;
148 FakeState fake_state_;
149 scoped_refptr<EvaluationContext> eval_ctx_;
150 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
151};
152
Alex Deymo63784a52014-05-28 10:46:14 -0700153TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700154 Time next_update_check;
155
Gilad Arnold38b14022014-07-09 12:45:56 -0700156 // Set the last update time so it'll appear as if this is a first update check
157 // in the lifetime of the current updater.
158 fake_state_.updater_provider()->var_last_checked_time()->reset(
159 new Time(fake_clock_.GetWallclockTime() - TimeDelta::FromMinutes(10)));
160
Alex Deymo0d11c602014-04-23 20:12:20 -0700161 ExpectPolicyStatus(EvalStatus::kSucceeded,
162 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
163
164 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
Gilad Arnold38b14022014-07-09 12:45:56 -0700165 EXPECT_GE(
166 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
167 ChromeOSPolicy::kTimeoutInitialInterval +
168 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
169 next_update_check);
170}
171
172TEST_F(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz) {
173 // Ensure that we're using the correct interval (kPeriodicInterval) and fuzz
174 // (kTimeoutRegularFuzz) as base values for period updates.
175 Time next_update_check;
176
177 ExpectPolicyStatus(EvalStatus::kSucceeded,
178 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
179
180 EXPECT_LE(
181 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
182 ChromeOSPolicy::kTimeoutPeriodicInterval -
183 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
184 next_update_check);
185 EXPECT_GE(
186 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
187 ChromeOSPolicy::kTimeoutPeriodicInterval +
188 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
189 next_update_check);
190}
191
192TEST_F(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz) {
193 // Ensure that we're properly backing off and fuzzing in the presence of
194 // failed updates attempts.
195 Time next_update_check;
196
197 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
198 reset(new unsigned int(2)); // NOLINT(readability/casting)
199
200 ExpectPolicyStatus(EvalStatus::kSucceeded,
201 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
202
203 int expected_interval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
204 EXPECT_LE(
205 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
206 expected_interval - expected_interval / 2),
207 next_update_check);
208 EXPECT_GE(
209 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
210 expected_interval + expected_interval / 2),
211 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700212}
213
Alex Deymo63784a52014-05-28 10:46:14 -0700214TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700215 Time next_update_check;
216
Alex Deymo0d11c602014-04-23 20:12:20 -0700217 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnold684219d2014-07-07 14:54:57 -0700218 reset(new unsigned int(100)); // NOLINT(readability/casting)
Gilad Arnold38b14022014-07-09 12:45:56 -0700219
Alex Deymo0d11c602014-04-23 20:12:20 -0700220 ExpectPolicyStatus(EvalStatus::kSucceeded,
221 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
222
Gilad Arnold38b14022014-07-09 12:45:56 -0700223 EXPECT_LE(
224 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
225 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
226 ChromeOSPolicy::kTimeoutMaxBackoffInterval / 2),
227 next_update_check);
228 EXPECT_GE(
229 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
230 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
231 ChromeOSPolicy::kTimeoutMaxBackoffInterval /2),
232 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700233}
234
Alex Deymo63784a52014-05-28 10:46:14 -0700235TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700236 // We get the next update_check timestamp from the policy's private method
237 // and then we check the public method respects that value on the normal
238 // case.
239 Time next_update_check;
240 Time last_checked_time =
241 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
242
Alex Deymo0d11c602014-04-23 20:12:20 -0700243 fake_state_.updater_provider()->var_last_checked_time()->reset(
244 new Time(last_checked_time));
245 ExpectPolicyStatus(EvalStatus::kSucceeded,
246 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
247
248 UpdateCheckParams result;
249
250 // Check that the policy blocks until the next_update_check is reached.
251 SetUpDefaultClock();
252 SetUpDefaultState();
253 fake_state_.updater_provider()->var_last_checked_time()->reset(
254 new Time(last_checked_time));
255 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
256 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
257 &Policy::UpdateCheckAllowed, &result);
258
259 SetUpDefaultClock();
260 SetUpDefaultState();
261 fake_state_.updater_provider()->var_last_checked_time()->reset(
262 new Time(last_checked_time));
263 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
264 ExpectPolicyStatus(EvalStatus::kSucceeded,
265 &Policy::UpdateCheckAllowed, &result);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700266 EXPECT_TRUE(result.updates_enabled);
267}
268
269TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE) {
270 // Update checks are defferred until OOBE is completed.
271
272 // Ensure that update is not allowed even if wait period is satisfied.
273 Time next_update_check;
274 Time last_checked_time =
275 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
276
277 fake_state_.updater_provider()->var_last_checked_time()->reset(
278 new Time(last_checked_time));
279 ExpectPolicyStatus(EvalStatus::kSucceeded,
280 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
281
282 SetUpDefaultClock();
283 SetUpDefaultState();
284 fake_state_.updater_provider()->var_last_checked_time()->reset(
285 new Time(last_checked_time));
286 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
287 fake_state_.system_provider()->var_is_oobe_complete()->reset(
288 new bool(false));
289
290 UpdateCheckParams result;
291 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
292 &Policy::UpdateCheckAllowed, &result);
293
294 // Now check that it is allowed if OOBE is completed.
295 SetUpDefaultClock();
296 SetUpDefaultState();
297 fake_state_.updater_provider()->var_last_checked_time()->reset(
298 new Time(last_checked_time));
299 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
300 ExpectPolicyStatus(EvalStatus::kSucceeded,
301 &Policy::UpdateCheckAllowed, &result);
302 EXPECT_TRUE(result.updates_enabled);
Alex Deymo0d11c602014-04-23 20:12:20 -0700303}
304
Gilad Arnold42f253b2014-06-25 12:39:17 -0700305TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
306 // Update check is allowed, reponse includes attributes for use in the
307 // request.
308 SetUpdateCheckAllowed(true);
309
310 // Override specific device policy attributes.
311 fake_state_.device_policy_provider()->var_release_channel_delegated()->
312 reset(new bool(false));
313 fake_state_.device_policy_provider()->var_release_channel()->
314 reset(new string("foo-channel"));
315
316 UpdateCheckParams result;
317 ExpectPolicyStatus(EvalStatus::kSucceeded,
318 &Policy::UpdateCheckAllowed, &result);
319 EXPECT_TRUE(result.updates_enabled);
320 EXPECT_EQ("foo-channel", result.target_channel);
321}
322
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700323TEST_F(UmChromeOSPolicyTest,
324 UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
325 // UpdateCheckAllowed should return false (kSucceeded) if this is an
326 // unofficial build; we don't want periodic update checks on developer images.
327
328 fake_state_.system_provider()->var_is_official_build()->reset(
329 new bool(false));
330
331 UpdateCheckParams result;
332 ExpectPolicyStatus(EvalStatus::kSucceeded,
333 &Policy::UpdateCheckAllowed, &result);
334 EXPECT_FALSE(result.updates_enabled);
335}
336
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700337TEST_F(UmChromeOSPolicyTest,
338 UpdateCheckAllowedUpdatesDisabledForRemovableBootDevice) {
339 // UpdateCheckAllowed should return false (kSucceeded) if the image booted
340 // from a removable device.
341
342 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
343 new bool(true));
344
345 UpdateCheckParams result;
346 ExpectPolicyStatus(EvalStatus::kSucceeded,
347 &Policy::UpdateCheckAllowed, &result);
348 EXPECT_FALSE(result.updates_enabled);
349}
350
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700351TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700352 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
353 // is loaded and prohibits updates.
354
355 SetUpdateCheckAllowed(false);
356 fake_state_.device_policy_provider()->var_update_disabled()->reset(
357 new bool(true));
358
Gilad Arnold42f253b2014-06-25 12:39:17 -0700359 UpdateCheckParams result;
360 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
361 &Policy::UpdateCheckAllowed, &result);
362}
363
Alex Deymo63784a52014-05-28 10:46:14 -0700364TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700365 // The UpdateCanStart policy fails, not being able to query
366 // UpdateCheckAllowed.
367
368 // Configure the UpdateCheckAllowed policy to fail.
369 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
370
371 // Check that the UpdateCanStart fails.
372 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700373 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700374 ExpectPolicyStatus(EvalStatus::kFailed,
375 &Policy::UpdateCanStart, &result, false, update_state);
376}
377
Alex Deymo63784a52014-05-28 10:46:14 -0700378TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700379 // The UpdateCanStart policy returns false because we are due for another
380 // update check.
381
382 SetUpdateCheckAllowed(true);
383
384 // Check that the UpdateCanStart returns false.
385 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700386 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700387 ExpectPolicyStatus(EvalStatus::kSucceeded,
388 &Policy::UpdateCanStart, &result, false, update_state);
389 EXPECT_FALSE(result.update_can_start);
390 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
391}
392
Alex Deymo63784a52014-05-28 10:46:14 -0700393TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700394 // The UpdateCanStart policy returns true; no device policy is loaded.
395
396 SetUpdateCheckAllowed(false);
397 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
398 new bool(false));
399
400 // Check that the UpdateCanStart returns true with no further attributes.
401 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700402 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700403 ExpectPolicyStatus(EvalStatus::kSucceeded,
404 &Policy::UpdateCanStart, &result, false, update_state);
405 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700406 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700407 EXPECT_EQ(0, result.download_url_idx);
408 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700409}
410
Alex Deymo63784a52014-05-28 10:46:14 -0700411TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700412 // The UpdateCanStart policy returns true; device policy is loaded but imposes
413 // no restrictions on updating.
414
415 SetUpdateCheckAllowed(false);
416
417 // Check that the UpdateCanStart returns true.
418 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700419 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700420 ExpectPolicyStatus(EvalStatus::kSucceeded,
421 &Policy::UpdateCanStart, &result, false, update_state);
422 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700423 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700424 EXPECT_EQ(0, result.download_url_idx);
425 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700426}
427
Alex Deymo63784a52014-05-28 10:46:14 -0700428TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700429 // The UpdateCanStart policy fails because the UpdateScattering policy it
430 // depends on fails (unset variable).
431
432 SetUpdateCheckAllowed(false);
433
434 // Override the default seed variable with a null value so that the policy
435 // request would fail.
436 fake_state_.random_provider()->var_seed()->reset(nullptr);
437
438 // Check that the UpdateCanStart fails.
439 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700440 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700441 ExpectPolicyStatus(EvalStatus::kFailed,
442 &Policy::UpdateCanStart, &result, false, update_state);
443}
444
Alex Deymo63784a52014-05-28 10:46:14 -0700445TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700446 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
447 // The UpdateCanStart policy returns false; device policy is loaded and
448 // scattering applies due to an unsatisfied wait period, which was newly
449 // generated.
450
451 SetUpdateCheckAllowed(false);
452 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
453 new TimeDelta(TimeDelta::FromMinutes(2)));
454
455
456 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
457
458 // Check that the UpdateCanStart returns false and a new wait period
459 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700460 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700461 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
462 false, update_state);
463 EXPECT_FALSE(result.update_can_start);
464 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
465 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
466 EXPECT_EQ(0, result.scatter_check_threshold);
467}
468
Alex Deymo63784a52014-05-28 10:46:14 -0700469TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700470 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
471 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
472 // is loaded and a previously generated scattering period still applies, none
473 // of the scattering values has changed.
474
475 SetUpdateCheckAllowed(false);
476 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
477 new TimeDelta(TimeDelta::FromMinutes(2)));
478
479 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
480 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
481
482 // Check that the UpdateCanStart returns false and a new wait period
483 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700484 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700485 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
486 &result, false, update_state);
487 EXPECT_FALSE(result.update_can_start);
488 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
489 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
490 EXPECT_EQ(0, result.scatter_check_threshold);
491}
492
Alex Deymo63784a52014-05-28 10:46:14 -0700493TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700494 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
495 // The UpdateCanStart policy returns false; device policy is loaded and
496 // scattering applies due to an unsatisfied update check count threshold.
497 //
498 // This ensures a non-zero check threshold, which may or may not be combined
499 // with a non-zero wait period (for which we cannot reliably control).
500
501 SetUpdateCheckAllowed(false);
502 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
503 new TimeDelta(TimeDelta::FromSeconds(1)));
504
505 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
506 update_state.scatter_check_threshold_min = 2;
507 update_state.scatter_check_threshold_max = 5;
508
509 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700510 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700511 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
512 false, update_state);
513 EXPECT_FALSE(result.update_can_start);
514 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
515 EXPECT_LE(2, result.scatter_check_threshold);
516 EXPECT_GE(5, result.scatter_check_threshold);
517}
518
Alex Deymo63784a52014-05-28 10:46:14 -0700519TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700520 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
521 // The UpdateCanStart policy returns false; device policy is loaded and
522 // scattering due to a previously generated count threshold still applies.
523
524 SetUpdateCheckAllowed(false);
525 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
526 new TimeDelta(TimeDelta::FromSeconds(1)));
527
528 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
529 update_state.scatter_check_threshold = 3;
530 update_state.scatter_check_threshold_min = 2;
531 update_state.scatter_check_threshold_max = 5;
532
533 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700534 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700535 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
536 false, update_state);
537 EXPECT_FALSE(result.update_can_start);
538 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
539 EXPECT_EQ(3, result.scatter_check_threshold);
540}
541
Alex Deymo63784a52014-05-28 10:46:14 -0700542TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700543 // The UpdateCanStart policy returns true; device policy is loaded and
544 // scattering is enabled, but both wait period and check threshold are
545 // satisfied.
546
547 SetUpdateCheckAllowed(false);
548 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
549 new TimeDelta(TimeDelta::FromSeconds(120)));
550
551 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
552 update_state.num_checks = 4;
553 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
554 update_state.scatter_check_threshold = 3;
555 update_state.scatter_check_threshold_min = 2;
556 update_state.scatter_check_threshold_max = 5;
557
558 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700559 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700560 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
561 false, update_state);
562 EXPECT_TRUE(result.update_can_start);
563 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
564 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700565 EXPECT_EQ(0, result.download_url_idx);
566 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700567}
568
Alex Deymo63784a52014-05-28 10:46:14 -0700569TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700570 UpdateCanStartAllowedInteractivePreventsScattering) {
571 // The UpdateCanStart policy returns true; device policy is loaded and
572 // scattering would have applied, except that the update check is interactive
573 // and so it is suppressed.
574
575 SetUpdateCheckAllowed(false);
576 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
577 new TimeDelta(TimeDelta::FromSeconds(1)));
578
579 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
580 update_state.scatter_check_threshold = 0;
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 true, 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 Arnold76a11f62014-05-20 09:02:12 -0700596 UpdateCanStartAllowedOobePreventsScattering) {
597 // The UpdateCanStart policy returns true; device policy is loaded and
598 // scattering would have applied, except that OOBE was not completed and so it
599 // is suppressed.
600
601 SetUpdateCheckAllowed(false);
602 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
603 new TimeDelta(TimeDelta::FromSeconds(1)));
604 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
605
606 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
607 update_state.scatter_check_threshold = 0;
608 update_state.scatter_check_threshold_min = 2;
609 update_state.scatter_check_threshold_max = 5;
610
611 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700612 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700613 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
614 true, update_state);
615 EXPECT_TRUE(result.update_can_start);
616 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
617 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700618 EXPECT_EQ(0, result.download_url_idx);
619 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700620}
621
Alex Deymo63784a52014-05-28 10:46:14 -0700622TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700623 // The UpdateCanStart policy returns true; device policy permits both HTTP and
624 // P2P updates, as well as a non-empty target channel string.
625
626 SetUpdateCheckAllowed(false);
627
628 // Override specific device policy attributes.
629 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
630 new bool(true));
631 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
632 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700633
634 // Check that the UpdateCanStart returns true.
635 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700636 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700637 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
638 false, update_state);
639 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700640 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700641 EXPECT_EQ(0, result.download_url_idx);
642 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700643}
644
Alex Deymo63784a52014-05-28 10:46:14 -0700645TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700646 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
647 // P2P updates, but the updater is configured to allow P2P and overrules the
648 // setting.
649
650 SetUpdateCheckAllowed(false);
651
652 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700653 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
654
655 // Check that the UpdateCanStart returns true.
656 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700657 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700658 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
659 false, update_state);
660 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700661 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700662 EXPECT_EQ(0, result.download_url_idx);
663 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700664}
665
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700666TEST_F(UmChromeOSPolicyTest,
667 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700668 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
669 // P2P updates, but marking this an unofficial build overrules the HTTP
670 // setting.
671
672 SetUpdateCheckAllowed(false);
673
674 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700675 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
676 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700677 fake_state_.system_provider()->var_is_official_build()->
678 reset(new bool(false));
679
680 // Check that the UpdateCanStart returns true.
681 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700682 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700683 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
684 false, update_state);
685 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700686 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700687 EXPECT_EQ(0, result.download_url_idx);
688 EXPECT_EQ(0, result.download_url_num_failures);
689}
690
691TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
692 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
693 // P2P updates, but an HTTPS URL is provided and selected for download.
694
695 SetUpdateCheckAllowed(false);
696
697 // Override specific device policy attributes.
698 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
699 new bool(false));
700
701 // Add an HTTPS URL.
702 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
703 update_state.download_urls.push_back("https://secure/url/");
704
705 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700706 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700707 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
708 false, update_state);
709 EXPECT_TRUE(result.update_can_start);
710 EXPECT_FALSE(result.p2p_allowed);
711 EXPECT_EQ(1, result.download_url_idx);
712 EXPECT_EQ(0, result.download_url_num_failures);
713}
714
715TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
716 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
717 // allowed number of failures, but a second URL is available.
718
719 SetUpdateCheckAllowed(false);
720
721 // Add a second URL; update with this URL attempted and failed enough times to
722 // disqualify the current (first) URL. This tests both the previously
723 // accounted failures (download_url_num_failures) as well as those occurring
724 // since the last call (download_url_error_codes).
725 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
726 update_state.num_checks = 10;
727 update_state.download_urls.push_back("http://another/fake/url/");
728 update_state.download_url_num_failures = 9;
729 update_state.download_url_error_codes.push_back(
730 ErrorCode::kDownloadTransferError);
731 update_state.download_url_error_codes.push_back(
732 ErrorCode::kDownloadWriteError);
733
734 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700735 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700736 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
737 false, update_state);
738 EXPECT_TRUE(result.update_can_start);
739 EXPECT_FALSE(result.p2p_allowed);
740 EXPECT_EQ(1, result.download_url_idx);
741 EXPECT_EQ(0, result.download_url_num_failures);
742}
743
744TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
745 // The UpdateCanStart policy returns true; the first URL fails with a hard
746 // error, but a second URL is available.
747
748 SetUpdateCheckAllowed(false);
749
750 // Add a second URL; update with this URL attempted and failed in a way that
751 // causes it to switch directly to the next URL.
752 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
753 update_state.num_checks = 10;
754 update_state.download_urls.push_back("http://another/fake/url/");
755 update_state.download_url_error_codes.push_back(
756 ErrorCode::kPayloadHashMismatchError);
757
758 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700759 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700760 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
761 false, update_state);
762 EXPECT_TRUE(result.update_can_start);
763 EXPECT_FALSE(result.p2p_allowed);
764 EXPECT_EQ(1, result.download_url_idx);
765 EXPECT_EQ(0, result.download_url_num_failures);
766}
767
768TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
769 // The UpdateCanStart policy returns true; URL search properly wraps around
770 // the last one on the list.
771
772 SetUpdateCheckAllowed(false);
773
774 // Add a second URL; update with this URL attempted and failed in a way that
775 // causes it to switch directly to the next URL.
776 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
777 update_state.num_checks = 10;
778 update_state.download_urls.push_back("http://another/fake/url/");
779 update_state.download_url_idx = 1;
780 update_state.download_url_error_codes.push_back(
781 ErrorCode::kPayloadHashMismatchError);
782
783 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700784 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700785 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
786 false, update_state);
787 EXPECT_TRUE(result.update_can_start);
788 EXPECT_FALSE(result.p2p_allowed);
789 EXPECT_EQ(0, result.download_url_idx);
790 EXPECT_EQ(0, result.download_url_num_failures);
791}
792
793TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
794 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
795 // use is forbidden by policy.
796
797 SetUpdateCheckAllowed(false);
798
799 // Override specific device policy attributes.
800 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
801 new bool(false));
802
803 // Check that the UpdateCanStart returns false.
804 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700805 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700806 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
807 false, update_state);
808}
809
810TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
811 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
812 // use is forbidden by policy, however P2P is enabled. The result indicates
813 // that no URL can be used.
814
815 SetUpdateCheckAllowed(false);
816
817 // Override specific device policy attributes.
818 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
819 new bool(true));
820 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
821 new bool(false));
822
823 // Check that the UpdateCanStart returns true.
824 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700825 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700826 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
827 false, update_state);
828 EXPECT_TRUE(result.update_can_start);
829 EXPECT_TRUE(result.p2p_allowed);
830 EXPECT_GT(0, result.download_url_idx);
831 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700832}
833
Gilad Arnold684219d2014-07-07 14:54:57 -0700834TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700835 // Ethernet is always allowed.
836
837 fake_state_.shill_provider()->var_conn_type()->
838 reset(new ConnectionType(ConnectionType::kEthernet));
839
840 bool result;
841 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700842 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700843 EXPECT_TRUE(result);
844}
845
Gilad Arnold684219d2014-07-07 14:54:57 -0700846TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700847 // Wifi is allowed if not tethered.
848
849 fake_state_.shill_provider()->var_conn_type()->
850 reset(new ConnectionType(ConnectionType::kWifi));
851
852 bool result;
853 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700854 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700855 EXPECT_TRUE(result);
856}
857
Alex Deymo63784a52014-05-28 10:46:14 -0700858TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700859 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
860 // Tethered wifi is not allowed by default.
861
862 fake_state_.shill_provider()->var_conn_type()->
863 reset(new ConnectionType(ConnectionType::kWifi));
864 fake_state_.shill_provider()->var_conn_tethering()->
865 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
866
867 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700868 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700869 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700870}
871
Alex Deymo63784a52014-05-28 10:46:14 -0700872TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700873 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700874 // Tethered wifi can be allowed by policy.
875
876 fake_state_.shill_provider()->var_conn_type()->
877 reset(new ConnectionType(ConnectionType::kWifi));
878 fake_state_.shill_provider()->var_conn_tethering()->
879 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
880 set<ConnectionType> allowed_connections;
881 allowed_connections.insert(ConnectionType::kCellular);
882 fake_state_.device_policy_provider()->
883 var_allowed_connection_types_for_update()->
884 reset(new set<ConnectionType>(allowed_connections));
885
886 bool result;
887 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700888 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700889 EXPECT_TRUE(result);
890}
891
Gilad Arnold684219d2014-07-07 14:54:57 -0700892TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700893 // Wimax is always allowed.
894
895 fake_state_.shill_provider()->var_conn_type()->
896 reset(new ConnectionType(ConnectionType::kWifi));
897
898 bool result;
899 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700900 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700901 EXPECT_TRUE(result);
902}
903
Alex Deymo63784a52014-05-28 10:46:14 -0700904TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700905 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
906 // Bluetooth is never allowed.
907
908 fake_state_.shill_provider()->var_conn_type()->
909 reset(new ConnectionType(ConnectionType::kBluetooth));
910
911 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700912 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700913 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700914}
915
Alex Deymo63784a52014-05-28 10:46:14 -0700916TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700917 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
918 // Bluetooth cannot be allowed even by policy.
919
920 fake_state_.shill_provider()->var_conn_type()->
921 reset(new ConnectionType(ConnectionType::kBluetooth));
922 set<ConnectionType> allowed_connections;
923 allowed_connections.insert(ConnectionType::kBluetooth);
924 fake_state_.device_policy_provider()->
925 var_allowed_connection_types_for_update()->
926 reset(new set<ConnectionType>(allowed_connections));
927
928 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700929 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700930 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700931}
932
Alex Deymo63784a52014-05-28 10:46:14 -0700933TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700934 // Cellular is not allowed by default.
935
936 fake_state_.shill_provider()->var_conn_type()->
937 reset(new ConnectionType(ConnectionType::kCellular));
938
939 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700940 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700941 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700942}
943
Alex Deymo63784a52014-05-28 10:46:14 -0700944TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700945 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700946 // Update over cellular can be enabled by policy.
947
948 fake_state_.shill_provider()->var_conn_type()->
949 reset(new ConnectionType(ConnectionType::kCellular));
950 set<ConnectionType> allowed_connections;
951 allowed_connections.insert(ConnectionType::kCellular);
952 fake_state_.device_policy_provider()->
953 var_allowed_connection_types_for_update()->
954 reset(new set<ConnectionType>(allowed_connections));
955
956 bool result;
957 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700958 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700959 EXPECT_TRUE(result);
960}
961
Alex Deymo63784a52014-05-28 10:46:14 -0700962TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700963 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700964 // Update over cellular can be enabled by user settings, but only if policy
965 // is present and does not determine allowed connections.
966
967 fake_state_.shill_provider()->var_conn_type()->
968 reset(new ConnectionType(ConnectionType::kCellular));
969 set<ConnectionType> allowed_connections;
970 allowed_connections.insert(ConnectionType::kCellular);
971 fake_state_.updater_provider()->var_cellular_enabled()->
972 reset(new bool(true));
973
974 bool result;
975 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700976 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700977 EXPECT_TRUE(result);
978}
979
Alex Deymo63784a52014-05-28 10:46:14 -0700980} // namespace chromeos_update_manager