blob: 4aff897b9d5f2ce8abcc73d977e79d87f7bdecfb [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Petkovf42cc1c2010-09-01 09:03:02 -070016
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/update_attempter.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Jae Hoon Kim051627a2019-09-03 12:56:32 -070021#include <limits>
Ben Chan02f7c1d2014-10-18 15:18:02 -070022#include <memory>
Jae Hoon Kimedb65502019-06-14 11:52:17 -070023#include <unordered_set>
Ben Chan02f7c1d2014-10-18 15:18:02 -070024
Ben Chan06c76a42014-09-05 08:21:06 -070025#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070026#include <base/message_loop/message_loop.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070027#include <brillo/message_loops/base_message_loop.h>
28#include <brillo/message_loops/message_loop.h>
29#include <brillo/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020031#include <policy/libpolicy.h>
32#include <policy/mock_device_policy.h>
Marton Hunyadye58bddb2018-04-10 20:27:26 +020033#include <policy/mock_libpolicy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070034
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070035#include "update_engine/common/constants.h"
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080036#include "update_engine/common/dlcservice_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/fake_clock.h"
38#include "update_engine/common/fake_prefs.h"
Alex Deymo14fd1ec2016-02-24 22:03:57 -080039#include "update_engine/common/mock_action.h"
40#include "update_engine/common/mock_action_processor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080041#include "update_engine/common/mock_http_fetcher.h"
42#include "update_engine/common/mock_prefs.h"
43#include "update_engine/common/platform_constants.h"
44#include "update_engine/common/prefs.h"
45#include "update_engine/common/test_utils.h"
46#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070047#include "update_engine/fake_system_state.h"
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070048#include "update_engine/libcurl_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070049#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050#include "update_engine/mock_payload_state.h"
Aaron Wood9321f502017-09-07 11:18:54 -070051#include "update_engine/mock_service_observer.h"
Jae Hoon Kim051627a2019-09-03 12:56:32 -070052#include "update_engine/omaha_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080053#include "update_engine/payload_consumer/filesystem_verifier_action.h"
54#include "update_engine/payload_consumer/install_plan.h"
55#include "update_engine/payload_consumer/payload_constants.h"
56#include "update_engine/payload_consumer/postinstall_runner_action.h"
Amin Hassani0882a512018-04-05 16:25:44 -070057#include "update_engine/update_boot_flags_action.h"
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -070058#include "update_engine/update_manager/mock_update_manager.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070059
David Zeuthen985b1122013-10-09 12:13:15 -070060using base::Time;
61using base::TimeDelta;
Aaron Woodbf5a2522017-10-04 10:58:36 -070062using chromeos_update_manager::EvalStatus;
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -070063using chromeos_update_manager::MockUpdateManager;
Adolfo Victoria497044c2018-07-18 07:51:42 -070064using chromeos_update_manager::StagingSchedule;
Aaron Woodbf5a2522017-10-04 10:58:36 -070065using chromeos_update_manager::UpdateCheckParams;
Adolfo Victoria497044c2018-07-18 07:51:42 -070066using policy::DevicePolicy;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070067using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070068using std::unique_ptr;
Jae Hoon Kimedb65502019-06-14 11:52:17 -070069using std::unordered_set;
Xiaochu Liu88d90382018-08-29 16:09:11 -070070using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070071using testing::_;
Darin Petkov36275772010-10-01 11:40:57 -070072using testing::DoAll;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070073using testing::Field;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070074using testing::InSequence;
Jae Hoon Kimedb65502019-06-14 11:52:17 -070075using testing::Invoke;
Darin Petkov2dd01092010-10-08 15:43:05 -070076using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080077using testing::NiceMock;
Amin Hassanid3f4bea2018-04-30 14:52:40 -070078using testing::Pointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070079using testing::Property;
80using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070081using testing::ReturnPointee;
Marton Hunyadye58bddb2018-04-10 20:27:26 +020082using testing::ReturnRef;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080083using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070084using testing::SetArgPointee;
Aaron Woodbf5a2522017-10-04 10:58:36 -070085using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070086using update_engine::UpdateEngineStatus;
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -070087using update_engine::UpdateStatus;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070088
89namespace chromeos_update_engine {
90
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080091namespace {
92
Jae Hoon Kimc437ea52019-07-11 11:20:38 -070093const UpdateStatus kNonIdleUpdateStatuses[] = {
94 UpdateStatus::CHECKING_FOR_UPDATE,
95 UpdateStatus::UPDATE_AVAILABLE,
96 UpdateStatus::DOWNLOADING,
97 UpdateStatus::VERIFYING,
98 UpdateStatus::FINALIZING,
99 UpdateStatus::UPDATED_NEED_REBOOT,
100 UpdateStatus::REPORTING_ERROR_EVENT,
101 UpdateStatus::ATTEMPTING_ROLLBACK,
102 UpdateStatus::DISABLED,
103 UpdateStatus::NEED_PERMISSION_TO_UPDATE,
104};
105
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700106struct CheckForUpdateTestParams {
107 // Setups + Inputs:
108 UpdateStatus status = UpdateStatus::IDLE;
109 string app_version = "fake_app_version";
110 string omaha_url = "fake_omaha_url";
111 UpdateAttemptFlags flags = UpdateAttemptFlags::kNone;
112 bool is_official_build = true;
113 bool are_dev_features_enabled = false;
114
115 // Expects:
116 string expected_forced_app_version = "";
117 string expected_forced_omaha_url = "";
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -0700118 bool should_schedule_updates_be_called = true;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700119 bool expected_result = true;
120};
121
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700122struct OnUpdateScheduledTestParams {
123 // Setups + Inputs:
124 UpdateCheckParams params = {};
125 EvalStatus status = EvalStatus::kFailed;
126 // Expects:
127 UpdateStatus exit_status = UpdateStatus::IDLE;
128 bool should_schedule_updates_be_called = false;
129 bool should_update_be_called = false;
130};
131
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700132struct ProcessingDoneTestParams {
133 // Setups + Inputs:
134 bool is_install = false;
135 UpdateStatus status = UpdateStatus::CHECKING_FOR_UPDATE;
136 ActionProcessor* processor = nullptr;
137 ErrorCode code = ErrorCode::kSuccess;
138
139 // Expects:
140 const bool kExpectedIsInstall = false;
141 bool should_schedule_updates_be_called = true;
142 UpdateStatus expected_exit_status = UpdateStatus::IDLE;
143};
144
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800145class MockDlcService : public DlcServiceInterface {
146 public:
147 MOCK_METHOD1(GetInstalled, bool(vector<string>*));
148};
149
150} // namespace
151
Marton Hunyadya0302682018-05-16 18:52:13 +0200152const char kRollbackVersion[] = "10575.39.2";
153
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700154// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -0700155// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700156// methods.
157class UpdateAttempterUnderTest : public UpdateAttempter {
158 public:
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700159 explicit UpdateAttempterUnderTest(SystemState* system_state)
160 : UpdateAttempter(system_state, nullptr) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700161
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700162 void Update(const std::string& app_version,
163 const std::string& omaha_url,
164 const std::string& target_channel,
165 const std::string& target_version_prefix,
166 bool rollback_allowed,
167 bool rollback_data_save_requested,
168 int rollback_allowed_milestones,
169 bool obey_proxies,
170 bool interactive) override {
171 update_called_ = true;
172 if (do_update_) {
173 UpdateAttempter::Update(app_version,
174 omaha_url,
175 target_channel,
176 target_version_prefix,
177 rollback_allowed,
178 rollback_data_save_requested,
179 rollback_allowed_milestones,
180 obey_proxies,
181 interactive);
182 return;
183 }
184 LOG(INFO) << "[TEST] Update() disabled.";
185 status_ = UpdateStatus::CHECKING_FOR_UPDATE;
186 }
187
188 void DisableUpdate() { do_update_ = false; }
189
190 bool WasUpdateCalled() const { return update_called_; }
191
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700192 // Wrap the update scheduling method, allowing us to opt out of scheduled
193 // updates for testing purposes.
Xiaochu Liu88d90382018-08-29 16:09:11 -0700194 bool ScheduleUpdates() override {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700195 schedule_updates_called_ = true;
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700196 if (do_schedule_updates_)
197 return UpdateAttempter::ScheduleUpdates();
198 LOG(INFO) << "[TEST] Update scheduling disabled.";
199 waiting_for_scheduled_check_ = true;
Xiaochu Liu88d90382018-08-29 16:09:11 -0700200 return true;
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700201 }
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700202
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700203 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
204
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700205 // Indicates whether |ScheduleUpdates()| was called.
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700206 bool WasScheduleUpdatesCalled() const { return schedule_updates_called_; }
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700207
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700208 // Need to expose following private members of |UpdateAttempter| for tests.
209 const string& forced_app_version() const { return forced_app_version_; }
Alex Deymo60ca1a72015-06-18 18:19:15 -0700210 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -0800211
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700212 // Need to expose |waiting_for_scheduled_check_| for testing.
213 void SetWaitingForScheduledCheck(bool waiting) {
214 waiting_for_scheduled_check_ = waiting;
215 }
216
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700217 private:
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700218 // Used for overrides of |Update()|.
219 bool update_called_ = false;
220 bool do_update_ = true;
221
222 // Used for overrides of |ScheduleUpdates()|.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700223 bool schedule_updates_called_ = false;
224 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700225};
226
227class UpdateAttempterTest : public ::testing::Test {
228 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700229 UpdateAttempterTest()
Daniel Erate5f6f252017-04-20 12:09:58 -0600230 : certificate_checker_(fake_system_state_.mock_prefs(),
Alex Deymo33e91e72015-12-01 18:26:08 -0300231 &openssl_wrapper_) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700232 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700233 fake_system_state_.set_connection_manager(&mock_connection_manager);
234 fake_system_state_.set_update_attempter(&attempter_);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800235 fake_system_state_.set_dlcservice(&mock_dlcservice_);
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -0700236 fake_system_state_.set_update_manager(&mock_update_manager_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700237 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700238
Alex Deymo33e91e72015-12-01 18:26:08 -0300239 certificate_checker_.Init();
240
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800241 attempter_.set_forced_update_pending_callback(
242 new base::Callback<void(bool, bool)>(base::Bind([](bool, bool) {})));
Sen Jiange67bb5b2016-06-20 15:53:56 -0700243 // Finish initializing the attempter.
Gilad Arnold1f847232014-04-07 12:07:49 -0700244 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -0700245 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700246
Alex Deymo610277e2014-11-11 21:18:11 -0800247 void SetUp() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700248 EXPECT_NE(nullptr, attempter_.system_state_);
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700249 EXPECT_NE(nullptr, attempter_.system_state_->update_manager());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700250 EXPECT_EQ(0, attempter_.http_response_code_);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700251 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700252 EXPECT_EQ(0.0, attempter_.download_progress_);
253 EXPECT_EQ(0, attempter_.last_checked_time_);
254 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700255 EXPECT_EQ(0ULL, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800256 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700257 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700258 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700259
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700260 // Setup store/load semantics of P2P properties via the mock |PayloadState|.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700261 actual_using_p2p_for_downloading_ = false;
262 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
263 SetUsingP2PForDownloading(_))
264 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
265 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
266 GetUsingP2PForDownloading())
267 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
268 actual_using_p2p_for_sharing_ = false;
269 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
270 SetUsingP2PForSharing(_))
271 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
272 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
273 GetUsingP2PForDownloading())
274 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700275 }
276
Alex Deymo60ca1a72015-06-18 18:19:15 -0700277 public:
278 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200279
Alex Deymo60ca1a72015-06-18 18:19:15 -0700280 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800281 void UpdateTestStart();
282 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700283 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700284 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700285 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700286 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700287 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700288 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700289 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700290 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700291 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700292 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700293 void P2PEnabledHousekeepingFailsStart();
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700294 void SessionIdTestChange();
295 void SessionIdTestEnforceEmptyStrPingOmaha();
296 void SessionIdTestConsistencyInUpdateFlow();
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700297 void SessionIdTestInDownloadAction();
Amr Aboelkher21ac9962019-05-15 14:50:05 +0200298 void UpdateToQuickFixBuildStart(bool set_token);
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200299 void ResetRollbackHappenedStart(bool is_consumer,
300 bool is_policy_available,
301 bool expected_reset);
Adolfo Victoria497044c2018-07-18 07:51:42 -0700302 // Staging related callbacks.
303 void SetUpStagingTest(const StagingSchedule& schedule, FakePrefs* prefs);
304 void CheckStagingOff();
305 void StagingSetsPrefsAndTurnsOffScatteringStart();
306 void StagingOffIfInteractiveStart();
307 void StagingOffIfOobeStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700308
Gilad Arnold74b5f552014-10-07 08:17:16 -0700309 bool actual_using_p2p_for_downloading() {
310 return actual_using_p2p_for_downloading_;
311 }
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800312 bool actual_using_p2p_for_sharing() { return actual_using_p2p_for_sharing_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700313
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700314 // |CheckForUpdate()| related member functions.
315 void TestCheckForUpdate();
316
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700317 // |OnUpdateScheduled()| related member functions.
318 void TestOnUpdateScheduled();
319
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700320 // |ProcessingDone()| related member functions.
321 void TestProcessingDone();
322
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700323 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700324 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700325
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700326 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700327 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300328 OpenSSLWrapper openssl_wrapper_;
329 CertificateChecker certificate_checker_;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800330 MockDlcService mock_dlcservice_;
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -0700331 MockUpdateManager mock_update_manager_;
Alex Deymo30534502015-07-20 15:06:33 -0700332
Alex Deymo8427b4a2014-11-05 14:00:32 -0800333 NiceMock<MockActionProcessor>* processor_;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700334 NiceMock<MockPrefs>*
335 prefs_; // Shortcut to |fake_system_state_->mock_prefs()|.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800336 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700337
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700338 // |CheckForUpdate()| test params.
339 CheckForUpdateTestParams cfu_params_;
340
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700341 // |OnUpdateScheduled()| test params.
342 OnUpdateScheduledTestParams ous_params_;
343
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700344 // |ProcessingDone()| test params.
345 ProcessingDoneTestParams pd_params_;
346
Gilad Arnold74b5f552014-10-07 08:17:16 -0700347 bool actual_using_p2p_for_downloading_;
348 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700349};
350
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700351void UpdateAttempterTest::TestCheckForUpdate() {
352 // Setup
353 attempter_.status_ = cfu_params_.status;
354 fake_system_state_.fake_hardware()->SetIsOfficialBuild(
355 cfu_params_.is_official_build);
356 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(
357 cfu_params_.are_dev_features_enabled);
358
359 // Invocation
360 EXPECT_EQ(
361 cfu_params_.expected_result,
362 attempter_.CheckForUpdate(
363 cfu_params_.app_version, cfu_params_.omaha_url, cfu_params_.flags));
364
365 // Verify
366 EXPECT_EQ(cfu_params_.expected_forced_app_version,
367 attempter_.forced_app_version());
368 EXPECT_EQ(cfu_params_.expected_forced_omaha_url,
369 attempter_.forced_omaha_url());
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -0700370 EXPECT_EQ(cfu_params_.should_schedule_updates_be_called,
371 attempter_.WasScheduleUpdatesCalled());
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700372}
373
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700374void UpdateAttempterTest::TestProcessingDone() {
375 // Setup
376 attempter_.DisableScheduleUpdates();
377 attempter_.is_install_ = pd_params_.is_install;
378 attempter_.status_ = pd_params_.status;
379
380 // Invocation
381 attempter_.ProcessingDone(pd_params_.processor, pd_params_.code);
382
383 // Verify
384 EXPECT_EQ(pd_params_.kExpectedIsInstall, attempter_.is_install_);
385 EXPECT_EQ(pd_params_.should_schedule_updates_be_called,
386 attempter_.WasScheduleUpdatesCalled());
387 EXPECT_EQ(pd_params_.expected_exit_status, attempter_.status_);
388}
389
Alex Deymo60ca1a72015-06-18 18:19:15 -0700390void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700391 loop_.PostTask(
392 FROM_HERE,
393 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
394 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700395}
396
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700397void UpdateAttempterTest::SessionIdTestChange() {
398 EXPECT_NE(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
399 const auto old_session_id = attempter_.session_id_;
400 attempter_.Update("", "", "", "", false, false, 0, false, false);
401 EXPECT_NE(old_session_id, attempter_.session_id_);
402 ScheduleQuitMainLoop();
403}
404
405TEST_F(UpdateAttempterTest, SessionIdTestChange) {
406 loop_.PostTask(FROM_HERE,
407 base::Bind(&UpdateAttempterTest::SessionIdTestChange,
408 base::Unretained(this)));
409 loop_.Run();
410}
411
412void UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha() {
413 // The |session_id_| should not be changed and should remain as an empty
414 // string when |status_| is |UPDATED_NEED_REBOOT| (only for consistency)
415 // and |PingOmaha()| is called.
416 attempter_.DisableScheduleUpdates();
417 attempter_.status_ = UpdateStatus::UPDATED_NEED_REBOOT;
418 const auto old_session_id = attempter_.session_id_;
419 auto CheckIfEmptySessionId = [](AbstractAction* aa) {
420 if (aa->Type() == OmahaRequestAction::StaticType()) {
421 EXPECT_TRUE(static_cast<OmahaRequestAction*>(aa)->session_id_.empty());
422 }
423 };
424 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
425 .WillRepeatedly(Invoke(CheckIfEmptySessionId));
426 EXPECT_CALL(*processor_, StartProcessing());
427 attempter_.PingOmaha();
428 EXPECT_EQ(old_session_id, attempter_.session_id_);
429 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status_);
430 ScheduleQuitMainLoop();
431}
432
433TEST_F(UpdateAttempterTest, SessionIdTestEnforceEmptyStrPingOmaha) {
434 loop_.PostTask(
435 FROM_HERE,
436 base::Bind(&UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha,
437 base::Unretained(this)));
438 loop_.Run();
439}
440
441void UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow() {
442 // All session IDs passed into |OmahaRequestActions| should be enforced to
443 // have the same value in |BuildUpdateActions()|.
444 unordered_set<string> session_ids;
445 // Gather all the session IDs being passed to |OmahaRequestActions|.
446 auto CheckSessionId = [&session_ids](AbstractAction* aa) {
447 if (aa->Type() == OmahaRequestAction::StaticType())
448 session_ids.insert(static_cast<OmahaRequestAction*>(aa)->session_id_);
449 };
450 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
451 .WillRepeatedly(Invoke(CheckSessionId));
452 attempter_.BuildUpdateActions(false);
453 // Validate that all the session IDs are the same.
454 EXPECT_EQ(1, session_ids.size());
455 ScheduleQuitMainLoop();
456}
457
458TEST_F(UpdateAttempterTest, SessionIdTestConsistencyInUpdateFlow) {
459 loop_.PostTask(
460 FROM_HERE,
461 base::Bind(&UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow,
462 base::Unretained(this)));
463 loop_.Run();
464}
465
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700466void UpdateAttempterTest::SessionIdTestInDownloadAction() {
467 // The session ID passed into |DownloadAction|'s |LibcurlHttpFetcher| should
468 // be enforced to be included in the HTTP header as X-Goog-Update-SessionId.
469 string header_value;
470 auto CheckSessionIdInDownloadAction = [&header_value](AbstractAction* aa) {
471 if (aa->Type() == DownloadAction::StaticType()) {
472 DownloadAction* da = static_cast<DownloadAction*>(aa);
473 EXPECT_TRUE(da->http_fetcher()->GetHeader(kXGoogleUpdateSessionId,
474 &header_value));
475 }
476 };
477 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
478 .WillRepeatedly(Invoke(CheckSessionIdInDownloadAction));
479 attempter_.BuildUpdateActions(false);
480 // Validate that X-Goog-Update_SessionId is set correctly in HTTP Header.
481 EXPECT_EQ(attempter_.session_id_, header_value);
482 ScheduleQuitMainLoop();
483}
484
485TEST_F(UpdateAttempterTest, SessionIdTestInDownloadAction) {
486 loop_.PostTask(FROM_HERE,
487 base::Bind(&UpdateAttempterTest::SessionIdTestInDownloadAction,
488 base::Unretained(this)));
489 loop_.Run();
490}
491
Darin Petkov1b003102010-11-30 10:18:36 -0800492TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700493 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800494 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700495 DownloadAction action(prefs_,
496 nullptr,
497 nullptr,
498 nullptr,
499 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700500 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800501 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700502 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700503 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700504 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700505 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800506}
507
508TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800509 MockAction action;
510 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700511 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800512 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800513 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700514 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
515 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800516}
517
Aaron Wood9321f502017-09-07 11:18:54 -0700518TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
519 // Simple test case, where all the values match (nothing was skipped)
520 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
521 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
522 uint64_t bytes_received_1 = bytes_progressed_1;
523 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
524 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
525
526 double progress_1 =
527 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
528 double progress_2 =
529 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
530
531 EXPECT_EQ(0.0, attempter_.download_progress_);
532 // This is set via inspecting the InstallPlan payloads when the
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700533 // |OmahaResponseAction| is completed.
Aaron Wood9321f502017-09-07 11:18:54 -0700534 attempter_.new_payload_size_ = bytes_total;
535 NiceMock<MockServiceObserver> observer;
536 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700537 SendStatusUpdate(AllOf(
538 Field(&UpdateEngineStatus::progress, progress_1),
539 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
540 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700541 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700542 SendStatusUpdate(AllOf(
543 Field(&UpdateEngineStatus::progress, progress_2),
544 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
545 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700546 attempter_.AddObserver(&observer);
547 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
548 EXPECT_EQ(progress_1, attempter_.download_progress_);
549 // This iteration validates that a later set of updates to the variables are
550 // properly handled (so that |getStatus()| will return the same progress info
551 // as the callback is receiving.
552 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
553 EXPECT_EQ(progress_2, attempter_.download_progress_);
554}
555
556TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700557 // The transition into |UpdateStatus::DOWNLOADING| happens when the
Aaron Wood9321f502017-09-07 11:18:54 -0700558 // first bytes are received.
559 uint64_t bytes_progressed = 1024 * 1024; // 1MB
560 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
561 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
562 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
563 // This is set via inspecting the InstallPlan payloads when the
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700564 // |OmahaResponseAction| is completed.
Aaron Wood9321f502017-09-07 11:18:54 -0700565 attempter_.new_payload_size_ = bytes_total;
566 EXPECT_EQ(0.0, attempter_.download_progress_);
567 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700568 EXPECT_CALL(observer,
569 SendStatusUpdate(AllOf(
570 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
571 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700572 attempter_.AddObserver(&observer);
573 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
574 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
575}
576
577TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
578 // There is a special case to ensure that at 100% downloaded,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700579 // |download_progress_| is updated and broadcastest.
Aaron Wood9321f502017-09-07 11:18:54 -0700580 uint64_t bytes_progressed = 0; // ignored
581 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
582 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
583 attempter_.status_ = UpdateStatus::DOWNLOADING;
584 attempter_.new_payload_size_ = bytes_total;
585 EXPECT_EQ(0.0, attempter_.download_progress_);
586 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700587 EXPECT_CALL(observer,
588 SendStatusUpdate(AllOf(
589 Field(&UpdateEngineStatus::progress, 1.0),
590 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
591 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700592 attempter_.AddObserver(&observer);
593 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
594 EXPECT_EQ(1.0, attempter_.download_progress_);
595}
596
Darin Petkov1b003102010-11-30 10:18:36 -0800597TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700598 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800599 fetcher->FailTransfer(500); // Sets the HTTP response code.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800600 OmahaRequestAction action(
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700601 &fake_system_state_, nullptr, std::move(fetcher), false, "");
Darin Petkov1b003102010-11-30 10:18:36 -0800602 ObjectCollectorAction<OmahaResponse> collector_action;
603 BondActions(&action, &collector_action);
604 OmahaResponse response;
605 response.poll_interval = 234;
606 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800607 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700608 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800609 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700610 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800611 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700612 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800613}
614
Alex Deymo30534502015-07-20 15:06:33 -0700615TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700616 FakePrefs fake_prefs;
617 string boot_id;
618 EXPECT_TRUE(utils::GetBootId(&boot_id));
619 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
620 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700621 attempter_.Init();
622 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700623}
624
625TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700626 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700627 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700628
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700629 FakeSystemState fake_system_state;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800630 OmahaRequestAction omaha_request_action(
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700631 &fake_system_state, nullptr, nullptr, false, "");
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700632 EXPECT_EQ(ErrorCode::kOmahaRequestError,
633 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700634 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800635 EXPECT_EQ(
636 ErrorCode::kOmahaResponseHandlerError,
637 GetErrorCodeForAction(&omaha_response_handler_action, ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700638 FilesystemVerifierAction filesystem_verifier_action;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800639 EXPECT_EQ(
640 ErrorCode::kFilesystemVerifierError,
641 GetErrorCodeForAction(&filesystem_verifier_action, ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300642 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700643 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800644 EXPECT_EQ(
645 ErrorCode::kPostinstallRunnerError,
646 GetErrorCodeForAction(&postinstall_runner_action, ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800647 MockAction action_mock;
648 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700649 EXPECT_EQ(ErrorCode::kError,
650 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700651}
652
Darin Petkov36275772010-10-01 11:40:57 -0700653TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700654 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800655 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700656 .WillOnce(Return(false));
657 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700658 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800659 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800660 .WillOnce(
661 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
662 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700663 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700664 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800665 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800666 .WillOnce(
667 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
668 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700669 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700670 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800671 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700672 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700673 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700674}
675
676TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800677 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700678 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700679 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
680 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800681 .WillOnce(
682 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
683 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800684 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700685 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800686 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700687 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800688 EXPECT_CALL(*prefs_,
689 SetInt64(kPrefsDeltaUpdateFailures,
690 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
691 for (int i = 0; i < 4; i++)
Darin Petkov36275772010-10-01 11:40:57 -0700692 attempter_.MarkDeltaUpdateFailure();
693}
694
Darin Petkov1b003102010-11-30 10:18:36 -0800695TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
696 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
697 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700698 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800699 .Times(0);
700 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700701 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800702 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700703 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700704 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700705 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800706 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700707 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800708}
709
710TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
711 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700712 EnqueueAction(Pointee(Property(
713 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700714 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700715 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700716 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800717 attempter_.error_event_.reset(new OmahaEvent(
718 OmahaEvent::kTypeUpdateComplete, OmahaEvent::kResultError, err));
Darin Petkov1b003102010-11-30 10:18:36 -0800719 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700720 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800721}
722
Darin Petkove6ef2f82011-03-07 17:31:11 -0800723namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700724// Actions that will be built as part of an update check.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200725vector<string> GetUpdateActionTypes() {
726 return {OmahaRequestAction::StaticType(),
727 OmahaResponseHandlerAction::StaticType(),
728 UpdateBootFlagsAction::StaticType(),
729 OmahaRequestAction::StaticType(),
730 DownloadAction::StaticType(),
731 OmahaRequestAction::StaticType(),
732 FilesystemVerifierAction::StaticType(),
733 PostinstallRunnerAction::StaticType(),
734 OmahaRequestAction::StaticType()};
735}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700736
737// Actions that will be built as part of a user-initiated rollback.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200738vector<string> GetRollbackActionTypes() {
739 return {InstallPlanAction::StaticType(),
740 PostinstallRunnerAction::StaticType()};
741}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700742
Adolfo Victoria497044c2018-07-18 07:51:42 -0700743const StagingSchedule kValidStagingSchedule = {
744 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
745
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700746} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800747
748void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700749 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700750
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700751 // Expect that the device policy is loaded by the |UpdateAttempter| at some
752 // point by calling |RefreshDevicePolicy()|.
Igor9fd76b62017-12-11 15:24:18 +0100753 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700754 EXPECT_CALL(*device_policy, LoadPolicy())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800755 .Times(testing::AtLeast(1))
756 .WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100757 attempter_.policy_provider_.reset(
758 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700759
760 {
761 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200762 for (const auto& update_action_type : GetUpdateActionTypes()) {
Alex Deymo749ecf12014-10-21 20:06:57 -0700763 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700764 EnqueueAction(Pointee(
Askar Aitzhan570ca872019-04-24 11:16:12 +0200765 Property(&AbstractAction::Type, update_action_type))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700766 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700767 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700768 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700769
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800770 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700771 loop_.PostTask(FROM_HERE,
772 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
773 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800774}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700775
Darin Petkove6ef2f82011-03-07 17:31:11 -0800776void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700777 EXPECT_EQ(0, attempter_.http_response_code());
778 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700779 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700780 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800781}
782
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800783void UpdateAttempterTest::RollbackTestStart(bool enterprise_rollback,
784 bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700785 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100786 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700787 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100788 fake_system_state_.set_device_policy(device_policy.get());
789 if (enterprise_rollback) {
790 // We return an empty owner as this is an enterprise.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800791 EXPECT_CALL(*device_policy, GetOwner(_))
792 .WillRepeatedly(DoAll(SetArgPointee<0>(string("")), Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100793 } else {
794 // We return a fake owner as this is an owned consumer device.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800795 EXPECT_CALL(*device_policy, GetOwner(_))
796 .WillRepeatedly(DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
797 Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100798 }
799
800 attempter_.policy_provider_.reset(
801 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700802
Alex Deymo763e7db2015-08-27 21:08:08 -0700803 if (valid_slot) {
804 BootControlInterface::Slot rollback_slot = 1;
805 LOG(INFO) << "Test Mark Bootable: "
806 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700807 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
808 true);
Don Garrett6646b442013-11-13 15:29:11 -0800809 }
810
Chris Sosa28e479c2013-07-12 11:39:53 -0700811 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700812
Chris Sosad38b1132014-03-25 10:43:59 -0700813 // We only allow rollback on devices that are not enterprise enrolled and
814 // which have a valid slot to rollback to.
815 if (!enterprise_rollback && valid_slot) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800816 is_rollback_allowed = true;
Chris Sosa28e479c2013-07-12 11:39:53 -0700817 }
818
Chris Sosa28e479c2013-07-12 11:39:53 -0700819 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700820 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200821 for (const auto& rollback_action_type : GetRollbackActionTypes()) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700822 EXPECT_CALL(*processor_,
Askar Aitzhan570ca872019-04-24 11:16:12 +0200823 EnqueueAction(Pointee(
824 Property(&AbstractAction::Type, rollback_action_type))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700825 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700826 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700827
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700828 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700829 loop_.PostTask(FROM_HERE,
830 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
831 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700832 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700833 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700834 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700835 }
836}
837
838void UpdateAttempterTest::RollbackTestVerify() {
839 // Verifies the actions that were enqueued.
840 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700841 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700842 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
843 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700844 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700845}
846
Darin Petkove6ef2f82011-03-07 17:31:11 -0800847TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700848 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700849 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700850}
851
Chris Sosa76a29ae2013-07-11 17:59:24 -0700852TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700853 loop_.PostTask(FROM_HERE,
854 base::Bind(&UpdateAttempterTest::RollbackTestStart,
855 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800856 false,
857 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700858 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700859}
860
Don Garrett6646b442013-11-13 15:29:11 -0800861TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700862 loop_.PostTask(FROM_HERE,
863 base::Bind(&UpdateAttempterTest::RollbackTestStart,
864 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800865 false,
866 false));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700867 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800868}
869
Chris Sosa76a29ae2013-07-11 17:59:24 -0700870TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700871 loop_.PostTask(FROM_HERE,
872 base::Bind(&UpdateAttempterTest::RollbackTestStart,
873 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800874 true,
875 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700876 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700877}
878
Thieu Le116fda32011-04-19 11:01:54 -0700879void UpdateAttempterTest::PingOmahaTestStart() {
880 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700881 EnqueueAction(Pointee(Property(
882 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700883 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700884 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700885 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700886}
887
888TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700889 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700890 EXPECT_FALSE(attempter_.WasScheduleUpdatesCalled());
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700891 // Disable scheduling of subsequnet checks; we're using the |DefaultPolicy| in
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700892 // testing, which is more permissive than we want to handle here.
893 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700894 loop_.PostTask(FROM_HERE,
895 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
896 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700897 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700898 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700899 EXPECT_TRUE(attempter_.WasScheduleUpdatesCalled());
Thieu Le116fda32011-04-19 11:01:54 -0700900}
901
Darin Petkov18c7bce2011-06-16 14:07:00 -0700902TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800903 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700904 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700905 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700906 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700907 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
908 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700909 EXPECT_EQ(
910 static_cast<ErrorCode>(static_cast<int>(kCode) |
911 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
912 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700913}
914
915TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700916 attempter_.install_plan_.reset(new InstallPlan);
917 attempter_.install_plan_->is_resume = true;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800918 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700919 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700920 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700921 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700922 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
923 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700924 EXPECT_EQ(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800925 static_cast<ErrorCode>(static_cast<int>(kCode) |
926 static_cast<int>(ErrorCode::kResumedFlag) |
927 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700928 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700929}
930
David Zeuthen8f191b22013-08-06 12:27:50 -0700931TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
932 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700933 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700934 mock_p2p_manager.fake().SetP2PEnabled(false);
935 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
936 attempter_.UpdateEngineStarted();
937}
938
939TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
940 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700941 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700942 mock_p2p_manager.fake().SetP2PEnabled(true);
943 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
944 attempter_.UpdateEngineStarted();
945}
946
947TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
948 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700949 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700950 mock_p2p_manager.fake().SetP2PEnabled(true);
951 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700952 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700953 attempter_.UpdateEngineStarted();
954}
955
956TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700957 loop_.PostTask(FROM_HERE,
958 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
959 base::Unretained(this)));
960 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700961}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700962
David Zeuthen8f191b22013-08-06 12:27:50 -0700963void UpdateAttempterTest::P2PNotEnabledStart() {
964 // If P2P is not enabled, check that we do not attempt housekeeping
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700965 // and do not convey that P2P is to be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700966 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700967 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700968 mock_p2p_manager.fake().SetP2PEnabled(false);
969 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800970 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700971 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700972 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700973 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700974}
975
976TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700977 loop_.PostTask(FROM_HERE,
978 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
979 base::Unretained(this)));
980 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700981}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700982
David Zeuthen8f191b22013-08-06 12:27:50 -0700983void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700984 // If P2P is enabled, but starting it fails ensure we don't do
985 // any housekeeping and do not convey that P2P should be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700986 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700987 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700988 mock_p2p_manager.fake().SetP2PEnabled(true);
989 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
990 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
991 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800992 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700993 EXPECT_FALSE(actual_using_p2p_for_downloading());
994 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700995 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700996}
997
998TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700999 loop_.PostTask(
1000 FROM_HERE,
1001 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
1002 base::Unretained(this)));
1003 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -07001004}
Alex Deymo60ca1a72015-06-18 18:19:15 -07001005
David Zeuthen8f191b22013-08-06 12:27:50 -07001006void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001007 // If P2P is enabled, starting it works but housekeeping fails, ensure
1008 // we do not convey P2P is to be used.
David Zeuthen8f191b22013-08-06 12:27:50 -07001009 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001010 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001011 mock_p2p_manager.fake().SetP2PEnabled(true);
1012 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
1013 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001014 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001015 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001016 EXPECT_FALSE(actual_using_p2p_for_downloading());
1017 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001018 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001019}
1020
1021TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001022 loop_.PostTask(FROM_HERE,
1023 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
1024 base::Unretained(this)));
1025 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -07001026}
Alex Deymo60ca1a72015-06-18 18:19:15 -07001027
David Zeuthen8f191b22013-08-06 12:27:50 -07001028void UpdateAttempterTest::P2PEnabledStart() {
1029 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001030 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001031 // If P2P is enabled and starting it works, check that we performed
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001032 // housekeeping and that we convey P2P should be used.
David Zeuthen8f191b22013-08-06 12:27:50 -07001033 mock_p2p_manager.fake().SetP2PEnabled(true);
1034 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
1035 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001036 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001037 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001038 EXPECT_TRUE(actual_using_p2p_for_downloading());
1039 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001040 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001041}
1042
1043TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001044 loop_.PostTask(FROM_HERE,
1045 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
1046 base::Unretained(this)));
1047 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -07001048}
Alex Deymo60ca1a72015-06-18 18:19:15 -07001049
David Zeuthen8f191b22013-08-06 12:27:50 -07001050void UpdateAttempterTest::P2PEnabledInteractiveStart() {
1051 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001052 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001053 // For an interactive check, if P2P is enabled and starting it
1054 // works, check that we performed housekeeping and that we convey
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001055 // P2P should be used for sharing but NOT for downloading.
David Zeuthen8f191b22013-08-06 12:27:50 -07001056 mock_p2p_manager.fake().SetP2PEnabled(true);
1057 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
1058 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001059 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001060 attempter_.Update("",
1061 "",
1062 "",
1063 "",
1064 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001065 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001066 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001067 false,
1068 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001069 EXPECT_FALSE(actual_using_p2p_for_downloading());
1070 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001071 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001072}
1073
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001074TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001075 loop_.PostTask(
1076 FROM_HERE,
1077 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
1078 base::Unretained(this)));
1079 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001080}
1081
1082// Tests that the scatter_factor_in_seconds value is properly fetched
1083// from the device policy.
1084void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -07001085 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001086
Igor9fd76b62017-12-11 15:24:18 +01001087 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001088 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001089 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001090
1091 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001092 .WillRepeatedly(
1093 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001094
Igor9fd76b62017-12-11 15:24:18 +01001095 attempter_.policy_provider_.reset(
1096 new policy::PolicyProvider(std::move(device_policy)));
1097
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001098 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001099 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1100
Alex Deymo60ca1a72015-06-18 18:19:15 -07001101 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001102}
1103
1104TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001105 loop_.PostTask(
1106 FROM_HERE,
1107 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
1108 base::Unretained(this)));
1109 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001110}
1111
1112void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
1113 // Tests that the scatter_factor_in_seconds value is properly fetched
1114 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -07001115 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001116 FakePrefs fake_prefs;
1117 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001118
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001119 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001120
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001121 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001122
Ben Chan9abb7632014-08-07 00:10:53 -07001123 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001124
Igor9fd76b62017-12-11 15:24:18 +01001125 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001126 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001127 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001128
1129 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001130 .WillRepeatedly(
1131 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001132
Igor9fd76b62017-12-11 15:24:18 +01001133 attempter_.policy_provider_.reset(
1134 new policy::PolicyProvider(std::move(device_policy)));
1135
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001136 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001137 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1138
1139 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001140 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001141
Ben Chan9abb7632014-08-07 00:10:53 -07001142 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001143 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001144 EXPECT_EQ(initial_value - 1, new_value);
1145
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001146 EXPECT_TRUE(
1147 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001148
1149 // However, if the count is already 0, it's not decremented. Test that.
1150 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001151 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001152 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001153 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
1154 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001155 EXPECT_EQ(initial_value, new_value);
1156
Alex Deymo60ca1a72015-06-18 18:19:15 -07001157 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001158}
1159
Jay Srinivasan08fce042012-06-07 16:31:01 -07001160TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001161 loop_.PostTask(
1162 FROM_HERE,
1163 base::Bind(
1164 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
1165 base::Unretained(this)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001166 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001167}
1168
1169void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
1170 // Tests that no scattering logic is enabled if the update check
1171 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -07001172 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001173 FakePrefs fake_prefs;
1174 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001175
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001176 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001177 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001178
Adolfo Victoriad3a1e352018-07-16 11:40:47 -07001179 EXPECT_TRUE(
1180 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001181 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001182
1183 // make sure scatter_factor is non-zero as scattering is disabled
1184 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -07001185 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001186
Igor9fd76b62017-12-11 15:24:18 +01001187 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001188 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001189 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001190
1191 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001192 .WillRepeatedly(
1193 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001194
Igor9fd76b62017-12-11 15:24:18 +01001195 attempter_.policy_provider_.reset(
1196 new policy::PolicyProvider(std::move(device_policy)));
1197
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001198 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001199 attempter_.Update("",
1200 "",
1201 "",
1202 "",
1203 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001204 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001205 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001206 false,
1207 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001208 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1209
1210 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001211 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001212 EXPECT_FALSE(
1213 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -07001214 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001215 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1216 EXPECT_FALSE(
1217 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001218 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001219
Alex Deymo60ca1a72015-06-18 18:19:15 -07001220 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001221}
1222
Adolfo Victoria497044c2018-07-18 07:51:42 -07001223void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
1224 FakePrefs* prefs) {
1225 attempter_.prefs_ = prefs;
1226 fake_system_state_.set_prefs(prefs);
1227
1228 int64_t initial_value = 8;
1229 EXPECT_TRUE(
1230 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
1231 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
1232 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
1233
1234 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1235 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
1236 fake_system_state_.set_device_policy(device_policy.get());
1237 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
1238 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
1239
1240 attempter_.policy_provider_.reset(
1241 new policy::PolicyProvider(std::move(device_policy)));
1242}
1243
1244TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
1245 loop_.PostTask(
1246 FROM_HERE,
1247 base::Bind(
1248 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
1249 base::Unretained(this)));
1250 loop_.Run();
1251}
1252
1253void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
1254 // Tests that staging sets its prefs properly and turns off scattering.
1255 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1256 FakePrefs fake_prefs;
1257 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1258
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001259 attempter_.Update("", "", "", "", false, false, 0, false, false);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001260 // Check that prefs have the correct values.
1261 int64_t update_count;
1262 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
1263 int64_t waiting_time_days;
1264 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
1265 &waiting_time_days));
1266 EXPECT_GT(waiting_time_days, 0);
1267 // Update count should have been decremented.
1268 EXPECT_EQ(7, update_count);
1269 // Check that Omaha parameters were updated correctly.
1270 EXPECT_TRUE(
1271 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1272 EXPECT_TRUE(
1273 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1274 EXPECT_EQ(waiting_time_days,
1275 attempter_.omaha_request_params_->waiting_period().InDays());
1276 // Check class variables.
1277 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1278 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1279 // Check that scattering is turned off
1280 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1281 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1282
1283 ScheduleQuitMainLoop();
1284}
1285
1286void UpdateAttempterTest::CheckStagingOff() {
1287 // Check that all prefs were removed.
1288 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1289 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1290 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1291 // Check that the Omaha parameters have the correct value.
1292 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1293 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1294 attempter_.staging_wait_time_);
1295 EXPECT_FALSE(
1296 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1297 EXPECT_FALSE(
1298 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1299 // Check that scattering is turned off too.
1300 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1301}
1302
1303TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1304 loop_.PostTask(FROM_HERE,
1305 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1306 base::Unretained(this)));
1307 loop_.Run();
1308}
1309
1310void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1311 // Tests that staging is turned off when an interactive update is requested.
1312 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1313 FakePrefs fake_prefs;
1314 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1315
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001316 attempter_.Update(
1317 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001318 CheckStagingOff();
1319
1320 ScheduleQuitMainLoop();
1321}
1322
1323TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1324 loop_.PostTask(FROM_HERE,
1325 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1326 base::Unretained(this)));
1327 loop_.Run();
1328}
1329
1330void UpdateAttempterTest::StagingOffIfOobeStart() {
1331 // Tests that staging is turned off if OOBE hasn't been completed.
1332 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1333 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1334 FakePrefs fake_prefs;
1335 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1336
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001337 attempter_.Update(
1338 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001339 CheckStagingOff();
1340
1341 ScheduleQuitMainLoop();
1342}
1343
David Zeuthen985b1122013-10-09 12:13:15 -07001344// Checks that we only report daily metrics at most every 24 hours.
1345TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1346 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001347 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001348
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001349 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001350 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001351
1352 Time epoch = Time::FromInternalValue(0);
1353 fake_clock.SetWallclockTime(epoch);
1354
1355 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1356 // we should report.
1357 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1358 // We should not report again if no time has passed.
1359 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1360
1361 // We should not report if only 10 hours has passed.
1362 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1363 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1364
1365 // We should not report if only 24 hours - 1 sec has passed.
1366 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1367 TimeDelta::FromSeconds(1));
1368 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1369
1370 // We should report if 24 hours has passed.
1371 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1372 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1373
1374 // But then we should not report again..
1375 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1376
1377 // .. until another 24 hours has passed
1378 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1379 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1380 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1381 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1382 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1383
1384 // .. and another 24 hours
1385 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1386 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1387 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1388 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1389 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1390
1391 // If the span between time of reporting and present time is
1392 // negative, we report. This is in order to reset the timestamp and
1393 // avoid an edge condition whereby a distant point in the future is
1394 // in the state variable resulting in us never ever reporting again.
1395 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1396 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1397 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1398
1399 // In this case we should not update until the clock reads 71 + 24 = 95.
1400 // Check that.
1401 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1402 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1403 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1404 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1405 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001406}
1407
David Zeuthen3c55abd2013-10-14 12:48:03 -07001408TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001409 FakeClock fake_clock;
1410 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001411 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001412 FakePrefs fake_prefs;
1413 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001414 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001415
1416 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001417 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001418
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001419 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001420
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001421 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001422 EXPECT_EQ(boot_time.ToTimeT(), 42);
1423}
1424
David Pursell02c18642014-11-06 11:26:11 -08001425TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1426 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1427 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1428}
1429
1430TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1431 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001432 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001433 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1434}
1435
1436TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1437 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001438 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001439 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1440}
1441
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001442// TODO(kimjae): Follow testing pattern with params for |CheckForInstall()|.
1443// When adding, remove older tests related to |CheckForInstall()|.
1444TEST_F(UpdateAttempterTest, CheckForInstallNotIdleFails) {
1445 for (const auto status : kNonIdleUpdateStatuses) {
1446 // GIVEN a non-idle status.
1447 attempter_.status_ = status;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001448
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001449 EXPECT_FALSE(attempter_.CheckForInstall({}, ""));
1450 }
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001451}
1452
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001453TEST_F(UpdateAttempterTest, CheckForUpdateNotIdleFails) {
1454 for (const auto status : kNonIdleUpdateStatuses) {
1455 // GIVEN a non-idle status.
1456 cfu_params_.status = status;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001457
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001458 // THEN |ScheduleUpdates()| should not be called.
1459 cfu_params_.should_schedule_updates_be_called = false;
1460 // THEN result should indicate failure.
1461 cfu_params_.expected_result = false;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001462
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001463 TestCheckForUpdate();
1464 }
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001465}
1466
1467TEST_F(UpdateAttempterTest, CheckForUpdateOfficalBuildClearsSource) {
1468 // GIVEN a official build.
1469
1470 // THEN we except forced app version + forced omaha url to be cleared.
1471
1472 TestCheckForUpdate();
1473}
1474
1475TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildChangesSource) {
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001476 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001477 cfu_params_.is_official_build = false;
1478 cfu_params_.are_dev_features_enabled = true;
1479
1480 // THEN the forced app version + forced omaha url changes based on input.
1481 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1482 cfu_params_.expected_forced_omaha_url = cfu_params_.omaha_url;
1483
1484 TestCheckForUpdate();
1485}
1486
1487TEST_F(UpdateAttempterTest, CheckForUpdateOfficialBuildScheduledAUTest) {
1488 // GIVEN a scheduled autest omaha url.
1489 cfu_params_.omaha_url = "autest-scheduled";
1490
1491 // THEN forced app version is cleared.
1492 // THEN forced omaha url changes to default constant.
1493 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1494
1495 TestCheckForUpdate();
1496}
1497
1498TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildScheduledAUTest) {
1499 // GIVEN a scheduled autest omaha url.
1500 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001501 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001502 cfu_params_.is_official_build = false;
1503 cfu_params_.are_dev_features_enabled = true;
1504
1505 // THEN forced app version changes based on input.
1506 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1507 // THEN forced omaha url changes to default constant.
1508 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1509
1510 TestCheckForUpdate();
1511}
1512
1513TEST_F(UpdateAttempterTest, CheckForUpdateOfficialBuildAUTest) {
1514 // GIVEN a autest omaha url.
1515 cfu_params_.omaha_url = "autest";
1516
1517 // THEN forced app version is cleared.
1518 // THEN forced omaha url changes to default constant.
1519 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1520
1521 TestCheckForUpdate();
1522}
1523
1524TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildAUTest) {
1525 // GIVEN a autest omha url.
1526 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001527 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001528 cfu_params_.is_official_build = false;
1529 cfu_params_.are_dev_features_enabled = true;
1530
1531 // THEN forced app version changes based on input.
1532 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1533 // THEN forced omaha url changes to default constant.
1534 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1535
1536 TestCheckForUpdate();
1537}
1538
1539TEST_F(UpdateAttempterTest,
1540 CheckForUpdateNonInteractiveOfficialBuildScheduledAUTest) {
1541 // GIVEN a scheduled autest omaha url.
1542 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001543 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001544 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
1545
1546 // THEN forced app version is cleared.
1547 // THEN forced omaha url changes to default constant.
1548 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1549
1550 TestCheckForUpdate();
1551}
1552
1553TEST_F(UpdateAttempterTest,
1554 CheckForUpdateNonInteractiveUnofficialBuildScheduledAUTest) {
1555 // GIVEN a scheduled autest omaha url.
1556 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001557 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001558 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001559 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001560 cfu_params_.is_official_build = false;
1561 cfu_params_.are_dev_features_enabled = true;
1562
1563 // THEN forced app version changes based on input.
1564 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1565 // THEN forced omaha url changes to default constant.
1566 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1567
1568 TestCheckForUpdate();
1569}
1570
1571TEST_F(UpdateAttempterTest, CheckForUpdateNonInteractiveOfficialBuildAUTest) {
1572 // GIVEN a autest omaha url.
1573 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001574 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001575 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
1576
1577 // THEN forced app version is cleared.
1578 // THEN forced omaha url changes to default constant.
1579 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1580
1581 TestCheckForUpdate();
1582}
1583
1584TEST_F(UpdateAttempterTest, CheckForUpdateNonInteractiveUnofficialBuildAUTest) {
1585 // GIVEN a autest omaha url.
1586 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001587 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001588 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001589 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001590 cfu_params_.is_official_build = false;
1591 cfu_params_.are_dev_features_enabled = true;
1592
1593 // THEN forced app version changes based on input.
1594 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1595 // THEN forced omaha url changes to default constant.
1596 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1597
1598 TestCheckForUpdate();
David Pursell02c18642014-11-06 11:26:11 -08001599}
1600
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -07001601TEST_F(UpdateAttempterTest, CheckForUpdateMissingForcedCallback1) {
1602 // GIVEN a official build.
1603 // GIVEN forced callback is not set.
1604 attempter_.set_forced_update_pending_callback(nullptr);
1605
1606 // THEN we except forced app version + forced omaha url to be cleared.
1607 // THEN |ScheduleUpdates()| should not be called.
1608 cfu_params_.should_schedule_updates_be_called = false;
1609
1610 TestCheckForUpdate();
1611}
1612
1613TEST_F(UpdateAttempterTest, CheckForUpdateMissingForcedCallback2) {
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001614 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -07001615 cfu_params_.is_official_build = false;
1616 cfu_params_.are_dev_features_enabled = true;
1617 // GIVEN forced callback is not set.
1618 attempter_.set_forced_update_pending_callback(nullptr);
1619
1620 // THEN the forced app version + forced omaha url changes based on input.
1621 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1622 cfu_params_.expected_forced_omaha_url = cfu_params_.omaha_url;
1623 // THEN |ScheduleUpdates()| should not be called.
1624 cfu_params_.should_schedule_updates_be_called = false;
1625
1626 TestCheckForUpdate();
1627}
1628
Xiaochu Liu88d90382018-08-29 16:09:11 -07001629TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1630 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1631 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1632 attempter_.CheckForInstall({}, "autest");
1633 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1634
1635 attempter_.CheckForInstall({}, "autest-scheduled");
1636 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1637
1638 attempter_.CheckForInstall({}, "http://omaha.phishing");
1639 EXPECT_EQ("", attempter_.forced_omaha_url());
1640}
1641
Colin Howesac170d92018-11-20 16:29:28 -08001642TEST_F(UpdateAttempterTest, InstallSetsStatusIdle) {
1643 attempter_.CheckForInstall({}, "http://foo.bar");
1644 attempter_.status_ = UpdateStatus::DOWNLOADING;
1645 EXPECT_TRUE(attempter_.is_install_);
1646 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1647 UpdateEngineStatus status;
1648 attempter_.GetStatus(&status);
1649 // Should set status to idle after an install operation.
1650 EXPECT_EQ(UpdateStatus::IDLE, status.status);
1651}
1652
Colin Howes978c1082018-12-03 11:46:12 -08001653TEST_F(UpdateAttempterTest, RollbackAfterInstall) {
1654 attempter_.is_install_ = true;
1655 attempter_.Rollback(false);
1656 EXPECT_FALSE(attempter_.is_install_);
1657}
1658
1659TEST_F(UpdateAttempterTest, UpdateAfterInstall) {
1660 attempter_.is_install_ = true;
1661 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1662 EXPECT_FALSE(attempter_.is_install_);
1663}
1664
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001665TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001666 attempter_.CalculateUpdateParams(
1667 "", "", "", "1234", false, false, 4, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001668 EXPECT_EQ("1234",
1669 fake_system_state_.request_params()->target_version_prefix());
1670
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001671 attempter_.CalculateUpdateParams(
1672 "", "", "", "", false, 4, false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001673 EXPECT_TRUE(
1674 fake_system_state_.request_params()->target_version_prefix().empty());
1675}
1676
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001677TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1678 attempter_.CalculateUpdateParams("",
1679 "",
1680 "",
1681 "1234",
1682 /*rollback_allowed=*/true,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001683 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001684 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001685 false,
1686 false);
1687 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001688 EXPECT_EQ(4,
1689 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001690
1691 attempter_.CalculateUpdateParams("",
1692 "",
1693 "",
1694 "1234",
1695 /*rollback_allowed=*/false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001696 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001697 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001698 false,
1699 false);
1700 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001701 EXPECT_EQ(4,
1702 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001703}
1704
Aaron Wood23bd3392017-10-06 14:48:25 -07001705TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1706 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1707 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001708 OmahaResponseHandlerAction response_action(&fake_system_state_);
1709 response_action.install_plan_.version = "a.b.c.d";
1710 response_action.install_plan_.system_version = "b.c.d.e";
1711 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001712 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001713 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1714 // completed, with the deferred-update error code.
1715 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001716 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001717 {
1718 UpdateEngineStatus status;
1719 attempter_.GetStatus(&status);
1720 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001721 EXPECT_TRUE(attempter_.install_plan_);
1722 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001723 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001724 status.new_size_bytes);
1725 }
1726 // An "error" event should have been created to tell Omaha that the update is
1727 // being deferred.
1728 EXPECT_TRUE(nullptr != attempter_.error_event_);
1729 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1730 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1731 ErrorCode expected_code = static_cast<ErrorCode>(
1732 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1733 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1734 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1735 // End the processing
1736 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1737 // Validate the state of the attempter.
1738 {
1739 UpdateEngineStatus status;
1740 attempter_.GetStatus(&status);
1741 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001742 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001743 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001744 status.new_size_bytes);
1745 }
1746}
1747
1748TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
Jae Hoon Kim75daa382019-07-02 11:17:24 -07001749 // Default construction for |waiting_for_scheduled_check_| is false.
Jae Hoon Kimba2fdce2019-07-11 13:18:58 -07001750 EXPECT_FALSE(attempter_.IsBusyOrUpdateScheduled());
Aaron Wood23bd3392017-10-06 14:48:25 -07001751 // Verify in-progress update with UPDATE_AVAILABLE is running
1752 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
Jae Hoon Kimba2fdce2019-07-11 13:18:58 -07001753 EXPECT_TRUE(attempter_.IsBusyOrUpdateScheduled());
Aaron Wood23bd3392017-10-06 14:48:25 -07001754}
1755
Aaron Woodbf5a2522017-10-04 10:58:36 -07001756TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1757 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1758
1759 UpdateCheckParams params = {.updates_enabled = true};
1760 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1761
1762 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1763 attempter_.GetCurrentUpdateAttemptFlags());
1764}
1765
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001766TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1767 UpdateCheckParams params = {.updates_enabled = true,
1768 .rollback_allowed = false};
1769 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1770 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1771}
1772
1773TEST_F(UpdateAttempterTest, RollbackAllowed) {
1774 UpdateCheckParams params = {.updates_enabled = true,
1775 .rollback_allowed = true};
1776 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1777 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1778}
1779
Aaron Wood081c0232017-10-19 17:14:58 -07001780TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1781 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1782
1783 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1784 EXPECT_EQ(UpdateAttemptFlags::kNone,
1785 attempter_.GetCurrentUpdateAttemptFlags());
1786}
1787
1788TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1789 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1790
1791 // This tests that when CheckForUpdate() is called with the non-interactive
1792 // flag set, that it doesn't change the current UpdateAttemptFlags.
1793 attempter_.CheckForUpdate("",
1794 "",
1795 UpdateAttemptFlags::kFlagNonInteractive |
1796 UpdateAttemptFlags::kFlagRestrictDownload);
1797 EXPECT_EQ(UpdateAttemptFlags::kNone,
1798 attempter_.GetCurrentUpdateAttemptFlags());
1799}
1800
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001801void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1802 bool is_policy_loaded,
1803 bool expected_reset) {
1804 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1805 .WillRepeatedly(Return(true));
1806 auto mock_policy_provider =
1807 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1808 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1809 .WillRepeatedly(Return(is_consumer));
1810 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1811 .WillRepeatedly(Return(is_policy_loaded));
1812 const policy::MockDevicePolicy device_policy;
1813 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1814 .WillRepeatedly(ReturnRef(device_policy));
1815 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1816 SetRollbackHappened(false))
1817 .Times(expected_reset ? 1 : 0);
1818 attempter_.policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001819 attempter_.Update("", "", "", "", false, false, 0, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001820 ScheduleQuitMainLoop();
1821}
1822
1823TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1824 loop_.PostTask(FROM_HERE,
1825 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1826 base::Unretained(this),
1827 /*is_consumer=*/false,
1828 /*is_policy_loaded=*/false,
1829 /*expected_reset=*/false));
1830 loop_.Run();
1831}
1832
1833TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1834 loop_.PostTask(FROM_HERE,
1835 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1836 base::Unretained(this),
1837 /*is_consumer=*/true,
1838 /*is_policy_loaded=*/false,
1839 /*expected_reset=*/true));
1840 loop_.Run();
1841}
1842
1843TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1844 loop_.PostTask(FROM_HERE,
1845 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1846 base::Unretained(this),
1847 /*is_consumer=*/false,
1848 /*is_policy_loaded=*/true,
1849 /*expected_reset=*/true));
1850 loop_.Run();
1851}
1852
Marton Hunyady199152d2018-05-07 19:08:48 +02001853TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001854 attempter_.install_plan_.reset(new InstallPlan);
1855 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001856
1857 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1858 SetRollbackHappened(true))
1859 .Times(1);
1860 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1861}
1862
1863TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001864 attempter_.install_plan_.reset(new InstallPlan);
1865 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001866
1867 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1868 SetRollbackHappened(true))
1869 .Times(0);
1870 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1871}
1872
Marton Hunyadya0302682018-05-16 18:52:13 +02001873TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001874 attempter_.install_plan_.reset(new InstallPlan);
1875 attempter_.install_plan_->is_rollback = true;
1876 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001877
1878 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1879 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1880 .Times(1);
1881 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1882}
1883
1884TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001885 attempter_.install_plan_.reset(new InstallPlan);
1886 attempter_.install_plan_->is_rollback = false;
1887 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001888
1889 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1890 ReportEnterpriseRollbackMetrics(_, _))
1891 .Times(0);
1892 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1893}
1894
1895TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001896 attempter_.install_plan_.reset(new InstallPlan);
1897 attempter_.install_plan_->is_rollback = true;
1898 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001899
1900 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1901 ReportEnterpriseRollbackMetrics(false, kRollbackVersion))
1902 .Times(1);
1903 MockAction action;
1904 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1905 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1906}
1907
1908TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001909 attempter_.install_plan_.reset(new InstallPlan);
1910 attempter_.install_plan_->is_rollback = false;
1911 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001912
1913 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1914 ReportEnterpriseRollbackMetrics(_, _))
1915 .Times(0);
1916 MockAction action;
1917 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1918 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1919}
1920
May Lippert60aa3ca2018-08-15 16:55:29 -07001921TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1922 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1923 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1924 .Times(0);
1925 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1926}
1927
1928TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1929 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1930 fake_system_state_.set_device_policy(device_policy.get());
1931 // Make device policy return that this is not enterprise enrolled
1932 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1933
1934 // Ensure that the metric is not recorded.
1935 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1936 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1937 .Times(0);
1938 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1939}
1940
1941TEST_F(UpdateAttempterTest,
1942 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1943 constexpr int kDaysToUpdate = 15;
1944 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1945 fake_system_state_.set_device_policy(device_policy.get());
1946 // Make device policy return that this is enterprise enrolled
1947 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1948 // Pretend that there's a time restriction policy in place
1949 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1950 .WillOnce(Return(true));
1951
1952 FakePrefs fake_prefs;
1953 Time update_first_seen_at = Time::Now();
1954 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1955 update_first_seen_at.ToInternalValue());
1956
1957 FakeClock fake_clock;
1958 Time update_finished_at =
1959 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1960 fake_clock.SetWallclockTime(update_finished_at);
1961
1962 fake_system_state_.set_clock(&fake_clock);
1963 fake_system_state_.set_prefs(&fake_prefs);
1964
1965 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1966 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1967 .Times(1);
1968 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1969}
1970
1971TEST_F(UpdateAttempterTest,
1972 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
1973 constexpr int kDaysToUpdate = 15;
1974 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1975 fake_system_state_.set_device_policy(device_policy.get());
1976 // Make device policy return that this is enterprise enrolled
1977 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1978 // Pretend that there's no time restriction policy in place
1979 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1980 .WillOnce(Return(false));
1981
1982 FakePrefs fake_prefs;
1983 Time update_first_seen_at = Time::Now();
1984 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1985 update_first_seen_at.ToInternalValue());
1986
1987 FakeClock fake_clock;
1988 Time update_finished_at =
1989 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1990 fake_clock.SetWallclockTime(update_finished_at);
1991
1992 fake_system_state_.set_clock(&fake_clock);
1993 fake_system_state_.set_prefs(&fake_prefs);
1994
1995 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1996 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
1997 .Times(1);
1998 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1999}
2000
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002001TEST_F(UpdateAttempterTest, ProcessingDoneUpdated) {
2002 // GIVEN an update finished.
2003
2004 // THEN need reboot since update applied.
2005 pd_params_.expected_exit_status = UpdateStatus::UPDATED_NEED_REBOOT;
2006 // THEN install indication should be false.
2007
2008 TestProcessingDone();
2009}
2010
2011TEST_F(UpdateAttempterTest, ProcessingDoneInstalled) {
2012 // GIVEN an install finished.
2013 pd_params_.is_install = true;
2014
2015 // THEN go idle.
2016 // THEN install indication should be false.
2017
2018 TestProcessingDone();
2019}
2020
2021TEST_F(UpdateAttempterTest, ProcessingDoneInstallReportingError) {
2022 // GIVEN an install finished.
2023 pd_params_.is_install = true;
2024 // GIVEN a reporting error occurred.
2025 pd_params_.status = UpdateStatus::REPORTING_ERROR_EVENT;
2026
2027 // THEN go idle.
2028 // THEN install indication should be false.
2029
2030 TestProcessingDone();
2031}
2032
2033TEST_F(UpdateAttempterTest, ProcessingDoneNoUpdate) {
2034 // GIVEN an update finished.
2035 // GIVEN an action error occured.
2036 pd_params_.code = ErrorCode::kNoUpdate;
2037
2038 // THEN go idle.
2039 // THEN install indication should be false.
2040
2041 TestProcessingDone();
2042}
2043
2044TEST_F(UpdateAttempterTest, ProcessingDoneNoInstall) {
2045 // GIVEN an install finished.
2046 pd_params_.is_install = true;
2047 // GIVEN an action error occured.
2048 pd_params_.code = ErrorCode::kNoUpdate;
2049
2050 // THEN go idle.
2051 // THEN install indication should be false.
2052
2053 TestProcessingDone();
2054}
2055
2056TEST_F(UpdateAttempterTest, ProcessingDoneUpdateError) {
2057 // GIVEN an update finished.
2058 // GIVEN an action error occured.
2059 pd_params_.code = ErrorCode::kError;
2060 // GIVEN an event error is set.
2061 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
2062 OmahaEvent::kResultError,
2063 ErrorCode::kError));
2064
2065 // THEN indicate a error event.
2066 pd_params_.expected_exit_status = UpdateStatus::REPORTING_ERROR_EVENT;
2067 // THEN install indication should be false.
2068
2069 // THEN expect critical actions of |ScheduleErrorEventAction()|.
2070 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))).Times(1);
2071 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
2072 // THEN |ScheduleUpdates()| will be called next |ProcessingDone()| so skip.
2073 pd_params_.should_schedule_updates_be_called = false;
2074
2075 TestProcessingDone();
2076}
2077
2078TEST_F(UpdateAttempterTest, ProcessingDoneInstallError) {
2079 // GIVEN an install finished.
2080 pd_params_.is_install = true;
2081 // GIVEN an action error occured.
2082 pd_params_.code = ErrorCode::kError;
2083 // GIVEN an event error is set.
2084 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
2085 OmahaEvent::kResultError,
2086 ErrorCode::kError));
2087
2088 // THEN indicate a error event.
2089 pd_params_.expected_exit_status = UpdateStatus::REPORTING_ERROR_EVENT;
2090 // THEN install indication should be false.
2091
2092 // THEN expect critical actions of |ScheduleErrorEventAction()|.
2093 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))).Times(1);
2094 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
2095 // THEN |ScheduleUpdates()| will be called next |ProcessingDone()| so skip.
2096 pd_params_.should_schedule_updates_be_called = false;
2097
2098 TestProcessingDone();
2099}
2100
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002101void UpdateAttempterTest::UpdateToQuickFixBuildStart(bool set_token) {
2102 // Tests that checks if |device_quick_fix_build_token| arrives when
2103 // policy is set and the device is enterprise enrolled based on |set_token|.
2104 string token = set_token ? "some_token" : "";
Askar Aitzhan570ca872019-04-24 11:16:12 +02002105 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
2106 fake_system_state_.set_device_policy(device_policy.get());
Askar Aitzhan570ca872019-04-24 11:16:12 +02002107 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002108
2109 if (set_token)
2110 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
2111 .WillOnce(DoAll(SetArgPointee<0>(token), Return(true)));
2112 else
2113 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
2114 .WillOnce(Return(false));
Askar Aitzhan570ca872019-04-24 11:16:12 +02002115 attempter_.policy_provider_.reset(
2116 new policy::PolicyProvider(std::move(device_policy)));
2117 attempter_.Update("", "", "", "", false, false, 0, false, false);
2118
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002119 EXPECT_EQ(token, attempter_.omaha_request_params_->autoupdate_token());
Askar Aitzhan570ca872019-04-24 11:16:12 +02002120 ScheduleQuitMainLoop();
2121}
2122
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002123TEST_F(UpdateAttempterTest,
2124 QuickFixTokenWhenDeviceIsEnterpriseEnrolledAndPolicyIsSet) {
Askar Aitzhan570ca872019-04-24 11:16:12 +02002125 loop_.PostTask(FROM_HERE,
2126 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002127 base::Unretained(this),
2128 /*set_token=*/true));
2129 loop_.Run();
2130}
2131
2132TEST_F(UpdateAttempterTest, EmptyQuickFixToken) {
2133 loop_.PostTask(FROM_HERE,
2134 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
2135 base::Unretained(this),
2136 /*set_token=*/false));
Askar Aitzhan570ca872019-04-24 11:16:12 +02002137 loop_.Run();
2138}
2139
Jae Hoon Kim75daa382019-07-02 11:17:24 -07002140TEST_F(UpdateAttempterTest, ScheduleUpdateSpamHandlerTest) {
2141 EXPECT_CALL(mock_update_manager_, AsyncPolicyRequestUpdateCheckAllowed(_, _))
2142 .Times(1);
2143 EXPECT_TRUE(attempter_.ScheduleUpdates());
2144 // Now there is an update scheduled which means that all subsequent
2145 // |ScheduleUpdates()| should fail.
2146 EXPECT_FALSE(attempter_.ScheduleUpdates());
2147 EXPECT_FALSE(attempter_.ScheduleUpdates());
2148 EXPECT_FALSE(attempter_.ScheduleUpdates());
2149}
2150
2151// Critical tests to always make sure that an update is scheduled. The following
2152// unittest(s) try and cover the correctness in synergy between
2153// |UpdateAttempter| and |UpdateManager|. Also it is good to remember the
2154// actions that happen in the flow when |UpdateAttempter| get callbacked on
2155// |OnUpdateScheduled()| -> (various cases which leads to) -> |ProcessingDone()|
2156void UpdateAttempterTest::TestOnUpdateScheduled() {
2157 // Setup
2158 attempter_.SetWaitingForScheduledCheck(true);
2159 attempter_.DisableUpdate();
2160 attempter_.DisableScheduleUpdates();
2161
2162 // Invocation
2163 attempter_.OnUpdateScheduled(ous_params_.status, ous_params_.params);
2164
2165 // Verify
2166 EXPECT_EQ(ous_params_.exit_status, attempter_.status());
2167 EXPECT_EQ(ous_params_.should_schedule_updates_be_called,
2168 attempter_.WasScheduleUpdatesCalled());
2169 EXPECT_EQ(ous_params_.should_update_be_called, attempter_.WasUpdateCalled());
2170}
2171
2172TEST_F(UpdateAttempterTest, OnUpdatesScheduledFailed) {
2173 // GIVEN failed status.
2174
2175 // THEN update should be scheduled.
2176 ous_params_.should_schedule_updates_be_called = true;
2177
2178 TestOnUpdateScheduled();
2179}
2180
2181TEST_F(UpdateAttempterTest, OnUpdatesScheduledAskMeAgainLater) {
2182 // GIVEN ask me again later status.
2183 ous_params_.status = EvalStatus::kAskMeAgainLater;
2184
2185 // THEN update should be scheduled.
2186 ous_params_.should_schedule_updates_be_called = true;
2187
2188 TestOnUpdateScheduled();
2189}
2190
2191TEST_F(UpdateAttempterTest, OnUpdatesScheduledContinue) {
2192 // GIVEN continue status.
2193 ous_params_.status = EvalStatus::kContinue;
2194
2195 // THEN update should be scheduled.
2196 ous_params_.should_schedule_updates_be_called = true;
2197
2198 TestOnUpdateScheduled();
2199}
2200
2201TEST_F(UpdateAttempterTest, OnUpdatesScheduledSucceededButUpdateDisabledFails) {
2202 // GIVEN updates disabled.
2203 ous_params_.params = {.updates_enabled = false};
2204 // GIVEN succeeded status.
2205 ous_params_.status = EvalStatus::kSucceeded;
2206
2207 // THEN update should not be scheduled.
2208
2209 TestOnUpdateScheduled();
2210}
2211
2212TEST_F(UpdateAttempterTest, OnUpdatesScheduledSucceeded) {
2213 // GIVEN updates enabled.
2214 ous_params_.params = {.updates_enabled = true};
2215 // GIVEN succeeded status.
2216 ous_params_.status = EvalStatus::kSucceeded;
2217
2218 // THEN update should be called indicating status change.
2219 ous_params_.exit_status = UpdateStatus::CHECKING_FOR_UPDATE;
2220 ous_params_.should_update_be_called = true;
2221
2222 TestOnUpdateScheduled();
2223}
2224
Amin Hassani9be122e2019-08-29 09:20:12 -07002225TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusDefault) {
2226 UpdateEngineStatus status;
2227 attempter_.GetStatus(&status);
2228 EXPECT_FALSE(status.is_enterprise_rollback);
2229}
2230
2231TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusFalse) {
2232 attempter_.install_plan_.reset(new InstallPlan);
2233 attempter_.install_plan_->is_rollback = false;
2234
2235 UpdateEngineStatus status;
2236 attempter_.GetStatus(&status);
2237 EXPECT_FALSE(status.is_enterprise_rollback);
2238}
2239
2240TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusTrue) {
2241 attempter_.install_plan_.reset(new InstallPlan);
2242 attempter_.install_plan_->is_rollback = true;
2243
2244 UpdateEngineStatus status;
2245 attempter_.GetStatus(&status);
2246 EXPECT_TRUE(status.is_enterprise_rollback);
2247}
2248
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002249TEST_F(UpdateAttempterTest, FutureEolTest) {
2250 EolDate eol_date = std::numeric_limits<int64_t>::max();
2251 EXPECT_CALL(*prefs_, GetString(kPrefsOmahaEolDate, _))
2252 .WillOnce(
2253 DoAll(SetArgPointee<1>(EolDateToString(eol_date)), Return(true)));
2254
2255 UpdateEngineStatus status;
2256 attempter_.GetStatus(&status);
2257 EXPECT_EQ(eol_date, status.eol_date);
2258}
2259
2260TEST_F(UpdateAttempterTest, PastEolTest) {
2261 EolDate eol_date = 1;
2262 EXPECT_CALL(*prefs_, GetString(kPrefsOmahaEolDate, _))
2263 .WillOnce(
2264 DoAll(SetArgPointee<1>(EolDateToString(eol_date)), Return(true)));
2265
2266 UpdateEngineStatus status;
2267 attempter_.GetStatus(&status);
2268 EXPECT_EQ(eol_date, status.eol_date);
2269}
2270
2271TEST_F(UpdateAttempterTest, FailedEolTest) {
2272 EolDate eol_date = kEolDateInvalid;
2273 EXPECT_CALL(*prefs_, GetString(kPrefsOmahaEolDate, _))
2274 .WillOnce(Return(false));
2275
2276 UpdateEngineStatus status;
2277 attempter_.GetStatus(&status);
2278 EXPECT_EQ(eol_date, status.eol_date);
2279}
2280
Darin Petkovf42cc1c2010-09-01 09:03:02 -07002281} // namespace chromeos_update_engine