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 | |
| 5 | #include "update_engine/policy_manager/chromeos_policy.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/time/time.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | #include "update_engine/fake_clock.h" |
| 13 | #include "update_engine/policy_manager/evaluation_context.h" |
| 14 | #include "update_engine/policy_manager/fake_state.h" |
| 15 | #include "update_engine/policy_manager/pmtest_utils.h" |
| 16 | |
| 17 | using base::Time; |
| 18 | using base::TimeDelta; |
| 19 | using chromeos_update_engine::FakeClock; |
| 20 | using std::string; |
| 21 | |
| 22 | namespace chromeos_policy_manager { |
| 23 | |
| 24 | class PmChromeOSPolicyTest : public ::testing::Test { |
| 25 | protected: |
| 26 | virtual void SetUp() { |
| 27 | SetUpDefaultClock(); |
| 28 | eval_ctx_ = new EvaluationContext(&fake_clock_); |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame^] | 29 | SetUpDefaultState(); |
| 30 | SetUpDefaultDevicePolicy(); |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // Sets the clock to fixed values. |
| 34 | void SetUpDefaultClock() { |
| 35 | fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L)); |
| 36 | fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L)); |
| 37 | } |
| 38 | |
| 39 | void SetUpDefaultState() { |
| 40 | fake_state_.updater_provider()->var_updater_started_time()->reset( |
| 41 | new Time(fake_clock_.GetWallclockTime())); |
| 42 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
| 43 | new Time(fake_clock_.GetWallclockTime())); |
| 44 | fake_state_.updater_provider()->var_consecutive_failed_update_checks()-> |
| 45 | reset(new unsigned int(0)); |
| 46 | |
| 47 | fake_state_.random_provider()->var_seed()->reset( |
| 48 | new uint64_t(4)); // chosen by fair dice roll. |
| 49 | // guaranteed to be random. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame^] | 50 | |
| 51 | // No device policy loaded by default. |
| 52 | fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset( |
| 53 | new bool(false)); |
| 54 | |
| 55 | // For the purpose of the tests, this is an official build. |
| 56 | fake_state_.system_provider()->var_is_official_build()->reset( |
| 57 | new bool(true)); |
| 58 | } |
| 59 | |
| 60 | // Sets up a default device policy that does not impose any restrictions, nor |
| 61 | // enables any features (HTTP, P2P). |
| 62 | void SetUpDefaultDevicePolicy() { |
| 63 | fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset( |
| 64 | new bool(true)); |
| 65 | fake_state_.device_policy_provider()->var_update_disabled()->reset( |
| 66 | new bool(false)); |
| 67 | fake_state_.device_policy_provider()-> |
| 68 | var_allowed_connection_types_for_update()->reset(nullptr); |
| 69 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 70 | new TimeDelta()); |
| 71 | fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset( |
| 72 | new bool(false)); |
| 73 | fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset( |
| 74 | new bool(false)); |
| 75 | fake_state_.device_policy_provider()->var_release_channel_delegated()-> |
| 76 | reset(new bool(true)); |
| 77 | } |
| 78 | |
| 79 | // Configures the UpdateCheckAllowed policy to return a desired value by |
| 80 | // faking the current wall clock time as needed. Restores the default state. |
| 81 | // This is used when testing policies that depend on this one. |
| 82 | void SetUpdateCheckAllowed(bool allow_check) { |
| 83 | Time next_update_check; |
| 84 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 85 | &ChromeOSPolicy::NextUpdateCheckTime, |
| 86 | &next_update_check); |
| 87 | SetUpDefaultState(); |
| 88 | SetUpDefaultDevicePolicy(); |
| 89 | Time curr_time = next_update_check; |
| 90 | if (allow_check) |
| 91 | curr_time += TimeDelta::FromSeconds(1); |
| 92 | else |
| 93 | curr_time -= TimeDelta::FromSeconds(1); |
| 94 | fake_clock_.SetWallclockTime(curr_time); |
| 95 | } |
| 96 | |
| 97 | // Returns a default UpdateState structure: first seen time is calculated |
| 98 | // backward from the current wall clock time, update was seen just once, there |
| 99 | // is no scattering wait period and the max allowed is 7 days, there is no |
| 100 | // check threshold and none is allowed. |
| 101 | UpdateState GetDefaultUpdateState(TimeDelta update_first_seen_period) { |
| 102 | UpdateState update_state = { |
| 103 | fake_clock_.GetWallclockTime() - update_first_seen_period, 1, |
| 104 | TimeDelta(), TimeDelta::FromDays(7), 0, 0, 0 |
| 105 | }; |
| 106 | return update_state; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Runs the passed |policy_method| policy and expects it to return the |
| 110 | // |expected| return value. |
| 111 | template<typename T, typename R, typename... Args> |
| 112 | void ExpectPolicyStatus( |
| 113 | EvalStatus expected, |
| 114 | T policy_method, |
| 115 | R* result, Args... args) { |
| 116 | string error = "<None>"; |
| 117 | eval_ctx_->ResetEvaluation(); |
| 118 | EXPECT_EQ(expected, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame^] | 119 | (policy_.*policy_method)(eval_ctx_, &fake_state_, &error, result, |
| 120 | args...)) |
Alex Deymo | c850b4f | 2014-05-19 11:06:41 -0700 | [diff] [blame] | 121 | << "Returned error: " << error |
| 122 | << "\nEvaluation context: " << eval_ctx_->DumpContext(); |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | FakeClock fake_clock_; |
| 126 | FakeState fake_state_; |
| 127 | scoped_refptr<EvaluationContext> eval_ctx_; |
| 128 | ChromeOSPolicy policy_; // ChromeOSPolicy under test. |
| 129 | }; |
| 130 | |
| 131 | TEST_F(PmChromeOSPolicyTest, FirstCheckIsAtMostInitialIntervalAfterStart) { |
| 132 | Time next_update_check; |
| 133 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 134 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 135 | &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check); |
| 136 | |
| 137 | EXPECT_LE(fake_clock_.GetWallclockTime(), next_update_check); |
| 138 | EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds( |
| 139 | ChromeOSPolicy::kTimeoutInitialInterval + |
| 140 | ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check); |
| 141 | } |
| 142 | |
| 143 | TEST_F(PmChromeOSPolicyTest, ExponentialBackoffIsCapped) { |
| 144 | Time next_update_check; |
| 145 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 146 | fake_state_.updater_provider()->var_consecutive_failed_update_checks()-> |
| 147 | reset(new unsigned int(100)); |
| 148 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 149 | &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check); |
| 150 | |
| 151 | EXPECT_LE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds( |
| 152 | ChromeOSPolicy::kTimeoutMaxBackoffInterval - |
| 153 | ChromeOSPolicy::kTimeoutRegularFuzz - 1), next_update_check); |
| 154 | EXPECT_GE(fake_clock_.GetWallclockTime() + TimeDelta::FromSeconds( |
| 155 | ChromeOSPolicy::kTimeoutMaxBackoffInterval + |
| 156 | ChromeOSPolicy::kTimeoutRegularFuzz), next_update_check); |
| 157 | } |
| 158 | |
| 159 | TEST_F(PmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) { |
| 160 | // We get the next update_check timestamp from the policy's private method |
| 161 | // and then we check the public method respects that value on the normal |
| 162 | // case. |
| 163 | Time next_update_check; |
| 164 | Time last_checked_time = |
| 165 | fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234); |
| 166 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 167 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
| 168 | new Time(last_checked_time)); |
| 169 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 170 | &ChromeOSPolicy::NextUpdateCheckTime, &next_update_check); |
| 171 | |
| 172 | UpdateCheckParams result; |
| 173 | |
| 174 | // Check that the policy blocks until the next_update_check is reached. |
| 175 | SetUpDefaultClock(); |
| 176 | SetUpDefaultState(); |
| 177 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
| 178 | new Time(last_checked_time)); |
| 179 | fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1)); |
| 180 | ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, |
| 181 | &Policy::UpdateCheckAllowed, &result); |
| 182 | |
| 183 | SetUpDefaultClock(); |
| 184 | SetUpDefaultState(); |
| 185 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
| 186 | new Time(last_checked_time)); |
| 187 | fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1)); |
| 188 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 189 | &Policy::UpdateCheckAllowed, &result); |
| 190 | } |
| 191 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame^] | 192 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartFailsCheckAllowedError) { |
| 193 | // The UpdateCanStart policy fails, not being able to query |
| 194 | // UpdateCheckAllowed. |
| 195 | |
| 196 | // Configure the UpdateCheckAllowed policy to fail. |
| 197 | fake_state_.updater_provider()->var_updater_started_time()->reset(nullptr); |
| 198 | |
| 199 | // Check that the UpdateCanStart fails. |
| 200 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 201 | UpdateCanStartResult result; |
| 202 | ExpectPolicyStatus(EvalStatus::kFailed, |
| 203 | &Policy::UpdateCanStart, &result, false, update_state); |
| 204 | } |
| 205 | |
| 206 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartNotAllowedCheckDue) { |
| 207 | // The UpdateCanStart policy returns false because we are due for another |
| 208 | // update check. |
| 209 | |
| 210 | SetUpdateCheckAllowed(true); |
| 211 | |
| 212 | // Check that the UpdateCanStart returns false. |
| 213 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 214 | UpdateCanStartResult result; |
| 215 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 216 | &Policy::UpdateCanStart, &result, false, update_state); |
| 217 | EXPECT_FALSE(result.update_can_start); |
| 218 | EXPECT_EQ(UpdateCannotStartReason::kCheckDue, result.cannot_start_reason); |
| 219 | } |
| 220 | |
| 221 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartAllowedNoDevicePolicy) { |
| 222 | // The UpdateCanStart policy returns true; no device policy is loaded. |
| 223 | |
| 224 | SetUpdateCheckAllowed(false); |
| 225 | fake_state_.device_policy_provider()->var_device_policy_is_loaded()->reset( |
| 226 | new bool(false)); |
| 227 | |
| 228 | // Check that the UpdateCanStart returns true with no further attributes. |
| 229 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 230 | UpdateCanStartResult result; |
| 231 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 232 | &Policy::UpdateCanStart, &result, false, update_state); |
| 233 | EXPECT_TRUE(result.update_can_start); |
| 234 | EXPECT_TRUE(result.http_allowed); |
| 235 | EXPECT_FALSE(result.p2p_allowed); |
| 236 | EXPECT_TRUE(result.target_channel.empty()); |
| 237 | } |
| 238 | |
| 239 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartAllowedBlankPolicy) { |
| 240 | // The UpdateCanStart policy returns true; device policy is loaded but imposes |
| 241 | // no restrictions on updating. |
| 242 | |
| 243 | SetUpdateCheckAllowed(false); |
| 244 | |
| 245 | // Check that the UpdateCanStart returns true. |
| 246 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 247 | UpdateCanStartResult result; |
| 248 | ExpectPolicyStatus(EvalStatus::kSucceeded, |
| 249 | &Policy::UpdateCanStart, &result, false, update_state); |
| 250 | EXPECT_TRUE(result.update_can_start); |
| 251 | EXPECT_FALSE(result.http_allowed); |
| 252 | EXPECT_FALSE(result.p2p_allowed); |
| 253 | EXPECT_TRUE(result.target_channel.empty()); |
| 254 | } |
| 255 | |
| 256 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartNotAllowedUpdatesDisabled) { |
| 257 | // The UpdateCanStart should return false (kAskMeAgainlater) because a device |
| 258 | // policy is loaded and prohibits updates. |
| 259 | |
| 260 | SetUpdateCheckAllowed(false); |
| 261 | fake_state_.device_policy_provider()->var_update_disabled()->reset( |
| 262 | new bool(true)); |
| 263 | |
| 264 | // Check that the UpdateCanStart returns false. |
| 265 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 266 | UpdateCanStartResult result; |
| 267 | ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, |
| 268 | &Policy::UpdateCanStart, &result, false, update_state); |
| 269 | EXPECT_FALSE(result.update_can_start); |
| 270 | EXPECT_EQ(UpdateCannotStartReason::kDisabledByPolicy, |
| 271 | result.cannot_start_reason); |
| 272 | } |
| 273 | |
| 274 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartFailsScatteringFailed) { |
| 275 | // The UpdateCanStart policy fails because the UpdateScattering policy it |
| 276 | // depends on fails (unset variable). |
| 277 | |
| 278 | SetUpdateCheckAllowed(false); |
| 279 | |
| 280 | // Override the default seed variable with a null value so that the policy |
| 281 | // request would fail. |
| 282 | fake_state_.random_provider()->var_seed()->reset(nullptr); |
| 283 | |
| 284 | // Check that the UpdateCanStart fails. |
| 285 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 286 | UpdateCanStartResult result; |
| 287 | ExpectPolicyStatus(EvalStatus::kFailed, |
| 288 | &Policy::UpdateCanStart, &result, false, update_state); |
| 289 | } |
| 290 | |
| 291 | TEST_F(PmChromeOSPolicyTest, |
| 292 | UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies) { |
| 293 | // The UpdateCanStart policy returns false; device policy is loaded and |
| 294 | // scattering applies due to an unsatisfied wait period, which was newly |
| 295 | // generated. |
| 296 | |
| 297 | SetUpdateCheckAllowed(false); |
| 298 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 299 | new TimeDelta(TimeDelta::FromMinutes(2))); |
| 300 | |
| 301 | |
| 302 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 303 | |
| 304 | // Check that the UpdateCanStart returns false and a new wait period |
| 305 | // generated. |
| 306 | UpdateCanStartResult result; |
| 307 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 308 | false, update_state); |
| 309 | EXPECT_FALSE(result.update_can_start); |
| 310 | EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason); |
| 311 | EXPECT_LT(TimeDelta(), result.scatter_wait_period); |
| 312 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 313 | } |
| 314 | |
| 315 | TEST_F(PmChromeOSPolicyTest, |
| 316 | UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies) { |
| 317 | // The UpdateCanStart policy returns false w/ kAskMeAgainLater; device policy |
| 318 | // is loaded and a previously generated scattering period still applies, none |
| 319 | // of the scattering values has changed. |
| 320 | |
| 321 | SetUpdateCheckAllowed(false); |
| 322 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 323 | new TimeDelta(TimeDelta::FromMinutes(2))); |
| 324 | |
| 325 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 326 | update_state.scatter_wait_period = TimeDelta::FromSeconds(35); |
| 327 | |
| 328 | // Check that the UpdateCanStart returns false and a new wait period |
| 329 | // generated. |
| 330 | UpdateCanStartResult result; |
| 331 | ExpectPolicyStatus(EvalStatus::kAskMeAgainLater, &Policy::UpdateCanStart, |
| 332 | &result, false, update_state); |
| 333 | EXPECT_FALSE(result.update_can_start); |
| 334 | EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason); |
| 335 | EXPECT_EQ(TimeDelta::FromSeconds(35), result.scatter_wait_period); |
| 336 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 337 | } |
| 338 | |
| 339 | TEST_F(PmChromeOSPolicyTest, |
| 340 | UpdateCanStartNotAllowedScatteringNewCountThresholdApplies) { |
| 341 | // The UpdateCanStart policy returns false; device policy is loaded and |
| 342 | // scattering applies due to an unsatisfied update check count threshold. |
| 343 | // |
| 344 | // This ensures a non-zero check threshold, which may or may not be combined |
| 345 | // with a non-zero wait period (for which we cannot reliably control). |
| 346 | |
| 347 | SetUpdateCheckAllowed(false); |
| 348 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 349 | new TimeDelta(TimeDelta::FromSeconds(1))); |
| 350 | |
| 351 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 352 | update_state.scatter_check_threshold_min = 2; |
| 353 | update_state.scatter_check_threshold_max = 5; |
| 354 | |
| 355 | // Check that the UpdateCanStart returns false. |
| 356 | UpdateCanStartResult result; |
| 357 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 358 | false, update_state); |
| 359 | EXPECT_FALSE(result.update_can_start); |
| 360 | EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason); |
| 361 | EXPECT_LE(2, result.scatter_check_threshold); |
| 362 | EXPECT_GE(5, result.scatter_check_threshold); |
| 363 | } |
| 364 | |
| 365 | TEST_F(PmChromeOSPolicyTest, |
| 366 | UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies) { |
| 367 | // The UpdateCanStart policy returns false; device policy is loaded and |
| 368 | // scattering due to a previously generated count threshold still applies. |
| 369 | |
| 370 | SetUpdateCheckAllowed(false); |
| 371 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 372 | new TimeDelta(TimeDelta::FromSeconds(1))); |
| 373 | |
| 374 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 375 | update_state.scatter_check_threshold = 3; |
| 376 | update_state.scatter_check_threshold_min = 2; |
| 377 | update_state.scatter_check_threshold_max = 5; |
| 378 | |
| 379 | // Check that the UpdateCanStart returns false. |
| 380 | UpdateCanStartResult result; |
| 381 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 382 | false, update_state); |
| 383 | EXPECT_FALSE(result.update_can_start); |
| 384 | EXPECT_EQ(UpdateCannotStartReason::kScattering, result.cannot_start_reason); |
| 385 | EXPECT_EQ(3, result.scatter_check_threshold); |
| 386 | } |
| 387 | |
| 388 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied) { |
| 389 | // The UpdateCanStart policy returns true; device policy is loaded and |
| 390 | // scattering is enabled, but both wait period and check threshold are |
| 391 | // satisfied. |
| 392 | |
| 393 | SetUpdateCheckAllowed(false); |
| 394 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 395 | new TimeDelta(TimeDelta::FromSeconds(120))); |
| 396 | |
| 397 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(75)); |
| 398 | update_state.num_checks = 4; |
| 399 | update_state.scatter_wait_period = TimeDelta::FromSeconds(60); |
| 400 | update_state.scatter_check_threshold = 3; |
| 401 | update_state.scatter_check_threshold_min = 2; |
| 402 | update_state.scatter_check_threshold_max = 5; |
| 403 | |
| 404 | // Check that the UpdateCanStart returns true. |
| 405 | UpdateCanStartResult result; |
| 406 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 407 | false, update_state); |
| 408 | EXPECT_TRUE(result.update_can_start); |
| 409 | EXPECT_EQ(TimeDelta(), result.scatter_wait_period); |
| 410 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 411 | } |
| 412 | |
| 413 | TEST_F(PmChromeOSPolicyTest, |
| 414 | UpdateCanStartAllowedInteractivePreventsScattering) { |
| 415 | // The UpdateCanStart policy returns true; device policy is loaded and |
| 416 | // scattering would have applied, except that the update check is interactive |
| 417 | // and so it is suppressed. |
| 418 | |
| 419 | SetUpdateCheckAllowed(false); |
| 420 | fake_state_.device_policy_provider()->var_scatter_factor()->reset( |
| 421 | new TimeDelta(TimeDelta::FromSeconds(1))); |
| 422 | |
| 423 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromSeconds(1)); |
| 424 | update_state.scatter_check_threshold = 0; |
| 425 | update_state.scatter_check_threshold_min = 2; |
| 426 | update_state.scatter_check_threshold_max = 5; |
| 427 | |
| 428 | // Check that the UpdateCanStart returns true. |
| 429 | UpdateCanStartResult result; |
| 430 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 431 | true, update_state); |
| 432 | EXPECT_TRUE(result.update_can_start); |
| 433 | EXPECT_EQ(TimeDelta(), result.scatter_wait_period); |
| 434 | EXPECT_EQ(0, result.scatter_check_threshold); |
| 435 | } |
| 436 | |
| 437 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartAllowedWithAttributes) { |
| 438 | // The UpdateCanStart policy returns true; device policy permits both HTTP and |
| 439 | // P2P updates, as well as a non-empty target channel string. |
| 440 | |
| 441 | SetUpdateCheckAllowed(false); |
| 442 | |
| 443 | // Override specific device policy attributes. |
| 444 | fake_state_.device_policy_provider()->var_http_downloads_enabled()->reset( |
| 445 | new bool(true)); |
| 446 | fake_state_.device_policy_provider()->var_au_p2p_enabled()->reset( |
| 447 | new bool(true)); |
| 448 | fake_state_.device_policy_provider()->var_release_channel_delegated()-> |
| 449 | reset(new bool(false)); |
| 450 | fake_state_.device_policy_provider()->var_release_channel()-> |
| 451 | reset(new string("foo-channel")); |
| 452 | |
| 453 | // Check that the UpdateCanStart returns true. |
| 454 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 455 | UpdateCanStartResult result; |
| 456 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 457 | false, update_state); |
| 458 | EXPECT_TRUE(result.update_can_start); |
| 459 | EXPECT_TRUE(result.http_allowed); |
| 460 | EXPECT_TRUE(result.p2p_allowed); |
| 461 | EXPECT_EQ("foo-channel", result.target_channel); |
| 462 | } |
| 463 | |
| 464 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartAllowedWithP2PFromUpdater) { |
| 465 | // The UpdateCanStart policy returns true; device policy forbids both HTTP and |
| 466 | // P2P updates, but the updater is configured to allow P2P and overrules the |
| 467 | // setting. |
| 468 | |
| 469 | SetUpdateCheckAllowed(false); |
| 470 | |
| 471 | // Override specific device policy attributes. |
| 472 | fake_state_.device_policy_provider()->var_release_channel_delegated()-> |
| 473 | reset(new bool(false)); |
| 474 | fake_state_.device_policy_provider()->var_release_channel()-> |
| 475 | reset(new string("foo-channel")); |
| 476 | fake_state_.updater_provider()->var_p2p_enabled()->reset(new bool(true)); |
| 477 | |
| 478 | // Check that the UpdateCanStart returns true. |
| 479 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 480 | UpdateCanStartResult result; |
| 481 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 482 | false, update_state); |
| 483 | EXPECT_TRUE(result.update_can_start); |
| 484 | EXPECT_FALSE(result.http_allowed); |
| 485 | EXPECT_TRUE(result.p2p_allowed); |
| 486 | EXPECT_EQ("foo-channel", result.target_channel); |
| 487 | } |
| 488 | |
| 489 | TEST_F(PmChromeOSPolicyTest, UpdateCanStartAllowedWithHttpForUnofficialBuild) { |
| 490 | // The UpdateCanStart policy returns true; device policy forbids both HTTP and |
| 491 | // P2P updates, but marking this an unofficial build overrules the HTTP |
| 492 | // setting. |
| 493 | |
| 494 | SetUpdateCheckAllowed(false); |
| 495 | |
| 496 | // Override specific device policy attributes. |
| 497 | fake_state_.device_policy_provider()->var_release_channel_delegated()-> |
| 498 | reset(new bool(false)); |
| 499 | fake_state_.device_policy_provider()->var_release_channel()-> |
| 500 | reset(new string("foo-channel")); |
| 501 | fake_state_.system_provider()->var_is_official_build()-> |
| 502 | reset(new bool(false)); |
| 503 | |
| 504 | // Check that the UpdateCanStart returns true. |
| 505 | UpdateState update_state = GetDefaultUpdateState(TimeDelta::FromMinutes(10)); |
| 506 | UpdateCanStartResult result; |
| 507 | ExpectPolicyStatus(EvalStatus::kSucceeded, &Policy::UpdateCanStart, &result, |
| 508 | false, update_state); |
| 509 | EXPECT_TRUE(result.update_can_start); |
| 510 | EXPECT_TRUE(result.http_allowed); |
| 511 | EXPECT_FALSE(result.p2p_allowed); |
| 512 | EXPECT_EQ("foo-channel", result.target_channel); |
| 513 | } |
| 514 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 515 | } // namespace chromeos_policy_manager |