blob: 0537a659f10077ae10025a6498bf02de74d03c1d [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
Gilad Arnold38b14022014-07-09 12:45:56 -0700150 // Set the last update time so it'll appear as if this is a first update check
151 // in the lifetime of the current updater.
152 fake_state_.updater_provider()->var_last_checked_time()->reset(
153 new Time(fake_clock_.GetWallclockTime() - TimeDelta::FromMinutes(10)));
154
Alex Deymo0d11c602014-04-23 20:12:20 -0700155 ExpectPolicyStatus(EvalStatus::kSucceeded,
156 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
157
158 EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check);
Gilad Arnold38b14022014-07-09 12:45:56 -0700159 EXPECT_GE(
160 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
161 ChromeOSPolicy::kTimeoutInitialInterval +
162 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
163 next_update_check);
164}
165
166TEST_F(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz) {
167 // Ensure that we're using the correct interval (kPeriodicInterval) and fuzz
168 // (kTimeoutRegularFuzz) as base values for period updates.
169 Time next_update_check;
170
171 ExpectPolicyStatus(EvalStatus::kSucceeded,
172 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
173
174 EXPECT_LE(
175 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
176 ChromeOSPolicy::kTimeoutPeriodicInterval -
177 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
178 next_update_check);
179 EXPECT_GE(
180 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
181 ChromeOSPolicy::kTimeoutPeriodicInterval +
182 ChromeOSPolicy::kTimeoutRegularFuzz / 2),
183 next_update_check);
184}
185
186TEST_F(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz) {
187 // Ensure that we're properly backing off and fuzzing in the presence of
188 // failed updates attempts.
189 Time next_update_check;
190
191 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
192 reset(new unsigned int(2)); // NOLINT(readability/casting)
193
194 ExpectPolicyStatus(EvalStatus::kSucceeded,
195 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
196
197 int expected_interval = ChromeOSPolicy::kTimeoutPeriodicInterval * 4;
198 EXPECT_LE(
199 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
200 expected_interval - expected_interval / 2),
201 next_update_check);
202 EXPECT_GE(
203 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
204 expected_interval + expected_interval / 2),
205 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700206}
207
Alex Deymo63784a52014-05-28 10:46:14 -0700208TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700209 Time next_update_check;
210
Alex Deymo0d11c602014-04-23 20:12:20 -0700211 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->
Gilad Arnold684219d2014-07-07 14:54:57 -0700212 reset(new unsigned int(100)); // NOLINT(readability/casting)
Gilad Arnold38b14022014-07-09 12:45:56 -0700213
Alex Deymo0d11c602014-04-23 20:12:20 -0700214 ExpectPolicyStatus(EvalStatus::kSucceeded,
215 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
216
Gilad Arnold38b14022014-07-09 12:45:56 -0700217 EXPECT_LE(
218 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
219 ChromeOSPolicy::kTimeoutMaxBackoffInterval -
220 ChromeOSPolicy::kTimeoutMaxBackoffInterval / 2),
221 next_update_check);
222 EXPECT_GE(
223 fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds(
224 ChromeOSPolicy::kTimeoutMaxBackoffInterval +
225 ChromeOSPolicy::kTimeoutMaxBackoffInterval /2),
226 next_update_check);
Alex Deymo0d11c602014-04-23 20:12:20 -0700227}
228
Alex Deymo63784a52014-05-28 10:46:14 -0700229TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
Alex Deymo0d11c602014-04-23 20:12:20 -0700230 // We get the next update_check timestamp from the policy's private method
231 // and then we check the public method respects that value on the normal
232 // case.
233 Time next_update_check;
234 Time last_checked_time =
235 fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
236
Alex Deymo0d11c602014-04-23 20:12:20 -0700237 fake_state_.updater_provider()->var_last_checked_time()->reset(
238 new Time(last_checked_time));
239 ExpectPolicyStatus(EvalStatus::kSucceeded,
240 &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check);
241
242 UpdateCheckParams result;
243
244 // Check that the policy blocks until the next_update_check is reached.
245 SetUpDefaultClock();
246 SetUpDefaultState();
247 fake_state_.updater_provider()->var_last_checked_time()->reset(
248 new Time(last_checked_time));
249 fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
250 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
251 &Policy::UpdateCheckAllowed, &result);
252
253 SetUpDefaultClock();
254 SetUpDefaultState();
255 fake_state_.updater_provider()->var_last_checked_time()->reset(
256 new Time(last_checked_time));
257 fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
258 ExpectPolicyStatus(EvalStatus::kSucceeded,
259 &Policy::UpdateCheckAllowed, &result);
260}
261
Gilad Arnold42f253b2014-06-25 12:39:17 -0700262TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWithAttributes) {
263 // Update check is allowed, reponse includes attributes for use in the
264 // request.
265 SetUpdateCheckAllowed(true);
266
267 // Override specific device policy attributes.
268 fake_state_.device_policy_provider()->var_release_channel_delegated()->
269 reset(new bool(false));
270 fake_state_.device_policy_provider()->var_release_channel()->
271 reset(new string("foo-channel"));
272
273 UpdateCheckParams result;
274 ExpectPolicyStatus(EvalStatus::kSucceeded,
275 &Policy::UpdateCheckAllowed, &result);
276 EXPECT_TRUE(result.updates_enabled);
277 EXPECT_EQ("foo-channel", result.target_channel);
278}
279
Gilad Arnoldfe12a0f2014-07-09 14:26:57 -0700280TEST_F(UmChromeOSPolicyTest,
281 UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
282 // UpdateCheckAllowed should return false (kSucceeded) if this is an
283 // unofficial build; we don't want periodic update checks on developer images.
284
285 fake_state_.system_provider()->var_is_official_build()->reset(
286 new bool(false));
287
288 UpdateCheckParams result;
289 ExpectPolicyStatus(EvalStatus::kSucceeded,
290 &Policy::UpdateCheckAllowed, &result);
291 EXPECT_FALSE(result.updates_enabled);
292}
293
294TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedUpdatesDisabledByPolicy) {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700295 // UpdateCheckAllowed should return kAskMeAgainLater because a device policy
296 // is loaded and prohibits updates.
297
298 SetUpdateCheckAllowed(false);
299 fake_state_.device_policy_provider()->var_update_disabled()->reset(
300 new bool(true));
301
Gilad Arnold42f253b2014-06-25 12:39:17 -0700302 UpdateCheckParams result;
303 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
304 &Policy::UpdateCheckAllowed, &result);
305}
306
Alex Deymo63784a52014-05-28 10:46:14 -0700307TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700308 // The UpdateCanStart policy fails, not being able to query
309 // UpdateCheckAllowed.
310
311 // Configure the UpdateCheckAllowed policy to fail.
312 fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr);
313
314 // Check that the UpdateCanStart fails.
315 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
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, UpdateCanStartNotAllowedCheckDue) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700322 // The UpdateCanStart policy returns false because we are due for another
323 // update check.
324
325 SetUpdateCheckAllowed(true);
326
327 // Check that the UpdateCanStart returns false.
328 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700329 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700330 ExpectPolicyStatus(EvalStatus::kSucceeded,
331 &Policy::UpdateCanStart, &result, false, update_state);
332 EXPECT_FALSE(result.update_can_start);
333 EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason);
334}
335
Alex Deymo63784a52014-05-28 10:46:14 -0700336TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700337 // The UpdateCanStart policy returns true; no device policy is loaded.
338
339 SetUpdateCheckAllowed(false);
340 fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset(
341 new bool(false));
342
343 // Check that the UpdateCanStart returns true with no further attributes.
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_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700349 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700350 EXPECT_EQ(0, result.download_url_idx);
351 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700352}
353
Alex Deymo63784a52014-05-28 10:46:14 -0700354TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700355 // The UpdateCanStart policy returns true; device policy is loaded but imposes
356 // no restrictions on updating.
357
358 SetUpdateCheckAllowed(false);
359
360 // Check that the UpdateCanStart returns true.
361 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700362 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700363 ExpectPolicyStatus(EvalStatus::kSucceeded,
364 &Policy::UpdateCanStart, &result, false, update_state);
365 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700366 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700367 EXPECT_EQ(0, result.download_url_idx);
368 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700369}
370
Alex Deymo63784a52014-05-28 10:46:14 -0700371TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700372 // The UpdateCanStart policy fails because the UpdateScattering policy it
373 // depends on fails (unset variable).
374
375 SetUpdateCheckAllowed(false);
376
377 // Override the default seed variable with a null value so that the policy
378 // request would fail.
379 fake_state_.random_provider()->var_seed()->reset(nullptr);
380
381 // Check that the UpdateCanStart fails.
382 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700383 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700384 ExpectPolicyStatus(EvalStatus::kFailed,
385 &Policy::UpdateCanStart, &result, false, update_state);
386}
387
Alex Deymo63784a52014-05-28 10:46:14 -0700388TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700389 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) {
390 // The UpdateCanStart policy returns false; device policy is loaded and
391 // scattering applies due to an unsatisfied wait period, which was newly
392 // generated.
393
394 SetUpdateCheckAllowed(false);
395 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
396 new TimeDelta(TimeDelta::FromMinutes(2)));
397
398
399 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
400
401 // Check that the UpdateCanStart returns false and a new wait period
402 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700403 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700404 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
405 false, update_state);
406 EXPECT_FALSE(result.update_can_start);
407 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
408 EXPECT_LT(TimeDelta(), result.scatter_wait_period);
409 EXPECT_EQ(0, result.scatter_check_threshold);
410}
411
Alex Deymo63784a52014-05-28 10:46:14 -0700412TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700413 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) {
414 // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy
415 // is loaded and a previously generated scattering period still applies, none
416 // of the scattering values has changed.
417
418 SetUpdateCheckAllowed(false);
419 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
420 new TimeDelta(TimeDelta::FromMinutes(2)));
421
422 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
423 update_state.scatter_wait_period = TimeDelta::FromSeconds(35);
424
425 // Check that the UpdateCanStart returns false and a new wait period
426 // generated.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700427 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700428 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart,
429 &result, false, update_state);
430 EXPECT_FALSE(result.update_can_start);
431 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
432 EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period);
433 EXPECT_EQ(0, result.scatter_check_threshold);
434}
435
Alex Deymo63784a52014-05-28 10:46:14 -0700436TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700437 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) {
438 // The UpdateCanStart policy returns false; device policy is loaded and
439 // scattering applies due to an unsatisfied update check count threshold.
440 //
441 // This ensures a non-zero check threshold, which may or may not be combined
442 // with a non-zero wait period (for which we cannot reliably control).
443
444 SetUpdateCheckAllowed(false);
445 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
446 new TimeDelta(TimeDelta::FromSeconds(1)));
447
448 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
449 update_state.scatter_check_threshold_min = 2;
450 update_state.scatter_check_threshold_max = 5;
451
452 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700453 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700454 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
455 false, update_state);
456 EXPECT_FALSE(result.update_can_start);
457 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
458 EXPECT_LE(2, result.scatter_check_threshold);
459 EXPECT_GE(5, result.scatter_check_threshold);
460}
461
Alex Deymo63784a52014-05-28 10:46:14 -0700462TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700463 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) {
464 // The UpdateCanStart policy returns false; device policy is loaded and
465 // scattering due to a previously generated count threshold still applies.
466
467 SetUpdateCheckAllowed(false);
468 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
469 new TimeDelta(TimeDelta::FromSeconds(1)));
470
471 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
472 update_state.scatter_check_threshold = 3;
473 update_state.scatter_check_threshold_min = 2;
474 update_state.scatter_check_threshold_max = 5;
475
476 // Check that the UpdateCanStart returns false.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700477 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700478 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
479 false, update_state);
480 EXPECT_FALSE(result.update_can_start);
481 EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason);
482 EXPECT_EQ(3, result.scatter_check_threshold);
483}
484
Alex Deymo63784a52014-05-28 10:46:14 -0700485TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700486 // The UpdateCanStart policy returns true; device policy is loaded and
487 // scattering is enabled, but both wait period and check threshold are
488 // satisfied.
489
490 SetUpdateCheckAllowed(false);
491 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
492 new TimeDelta(TimeDelta::FromSeconds(120)));
493
494 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75));
495 update_state.num_checks = 4;
496 update_state.scatter_wait_period = TimeDelta::FromSeconds(60);
497 update_state.scatter_check_threshold = 3;
498 update_state.scatter_check_threshold_min = 2;
499 update_state.scatter_check_threshold_max = 5;
500
501 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700502 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700503 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
504 false, update_state);
505 EXPECT_TRUE(result.update_can_start);
506 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
507 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700508 EXPECT_EQ(0, result.download_url_idx);
509 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700510}
511
Alex Deymo63784a52014-05-28 10:46:14 -0700512TEST_F(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700513 UpdateCanStartAllowedInteractivePreventsScattering) {
514 // The UpdateCanStart policy returns true; device policy is loaded and
515 // scattering would have applied, except that the update check is interactive
516 // and so it is suppressed.
517
518 SetUpdateCheckAllowed(false);
519 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
520 new TimeDelta(TimeDelta::FromSeconds(1)));
521
522 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
523 update_state.scatter_check_threshold = 0;
524 update_state.scatter_check_threshold_min = 2;
525 update_state.scatter_check_threshold_max = 5;
526
527 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700528 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700529 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
530 true, update_state);
531 EXPECT_TRUE(result.update_can_start);
532 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
533 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700534 EXPECT_EQ(0, result.download_url_idx);
535 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700536}
537
Alex Deymo63784a52014-05-28 10:46:14 -0700538TEST_F(UmChromeOSPolicyTest,
Gilad Arnold76a11f62014-05-20 09:02:12 -0700539 UpdateCanStartAllowedOobePreventsScattering) {
540 // The UpdateCanStart policy returns true; device policy is loaded and
541 // scattering would have applied, except that OOBE was not completed and so it
542 // is suppressed.
543
544 SetUpdateCheckAllowed(false);
545 fake_state_.device_policy_provider()->var_scatter_factor()->reset(
546 new TimeDelta(TimeDelta::FromSeconds(1)));
547 fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false));
548
549 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1));
550 update_state.scatter_check_threshold = 0;
551 update_state.scatter_check_threshold_min = 2;
552 update_state.scatter_check_threshold_max = 5;
553
554 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700555 UpdateDownloadParams result;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700556 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
557 true, update_state);
558 EXPECT_TRUE(result.update_can_start);
559 EXPECT_EQ(TimeDelta(), result.scatter_wait_period);
560 EXPECT_EQ(0, result.scatter_check_threshold);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700561 EXPECT_EQ(0, result.download_url_idx);
562 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700563}
564
Alex Deymo63784a52014-05-28 10:46:14 -0700565TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700566 // The UpdateCanStart policy returns true; device policy permits both HTTP and
567 // P2P updates, as well as a non-empty target channel string.
568
569 SetUpdateCheckAllowed(false);
570
571 // Override specific device policy attributes.
572 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
573 new bool(true));
574 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
575 new bool(true));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700576
577 // Check that the UpdateCanStart returns true.
578 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700579 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700580 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
581 false, update_state);
582 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700583 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700584 EXPECT_EQ(0, result.download_url_idx);
585 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700586}
587
Alex Deymo63784a52014-05-28 10:46:14 -0700588TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700589 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
590 // P2P updates, but the updater is configured to allow P2P and overrules the
591 // setting.
592
593 SetUpdateCheckAllowed(false);
594
595 // Override specific device policy attributes.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700596 fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true));
597
598 // Check that the UpdateCanStart returns true.
599 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700600 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700601 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
602 false, update_state);
603 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700604 EXPECT_TRUE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700605 EXPECT_EQ(0, result.download_url_idx);
606 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700607}
608
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700609TEST_F(UmChromeOSPolicyTest,
610 UpdateCanStartAllowedWithHttpUrlForUnofficialBuild) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700611 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
612 // P2P updates, but marking this an unofficial build overrules the HTTP
613 // setting.
614
615 SetUpdateCheckAllowed(false);
616
617 // Override specific device policy attributes.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700618 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
619 new bool(false));
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700620 fake_state_.system_provider()->var_is_official_build()->
621 reset(new bool(false));
622
623 // Check that the UpdateCanStart returns true.
624 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700625 UpdateDownloadParams result;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700626 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
627 false, update_state);
628 EXPECT_TRUE(result.update_can_start);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700629 EXPECT_FALSE(result.p2p_allowed);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700630 EXPECT_EQ(0, result.download_url_idx);
631 EXPECT_EQ(0, result.download_url_num_failures);
632}
633
634TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpsUrl) {
635 // The UpdateCanStart policy returns true; device policy forbids both HTTP and
636 // P2P updates, but an HTTPS URL is provided and selected for download.
637
638 SetUpdateCheckAllowed(false);
639
640 // Override specific device policy attributes.
641 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
642 new bool(false));
643
644 // Add an HTTPS URL.
645 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
646 update_state.download_urls.push_back("https://secure/url/");
647
648 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700649 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700650 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
651 false, update_state);
652 EXPECT_TRUE(result.update_can_start);
653 EXPECT_FALSE(result.p2p_allowed);
654 EXPECT_EQ(1, result.download_url_idx);
655 EXPECT_EQ(0, result.download_url_num_failures);
656}
657
658TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlMaxExceeded) {
659 // The UpdateCanStart policy returns true; the first URL exceeded the maximum
660 // allowed number of failures, but a second URL is available.
661
662 SetUpdateCheckAllowed(false);
663
664 // Add a second URL; update with this URL attempted and failed enough times to
665 // disqualify the current (first) URL. This tests both the previously
666 // accounted failures (download_url_num_failures) as well as those occurring
667 // since the last call (download_url_error_codes).
668 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
669 update_state.num_checks = 10;
670 update_state.download_urls.push_back("http://another/fake/url/");
671 update_state.download_url_num_failures = 9;
672 update_state.download_url_error_codes.push_back(
673 ErrorCode::kDownloadTransferError);
674 update_state.download_url_error_codes.push_back(
675 ErrorCode::kDownloadWriteError);
676
677 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700678 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700679 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
680 false, update_state);
681 EXPECT_TRUE(result.update_can_start);
682 EXPECT_FALSE(result.p2p_allowed);
683 EXPECT_EQ(1, result.download_url_idx);
684 EXPECT_EQ(0, result.download_url_num_failures);
685}
686
687TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithSecondUrlHardError) {
688 // The UpdateCanStart policy returns true; the first URL fails with a hard
689 // error, but a second URL is available.
690
691 SetUpdateCheckAllowed(false);
692
693 // Add a second URL; update with this URL attempted and failed in a way that
694 // causes it to switch directly to the next URL.
695 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
696 update_state.num_checks = 10;
697 update_state.download_urls.push_back("http://another/fake/url/");
698 update_state.download_url_error_codes.push_back(
699 ErrorCode::kPayloadHashMismatchError);
700
701 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700702 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700703 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
704 false, update_state);
705 EXPECT_TRUE(result.update_can_start);
706 EXPECT_FALSE(result.p2p_allowed);
707 EXPECT_EQ(1, result.download_url_idx);
708 EXPECT_EQ(0, result.download_url_num_failures);
709}
710
711TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedUrlWrapsAround) {
712 // The UpdateCanStart policy returns true; URL search properly wraps around
713 // the last one on the list.
714
715 SetUpdateCheckAllowed(false);
716
717 // Add a second URL; update with this URL attempted and failed in a way that
718 // causes it to switch directly to the next URL.
719 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
720 update_state.num_checks = 10;
721 update_state.download_urls.push_back("http://another/fake/url/");
722 update_state.download_url_idx = 1;
723 update_state.download_url_error_codes.push_back(
724 ErrorCode::kPayloadHashMismatchError);
725
726 // Check that the UpdateCanStart returns true.
Gilad Arnold42f253b2014-06-25 12:39:17 -0700727 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700728 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
729 false, update_state);
730 EXPECT_TRUE(result.update_can_start);
731 EXPECT_FALSE(result.p2p_allowed);
732 EXPECT_EQ(0, result.download_url_idx);
733 EXPECT_EQ(0, result.download_url_num_failures);
734}
735
736TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedNoUsableUrls) {
737 // The UpdateCanStart policy returns false; there's a single HTTP URL but its
738 // use is forbidden by policy.
739
740 SetUpdateCheckAllowed(false);
741
742 // Override specific device policy attributes.
743 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
744 new bool(false));
745
746 // Check that the UpdateCanStart returns false.
747 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700748 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700749 ExpectPolicyStatus(EvalStatus::kFailed, &Policy::UpdateCanStart, &result,
750 false, update_state);
751}
752
753TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoUsableUrlsButP2PEnabled) {
754 // The UpdateCanStart policy returns true; there's a single HTTP URL but its
755 // use is forbidden by policy, however P2P is enabled. The result indicates
756 // that no URL can be used.
757
758 SetUpdateCheckAllowed(false);
759
760 // Override specific device policy attributes.
761 fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset(
762 new bool(true));
763 fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset(
764 new bool(false));
765
766 // Check that the UpdateCanStart returns true.
767 UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10));
Gilad Arnold42f253b2014-06-25 12:39:17 -0700768 UpdateDownloadParams result;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700769 ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result,
770 false, update_state);
771 EXPECT_TRUE(result.update_can_start);
772 EXPECT_TRUE(result.p2p_allowed);
773 EXPECT_GT(0, result.download_url_idx);
774 EXPECT_EQ(0, result.download_url_num_failures);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700775}
776
Gilad Arnold684219d2014-07-07 14:54:57 -0700777TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedEthernetDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700778 // Ethernet is always allowed.
779
780 fake_state_.shill_provider()->var_conn_type()->
781 reset(new ConnectionType(ConnectionType::kEthernet));
782
783 bool result;
784 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700785 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700786 EXPECT_TRUE(result);
787}
788
Gilad Arnold684219d2014-07-07 14:54:57 -0700789TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWifiDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700790 // Wifi is allowed if not tethered.
791
792 fake_state_.shill_provider()->var_conn_type()->
793 reset(new ConnectionType(ConnectionType::kWifi));
794
795 bool result;
796 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700797 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700798 EXPECT_TRUE(result);
799}
800
Alex Deymo63784a52014-05-28 10:46:14 -0700801TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700802 UpdateCurrentConnectionNotAllowedWifiTetheredDefault) {
803 // Tethered wifi is not allowed by default.
804
805 fake_state_.shill_provider()->var_conn_type()->
806 reset(new ConnectionType(ConnectionType::kWifi));
807 fake_state_.shill_provider()->var_conn_tethering()->
808 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
809
810 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700811 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700812 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700813}
814
Alex Deymo63784a52014-05-28 10:46:14 -0700815TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700816 UpdateDownloadAllowedWifiTetheredPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700817 // Tethered wifi can be allowed by policy.
818
819 fake_state_.shill_provider()->var_conn_type()->
820 reset(new ConnectionType(ConnectionType::kWifi));
821 fake_state_.shill_provider()->var_conn_tethering()->
822 reset(new ConnectionTethering(ConnectionTethering::kConfirmed));
823 set<ConnectionType> allowed_connections;
824 allowed_connections.insert(ConnectionType::kCellular);
825 fake_state_.device_policy_provider()->
826 var_allowed_connection_types_for_update()->
827 reset(new set<ConnectionType>(allowed_connections));
828
829 bool result;
830 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700831 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700832 EXPECT_TRUE(result);
833}
834
Gilad Arnold684219d2014-07-07 14:54:57 -0700835TEST_F(UmChromeOSPolicyTest, UpdateDownloadAllowedWimaxDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700836 // Wimax is always allowed.
837
838 fake_state_.shill_provider()->var_conn_type()->
839 reset(new ConnectionType(ConnectionType::kWifi));
840
841 bool result;
842 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700843 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700844 EXPECT_TRUE(result);
845}
846
Alex Deymo63784a52014-05-28 10:46:14 -0700847TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700848 UpdateCurrentConnectionNotAllowedBluetoothDefault) {
849 // Bluetooth is never allowed.
850
851 fake_state_.shill_provider()->var_conn_type()->
852 reset(new ConnectionType(ConnectionType::kBluetooth));
853
854 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700855 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700856 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700857}
858
Alex Deymo63784a52014-05-28 10:46:14 -0700859TEST_F(UmChromeOSPolicyTest,
Gilad Arnold0adbc942014-05-12 10:35:43 -0700860 UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) {
861 // Bluetooth cannot be allowed even by policy.
862
863 fake_state_.shill_provider()->var_conn_type()->
864 reset(new ConnectionType(ConnectionType::kBluetooth));
865 set<ConnectionType> allowed_connections;
866 allowed_connections.insert(ConnectionType::kBluetooth);
867 fake_state_.device_policy_provider()->
868 var_allowed_connection_types_for_update()->
869 reset(new set<ConnectionType>(allowed_connections));
870
871 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700872 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700873 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700874}
875
Alex Deymo63784a52014-05-28 10:46:14 -0700876TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700877 // Cellular is not allowed by default.
878
879 fake_state_.shill_provider()->var_conn_type()->
880 reset(new ConnectionType(ConnectionType::kCellular));
881
882 bool result;
Gilad Arnold28d6be62014-06-30 14:04:04 -0700883 ExpectPolicyStatus(EvalStatus::kAskMeAgainLater,
Gilad Arnold684219d2014-07-07 14:54:57 -0700884 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700885}
886
Alex Deymo63784a52014-05-28 10:46:14 -0700887TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700888 UpdateDownloadAllowedCellularPolicyOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700889 // Update over cellular can be enabled by policy.
890
891 fake_state_.shill_provider()->var_conn_type()->
892 reset(new ConnectionType(ConnectionType::kCellular));
893 set<ConnectionType> allowed_connections;
894 allowed_connections.insert(ConnectionType::kCellular);
895 fake_state_.device_policy_provider()->
896 var_allowed_connection_types_for_update()->
897 reset(new set<ConnectionType>(allowed_connections));
898
899 bool result;
900 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700901 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700902 EXPECT_TRUE(result);
903}
904
Alex Deymo63784a52014-05-28 10:46:14 -0700905TEST_F(UmChromeOSPolicyTest,
Gilad Arnold684219d2014-07-07 14:54:57 -0700906 UpdateDownloadAllowedCellularUserOverride) {
Gilad Arnold0adbc942014-05-12 10:35:43 -0700907 // Update over cellular can be enabled by user settings, but only if policy
908 // is present and does not determine allowed connections.
909
910 fake_state_.shill_provider()->var_conn_type()->
911 reset(new ConnectionType(ConnectionType::kCellular));
912 set<ConnectionType> allowed_connections;
913 allowed_connections.insert(ConnectionType::kCellular);
914 fake_state_.updater_provider()->var_cellular_enabled()->
915 reset(new bool(true));
916
917 bool result;
918 ExpectPolicyStatus(EvalStatus::kSucceeded,
Gilad Arnold684219d2014-07-07 14:54:57 -0700919 &Policy::UpdateDownloadAllowed, &result);
Gilad Arnold0adbc942014-05-12 10:35:43 -0700920 EXPECT_TRUE(result);
921}
922
Alex Deymo63784a52014-05-28 10:46:14 -0700923} // namespace chromeos_update_manager