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