Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 1 | // 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 Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 5 | #include "update_engine/update_manager/chromeos_policy.h" |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 6 | |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 7 | #include <set> |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include <base/time/time.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | #include "update_engine/fake_clock.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 14 | #include "update_engine/update_manager/evaluation_context.h" |
| 15 | #include "update_engine/update_manager/fake_state.h" |
| 16 | #include "update_engine/update_manager/umtest_utils.h" |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 17 | |
| 18 | using base::Time; |
| 19 | using base::TimeDelta; |
| 20 | using chromeos_update_engine::FakeClock; |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 21 | using std::set; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 22 | using std::string; |
| 23 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 24 | namespace chromeos_update_manager { |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 25 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 26 | class UmChromeOSPolicyTest : public ::testing::Test { |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 27 | protected: |
| 28 | virtual void SetUp() { |
| 29 | SetUpDefaultClock(); |
| 30 | eval_ctx_ = new EvaluationContext(&fake_clock_); |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 31 | SetUpDefaultState(); |
| 32 | SetUpDefaultDevicePolicy(); |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Sets the clock to fixed values. |
| 36 | void SetUpDefaultClock() { |
| 37 | fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L)); |
| 38 | fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L)); |
| 39 | } |
| 40 | |
| 41 | void SetUpDefaultState() { |
| 42 | fake_state_.updater_provider()->var_updater_started_time()->reset( |
| 43 | new Time(fake_clock_.GetWallclockTime())); |
| 44 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
| 45 | new Time(fake_clock_.GetWallclockTime())); |
| 46 | fake_state_.updater_provider()->var_consecutive_failed_update_checks()-> |
| 47 | reset(new unsigned int(0)); |
| 48 | |
| 49 | fake_state_.random_provider()->var_seed()->reset( |
| 50 | new uint64_t(4)); // chosen by fair dice roll. |
| 51 | // guaranteed to be random. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 52 | |
| 53 | // No device policy loaded by default. |
| 54 | fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset( |
| 55 | new bool(false)); |
| 56 | |
Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 57 | // For the purpose of the tests, this is an official build and OOBE was |
| 58 | // completed. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 59 | fake_state_.system_provider()->var_is_official_build()->reset( |
| 60 | new bool(true)); |
Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 61 | fake_state_.system_provider()->var_is_oobe_complete()->reset( |
| 62 | new bool(true)); |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 63 | |
| 64 | // Connection is wifi, untethered. |
| 65 | fake_state_.shill_provider()->var_conn_type()-> |
| 66 | reset(new ConnectionType(ConnectionType::kWifi)); |
| 67 | fake_state_.shill_provider()->var_conn_tethering()-> |
| 68 | reset(new ConnectionTethering(ConnectionTethering::kNotDetected)); |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Sets up a default device policy that does not impose any restrictions, nor |
| 72 | // enables any features (HTTP, P2P). |
| 73 | void SetUpDefaultDevicePolicy() { |
| 74 | fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset( |
| 75 | new bool(true)); |
| 76 | fake_state_.device_policy_provider()->var_update_disabled()->reset( |
| 77 | new bool(false)); |
| 78 | fake_state_.device_policy_provider()-> |
| 79 | var_allowed_connection_types_for_update()->reset(nullptr); |
| 80 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 81 | new TimeDelta()); |
| 82 | fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset( |
| 83 | new bool(false)); |
| 84 | fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset( |
| 85 | new bool(false)); |
| 86 | fake_state_.device_policy_provider()->var_release_channel_delegated()-> |
| 87 | reset(new bool(true)); |
| 88 | } |
| 89 | |
| 90 | // Configures the UpdateCheckAllowed policy to return a desired value by |
| 91 | // faking the current wall clock time as needed. Restores the default state. |
| 92 | // This is used when testing policies that depend on this one. |
| 93 | void SetUpdateCheckAllowed(bool allow_check) { |
| 94 | Time next_update_check; |
| 95 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 96 | &ChromeOSPolicy::NextUpdateCheckTime, |
| 97 | &next_update_check); |
| 98 | SetUpDefaultState(); |
| 99 | SetUpDefaultDevicePolicy(); |
| 100 | Time curr_time = next_update_check; |
| 101 | if (allow_check) |
| 102 | curr_time += TimeDelta::FromSeconds(1); |
| 103 | else |
| 104 | curr_time -= TimeDelta::FromSeconds(1); |
| 105 | fake_clock_.SetWallclockTime(curr_time); |
| 106 | } |
| 107 | |
| 108 | // Returns a default UpdateState structure: first seen time is calculated |
| 109 | // backward from the current wall clock time, update was seen just once, there |
| 110 | // is no scattering wait period and the max allowed is 7 days, there is no |
| 111 | // check threshold and none is allowed. |
| 112 | UpdateState GetDefaultUpdateState(TimeDelta update_first_seen_period) { |
| 113 | UpdateState update_state = { |
| 114 | fake_clock_.GetWallclockTime() - update_first_seen_period, 1, |
| 115 | TimeDelta(), TimeDelta::FromDays(7), 0, 0, 0 |
| 116 | }; |
| 117 | return update_state; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Runs the passed |policy_method| policy and expects it to return the |
| 121 | // |expected| return value. |
| 122 | template<typename T, typename R, typename... Args> |
| 123 | void ExpectPolicyStatus( |
| 124 | EvalStatus expected, |
| 125 | T policy_method, |
| 126 | R* result, Args... args) { |
| 127 | string error = "<None>"; |
| 128 | eval_ctx_->ResetEvaluation(); |
| 129 | EXPECT_EQ(expected, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 130 | (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result, |
| 131 | args...)) |
Alex Deymo | c850b4f | 2014-05-19 11:06:41 -0700 | [diff] [blame] | 132 | << "Returned error: " << error |
| 133 | << "\nEvaluation context: " << eval_ctx_->DumpContext(); |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | FakeClock fake_clock_; |
| 137 | FakeState fake_state_; |
| 138 | scoped_refptr<EvaluationContext> eval_ctx_; |
| 139 | ChromeOSPolicy policy_; // ChromeOSPolicy under test. |
| 140 | }; |
| 141 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 142 | TEST_F(UmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) { |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 143 | Time next_update_check; |
| 144 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 145 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 146 | &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check); |
| 147 | |
| 148 | EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check); |
| 149 | EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds( |
| 150 | ChromeOSPolicy::kTimeoutInitialInterval + |
| 151 | ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check); |
| 152 | } |
| 153 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 154 | TEST_F(UmChromeOSPolicyTest, ExponentialBackoffIsCapped) { |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 155 | Time next_update_check; |
| 156 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 157 | fake_state_.updater_provider()->var_consecutive_failed_update_checks()-> |
| 158 | reset(new unsigned int(100)); |
| 159 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 160 | &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check); |
| 161 | |
| 162 | EXPECT_LE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds( |
| 163 | ChromeOSPolicy::kTimeoutMaxBackoffInterval - |
| 164 | ChromeOSPolicy::kTimeoutRegularFuzz - 1), next_update_check); |
| 165 | EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds( |
| 166 | ChromeOSPolicy::kTimeoutMaxBackoffInterval + |
| 167 | ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check); |
| 168 | } |
| 169 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 170 | TEST_F(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) { |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 171 | // We get the next update_check timestamp from the policy's private method |
| 172 | // and then we check the public method respects that value on the normal |
| 173 | // case. |
| 174 | Time next_update_check; |
| 175 | Time last_checked_time = |
| 176 | fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234); |
| 177 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 178 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
| 179 | new Time(last_checked_time)); |
| 180 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 181 | &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check); |
| 182 | |
| 183 | UpdateCheckParams result; |
| 184 | |
| 185 | // Check that the policy blocks until the next_update_check is reached. |
| 186 | SetUpDefaultClock(); |
| 187 | SetUpDefaultState(); |
| 188 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
| 189 | new Time(last_checked_time)); |
| 190 | fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1)); |
| 191 | ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, |
| 192 | &Policy::UpdateCheckAllowed, &result); |
| 193 | |
| 194 | SetUpDefaultClock(); |
| 195 | SetUpDefaultState(); |
| 196 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
| 197 | new Time(last_checked_time)); |
| 198 | fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1)); |
| 199 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 200 | &Policy::UpdateCheckAllowed, &result); |
| 201 | } |
| 202 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 203 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 204 | // The UpdateCanStart policy fails, not being able to query |
| 205 | // UpdateCheckAllowed. |
| 206 | |
| 207 | // Configure the UpdateCheckAllowed policy to fail. |
| 208 | fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr); |
| 209 | |
| 210 | // Check that the UpdateCanStart fails. |
| 211 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 212 | UpdateCanStartResult result; |
| 213 | ExpectPolicyStatus(EvalStatus::kFailed, |
| 214 | &Policy::UpdateCanStart, &result, false, update_state); |
| 215 | } |
| 216 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 217 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 218 | // The UpdateCanStart policy returns false because we are due for another |
| 219 | // update check. |
| 220 | |
| 221 | SetUpdateCheckAllowed(true); |
| 222 | |
| 223 | // Check that the UpdateCanStart returns false. |
| 224 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 225 | UpdateCanStartResult result; |
| 226 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 227 | &Policy::UpdateCanStart, &result, false, update_state); |
| 228 | EXPECT_FALSE(result.update_can_start); |
| 229 | EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason); |
| 230 | } |
| 231 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 232 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 233 | // The UpdateCanStart policy returns true; no device policy is loaded. |
| 234 | |
| 235 | SetUpdateCheckAllowed(false); |
| 236 | fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset( |
| 237 | new bool(false)); |
| 238 | |
| 239 | // Check that the UpdateCanStart returns true with no further attributes. |
| 240 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 241 | UpdateCanStartResult result; |
| 242 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 243 | &Policy::UpdateCanStart, &result, false, update_state); |
| 244 | EXPECT_TRUE(result.update_can_start); |
| 245 | EXPECT_TRUE(result.http_allowed); |
| 246 | EXPECT_FALSE(result.p2p_allowed); |
| 247 | EXPECT_TRUE(result.target_channel.empty()); |
| 248 | } |
| 249 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 250 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 251 | // The UpdateCanStart policy returns true; device policy is loaded but imposes |
| 252 | // no restrictions on updating. |
| 253 | |
| 254 | SetUpdateCheckAllowed(false); |
| 255 | |
| 256 | // Check that the UpdateCanStart returns true. |
| 257 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 258 | UpdateCanStartResult result; |
| 259 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 260 | &Policy::UpdateCanStart, &result, false, update_state); |
| 261 | EXPECT_TRUE(result.update_can_start); |
| 262 | EXPECT_FALSE(result.http_allowed); |
| 263 | EXPECT_FALSE(result.p2p_allowed); |
| 264 | EXPECT_TRUE(result.target_channel.empty()); |
| 265 | } |
| 266 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 267 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartNotAllowedUpdatesDisabled) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 268 | // The UpdateCanStart should return false (kAskMeAgainlater) because a device |
| 269 | // policy is loaded and prohibits updates. |
| 270 | |
| 271 | SetUpdateCheckAllowed(false); |
| 272 | fake_state_.device_policy_provider()->var_update_disabled()->reset( |
| 273 | new bool(true)); |
| 274 | |
| 275 | // Check that the UpdateCanStart returns false. |
| 276 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 277 | UpdateCanStartResult result; |
| 278 | ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, |
| 279 | &Policy::UpdateCanStart, &result, false, update_state); |
| 280 | EXPECT_FALSE(result.update_can_start); |
| 281 | EXPECT_EQ(UpdateCannotStartReason::kDisabledByPolicy, |
| 282 | result.cannot_start_reason); |
| 283 | } |
| 284 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 285 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 286 | // The UpdateCanStart policy fails because the UpdateScattering policy it |
| 287 | // depends on fails (unset variable). |
| 288 | |
| 289 | SetUpdateCheckAllowed(false); |
| 290 | |
| 291 | // Override the default seed variable with a null value so that the policy |
| 292 | // request would fail. |
| 293 | fake_state_.random_provider()->var_seed()->reset(nullptr); |
| 294 | |
| 295 | // Check that the UpdateCanStart fails. |
| 296 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 297 | UpdateCanStartResult result; |
| 298 | ExpectPolicyStatus(EvalStatus::kFailed, |
| 299 | &Policy::UpdateCanStart, &result, false, update_state); |
| 300 | } |
| 301 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 302 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 303 | UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) { |
| 304 | // The UpdateCanStart policy returns false; device policy is loaded and |
| 305 | // scattering applies due to an unsatisfied wait period, which was newly |
| 306 | // generated. |
| 307 | |
| 308 | SetUpdateCheckAllowed(false); |
| 309 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 310 | new TimeDelta(TimeDelta::FromMinutes(2))); |
| 311 | |
| 312 | |
| 313 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 314 | |
| 315 | // Check that the UpdateCanStart returns false and a new wait period |
| 316 | // generated. |
| 317 | UpdateCanStartResult result; |
| 318 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 319 | false, update_state); |
| 320 | EXPECT_FALSE(result.update_can_start); |
| 321 | EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason); |
| 322 | EXPECT_LT(TimeDelta(), result.scatter_wait_period); |
| 323 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 324 | } |
| 325 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 326 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 327 | UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) { |
| 328 | // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy |
| 329 | // is loaded and a previously generated scattering period still applies, none |
| 330 | // of the scattering values has changed. |
| 331 | |
| 332 | SetUpdateCheckAllowed(false); |
| 333 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 334 | new TimeDelta(TimeDelta::FromMinutes(2))); |
| 335 | |
| 336 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 337 | update_state.scatter_wait_period = TimeDelta::FromSeconds(35); |
| 338 | |
| 339 | // Check that the UpdateCanStart returns false and a new wait period |
| 340 | // generated. |
| 341 | UpdateCanStartResult result; |
| 342 | ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart, |
| 343 | &result, false, update_state); |
| 344 | EXPECT_FALSE(result.update_can_start); |
| 345 | EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason); |
| 346 | EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period); |
| 347 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 348 | } |
| 349 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 350 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 351 | UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) { |
| 352 | // The UpdateCanStart policy returns false; device policy is loaded and |
| 353 | // scattering applies due to an unsatisfied update check count threshold. |
| 354 | // |
| 355 | // This ensures a non-zero check threshold, which may or may not be combined |
| 356 | // with a non-zero wait period (for which we cannot reliably control). |
| 357 | |
| 358 | SetUpdateCheckAllowed(false); |
| 359 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 360 | new TimeDelta(TimeDelta::FromSeconds(1))); |
| 361 | |
| 362 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 363 | update_state.scatter_check_threshold_min = 2; |
| 364 | update_state.scatter_check_threshold_max = 5; |
| 365 | |
| 366 | // Check that the UpdateCanStart returns false. |
| 367 | UpdateCanStartResult result; |
| 368 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 369 | false, update_state); |
| 370 | EXPECT_FALSE(result.update_can_start); |
| 371 | EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason); |
| 372 | EXPECT_LE(2, result.scatter_check_threshold); |
| 373 | EXPECT_GE(5, result.scatter_check_threshold); |
| 374 | } |
| 375 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 376 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 377 | UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) { |
| 378 | // The UpdateCanStart policy returns false; device policy is loaded and |
| 379 | // scattering due to a previously generated count threshold still applies. |
| 380 | |
| 381 | SetUpdateCheckAllowed(false); |
| 382 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 383 | new TimeDelta(TimeDelta::FromSeconds(1))); |
| 384 | |
| 385 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 386 | update_state.scatter_check_threshold = 3; |
| 387 | update_state.scatter_check_threshold_min = 2; |
| 388 | update_state.scatter_check_threshold_max = 5; |
| 389 | |
| 390 | // Check that the UpdateCanStart returns false. |
| 391 | UpdateCanStartResult result; |
| 392 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 393 | false, update_state); |
| 394 | EXPECT_FALSE(result.update_can_start); |
| 395 | EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason); |
| 396 | EXPECT_EQ(3, result.scatter_check_threshold); |
| 397 | } |
| 398 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 399 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 400 | // The UpdateCanStart policy returns true; device policy is loaded and |
| 401 | // scattering is enabled, but both wait period and check threshold are |
| 402 | // satisfied. |
| 403 | |
| 404 | SetUpdateCheckAllowed(false); |
| 405 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 406 | new TimeDelta(TimeDelta::FromSeconds(120))); |
| 407 | |
| 408 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75)); |
| 409 | update_state.num_checks = 4; |
| 410 | update_state.scatter_wait_period = TimeDelta::FromSeconds(60); |
| 411 | update_state.scatter_check_threshold = 3; |
| 412 | update_state.scatter_check_threshold_min = 2; |
| 413 | update_state.scatter_check_threshold_max = 5; |
| 414 | |
| 415 | // Check that the UpdateCanStart returns true. |
| 416 | UpdateCanStartResult result; |
| 417 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 418 | false, update_state); |
| 419 | EXPECT_TRUE(result.update_can_start); |
| 420 | EXPECT_EQ(TimeDelta(), result.scatter_wait_period); |
| 421 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 422 | } |
| 423 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 424 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 425 | UpdateCanStartAllowedInteractivePreventsScattering) { |
| 426 | // The UpdateCanStart policy returns true; device policy is loaded and |
| 427 | // scattering would have applied, except that the update check is interactive |
| 428 | // and so it is suppressed. |
| 429 | |
| 430 | SetUpdateCheckAllowed(false); |
| 431 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 432 | new TimeDelta(TimeDelta::FromSeconds(1))); |
| 433 | |
| 434 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 435 | update_state.scatter_check_threshold = 0; |
| 436 | update_state.scatter_check_threshold_min = 2; |
| 437 | update_state.scatter_check_threshold_max = 5; |
| 438 | |
| 439 | // Check that the UpdateCanStart returns true. |
| 440 | UpdateCanStartResult result; |
| 441 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 442 | true, update_state); |
| 443 | EXPECT_TRUE(result.update_can_start); |
| 444 | EXPECT_EQ(TimeDelta(), result.scatter_wait_period); |
| 445 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 446 | } |
| 447 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 448 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 449 | UpdateCanStartAllowedOobePreventsScattering) { |
| 450 | // The UpdateCanStart policy returns true; device policy is loaded and |
| 451 | // scattering would have applied, except that OOBE was not completed and so it |
| 452 | // is suppressed. |
| 453 | |
| 454 | SetUpdateCheckAllowed(false); |
| 455 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 456 | new TimeDelta(TimeDelta::FromSeconds(1))); |
| 457 | fake_state_.system_provider()->var_is_oobe_complete()->reset(new bool(false)); |
| 458 | |
| 459 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 460 | update_state.scatter_check_threshold = 0; |
| 461 | update_state.scatter_check_threshold_min = 2; |
| 462 | update_state.scatter_check_threshold_max = 5; |
| 463 | |
| 464 | // Check that the UpdateCanStart returns true. |
| 465 | UpdateCanStartResult result; |
| 466 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 467 | true, update_state); |
| 468 | EXPECT_TRUE(result.update_can_start); |
| 469 | EXPECT_EQ(TimeDelta(), result.scatter_wait_period); |
| 470 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 471 | } |
| 472 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 473 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 474 | // The UpdateCanStart policy returns true; device policy permits both HTTP and |
| 475 | // P2P updates, as well as a non-empty target channel string. |
| 476 | |
| 477 | SetUpdateCheckAllowed(false); |
| 478 | |
| 479 | // Override specific device policy attributes. |
| 480 | fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset( |
| 481 | new bool(true)); |
| 482 | fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset( |
| 483 | new bool(true)); |
| 484 | fake_state_.device_policy_provider()->var_release_channel_delegated()-> |
| 485 | reset(new bool(false)); |
| 486 | fake_state_.device_policy_provider()->var_release_channel()-> |
| 487 | reset(new string("foo-channel")); |
| 488 | |
| 489 | // Check that the UpdateCanStart returns true. |
| 490 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 491 | UpdateCanStartResult result; |
| 492 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 493 | false, update_state); |
| 494 | EXPECT_TRUE(result.update_can_start); |
| 495 | EXPECT_TRUE(result.http_allowed); |
| 496 | EXPECT_TRUE(result.p2p_allowed); |
| 497 | EXPECT_EQ("foo-channel", result.target_channel); |
| 498 | } |
| 499 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 500 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 501 | // The UpdateCanStart policy returns true; device policy forbids both HTTP and |
| 502 | // P2P updates, but the updater is configured to allow P2P and overrules the |
| 503 | // setting. |
| 504 | |
| 505 | SetUpdateCheckAllowed(false); |
| 506 | |
| 507 | // Override specific device policy attributes. |
| 508 | fake_state_.device_policy_provider()->var_release_channel_delegated()-> |
| 509 | reset(new bool(false)); |
| 510 | fake_state_.device_policy_provider()->var_release_channel()-> |
| 511 | reset(new string("foo-channel")); |
| 512 | fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true)); |
| 513 | |
| 514 | // Check that the UpdateCanStart returns true. |
| 515 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 516 | UpdateCanStartResult result; |
| 517 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 518 | false, update_state); |
| 519 | EXPECT_TRUE(result.update_can_start); |
| 520 | EXPECT_FALSE(result.http_allowed); |
| 521 | EXPECT_TRUE(result.p2p_allowed); |
| 522 | EXPECT_EQ("foo-channel", result.target_channel); |
| 523 | } |
| 524 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 525 | TEST_F(UmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpForUnofficialBuild) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 526 | // The UpdateCanStart policy returns true; device policy forbids both HTTP and |
| 527 | // P2P updates, but marking this an unofficial build overrules the HTTP |
| 528 | // setting. |
| 529 | |
| 530 | SetUpdateCheckAllowed(false); |
| 531 | |
| 532 | // Override specific device policy attributes. |
| 533 | fake_state_.device_policy_provider()->var_release_channel_delegated()-> |
| 534 | reset(new bool(false)); |
| 535 | fake_state_.device_policy_provider()->var_release_channel()-> |
| 536 | reset(new string("foo-channel")); |
| 537 | fake_state_.system_provider()->var_is_official_build()-> |
| 538 | reset(new bool(false)); |
| 539 | |
| 540 | // Check that the UpdateCanStart returns true. |
| 541 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 542 | UpdateCanStartResult result; |
| 543 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 544 | false, update_state); |
| 545 | EXPECT_TRUE(result.update_can_start); |
| 546 | EXPECT_TRUE(result.http_allowed); |
| 547 | EXPECT_FALSE(result.p2p_allowed); |
| 548 | EXPECT_EQ("foo-channel", result.target_channel); |
| 549 | } |
| 550 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 551 | TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionAllowedEthernetDefault) { |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 552 | // Ethernet is always allowed. |
| 553 | |
| 554 | fake_state_.shill_provider()->var_conn_type()-> |
| 555 | reset(new ConnectionType(ConnectionType::kEthernet)); |
| 556 | |
| 557 | bool result; |
| 558 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 559 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 560 | EXPECT_TRUE(result); |
| 561 | } |
| 562 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 563 | TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionAllowedWifiDefault) { |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 564 | // Wifi is allowed if not tethered. |
| 565 | |
| 566 | fake_state_.shill_provider()->var_conn_type()-> |
| 567 | reset(new ConnectionType(ConnectionType::kWifi)); |
| 568 | |
| 569 | bool result; |
| 570 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 571 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 572 | EXPECT_TRUE(result); |
| 573 | } |
| 574 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 575 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 576 | UpdateCurrentConnectionNotAllowedWifiTetheredDefault) { |
| 577 | // Tethered wifi is not allowed by default. |
| 578 | |
| 579 | fake_state_.shill_provider()->var_conn_type()-> |
| 580 | reset(new ConnectionType(ConnectionType::kWifi)); |
| 581 | fake_state_.shill_provider()->var_conn_tethering()-> |
| 582 | reset(new ConnectionTethering(ConnectionTethering::kConfirmed)); |
| 583 | |
| 584 | bool result; |
| 585 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 586 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 587 | EXPECT_FALSE(result); |
| 588 | } |
| 589 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 590 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 591 | UpdateCurrentConnectionAllowedWifiTetheredPolicyOverride) { |
| 592 | // Tethered wifi can be allowed by policy. |
| 593 | |
| 594 | fake_state_.shill_provider()->var_conn_type()-> |
| 595 | reset(new ConnectionType(ConnectionType::kWifi)); |
| 596 | fake_state_.shill_provider()->var_conn_tethering()-> |
| 597 | reset(new ConnectionTethering(ConnectionTethering::kConfirmed)); |
| 598 | set<ConnectionType> allowed_connections; |
| 599 | allowed_connections.insert(ConnectionType::kCellular); |
| 600 | fake_state_.device_policy_provider()-> |
| 601 | var_allowed_connection_types_for_update()-> |
| 602 | reset(new set<ConnectionType>(allowed_connections)); |
| 603 | |
| 604 | bool result; |
| 605 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 606 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 607 | EXPECT_TRUE(result); |
| 608 | } |
| 609 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 610 | TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionAllowedWimaxDefault) { |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 611 | // Wimax is always allowed. |
| 612 | |
| 613 | fake_state_.shill_provider()->var_conn_type()-> |
| 614 | reset(new ConnectionType(ConnectionType::kWifi)); |
| 615 | |
| 616 | bool result; |
| 617 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 618 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 619 | EXPECT_TRUE(result); |
| 620 | } |
| 621 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 622 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 623 | UpdateCurrentConnectionNotAllowedBluetoothDefault) { |
| 624 | // Bluetooth is never allowed. |
| 625 | |
| 626 | fake_state_.shill_provider()->var_conn_type()-> |
| 627 | reset(new ConnectionType(ConnectionType::kBluetooth)); |
| 628 | |
| 629 | bool result; |
| 630 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 631 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 632 | EXPECT_FALSE(result); |
| 633 | } |
| 634 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 635 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 636 | UpdateCurrentConnectionNotAllowedBluetoothPolicyCannotOverride) { |
| 637 | // Bluetooth cannot be allowed even by policy. |
| 638 | |
| 639 | fake_state_.shill_provider()->var_conn_type()-> |
| 640 | reset(new ConnectionType(ConnectionType::kBluetooth)); |
| 641 | set<ConnectionType> allowed_connections; |
| 642 | allowed_connections.insert(ConnectionType::kBluetooth); |
| 643 | fake_state_.device_policy_provider()-> |
| 644 | var_allowed_connection_types_for_update()-> |
| 645 | reset(new set<ConnectionType>(allowed_connections)); |
| 646 | |
| 647 | bool result; |
| 648 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 649 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 650 | EXPECT_FALSE(result); |
| 651 | } |
| 652 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 653 | TEST_F(UmChromeOSPolicyTest, UpdateCurrentConnectionNotAllowedCellularDefault) { |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 654 | // Cellular is not allowed by default. |
| 655 | |
| 656 | fake_state_.shill_provider()->var_conn_type()-> |
| 657 | reset(new ConnectionType(ConnectionType::kCellular)); |
| 658 | |
| 659 | bool result; |
| 660 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 661 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 662 | EXPECT_FALSE(result); |
| 663 | } |
| 664 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 665 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 666 | UpdateCurrentConnectionAllowedCellularPolicyOverride) { |
| 667 | // Update over cellular can be enabled by policy. |
| 668 | |
| 669 | fake_state_.shill_provider()->var_conn_type()-> |
| 670 | reset(new ConnectionType(ConnectionType::kCellular)); |
| 671 | set<ConnectionType> allowed_connections; |
| 672 | allowed_connections.insert(ConnectionType::kCellular); |
| 673 | fake_state_.device_policy_provider()-> |
| 674 | var_allowed_connection_types_for_update()-> |
| 675 | reset(new set<ConnectionType>(allowed_connections)); |
| 676 | |
| 677 | bool result; |
| 678 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 679 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 680 | EXPECT_TRUE(result); |
| 681 | } |
| 682 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 683 | TEST_F(UmChromeOSPolicyTest, |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 684 | UpdateCurrentConnectionAllowedCellularUserOverride) { |
| 685 | // Update over cellular can be enabled by user settings, but only if policy |
| 686 | // is present and does not determine allowed connections. |
| 687 | |
| 688 | fake_state_.shill_provider()->var_conn_type()-> |
| 689 | reset(new ConnectionType(ConnectionType::kCellular)); |
| 690 | set<ConnectionType> allowed_connections; |
| 691 | allowed_connections.insert(ConnectionType::kCellular); |
| 692 | fake_state_.updater_provider()->var_cellular_enabled()-> |
| 693 | reset(new bool(true)); |
| 694 | |
| 695 | bool result; |
| 696 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 697 | &Policy::UpdateCurrentConnectionAllowed, &result); |
| 698 | EXPECT_TRUE(result); |
| 699 | } |
| 700 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame^] | 701 | } // namespace chromeos_update_manager |