blob: 5849c38389e14a1ea3ad8d4e665464aae36f4800 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/update_attempter.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Jae Hoon Kim051627a2019-09-03 12:56:32 -070021#include <limits>
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -070022#include <map>
Ben Chan02f7c1d2014-10-18 15:18:02 -070023#include <memory>
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -070024#include <string>
Jae Hoon Kimedb65502019-06-14 11:52:17 -070025#include <unordered_set>
Ben Chan02f7c1d2014-10-18 15:18:02 -070026
Ben Chan06c76a42014-09-05 08:21:06 -070027#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070028#include <base/message_loop/message_loop.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029#include <brillo/message_loops/base_message_loop.h>
30#include <brillo/message_loops/message_loop.h>
31#include <brillo/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070032#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020033#include <policy/libpolicy.h>
34#include <policy/mock_device_policy.h>
Marton Hunyadye58bddb2018-04-10 20:27:26 +020035#include <policy/mock_libpolicy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070036
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070037#include "update_engine/common/constants.h"
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080038#include "update_engine/common/dlcservice_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080039#include "update_engine/common/fake_clock.h"
40#include "update_engine/common/fake_prefs.h"
Alex Deymo14fd1ec2016-02-24 22:03:57 -080041#include "update_engine/common/mock_action.h"
42#include "update_engine/common/mock_action_processor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080043#include "update_engine/common/mock_http_fetcher.h"
44#include "update_engine/common/mock_prefs.h"
45#include "update_engine/common/platform_constants.h"
46#include "update_engine/common/prefs.h"
47#include "update_engine/common/test_utils.h"
48#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070049#include "update_engine/fake_system_state.h"
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070050#include "update_engine/libcurl_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070051#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052#include "update_engine/mock_payload_state.h"
Aaron Wood9321f502017-09-07 11:18:54 -070053#include "update_engine/mock_service_observer.h"
Jae Hoon Kim051627a2019-09-03 12:56:32 -070054#include "update_engine/omaha_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080055#include "update_engine/payload_consumer/filesystem_verifier_action.h"
56#include "update_engine/payload_consumer/install_plan.h"
57#include "update_engine/payload_consumer/payload_constants.h"
58#include "update_engine/payload_consumer/postinstall_runner_action.h"
Amin Hassani0882a512018-04-05 16:25:44 -070059#include "update_engine/update_boot_flags_action.h"
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -070060#include "update_engine/update_manager/mock_update_manager.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061
David Zeuthen985b1122013-10-09 12:13:15 -070062using base::Time;
63using base::TimeDelta;
Aaron Woodbf5a2522017-10-04 10:58:36 -070064using chromeos_update_manager::EvalStatus;
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -070065using chromeos_update_manager::MockUpdateManager;
Adolfo Victoria497044c2018-07-18 07:51:42 -070066using chromeos_update_manager::StagingSchedule;
Aaron Woodbf5a2522017-10-04 10:58:36 -070067using chromeos_update_manager::UpdateCheckParams;
Adolfo Victoria497044c2018-07-18 07:51:42 -070068using policy::DevicePolicy;
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -070069using std::map;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070070using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070071using std::unique_ptr;
Jae Hoon Kimedb65502019-06-14 11:52:17 -070072using std::unordered_set;
Xiaochu Liu88d90382018-08-29 16:09:11 -070073using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070074using testing::_;
Darin Petkov36275772010-10-01 11:40:57 -070075using testing::DoAll;
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -070076using testing::ElementsAre;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070077using testing::Field;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070078using testing::InSequence;
Jae Hoon Kimedb65502019-06-14 11:52:17 -070079using testing::Invoke;
Darin Petkov2dd01092010-10-08 15:43:05 -070080using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080081using testing::NiceMock;
Amin Hassanid3f4bea2018-04-30 14:52:40 -070082using testing::Pointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070083using testing::Property;
84using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070085using testing::ReturnPointee;
Marton Hunyadye58bddb2018-04-10 20:27:26 +020086using testing::ReturnRef;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080087using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070088using testing::SetArgPointee;
Aaron Woodbf5a2522017-10-04 10:58:36 -070089using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070090using update_engine::UpdateEngineStatus;
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -070091using update_engine::UpdateStatus;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070092
93namespace chromeos_update_engine {
94
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080095namespace {
96
Jae Hoon Kimc437ea52019-07-11 11:20:38 -070097const UpdateStatus kNonIdleUpdateStatuses[] = {
98 UpdateStatus::CHECKING_FOR_UPDATE,
99 UpdateStatus::UPDATE_AVAILABLE,
100 UpdateStatus::DOWNLOADING,
101 UpdateStatus::VERIFYING,
102 UpdateStatus::FINALIZING,
103 UpdateStatus::UPDATED_NEED_REBOOT,
104 UpdateStatus::REPORTING_ERROR_EVENT,
105 UpdateStatus::ATTEMPTING_ROLLBACK,
106 UpdateStatus::DISABLED,
107 UpdateStatus::NEED_PERMISSION_TO_UPDATE,
108};
109
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700110struct CheckForUpdateTestParams {
111 // Setups + Inputs:
112 UpdateStatus status = UpdateStatus::IDLE;
113 string app_version = "fake_app_version";
114 string omaha_url = "fake_omaha_url";
115 UpdateAttemptFlags flags = UpdateAttemptFlags::kNone;
116 bool is_official_build = true;
117 bool are_dev_features_enabled = false;
118
119 // Expects:
120 string expected_forced_app_version = "";
121 string expected_forced_omaha_url = "";
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -0700122 bool should_schedule_updates_be_called = true;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700123 bool expected_result = true;
124};
125
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700126struct OnUpdateScheduledTestParams {
127 // Setups + Inputs:
128 UpdateCheckParams params = {};
129 EvalStatus status = EvalStatus::kFailed;
130 // Expects:
131 UpdateStatus exit_status = UpdateStatus::IDLE;
132 bool should_schedule_updates_be_called = false;
133 bool should_update_be_called = false;
134};
135
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700136struct ProcessingDoneTestParams {
137 // Setups + Inputs:
138 bool is_install = false;
139 UpdateStatus status = UpdateStatus::CHECKING_FOR_UPDATE;
140 ActionProcessor* processor = nullptr;
141 ErrorCode code = ErrorCode::kSuccess;
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -0700142 map<string, OmahaRequestParams::AppParams> dlc_apps_params;
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700143
144 // Expects:
145 const bool kExpectedIsInstall = false;
146 bool should_schedule_updates_be_called = true;
147 UpdateStatus expected_exit_status = UpdateStatus::IDLE;
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -0700148 bool should_install_completed_be_called = false;
149 bool should_update_completed_be_called = false;
150 vector<string> args_to_install_completed;
151 vector<string> args_to_update_completed;
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700152};
153
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800154class MockDlcService : public DlcServiceInterface {
155 public:
156 MOCK_METHOD1(GetInstalled, bool(vector<string>*));
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -0700157 MOCK_METHOD1(InstallCompleted, bool(const vector<string>&));
158 MOCK_METHOD1(UpdateCompleted, bool(const vector<string>&));
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800159};
160
161} // namespace
162
Marton Hunyadya0302682018-05-16 18:52:13 +0200163const char kRollbackVersion[] = "10575.39.2";
164
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700165// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -0700166// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700167// methods.
168class UpdateAttempterUnderTest : public UpdateAttempter {
169 public:
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700170 explicit UpdateAttempterUnderTest(SystemState* system_state)
171 : UpdateAttempter(system_state, nullptr) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700172
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700173 void Update(const std::string& app_version,
174 const std::string& omaha_url,
175 const std::string& target_channel,
176 const std::string& target_version_prefix,
177 bool rollback_allowed,
178 bool rollback_data_save_requested,
179 int rollback_allowed_milestones,
180 bool obey_proxies,
181 bool interactive) override {
182 update_called_ = true;
183 if (do_update_) {
184 UpdateAttempter::Update(app_version,
185 omaha_url,
186 target_channel,
187 target_version_prefix,
188 rollback_allowed,
189 rollback_data_save_requested,
190 rollback_allowed_milestones,
191 obey_proxies,
192 interactive);
193 return;
194 }
195 LOG(INFO) << "[TEST] Update() disabled.";
196 status_ = UpdateStatus::CHECKING_FOR_UPDATE;
197 }
198
199 void DisableUpdate() { do_update_ = false; }
200
201 bool WasUpdateCalled() const { return update_called_; }
202
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700203 // Wrap the update scheduling method, allowing us to opt out of scheduled
204 // updates for testing purposes.
Xiaochu Liu88d90382018-08-29 16:09:11 -0700205 bool ScheduleUpdates() override {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700206 schedule_updates_called_ = true;
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700207 if (do_schedule_updates_)
208 return UpdateAttempter::ScheduleUpdates();
209 LOG(INFO) << "[TEST] Update scheduling disabled.";
210 waiting_for_scheduled_check_ = true;
Xiaochu Liu88d90382018-08-29 16:09:11 -0700211 return true;
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700212 }
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700213
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700214 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
215
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700216 // Indicates whether |ScheduleUpdates()| was called.
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700217 bool WasScheduleUpdatesCalled() const { return schedule_updates_called_; }
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700218
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700219 // Need to expose following private members of |UpdateAttempter| for tests.
220 const string& forced_app_version() const { return forced_app_version_; }
Alex Deymo60ca1a72015-06-18 18:19:15 -0700221 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -0800222
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700223 // Need to expose |waiting_for_scheduled_check_| for testing.
224 void SetWaitingForScheduledCheck(bool waiting) {
225 waiting_for_scheduled_check_ = waiting;
226 }
227
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700228 private:
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700229 // Used for overrides of |Update()|.
230 bool update_called_ = false;
231 bool do_update_ = true;
232
233 // Used for overrides of |ScheduleUpdates()|.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700234 bool schedule_updates_called_ = false;
235 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700236};
237
238class UpdateAttempterTest : public ::testing::Test {
239 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700240 UpdateAttempterTest()
Daniel Erate5f6f252017-04-20 12:09:58 -0600241 : certificate_checker_(fake_system_state_.mock_prefs(),
Alex Deymo33e91e72015-12-01 18:26:08 -0300242 &openssl_wrapper_) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700243 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700244 fake_system_state_.set_connection_manager(&mock_connection_manager);
245 fake_system_state_.set_update_attempter(&attempter_);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800246 fake_system_state_.set_dlcservice(&mock_dlcservice_);
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -0700247 fake_system_state_.set_update_manager(&mock_update_manager_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700248 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700249
Alex Deymo33e91e72015-12-01 18:26:08 -0300250 certificate_checker_.Init();
251
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800252 attempter_.set_forced_update_pending_callback(
253 new base::Callback<void(bool, bool)>(base::Bind([](bool, bool) {})));
Sen Jiange67bb5b2016-06-20 15:53:56 -0700254 // Finish initializing the attempter.
Gilad Arnold1f847232014-04-07 12:07:49 -0700255 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -0700256 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700257
Alex Deymo610277e2014-11-11 21:18:11 -0800258 void SetUp() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700259 EXPECT_NE(nullptr, attempter_.system_state_);
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700260 EXPECT_NE(nullptr, attempter_.system_state_->update_manager());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700261 EXPECT_EQ(0, attempter_.http_response_code_);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700262 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700263 EXPECT_EQ(0.0, attempter_.download_progress_);
264 EXPECT_EQ(0, attempter_.last_checked_time_);
265 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700266 EXPECT_EQ(0ULL, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800267 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700268 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700269 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700270
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700271 // Setup store/load semantics of P2P properties via the mock |PayloadState|.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700272 actual_using_p2p_for_downloading_ = false;
273 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
274 SetUsingP2PForDownloading(_))
275 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
276 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
277 GetUsingP2PForDownloading())
278 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
279 actual_using_p2p_for_sharing_ = false;
280 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
281 SetUsingP2PForSharing(_))
282 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
283 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
284 GetUsingP2PForDownloading())
285 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700286 }
287
Alex Deymo60ca1a72015-06-18 18:19:15 -0700288 public:
289 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200290
Alex Deymo60ca1a72015-06-18 18:19:15 -0700291 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800292 void UpdateTestStart();
293 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700294 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700295 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700296 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700297 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700298 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700299 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700300 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700301 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700302 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700303 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700304 void P2PEnabledHousekeepingFailsStart();
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700305 void SessionIdTestChange();
306 void SessionIdTestEnforceEmptyStrPingOmaha();
307 void SessionIdTestConsistencyInUpdateFlow();
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700308 void SessionIdTestInDownloadAction();
Amr Aboelkher21ac9962019-05-15 14:50:05 +0200309 void UpdateToQuickFixBuildStart(bool set_token);
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200310 void ResetRollbackHappenedStart(bool is_consumer,
311 bool is_policy_available,
312 bool expected_reset);
Adolfo Victoria497044c2018-07-18 07:51:42 -0700313 // Staging related callbacks.
314 void SetUpStagingTest(const StagingSchedule& schedule, FakePrefs* prefs);
315 void CheckStagingOff();
316 void StagingSetsPrefsAndTurnsOffScatteringStart();
317 void StagingOffIfInteractiveStart();
318 void StagingOffIfOobeStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700319
Gilad Arnold74b5f552014-10-07 08:17:16 -0700320 bool actual_using_p2p_for_downloading() {
321 return actual_using_p2p_for_downloading_;
322 }
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800323 bool actual_using_p2p_for_sharing() { return actual_using_p2p_for_sharing_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700324
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700325 // |CheckForUpdate()| related member functions.
326 void TestCheckForUpdate();
327
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700328 // |OnUpdateScheduled()| related member functions.
329 void TestOnUpdateScheduled();
330
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700331 // |ProcessingDone()| related member functions.
332 void TestProcessingDone();
333
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700334 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700335 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700336
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700337 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700338 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300339 OpenSSLWrapper openssl_wrapper_;
340 CertificateChecker certificate_checker_;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800341 MockDlcService mock_dlcservice_;
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -0700342 MockUpdateManager mock_update_manager_;
Alex Deymo30534502015-07-20 15:06:33 -0700343
Alex Deymo8427b4a2014-11-05 14:00:32 -0800344 NiceMock<MockActionProcessor>* processor_;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700345 NiceMock<MockPrefs>*
346 prefs_; // Shortcut to |fake_system_state_->mock_prefs()|.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800347 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700348
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700349 // |CheckForUpdate()| test params.
350 CheckForUpdateTestParams cfu_params_;
351
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700352 // |OnUpdateScheduled()| test params.
353 OnUpdateScheduledTestParams ous_params_;
354
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700355 // |ProcessingDone()| test params.
356 ProcessingDoneTestParams pd_params_;
357
Gilad Arnold74b5f552014-10-07 08:17:16 -0700358 bool actual_using_p2p_for_downloading_;
359 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700360};
361
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700362void UpdateAttempterTest::TestCheckForUpdate() {
363 // Setup
364 attempter_.status_ = cfu_params_.status;
365 fake_system_state_.fake_hardware()->SetIsOfficialBuild(
366 cfu_params_.is_official_build);
367 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(
368 cfu_params_.are_dev_features_enabled);
369
370 // Invocation
371 EXPECT_EQ(
372 cfu_params_.expected_result,
373 attempter_.CheckForUpdate(
374 cfu_params_.app_version, cfu_params_.omaha_url, cfu_params_.flags));
375
376 // Verify
377 EXPECT_EQ(cfu_params_.expected_forced_app_version,
378 attempter_.forced_app_version());
379 EXPECT_EQ(cfu_params_.expected_forced_omaha_url,
380 attempter_.forced_omaha_url());
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -0700381 EXPECT_EQ(cfu_params_.should_schedule_updates_be_called,
382 attempter_.WasScheduleUpdatesCalled());
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -0700383}
384
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700385void UpdateAttempterTest::TestProcessingDone() {
386 // Setup
387 attempter_.DisableScheduleUpdates();
388 attempter_.is_install_ = pd_params_.is_install;
389 attempter_.status_ = pd_params_.status;
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -0700390 attempter_.omaha_request_params_->set_dlc_apps_params(
391 pd_params_.dlc_apps_params);
392
393 // Expects
394 if (pd_params_.should_install_completed_be_called)
395 EXPECT_CALL(mock_dlcservice_,
396 InstallCompleted(pd_params_.args_to_install_completed))
397 .WillOnce(Return(true));
398 else
399 EXPECT_CALL(mock_dlcservice_, InstallCompleted(_)).Times(0);
400 if (pd_params_.should_update_completed_be_called)
401 EXPECT_CALL(mock_dlcservice_,
402 UpdateCompleted(pd_params_.args_to_update_completed))
403 .WillOnce(Return(true));
404 else
405 EXPECT_CALL(mock_dlcservice_, UpdateCompleted(_)).Times(0);
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -0700406
407 // Invocation
408 attempter_.ProcessingDone(pd_params_.processor, pd_params_.code);
409
410 // Verify
411 EXPECT_EQ(pd_params_.kExpectedIsInstall, attempter_.is_install_);
412 EXPECT_EQ(pd_params_.should_schedule_updates_be_called,
413 attempter_.WasScheduleUpdatesCalled());
414 EXPECT_EQ(pd_params_.expected_exit_status, attempter_.status_);
415}
416
Alex Deymo60ca1a72015-06-18 18:19:15 -0700417void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700418 loop_.PostTask(
419 FROM_HERE,
420 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
421 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700422}
423
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700424void UpdateAttempterTest::SessionIdTestChange() {
425 EXPECT_NE(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
426 const auto old_session_id = attempter_.session_id_;
427 attempter_.Update("", "", "", "", false, false, 0, false, false);
428 EXPECT_NE(old_session_id, attempter_.session_id_);
429 ScheduleQuitMainLoop();
430}
431
432TEST_F(UpdateAttempterTest, SessionIdTestChange) {
433 loop_.PostTask(FROM_HERE,
434 base::Bind(&UpdateAttempterTest::SessionIdTestChange,
435 base::Unretained(this)));
436 loop_.Run();
437}
438
439void UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha() {
440 // The |session_id_| should not be changed and should remain as an empty
441 // string when |status_| is |UPDATED_NEED_REBOOT| (only for consistency)
442 // and |PingOmaha()| is called.
443 attempter_.DisableScheduleUpdates();
444 attempter_.status_ = UpdateStatus::UPDATED_NEED_REBOOT;
445 const auto old_session_id = attempter_.session_id_;
446 auto CheckIfEmptySessionId = [](AbstractAction* aa) {
447 if (aa->Type() == OmahaRequestAction::StaticType()) {
448 EXPECT_TRUE(static_cast<OmahaRequestAction*>(aa)->session_id_.empty());
449 }
450 };
451 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
452 .WillRepeatedly(Invoke(CheckIfEmptySessionId));
453 EXPECT_CALL(*processor_, StartProcessing());
454 attempter_.PingOmaha();
455 EXPECT_EQ(old_session_id, attempter_.session_id_);
456 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status_);
457 ScheduleQuitMainLoop();
458}
459
460TEST_F(UpdateAttempterTest, SessionIdTestEnforceEmptyStrPingOmaha) {
461 loop_.PostTask(
462 FROM_HERE,
463 base::Bind(&UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha,
464 base::Unretained(this)));
465 loop_.Run();
466}
467
468void UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow() {
469 // All session IDs passed into |OmahaRequestActions| should be enforced to
470 // have the same value in |BuildUpdateActions()|.
471 unordered_set<string> session_ids;
472 // Gather all the session IDs being passed to |OmahaRequestActions|.
473 auto CheckSessionId = [&session_ids](AbstractAction* aa) {
474 if (aa->Type() == OmahaRequestAction::StaticType())
475 session_ids.insert(static_cast<OmahaRequestAction*>(aa)->session_id_);
476 };
477 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
478 .WillRepeatedly(Invoke(CheckSessionId));
479 attempter_.BuildUpdateActions(false);
480 // Validate that all the session IDs are the same.
481 EXPECT_EQ(1, session_ids.size());
482 ScheduleQuitMainLoop();
483}
484
485TEST_F(UpdateAttempterTest, SessionIdTestConsistencyInUpdateFlow) {
486 loop_.PostTask(
487 FROM_HERE,
488 base::Bind(&UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow,
489 base::Unretained(this)));
490 loop_.Run();
491}
492
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700493void UpdateAttempterTest::SessionIdTestInDownloadAction() {
494 // The session ID passed into |DownloadAction|'s |LibcurlHttpFetcher| should
495 // be enforced to be included in the HTTP header as X-Goog-Update-SessionId.
496 string header_value;
497 auto CheckSessionIdInDownloadAction = [&header_value](AbstractAction* aa) {
498 if (aa->Type() == DownloadAction::StaticType()) {
499 DownloadAction* da = static_cast<DownloadAction*>(aa);
500 EXPECT_TRUE(da->http_fetcher()->GetHeader(kXGoogleUpdateSessionId,
501 &header_value));
502 }
503 };
504 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
505 .WillRepeatedly(Invoke(CheckSessionIdInDownloadAction));
506 attempter_.BuildUpdateActions(false);
507 // Validate that X-Goog-Update_SessionId is set correctly in HTTP Header.
508 EXPECT_EQ(attempter_.session_id_, header_value);
509 ScheduleQuitMainLoop();
510}
511
512TEST_F(UpdateAttempterTest, SessionIdTestInDownloadAction) {
513 loop_.PostTask(FROM_HERE,
514 base::Bind(&UpdateAttempterTest::SessionIdTestInDownloadAction,
515 base::Unretained(this)));
516 loop_.Run();
517}
518
Darin Petkov1b003102010-11-30 10:18:36 -0800519TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700520 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800521 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700522 DownloadAction action(prefs_,
523 nullptr,
524 nullptr,
525 nullptr,
526 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700527 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800528 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700529 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700530 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700531 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700532 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800533}
534
535TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800536 MockAction action;
537 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700538 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800539 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800540 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700541 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
542 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800543}
544
Aaron Wood9321f502017-09-07 11:18:54 -0700545TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
546 // Simple test case, where all the values match (nothing was skipped)
547 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
548 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
549 uint64_t bytes_received_1 = bytes_progressed_1;
550 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
551 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
552
553 double progress_1 =
554 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
555 double progress_2 =
556 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
557
558 EXPECT_EQ(0.0, attempter_.download_progress_);
559 // This is set via inspecting the InstallPlan payloads when the
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700560 // |OmahaResponseAction| is completed.
Aaron Wood9321f502017-09-07 11:18:54 -0700561 attempter_.new_payload_size_ = bytes_total;
562 NiceMock<MockServiceObserver> observer;
563 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700564 SendStatusUpdate(AllOf(
565 Field(&UpdateEngineStatus::progress, progress_1),
566 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
567 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700568 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700569 SendStatusUpdate(AllOf(
570 Field(&UpdateEngineStatus::progress, progress_2),
571 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
572 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700573 attempter_.AddObserver(&observer);
574 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
575 EXPECT_EQ(progress_1, attempter_.download_progress_);
576 // This iteration validates that a later set of updates to the variables are
577 // properly handled (so that |getStatus()| will return the same progress info
578 // as the callback is receiving.
579 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
580 EXPECT_EQ(progress_2, attempter_.download_progress_);
581}
582
583TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700584 // The transition into |UpdateStatus::DOWNLOADING| happens when the
Aaron Wood9321f502017-09-07 11:18:54 -0700585 // first bytes are received.
586 uint64_t bytes_progressed = 1024 * 1024; // 1MB
587 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
588 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
589 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
590 // This is set via inspecting the InstallPlan payloads when the
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700591 // |OmahaResponseAction| is completed.
Aaron Wood9321f502017-09-07 11:18:54 -0700592 attempter_.new_payload_size_ = bytes_total;
593 EXPECT_EQ(0.0, attempter_.download_progress_);
594 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700595 EXPECT_CALL(observer,
596 SendStatusUpdate(AllOf(
597 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
598 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700599 attempter_.AddObserver(&observer);
600 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
601 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
602}
603
604TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
605 // There is a special case to ensure that at 100% downloaded,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700606 // |download_progress_| is updated and broadcastest.
Aaron Wood9321f502017-09-07 11:18:54 -0700607 uint64_t bytes_progressed = 0; // ignored
608 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
609 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
610 attempter_.status_ = UpdateStatus::DOWNLOADING;
611 attempter_.new_payload_size_ = bytes_total;
612 EXPECT_EQ(0.0, attempter_.download_progress_);
613 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700614 EXPECT_CALL(observer,
615 SendStatusUpdate(AllOf(
616 Field(&UpdateEngineStatus::progress, 1.0),
617 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
618 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700619 attempter_.AddObserver(&observer);
620 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
621 EXPECT_EQ(1.0, attempter_.download_progress_);
622}
623
Darin Petkov1b003102010-11-30 10:18:36 -0800624TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700625 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800626 fetcher->FailTransfer(500); // Sets the HTTP response code.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800627 OmahaRequestAction action(
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700628 &fake_system_state_, nullptr, std::move(fetcher), false, "");
Darin Petkov1b003102010-11-30 10:18:36 -0800629 ObjectCollectorAction<OmahaResponse> collector_action;
630 BondActions(&action, &collector_action);
631 OmahaResponse response;
632 response.poll_interval = 234;
633 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800634 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700635 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800636 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700637 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800638 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700639 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800640}
641
Alex Deymo30534502015-07-20 15:06:33 -0700642TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700643 FakePrefs fake_prefs;
644 string boot_id;
645 EXPECT_TRUE(utils::GetBootId(&boot_id));
646 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
647 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700648 attempter_.Init();
649 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700650}
651
652TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700653 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700654 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700655
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700656 FakeSystemState fake_system_state;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800657 OmahaRequestAction omaha_request_action(
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700658 &fake_system_state, nullptr, nullptr, false, "");
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700659 EXPECT_EQ(ErrorCode::kOmahaRequestError,
660 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700661 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800662 EXPECT_EQ(
663 ErrorCode::kOmahaResponseHandlerError,
664 GetErrorCodeForAction(&omaha_response_handler_action, ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700665 FilesystemVerifierAction filesystem_verifier_action;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800666 EXPECT_EQ(
667 ErrorCode::kFilesystemVerifierError,
668 GetErrorCodeForAction(&filesystem_verifier_action, ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300669 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700670 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800671 EXPECT_EQ(
672 ErrorCode::kPostinstallRunnerError,
673 GetErrorCodeForAction(&postinstall_runner_action, ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800674 MockAction action_mock;
675 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700676 EXPECT_EQ(ErrorCode::kError,
677 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700678}
679
Darin Petkov36275772010-10-01 11:40:57 -0700680TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700681 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800682 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700683 .WillOnce(Return(false));
684 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700685 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800686 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800687 .WillOnce(
688 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
689 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700690 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700691 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800692 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800693 .WillOnce(
694 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
695 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700696 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700697 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800698 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700699 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700700 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700701}
702
703TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800704 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700705 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700706 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
707 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800708 .WillOnce(
709 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
710 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800711 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700712 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800713 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700714 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800715 EXPECT_CALL(*prefs_,
716 SetInt64(kPrefsDeltaUpdateFailures,
717 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
718 for (int i = 0; i < 4; i++)
Darin Petkov36275772010-10-01 11:40:57 -0700719 attempter_.MarkDeltaUpdateFailure();
720}
721
Darin Petkov1b003102010-11-30 10:18:36 -0800722TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
723 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
724 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700725 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800726 .Times(0);
727 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700728 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800729 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700730 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700731 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700732 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800733 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700734 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800735}
736
737TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
738 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700739 EnqueueAction(Pointee(Property(
740 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700741 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700742 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700743 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800744 attempter_.error_event_.reset(new OmahaEvent(
745 OmahaEvent::kTypeUpdateComplete, OmahaEvent::kResultError, err));
Darin Petkov1b003102010-11-30 10:18:36 -0800746 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700747 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800748}
749
Darin Petkove6ef2f82011-03-07 17:31:11 -0800750namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700751// Actions that will be built as part of an update check.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200752vector<string> GetUpdateActionTypes() {
753 return {OmahaRequestAction::StaticType(),
754 OmahaResponseHandlerAction::StaticType(),
755 UpdateBootFlagsAction::StaticType(),
756 OmahaRequestAction::StaticType(),
757 DownloadAction::StaticType(),
758 OmahaRequestAction::StaticType(),
759 FilesystemVerifierAction::StaticType(),
760 PostinstallRunnerAction::StaticType(),
761 OmahaRequestAction::StaticType()};
762}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700763
764// Actions that will be built as part of a user-initiated rollback.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200765vector<string> GetRollbackActionTypes() {
766 return {InstallPlanAction::StaticType(),
767 PostinstallRunnerAction::StaticType()};
768}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700769
Adolfo Victoria497044c2018-07-18 07:51:42 -0700770const StagingSchedule kValidStagingSchedule = {
771 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
772
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700773} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800774
775void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700776 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700777
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700778 // Expect that the device policy is loaded by the |UpdateAttempter| at some
779 // point by calling |RefreshDevicePolicy()|.
Igor9fd76b62017-12-11 15:24:18 +0100780 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700781 EXPECT_CALL(*device_policy, LoadPolicy())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800782 .Times(testing::AtLeast(1))
783 .WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100784 attempter_.policy_provider_.reset(
785 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700786
787 {
788 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200789 for (const auto& update_action_type : GetUpdateActionTypes()) {
Alex Deymo749ecf12014-10-21 20:06:57 -0700790 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700791 EnqueueAction(Pointee(
Askar Aitzhan570ca872019-04-24 11:16:12 +0200792 Property(&AbstractAction::Type, update_action_type))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700793 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700794 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700795 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700796
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800797 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700798 loop_.PostTask(FROM_HERE,
799 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
800 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800801}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700802
Darin Petkove6ef2f82011-03-07 17:31:11 -0800803void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700804 EXPECT_EQ(0, attempter_.http_response_code());
805 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700806 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700807 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800808}
809
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800810void UpdateAttempterTest::RollbackTestStart(bool enterprise_rollback,
811 bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700812 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100813 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700814 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100815 fake_system_state_.set_device_policy(device_policy.get());
816 if (enterprise_rollback) {
817 // We return an empty owner as this is an enterprise.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800818 EXPECT_CALL(*device_policy, GetOwner(_))
819 .WillRepeatedly(DoAll(SetArgPointee<0>(string("")), Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100820 } else {
821 // We return a fake owner as this is an owned consumer device.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800822 EXPECT_CALL(*device_policy, GetOwner(_))
823 .WillRepeatedly(DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
824 Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100825 }
826
827 attempter_.policy_provider_.reset(
828 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700829
Alex Deymo763e7db2015-08-27 21:08:08 -0700830 if (valid_slot) {
831 BootControlInterface::Slot rollback_slot = 1;
832 LOG(INFO) << "Test Mark Bootable: "
833 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700834 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
835 true);
Don Garrett6646b442013-11-13 15:29:11 -0800836 }
837
Chris Sosa28e479c2013-07-12 11:39:53 -0700838 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700839
Chris Sosad38b1132014-03-25 10:43:59 -0700840 // We only allow rollback on devices that are not enterprise enrolled and
841 // which have a valid slot to rollback to.
842 if (!enterprise_rollback && valid_slot) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800843 is_rollback_allowed = true;
Chris Sosa28e479c2013-07-12 11:39:53 -0700844 }
845
Chris Sosa28e479c2013-07-12 11:39:53 -0700846 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700847 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200848 for (const auto& rollback_action_type : GetRollbackActionTypes()) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700849 EXPECT_CALL(*processor_,
Askar Aitzhan570ca872019-04-24 11:16:12 +0200850 EnqueueAction(Pointee(
851 Property(&AbstractAction::Type, rollback_action_type))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700852 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700853 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700854
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700855 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700856 loop_.PostTask(FROM_HERE,
857 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
858 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700859 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700860 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700861 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700862 }
863}
864
865void UpdateAttempterTest::RollbackTestVerify() {
866 // Verifies the actions that were enqueued.
867 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700868 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700869 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
870 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700871 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700872}
873
Darin Petkove6ef2f82011-03-07 17:31:11 -0800874TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700875 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700876 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700877}
878
Chris Sosa76a29ae2013-07-11 17:59:24 -0700879TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700880 loop_.PostTask(FROM_HERE,
881 base::Bind(&UpdateAttempterTest::RollbackTestStart,
882 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800883 false,
884 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700885 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700886}
887
Don Garrett6646b442013-11-13 15:29:11 -0800888TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700889 loop_.PostTask(FROM_HERE,
890 base::Bind(&UpdateAttempterTest::RollbackTestStart,
891 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800892 false,
893 false));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700894 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800895}
896
Chris Sosa76a29ae2013-07-11 17:59:24 -0700897TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700898 loop_.PostTask(FROM_HERE,
899 base::Bind(&UpdateAttempterTest::RollbackTestStart,
900 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800901 true,
902 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700903 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700904}
905
Thieu Le116fda32011-04-19 11:01:54 -0700906void UpdateAttempterTest::PingOmahaTestStart() {
907 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700908 EnqueueAction(Pointee(Property(
909 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700910 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700911 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700912 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700913}
914
915TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700916 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700917 EXPECT_FALSE(attempter_.WasScheduleUpdatesCalled());
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700918 // Disable scheduling of subsequnet checks; we're using the |DefaultPolicy| in
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700919 // testing, which is more permissive than we want to handle here.
920 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700921 loop_.PostTask(FROM_HERE,
922 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
923 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700924 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700925 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Jae Hoon Kim75daa382019-07-02 11:17:24 -0700926 EXPECT_TRUE(attempter_.WasScheduleUpdatesCalled());
Thieu Le116fda32011-04-19 11:01:54 -0700927}
928
Darin Petkov18c7bce2011-06-16 14:07:00 -0700929TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800930 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700931 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700932 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700933 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700934 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
935 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700936 EXPECT_EQ(
937 static_cast<ErrorCode>(static_cast<int>(kCode) |
938 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
939 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700940}
941
942TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700943 attempter_.install_plan_.reset(new InstallPlan);
944 attempter_.install_plan_->is_resume = true;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800945 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700946 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700947 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700948 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700949 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
950 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700951 EXPECT_EQ(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800952 static_cast<ErrorCode>(static_cast<int>(kCode) |
953 static_cast<int>(ErrorCode::kResumedFlag) |
954 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700955 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700956}
957
David Zeuthen8f191b22013-08-06 12:27:50 -0700958TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
959 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700960 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700961 mock_p2p_manager.fake().SetP2PEnabled(false);
962 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
963 attempter_.UpdateEngineStarted();
964}
965
966TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
967 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700968 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700969 mock_p2p_manager.fake().SetP2PEnabled(true);
970 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
971 attempter_.UpdateEngineStarted();
972}
973
974TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
975 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700976 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700977 mock_p2p_manager.fake().SetP2PEnabled(true);
978 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700979 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700980 attempter_.UpdateEngineStarted();
981}
982
983TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700984 loop_.PostTask(FROM_HERE,
985 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
986 base::Unretained(this)));
987 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700988}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700989
David Zeuthen8f191b22013-08-06 12:27:50 -0700990void UpdateAttempterTest::P2PNotEnabledStart() {
991 // If P2P is not enabled, check that we do not attempt housekeeping
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700992 // and do not convey that P2P is to be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700993 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700994 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700995 mock_p2p_manager.fake().SetP2PEnabled(false);
996 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800997 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700998 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700999 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001000 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001001}
1002
1003TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001004 loop_.PostTask(FROM_HERE,
1005 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
1006 base::Unretained(this)));
1007 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -07001008}
Alex Deymo60ca1a72015-06-18 18:19:15 -07001009
David Zeuthen8f191b22013-08-06 12:27:50 -07001010void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001011 // If P2P is enabled, but starting it fails ensure we don't do
1012 // any housekeeping and do not convey that P2P should be used.
David Zeuthen8f191b22013-08-06 12:27:50 -07001013 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001014 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001015 mock_p2p_manager.fake().SetP2PEnabled(true);
1016 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
1017 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
1018 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001019 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001020 EXPECT_FALSE(actual_using_p2p_for_downloading());
1021 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001022 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001023}
1024
1025TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001026 loop_.PostTask(
1027 FROM_HERE,
1028 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
1029 base::Unretained(this)));
1030 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -07001031}
Alex Deymo60ca1a72015-06-18 18:19:15 -07001032
David Zeuthen8f191b22013-08-06 12:27:50 -07001033void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001034 // If P2P is enabled, starting it works but housekeeping fails, ensure
1035 // we do not convey P2P is to be used.
David Zeuthen8f191b22013-08-06 12:27:50 -07001036 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001037 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001038 mock_p2p_manager.fake().SetP2PEnabled(true);
1039 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
1040 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001041 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001042 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001043 EXPECT_FALSE(actual_using_p2p_for_downloading());
1044 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001045 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001046}
1047
1048TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001049 loop_.PostTask(FROM_HERE,
1050 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
1051 base::Unretained(this)));
1052 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -07001053}
Alex Deymo60ca1a72015-06-18 18:19:15 -07001054
David Zeuthen8f191b22013-08-06 12:27:50 -07001055void UpdateAttempterTest::P2PEnabledStart() {
1056 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001057 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001058 // If P2P is enabled and starting it works, check that we performed
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001059 // housekeeping and that we convey P2P should be used.
David Zeuthen8f191b22013-08-06 12:27:50 -07001060 mock_p2p_manager.fake().SetP2PEnabled(true);
1061 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
1062 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001063 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001064 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001065 EXPECT_TRUE(actual_using_p2p_for_downloading());
1066 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001067 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001068}
1069
1070TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001071 loop_.PostTask(FROM_HERE,
1072 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
1073 base::Unretained(this)));
1074 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -07001075}
Alex Deymo60ca1a72015-06-18 18:19:15 -07001076
David Zeuthen8f191b22013-08-06 12:27:50 -07001077void UpdateAttempterTest::P2PEnabledInteractiveStart() {
1078 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001079 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001080 // For an interactive check, if P2P is enabled and starting it
1081 // works, check that we performed housekeeping and that we convey
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001082 // P2P should be used for sharing but NOT for downloading.
David Zeuthen8f191b22013-08-06 12:27:50 -07001083 mock_p2p_manager.fake().SetP2PEnabled(true);
1084 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
1085 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001086 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001087 attempter_.Update("",
1088 "",
1089 "",
1090 "",
1091 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001092 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001093 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001094 false,
1095 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -07001096 EXPECT_FALSE(actual_using_p2p_for_downloading());
1097 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001098 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -07001099}
1100
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001101TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001102 loop_.PostTask(
1103 FROM_HERE,
1104 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
1105 base::Unretained(this)));
1106 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001107}
1108
1109// Tests that the scatter_factor_in_seconds value is properly fetched
1110// from the device policy.
1111void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -07001112 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001113
Igor9fd76b62017-12-11 15:24:18 +01001114 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001115 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001116 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001117
1118 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001119 .WillRepeatedly(
1120 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001121
Igor9fd76b62017-12-11 15:24:18 +01001122 attempter_.policy_provider_.reset(
1123 new policy::PolicyProvider(std::move(device_policy)));
1124
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001125 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001126 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1127
Alex Deymo60ca1a72015-06-18 18:19:15 -07001128 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001129}
1130
1131TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001132 loop_.PostTask(
1133 FROM_HERE,
1134 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
1135 base::Unretained(this)));
1136 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001137}
1138
1139void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
1140 // Tests that the scatter_factor_in_seconds value is properly fetched
1141 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -07001142 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001143 FakePrefs fake_prefs;
1144 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001145
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001146 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001147
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001148 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001149
Ben Chan9abb7632014-08-07 00:10:53 -07001150 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001151
Igor9fd76b62017-12-11 15:24:18 +01001152 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001153 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001154 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001155
1156 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001157 .WillRepeatedly(
1158 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001159
Igor9fd76b62017-12-11 15:24:18 +01001160 attempter_.policy_provider_.reset(
1161 new policy::PolicyProvider(std::move(device_policy)));
1162
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001163 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001164 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1165
1166 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001167 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001168
Ben Chan9abb7632014-08-07 00:10:53 -07001169 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001170 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001171 EXPECT_EQ(initial_value - 1, new_value);
1172
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001173 EXPECT_TRUE(
1174 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001175
1176 // However, if the count is already 0, it's not decremented. Test that.
1177 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001178 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001179 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001180 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
1181 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001182 EXPECT_EQ(initial_value, new_value);
1183
Alex Deymo60ca1a72015-06-18 18:19:15 -07001184 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001185}
1186
Jay Srinivasan08fce042012-06-07 16:31:01 -07001187TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001188 loop_.PostTask(
1189 FROM_HERE,
1190 base::Bind(
1191 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
1192 base::Unretained(this)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001193 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001194}
1195
1196void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
1197 // Tests that no scattering logic is enabled if the update check
1198 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -07001199 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001200 FakePrefs fake_prefs;
1201 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001202
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001203 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001204 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001205
Adolfo Victoriad3a1e352018-07-16 11:40:47 -07001206 EXPECT_TRUE(
1207 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001208 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001209
1210 // make sure scatter_factor is non-zero as scattering is disabled
1211 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -07001212 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001213
Igor9fd76b62017-12-11 15:24:18 +01001214 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001215 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001216 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001217
1218 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001219 .WillRepeatedly(
1220 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001221
Igor9fd76b62017-12-11 15:24:18 +01001222 attempter_.policy_provider_.reset(
1223 new policy::PolicyProvider(std::move(device_policy)));
1224
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001225 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001226 attempter_.Update("",
1227 "",
1228 "",
1229 "",
1230 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001231 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001232 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001233 false,
1234 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001235 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1236
1237 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001238 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001239 EXPECT_FALSE(
1240 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -07001241 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001242 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1243 EXPECT_FALSE(
1244 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001245 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001246
Alex Deymo60ca1a72015-06-18 18:19:15 -07001247 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001248}
1249
Adolfo Victoria497044c2018-07-18 07:51:42 -07001250void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
1251 FakePrefs* prefs) {
1252 attempter_.prefs_ = prefs;
1253 fake_system_state_.set_prefs(prefs);
1254
1255 int64_t initial_value = 8;
1256 EXPECT_TRUE(
1257 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
1258 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
1259 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
1260
1261 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1262 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
1263 fake_system_state_.set_device_policy(device_policy.get());
1264 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
1265 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
1266
1267 attempter_.policy_provider_.reset(
1268 new policy::PolicyProvider(std::move(device_policy)));
1269}
1270
1271TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
1272 loop_.PostTask(
1273 FROM_HERE,
1274 base::Bind(
1275 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
1276 base::Unretained(this)));
1277 loop_.Run();
1278}
1279
1280void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
1281 // Tests that staging sets its prefs properly and turns off scattering.
1282 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1283 FakePrefs fake_prefs;
1284 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1285
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001286 attempter_.Update("", "", "", "", false, false, 0, false, false);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001287 // Check that prefs have the correct values.
1288 int64_t update_count;
1289 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
1290 int64_t waiting_time_days;
1291 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
1292 &waiting_time_days));
1293 EXPECT_GT(waiting_time_days, 0);
1294 // Update count should have been decremented.
1295 EXPECT_EQ(7, update_count);
1296 // Check that Omaha parameters were updated correctly.
1297 EXPECT_TRUE(
1298 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1299 EXPECT_TRUE(
1300 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1301 EXPECT_EQ(waiting_time_days,
1302 attempter_.omaha_request_params_->waiting_period().InDays());
1303 // Check class variables.
1304 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1305 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1306 // Check that scattering is turned off
1307 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1308 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1309
1310 ScheduleQuitMainLoop();
1311}
1312
1313void UpdateAttempterTest::CheckStagingOff() {
1314 // Check that all prefs were removed.
1315 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1316 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1317 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1318 // Check that the Omaha parameters have the correct value.
1319 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1320 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1321 attempter_.staging_wait_time_);
1322 EXPECT_FALSE(
1323 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1324 EXPECT_FALSE(
1325 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1326 // Check that scattering is turned off too.
1327 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1328}
1329
1330TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1331 loop_.PostTask(FROM_HERE,
1332 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1333 base::Unretained(this)));
1334 loop_.Run();
1335}
1336
1337void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1338 // Tests that staging is turned off when an interactive update is requested.
1339 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1340 FakePrefs fake_prefs;
1341 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1342
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001343 attempter_.Update(
1344 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001345 CheckStagingOff();
1346
1347 ScheduleQuitMainLoop();
1348}
1349
1350TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1351 loop_.PostTask(FROM_HERE,
1352 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1353 base::Unretained(this)));
1354 loop_.Run();
1355}
1356
1357void UpdateAttempterTest::StagingOffIfOobeStart() {
1358 // Tests that staging is turned off if OOBE hasn't been completed.
1359 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1360 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1361 FakePrefs fake_prefs;
1362 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1363
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001364 attempter_.Update(
1365 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001366 CheckStagingOff();
1367
1368 ScheduleQuitMainLoop();
1369}
1370
David Zeuthen985b1122013-10-09 12:13:15 -07001371// Checks that we only report daily metrics at most every 24 hours.
1372TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1373 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001374 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001375
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001376 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001377 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001378
1379 Time epoch = Time::FromInternalValue(0);
1380 fake_clock.SetWallclockTime(epoch);
1381
1382 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1383 // we should report.
1384 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1385 // We should not report again if no time has passed.
1386 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1387
1388 // We should not report if only 10 hours has passed.
1389 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1390 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1391
1392 // We should not report if only 24 hours - 1 sec has passed.
1393 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1394 TimeDelta::FromSeconds(1));
1395 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1396
1397 // We should report if 24 hours has passed.
1398 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1399 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1400
1401 // But then we should not report again..
1402 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1403
1404 // .. until another 24 hours has passed
1405 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1406 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1407 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1408 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1409 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1410
1411 // .. and another 24 hours
1412 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1413 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1414 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1415 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1416 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1417
1418 // If the span between time of reporting and present time is
1419 // negative, we report. This is in order to reset the timestamp and
1420 // avoid an edge condition whereby a distant point in the future is
1421 // in the state variable resulting in us never ever reporting again.
1422 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1423 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1424 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1425
1426 // In this case we should not update until the clock reads 71 + 24 = 95.
1427 // Check that.
1428 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1429 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1430 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1431 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1432 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001433}
1434
David Zeuthen3c55abd2013-10-14 12:48:03 -07001435TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001436 FakeClock fake_clock;
1437 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001438 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001439 FakePrefs fake_prefs;
1440 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001441 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001442
1443 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001444 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001445
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001446 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001447
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001448 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001449 EXPECT_EQ(boot_time.ToTimeT(), 42);
1450}
1451
David Pursell02c18642014-11-06 11:26:11 -08001452TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1453 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1454 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1455}
1456
1457TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1458 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001459 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001460 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1461}
1462
1463TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1464 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001465 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001466 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1467}
1468
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001469// TODO(kimjae): Follow testing pattern with params for |CheckForInstall()|.
1470// When adding, remove older tests related to |CheckForInstall()|.
1471TEST_F(UpdateAttempterTest, CheckForInstallNotIdleFails) {
1472 for (const auto status : kNonIdleUpdateStatuses) {
1473 // GIVEN a non-idle status.
1474 attempter_.status_ = status;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001475
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001476 EXPECT_FALSE(attempter_.CheckForInstall({}, ""));
1477 }
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001478}
1479
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001480TEST_F(UpdateAttempterTest, CheckForUpdateNotIdleFails) {
1481 for (const auto status : kNonIdleUpdateStatuses) {
1482 // GIVEN a non-idle status.
1483 cfu_params_.status = status;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001484
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001485 // THEN |ScheduleUpdates()| should not be called.
1486 cfu_params_.should_schedule_updates_be_called = false;
1487 // THEN result should indicate failure.
1488 cfu_params_.expected_result = false;
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001489
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001490 TestCheckForUpdate();
1491 }
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001492}
1493
1494TEST_F(UpdateAttempterTest, CheckForUpdateOfficalBuildClearsSource) {
1495 // GIVEN a official build.
1496
1497 // THEN we except forced app version + forced omaha url to be cleared.
1498
1499 TestCheckForUpdate();
1500}
1501
1502TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildChangesSource) {
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001503 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001504 cfu_params_.is_official_build = false;
1505 cfu_params_.are_dev_features_enabled = true;
1506
1507 // THEN the forced app version + forced omaha url changes based on input.
1508 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1509 cfu_params_.expected_forced_omaha_url = cfu_params_.omaha_url;
1510
1511 TestCheckForUpdate();
1512}
1513
1514TEST_F(UpdateAttempterTest, CheckForUpdateOfficialBuildScheduledAUTest) {
1515 // GIVEN a scheduled autest omaha url.
1516 cfu_params_.omaha_url = "autest-scheduled";
1517
1518 // THEN forced app version is cleared.
1519 // THEN forced omaha url changes to default constant.
1520 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1521
1522 TestCheckForUpdate();
1523}
1524
1525TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildScheduledAUTest) {
1526 // GIVEN a scheduled autest omaha url.
1527 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001528 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001529 cfu_params_.is_official_build = false;
1530 cfu_params_.are_dev_features_enabled = true;
1531
1532 // THEN forced app version changes based on input.
1533 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1534 // THEN forced omaha url changes to default constant.
1535 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1536
1537 TestCheckForUpdate();
1538}
1539
1540TEST_F(UpdateAttempterTest, CheckForUpdateOfficialBuildAUTest) {
1541 // GIVEN a autest omaha url.
1542 cfu_params_.omaha_url = "autest";
1543
1544 // THEN forced app version is cleared.
1545 // THEN forced omaha url changes to default constant.
1546 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1547
1548 TestCheckForUpdate();
1549}
1550
1551TEST_F(UpdateAttempterTest, CheckForUpdateUnofficialBuildAUTest) {
1552 // GIVEN a autest omha url.
1553 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001554 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001555 cfu_params_.is_official_build = false;
1556 cfu_params_.are_dev_features_enabled = true;
1557
1558 // THEN forced app version changes based on input.
1559 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1560 // THEN forced omaha url changes to default constant.
1561 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1562
1563 TestCheckForUpdate();
1564}
1565
1566TEST_F(UpdateAttempterTest,
1567 CheckForUpdateNonInteractiveOfficialBuildScheduledAUTest) {
1568 // GIVEN a scheduled autest omaha url.
1569 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001570 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001571 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
1572
1573 // THEN forced app version is cleared.
1574 // THEN forced omaha url changes to default constant.
1575 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1576
1577 TestCheckForUpdate();
1578}
1579
1580TEST_F(UpdateAttempterTest,
1581 CheckForUpdateNonInteractiveUnofficialBuildScheduledAUTest) {
1582 // GIVEN a scheduled autest omaha url.
1583 cfu_params_.omaha_url = "autest-scheduled";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001584 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001585 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001586 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001587 cfu_params_.is_official_build = false;
1588 cfu_params_.are_dev_features_enabled = true;
1589
1590 // THEN forced app version changes based on input.
1591 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1592 // THEN forced omaha url changes to default constant.
1593 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1594
1595 TestCheckForUpdate();
1596}
1597
1598TEST_F(UpdateAttempterTest, CheckForUpdateNonInteractiveOfficialBuildAUTest) {
1599 // GIVEN a autest omaha url.
1600 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001601 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001602 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
1603
1604 // THEN forced app version is cleared.
1605 // THEN forced omaha url changes to default constant.
1606 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1607
1608 TestCheckForUpdate();
1609}
1610
1611TEST_F(UpdateAttempterTest, CheckForUpdateNonInteractiveUnofficialBuildAUTest) {
1612 // GIVEN a autest omaha url.
1613 cfu_params_.omaha_url = "autest";
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001614 // GIVEN a noninteractive update.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001615 cfu_params_.flags = UpdateAttemptFlags::kFlagNonInteractive;
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001616 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim51ea9ae2019-07-03 16:56:30 -07001617 cfu_params_.is_official_build = false;
1618 cfu_params_.are_dev_features_enabled = true;
1619
1620 // THEN forced app version changes based on input.
1621 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1622 // THEN forced omaha url changes to default constant.
1623 cfu_params_.expected_forced_omaha_url = constants::kOmahaDefaultAUTestURL;
1624
1625 TestCheckForUpdate();
David Pursell02c18642014-11-06 11:26:11 -08001626}
1627
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -07001628TEST_F(UpdateAttempterTest, CheckForUpdateMissingForcedCallback1) {
1629 // GIVEN a official build.
1630 // GIVEN forced callback is not set.
1631 attempter_.set_forced_update_pending_callback(nullptr);
1632
1633 // THEN we except forced app version + forced omaha url to be cleared.
1634 // THEN |ScheduleUpdates()| should not be called.
1635 cfu_params_.should_schedule_updates_be_called = false;
1636
1637 TestCheckForUpdate();
1638}
1639
1640TEST_F(UpdateAttempterTest, CheckForUpdateMissingForcedCallback2) {
Jae Hoon Kimc437ea52019-07-11 11:20:38 -07001641 // GIVEN a nonofficial build with dev features enabled.
Jae Hoon Kim2b73ac22019-07-02 11:17:24 -07001642 cfu_params_.is_official_build = false;
1643 cfu_params_.are_dev_features_enabled = true;
1644 // GIVEN forced callback is not set.
1645 attempter_.set_forced_update_pending_callback(nullptr);
1646
1647 // THEN the forced app version + forced omaha url changes based on input.
1648 cfu_params_.expected_forced_app_version = cfu_params_.app_version;
1649 cfu_params_.expected_forced_omaha_url = cfu_params_.omaha_url;
1650 // THEN |ScheduleUpdates()| should not be called.
1651 cfu_params_.should_schedule_updates_be_called = false;
1652
1653 TestCheckForUpdate();
1654}
1655
Xiaochu Liu88d90382018-08-29 16:09:11 -07001656TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1657 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1658 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1659 attempter_.CheckForInstall({}, "autest");
1660 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1661
1662 attempter_.CheckForInstall({}, "autest-scheduled");
1663 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1664
1665 attempter_.CheckForInstall({}, "http://omaha.phishing");
1666 EXPECT_EQ("", attempter_.forced_omaha_url());
1667}
1668
Colin Howesac170d92018-11-20 16:29:28 -08001669TEST_F(UpdateAttempterTest, InstallSetsStatusIdle) {
1670 attempter_.CheckForInstall({}, "http://foo.bar");
1671 attempter_.status_ = UpdateStatus::DOWNLOADING;
1672 EXPECT_TRUE(attempter_.is_install_);
1673 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1674 UpdateEngineStatus status;
1675 attempter_.GetStatus(&status);
1676 // Should set status to idle after an install operation.
1677 EXPECT_EQ(UpdateStatus::IDLE, status.status);
1678}
1679
Colin Howes978c1082018-12-03 11:46:12 -08001680TEST_F(UpdateAttempterTest, RollbackAfterInstall) {
1681 attempter_.is_install_ = true;
1682 attempter_.Rollback(false);
1683 EXPECT_FALSE(attempter_.is_install_);
1684}
1685
1686TEST_F(UpdateAttempterTest, UpdateAfterInstall) {
1687 attempter_.is_install_ = true;
1688 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1689 EXPECT_FALSE(attempter_.is_install_);
1690}
1691
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001692TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001693 attempter_.CalculateUpdateParams(
1694 "", "", "", "1234", false, false, 4, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001695 EXPECT_EQ("1234",
1696 fake_system_state_.request_params()->target_version_prefix());
1697
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001698 attempter_.CalculateUpdateParams(
1699 "", "", "", "", false, 4, false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001700 EXPECT_TRUE(
1701 fake_system_state_.request_params()->target_version_prefix().empty());
1702}
1703
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001704TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1705 attempter_.CalculateUpdateParams("",
1706 "",
1707 "",
1708 "1234",
1709 /*rollback_allowed=*/true,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001710 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001711 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001712 false,
1713 false);
1714 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001715 EXPECT_EQ(4,
1716 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001717
1718 attempter_.CalculateUpdateParams("",
1719 "",
1720 "",
1721 "1234",
1722 /*rollback_allowed=*/false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001723 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001724 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001725 false,
1726 false);
1727 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001728 EXPECT_EQ(4,
1729 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001730}
1731
Aaron Wood23bd3392017-10-06 14:48:25 -07001732TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1733 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1734 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001735 OmahaResponseHandlerAction response_action(&fake_system_state_);
1736 response_action.install_plan_.version = "a.b.c.d";
1737 response_action.install_plan_.system_version = "b.c.d.e";
1738 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001739 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001740 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1741 // completed, with the deferred-update error code.
1742 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001743 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001744 {
1745 UpdateEngineStatus status;
1746 attempter_.GetStatus(&status);
1747 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001748 EXPECT_TRUE(attempter_.install_plan_);
1749 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001750 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001751 status.new_size_bytes);
1752 }
1753 // An "error" event should have been created to tell Omaha that the update is
1754 // being deferred.
1755 EXPECT_TRUE(nullptr != attempter_.error_event_);
1756 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1757 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1758 ErrorCode expected_code = static_cast<ErrorCode>(
1759 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1760 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1761 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1762 // End the processing
1763 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1764 // Validate the state of the attempter.
1765 {
1766 UpdateEngineStatus status;
1767 attempter_.GetStatus(&status);
1768 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001769 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001770 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001771 status.new_size_bytes);
1772 }
1773}
1774
1775TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
Jae Hoon Kim75daa382019-07-02 11:17:24 -07001776 // Default construction for |waiting_for_scheduled_check_| is false.
Jae Hoon Kimba2fdce2019-07-11 13:18:58 -07001777 EXPECT_FALSE(attempter_.IsBusyOrUpdateScheduled());
Aaron Wood23bd3392017-10-06 14:48:25 -07001778 // Verify in-progress update with UPDATE_AVAILABLE is running
1779 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
Jae Hoon Kimba2fdce2019-07-11 13:18:58 -07001780 EXPECT_TRUE(attempter_.IsBusyOrUpdateScheduled());
Aaron Wood23bd3392017-10-06 14:48:25 -07001781}
1782
Aaron Woodbf5a2522017-10-04 10:58:36 -07001783TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1784 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1785
1786 UpdateCheckParams params = {.updates_enabled = true};
1787 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1788
1789 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1790 attempter_.GetCurrentUpdateAttemptFlags());
1791}
1792
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001793TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1794 UpdateCheckParams params = {.updates_enabled = true,
1795 .rollback_allowed = false};
1796 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1797 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1798}
1799
1800TEST_F(UpdateAttempterTest, RollbackAllowed) {
1801 UpdateCheckParams params = {.updates_enabled = true,
1802 .rollback_allowed = true};
1803 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1804 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1805}
1806
Aaron Wood081c0232017-10-19 17:14:58 -07001807TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1808 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1809
1810 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1811 EXPECT_EQ(UpdateAttemptFlags::kNone,
1812 attempter_.GetCurrentUpdateAttemptFlags());
1813}
1814
1815TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1816 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1817
1818 // This tests that when CheckForUpdate() is called with the non-interactive
1819 // flag set, that it doesn't change the current UpdateAttemptFlags.
1820 attempter_.CheckForUpdate("",
1821 "",
1822 UpdateAttemptFlags::kFlagNonInteractive |
1823 UpdateAttemptFlags::kFlagRestrictDownload);
1824 EXPECT_EQ(UpdateAttemptFlags::kNone,
1825 attempter_.GetCurrentUpdateAttemptFlags());
1826}
1827
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001828void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1829 bool is_policy_loaded,
1830 bool expected_reset) {
1831 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1832 .WillRepeatedly(Return(true));
1833 auto mock_policy_provider =
1834 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1835 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1836 .WillRepeatedly(Return(is_consumer));
1837 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1838 .WillRepeatedly(Return(is_policy_loaded));
1839 const policy::MockDevicePolicy device_policy;
1840 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1841 .WillRepeatedly(ReturnRef(device_policy));
1842 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1843 SetRollbackHappened(false))
1844 .Times(expected_reset ? 1 : 0);
1845 attempter_.policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001846 attempter_.Update("", "", "", "", false, false, 0, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001847 ScheduleQuitMainLoop();
1848}
1849
1850TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1851 loop_.PostTask(FROM_HERE,
1852 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1853 base::Unretained(this),
1854 /*is_consumer=*/false,
1855 /*is_policy_loaded=*/false,
1856 /*expected_reset=*/false));
1857 loop_.Run();
1858}
1859
1860TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1861 loop_.PostTask(FROM_HERE,
1862 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1863 base::Unretained(this),
1864 /*is_consumer=*/true,
1865 /*is_policy_loaded=*/false,
1866 /*expected_reset=*/true));
1867 loop_.Run();
1868}
1869
1870TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1871 loop_.PostTask(FROM_HERE,
1872 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1873 base::Unretained(this),
1874 /*is_consumer=*/false,
1875 /*is_policy_loaded=*/true,
1876 /*expected_reset=*/true));
1877 loop_.Run();
1878}
1879
Marton Hunyady199152d2018-05-07 19:08:48 +02001880TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001881 attempter_.install_plan_.reset(new InstallPlan);
1882 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001883
1884 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1885 SetRollbackHappened(true))
1886 .Times(1);
1887 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1888}
1889
1890TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001891 attempter_.install_plan_.reset(new InstallPlan);
1892 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001893
1894 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1895 SetRollbackHappened(true))
1896 .Times(0);
1897 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1898}
1899
Marton Hunyadya0302682018-05-16 18:52:13 +02001900TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001901 attempter_.install_plan_.reset(new InstallPlan);
1902 attempter_.install_plan_->is_rollback = true;
1903 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001904
1905 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1906 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1907 .Times(1);
1908 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1909}
1910
1911TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001912 attempter_.install_plan_.reset(new InstallPlan);
1913 attempter_.install_plan_->is_rollback = false;
1914 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001915
1916 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1917 ReportEnterpriseRollbackMetrics(_, _))
1918 .Times(0);
1919 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1920}
1921
1922TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001923 attempter_.install_plan_.reset(new InstallPlan);
1924 attempter_.install_plan_->is_rollback = true;
1925 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001926
1927 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1928 ReportEnterpriseRollbackMetrics(false, kRollbackVersion))
1929 .Times(1);
1930 MockAction action;
1931 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1932 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1933}
1934
1935TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001936 attempter_.install_plan_.reset(new InstallPlan);
1937 attempter_.install_plan_->is_rollback = false;
1938 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001939
1940 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1941 ReportEnterpriseRollbackMetrics(_, _))
1942 .Times(0);
1943 MockAction action;
1944 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1945 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1946}
1947
May Lippert60aa3ca2018-08-15 16:55:29 -07001948TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1949 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1950 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1951 .Times(0);
1952 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1953}
1954
1955TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1956 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1957 fake_system_state_.set_device_policy(device_policy.get());
1958 // Make device policy return that this is not enterprise enrolled
1959 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1960
1961 // Ensure that the metric is not recorded.
1962 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1963 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1964 .Times(0);
1965 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1966}
1967
1968TEST_F(UpdateAttempterTest,
1969 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1970 constexpr int kDaysToUpdate = 15;
1971 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1972 fake_system_state_.set_device_policy(device_policy.get());
1973 // Make device policy return that this is enterprise enrolled
1974 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1975 // Pretend that there's a time restriction policy in place
1976 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1977 .WillOnce(Return(true));
1978
1979 FakePrefs fake_prefs;
1980 Time update_first_seen_at = Time::Now();
1981 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1982 update_first_seen_at.ToInternalValue());
1983
1984 FakeClock fake_clock;
1985 Time update_finished_at =
1986 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1987 fake_clock.SetWallclockTime(update_finished_at);
1988
1989 fake_system_state_.set_clock(&fake_clock);
1990 fake_system_state_.set_prefs(&fake_prefs);
1991
1992 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1993 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1994 .Times(1);
1995 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1996}
1997
1998TEST_F(UpdateAttempterTest,
1999 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
2000 constexpr int kDaysToUpdate = 15;
2001 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
2002 fake_system_state_.set_device_policy(device_policy.get());
2003 // Make device policy return that this is enterprise enrolled
2004 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
2005 // Pretend that there's no time restriction policy in place
2006 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
2007 .WillOnce(Return(false));
2008
2009 FakePrefs fake_prefs;
2010 Time update_first_seen_at = Time::Now();
2011 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
2012 update_first_seen_at.ToInternalValue());
2013
2014 FakeClock fake_clock;
2015 Time update_finished_at =
2016 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
2017 fake_clock.SetWallclockTime(update_finished_at);
2018
2019 fake_system_state_.set_clock(&fake_clock);
2020 fake_system_state_.set_prefs(&fake_prefs);
2021
2022 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
2023 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
2024 .Times(1);
2025 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
2026}
2027
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002028TEST_F(UpdateAttempterTest, ProcessingDoneUpdated) {
2029 // GIVEN an update finished.
2030
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -07002031 // THEN update_engine should call update completion.
2032 pd_params_.should_update_completed_be_called = true;
2033 // THEN need reboot since update applied.
2034 pd_params_.expected_exit_status = UpdateStatus::UPDATED_NEED_REBOOT;
2035 // THEN install indication should be false.
2036
2037 TestProcessingDone();
2038}
2039
2040TEST_F(UpdateAttempterTest, ProcessingDoneUpdatedDlcFilter) {
2041 // GIVEN an update finished.
2042 // GIVEN DLC |AppParams| list.
2043 auto dlc_1 = "dlc_1", dlc_2 = "dlc_2";
2044 pd_params_.dlc_apps_params = {{dlc_1, {.name = dlc_1, .updated = false}},
2045 {dlc_2, {.name = dlc_2}}};
2046
2047 // THEN update_engine should call update completion.
2048 pd_params_.should_update_completed_be_called = true;
2049 pd_params_.args_to_update_completed = {dlc_2};
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002050 // THEN need reboot since update applied.
2051 pd_params_.expected_exit_status = UpdateStatus::UPDATED_NEED_REBOOT;
2052 // THEN install indication should be false.
2053
2054 TestProcessingDone();
2055}
2056
2057TEST_F(UpdateAttempterTest, ProcessingDoneInstalled) {
2058 // GIVEN an install finished.
2059 pd_params_.is_install = true;
2060
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -07002061 // THEN update_engine should call install completion.
2062 pd_params_.should_install_completed_be_called = true;
2063 // THEN go idle.
2064 // THEN install indication should be false.
2065
2066 TestProcessingDone();
2067}
2068
2069TEST_F(UpdateAttempterTest, ProcessingDoneInstalledDlcFilter) {
2070 // GIVEN an install finished.
2071 pd_params_.is_install = true;
2072 // GIVEN DLC |AppParams| list.
2073 auto dlc_1 = "dlc_1", dlc_2 = "dlc_2";
2074 pd_params_.dlc_apps_params = {{dlc_1, {.name = dlc_1, .updated = false}},
2075 {dlc_2, {.name = dlc_2}}};
2076
2077 // THEN update_engine should call install completion.
2078 pd_params_.should_install_completed_be_called = true;
2079 pd_params_.args_to_install_completed = {dlc_2};
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002080 // THEN go idle.
2081 // THEN install indication should be false.
2082
2083 TestProcessingDone();
2084}
2085
2086TEST_F(UpdateAttempterTest, ProcessingDoneInstallReportingError) {
2087 // GIVEN an install finished.
2088 pd_params_.is_install = true;
2089 // GIVEN a reporting error occurred.
2090 pd_params_.status = UpdateStatus::REPORTING_ERROR_EVENT;
2091
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -07002092 // THEN update_engine should not call install completion.
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002093 // THEN go idle.
2094 // THEN install indication should be false.
2095
2096 TestProcessingDone();
2097}
2098
2099TEST_F(UpdateAttempterTest, ProcessingDoneNoUpdate) {
2100 // GIVEN an update finished.
2101 // GIVEN an action error occured.
2102 pd_params_.code = ErrorCode::kNoUpdate;
2103
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -07002104 // THEN update_engine should not call update completion.
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002105 // THEN go idle.
2106 // THEN install indication should be false.
2107
2108 TestProcessingDone();
2109}
2110
2111TEST_F(UpdateAttempterTest, ProcessingDoneNoInstall) {
2112 // GIVEN an install finished.
2113 pd_params_.is_install = true;
2114 // GIVEN an action error occured.
2115 pd_params_.code = ErrorCode::kNoUpdate;
2116
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -07002117 // THEN update_engine should not call install completion.
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002118 // THEN go idle.
2119 // THEN install indication should be false.
2120
2121 TestProcessingDone();
2122}
2123
2124TEST_F(UpdateAttempterTest, ProcessingDoneUpdateError) {
2125 // GIVEN an update finished.
2126 // GIVEN an action error occured.
2127 pd_params_.code = ErrorCode::kError;
2128 // GIVEN an event error is set.
2129 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
2130 OmahaEvent::kResultError,
2131 ErrorCode::kError));
2132
2133 // THEN indicate a error event.
2134 pd_params_.expected_exit_status = UpdateStatus::REPORTING_ERROR_EVENT;
2135 // THEN install indication should be false.
2136
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -07002137 // THEN update_engine should not call update completion.
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002138 // THEN expect critical actions of |ScheduleErrorEventAction()|.
2139 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))).Times(1);
2140 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
2141 // THEN |ScheduleUpdates()| will be called next |ProcessingDone()| so skip.
2142 pd_params_.should_schedule_updates_be_called = false;
2143
2144 TestProcessingDone();
2145}
2146
2147TEST_F(UpdateAttempterTest, ProcessingDoneInstallError) {
2148 // GIVEN an install finished.
2149 pd_params_.is_install = true;
2150 // GIVEN an action error occured.
2151 pd_params_.code = ErrorCode::kError;
2152 // GIVEN an event error is set.
2153 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
2154 OmahaEvent::kResultError,
2155 ErrorCode::kError));
2156
2157 // THEN indicate a error event.
2158 pd_params_.expected_exit_status = UpdateStatus::REPORTING_ERROR_EVENT;
2159 // THEN install indication should be false.
2160
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -07002161 // THEN update_engine should not call install completion.
Jae Hoon Kimed3fcc02019-07-11 14:35:38 -07002162 // THEN expect critical actions of |ScheduleErrorEventAction()|.
2163 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_))).Times(1);
2164 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
2165 // THEN |ScheduleUpdates()| will be called next |ProcessingDone()| so skip.
2166 pd_params_.should_schedule_updates_be_called = false;
2167
2168 TestProcessingDone();
2169}
2170
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002171void UpdateAttempterTest::UpdateToQuickFixBuildStart(bool set_token) {
2172 // Tests that checks if |device_quick_fix_build_token| arrives when
2173 // policy is set and the device is enterprise enrolled based on |set_token|.
2174 string token = set_token ? "some_token" : "";
Askar Aitzhan570ca872019-04-24 11:16:12 +02002175 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
2176 fake_system_state_.set_device_policy(device_policy.get());
Askar Aitzhan570ca872019-04-24 11:16:12 +02002177 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002178
2179 if (set_token)
2180 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
2181 .WillOnce(DoAll(SetArgPointee<0>(token), Return(true)));
2182 else
2183 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
2184 .WillOnce(Return(false));
Askar Aitzhan570ca872019-04-24 11:16:12 +02002185 attempter_.policy_provider_.reset(
2186 new policy::PolicyProvider(std::move(device_policy)));
2187 attempter_.Update("", "", "", "", false, false, 0, false, false);
2188
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002189 EXPECT_EQ(token, attempter_.omaha_request_params_->autoupdate_token());
Askar Aitzhan570ca872019-04-24 11:16:12 +02002190 ScheduleQuitMainLoop();
2191}
2192
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002193TEST_F(UpdateAttempterTest,
2194 QuickFixTokenWhenDeviceIsEnterpriseEnrolledAndPolicyIsSet) {
Askar Aitzhan570ca872019-04-24 11:16:12 +02002195 loop_.PostTask(FROM_HERE,
2196 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
Amr Aboelkher21ac9962019-05-15 14:50:05 +02002197 base::Unretained(this),
2198 /*set_token=*/true));
2199 loop_.Run();
2200}
2201
2202TEST_F(UpdateAttempterTest, EmptyQuickFixToken) {
2203 loop_.PostTask(FROM_HERE,
2204 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
2205 base::Unretained(this),
2206 /*set_token=*/false));
Askar Aitzhan570ca872019-04-24 11:16:12 +02002207 loop_.Run();
2208}
2209
Jae Hoon Kim75daa382019-07-02 11:17:24 -07002210TEST_F(UpdateAttempterTest, ScheduleUpdateSpamHandlerTest) {
2211 EXPECT_CALL(mock_update_manager_, AsyncPolicyRequestUpdateCheckAllowed(_, _))
2212 .Times(1);
2213 EXPECT_TRUE(attempter_.ScheduleUpdates());
2214 // Now there is an update scheduled which means that all subsequent
2215 // |ScheduleUpdates()| should fail.
2216 EXPECT_FALSE(attempter_.ScheduleUpdates());
2217 EXPECT_FALSE(attempter_.ScheduleUpdates());
2218 EXPECT_FALSE(attempter_.ScheduleUpdates());
2219}
2220
2221// Critical tests to always make sure that an update is scheduled. The following
2222// unittest(s) try and cover the correctness in synergy between
2223// |UpdateAttempter| and |UpdateManager|. Also it is good to remember the
2224// actions that happen in the flow when |UpdateAttempter| get callbacked on
2225// |OnUpdateScheduled()| -> (various cases which leads to) -> |ProcessingDone()|
2226void UpdateAttempterTest::TestOnUpdateScheduled() {
2227 // Setup
2228 attempter_.SetWaitingForScheduledCheck(true);
2229 attempter_.DisableUpdate();
2230 attempter_.DisableScheduleUpdates();
2231
2232 // Invocation
2233 attempter_.OnUpdateScheduled(ous_params_.status, ous_params_.params);
2234
2235 // Verify
2236 EXPECT_EQ(ous_params_.exit_status, attempter_.status());
2237 EXPECT_EQ(ous_params_.should_schedule_updates_be_called,
2238 attempter_.WasScheduleUpdatesCalled());
2239 EXPECT_EQ(ous_params_.should_update_be_called, attempter_.WasUpdateCalled());
2240}
2241
2242TEST_F(UpdateAttempterTest, OnUpdatesScheduledFailed) {
2243 // GIVEN failed status.
2244
2245 // THEN update should be scheduled.
2246 ous_params_.should_schedule_updates_be_called = true;
2247
2248 TestOnUpdateScheduled();
2249}
2250
2251TEST_F(UpdateAttempterTest, OnUpdatesScheduledAskMeAgainLater) {
2252 // GIVEN ask me again later status.
2253 ous_params_.status = EvalStatus::kAskMeAgainLater;
2254
2255 // THEN update should be scheduled.
2256 ous_params_.should_schedule_updates_be_called = true;
2257
2258 TestOnUpdateScheduled();
2259}
2260
2261TEST_F(UpdateAttempterTest, OnUpdatesScheduledContinue) {
2262 // GIVEN continue status.
2263 ous_params_.status = EvalStatus::kContinue;
2264
2265 // THEN update should be scheduled.
2266 ous_params_.should_schedule_updates_be_called = true;
2267
2268 TestOnUpdateScheduled();
2269}
2270
2271TEST_F(UpdateAttempterTest, OnUpdatesScheduledSucceededButUpdateDisabledFails) {
2272 // GIVEN updates disabled.
2273 ous_params_.params = {.updates_enabled = false};
2274 // GIVEN succeeded status.
2275 ous_params_.status = EvalStatus::kSucceeded;
2276
2277 // THEN update should not be scheduled.
2278
2279 TestOnUpdateScheduled();
2280}
2281
2282TEST_F(UpdateAttempterTest, OnUpdatesScheduledSucceeded) {
2283 // GIVEN updates enabled.
2284 ous_params_.params = {.updates_enabled = true};
2285 // GIVEN succeeded status.
2286 ous_params_.status = EvalStatus::kSucceeded;
2287
2288 // THEN update should be called indicating status change.
2289 ous_params_.exit_status = UpdateStatus::CHECKING_FOR_UPDATE;
2290 ous_params_.should_update_be_called = true;
2291
2292 TestOnUpdateScheduled();
2293}
2294
Amin Hassani9be122e2019-08-29 09:20:12 -07002295TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusDefault) {
2296 UpdateEngineStatus status;
2297 attempter_.GetStatus(&status);
2298 EXPECT_FALSE(status.is_enterprise_rollback);
2299}
2300
2301TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusFalse) {
2302 attempter_.install_plan_.reset(new InstallPlan);
2303 attempter_.install_plan_->is_rollback = false;
2304
2305 UpdateEngineStatus status;
2306 attempter_.GetStatus(&status);
2307 EXPECT_FALSE(status.is_enterprise_rollback);
2308}
2309
2310TEST_F(UpdateAttempterTest, IsEnterpriseRollbackInGetStatusTrue) {
2311 attempter_.install_plan_.reset(new InstallPlan);
2312 attempter_.install_plan_->is_rollback = true;
2313
2314 UpdateEngineStatus status;
2315 attempter_.GetStatus(&status);
2316 EXPECT_TRUE(status.is_enterprise_rollback);
2317}
2318
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002319TEST_F(UpdateAttempterTest, FutureEolTest) {
2320 EolDate eol_date = std::numeric_limits<int64_t>::max();
2321 EXPECT_CALL(*prefs_, GetString(kPrefsOmahaEolDate, _))
2322 .WillOnce(
2323 DoAll(SetArgPointee<1>(EolDateToString(eol_date)), Return(true)));
2324
2325 UpdateEngineStatus status;
2326 attempter_.GetStatus(&status);
2327 EXPECT_EQ(eol_date, status.eol_date);
2328}
2329
2330TEST_F(UpdateAttempterTest, PastEolTest) {
2331 EolDate eol_date = 1;
2332 EXPECT_CALL(*prefs_, GetString(kPrefsOmahaEolDate, _))
2333 .WillOnce(
2334 DoAll(SetArgPointee<1>(EolDateToString(eol_date)), Return(true)));
2335
2336 UpdateEngineStatus status;
2337 attempter_.GetStatus(&status);
2338 EXPECT_EQ(eol_date, status.eol_date);
2339}
2340
2341TEST_F(UpdateAttempterTest, FailedEolTest) {
2342 EolDate eol_date = kEolDateInvalid;
2343 EXPECT_CALL(*prefs_, GetString(kPrefsOmahaEolDate, _))
2344 .WillOnce(Return(false));
2345
2346 UpdateEngineStatus status;
2347 attempter_.GetStatus(&status);
2348 EXPECT_EQ(eol_date, status.eol_date);
2349}
2350
Andrewe045aef2020-01-08 16:29:22 -08002351TEST_F(UpdateAttempterTest, CalculateDlcParamsInstallTest) {
2352 // Create a uniquely named test directory.
2353 base::ScopedTempDir tempdir;
2354 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
2355 fake_system_state_.request_params()->set_root(tempdir.GetPath().value());
2356 string dlc_id = "dlc0";
2357 base::FilePath metadata_path_dlc0 =
2358 base::FilePath(fake_system_state_.request_params()->dlc_prefs_root())
2359 .Append(dlc_id);
2360
2361 ASSERT_TRUE(base::CreateDirectory(metadata_path_dlc0));
2362 attempter_.is_install_ = true;
2363 attempter_.dlc_module_ids_ = {dlc_id};
2364 attempter_.CalculateDlcParams();
2365
2366 OmahaRequestParams* params = fake_system_state_.request_params();
2367 EXPECT_EQ(1, params->dlc_apps_params().count(params->GetDlcAppId(dlc_id)));
2368 OmahaRequestParams::AppParams dlc_app_params =
2369 params->dlc_apps_params().at(params->GetDlcAppId(dlc_id));
2370 EXPECT_STREQ(dlc_id.c_str(), dlc_app_params.name.c_str());
2371 EXPECT_EQ(false, dlc_app_params.send_ping);
2372 // When the DLC gets installed, a ping is not sent, therefore we don't store
2373 // the values sent by Omaha.
2374 EXPECT_FALSE(
2375 base::PathExists(metadata_path_dlc0.Append(kPrefsPingLastActive)));
2376 EXPECT_FALSE(
2377 base::PathExists(metadata_path_dlc0.Append(kPrefsPingLastRollcall)));
2378}
2379
2380TEST_F(UpdateAttempterTest, CalculateDlcParamsNoPrefFilesTest) {
2381 // Create a uniquely named test directory.
2382 base::ScopedTempDir tempdir;
2383 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
2384 fake_system_state_.request_params()->set_root(tempdir.GetPath().value());
2385 string dlc_id = "dlc0";
2386 base::FilePath metadata_path_dlc0 =
2387 base::FilePath(fake_system_state_.request_params()->dlc_prefs_root())
2388 .Append(dlc_id);
2389 ASSERT_TRUE(base::CreateDirectory(metadata_path_dlc0));
2390 EXPECT_CALL(mock_dlcservice_, GetInstalled(_))
2391 .WillOnce(
2392 DoAll(SetArgPointee<0>(std::vector<string>({dlc_id})), Return(true)));
2393
2394 attempter_.is_install_ = false;
2395 attempter_.CalculateDlcParams();
2396
2397 OmahaRequestParams* params = fake_system_state_.request_params();
2398 EXPECT_EQ(1, params->dlc_apps_params().count(params->GetDlcAppId(dlc_id)));
2399 OmahaRequestParams::AppParams dlc_app_params =
2400 params->dlc_apps_params().at(params->GetDlcAppId(dlc_id));
2401 EXPECT_STREQ(dlc_id.c_str(), dlc_app_params.name.c_str());
2402
2403 EXPECT_EQ(true, dlc_app_params.send_ping);
2404 EXPECT_EQ(0, dlc_app_params.ping_active);
2405 EXPECT_EQ(-1, dlc_app_params.ping_date_last_active);
2406 EXPECT_EQ(-1, dlc_app_params.ping_date_last_rollcall);
2407}
2408
2409TEST_F(UpdateAttempterTest, CalculateDlcParamsNonParseableValuesTest) {
2410 // Create a uniquely named test directory.
2411 base::ScopedTempDir tempdir;
2412 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
2413 fake_system_state_.request_params()->set_root(tempdir.GetPath().value());
2414 string dlc_id = "dlc0";
2415 base::FilePath metadata_path_dlc0 =
2416 base::FilePath(fake_system_state_.request_params()->dlc_prefs_root())
2417 .Append(dlc_id);
2418 ASSERT_TRUE(base::CreateDirectory(metadata_path_dlc0));
2419 EXPECT_CALL(mock_dlcservice_, GetInstalled(_))
2420 .WillOnce(
2421 DoAll(SetArgPointee<0>(std::vector<string>({dlc_id})), Return(true)));
2422
2423 // Write non numeric values in the metadata files.
2424 base::WriteFile(metadata_path_dlc0.Append(kPrefsPingActive), "z2yz", 4);
2425 base::WriteFile(metadata_path_dlc0.Append(kPrefsPingLastActive), "z2yz", 4);
2426 base::WriteFile(metadata_path_dlc0.Append(kPrefsPingLastRollcall), "z2yz", 4);
2427 attempter_.is_install_ = false;
2428 attempter_.CalculateDlcParams();
2429
2430 OmahaRequestParams* params = fake_system_state_.request_params();
2431 EXPECT_EQ(1, params->dlc_apps_params().count(params->GetDlcAppId(dlc_id)));
2432 OmahaRequestParams::AppParams dlc_app_params =
2433 params->dlc_apps_params().at(params->GetDlcAppId(dlc_id));
2434 EXPECT_STREQ(dlc_id.c_str(), dlc_app_params.name.c_str());
2435
2436 EXPECT_EQ(true, dlc_app_params.send_ping);
2437 EXPECT_EQ(0, dlc_app_params.ping_active);
2438 EXPECT_EQ(-2, dlc_app_params.ping_date_last_active);
2439 EXPECT_EQ(-2, dlc_app_params.ping_date_last_rollcall);
2440}
2441
2442TEST_F(UpdateAttempterTest, CalculateDlcParamsValidValuesTest) {
2443 // Create a uniquely named test directory.
2444 base::ScopedTempDir tempdir;
2445 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
2446 fake_system_state_.request_params()->set_root(tempdir.GetPath().value());
2447 string dlc_id = "dlc0";
2448 base::FilePath metadata_path_dlc0 =
2449 base::FilePath(fake_system_state_.request_params()->dlc_prefs_root())
2450 .Append(dlc_id);
2451 ASSERT_TRUE(base::CreateDirectory(metadata_path_dlc0));
2452 EXPECT_CALL(mock_dlcservice_, GetInstalled(_))
2453 .WillOnce(
2454 DoAll(SetArgPointee<0>(std::vector<string>({dlc_id})), Return(true)));
2455
2456 // Write numeric values in the metadata files.
2457 base::WriteFile(metadata_path_dlc0.Append(kPrefsPingActive), "1", 1);
2458 base::WriteFile(metadata_path_dlc0.Append(kPrefsPingLastActive), "78", 2);
2459 base::WriteFile(metadata_path_dlc0.Append(kPrefsPingLastRollcall), "99", 2);
2460 attempter_.is_install_ = false;
2461 attempter_.CalculateDlcParams();
2462
2463 OmahaRequestParams* params = fake_system_state_.request_params();
2464 EXPECT_EQ(1, params->dlc_apps_params().count(params->GetDlcAppId(dlc_id)));
2465 OmahaRequestParams::AppParams dlc_app_params =
2466 params->dlc_apps_params().at(params->GetDlcAppId(dlc_id));
2467 EXPECT_STREQ(dlc_id.c_str(), dlc_app_params.name.c_str());
2468
2469 EXPECT_EQ(true, dlc_app_params.send_ping);
2470 EXPECT_EQ(1, dlc_app_params.ping_active);
2471 EXPECT_EQ(78, dlc_app_params.ping_date_last_active);
2472 EXPECT_EQ(99, dlc_app_params.ping_date_last_rollcall);
2473}
Andrewa8d7df32020-03-15 20:10:01 -07002474
2475TEST_F(UpdateAttempterTest, CalculateDlcParamsRemoveStaleMetadata) {
2476 base::ScopedTempDir tempdir;
2477 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
2478 fake_system_state_.request_params()->set_root(tempdir.GetPath().value());
2479 string dlc_id = "dlc0";
2480 base::FilePath metadata_path_dlc0 =
2481 base::FilePath(fake_system_state_.request_params()->dlc_prefs_root())
2482 .Append(dlc_id);
2483 ASSERT_TRUE(base::CreateDirectory(metadata_path_dlc0));
2484 base::FilePath metadata_path_dlc_stale =
2485 base::FilePath(fake_system_state_.request_params()->dlc_prefs_root())
2486 .Append("stale");
2487 ASSERT_TRUE(base::CreateDirectory(metadata_path_dlc_stale));
2488 EXPECT_CALL(mock_dlcservice_, GetInstalled(_))
2489 .WillOnce(
2490 DoAll(SetArgPointee<0>(std::vector<string>({dlc_id})), Return(true)));
2491
2492 attempter_.is_install_ = false;
2493 attempter_.CalculateDlcParams();
2494
2495 EXPECT_TRUE(base::PathExists(metadata_path_dlc0));
2496 EXPECT_FALSE(base::PathExists(metadata_path_dlc_stale));
2497}
2498
2499TEST_F(UpdateAttempterTest, SetDlcActiveValue) {
2500 base::ScopedTempDir tempdir;
2501 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
2502 fake_system_state_.request_params()->set_root(tempdir.GetPath().value());
2503 string dlc_id = "dlc0";
2504 base::FilePath metadata_path_dlc0 =
2505 base::FilePath(fake_system_state_.request_params()->dlc_prefs_root())
2506 .Append(dlc_id);
2507 attempter_.SetDlcActiveValue(true, dlc_id);
2508 Prefs prefs;
2509 ASSERT_TRUE(base::PathExists(metadata_path_dlc0));
2510 ASSERT_TRUE(prefs.Init(metadata_path_dlc0));
2511 int64_t temp_int;
2512 EXPECT_TRUE(prefs.GetInt64(kPrefsPingActive, &temp_int));
2513 EXPECT_EQ(temp_int, kPingActiveValue);
2514}
2515
2516TEST_F(UpdateAttempterTest, SetDlcInactive) {
2517 base::ScopedTempDir tempdir;
2518 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
2519 fake_system_state_.request_params()->set_root(tempdir.GetPath().value());
2520 string dlc_id = "dlc0";
2521 base::FilePath metadata_path_dlc0 =
2522 base::FilePath(fake_system_state_.request_params()->dlc_prefs_root())
2523 .Append(dlc_id);
2524 base::CreateDirectory(metadata_path_dlc0);
2525 EXPECT_TRUE(base::PathExists(metadata_path_dlc0));
2526 attempter_.SetDlcActiveValue(false, dlc_id);
2527 EXPECT_FALSE(base::PathExists(metadata_path_dlc0));
2528}
2529
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -07002530TEST_F(UpdateAttempterTest, GetSuccessfulDlcIds) {
2531 auto dlc_1 = "1", dlc_2 = "2", dlc_3 = "3";
2532 attempter_.omaha_request_params_->set_dlc_apps_params(
2533 {{dlc_1, {.name = dlc_1, .updated = false}},
2534 {dlc_2, {.name = dlc_2}},
2535 {dlc_3, {.name = dlc_3, .updated = false}}});
2536 EXPECT_THAT(attempter_.GetSuccessfulDlcIds(), ElementsAre(dlc_2));
2537}
2538
Darin Petkovf42cc1c2010-09-01 09:03:02 -07002539} // namespace chromeos_update_engine