blob: 16819f8309897449d0c41be38c09589b1f4f1200 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/update_attempter.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Ben Chan02f7c1d2014-10-18 15:18:02 -070021#include <memory>
22
Ben Chan06c76a42014-09-05 08:21:06 -070023#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070024#include <base/message_loop/message_loop.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/message_loops/base_message_loop.h>
26#include <brillo/message_loops/message_loop.h>
27#include <brillo/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020029#include <policy/libpolicy.h>
30#include <policy/mock_device_policy.h>
Marton Hunyadye58bddb2018-04-10 20:27:26 +020031#include <policy/mock_libpolicy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070032
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080033#include "update_engine/common/dlcservice_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/fake_clock.h"
35#include "update_engine/common/fake_prefs.h"
Alex Deymo14fd1ec2016-02-24 22:03:57 -080036#include "update_engine/common/mock_action.h"
37#include "update_engine/common/mock_action_processor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/mock_http_fetcher.h"
39#include "update_engine/common/mock_prefs.h"
40#include "update_engine/common/platform_constants.h"
41#include "update_engine/common/prefs.h"
42#include "update_engine/common/test_utils.h"
43#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070044#include "update_engine/fake_system_state.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070045#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046#include "update_engine/mock_payload_state.h"
Aaron Wood9321f502017-09-07 11:18:54 -070047#include "update_engine/mock_service_observer.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080048#include "update_engine/payload_consumer/filesystem_verifier_action.h"
49#include "update_engine/payload_consumer/install_plan.h"
50#include "update_engine/payload_consumer/payload_constants.h"
51#include "update_engine/payload_consumer/postinstall_runner_action.h"
Amin Hassani0882a512018-04-05 16:25:44 -070052#include "update_engine/update_boot_flags_action.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070053
David Zeuthen985b1122013-10-09 12:13:15 -070054using base::Time;
55using base::TimeDelta;
Aaron Woodbf5a2522017-10-04 10:58:36 -070056using chromeos_update_manager::EvalStatus;
Adolfo Victoria497044c2018-07-18 07:51:42 -070057using chromeos_update_manager::StagingSchedule;
Aaron Woodbf5a2522017-10-04 10:58:36 -070058using chromeos_update_manager::UpdateCheckParams;
Adolfo Victoria497044c2018-07-18 07:51:42 -070059using policy::DevicePolicy;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070060using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070061using std::unique_ptr;
Xiaochu Liu88d90382018-08-29 16:09:11 -070062using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070063using testing::_;
Darin Petkov36275772010-10-01 11:40:57 -070064using testing::DoAll;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070065using testing::Field;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070066using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070067using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080068using testing::NiceMock;
Amin Hassanid3f4bea2018-04-30 14:52:40 -070069using testing::Pointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070070using testing::Property;
71using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070072using testing::ReturnPointee;
Marton Hunyadye58bddb2018-04-10 20:27:26 +020073using testing::ReturnRef;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080074using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070075using testing::SetArgPointee;
Aaron Woodbf5a2522017-10-04 10:58:36 -070076using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070077using update_engine::UpdateEngineStatus;
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -070078using update_engine::UpdateStatus;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070079
80namespace chromeos_update_engine {
81
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080082namespace {
83
84class MockDlcService : public DlcServiceInterface {
85 public:
86 MOCK_METHOD1(GetInstalled, bool(vector<string>*));
87};
88
89} // namespace
90
Marton Hunyadya0302682018-05-16 18:52:13 +020091const char kRollbackVersion[] = "10575.39.2";
92
Darin Petkovf42cc1c2010-09-01 09:03:02 -070093// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070094// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070095// methods.
96class UpdateAttempterUnderTest : public UpdateAttempter {
97 public:
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070098 explicit UpdateAttempterUnderTest(SystemState* system_state)
99 : UpdateAttempter(system_state, nullptr) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700100
101 // Wrap the update scheduling method, allowing us to opt out of scheduled
102 // updates for testing purposes.
Xiaochu Liu88d90382018-08-29 16:09:11 -0700103 bool ScheduleUpdates() override {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700104 schedule_updates_called_ = true;
105 if (do_schedule_updates_) {
106 UpdateAttempter::ScheduleUpdates();
107 } else {
108 LOG(INFO) << "[TEST] Update scheduling disabled.";
109 }
Xiaochu Liu88d90382018-08-29 16:09:11 -0700110 return true;
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700111 }
112 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
113 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
114
115 // Indicates whether ScheduleUpdates() was called.
116 bool schedule_updates_called() const { return schedule_updates_called_; }
117
David Pursell02c18642014-11-06 11:26:11 -0800118 // Need to expose forced_omaha_url_ so we can test it.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700119 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -0800120
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700121 private:
122 bool schedule_updates_called_ = false;
123 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700124};
125
126class UpdateAttempterTest : public ::testing::Test {
127 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700128 UpdateAttempterTest()
Daniel Erate5f6f252017-04-20 12:09:58 -0600129 : certificate_checker_(fake_system_state_.mock_prefs(),
Alex Deymo33e91e72015-12-01 18:26:08 -0300130 &openssl_wrapper_) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700131 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700132 fake_system_state_.set_connection_manager(&mock_connection_manager);
133 fake_system_state_.set_update_attempter(&attempter_);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800134 fake_system_state_.set_dlcservice(&mock_dlcservice_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700135 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700136
Alex Deymo33e91e72015-12-01 18:26:08 -0300137 certificate_checker_.Init();
138
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800139 attempter_.set_forced_update_pending_callback(
140 new base::Callback<void(bool, bool)>(base::Bind([](bool, bool) {})));
Sen Jiange67bb5b2016-06-20 15:53:56 -0700141 // Finish initializing the attempter.
Gilad Arnold1f847232014-04-07 12:07:49 -0700142 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -0700143 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700144
Alex Deymo610277e2014-11-11 21:18:11 -0800145 void SetUp() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700146 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700147 EXPECT_EQ(0, attempter_.http_response_code_);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700148 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700149 EXPECT_EQ(0.0, attempter_.download_progress_);
150 EXPECT_EQ(0, attempter_.last_checked_time_);
151 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700152 EXPECT_EQ(0ULL, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800153 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700154 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700155 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700156
157 // Set up store/load semantics of P2P properties via the mock PayloadState.
158 actual_using_p2p_for_downloading_ = false;
159 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
160 SetUsingP2PForDownloading(_))
161 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
162 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
163 GetUsingP2PForDownloading())
164 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
165 actual_using_p2p_for_sharing_ = false;
166 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
167 SetUsingP2PForSharing(_))
168 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
169 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
170 GetUsingP2PForDownloading())
171 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700172 }
173
Alex Deymo60ca1a72015-06-18 18:19:15 -0700174 public:
175 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200176
Alex Deymo60ca1a72015-06-18 18:19:15 -0700177 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800178 void UpdateTestStart();
179 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700180 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700181 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700182 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700183 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700184 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700185 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700186 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700187 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700188 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700189 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700190 void P2PEnabledHousekeepingFailsStart();
Askar Aitzhan570ca872019-04-24 11:16:12 +0200191 void UpdateToQuickFixBuildStart();
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200192 void ResetRollbackHappenedStart(bool is_consumer,
193 bool is_policy_available,
194 bool expected_reset);
Adolfo Victoria497044c2018-07-18 07:51:42 -0700195 // Staging related callbacks.
196 void SetUpStagingTest(const StagingSchedule& schedule, FakePrefs* prefs);
197 void CheckStagingOff();
198 void StagingSetsPrefsAndTurnsOffScatteringStart();
199 void StagingOffIfInteractiveStart();
200 void StagingOffIfOobeStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700201
Gilad Arnold74b5f552014-10-07 08:17:16 -0700202 bool actual_using_p2p_for_downloading() {
203 return actual_using_p2p_for_downloading_;
204 }
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800205 bool actual_using_p2p_for_sharing() { return actual_using_p2p_for_sharing_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700206
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700207 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700208 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700209
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700210 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700211 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300212 OpenSSLWrapper openssl_wrapper_;
213 CertificateChecker certificate_checker_;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800214 MockDlcService mock_dlcservice_;
Alex Deymo30534502015-07-20 15:06:33 -0700215
Alex Deymo8427b4a2014-11-05 14:00:32 -0800216 NiceMock<MockActionProcessor>* processor_;
217 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800218 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700219
Gilad Arnold74b5f552014-10-07 08:17:16 -0700220 bool actual_using_p2p_for_downloading_;
221 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700222};
223
Alex Deymo60ca1a72015-06-18 18:19:15 -0700224void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700225 loop_.PostTask(
226 FROM_HERE,
227 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
228 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700229}
230
Darin Petkov1b003102010-11-30 10:18:36 -0800231TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700232 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800233 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700234 DownloadAction action(prefs_,
235 nullptr,
236 nullptr,
237 nullptr,
238 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700239 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800240 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700241 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700242 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700243 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700244 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800245}
246
247TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800248 MockAction action;
249 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700250 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800251 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800252 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700253 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
254 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800255}
256
Aaron Wood9321f502017-09-07 11:18:54 -0700257TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
258 // Simple test case, where all the values match (nothing was skipped)
259 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
260 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
261 uint64_t bytes_received_1 = bytes_progressed_1;
262 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
263 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
264
265 double progress_1 =
266 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
267 double progress_2 =
268 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
269
270 EXPECT_EQ(0.0, attempter_.download_progress_);
271 // This is set via inspecting the InstallPlan payloads when the
272 // OmahaResponseAction is completed
273 attempter_.new_payload_size_ = bytes_total;
274 NiceMock<MockServiceObserver> observer;
275 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700276 SendStatusUpdate(AllOf(
277 Field(&UpdateEngineStatus::progress, progress_1),
278 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
279 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700280 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700281 SendStatusUpdate(AllOf(
282 Field(&UpdateEngineStatus::progress, progress_2),
283 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
284 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700285 attempter_.AddObserver(&observer);
286 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
287 EXPECT_EQ(progress_1, attempter_.download_progress_);
288 // This iteration validates that a later set of updates to the variables are
289 // properly handled (so that |getStatus()| will return the same progress info
290 // as the callback is receiving.
291 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
292 EXPECT_EQ(progress_2, attempter_.download_progress_);
293}
294
295TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
296 // The transition into UpdateStatus::DOWNLOADING happens when the
297 // first bytes are received.
298 uint64_t bytes_progressed = 1024 * 1024; // 1MB
299 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
300 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
301 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
302 // This is set via inspecting the InstallPlan payloads when the
303 // OmahaResponseAction is completed
304 attempter_.new_payload_size_ = bytes_total;
305 EXPECT_EQ(0.0, attempter_.download_progress_);
306 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700307 EXPECT_CALL(observer,
308 SendStatusUpdate(AllOf(
309 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
310 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700311 attempter_.AddObserver(&observer);
312 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
313 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
314}
315
316TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
317 // There is a special case to ensure that at 100% downloaded,
318 // download_progress_ is updated and that value broadcast. This test confirms
319 // that.
320 uint64_t bytes_progressed = 0; // ignored
321 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
322 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
323 attempter_.status_ = UpdateStatus::DOWNLOADING;
324 attempter_.new_payload_size_ = bytes_total;
325 EXPECT_EQ(0.0, attempter_.download_progress_);
326 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700327 EXPECT_CALL(observer,
328 SendStatusUpdate(AllOf(
329 Field(&UpdateEngineStatus::progress, 1.0),
330 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
331 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700332 attempter_.AddObserver(&observer);
333 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
334 EXPECT_EQ(1.0, attempter_.download_progress_);
335}
336
Darin Petkov1b003102010-11-30 10:18:36 -0800337TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700338 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800339 fetcher->FailTransfer(500); // Sets the HTTP response code.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800340 OmahaRequestAction action(
341 &fake_system_state_, nullptr, std::move(fetcher), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800342 ObjectCollectorAction<OmahaResponse> collector_action;
343 BondActions(&action, &collector_action);
344 OmahaResponse response;
345 response.poll_interval = 234;
346 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800347 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700348 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800349 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700350 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800351 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700352 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800353}
354
Alex Deymo30534502015-07-20 15:06:33 -0700355TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700356 FakePrefs fake_prefs;
357 string boot_id;
358 EXPECT_TRUE(utils::GetBootId(&boot_id));
359 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
360 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700361 attempter_.Init();
362 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700363}
364
365TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700366 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700367 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700368
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700369 FakeSystemState fake_system_state;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800370 OmahaRequestAction omaha_request_action(
371 &fake_system_state, nullptr, nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700372 EXPECT_EQ(ErrorCode::kOmahaRequestError,
373 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700374 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800375 EXPECT_EQ(
376 ErrorCode::kOmahaResponseHandlerError,
377 GetErrorCodeForAction(&omaha_response_handler_action, ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700378 FilesystemVerifierAction filesystem_verifier_action;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800379 EXPECT_EQ(
380 ErrorCode::kFilesystemVerifierError,
381 GetErrorCodeForAction(&filesystem_verifier_action, ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300382 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700383 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800384 EXPECT_EQ(
385 ErrorCode::kPostinstallRunnerError,
386 GetErrorCodeForAction(&postinstall_runner_action, ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800387 MockAction action_mock;
388 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700389 EXPECT_EQ(ErrorCode::kError,
390 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700391}
392
Darin Petkov36275772010-10-01 11:40:57 -0700393TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700394 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800395 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700396 .WillOnce(Return(false));
397 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700398 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800399 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800400 .WillOnce(
401 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
402 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700403 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700404 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800405 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800406 .WillOnce(
407 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
408 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700409 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700410 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800411 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700412 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700413 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700414}
415
416TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800417 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700418 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700419 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
420 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800421 .WillOnce(
422 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
423 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800424 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700425 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800426 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700427 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800428 EXPECT_CALL(*prefs_,
429 SetInt64(kPrefsDeltaUpdateFailures,
430 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
431 for (int i = 0; i < 4; i++)
Darin Petkov36275772010-10-01 11:40:57 -0700432 attempter_.MarkDeltaUpdateFailure();
433}
434
Darin Petkov1b003102010-11-30 10:18:36 -0800435TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
436 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
437 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700438 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800439 .Times(0);
440 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700441 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800442 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700443 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700444 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700445 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800446 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700447 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800448}
449
450TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
451 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700452 EnqueueAction(Pointee(Property(
453 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700454 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700455 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700456 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800457 attempter_.error_event_.reset(new OmahaEvent(
458 OmahaEvent::kTypeUpdateComplete, OmahaEvent::kResultError, err));
Darin Petkov1b003102010-11-30 10:18:36 -0800459 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700460 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800461}
462
Darin Petkove6ef2f82011-03-07 17:31:11 -0800463namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700464// Actions that will be built as part of an update check.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200465vector<string> GetUpdateActionTypes() {
466 return {OmahaRequestAction::StaticType(),
467 OmahaResponseHandlerAction::StaticType(),
468 UpdateBootFlagsAction::StaticType(),
469 OmahaRequestAction::StaticType(),
470 DownloadAction::StaticType(),
471 OmahaRequestAction::StaticType(),
472 FilesystemVerifierAction::StaticType(),
473 PostinstallRunnerAction::StaticType(),
474 OmahaRequestAction::StaticType()};
475}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700476
477// Actions that will be built as part of a user-initiated rollback.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200478vector<string> GetRollbackActionTypes() {
479 return {InstallPlanAction::StaticType(),
480 PostinstallRunnerAction::StaticType()};
481}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700482
Adolfo Victoria497044c2018-07-18 07:51:42 -0700483const StagingSchedule kValidStagingSchedule = {
484 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
485
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700486} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800487
488void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700489 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700490
491 // Expect that the device policy is loaded by the UpdateAttempter at some
492 // point by calling RefreshDevicePolicy.
Igor9fd76b62017-12-11 15:24:18 +0100493 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700494 EXPECT_CALL(*device_policy, LoadPolicy())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800495 .Times(testing::AtLeast(1))
496 .WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100497 attempter_.policy_provider_.reset(
498 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700499
500 {
501 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200502 for (const auto& update_action_type : GetUpdateActionTypes()) {
Alex Deymo749ecf12014-10-21 20:06:57 -0700503 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700504 EnqueueAction(Pointee(
Askar Aitzhan570ca872019-04-24 11:16:12 +0200505 Property(&AbstractAction::Type, update_action_type))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700506 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700507 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700508 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700509
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800510 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700511 loop_.PostTask(FROM_HERE,
512 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
513 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800514}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700515
Darin Petkove6ef2f82011-03-07 17:31:11 -0800516void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700517 EXPECT_EQ(0, attempter_.http_response_code());
518 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700519 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700520 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800521}
522
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800523void UpdateAttempterTest::RollbackTestStart(bool enterprise_rollback,
524 bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700525 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100526 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700527 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100528 fake_system_state_.set_device_policy(device_policy.get());
529 if (enterprise_rollback) {
530 // We return an empty owner as this is an enterprise.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800531 EXPECT_CALL(*device_policy, GetOwner(_))
532 .WillRepeatedly(DoAll(SetArgPointee<0>(string("")), Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100533 } else {
534 // We return a fake owner as this is an owned consumer device.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800535 EXPECT_CALL(*device_policy, GetOwner(_))
536 .WillRepeatedly(DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
537 Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100538 }
539
540 attempter_.policy_provider_.reset(
541 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700542
Alex Deymo763e7db2015-08-27 21:08:08 -0700543 if (valid_slot) {
544 BootControlInterface::Slot rollback_slot = 1;
545 LOG(INFO) << "Test Mark Bootable: "
546 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700547 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
548 true);
Don Garrett6646b442013-11-13 15:29:11 -0800549 }
550
Chris Sosa28e479c2013-07-12 11:39:53 -0700551 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700552
Chris Sosad38b1132014-03-25 10:43:59 -0700553 // We only allow rollback on devices that are not enterprise enrolled and
554 // which have a valid slot to rollback to.
555 if (!enterprise_rollback && valid_slot) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800556 is_rollback_allowed = true;
Chris Sosa28e479c2013-07-12 11:39:53 -0700557 }
558
Chris Sosa28e479c2013-07-12 11:39:53 -0700559 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700560 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200561 for (const auto& rollback_action_type : GetRollbackActionTypes()) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700562 EXPECT_CALL(*processor_,
Askar Aitzhan570ca872019-04-24 11:16:12 +0200563 EnqueueAction(Pointee(
564 Property(&AbstractAction::Type, rollback_action_type))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700565 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700566 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700567
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700568 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700569 loop_.PostTask(FROM_HERE,
570 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
571 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700572 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700573 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700574 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700575 }
576}
577
578void UpdateAttempterTest::RollbackTestVerify() {
579 // Verifies the actions that were enqueued.
580 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700581 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700582 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
583 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700584 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700585}
586
Darin Petkove6ef2f82011-03-07 17:31:11 -0800587TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700588 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700589 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700590}
591
Chris Sosa76a29ae2013-07-11 17:59:24 -0700592TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700593 loop_.PostTask(FROM_HERE,
594 base::Bind(&UpdateAttempterTest::RollbackTestStart,
595 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800596 false,
597 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700598 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700599}
600
Don Garrett6646b442013-11-13 15:29:11 -0800601TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700602 loop_.PostTask(FROM_HERE,
603 base::Bind(&UpdateAttempterTest::RollbackTestStart,
604 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800605 false,
606 false));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700607 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800608}
609
Chris Sosa76a29ae2013-07-11 17:59:24 -0700610TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700611 loop_.PostTask(FROM_HERE,
612 base::Bind(&UpdateAttempterTest::RollbackTestStart,
613 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800614 true,
615 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700616 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700617}
618
Thieu Le116fda32011-04-19 11:01:54 -0700619void UpdateAttempterTest::PingOmahaTestStart() {
620 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700621 EnqueueAction(Pointee(Property(
622 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700623 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700624 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700625 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700626}
627
628TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700629 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
630 EXPECT_FALSE(attempter_.schedule_updates_called());
631 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
632 // testing, which is more permissive than we want to handle here.
633 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700634 loop_.PostTask(FROM_HERE,
635 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
636 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700637 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700638 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700639 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700640}
641
Darin Petkov18c7bce2011-06-16 14:07:00 -0700642TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800643 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700644 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700645 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700646 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700647 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
648 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700649 EXPECT_EQ(
650 static_cast<ErrorCode>(static_cast<int>(kCode) |
651 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
652 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700653}
654
655TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700656 attempter_.install_plan_.reset(new InstallPlan);
657 attempter_.install_plan_->is_resume = true;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800658 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700659 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700660 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700661 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700662 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
663 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700664 EXPECT_EQ(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800665 static_cast<ErrorCode>(static_cast<int>(kCode) |
666 static_cast<int>(ErrorCode::kResumedFlag) |
667 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700668 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700669}
670
David Zeuthen8f191b22013-08-06 12:27:50 -0700671TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
672 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700673 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700674 mock_p2p_manager.fake().SetP2PEnabled(false);
675 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
676 attempter_.UpdateEngineStarted();
677}
678
679TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
680 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700681 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700682 mock_p2p_manager.fake().SetP2PEnabled(true);
683 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
684 attempter_.UpdateEngineStarted();
685}
686
687TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
688 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700689 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700690 mock_p2p_manager.fake().SetP2PEnabled(true);
691 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700692 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700693 attempter_.UpdateEngineStarted();
694}
695
696TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700697 loop_.PostTask(FROM_HERE,
698 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
699 base::Unretained(this)));
700 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700701}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700702
David Zeuthen8f191b22013-08-06 12:27:50 -0700703void UpdateAttempterTest::P2PNotEnabledStart() {
704 // If P2P is not enabled, check that we do not attempt housekeeping
705 // and do not convey that p2p is to be used.
706 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700707 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700708 mock_p2p_manager.fake().SetP2PEnabled(false);
709 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800710 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700711 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700712 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700713 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700714}
715
716TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700717 loop_.PostTask(FROM_HERE,
718 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
719 base::Unretained(this)));
720 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700721}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700722
David Zeuthen8f191b22013-08-06 12:27:50 -0700723void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
724 // If p2p is enabled, but starting it fails ensure we don't do
725 // any housekeeping and do not convey that p2p should be used.
726 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700727 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700728 mock_p2p_manager.fake().SetP2PEnabled(true);
729 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
730 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
731 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800732 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700733 EXPECT_FALSE(actual_using_p2p_for_downloading());
734 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700735 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700736}
737
738TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700739 loop_.PostTask(
740 FROM_HERE,
741 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
742 base::Unretained(this)));
743 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700744}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700745
David Zeuthen8f191b22013-08-06 12:27:50 -0700746void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
747 // If p2p is enabled, starting it works but housekeeping fails, ensure
748 // we do not convey p2p is to be used.
749 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700750 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700751 mock_p2p_manager.fake().SetP2PEnabled(true);
752 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
753 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700754 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800755 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700756 EXPECT_FALSE(actual_using_p2p_for_downloading());
757 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700758 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700759}
760
761TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700762 loop_.PostTask(FROM_HERE,
763 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
764 base::Unretained(this)));
765 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700766}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700767
David Zeuthen8f191b22013-08-06 12:27:50 -0700768void UpdateAttempterTest::P2PEnabledStart() {
769 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700770 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700771 // If P2P is enabled and starting it works, check that we performed
772 // housekeeping and that we convey p2p should be used.
773 mock_p2p_manager.fake().SetP2PEnabled(true);
774 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
775 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700776 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800777 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700778 EXPECT_TRUE(actual_using_p2p_for_downloading());
779 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700780 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700781}
782
783TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700784 loop_.PostTask(FROM_HERE,
785 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
786 base::Unretained(this)));
787 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700788}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700789
David Zeuthen8f191b22013-08-06 12:27:50 -0700790void UpdateAttempterTest::P2PEnabledInteractiveStart() {
791 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700792 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700793 // For an interactive check, if P2P is enabled and starting it
794 // works, check that we performed housekeeping and that we convey
795 // p2p should be used for sharing but NOT for downloading.
796 mock_p2p_manager.fake().SetP2PEnabled(true);
797 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
798 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700799 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200800 attempter_.Update("",
801 "",
802 "",
803 "",
804 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800805 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700806 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200807 false,
808 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700809 EXPECT_FALSE(actual_using_p2p_for_downloading());
810 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700811 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700812}
813
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700814TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700815 loop_.PostTask(
816 FROM_HERE,
817 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
818 base::Unretained(this)));
819 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700820}
821
822// Tests that the scatter_factor_in_seconds value is properly fetched
823// from the device policy.
824void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700825 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700826
Igor9fd76b62017-12-11 15:24:18 +0100827 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700828 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100829 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700830
831 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800832 .WillRepeatedly(
833 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700834
Igor9fd76b62017-12-11 15:24:18 +0100835 attempter_.policy_provider_.reset(
836 new policy::PolicyProvider(std::move(device_policy)));
837
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800838 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700839 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
840
Alex Deymo60ca1a72015-06-18 18:19:15 -0700841 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700842}
843
844TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700845 loop_.PostTask(
846 FROM_HERE,
847 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
848 base::Unretained(this)));
849 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700850}
851
852void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
853 // Tests that the scatter_factor_in_seconds value is properly fetched
854 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700855 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800856 FakePrefs fake_prefs;
857 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700858
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700859 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700860
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800861 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700862
Ben Chan9abb7632014-08-07 00:10:53 -0700863 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700864
Igor9fd76b62017-12-11 15:24:18 +0100865 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700866 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100867 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700868
869 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800870 .WillRepeatedly(
871 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700872
Igor9fd76b62017-12-11 15:24:18 +0100873 attempter_.policy_provider_.reset(
874 new policy::PolicyProvider(std::move(device_policy)));
875
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800876 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700877 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
878
879 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800880 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700881
Ben Chan9abb7632014-08-07 00:10:53 -0700882 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800883 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700884 EXPECT_EQ(initial_value - 1, new_value);
885
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700886 EXPECT_TRUE(
887 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700888
889 // However, if the count is already 0, it's not decremented. Test that.
890 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800891 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800892 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800893 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
894 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700895 EXPECT_EQ(initial_value, new_value);
896
Alex Deymo60ca1a72015-06-18 18:19:15 -0700897 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700898}
899
Jay Srinivasan08fce042012-06-07 16:31:01 -0700900TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800901 loop_.PostTask(
902 FROM_HERE,
903 base::Bind(
904 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
905 base::Unretained(this)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700906 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700907}
908
909void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
910 // Tests that no scattering logic is enabled if the update check
911 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700912 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800913 FakePrefs fake_prefs;
914 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700915
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700916 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800917 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700918
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700919 EXPECT_TRUE(
920 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800921 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700922
923 // make sure scatter_factor is non-zero as scattering is disabled
924 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700925 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700926
Igor9fd76b62017-12-11 15:24:18 +0100927 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700928 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100929 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700930
931 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800932 .WillRepeatedly(
933 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700934
Igor9fd76b62017-12-11 15:24:18 +0100935 attempter_.policy_provider_.reset(
936 new policy::PolicyProvider(std::move(device_policy)));
937
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800938 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200939 attempter_.Update("",
940 "",
941 "",
942 "",
943 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800944 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700945 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200946 false,
947 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700948 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
949
950 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700951 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700952 EXPECT_FALSE(
953 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700954 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700955 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
956 EXPECT_FALSE(
957 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800958 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700959
Alex Deymo60ca1a72015-06-18 18:19:15 -0700960 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700961}
962
Adolfo Victoria497044c2018-07-18 07:51:42 -0700963void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
964 FakePrefs* prefs) {
965 attempter_.prefs_ = prefs;
966 fake_system_state_.set_prefs(prefs);
967
968 int64_t initial_value = 8;
969 EXPECT_TRUE(
970 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
971 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
972 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
973
974 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
975 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
976 fake_system_state_.set_device_policy(device_policy.get());
977 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
978 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
979
980 attempter_.policy_provider_.reset(
981 new policy::PolicyProvider(std::move(device_policy)));
982}
983
984TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
985 loop_.PostTask(
986 FROM_HERE,
987 base::Bind(
988 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
989 base::Unretained(this)));
990 loop_.Run();
991}
992
993void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
994 // Tests that staging sets its prefs properly and turns off scattering.
995 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
996 FakePrefs fake_prefs;
997 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
998
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800999 attempter_.Update("", "", "", "", false, false, 0, false, false);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001000 // Check that prefs have the correct values.
1001 int64_t update_count;
1002 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
1003 int64_t waiting_time_days;
1004 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
1005 &waiting_time_days));
1006 EXPECT_GT(waiting_time_days, 0);
1007 // Update count should have been decremented.
1008 EXPECT_EQ(7, update_count);
1009 // Check that Omaha parameters were updated correctly.
1010 EXPECT_TRUE(
1011 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1012 EXPECT_TRUE(
1013 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1014 EXPECT_EQ(waiting_time_days,
1015 attempter_.omaha_request_params_->waiting_period().InDays());
1016 // Check class variables.
1017 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1018 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1019 // Check that scattering is turned off
1020 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1021 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1022
1023 ScheduleQuitMainLoop();
1024}
1025
1026void UpdateAttempterTest::CheckStagingOff() {
1027 // Check that all prefs were removed.
1028 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1029 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1030 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1031 // Check that the Omaha parameters have the correct value.
1032 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1033 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1034 attempter_.staging_wait_time_);
1035 EXPECT_FALSE(
1036 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1037 EXPECT_FALSE(
1038 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1039 // Check that scattering is turned off too.
1040 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1041}
1042
1043TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1044 loop_.PostTask(FROM_HERE,
1045 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1046 base::Unretained(this)));
1047 loop_.Run();
1048}
1049
1050void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1051 // Tests that staging is turned off when an interactive update is requested.
1052 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1053 FakePrefs fake_prefs;
1054 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1055
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001056 attempter_.Update(
1057 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001058 CheckStagingOff();
1059
1060 ScheduleQuitMainLoop();
1061}
1062
1063TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1064 loop_.PostTask(FROM_HERE,
1065 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1066 base::Unretained(this)));
1067 loop_.Run();
1068}
1069
1070void UpdateAttempterTest::StagingOffIfOobeStart() {
1071 // Tests that staging is turned off if OOBE hasn't been completed.
1072 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1073 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1074 FakePrefs fake_prefs;
1075 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1076
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001077 attempter_.Update(
1078 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001079 CheckStagingOff();
1080
1081 ScheduleQuitMainLoop();
1082}
1083
David Zeuthen985b1122013-10-09 12:13:15 -07001084// Checks that we only report daily metrics at most every 24 hours.
1085TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1086 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001087 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001088
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001089 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001090 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001091
1092 Time epoch = Time::FromInternalValue(0);
1093 fake_clock.SetWallclockTime(epoch);
1094
1095 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1096 // we should report.
1097 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1098 // We should not report again if no time has passed.
1099 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1100
1101 // We should not report if only 10 hours has passed.
1102 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1103 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1104
1105 // We should not report if only 24 hours - 1 sec has passed.
1106 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1107 TimeDelta::FromSeconds(1));
1108 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1109
1110 // We should report if 24 hours has passed.
1111 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1112 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1113
1114 // But then we should not report again..
1115 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1116
1117 // .. until another 24 hours has passed
1118 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1119 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1120 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1121 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1122 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1123
1124 // .. and another 24 hours
1125 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1126 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1127 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1128 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1129 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1130
1131 // If the span between time of reporting and present time is
1132 // negative, we report. This is in order to reset the timestamp and
1133 // avoid an edge condition whereby a distant point in the future is
1134 // in the state variable resulting in us never ever reporting again.
1135 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1136 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1137 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1138
1139 // In this case we should not update until the clock reads 71 + 24 = 95.
1140 // Check that.
1141 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1142 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1143 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1144 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1145 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001146}
1147
David Zeuthen3c55abd2013-10-14 12:48:03 -07001148TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001149 FakeClock fake_clock;
1150 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001151 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001152 FakePrefs fake_prefs;
1153 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001154 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001155
1156 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001157 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001158
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001159 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001160
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001161 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001162 EXPECT_EQ(boot_time.ToTimeT(), 42);
1163}
1164
David Pursell02c18642014-11-06 11:26:11 -08001165TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1166 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1167 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1168}
1169
1170TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1171 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001172 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001173 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1174}
1175
1176TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1177 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001178 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001179 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1180}
1181
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001182TEST_F(UpdateAttempterTest, CheckForUpdateAUDlcTest) {
1183 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1184 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1185
1186 const string dlc_module_id = "a_dlc_module_id";
1187 vector<string> dlc_module_ids = {dlc_module_id};
1188 ON_CALL(mock_dlcservice_, GetInstalled(testing::_))
1189 .WillByDefault(DoAll(testing::SetArgPointee<0>(dlc_module_ids),
1190 testing::Return(true)));
1191
1192 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08001193 EXPECT_EQ(attempter_.dlc_module_ids_.size(), 1);
1194 EXPECT_EQ(attempter_.dlc_module_ids_[0], dlc_module_id);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001195}
1196
David Pursell02c18642014-11-06 11:26:11 -08001197TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1198 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001199 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001200 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001201 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001202}
1203
1204TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
1205 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001206 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001207 attempter_.CheckForUpdate("", "autest-scheduled", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001208 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001209}
1210
Xiaochu Liu88d90382018-08-29 16:09:11 -07001211TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1212 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1213 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1214 attempter_.CheckForInstall({}, "autest");
1215 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1216
1217 attempter_.CheckForInstall({}, "autest-scheduled");
1218 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1219
1220 attempter_.CheckForInstall({}, "http://omaha.phishing");
1221 EXPECT_EQ("", attempter_.forced_omaha_url());
1222}
1223
Colin Howesac170d92018-11-20 16:29:28 -08001224TEST_F(UpdateAttempterTest, InstallSetsStatusIdle) {
1225 attempter_.CheckForInstall({}, "http://foo.bar");
1226 attempter_.status_ = UpdateStatus::DOWNLOADING;
1227 EXPECT_TRUE(attempter_.is_install_);
1228 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1229 UpdateEngineStatus status;
1230 attempter_.GetStatus(&status);
1231 // Should set status to idle after an install operation.
1232 EXPECT_EQ(UpdateStatus::IDLE, status.status);
1233}
1234
Colin Howes978c1082018-12-03 11:46:12 -08001235TEST_F(UpdateAttempterTest, RollbackAfterInstall) {
1236 attempter_.is_install_ = true;
1237 attempter_.Rollback(false);
1238 EXPECT_FALSE(attempter_.is_install_);
1239}
1240
1241TEST_F(UpdateAttempterTest, UpdateAfterInstall) {
1242 attempter_.is_install_ = true;
1243 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1244 EXPECT_FALSE(attempter_.is_install_);
1245}
1246
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001247TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001248 attempter_.CalculateUpdateParams(
1249 "", "", "", "1234", false, false, 4, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001250 EXPECT_EQ("1234",
1251 fake_system_state_.request_params()->target_version_prefix());
1252
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001253 attempter_.CalculateUpdateParams(
1254 "", "", "", "", false, 4, false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001255 EXPECT_TRUE(
1256 fake_system_state_.request_params()->target_version_prefix().empty());
1257}
1258
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001259TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1260 attempter_.CalculateUpdateParams("",
1261 "",
1262 "",
1263 "1234",
1264 /*rollback_allowed=*/true,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001265 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001266 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001267 false,
1268 false);
1269 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001270 EXPECT_EQ(4,
1271 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001272
1273 attempter_.CalculateUpdateParams("",
1274 "",
1275 "",
1276 "1234",
1277 /*rollback_allowed=*/false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001278 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001279 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001280 false,
1281 false);
1282 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001283 EXPECT_EQ(4,
1284 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001285}
1286
Aaron Wood23bd3392017-10-06 14:48:25 -07001287TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1288 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1289 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001290 OmahaResponseHandlerAction response_action(&fake_system_state_);
1291 response_action.install_plan_.version = "a.b.c.d";
1292 response_action.install_plan_.system_version = "b.c.d.e";
1293 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001294 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001295 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1296 // completed, with the deferred-update error code.
1297 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001298 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001299 {
1300 UpdateEngineStatus status;
1301 attempter_.GetStatus(&status);
1302 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001303 EXPECT_TRUE(attempter_.install_plan_);
1304 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
1305 EXPECT_EQ(attempter_.install_plan_->system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001306 status.new_system_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001307 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001308 status.new_size_bytes);
1309 }
1310 // An "error" event should have been created to tell Omaha that the update is
1311 // being deferred.
1312 EXPECT_TRUE(nullptr != attempter_.error_event_);
1313 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1314 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1315 ErrorCode expected_code = static_cast<ErrorCode>(
1316 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1317 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1318 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1319 // End the processing
1320 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1321 // Validate the state of the attempter.
1322 {
1323 UpdateEngineStatus status;
1324 attempter_.GetStatus(&status);
1325 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001326 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
1327 EXPECT_EQ(response_action.install_plan_.system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001328 status.new_system_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001329 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001330 status.new_size_bytes);
1331 }
1332}
1333
1334TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
1335 EXPECT_FALSE(attempter_.IsUpdateRunningOrScheduled());
1336 // Verify in-progress update with UPDATE_AVAILABLE is running
1337 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
1338 EXPECT_TRUE(attempter_.IsUpdateRunningOrScheduled());
1339}
1340
Aaron Woodbf5a2522017-10-04 10:58:36 -07001341TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1342 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1343
1344 UpdateCheckParams params = {.updates_enabled = true};
1345 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1346
1347 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1348 attempter_.GetCurrentUpdateAttemptFlags());
1349}
1350
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001351TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1352 UpdateCheckParams params = {.updates_enabled = true,
1353 .rollback_allowed = false};
1354 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1355 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1356}
1357
1358TEST_F(UpdateAttempterTest, RollbackAllowed) {
1359 UpdateCheckParams params = {.updates_enabled = true,
1360 .rollback_allowed = true};
1361 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1362 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1363}
1364
Aaron Wood081c0232017-10-19 17:14:58 -07001365TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1366 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1367
1368 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1369 EXPECT_EQ(UpdateAttemptFlags::kNone,
1370 attempter_.GetCurrentUpdateAttemptFlags());
1371}
1372
1373TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1374 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1375
1376 // This tests that when CheckForUpdate() is called with the non-interactive
1377 // flag set, that it doesn't change the current UpdateAttemptFlags.
1378 attempter_.CheckForUpdate("",
1379 "",
1380 UpdateAttemptFlags::kFlagNonInteractive |
1381 UpdateAttemptFlags::kFlagRestrictDownload);
1382 EXPECT_EQ(UpdateAttemptFlags::kNone,
1383 attempter_.GetCurrentUpdateAttemptFlags());
1384}
1385
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001386void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1387 bool is_policy_loaded,
1388 bool expected_reset) {
1389 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1390 .WillRepeatedly(Return(true));
1391 auto mock_policy_provider =
1392 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1393 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1394 .WillRepeatedly(Return(is_consumer));
1395 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1396 .WillRepeatedly(Return(is_policy_loaded));
1397 const policy::MockDevicePolicy device_policy;
1398 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1399 .WillRepeatedly(ReturnRef(device_policy));
1400 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1401 SetRollbackHappened(false))
1402 .Times(expected_reset ? 1 : 0);
1403 attempter_.policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001404 attempter_.Update("", "", "", "", false, false, 0, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001405 ScheduleQuitMainLoop();
1406}
1407
1408TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1409 loop_.PostTask(FROM_HERE,
1410 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1411 base::Unretained(this),
1412 /*is_consumer=*/false,
1413 /*is_policy_loaded=*/false,
1414 /*expected_reset=*/false));
1415 loop_.Run();
1416}
1417
1418TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1419 loop_.PostTask(FROM_HERE,
1420 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1421 base::Unretained(this),
1422 /*is_consumer=*/true,
1423 /*is_policy_loaded=*/false,
1424 /*expected_reset=*/true));
1425 loop_.Run();
1426}
1427
1428TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1429 loop_.PostTask(FROM_HERE,
1430 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1431 base::Unretained(this),
1432 /*is_consumer=*/false,
1433 /*is_policy_loaded=*/true,
1434 /*expected_reset=*/true));
1435 loop_.Run();
1436}
1437
Marton Hunyady199152d2018-05-07 19:08:48 +02001438TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001439 attempter_.install_plan_.reset(new InstallPlan);
1440 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001441
1442 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1443 SetRollbackHappened(true))
1444 .Times(1);
1445 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1446}
1447
1448TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001449 attempter_.install_plan_.reset(new InstallPlan);
1450 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001451
1452 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1453 SetRollbackHappened(true))
1454 .Times(0);
1455 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1456}
1457
Marton Hunyadya0302682018-05-16 18:52:13 +02001458TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001459 attempter_.install_plan_.reset(new InstallPlan);
1460 attempter_.install_plan_->is_rollback = true;
1461 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001462
1463 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1464 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1465 .Times(1);
1466 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1467}
1468
1469TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001470 attempter_.install_plan_.reset(new InstallPlan);
1471 attempter_.install_plan_->is_rollback = false;
1472 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001473
1474 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1475 ReportEnterpriseRollbackMetrics(_, _))
1476 .Times(0);
1477 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1478}
1479
1480TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001481 attempter_.install_plan_.reset(new InstallPlan);
1482 attempter_.install_plan_->is_rollback = true;
1483 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001484
1485 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1486 ReportEnterpriseRollbackMetrics(false, kRollbackVersion))
1487 .Times(1);
1488 MockAction action;
1489 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1490 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1491}
1492
1493TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001494 attempter_.install_plan_.reset(new InstallPlan);
1495 attempter_.install_plan_->is_rollback = false;
1496 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001497
1498 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1499 ReportEnterpriseRollbackMetrics(_, _))
1500 .Times(0);
1501 MockAction action;
1502 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1503 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1504}
1505
May Lippert60aa3ca2018-08-15 16:55:29 -07001506TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1507 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1508 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1509 .Times(0);
1510 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1511}
1512
1513TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1514 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1515 fake_system_state_.set_device_policy(device_policy.get());
1516 // Make device policy return that this is not enterprise enrolled
1517 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1518
1519 // Ensure that the metric is not recorded.
1520 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1521 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1522 .Times(0);
1523 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1524}
1525
1526TEST_F(UpdateAttempterTest,
1527 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1528 constexpr int kDaysToUpdate = 15;
1529 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1530 fake_system_state_.set_device_policy(device_policy.get());
1531 // Make device policy return that this is enterprise enrolled
1532 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1533 // Pretend that there's a time restriction policy in place
1534 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1535 .WillOnce(Return(true));
1536
1537 FakePrefs fake_prefs;
1538 Time update_first_seen_at = Time::Now();
1539 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1540 update_first_seen_at.ToInternalValue());
1541
1542 FakeClock fake_clock;
1543 Time update_finished_at =
1544 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1545 fake_clock.SetWallclockTime(update_finished_at);
1546
1547 fake_system_state_.set_clock(&fake_clock);
1548 fake_system_state_.set_prefs(&fake_prefs);
1549
1550 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1551 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1552 .Times(1);
1553 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1554}
1555
1556TEST_F(UpdateAttempterTest,
1557 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
1558 constexpr int kDaysToUpdate = 15;
1559 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1560 fake_system_state_.set_device_policy(device_policy.get());
1561 // Make device policy return that this is enterprise enrolled
1562 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1563 // Pretend that there's no time restriction policy in place
1564 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1565 .WillOnce(Return(false));
1566
1567 FakePrefs fake_prefs;
1568 Time update_first_seen_at = Time::Now();
1569 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1570 update_first_seen_at.ToInternalValue());
1571
1572 FakeClock fake_clock;
1573 Time update_finished_at =
1574 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1575 fake_clock.SetWallclockTime(update_finished_at);
1576
1577 fake_system_state_.set_clock(&fake_clock);
1578 fake_system_state_.set_prefs(&fake_prefs);
1579
1580 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1581 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
1582 .Times(1);
1583 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1584}
1585
Askar Aitzhan570ca872019-04-24 11:16:12 +02001586void UpdateAttempterTest::UpdateToQuickFixBuildStart() {
1587 // Tests that checks if device_quick_fix_build_token arrives when
1588 // policy is set.
1589 const char kToken[] = "some_token";
1590
1591 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1592 fake_system_state_.set_device_policy(device_policy.get());
1593
1594 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
1595 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
1596 .WillOnce(DoAll(SetArgPointee<0>(string(kToken)), Return(true)));
1597 attempter_.policy_provider_.reset(
1598 new policy::PolicyProvider(std::move(device_policy)));
1599 attempter_.Update("", "", "", "", false, false, 0, false, false);
1600
1601 ScheduleQuitMainLoop();
1602}
1603
1604TEST_F(UpdateAttempterTest, UpdateToQuickFixBuildStart) {
1605 loop_.PostTask(FROM_HERE,
1606 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
1607 base::Unretained(this)));
1608 loop_.Run();
1609}
1610
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001611} // namespace chromeos_update_engine