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