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