blob: 254579c05b88889c1dce2598bc34500c2d140439 [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
Ben Chan02f7c1d2014-10-18 15:18:02 -070021#include <memory>
Jae Hoon Kimedb65502019-06-14 11:52:17 -070022#include <unordered_set>
Ben Chan02f7c1d2014-10-18 15:18:02 -070023
Ben Chan06c76a42014-09-05 08:21:06 -070024#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070025#include <base/message_loop/message_loop.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070026#include <brillo/message_loops/base_message_loop.h>
27#include <brillo/message_loops/message_loop.h>
28#include <brillo/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070029#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020030#include <policy/libpolicy.h>
31#include <policy/mock_device_policy.h>
Marton Hunyadye58bddb2018-04-10 20:27:26 +020032#include <policy/mock_libpolicy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070034#include "update_engine/common/constants.h"
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080035#include "update_engine/common/dlcservice_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/fake_clock.h"
37#include "update_engine/common/fake_prefs.h"
Alex Deymo14fd1ec2016-02-24 22:03:57 -080038#include "update_engine/common/mock_action.h"
39#include "update_engine/common/mock_action_processor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080040#include "update_engine/common/mock_http_fetcher.h"
41#include "update_engine/common/mock_prefs.h"
42#include "update_engine/common/platform_constants.h"
43#include "update_engine/common/prefs.h"
44#include "update_engine/common/test_utils.h"
45#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070046#include "update_engine/fake_system_state.h"
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070047#include "update_engine/libcurl_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070048#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049#include "update_engine/mock_payload_state.h"
Aaron Wood9321f502017-09-07 11:18:54 -070050#include "update_engine/mock_service_observer.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080051#include "update_engine/payload_consumer/filesystem_verifier_action.h"
52#include "update_engine/payload_consumer/install_plan.h"
53#include "update_engine/payload_consumer/payload_constants.h"
54#include "update_engine/payload_consumer/postinstall_runner_action.h"
Amin Hassani0882a512018-04-05 16:25:44 -070055#include "update_engine/update_boot_flags_action.h"
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -070056#include "update_engine/update_manager/mock_update_manager.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070057
David Zeuthen985b1122013-10-09 12:13:15 -070058using base::Time;
59using base::TimeDelta;
Aaron Woodbf5a2522017-10-04 10:58:36 -070060using chromeos_update_manager::EvalStatus;
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -070061using chromeos_update_manager::MockUpdateManager;
Adolfo Victoria497044c2018-07-18 07:51:42 -070062using chromeos_update_manager::StagingSchedule;
Aaron Woodbf5a2522017-10-04 10:58:36 -070063using chromeos_update_manager::UpdateCheckParams;
Adolfo Victoria497044c2018-07-18 07:51:42 -070064using policy::DevicePolicy;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070065using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070066using std::unique_ptr;
Jae Hoon Kimedb65502019-06-14 11:52:17 -070067using std::unordered_set;
Xiaochu Liu88d90382018-08-29 16:09:11 -070068using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070069using testing::_;
Darin Petkov36275772010-10-01 11:40:57 -070070using testing::DoAll;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070071using testing::Field;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070072using testing::InSequence;
Jae Hoon Kimedb65502019-06-14 11:52:17 -070073using testing::Invoke;
Darin Petkov2dd01092010-10-08 15:43:05 -070074using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080075using testing::NiceMock;
Amin Hassanid3f4bea2018-04-30 14:52:40 -070076using testing::Pointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070077using testing::Property;
78using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070079using testing::ReturnPointee;
Marton Hunyadye58bddb2018-04-10 20:27:26 +020080using testing::ReturnRef;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080081using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070082using testing::SetArgPointee;
Aaron Woodbf5a2522017-10-04 10:58:36 -070083using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070084using update_engine::UpdateEngineStatus;
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -070085using update_engine::UpdateStatus;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070086
87namespace chromeos_update_engine {
88
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080089namespace {
90
Jae Hoon Kimc437ea52019-07-11 11:20:38 -070091const UpdateStatus kNonIdleUpdateStatuses[] = {
92 UpdateStatus::CHECKING_FOR_UPDATE,
93 UpdateStatus::UPDATE_AVAILABLE,
94 UpdateStatus::DOWNLOADING,
95 UpdateStatus::VERIFYING,
96 UpdateStatus::FINALIZING,
97 UpdateStatus::UPDATED_NEED_REBOOT,
98 UpdateStatus::REPORTING_ERROR_EVENT,
99 UpdateStatus::ATTEMPTING_ROLLBACK,
100 UpdateStatus::DISABLED,
101 UpdateStatus::NEED_PERMISSION_TO_UPDATE,
102};
103
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700104struct CheckForUpdateTestParams {
105 // Setups + Inputs:
106 UpdateStatus status = UpdateStatus::IDLE;
107 string app_version = "fake_app_version";
108 string omaha_url = "fake_omaha_url";
109 UpdateAttemptFlags flags = UpdateAttemptFlags::kNone;
110 bool is_official_build = true;
111 bool are_dev_features_enabled = false;
112
113 // Expects:
114 string expected_forced_app_version = "";
115 string expected_forced_omaha_url = "";
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -0700116 bool should_schedule_updates_be_called = true;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700117 bool expected_result = true;
118};
119
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700120struct OnUpdateScheduledTestParams {
121 // Setups + Inputs:
122 UpdateCheckParams params = {};
123 EvalStatus status = EvalStatus::kFailed;
124 // Expects:
125 UpdateStatus exit_status = UpdateStatus::IDLE;
126 bool should_schedule_updates_be_called = false;
127 bool should_update_be_called = false;
128};
129
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800130class MockDlcService : public DlcServiceInterface {
131 public:
132 MOCK_METHOD1(GetInstalled, bool(vector<string>*));
133};
134
135} // namespace
136
Marton Hunyadya0302682018-05-16 18:52:13 +0200137const char kRollbackVersion[] = "10575.39.2";
138
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700139// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -0700140// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700141// methods.
142class UpdateAttempterUnderTest : public UpdateAttempter {
143 public:
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700144 explicit UpdateAttempterUnderTest(SystemState* system_state)
145 : UpdateAttempter(system_state, nullptr) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700146
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700147 void Update(const std::string& app_version,
148 const std::string& omaha_url,
149 const std::string& target_channel,
150 const std::string& target_version_prefix,
151 bool rollback_allowed,
152 bool rollback_data_save_requested,
153 int rollback_allowed_milestones,
154 bool obey_proxies,
155 bool interactive) override {
156 update_called_ = true;
157 if (do_update_) {
158 UpdateAttempter::Update(app_version,
159 omaha_url,
160 target_channel,
161 target_version_prefix,
162 rollback_allowed,
163 rollback_data_save_requested,
164 rollback_allowed_milestones,
165 obey_proxies,
166 interactive);
167 return;
168 }
169 LOG(INFO) << "[TEST] Update() disabled.";
170 status_ = UpdateStatus::CHECKING_FOR_UPDATE;
171 }
172
173 void DisableUpdate() { do_update_ = false; }
174
175 bool WasUpdateCalled() const { return update_called_; }
176
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700177 // Wrap the update scheduling method, allowing us to opt out of scheduled
178 // updates for testing purposes.
Xiaochu Liu88d90382018-08-29 16:09:11 -0700179 bool ScheduleUpdates() override {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700180 schedule_updates_called_ = true;
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700181 if (do_schedule_updates_)
182 return UpdateAttempter::ScheduleUpdates();
183 LOG(INFO) << "[TEST] Update scheduling disabled.";
184 waiting_for_scheduled_check_ = true;
Xiaochu Liu88d90382018-08-29 16:09:11 -0700185 return true;
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700186 }
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700187
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700188 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
189
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700190 // Indicates whether |ScheduleUpdates()| was called.
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700191 bool WasScheduleUpdatesCalled() const { return schedule_updates_called_; }
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700192
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700193 // Need to expose following private members of |UpdateAttempter| for tests.
194 const string& forced_app_version() const { return forced_app_version_; }
Alex Deymo60ca1a72015-06-18 18:19:15 -0700195 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -0800196
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700197 // Need to expose |waiting_for_scheduled_check_| for testing.
198 void SetWaitingForScheduledCheck(bool waiting) {
199 waiting_for_scheduled_check_ = waiting;
200 }
201
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700202 private:
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700203 // Used for overrides of |Update()|.
204 bool update_called_ = false;
205 bool do_update_ = true;
206
207 // Used for overrides of |ScheduleUpdates()|.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700208 bool schedule_updates_called_ = false;
209 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700210};
211
212class UpdateAttempterTest : public ::testing::Test {
213 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700214 UpdateAttempterTest()
Daniel Erate5f6f252017-04-20 12:09:58 -0600215 : certificate_checker_(fake_system_state_.mock_prefs(),
Alex Deymo33e91e72015-12-01 18:26:08 -0300216 &openssl_wrapper_) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700217 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700218 fake_system_state_.set_connection_manager(&mock_connection_manager);
219 fake_system_state_.set_update_attempter(&attempter_);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800220 fake_system_state_.set_dlcservice(&mock_dlcservice_);
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -0700221 fake_system_state_.set_update_manager(&mock_update_manager_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700222 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700223
Alex Deymo33e91e72015-12-01 18:26:08 -0300224 certificate_checker_.Init();
225
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800226 attempter_.set_forced_update_pending_callback(
227 new base::Callback<void(bool, bool)>(base::Bind([](bool, bool) {})));
Sen Jiange67bb5b2016-06-20 15:53:56 -0700228 // Finish initializing the attempter.
Gilad Arnold1f847232014-04-07 12:07:49 -0700229 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -0700230 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700231
Alex Deymo610277e2014-11-11 21:18:11 -0800232 void SetUp() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700233 EXPECT_NE(nullptr, attempter_.system_state_);
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700234 EXPECT_NE(nullptr, attempter_.system_state_->update_manager());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700235 EXPECT_EQ(0, attempter_.http_response_code_);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700236 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700237 EXPECT_EQ(0.0, attempter_.download_progress_);
238 EXPECT_EQ(0, attempter_.last_checked_time_);
239 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700240 EXPECT_EQ(0ULL, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800241 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700242 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700243 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700244
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700245 // Setup store/load semantics of P2P properties via the mock |PayloadState|.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700246 actual_using_p2p_for_downloading_ = false;
247 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
248 SetUsingP2PForDownloading(_))
249 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
250 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
251 GetUsingP2PForDownloading())
252 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
253 actual_using_p2p_for_sharing_ = false;
254 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
255 SetUsingP2PForSharing(_))
256 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
257 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
258 GetUsingP2PForDownloading())
259 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700260 }
261
Alex Deymo60ca1a72015-06-18 18:19:15 -0700262 public:
263 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200264
Alex Deymo60ca1a72015-06-18 18:19:15 -0700265 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800266 void UpdateTestStart();
267 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700268 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700269 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700270 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700271 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700272 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700273 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700274 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700275 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700276 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700277 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700278 void P2PEnabledHousekeepingFailsStart();
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700279 void SessionIdTestChange();
280 void SessionIdTestEnforceEmptyStrPingOmaha();
281 void SessionIdTestConsistencyInUpdateFlow();
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700282 void SessionIdTestInDownloadAction();
Amr Aboelkher21ac9962019-05-15 14:50:05 +0200283 void UpdateToQuickFixBuildStart(bool set_token);
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200284 void ResetRollbackHappenedStart(bool is_consumer,
285 bool is_policy_available,
286 bool expected_reset);
Adolfo Victoria497044c2018-07-18 07:51:42 -0700287 // Staging related callbacks.
288 void SetUpStagingTest(const StagingSchedule& schedule, FakePrefs* prefs);
289 void CheckStagingOff();
290 void StagingSetsPrefsAndTurnsOffScatteringStart();
291 void StagingOffIfInteractiveStart();
292 void StagingOffIfOobeStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700293
Gilad Arnold74b5f552014-10-07 08:17:16 -0700294 bool actual_using_p2p_for_downloading() {
295 return actual_using_p2p_for_downloading_;
296 }
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800297 bool actual_using_p2p_for_sharing() { return actual_using_p2p_for_sharing_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700298
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700299 // |CheckForUpdate()| related member functions.
300 void TestCheckForUpdate();
301
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700302 // |OnUpdateScheduled()| related member functions.
303 void TestOnUpdateScheduled();
304
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700305 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700306 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700307
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700308 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700309 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300310 OpenSSLWrapper openssl_wrapper_;
311 CertificateChecker certificate_checker_;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800312 MockDlcService mock_dlcservice_;
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -0700313 MockUpdateManager mock_update_manager_;
Alex Deymo30534502015-07-20 15:06:33 -0700314
Alex Deymo8427b4a2014-11-05 14:00:32 -0800315 NiceMock<MockActionProcessor>* processor_;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700316 NiceMock<MockPrefs>*
317 prefs_; // Shortcut to |fake_system_state_->mock_prefs()|.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800318 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700319
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700320 // |CheckForUpdate()| test params.
321 CheckForUpdateTestParams cfu_params_;
322
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700323 // |OnUpdateScheduled()| test params.
324 OnUpdateScheduledTestParams ous_params_;
325
Gilad Arnold74b5f552014-10-07 08:17:16 -0700326 bool actual_using_p2p_for_downloading_;
327 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700328};
329
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700330void UpdateAttempterTest::TestCheckForUpdate() {
331 // Setup
332 attempter_.status_ = cfu_params_.status;
333 fake_system_state_.fake_hardware()->SetIsOfficialBuild(
334 cfu_params_.is_official_build);
335 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(
336 cfu_params_.are_dev_features_enabled);
337
338 // Invocation
339 EXPECT_EQ(
340 cfu_params_.expected_result,
341 attempter_.CheckForUpdate(
342 cfu_params_.app_version, cfu_params_.omaha_url, cfu_params_.flags));
343
344 // Verify
345 EXPECT_EQ(cfu_params_.expected_forced_app_version,
346 attempter_.forced_app_version());
347 EXPECT_EQ(cfu_params_.expected_forced_omaha_url,
348 attempter_.forced_omaha_url());
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -0700349 EXPECT_EQ(cfu_params_.should_schedule_updates_be_called,
350 attempter_.WasScheduleUpdatesCalled());
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700351}
352
Alex Deymo60ca1a72015-06-18 18:19:15 -0700353void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700354 loop_.PostTask(
355 FROM_HERE,
356 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
357 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700358}
359
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700360void UpdateAttempterTest::SessionIdTestChange() {
361 EXPECT_NE(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
362 const auto old_session_id = attempter_.session_id_;
363 attempter_.Update("", "", "", "", false, false, 0, false, false);
364 EXPECT_NE(old_session_id, attempter_.session_id_);
365 ScheduleQuitMainLoop();
366}
367
368TEST_F(UpdateAttempterTest, SessionIdTestChange) {
369 loop_.PostTask(FROM_HERE,
370 base::Bind(&UpdateAttempterTest::SessionIdTestChange,
371 base::Unretained(this)));
372 loop_.Run();
373}
374
375void UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha() {
376 // The |session_id_| should not be changed and should remain as an empty
377 // string when |status_| is |UPDATED_NEED_REBOOT| (only for consistency)
378 // and |PingOmaha()| is called.
379 attempter_.DisableScheduleUpdates();
380 attempter_.status_ = UpdateStatus::UPDATED_NEED_REBOOT;
381 const auto old_session_id = attempter_.session_id_;
382 auto CheckIfEmptySessionId = [](AbstractAction* aa) {
383 if (aa->Type() == OmahaRequestAction::StaticType()) {
384 EXPECT_TRUE(static_cast<OmahaRequestAction*>(aa)->session_id_.empty());
385 }
386 };
387 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
388 .WillRepeatedly(Invoke(CheckIfEmptySessionId));
389 EXPECT_CALL(*processor_, StartProcessing());
390 attempter_.PingOmaha();
391 EXPECT_EQ(old_session_id, attempter_.session_id_);
392 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status_);
393 ScheduleQuitMainLoop();
394}
395
396TEST_F(UpdateAttempterTest, SessionIdTestEnforceEmptyStrPingOmaha) {
397 loop_.PostTask(
398 FROM_HERE,
399 base::Bind(&UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha,
400 base::Unretained(this)));
401 loop_.Run();
402}
403
404void UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow() {
405 // All session IDs passed into |OmahaRequestActions| should be enforced to
406 // have the same value in |BuildUpdateActions()|.
407 unordered_set<string> session_ids;
408 // Gather all the session IDs being passed to |OmahaRequestActions|.
409 auto CheckSessionId = [&session_ids](AbstractAction* aa) {
410 if (aa->Type() == OmahaRequestAction::StaticType())
411 session_ids.insert(static_cast<OmahaRequestAction*>(aa)->session_id_);
412 };
413 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
414 .WillRepeatedly(Invoke(CheckSessionId));
415 attempter_.BuildUpdateActions(false);
416 // Validate that all the session IDs are the same.
417 EXPECT_EQ(1, session_ids.size());
418 ScheduleQuitMainLoop();
419}
420
421TEST_F(UpdateAttempterTest, SessionIdTestConsistencyInUpdateFlow) {
422 loop_.PostTask(
423 FROM_HERE,
424 base::Bind(&UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow,
425 base::Unretained(this)));
426 loop_.Run();
427}
428
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700429void UpdateAttempterTest::SessionIdTestInDownloadAction() {
430 // The session ID passed into |DownloadAction|'s |LibcurlHttpFetcher| should
431 // be enforced to be included in the HTTP header as X-Goog-Update-SessionId.
432 string header_value;
433 auto CheckSessionIdInDownloadAction = [&header_value](AbstractAction* aa) {
434 if (aa->Type() == DownloadAction::StaticType()) {
435 DownloadAction* da = static_cast<DownloadAction*>(aa);
436 EXPECT_TRUE(da->http_fetcher()->GetHeader(kXGoogleUpdateSessionId,
437 &header_value));
438 }
439 };
440 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
441 .WillRepeatedly(Invoke(CheckSessionIdInDownloadAction));
442 attempter_.BuildUpdateActions(false);
443 // Validate that X-Goog-Update_SessionId is set correctly in HTTP Header.
444 EXPECT_EQ(attempter_.session_id_, header_value);
445 ScheduleQuitMainLoop();
446}
447
448TEST_F(UpdateAttempterTest, SessionIdTestInDownloadAction) {
449 loop_.PostTask(FROM_HERE,
450 base::Bind(&UpdateAttempterTest::SessionIdTestInDownloadAction,
451 base::Unretained(this)));
452 loop_.Run();
453}
454
Darin Petkov1b003102010-11-30 10:18:36 -0800455TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700456 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800457 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700458 DownloadAction action(prefs_,
459 nullptr,
460 nullptr,
461 nullptr,
462 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700463 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800464 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700465 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700466 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700467 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700468 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800469}
470
471TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800472 MockAction action;
473 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700474 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800475 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800476 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700477 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
478 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800479}
480
Aaron Wood9321f502017-09-07 11:18:54 -0700481TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
482 // Simple test case, where all the values match (nothing was skipped)
483 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
484 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
485 uint64_t bytes_received_1 = bytes_progressed_1;
486 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
487 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
488
489 double progress_1 =
490 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
491 double progress_2 =
492 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
493
494 EXPECT_EQ(0.0, attempter_.download_progress_);
495 // This is set via inspecting the InstallPlan payloads when the
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700496 // |OmahaResponseAction| is completed.
Aaron Wood9321f502017-09-07 11:18:54 -0700497 attempter_.new_payload_size_ = bytes_total;
498 NiceMock<MockServiceObserver> observer;
499 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700500 SendStatusUpdate(AllOf(
501 Field(&UpdateEngineStatus::progress, progress_1),
502 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
503 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700504 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700505 SendStatusUpdate(AllOf(
506 Field(&UpdateEngineStatus::progress, progress_2),
507 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
508 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700509 attempter_.AddObserver(&observer);
510 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
511 EXPECT_EQ(progress_1, attempter_.download_progress_);
512 // This iteration validates that a later set of updates to the variables are
513 // properly handled (so that |getStatus()| will return the same progress info
514 // as the callback is receiving.
515 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
516 EXPECT_EQ(progress_2, attempter_.download_progress_);
517}
518
519TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700520 // The transition into |UpdateStatus::DOWNLOADING| happens when the
Aaron Wood9321f502017-09-07 11:18:54 -0700521 // first bytes are received.
522 uint64_t bytes_progressed = 1024 * 1024; // 1MB
523 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
524 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
525 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
526 // This is set via inspecting the InstallPlan payloads when the
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700527 // |OmahaResponseAction| is completed.
Aaron Wood9321f502017-09-07 11:18:54 -0700528 attempter_.new_payload_size_ = bytes_total;
529 EXPECT_EQ(0.0, attempter_.download_progress_);
530 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700531 EXPECT_CALL(observer,
532 SendStatusUpdate(AllOf(
533 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
534 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700535 attempter_.AddObserver(&observer);
536 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
537 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
538}
539
540TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
541 // There is a special case to ensure that at 100% downloaded,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700542 // |download_progress_| is updated and broadcastest.
Aaron Wood9321f502017-09-07 11:18:54 -0700543 uint64_t bytes_progressed = 0; // ignored
544 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
545 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
546 attempter_.status_ = UpdateStatus::DOWNLOADING;
547 attempter_.new_payload_size_ = bytes_total;
548 EXPECT_EQ(0.0, attempter_.download_progress_);
549 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700550 EXPECT_CALL(observer,
551 SendStatusUpdate(AllOf(
552 Field(&UpdateEngineStatus::progress, 1.0),
553 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
554 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700555 attempter_.AddObserver(&observer);
556 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
557 EXPECT_EQ(1.0, attempter_.download_progress_);
558}
559
Darin Petkov1b003102010-11-30 10:18:36 -0800560TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700561 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800562 fetcher->FailTransfer(500); // Sets the HTTP response code.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800563 OmahaRequestAction action(
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700564 &fake_system_state_, nullptr, std::move(fetcher), false, "");
Darin Petkov1b003102010-11-30 10:18:36 -0800565 ObjectCollectorAction<OmahaResponse> collector_action;
566 BondActions(&action, &collector_action);
567 OmahaResponse response;
568 response.poll_interval = 234;
569 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800570 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700571 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800572 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700573 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800574 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700575 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800576}
577
Alex Deymo30534502015-07-20 15:06:33 -0700578TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700579 FakePrefs fake_prefs;
580 string boot_id;
581 EXPECT_TRUE(utils::GetBootId(&boot_id));
582 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
583 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700584 attempter_.Init();
585 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700586}
587
588TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700589 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700590 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700591
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700592 FakeSystemState fake_system_state;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800593 OmahaRequestAction omaha_request_action(
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700594 &fake_system_state, nullptr, nullptr, false, "");
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700595 EXPECT_EQ(ErrorCode::kOmahaRequestError,
596 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700597 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800598 EXPECT_EQ(
599 ErrorCode::kOmahaResponseHandlerError,
600 GetErrorCodeForAction(&omaha_response_handler_action, ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700601 FilesystemVerifierAction filesystem_verifier_action;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800602 EXPECT_EQ(
603 ErrorCode::kFilesystemVerifierError,
604 GetErrorCodeForAction(&filesystem_verifier_action, ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300605 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700606 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800607 EXPECT_EQ(
608 ErrorCode::kPostinstallRunnerError,
609 GetErrorCodeForAction(&postinstall_runner_action, ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800610 MockAction action_mock;
611 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700612 EXPECT_EQ(ErrorCode::kError,
613 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700614}
615
Darin Petkov36275772010-10-01 11:40:57 -0700616TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700617 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800618 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700619 .WillOnce(Return(false));
620 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700621 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800622 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800623 .WillOnce(
624 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
625 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700626 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700627 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800628 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800629 .WillOnce(
630 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
631 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700632 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700633 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800634 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700635 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700636 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700637}
638
639TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800640 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700641 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700642 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
643 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800644 .WillOnce(
645 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
646 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800647 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700648 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800649 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700650 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800651 EXPECT_CALL(*prefs_,
652 SetInt64(kPrefsDeltaUpdateFailures,
653 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
654 for (int i = 0; i < 4; i++)
Darin Petkov36275772010-10-01 11:40:57 -0700655 attempter_.MarkDeltaUpdateFailure();
656}
657
Darin Petkov1b003102010-11-30 10:18:36 -0800658TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
659 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
660 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700661 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800662 .Times(0);
663 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700664 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800665 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700666 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700667 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700668 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800669 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700670 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800671}
672
673TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
674 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700675 EnqueueAction(Pointee(Property(
676 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700677 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700678 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700679 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800680 attempter_.error_event_.reset(new OmahaEvent(
681 OmahaEvent::kTypeUpdateComplete, OmahaEvent::kResultError, err));
Darin Petkov1b003102010-11-30 10:18:36 -0800682 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700683 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800684}
685
Darin Petkove6ef2f82011-03-07 17:31:11 -0800686namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700687// Actions that will be built as part of an update check.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200688vector<string> GetUpdateActionTypes() {
689 return {OmahaRequestAction::StaticType(),
690 OmahaResponseHandlerAction::StaticType(),
691 UpdateBootFlagsAction::StaticType(),
692 OmahaRequestAction::StaticType(),
693 DownloadAction::StaticType(),
694 OmahaRequestAction::StaticType(),
695 FilesystemVerifierAction::StaticType(),
696 PostinstallRunnerAction::StaticType(),
697 OmahaRequestAction::StaticType()};
698}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700699
700// Actions that will be built as part of a user-initiated rollback.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200701vector<string> GetRollbackActionTypes() {
702 return {InstallPlanAction::StaticType(),
703 PostinstallRunnerAction::StaticType()};
704}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700705
Adolfo Victoria497044c2018-07-18 07:51:42 -0700706const StagingSchedule kValidStagingSchedule = {
707 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
708
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700709} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800710
711void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700712 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700713
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700714 // Expect that the device policy is loaded by the |UpdateAttempter| at some
715 // point by calling |RefreshDevicePolicy()|.
Igor9fd76b62017-12-11 15:24:18 +0100716 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700717 EXPECT_CALL(*device_policy, LoadPolicy())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800718 .Times(testing::AtLeast(1))
719 .WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100720 attempter_.policy_provider_.reset(
721 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700722
723 {
724 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200725 for (const auto& update_action_type : GetUpdateActionTypes()) {
Alex Deymo749ecf12014-10-21 20:06:57 -0700726 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700727 EnqueueAction(Pointee(
Askar Aitzhan570ca872019-04-24 11:16:12 +0200728 Property(&AbstractAction::Type, update_action_type))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700729 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700730 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700731 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700732
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800733 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700734 loop_.PostTask(FROM_HERE,
735 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
736 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800737}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700738
Darin Petkove6ef2f82011-03-07 17:31:11 -0800739void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700740 EXPECT_EQ(0, attempter_.http_response_code());
741 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700742 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700743 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800744}
745
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800746void UpdateAttempterTest::RollbackTestStart(bool enterprise_rollback,
747 bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700748 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100749 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700750 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100751 fake_system_state_.set_device_policy(device_policy.get());
752 if (enterprise_rollback) {
753 // We return an empty owner as this is an enterprise.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800754 EXPECT_CALL(*device_policy, GetOwner(_))
755 .WillRepeatedly(DoAll(SetArgPointee<0>(string("")), Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100756 } else {
757 // We return a fake owner as this is an owned consumer device.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800758 EXPECT_CALL(*device_policy, GetOwner(_))
759 .WillRepeatedly(DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
760 Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100761 }
762
763 attempter_.policy_provider_.reset(
764 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700765
Alex Deymo763e7db2015-08-27 21:08:08 -0700766 if (valid_slot) {
767 BootControlInterface::Slot rollback_slot = 1;
768 LOG(INFO) << "Test Mark Bootable: "
769 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700770 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
771 true);
Don Garrett6646b442013-11-13 15:29:11 -0800772 }
773
Chris Sosa28e479c2013-07-12 11:39:53 -0700774 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700775
Chris Sosad38b1132014-03-25 10:43:59 -0700776 // We only allow rollback on devices that are not enterprise enrolled and
777 // which have a valid slot to rollback to.
778 if (!enterprise_rollback && valid_slot) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800779 is_rollback_allowed = true;
Chris Sosa28e479c2013-07-12 11:39:53 -0700780 }
781
Chris Sosa28e479c2013-07-12 11:39:53 -0700782 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700783 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200784 for (const auto& rollback_action_type : GetRollbackActionTypes()) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700785 EXPECT_CALL(*processor_,
Askar Aitzhan570ca872019-04-24 11:16:12 +0200786 EnqueueAction(Pointee(
787 Property(&AbstractAction::Type, rollback_action_type))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700788 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700789 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700790
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700791 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700792 loop_.PostTask(FROM_HERE,
793 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
794 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700795 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700796 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700797 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700798 }
799}
800
801void UpdateAttempterTest::RollbackTestVerify() {
802 // Verifies the actions that were enqueued.
803 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700804 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700805 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
806 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700807 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700808}
809
Darin Petkove6ef2f82011-03-07 17:31:11 -0800810TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700811 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700812 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700813}
814
Chris Sosa76a29ae2013-07-11 17:59:24 -0700815TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700816 loop_.PostTask(FROM_HERE,
817 base::Bind(&UpdateAttempterTest::RollbackTestStart,
818 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800819 false,
820 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700821 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700822}
823
Don Garrett6646b442013-11-13 15:29:11 -0800824TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700825 loop_.PostTask(FROM_HERE,
826 base::Bind(&UpdateAttempterTest::RollbackTestStart,
827 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800828 false,
829 false));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700830 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800831}
832
Chris Sosa76a29ae2013-07-11 17:59:24 -0700833TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700834 loop_.PostTask(FROM_HERE,
835 base::Bind(&UpdateAttempterTest::RollbackTestStart,
836 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800837 true,
838 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700839 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700840}
841
Thieu Le116fda32011-04-19 11:01:54 -0700842void UpdateAttempterTest::PingOmahaTestStart() {
843 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700844 EnqueueAction(Pointee(Property(
845 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700846 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700847 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700848 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700849}
850
851TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700852 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700853 EXPECT_FALSE(attempter_.WasScheduleUpdatesCalled());
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700854 // Disable scheduling of subsequnet checks; we're using the |DefaultPolicy| in
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700855 // testing, which is more permissive than we want to handle here.
856 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700857 loop_.PostTask(FROM_HERE,
858 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
859 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700860 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700861 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700862 EXPECT_TRUE(attempter_.WasScheduleUpdatesCalled());
Thieu Le116fda32011-04-19 11:01:54 -0700863}
864
Darin Petkov18c7bce2011-06-16 14:07:00 -0700865TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800866 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700867 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700868 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700869 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700870 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
871 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700872 EXPECT_EQ(
873 static_cast<ErrorCode>(static_cast<int>(kCode) |
874 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
875 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700876}
877
878TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700879 attempter_.install_plan_.reset(new InstallPlan);
880 attempter_.install_plan_->is_resume = true;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800881 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700882 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700883 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700884 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700885 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
886 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700887 EXPECT_EQ(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800888 static_cast<ErrorCode>(static_cast<int>(kCode) |
889 static_cast<int>(ErrorCode::kResumedFlag) |
890 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700891 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700892}
893
David Zeuthen8f191b22013-08-06 12:27:50 -0700894TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
895 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700896 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700897 mock_p2p_manager.fake().SetP2PEnabled(false);
898 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
899 attempter_.UpdateEngineStarted();
900}
901
902TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
903 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700904 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700905 mock_p2p_manager.fake().SetP2PEnabled(true);
906 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
907 attempter_.UpdateEngineStarted();
908}
909
910TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
911 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700912 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700913 mock_p2p_manager.fake().SetP2PEnabled(true);
914 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700915 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700916 attempter_.UpdateEngineStarted();
917}
918
919TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700920 loop_.PostTask(FROM_HERE,
921 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
922 base::Unretained(this)));
923 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700924}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700925
David Zeuthen8f191b22013-08-06 12:27:50 -0700926void UpdateAttempterTest::P2PNotEnabledStart() {
927 // If P2P is not enabled, check that we do not attempt housekeeping
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700928 // and do not convey that P2P is to be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700929 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700930 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700931 mock_p2p_manager.fake().SetP2PEnabled(false);
932 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800933 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700934 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700935 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700936 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700937}
938
939TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700940 loop_.PostTask(FROM_HERE,
941 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
942 base::Unretained(this)));
943 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700944}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700945
David Zeuthen8f191b22013-08-06 12:27:50 -0700946void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700947 // If P2P is enabled, but starting it fails ensure we don't do
948 // any housekeeping and do not convey that P2P should be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700949 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700950 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700951 mock_p2p_manager.fake().SetP2PEnabled(true);
952 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
953 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
954 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800955 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700956 EXPECT_FALSE(actual_using_p2p_for_downloading());
957 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700958 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700959}
960
961TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700962 loop_.PostTask(
963 FROM_HERE,
964 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
965 base::Unretained(this)));
966 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700967}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700968
David Zeuthen8f191b22013-08-06 12:27:50 -0700969void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700970 // If P2P is enabled, starting it works but housekeeping fails, ensure
971 // we do not convey P2P is to be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700972 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700973 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700974 mock_p2p_manager.fake().SetP2PEnabled(true);
975 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
976 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700977 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800978 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700979 EXPECT_FALSE(actual_using_p2p_for_downloading());
980 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700981 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700982}
983
984TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700985 loop_.PostTask(FROM_HERE,
986 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
987 base::Unretained(this)));
988 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700989}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700990
David Zeuthen8f191b22013-08-06 12:27:50 -0700991void UpdateAttempterTest::P2PEnabledStart() {
992 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700993 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700994 // If P2P is enabled and starting it works, check that we performed
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700995 // housekeeping and that we convey P2P should be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700996 mock_p2p_manager.fake().SetP2PEnabled(true);
997 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
998 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700999 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001000 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001001 EXPECT_TRUE(actual_using_p2p_for_downloading());
1002 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001003 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001004}
1005
1006TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001007 loop_.PostTask(FROM_HERE,
1008 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
1009 base::Unretained(this)));
1010 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -07001011}
Alex Deymo60ca1a72015-06-18 18:19:15 -07001012
David Zeuthen8f191b22013-08-06 12:27:50 -07001013void UpdateAttempterTest::P2PEnabledInteractiveStart() {
1014 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001015 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001016 // For an interactive check, if P2P is enabled and starting it
1017 // works, check that we performed housekeeping and that we convey
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001018 // P2P should be used for sharing but NOT for downloading.
David Zeuthen8f191b22013-08-06 12:27:50 -07001019 mock_p2p_manager.fake().SetP2PEnabled(true);
1020 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
1021 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001022 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001023 attempter_.Update("",
1024 "",
1025 "",
1026 "",
1027 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001028 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001029 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001030 false,
1031 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001032 EXPECT_FALSE(actual_using_p2p_for_downloading());
1033 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001034 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001035}
1036
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001037TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001038 loop_.PostTask(
1039 FROM_HERE,
1040 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
1041 base::Unretained(this)));
1042 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001043}
1044
1045// Tests that the scatter_factor_in_seconds value is properly fetched
1046// from the device policy.
1047void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -07001048 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001049
Igor9fd76b62017-12-11 15:24:18 +01001050 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001051 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001052 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001053
1054 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001055 .WillRepeatedly(
1056 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001057
Igor9fd76b62017-12-11 15:24:18 +01001058 attempter_.policy_provider_.reset(
1059 new policy::PolicyProvider(std::move(device_policy)));
1060
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001061 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001062 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1063
Alex Deymo60ca1a72015-06-18 18:19:15 -07001064 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001065}
1066
1067TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001068 loop_.PostTask(
1069 FROM_HERE,
1070 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
1071 base::Unretained(this)));
1072 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001073}
1074
1075void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
1076 // Tests that the scatter_factor_in_seconds value is properly fetched
1077 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -07001078 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001079 FakePrefs fake_prefs;
1080 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001081
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001082 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001083
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001084 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001085
Ben Chan9abb7632014-08-07 00:10:53 -07001086 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001087
Igor9fd76b62017-12-11 15:24:18 +01001088 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001089 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001090 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001091
1092 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001093 .WillRepeatedly(
1094 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001095
Igor9fd76b62017-12-11 15:24:18 +01001096 attempter_.policy_provider_.reset(
1097 new policy::PolicyProvider(std::move(device_policy)));
1098
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001099 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001100 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1101
1102 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001103 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001104
Ben Chan9abb7632014-08-07 00:10:53 -07001105 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001106 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001107 EXPECT_EQ(initial_value - 1, new_value);
1108
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001109 EXPECT_TRUE(
1110 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001111
1112 // However, if the count is already 0, it's not decremented. Test that.
1113 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001114 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001115 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001116 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
1117 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001118 EXPECT_EQ(initial_value, new_value);
1119
Alex Deymo60ca1a72015-06-18 18:19:15 -07001120 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001121}
1122
Jay Srinivasan08fce042012-06-07 16:31:01 -07001123TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001124 loop_.PostTask(
1125 FROM_HERE,
1126 base::Bind(
1127 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
1128 base::Unretained(this)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001129 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001130}
1131
1132void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
1133 // Tests that no scattering logic is enabled if the update check
1134 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -07001135 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001136 FakePrefs fake_prefs;
1137 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001138
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001139 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001140 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001141
Adolfo Victoriad3a1e352018-07-16 11:40:47 -07001142 EXPECT_TRUE(
1143 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001144 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001145
1146 // make sure scatter_factor is non-zero as scattering is disabled
1147 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -07001148 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001149
Igor9fd76b62017-12-11 15:24:18 +01001150 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001151 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001152 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001153
1154 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001155 .WillRepeatedly(
1156 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001157
Igor9fd76b62017-12-11 15:24:18 +01001158 attempter_.policy_provider_.reset(
1159 new policy::PolicyProvider(std::move(device_policy)));
1160
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001161 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001162 attempter_.Update("",
1163 "",
1164 "",
1165 "",
1166 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001167 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001168 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001169 false,
1170 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001171 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1172
1173 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001174 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001175 EXPECT_FALSE(
1176 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -07001177 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001178 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1179 EXPECT_FALSE(
1180 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001181 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001182
Alex Deymo60ca1a72015-06-18 18:19:15 -07001183 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001184}
1185
Adolfo Victoria497044c2018-07-18 07:51:42 -07001186void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
1187 FakePrefs* prefs) {
1188 attempter_.prefs_ = prefs;
1189 fake_system_state_.set_prefs(prefs);
1190
1191 int64_t initial_value = 8;
1192 EXPECT_TRUE(
1193 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
1194 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
1195 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
1196
1197 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1198 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
1199 fake_system_state_.set_device_policy(device_policy.get());
1200 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
1201 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
1202
1203 attempter_.policy_provider_.reset(
1204 new policy::PolicyProvider(std::move(device_policy)));
1205}
1206
1207TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
1208 loop_.PostTask(
1209 FROM_HERE,
1210 base::Bind(
1211 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
1212 base::Unretained(this)));
1213 loop_.Run();
1214}
1215
1216void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
1217 // Tests that staging sets its prefs properly and turns off scattering.
1218 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1219 FakePrefs fake_prefs;
1220 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1221
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001222 attempter_.Update("", "", "", "", false, false, 0, false, false);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001223 // Check that prefs have the correct values.
1224 int64_t update_count;
1225 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
1226 int64_t waiting_time_days;
1227 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
1228 &waiting_time_days));
1229 EXPECT_GT(waiting_time_days, 0);
1230 // Update count should have been decremented.
1231 EXPECT_EQ(7, update_count);
1232 // Check that Omaha parameters were updated correctly.
1233 EXPECT_TRUE(
1234 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1235 EXPECT_TRUE(
1236 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1237 EXPECT_EQ(waiting_time_days,
1238 attempter_.omaha_request_params_->waiting_period().InDays());
1239 // Check class variables.
1240 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1241 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1242 // Check that scattering is turned off
1243 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1244 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1245
1246 ScheduleQuitMainLoop();
1247}
1248
1249void UpdateAttempterTest::CheckStagingOff() {
1250 // Check that all prefs were removed.
1251 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1252 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1253 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1254 // Check that the Omaha parameters have the correct value.
1255 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1256 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1257 attempter_.staging_wait_time_);
1258 EXPECT_FALSE(
1259 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1260 EXPECT_FALSE(
1261 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1262 // Check that scattering is turned off too.
1263 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1264}
1265
1266TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1267 loop_.PostTask(FROM_HERE,
1268 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1269 base::Unretained(this)));
1270 loop_.Run();
1271}
1272
1273void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1274 // Tests that staging is turned off when an interactive update is requested.
1275 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1276 FakePrefs fake_prefs;
1277 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1278
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001279 attempter_.Update(
1280 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001281 CheckStagingOff();
1282
1283 ScheduleQuitMainLoop();
1284}
1285
1286TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1287 loop_.PostTask(FROM_HERE,
1288 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1289 base::Unretained(this)));
1290 loop_.Run();
1291}
1292
1293void UpdateAttempterTest::StagingOffIfOobeStart() {
1294 // Tests that staging is turned off if OOBE hasn't been completed.
1295 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1296 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1297 FakePrefs fake_prefs;
1298 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1299
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001300 attempter_.Update(
1301 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001302 CheckStagingOff();
1303
1304 ScheduleQuitMainLoop();
1305}
1306
David Zeuthen985b1122013-10-09 12:13:15 -07001307// Checks that we only report daily metrics at most every 24 hours.
1308TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1309 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001310 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001311
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001312 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001313 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001314
1315 Time epoch = Time::FromInternalValue(0);
1316 fake_clock.SetWallclockTime(epoch);
1317
1318 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1319 // we should report.
1320 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1321 // We should not report again if no time has passed.
1322 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1323
1324 // We should not report if only 10 hours has passed.
1325 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1326 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1327
1328 // We should not report if only 24 hours - 1 sec has passed.
1329 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1330 TimeDelta::FromSeconds(1));
1331 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1332
1333 // We should report if 24 hours has passed.
1334 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1335 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1336
1337 // But then we should not report again..
1338 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1339
1340 // .. until another 24 hours has passed
1341 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1342 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1343 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1344 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1345 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1346
1347 // .. and another 24 hours
1348 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1349 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1350 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1351 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1352 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1353
1354 // If the span between time of reporting and present time is
1355 // negative, we report. This is in order to reset the timestamp and
1356 // avoid an edge condition whereby a distant point in the future is
1357 // in the state variable resulting in us never ever reporting again.
1358 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1359 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1360 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1361
1362 // In this case we should not update until the clock reads 71 + 24 = 95.
1363 // Check that.
1364 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1365 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1366 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1367 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1368 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001369}
1370
David Zeuthen3c55abd2013-10-14 12:48:03 -07001371TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001372 FakeClock fake_clock;
1373 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001374 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001375 FakePrefs fake_prefs;
1376 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001377 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001378
1379 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001380 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001381
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001382 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001383
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001384 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001385 EXPECT_EQ(boot_time.ToTimeT(), 42);
1386}
1387
David Pursell02c18642014-11-06 11:26:11 -08001388TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1389 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1390 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1391}
1392
1393TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1394 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001395 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001396 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1397}
1398
1399TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1400 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001401 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001402 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1403}
1404
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001405// TODO(kimjae): Follow testing pattern with params for |CheckForInstall()|.
1406// When adding, remove older tests related to |CheckForInstall()|.
1407TEST_F(UpdateAttempterTest, CheckForInstallNotIdleFails) {
1408 for (const auto status : kNonIdleUpdateStatuses) {
1409 // GIVEN a non-idle status.
1410 attempter_.status_ = status;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001411
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001412 EXPECT_FALSE(attempter_.CheckForInstall({}, ""));
1413 }
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001414}
1415
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001416TEST_F(UpdateAttempterTest, CheckForUpdateNotIdleFails) {
1417 for (const auto status : kNonIdleUpdateStatuses) {
1418 // GIVEN a non-idle status.
1419 cfu_params_.status = status;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001420
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001421 // THEN |ScheduleUpdates()| should not be called.
1422 cfu_params_.should_schedule_updates_be_called = false;
1423 // THEN result should indicate failure.
1424 cfu_params_.expected_result = false;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001425
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001426 TestCheckForUpdate();
1427 }
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001428}
1429
1430TEST_F(UpdateAttempterTest, CheckForUpdateOfficalBuildClearsSource) {
1431 // GIVEN a official build.
1432
1433 // THEN we except forced app version + forced omaha url to be cleared.
1434
1435 TestCheckForUpdate();
1436}
1437
1438TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildChangesSource) {
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001439 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001440 cfu_params_.is_official_build = false;
1441 cfu_params_.are_dev_features_enabled = true;
1442
1443 // THEN the forced app version + forced omaha url changes based on input.
1444 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1445 cfu_params_.expected_forced_omaha_url = cfu_params_.omaha_url;
1446
1447 TestCheckForUpdate();
1448}
1449
1450TEST_F(UpdateAttempterTest, CheckForUpdateOfficialBuildScheduledAUTest) {
1451 // GIVEN a scheduled autest omaha url.
1452 cfu_params_.omaha_url = "autest-scheduled";
1453
1454 // THEN forced app version is cleared.
1455 // THEN forced omaha url changes to default constant.
1456 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1457
1458 TestCheckForUpdate();
1459}
1460
1461TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildScheduledAUTest) {
1462 // GIVEN a scheduled autest omaha url.
1463 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001464 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001465 cfu_params_.is_official_build = false;
1466 cfu_params_.are_dev_features_enabled = true;
1467
1468 // THEN forced app version changes based on input.
1469 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1470 // THEN forced omaha url changes to default constant.
1471 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1472
1473 TestCheckForUpdate();
1474}
1475
1476TEST_F(UpdateAttempterTest, CheckForUpdateOfficialBuildAUTest) {
1477 // GIVEN a autest omaha url.
1478 cfu_params_.omaha_url = "autest";
1479
1480 // THEN forced app version is cleared.
1481 // THEN forced omaha url changes to default constant.
1482 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1483
1484 TestCheckForUpdate();
1485}
1486
1487TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildAUTest) {
1488 // GIVEN a autest omha url.
1489 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001490 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001491 cfu_params_.is_official_build = false;
1492 cfu_params_.are_dev_features_enabled = true;
1493
1494 // THEN forced app version changes based on input.
1495 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1496 // THEN forced omaha url changes to default constant.
1497 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1498
1499 TestCheckForUpdate();
1500}
1501
1502TEST_F(UpdateAttempterTest,
1503 CheckForUpdateNonInteractiveOfficialBuildScheduledAUTest) {
1504 // GIVEN a scheduled autest omaha url.
1505 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001506 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001507 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
1508
1509 // THEN forced app version is cleared.
1510 // THEN forced omaha url changes to default constant.
1511 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1512
1513 TestCheckForUpdate();
1514}
1515
1516TEST_F(UpdateAttempterTest,
1517 CheckForUpdateNonInteractiveUnofficialBuildScheduledAUTest) {
1518 // GIVEN a scheduled autest omaha url.
1519 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001520 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001521 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001522 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001523 cfu_params_.is_official_build = false;
1524 cfu_params_.are_dev_features_enabled = true;
1525
1526 // THEN forced app version changes based on input.
1527 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1528 // THEN forced omaha url changes to default constant.
1529 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1530
1531 TestCheckForUpdate();
1532}
1533
1534TEST_F(UpdateAttempterTest, CheckForUpdateNonInteractiveOfficialBuildAUTest) {
1535 // GIVEN a autest omaha url.
1536 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001537 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001538 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
1539
1540 // THEN forced app version is cleared.
1541 // THEN forced omaha url changes to default constant.
1542 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1543
1544 TestCheckForUpdate();
1545}
1546
1547TEST_F(UpdateAttempterTest, CheckForUpdateNonInteractiveUnofficialBuildAUTest) {
1548 // GIVEN a autest omaha url.
1549 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001550 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001551 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001552 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001553 cfu_params_.is_official_build = false;
1554 cfu_params_.are_dev_features_enabled = true;
1555
1556 // THEN forced app version changes based on input.
1557 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1558 // THEN forced omaha url changes to default constant.
1559 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1560
1561 TestCheckForUpdate();
David Pursell02c18642014-11-06 11:26:11 -08001562}
1563
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -07001564TEST_F(UpdateAttempterTest, CheckForUpdateMissingForcedCallback1) {
1565 // GIVEN a official build.
1566 // GIVEN forced callback is not set.
1567 attempter_.set_forced_update_pending_callback(nullptr);
1568
1569 // THEN we except forced app version + forced omaha url to be cleared.
1570 // THEN |ScheduleUpdates()| should not be called.
1571 cfu_params_.should_schedule_updates_be_called = false;
1572
1573 TestCheckForUpdate();
1574}
1575
1576TEST_F(UpdateAttempterTest, CheckForUpdateMissingForcedCallback2) {
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001577 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -07001578 cfu_params_.is_official_build = false;
1579 cfu_params_.are_dev_features_enabled = true;
1580 // GIVEN forced callback is not set.
1581 attempter_.set_forced_update_pending_callback(nullptr);
1582
1583 // THEN the forced app version + forced omaha url changes based on input.
1584 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1585 cfu_params_.expected_forced_omaha_url = cfu_params_.omaha_url;
1586 // THEN |ScheduleUpdates()| should not be called.
1587 cfu_params_.should_schedule_updates_be_called = false;
1588
1589 TestCheckForUpdate();
1590}
1591
Xiaochu Liu88d90382018-08-29 16:09:11 -07001592TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1593 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1594 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1595 attempter_.CheckForInstall({}, "autest");
1596 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1597
1598 attempter_.CheckForInstall({}, "autest-scheduled");
1599 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1600
1601 attempter_.CheckForInstall({}, "http://omaha.phishing");
1602 EXPECT_EQ("", attempter_.forced_omaha_url());
1603}
1604
Colin Howesac170d92018-11-20 16:29:28 -08001605TEST_F(UpdateAttempterTest, InstallSetsStatusIdle) {
1606 attempter_.CheckForInstall({}, "http://foo.bar");
1607 attempter_.status_ = UpdateStatus::DOWNLOADING;
1608 EXPECT_TRUE(attempter_.is_install_);
1609 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1610 UpdateEngineStatus status;
1611 attempter_.GetStatus(&status);
1612 // Should set status to idle after an install operation.
1613 EXPECT_EQ(UpdateStatus::IDLE, status.status);
1614}
1615
Colin Howes978c1082018-12-03 11:46:12 -08001616TEST_F(UpdateAttempterTest, RollbackAfterInstall) {
1617 attempter_.is_install_ = true;
1618 attempter_.Rollback(false);
1619 EXPECT_FALSE(attempter_.is_install_);
1620}
1621
1622TEST_F(UpdateAttempterTest, UpdateAfterInstall) {
1623 attempter_.is_install_ = true;
1624 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1625 EXPECT_FALSE(attempter_.is_install_);
1626}
1627
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001628TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001629 attempter_.CalculateUpdateParams(
1630 "", "", "", "1234", false, false, 4, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001631 EXPECT_EQ("1234",
1632 fake_system_state_.request_params()->target_version_prefix());
1633
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001634 attempter_.CalculateUpdateParams(
1635 "", "", "", "", false, 4, false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001636 EXPECT_TRUE(
1637 fake_system_state_.request_params()->target_version_prefix().empty());
1638}
1639
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001640TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1641 attempter_.CalculateUpdateParams("",
1642 "",
1643 "",
1644 "1234",
1645 /*rollback_allowed=*/true,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001646 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001647 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001648 false,
1649 false);
1650 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001651 EXPECT_EQ(4,
1652 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001653
1654 attempter_.CalculateUpdateParams("",
1655 "",
1656 "",
1657 "1234",
1658 /*rollback_allowed=*/false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001659 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001660 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001661 false,
1662 false);
1663 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001664 EXPECT_EQ(4,
1665 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001666}
1667
Aaron Wood23bd3392017-10-06 14:48:25 -07001668TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1669 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1670 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001671 OmahaResponseHandlerAction response_action(&fake_system_state_);
1672 response_action.install_plan_.version = "a.b.c.d";
1673 response_action.install_plan_.system_version = "b.c.d.e";
1674 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001675 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001676 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1677 // completed, with the deferred-update error code.
1678 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001679 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001680 {
1681 UpdateEngineStatus status;
1682 attempter_.GetStatus(&status);
1683 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001684 EXPECT_TRUE(attempter_.install_plan_);
1685 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001686 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001687 status.new_size_bytes);
1688 }
1689 // An "error" event should have been created to tell Omaha that the update is
1690 // being deferred.
1691 EXPECT_TRUE(nullptr != attempter_.error_event_);
1692 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1693 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1694 ErrorCode expected_code = static_cast<ErrorCode>(
1695 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1696 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1697 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1698 // End the processing
1699 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1700 // Validate the state of the attempter.
1701 {
1702 UpdateEngineStatus status;
1703 attempter_.GetStatus(&status);
1704 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001705 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001706 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001707 status.new_size_bytes);
1708 }
1709}
1710
1711TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
Jae Hoon Kim75daa382019-07-02 11:17:24 -07001712 // Default construction for |waiting_for_scheduled_check_| is false.
Jae Hoon Kimba2fdce2019-07-11 13:18:58 -07001713 EXPECT_FALSE(attempter_.IsBusyOrUpdateScheduled());
Aaron Wood23bd3392017-10-06 14:48:25 -07001714 // Verify in-progress update with UPDATE_AVAILABLE is running
1715 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
Jae Hoon Kimba2fdce2019-07-11 13:18:58 -07001716 EXPECT_TRUE(attempter_.IsBusyOrUpdateScheduled());
Aaron Wood23bd3392017-10-06 14:48:25 -07001717}
1718
Aaron Woodbf5a2522017-10-04 10:58:36 -07001719TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1720 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1721
1722 UpdateCheckParams params = {.updates_enabled = true};
1723 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1724
1725 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1726 attempter_.GetCurrentUpdateAttemptFlags());
1727}
1728
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001729TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1730 UpdateCheckParams params = {.updates_enabled = true,
1731 .rollback_allowed = false};
1732 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1733 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1734}
1735
1736TEST_F(UpdateAttempterTest, RollbackAllowed) {
1737 UpdateCheckParams params = {.updates_enabled = true,
1738 .rollback_allowed = true};
1739 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1740 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1741}
1742
Aaron Wood081c0232017-10-19 17:14:58 -07001743TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1744 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1745
1746 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1747 EXPECT_EQ(UpdateAttemptFlags::kNone,
1748 attempter_.GetCurrentUpdateAttemptFlags());
1749}
1750
1751TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1752 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1753
1754 // This tests that when CheckForUpdate() is called with the non-interactive
1755 // flag set, that it doesn't change the current UpdateAttemptFlags.
1756 attempter_.CheckForUpdate("",
1757 "",
1758 UpdateAttemptFlags::kFlagNonInteractive |
1759 UpdateAttemptFlags::kFlagRestrictDownload);
1760 EXPECT_EQ(UpdateAttemptFlags::kNone,
1761 attempter_.GetCurrentUpdateAttemptFlags());
1762}
1763
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001764void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1765 bool is_policy_loaded,
1766 bool expected_reset) {
1767 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1768 .WillRepeatedly(Return(true));
1769 auto mock_policy_provider =
1770 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1771 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1772 .WillRepeatedly(Return(is_consumer));
1773 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1774 .WillRepeatedly(Return(is_policy_loaded));
1775 const policy::MockDevicePolicy device_policy;
1776 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1777 .WillRepeatedly(ReturnRef(device_policy));
1778 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1779 SetRollbackHappened(false))
1780 .Times(expected_reset ? 1 : 0);
1781 attempter_.policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001782 attempter_.Update("", "", "", "", false, false, 0, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001783 ScheduleQuitMainLoop();
1784}
1785
1786TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1787 loop_.PostTask(FROM_HERE,
1788 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1789 base::Unretained(this),
1790 /*is_consumer=*/false,
1791 /*is_policy_loaded=*/false,
1792 /*expected_reset=*/false));
1793 loop_.Run();
1794}
1795
1796TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1797 loop_.PostTask(FROM_HERE,
1798 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1799 base::Unretained(this),
1800 /*is_consumer=*/true,
1801 /*is_policy_loaded=*/false,
1802 /*expected_reset=*/true));
1803 loop_.Run();
1804}
1805
1806TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1807 loop_.PostTask(FROM_HERE,
1808 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1809 base::Unretained(this),
1810 /*is_consumer=*/false,
1811 /*is_policy_loaded=*/true,
1812 /*expected_reset=*/true));
1813 loop_.Run();
1814}
1815
Marton Hunyady199152d2018-05-07 19:08:48 +02001816TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001817 attempter_.install_plan_.reset(new InstallPlan);
1818 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001819
1820 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1821 SetRollbackHappened(true))
1822 .Times(1);
1823 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1824}
1825
1826TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001827 attempter_.install_plan_.reset(new InstallPlan);
1828 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001829
1830 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1831 SetRollbackHappened(true))
1832 .Times(0);
1833 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1834}
1835
Marton Hunyadya0302682018-05-16 18:52:13 +02001836TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001837 attempter_.install_plan_.reset(new InstallPlan);
1838 attempter_.install_plan_->is_rollback = true;
1839 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001840
1841 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1842 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1843 .Times(1);
1844 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1845}
1846
1847TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001848 attempter_.install_plan_.reset(new InstallPlan);
1849 attempter_.install_plan_->is_rollback = false;
1850 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001851
1852 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1853 ReportEnterpriseRollbackMetrics(_, _))
1854 .Times(0);
1855 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1856}
1857
1858TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001859 attempter_.install_plan_.reset(new InstallPlan);
1860 attempter_.install_plan_->is_rollback = true;
1861 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001862
1863 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1864 ReportEnterpriseRollbackMetrics(false, kRollbackVersion))
1865 .Times(1);
1866 MockAction action;
1867 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1868 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1869}
1870
1871TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001872 attempter_.install_plan_.reset(new InstallPlan);
1873 attempter_.install_plan_->is_rollback = false;
1874 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001875
1876 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1877 ReportEnterpriseRollbackMetrics(_, _))
1878 .Times(0);
1879 MockAction action;
1880 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1881 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1882}
1883
May Lippert60aa3ca2018-08-15 16:55:29 -07001884TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1885 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1886 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1887 .Times(0);
1888 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1889}
1890
1891TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1892 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1893 fake_system_state_.set_device_policy(device_policy.get());
1894 // Make device policy return that this is not enterprise enrolled
1895 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1896
1897 // Ensure that the metric is not recorded.
1898 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1899 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1900 .Times(0);
1901 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1902}
1903
1904TEST_F(UpdateAttempterTest,
1905 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1906 constexpr int kDaysToUpdate = 15;
1907 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1908 fake_system_state_.set_device_policy(device_policy.get());
1909 // Make device policy return that this is enterprise enrolled
1910 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1911 // Pretend that there's a time restriction policy in place
1912 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1913 .WillOnce(Return(true));
1914
1915 FakePrefs fake_prefs;
1916 Time update_first_seen_at = Time::Now();
1917 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1918 update_first_seen_at.ToInternalValue());
1919
1920 FakeClock fake_clock;
1921 Time update_finished_at =
1922 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1923 fake_clock.SetWallclockTime(update_finished_at);
1924
1925 fake_system_state_.set_clock(&fake_clock);
1926 fake_system_state_.set_prefs(&fake_prefs);
1927
1928 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1929 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1930 .Times(1);
1931 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1932}
1933
1934TEST_F(UpdateAttempterTest,
1935 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
1936 constexpr int kDaysToUpdate = 15;
1937 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1938 fake_system_state_.set_device_policy(device_policy.get());
1939 // Make device policy return that this is enterprise enrolled
1940 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1941 // Pretend that there's no time restriction policy in place
1942 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1943 .WillOnce(Return(false));
1944
1945 FakePrefs fake_prefs;
1946 Time update_first_seen_at = Time::Now();
1947 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1948 update_first_seen_at.ToInternalValue());
1949
1950 FakeClock fake_clock;
1951 Time update_finished_at =
1952 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1953 fake_clock.SetWallclockTime(update_finished_at);
1954
1955 fake_system_state_.set_clock(&fake_clock);
1956 fake_system_state_.set_prefs(&fake_prefs);
1957
1958 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1959 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
1960 .Times(1);
1961 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1962}
1963
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001964void UpdateAttempterTest::UpdateToQuickFixBuildStart(bool set_token) {
1965 // Tests that checks if |device_quick_fix_build_token| arrives when
1966 // policy is set and the device is enterprise enrolled based on |set_token|.
1967 string token = set_token ? "some_token" : "";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001968 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1969 fake_system_state_.set_device_policy(device_policy.get());
Askar Aitzhan570ca872019-04-24 11:16:12 +02001970 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001971
1972 if (set_token)
1973 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
1974 .WillOnce(DoAll(SetArgPointee<0>(token), Return(true)));
1975 else
1976 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
1977 .WillOnce(Return(false));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001978 attempter_.policy_provider_.reset(
1979 new policy::PolicyProvider(std::move(device_policy)));
1980 attempter_.Update("", "", "", "", false, false, 0, false, false);
1981
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001982 EXPECT_EQ(token, attempter_.omaha_request_params_->autoupdate_token());
Askar Aitzhan570ca872019-04-24 11:16:12 +02001983 ScheduleQuitMainLoop();
1984}
1985
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001986TEST_F(UpdateAttempterTest,
1987 QuickFixTokenWhenDeviceIsEnterpriseEnrolledAndPolicyIsSet) {
Askar Aitzhan570ca872019-04-24 11:16:12 +02001988 loop_.PostTask(FROM_HERE,
1989 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001990 base::Unretained(this),
1991 /*set_token=*/true));
1992 loop_.Run();
1993}
1994
1995TEST_F(UpdateAttempterTest, EmptyQuickFixToken) {
1996 loop_.PostTask(FROM_HERE,
1997 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
1998 base::Unretained(this),
1999 /*set_token=*/false));
Askar Aitzhan570ca872019-04-24 11:16:12 +02002000 loop_.Run();
2001}
2002
Jae Hoon Kim75daa382019-07-02 11:17:24 -07002003TEST_F(UpdateAttempterTest, ScheduleUpdateSpamHandlerTest) {
2004 EXPECT_CALL(mock_update_manager_, AsyncPolicyRequestUpdateCheckAllowed(_, _))
2005 .Times(1);
2006 EXPECT_TRUE(attempter_.ScheduleUpdates());
2007 // Now there is an update scheduled which means that all subsequent
2008 // |ScheduleUpdates()| should fail.
2009 EXPECT_FALSE(attempter_.ScheduleUpdates());
2010 EXPECT_FALSE(attempter_.ScheduleUpdates());
2011 EXPECT_FALSE(attempter_.ScheduleUpdates());
2012}
2013
2014// Critical tests to always make sure that an update is scheduled. The following
2015// unittest(s) try and cover the correctness in synergy between
2016// |UpdateAttempter| and |UpdateManager|. Also it is good to remember the
2017// actions that happen in the flow when |UpdateAttempter| get callbacked on
2018// |OnUpdateScheduled()| -> (various cases which leads to) -> |ProcessingDone()|
2019void UpdateAttempterTest::TestOnUpdateScheduled() {
2020 // Setup
2021 attempter_.SetWaitingForScheduledCheck(true);
2022 attempter_.DisableUpdate();
2023 attempter_.DisableScheduleUpdates();
2024
2025 // Invocation
2026 attempter_.OnUpdateScheduled(ous_params_.status, ous_params_.params);
2027
2028 // Verify
2029 EXPECT_EQ(ous_params_.exit_status, attempter_.status());
2030 EXPECT_EQ(ous_params_.should_schedule_updates_be_called,
2031 attempter_.WasScheduleUpdatesCalled());
2032 EXPECT_EQ(ous_params_.should_update_be_called, attempter_.WasUpdateCalled());
2033}
2034
2035TEST_F(UpdateAttempterTest, OnUpdatesScheduledFailed) {
2036 // GIVEN failed status.
2037
2038 // THEN update should be scheduled.
2039 ous_params_.should_schedule_updates_be_called = true;
2040
2041 TestOnUpdateScheduled();
2042}
2043
2044TEST_F(UpdateAttempterTest, OnUpdatesScheduledAskMeAgainLater) {
2045 // GIVEN ask me again later status.
2046 ous_params_.status = EvalStatus::kAskMeAgainLater;
2047
2048 // THEN update should be scheduled.
2049 ous_params_.should_schedule_updates_be_called = true;
2050
2051 TestOnUpdateScheduled();
2052}
2053
2054TEST_F(UpdateAttempterTest, OnUpdatesScheduledContinue) {
2055 // GIVEN continue status.
2056 ous_params_.status = EvalStatus::kContinue;
2057
2058 // THEN update should be scheduled.
2059 ous_params_.should_schedule_updates_be_called = true;
2060
2061 TestOnUpdateScheduled();
2062}
2063
2064TEST_F(UpdateAttempterTest, OnUpdatesScheduledSucceededButUpdateDisabledFails) {
2065 // GIVEN updates disabled.
2066 ous_params_.params = {.updates_enabled = false};
2067 // GIVEN succeeded status.
2068 ous_params_.status = EvalStatus::kSucceeded;
2069
2070 // THEN update should not be scheduled.
2071
2072 TestOnUpdateScheduled();
2073}
2074
2075TEST_F(UpdateAttempterTest, OnUpdatesScheduledSucceeded) {
2076 // GIVEN updates enabled.
2077 ous_params_.params = {.updates_enabled = true};
2078 // GIVEN succeeded status.
2079 ous_params_.status = EvalStatus::kSucceeded;
2080
2081 // THEN update should be called indicating status change.
2082 ous_params_.exit_status = UpdateStatus::CHECKING_FOR_UPDATE;
2083 ous_params_.should_update_be_called = true;
2084
2085 TestOnUpdateScheduled();
2086}
2087
Darin Petkovf42cc1c2010-09-01 09:03:02 -07002088} // namespace chromeos_update_engine