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