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