Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 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 | |
| 17 | #include "update_engine/update_manager/policy_test_utils.h" |
| 18 | |
Aaron Wood | c73fdc1 | 2017-12-06 11:09:15 -0800 | [diff] [blame] | 19 | #include <memory> |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 20 | #include <tuple> |
| 21 | #include <vector> |
| 22 | |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 23 | #include "update_engine/cros/fake_system_state.h" |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 24 | #include "update_engine/update_manager/next_update_check_policy_impl.h" |
| 25 | |
| 26 | using base::Time; |
| 27 | using base::TimeDelta; |
| 28 | using chromeos_update_engine::ErrorCode; |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 29 | using chromeos_update_engine::FakeSystemState; |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 30 | using std::string; |
| 31 | using std::tuple; |
| 32 | using std::vector; |
| 33 | |
| 34 | namespace chromeos_update_manager { |
| 35 | |
| 36 | void UmPolicyTestBase::SetUp() { |
| 37 | loop_.SetAsCurrent(); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 38 | FakeSystemState::CreateInstance(); |
| 39 | fake_clock_ = FakeSystemState::Get()->fake_clock(); |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 40 | SetUpDefaultClock(); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 41 | eval_ctx_.reset(new EvaluationContext(TimeDelta::FromSeconds(5))); |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 42 | SetUpDefaultState(); |
| 43 | } |
| 44 | |
| 45 | void UmPolicyTestBase::TearDown() { |
| 46 | EXPECT_FALSE(loop_.PendingTasks()); |
| 47 | } |
| 48 | |
| 49 | // Sets the clock to fixed values. |
| 50 | void UmPolicyTestBase::SetUpDefaultClock() { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 51 | fake_clock_->SetMonotonicTime(Time::FromInternalValue(12345678L)); |
| 52 | fake_clock_->SetWallclockTime(Time::FromInternalValue(12345678901234L)); |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 55 | void UmPolicyTestBase::SetUpDefaultTimeProvider() { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 56 | Time current_time = FakeSystemState::Get()->clock()->GetWallclockTime(); |
Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 57 | base::Time::Exploded exploded; |
| 58 | current_time.LocalExplode(&exploded); |
| 59 | fake_state_.time_provider()->var_curr_hour()->reset(new int(exploded.hour)); |
| 60 | fake_state_.time_provider()->var_curr_minute()->reset( |
| 61 | new int(exploded.minute)); |
| 62 | fake_state_.time_provider()->var_curr_date()->reset( |
| 63 | new Time(current_time.LocalMidnight())); |
| 64 | } |
| 65 | |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 66 | void UmPolicyTestBase::SetUpDefaultState() { |
| 67 | fake_state_.updater_provider()->var_updater_started_time()->reset( |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 68 | new Time(fake_clock_->GetWallclockTime())); |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 69 | fake_state_.updater_provider()->var_last_checked_time()->reset( |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 70 | new Time(fake_clock_->GetWallclockTime())); |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 71 | fake_state_.updater_provider()->var_consecutive_failed_update_checks()->reset( |
Aaron Wood | c73fdc1 | 2017-12-06 11:09:15 -0800 | [diff] [blame] | 72 | new unsigned int(0)); // NOLINT(readability/casting) |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 73 | fake_state_.updater_provider()->var_server_dictated_poll_interval()->reset( |
Aaron Wood | c73fdc1 | 2017-12-06 11:09:15 -0800 | [diff] [blame] | 74 | new unsigned int(0)); // NOLINT(readability/casting) |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 75 | fake_state_.updater_provider()->var_forced_update_requested()->reset( |
| 76 | new UpdateRequestStatus{UpdateRequestStatus::kNone}); |
| 77 | |
| 78 | // Chosen by fair dice roll. Guaranteed to be random. |
| 79 | fake_state_.random_provider()->var_seed()->reset(new uint64_t(4)); |
| 80 | } |
| 81 | |
| 82 | // Returns a default UpdateState structure: |
| 83 | UpdateState UmPolicyTestBase::GetDefaultUpdateState( |
| 84 | TimeDelta first_seen_period) { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame^] | 85 | Time first_seen_time = |
| 86 | FakeSystemState::Get()->clock()->GetWallclockTime() - first_seen_period; |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 87 | UpdateState update_state = UpdateState(); |
| 88 | |
| 89 | // This is a non-interactive check returning a delta payload, seen for the |
| 90 | // first time (|first_seen_period| ago). Clearly, there were no failed |
| 91 | // attempts so far. |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 92 | update_state.interactive = false; |
Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 93 | update_state.is_delta_payload = false; |
| 94 | update_state.first_seen = first_seen_time; |
| 95 | update_state.num_checks = 1; |
| 96 | update_state.num_failures = 0; |
| 97 | update_state.failures_last_updated = Time(); // Needs to be zero. |
| 98 | // There's a single HTTP download URL with a maximum of 10 retries. |
| 99 | update_state.download_urls = vector<string>{"http://fake/url/"}; |
| 100 | update_state.download_errors_max = 10; |
| 101 | // Download was never attempted. |
| 102 | update_state.last_download_url_idx = -1; |
| 103 | update_state.last_download_url_num_errors = 0; |
| 104 | // There were no download errors. |
| 105 | update_state.download_errors = vector<tuple<int, ErrorCode, Time>>(); |
| 106 | // P2P is not disabled by Omaha. |
| 107 | update_state.p2p_downloading_disabled = false; |
| 108 | update_state.p2p_sharing_disabled = false; |
| 109 | // P2P was not attempted. |
| 110 | update_state.p2p_num_attempts = 0; |
| 111 | update_state.p2p_first_attempted = Time(); |
| 112 | // No active backoff period, backoff is not disabled by Omaha. |
| 113 | update_state.backoff_expiry = Time(); |
| 114 | update_state.is_backoff_disabled = false; |
| 115 | // There is no active scattering wait period (max 7 days allowed) nor check |
| 116 | // threshold (none allowed). |
| 117 | update_state.scatter_wait_period = TimeDelta(); |
| 118 | update_state.scatter_check_threshold = 0; |
| 119 | update_state.scatter_wait_period_max = TimeDelta::FromDays(7); |
| 120 | update_state.scatter_check_threshold_min = 0; |
| 121 | update_state.scatter_check_threshold_max = 0; |
| 122 | |
| 123 | return update_state; |
| 124 | } |
| 125 | |
Aaron Wood | c73fdc1 | 2017-12-06 11:09:15 -0800 | [diff] [blame] | 126 | } // namespace chromeos_update_manager |