blob: 0e1be2c40ba8049dc6c20995e0c1213c44f9dd49 [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
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
Marton Hunyadya0302682018-05-16 18:52:13 +020082const char kRollbackVersion[] = "10575.39.2";
83
Darin Petkovf42cc1c2010-09-01 09:03:02 -070084// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070085// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070086// methods.
87class UpdateAttempterUnderTest : public UpdateAttempter {
88 public:
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070089 explicit UpdateAttempterUnderTest(SystemState* system_state)
90 : UpdateAttempter(system_state, nullptr) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070091
92 // Wrap the update scheduling method, allowing us to opt out of scheduled
93 // updates for testing purposes.
Xiaochu Liu88d90382018-08-29 16:09:11 -070094 bool ScheduleUpdates() override {
Gilad Arnoldec7f9162014-07-15 13:24:46 -070095 schedule_updates_called_ = true;
96 if (do_schedule_updates_) {
97 UpdateAttempter::ScheduleUpdates();
98 } else {
99 LOG(INFO) << "[TEST] Update scheduling disabled.";
100 }
Xiaochu Liu88d90382018-08-29 16:09:11 -0700101 return true;
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700102 }
103 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
104 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
105
106 // Indicates whether ScheduleUpdates() was called.
107 bool schedule_updates_called() const { return schedule_updates_called_; }
108
David Pursell02c18642014-11-06 11:26:11 -0800109 // Need to expose forced_omaha_url_ so we can test it.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700110 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -0800111
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700112 private:
113 bool schedule_updates_called_ = false;
114 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700115};
116
117class UpdateAttempterTest : public ::testing::Test {
118 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700119 UpdateAttempterTest()
Daniel Erate5f6f252017-04-20 12:09:58 -0600120 : certificate_checker_(fake_system_state_.mock_prefs(),
Alex Deymo33e91e72015-12-01 18:26:08 -0300121 &openssl_wrapper_) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700122 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700123 fake_system_state_.set_connection_manager(&mock_connection_manager);
124 fake_system_state_.set_update_attempter(&attempter_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700125 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700126
Alex Deymo33e91e72015-12-01 18:26:08 -0300127 certificate_checker_.Init();
128
Sen Jiange67bb5b2016-06-20 15:53:56 -0700129 // Finish initializing the attempter.
Gilad Arnold1f847232014-04-07 12:07:49 -0700130 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -0700131 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700132
Alex Deymo610277e2014-11-11 21:18:11 -0800133 void SetUp() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700134 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700135 EXPECT_EQ(0, attempter_.http_response_code_);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700136 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700137 EXPECT_EQ(0.0, attempter_.download_progress_);
138 EXPECT_EQ(0, attempter_.last_checked_time_);
139 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700140 EXPECT_EQ(0ULL, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800141 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700142 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700143 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700144
145 // Set up store/load semantics of P2P properties via the mock PayloadState.
146 actual_using_p2p_for_downloading_ = false;
147 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
148 SetUsingP2PForDownloading(_))
149 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
150 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
151 GetUsingP2PForDownloading())
152 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
153 actual_using_p2p_for_sharing_ = false;
154 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
155 SetUsingP2PForSharing(_))
156 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
157 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
158 GetUsingP2PForDownloading())
159 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700160 }
161
Alex Deymo60ca1a72015-06-18 18:19:15 -0700162 public:
163 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200164
Alex Deymo60ca1a72015-06-18 18:19:15 -0700165 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800166 void UpdateTestStart();
167 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700168 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700169 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700170 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700171 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700172 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700173 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700174 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700175 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700176 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700177 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700178 void P2PEnabledHousekeepingFailsStart();
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200179 void ResetRollbackHappenedStart(bool is_consumer,
180 bool is_policy_available,
181 bool expected_reset);
Adolfo Victoria497044c2018-07-18 07:51:42 -0700182 // Staging related callbacks.
183 void SetUpStagingTest(const StagingSchedule& schedule, FakePrefs* prefs);
184 void CheckStagingOff();
185 void StagingSetsPrefsAndTurnsOffScatteringStart();
186 void StagingOffIfInteractiveStart();
187 void StagingOffIfOobeStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700188
Gilad Arnold74b5f552014-10-07 08:17:16 -0700189 bool actual_using_p2p_for_downloading() {
190 return actual_using_p2p_for_downloading_;
191 }
192 bool actual_using_p2p_for_sharing() {
193 return actual_using_p2p_for_sharing_;
194 }
195
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700196 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700197 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700198
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700199 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700200 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300201 OpenSSLWrapper openssl_wrapper_;
202 CertificateChecker certificate_checker_;
Alex Deymo30534502015-07-20 15:06:33 -0700203
Alex Deymo8427b4a2014-11-05 14:00:32 -0800204 NiceMock<MockActionProcessor>* processor_;
205 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800206 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700207
Gilad Arnold74b5f552014-10-07 08:17:16 -0700208 bool actual_using_p2p_for_downloading_;
209 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700210};
211
Alex Deymo60ca1a72015-06-18 18:19:15 -0700212void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700213 loop_.PostTask(
214 FROM_HERE,
215 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
216 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700217}
218
Darin Petkov1b003102010-11-30 10:18:36 -0800219TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700220 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800221 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700222 DownloadAction action(prefs_,
223 nullptr,
224 nullptr,
225 nullptr,
226 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700227 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800228 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700229 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700230 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700231 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700232 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800233}
234
235TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800236 MockAction action;
237 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700238 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800239 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800240 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700241 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
242 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800243}
244
Aaron Wood9321f502017-09-07 11:18:54 -0700245TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
246 // Simple test case, where all the values match (nothing was skipped)
247 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
248 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
249 uint64_t bytes_received_1 = bytes_progressed_1;
250 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
251 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
252
253 double progress_1 =
254 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
255 double progress_2 =
256 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
257
258 EXPECT_EQ(0.0, attempter_.download_progress_);
259 // This is set via inspecting the InstallPlan payloads when the
260 // OmahaResponseAction is completed
261 attempter_.new_payload_size_ = bytes_total;
262 NiceMock<MockServiceObserver> observer;
263 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700264 SendStatusUpdate(AllOf(
265 Field(&UpdateEngineStatus::progress, progress_1),
266 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
267 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700268 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700269 SendStatusUpdate(AllOf(
270 Field(&UpdateEngineStatus::progress, progress_2),
271 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
272 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700273 attempter_.AddObserver(&observer);
274 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
275 EXPECT_EQ(progress_1, attempter_.download_progress_);
276 // This iteration validates that a later set of updates to the variables are
277 // properly handled (so that |getStatus()| will return the same progress info
278 // as the callback is receiving.
279 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
280 EXPECT_EQ(progress_2, attempter_.download_progress_);
281}
282
283TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
284 // The transition into UpdateStatus::DOWNLOADING happens when the
285 // first bytes are received.
286 uint64_t bytes_progressed = 1024 * 1024; // 1MB
287 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
288 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
289 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
290 // This is set via inspecting the InstallPlan payloads when the
291 // OmahaResponseAction is completed
292 attempter_.new_payload_size_ = bytes_total;
293 EXPECT_EQ(0.0, attempter_.download_progress_);
294 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700295 EXPECT_CALL(observer,
296 SendStatusUpdate(AllOf(
297 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
298 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700299 attempter_.AddObserver(&observer);
300 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
301 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
302}
303
304TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
305 // There is a special case to ensure that at 100% downloaded,
306 // download_progress_ is updated and that value broadcast. This test confirms
307 // that.
308 uint64_t bytes_progressed = 0; // ignored
309 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
310 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
311 attempter_.status_ = UpdateStatus::DOWNLOADING;
312 attempter_.new_payload_size_ = bytes_total;
313 EXPECT_EQ(0.0, attempter_.download_progress_);
314 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700315 EXPECT_CALL(observer,
316 SendStatusUpdate(AllOf(
317 Field(&UpdateEngineStatus::progress, 1.0),
318 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
319 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700320 attempter_.AddObserver(&observer);
321 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
322 EXPECT_EQ(1.0, attempter_.download_progress_);
323}
324
Darin Petkov1b003102010-11-30 10:18:36 -0800325TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700326 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800327 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700328 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300329 std::move(fetcher), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800330 ObjectCollectorAction<OmahaResponse> collector_action;
331 BondActions(&action, &collector_action);
332 OmahaResponse response;
333 response.poll_interval = 234;
334 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800335 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700336 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800337 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700338 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800339 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700340 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800341}
342
Alex Deymo30534502015-07-20 15:06:33 -0700343TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700344 FakePrefs fake_prefs;
345 string boot_id;
346 EXPECT_TRUE(utils::GetBootId(&boot_id));
347 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
348 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700349 attempter_.Init();
350 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700351}
352
353TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700354 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
355 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700356 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700357 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700358
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700359 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700360 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
361 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700362 EXPECT_EQ(ErrorCode::kOmahaRequestError,
363 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700364 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700365 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700366 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700367 ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700368 FilesystemVerifierAction filesystem_verifier_action;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700369 EXPECT_EQ(ErrorCode::kFilesystemVerifierError,
370 GetErrorCodeForAction(&filesystem_verifier_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700371 ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300372 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700373 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700374 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700375 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700376 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800377 MockAction action_mock;
378 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700379 EXPECT_EQ(ErrorCode::kError,
380 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700381}
382
Darin Petkov36275772010-10-01 11:40:57 -0700383TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700384 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800385 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700386 .WillOnce(Return(false));
387 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700388 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800389 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700390 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700391 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
Darin Petkov36275772010-10-01 11:40:57 -0700392 Return(true)));
393 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700394 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800395 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700396 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700397 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
Darin Petkov36275772010-10-01 11:40:57 -0700398 Return(true)));
399 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700400 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800401 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700402 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700403 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700404}
405
406TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800407 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700408 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700409 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
410 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Darin Petkov36275772010-10-01 11:40:57 -0700411 .WillOnce(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700412 SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
Darin Petkov36275772010-10-01 11:40:57 -0700413 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800414 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700415 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800416 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700417 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800418 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700419 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700420 for (int i = 0; i < 4; i ++)
421 attempter_.MarkDeltaUpdateFailure();
422}
423
Darin Petkov1b003102010-11-30 10:18:36 -0800424TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
425 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
426 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700427 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800428 .Times(0);
429 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700430 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800431 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700432 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700433 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700434 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800435 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700436 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800437}
438
439TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
440 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700441 EnqueueAction(Pointee(Property(
442 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700443 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700444 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700445 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800446 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
447 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800448 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800449 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700450 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800451}
452
Darin Petkove6ef2f82011-03-07 17:31:11 -0800453namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700454// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700455const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Amin Hassani0882a512018-04-05 16:25:44 -0700456 OmahaRequestAction::StaticType(),
457 OmahaResponseHandlerAction::StaticType(),
458 UpdateBootFlagsAction::StaticType(),
459 OmahaRequestAction::StaticType(),
460 DownloadAction::StaticType(),
461 OmahaRequestAction::StaticType(),
462 FilesystemVerifierAction::StaticType(),
463 PostinstallRunnerAction::StaticType(),
464 OmahaRequestAction::StaticType()};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700465
466// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700467const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700468 InstallPlanAction::StaticType(),
469 PostinstallRunnerAction::StaticType(),
470};
471
Adolfo Victoria497044c2018-07-18 07:51:42 -0700472const StagingSchedule kValidStagingSchedule = {
473 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
474
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700475} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800476
477void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700478 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700479
480 // Expect that the device policy is loaded by the UpdateAttempter at some
481 // point by calling RefreshDevicePolicy.
Igor9fd76b62017-12-11 15:24:18 +0100482 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700483 EXPECT_CALL(*device_policy, LoadPolicy())
484 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100485 attempter_.policy_provider_.reset(
486 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700487
488 {
489 InSequence s;
490 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
491 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700492 EnqueueAction(Pointee(
493 Property(&AbstractAction::Type, kUpdateActionTypes[i]))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700494 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700495 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700496 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700497
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200498 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700499 loop_.PostTask(FROM_HERE,
500 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
501 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800502}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700503
Darin Petkove6ef2f82011-03-07 17:31:11 -0800504void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700505 EXPECT_EQ(0, attempter_.http_response_code());
506 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700507 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700508 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800509}
510
Chris Sosa28e479c2013-07-12 11:39:53 -0700511void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700512 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700513 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100514 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700515 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100516 fake_system_state_.set_device_policy(device_policy.get());
517 if (enterprise_rollback) {
518 // We return an empty owner as this is an enterprise.
519 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
520 DoAll(SetArgPointee<0>(string("")),
521 Return(true)));
522 } else {
523 // We return a fake owner as this is an owned consumer device.
524 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
525 DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
526 Return(true)));
527 }
528
529 attempter_.policy_provider_.reset(
530 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700531
Alex Deymo763e7db2015-08-27 21:08:08 -0700532 if (valid_slot) {
533 BootControlInterface::Slot rollback_slot = 1;
534 LOG(INFO) << "Test Mark Bootable: "
535 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700536 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
537 true);
Don Garrett6646b442013-11-13 15:29:11 -0800538 }
539
Chris Sosa28e479c2013-07-12 11:39:53 -0700540 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700541
Chris Sosad38b1132014-03-25 10:43:59 -0700542 // We only allow rollback on devices that are not enterprise enrolled and
543 // which have a valid slot to rollback to.
544 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700545 is_rollback_allowed = true;
546 }
547
Chris Sosa28e479c2013-07-12 11:39:53 -0700548 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700549 InSequence s;
550 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
551 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700552 EnqueueAction(Pointee(Property(&AbstractAction::Type,
553 kRollbackActionTypes[i]))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700554 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700555 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700556
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700557 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700558 loop_.PostTask(FROM_HERE,
559 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
560 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700561 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700562 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700563 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700564 }
565}
566
567void UpdateAttempterTest::RollbackTestVerify() {
568 // Verifies the actions that were enqueued.
569 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700570 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700571 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
572 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700573 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700574}
575
Darin Petkove6ef2f82011-03-07 17:31:11 -0800576TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700577 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700578 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700579}
580
Chris Sosa76a29ae2013-07-11 17:59:24 -0700581TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700582 loop_.PostTask(FROM_HERE,
583 base::Bind(&UpdateAttempterTest::RollbackTestStart,
584 base::Unretained(this),
585 false, true));
586 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700587}
588
Don Garrett6646b442013-11-13 15:29:11 -0800589TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700590 loop_.PostTask(FROM_HERE,
591 base::Bind(&UpdateAttempterTest::RollbackTestStart,
592 base::Unretained(this),
593 false, false));
594 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800595}
596
Chris Sosa76a29ae2013-07-11 17:59:24 -0700597TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700598 loop_.PostTask(FROM_HERE,
599 base::Bind(&UpdateAttempterTest::RollbackTestStart,
600 base::Unretained(this),
601 true, true));
602 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700603}
604
Thieu Le116fda32011-04-19 11:01:54 -0700605void UpdateAttempterTest::PingOmahaTestStart() {
606 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700607 EnqueueAction(Pointee(Property(
608 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700609 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700610 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700611 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700612}
613
614TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700615 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
616 EXPECT_FALSE(attempter_.schedule_updates_called());
617 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
618 // testing, which is more permissive than we want to handle here.
619 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700620 loop_.PostTask(FROM_HERE,
621 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
622 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700623 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700624 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700625 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700626}
627
Darin Petkov18c7bce2011-06-16 14:07:00 -0700628TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800629 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700630 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700631 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700632 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700633 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
634 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700635 EXPECT_EQ(
636 static_cast<ErrorCode>(static_cast<int>(kCode) |
637 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
638 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700639}
640
641TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700642 attempter_.install_plan_.reset(new InstallPlan);
643 attempter_.install_plan_->is_resume = true;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800644 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700645 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700646 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700647 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700648 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
649 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700650 EXPECT_EQ(
651 static_cast<ErrorCode>(
652 static_cast<int>(kCode) |
653 static_cast<int>(ErrorCode::kResumedFlag) |
654 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
655 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700656}
657
David Zeuthen8f191b22013-08-06 12:27:50 -0700658TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
659 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700660 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700661 mock_p2p_manager.fake().SetP2PEnabled(false);
662 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
663 attempter_.UpdateEngineStarted();
664}
665
666TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
667 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700668 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700669 mock_p2p_manager.fake().SetP2PEnabled(true);
670 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
671 attempter_.UpdateEngineStarted();
672}
673
674TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
675 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700676 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700677 mock_p2p_manager.fake().SetP2PEnabled(true);
678 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700679 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700680 attempter_.UpdateEngineStarted();
681}
682
683TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700684 loop_.PostTask(FROM_HERE,
685 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
686 base::Unretained(this)));
687 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700688}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700689
David Zeuthen8f191b22013-08-06 12:27:50 -0700690void UpdateAttempterTest::P2PNotEnabledStart() {
691 // If P2P is not enabled, check that we do not attempt housekeeping
692 // and do not convey that p2p is to be used.
693 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700694 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700695 mock_p2p_manager.fake().SetP2PEnabled(false);
696 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200697 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700698 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700699 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700700 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700701}
702
703TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700704 loop_.PostTask(FROM_HERE,
705 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
706 base::Unretained(this)));
707 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700708}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700709
David Zeuthen8f191b22013-08-06 12:27:50 -0700710void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
711 // If p2p is enabled, but starting it fails ensure we don't do
712 // any housekeeping and do not convey that p2p should be used.
713 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700714 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700715 mock_p2p_manager.fake().SetP2PEnabled(true);
716 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
717 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
718 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200719 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700720 EXPECT_FALSE(actual_using_p2p_for_downloading());
721 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700722 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700723}
724
725TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700726 loop_.PostTask(
727 FROM_HERE,
728 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
729 base::Unretained(this)));
730 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700731}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700732
David Zeuthen8f191b22013-08-06 12:27:50 -0700733void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
734 // If p2p is enabled, starting it works but housekeeping fails, ensure
735 // we do not convey p2p is to be used.
736 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700737 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700738 mock_p2p_manager.fake().SetP2PEnabled(true);
739 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
740 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700741 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200742 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700743 EXPECT_FALSE(actual_using_p2p_for_downloading());
744 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700745 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700746}
747
748TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700749 loop_.PostTask(FROM_HERE,
750 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
751 base::Unretained(this)));
752 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700753}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700754
David Zeuthen8f191b22013-08-06 12:27:50 -0700755void UpdateAttempterTest::P2PEnabledStart() {
756 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700757 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700758 // If P2P is enabled and starting it works, check that we performed
759 // housekeeping and that we convey p2p should be used.
760 mock_p2p_manager.fake().SetP2PEnabled(true);
761 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
762 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700763 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200764 attempter_.Update("", "", "", "", false, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700765 EXPECT_TRUE(actual_using_p2p_for_downloading());
766 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700767 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700768}
769
770TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700771 loop_.PostTask(FROM_HERE,
772 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
773 base::Unretained(this)));
774 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700775}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700776
David Zeuthen8f191b22013-08-06 12:27:50 -0700777void UpdateAttempterTest::P2PEnabledInteractiveStart() {
778 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700779 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700780 // For an interactive check, if P2P is enabled and starting it
781 // works, check that we performed housekeeping and that we convey
782 // p2p should be used for sharing but NOT for downloading.
783 mock_p2p_manager.fake().SetP2PEnabled(true);
784 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
785 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700786 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200787 attempter_.Update("",
788 "",
789 "",
790 "",
791 false,
792 false,
793 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700794 EXPECT_FALSE(actual_using_p2p_for_downloading());
795 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700796 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700797}
798
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700799TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700800 loop_.PostTask(
801 FROM_HERE,
802 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
803 base::Unretained(this)));
804 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700805}
806
807// Tests that the scatter_factor_in_seconds value is properly fetched
808// from the device policy.
809void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700810 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700811
Igor9fd76b62017-12-11 15:24:18 +0100812 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700813 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100814 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700815
816 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
817 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700818 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700819 Return(true)));
820
Igor9fd76b62017-12-11 15:24:18 +0100821 attempter_.policy_provider_.reset(
822 new policy::PolicyProvider(std::move(device_policy)));
823
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200824 attempter_.Update("", "", "", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700825 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
826
Alex Deymo60ca1a72015-06-18 18:19:15 -0700827 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700828}
829
830TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700831 loop_.PostTask(
832 FROM_HERE,
833 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
834 base::Unretained(this)));
835 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700836}
837
838void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
839 // Tests that the scatter_factor_in_seconds value is properly fetched
840 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700841 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800842 FakePrefs fake_prefs;
843 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700844
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700845 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700846
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800847 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700848
Ben Chan9abb7632014-08-07 00:10:53 -0700849 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700850
Igor9fd76b62017-12-11 15:24:18 +0100851 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700852 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100853 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700854
855 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
856 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700857 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700858 Return(true)));
859
Igor9fd76b62017-12-11 15:24:18 +0100860 attempter_.policy_provider_.reset(
861 new policy::PolicyProvider(std::move(device_policy)));
862
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200863 attempter_.Update("", "", "", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700864 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
865
866 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800867 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700868
Ben Chan9abb7632014-08-07 00:10:53 -0700869 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800870 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700871 EXPECT_EQ(initial_value - 1, new_value);
872
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700873 EXPECT_TRUE(
874 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700875
876 // However, if the count is already 0, it's not decremented. Test that.
877 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800878 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200879 attempter_.Update("", "", "", "", false, false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800880 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
881 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700882 EXPECT_EQ(initial_value, new_value);
883
Alex Deymo60ca1a72015-06-18 18:19:15 -0700884 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700885}
886
Jay Srinivasan08fce042012-06-07 16:31:01 -0700887TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700888 loop_.PostTask(FROM_HERE, base::Bind(
889 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
890 base::Unretained(this)));
891 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700892}
893
894void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
895 // Tests that no scattering logic is enabled if the update check
896 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700897 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800898 FakePrefs fake_prefs;
899 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700900
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700901 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800902 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700903
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700904 EXPECT_TRUE(
905 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800906 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700907
908 // make sure scatter_factor is non-zero as scattering is disabled
909 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700910 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700911
Igor9fd76b62017-12-11 15:24:18 +0100912 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700913 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100914 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700915
916 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
917 .WillRepeatedly(DoAll(
Ben Chan672c1f52017-10-23 15:41:39 -0700918 SetArgPointee<0>(scatter_factor_in_seconds),
Jay Srinivasan08fce042012-06-07 16:31:01 -0700919 Return(true)));
920
Igor9fd76b62017-12-11 15:24:18 +0100921 attempter_.policy_provider_.reset(
922 new policy::PolicyProvider(std::move(device_policy)));
923
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800924 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200925 attempter_.Update("",
926 "",
927 "",
928 "",
929 false,
930 false,
931 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700932 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
933
934 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700935 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700936 EXPECT_FALSE(
937 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700938 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700939 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
940 EXPECT_FALSE(
941 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800942 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700943
Alex Deymo60ca1a72015-06-18 18:19:15 -0700944 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700945}
946
Adolfo Victoria497044c2018-07-18 07:51:42 -0700947void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
948 FakePrefs* prefs) {
949 attempter_.prefs_ = prefs;
950 fake_system_state_.set_prefs(prefs);
951
952 int64_t initial_value = 8;
953 EXPECT_TRUE(
954 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
955 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
956 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
957
958 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
959 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
960 fake_system_state_.set_device_policy(device_policy.get());
961 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
962 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
963
964 attempter_.policy_provider_.reset(
965 new policy::PolicyProvider(std::move(device_policy)));
966}
967
968TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
969 loop_.PostTask(
970 FROM_HERE,
971 base::Bind(
972 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
973 base::Unretained(this)));
974 loop_.Run();
975}
976
977void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
978 // Tests that staging sets its prefs properly and turns off scattering.
979 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
980 FakePrefs fake_prefs;
981 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
982
983 attempter_.Update("", "", "", "", false, false, false);
984 // Check that prefs have the correct values.
985 int64_t update_count;
986 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
987 int64_t waiting_time_days;
988 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
989 &waiting_time_days));
990 EXPECT_GT(waiting_time_days, 0);
991 // Update count should have been decremented.
992 EXPECT_EQ(7, update_count);
993 // Check that Omaha parameters were updated correctly.
994 EXPECT_TRUE(
995 attempter_.omaha_request_params_->update_check_count_wait_enabled());
996 EXPECT_TRUE(
997 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
998 EXPECT_EQ(waiting_time_days,
999 attempter_.omaha_request_params_->waiting_period().InDays());
1000 // Check class variables.
1001 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1002 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1003 // Check that scattering is turned off
1004 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1005 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1006
1007 ScheduleQuitMainLoop();
1008}
1009
1010void UpdateAttempterTest::CheckStagingOff() {
1011 // Check that all prefs were removed.
1012 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1013 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1014 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1015 // Check that the Omaha parameters have the correct value.
1016 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1017 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1018 attempter_.staging_wait_time_);
1019 EXPECT_FALSE(
1020 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1021 EXPECT_FALSE(
1022 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1023 // Check that scattering is turned off too.
1024 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1025}
1026
1027TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1028 loop_.PostTask(FROM_HERE,
1029 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1030 base::Unretained(this)));
1031 loop_.Run();
1032}
1033
1034void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1035 // Tests that staging is turned off when an interactive update is requested.
1036 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1037 FakePrefs fake_prefs;
1038 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1039
1040 attempter_.Update("", "", "", "", false, false, /* interactive = */ true);
1041 CheckStagingOff();
1042
1043 ScheduleQuitMainLoop();
1044}
1045
1046TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1047 loop_.PostTask(FROM_HERE,
1048 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1049 base::Unretained(this)));
1050 loop_.Run();
1051}
1052
1053void UpdateAttempterTest::StagingOffIfOobeStart() {
1054 // Tests that staging is turned off if OOBE hasn't been completed.
1055 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1056 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1057 FakePrefs fake_prefs;
1058 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1059
1060 attempter_.Update("", "", "", "", false, false, /* interactive = */ true);
1061 CheckStagingOff();
1062
1063 ScheduleQuitMainLoop();
1064}
1065
David Zeuthen985b1122013-10-09 12:13:15 -07001066// Checks that we only report daily metrics at most every 24 hours.
1067TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1068 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001069 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001070
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001071 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001072 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001073
1074 Time epoch = Time::FromInternalValue(0);
1075 fake_clock.SetWallclockTime(epoch);
1076
1077 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1078 // we should report.
1079 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1080 // We should not report again if no time has passed.
1081 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1082
1083 // We should not report if only 10 hours has passed.
1084 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1085 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1086
1087 // We should not report if only 24 hours - 1 sec has passed.
1088 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1089 TimeDelta::FromSeconds(1));
1090 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1091
1092 // We should report if 24 hours has passed.
1093 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1094 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1095
1096 // But then we should not report again..
1097 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1098
1099 // .. until another 24 hours has passed
1100 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1101 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1102 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1103 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1104 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1105
1106 // .. and another 24 hours
1107 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1108 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1109 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1110 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1111 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1112
1113 // If the span between time of reporting and present time is
1114 // negative, we report. This is in order to reset the timestamp and
1115 // avoid an edge condition whereby a distant point in the future is
1116 // in the state variable resulting in us never ever reporting again.
1117 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1118 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1119 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1120
1121 // In this case we should not update until the clock reads 71 + 24 = 95.
1122 // Check that.
1123 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1124 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1125 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1126 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1127 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001128}
1129
David Zeuthen3c55abd2013-10-14 12:48:03 -07001130TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001131 FakeClock fake_clock;
1132 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001133 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001134 FakePrefs fake_prefs;
1135 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001136 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001137
1138 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001139 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001140
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001141 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001142
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001143 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001144 EXPECT_EQ(boot_time.ToTimeT(), 42);
1145}
1146
David Pursell02c18642014-11-06 11:26:11 -08001147TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1148 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1149 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1150}
1151
1152TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1153 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001154 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001155 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1156}
1157
1158TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1159 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001160 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001161 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1162}
1163
1164TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1165 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001166 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001167 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001168 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001169}
1170
1171TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
1172 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001173 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001174 attempter_.CheckForUpdate("", "autest-scheduled", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001175 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001176}
1177
Xiaochu Liu88d90382018-08-29 16:09:11 -07001178TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1179 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1180 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1181 attempter_.CheckForInstall({}, "autest");
1182 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1183
1184 attempter_.CheckForInstall({}, "autest-scheduled");
1185 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1186
1187 attempter_.CheckForInstall({}, "http://omaha.phishing");
1188 EXPECT_EQ("", attempter_.forced_omaha_url());
1189}
1190
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001191TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001192 attempter_.CalculateUpdateParams("", "", "", "1234", false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001193 EXPECT_EQ("1234",
1194 fake_system_state_.request_params()->target_version_prefix());
1195
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001196 attempter_.CalculateUpdateParams("", "", "", "", false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001197 EXPECT_TRUE(
1198 fake_system_state_.request_params()->target_version_prefix().empty());
1199}
1200
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001201TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1202 attempter_.CalculateUpdateParams("",
1203 "",
1204 "",
1205 "1234",
1206 /*rollback_allowed=*/true,
1207 false,
1208 false);
1209 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1210
1211 attempter_.CalculateUpdateParams("",
1212 "",
1213 "",
1214 "1234",
1215 /*rollback_allowed=*/false,
1216 false,
1217 false);
1218 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1219}
1220
Aaron Wood23bd3392017-10-06 14:48:25 -07001221TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1222 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1223 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001224 OmahaResponseHandlerAction response_action(&fake_system_state_);
1225 response_action.install_plan_.version = "a.b.c.d";
1226 response_action.install_plan_.system_version = "b.c.d.e";
1227 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001228 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001229 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1230 // completed, with the deferred-update error code.
1231 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001232 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001233 {
1234 UpdateEngineStatus status;
1235 attempter_.GetStatus(&status);
1236 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001237 EXPECT_TRUE(attempter_.install_plan_);
1238 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
1239 EXPECT_EQ(attempter_.install_plan_->system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001240 status.new_system_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001241 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001242 status.new_size_bytes);
1243 }
1244 // An "error" event should have been created to tell Omaha that the update is
1245 // being deferred.
1246 EXPECT_TRUE(nullptr != attempter_.error_event_);
1247 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1248 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1249 ErrorCode expected_code = static_cast<ErrorCode>(
1250 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1251 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1252 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1253 // End the processing
1254 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1255 // Validate the state of the attempter.
1256 {
1257 UpdateEngineStatus status;
1258 attempter_.GetStatus(&status);
1259 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001260 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
1261 EXPECT_EQ(response_action.install_plan_.system_version,
Aaron Wood23bd3392017-10-06 14:48:25 -07001262 status.new_system_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001263 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001264 status.new_size_bytes);
1265 }
1266}
1267
1268TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
1269 EXPECT_FALSE(attempter_.IsUpdateRunningOrScheduled());
1270 // Verify in-progress update with UPDATE_AVAILABLE is running
1271 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
1272 EXPECT_TRUE(attempter_.IsUpdateRunningOrScheduled());
1273}
1274
Aaron Woodbf5a2522017-10-04 10:58:36 -07001275TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1276 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1277
1278 UpdateCheckParams params = {.updates_enabled = true};
1279 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1280
1281 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1282 attempter_.GetCurrentUpdateAttemptFlags());
1283}
1284
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001285TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1286 UpdateCheckParams params = {.updates_enabled = true,
1287 .rollback_allowed = false};
1288 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1289 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1290}
1291
1292TEST_F(UpdateAttempterTest, RollbackAllowed) {
1293 UpdateCheckParams params = {.updates_enabled = true,
1294 .rollback_allowed = true};
1295 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1296 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1297}
1298
Aaron Wood081c0232017-10-19 17:14:58 -07001299TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1300 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1301
1302 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1303 EXPECT_EQ(UpdateAttemptFlags::kNone,
1304 attempter_.GetCurrentUpdateAttemptFlags());
1305}
1306
1307TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1308 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1309
1310 // This tests that when CheckForUpdate() is called with the non-interactive
1311 // flag set, that it doesn't change the current UpdateAttemptFlags.
1312 attempter_.CheckForUpdate("",
1313 "",
1314 UpdateAttemptFlags::kFlagNonInteractive |
1315 UpdateAttemptFlags::kFlagRestrictDownload);
1316 EXPECT_EQ(UpdateAttemptFlags::kNone,
1317 attempter_.GetCurrentUpdateAttemptFlags());
1318}
1319
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001320void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1321 bool is_policy_loaded,
1322 bool expected_reset) {
1323 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1324 .WillRepeatedly(Return(true));
1325 auto mock_policy_provider =
1326 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1327 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1328 .WillRepeatedly(Return(is_consumer));
1329 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1330 .WillRepeatedly(Return(is_policy_loaded));
1331 const policy::MockDevicePolicy device_policy;
1332 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1333 .WillRepeatedly(ReturnRef(device_policy));
1334 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1335 SetRollbackHappened(false))
1336 .Times(expected_reset ? 1 : 0);
1337 attempter_.policy_provider_ = std::move(mock_policy_provider);
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001338 attempter_.Update("", "", "", "", false, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001339 ScheduleQuitMainLoop();
1340}
1341
1342TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1343 loop_.PostTask(FROM_HERE,
1344 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1345 base::Unretained(this),
1346 /*is_consumer=*/false,
1347 /*is_policy_loaded=*/false,
1348 /*expected_reset=*/false));
1349 loop_.Run();
1350}
1351
1352TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1353 loop_.PostTask(FROM_HERE,
1354 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1355 base::Unretained(this),
1356 /*is_consumer=*/true,
1357 /*is_policy_loaded=*/false,
1358 /*expected_reset=*/true));
1359 loop_.Run();
1360}
1361
1362TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1363 loop_.PostTask(FROM_HERE,
1364 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1365 base::Unretained(this),
1366 /*is_consumer=*/false,
1367 /*is_policy_loaded=*/true,
1368 /*expected_reset=*/true));
1369 loop_.Run();
1370}
1371
Marton Hunyady199152d2018-05-07 19:08:48 +02001372TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001373 attempter_.install_plan_.reset(new InstallPlan);
1374 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001375
1376 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1377 SetRollbackHappened(true))
1378 .Times(1);
1379 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1380}
1381
1382TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001383 attempter_.install_plan_.reset(new InstallPlan);
1384 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001385
1386 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1387 SetRollbackHappened(true))
1388 .Times(0);
1389 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1390}
1391
Marton Hunyadya0302682018-05-16 18:52:13 +02001392TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001393 attempter_.install_plan_.reset(new InstallPlan);
1394 attempter_.install_plan_->is_rollback = true;
1395 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001396
1397 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1398 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1399 .Times(1);
1400 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1401}
1402
1403TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001404 attempter_.install_plan_.reset(new InstallPlan);
1405 attempter_.install_plan_->is_rollback = false;
1406 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001407
1408 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1409 ReportEnterpriseRollbackMetrics(_, _))
1410 .Times(0);
1411 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1412}
1413
1414TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001415 attempter_.install_plan_.reset(new InstallPlan);
1416 attempter_.install_plan_->is_rollback = true;
1417 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001418
1419 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1420 ReportEnterpriseRollbackMetrics(false, kRollbackVersion))
1421 .Times(1);
1422 MockAction action;
1423 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1424 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1425}
1426
1427TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001428 attempter_.install_plan_.reset(new InstallPlan);
1429 attempter_.install_plan_->is_rollback = false;
1430 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001431
1432 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1433 ReportEnterpriseRollbackMetrics(_, _))
1434 .Times(0);
1435 MockAction action;
1436 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1437 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1438}
1439
May Lippert60aa3ca2018-08-15 16:55:29 -07001440TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1441 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1442 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1443 .Times(0);
1444 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1445}
1446
1447TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1448 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1449 fake_system_state_.set_device_policy(device_policy.get());
1450 // Make device policy return that this is not enterprise enrolled
1451 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1452
1453 // Ensure that the metric is not recorded.
1454 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1455 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1456 .Times(0);
1457 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1458}
1459
1460TEST_F(UpdateAttempterTest,
1461 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1462 constexpr int kDaysToUpdate = 15;
1463 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1464 fake_system_state_.set_device_policy(device_policy.get());
1465 // Make device policy return that this is enterprise enrolled
1466 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1467 // Pretend that there's a time restriction policy in place
1468 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1469 .WillOnce(Return(true));
1470
1471 FakePrefs fake_prefs;
1472 Time update_first_seen_at = Time::Now();
1473 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1474 update_first_seen_at.ToInternalValue());
1475
1476 FakeClock fake_clock;
1477 Time update_finished_at =
1478 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1479 fake_clock.SetWallclockTime(update_finished_at);
1480
1481 fake_system_state_.set_clock(&fake_clock);
1482 fake_system_state_.set_prefs(&fake_prefs);
1483
1484 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1485 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1486 .Times(1);
1487 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1488}
1489
1490TEST_F(UpdateAttempterTest,
1491 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
1492 constexpr int kDaysToUpdate = 15;
1493 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1494 fake_system_state_.set_device_policy(device_policy.get());
1495 // Make device policy return that this is enterprise enrolled
1496 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1497 // Pretend that there's no time restriction policy in place
1498 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1499 .WillOnce(Return(false));
1500
1501 FakePrefs fake_prefs;
1502 Time update_first_seen_at = Time::Now();
1503 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1504 update_first_seen_at.ToInternalValue());
1505
1506 FakeClock fake_clock;
1507 Time update_finished_at =
1508 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1509 fake_clock.SetWallclockTime(update_finished_at);
1510
1511 fake_system_state_.set_clock(&fake_clock);
1512 fake_system_state_.set_prefs(&fake_prefs);
1513
1514 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1515 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
1516 .Times(1);
1517 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1518}
1519
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001520} // namespace chromeos_update_engine