blob: 3209f15f0275f63885bac4929b88e18ce3c52a71 [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();
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200191 void ResetRollbackHappenedStart(bool is_consumer,
192 bool is_policy_available,
193 bool expected_reset);
Adolfo Victoria497044c2018-07-18 07:51:42 -0700194 // Staging related callbacks.
195 void SetUpStagingTest(const StagingSchedule& schedule, FakePrefs* prefs);
196 void CheckStagingOff();
197 void StagingSetsPrefsAndTurnsOffScatteringStart();
198 void StagingOffIfInteractiveStart();
199 void StagingOffIfOobeStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700200
Gilad Arnold74b5f552014-10-07 08:17:16 -0700201 bool actual_using_p2p_for_downloading() {
202 return actual_using_p2p_for_downloading_;
203 }
204 bool actual_using_p2p_for_sharing() {
205 return actual_using_p2p_for_sharing_;
206 }
207
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700208 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700209 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700210
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700211 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700212 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300213 OpenSSLWrapper openssl_wrapper_;
214 CertificateChecker certificate_checker_;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800215 MockDlcService mock_dlcservice_;
Alex Deymo30534502015-07-20 15:06:33 -0700216
Alex Deymo8427b4a2014-11-05 14:00:32 -0800217 NiceMock<MockActionProcessor>* processor_;
218 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800219 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700220
Gilad Arnold74b5f552014-10-07 08:17:16 -0700221 bool actual_using_p2p_for_downloading_;
222 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700223};
224
Alex Deymo60ca1a72015-06-18 18:19:15 -0700225void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700226 loop_.PostTask(
227 FROM_HERE,
228 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
229 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700230}
231
Darin Petkov1b003102010-11-30 10:18:36 -0800232TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700233 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800234 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700235 DownloadAction action(prefs_,
236 nullptr,
237 nullptr,
238 nullptr,
239 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700240 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800241 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700242 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700243 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700244 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700245 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800246}
247
248TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800249 MockAction action;
250 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700251 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800252 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800253 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700254 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
255 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800256}
257
Aaron Wood9321f502017-09-07 11:18:54 -0700258TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
259 // Simple test case, where all the values match (nothing was skipped)
260 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
261 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
262 uint64_t bytes_received_1 = bytes_progressed_1;
263 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
264 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
265
266 double progress_1 =
267 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
268 double progress_2 =
269 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
270
271 EXPECT_EQ(0.0, attempter_.download_progress_);
272 // This is set via inspecting the InstallPlan payloads when the
273 // OmahaResponseAction is completed
274 attempter_.new_payload_size_ = bytes_total;
275 NiceMock<MockServiceObserver> observer;
276 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700277 SendStatusUpdate(AllOf(
278 Field(&UpdateEngineStatus::progress, progress_1),
279 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
280 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700281 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700282 SendStatusUpdate(AllOf(
283 Field(&UpdateEngineStatus::progress, progress_2),
284 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
285 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700286 attempter_.AddObserver(&observer);
287 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
288 EXPECT_EQ(progress_1, attempter_.download_progress_);
289 // This iteration validates that a later set of updates to the variables are
290 // properly handled (so that |getStatus()| will return the same progress info
291 // as the callback is receiving.
292 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
293 EXPECT_EQ(progress_2, attempter_.download_progress_);
294}
295
296TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
297 // The transition into UpdateStatus::DOWNLOADING happens when the
298 // first bytes are received.
299 uint64_t bytes_progressed = 1024 * 1024; // 1MB
300 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
301 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
302 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
303 // This is set via inspecting the InstallPlan payloads when the
304 // OmahaResponseAction is completed
305 attempter_.new_payload_size_ = bytes_total;
306 EXPECT_EQ(0.0, attempter_.download_progress_);
307 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700308 EXPECT_CALL(observer,
309 SendStatusUpdate(AllOf(
310 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
311 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700312 attempter_.AddObserver(&observer);
313 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
314 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
315}
316
317TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
318 // There is a special case to ensure that at 100% downloaded,
319 // download_progress_ is updated and that value broadcast. This test confirms
320 // that.
321 uint64_t bytes_progressed = 0; // ignored
322 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
323 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
324 attempter_.status_ = UpdateStatus::DOWNLOADING;
325 attempter_.new_payload_size_ = bytes_total;
326 EXPECT_EQ(0.0, attempter_.download_progress_);
327 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700328 EXPECT_CALL(observer,
329 SendStatusUpdate(AllOf(
330 Field(&UpdateEngineStatus::progress, 1.0),
331 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
332 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700333 attempter_.AddObserver(&observer);
334 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
335 EXPECT_EQ(1.0, attempter_.download_progress_);
336}
337
Darin Petkov1b003102010-11-30 10:18:36 -0800338TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700339 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800340 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700341 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300342 std::move(fetcher), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800343 ObjectCollectorAction<OmahaResponse> collector_action;
344 BondActions(&action, &collector_action);
345 OmahaResponse response;
346 response.poll_interval = 234;
347 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800348 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700349 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800350 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700351 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800352 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700353 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800354}
355
Alex Deymo30534502015-07-20 15:06:33 -0700356TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700357 FakePrefs fake_prefs;
358 string boot_id;
359 EXPECT_TRUE(utils::GetBootId(&boot_id));
360 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
361 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700362 attempter_.Init();
363 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700364}
365
366TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700367 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
368 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700369 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700370 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700371
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700372 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700373 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
374 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700375 EXPECT_EQ(ErrorCode::kOmahaRequestError,
376 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700377 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700378 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700379 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700380 ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700381 FilesystemVerifierAction filesystem_verifier_action;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700382 EXPECT_EQ(ErrorCode::kFilesystemVerifierError,
383 GetErrorCodeForAction(&filesystem_verifier_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700384 ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300385 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700386 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700387 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700388 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700389 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800390 MockAction action_mock;
391 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700392 EXPECT_EQ(ErrorCode::kError,
393 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700394}
395
Darin Petkov36275772010-10-01 11:40:57 -0700396TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700397 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800398 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700399 .WillOnce(Return(false));
400 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700401 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800402 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700403 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700404 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
Darin Petkov36275772010-10-01 11:40:57 -0700405 Return(true)));
406 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700407 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800408 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700409 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700410 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
Darin Petkov36275772010-10-01 11:40:57 -0700411 Return(true)));
412 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700413 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800414 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700415 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700416 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700417}
418
419TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800420 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700421 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700422 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
423 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Darin Petkov36275772010-10-01 11:40:57 -0700424 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700425 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
Darin Petkov36275772010-10-01 11:40:57 -0700426 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800427 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700428 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800429 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700430 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800431 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700432 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700433 for (int i = 0; i < 4; i ++)
434 attempter_.MarkDeltaUpdateFailure();
435}
436
Darin Petkov1b003102010-11-30 10:18:36 -0800437TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
438 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
439 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700440 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800441 .Times(0);
442 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700443 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800444 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700445 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700446 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700447 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800448 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700449 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800450}
451
452TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
453 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700454 EnqueueAction(Pointee(Property(
455 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700456 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700457 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700458 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800459 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
460 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800461 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800462 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700463 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800464}
465
Darin Petkove6ef2f82011-03-07 17:31:11 -0800466namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700467// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700468const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Amin Hassani0882a512018-04-05 16:25:44 -0700469 OmahaRequestAction::StaticType(),
470 OmahaResponseHandlerAction::StaticType(),
471 UpdateBootFlagsAction::StaticType(),
472 OmahaRequestAction::StaticType(),
473 DownloadAction::StaticType(),
474 OmahaRequestAction::StaticType(),
475 FilesystemVerifierAction::StaticType(),
476 PostinstallRunnerAction::StaticType(),
477 OmahaRequestAction::StaticType()};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700478
479// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700480const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700481 InstallPlanAction::StaticType(),
482 PostinstallRunnerAction::StaticType(),
483};
484
Adolfo Victoria497044c2018-07-18 07:51:42 -0700485const StagingSchedule kValidStagingSchedule = {
486 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
487
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700488} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800489
490void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700491 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700492
493 // Expect that the device policy is loaded by the UpdateAttempter at some
494 // point by calling RefreshDevicePolicy.
Igor9fd76b62017-12-11 15:24:18 +0100495 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700496 EXPECT_CALL(*device_policy, LoadPolicy())
497 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100498 attempter_.policy_provider_.reset(
499 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700500
501 {
502 InSequence s;
503 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
504 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700505 EnqueueAction(Pointee(
506 Property(&AbstractAction::Type, kUpdateActionTypes[i]))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700507 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700508 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700509 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700510
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200511 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700512 loop_.PostTask(FROM_HERE,
513 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
514 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800515}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700516
Darin Petkove6ef2f82011-03-07 17:31:11 -0800517void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700518 EXPECT_EQ(0, attempter_.http_response_code());
519 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700520 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700521 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800522}
523
Chris Sosa28e479c2013-07-12 11:39:53 -0700524void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700525 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700526 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100527 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700528 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100529 fake_system_state_.set_device_policy(device_policy.get());
530 if (enterprise_rollback) {
531 // We return an empty owner as this is an enterprise.
532 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
533 DoAll(SetArgPointee<0>(string("")),
534 Return(true)));
535 } else {
536 // We return a fake owner as this is an owned consumer device.
537 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
538 DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
539 Return(true)));
540 }
541
542 attempter_.policy_provider_.reset(
543 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700544
Alex Deymo763e7db2015-08-27 21:08:08 -0700545 if (valid_slot) {
546 BootControlInterface::Slot rollback_slot = 1;
547 LOG(INFO) << "Test Mark Bootable: "
548 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700549 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
550 true);
Don Garrett6646b442013-11-13 15:29:11 -0800551 }
552
Chris Sosa28e479c2013-07-12 11:39:53 -0700553 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700554
Chris Sosad38b1132014-03-25 10:43:59 -0700555 // We only allow rollback on devices that are not enterprise enrolled and
556 // which have a valid slot to rollback to.
557 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700558 is_rollback_allowed = true;
559 }
560
Chris Sosa28e479c2013-07-12 11:39:53 -0700561 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700562 InSequence s;
563 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
564 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700565 EnqueueAction(Pointee(Property(&AbstractAction::Type,
566 kRollbackActionTypes[i]))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700567 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700568 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700569
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700570 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700571 loop_.PostTask(FROM_HERE,
572 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
573 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700574 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700575 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700576 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700577 }
578}
579
580void UpdateAttempterTest::RollbackTestVerify() {
581 // Verifies the actions that were enqueued.
582 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700583 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700584 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
585 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700586 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700587}
588
Darin Petkove6ef2f82011-03-07 17:31:11 -0800589TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700590 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700591 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700592}
593
Chris Sosa76a29ae2013-07-11 17:59:24 -0700594TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700595 loop_.PostTask(FROM_HERE,
596 base::Bind(&UpdateAttempterTest::RollbackTestStart,
597 base::Unretained(this),
598 false, true));
599 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700600}
601
Don Garrett6646b442013-11-13 15:29:11 -0800602TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700603 loop_.PostTask(FROM_HERE,
604 base::Bind(&UpdateAttempterTest::RollbackTestStart,
605 base::Unretained(this),
606 false, false));
607 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),
614 true, true));
615 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700616}
617
Thieu Le116fda32011-04-19 11:01:54 -0700618void UpdateAttempterTest::PingOmahaTestStart() {
619 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700620 EnqueueAction(Pointee(Property(
621 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700622 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700623 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700624 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700625}
626
627TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700628 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
629 EXPECT_FALSE(attempter_.schedule_updates_called());
630 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
631 // testing, which is more permissive than we want to handle here.
632 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700633 loop_.PostTask(FROM_HERE,
634 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
635 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700636 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700637 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700638 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700639}
640
Darin Petkov18c7bce2011-06-16 14:07:00 -0700641TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800642 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700643 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700644 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700645 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700646 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
647 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700648 EXPECT_EQ(
649 static_cast<ErrorCode>(static_cast<int>(kCode) |
650 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
651 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700652}
653
654TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700655 attempter_.install_plan_.reset(new InstallPlan);
656 attempter_.install_plan_->is_resume = true;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800657 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700658 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700659 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700660 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700661 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
662 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700663 EXPECT_EQ(
664 static_cast<ErrorCode>(
665 static_cast<int>(kCode) |
666 static_cast<int>(ErrorCode::kResumedFlag) |
667 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
668 attempter_.error_event_->error_code);
Darin 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);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200710 attempter_.Update("", "", "", "", false, 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);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200732 attempter_.Update("", "", "", "", false, 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());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200755 attempter_.Update("", "", "", "", false, 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());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200777 attempter_.Update("", "", "", "", false, 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,
805 false,
806 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700807 EXPECT_FALSE(actual_using_p2p_for_downloading());
808 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700809 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700810}
811
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700812TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700813 loop_.PostTask(
814 FROM_HERE,
815 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
816 base::Unretained(this)));
817 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700818}
819
820// Tests that the scatter_factor_in_seconds value is properly fetched
821// from the device policy.
822void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700823 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700824
Igor9fd76b62017-12-11 15:24:18 +0100825 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700826 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100827 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700828
829 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
830 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700831 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700832 Return(true)));
833
Igor9fd76b62017-12-11 15:24:18 +0100834 attempter_.policy_provider_.reset(
835 new policy::PolicyProvider(std::move(device_policy)));
836
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200837 attempter_.Update("", "", "", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700838 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
839
Alex Deymo60ca1a72015-06-18 18:19:15 -0700840 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700841}
842
843TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700844 loop_.PostTask(
845 FROM_HERE,
846 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
847 base::Unretained(this)));
848 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700849}
850
851void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
852 // Tests that the scatter_factor_in_seconds value is properly fetched
853 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700854 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800855 FakePrefs fake_prefs;
856 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700857
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700858 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700859
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800860 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700861
Ben Chan9abb7632014-08-07 00:10:53 -0700862 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700863
Igor9fd76b62017-12-11 15:24:18 +0100864 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700865 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100866 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700867
868 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
869 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700870 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700871 Return(true)));
872
Igor9fd76b62017-12-11 15:24:18 +0100873 attempter_.policy_provider_.reset(
874 new policy::PolicyProvider(std::move(device_policy)));
875
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200876 attempter_.Update("", "", "", "", false, 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));
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200892 attempter_.Update("", "", "", "", false, 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) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700901 loop_.PostTask(FROM_HERE, base::Bind(
902 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
903 base::Unretained(this)));
904 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700905}
906
907void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
908 // Tests that no scattering logic is enabled if the update check
909 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700910 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800911 FakePrefs fake_prefs;
912 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700913
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700914 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800915 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700916
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700917 EXPECT_TRUE(
918 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800919 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700920
921 // make sure scatter_factor is non-zero as scattering is disabled
922 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700923 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700924
Igor9fd76b62017-12-11 15:24:18 +0100925 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700926 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100927 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700928
929 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
930 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700931 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700932 Return(true)));
933
Igor9fd76b62017-12-11 15:24:18 +0100934 attempter_.policy_provider_.reset(
935 new policy::PolicyProvider(std::move(device_policy)));
936
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800937 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200938 attempter_.Update("",
939 "",
940 "",
941 "",
942 false,
943 false,
944 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700945 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
946
947 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700948 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700949 EXPECT_FALSE(
950 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700951 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700952 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
953 EXPECT_FALSE(
954 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800955 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700956
Alex Deymo60ca1a72015-06-18 18:19:15 -0700957 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700958}
959
Adolfo Victoria497044c2018-07-18 07:51:42 -0700960void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
961 FakePrefs* prefs) {
962 attempter_.prefs_ = prefs;
963 fake_system_state_.set_prefs(prefs);
964
965 int64_t initial_value = 8;
966 EXPECT_TRUE(
967 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
968 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
969 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
970
971 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
972 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
973 fake_system_state_.set_device_policy(device_policy.get());
974 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
975 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
976
977 attempter_.policy_provider_.reset(
978 new policy::PolicyProvider(std::move(device_policy)));
979}
980
981TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
982 loop_.PostTask(
983 FROM_HERE,
984 base::Bind(
985 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
986 base::Unretained(this)));
987 loop_.Run();
988}
989
990void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
991 // Tests that staging sets its prefs properly and turns off scattering.
992 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
993 FakePrefs fake_prefs;
994 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
995
996 attempter_.Update("", "", "", "", false, false, false);
997 // Check that prefs have the correct values.
998 int64_t update_count;
999 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
1000 int64_t waiting_time_days;
1001 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
1002 &waiting_time_days));
1003 EXPECT_GT(waiting_time_days, 0);
1004 // Update count should have been decremented.
1005 EXPECT_EQ(7, update_count);
1006 // Check that Omaha parameters were updated correctly.
1007 EXPECT_TRUE(
1008 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1009 EXPECT_TRUE(
1010 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1011 EXPECT_EQ(waiting_time_days,
1012 attempter_.omaha_request_params_->waiting_period().InDays());
1013 // Check class variables.
1014 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1015 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1016 // Check that scattering is turned off
1017 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1018 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1019
1020 ScheduleQuitMainLoop();
1021}
1022
1023void UpdateAttempterTest::CheckStagingOff() {
1024 // Check that all prefs were removed.
1025 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1026 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1027 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1028 // Check that the Omaha parameters have the correct value.
1029 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1030 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1031 attempter_.staging_wait_time_);
1032 EXPECT_FALSE(
1033 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1034 EXPECT_FALSE(
1035 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1036 // Check that scattering is turned off too.
1037 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1038}
1039
1040TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1041 loop_.PostTask(FROM_HERE,
1042 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1043 base::Unretained(this)));
1044 loop_.Run();
1045}
1046
1047void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1048 // Tests that staging is turned off when an interactive update is requested.
1049 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1050 FakePrefs fake_prefs;
1051 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1052
1053 attempter_.Update("", "", "", "", false, false, /* interactive = */ true);
1054 CheckStagingOff();
1055
1056 ScheduleQuitMainLoop();
1057}
1058
1059TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1060 loop_.PostTask(FROM_HERE,
1061 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1062 base::Unretained(this)));
1063 loop_.Run();
1064}
1065
1066void UpdateAttempterTest::StagingOffIfOobeStart() {
1067 // Tests that staging is turned off if OOBE hasn't been completed.
1068 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1069 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1070 FakePrefs fake_prefs;
1071 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1072
1073 attempter_.Update("", "", "", "", false, false, /* interactive = */ true);
1074 CheckStagingOff();
1075
1076 ScheduleQuitMainLoop();
1077}
1078
David Zeuthen985b1122013-10-09 12:13:15 -07001079// Checks that we only report daily metrics at most every 24 hours.
1080TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1081 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001082 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001083
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001084 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001085 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001086
1087 Time epoch = Time::FromInternalValue(0);
1088 fake_clock.SetWallclockTime(epoch);
1089
1090 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1091 // we should report.
1092 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1093 // We should not report again if no time has passed.
1094 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1095
1096 // We should not report if only 10 hours has passed.
1097 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1098 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1099
1100 // We should not report if only 24 hours - 1 sec has passed.
1101 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1102 TimeDelta::FromSeconds(1));
1103 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1104
1105 // We should report if 24 hours has passed.
1106 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1107 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1108
1109 // But then we should not report again..
1110 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1111
1112 // .. until another 24 hours has passed
1113 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1114 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1115 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1116 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1117 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1118
1119 // .. and another 24 hours
1120 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1121 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1122 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1123 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1124 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1125
1126 // If the span between time of reporting and present time is
1127 // negative, we report. This is in order to reset the timestamp and
1128 // avoid an edge condition whereby a distant point in the future is
1129 // in the state variable resulting in us never ever reporting again.
1130 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1131 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1132 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1133
1134 // In this case we should not update until the clock reads 71 + 24 = 95.
1135 // Check that.
1136 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1137 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1138 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1139 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1140 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001141}
1142
David Zeuthen3c55abd2013-10-14 12:48:03 -07001143TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001144 FakeClock fake_clock;
1145 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001146 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001147 FakePrefs fake_prefs;
1148 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001149 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001150
1151 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001152 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001153
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001154 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001155
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001156 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001157 EXPECT_EQ(boot_time.ToTimeT(), 42);
1158}
1159
David Pursell02c18642014-11-06 11:26:11 -08001160TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1161 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1162 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1163}
1164
1165TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1166 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001167 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001168 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1169}
1170
1171TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1172 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001173 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001174 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1175}
1176
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001177TEST_F(UpdateAttempterTest, CheckForUpdateAUDlcTest) {
1178 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1179 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1180
1181 const string dlc_module_id = "a_dlc_module_id";
1182 vector<string> dlc_module_ids = {dlc_module_id};
1183 ON_CALL(mock_dlcservice_, GetInstalled(testing::_))
1184 .WillByDefault(DoAll(testing::SetArgPointee<0>(dlc_module_ids),
1185 testing::Return(true)));
1186
1187 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08001188 EXPECT_EQ(attempter_.dlc_module_ids_.size(), 1);
1189 EXPECT_EQ(attempter_.dlc_module_ids_[0], dlc_module_id);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001190}
1191
David Pursell02c18642014-11-06 11:26:11 -08001192TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1193 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001194 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001195 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001196 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001197}
1198
1199TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
1200 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001201 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001202 attempter_.CheckForUpdate("", "autest-scheduled", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001203 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001204}
1205
Xiaochu Liu88d90382018-08-29 16:09:11 -07001206TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1207 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1208 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1209 attempter_.CheckForInstall({}, "autest");
1210 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1211
1212 attempter_.CheckForInstall({}, "autest-scheduled");
1213 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1214
1215 attempter_.CheckForInstall({}, "http://omaha.phishing");
1216 EXPECT_EQ("", attempter_.forced_omaha_url());
1217}
1218
Colin Howesac170d92018-11-20 16:29:28 -08001219TEST_F(UpdateAttempterTest, InstallSetsStatusIdle) {
1220 attempter_.CheckForInstall({}, "http://foo.bar");
1221 attempter_.status_ = UpdateStatus::DOWNLOADING;
1222 EXPECT_TRUE(attempter_.is_install_);
1223 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1224 UpdateEngineStatus status;
1225 attempter_.GetStatus(&status);
1226 // Should set status to idle after an install operation.
1227 EXPECT_EQ(UpdateStatus::IDLE, status.status);
1228}
1229
Colin Howes978c1082018-12-03 11:46:12 -08001230TEST_F(UpdateAttempterTest, RollbackAfterInstall) {
1231 attempter_.is_install_ = true;
1232 attempter_.Rollback(false);
1233 EXPECT_FALSE(attempter_.is_install_);
1234}
1235
1236TEST_F(UpdateAttempterTest, UpdateAfterInstall) {
1237 attempter_.is_install_ = true;
1238 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1239 EXPECT_FALSE(attempter_.is_install_);
1240}
1241
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001242TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001243 attempter_.CalculateUpdateParams("", "", "", "1234", false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001244 EXPECT_EQ("1234",
1245 fake_system_state_.request_params()->target_version_prefix());
1246
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001247 attempter_.CalculateUpdateParams("", "", "", "", false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001248 EXPECT_TRUE(
1249 fake_system_state_.request_params()->target_version_prefix().empty());
1250}
1251
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001252TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1253 attempter_.CalculateUpdateParams("",
1254 "",
1255 "",
1256 "1234",
1257 /*rollback_allowed=*/true,
1258 false,
1259 false);
1260 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1261
1262 attempter_.CalculateUpdateParams("",
1263 "",
1264 "",
1265 "1234",
1266 /*rollback_allowed=*/false,
1267 false,
1268 false);
1269 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1270}
1271
Aaron Wood23bd3392017-10-06 14:48:25 -07001272TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1273 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1274 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001275 OmahaResponseHandlerAction response_action(&fake_system_state_);
1276 response_action.install_plan_.version = "a.b.c.d";
1277 response_action.install_plan_.system_version = "b.c.d.e";
1278 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001279 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001280 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1281 // completed, with the deferred-update error code.
1282 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001283 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001284 {
1285 UpdateEngineStatus status;
1286 attempter_.GetStatus(&status);
1287 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001288 EXPECT_TRUE(attempter_.install_plan_);
1289 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
1290 EXPECT_EQ(attempter_.install_plan_->system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001291 status.new_system_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001292 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001293 status.new_size_bytes);
1294 }
1295 // An "error" event should have been created to tell Omaha that the update is
1296 // being deferred.
1297 EXPECT_TRUE(nullptr != attempter_.error_event_);
1298 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1299 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1300 ErrorCode expected_code = static_cast<ErrorCode>(
1301 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1302 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1303 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1304 // End the processing
1305 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1306 // Validate the state of the attempter.
1307 {
1308 UpdateEngineStatus status;
1309 attempter_.GetStatus(&status);
1310 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001311 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
1312 EXPECT_EQ(response_action.install_plan_.system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001313 status.new_system_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001314 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001315 status.new_size_bytes);
1316 }
1317}
1318
1319TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
1320 EXPECT_FALSE(attempter_.IsUpdateRunningOrScheduled());
1321 // Verify in-progress update with UPDATE_AVAILABLE is running
1322 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
1323 EXPECT_TRUE(attempter_.IsUpdateRunningOrScheduled());
1324}
1325
Aaron Woodbf5a2522017-10-04 10:58:36 -07001326TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1327 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1328
1329 UpdateCheckParams params = {.updates_enabled = true};
1330 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1331
1332 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1333 attempter_.GetCurrentUpdateAttemptFlags());
1334}
1335
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001336TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1337 UpdateCheckParams params = {.updates_enabled = true,
1338 .rollback_allowed = false};
1339 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1340 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1341}
1342
1343TEST_F(UpdateAttempterTest, RollbackAllowed) {
1344 UpdateCheckParams params = {.updates_enabled = true,
1345 .rollback_allowed = true};
1346 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1347 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1348}
1349
Aaron Wood081c0232017-10-19 17:14:58 -07001350TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1351 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1352
1353 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1354 EXPECT_EQ(UpdateAttemptFlags::kNone,
1355 attempter_.GetCurrentUpdateAttemptFlags());
1356}
1357
1358TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1359 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1360
1361 // This tests that when CheckForUpdate() is called with the non-interactive
1362 // flag set, that it doesn't change the current UpdateAttemptFlags.
1363 attempter_.CheckForUpdate("",
1364 "",
1365 UpdateAttemptFlags::kFlagNonInteractive |
1366 UpdateAttemptFlags::kFlagRestrictDownload);
1367 EXPECT_EQ(UpdateAttemptFlags::kNone,
1368 attempter_.GetCurrentUpdateAttemptFlags());
1369}
1370
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001371void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1372 bool is_policy_loaded,
1373 bool expected_reset) {
1374 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1375 .WillRepeatedly(Return(true));
1376 auto mock_policy_provider =
1377 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1378 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1379 .WillRepeatedly(Return(is_consumer));
1380 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1381 .WillRepeatedly(Return(is_policy_loaded));
1382 const policy::MockDevicePolicy device_policy;
1383 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1384 .WillRepeatedly(ReturnRef(device_policy));
1385 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1386 SetRollbackHappened(false))
1387 .Times(expected_reset ? 1 : 0);
1388 attempter_.policy_provider_ = std::move(mock_policy_provider);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001389 attempter_.Update("", "", "", "", false, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001390 ScheduleQuitMainLoop();
1391}
1392
1393TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1394 loop_.PostTask(FROM_HERE,
1395 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1396 base::Unretained(this),
1397 /*is_consumer=*/false,
1398 /*is_policy_loaded=*/false,
1399 /*expected_reset=*/false));
1400 loop_.Run();
1401}
1402
1403TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1404 loop_.PostTask(FROM_HERE,
1405 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1406 base::Unretained(this),
1407 /*is_consumer=*/true,
1408 /*is_policy_loaded=*/false,
1409 /*expected_reset=*/true));
1410 loop_.Run();
1411}
1412
1413TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1414 loop_.PostTask(FROM_HERE,
1415 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1416 base::Unretained(this),
1417 /*is_consumer=*/false,
1418 /*is_policy_loaded=*/true,
1419 /*expected_reset=*/true));
1420 loop_.Run();
1421}
1422
Marton Hunyady199152d2018-05-07 19:08:48 +02001423TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001424 attempter_.install_plan_.reset(new InstallPlan);
1425 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001426
1427 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1428 SetRollbackHappened(true))
1429 .Times(1);
1430 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1431}
1432
1433TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001434 attempter_.install_plan_.reset(new InstallPlan);
1435 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001436
1437 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1438 SetRollbackHappened(true))
1439 .Times(0);
1440 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1441}
1442
Marton Hunyadya0302682018-05-16 18:52:13 +02001443TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001444 attempter_.install_plan_.reset(new InstallPlan);
1445 attempter_.install_plan_->is_rollback = true;
1446 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001447
1448 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1449 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1450 .Times(1);
1451 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1452}
1453
1454TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001455 attempter_.install_plan_.reset(new InstallPlan);
1456 attempter_.install_plan_->is_rollback = false;
1457 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001458
1459 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1460 ReportEnterpriseRollbackMetrics(_, _))
1461 .Times(0);
1462 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1463}
1464
1465TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001466 attempter_.install_plan_.reset(new InstallPlan);
1467 attempter_.install_plan_->is_rollback = true;
1468 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001469
1470 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1471 ReportEnterpriseRollbackMetrics(false, kRollbackVersion))
1472 .Times(1);
1473 MockAction action;
1474 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1475 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1476}
1477
1478TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001479 attempter_.install_plan_.reset(new InstallPlan);
1480 attempter_.install_plan_->is_rollback = false;
1481 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001482
1483 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1484 ReportEnterpriseRollbackMetrics(_, _))
1485 .Times(0);
1486 MockAction action;
1487 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1488 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1489}
1490
May Lippert60aa3ca2018-08-15 16:55:29 -07001491TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1492 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1493 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1494 .Times(0);
1495 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1496}
1497
1498TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1499 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1500 fake_system_state_.set_device_policy(device_policy.get());
1501 // Make device policy return that this is not enterprise enrolled
1502 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1503
1504 // Ensure that the metric is not recorded.
1505 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1506 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1507 .Times(0);
1508 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1509}
1510
1511TEST_F(UpdateAttempterTest,
1512 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1513 constexpr int kDaysToUpdate = 15;
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 enterprise enrolled
1517 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1518 // Pretend that there's a time restriction policy in place
1519 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1520 .WillOnce(Return(true));
1521
1522 FakePrefs fake_prefs;
1523 Time update_first_seen_at = Time::Now();
1524 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1525 update_first_seen_at.ToInternalValue());
1526
1527 FakeClock fake_clock;
1528 Time update_finished_at =
1529 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1530 fake_clock.SetWallclockTime(update_finished_at);
1531
1532 fake_system_state_.set_clock(&fake_clock);
1533 fake_system_state_.set_prefs(&fake_prefs);
1534
1535 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1536 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1537 .Times(1);
1538 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1539}
1540
1541TEST_F(UpdateAttempterTest,
1542 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
1543 constexpr int kDaysToUpdate = 15;
1544 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1545 fake_system_state_.set_device_policy(device_policy.get());
1546 // Make device policy return that this is enterprise enrolled
1547 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1548 // Pretend that there's no time restriction policy in place
1549 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1550 .WillOnce(Return(false));
1551
1552 FakePrefs fake_prefs;
1553 Time update_first_seen_at = Time::Now();
1554 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1555 update_first_seen_at.ToInternalValue());
1556
1557 FakeClock fake_clock;
1558 Time update_finished_at =
1559 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1560 fake_clock.SetWallclockTime(update_finished_at);
1561
1562 fake_system_state_.set_clock(&fake_clock);
1563 fake_system_state_.set_prefs(&fake_prefs);
1564
1565 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1566 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
1567 .Times(1);
1568 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1569}
1570
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001571} // namespace chromeos_update_engine