blob: 8fe2cb9cd408bbc8095a268ce14b80bb70e64bce [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>
Amin Hassani71818c82018-03-06 13:25:40 -080025#include <brillo/bind_lambda.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070026#include <brillo/message_loops/base_message_loop.h>
27#include <brillo/message_loops/message_loop.h>
28#include <brillo/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070029#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020030#include <policy/libpolicy.h>
31#include <policy/mock_device_policy.h>
Marton Hunyadye58bddb2018-04-10 20:27:26 +020032#include <policy/mock_libpolicy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080034#include "update_engine/common/dlcservice_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/fake_clock.h"
36#include "update_engine/common/fake_prefs.h"
Alex Deymo14fd1ec2016-02-24 22:03:57 -080037#include "update_engine/common/mock_action.h"
38#include "update_engine/common/mock_action_processor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080039#include "update_engine/common/mock_http_fetcher.h"
40#include "update_engine/common/mock_prefs.h"
41#include "update_engine/common/platform_constants.h"
42#include "update_engine/common/prefs.h"
43#include "update_engine/common/test_utils.h"
44#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070045#include "update_engine/fake_system_state.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070046#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080047#include "update_engine/mock_payload_state.h"
Aaron Wood9321f502017-09-07 11:18:54 -070048#include "update_engine/mock_service_observer.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080049#include "update_engine/payload_consumer/filesystem_verifier_action.h"
50#include "update_engine/payload_consumer/install_plan.h"
51#include "update_engine/payload_consumer/payload_constants.h"
52#include "update_engine/payload_consumer/postinstall_runner_action.h"
Amin Hassani0882a512018-04-05 16:25:44 -070053#include "update_engine/update_boot_flags_action.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070054
David Zeuthen985b1122013-10-09 12:13:15 -070055using base::Time;
56using base::TimeDelta;
Aaron Woodbf5a2522017-10-04 10:58:36 -070057using chromeos_update_manager::EvalStatus;
Adolfo Victoria497044c2018-07-18 07:51:42 -070058using chromeos_update_manager::StagingSchedule;
Aaron Woodbf5a2522017-10-04 10:58:36 -070059using chromeos_update_manager::UpdateCheckParams;
Adolfo Victoria497044c2018-07-18 07:51:42 -070060using policy::DevicePolicy;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070062using std::unique_ptr;
Xiaochu Liu88d90382018-08-29 16:09:11 -070063using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070064using testing::_;
Darin Petkov36275772010-10-01 11:40:57 -070065using testing::DoAll;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070066using testing::Field;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070067using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070068using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080069using testing::NiceMock;
Amin Hassanid3f4bea2018-04-30 14:52:40 -070070using testing::Pointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070071using testing::Property;
72using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070073using testing::ReturnPointee;
Marton Hunyadye58bddb2018-04-10 20:27:26 +020074using testing::ReturnRef;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080075using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070076using testing::SetArgPointee;
Aaron Woodbf5a2522017-10-04 10:58:36 -070077using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070078using update_engine::UpdateEngineStatus;
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -070079using update_engine::UpdateStatus;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070080
81namespace chromeos_update_engine {
82
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080083namespace {
84
85class MockDlcService : public DlcServiceInterface {
86 public:
87 MOCK_METHOD1(GetInstalled, bool(vector<string>*));
88};
89
90} // namespace
91
Marton Hunyadya0302682018-05-16 18:52:13 +020092const char kRollbackVersion[] = "10575.39.2";
93
Darin Petkovf42cc1c2010-09-01 09:03:02 -070094// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070095// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070096// methods.
97class UpdateAttempterUnderTest : public UpdateAttempter {
98 public:
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070099 explicit UpdateAttempterUnderTest(SystemState* system_state)
100 : UpdateAttempter(system_state, nullptr) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700101
102 // Wrap the update scheduling method, allowing us to opt out of scheduled
103 // updates for testing purposes.
Xiaochu Liu88d90382018-08-29 16:09:11 -0700104 bool ScheduleUpdates() override {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700105 schedule_updates_called_ = true;
106 if (do_schedule_updates_) {
107 UpdateAttempter::ScheduleUpdates();
108 } else {
109 LOG(INFO) << "[TEST] Update scheduling disabled.";
110 }
Xiaochu Liu88d90382018-08-29 16:09:11 -0700111 return true;
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700112 }
113 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
114 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
115
116 // Indicates whether ScheduleUpdates() was called.
117 bool schedule_updates_called() const { return schedule_updates_called_; }
118
David Pursell02c18642014-11-06 11:26:11 -0800119 // Need to expose forced_omaha_url_ so we can test it.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700120 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -0800121
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700122 private:
123 bool schedule_updates_called_ = false;
124 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700125};
126
127class UpdateAttempterTest : public ::testing::Test {
128 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700129 UpdateAttempterTest()
Daniel Erate5f6f252017-04-20 12:09:58 -0600130 : certificate_checker_(fake_system_state_.mock_prefs(),
Alex Deymo33e91e72015-12-01 18:26:08 -0300131 &openssl_wrapper_) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700132 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700133 fake_system_state_.set_connection_manager(&mock_connection_manager);
134 fake_system_state_.set_update_attempter(&attempter_);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800135 fake_system_state_.set_dlcservice(&mock_dlcservice_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700136 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700137
Alex Deymo33e91e72015-12-01 18:26:08 -0300138 certificate_checker_.Init();
139
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800140 attempter_.set_forced_update_pending_callback(
141 new base::Callback<void(bool, bool)>(base::Bind([](bool, bool) {})));
Sen Jiange67bb5b2016-06-20 15:53:56 -0700142 // Finish initializing the attempter.
Gilad Arnold1f847232014-04-07 12:07:49 -0700143 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -0700144 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700145
Alex Deymo610277e2014-11-11 21:18:11 -0800146 void SetUp() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700147 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700148 EXPECT_EQ(0, attempter_.http_response_code_);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700149 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700150 EXPECT_EQ(0.0, attempter_.download_progress_);
151 EXPECT_EQ(0, attempter_.last_checked_time_);
152 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700153 EXPECT_EQ(0ULL, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800154 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700155 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700156 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700157
158 // Set up store/load semantics of P2P properties via the mock PayloadState.
159 actual_using_p2p_for_downloading_ = false;
160 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
161 SetUsingP2PForDownloading(_))
162 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
163 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
164 GetUsingP2PForDownloading())
165 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
166 actual_using_p2p_for_sharing_ = false;
167 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
168 SetUsingP2PForSharing(_))
169 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
170 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
171 GetUsingP2PForDownloading())
172 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700173 }
174
Alex Deymo60ca1a72015-06-18 18:19:15 -0700175 public:
176 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200177
Alex Deymo60ca1a72015-06-18 18:19:15 -0700178 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800179 void UpdateTestStart();
180 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700181 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700182 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700183 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700184 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700185 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700186 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700187 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700188 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700189 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700190 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700191 void P2PEnabledHousekeepingFailsStart();
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200192 void ResetRollbackHappenedStart(bool is_consumer,
193 bool is_policy_available,
194 bool expected_reset);
Adolfo Victoria497044c2018-07-18 07:51:42 -0700195 // Staging related callbacks.
196 void SetUpStagingTest(const StagingSchedule& schedule, FakePrefs* prefs);
197 void CheckStagingOff();
198 void StagingSetsPrefsAndTurnsOffScatteringStart();
199 void StagingOffIfInteractiveStart();
200 void StagingOffIfOobeStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700201
Gilad Arnold74b5f552014-10-07 08:17:16 -0700202 bool actual_using_p2p_for_downloading() {
203 return actual_using_p2p_for_downloading_;
204 }
205 bool actual_using_p2p_for_sharing() {
206 return actual_using_p2p_for_sharing_;
207 }
208
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700209 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700210 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700211
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700212 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700213 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300214 OpenSSLWrapper openssl_wrapper_;
215 CertificateChecker certificate_checker_;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800216 MockDlcService mock_dlcservice_;
Alex Deymo30534502015-07-20 15:06:33 -0700217
Alex Deymo8427b4a2014-11-05 14:00:32 -0800218 NiceMock<MockActionProcessor>* processor_;
219 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800220 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700221
Gilad Arnold74b5f552014-10-07 08:17:16 -0700222 bool actual_using_p2p_for_downloading_;
223 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700224};
225
Alex Deymo60ca1a72015-06-18 18:19:15 -0700226void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700227 loop_.PostTask(
228 FROM_HERE,
229 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
230 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700231}
232
Darin Petkov1b003102010-11-30 10:18:36 -0800233TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700234 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800235 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700236 DownloadAction action(prefs_,
237 nullptr,
238 nullptr,
239 nullptr,
240 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700241 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800242 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700243 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700244 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700245 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700246 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800247}
248
249TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800250 MockAction action;
251 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700252 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800253 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800254 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700255 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
256 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800257}
258
Aaron Wood9321f502017-09-07 11:18:54 -0700259TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
260 // Simple test case, where all the values match (nothing was skipped)
261 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
262 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
263 uint64_t bytes_received_1 = bytes_progressed_1;
264 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
265 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
266
267 double progress_1 =
268 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
269 double progress_2 =
270 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
271
272 EXPECT_EQ(0.0, attempter_.download_progress_);
273 // This is set via inspecting the InstallPlan payloads when the
274 // OmahaResponseAction is completed
275 attempter_.new_payload_size_ = bytes_total;
276 NiceMock<MockServiceObserver> observer;
277 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700278 SendStatusUpdate(AllOf(
279 Field(&UpdateEngineStatus::progress, progress_1),
280 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
281 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700282 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700283 SendStatusUpdate(AllOf(
284 Field(&UpdateEngineStatus::progress, progress_2),
285 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
286 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700287 attempter_.AddObserver(&observer);
288 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
289 EXPECT_EQ(progress_1, attempter_.download_progress_);
290 // This iteration validates that a later set of updates to the variables are
291 // properly handled (so that |getStatus()| will return the same progress info
292 // as the callback is receiving.
293 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
294 EXPECT_EQ(progress_2, attempter_.download_progress_);
295}
296
297TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
298 // The transition into UpdateStatus::DOWNLOADING happens when the
299 // first bytes are received.
300 uint64_t bytes_progressed = 1024 * 1024; // 1MB
301 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
302 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
303 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
304 // This is set via inspecting the InstallPlan payloads when the
305 // OmahaResponseAction is completed
306 attempter_.new_payload_size_ = bytes_total;
307 EXPECT_EQ(0.0, attempter_.download_progress_);
308 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700309 EXPECT_CALL(observer,
310 SendStatusUpdate(AllOf(
311 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
312 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700313 attempter_.AddObserver(&observer);
314 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
315 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
316}
317
318TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
319 // There is a special case to ensure that at 100% downloaded,
320 // download_progress_ is updated and that value broadcast. This test confirms
321 // that.
322 uint64_t bytes_progressed = 0; // ignored
323 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
324 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
325 attempter_.status_ = UpdateStatus::DOWNLOADING;
326 attempter_.new_payload_size_ = bytes_total;
327 EXPECT_EQ(0.0, attempter_.download_progress_);
328 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700329 EXPECT_CALL(observer,
330 SendStatusUpdate(AllOf(
331 Field(&UpdateEngineStatus::progress, 1.0),
332 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
333 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700334 attempter_.AddObserver(&observer);
335 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
336 EXPECT_EQ(1.0, attempter_.download_progress_);
337}
338
Darin Petkov1b003102010-11-30 10:18:36 -0800339TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700340 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800341 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700342 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300343 std::move(fetcher), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800344 ObjectCollectorAction<OmahaResponse> collector_action;
345 BondActions(&action, &collector_action);
346 OmahaResponse response;
347 response.poll_interval = 234;
348 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800349 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700350 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800351 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700352 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800353 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700354 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800355}
356
Alex Deymo30534502015-07-20 15:06:33 -0700357TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700358 FakePrefs fake_prefs;
359 string boot_id;
360 EXPECT_TRUE(utils::GetBootId(&boot_id));
361 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
362 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700363 attempter_.Init();
364 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700365}
366
367TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700368 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
369 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700370 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700371 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700372
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700373 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700374 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
375 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700376 EXPECT_EQ(ErrorCode::kOmahaRequestError,
377 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700378 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700379 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700380 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700381 ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700382 FilesystemVerifierAction filesystem_verifier_action;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700383 EXPECT_EQ(ErrorCode::kFilesystemVerifierError,
384 GetErrorCodeForAction(&filesystem_verifier_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700385 ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300386 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700387 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700388 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700389 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700390 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800391 MockAction action_mock;
392 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700393 EXPECT_EQ(ErrorCode::kError,
394 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700395}
396
Darin Petkov36275772010-10-01 11:40:57 -0700397TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700398 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800399 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700400 .WillOnce(Return(false));
401 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700402 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800403 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700404 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700405 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
Darin Petkov36275772010-10-01 11:40:57 -0700406 Return(true)));
407 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700408 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800409 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700410 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700411 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
Darin Petkov36275772010-10-01 11:40:57 -0700412 Return(true)));
413 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700414 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800415 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700416 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700417 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700418}
419
420TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800421 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700422 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700423 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
424 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Darin Petkov36275772010-10-01 11:40:57 -0700425 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700426 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
Darin Petkov36275772010-10-01 11:40:57 -0700427 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800428 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700429 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800430 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700431 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800432 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700433 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700434 for (int i = 0; i < 4; i ++)
435 attempter_.MarkDeltaUpdateFailure();
436}
437
Darin Petkov1b003102010-11-30 10:18:36 -0800438TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
439 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
440 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700441 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800442 .Times(0);
443 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700444 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800445 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700446 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700447 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700448 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800449 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700450 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800451}
452
453TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
454 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700455 EnqueueAction(Pointee(Property(
456 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700457 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700458 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700459 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800460 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
461 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800462 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800463 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700464 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800465}
466
Darin Petkove6ef2f82011-03-07 17:31:11 -0800467namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700468// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700469const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Amin Hassani0882a512018-04-05 16:25:44 -0700470 OmahaRequestAction::StaticType(),
471 OmahaResponseHandlerAction::StaticType(),
472 UpdateBootFlagsAction::StaticType(),
473 OmahaRequestAction::StaticType(),
474 DownloadAction::StaticType(),
475 OmahaRequestAction::StaticType(),
476 FilesystemVerifierAction::StaticType(),
477 PostinstallRunnerAction::StaticType(),
478 OmahaRequestAction::StaticType()};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700479
480// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700481const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700482 InstallPlanAction::StaticType(),
483 PostinstallRunnerAction::StaticType(),
484};
485
Adolfo Victoria497044c2018-07-18 07:51:42 -0700486const StagingSchedule kValidStagingSchedule = {
487 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
488
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700489} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800490
491void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700492 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700493
494 // Expect that the device policy is loaded by the UpdateAttempter at some
495 // point by calling RefreshDevicePolicy.
Igor9fd76b62017-12-11 15:24:18 +0100496 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700497 EXPECT_CALL(*device_policy, LoadPolicy())
498 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100499 attempter_.policy_provider_.reset(
500 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700501
502 {
503 InSequence s;
504 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
505 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700506 EnqueueAction(Pointee(
507 Property(&AbstractAction::Type, kUpdateActionTypes[i]))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700508 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700509 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700510 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700511
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200512 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700513 loop_.PostTask(FROM_HERE,
514 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
515 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800516}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700517
Darin Petkove6ef2f82011-03-07 17:31:11 -0800518void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700519 EXPECT_EQ(0, attempter_.http_response_code());
520 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700521 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700522 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800523}
524
Chris Sosa28e479c2013-07-12 11:39:53 -0700525void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700526 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700527 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100528 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700529 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100530 fake_system_state_.set_device_policy(device_policy.get());
531 if (enterprise_rollback) {
532 // We return an empty owner as this is an enterprise.
533 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
534 DoAll(SetArgPointee<0>(string("")),
535 Return(true)));
536 } else {
537 // We return a fake owner as this is an owned consumer device.
538 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
539 DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
540 Return(true)));
541 }
542
543 attempter_.policy_provider_.reset(
544 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700545
Alex Deymo763e7db2015-08-27 21:08:08 -0700546 if (valid_slot) {
547 BootControlInterface::Slot rollback_slot = 1;
548 LOG(INFO) << "Test Mark Bootable: "
549 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700550 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
551 true);
Don Garrett6646b442013-11-13 15:29:11 -0800552 }
553
Chris Sosa28e479c2013-07-12 11:39:53 -0700554 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700555
Chris Sosad38b1132014-03-25 10:43:59 -0700556 // We only allow rollback on devices that are not enterprise enrolled and
557 // which have a valid slot to rollback to.
558 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700559 is_rollback_allowed = true;
560 }
561
Chris Sosa28e479c2013-07-12 11:39:53 -0700562 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700563 InSequence s;
564 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
565 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700566 EnqueueAction(Pointee(Property(&AbstractAction::Type,
567 kRollbackActionTypes[i]))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700568 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700569 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700570
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700571 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700572 loop_.PostTask(FROM_HERE,
573 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
574 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700575 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700576 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700577 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700578 }
579}
580
581void UpdateAttempterTest::RollbackTestVerify() {
582 // Verifies the actions that were enqueued.
583 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700584 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700585 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
586 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700587 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700588}
589
Darin Petkove6ef2f82011-03-07 17:31:11 -0800590TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700591 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700592 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700593}
594
Chris Sosa76a29ae2013-07-11 17:59:24 -0700595TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700596 loop_.PostTask(FROM_HERE,
597 base::Bind(&UpdateAttempterTest::RollbackTestStart,
598 base::Unretained(this),
599 false, true));
600 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700601}
602
Don Garrett6646b442013-11-13 15:29:11 -0800603TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700604 loop_.PostTask(FROM_HERE,
605 base::Bind(&UpdateAttempterTest::RollbackTestStart,
606 base::Unretained(this),
607 false, false));
608 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800609}
610
Chris Sosa76a29ae2013-07-11 17:59:24 -0700611TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700612 loop_.PostTask(FROM_HERE,
613 base::Bind(&UpdateAttempterTest::RollbackTestStart,
614 base::Unretained(this),
615 true, true));
616 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700617}
618
Thieu Le116fda32011-04-19 11:01:54 -0700619void UpdateAttempterTest::PingOmahaTestStart() {
620 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700621 EnqueueAction(Pointee(Property(
622 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700623 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700624 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700625 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700626}
627
628TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700629 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
630 EXPECT_FALSE(attempter_.schedule_updates_called());
631 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
632 // testing, which is more permissive than we want to handle here.
633 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700634 loop_.PostTask(FROM_HERE,
635 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
636 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700637 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700638 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700639 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700640}
641
Darin Petkov18c7bce2011-06-16 14:07:00 -0700642TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800643 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700644 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700645 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700646 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700647 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
648 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700649 EXPECT_EQ(
650 static_cast<ErrorCode>(static_cast<int>(kCode) |
651 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
652 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700653}
654
655TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700656 attempter_.install_plan_.reset(new InstallPlan);
657 attempter_.install_plan_->is_resume = true;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800658 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700659 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700660 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700661 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700662 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
663 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700664 EXPECT_EQ(
665 static_cast<ErrorCode>(
666 static_cast<int>(kCode) |
667 static_cast<int>(ErrorCode::kResumedFlag) |
668 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
669 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700670}
671
David Zeuthen8f191b22013-08-06 12:27:50 -0700672TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
673 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700674 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700675 mock_p2p_manager.fake().SetP2PEnabled(false);
676 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
677 attempter_.UpdateEngineStarted();
678}
679
680TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
681 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700682 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700683 mock_p2p_manager.fake().SetP2PEnabled(true);
684 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
685 attempter_.UpdateEngineStarted();
686}
687
688TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
689 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700690 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700691 mock_p2p_manager.fake().SetP2PEnabled(true);
692 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700693 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700694 attempter_.UpdateEngineStarted();
695}
696
697TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700698 loop_.PostTask(FROM_HERE,
699 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
700 base::Unretained(this)));
701 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700702}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700703
David Zeuthen8f191b22013-08-06 12:27:50 -0700704void UpdateAttempterTest::P2PNotEnabledStart() {
705 // If P2P is not enabled, check that we do not attempt housekeeping
706 // and do not convey that p2p is to be used.
707 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700708 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700709 mock_p2p_manager.fake().SetP2PEnabled(false);
710 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200711 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700712 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700713 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700714 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700715}
716
717TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700718 loop_.PostTask(FROM_HERE,
719 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
720 base::Unretained(this)));
721 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700722}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700723
David Zeuthen8f191b22013-08-06 12:27:50 -0700724void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
725 // If p2p is enabled, but starting it fails ensure we don't do
726 // any housekeeping and do not convey that p2p should be used.
727 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700728 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700729 mock_p2p_manager.fake().SetP2PEnabled(true);
730 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
731 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
732 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200733 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700734 EXPECT_FALSE(actual_using_p2p_for_downloading());
735 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700736 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700737}
738
739TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700740 loop_.PostTask(
741 FROM_HERE,
742 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
743 base::Unretained(this)));
744 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700745}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700746
David Zeuthen8f191b22013-08-06 12:27:50 -0700747void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
748 // If p2p is enabled, starting it works but housekeeping fails, ensure
749 // we do not convey p2p is to be used.
750 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700751 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700752 mock_p2p_manager.fake().SetP2PEnabled(true);
753 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
754 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700755 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200756 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700757 EXPECT_FALSE(actual_using_p2p_for_downloading());
758 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700759 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700760}
761
762TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700763 loop_.PostTask(FROM_HERE,
764 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
765 base::Unretained(this)));
766 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700767}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700768
David Zeuthen8f191b22013-08-06 12:27:50 -0700769void UpdateAttempterTest::P2PEnabledStart() {
770 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700771 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700772 // If P2P is enabled and starting it works, check that we performed
773 // housekeeping and that we convey p2p should be used.
774 mock_p2p_manager.fake().SetP2PEnabled(true);
775 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
776 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700777 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200778 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700779 EXPECT_TRUE(actual_using_p2p_for_downloading());
780 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700781 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700782}
783
784TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700785 loop_.PostTask(FROM_HERE,
786 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
787 base::Unretained(this)));
788 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700789}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700790
David Zeuthen8f191b22013-08-06 12:27:50 -0700791void UpdateAttempterTest::P2PEnabledInteractiveStart() {
792 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700793 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700794 // For an interactive check, if P2P is enabled and starting it
795 // works, check that we performed housekeeping and that we convey
796 // p2p should be used for sharing but NOT for downloading.
797 mock_p2p_manager.fake().SetP2PEnabled(true);
798 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
799 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700800 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200801 attempter_.Update("",
802 "",
803 "",
804 "",
805 false,
806 false,
807 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700808 EXPECT_FALSE(actual_using_p2p_for_downloading());
809 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700810 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700811}
812
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700813TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700814 loop_.PostTask(
815 FROM_HERE,
816 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
817 base::Unretained(this)));
818 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700819}
820
821// Tests that the scatter_factor_in_seconds value is properly fetched
822// from the device policy.
823void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700824 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700825
Igor9fd76b62017-12-11 15:24:18 +0100826 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700827 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100828 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700829
830 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
831 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700832 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700833 Return(true)));
834
Igor9fd76b62017-12-11 15:24:18 +0100835 attempter_.policy_provider_.reset(
836 new policy::PolicyProvider(std::move(device_policy)));
837
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200838 attempter_.Update("", "", "", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700839 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
840
Alex Deymo60ca1a72015-06-18 18:19:15 -0700841 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700842}
843
844TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700845 loop_.PostTask(
846 FROM_HERE,
847 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
848 base::Unretained(this)));
849 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700850}
851
852void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
853 // Tests that the scatter_factor_in_seconds value is properly fetched
854 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700855 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800856 FakePrefs fake_prefs;
857 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700858
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700859 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700860
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800861 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700862
Ben Chan9abb7632014-08-07 00:10:53 -0700863 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700864
Igor9fd76b62017-12-11 15:24:18 +0100865 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700866 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100867 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700868
869 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
870 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700871 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700872 Return(true)));
873
Igor9fd76b62017-12-11 15:24:18 +0100874 attempter_.policy_provider_.reset(
875 new policy::PolicyProvider(std::move(device_policy)));
876
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200877 attempter_.Update("", "", "", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700878 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
879
880 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800881 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700882
Ben Chan9abb7632014-08-07 00:10:53 -0700883 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800884 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700885 EXPECT_EQ(initial_value - 1, new_value);
886
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700887 EXPECT_TRUE(
888 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700889
890 // However, if the count is already 0, it's not decremented. Test that.
891 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800892 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200893 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800894 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
895 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700896 EXPECT_EQ(initial_value, new_value);
897
Alex Deymo60ca1a72015-06-18 18:19:15 -0700898 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700899}
900
Jay Srinivasan08fce042012-06-07 16:31:01 -0700901TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700902 loop_.PostTask(FROM_HERE, base::Bind(
903 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
904 base::Unretained(this)));
905 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700906}
907
908void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
909 // Tests that no scattering logic is enabled if the update check
910 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700911 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800912 FakePrefs fake_prefs;
913 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700914
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700915 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800916 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700917
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700918 EXPECT_TRUE(
919 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800920 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700921
922 // make sure scatter_factor is non-zero as scattering is disabled
923 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700924 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700925
Igor9fd76b62017-12-11 15:24:18 +0100926 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700927 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100928 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700929
930 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
931 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700932 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700933 Return(true)));
934
Igor9fd76b62017-12-11 15:24:18 +0100935 attempter_.policy_provider_.reset(
936 new policy::PolicyProvider(std::move(device_policy)));
937
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800938 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200939 attempter_.Update("",
940 "",
941 "",
942 "",
943 false,
944 false,
945 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700946 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
947
948 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700949 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700950 EXPECT_FALSE(
951 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700952 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700953 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
954 EXPECT_FALSE(
955 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800956 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700957
Alex Deymo60ca1a72015-06-18 18:19:15 -0700958 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700959}
960
Adolfo Victoria497044c2018-07-18 07:51:42 -0700961void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
962 FakePrefs* prefs) {
963 attempter_.prefs_ = prefs;
964 fake_system_state_.set_prefs(prefs);
965
966 int64_t initial_value = 8;
967 EXPECT_TRUE(
968 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
969 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
970 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
971
972 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
973 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
974 fake_system_state_.set_device_policy(device_policy.get());
975 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
976 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
977
978 attempter_.policy_provider_.reset(
979 new policy::PolicyProvider(std::move(device_policy)));
980}
981
982TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
983 loop_.PostTask(
984 FROM_HERE,
985 base::Bind(
986 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
987 base::Unretained(this)));
988 loop_.Run();
989}
990
991void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
992 // Tests that staging sets its prefs properly and turns off scattering.
993 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
994 FakePrefs fake_prefs;
995 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
996
997 attempter_.Update("", "", "", "", false, false, false);
998 // Check that prefs have the correct values.
999 int64_t update_count;
1000 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
1001 int64_t waiting_time_days;
1002 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
1003 &waiting_time_days));
1004 EXPECT_GT(waiting_time_days, 0);
1005 // Update count should have been decremented.
1006 EXPECT_EQ(7, update_count);
1007 // Check that Omaha parameters were updated correctly.
1008 EXPECT_TRUE(
1009 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1010 EXPECT_TRUE(
1011 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1012 EXPECT_EQ(waiting_time_days,
1013 attempter_.omaha_request_params_->waiting_period().InDays());
1014 // Check class variables.
1015 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1016 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1017 // Check that scattering is turned off
1018 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1019 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1020
1021 ScheduleQuitMainLoop();
1022}
1023
1024void UpdateAttempterTest::CheckStagingOff() {
1025 // Check that all prefs were removed.
1026 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1027 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1028 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1029 // Check that the Omaha parameters have the correct value.
1030 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1031 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1032 attempter_.staging_wait_time_);
1033 EXPECT_FALSE(
1034 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1035 EXPECT_FALSE(
1036 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1037 // Check that scattering is turned off too.
1038 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1039}
1040
1041TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1042 loop_.PostTask(FROM_HERE,
1043 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1044 base::Unretained(this)));
1045 loop_.Run();
1046}
1047
1048void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1049 // Tests that staging is turned off when an interactive update is requested.
1050 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1051 FakePrefs fake_prefs;
1052 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1053
1054 attempter_.Update("", "", "", "", false, false, /* interactive = */ true);
1055 CheckStagingOff();
1056
1057 ScheduleQuitMainLoop();
1058}
1059
1060TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1061 loop_.PostTask(FROM_HERE,
1062 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1063 base::Unretained(this)));
1064 loop_.Run();
1065}
1066
1067void UpdateAttempterTest::StagingOffIfOobeStart() {
1068 // Tests that staging is turned off if OOBE hasn't been completed.
1069 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1070 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1071 FakePrefs fake_prefs;
1072 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1073
1074 attempter_.Update("", "", "", "", false, false, /* interactive = */ true);
1075 CheckStagingOff();
1076
1077 ScheduleQuitMainLoop();
1078}
1079
David Zeuthen985b1122013-10-09 12:13:15 -07001080// Checks that we only report daily metrics at most every 24 hours.
1081TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1082 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001083 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001084
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001085 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001086 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001087
1088 Time epoch = Time::FromInternalValue(0);
1089 fake_clock.SetWallclockTime(epoch);
1090
1091 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1092 // we should report.
1093 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1094 // We should not report again if no time has passed.
1095 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1096
1097 // We should not report if only 10 hours has passed.
1098 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1099 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1100
1101 // We should not report if only 24 hours - 1 sec has passed.
1102 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1103 TimeDelta::FromSeconds(1));
1104 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1105
1106 // We should report if 24 hours has passed.
1107 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1108 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1109
1110 // But then we should not report again..
1111 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1112
1113 // .. until another 24 hours has passed
1114 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1115 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1116 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1117 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1118 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1119
1120 // .. and another 24 hours
1121 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1122 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1123 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1124 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1125 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1126
1127 // If the span between time of reporting and present time is
1128 // negative, we report. This is in order to reset the timestamp and
1129 // avoid an edge condition whereby a distant point in the future is
1130 // in the state variable resulting in us never ever reporting again.
1131 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1132 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1133 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1134
1135 // In this case we should not update until the clock reads 71 + 24 = 95.
1136 // Check that.
1137 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1138 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1139 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1140 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1141 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001142}
1143
David Zeuthen3c55abd2013-10-14 12:48:03 -07001144TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001145 FakeClock fake_clock;
1146 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001147 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001148 FakePrefs fake_prefs;
1149 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001150 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001151
1152 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001153 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001154
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001155 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001156
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001157 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001158 EXPECT_EQ(boot_time.ToTimeT(), 42);
1159}
1160
David Pursell02c18642014-11-06 11:26:11 -08001161TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1162 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1163 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1164}
1165
1166TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1167 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001168 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001169 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1170}
1171
1172TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1173 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001174 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001175 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1176}
1177
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001178TEST_F(UpdateAttempterTest, CheckForUpdateAUDlcTest) {
1179 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1180 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1181
1182 const string dlc_module_id = "a_dlc_module_id";
1183 vector<string> dlc_module_ids = {dlc_module_id};
1184 ON_CALL(mock_dlcservice_, GetInstalled(testing::_))
1185 .WillByDefault(DoAll(testing::SetArgPointee<0>(dlc_module_ids),
1186 testing::Return(true)));
1187
1188 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
1189 EXPECT_EQ(attempter_.dlc_ids_.size(), 1);
1190 EXPECT_EQ(attempter_.dlc_ids_[0], dlc_module_id);
1191}
1192
David Pursell02c18642014-11-06 11:26:11 -08001193TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1194 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001195 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001196 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001197 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001198}
1199
1200TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
1201 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001202 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001203 attempter_.CheckForUpdate("", "autest-scheduled", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001204 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001205}
1206
Xiaochu Liu88d90382018-08-29 16:09:11 -07001207TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1208 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1209 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1210 attempter_.CheckForInstall({}, "autest");
1211 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1212
1213 attempter_.CheckForInstall({}, "autest-scheduled");
1214 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1215
1216 attempter_.CheckForInstall({}, "http://omaha.phishing");
1217 EXPECT_EQ("", attempter_.forced_omaha_url());
1218}
1219
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001220TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001221 attempter_.CalculateUpdateParams("", "", "", "1234", false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001222 EXPECT_EQ("1234",
1223 fake_system_state_.request_params()->target_version_prefix());
1224
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001225 attempter_.CalculateUpdateParams("", "", "", "", false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001226 EXPECT_TRUE(
1227 fake_system_state_.request_params()->target_version_prefix().empty());
1228}
1229
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001230TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1231 attempter_.CalculateUpdateParams("",
1232 "",
1233 "",
1234 "1234",
1235 /*rollback_allowed=*/true,
1236 false,
1237 false);
1238 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1239
1240 attempter_.CalculateUpdateParams("",
1241 "",
1242 "",
1243 "1234",
1244 /*rollback_allowed=*/false,
1245 false,
1246 false);
1247 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1248}
1249
Aaron Wood23bd3392017-10-06 14:48:25 -07001250TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1251 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1252 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001253 OmahaResponseHandlerAction response_action(&fake_system_state_);
1254 response_action.install_plan_.version = "a.b.c.d";
1255 response_action.install_plan_.system_version = "b.c.d.e";
1256 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001257 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001258 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1259 // completed, with the deferred-update error code.
1260 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001261 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001262 {
1263 UpdateEngineStatus status;
1264 attempter_.GetStatus(&status);
1265 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001266 EXPECT_TRUE(attempter_.install_plan_);
1267 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
1268 EXPECT_EQ(attempter_.install_plan_->system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001269 status.new_system_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001270 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001271 status.new_size_bytes);
1272 }
1273 // An "error" event should have been created to tell Omaha that the update is
1274 // being deferred.
1275 EXPECT_TRUE(nullptr != attempter_.error_event_);
1276 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1277 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1278 ErrorCode expected_code = static_cast<ErrorCode>(
1279 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1280 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1281 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1282 // End the processing
1283 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1284 // Validate the state of the attempter.
1285 {
1286 UpdateEngineStatus status;
1287 attempter_.GetStatus(&status);
1288 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001289 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
1290 EXPECT_EQ(response_action.install_plan_.system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001291 status.new_system_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001292 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001293 status.new_size_bytes);
1294 }
1295}
1296
1297TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
1298 EXPECT_FALSE(attempter_.IsUpdateRunningOrScheduled());
1299 // Verify in-progress update with UPDATE_AVAILABLE is running
1300 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
1301 EXPECT_TRUE(attempter_.IsUpdateRunningOrScheduled());
1302}
1303
Aaron Woodbf5a2522017-10-04 10:58:36 -07001304TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1305 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1306
1307 UpdateCheckParams params = {.updates_enabled = true};
1308 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1309
1310 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1311 attempter_.GetCurrentUpdateAttemptFlags());
1312}
1313
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001314TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1315 UpdateCheckParams params = {.updates_enabled = true,
1316 .rollback_allowed = false};
1317 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1318 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1319}
1320
1321TEST_F(UpdateAttempterTest, RollbackAllowed) {
1322 UpdateCheckParams params = {.updates_enabled = true,
1323 .rollback_allowed = true};
1324 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1325 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1326}
1327
Aaron Wood081c0232017-10-19 17:14:58 -07001328TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1329 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1330
1331 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1332 EXPECT_EQ(UpdateAttemptFlags::kNone,
1333 attempter_.GetCurrentUpdateAttemptFlags());
1334}
1335
1336TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1337 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1338
1339 // This tests that when CheckForUpdate() is called with the non-interactive
1340 // flag set, that it doesn't change the current UpdateAttemptFlags.
1341 attempter_.CheckForUpdate("",
1342 "",
1343 UpdateAttemptFlags::kFlagNonInteractive |
1344 UpdateAttemptFlags::kFlagRestrictDownload);
1345 EXPECT_EQ(UpdateAttemptFlags::kNone,
1346 attempter_.GetCurrentUpdateAttemptFlags());
1347}
1348
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001349void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1350 bool is_policy_loaded,
1351 bool expected_reset) {
1352 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1353 .WillRepeatedly(Return(true));
1354 auto mock_policy_provider =
1355 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1356 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1357 .WillRepeatedly(Return(is_consumer));
1358 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1359 .WillRepeatedly(Return(is_policy_loaded));
1360 const policy::MockDevicePolicy device_policy;
1361 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1362 .WillRepeatedly(ReturnRef(device_policy));
1363 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1364 SetRollbackHappened(false))
1365 .Times(expected_reset ? 1 : 0);
1366 attempter_.policy_provider_ = std::move(mock_policy_provider);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001367 attempter_.Update("", "", "", "", false, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001368 ScheduleQuitMainLoop();
1369}
1370
1371TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1372 loop_.PostTask(FROM_HERE,
1373 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1374 base::Unretained(this),
1375 /*is_consumer=*/false,
1376 /*is_policy_loaded=*/false,
1377 /*expected_reset=*/false));
1378 loop_.Run();
1379}
1380
1381TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1382 loop_.PostTask(FROM_HERE,
1383 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1384 base::Unretained(this),
1385 /*is_consumer=*/true,
1386 /*is_policy_loaded=*/false,
1387 /*expected_reset=*/true));
1388 loop_.Run();
1389}
1390
1391TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1392 loop_.PostTask(FROM_HERE,
1393 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1394 base::Unretained(this),
1395 /*is_consumer=*/false,
1396 /*is_policy_loaded=*/true,
1397 /*expected_reset=*/true));
1398 loop_.Run();
1399}
1400
Marton Hunyady199152d2018-05-07 19:08:48 +02001401TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001402 attempter_.install_plan_.reset(new InstallPlan);
1403 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001404
1405 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1406 SetRollbackHappened(true))
1407 .Times(1);
1408 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1409}
1410
1411TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001412 attempter_.install_plan_.reset(new InstallPlan);
1413 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001414
1415 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1416 SetRollbackHappened(true))
1417 .Times(0);
1418 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1419}
1420
Marton Hunyadya0302682018-05-16 18:52:13 +02001421TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001422 attempter_.install_plan_.reset(new InstallPlan);
1423 attempter_.install_plan_->is_rollback = true;
1424 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001425
1426 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1427 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1428 .Times(1);
1429 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1430}
1431
1432TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001433 attempter_.install_plan_.reset(new InstallPlan);
1434 attempter_.install_plan_->is_rollback = false;
1435 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001436
1437 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1438 ReportEnterpriseRollbackMetrics(_, _))
1439 .Times(0);
1440 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1441}
1442
1443TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
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(false, kRollbackVersion))
1450 .Times(1);
1451 MockAction action;
1452 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1453 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1454}
1455
1456TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001457 attempter_.install_plan_.reset(new InstallPlan);
1458 attempter_.install_plan_->is_rollback = false;
1459 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001460
1461 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1462 ReportEnterpriseRollbackMetrics(_, _))
1463 .Times(0);
1464 MockAction action;
1465 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1466 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1467}
1468
May Lippert60aa3ca2018-08-15 16:55:29 -07001469TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1470 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1471 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1472 .Times(0);
1473 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1474}
1475
1476TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1477 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1478 fake_system_state_.set_device_policy(device_policy.get());
1479 // Make device policy return that this is not enterprise enrolled
1480 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1481
1482 // Ensure that the metric is not recorded.
1483 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1484 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1485 .Times(0);
1486 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1487}
1488
1489TEST_F(UpdateAttempterTest,
1490 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1491 constexpr int kDaysToUpdate = 15;
1492 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1493 fake_system_state_.set_device_policy(device_policy.get());
1494 // Make device policy return that this is enterprise enrolled
1495 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1496 // Pretend that there's a time restriction policy in place
1497 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1498 .WillOnce(Return(true));
1499
1500 FakePrefs fake_prefs;
1501 Time update_first_seen_at = Time::Now();
1502 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1503 update_first_seen_at.ToInternalValue());
1504
1505 FakeClock fake_clock;
1506 Time update_finished_at =
1507 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1508 fake_clock.SetWallclockTime(update_finished_at);
1509
1510 fake_system_state_.set_clock(&fake_clock);
1511 fake_system_state_.set_prefs(&fake_prefs);
1512
1513 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1514 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1515 .Times(1);
1516 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1517}
1518
1519TEST_F(UpdateAttempterTest,
1520 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
1521 constexpr int kDaysToUpdate = 15;
1522 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1523 fake_system_state_.set_device_policy(device_policy.get());
1524 // Make device policy return that this is enterprise enrolled
1525 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1526 // Pretend that there's no time restriction policy in place
1527 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1528 .WillOnce(Return(false));
1529
1530 FakePrefs fake_prefs;
1531 Time update_first_seen_at = Time::Now();
1532 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1533 update_first_seen_at.ToInternalValue());
1534
1535 FakeClock fake_clock;
1536 Time update_finished_at =
1537 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1538 fake_clock.SetWallclockTime(update_finished_at);
1539
1540 fake_system_state_.set_clock(&fake_clock);
1541 fake_system_state_.set_prefs(&fake_prefs);
1542
1543 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1544 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
1545 .Times(1);
1546 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1547}
1548
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001549} // namespace chromeos_update_engine