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