blob: 579c7368fa043760e290a874fc7d3f14b5b7eced [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 }
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800204 bool actual_using_p2p_for_sharing() { return actual_using_p2p_for_sharing_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700205
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700206 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700207 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700208
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700209 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700210 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300211 OpenSSLWrapper openssl_wrapper_;
212 CertificateChecker certificate_checker_;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800213 MockDlcService mock_dlcservice_;
Alex Deymo30534502015-07-20 15:06:33 -0700214
Alex Deymo8427b4a2014-11-05 14:00:32 -0800215 NiceMock<MockActionProcessor>* processor_;
216 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800217 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700218
Gilad Arnold74b5f552014-10-07 08:17:16 -0700219 bool actual_using_p2p_for_downloading_;
220 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700221};
222
Alex Deymo60ca1a72015-06-18 18:19:15 -0700223void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700224 loop_.PostTask(
225 FROM_HERE,
226 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
227 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700228}
229
Darin Petkov1b003102010-11-30 10:18:36 -0800230TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700231 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800232 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700233 DownloadAction action(prefs_,
234 nullptr,
235 nullptr,
236 nullptr,
237 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700238 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800239 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700240 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700241 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700242 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700243 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800244}
245
246TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800247 MockAction action;
248 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700249 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800250 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800251 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700252 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
253 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800254}
255
Aaron Wood9321f502017-09-07 11:18:54 -0700256TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
257 // Simple test case, where all the values match (nothing was skipped)
258 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
259 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
260 uint64_t bytes_received_1 = bytes_progressed_1;
261 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
262 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
263
264 double progress_1 =
265 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
266 double progress_2 =
267 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
268
269 EXPECT_EQ(0.0, attempter_.download_progress_);
270 // This is set via inspecting the InstallPlan payloads when the
271 // OmahaResponseAction is completed
272 attempter_.new_payload_size_ = bytes_total;
273 NiceMock<MockServiceObserver> observer;
274 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700275 SendStatusUpdate(AllOf(
276 Field(&UpdateEngineStatus::progress, progress_1),
277 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
278 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700279 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700280 SendStatusUpdate(AllOf(
281 Field(&UpdateEngineStatus::progress, progress_2),
282 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
283 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700284 attempter_.AddObserver(&observer);
285 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
286 EXPECT_EQ(progress_1, attempter_.download_progress_);
287 // This iteration validates that a later set of updates to the variables are
288 // properly handled (so that |getStatus()| will return the same progress info
289 // as the callback is receiving.
290 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
291 EXPECT_EQ(progress_2, attempter_.download_progress_);
292}
293
294TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
295 // The transition into UpdateStatus::DOWNLOADING happens when the
296 // first bytes are received.
297 uint64_t bytes_progressed = 1024 * 1024; // 1MB
298 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
299 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
300 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
301 // This is set via inspecting the InstallPlan payloads when the
302 // OmahaResponseAction is completed
303 attempter_.new_payload_size_ = bytes_total;
304 EXPECT_EQ(0.0, attempter_.download_progress_);
305 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700306 EXPECT_CALL(observer,
307 SendStatusUpdate(AllOf(
308 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
309 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700310 attempter_.AddObserver(&observer);
311 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
312 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
313}
314
315TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
316 // There is a special case to ensure that at 100% downloaded,
317 // download_progress_ is updated and that value broadcast. This test confirms
318 // that.
319 uint64_t bytes_progressed = 0; // ignored
320 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
321 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
322 attempter_.status_ = UpdateStatus::DOWNLOADING;
323 attempter_.new_payload_size_ = bytes_total;
324 EXPECT_EQ(0.0, attempter_.download_progress_);
325 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700326 EXPECT_CALL(observer,
327 SendStatusUpdate(AllOf(
328 Field(&UpdateEngineStatus::progress, 1.0),
329 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
330 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700331 attempter_.AddObserver(&observer);
332 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
333 EXPECT_EQ(1.0, attempter_.download_progress_);
334}
335
Darin Petkov1b003102010-11-30 10:18:36 -0800336TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700337 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800338 fetcher->FailTransfer(500); // Sets the HTTP response code.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800339 OmahaRequestAction action(
340 &fake_system_state_, nullptr, std::move(fetcher), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800341 ObjectCollectorAction<OmahaResponse> collector_action;
342 BondActions(&action, &collector_action);
343 OmahaResponse response;
344 response.poll_interval = 234;
345 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800346 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700347 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800348 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700349 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800350 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700351 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800352}
353
Alex Deymo30534502015-07-20 15:06:33 -0700354TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700355 FakePrefs fake_prefs;
356 string boot_id;
357 EXPECT_TRUE(utils::GetBootId(&boot_id));
358 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
359 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700360 attempter_.Init();
361 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700362}
363
364TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700365 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700366 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700367
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700368 FakeSystemState fake_system_state;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800369 OmahaRequestAction omaha_request_action(
370 &fake_system_state, nullptr, nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700371 EXPECT_EQ(ErrorCode::kOmahaRequestError,
372 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700373 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800374 EXPECT_EQ(
375 ErrorCode::kOmahaResponseHandlerError,
376 GetErrorCodeForAction(&omaha_response_handler_action, ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700377 FilesystemVerifierAction filesystem_verifier_action;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800378 EXPECT_EQ(
379 ErrorCode::kFilesystemVerifierError,
380 GetErrorCodeForAction(&filesystem_verifier_action, ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300381 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700382 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800383 EXPECT_EQ(
384 ErrorCode::kPostinstallRunnerError,
385 GetErrorCodeForAction(&postinstall_runner_action, ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800386 MockAction action_mock;
387 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700388 EXPECT_EQ(ErrorCode::kError,
389 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700390}
391
Darin Petkov36275772010-10-01 11:40:57 -0700392TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700393 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800394 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700395 .WillOnce(Return(false));
396 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700397 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800398 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800399 .WillOnce(
400 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
401 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700402 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700403 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800404 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800405 .WillOnce(
406 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
407 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700408 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700409 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800410 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700411 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700412 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700413}
414
415TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800416 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700417 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700418 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
419 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800420 .WillOnce(
421 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
422 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800423 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700424 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800425 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700426 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800427 EXPECT_CALL(*prefs_,
428 SetInt64(kPrefsDeltaUpdateFailures,
429 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
430 for (int i = 0; i < 4; i++)
Darin Petkov36275772010-10-01 11:40:57 -0700431 attempter_.MarkDeltaUpdateFailure();
432}
433
Darin Petkov1b003102010-11-30 10:18:36 -0800434TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
435 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
436 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700437 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800438 .Times(0);
439 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700440 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800441 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700442 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700443 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700444 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800445 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700446 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800447}
448
449TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
450 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700451 EnqueueAction(Pointee(Property(
452 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700453 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700454 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700455 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800456 attempter_.error_event_.reset(new OmahaEvent(
457 OmahaEvent::kTypeUpdateComplete, OmahaEvent::kResultError, err));
Darin Petkov1b003102010-11-30 10:18:36 -0800458 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700459 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800460}
461
Darin Petkove6ef2f82011-03-07 17:31:11 -0800462namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700463// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700464const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Amin Hassani0882a512018-04-05 16:25:44 -0700465 OmahaRequestAction::StaticType(),
466 OmahaResponseHandlerAction::StaticType(),
467 UpdateBootFlagsAction::StaticType(),
468 OmahaRequestAction::StaticType(),
469 DownloadAction::StaticType(),
470 OmahaRequestAction::StaticType(),
471 FilesystemVerifierAction::StaticType(),
472 PostinstallRunnerAction::StaticType(),
473 OmahaRequestAction::StaticType()};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700474
475// Actions that will be built as part of a user-initiated rollback.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800476const string kRollbackActionTypes[] = {
477 // NOLINT(runtime/string)
478 InstallPlanAction::StaticType(),
479 PostinstallRunnerAction::StaticType(),
Chris Sosa76a29ae2013-07-11 17:59:24 -0700480};
481
Adolfo Victoria497044c2018-07-18 07:51:42 -0700482const StagingSchedule kValidStagingSchedule = {
483 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
484
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700485} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800486
487void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700488 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700489
490 // Expect that the device policy is loaded by the UpdateAttempter at some
491 // point by calling RefreshDevicePolicy.
Igor9fd76b62017-12-11 15:24:18 +0100492 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700493 EXPECT_CALL(*device_policy, LoadPolicy())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800494 .Times(testing::AtLeast(1))
495 .WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100496 attempter_.policy_provider_.reset(
497 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700498
499 {
500 InSequence s;
501 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
502 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700503 EnqueueAction(Pointee(
504 Property(&AbstractAction::Type, kUpdateActionTypes[i]))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700505 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700506 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700507 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700508
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200509 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700510 loop_.PostTask(FROM_HERE,
511 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
512 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800513}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700514
Darin Petkove6ef2f82011-03-07 17:31:11 -0800515void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700516 EXPECT_EQ(0, attempter_.http_response_code());
517 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700518 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700519 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800520}
521
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800522void UpdateAttempterTest::RollbackTestStart(bool enterprise_rollback,
523 bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700524 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100525 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700526 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100527 fake_system_state_.set_device_policy(device_policy.get());
528 if (enterprise_rollback) {
529 // We return an empty owner as this is an enterprise.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800530 EXPECT_CALL(*device_policy, GetOwner(_))
531 .WillRepeatedly(DoAll(SetArgPointee<0>(string("")), Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100532 } else {
533 // We return a fake owner as this is an owned consumer device.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800534 EXPECT_CALL(*device_policy, GetOwner(_))
535 .WillRepeatedly(DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
536 Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100537 }
538
539 attempter_.policy_provider_.reset(
540 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700541
Alex Deymo763e7db2015-08-27 21:08:08 -0700542 if (valid_slot) {
543 BootControlInterface::Slot rollback_slot = 1;
544 LOG(INFO) << "Test Mark Bootable: "
545 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700546 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
547 true);
Don Garrett6646b442013-11-13 15:29:11 -0800548 }
549
Chris Sosa28e479c2013-07-12 11:39:53 -0700550 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700551
Chris Sosad38b1132014-03-25 10:43:59 -0700552 // We only allow rollback on devices that are not enterprise enrolled and
553 // which have a valid slot to rollback to.
554 if (!enterprise_rollback && valid_slot) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800555 is_rollback_allowed = true;
Chris Sosa28e479c2013-07-12 11:39:53 -0700556 }
557
Chris Sosa28e479c2013-07-12 11:39:53 -0700558 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700559 InSequence s;
560 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
561 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700562 EnqueueAction(Pointee(Property(&AbstractAction::Type,
563 kRollbackActionTypes[i]))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700564 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700565 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700566
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700567 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700568 loop_.PostTask(FROM_HERE,
569 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
570 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700571 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700572 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700573 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700574 }
575}
576
577void UpdateAttempterTest::RollbackTestVerify() {
578 // Verifies the actions that were enqueued.
579 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700580 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700581 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
582 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700583 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700584}
585
Darin Petkove6ef2f82011-03-07 17:31:11 -0800586TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700587 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700588 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700589}
590
Chris Sosa76a29ae2013-07-11 17:59:24 -0700591TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700592 loop_.PostTask(FROM_HERE,
593 base::Bind(&UpdateAttempterTest::RollbackTestStart,
594 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800595 false,
596 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700597 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700598}
599
Don Garrett6646b442013-11-13 15:29:11 -0800600TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700601 loop_.PostTask(FROM_HERE,
602 base::Bind(&UpdateAttempterTest::RollbackTestStart,
603 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800604 false,
605 false));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700606 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800607}
608
Chris Sosa76a29ae2013-07-11 17:59:24 -0700609TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700610 loop_.PostTask(FROM_HERE,
611 base::Bind(&UpdateAttempterTest::RollbackTestStart,
612 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800613 true,
614 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700615 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(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800664 static_cast<ErrorCode>(static_cast<int>(kCode) |
665 static_cast<int>(ErrorCode::kResumedFlag) |
666 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700667 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700668}
669
David Zeuthen8f191b22013-08-06 12:27:50 -0700670TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
671 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700672 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700673 mock_p2p_manager.fake().SetP2PEnabled(false);
674 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
675 attempter_.UpdateEngineStarted();
676}
677
678TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
679 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700680 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700681 mock_p2p_manager.fake().SetP2PEnabled(true);
682 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
683 attempter_.UpdateEngineStarted();
684}
685
686TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
687 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700688 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700689 mock_p2p_manager.fake().SetP2PEnabled(true);
690 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700691 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700692 attempter_.UpdateEngineStarted();
693}
694
695TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700696 loop_.PostTask(FROM_HERE,
697 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
698 base::Unretained(this)));
699 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700700}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700701
David Zeuthen8f191b22013-08-06 12:27:50 -0700702void UpdateAttempterTest::P2PNotEnabledStart() {
703 // If P2P is not enabled, check that we do not attempt housekeeping
704 // and do not convey that p2p is to be used.
705 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700706 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700707 mock_p2p_manager.fake().SetP2PEnabled(false);
708 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200709 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700710 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700711 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700712 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700713}
714
715TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700716 loop_.PostTask(FROM_HERE,
717 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
718 base::Unretained(this)));
719 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700720}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700721
David Zeuthen8f191b22013-08-06 12:27:50 -0700722void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
723 // If p2p is enabled, but starting it fails ensure we don't do
724 // any housekeeping and do not convey that p2p should be used.
725 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700726 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700727 mock_p2p_manager.fake().SetP2PEnabled(true);
728 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
729 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
730 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200731 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700732 EXPECT_FALSE(actual_using_p2p_for_downloading());
733 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700734 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700735}
736
737TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700738 loop_.PostTask(
739 FROM_HERE,
740 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
741 base::Unretained(this)));
742 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700743}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700744
David Zeuthen8f191b22013-08-06 12:27:50 -0700745void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
746 // If p2p is enabled, starting it works but housekeeping fails, ensure
747 // we do not convey p2p is to be used.
748 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700749 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700750 mock_p2p_manager.fake().SetP2PEnabled(true);
751 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
752 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700753 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200754 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700755 EXPECT_FALSE(actual_using_p2p_for_downloading());
756 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700757 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700758}
759
760TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700761 loop_.PostTask(FROM_HERE,
762 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
763 base::Unretained(this)));
764 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700765}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700766
David Zeuthen8f191b22013-08-06 12:27:50 -0700767void UpdateAttempterTest::P2PEnabledStart() {
768 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700769 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700770 // If P2P is enabled and starting it works, check that we performed
771 // housekeeping and that we convey p2p should be used.
772 mock_p2p_manager.fake().SetP2PEnabled(true);
773 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
774 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700775 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200776 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700777 EXPECT_TRUE(actual_using_p2p_for_downloading());
778 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700779 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700780}
781
782TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700783 loop_.PostTask(FROM_HERE,
784 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
785 base::Unretained(this)));
786 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700787}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700788
David Zeuthen8f191b22013-08-06 12:27:50 -0700789void UpdateAttempterTest::P2PEnabledInteractiveStart() {
790 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700791 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700792 // For an interactive check, if P2P is enabled and starting it
793 // works, check that we performed housekeeping and that we convey
794 // p2p should be used for sharing but NOT for downloading.
795 mock_p2p_manager.fake().SetP2PEnabled(true);
796 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
797 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700798 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200799 attempter_.Update("",
800 "",
801 "",
802 "",
803 false,
804 false,
805 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700806 EXPECT_FALSE(actual_using_p2p_for_downloading());
807 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700808 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700809}
810
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700811TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700812 loop_.PostTask(
813 FROM_HERE,
814 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
815 base::Unretained(this)));
816 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700817}
818
819// Tests that the scatter_factor_in_seconds value is properly fetched
820// from the device policy.
821void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700822 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700823
Igor9fd76b62017-12-11 15:24:18 +0100824 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700825 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100826 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700827
828 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800829 .WillRepeatedly(
830 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700831
Igor9fd76b62017-12-11 15:24:18 +0100832 attempter_.policy_provider_.reset(
833 new policy::PolicyProvider(std::move(device_policy)));
834
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200835 attempter_.Update("", "", "", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700836 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
837
Alex Deymo60ca1a72015-06-18 18:19:15 -0700838 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700839}
840
841TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700842 loop_.PostTask(
843 FROM_HERE,
844 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
845 base::Unretained(this)));
846 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700847}
848
849void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
850 // Tests that the scatter_factor_in_seconds value is properly fetched
851 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700852 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800853 FakePrefs fake_prefs;
854 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700855
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700856 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700857
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800858 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700859
Ben Chan9abb7632014-08-07 00:10:53 -0700860 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700861
Igor9fd76b62017-12-11 15:24:18 +0100862 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700863 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100864 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700865
866 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800867 .WillRepeatedly(
868 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700869
Igor9fd76b62017-12-11 15:24:18 +0100870 attempter_.policy_provider_.reset(
871 new policy::PolicyProvider(std::move(device_policy)));
872
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200873 attempter_.Update("", "", "", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700874 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
875
876 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800877 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700878
Ben Chan9abb7632014-08-07 00:10:53 -0700879 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800880 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700881 EXPECT_EQ(initial_value - 1, new_value);
882
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700883 EXPECT_TRUE(
884 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700885
886 // However, if the count is already 0, it's not decremented. Test that.
887 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800888 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200889 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800890 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
891 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700892 EXPECT_EQ(initial_value, new_value);
893
Alex Deymo60ca1a72015-06-18 18:19:15 -0700894 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700895}
896
Jay Srinivasan08fce042012-06-07 16:31:01 -0700897TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800898 loop_.PostTask(
899 FROM_HERE,
900 base::Bind(
901 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
902 base::Unretained(this)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700903 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700904}
905
906void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
907 // Tests that no scattering logic is enabled if the update check
908 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700909 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800910 FakePrefs fake_prefs;
911 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700912
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700913 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800914 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700915
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700916 EXPECT_TRUE(
917 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800918 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700919
920 // make sure scatter_factor is non-zero as scattering is disabled
921 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700922 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700923
Igor9fd76b62017-12-11 15:24:18 +0100924 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700925 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100926 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700927
928 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800929 .WillRepeatedly(
930 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700931
Igor9fd76b62017-12-11 15:24:18 +0100932 attempter_.policy_provider_.reset(
933 new policy::PolicyProvider(std::move(device_policy)));
934
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800935 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200936 attempter_.Update("",
937 "",
938 "",
939 "",
940 false,
941 false,
942 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700943 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
944
945 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700946 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700947 EXPECT_FALSE(
948 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700949 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700950 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
951 EXPECT_FALSE(
952 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800953 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700954
Alex Deymo60ca1a72015-06-18 18:19:15 -0700955 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700956}
957
Adolfo Victoria497044c2018-07-18 07:51:42 -0700958void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
959 FakePrefs* prefs) {
960 attempter_.prefs_ = prefs;
961 fake_system_state_.set_prefs(prefs);
962
963 int64_t initial_value = 8;
964 EXPECT_TRUE(
965 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
966 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
967 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
968
969 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
970 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
971 fake_system_state_.set_device_policy(device_policy.get());
972 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
973 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
974
975 attempter_.policy_provider_.reset(
976 new policy::PolicyProvider(std::move(device_policy)));
977}
978
979TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
980 loop_.PostTask(
981 FROM_HERE,
982 base::Bind(
983 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
984 base::Unretained(this)));
985 loop_.Run();
986}
987
988void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
989 // Tests that staging sets its prefs properly and turns off scattering.
990 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
991 FakePrefs fake_prefs;
992 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
993
994 attempter_.Update("", "", "", "", false, false, false);
995 // Check that prefs have the correct values.
996 int64_t update_count;
997 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
998 int64_t waiting_time_days;
999 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
1000 &waiting_time_days));
1001 EXPECT_GT(waiting_time_days, 0);
1002 // Update count should have been decremented.
1003 EXPECT_EQ(7, update_count);
1004 // Check that Omaha parameters were updated correctly.
1005 EXPECT_TRUE(
1006 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1007 EXPECT_TRUE(
1008 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1009 EXPECT_EQ(waiting_time_days,
1010 attempter_.omaha_request_params_->waiting_period().InDays());
1011 // Check class variables.
1012 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1013 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1014 // Check that scattering is turned off
1015 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1016 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1017
1018 ScheduleQuitMainLoop();
1019}
1020
1021void UpdateAttempterTest::CheckStagingOff() {
1022 // Check that all prefs were removed.
1023 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1024 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1025 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1026 // Check that the Omaha parameters have the correct value.
1027 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1028 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1029 attempter_.staging_wait_time_);
1030 EXPECT_FALSE(
1031 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1032 EXPECT_FALSE(
1033 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1034 // Check that scattering is turned off too.
1035 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1036}
1037
1038TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1039 loop_.PostTask(FROM_HERE,
1040 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1041 base::Unretained(this)));
1042 loop_.Run();
1043}
1044
1045void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1046 // Tests that staging is turned off when an interactive update is requested.
1047 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1048 FakePrefs fake_prefs;
1049 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1050
1051 attempter_.Update("", "", "", "", false, false, /* interactive = */ true);
1052 CheckStagingOff();
1053
1054 ScheduleQuitMainLoop();
1055}
1056
1057TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1058 loop_.PostTask(FROM_HERE,
1059 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1060 base::Unretained(this)));
1061 loop_.Run();
1062}
1063
1064void UpdateAttempterTest::StagingOffIfOobeStart() {
1065 // Tests that staging is turned off if OOBE hasn't been completed.
1066 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1067 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1068 FakePrefs fake_prefs;
1069 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1070
1071 attempter_.Update("", "", "", "", false, false, /* interactive = */ true);
1072 CheckStagingOff();
1073
1074 ScheduleQuitMainLoop();
1075}
1076
David Zeuthen985b1122013-10-09 12:13:15 -07001077// Checks that we only report daily metrics at most every 24 hours.
1078TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1079 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001080 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001081
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001082 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001083 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001084
1085 Time epoch = Time::FromInternalValue(0);
1086 fake_clock.SetWallclockTime(epoch);
1087
1088 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1089 // we should report.
1090 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1091 // We should not report again if no time has passed.
1092 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1093
1094 // We should not report if only 10 hours has passed.
1095 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1096 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1097
1098 // We should not report if only 24 hours - 1 sec has passed.
1099 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1100 TimeDelta::FromSeconds(1));
1101 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1102
1103 // We should report if 24 hours has passed.
1104 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1105 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1106
1107 // But then we should not report again..
1108 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1109
1110 // .. until another 24 hours has passed
1111 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1112 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1113 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1114 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1115 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1116
1117 // .. and another 24 hours
1118 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1119 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1120 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1121 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1122 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1123
1124 // If the span between time of reporting and present time is
1125 // negative, we report. This is in order to reset the timestamp and
1126 // avoid an edge condition whereby a distant point in the future is
1127 // in the state variable resulting in us never ever reporting again.
1128 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1129 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1130 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1131
1132 // In this case we should not update until the clock reads 71 + 24 = 95.
1133 // Check that.
1134 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1135 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1136 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1137 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1138 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001139}
1140
David Zeuthen3c55abd2013-10-14 12:48:03 -07001141TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001142 FakeClock fake_clock;
1143 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001144 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001145 FakePrefs fake_prefs;
1146 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001147 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001148
1149 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001150 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001151
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001152 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001153
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001154 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001155 EXPECT_EQ(boot_time.ToTimeT(), 42);
1156}
1157
David Pursell02c18642014-11-06 11:26:11 -08001158TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1159 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1160 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1161}
1162
1163TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1164 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001165 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001166 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1167}
1168
1169TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1170 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001171 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001172 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1173}
1174
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001175TEST_F(UpdateAttempterTest, CheckForUpdateAUDlcTest) {
1176 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1177 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1178
1179 const string dlc_module_id = "a_dlc_module_id";
1180 vector<string> dlc_module_ids = {dlc_module_id};
1181 ON_CALL(mock_dlcservice_, GetInstalled(testing::_))
1182 .WillByDefault(DoAll(testing::SetArgPointee<0>(dlc_module_ids),
1183 testing::Return(true)));
1184
1185 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08001186 EXPECT_EQ(attempter_.dlc_module_ids_.size(), 1);
1187 EXPECT_EQ(attempter_.dlc_module_ids_[0], dlc_module_id);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001188}
1189
David Pursell02c18642014-11-06 11:26:11 -08001190TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1191 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001192 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001193 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001194 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001195}
1196
1197TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
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-scheduled", 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
Xiaochu Liu88d90382018-08-29 16:09:11 -07001204TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1205 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1206 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1207 attempter_.CheckForInstall({}, "autest");
1208 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1209
1210 attempter_.CheckForInstall({}, "autest-scheduled");
1211 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1212
1213 attempter_.CheckForInstall({}, "http://omaha.phishing");
1214 EXPECT_EQ("", attempter_.forced_omaha_url());
1215}
1216
Colin Howesac170d92018-11-20 16:29:28 -08001217TEST_F(UpdateAttempterTest, InstallSetsStatusIdle) {
1218 attempter_.CheckForInstall({}, "http://foo.bar");
1219 attempter_.status_ = UpdateStatus::DOWNLOADING;
1220 EXPECT_TRUE(attempter_.is_install_);
1221 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1222 UpdateEngineStatus status;
1223 attempter_.GetStatus(&status);
1224 // Should set status to idle after an install operation.
1225 EXPECT_EQ(UpdateStatus::IDLE, status.status);
1226}
1227
Colin Howes978c1082018-12-03 11:46:12 -08001228TEST_F(UpdateAttempterTest, RollbackAfterInstall) {
1229 attempter_.is_install_ = true;
1230 attempter_.Rollback(false);
1231 EXPECT_FALSE(attempter_.is_install_);
1232}
1233
1234TEST_F(UpdateAttempterTest, UpdateAfterInstall) {
1235 attempter_.is_install_ = true;
1236 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1237 EXPECT_FALSE(attempter_.is_install_);
1238}
1239
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001240TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001241 attempter_.CalculateUpdateParams("", "", "", "1234", false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001242 EXPECT_EQ("1234",
1243 fake_system_state_.request_params()->target_version_prefix());
1244
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001245 attempter_.CalculateUpdateParams("", "", "", "", false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001246 EXPECT_TRUE(
1247 fake_system_state_.request_params()->target_version_prefix().empty());
1248}
1249
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001250TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1251 attempter_.CalculateUpdateParams("",
1252 "",
1253 "",
1254 "1234",
1255 /*rollback_allowed=*/true,
1256 false,
1257 false);
1258 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1259
1260 attempter_.CalculateUpdateParams("",
1261 "",
1262 "",
1263 "1234",
1264 /*rollback_allowed=*/false,
1265 false,
1266 false);
1267 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1268}
1269
Aaron Wood23bd3392017-10-06 14:48:25 -07001270TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1271 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1272 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001273 OmahaResponseHandlerAction response_action(&fake_system_state_);
1274 response_action.install_plan_.version = "a.b.c.d";
1275 response_action.install_plan_.system_version = "b.c.d.e";
1276 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001277 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001278 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1279 // completed, with the deferred-update error code.
1280 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001281 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001282 {
1283 UpdateEngineStatus status;
1284 attempter_.GetStatus(&status);
1285 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001286 EXPECT_TRUE(attempter_.install_plan_);
1287 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
1288 EXPECT_EQ(attempter_.install_plan_->system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001289 status.new_system_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001290 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001291 status.new_size_bytes);
1292 }
1293 // An "error" event should have been created to tell Omaha that the update is
1294 // being deferred.
1295 EXPECT_TRUE(nullptr != attempter_.error_event_);
1296 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1297 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1298 ErrorCode expected_code = static_cast<ErrorCode>(
1299 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1300 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1301 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1302 // End the processing
1303 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1304 // Validate the state of the attempter.
1305 {
1306 UpdateEngineStatus status;
1307 attempter_.GetStatus(&status);
1308 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001309 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
1310 EXPECT_EQ(response_action.install_plan_.system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001311 status.new_system_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001312 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001313 status.new_size_bytes);
1314 }
1315}
1316
1317TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
1318 EXPECT_FALSE(attempter_.IsUpdateRunningOrScheduled());
1319 // Verify in-progress update with UPDATE_AVAILABLE is running
1320 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
1321 EXPECT_TRUE(attempter_.IsUpdateRunningOrScheduled());
1322}
1323
Aaron Woodbf5a2522017-10-04 10:58:36 -07001324TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1325 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1326
1327 UpdateCheckParams params = {.updates_enabled = true};
1328 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1329
1330 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1331 attempter_.GetCurrentUpdateAttemptFlags());
1332}
1333
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001334TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1335 UpdateCheckParams params = {.updates_enabled = true,
1336 .rollback_allowed = false};
1337 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1338 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1339}
1340
1341TEST_F(UpdateAttempterTest, RollbackAllowed) {
1342 UpdateCheckParams params = {.updates_enabled = true,
1343 .rollback_allowed = true};
1344 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1345 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1346}
1347
Aaron Wood081c0232017-10-19 17:14:58 -07001348TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1349 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1350
1351 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1352 EXPECT_EQ(UpdateAttemptFlags::kNone,
1353 attempter_.GetCurrentUpdateAttemptFlags());
1354}
1355
1356TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1357 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1358
1359 // This tests that when CheckForUpdate() is called with the non-interactive
1360 // flag set, that it doesn't change the current UpdateAttemptFlags.
1361 attempter_.CheckForUpdate("",
1362 "",
1363 UpdateAttemptFlags::kFlagNonInteractive |
1364 UpdateAttemptFlags::kFlagRestrictDownload);
1365 EXPECT_EQ(UpdateAttemptFlags::kNone,
1366 attempter_.GetCurrentUpdateAttemptFlags());
1367}
1368
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001369void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1370 bool is_policy_loaded,
1371 bool expected_reset) {
1372 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1373 .WillRepeatedly(Return(true));
1374 auto mock_policy_provider =
1375 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1376 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1377 .WillRepeatedly(Return(is_consumer));
1378 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1379 .WillRepeatedly(Return(is_policy_loaded));
1380 const policy::MockDevicePolicy device_policy;
1381 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1382 .WillRepeatedly(ReturnRef(device_policy));
1383 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1384 SetRollbackHappened(false))
1385 .Times(expected_reset ? 1 : 0);
1386 attempter_.policy_provider_ = std::move(mock_policy_provider);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001387 attempter_.Update("", "", "", "", false, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001388 ScheduleQuitMainLoop();
1389}
1390
1391TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1392 loop_.PostTask(FROM_HERE,
1393 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1394 base::Unretained(this),
1395 /*is_consumer=*/false,
1396 /*is_policy_loaded=*/false,
1397 /*expected_reset=*/false));
1398 loop_.Run();
1399}
1400
1401TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1402 loop_.PostTask(FROM_HERE,
1403 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1404 base::Unretained(this),
1405 /*is_consumer=*/true,
1406 /*is_policy_loaded=*/false,
1407 /*expected_reset=*/true));
1408 loop_.Run();
1409}
1410
1411TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1412 loop_.PostTask(FROM_HERE,
1413 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1414 base::Unretained(this),
1415 /*is_consumer=*/false,
1416 /*is_policy_loaded=*/true,
1417 /*expected_reset=*/true));
1418 loop_.Run();
1419}
1420
Marton Hunyady199152d2018-05-07 19:08:48 +02001421TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001422 attempter_.install_plan_.reset(new InstallPlan);
1423 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001424
1425 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1426 SetRollbackHappened(true))
1427 .Times(1);
1428 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1429}
1430
1431TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001432 attempter_.install_plan_.reset(new InstallPlan);
1433 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001434
1435 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1436 SetRollbackHappened(true))
1437 .Times(0);
1438 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1439}
1440
Marton Hunyadya0302682018-05-16 18:52:13 +02001441TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001442 attempter_.install_plan_.reset(new InstallPlan);
1443 attempter_.install_plan_->is_rollback = true;
1444 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001445
1446 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1447 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1448 .Times(1);
1449 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1450}
1451
1452TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001453 attempter_.install_plan_.reset(new InstallPlan);
1454 attempter_.install_plan_->is_rollback = false;
1455 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001456
1457 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1458 ReportEnterpriseRollbackMetrics(_, _))
1459 .Times(0);
1460 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1461}
1462
1463TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001464 attempter_.install_plan_.reset(new InstallPlan);
1465 attempter_.install_plan_->is_rollback = true;
1466 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001467
1468 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1469 ReportEnterpriseRollbackMetrics(false, kRollbackVersion))
1470 .Times(1);
1471 MockAction action;
1472 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1473 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1474}
1475
1476TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001477 attempter_.install_plan_.reset(new InstallPlan);
1478 attempter_.install_plan_->is_rollback = false;
1479 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001480
1481 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1482 ReportEnterpriseRollbackMetrics(_, _))
1483 .Times(0);
1484 MockAction action;
1485 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1486 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1487}
1488
May Lippert60aa3ca2018-08-15 16:55:29 -07001489TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1490 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1491 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1492 .Times(0);
1493 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1494}
1495
1496TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1497 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1498 fake_system_state_.set_device_policy(device_policy.get());
1499 // Make device policy return that this is not enterprise enrolled
1500 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1501
1502 // Ensure that the metric is not recorded.
1503 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1504 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1505 .Times(0);
1506 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1507}
1508
1509TEST_F(UpdateAttempterTest,
1510 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1511 constexpr int kDaysToUpdate = 15;
1512 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1513 fake_system_state_.set_device_policy(device_policy.get());
1514 // Make device policy return that this is enterprise enrolled
1515 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1516 // Pretend that there's a time restriction policy in place
1517 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1518 .WillOnce(Return(true));
1519
1520 FakePrefs fake_prefs;
1521 Time update_first_seen_at = Time::Now();
1522 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1523 update_first_seen_at.ToInternalValue());
1524
1525 FakeClock fake_clock;
1526 Time update_finished_at =
1527 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1528 fake_clock.SetWallclockTime(update_finished_at);
1529
1530 fake_system_state_.set_clock(&fake_clock);
1531 fake_system_state_.set_prefs(&fake_prefs);
1532
1533 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1534 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1535 .Times(1);
1536 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1537}
1538
1539TEST_F(UpdateAttempterTest,
1540 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
1541 constexpr int kDaysToUpdate = 15;
1542 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1543 fake_system_state_.set_device_policy(device_policy.get());
1544 // Make device policy return that this is enterprise enrolled
1545 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1546 // Pretend that there's no time restriction policy in place
1547 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1548 .WillOnce(Return(false));
1549
1550 FakePrefs fake_prefs;
1551 Time update_first_seen_at = Time::Now();
1552 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1553 update_first_seen_at.ToInternalValue());
1554
1555 FakeClock fake_clock;
1556 Time update_finished_at =
1557 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1558 fake_clock.SetWallclockTime(update_finished_at);
1559
1560 fake_system_state_.set_clock(&fake_clock);
1561 fake_system_state_.set_prefs(&fake_prefs);
1562
1563 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1564 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
1565 .Times(1);
1566 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1567}
1568
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001569} // namespace chromeos_update_engine