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