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