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