Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 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 | // |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 16 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/cros/update_attempter.h" |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 18 | |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
Jae Hoon Kim | 051627a | 2019-09-03 12:56:32 -0700 | [diff] [blame] | 21 | #include <limits> |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 22 | #include <map> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 23 | #include <memory> |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 24 | #include <string> |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 25 | #include <unordered_set> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 26 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 27 | #include <base/files/file_util.h> |
Miriam Polzer | aff7200 | 2020-08-27 08:20:39 +0200 | [diff] [blame] | 28 | #include <base/files/scoped_temp_dir.h> |
Qijiang Fan | b0b6cc2 | 2020-10-15 21:54:11 +0900 | [diff] [blame] | 29 | #include <base/task/single_thread_task_executor.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 30 | #include <brillo/message_loops/base_message_loop.h> |
| 31 | #include <brillo/message_loops/message_loop.h> |
| 32 | #include <brillo/message_loops/message_loop_utils.h> |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 33 | #include <gtest/gtest.h> |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 34 | #include <policy/libpolicy.h> |
| 35 | #include <policy/mock_device_policy.h> |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 36 | #include <policy/mock_libpolicy.h> |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 37 | |
Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame] | 38 | #include "update_engine/common/constants.h" |
Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 39 | #include "update_engine/common/dlcservice_interface.h" |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 40 | #include "update_engine/common/mock_action.h" |
| 41 | #include "update_engine/common/mock_action_processor.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 42 | #include "update_engine/common/mock_http_fetcher.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 43 | #include "update_engine/common/mock_service_observer.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 44 | #include "update_engine/common/platform_constants.h" |
| 45 | #include "update_engine/common/prefs.h" |
| 46 | #include "update_engine/common/test_utils.h" |
| 47 | #include "update_engine/common/utils.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 48 | #include "update_engine/cros/fake_system_state.h" |
| 49 | #include "update_engine/cros/mock_p2p_manager.h" |
| 50 | #include "update_engine/cros/mock_payload_state.h" |
| 51 | #include "update_engine/cros/omaha_utils.h" |
Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame] | 52 | #include "update_engine/libcurl_http_fetcher.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 53 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
| 54 | #include "update_engine/payload_consumer/install_plan.h" |
| 55 | #include "update_engine/payload_consumer/payload_constants.h" |
| 56 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Amin Hassani | 0882a51 | 2018-04-05 16:25:44 -0700 | [diff] [blame] | 57 | #include "update_engine/update_boot_flags_action.h" |
Jae Hoon Kim | 504c3cb | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 58 | #include "update_engine/update_manager/mock_update_manager.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 59 | |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 60 | using base::Time; |
| 61 | using base::TimeDelta; |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 62 | using chromeos_update_manager::EvalStatus; |
Jae Hoon Kim | 504c3cb | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 63 | using chromeos_update_manager::MockUpdateManager; |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 64 | using chromeos_update_manager::StagingSchedule; |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 65 | using chromeos_update_manager::UpdateCheckParams; |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 66 | using policy::DevicePolicy; |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 67 | using std::map; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 68 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 69 | using std::unique_ptr; |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 70 | using std::unordered_set; |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 71 | using std::vector; |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 72 | using testing::_; |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 73 | using testing::Contains; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 74 | using testing::DoAll; |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 75 | using testing::ElementsAre; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 76 | using testing::Field; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 77 | using testing::InSequence; |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 78 | using testing::Invoke; |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 79 | using testing::Ne; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 80 | using testing::NiceMock; |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 81 | using testing::Pointee; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 82 | using testing::Property; |
| 83 | using testing::Return; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 84 | using testing::ReturnPointee; |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 85 | using testing::ReturnRef; |
Alex Deymo | 2c0db7b | 2014-11-04 12:23:39 -0800 | [diff] [blame] | 86 | using testing::SaveArg; |
Ben Chan | 672c1f5 | 2017-10-23 15:41:39 -0700 | [diff] [blame] | 87 | using testing::SetArgPointee; |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 88 | using update_engine::UpdateAttemptFlags; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 89 | using update_engine::UpdateEngineStatus; |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 90 | using update_engine::UpdateStatus; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 91 | |
| 92 | namespace chromeos_update_engine { |
| 93 | |
Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 94 | namespace { |
| 95 | |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 96 | const UpdateStatus kNonIdleUpdateStatuses[] = { |
| 97 | UpdateStatus::CHECKING_FOR_UPDATE, |
| 98 | UpdateStatus::UPDATE_AVAILABLE, |
| 99 | UpdateStatus::DOWNLOADING, |
| 100 | UpdateStatus::VERIFYING, |
| 101 | UpdateStatus::FINALIZING, |
| 102 | UpdateStatus::UPDATED_NEED_REBOOT, |
| 103 | UpdateStatus::REPORTING_ERROR_EVENT, |
| 104 | UpdateStatus::ATTEMPTING_ROLLBACK, |
| 105 | UpdateStatus::DISABLED, |
| 106 | UpdateStatus::NEED_PERMISSION_TO_UPDATE, |
| 107 | }; |
| 108 | |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 109 | struct CheckForUpdateTestParams { |
| 110 | // Setups + Inputs: |
| 111 | UpdateStatus status = UpdateStatus::IDLE; |
| 112 | string app_version = "fake_app_version"; |
| 113 | string omaha_url = "fake_omaha_url"; |
| 114 | UpdateAttemptFlags flags = UpdateAttemptFlags::kNone; |
| 115 | bool is_official_build = true; |
| 116 | bool are_dev_features_enabled = false; |
| 117 | |
| 118 | // Expects: |
| 119 | string expected_forced_app_version = ""; |
| 120 | string expected_forced_omaha_url = ""; |
Jae Hoon Kim | 2b73ac2 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 121 | bool should_schedule_updates_be_called = true; |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 122 | bool expected_result = true; |
| 123 | }; |
| 124 | |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 125 | struct OnUpdateScheduledTestParams { |
| 126 | // Setups + Inputs: |
| 127 | UpdateCheckParams params = {}; |
| 128 | EvalStatus status = EvalStatus::kFailed; |
| 129 | // Expects: |
| 130 | UpdateStatus exit_status = UpdateStatus::IDLE; |
| 131 | bool should_schedule_updates_be_called = false; |
| 132 | bool should_update_be_called = false; |
| 133 | }; |
| 134 | |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 135 | struct ProcessingDoneTestParams { |
| 136 | // Setups + Inputs: |
| 137 | bool is_install = false; |
| 138 | UpdateStatus status = UpdateStatus::CHECKING_FOR_UPDATE; |
| 139 | ActionProcessor* processor = nullptr; |
| 140 | ErrorCode code = ErrorCode::kSuccess; |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 141 | map<string, OmahaRequestParams::AppParams> dlc_apps_params; |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 142 | |
| 143 | // Expects: |
| 144 | const bool kExpectedIsInstall = false; |
| 145 | bool should_schedule_updates_be_called = true; |
| 146 | UpdateStatus expected_exit_status = UpdateStatus::IDLE; |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 147 | bool should_install_completed_be_called = false; |
| 148 | bool should_update_completed_be_called = false; |
| 149 | vector<string> args_to_install_completed; |
| 150 | vector<string> args_to_update_completed; |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 153 | class MockDlcService : public DlcServiceInterface { |
| 154 | public: |
Amin Hassani | 2b68e6b | 2020-04-17 10:49:12 -0700 | [diff] [blame] | 155 | MOCK_METHOD1(GetDlcsToUpdate, bool(vector<string>*)); |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 156 | MOCK_METHOD1(InstallCompleted, bool(const vector<string>&)); |
| 157 | MOCK_METHOD1(UpdateCompleted, bool(const vector<string>&)); |
Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | } // namespace |
| 161 | |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 162 | const char kRollbackVersion[] = "10575.39.2"; |
| 163 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 164 | // Test a subclass rather than the main class directly so that we can mock out |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 165 | // methods within the class. There're explicit unit tests for the mocked out |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 166 | // methods. |
| 167 | class UpdateAttempterUnderTest : public UpdateAttempter { |
| 168 | public: |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 169 | UpdateAttempterUnderTest() : UpdateAttempter(nullptr) {} |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 170 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 171 | void Update(const UpdateCheckParams& params) override { |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 172 | update_called_ = true; |
| 173 | if (do_update_) { |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 174 | UpdateAttempter::Update(params); |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | LOG(INFO) << "[TEST] Update() disabled."; |
| 178 | status_ = UpdateStatus::CHECKING_FOR_UPDATE; |
| 179 | } |
| 180 | |
| 181 | void DisableUpdate() { do_update_ = false; } |
| 182 | |
| 183 | bool WasUpdateCalled() const { return update_called_; } |
| 184 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 185 | // Wrap the update scheduling method, allowing us to opt out of scheduled |
| 186 | // updates for testing purposes. |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 187 | bool ScheduleUpdates() override { |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 188 | schedule_updates_called_ = true; |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 189 | if (do_schedule_updates_) |
| 190 | return UpdateAttempter::ScheduleUpdates(); |
| 191 | LOG(INFO) << "[TEST] Update scheduling disabled."; |
| 192 | waiting_for_scheduled_check_ = true; |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 193 | return true; |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 194 | } |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 195 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 196 | void DisableScheduleUpdates() { do_schedule_updates_ = false; } |
| 197 | |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 198 | // Indicates whether |ScheduleUpdates()| was called. |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 199 | bool WasScheduleUpdatesCalled() const { return schedule_updates_called_; } |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 200 | |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 201 | // Need to expose following private members of |UpdateAttempter| for tests. |
| 202 | const string& forced_app_version() const { return forced_app_version_; } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 203 | const string& forced_omaha_url() const { return forced_omaha_url_; } |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 204 | |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 205 | // Need to expose |waiting_for_scheduled_check_| for testing. |
| 206 | void SetWaitingForScheduledCheck(bool waiting) { |
| 207 | waiting_for_scheduled_check_ = waiting; |
| 208 | } |
| 209 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 210 | private: |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 211 | // Used for overrides of |Update()|. |
| 212 | bool update_called_ = false; |
| 213 | bool do_update_ = true; |
| 214 | |
| 215 | // Used for overrides of |ScheduleUpdates()|. |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 216 | bool schedule_updates_called_ = false; |
| 217 | bool do_schedule_updates_ = true; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | class UpdateAttempterTest : public ::testing::Test { |
| 221 | protected: |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 222 | void SetUp() override { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 223 | // Override system state members. |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 224 | FakeSystemState::CreateInstance(); |
| 225 | FakeSystemState::Get()->set_connection_manager(&mock_connection_manager); |
| 226 | FakeSystemState::Get()->set_update_attempter(&attempter_); |
| 227 | FakeSystemState::Get()->set_dlcservice(&mock_dlcservice_); |
| 228 | FakeSystemState::Get()->set_update_manager(&mock_update_manager_); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 229 | loop_.SetAsCurrent(); |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 230 | |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 231 | prefs_ = FakeSystemState::Get()->fake_prefs(); |
| 232 | certificate_checker_.reset( |
| 233 | new CertificateChecker(prefs_, &openssl_wrapper_)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 234 | certificate_checker_->Init(); |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 235 | |
Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 236 | attempter_.set_forced_update_pending_callback( |
| 237 | new base::Callback<void(bool, bool)>(base::Bind([](bool, bool) {}))); |
Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 238 | // Finish initializing the attempter. |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 239 | attempter_.Init(); |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 240 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 241 | EXPECT_EQ(0, attempter_.http_response_code_); |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 242 | EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 243 | EXPECT_EQ(0.0, attempter_.download_progress_); |
| 244 | EXPECT_EQ(0, attempter_.last_checked_time_); |
| 245 | EXPECT_EQ("0.0.0.0", attempter_.new_version_); |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 246 | EXPECT_EQ(0ULL, attempter_.new_payload_size_); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 247 | processor_ = new NiceMock<MockActionProcessor>(); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 248 | attempter_.processor_.reset(processor_); // Transfers ownership. |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 249 | |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 250 | // Setup store/load semantics of P2P properties via the mock |PayloadState|. |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 251 | actual_using_p2p_for_downloading_ = false; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 252 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 253 | SetUsingP2PForDownloading(_)) |
| 254 | .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 255 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 256 | GetUsingP2PForDownloading()) |
| 257 | .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_)); |
| 258 | actual_using_p2p_for_sharing_ = false; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 259 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 260 | SetUsingP2PForSharing(_)) |
| 261 | .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 262 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 263 | GetUsingP2PForDownloading()) |
| 264 | .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 267 | public: |
| 268 | void ScheduleQuitMainLoop(); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 269 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 270 | // Callbacks to run the different tests from the main loop. |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 271 | void UpdateTestStart(); |
| 272 | void UpdateTestVerify(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 273 | void RollbackTestStart(bool enterprise_rollback, bool valid_slot); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 274 | void RollbackTestVerify(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 275 | void PingOmahaTestStart(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 276 | void ReadScatterFactorFromPolicyTestStart(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 277 | void DecrementUpdateCheckCountTestStart(); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 278 | void NoScatteringDoneDuringManualUpdateTestStart(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 279 | void P2PNotEnabledStart(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 280 | void P2PEnabledStart(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 281 | void P2PEnabledInteractiveStart(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 282 | void P2PEnabledStartingFailsStart(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 283 | void P2PEnabledHousekeepingFailsStart(); |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 284 | void SessionIdTestChange(); |
| 285 | void SessionIdTestEnforceEmptyStrPingOmaha(); |
| 286 | void SessionIdTestConsistencyInUpdateFlow(); |
Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame] | 287 | void SessionIdTestInDownloadAction(); |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 288 | void ResetRollbackHappenedStart(bool is_consumer, |
| 289 | bool is_policy_available, |
| 290 | bool expected_reset); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 291 | // Staging related callbacks. |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 292 | void SetUpStagingTest(const StagingSchedule& schedule); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 293 | void CheckStagingOff(); |
| 294 | void StagingSetsPrefsAndTurnsOffScatteringStart(); |
| 295 | void StagingOffIfInteractiveStart(); |
| 296 | void StagingOffIfOobeStart(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 297 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 298 | bool actual_using_p2p_for_downloading() { |
| 299 | return actual_using_p2p_for_downloading_; |
| 300 | } |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 301 | bool actual_using_p2p_for_sharing() { return actual_using_p2p_for_sharing_; } |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 302 | |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 303 | // |CheckForUpdate()| related member functions. |
| 304 | void TestCheckForUpdate(); |
| 305 | |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 306 | // |OnUpdateScheduled()| related member functions. |
| 307 | void TestOnUpdateScheduled(); |
| 308 | |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 309 | // |ProcessingDone()| related member functions. |
| 310 | void TestProcessingDone(); |
| 311 | |
Qijiang Fan | b0b6cc2 | 2020-10-15 21:54:11 +0900 | [diff] [blame] | 312 | base::SingleThreadTaskExecutor base_loop_{base::MessagePumpType::IO}; |
| 313 | brillo::BaseMessageLoop loop_{base_loop_.task_runner()}; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 314 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 315 | UpdateAttempterUnderTest attempter_; |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 316 | OpenSSLWrapper openssl_wrapper_; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 317 | std::unique_ptr<CertificateChecker> certificate_checker_; |
Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 318 | MockDlcService mock_dlcservice_; |
Jae Hoon Kim | 504c3cb | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 319 | MockUpdateManager mock_update_manager_; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 320 | |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 321 | NiceMock<MockActionProcessor>* processor_; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 322 | NiceMock<MockConnectionManager> mock_connection_manager; |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 323 | |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 324 | FakePrefs* prefs_; |
| 325 | |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 326 | // |CheckForUpdate()| test params. |
| 327 | CheckForUpdateTestParams cfu_params_; |
| 328 | |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 329 | // |OnUpdateScheduled()| test params. |
| 330 | OnUpdateScheduledTestParams ous_params_; |
| 331 | |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 332 | // |ProcessingDone()| test params. |
| 333 | ProcessingDoneTestParams pd_params_; |
| 334 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 335 | bool actual_using_p2p_for_downloading_; |
| 336 | bool actual_using_p2p_for_sharing_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 339 | void UpdateAttempterTest::TestCheckForUpdate() { |
| 340 | // Setup |
| 341 | attempter_.status_ = cfu_params_.status; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 342 | FakeSystemState::Get()->fake_hardware()->SetIsOfficialBuild( |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 343 | cfu_params_.is_official_build); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 344 | FakeSystemState::Get()->fake_hardware()->SetAreDevFeaturesEnabled( |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 345 | cfu_params_.are_dev_features_enabled); |
| 346 | |
| 347 | // Invocation |
| 348 | EXPECT_EQ( |
| 349 | cfu_params_.expected_result, |
| 350 | attempter_.CheckForUpdate( |
| 351 | cfu_params_.app_version, cfu_params_.omaha_url, cfu_params_.flags)); |
| 352 | |
| 353 | // Verify |
| 354 | EXPECT_EQ(cfu_params_.expected_forced_app_version, |
| 355 | attempter_.forced_app_version()); |
| 356 | EXPECT_EQ(cfu_params_.expected_forced_omaha_url, |
| 357 | attempter_.forced_omaha_url()); |
Jae Hoon Kim | 2b73ac2 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 358 | EXPECT_EQ(cfu_params_.should_schedule_updates_be_called, |
| 359 | attempter_.WasScheduleUpdatesCalled()); |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 362 | void UpdateAttempterTest::TestProcessingDone() { |
| 363 | // Setup |
| 364 | attempter_.DisableScheduleUpdates(); |
| 365 | attempter_.is_install_ = pd_params_.is_install; |
| 366 | attempter_.status_ = pd_params_.status; |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 367 | attempter_.omaha_request_params_->set_dlc_apps_params( |
| 368 | pd_params_.dlc_apps_params); |
| 369 | |
| 370 | // Expects |
| 371 | if (pd_params_.should_install_completed_be_called) |
| 372 | EXPECT_CALL(mock_dlcservice_, |
| 373 | InstallCompleted(pd_params_.args_to_install_completed)) |
| 374 | .WillOnce(Return(true)); |
| 375 | else |
| 376 | EXPECT_CALL(mock_dlcservice_, InstallCompleted(_)).Times(0); |
| 377 | if (pd_params_.should_update_completed_be_called) |
| 378 | EXPECT_CALL(mock_dlcservice_, |
| 379 | UpdateCompleted(pd_params_.args_to_update_completed)) |
| 380 | .WillOnce(Return(true)); |
| 381 | else |
| 382 | EXPECT_CALL(mock_dlcservice_, UpdateCompleted(_)).Times(0); |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 383 | |
| 384 | // Invocation |
| 385 | attempter_.ProcessingDone(pd_params_.processor, pd_params_.code); |
| 386 | |
| 387 | // Verify |
| 388 | EXPECT_EQ(pd_params_.kExpectedIsInstall, attempter_.is_install_); |
| 389 | EXPECT_EQ(pd_params_.should_schedule_updates_be_called, |
| 390 | attempter_.WasScheduleUpdatesCalled()); |
| 391 | EXPECT_EQ(pd_params_.expected_exit_status, attempter_.status_); |
| 392 | } |
| 393 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 394 | void UpdateAttempterTest::ScheduleQuitMainLoop() { |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 395 | loop_.PostTask( |
| 396 | FROM_HERE, |
| 397 | base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); }, |
| 398 | base::Unretained(&loop_))); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 401 | void UpdateAttempterTest::SessionIdTestChange() { |
| 402 | EXPECT_NE(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status()); |
| 403 | const auto old_session_id = attempter_.session_id_; |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 404 | attempter_.Update({}); |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 405 | EXPECT_NE(old_session_id, attempter_.session_id_); |
| 406 | ScheduleQuitMainLoop(); |
| 407 | } |
| 408 | |
| 409 | TEST_F(UpdateAttempterTest, SessionIdTestChange) { |
| 410 | loop_.PostTask(FROM_HERE, |
| 411 | base::Bind(&UpdateAttempterTest::SessionIdTestChange, |
| 412 | base::Unretained(this))); |
| 413 | loop_.Run(); |
| 414 | } |
| 415 | |
| 416 | void UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha() { |
| 417 | // The |session_id_| should not be changed and should remain as an empty |
| 418 | // string when |status_| is |UPDATED_NEED_REBOOT| (only for consistency) |
| 419 | // and |PingOmaha()| is called. |
| 420 | attempter_.DisableScheduleUpdates(); |
| 421 | attempter_.status_ = UpdateStatus::UPDATED_NEED_REBOOT; |
| 422 | const auto old_session_id = attempter_.session_id_; |
| 423 | auto CheckIfEmptySessionId = [](AbstractAction* aa) { |
| 424 | if (aa->Type() == OmahaRequestAction::StaticType()) { |
| 425 | EXPECT_TRUE(static_cast<OmahaRequestAction*>(aa)->session_id_.empty()); |
| 426 | } |
| 427 | }; |
| 428 | EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))) |
| 429 | .WillRepeatedly(Invoke(CheckIfEmptySessionId)); |
| 430 | EXPECT_CALL(*processor_, StartProcessing()); |
| 431 | attempter_.PingOmaha(); |
| 432 | EXPECT_EQ(old_session_id, attempter_.session_id_); |
| 433 | EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status_); |
| 434 | ScheduleQuitMainLoop(); |
| 435 | } |
| 436 | |
| 437 | TEST_F(UpdateAttempterTest, SessionIdTestEnforceEmptyStrPingOmaha) { |
| 438 | loop_.PostTask( |
| 439 | FROM_HERE, |
| 440 | base::Bind(&UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha, |
| 441 | base::Unretained(this))); |
| 442 | loop_.Run(); |
| 443 | } |
| 444 | |
| 445 | void UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow() { |
| 446 | // All session IDs passed into |OmahaRequestActions| should be enforced to |
| 447 | // have the same value in |BuildUpdateActions()|. |
| 448 | unordered_set<string> session_ids; |
| 449 | // Gather all the session IDs being passed to |OmahaRequestActions|. |
| 450 | auto CheckSessionId = [&session_ids](AbstractAction* aa) { |
| 451 | if (aa->Type() == OmahaRequestAction::StaticType()) |
| 452 | session_ids.insert(static_cast<OmahaRequestAction*>(aa)->session_id_); |
| 453 | }; |
| 454 | EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))) |
| 455 | .WillRepeatedly(Invoke(CheckSessionId)); |
| 456 | attempter_.BuildUpdateActions(false); |
| 457 | // Validate that all the session IDs are the same. |
| 458 | EXPECT_EQ(1, session_ids.size()); |
| 459 | ScheduleQuitMainLoop(); |
| 460 | } |
| 461 | |
| 462 | TEST_F(UpdateAttempterTest, SessionIdTestConsistencyInUpdateFlow) { |
| 463 | loop_.PostTask( |
| 464 | FROM_HERE, |
| 465 | base::Bind(&UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow, |
| 466 | base::Unretained(this))); |
| 467 | loop_.Run(); |
| 468 | } |
| 469 | |
Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame] | 470 | void UpdateAttempterTest::SessionIdTestInDownloadAction() { |
| 471 | // The session ID passed into |DownloadAction|'s |LibcurlHttpFetcher| should |
| 472 | // be enforced to be included in the HTTP header as X-Goog-Update-SessionId. |
| 473 | string header_value; |
| 474 | auto CheckSessionIdInDownloadAction = [&header_value](AbstractAction* aa) { |
| 475 | if (aa->Type() == DownloadAction::StaticType()) { |
| 476 | DownloadAction* da = static_cast<DownloadAction*>(aa); |
| 477 | EXPECT_TRUE(da->http_fetcher()->GetHeader(kXGoogleUpdateSessionId, |
| 478 | &header_value)); |
| 479 | } |
| 480 | }; |
| 481 | EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))) |
| 482 | .WillRepeatedly(Invoke(CheckSessionIdInDownloadAction)); |
| 483 | attempter_.BuildUpdateActions(false); |
| 484 | // Validate that X-Goog-Update_SessionId is set correctly in HTTP Header. |
| 485 | EXPECT_EQ(attempter_.session_id_, header_value); |
| 486 | ScheduleQuitMainLoop(); |
| 487 | } |
| 488 | |
| 489 | TEST_F(UpdateAttempterTest, SessionIdTestInDownloadAction) { |
| 490 | loop_.PostTask(FROM_HERE, |
| 491 | base::Bind(&UpdateAttempterTest::SessionIdTestInDownloadAction, |
| 492 | base::Unretained(this))); |
| 493 | loop_.Run(); |
| 494 | } |
| 495 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 496 | TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 497 | unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr)); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 498 | fetcher->FailTransfer(503); // Sets the HTTP response code. |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 499 | DownloadAction action(prefs_, |
| 500 | nullptr, |
| 501 | nullptr, |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 502 | fetcher.release(), |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 503 | false /* interactive */); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 504 | attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 505 | EXPECT_FALSE(prefs_->Exists(kPrefsDeltaUpdateFailures)); |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 506 | EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status()); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 507 | EXPECT_EQ(0.0, attempter_.download_progress_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 508 | ASSERT_EQ(nullptr, attempter_.error_event_.get()); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) { |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 512 | MockAction action; |
| 513 | EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction")); |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 514 | attempter_.status_ = UpdateStatus::DOWNLOADING; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 515 | attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError); |
| 516 | ASSERT_NE(nullptr, attempter_.error_event_.get()); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 519 | TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) { |
| 520 | // Simple test case, where all the values match (nothing was skipped) |
| 521 | uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB |
| 522 | uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB |
| 523 | uint64_t bytes_received_1 = bytes_progressed_1; |
| 524 | uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2; |
| 525 | uint64_t bytes_total = 20 * 1024 * 1024; // 20MB |
| 526 | |
| 527 | double progress_1 = |
| 528 | static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total); |
| 529 | double progress_2 = |
| 530 | static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total); |
| 531 | |
| 532 | EXPECT_EQ(0.0, attempter_.download_progress_); |
| 533 | // This is set via inspecting the InstallPlan payloads when the |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 534 | // |OmahaResponseAction| is completed. |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 535 | attempter_.new_payload_size_ = bytes_total; |
| 536 | NiceMock<MockServiceObserver> observer; |
| 537 | EXPECT_CALL(observer, |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 538 | SendStatusUpdate(AllOf( |
| 539 | Field(&UpdateEngineStatus::progress, progress_1), |
| 540 | Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING), |
| 541 | Field(&UpdateEngineStatus::new_size_bytes, bytes_total)))); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 542 | EXPECT_CALL(observer, |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 543 | SendStatusUpdate(AllOf( |
| 544 | Field(&UpdateEngineStatus::progress, progress_2), |
| 545 | Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING), |
| 546 | Field(&UpdateEngineStatus::new_size_bytes, bytes_total)))); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 547 | attempter_.AddObserver(&observer); |
| 548 | attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total); |
| 549 | EXPECT_EQ(progress_1, attempter_.download_progress_); |
| 550 | // This iteration validates that a later set of updates to the variables are |
| 551 | // properly handled (so that |getStatus()| will return the same progress info |
| 552 | // as the callback is receiving. |
| 553 | attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total); |
| 554 | EXPECT_EQ(progress_2, attempter_.download_progress_); |
| 555 | } |
| 556 | |
| 557 | TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) { |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 558 | // The transition into |UpdateStatus::DOWNLOADING| happens when the |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 559 | // first bytes are received. |
| 560 | uint64_t bytes_progressed = 1024 * 1024; // 1MB |
| 561 | uint64_t bytes_received = 2 * 1024 * 1024; // 2MB |
| 562 | uint64_t bytes_total = 20 * 1024 * 1024; // 300MB |
| 563 | attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE; |
| 564 | // This is set via inspecting the InstallPlan payloads when the |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 565 | // |OmahaResponseAction| is completed. |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 566 | attempter_.new_payload_size_ = bytes_total; |
| 567 | EXPECT_EQ(0.0, attempter_.download_progress_); |
| 568 | NiceMock<MockServiceObserver> observer; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 569 | EXPECT_CALL(observer, |
| 570 | SendStatusUpdate(AllOf( |
| 571 | Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING), |
| 572 | Field(&UpdateEngineStatus::new_size_bytes, bytes_total)))); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 573 | attempter_.AddObserver(&observer); |
| 574 | attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total); |
| 575 | EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_); |
| 576 | } |
| 577 | |
| 578 | TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) { |
| 579 | // There is a special case to ensure that at 100% downloaded, |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 580 | // |download_progress_| is updated and broadcastest. |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 581 | uint64_t bytes_progressed = 0; // ignored |
| 582 | uint64_t bytes_received = 5 * 1024 * 1024; // ignored |
| 583 | uint64_t bytes_total = 5 * 1024 * 1024; // 300MB |
| 584 | attempter_.status_ = UpdateStatus::DOWNLOADING; |
| 585 | attempter_.new_payload_size_ = bytes_total; |
| 586 | EXPECT_EQ(0.0, attempter_.download_progress_); |
| 587 | NiceMock<MockServiceObserver> observer; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 588 | EXPECT_CALL(observer, |
| 589 | SendStatusUpdate(AllOf( |
| 590 | Field(&UpdateEngineStatus::progress, 1.0), |
| 591 | Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING), |
| 592 | Field(&UpdateEngineStatus::new_size_bytes, bytes_total)))); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 593 | attempter_.AddObserver(&observer); |
| 594 | attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total); |
| 595 | EXPECT_EQ(1.0, attempter_.download_progress_); |
| 596 | } |
| 597 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 598 | TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 599 | unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr)); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 600 | fetcher->FailTransfer(500); // Sets the HTTP response code. |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 601 | OmahaRequestAction action(nullptr, std::move(fetcher), false, ""); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 602 | ObjectCollectorAction<OmahaResponse> collector_action; |
| 603 | BondActions(&action, &collector_action); |
| 604 | OmahaResponse response; |
| 605 | response.poll_interval = 234; |
| 606 | action.SetOutputObject(response); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 607 | attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 608 | EXPECT_FALSE(prefs_->Exists(kPrefsDeltaUpdateFailures)); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 609 | EXPECT_EQ(500, attempter_.http_response_code()); |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 610 | EXPECT_EQ(UpdateStatus::IDLE, attempter_.status()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 611 | EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 612 | ASSERT_TRUE(attempter_.error_event_.get() == nullptr); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 615 | TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) { |
Alex Deymo | 906191f | 2015-10-12 12:22:44 -0700 | [diff] [blame] | 616 | string boot_id; |
| 617 | EXPECT_TRUE(utils::GetBootId(&boot_id)); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 618 | FakeSystemState::Get()->fake_prefs()->SetString(kPrefsUpdateCompletedOnBootId, |
| 619 | boot_id); |
Sen Jiang | aeeb2e0 | 2016-06-09 15:00:16 -0700 | [diff] [blame] | 620 | attempter_.Init(); |
| 621 | EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status()); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 625 | EXPECT_EQ(ErrorCode::kSuccess, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 626 | GetErrorCodeForAction(nullptr, ErrorCode::kSuccess)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 627 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 628 | OmahaRequestAction omaha_request_action(nullptr, nullptr, false, ""); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 629 | EXPECT_EQ(ErrorCode::kOmahaRequestError, |
| 630 | GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 631 | OmahaResponseHandlerAction omaha_response_handler_action; |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 632 | EXPECT_EQ( |
| 633 | ErrorCode::kOmahaResponseHandlerError, |
| 634 | GetErrorCodeForAction(&omaha_response_handler_action, ErrorCode::kError)); |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 635 | DynamicPartitionControlStub dynamic_control_stub; |
| 636 | FilesystemVerifierAction filesystem_verifier_action(&dynamic_control_stub); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 637 | EXPECT_EQ( |
| 638 | ErrorCode::kFilesystemVerifierError, |
| 639 | GetErrorCodeForAction(&filesystem_verifier_action, ErrorCode::kError)); |
Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 640 | PostinstallRunnerAction postinstall_runner_action( |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 641 | FakeSystemState::Get()->fake_boot_control(), |
| 642 | FakeSystemState::Get()->fake_hardware()); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 643 | EXPECT_EQ( |
| 644 | ErrorCode::kPostinstallRunnerError, |
| 645 | GetErrorCodeForAction(&postinstall_runner_action, ErrorCode::kError)); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 646 | MockAction action_mock; |
| 647 | EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction")); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 648 | EXPECT_EQ(ErrorCode::kError, |
| 649 | GetErrorCodeForAction(&action_mock, ErrorCode::kError)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 652 | TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 653 | attempter_.omaha_request_params_->set_delta_okay(true); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 654 | attempter_.DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 655 | EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay()); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 656 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, |
| 657 | UpdateAttempter::kMaxDeltaUpdateFailures - 1); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 658 | attempter_.DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 659 | EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay()); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 660 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, |
| 661 | UpdateAttempter::kMaxDeltaUpdateFailures); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 662 | attempter_.DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 663 | EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay()); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 664 | attempter_.DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 665 | EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay()); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) { |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 669 | attempter_.MarkDeltaUpdateFailure(); |
| 670 | |
| 671 | EXPECT_TRUE(prefs_->SetInt64(kPrefsDeltaUpdateFailures, -1)); |
| 672 | attempter_.MarkDeltaUpdateFailure(); |
| 673 | int64_t value = 0; |
| 674 | EXPECT_TRUE(prefs_->GetInt64(kPrefsDeltaUpdateFailures, &value)); |
| 675 | EXPECT_EQ(value, 1); |
| 676 | |
| 677 | attempter_.MarkDeltaUpdateFailure(); |
| 678 | EXPECT_TRUE(prefs_->GetInt64(kPrefsDeltaUpdateFailures, &value)); |
| 679 | EXPECT_EQ(value, 2); |
| 680 | |
| 681 | EXPECT_TRUE(prefs_->SetInt64(kPrefsDeltaUpdateFailures, |
| 682 | UpdateAttempter::kMaxDeltaUpdateFailures)); |
| 683 | attempter_.MarkDeltaUpdateFailure(); |
| 684 | EXPECT_TRUE(prefs_->GetInt64(kPrefsDeltaUpdateFailures, &value)); |
| 685 | EXPECT_EQ(value, UpdateAttempter::kMaxDeltaUpdateFailures + 1); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 688 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) { |
| 689 | EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0); |
| 690 | EXPECT_CALL(*processor_, StartProcessing()).Times(0); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 691 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), UpdateFailed(_)) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 692 | .Times(0); |
| 693 | OmahaResponse response; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 694 | string url1 = "http://url1"; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 695 | response.packages.push_back({.payload_urls = {url1, "https://url"}}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 696 | EXPECT_CALL(*(FakeSystemState::Get()->mock_payload_state()), GetCurrentUrl()) |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 697 | .WillRepeatedly(Return(url1)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 698 | FakeSystemState::Get()->mock_payload_state()->SetResponse(response); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 699 | attempter_.ScheduleErrorEventAction(); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 700 | EXPECT_EQ(url1, |
| 701 | FakeSystemState::Get()->mock_payload_state()->GetCurrentUrl()); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) { |
| 705 | EXPECT_CALL(*processor_, |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 706 | EnqueueAction(Pointee(Property( |
| 707 | &AbstractAction::Type, OmahaRequestAction::StaticType())))); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 708 | EXPECT_CALL(*processor_, StartProcessing()); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 709 | ErrorCode err = ErrorCode::kError; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 710 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), UpdateFailed(err)); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 711 | attempter_.error_event_.reset(new OmahaEvent( |
| 712 | OmahaEvent::kTypeUpdateComplete, OmahaEvent::kResultError, err)); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 713 | attempter_.ScheduleErrorEventAction(); |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 714 | EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status()); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 717 | namespace { |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 718 | // Actions that will be built as part of an update check. |
Askar Aitzhan | 570ca87 | 2019-04-24 11:16:12 +0200 | [diff] [blame] | 719 | vector<string> GetUpdateActionTypes() { |
| 720 | return {OmahaRequestAction::StaticType(), |
| 721 | OmahaResponseHandlerAction::StaticType(), |
| 722 | UpdateBootFlagsAction::StaticType(), |
| 723 | OmahaRequestAction::StaticType(), |
| 724 | DownloadAction::StaticType(), |
| 725 | OmahaRequestAction::StaticType(), |
| 726 | FilesystemVerifierAction::StaticType(), |
| 727 | PostinstallRunnerAction::StaticType(), |
| 728 | OmahaRequestAction::StaticType()}; |
| 729 | } |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 730 | |
| 731 | // Actions that will be built as part of a user-initiated rollback. |
Askar Aitzhan | 570ca87 | 2019-04-24 11:16:12 +0200 | [diff] [blame] | 732 | vector<string> GetRollbackActionTypes() { |
| 733 | return {InstallPlanAction::StaticType(), |
| 734 | PostinstallRunnerAction::StaticType()}; |
| 735 | } |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 736 | |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 737 | const StagingSchedule kValidStagingSchedule = { |
| 738 | {4, 10}, {10, 40}, {19, 70}, {26, 100}}; |
| 739 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 740 | } // namespace |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 741 | |
| 742 | void UpdateAttempterTest::UpdateTestStart() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 743 | attempter_.set_http_response_code(200); |
Alex Deymo | 749ecf1 | 2014-10-21 20:06:57 -0700 | [diff] [blame] | 744 | |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 745 | // Expect that the device policy is loaded by the |UpdateAttempter| at some |
| 746 | // point by calling |RefreshDevicePolicy()|. |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 747 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
Alex Deymo | 749ecf1 | 2014-10-21 20:06:57 -0700 | [diff] [blame] | 748 | EXPECT_CALL(*device_policy, LoadPolicy()) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 749 | .Times(testing::AtLeast(1)) |
| 750 | .WillRepeatedly(Return(true)); |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 751 | attempter_.policy_provider_.reset( |
| 752 | new policy::PolicyProvider(std::move(device_policy))); |
Alex Deymo | 749ecf1 | 2014-10-21 20:06:57 -0700 | [diff] [blame] | 753 | |
| 754 | { |
| 755 | InSequence s; |
Askar Aitzhan | 570ca87 | 2019-04-24 11:16:12 +0200 | [diff] [blame] | 756 | for (const auto& update_action_type : GetUpdateActionTypes()) { |
Alex Deymo | 749ecf1 | 2014-10-21 20:06:57 -0700 | [diff] [blame] | 757 | EXPECT_CALL(*processor_, |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 758 | EnqueueAction(Pointee( |
Askar Aitzhan | 570ca87 | 2019-04-24 11:16:12 +0200 | [diff] [blame] | 759 | Property(&AbstractAction::Type, update_action_type)))); |
Alex Deymo | 749ecf1 | 2014-10-21 20:06:57 -0700 | [diff] [blame] | 760 | } |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 761 | EXPECT_CALL(*processor_, StartProcessing()); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 762 | } |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 763 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 764 | attempter_.Update({}); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 765 | loop_.PostTask(FROM_HERE, |
| 766 | base::Bind(&UpdateAttempterTest::UpdateTestVerify, |
| 767 | base::Unretained(this))); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 768 | } |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 769 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 770 | void UpdateAttempterTest::UpdateTestVerify() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 771 | EXPECT_EQ(0, attempter_.http_response_code()); |
| 772 | EXPECT_EQ(&attempter_, processor_->delegate()); |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 773 | EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 774 | loop_.BreakLoop(); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 777 | void UpdateAttempterTest::RollbackTestStart(bool enterprise_rollback, |
| 778 | bool valid_slot) { |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 779 | // Create a device policy so that we can change settings. |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 780 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 781 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 782 | FakeSystemState::Get()->set_device_policy(device_policy.get()); |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 783 | if (enterprise_rollback) { |
| 784 | // We return an empty owner as this is an enterprise. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 785 | EXPECT_CALL(*device_policy, GetOwner(_)) |
| 786 | .WillRepeatedly(DoAll(SetArgPointee<0>(string("")), Return(true))); |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 787 | } else { |
| 788 | // We return a fake owner as this is an owned consumer device. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 789 | EXPECT_CALL(*device_policy, GetOwner(_)) |
| 790 | .WillRepeatedly(DoAll(SetArgPointee<0>(string("fake.mail@fake.com")), |
| 791 | Return(true))); |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | attempter_.policy_provider_.reset( |
| 795 | new policy::PolicyProvider(std::move(device_policy))); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 796 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 797 | if (valid_slot) { |
| 798 | BootControlInterface::Slot rollback_slot = 1; |
| 799 | LOG(INFO) << "Test Mark Bootable: " |
| 800 | << BootControlInterface::SlotName(rollback_slot); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 801 | FakeSystemState::Get()->fake_boot_control()->SetSlotBootable(rollback_slot, |
| 802 | true); |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 805 | bool is_rollback_allowed = false; |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 806 | |
Chris Sosa | d38b113 | 2014-03-25 10:43:59 -0700 | [diff] [blame] | 807 | // We only allow rollback on devices that are not enterprise enrolled and |
| 808 | // which have a valid slot to rollback to. |
| 809 | if (!enterprise_rollback && valid_slot) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 810 | is_rollback_allowed = true; |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 813 | if (is_rollback_allowed) { |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 814 | InSequence s; |
Askar Aitzhan | 570ca87 | 2019-04-24 11:16:12 +0200 | [diff] [blame] | 815 | for (const auto& rollback_action_type : GetRollbackActionTypes()) { |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 816 | EXPECT_CALL(*processor_, |
Askar Aitzhan | 570ca87 | 2019-04-24 11:16:12 +0200 | [diff] [blame] | 817 | EnqueueAction(Pointee( |
| 818 | Property(&AbstractAction::Type, rollback_action_type)))); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 819 | } |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 820 | EXPECT_CALL(*processor_, StartProcessing()); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 821 | |
Chris Sosa | 44b9b7e | 2014-04-02 13:53:46 -0700 | [diff] [blame] | 822 | EXPECT_TRUE(attempter_.Rollback(true)); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 823 | loop_.PostTask(FROM_HERE, |
| 824 | base::Bind(&UpdateAttempterTest::RollbackTestVerify, |
| 825 | base::Unretained(this))); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 826 | } else { |
Chris Sosa | 44b9b7e | 2014-04-02 13:53:46 -0700 | [diff] [blame] | 827 | EXPECT_FALSE(attempter_.Rollback(true)); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 828 | loop_.BreakLoop(); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | void UpdateAttempterTest::RollbackTestVerify() { |
| 833 | // Verifies the actions that were enqueued. |
| 834 | EXPECT_EQ(&attempter_, processor_->delegate()); |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 835 | EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status()); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 836 | EXPECT_EQ(0U, attempter_.install_plan_->partitions.size()); |
| 837 | EXPECT_EQ(attempter_.install_plan_->powerwash_required, true); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 838 | loop_.BreakLoop(); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 841 | TEST_F(UpdateAttempterTest, UpdateTest) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 842 | UpdateTestStart(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 843 | loop_.Run(); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 846 | TEST_F(UpdateAttempterTest, RollbackTest) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 847 | loop_.PostTask(FROM_HERE, |
| 848 | base::Bind(&UpdateAttempterTest::RollbackTestStart, |
| 849 | base::Unretained(this), |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 850 | false, |
| 851 | true)); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 852 | loop_.Run(); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 855 | TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 856 | loop_.PostTask(FROM_HERE, |
| 857 | base::Bind(&UpdateAttempterTest::RollbackTestStart, |
| 858 | base::Unretained(this), |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 859 | false, |
| 860 | false)); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 861 | loop_.Run(); |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 864 | TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 865 | loop_.PostTask(FROM_HERE, |
| 866 | base::Bind(&UpdateAttempterTest::RollbackTestStart, |
| 867 | base::Unretained(this), |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 868 | true, |
| 869 | true)); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 870 | loop_.Run(); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 873 | void UpdateAttempterTest::PingOmahaTestStart() { |
| 874 | EXPECT_CALL(*processor_, |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 875 | EnqueueAction(Pointee(Property( |
| 876 | &AbstractAction::Type, OmahaRequestAction::StaticType())))); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 877 | EXPECT_CALL(*processor_, StartProcessing()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 878 | attempter_.PingOmaha(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 879 | ScheduleQuitMainLoop(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | TEST_F(UpdateAttempterTest, PingOmahaTest) { |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 883 | EXPECT_FALSE(attempter_.waiting_for_scheduled_check_); |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 884 | EXPECT_FALSE(attempter_.WasScheduleUpdatesCalled()); |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 885 | // Disable scheduling of subsequnet checks; we're using the |DefaultPolicy| in |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 886 | // testing, which is more permissive than we want to handle here. |
| 887 | attempter_.DisableScheduleUpdates(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 888 | loop_.PostTask(FROM_HERE, |
| 889 | base::Bind(&UpdateAttempterTest::PingOmahaTestStart, |
| 890 | base::Unretained(this))); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 891 | brillo::MessageLoopRunMaxIterations(&loop_, 100); |
Christopher Wiley | 6b6cc1b | 2015-10-05 17:50:07 -0700 | [diff] [blame] | 892 | EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status()); |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 893 | EXPECT_TRUE(attempter_.WasScheduleUpdatesCalled()); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 896 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) { |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 897 | MockAction action; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 898 | const ErrorCode kCode = ErrorCode::kDownloadTransferError; |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 899 | attempter_.CreatePendingErrorEvent(&action, kCode); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 900 | ASSERT_NE(nullptr, attempter_.error_event_.get()); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 901 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 902 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 903 | EXPECT_EQ( |
| 904 | static_cast<ErrorCode>(static_cast<int>(kCode) | |
| 905 | static_cast<int>(ErrorCode::kTestOmahaUrlFlag)), |
| 906 | attempter_.error_event_->error_code); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 910 | attempter_.install_plan_.reset(new InstallPlan); |
| 911 | attempter_.install_plan_->is_resume = true; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 912 | MockAction action; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 913 | const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError; |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 914 | attempter_.CreatePendingErrorEvent(&action, kCode); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 915 | ASSERT_NE(nullptr, attempter_.error_event_.get()); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 916 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 917 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 918 | EXPECT_EQ( |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 919 | static_cast<ErrorCode>(static_cast<int>(kCode) | |
| 920 | static_cast<int>(ErrorCode::kResumedFlag) | |
| 921 | static_cast<int>(ErrorCode::kTestOmahaUrlFlag)), |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 922 | attempter_.error_event_->error_code); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 923 | } |
| 924 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 925 | TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) { |
| 926 | MockP2PManager mock_p2p_manager; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 927 | FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 928 | mock_p2p_manager.fake().SetP2PEnabled(false); |
| 929 | EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0); |
| 930 | attempter_.UpdateEngineStarted(); |
| 931 | } |
| 932 | |
| 933 | TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) { |
| 934 | MockP2PManager mock_p2p_manager; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 935 | FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 936 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 937 | EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0); |
| 938 | attempter_.UpdateEngineStarted(); |
| 939 | } |
| 940 | |
| 941 | TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) { |
| 942 | MockP2PManager mock_p2p_manager; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 943 | FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 944 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 945 | mock_p2p_manager.fake().SetCountSharedFilesResult(1); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 946 | EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 947 | attempter_.UpdateEngineStarted(); |
| 948 | } |
| 949 | |
| 950 | TEST_F(UpdateAttempterTest, P2PNotEnabled) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 951 | loop_.PostTask(FROM_HERE, |
| 952 | base::Bind(&UpdateAttempterTest::P2PNotEnabledStart, |
| 953 | base::Unretained(this))); |
| 954 | loop_.Run(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 955 | } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 956 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 957 | void UpdateAttempterTest::P2PNotEnabledStart() { |
| 958 | // If P2P is not enabled, check that we do not attempt housekeeping |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 959 | // and do not convey that P2P is to be used. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 960 | MockP2PManager mock_p2p_manager; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 961 | FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 962 | mock_p2p_manager.fake().SetP2PEnabled(false); |
| 963 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 964 | attempter_.Update({}); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 965 | EXPECT_FALSE(actual_using_p2p_for_downloading_); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 966 | EXPECT_FALSE(actual_using_p2p_for_sharing()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 967 | ScheduleQuitMainLoop(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 971 | loop_.PostTask(FROM_HERE, |
| 972 | base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart, |
| 973 | base::Unretained(this))); |
| 974 | loop_.Run(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 975 | } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 976 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 977 | void UpdateAttempterTest::P2PEnabledStartingFailsStart() { |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 978 | // If P2P is enabled, but starting it fails ensure we don't do |
| 979 | // any housekeeping and do not convey that P2P should be used. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 980 | MockP2PManager mock_p2p_manager; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 981 | FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 982 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 983 | mock_p2p_manager.fake().SetEnsureP2PRunningResult(false); |
| 984 | mock_p2p_manager.fake().SetPerformHousekeepingResult(false); |
| 985 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 986 | attempter_.Update({}); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 987 | EXPECT_FALSE(actual_using_p2p_for_downloading()); |
| 988 | EXPECT_FALSE(actual_using_p2p_for_sharing()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 989 | ScheduleQuitMainLoop(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 993 | loop_.PostTask( |
| 994 | FROM_HERE, |
| 995 | base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart, |
| 996 | base::Unretained(this))); |
| 997 | loop_.Run(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 998 | } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 999 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1000 | void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() { |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 1001 | // If P2P is enabled, starting it works but housekeeping fails, ensure |
| 1002 | // we do not convey P2P is to be used. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1003 | MockP2PManager mock_p2p_manager; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1004 | FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1005 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 1006 | mock_p2p_manager.fake().SetEnsureP2PRunningResult(true); |
| 1007 | mock_p2p_manager.fake().SetPerformHousekeepingResult(false); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1008 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1009 | attempter_.Update({}); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1010 | EXPECT_FALSE(actual_using_p2p_for_downloading()); |
| 1011 | EXPECT_FALSE(actual_using_p2p_for_sharing()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1012 | ScheduleQuitMainLoop(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | TEST_F(UpdateAttempterTest, P2PEnabled) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1016 | loop_.PostTask(FROM_HERE, |
| 1017 | base::Bind(&UpdateAttempterTest::P2PEnabledStart, |
| 1018 | base::Unretained(this))); |
| 1019 | loop_.Run(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1020 | } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1021 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1022 | void UpdateAttempterTest::P2PEnabledStart() { |
| 1023 | MockP2PManager mock_p2p_manager; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1024 | FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1025 | // If P2P is enabled and starting it works, check that we performed |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 1026 | // housekeeping and that we convey P2P should be used. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1027 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 1028 | mock_p2p_manager.fake().SetEnsureP2PRunningResult(true); |
| 1029 | mock_p2p_manager.fake().SetPerformHousekeepingResult(true); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1030 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1031 | attempter_.Update({}); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1032 | EXPECT_TRUE(actual_using_p2p_for_downloading()); |
| 1033 | EXPECT_TRUE(actual_using_p2p_for_sharing()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1034 | ScheduleQuitMainLoop(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | TEST_F(UpdateAttempterTest, P2PEnabledInteractive) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1038 | loop_.PostTask(FROM_HERE, |
| 1039 | base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart, |
| 1040 | base::Unretained(this))); |
| 1041 | loop_.Run(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1042 | } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1043 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1044 | void UpdateAttempterTest::P2PEnabledInteractiveStart() { |
| 1045 | MockP2PManager mock_p2p_manager; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1046 | FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1047 | // For an interactive check, if P2P is enabled and starting it |
| 1048 | // works, check that we performed housekeeping and that we convey |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 1049 | // P2P should be used for sharing but NOT for downloading. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1050 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 1051 | mock_p2p_manager.fake().SetEnsureP2PRunningResult(true); |
| 1052 | mock_p2p_manager.fake().SetPerformHousekeepingResult(true); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1053 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1054 | attempter_.Update({.interactive = true}); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1055 | EXPECT_FALSE(actual_using_p2p_for_downloading()); |
| 1056 | EXPECT_TRUE(actual_using_p2p_for_sharing()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1057 | ScheduleQuitMainLoop(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1060 | TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1061 | loop_.PostTask( |
| 1062 | FROM_HERE, |
| 1063 | base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart, |
| 1064 | base::Unretained(this))); |
| 1065 | loop_.Run(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | // Tests that the scatter_factor_in_seconds value is properly fetched |
| 1069 | // from the device policy. |
| 1070 | void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() { |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1071 | int64_t scatter_factor_in_seconds = 36000; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1072 | |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 1073 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1074 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1075 | FakeSystemState::Get()->set_device_policy(device_policy.get()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1076 | |
| 1077 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1078 | .WillRepeatedly( |
| 1079 | DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true))); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1080 | |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 1081 | attempter_.policy_provider_.reset( |
| 1082 | new policy::PolicyProvider(std::move(device_policy))); |
| 1083 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1084 | attempter_.Update({}); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1085 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 1086 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1087 | ScheduleQuitMainLoop(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1091 | loop_.PostTask( |
| 1092 | FROM_HERE, |
| 1093 | base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart, |
| 1094 | base::Unretained(this))); |
| 1095 | loop_.Run(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() { |
| 1099 | // Tests that the scatter_factor_in_seconds value is properly fetched |
| 1100 | // from the device policy and is decremented if value > 0. |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1101 | int64_t initial_value = 5; |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1102 | auto* fake_prefs = FakeSystemState::Get()->fake_prefs(); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1103 | FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch()); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1104 | |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1105 | EXPECT_TRUE(fake_prefs->SetInt64(kPrefsUpdateCheckCount, initial_value)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1106 | |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1107 | int64_t scatter_factor_in_seconds = 10; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1108 | |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 1109 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1110 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1111 | FakeSystemState::Get()->set_device_policy(device_policy.get()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1112 | |
| 1113 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1114 | .WillRepeatedly( |
| 1115 | DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true))); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1116 | |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 1117 | attempter_.policy_provider_.reset( |
| 1118 | new policy::PolicyProvider(std::move(device_policy))); |
| 1119 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1120 | attempter_.Update({}); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1121 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 1122 | |
| 1123 | // Make sure the file still exists. |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1124 | EXPECT_TRUE(fake_prefs->Exists(kPrefsUpdateCheckCount)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1125 | |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1126 | int64_t new_value; |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1127 | EXPECT_TRUE(fake_prefs->GetInt64(kPrefsUpdateCheckCount, &new_value)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1128 | EXPECT_EQ(initial_value - 1, new_value); |
| 1129 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1130 | EXPECT_TRUE( |
| 1131 | attempter_.omaha_request_params_->update_check_count_wait_enabled()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1132 | |
| 1133 | // However, if the count is already 0, it's not decremented. Test that. |
| 1134 | initial_value = 0; |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1135 | EXPECT_TRUE(fake_prefs->SetInt64(kPrefsUpdateCheckCount, initial_value)); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1136 | attempter_.Update({}); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1137 | EXPECT_TRUE(fake_prefs->Exists(kPrefsUpdateCheckCount)); |
| 1138 | EXPECT_TRUE(fake_prefs->GetInt64(kPrefsUpdateCheckCount, &new_value)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1139 | EXPECT_EQ(initial_value, new_value); |
| 1140 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1141 | ScheduleQuitMainLoop(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1144 | TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1145 | loop_.PostTask( |
| 1146 | FROM_HERE, |
| 1147 | base::Bind( |
| 1148 | &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart, |
| 1149 | base::Unretained(this))); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1150 | loop_.Run(); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() { |
| 1154 | // Tests that no scattering logic is enabled if the update check |
| 1155 | // is manually done (as opposed to a scheduled update check) |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1156 | int64_t initial_value = 8; |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1157 | auto* fake_prefs = FakeSystemState::Get()->fake_prefs(); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1158 | FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch()); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1159 | |
Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 1160 | EXPECT_TRUE( |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1161 | fake_prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value)); |
| 1162 | EXPECT_TRUE(fake_prefs->SetInt64(kPrefsUpdateCheckCount, initial_value)); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1163 | |
| 1164 | // make sure scatter_factor is non-zero as scattering is disabled |
| 1165 | // otherwise. |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1166 | int64_t scatter_factor_in_seconds = 50; |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1167 | |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 1168 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1169 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1170 | FakeSystemState::Get()->set_device_policy(device_policy.get()); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1171 | |
| 1172 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1173 | .WillRepeatedly( |
| 1174 | DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true))); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1175 | |
Igor | 9fd76b6 | 2017-12-11 15:24:18 +0100 | [diff] [blame] | 1176 | attempter_.policy_provider_.reset( |
| 1177 | new policy::PolicyProvider(std::move(device_policy))); |
| 1178 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 1179 | // Trigger an interactive check so we can test that scattering is disabled. |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1180 | attempter_.Update({.interactive = true}); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1181 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 1182 | |
| 1183 | // Make sure scattering is disabled for manual (i.e. user initiated) update |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 1184 | // checks and all artifacts are removed. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1185 | EXPECT_FALSE( |
| 1186 | attempter_.omaha_request_params_->wall_clock_based_wait_enabled()); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1187 | EXPECT_FALSE(fake_prefs->Exists(kPrefsWallClockScatteringWaitPeriod)); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1188 | EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds()); |
| 1189 | EXPECT_FALSE( |
| 1190 | attempter_.omaha_request_params_->update_check_count_wait_enabled()); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1191 | EXPECT_FALSE(fake_prefs->Exists(kPrefsUpdateCheckCount)); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1192 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1193 | ScheduleQuitMainLoop(); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1196 | void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule) { |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1197 | int64_t initial_value = 8; |
| 1198 | EXPECT_TRUE( |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1199 | prefs_->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value)); |
| 1200 | EXPECT_TRUE(prefs_->SetInt64(kPrefsUpdateCheckCount, initial_value)); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1201 | attempter_.scatter_factor_ = TimeDelta::FromSeconds(20); |
| 1202 | |
| 1203 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
| 1204 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1205 | FakeSystemState::Get()->set_device_policy(device_policy.get()); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1206 | EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_)) |
| 1207 | .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true))); |
| 1208 | |
| 1209 | attempter_.policy_provider_.reset( |
| 1210 | new policy::PolicyProvider(std::move(device_policy))); |
| 1211 | } |
| 1212 | |
| 1213 | TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) { |
| 1214 | loop_.PostTask( |
| 1215 | FROM_HERE, |
| 1216 | base::Bind( |
| 1217 | &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart, |
| 1218 | base::Unretained(this))); |
| 1219 | loop_.Run(); |
| 1220 | } |
| 1221 | |
| 1222 | void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() { |
| 1223 | // Tests that staging sets its prefs properly and turns off scattering. |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1224 | FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch()); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1225 | SetUpStagingTest(kValidStagingSchedule); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1226 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1227 | attempter_.Update({}); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1228 | auto* fake_prefs = FakeSystemState::Get()->fake_prefs(); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1229 | // Check that prefs have the correct values. |
| 1230 | int64_t update_count; |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1231 | EXPECT_TRUE(fake_prefs->GetInt64(kPrefsUpdateCheckCount, &update_count)); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1232 | int64_t waiting_time_days; |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1233 | EXPECT_TRUE(fake_prefs->GetInt64(kPrefsWallClockStagingWaitPeriod, |
| 1234 | &waiting_time_days)); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1235 | EXPECT_GT(waiting_time_days, 0); |
| 1236 | // Update count should have been decremented. |
| 1237 | EXPECT_EQ(7, update_count); |
| 1238 | // Check that Omaha parameters were updated correctly. |
| 1239 | EXPECT_TRUE( |
| 1240 | attempter_.omaha_request_params_->update_check_count_wait_enabled()); |
| 1241 | EXPECT_TRUE( |
| 1242 | attempter_.omaha_request_params_->wall_clock_based_wait_enabled()); |
| 1243 | EXPECT_EQ(waiting_time_days, |
| 1244 | attempter_.omaha_request_params_->waiting_period().InDays()); |
| 1245 | // Check class variables. |
| 1246 | EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays()); |
| 1247 | EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_); |
| 1248 | // Check that scattering is turned off |
| 1249 | EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds()); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1250 | EXPECT_FALSE(fake_prefs->Exists(kPrefsWallClockScatteringWaitPeriod)); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1251 | |
| 1252 | ScheduleQuitMainLoop(); |
| 1253 | } |
| 1254 | |
| 1255 | void UpdateAttempterTest::CheckStagingOff() { |
| 1256 | // Check that all prefs were removed. |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1257 | EXPECT_FALSE(prefs_->Exists(kPrefsUpdateCheckCount)); |
| 1258 | EXPECT_FALSE(prefs_->Exists(kPrefsWallClockScatteringWaitPeriod)); |
| 1259 | EXPECT_FALSE(prefs_->Exists(kPrefsWallClockStagingWaitPeriod)); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1260 | // Check that the Omaha parameters have the correct value. |
| 1261 | EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays()); |
| 1262 | EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(), |
| 1263 | attempter_.staging_wait_time_); |
| 1264 | EXPECT_FALSE( |
| 1265 | attempter_.omaha_request_params_->update_check_count_wait_enabled()); |
| 1266 | EXPECT_FALSE( |
| 1267 | attempter_.omaha_request_params_->wall_clock_based_wait_enabled()); |
| 1268 | // Check that scattering is turned off too. |
| 1269 | EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds()); |
| 1270 | } |
| 1271 | |
| 1272 | TEST_F(UpdateAttempterTest, StagingOffIfInteractive) { |
| 1273 | loop_.PostTask(FROM_HERE, |
| 1274 | base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart, |
| 1275 | base::Unretained(this))); |
| 1276 | loop_.Run(); |
| 1277 | } |
| 1278 | |
| 1279 | void UpdateAttempterTest::StagingOffIfInteractiveStart() { |
| 1280 | // Tests that staging is turned off when an interactive update is requested. |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1281 | FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch()); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1282 | SetUpStagingTest(kValidStagingSchedule); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1283 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1284 | attempter_.Update({.interactive = true}); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1285 | CheckStagingOff(); |
| 1286 | |
| 1287 | ScheduleQuitMainLoop(); |
| 1288 | } |
| 1289 | |
| 1290 | TEST_F(UpdateAttempterTest, StagingOffIfOobe) { |
| 1291 | loop_.PostTask(FROM_HERE, |
| 1292 | base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart, |
| 1293 | base::Unretained(this))); |
| 1294 | loop_.Run(); |
| 1295 | } |
| 1296 | |
| 1297 | void UpdateAttempterTest::StagingOffIfOobeStart() { |
| 1298 | // Tests that staging is turned off if OOBE hasn't been completed. |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1299 | FakeSystemState::Get()->fake_hardware()->SetIsOOBEEnabled(true); |
| 1300 | FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete(); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1301 | SetUpStagingTest(kValidStagingSchedule); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1302 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1303 | attempter_.Update({.interactive = true}); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1304 | CheckStagingOff(); |
| 1305 | |
| 1306 | ScheduleQuitMainLoop(); |
| 1307 | } |
| 1308 | |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1309 | // Checks that we only report daily metrics at most every 24 hours. |
| 1310 | TEST_F(UpdateAttempterTest, ReportDailyMetrics) { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1311 | auto* fake_clock = FakeSystemState::Get()->fake_clock(); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1312 | Time epoch = Time::FromInternalValue(0); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1313 | fake_clock->SetWallclockTime(epoch); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1314 | |
| 1315 | // If there is no kPrefsDailyMetricsLastReportedAt state variable, |
| 1316 | // we should report. |
| 1317 | EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics()); |
| 1318 | // We should not report again if no time has passed. |
| 1319 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
| 1320 | |
| 1321 | // We should not report if only 10 hours has passed. |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1322 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(10)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1323 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
| 1324 | |
| 1325 | // We should not report if only 24 hours - 1 sec has passed. |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1326 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(24) - |
| 1327 | TimeDelta::FromSeconds(1)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1328 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
| 1329 | |
| 1330 | // We should report if 24 hours has passed. |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1331 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(24)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1332 | EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics()); |
| 1333 | |
| 1334 | // But then we should not report again.. |
| 1335 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
| 1336 | |
| 1337 | // .. until another 24 hours has passed |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1338 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(47)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1339 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1340 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(48)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1341 | EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics()); |
| 1342 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
| 1343 | |
| 1344 | // .. and another 24 hours |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1345 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(71)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1346 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1347 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(72)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1348 | EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics()); |
| 1349 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
| 1350 | |
| 1351 | // If the span between time of reporting and present time is |
| 1352 | // negative, we report. This is in order to reset the timestamp and |
| 1353 | // avoid an edge condition whereby a distant point in the future is |
| 1354 | // in the state variable resulting in us never ever reporting again. |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1355 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(71)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1356 | EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics()); |
| 1357 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
| 1358 | |
| 1359 | // In this case we should not update until the clock reads 71 + 24 = 95. |
| 1360 | // Check that. |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1361 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(94)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1362 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1363 | fake_clock->SetWallclockTime(epoch + TimeDelta::FromHours(95)); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1364 | EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics()); |
| 1365 | EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics()); |
David Zeuthen | 985b112 | 2013-10-09 12:13:15 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 1368 | TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1369 | FakeSystemState::Get()->fake_clock()->SetBootTime(Time::FromTimeT(42)); |
Sen Jiang | aeeb2e0 | 2016-06-09 15:00:16 -0700 | [diff] [blame] | 1370 | attempter_.Init(); |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 1371 | |
| 1372 | Time boot_time; |
Sen Jiang | aeeb2e0 | 2016-06-09 15:00:16 -0700 | [diff] [blame] | 1373 | EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time)); |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 1374 | |
Sen Jiang | aeeb2e0 | 2016-06-09 15:00:16 -0700 | [diff] [blame] | 1375 | attempter_.WriteUpdateCompletedMarker(); |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 1376 | |
Sen Jiang | aeeb2e0 | 2016-06-09 15:00:16 -0700 | [diff] [blame] | 1377 | EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time)); |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 1378 | EXPECT_EQ(boot_time.ToTimeT(), 42); |
| 1379 | } |
| 1380 | |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 1381 | TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1382 | FakeSystemState::Get()->fake_hardware()->SetIsOfficialBuild(false); |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 1383 | EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed()); |
| 1384 | } |
| 1385 | |
| 1386 | TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1387 | FakeSystemState::Get()->fake_hardware()->SetIsOfficialBuild(true); |
| 1388 | FakeSystemState::Get()->fake_hardware()->SetAreDevFeaturesEnabled(true); |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 1389 | EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed()); |
| 1390 | } |
| 1391 | |
| 1392 | TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1393 | FakeSystemState::Get()->fake_hardware()->SetIsOfficialBuild(true); |
| 1394 | FakeSystemState::Get()->fake_hardware()->SetAreDevFeaturesEnabled(false); |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 1395 | EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed()); |
| 1396 | } |
| 1397 | |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1398 | // TODO(kimjae): Follow testing pattern with params for |CheckForInstall()|. |
| 1399 | // When adding, remove older tests related to |CheckForInstall()|. |
| 1400 | TEST_F(UpdateAttempterTest, CheckForInstallNotIdleFails) { |
| 1401 | for (const auto status : kNonIdleUpdateStatuses) { |
| 1402 | // GIVEN a non-idle status. |
| 1403 | attempter_.status_ = status; |
Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 1404 | |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1405 | EXPECT_FALSE(attempter_.CheckForInstall({}, "")); |
| 1406 | } |
Xiaochu Liu | 8ba486f | 2018-11-06 11:14:10 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1409 | TEST_F(UpdateAttempterTest, CheckForUpdateNotIdleFails) { |
| 1410 | for (const auto status : kNonIdleUpdateStatuses) { |
| 1411 | // GIVEN a non-idle status. |
| 1412 | cfu_params_.status = status; |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1413 | |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1414 | // THEN |ScheduleUpdates()| should not be called. |
| 1415 | cfu_params_.should_schedule_updates_be_called = false; |
| 1416 | // THEN result should indicate failure. |
| 1417 | cfu_params_.expected_result = false; |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1418 | |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1419 | TestCheckForUpdate(); |
| 1420 | } |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | TEST_F(UpdateAttempterTest, CheckForUpdateOfficalBuildClearsSource) { |
| 1424 | // GIVEN a official build. |
| 1425 | |
| 1426 | // THEN we except forced app version + forced omaha url to be cleared. |
| 1427 | |
| 1428 | TestCheckForUpdate(); |
| 1429 | } |
| 1430 | |
| 1431 | TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildChangesSource) { |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1432 | // GIVEN a nonofficial build with dev features enabled. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1433 | cfu_params_.is_official_build = false; |
| 1434 | cfu_params_.are_dev_features_enabled = true; |
| 1435 | |
| 1436 | // THEN the forced app version + forced omaha url changes based on input. |
| 1437 | cfu_params_.expected_forced_app_version = cfu_params_.app_version; |
| 1438 | cfu_params_.expected_forced_omaha_url = cfu_params_.omaha_url; |
| 1439 | |
| 1440 | TestCheckForUpdate(); |
| 1441 | } |
| 1442 | |
| 1443 | TEST_F(UpdateAttempterTest, CheckForUpdateOfficialBuildScheduledAUTest) { |
| 1444 | // GIVEN a scheduled autest omaha url. |
| 1445 | cfu_params_.omaha_url = "autest-scheduled"; |
| 1446 | |
| 1447 | // THEN forced app version is cleared. |
| 1448 | // THEN forced omaha url changes to default constant. |
| 1449 | cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL; |
| 1450 | |
| 1451 | TestCheckForUpdate(); |
| 1452 | } |
| 1453 | |
| 1454 | TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildScheduledAUTest) { |
| 1455 | // GIVEN a scheduled autest omaha url. |
| 1456 | cfu_params_.omaha_url = "autest-scheduled"; |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1457 | // GIVEN a nonofficial build with dev features enabled. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1458 | cfu_params_.is_official_build = false; |
| 1459 | cfu_params_.are_dev_features_enabled = true; |
| 1460 | |
| 1461 | // THEN forced app version changes based on input. |
| 1462 | cfu_params_.expected_forced_app_version = cfu_params_.app_version; |
| 1463 | // THEN forced omaha url changes to default constant. |
| 1464 | cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL; |
| 1465 | |
| 1466 | TestCheckForUpdate(); |
| 1467 | } |
| 1468 | |
| 1469 | TEST_F(UpdateAttempterTest, CheckForUpdateOfficialBuildAUTest) { |
| 1470 | // GIVEN a autest omaha url. |
| 1471 | cfu_params_.omaha_url = "autest"; |
| 1472 | |
| 1473 | // THEN forced app version is cleared. |
| 1474 | // THEN forced omaha url changes to default constant. |
| 1475 | cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL; |
| 1476 | |
| 1477 | TestCheckForUpdate(); |
| 1478 | } |
| 1479 | |
| 1480 | TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildAUTest) { |
| 1481 | // GIVEN a autest omha url. |
| 1482 | cfu_params_.omaha_url = "autest"; |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1483 | // GIVEN a nonofficial build with dev features enabled. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1484 | cfu_params_.is_official_build = false; |
| 1485 | cfu_params_.are_dev_features_enabled = true; |
| 1486 | |
| 1487 | // THEN forced app version changes based on input. |
| 1488 | cfu_params_.expected_forced_app_version = cfu_params_.app_version; |
| 1489 | // THEN forced omaha url changes to default constant. |
| 1490 | cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL; |
| 1491 | |
| 1492 | TestCheckForUpdate(); |
| 1493 | } |
| 1494 | |
| 1495 | TEST_F(UpdateAttempterTest, |
| 1496 | CheckForUpdateNonInteractiveOfficialBuildScheduledAUTest) { |
| 1497 | // GIVEN a scheduled autest omaha url. |
| 1498 | cfu_params_.omaha_url = "autest-scheduled"; |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1499 | // GIVEN a noninteractive update. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1500 | cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive; |
| 1501 | |
| 1502 | // THEN forced app version is cleared. |
| 1503 | // THEN forced omaha url changes to default constant. |
| 1504 | cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL; |
| 1505 | |
| 1506 | TestCheckForUpdate(); |
| 1507 | } |
| 1508 | |
| 1509 | TEST_F(UpdateAttempterTest, |
| 1510 | CheckForUpdateNonInteractiveUnofficialBuildScheduledAUTest) { |
| 1511 | // GIVEN a scheduled autest omaha url. |
| 1512 | cfu_params_.omaha_url = "autest-scheduled"; |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1513 | // GIVEN a noninteractive update. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1514 | cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive; |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1515 | // GIVEN a nonofficial build with dev features enabled. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1516 | cfu_params_.is_official_build = false; |
| 1517 | cfu_params_.are_dev_features_enabled = true; |
| 1518 | |
| 1519 | // THEN forced app version changes based on input. |
| 1520 | cfu_params_.expected_forced_app_version = cfu_params_.app_version; |
| 1521 | // THEN forced omaha url changes to default constant. |
| 1522 | cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL; |
| 1523 | |
| 1524 | TestCheckForUpdate(); |
| 1525 | } |
| 1526 | |
| 1527 | TEST_F(UpdateAttempterTest, CheckForUpdateNonInteractiveOfficialBuildAUTest) { |
| 1528 | // GIVEN a autest omaha url. |
| 1529 | cfu_params_.omaha_url = "autest"; |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1530 | // GIVEN a noninteractive update. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1531 | cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive; |
| 1532 | |
| 1533 | // THEN forced app version is cleared. |
| 1534 | // THEN forced omaha url changes to default constant. |
| 1535 | cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL; |
| 1536 | |
| 1537 | TestCheckForUpdate(); |
| 1538 | } |
| 1539 | |
| 1540 | TEST_F(UpdateAttempterTest, CheckForUpdateNonInteractiveUnofficialBuildAUTest) { |
| 1541 | // GIVEN a autest omaha url. |
| 1542 | cfu_params_.omaha_url = "autest"; |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1543 | // GIVEN a noninteractive update. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1544 | cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive; |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1545 | // GIVEN a nonofficial build with dev features enabled. |
Jae Hoon Kim | 51ea9ae | 2019-07-03 16:56:30 -0700 | [diff] [blame] | 1546 | cfu_params_.is_official_build = false; |
| 1547 | cfu_params_.are_dev_features_enabled = true; |
| 1548 | |
| 1549 | // THEN forced app version changes based on input. |
| 1550 | cfu_params_.expected_forced_app_version = cfu_params_.app_version; |
| 1551 | // THEN forced omaha url changes to default constant. |
| 1552 | cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL; |
| 1553 | |
| 1554 | TestCheckForUpdate(); |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
Jae Hoon Kim | 2b73ac2 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 1557 | TEST_F(UpdateAttempterTest, CheckForUpdateMissingForcedCallback1) { |
| 1558 | // GIVEN a official build. |
| 1559 | // GIVEN forced callback is not set. |
| 1560 | attempter_.set_forced_update_pending_callback(nullptr); |
| 1561 | |
| 1562 | // THEN we except forced app version + forced omaha url to be cleared. |
| 1563 | // THEN |ScheduleUpdates()| should not be called. |
| 1564 | cfu_params_.should_schedule_updates_be_called = false; |
| 1565 | |
| 1566 | TestCheckForUpdate(); |
| 1567 | } |
| 1568 | |
| 1569 | TEST_F(UpdateAttempterTest, CheckForUpdateMissingForcedCallback2) { |
Jae Hoon Kim | c437ea5 | 2019-07-11 11:20:38 -0700 | [diff] [blame] | 1570 | // GIVEN a nonofficial build with dev features enabled. |
Jae Hoon Kim | 2b73ac2 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 1571 | cfu_params_.is_official_build = false; |
| 1572 | cfu_params_.are_dev_features_enabled = true; |
| 1573 | // GIVEN forced callback is not set. |
| 1574 | attempter_.set_forced_update_pending_callback(nullptr); |
| 1575 | |
| 1576 | // THEN the forced app version + forced omaha url changes based on input. |
| 1577 | cfu_params_.expected_forced_app_version = cfu_params_.app_version; |
| 1578 | cfu_params_.expected_forced_omaha_url = cfu_params_.omaha_url; |
| 1579 | // THEN |ScheduleUpdates()| should not be called. |
| 1580 | cfu_params_.should_schedule_updates_be_called = false; |
| 1581 | |
| 1582 | TestCheckForUpdate(); |
| 1583 | } |
| 1584 | |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 1585 | TEST_F(UpdateAttempterTest, CheckForInstallTest) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1586 | FakeSystemState::Get()->fake_hardware()->SetIsOfficialBuild(true); |
| 1587 | FakeSystemState::Get()->fake_hardware()->SetAreDevFeaturesEnabled(false); |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 1588 | attempter_.CheckForInstall({}, "autest"); |
| 1589 | EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url()); |
| 1590 | |
| 1591 | attempter_.CheckForInstall({}, "autest-scheduled"); |
| 1592 | EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url()); |
| 1593 | |
| 1594 | attempter_.CheckForInstall({}, "http://omaha.phishing"); |
| 1595 | EXPECT_EQ("", attempter_.forced_omaha_url()); |
| 1596 | } |
| 1597 | |
Colin Howes | ac170d9 | 2018-11-20 16:29:28 -0800 | [diff] [blame] | 1598 | TEST_F(UpdateAttempterTest, InstallSetsStatusIdle) { |
| 1599 | attempter_.CheckForInstall({}, "http://foo.bar"); |
| 1600 | attempter_.status_ = UpdateStatus::DOWNLOADING; |
| 1601 | EXPECT_TRUE(attempter_.is_install_); |
| 1602 | attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess); |
| 1603 | UpdateEngineStatus status; |
| 1604 | attempter_.GetStatus(&status); |
| 1605 | // Should set status to idle after an install operation. |
| 1606 | EXPECT_EQ(UpdateStatus::IDLE, status.status); |
| 1607 | } |
| 1608 | |
Colin Howes | 978c108 | 2018-12-03 11:46:12 -0800 | [diff] [blame] | 1609 | TEST_F(UpdateAttempterTest, RollbackAfterInstall) { |
| 1610 | attempter_.is_install_ = true; |
| 1611 | attempter_.Rollback(false); |
| 1612 | EXPECT_FALSE(attempter_.is_install_); |
| 1613 | } |
| 1614 | |
| 1615 | TEST_F(UpdateAttempterTest, UpdateAfterInstall) { |
| 1616 | attempter_.is_install_ = true; |
| 1617 | attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone); |
| 1618 | EXPECT_FALSE(attempter_.is_install_); |
| 1619 | } |
| 1620 | |
Xiyuan Xia | c0e8f9a | 2017-02-22 13:19:35 -0800 | [diff] [blame] | 1621 | TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) { |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1622 | UpdateCheckParams params; |
| 1623 | attempter_.CalculateUpdateParams({.target_version_prefix = "1234"}); |
Xiyuan Xia | c0e8f9a | 2017-02-22 13:19:35 -0800 | [diff] [blame] | 1624 | EXPECT_EQ("1234", |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1625 | FakeSystemState::Get()->request_params()->target_version_prefix()); |
Xiyuan Xia | c0e8f9a | 2017-02-22 13:19:35 -0800 | [diff] [blame] | 1626 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1627 | attempter_.CalculateUpdateParams({}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1628 | EXPECT_TRUE(FakeSystemState::Get() |
| 1629 | ->request_params() |
| 1630 | ->target_version_prefix() |
| 1631 | .empty()); |
Xiyuan Xia | c0e8f9a | 2017-02-22 13:19:35 -0800 | [diff] [blame] | 1632 | } |
| 1633 | |
Amin Hassani | 37b6723 | 2020-08-13 09:29:48 -0700 | [diff] [blame] | 1634 | TEST_F(UpdateAttempterTest, TargetChannelHintSetAndReset) { |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1635 | attempter_.CalculateUpdateParams({.lts_tag = "hint"}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1636 | EXPECT_EQ("hint", FakeSystemState::Get()->request_params()->lts_tag()); |
Amin Hassani | 37b6723 | 2020-08-13 09:29:48 -0700 | [diff] [blame] | 1637 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1638 | attempter_.CalculateUpdateParams({}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1639 | EXPECT_TRUE(FakeSystemState::Get()->request_params()->lts_tag().empty()); |
Amin Hassani | 37b6723 | 2020-08-13 09:29:48 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 1642 | TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) { |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1643 | attempter_.CalculateUpdateParams({ |
| 1644 | .target_version_prefix = "1234", |
| 1645 | .rollback_allowed = true, |
| 1646 | .rollback_allowed_milestones = 4, |
| 1647 | }); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1648 | EXPECT_TRUE(FakeSystemState::Get()->request_params()->rollback_allowed()); |
| 1649 | EXPECT_EQ( |
| 1650 | 4, |
| 1651 | FakeSystemState::Get()->request_params()->rollback_allowed_milestones()); |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 1652 | |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1653 | attempter_.CalculateUpdateParams({ |
| 1654 | .target_version_prefix = "1234", |
| 1655 | .rollback_allowed_milestones = 4, |
| 1656 | }); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1657 | EXPECT_FALSE(FakeSystemState::Get()->request_params()->rollback_allowed()); |
| 1658 | EXPECT_EQ( |
| 1659 | 4, |
| 1660 | FakeSystemState::Get()->request_params()->rollback_allowed_milestones()); |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 1661 | } |
| 1662 | |
Miriam Polzer | aff7200 | 2020-08-27 08:20:39 +0200 | [diff] [blame] | 1663 | TEST_F(UpdateAttempterTest, ChannelDowngradeNoRollback) { |
| 1664 | base::ScopedTempDir tempdir; |
| 1665 | ASSERT_TRUE(tempdir.CreateUniqueTempDir()); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1666 | FakeSystemState::Get()->request_params()->set_root(tempdir.GetPath().value()); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1667 | attempter_.CalculateUpdateParams({ |
| 1668 | .target_channel = "stable-channel", |
| 1669 | }); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1670 | EXPECT_FALSE( |
| 1671 | FakeSystemState::Get()->request_params()->is_powerwash_allowed()); |
Miriam Polzer | aff7200 | 2020-08-27 08:20:39 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | TEST_F(UpdateAttempterTest, ChannelDowngradeRollback) { |
| 1675 | base::ScopedTempDir tempdir; |
| 1676 | ASSERT_TRUE(tempdir.CreateUniqueTempDir()); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1677 | FakeSystemState::Get()->request_params()->set_root(tempdir.GetPath().value()); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1678 | attempter_.CalculateUpdateParams({ |
| 1679 | .rollback_on_channel_downgrade = true, |
| 1680 | .target_channel = "stable-channel", |
| 1681 | }); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1682 | EXPECT_TRUE(FakeSystemState::Get()->request_params()->is_powerwash_allowed()); |
Miriam Polzer | aff7200 | 2020-08-27 08:20:39 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 1685 | TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) { |
| 1686 | // Construct an OmahaResponseHandlerAction that has processed an InstallPlan, |
| 1687 | // but the update is being deferred by the Policy. |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1688 | OmahaResponseHandlerAction response_action; |
Amin Hassani | 68512d4 | 2018-07-31 23:52:33 -0700 | [diff] [blame] | 1689 | response_action.install_plan_.version = "a.b.c.d"; |
Amin Hassani | 68512d4 | 2018-07-31 23:52:33 -0700 | [diff] [blame] | 1690 | response_action.install_plan_.payloads.push_back( |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 1691 | {.size = 1234ULL, .type = InstallPayloadType::kFull}); |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 1692 | // Inform the UpdateAttempter that the OmahaResponseHandlerAction has |
| 1693 | // completed, with the deferred-update error code. |
| 1694 | attempter_.ActionCompleted( |
Amin Hassani | 68512d4 | 2018-07-31 23:52:33 -0700 | [diff] [blame] | 1695 | nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy); |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 1696 | { |
| 1697 | UpdateEngineStatus status; |
| 1698 | attempter_.GetStatus(&status); |
| 1699 | EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 1700 | EXPECT_TRUE(attempter_.install_plan_); |
| 1701 | EXPECT_EQ(attempter_.install_plan_->version, status.new_version); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 1702 | EXPECT_EQ(attempter_.install_plan_->payloads[0].size, |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 1703 | status.new_size_bytes); |
| 1704 | } |
| 1705 | // An "error" event should have been created to tell Omaha that the update is |
| 1706 | // being deferred. |
| 1707 | EXPECT_TRUE(nullptr != attempter_.error_event_); |
| 1708 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 1709 | EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result); |
| 1710 | ErrorCode expected_code = static_cast<ErrorCode>( |
| 1711 | static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) | |
| 1712 | static_cast<int>(ErrorCode::kTestOmahaUrlFlag)); |
| 1713 | EXPECT_EQ(expected_code, attempter_.error_event_->error_code); |
| 1714 | // End the processing |
| 1715 | attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy); |
| 1716 | // Validate the state of the attempter. |
| 1717 | { |
| 1718 | UpdateEngineStatus status; |
| 1719 | attempter_.GetStatus(&status); |
| 1720 | EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status); |
Amin Hassani | 68512d4 | 2018-07-31 23:52:33 -0700 | [diff] [blame] | 1721 | EXPECT_EQ(response_action.install_plan_.version, status.new_version); |
Amin Hassani | 68512d4 | 2018-07-31 23:52:33 -0700 | [diff] [blame] | 1722 | EXPECT_EQ(response_action.install_plan_.payloads[0].size, |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 1723 | status.new_size_bytes); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) { |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 1728 | // Default construction for |waiting_for_scheduled_check_| is false. |
Jae Hoon Kim | ba2fdce | 2019-07-11 13:18:58 -0700 | [diff] [blame] | 1729 | EXPECT_FALSE(attempter_.IsBusyOrUpdateScheduled()); |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 1730 | // Verify in-progress update with UPDATE_AVAILABLE is running |
| 1731 | attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE; |
Jae Hoon Kim | ba2fdce | 2019-07-11 13:18:58 -0700 | [diff] [blame] | 1732 | EXPECT_TRUE(attempter_.IsBusyOrUpdateScheduled()); |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 1735 | TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) { |
| 1736 | attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload); |
| 1737 | |
| 1738 | UpdateCheckParams params = {.updates_enabled = true}; |
| 1739 | attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params); |
| 1740 | |
| 1741 | EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload, |
| 1742 | attempter_.GetCurrentUpdateAttemptFlags()); |
| 1743 | } |
| 1744 | |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 1745 | TEST_F(UpdateAttempterTest, RollbackNotAllowed) { |
| 1746 | UpdateCheckParams params = {.updates_enabled = true, |
| 1747 | .rollback_allowed = false}; |
| 1748 | attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1749 | EXPECT_FALSE(FakeSystemState::Get()->request_params()->rollback_allowed()); |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | TEST_F(UpdateAttempterTest, RollbackAllowed) { |
| 1753 | UpdateCheckParams params = {.updates_enabled = true, |
| 1754 | .rollback_allowed = true}; |
| 1755 | attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1756 | EXPECT_TRUE(FakeSystemState::Get()->request_params()->rollback_allowed()); |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 1757 | } |
| 1758 | |
Aaron Wood | 081c023 | 2017-10-19 17:14:58 -0700 | [diff] [blame] | 1759 | TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) { |
| 1760 | attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload); |
| 1761 | |
| 1762 | attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone); |
| 1763 | EXPECT_EQ(UpdateAttemptFlags::kNone, |
| 1764 | attempter_.GetCurrentUpdateAttemptFlags()); |
| 1765 | } |
| 1766 | |
| 1767 | TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) { |
| 1768 | attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone); |
| 1769 | |
| 1770 | // This tests that when CheckForUpdate() is called with the non-interactive |
| 1771 | // flag set, that it doesn't change the current UpdateAttemptFlags. |
| 1772 | attempter_.CheckForUpdate("", |
| 1773 | "", |
| 1774 | UpdateAttemptFlags::kFlagNonInteractive | |
| 1775 | UpdateAttemptFlags::kFlagRestrictDownload); |
| 1776 | EXPECT_EQ(UpdateAttemptFlags::kNone, |
| 1777 | attempter_.GetCurrentUpdateAttemptFlags()); |
| 1778 | } |
| 1779 | |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 1780 | void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer, |
| 1781 | bool is_policy_loaded, |
| 1782 | bool expected_reset) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1783 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), |
| 1784 | GetRollbackHappened()) |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 1785 | .WillRepeatedly(Return(true)); |
| 1786 | auto mock_policy_provider = |
| 1787 | std::make_unique<NiceMock<policy::MockPolicyProvider>>(); |
| 1788 | EXPECT_CALL(*mock_policy_provider, IsConsumerDevice()) |
| 1789 | .WillRepeatedly(Return(is_consumer)); |
| 1790 | EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded()) |
| 1791 | .WillRepeatedly(Return(is_policy_loaded)); |
| 1792 | const policy::MockDevicePolicy device_policy; |
| 1793 | EXPECT_CALL(*mock_policy_provider, GetDevicePolicy()) |
| 1794 | .WillRepeatedly(ReturnRef(device_policy)); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1795 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 1796 | SetRollbackHappened(false)) |
| 1797 | .Times(expected_reset ? 1 : 0); |
| 1798 | attempter_.policy_provider_ = std::move(mock_policy_provider); |
Amin Hassani | e815363 | 2020-10-27 15:11:28 -0700 | [diff] [blame] | 1799 | attempter_.Update({}); |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 1800 | ScheduleQuitMainLoop(); |
| 1801 | } |
| 1802 | |
| 1803 | TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) { |
| 1804 | loop_.PostTask(FROM_HERE, |
| 1805 | base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart, |
| 1806 | base::Unretained(this), |
| 1807 | /*is_consumer=*/false, |
| 1808 | /*is_policy_loaded=*/false, |
| 1809 | /*expected_reset=*/false)); |
| 1810 | loop_.Run(); |
| 1811 | } |
| 1812 | |
| 1813 | TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) { |
| 1814 | loop_.PostTask(FROM_HERE, |
| 1815 | base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart, |
| 1816 | base::Unretained(this), |
| 1817 | /*is_consumer=*/true, |
| 1818 | /*is_policy_loaded=*/false, |
| 1819 | /*expected_reset=*/true)); |
| 1820 | loop_.Run(); |
| 1821 | } |
| 1822 | |
| 1823 | TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) { |
| 1824 | loop_.PostTask(FROM_HERE, |
| 1825 | base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart, |
| 1826 | base::Unretained(this), |
| 1827 | /*is_consumer=*/false, |
| 1828 | /*is_policy_loaded=*/true, |
| 1829 | /*expected_reset=*/true)); |
| 1830 | loop_.Run(); |
| 1831 | } |
| 1832 | |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 1833 | TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 1834 | attempter_.install_plan_.reset(new InstallPlan); |
| 1835 | attempter_.install_plan_->is_rollback = true; |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 1836 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1837 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 1838 | SetRollbackHappened(true)) |
| 1839 | .Times(1); |
| 1840 | attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess); |
| 1841 | } |
| 1842 | |
| 1843 | TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 1844 | attempter_.install_plan_.reset(new InstallPlan); |
| 1845 | attempter_.install_plan_->is_rollback = false; |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 1846 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1847 | EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 1848 | SetRollbackHappened(true)) |
| 1849 | .Times(0); |
| 1850 | attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess); |
| 1851 | } |
| 1852 | |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1853 | TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 1854 | attempter_.install_plan_.reset(new InstallPlan); |
| 1855 | attempter_.install_plan_->is_rollback = true; |
| 1856 | attempter_.install_plan_->version = kRollbackVersion; |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1857 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1858 | EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(), |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1859 | ReportEnterpriseRollbackMetrics(true, kRollbackVersion)) |
| 1860 | .Times(1); |
| 1861 | attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess); |
| 1862 | } |
| 1863 | |
| 1864 | TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 1865 | attempter_.install_plan_.reset(new InstallPlan); |
| 1866 | attempter_.install_plan_->is_rollback = false; |
| 1867 | attempter_.install_plan_->version = kRollbackVersion; |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1868 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1869 | EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(), |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1870 | ReportEnterpriseRollbackMetrics(_, _)) |
| 1871 | .Times(0); |
| 1872 | attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess); |
| 1873 | } |
| 1874 | |
| 1875 | TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 1876 | attempter_.install_plan_.reset(new InstallPlan); |
| 1877 | attempter_.install_plan_->is_rollback = true; |
| 1878 | attempter_.install_plan_->version = kRollbackVersion; |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1879 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1880 | EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(), |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1881 | ReportEnterpriseRollbackMetrics(false, kRollbackVersion)) |
| 1882 | .Times(1); |
| 1883 | MockAction action; |
| 1884 | attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible); |
| 1885 | attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible); |
| 1886 | } |
| 1887 | |
| 1888 | TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 1889 | attempter_.install_plan_.reset(new InstallPlan); |
| 1890 | attempter_.install_plan_->is_rollback = false; |
| 1891 | attempter_.install_plan_->version = kRollbackVersion; |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1892 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1893 | EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(), |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1894 | ReportEnterpriseRollbackMetrics(_, _)) |
| 1895 | .Times(0); |
| 1896 | MockAction action; |
| 1897 | attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible); |
| 1898 | attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible); |
| 1899 | } |
| 1900 | |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1901 | TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) { |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1902 | EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(), |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1903 | ReportEnterpriseUpdateSeenToDownloadDays(_, _)) |
| 1904 | .Times(0); |
| 1905 | attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy); |
| 1906 | } |
| 1907 | |
| 1908 | TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) { |
| 1909 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1910 | FakeSystemState::Get()->set_device_policy(device_policy.get()); |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1911 | // Make device policy return that this is not enterprise enrolled |
| 1912 | EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false)); |
| 1913 | |
| 1914 | // Ensure that the metric is not recorded. |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1915 | EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(), |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1916 | ReportEnterpriseUpdateSeenToDownloadDays(_, _)) |
| 1917 | .Times(0); |
| 1918 | attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess); |
| 1919 | } |
| 1920 | |
| 1921 | TEST_F(UpdateAttempterTest, |
| 1922 | TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) { |
| 1923 | constexpr int kDaysToUpdate = 15; |
| 1924 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1925 | FakeSystemState::Get()->set_device_policy(device_policy.get()); |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1926 | // Make device policy return that this is enterprise enrolled |
| 1927 | EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true)); |
| 1928 | // Pretend that there's a time restriction policy in place |
| 1929 | EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_)) |
| 1930 | .WillOnce(Return(true)); |
| 1931 | |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1932 | Time update_first_seen_at = Time::Now(); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1933 | FakeSystemState::Get()->fake_prefs()->SetInt64( |
| 1934 | kPrefsUpdateFirstSeenAt, update_first_seen_at.ToInternalValue()); |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1935 | |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1936 | Time update_finished_at = |
| 1937 | update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1938 | FakeSystemState::Get()->fake_clock()->SetWallclockTime(update_finished_at); |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1939 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1940 | EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(), |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1941 | ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate)) |
| 1942 | .Times(1); |
| 1943 | attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess); |
| 1944 | } |
| 1945 | |
| 1946 | TEST_F(UpdateAttempterTest, |
| 1947 | TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) { |
| 1948 | constexpr int kDaysToUpdate = 15; |
| 1949 | auto device_policy = std::make_unique<policy::MockDevicePolicy>(); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1950 | FakeSystemState::Get()->set_device_policy(device_policy.get()); |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1951 | // Make device policy return that this is enterprise enrolled |
| 1952 | EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true)); |
| 1953 | // Pretend that there's no time restriction policy in place |
| 1954 | EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_)) |
| 1955 | .WillOnce(Return(false)); |
| 1956 | |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1957 | Time update_first_seen_at = Time::Now(); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 1958 | FakeSystemState::Get()->fake_prefs()->SetInt64( |
| 1959 | kPrefsUpdateFirstSeenAt, update_first_seen_at.ToInternalValue()); |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1960 | |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1961 | Time update_finished_at = |
| 1962 | update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 1963 | FakeSystemState::Get()->fake_clock()->SetWallclockTime(update_finished_at); |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1964 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1965 | EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(), |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1966 | ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate)) |
| 1967 | .Times(1); |
| 1968 | attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess); |
| 1969 | } |
| 1970 | |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 1971 | TEST_F(UpdateAttempterTest, ProcessingDoneUpdated) { |
| 1972 | // GIVEN an update finished. |
| 1973 | |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 1974 | // THEN update_engine should call update completion. |
| 1975 | pd_params_.should_update_completed_be_called = true; |
| 1976 | // THEN need reboot since update applied. |
| 1977 | pd_params_.expected_exit_status = UpdateStatus::UPDATED_NEED_REBOOT; |
| 1978 | // THEN install indication should be false. |
| 1979 | |
| 1980 | TestProcessingDone(); |
| 1981 | } |
| 1982 | |
| 1983 | TEST_F(UpdateAttempterTest, ProcessingDoneUpdatedDlcFilter) { |
| 1984 | // GIVEN an update finished. |
| 1985 | // GIVEN DLC |AppParams| list. |
| 1986 | auto dlc_1 = "dlc_1", dlc_2 = "dlc_2"; |
| 1987 | pd_params_.dlc_apps_params = {{dlc_1, {.name = dlc_1, .updated = false}}, |
| 1988 | {dlc_2, {.name = dlc_2}}}; |
| 1989 | |
| 1990 | // THEN update_engine should call update completion. |
| 1991 | pd_params_.should_update_completed_be_called = true; |
| 1992 | pd_params_.args_to_update_completed = {dlc_2}; |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 1993 | // THEN need reboot since update applied. |
| 1994 | pd_params_.expected_exit_status = UpdateStatus::UPDATED_NEED_REBOOT; |
| 1995 | // THEN install indication should be false. |
| 1996 | |
| 1997 | TestProcessingDone(); |
| 1998 | } |
| 1999 | |
| 2000 | TEST_F(UpdateAttempterTest, ProcessingDoneInstalled) { |
| 2001 | // GIVEN an install finished. |
| 2002 | pd_params_.is_install = true; |
| 2003 | |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 2004 | // THEN update_engine should call install completion. |
| 2005 | pd_params_.should_install_completed_be_called = true; |
| 2006 | // THEN go idle. |
| 2007 | // THEN install indication should be false. |
| 2008 | |
| 2009 | TestProcessingDone(); |
| 2010 | } |
| 2011 | |
| 2012 | TEST_F(UpdateAttempterTest, ProcessingDoneInstalledDlcFilter) { |
| 2013 | // GIVEN an install finished. |
| 2014 | pd_params_.is_install = true; |
| 2015 | // GIVEN DLC |AppParams| list. |
| 2016 | auto dlc_1 = "dlc_1", dlc_2 = "dlc_2"; |
| 2017 | pd_params_.dlc_apps_params = {{dlc_1, {.name = dlc_1, .updated = false}}, |
| 2018 | {dlc_2, {.name = dlc_2}}}; |
| 2019 | |
| 2020 | // THEN update_engine should call install completion. |
| 2021 | pd_params_.should_install_completed_be_called = true; |
| 2022 | pd_params_.args_to_install_completed = {dlc_2}; |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 2023 | // THEN go idle. |
| 2024 | // THEN install indication should be false. |
| 2025 | |
| 2026 | TestProcessingDone(); |
| 2027 | } |
| 2028 | |
| 2029 | TEST_F(UpdateAttempterTest, ProcessingDoneInstallReportingError) { |
| 2030 | // GIVEN an install finished. |
| 2031 | pd_params_.is_install = true; |
| 2032 | // GIVEN a reporting error occurred. |
| 2033 | pd_params_.status = UpdateStatus::REPORTING_ERROR_EVENT; |
| 2034 | |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 2035 | // THEN update_engine should not call install completion. |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 2036 | // THEN go idle. |
| 2037 | // THEN install indication should be false. |
| 2038 | |
| 2039 | TestProcessingDone(); |
| 2040 | } |
| 2041 | |
| 2042 | TEST_F(UpdateAttempterTest, ProcessingDoneNoUpdate) { |
| 2043 | // GIVEN an update finished. |
| 2044 | // GIVEN an action error occured. |
| 2045 | pd_params_.code = ErrorCode::kNoUpdate; |
| 2046 | |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 2047 | // THEN update_engine should not call update completion. |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 2048 | // THEN go idle. |
| 2049 | // THEN install indication should be false. |
| 2050 | |
| 2051 | TestProcessingDone(); |
| 2052 | } |
| 2053 | |
| 2054 | TEST_F(UpdateAttempterTest, ProcessingDoneNoInstall) { |
| 2055 | // GIVEN an install finished. |
| 2056 | pd_params_.is_install = true; |
| 2057 | // GIVEN an action error occured. |
| 2058 | pd_params_.code = ErrorCode::kNoUpdate; |
| 2059 | |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 2060 | // THEN update_engine should not call install completion. |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 2061 | // THEN go idle. |
| 2062 | // THEN install indication should be false. |
| 2063 | |
| 2064 | TestProcessingDone(); |
| 2065 | } |
| 2066 | |
| 2067 | TEST_F(UpdateAttempterTest, ProcessingDoneUpdateError) { |
| 2068 | // GIVEN an update finished. |
| 2069 | // GIVEN an action error occured. |
| 2070 | pd_params_.code = ErrorCode::kError; |
| 2071 | // GIVEN an event error is set. |
| 2072 | attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 2073 | OmahaEvent::kResultError, |
| 2074 | ErrorCode::kError)); |
| 2075 | |
| 2076 | // THEN indicate a error event. |
| 2077 | pd_params_.expected_exit_status = UpdateStatus::REPORTING_ERROR_EVENT; |
| 2078 | // THEN install indication should be false. |
| 2079 | |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 2080 | // THEN update_engine should not call update completion. |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 2081 | // THEN expect critical actions of |ScheduleErrorEventAction()|. |
| 2082 | EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))).Times(1); |
| 2083 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 2084 | // THEN |ScheduleUpdates()| will be called next |ProcessingDone()| so skip. |
| 2085 | pd_params_.should_schedule_updates_be_called = false; |
| 2086 | |
| 2087 | TestProcessingDone(); |
| 2088 | } |
| 2089 | |
| 2090 | TEST_F(UpdateAttempterTest, ProcessingDoneInstallError) { |
| 2091 | // GIVEN an install finished. |
| 2092 | pd_params_.is_install = true; |
| 2093 | // GIVEN an action error occured. |
| 2094 | pd_params_.code = ErrorCode::kError; |
| 2095 | // GIVEN an event error is set. |
| 2096 | attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 2097 | OmahaEvent::kResultError, |
| 2098 | ErrorCode::kError)); |
| 2099 | |
| 2100 | // THEN indicate a error event. |
| 2101 | pd_params_.expected_exit_status = UpdateStatus::REPORTING_ERROR_EVENT; |
| 2102 | // THEN install indication should be false. |
| 2103 | |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 2104 | // THEN update_engine should not call install completion. |
Jae Hoon Kim | ed3fcc0 | 2019-07-11 14:35:38 -0700 | [diff] [blame] | 2105 | // THEN expect critical actions of |ScheduleErrorEventAction()|. |
| 2106 | EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))).Times(1); |
| 2107 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 2108 | // THEN |ScheduleUpdates()| will be called next |ProcessingDone()| so skip. |
| 2109 | pd_params_.should_schedule_updates_be_called = false; |
| 2110 | |
| 2111 | TestProcessingDone(); |
| 2112 | } |
| 2113 | |
Saurabh Nijhara | 43d7adc | 2020-11-06 16:13:02 +0100 | [diff] [blame] | 2114 | TEST_F(UpdateAttempterTest, QuickFixTokenWhenDeviceIsEnterpriseEnrolled) { |
| 2115 | attempter_.CalculateUpdateParams({.quick_fix_build_token = "token"}); |
| 2116 | EXPECT_EQ("token", |
| 2117 | FakeSystemState::Get()->request_params()->autoupdate_token()); |
Amr Aboelkher | 21ac996 | 2019-05-15 14:50:05 +0200 | [diff] [blame] | 2118 | |
Saurabh Nijhara | 43d7adc | 2020-11-06 16:13:02 +0100 | [diff] [blame] | 2119 | attempter_.CalculateUpdateParams({}); |
| 2120 | EXPECT_TRUE( |
| 2121 | FakeSystemState::Get()->request_params()->autoupdate_token().empty()); |
Askar Aitzhan | 570ca87 | 2019-04-24 11:16:12 +0200 | [diff] [blame] | 2122 | } |
| 2123 | |
Jae Hoon Kim | 75daa38 | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 2124 | TEST_F(UpdateAttempterTest, ScheduleUpdateSpamHandlerTest) { |
| 2125 | EXPECT_CALL(mock_update_manager_, AsyncPolicyRequestUpdateCheckAllowed(_, _)) |
| 2126 | .Times(1); |
| 2127 | EXPECT_TRUE(attempter_.ScheduleUpdates()); |
| 2128 | // Now there is an update scheduled which means that all subsequent |
| 2129 | // |ScheduleUpdates()| should fail. |
| 2130 | EXPECT_FALSE(attempter_.ScheduleUpdates()); |
| 2131 | EXPECT_FALSE(attempter_.ScheduleUpdates()); |
| 2132 | EXPECT_FALSE(attempter_.ScheduleUpdates()); |
| 2133 | } |
| 2134 | |
| 2135 | // Critical tests to always make sure that an update is scheduled. The following |
| 2136 | // unittest(s) try and cover the correctness in synergy between |
| 2137 | // |UpdateAttempter| and |UpdateManager|. Also it is good to remember the |
| 2138 | // actions that happen in the flow when |UpdateAttempter| get callbacked on |
| 2139 | // |OnUpdateScheduled()| -> (various cases which leads to) -> |ProcessingDone()| |
| 2140 | void UpdateAttempterTest::TestOnUpdateScheduled() { |
| 2141 | // Setup |
| 2142 | attempter_.SetWaitingForScheduledCheck(true); |
| 2143 | attempter_.DisableUpdate(); |
| 2144 | attempter_.DisableScheduleUpdates(); |
| 2145 | |
| 2146 | // Invocation |
| 2147 | attempter_.OnUpdateScheduled(ous_params_.status, ous_params_.params); |
| 2148 | |
| 2149 | // Verify |
| 2150 | EXPECT_EQ(ous_params_.exit_status, attempter_.status()); |
| 2151 | EXPECT_EQ(ous_params_.should_schedule_updates_be_called, |
| 2152 | attempter_.WasScheduleUpdatesCalled()); |
| 2153 | EXPECT_EQ(ous_params_.should_update_be_called, attempter_.WasUpdateCalled()); |
| 2154 | } |
| 2155 | |
| 2156 | TEST_F(UpdateAttempterTest, OnUpdatesScheduledFailed) { |
| 2157 | // GIVEN failed status. |
| 2158 | |
| 2159 | // THEN update should be scheduled. |
| 2160 | ous_params_.should_schedule_updates_be_called = true; |
| 2161 | |
| 2162 | TestOnUpdateScheduled(); |
| 2163 | } |
| 2164 | |
| 2165 | TEST_F(UpdateAttempterTest, OnUpdatesScheduledAskMeAgainLater) { |
| 2166 | // GIVEN ask me again later status. |
| 2167 | ous_params_.status = EvalStatus::kAskMeAgainLater; |
| 2168 | |
| 2169 | // THEN update should be scheduled. |
| 2170 | ous_params_.should_schedule_updates_be_called = true; |
| 2171 | |
| 2172 | TestOnUpdateScheduled(); |
| 2173 | } |
| 2174 | |
| 2175 | TEST_F(UpdateAttempterTest, OnUpdatesScheduledContinue) { |
| 2176 | // GIVEN continue status. |
| 2177 | ous_params_.status = EvalStatus::kContinue; |
| 2178 | |
| 2179 | // THEN update should be scheduled. |
| 2180 | ous_params_.should_schedule_updates_be_called = true; |
| 2181 | |
| 2182 | TestOnUpdateScheduled(); |
| 2183 | } |
| 2184 | |
| 2185 | TEST_F(UpdateAttempterTest, OnUpdatesScheduledSucceededButUpdateDisabledFails) { |
| 2186 | // GIVEN updates disabled. |
| 2187 | ous_params_.params = {.updates_enabled = false}; |
| 2188 | // GIVEN succeeded status. |
| 2189 | ous_params_.status = EvalStatus::kSucceeded; |
| 2190 | |
| 2191 | // THEN update should not be scheduled. |
| 2192 | |
| 2193 | TestOnUpdateScheduled(); |
| 2194 | } |
| 2195 | |
| 2196 | TEST_F(UpdateAttempterTest, OnUpdatesScheduledSucceeded) { |
| 2197 | // GIVEN updates enabled. |
| 2198 | ous_params_.params = {.updates_enabled = true}; |
| 2199 | // GIVEN succeeded status. |
| 2200 | ous_params_.status = EvalStatus::kSucceeded; |
| 2201 | |
| 2202 | // THEN update should be called indicating status change. |
| 2203 | ous_params_.exit_status = UpdateStatus::CHECKING_FOR_UPDATE; |
| 2204 | ous_params_.should_update_be_called = true; |
| 2205 | |
| 2206 | TestOnUpdateScheduled(); |
| 2207 | } |
| 2208 | |
Amin Hassani | 9be122e | 2019-08-29 09:20:12 -0700 | [diff] [blame] | 2209 | TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusDefault) { |
| 2210 | UpdateEngineStatus status; |
| 2211 | attempter_.GetStatus(&status); |
| 2212 | EXPECT_FALSE(status.is_enterprise_rollback); |
| 2213 | } |
| 2214 | |
| 2215 | TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusFalse) { |
| 2216 | attempter_.install_plan_.reset(new InstallPlan); |
| 2217 | attempter_.install_plan_->is_rollback = false; |
| 2218 | |
| 2219 | UpdateEngineStatus status; |
| 2220 | attempter_.GetStatus(&status); |
| 2221 | EXPECT_FALSE(status.is_enterprise_rollback); |
| 2222 | } |
| 2223 | |
| 2224 | TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusTrue) { |
| 2225 | attempter_.install_plan_.reset(new InstallPlan); |
| 2226 | attempter_.install_plan_->is_rollback = true; |
| 2227 | |
| 2228 | UpdateEngineStatus status; |
| 2229 | attempter_.GetStatus(&status); |
| 2230 | EXPECT_TRUE(status.is_enterprise_rollback); |
| 2231 | } |
| 2232 | |
Miriam Polzer | 0cf1acb | 2020-04-29 17:39:51 +0200 | [diff] [blame] | 2233 | TEST_F(UpdateAttempterTest, PowerwashInGetStatusDefault) { |
| 2234 | UpdateEngineStatus status; |
| 2235 | attempter_.GetStatus(&status); |
| 2236 | EXPECT_FALSE(status.will_powerwash_after_reboot); |
| 2237 | } |
| 2238 | |
| 2239 | TEST_F(UpdateAttempterTest, PowerwashInGetStatusTrueBecausePowerwashRequired) { |
| 2240 | attempter_.install_plan_.reset(new InstallPlan); |
| 2241 | attempter_.install_plan_->powerwash_required = true; |
| 2242 | |
| 2243 | UpdateEngineStatus status; |
| 2244 | attempter_.GetStatus(&status); |
| 2245 | EXPECT_TRUE(status.will_powerwash_after_reboot); |
| 2246 | } |
| 2247 | |
| 2248 | TEST_F(UpdateAttempterTest, PowerwashInGetStatusTrueBecauseRollback) { |
| 2249 | attempter_.install_plan_.reset(new InstallPlan); |
| 2250 | attempter_.install_plan_->is_rollback = true; |
| 2251 | |
| 2252 | UpdateEngineStatus status; |
| 2253 | attempter_.GetStatus(&status); |
| 2254 | EXPECT_TRUE(status.will_powerwash_after_reboot); |
| 2255 | } |
| 2256 | |
Jae Hoon Kim | 051627a | 2019-09-03 12:56:32 -0700 | [diff] [blame] | 2257 | TEST_F(UpdateAttempterTest, FutureEolTest) { |
| 2258 | EolDate eol_date = std::numeric_limits<int64_t>::max(); |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 2259 | EXPECT_TRUE(prefs_->SetString(kPrefsOmahaEolDate, EolDateToString(eol_date))); |
Jae Hoon Kim | 051627a | 2019-09-03 12:56:32 -0700 | [diff] [blame] | 2260 | UpdateEngineStatus status; |
| 2261 | attempter_.GetStatus(&status); |
| 2262 | EXPECT_EQ(eol_date, status.eol_date); |
| 2263 | } |
| 2264 | |
| 2265 | TEST_F(UpdateAttempterTest, PastEolTest) { |
| 2266 | EolDate eol_date = 1; |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 2267 | EXPECT_TRUE(prefs_->SetString(kPrefsOmahaEolDate, EolDateToString(eol_date))); |
Jae Hoon Kim | 051627a | 2019-09-03 12:56:32 -0700 | [diff] [blame] | 2268 | UpdateEngineStatus status; |
| 2269 | attempter_.GetStatus(&status); |
| 2270 | EXPECT_EQ(eol_date, status.eol_date); |
| 2271 | } |
| 2272 | |
Jae Hoon Kim | 2dfd35d | 2020-06-02 10:53:13 -0700 | [diff] [blame] | 2273 | TEST_F(UpdateAttempterTest, MissingEolTest) { |
Jae Hoon Kim | 2dfd35d | 2020-06-02 10:53:13 -0700 | [diff] [blame] | 2274 | UpdateEngineStatus status; |
| 2275 | attempter_.GetStatus(&status); |
| 2276 | EXPECT_EQ(kEolDateInvalid, status.eol_date); |
Jae Hoon Kim | 051627a | 2019-09-03 12:56:32 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2279 | TEST_F(UpdateAttempterTest, CalculateDlcParamsInstallTest) { |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2280 | string dlc_id = "dlc0"; |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2281 | attempter_.is_install_ = true; |
Amin Hassani | 2b68e6b | 2020-04-17 10:49:12 -0700 | [diff] [blame] | 2282 | attempter_.dlc_ids_ = {dlc_id}; |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2283 | attempter_.CalculateDlcParams(); |
| 2284 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2285 | OmahaRequestParams* params = FakeSystemState::Get()->request_params(); |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2286 | EXPECT_EQ(1, params->dlc_apps_params().count(params->GetDlcAppId(dlc_id))); |
| 2287 | OmahaRequestParams::AppParams dlc_app_params = |
| 2288 | params->dlc_apps_params().at(params->GetDlcAppId(dlc_id)); |
| 2289 | EXPECT_STREQ(dlc_id.c_str(), dlc_app_params.name.c_str()); |
| 2290 | EXPECT_EQ(false, dlc_app_params.send_ping); |
| 2291 | // When the DLC gets installed, a ping is not sent, therefore we don't store |
| 2292 | // the values sent by Omaha. |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2293 | auto last_active_key = PrefsInterface::CreateSubKey( |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2294 | {kDlcPrefsSubDir, dlc_id, kPrefsPingLastActive}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2295 | EXPECT_FALSE(FakeSystemState::Get()->prefs()->Exists(last_active_key)); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2296 | auto last_rollcall_key = PrefsInterface::CreateSubKey( |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2297 | {kDlcPrefsSubDir, dlc_id, kPrefsPingLastRollcall}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2298 | EXPECT_FALSE(FakeSystemState::Get()->prefs()->Exists(last_rollcall_key)); |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | TEST_F(UpdateAttempterTest, CalculateDlcParamsNoPrefFilesTest) { |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2302 | string dlc_id = "dlc0"; |
Amin Hassani | 2b68e6b | 2020-04-17 10:49:12 -0700 | [diff] [blame] | 2303 | EXPECT_CALL(mock_dlcservice_, GetDlcsToUpdate(_)) |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2304 | .WillOnce( |
| 2305 | DoAll(SetArgPointee<0>(std::vector<string>({dlc_id})), Return(true))); |
| 2306 | |
| 2307 | attempter_.is_install_ = false; |
| 2308 | attempter_.CalculateDlcParams(); |
| 2309 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2310 | OmahaRequestParams* params = FakeSystemState::Get()->request_params(); |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2311 | EXPECT_EQ(1, params->dlc_apps_params().count(params->GetDlcAppId(dlc_id))); |
| 2312 | OmahaRequestParams::AppParams dlc_app_params = |
| 2313 | params->dlc_apps_params().at(params->GetDlcAppId(dlc_id)); |
| 2314 | EXPECT_STREQ(dlc_id.c_str(), dlc_app_params.name.c_str()); |
| 2315 | |
| 2316 | EXPECT_EQ(true, dlc_app_params.send_ping); |
| 2317 | EXPECT_EQ(0, dlc_app_params.ping_active); |
| 2318 | EXPECT_EQ(-1, dlc_app_params.ping_date_last_active); |
| 2319 | EXPECT_EQ(-1, dlc_app_params.ping_date_last_rollcall); |
| 2320 | } |
| 2321 | |
| 2322 | TEST_F(UpdateAttempterTest, CalculateDlcParamsNonParseableValuesTest) { |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2323 | string dlc_id = "dlc0"; |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2324 | MemoryPrefs prefs; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2325 | FakeSystemState::Get()->set_prefs(&prefs); |
Amin Hassani | 2b68e6b | 2020-04-17 10:49:12 -0700 | [diff] [blame] | 2326 | EXPECT_CALL(mock_dlcservice_, GetDlcsToUpdate(_)) |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2327 | .WillOnce( |
| 2328 | DoAll(SetArgPointee<0>(std::vector<string>({dlc_id})), Return(true))); |
| 2329 | |
| 2330 | // Write non numeric values in the metadata files. |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2331 | auto active_key = |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2332 | PrefsInterface::CreateSubKey({kDlcPrefsSubDir, dlc_id, kPrefsPingActive}); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2333 | auto last_active_key = PrefsInterface::CreateSubKey( |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2334 | {kDlcPrefsSubDir, dlc_id, kPrefsPingLastActive}); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2335 | auto last_rollcall_key = PrefsInterface::CreateSubKey( |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2336 | {kDlcPrefsSubDir, dlc_id, kPrefsPingLastRollcall}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2337 | FakeSystemState::Get()->prefs()->SetString(active_key, "z2yz"); |
| 2338 | FakeSystemState::Get()->prefs()->SetString(last_active_key, "z2yz"); |
| 2339 | FakeSystemState::Get()->prefs()->SetString(last_rollcall_key, "z2yz"); |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2340 | attempter_.is_install_ = false; |
| 2341 | attempter_.CalculateDlcParams(); |
| 2342 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2343 | OmahaRequestParams* params = FakeSystemState::Get()->request_params(); |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2344 | EXPECT_EQ(1, params->dlc_apps_params().count(params->GetDlcAppId(dlc_id))); |
| 2345 | OmahaRequestParams::AppParams dlc_app_params = |
| 2346 | params->dlc_apps_params().at(params->GetDlcAppId(dlc_id)); |
| 2347 | EXPECT_STREQ(dlc_id.c_str(), dlc_app_params.name.c_str()); |
| 2348 | |
| 2349 | EXPECT_EQ(true, dlc_app_params.send_ping); |
| 2350 | EXPECT_EQ(0, dlc_app_params.ping_active); |
| 2351 | EXPECT_EQ(-2, dlc_app_params.ping_date_last_active); |
| 2352 | EXPECT_EQ(-2, dlc_app_params.ping_date_last_rollcall); |
| 2353 | } |
| 2354 | |
| 2355 | TEST_F(UpdateAttempterTest, CalculateDlcParamsValidValuesTest) { |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2356 | string dlc_id = "dlc0"; |
Amin Hassani | 2b68e6b | 2020-04-17 10:49:12 -0700 | [diff] [blame] | 2357 | EXPECT_CALL(mock_dlcservice_, GetDlcsToUpdate(_)) |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2358 | .WillOnce( |
| 2359 | DoAll(SetArgPointee<0>(std::vector<string>({dlc_id})), Return(true))); |
| 2360 | |
| 2361 | // Write numeric values in the metadata files. |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2362 | auto active_key = |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2363 | PrefsInterface::CreateSubKey({kDlcPrefsSubDir, dlc_id, kPrefsPingActive}); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2364 | auto last_active_key = PrefsInterface::CreateSubKey( |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2365 | {kDlcPrefsSubDir, dlc_id, kPrefsPingLastActive}); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2366 | auto last_rollcall_key = PrefsInterface::CreateSubKey( |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2367 | {kDlcPrefsSubDir, dlc_id, kPrefsPingLastRollcall}); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2368 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2369 | FakeSystemState::Get()->prefs()->SetInt64(active_key, 1); |
| 2370 | FakeSystemState::Get()->prefs()->SetInt64(last_active_key, 78); |
| 2371 | FakeSystemState::Get()->prefs()->SetInt64(last_rollcall_key, 99); |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2372 | attempter_.is_install_ = false; |
| 2373 | attempter_.CalculateDlcParams(); |
| 2374 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2375 | OmahaRequestParams* params = FakeSystemState::Get()->request_params(); |
Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 2376 | EXPECT_EQ(1, params->dlc_apps_params().count(params->GetDlcAppId(dlc_id))); |
| 2377 | OmahaRequestParams::AppParams dlc_app_params = |
| 2378 | params->dlc_apps_params().at(params->GetDlcAppId(dlc_id)); |
| 2379 | EXPECT_STREQ(dlc_id.c_str(), dlc_app_params.name.c_str()); |
| 2380 | |
| 2381 | EXPECT_EQ(true, dlc_app_params.send_ping); |
| 2382 | EXPECT_EQ(1, dlc_app_params.ping_active); |
| 2383 | EXPECT_EQ(78, dlc_app_params.ping_date_last_active); |
| 2384 | EXPECT_EQ(99, dlc_app_params.ping_date_last_rollcall); |
| 2385 | } |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2386 | |
| 2387 | TEST_F(UpdateAttempterTest, CalculateDlcParamsRemoveStaleMetadata) { |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2388 | string dlc_id = "dlc0"; |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2389 | auto active_key = |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2390 | PrefsInterface::CreateSubKey({kDlcPrefsSubDir, dlc_id, kPrefsPingActive}); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2391 | auto last_active_key = PrefsInterface::CreateSubKey( |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2392 | {kDlcPrefsSubDir, dlc_id, kPrefsPingLastActive}); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2393 | auto last_rollcall_key = PrefsInterface::CreateSubKey( |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2394 | {kDlcPrefsSubDir, dlc_id, kPrefsPingLastRollcall}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2395 | FakeSystemState::Get()->prefs()->SetInt64(active_key, kPingInactiveValue); |
| 2396 | FakeSystemState::Get()->prefs()->SetInt64(last_active_key, 0); |
| 2397 | FakeSystemState::Get()->prefs()->SetInt64(last_rollcall_key, 0); |
| 2398 | EXPECT_TRUE(FakeSystemState::Get()->prefs()->Exists(active_key)); |
| 2399 | EXPECT_TRUE(FakeSystemState::Get()->prefs()->Exists(last_active_key)); |
| 2400 | EXPECT_TRUE(FakeSystemState::Get()->prefs()->Exists(last_rollcall_key)); |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2401 | |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2402 | attempter_.dlc_ids_ = {dlc_id}; |
| 2403 | attempter_.is_install_ = true; |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2404 | attempter_.CalculateDlcParams(); |
| 2405 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2406 | EXPECT_FALSE(FakeSystemState::Get()->prefs()->Exists(last_active_key)); |
| 2407 | EXPECT_FALSE(FakeSystemState::Get()->prefs()->Exists(last_rollcall_key)); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2408 | // Active key is set on install. |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2409 | EXPECT_TRUE(FakeSystemState::Get()->prefs()->Exists(active_key)); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2410 | int64_t temp_int; |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2411 | EXPECT_TRUE(FakeSystemState::Get()->prefs()->GetInt64(active_key, &temp_int)); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2412 | EXPECT_EQ(temp_int, kPingActiveValue); |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | TEST_F(UpdateAttempterTest, SetDlcActiveValue) { |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2416 | string dlc_id = "dlc0"; |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2417 | attempter_.SetDlcActiveValue(true, dlc_id); |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2418 | int64_t temp_int; |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2419 | auto active_key = |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2420 | PrefsInterface::CreateSubKey({kDlcPrefsSubDir, dlc_id, kPrefsPingActive}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2421 | EXPECT_TRUE(FakeSystemState::Get()->prefs()->Exists(active_key)); |
| 2422 | EXPECT_TRUE(FakeSystemState::Get()->prefs()->GetInt64(active_key, &temp_int)); |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2423 | EXPECT_EQ(temp_int, kPingActiveValue); |
| 2424 | } |
| 2425 | |
| 2426 | TEST_F(UpdateAttempterTest, SetDlcInactive) { |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2427 | string dlc_id = "dlc0"; |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2428 | auto sub_keys = { |
| 2429 | kPrefsPingActive, kPrefsPingLastActive, kPrefsPingLastRollcall}; |
| 2430 | for (auto& sub_key : sub_keys) { |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2431 | auto key = PrefsInterface::CreateSubKey({kDlcPrefsSubDir, dlc_id, sub_key}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2432 | FakeSystemState::Get()->prefs()->SetInt64(key, 1); |
| 2433 | EXPECT_TRUE(FakeSystemState::Get()->prefs()->Exists(key)); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2434 | } |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2435 | attempter_.SetDlcActiveValue(false, dlc_id); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2436 | for (auto& sub_key : sub_keys) { |
Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 2437 | auto key = PrefsInterface::CreateSubKey({kDlcPrefsSubDir, dlc_id, sub_key}); |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 2438 | EXPECT_FALSE(FakeSystemState::Get()->prefs()->Exists(key)); |
Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 2439 | } |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Jae Hoon Kim | 7fdfbf1 | 2020-04-10 18:15:50 -0700 | [diff] [blame] | 2442 | TEST_F(UpdateAttempterTest, GetSuccessfulDlcIds) { |
| 2443 | auto dlc_1 = "1", dlc_2 = "2", dlc_3 = "3"; |
| 2444 | attempter_.omaha_request_params_->set_dlc_apps_params( |
| 2445 | {{dlc_1, {.name = dlc_1, .updated = false}}, |
| 2446 | {dlc_2, {.name = dlc_2}}, |
| 2447 | {dlc_3, {.name = dlc_3, .updated = false}}}); |
| 2448 | EXPECT_THAT(attempter_.GetSuccessfulDlcIds(), ElementsAre(dlc_2)); |
| 2449 | } |
| 2450 | |
Vyshu | 6e8a37b | 2020-11-25 10:31:00 -0800 | [diff] [blame] | 2451 | TEST_F(UpdateAttempterTest, MoveToPrefs) { |
| 2452 | string key1 = kPrefsLastActivePingDay; |
| 2453 | string key2 = kPrefsPingLastRollcall; |
| 2454 | |
| 2455 | FakePrefs fake_prefs; |
| 2456 | EXPECT_TRUE(fake_prefs.SetString(key2, "current-rollcall")); |
| 2457 | FakeSystemState::Get()->set_prefs(&fake_prefs); |
| 2458 | |
| 2459 | FakePrefs powerwash_safe_prefs; |
| 2460 | EXPECT_TRUE(powerwash_safe_prefs.SetString(key1, "powerwash-last-active")); |
| 2461 | EXPECT_TRUE(powerwash_safe_prefs.SetString(key2, "powerwash-last-rollcall")); |
| 2462 | FakeSystemState::Get()->set_powerwash_safe_prefs(&powerwash_safe_prefs); |
| 2463 | |
| 2464 | attempter_.Init(); |
| 2465 | attempter_.MoveToPrefs({key1, key2}); |
| 2466 | |
| 2467 | string pref_value_1; |
| 2468 | fake_prefs.GetString(key1, &pref_value_1); |
| 2469 | EXPECT_EQ(pref_value_1, "powerwash-last-active"); |
| 2470 | // Do not overwrite if value already exists. |
| 2471 | string pref_value_2; |
| 2472 | fake_prefs.GetString(key2, &pref_value_2); |
| 2473 | EXPECT_EQ(pref_value_2, "current-rollcall"); |
| 2474 | |
| 2475 | // Make sure keys are deleted from powerwash safe prefs regardless of whether |
| 2476 | // they are written to prefs. |
| 2477 | EXPECT_FALSE(FakeSystemState::Get()->powerwash_safe_prefs()->Exists(key1)); |
| 2478 | EXPECT_FALSE(FakeSystemState::Get()->powerwash_safe_prefs()->Exists(key2)); |
| 2479 | } |
| 2480 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 2481 | } // namespace chromeos_update_engine |