blob: 17eda8384dcd3cd958f63eadaf5aea0d4c306ac8 [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 Arnold76a11f62014-05-20 09:02:12 -070060 // For the purpose of the tests, this is an official build and OOBE was
61 // completed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070062 fake_state_.system_provider()->var_is_official_build()->reset(
63 new bool(true));
Gilad Arnold76a11f62014-05-20 09:02:12 -070064 fake_state_.system_provider()->var_is_oobe_complete()->reset(
65 new bool(true));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -070066 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
67 new bool(false));
Gilad Arnold0adbc942014-05-12 10:35:43 -070068
69 // Connection is wifi, untethered.
70 fake_state_.shill_provider()->var_conn_type()->
71 reset(new ConnectionType(ConnectionType::kWifi));
72 fake_state_.shill_provider()->var_conn_tethering()->
73 reset(new ConnectionTethering(ConnectionTethering::kNotDetected));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070074 }
75
Gilad Arnoldb3b05442014-05-30 14:25:05 -070076 // Sets up a default device policy that does not impose any restrictions
77 // (HTTP) nor enables any features (P2P).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070078 void SetUpDefaultDevicePolicy() {
79 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
80 new bool(true));
81 fake_state_.device_policy_provider()->var_update_disabled()->reset(
82 new bool(false));
83 fake_state_.device_policy_provider()->
84 var_allowed_connection_types_for_update()->reset(nullptr);
85 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
86 new TimeDelta());
87 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
Gilad Arnoldb3b05442014-05-30 14:25:05 -070088 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070089 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
90 new bool(false));
91 fake_state_.device_policy_provider()->var_release_channel_delegated()->
92 reset(new bool(true));
93 }
94
95 // Configures the UpdateCheckAllowed policy to return a desired value by
96 // faking the current wall clock time as needed. Restores the default state.
97 // This is used when testing policies that depend on this one.
98 void SetUpdateCheckAllowed(bool allow_check) {
99 Time next_update_check;
100 ExpectPolicyStatus(EvalStatus::kSucceeded,
101 &ChromeOSPolicy::NextUpdateCheckTime,
102 &next_update_check);
103 SetUpDefaultState();
104 SetUpDefaultDevicePolicy();
105 Time curr_time = next_update_check;
106 if (allow_check)
107 curr_time += TimeDelta::FromSeconds(1);
108 else
109 curr_time -= TimeDelta::FromSeconds(1);
110 fake_clock_.SetWallclockTime(curr_time);
111 }
112
113 // Returns a default UpdateState structure: first seen time is calculated
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700114 // backward from the current wall clock time, update was seen just once;
115 // there's a single HTTP download URL with a maximum of 10 allowed failures;
116 // there is no scattering wait period and the max allowed is 7 days, there is
117 // no check threshold and none is allowed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700118 UpdateState GetDefaultUpdateState(TimeDelta update_first_seen_period) {
119 UpdateState update_state = {
120 fake_clock_.GetWallclockTime() - update_first_seen_period, 1,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700121 vector<string>(1, "http://fake/url/"), 10, 0, 0, vector<ErrorCode>(),
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700122 TimeDelta(), TimeDelta::FromDays(7), 0, 0, 0
123 };
124 return update_state;
Alex Deymo0d11c602014-04-23 20:12:20 -0700125 }
126
127 // Runs the passed |policy_method| policy and expects it to return the
128 // |expected| return value.
129 template<typename T, typename R, typename... Args>
130 void ExpectPolicyStatus(
131 EvalStatus expected,
132 T policy_method,
133 R* result, Args... args) {
134 string error = "<None>";
135 eval_ctx_->ResetEvaluation();
136 EXPECT_EQ(expected,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700137 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result,
138 args...))
Alex Deymoc850b4f2014-05-19 11:06:41 -0700139 << "Returned error: " << error
140 << "\nEvaluation context: " << eval_ctx_->DumpContext();
Alex Deymo0d11c602014-04-23 20:12:20 -0700141 }
142
143 FakeClock fake_clock_;
144 FakeState fake_state_;
145 scoped_refptr<EvaluationContext> eval_ctx_;
146 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
147};
148
Alex Deymo63784a52014-05-28 10:46:14 -0700149TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700150 Time next_update_check;
151
Gilad Arnold38b14022014-07-09 12:45:56 -0700152 // Set the last update time so it'll appear as if this is a first update check
153 // in the lifetime of the current updater.
154 fake_state_.updater_provider()->var_last_checked_time()->reset(
155 new Time(fake_clock_.GetWallclockTime() - TimeDelta::FromMinutes(10)));
156
Alex Deymo0d11c602014-04-23 20:12:20 -0700157 ExpectPolicyStatus(EvalStatus::kSucceeded,
158 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
159
160 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
Gilad Arnold38b14022014-07-09 12:45:56 -0700161 EXPECT_GE(
162 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
163 ChromeOSPolicy::kTimeoutInitialInterval +
164 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
165 next_update_check);
166}
167
168TEST_F(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz) {
169 // Ensure that we're using the correct interval (kPeriodicInterval) and fuzz
170 // (kTimeoutRegularFuzz) as base values for period updates.
171 Time next_update_check;
172
173 ExpectPolicyStatus(EvalStatus::kSucceeded,
174 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
175
176 EXPECT_LE(
177 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
178 ChromeOSPolicy::kTimeoutPeriodicInterval -
179 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
180 next_update_check);
181 EXPECT_GE(
182 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
183 ChromeOSPolicy::kTimeoutPeriodicInterval +
184 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
185 next_update_check);
186}
187
188TEST_F(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz) {
189 // Ensure that we're properly backing off and fuzzing in the presence of
190 // failed updates attempts.
191 Time next_update_check;
192
193 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
194 reset(new unsigned int(2)); // NOLINT(readability/casting)
195
196 ExpectPolicyStatus(EvalStatus::kSucceeded,
197 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
198
199 int expected_interval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
200 EXPECT_LE(
201 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
202 expected_interval - expected_interval / 2),
203 next_update_check);
204 EXPECT_GE(
205 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
206 expected_interval + expected_interval / 2),
207 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700208}
209
Alex Deymo63784a52014-05-28 10:46:14 -0700210TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700211 Time next_update_check;
212
Alex Deymo0d11c602014-04-23 20:12:20 -0700213 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnold684219d2014-07-07 14:54:57 -0700214 reset(new unsigned int(100)); // NOLINT(readability/casting)
Gilad Arnold38b14022014-07-09 12:45:56 -0700215
Alex Deymo0d11c602014-04-23 20:12:20 -0700216 ExpectPolicyStatus(EvalStatus::kSucceeded,
217 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
218
Gilad Arnold38b14022014-07-09 12:45:56 -0700219 EXPECT_LE(
220 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
221 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
222 ChromeOSPolicy::kTimeoutMaxBackoffInterval / 2),
223 next_update_check);
224 EXPECT_GE(
225 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
226 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
227 ChromeOSPolicy::kTimeoutMaxBackoffInterval /2),
228 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700229}
230
Alex Deymo63784a52014-05-28 10:46:14 -0700231TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700232 // We get the next update_check timestamp from the policy's private method
233 // and then we check the public method respects that value on the normal
234 // case.
235 Time next_update_check;
236 Time last_checked_time =
237 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
238
Alex Deymo0d11c602014-04-23 20:12:20 -0700239 fake_state_.updater_provider()->var_last_checked_time()->reset(
240 new Time(last_checked_time));
241 ExpectPolicyStatus(EvalStatus::kSucceeded,
242 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
243
244 UpdateCheckParams result;
245
246 // Check that the policy blocks until the next_update_check is reached.
247 SetUpDefaultClock();
248 SetUpDefaultState();
249 fake_state_.updater_provider()->var_last_checked_time()->reset(
250 new Time(last_checked_time));
251 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
252 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
253 &Policy::UpdateCheckAllowed, &result);
254
255 SetUpDefaultClock();
256 SetUpDefaultState();
257 fake_state_.updater_provider()->var_last_checked_time()->reset(
258 new Time(last_checked_time));
259 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
260 ExpectPolicyStatus(EvalStatus::kSucceeded,
261 &Policy::UpdateCheckAllowed, &result);
262}
263
Gilad Arnold42f253b2014-06-25 12:39:17 -0700264TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
265 // Update check is allowed, reponse includes attributes for use in the
266 // request.
267 SetUpdateCheckAllowed(true);
268
269 // Override specific device policy attributes.
270 fake_state_.device_policy_provider()->var_release_channel_delegated()->
271 reset(new bool(false));
272 fake_state_.device_policy_provider()->var_release_channel()->
273 reset(new string("foo-channel"));
274
275 UpdateCheckParams result;
276 ExpectPolicyStatus(EvalStatus::kSucceeded,
277 &Policy::UpdateCheckAllowed, &result);
278 EXPECT_TRUE(result.updates_enabled);
279 EXPECT_EQ("foo-channel", result.target_channel);
280}
281
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700282TEST_F(UmChromeOSPolicyTest,
283 UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
284 // UpdateCheckAllowed should return false (kSucceeded) if this is an
285 // unofficial build; we don't want periodic update checks on developer images.
286
287 fake_state_.system_provider()->var_is_official_build()->reset(
288 new bool(false));
289
290 UpdateCheckParams result;
291 ExpectPolicyStatus(EvalStatus::kSucceeded,
292 &Policy::UpdateCheckAllowed, &result);
293 EXPECT_FALSE(result.updates_enabled);
294}
295
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700296TEST_F(UmChromeOSPolicyTest,
297 UpdateCheckAllowedUpdatesDisabledForRemovableBootDevice) {
298 // UpdateCheckAllowed should return false (kSucceeded) if the image booted
299 // from a removable device.
300
301 fake_state_.system_provider()->var_is_boot_device_removable()->reset(
302 new bool(true));
303
304 UpdateCheckParams result;
305 ExpectPolicyStatus(EvalStatus::kSucceeded,
306 &Policy::UpdateCheckAllowed, &result);
307 EXPECT_FALSE(result.updates_enabled);
308}
309
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700310TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700311 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
312 // is loaded and prohibits updates.
313
314 SetUpdateCheckAllowed(false);
315 fake_state_.device_policy_provider()->var_update_disabled()->reset(
316 new bool(true));
317
Gilad Arnold42f253b2014-06-25 12:39:17 -0700318 UpdateCheckParams result;
319 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
320 &Policy::UpdateCheckAllowed, &result);
321}
322
Alex Deymo63784a52014-05-28 10:46:14 -0700323TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700324 // The UpdateCanStart policy fails, not being able to query
325 // UpdateCheckAllowed.
326
327 // Configure the UpdateCheckAllowed policy to fail.
328 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
329
330 // Check that the UpdateCanStart fails.
331 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700332 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700333 ExpectPolicyStatus(EvalStatus::kFailed,
334 &Policy::UpdateCanStart, &result, false, update_state);
335}
336
Alex Deymo63784a52014-05-28 10:46:14 -0700337TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700338 // The UpdateCanStart policy returns false because we are due for another
339 // update check.
340
341 SetUpdateCheckAllowed(true);
342
343 // Check that the UpdateCanStart returns false.
344 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700345 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700346 ExpectPolicyStatus(EvalStatus::kSucceeded,
347 &Policy::UpdateCanStart, &result, false, update_state);
348 EXPECT_FALSE(result.update_can_start);
349 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
350}
351
Alex Deymo63784a52014-05-28 10:46:14 -0700352TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700353 // The UpdateCanStart policy returns true; no device policy is loaded.
354
355 SetUpdateCheckAllowed(false);
356 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
357 new bool(false));
358
359 // Check that the UpdateCanStart returns true with no further attributes.
360 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700361 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700362 ExpectPolicyStatus(EvalStatus::kSucceeded,
363 &Policy::UpdateCanStart, &result, false, update_state);
364 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700365 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700366 EXPECT_EQ(0, result.download_url_idx);
367 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700368}
369
Alex Deymo63784a52014-05-28 10:46:14 -0700370TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700371 // The UpdateCanStart policy returns true; device policy is loaded but imposes
372 // no restrictions on updating.
373
374 SetUpdateCheckAllowed(false);
375
376 // Check that the UpdateCanStart returns true.
377 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700378 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700379 ExpectPolicyStatus(EvalStatus::kSucceeded,
380 &Policy::UpdateCanStart, &result, false, update_state);
381 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700382 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700383 EXPECT_EQ(0, result.download_url_idx);
384 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700385}
386
Alex Deymo63784a52014-05-28 10:46:14 -0700387TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700388 // The UpdateCanStart policy fails because the UpdateScattering policy it
389 // depends on fails (unset variable).
390
391 SetUpdateCheckAllowed(false);
392
393 // Override the default seed variable with a null value so that the policy
394 // request would fail.
395 fake_state_.random_provider()->var_seed()->reset(nullptr);
396
397 // Check that the UpdateCanStart fails.
398 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
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,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700405 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
406 // The UpdateCanStart policy returns false; device policy is loaded and
407 // scattering applies due to an unsatisfied wait period, which was newly
408 // generated.
409
410 SetUpdateCheckAllowed(false);
411 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
412 new TimeDelta(TimeDelta::FromMinutes(2)));
413
414
415 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
416
417 // Check that the UpdateCanStart returns false and a new wait period
418 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700419 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700420 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
421 false, update_state);
422 EXPECT_FALSE(result.update_can_start);
423 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
424 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
425 EXPECT_EQ(0, result.scatter_check_threshold);
426}
427
Alex Deymo63784a52014-05-28 10:46:14 -0700428TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700429 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
430 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
431 // is loaded and a previously generated scattering period still applies, none
432 // of the scattering values has changed.
433
434 SetUpdateCheckAllowed(false);
435 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
436 new TimeDelta(TimeDelta::FromMinutes(2)));
437
438 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
439 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
440
441 // Check that the UpdateCanStart returns false and a new wait period
442 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700443 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700444 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
445 &result, false, update_state);
446 EXPECT_FALSE(result.update_can_start);
447 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
448 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
449 EXPECT_EQ(0, result.scatter_check_threshold);
450}
451
Alex Deymo63784a52014-05-28 10:46:14 -0700452TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700453 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
454 // The UpdateCanStart policy returns false; device policy is loaded and
455 // scattering applies due to an unsatisfied update check count threshold.
456 //
457 // This ensures a non-zero check threshold, which may or may not be combined
458 // with a non-zero wait period (for which we cannot reliably control).
459
460 SetUpdateCheckAllowed(false);
461 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
462 new TimeDelta(TimeDelta::FromSeconds(1)));
463
464 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
465 update_state.scatter_check_threshold_min = 2;
466 update_state.scatter_check_threshold_max = 5;
467
468 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700469 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700470 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
471 false, update_state);
472 EXPECT_FALSE(result.update_can_start);
473 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
474 EXPECT_LE(2, result.scatter_check_threshold);
475 EXPECT_GE(5, result.scatter_check_threshold);
476}
477
Alex Deymo63784a52014-05-28 10:46:14 -0700478TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700479 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
480 // The UpdateCanStart policy returns false; device policy is loaded and
481 // scattering due to a previously generated count threshold still applies.
482
483 SetUpdateCheckAllowed(false);
484 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
485 new TimeDelta(TimeDelta::FromSeconds(1)));
486
487 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
488 update_state.scatter_check_threshold = 3;
489 update_state.scatter_check_threshold_min = 2;
490 update_state.scatter_check_threshold_max = 5;
491
492 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700493 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700494 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
495 false, update_state);
496 EXPECT_FALSE(result.update_can_start);
497 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
498 EXPECT_EQ(3, result.scatter_check_threshold);
499}
500
Alex Deymo63784a52014-05-28 10:46:14 -0700501TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700502 // The UpdateCanStart policy returns true; device policy is loaded and
503 // scattering is enabled, but both wait period and check threshold are
504 // satisfied.
505
506 SetUpdateCheckAllowed(false);
507 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
508 new TimeDelta(TimeDelta::FromSeconds(120)));
509
510 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
511 update_state.num_checks = 4;
512 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
513 update_state.scatter_check_threshold = 3;
514 update_state.scatter_check_threshold_min = 2;
515 update_state.scatter_check_threshold_max = 5;
516
517 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700518 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700519 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
520 false, update_state);
521 EXPECT_TRUE(result.update_can_start);
522 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
523 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700524 EXPECT_EQ(0, result.download_url_idx);
525 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700526}
527
Alex Deymo63784a52014-05-28 10:46:14 -0700528TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700529 UpdateCanStartAllowedInteractivePreventsScattering) {
530 // The UpdateCanStart policy returns true; device policy is loaded and
531 // scattering would have applied, except that the update check is interactive
532 // and so it is suppressed.
533
534 SetUpdateCheckAllowed(false);
535 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
536 new TimeDelta(TimeDelta::FromSeconds(1)));
537
538 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
539 update_state.scatter_check_threshold = 0;
540 update_state.scatter_check_threshold_min = 2;
541 update_state.scatter_check_threshold_max = 5;
542
543 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700544 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700545 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
546 true, update_state);
547 EXPECT_TRUE(result.update_can_start);
548 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
549 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700550 EXPECT_EQ(0, result.download_url_idx);
551 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700552}
553
Alex Deymo63784a52014-05-28 10:46:14 -0700554TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700555 UpdateCanStartAllowedOobePreventsScattering) {
556 // The UpdateCanStart policy returns true; device policy is loaded and
557 // scattering would have applied, except that OOBE was not completed and so it
558 // is suppressed.
559
560 SetUpdateCheckAllowed(false);
561 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
562 new TimeDelta(TimeDelta::FromSeconds(1)));
563 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
564
565 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
566 update_state.scatter_check_threshold = 0;
567 update_state.scatter_check_threshold_min = 2;
568 update_state.scatter_check_threshold_max = 5;
569
570 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700571 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700572 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
573 true, update_state);
574 EXPECT_TRUE(result.update_can_start);
575 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
576 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700577 EXPECT_EQ(0, result.download_url_idx);
578 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700579}
580
Alex Deymo63784a52014-05-28 10:46:14 -0700581TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700582 // The UpdateCanStart policy returns true; device policy permits both HTTP and
583 // P2P updates, as well as a non-empty target channel string.
584
585 SetUpdateCheckAllowed(false);
586
587 // Override specific device policy attributes.
588 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
589 new bool(true));
590 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
591 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700592
593 // Check that the UpdateCanStart returns true.
594 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700595 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700596 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
597 false, update_state);
598 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700599 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700600 EXPECT_EQ(0, result.download_url_idx);
601 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700602}
603
Alex Deymo63784a52014-05-28 10:46:14 -0700604TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700605 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
606 // P2P updates, but the updater is configured to allow P2P and overrules the
607 // setting.
608
609 SetUpdateCheckAllowed(false);
610
611 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700612 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
613
614 // Check that the UpdateCanStart returns true.
615 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700616 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700617 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
618 false, update_state);
619 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700620 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700621 EXPECT_EQ(0, result.download_url_idx);
622 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700623}
624
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700625TEST_F(UmChromeOSPolicyTest,
626 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700627 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
628 // P2P updates, but marking this an unofficial build overrules the HTTP
629 // setting.
630
631 SetUpdateCheckAllowed(false);
632
633 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700634 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
635 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700636 fake_state_.system_provider()->var_is_official_build()->
637 reset(new bool(false));
638
639 // Check that the UpdateCanStart returns true.
640 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700641 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700642 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
643 false, update_state);
644 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700645 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700646 EXPECT_EQ(0, result.download_url_idx);
647 EXPECT_EQ(0, result.download_url_num_failures);
648}
649
650TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
651 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
652 // P2P updates, but an HTTPS URL is provided and selected for download.
653
654 SetUpdateCheckAllowed(false);
655
656 // Override specific device policy attributes.
657 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
658 new bool(false));
659
660 // Add an HTTPS URL.
661 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
662 update_state.download_urls.push_back("https://secure/url/");
663
664 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700665 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700666 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
667 false, update_state);
668 EXPECT_TRUE(result.update_can_start);
669 EXPECT_FALSE(result.p2p_allowed);
670 EXPECT_EQ(1, result.download_url_idx);
671 EXPECT_EQ(0, result.download_url_num_failures);
672}
673
674TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
675 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
676 // allowed number of failures, but a second URL is available.
677
678 SetUpdateCheckAllowed(false);
679
680 // Add a second URL; update with this URL attempted and failed enough times to
681 // disqualify the current (first) URL. This tests both the previously
682 // accounted failures (download_url_num_failures) as well as those occurring
683 // since the last call (download_url_error_codes).
684 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
685 update_state.num_checks = 10;
686 update_state.download_urls.push_back("http://another/fake/url/");
687 update_state.download_url_num_failures = 9;
688 update_state.download_url_error_codes.push_back(
689 ErrorCode::kDownloadTransferError);
690 update_state.download_url_error_codes.push_back(
691 ErrorCode::kDownloadWriteError);
692
693 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700694 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700695 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
696 false, update_state);
697 EXPECT_TRUE(result.update_can_start);
698 EXPECT_FALSE(result.p2p_allowed);
699 EXPECT_EQ(1, result.download_url_idx);
700 EXPECT_EQ(0, result.download_url_num_failures);
701}
702
703TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
704 // The UpdateCanStart policy returns true; the first URL fails with a hard
705 // error, but a second URL is available.
706
707 SetUpdateCheckAllowed(false);
708
709 // Add a second URL; update with this URL attempted and failed in a way that
710 // causes it to switch directly to the next URL.
711 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
712 update_state.num_checks = 10;
713 update_state.download_urls.push_back("http://another/fake/url/");
714 update_state.download_url_error_codes.push_back(
715 ErrorCode::kPayloadHashMismatchError);
716
717 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700718 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700719 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
720 false, update_state);
721 EXPECT_TRUE(result.update_can_start);
722 EXPECT_FALSE(result.p2p_allowed);
723 EXPECT_EQ(1, result.download_url_idx);
724 EXPECT_EQ(0, result.download_url_num_failures);
725}
726
727TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
728 // The UpdateCanStart policy returns true; URL search properly wraps around
729 // the last one on the list.
730
731 SetUpdateCheckAllowed(false);
732
733 // Add a second URL; update with this URL attempted and failed in a way that
734 // causes it to switch directly to the next URL.
735 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
736 update_state.num_checks = 10;
737 update_state.download_urls.push_back("http://another/fake/url/");
738 update_state.download_url_idx = 1;
739 update_state.download_url_error_codes.push_back(
740 ErrorCode::kPayloadHashMismatchError);
741
742 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700743 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700744 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
745 false, update_state);
746 EXPECT_TRUE(result.update_can_start);
747 EXPECT_FALSE(result.p2p_allowed);
748 EXPECT_EQ(0, result.download_url_idx);
749 EXPECT_EQ(0, result.download_url_num_failures);
750}
751
752TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
753 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
754 // use is forbidden by policy.
755
756 SetUpdateCheckAllowed(false);
757
758 // Override specific device policy attributes.
759 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
760 new bool(false));
761
762 // Check that the UpdateCanStart returns false.
763 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700764 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700765 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
766 false, update_state);
767}
768
769TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
770 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
771 // use is forbidden by policy, however P2P is enabled. The result indicates
772 // that no URL can be used.
773
774 SetUpdateCheckAllowed(false);
775
776 // Override specific device policy attributes.
777 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
778 new bool(true));
779 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
780 new bool(false));
781
782 // Check that the UpdateCanStart returns true.
783 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
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_TRUE(result.p2p_allowed);
789 EXPECT_GT(0, result.download_url_idx);
790 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700791}
792
Gilad Arnold684219d2014-07-07 14:54:57 -0700793TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700794 // Ethernet is always allowed.
795
796 fake_state_.shill_provider()->var_conn_type()->
797 reset(new ConnectionType(ConnectionType::kEthernet));
798
799 bool result;
800 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700801 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700802 EXPECT_TRUE(result);
803}
804
Gilad Arnold684219d2014-07-07 14:54:57 -0700805TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700806 // Wifi is allowed if not tethered.
807
808 fake_state_.shill_provider()->var_conn_type()->
809 reset(new ConnectionType(ConnectionType::kWifi));
810
811 bool result;
812 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700813 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700814 EXPECT_TRUE(result);
815}
816
Alex Deymo63784a52014-05-28 10:46:14 -0700817TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700818 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
819 // Tethered wifi is not allowed by default.
820
821 fake_state_.shill_provider()->var_conn_type()->
822 reset(new ConnectionType(ConnectionType::kWifi));
823 fake_state_.shill_provider()->var_conn_tethering()->
824 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
825
826 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700827 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700828 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700829}
830
Alex Deymo63784a52014-05-28 10:46:14 -0700831TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700832 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700833 // Tethered wifi can be allowed by policy.
834
835 fake_state_.shill_provider()->var_conn_type()->
836 reset(new ConnectionType(ConnectionType::kWifi));
837 fake_state_.shill_provider()->var_conn_tethering()->
838 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
839 set<ConnectionType> allowed_connections;
840 allowed_connections.insert(ConnectionType::kCellular);
841 fake_state_.device_policy_provider()->
842 var_allowed_connection_types_for_update()->
843 reset(new set<ConnectionType>(allowed_connections));
844
845 bool result;
846 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700847 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700848 EXPECT_TRUE(result);
849}
850
Gilad Arnold684219d2014-07-07 14:54:57 -0700851TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700852 // Wimax is always allowed.
853
854 fake_state_.shill_provider()->var_conn_type()->
855 reset(new ConnectionType(ConnectionType::kWifi));
856
857 bool result;
858 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700859 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700860 EXPECT_TRUE(result);
861}
862
Alex Deymo63784a52014-05-28 10:46:14 -0700863TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700864 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
865 // Bluetooth is never allowed.
866
867 fake_state_.shill_provider()->var_conn_type()->
868 reset(new ConnectionType(ConnectionType::kBluetooth));
869
870 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700871 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700872 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700873}
874
Alex Deymo63784a52014-05-28 10:46:14 -0700875TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700876 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
877 // Bluetooth cannot be allowed even by policy.
878
879 fake_state_.shill_provider()->var_conn_type()->
880 reset(new ConnectionType(ConnectionType::kBluetooth));
881 set<ConnectionType> allowed_connections;
882 allowed_connections.insert(ConnectionType::kBluetooth);
883 fake_state_.device_policy_provider()->
884 var_allowed_connection_types_for_update()->
885 reset(new set<ConnectionType>(allowed_connections));
886
887 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700888 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700889 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700890}
891
Alex Deymo63784a52014-05-28 10:46:14 -0700892TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700893 // Cellular is not allowed by default.
894
895 fake_state_.shill_provider()->var_conn_type()->
896 reset(new ConnectionType(ConnectionType::kCellular));
897
898 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700899 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700900 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700901}
902
Alex Deymo63784a52014-05-28 10:46:14 -0700903TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700904 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700905 // Update over cellular can be enabled by policy.
906
907 fake_state_.shill_provider()->var_conn_type()->
908 reset(new ConnectionType(ConnectionType::kCellular));
909 set<ConnectionType> allowed_connections;
910 allowed_connections.insert(ConnectionType::kCellular);
911 fake_state_.device_policy_provider()->
912 var_allowed_connection_types_for_update()->
913 reset(new set<ConnectionType>(allowed_connections));
914
915 bool result;
916 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700917 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700918 EXPECT_TRUE(result);
919}
920
Alex Deymo63784a52014-05-28 10:46:14 -0700921TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700922 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700923 // Update over cellular can be enabled by user settings, but only if policy
924 // is present and does not determine allowed connections.
925
926 fake_state_.shill_provider()->var_conn_type()->
927 reset(new ConnectionType(ConnectionType::kCellular));
928 set<ConnectionType> allowed_connections;
929 allowed_connections.insert(ConnectionType::kCellular);
930 fake_state_.updater_provider()->var_cellular_enabled()->
931 reset(new bool(true));
932
933 bool result;
934 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700935 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700936 EXPECT_TRUE(result);
937}
938
Alex Deymo63784a52014-05-28 10:46:14 -0700939} // namespace chromeos_update_manager