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