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