blob: eef2250901153a65a73a37517fb472ceefa98462 [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 Arnold0adbc942014-05-12 10:35:43 -070066
67 // Connection is wifi, untethered.
68 fake_state_.shill_provider()->var_conn_type()->
69 reset(new ConnectionType(ConnectionType::kWifi));
70 fake_state_.shill_provider()->var_conn_tethering()->
71 reset(new ConnectionTethering(ConnectionTethering::kNotDetected));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070072 }
73
Gilad Arnoldb3b05442014-05-30 14:25:05 -070074 // Sets up a default device policy that does not impose any restrictions
75 // (HTTP) nor enables any features (P2P).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070076 void SetUpDefaultDevicePolicy() {
77 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
78 new bool(true));
79 fake_state_.device_policy_provider()->var_update_disabled()->reset(
80 new bool(false));
81 fake_state_.device_policy_provider()->
82 var_allowed_connection_types_for_update()->reset(nullptr);
83 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
84 new TimeDelta());
85 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
Gilad Arnoldb3b05442014-05-30 14:25:05 -070086 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070087 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
88 new bool(false));
89 fake_state_.device_policy_provider()->var_release_channel_delegated()->
90 reset(new bool(true));
91 }
92
93 // Configures the UpdateCheckAllowed policy to return a desired value by
94 // faking the current wall clock time as needed. Restores the default state.
95 // This is used when testing policies that depend on this one.
96 void SetUpdateCheckAllowed(bool allow_check) {
97 Time next_update_check;
98 ExpectPolicyStatus(EvalStatus::kSucceeded,
99 &ChromeOSPolicy::NextUpdateCheckTime,
100 &next_update_check);
101 SetUpDefaultState();
102 SetUpDefaultDevicePolicy();
103 Time curr_time = next_update_check;
104 if (allow_check)
105 curr_time += TimeDelta::FromSeconds(1);
106 else
107 curr_time -= TimeDelta::FromSeconds(1);
108 fake_clock_.SetWallclockTime(curr_time);
109 }
110
111 // Returns a default UpdateState structure: first seen time is calculated
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700112 // backward from the current wall clock time, update was seen just once;
113 // there's a single HTTP download URL with a maximum of 10 allowed failures;
114 // there is no scattering wait period and the max allowed is 7 days, there is
115 // no check threshold and none is allowed.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700116 UpdateState GetDefaultUpdateState(TimeDelta update_first_seen_period) {
117 UpdateState update_state = {
118 fake_clock_.GetWallclockTime() - update_first_seen_period, 1,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700119 vector<string>(1, "http://fake/url/"), 10, 0, 0, vector<ErrorCode>(),
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700120 TimeDelta(), TimeDelta::FromDays(7), 0, 0, 0
121 };
122 return update_state;
Alex Deymo0d11c602014-04-23 20:12:20 -0700123 }
124
125 // Runs the passed |policy_method| policy and expects it to return the
126 // |expected| return value.
127 template<typename T, typename R, typename... Args>
128 void ExpectPolicyStatus(
129 EvalStatus expected,
130 T policy_method,
131 R* result, Args... args) {
132 string error = "<None>";
133 eval_ctx_->ResetEvaluation();
134 EXPECT_EQ(expected,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700135 (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result,
136 args...))
Alex Deymoc850b4f2014-05-19 11:06:41 -0700137 << "Returned error: " << error
138 << "\nEvaluation context: " << eval_ctx_->DumpContext();
Alex Deymo0d11c602014-04-23 20:12:20 -0700139 }
140
141 FakeClock fake_clock_;
142 FakeState fake_state_;
143 scoped_refptr<EvaluationContext> eval_ctx_;
144 ChromeOSPolicy policy_; // ChromeOSPolicy under test.
145};
146
Alex Deymo63784a52014-05-28 10:46:14 -0700147TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700148 Time next_update_check;
149
Alex Deymo0d11c602014-04-23 20:12:20 -0700150 ExpectPolicyStatus(EvalStatus::kSucceeded,
151 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
152
153 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
154 EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
155 ChromeOSPolicy::kTimeoutInitialInterval +
156 ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check);
157}
158
Alex Deymo63784a52014-05-28 10:46:14 -0700159TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700160 Time next_update_check;
161
Alex Deymo0d11c602014-04-23 20:12:20 -0700162 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnold684219d2014-07-07 14:54:57 -0700163 reset(new unsigned int(100)); // NOLINT(readability/casting)
Alex Deymo0d11c602014-04-23 20:12:20 -0700164 ExpectPolicyStatus(EvalStatus::kSucceeded,
165 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
166
167 EXPECT_LE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
168 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
169 ChromeOSPolicy::kTimeoutRegularFuzz - 1), next_update_check);
170 EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
171 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
172 ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check);
173}
174
Alex Deymo63784a52014-05-28 10:46:14 -0700175TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700176 // We get the next update_check timestamp from the policy's private method
177 // and then we check the public method respects that value on the normal
178 // case.
179 Time next_update_check;
180 Time last_checked_time =
181 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
182
Alex Deymo0d11c602014-04-23 20:12:20 -0700183 fake_state_.updater_provider()->var_last_checked_time()->reset(
184 new Time(last_checked_time));
185 ExpectPolicyStatus(EvalStatus::kSucceeded,
186 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
187
188 UpdateCheckParams result;
189
190 // Check that the policy blocks until the next_update_check is reached.
191 SetUpDefaultClock();
192 SetUpDefaultState();
193 fake_state_.updater_provider()->var_last_checked_time()->reset(
194 new Time(last_checked_time));
195 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
196 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
197 &Policy::UpdateCheckAllowed, &result);
198
199 SetUpDefaultClock();
200 SetUpDefaultState();
201 fake_state_.updater_provider()->var_last_checked_time()->reset(
202 new Time(last_checked_time));
203 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
204 ExpectPolicyStatus(EvalStatus::kSucceeded,
205 &Policy::UpdateCheckAllowed, &result);
206}
207
Gilad Arnold42f253b2014-06-25 12:39:17 -0700208TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
209 // Update check is allowed, reponse includes attributes for use in the
210 // request.
211 SetUpdateCheckAllowed(true);
212
213 // Override specific device policy attributes.
214 fake_state_.device_policy_provider()->var_release_channel_delegated()->
215 reset(new bool(false));
216 fake_state_.device_policy_provider()->var_release_channel()->
217 reset(new string("foo-channel"));
218
219 UpdateCheckParams result;
220 ExpectPolicyStatus(EvalStatus::kSucceeded,
221 &Policy::UpdateCheckAllowed, &result);
222 EXPECT_TRUE(result.updates_enabled);
223 EXPECT_EQ("foo-channel", result.target_channel);
224}
225
226TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabled) {
227 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
228 // is loaded and prohibits updates.
229
230 SetUpdateCheckAllowed(false);
231 fake_state_.device_policy_provider()->var_update_disabled()->reset(
232 new bool(true));
233
234 // Check that the UpdateCanStart returns false.
235 UpdateCheckParams result;
236 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
237 &Policy::UpdateCheckAllowed, &result);
238}
239
Alex Deymo63784a52014-05-28 10:46:14 -0700240TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700241 // The UpdateCanStart policy fails, not being able to query
242 // UpdateCheckAllowed.
243
244 // Configure the UpdateCheckAllowed policy to fail.
245 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
246
247 // Check that the UpdateCanStart fails.
248 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700249 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700250 ExpectPolicyStatus(EvalStatus::kFailed,
251 &Policy::UpdateCanStart, &result, false, update_state);
252}
253
Alex Deymo63784a52014-05-28 10:46:14 -0700254TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700255 // The UpdateCanStart policy returns false because we are due for another
256 // update check.
257
258 SetUpdateCheckAllowed(true);
259
260 // Check that the UpdateCanStart returns false.
261 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700262 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700263 ExpectPolicyStatus(EvalStatus::kSucceeded,
264 &Policy::UpdateCanStart, &result, false, update_state);
265 EXPECT_FALSE(result.update_can_start);
266 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
267}
268
Alex Deymo63784a52014-05-28 10:46:14 -0700269TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700270 // The UpdateCanStart policy returns true; no device policy is loaded.
271
272 SetUpdateCheckAllowed(false);
273 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
274 new bool(false));
275
276 // Check that the UpdateCanStart returns true with no further attributes.
277 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700278 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700279 ExpectPolicyStatus(EvalStatus::kSucceeded,
280 &Policy::UpdateCanStart, &result, false, update_state);
281 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700282 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700283 EXPECT_EQ(0, result.download_url_idx);
284 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700285}
286
Alex Deymo63784a52014-05-28 10:46:14 -0700287TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700288 // The UpdateCanStart policy returns true; device policy is loaded but imposes
289 // no restrictions on updating.
290
291 SetUpdateCheckAllowed(false);
292
293 // Check that the UpdateCanStart returns true.
294 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700295 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700296 ExpectPolicyStatus(EvalStatus::kSucceeded,
297 &Policy::UpdateCanStart, &result, false, update_state);
298 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700299 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700300 EXPECT_EQ(0, result.download_url_idx);
301 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700302}
303
Alex Deymo63784a52014-05-28 10:46:14 -0700304TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700305 // The UpdateCanStart policy fails because the UpdateScattering policy it
306 // depends on fails (unset variable).
307
308 SetUpdateCheckAllowed(false);
309
310 // Override the default seed variable with a null value so that the policy
311 // request would fail.
312 fake_state_.random_provider()->var_seed()->reset(nullptr);
313
314 // Check that the UpdateCanStart fails.
315 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700316 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700317 ExpectPolicyStatus(EvalStatus::kFailed,
318 &Policy::UpdateCanStart, &result, false, update_state);
319}
320
Alex Deymo63784a52014-05-28 10:46:14 -0700321TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700322 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
323 // The UpdateCanStart policy returns false; device policy is loaded and
324 // scattering applies due to an unsatisfied wait period, which was newly
325 // generated.
326
327 SetUpdateCheckAllowed(false);
328 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
329 new TimeDelta(TimeDelta::FromMinutes(2)));
330
331
332 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
333
334 // Check that the UpdateCanStart returns false and a new wait period
335 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700336 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700337 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
338 false, update_state);
339 EXPECT_FALSE(result.update_can_start);
340 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
341 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
342 EXPECT_EQ(0, result.scatter_check_threshold);
343}
344
Alex Deymo63784a52014-05-28 10:46:14 -0700345TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700346 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
347 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
348 // is loaded and a previously generated scattering period still applies, none
349 // of the scattering values has changed.
350
351 SetUpdateCheckAllowed(false);
352 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
353 new TimeDelta(TimeDelta::FromMinutes(2)));
354
355 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
356 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
357
358 // Check that the UpdateCanStart returns false and a new wait period
359 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700360 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700361 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
362 &result, false, update_state);
363 EXPECT_FALSE(result.update_can_start);
364 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
365 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
366 EXPECT_EQ(0, result.scatter_check_threshold);
367}
368
Alex Deymo63784a52014-05-28 10:46:14 -0700369TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700370 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
371 // The UpdateCanStart policy returns false; device policy is loaded and
372 // scattering applies due to an unsatisfied update check count threshold.
373 //
374 // This ensures a non-zero check threshold, which may or may not be combined
375 // with a non-zero wait period (for which we cannot reliably control).
376
377 SetUpdateCheckAllowed(false);
378 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
379 new TimeDelta(TimeDelta::FromSeconds(1)));
380
381 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
382 update_state.scatter_check_threshold_min = 2;
383 update_state.scatter_check_threshold_max = 5;
384
385 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700386 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700387 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
388 false, update_state);
389 EXPECT_FALSE(result.update_can_start);
390 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
391 EXPECT_LE(2, result.scatter_check_threshold);
392 EXPECT_GE(5, result.scatter_check_threshold);
393}
394
Alex Deymo63784a52014-05-28 10:46:14 -0700395TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700396 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
397 // The UpdateCanStart policy returns false; device policy is loaded and
398 // scattering due to a previously generated count threshold still applies.
399
400 SetUpdateCheckAllowed(false);
401 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
402 new TimeDelta(TimeDelta::FromSeconds(1)));
403
404 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
405 update_state.scatter_check_threshold = 3;
406 update_state.scatter_check_threshold_min = 2;
407 update_state.scatter_check_threshold_max = 5;
408
409 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700410 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700411 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
412 false, update_state);
413 EXPECT_FALSE(result.update_can_start);
414 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
415 EXPECT_EQ(3, result.scatter_check_threshold);
416}
417
Alex Deymo63784a52014-05-28 10:46:14 -0700418TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700419 // The UpdateCanStart policy returns true; device policy is loaded and
420 // scattering is enabled, but both wait period and check threshold are
421 // satisfied.
422
423 SetUpdateCheckAllowed(false);
424 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
425 new TimeDelta(TimeDelta::FromSeconds(120)));
426
427 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
428 update_state.num_checks = 4;
429 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
430 update_state.scatter_check_threshold = 3;
431 update_state.scatter_check_threshold_min = 2;
432 update_state.scatter_check_threshold_max = 5;
433
434 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700435 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700436 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
437 false, update_state);
438 EXPECT_TRUE(result.update_can_start);
439 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
440 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700441 EXPECT_EQ(0, result.download_url_idx);
442 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700443}
444
Alex Deymo63784a52014-05-28 10:46:14 -0700445TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700446 UpdateCanStartAllowedInteractivePreventsScattering) {
447 // The UpdateCanStart policy returns true; device policy is loaded and
448 // scattering would have applied, except that the update check is interactive
449 // and so it is suppressed.
450
451 SetUpdateCheckAllowed(false);
452 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
453 new TimeDelta(TimeDelta::FromSeconds(1)));
454
455 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
456 update_state.scatter_check_threshold = 0;
457 update_state.scatter_check_threshold_min = 2;
458 update_state.scatter_check_threshold_max = 5;
459
460 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700461 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700462 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
463 true, update_state);
464 EXPECT_TRUE(result.update_can_start);
465 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
466 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700467 EXPECT_EQ(0, result.download_url_idx);
468 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700469}
470
Alex Deymo63784a52014-05-28 10:46:14 -0700471TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700472 UpdateCanStartAllowedOobePreventsScattering) {
473 // The UpdateCanStart policy returns true; device policy is loaded and
474 // scattering would have applied, except that OOBE was not completed and so it
475 // is suppressed.
476
477 SetUpdateCheckAllowed(false);
478 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
479 new TimeDelta(TimeDelta::FromSeconds(1)));
480 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
481
482 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
483 update_state.scatter_check_threshold = 0;
484 update_state.scatter_check_threshold_min = 2;
485 update_state.scatter_check_threshold_max = 5;
486
487 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700488 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700489 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
490 true, update_state);
491 EXPECT_TRUE(result.update_can_start);
492 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
493 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700494 EXPECT_EQ(0, result.download_url_idx);
495 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700496}
497
Alex Deymo63784a52014-05-28 10:46:14 -0700498TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700499 // The UpdateCanStart policy returns true; device policy permits both HTTP and
500 // P2P updates, as well as a non-empty target channel string.
501
502 SetUpdateCheckAllowed(false);
503
504 // Override specific device policy attributes.
505 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
506 new bool(true));
507 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
508 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700509
510 // Check that the UpdateCanStart returns true.
511 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700512 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700513 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
514 false, update_state);
515 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700516 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700517 EXPECT_EQ(0, result.download_url_idx);
518 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700519}
520
Alex Deymo63784a52014-05-28 10:46:14 -0700521TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700522 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
523 // P2P updates, but the updater is configured to allow P2P and overrules the
524 // setting.
525
526 SetUpdateCheckAllowed(false);
527
528 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700529 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
530
531 // Check that the UpdateCanStart returns true.
532 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700533 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700534 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
535 false, update_state);
536 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700537 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700538 EXPECT_EQ(0, result.download_url_idx);
539 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700540}
541
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700542TEST_F(UmChromeOSPolicyTest,
543 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700544 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
545 // P2P updates, but marking this an unofficial build overrules the HTTP
546 // setting.
547
548 SetUpdateCheckAllowed(false);
549
550 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700551 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
552 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700553 fake_state_.system_provider()->var_is_official_build()->
554 reset(new bool(false));
555
556 // Check that the UpdateCanStart returns true.
557 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700558 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700559 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
560 false, update_state);
561 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700562 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700563 EXPECT_EQ(0, result.download_url_idx);
564 EXPECT_EQ(0, result.download_url_num_failures);
565}
566
567TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
568 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
569 // P2P updates, but an HTTPS URL is provided and selected for download.
570
571 SetUpdateCheckAllowed(false);
572
573 // Override specific device policy attributes.
574 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
575 new bool(false));
576
577 // Add an HTTPS URL.
578 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
579 update_state.download_urls.push_back("https://secure/url/");
580
581 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700582 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700583 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
584 false, update_state);
585 EXPECT_TRUE(result.update_can_start);
586 EXPECT_FALSE(result.p2p_allowed);
587 EXPECT_EQ(1, result.download_url_idx);
588 EXPECT_EQ(0, result.download_url_num_failures);
589}
590
591TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
592 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
593 // allowed number of failures, but a second URL is available.
594
595 SetUpdateCheckAllowed(false);
596
597 // Add a second URL; update with this URL attempted and failed enough times to
598 // disqualify the current (first) URL. This tests both the previously
599 // accounted failures (download_url_num_failures) as well as those occurring
600 // since the last call (download_url_error_codes).
601 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
602 update_state.num_checks = 10;
603 update_state.download_urls.push_back("http://another/fake/url/");
604 update_state.download_url_num_failures = 9;
605 update_state.download_url_error_codes.push_back(
606 ErrorCode::kDownloadTransferError);
607 update_state.download_url_error_codes.push_back(
608 ErrorCode::kDownloadWriteError);
609
610 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700611 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700612 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
613 false, update_state);
614 EXPECT_TRUE(result.update_can_start);
615 EXPECT_FALSE(result.p2p_allowed);
616 EXPECT_EQ(1, result.download_url_idx);
617 EXPECT_EQ(0, result.download_url_num_failures);
618}
619
620TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
621 // The UpdateCanStart policy returns true; the first URL fails with a hard
622 // error, but a second URL is available.
623
624 SetUpdateCheckAllowed(false);
625
626 // Add a second URL; update with this URL attempted and failed in a way that
627 // causes it to switch directly to the next URL.
628 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
629 update_state.num_checks = 10;
630 update_state.download_urls.push_back("http://another/fake/url/");
631 update_state.download_url_error_codes.push_back(
632 ErrorCode::kPayloadHashMismatchError);
633
634 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700635 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700636 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
637 false, update_state);
638 EXPECT_TRUE(result.update_can_start);
639 EXPECT_FALSE(result.p2p_allowed);
640 EXPECT_EQ(1, result.download_url_idx);
641 EXPECT_EQ(0, result.download_url_num_failures);
642}
643
644TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
645 // The UpdateCanStart policy returns true; URL search properly wraps around
646 // the last one on the list.
647
648 SetUpdateCheckAllowed(false);
649
650 // Add a second URL; update with this URL attempted and failed in a way that
651 // causes it to switch directly to the next URL.
652 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
653 update_state.num_checks = 10;
654 update_state.download_urls.push_back("http://another/fake/url/");
655 update_state.download_url_idx = 1;
656 update_state.download_url_error_codes.push_back(
657 ErrorCode::kPayloadHashMismatchError);
658
659 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700660 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700661 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
662 false, update_state);
663 EXPECT_TRUE(result.update_can_start);
664 EXPECT_FALSE(result.p2p_allowed);
665 EXPECT_EQ(0, result.download_url_idx);
666 EXPECT_EQ(0, result.download_url_num_failures);
667}
668
669TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
670 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
671 // use is forbidden by policy.
672
673 SetUpdateCheckAllowed(false);
674
675 // Override specific device policy attributes.
676 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
677 new bool(false));
678
679 // Check that the UpdateCanStart returns false.
680 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700681 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700682 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
683 false, update_state);
684}
685
686TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
687 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
688 // use is forbidden by policy, however P2P is enabled. The result indicates
689 // that no URL can be used.
690
691 SetUpdateCheckAllowed(false);
692
693 // Override specific device policy attributes.
694 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
695 new bool(true));
696 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
697 new bool(false));
698
699 // Check that the UpdateCanStart returns true.
700 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700701 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700702 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
703 false, update_state);
704 EXPECT_TRUE(result.update_can_start);
705 EXPECT_TRUE(result.p2p_allowed);
706 EXPECT_GT(0, result.download_url_idx);
707 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700708}
709
Gilad Arnold684219d2014-07-07 14:54:57 -0700710TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700711 // Ethernet is always allowed.
712
713 fake_state_.shill_provider()->var_conn_type()->
714 reset(new ConnectionType(ConnectionType::kEthernet));
715
716 bool result;
717 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700718 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700719 EXPECT_TRUE(result);
720}
721
Gilad Arnold684219d2014-07-07 14:54:57 -0700722TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700723 // Wifi is allowed if not tethered.
724
725 fake_state_.shill_provider()->var_conn_type()->
726 reset(new ConnectionType(ConnectionType::kWifi));
727
728 bool result;
729 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700730 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700731 EXPECT_TRUE(result);
732}
733
Alex Deymo63784a52014-05-28 10:46:14 -0700734TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700735 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
736 // Tethered wifi is not allowed by default.
737
738 fake_state_.shill_provider()->var_conn_type()->
739 reset(new ConnectionType(ConnectionType::kWifi));
740 fake_state_.shill_provider()->var_conn_tethering()->
741 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
742
743 bool result;
744 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700745 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700746 EXPECT_FALSE(result);
747}
748
Alex Deymo63784a52014-05-28 10:46:14 -0700749TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700750 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700751 // Tethered wifi can be allowed by policy.
752
753 fake_state_.shill_provider()->var_conn_type()->
754 reset(new ConnectionType(ConnectionType::kWifi));
755 fake_state_.shill_provider()->var_conn_tethering()->
756 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
757 set<ConnectionType> allowed_connections;
758 allowed_connections.insert(ConnectionType::kCellular);
759 fake_state_.device_policy_provider()->
760 var_allowed_connection_types_for_update()->
761 reset(new set<ConnectionType>(allowed_connections));
762
763 bool result;
764 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700765 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700766 EXPECT_TRUE(result);
767}
768
Gilad Arnold684219d2014-07-07 14:54:57 -0700769TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700770 // Wimax is always allowed.
771
772 fake_state_.shill_provider()->var_conn_type()->
773 reset(new ConnectionType(ConnectionType::kWifi));
774
775 bool result;
776 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700777 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700778 EXPECT_TRUE(result);
779}
780
Alex Deymo63784a52014-05-28 10:46:14 -0700781TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700782 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
783 // Bluetooth is never allowed.
784
785 fake_state_.shill_provider()->var_conn_type()->
786 reset(new ConnectionType(ConnectionType::kBluetooth));
787
788 bool result;
789 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700790 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700791 EXPECT_FALSE(result);
792}
793
Alex Deymo63784a52014-05-28 10:46:14 -0700794TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700795 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
796 // Bluetooth cannot be allowed even by policy.
797
798 fake_state_.shill_provider()->var_conn_type()->
799 reset(new ConnectionType(ConnectionType::kBluetooth));
800 set<ConnectionType> allowed_connections;
801 allowed_connections.insert(ConnectionType::kBluetooth);
802 fake_state_.device_policy_provider()->
803 var_allowed_connection_types_for_update()->
804 reset(new set<ConnectionType>(allowed_connections));
805
806 bool result;
807 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700808 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700809 EXPECT_FALSE(result);
810}
811
Alex Deymo63784a52014-05-28 10:46:14 -0700812TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700813 // Cellular is not allowed by default.
814
815 fake_state_.shill_provider()->var_conn_type()->
816 reset(new ConnectionType(ConnectionType::kCellular));
817
818 bool result;
819 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700820 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700821 EXPECT_FALSE(result);
822}
823
Alex Deymo63784a52014-05-28 10:46:14 -0700824TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700825 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700826 // Update over cellular can be enabled by policy.
827
828 fake_state_.shill_provider()->var_conn_type()->
829 reset(new ConnectionType(ConnectionType::kCellular));
830 set<ConnectionType> allowed_connections;
831 allowed_connections.insert(ConnectionType::kCellular);
832 fake_state_.device_policy_provider()->
833 var_allowed_connection_types_for_update()->
834 reset(new set<ConnectionType>(allowed_connections));
835
836 bool result;
837 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700838 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700839 EXPECT_TRUE(result);
840}
841
Alex Deymo63784a52014-05-28 10:46:14 -0700842TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700843 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700844 // Update over cellular can be enabled by user settings, but only if policy
845 // is present and does not determine allowed connections.
846
847 fake_state_.shill_provider()->var_conn_type()->
848 reset(new ConnectionType(ConnectionType::kCellular));
849 set<ConnectionType> allowed_connections;
850 allowed_connections.insert(ConnectionType::kCellular);
851 fake_state_.updater_provider()->var_cellular_enabled()->
852 reset(new bool(true));
853
854 bool result;
855 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700856 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700857 EXPECT_TRUE(result);
858}
859
Alex Deymo63784a52014-05-28 10:46:14 -0700860} // namespace chromeos_update_manager