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