policy_test_utils.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright (C) 2017 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/update_manager/policy_test_utils.h"
  17. #include <memory>
  18. #include <tuple>
  19. #include <vector>
  20. #include "update_engine/update_manager/next_update_check_policy_impl.h"
  21. using base::Time;
  22. using base::TimeDelta;
  23. using chromeos_update_engine::ErrorCode;
  24. using std::string;
  25. using std::tuple;
  26. using std::vector;
  27. namespace chromeos_update_manager {
  28. void UmPolicyTestBase::SetUp() {
  29. loop_.SetAsCurrent();
  30. SetUpDefaultClock();
  31. eval_ctx_ = new EvaluationContext(&fake_clock_, TimeDelta::FromSeconds(5));
  32. SetUpDefaultState();
  33. }
  34. void UmPolicyTestBase::TearDown() {
  35. EXPECT_FALSE(loop_.PendingTasks());
  36. }
  37. // Sets the clock to fixed values.
  38. void UmPolicyTestBase::SetUpDefaultClock() {
  39. fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
  40. fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L));
  41. }
  42. void UmPolicyTestBase::SetUpDefaultTimeProvider() {
  43. Time current_time = fake_clock_.GetWallclockTime();
  44. base::Time::Exploded exploded;
  45. current_time.LocalExplode(&exploded);
  46. fake_state_.time_provider()->var_curr_hour()->reset(new int(exploded.hour));
  47. fake_state_.time_provider()->var_curr_minute()->reset(
  48. new int(exploded.minute));
  49. fake_state_.time_provider()->var_curr_date()->reset(
  50. new Time(current_time.LocalMidnight()));
  51. }
  52. void UmPolicyTestBase::SetUpDefaultState() {
  53. fake_state_.updater_provider()->var_updater_started_time()->reset(
  54. new Time(fake_clock_.GetWallclockTime()));
  55. fake_state_.updater_provider()->var_last_checked_time()->reset(
  56. new Time(fake_clock_.GetWallclockTime()));
  57. fake_state_.updater_provider()->var_consecutive_failed_update_checks()->reset(
  58. new unsigned int(0)); // NOLINT(readability/casting)
  59. fake_state_.updater_provider()->var_server_dictated_poll_interval()->reset(
  60. new unsigned int(0)); // NOLINT(readability/casting)
  61. fake_state_.updater_provider()->var_forced_update_requested()->reset(
  62. new UpdateRequestStatus{UpdateRequestStatus::kNone});
  63. // Chosen by fair dice roll. Guaranteed to be random.
  64. fake_state_.random_provider()->var_seed()->reset(new uint64_t(4));
  65. }
  66. // Returns a default UpdateState structure:
  67. UpdateState UmPolicyTestBase::GetDefaultUpdateState(
  68. TimeDelta first_seen_period) {
  69. Time first_seen_time = fake_clock_.GetWallclockTime() - first_seen_period;
  70. UpdateState update_state = UpdateState();
  71. // This is a non-interactive check returning a delta payload, seen for the
  72. // first time (|first_seen_period| ago). Clearly, there were no failed
  73. // attempts so far.
  74. update_state.interactive = false;
  75. update_state.is_delta_payload = false;
  76. update_state.first_seen = first_seen_time;
  77. update_state.num_checks = 1;
  78. update_state.num_failures = 0;
  79. update_state.failures_last_updated = Time(); // Needs to be zero.
  80. // There's a single HTTP download URL with a maximum of 10 retries.
  81. update_state.download_urls = vector<string>{"http://fake/url/"};
  82. update_state.download_errors_max = 10;
  83. // Download was never attempted.
  84. update_state.last_download_url_idx = -1;
  85. update_state.last_download_url_num_errors = 0;
  86. // There were no download errors.
  87. update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
  88. // P2P is not disabled by Omaha.
  89. update_state.p2p_downloading_disabled = false;
  90. update_state.p2p_sharing_disabled = false;
  91. // P2P was not attempted.
  92. update_state.p2p_num_attempts = 0;
  93. update_state.p2p_first_attempted = Time();
  94. // No active backoff period, backoff is not disabled by Omaha.
  95. update_state.backoff_expiry = Time();
  96. update_state.is_backoff_disabled = false;
  97. // There is no active scattering wait period (max 7 days allowed) nor check
  98. // threshold (none allowed).
  99. update_state.scatter_wait_period = TimeDelta();
  100. update_state.scatter_check_threshold = 0;
  101. update_state.scatter_wait_period_max = TimeDelta::FromDays(7);
  102. update_state.scatter_check_threshold_min = 0;
  103. update_state.scatter_check_threshold_max = 0;
  104. return update_state;
  105. }
  106. } // namespace chromeos_update_manager