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