blob: 9421c19ffbc1c36a0b7a9922232f2cd3d7d5f78c [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>
Jae Hoon Kimedb65502019-06-14 11:52:17 -070022#include <unordered_set>
Ben Chan02f7c1d2014-10-18 15:18:02 -070023
Ben Chan06c76a42014-09-05 08:21:06 -070024#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070025#include <base/message_loop/message_loop.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
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070034#include "update_engine/common/constants.h"
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080035#include "update_engine/common/dlcservice_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/fake_clock.h"
37#include "update_engine/common/fake_prefs.h"
Alex Deymo14fd1ec2016-02-24 22:03:57 -080038#include "update_engine/common/mock_action.h"
39#include "update_engine/common/mock_action_processor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080040#include "update_engine/common/mock_http_fetcher.h"
41#include "update_engine/common/mock_prefs.h"
42#include "update_engine/common/platform_constants.h"
43#include "update_engine/common/prefs.h"
44#include "update_engine/common/test_utils.h"
45#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070046#include "update_engine/fake_system_state.h"
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070047#include "update_engine/libcurl_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070048#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049#include "update_engine/mock_payload_state.h"
Aaron Wood9321f502017-09-07 11:18:54 -070050#include "update_engine/mock_service_observer.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080051#include "update_engine/payload_consumer/filesystem_verifier_action.h"
52#include "update_engine/payload_consumer/install_plan.h"
53#include "update_engine/payload_consumer/payload_constants.h"
54#include "update_engine/payload_consumer/postinstall_runner_action.h"
Amin Hassani0882a512018-04-05 16:25:44 -070055#include "update_engine/update_boot_flags_action.h"
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -070056#include "update_engine/update_manager/mock_update_manager.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070057
David Zeuthen985b1122013-10-09 12:13:15 -070058using base::Time;
59using base::TimeDelta;
Aaron Woodbf5a2522017-10-04 10:58:36 -070060using chromeos_update_manager::EvalStatus;
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -070061using chromeos_update_manager::MockUpdateManager;
Adolfo Victoria497044c2018-07-18 07:51:42 -070062using chromeos_update_manager::StagingSchedule;
Aaron Woodbf5a2522017-10-04 10:58:36 -070063using chromeos_update_manager::UpdateCheckParams;
Adolfo Victoria497044c2018-07-18 07:51:42 -070064using policy::DevicePolicy;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070065using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070066using std::unique_ptr;
Jae Hoon Kimedb65502019-06-14 11:52:17 -070067using std::unordered_set;
Xiaochu Liu88d90382018-08-29 16:09:11 -070068using std::vector;
Aaron Woodbf5a2522017-10-04 10:58:36 -070069using testing::_;
Darin Petkov36275772010-10-01 11:40:57 -070070using testing::DoAll;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070071using testing::Field;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070072using testing::InSequence;
Jae Hoon Kimedb65502019-06-14 11:52:17 -070073using testing::Invoke;
Darin Petkov2dd01092010-10-08 15:43:05 -070074using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080075using testing::NiceMock;
Amin Hassanid3f4bea2018-04-30 14:52:40 -070076using testing::Pointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070077using testing::Property;
78using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070079using testing::ReturnPointee;
Marton Hunyadye58bddb2018-04-10 20:27:26 +020080using testing::ReturnRef;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080081using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070082using testing::SetArgPointee;
Aaron Woodbf5a2522017-10-04 10:58:36 -070083using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070084using update_engine::UpdateEngineStatus;
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -070085using update_engine::UpdateStatus;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070086
87namespace chromeos_update_engine {
88
Xiaochu Liu8ba486f2018-11-06 11:14:10 -080089namespace {
90
91class MockDlcService : public DlcServiceInterface {
92 public:
93 MOCK_METHOD1(GetInstalled, bool(vector<string>*));
94};
95
96} // namespace
97
Marton Hunyadya0302682018-05-16 18:52:13 +020098const char kRollbackVersion[] = "10575.39.2";
99
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700100// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -0700101// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700102// methods.
103class UpdateAttempterUnderTest : public UpdateAttempter {
104 public:
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700105 explicit UpdateAttempterUnderTest(SystemState* system_state)
106 : UpdateAttempter(system_state, nullptr) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700107
108 // Wrap the update scheduling method, allowing us to opt out of scheduled
109 // updates for testing purposes.
Xiaochu Liu88d90382018-08-29 16:09:11 -0700110 bool ScheduleUpdates() override {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700111 schedule_updates_called_ = true;
112 if (do_schedule_updates_) {
113 UpdateAttempter::ScheduleUpdates();
114 } else {
115 LOG(INFO) << "[TEST] Update scheduling disabled.";
116 }
Xiaochu Liu88d90382018-08-29 16:09:11 -0700117 return true;
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700118 }
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700119
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700120 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
121
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700122 // Indicates whether |ScheduleUpdates()| was called.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700123 bool schedule_updates_called() const { return schedule_updates_called_; }
124
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700125 // Need to expose |forced_omaha_url_| so we can test it.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700126 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -0800127
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700128 private:
129 bool schedule_updates_called_ = false;
130 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700131};
132
133class UpdateAttempterTest : public ::testing::Test {
134 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700135 UpdateAttempterTest()
Daniel Erate5f6f252017-04-20 12:09:58 -0600136 : certificate_checker_(fake_system_state_.mock_prefs(),
Alex Deymo33e91e72015-12-01 18:26:08 -0300137 &openssl_wrapper_) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700138 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700139 fake_system_state_.set_connection_manager(&mock_connection_manager);
140 fake_system_state_.set_update_attempter(&attempter_);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800141 fake_system_state_.set_dlcservice(&mock_dlcservice_);
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -0700142 fake_system_state_.set_update_manager(&mock_update_manager_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700143 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700144
Alex Deymo33e91e72015-12-01 18:26:08 -0300145 certificate_checker_.Init();
146
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800147 attempter_.set_forced_update_pending_callback(
148 new base::Callback<void(bool, bool)>(base::Bind([](bool, bool) {})));
Sen Jiange67bb5b2016-06-20 15:53:56 -0700149 // Finish initializing the attempter.
Gilad Arnold1f847232014-04-07 12:07:49 -0700150 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -0700151 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700152
Alex Deymo610277e2014-11-11 21:18:11 -0800153 void SetUp() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700154 EXPECT_NE(nullptr, attempter_.system_state_);
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700155 EXPECT_NE(nullptr, attempter_.system_state_->update_manager());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700156 EXPECT_EQ(0, attempter_.http_response_code_);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700157 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700158 EXPECT_EQ(0.0, attempter_.download_progress_);
159 EXPECT_EQ(0, attempter_.last_checked_time_);
160 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700161 EXPECT_EQ(0ULL, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800162 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700163 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700164 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700165
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700166 // Setup store/load semantics of P2P properties via the mock |PayloadState|.
Gilad Arnold74b5f552014-10-07 08:17:16 -0700167 actual_using_p2p_for_downloading_ = false;
168 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
169 SetUsingP2PForDownloading(_))
170 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
171 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
172 GetUsingP2PForDownloading())
173 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
174 actual_using_p2p_for_sharing_ = false;
175 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
176 SetUsingP2PForSharing(_))
177 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
178 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
179 GetUsingP2PForDownloading())
180 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700181 }
182
Alex Deymo60ca1a72015-06-18 18:19:15 -0700183 public:
184 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200185
Alex Deymo60ca1a72015-06-18 18:19:15 -0700186 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800187 void UpdateTestStart();
188 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700189 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700190 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700191 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700192 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700193 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700194 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700195 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700196 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700197 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700198 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700199 void P2PEnabledHousekeepingFailsStart();
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700200 void SessionIdTestChange();
201 void SessionIdTestEnforceEmptyStrPingOmaha();
202 void SessionIdTestConsistencyInUpdateFlow();
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700203 void SessionIdTestInDownloadAction();
Amr Aboelkher21ac9962019-05-15 14:50:05 +0200204 void UpdateToQuickFixBuildStart(bool set_token);
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200205 void ResetRollbackHappenedStart(bool is_consumer,
206 bool is_policy_available,
207 bool expected_reset);
Adolfo Victoria497044c2018-07-18 07:51:42 -0700208 // Staging related callbacks.
209 void SetUpStagingTest(const StagingSchedule& schedule, FakePrefs* prefs);
210 void CheckStagingOff();
211 void StagingSetsPrefsAndTurnsOffScatteringStart();
212 void StagingOffIfInteractiveStart();
213 void StagingOffIfOobeStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700214
Gilad Arnold74b5f552014-10-07 08:17:16 -0700215 bool actual_using_p2p_for_downloading() {
216 return actual_using_p2p_for_downloading_;
217 }
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800218 bool actual_using_p2p_for_sharing() { return actual_using_p2p_for_sharing_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700219
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700220 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700221 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700222
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700223 FakeSystemState fake_system_state_;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -0700224 UpdateAttempterUnderTest attempter_{&fake_system_state_};
Alex Deymo33e91e72015-12-01 18:26:08 -0300225 OpenSSLWrapper openssl_wrapper_;
226 CertificateChecker certificate_checker_;
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800227 MockDlcService mock_dlcservice_;
Jae Hoon Kim504c3cb2019-07-02 11:17:24 -0700228 MockUpdateManager mock_update_manager_;
Alex Deymo30534502015-07-20 15:06:33 -0700229
Alex Deymo8427b4a2014-11-05 14:00:32 -0800230 NiceMock<MockActionProcessor>* processor_;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700231 NiceMock<MockPrefs>*
232 prefs_; // Shortcut to |fake_system_state_->mock_prefs()|.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800233 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700234
Gilad Arnold74b5f552014-10-07 08:17:16 -0700235 bool actual_using_p2p_for_downloading_;
236 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700237};
238
Alex Deymo60ca1a72015-06-18 18:19:15 -0700239void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700240 loop_.PostTask(
241 FROM_HERE,
242 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
243 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700244}
245
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700246void UpdateAttempterTest::SessionIdTestChange() {
247 EXPECT_NE(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
248 const auto old_session_id = attempter_.session_id_;
249 attempter_.Update("", "", "", "", false, false, 0, false, false);
250 EXPECT_NE(old_session_id, attempter_.session_id_);
251 ScheduleQuitMainLoop();
252}
253
254TEST_F(UpdateAttempterTest, SessionIdTestChange) {
255 loop_.PostTask(FROM_HERE,
256 base::Bind(&UpdateAttempterTest::SessionIdTestChange,
257 base::Unretained(this)));
258 loop_.Run();
259}
260
261void UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha() {
262 // The |session_id_| should not be changed and should remain as an empty
263 // string when |status_| is |UPDATED_NEED_REBOOT| (only for consistency)
264 // and |PingOmaha()| is called.
265 attempter_.DisableScheduleUpdates();
266 attempter_.status_ = UpdateStatus::UPDATED_NEED_REBOOT;
267 const auto old_session_id = attempter_.session_id_;
268 auto CheckIfEmptySessionId = [](AbstractAction* aa) {
269 if (aa->Type() == OmahaRequestAction::StaticType()) {
270 EXPECT_TRUE(static_cast<OmahaRequestAction*>(aa)->session_id_.empty());
271 }
272 };
273 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
274 .WillRepeatedly(Invoke(CheckIfEmptySessionId));
275 EXPECT_CALL(*processor_, StartProcessing());
276 attempter_.PingOmaha();
277 EXPECT_EQ(old_session_id, attempter_.session_id_);
278 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status_);
279 ScheduleQuitMainLoop();
280}
281
282TEST_F(UpdateAttempterTest, SessionIdTestEnforceEmptyStrPingOmaha) {
283 loop_.PostTask(
284 FROM_HERE,
285 base::Bind(&UpdateAttempterTest::SessionIdTestEnforceEmptyStrPingOmaha,
286 base::Unretained(this)));
287 loop_.Run();
288}
289
290void UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow() {
291 // All session IDs passed into |OmahaRequestActions| should be enforced to
292 // have the same value in |BuildUpdateActions()|.
293 unordered_set<string> session_ids;
294 // Gather all the session IDs being passed to |OmahaRequestActions|.
295 auto CheckSessionId = [&session_ids](AbstractAction* aa) {
296 if (aa->Type() == OmahaRequestAction::StaticType())
297 session_ids.insert(static_cast<OmahaRequestAction*>(aa)->session_id_);
298 };
299 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
300 .WillRepeatedly(Invoke(CheckSessionId));
301 attempter_.BuildUpdateActions(false);
302 // Validate that all the session IDs are the same.
303 EXPECT_EQ(1, session_ids.size());
304 ScheduleQuitMainLoop();
305}
306
307TEST_F(UpdateAttempterTest, SessionIdTestConsistencyInUpdateFlow) {
308 loop_.PostTask(
309 FROM_HERE,
310 base::Bind(&UpdateAttempterTest::SessionIdTestConsistencyInUpdateFlow,
311 base::Unretained(this)));
312 loop_.Run();
313}
314
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -0700315void UpdateAttempterTest::SessionIdTestInDownloadAction() {
316 // The session ID passed into |DownloadAction|'s |LibcurlHttpFetcher| should
317 // be enforced to be included in the HTTP header as X-Goog-Update-SessionId.
318 string header_value;
319 auto CheckSessionIdInDownloadAction = [&header_value](AbstractAction* aa) {
320 if (aa->Type() == DownloadAction::StaticType()) {
321 DownloadAction* da = static_cast<DownloadAction*>(aa);
322 EXPECT_TRUE(da->http_fetcher()->GetHeader(kXGoogleUpdateSessionId,
323 &header_value));
324 }
325 };
326 EXPECT_CALL(*processor_, EnqueueAction(Pointee(_)))
327 .WillRepeatedly(Invoke(CheckSessionIdInDownloadAction));
328 attempter_.BuildUpdateActions(false);
329 // Validate that X-Goog-Update_SessionId is set correctly in HTTP Header.
330 EXPECT_EQ(attempter_.session_id_, header_value);
331 ScheduleQuitMainLoop();
332}
333
334TEST_F(UpdateAttempterTest, SessionIdTestInDownloadAction) {
335 loop_.PostTask(FROM_HERE,
336 base::Bind(&UpdateAttempterTest::SessionIdTestInDownloadAction,
337 base::Unretained(this)));
338 loop_.Run();
339}
340
Darin Petkov1b003102010-11-30 10:18:36 -0800341TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700342 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800343 fetcher->FailTransfer(503); // Sets the HTTP response code.
Amin Hassani7ecda262017-07-11 17:10:50 -0700344 DownloadAction action(prefs_,
345 nullptr,
346 nullptr,
347 nullptr,
348 fetcher.release(),
Amin Hassanied37d682018-04-06 13:22:00 -0700349 false /* interactive */);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800350 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700351 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700352 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Aaron Wood9321f502017-09-07 11:18:54 -0700353 EXPECT_EQ(0.0, attempter_.download_progress_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700354 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800355}
356
357TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800358 MockAction action;
359 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700360 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800361 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800362 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700363 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
364 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800365}
366
Aaron Wood9321f502017-09-07 11:18:54 -0700367TEST_F(UpdateAttempterTest, DownloadProgressAccumulationTest) {
368 // Simple test case, where all the values match (nothing was skipped)
369 uint64_t bytes_progressed_1 = 1024 * 1024; // 1MB
370 uint64_t bytes_progressed_2 = 1024 * 1024; // 1MB
371 uint64_t bytes_received_1 = bytes_progressed_1;
372 uint64_t bytes_received_2 = bytes_received_1 + bytes_progressed_2;
373 uint64_t bytes_total = 20 * 1024 * 1024; // 20MB
374
375 double progress_1 =
376 static_cast<double>(bytes_received_1) / static_cast<double>(bytes_total);
377 double progress_2 =
378 static_cast<double>(bytes_received_2) / static_cast<double>(bytes_total);
379
380 EXPECT_EQ(0.0, attempter_.download_progress_);
381 // This is set via inspecting the InstallPlan payloads when the
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700382 // |OmahaResponseAction| is completed.
Aaron Wood9321f502017-09-07 11:18:54 -0700383 attempter_.new_payload_size_ = bytes_total;
384 NiceMock<MockServiceObserver> observer;
385 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700386 SendStatusUpdate(AllOf(
387 Field(&UpdateEngineStatus::progress, progress_1),
388 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
389 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700390 EXPECT_CALL(observer,
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700391 SendStatusUpdate(AllOf(
392 Field(&UpdateEngineStatus::progress, progress_2),
393 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
394 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700395 attempter_.AddObserver(&observer);
396 attempter_.BytesReceived(bytes_progressed_1, bytes_received_1, bytes_total);
397 EXPECT_EQ(progress_1, attempter_.download_progress_);
398 // This iteration validates that a later set of updates to the variables are
399 // properly handled (so that |getStatus()| will return the same progress info
400 // as the callback is receiving.
401 attempter_.BytesReceived(bytes_progressed_2, bytes_received_2, bytes_total);
402 EXPECT_EQ(progress_2, attempter_.download_progress_);
403}
404
405TEST_F(UpdateAttempterTest, ChangeToDownloadingOnReceivedBytesTest) {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700406 // The transition into |UpdateStatus::DOWNLOADING| happens when the
Aaron Wood9321f502017-09-07 11:18:54 -0700407 // first bytes are received.
408 uint64_t bytes_progressed = 1024 * 1024; // 1MB
409 uint64_t bytes_received = 2 * 1024 * 1024; // 2MB
410 uint64_t bytes_total = 20 * 1024 * 1024; // 300MB
411 attempter_.status_ = UpdateStatus::CHECKING_FOR_UPDATE;
412 // This is set via inspecting the InstallPlan payloads when the
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700413 // |OmahaResponseAction| is completed.
Aaron Wood9321f502017-09-07 11:18:54 -0700414 attempter_.new_payload_size_ = bytes_total;
415 EXPECT_EQ(0.0, attempter_.download_progress_);
416 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700417 EXPECT_CALL(observer,
418 SendStatusUpdate(AllOf(
419 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
420 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700421 attempter_.AddObserver(&observer);
422 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
423 EXPECT_EQ(UpdateStatus::DOWNLOADING, attempter_.status_);
424}
425
426TEST_F(UpdateAttempterTest, BroadcastCompleteDownloadTest) {
427 // There is a special case to ensure that at 100% downloaded,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700428 // |download_progress_| is updated and broadcastest.
Aaron Wood9321f502017-09-07 11:18:54 -0700429 uint64_t bytes_progressed = 0; // ignored
430 uint64_t bytes_received = 5 * 1024 * 1024; // ignored
431 uint64_t bytes_total = 5 * 1024 * 1024; // 300MB
432 attempter_.status_ = UpdateStatus::DOWNLOADING;
433 attempter_.new_payload_size_ = bytes_total;
434 EXPECT_EQ(0.0, attempter_.download_progress_);
435 NiceMock<MockServiceObserver> observer;
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700436 EXPECT_CALL(observer,
437 SendStatusUpdate(AllOf(
438 Field(&UpdateEngineStatus::progress, 1.0),
439 Field(&UpdateEngineStatus::status, UpdateStatus::DOWNLOADING),
440 Field(&UpdateEngineStatus::new_size_bytes, bytes_total))));
Aaron Wood9321f502017-09-07 11:18:54 -0700441 attempter_.AddObserver(&observer);
442 attempter_.BytesReceived(bytes_progressed, bytes_received, bytes_total);
443 EXPECT_EQ(1.0, attempter_.download_progress_);
444}
445
Darin Petkov1b003102010-11-30 10:18:36 -0800446TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700447 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800448 fetcher->FailTransfer(500); // Sets the HTTP response code.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800449 OmahaRequestAction action(
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700450 &fake_system_state_, nullptr, std::move(fetcher), false, "");
Darin Petkov1b003102010-11-30 10:18:36 -0800451 ObjectCollectorAction<OmahaResponse> collector_action;
452 BondActions(&action, &collector_action);
453 OmahaResponse response;
454 response.poll_interval = 234;
455 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800456 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700457 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800458 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700459 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800460 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700461 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800462}
463
Alex Deymo30534502015-07-20 15:06:33 -0700464TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700465 FakePrefs fake_prefs;
466 string boot_id;
467 EXPECT_TRUE(utils::GetBootId(&boot_id));
468 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
469 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700470 attempter_.Init();
471 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700472}
473
474TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700475 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700476 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700477
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700478 FakeSystemState fake_system_state;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800479 OmahaRequestAction omaha_request_action(
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700480 &fake_system_state, nullptr, nullptr, false, "");
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700481 EXPECT_EQ(ErrorCode::kOmahaRequestError,
482 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700483 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800484 EXPECT_EQ(
485 ErrorCode::kOmahaResponseHandlerError,
486 GetErrorCodeForAction(&omaha_response_handler_action, ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700487 FilesystemVerifierAction filesystem_verifier_action;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800488 EXPECT_EQ(
489 ErrorCode::kFilesystemVerifierError,
490 GetErrorCodeForAction(&filesystem_verifier_action, ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300491 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700492 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800493 EXPECT_EQ(
494 ErrorCode::kPostinstallRunnerError,
495 GetErrorCodeForAction(&postinstall_runner_action, ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800496 MockAction action_mock;
497 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700498 EXPECT_EQ(ErrorCode::kError,
499 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700500}
501
Darin Petkov36275772010-10-01 11:40:57 -0700502TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700503 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800504 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700505 .WillOnce(Return(false));
506 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700507 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800508 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800509 .WillOnce(
510 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
511 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700512 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700513 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800514 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800515 .WillOnce(
516 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
517 Return(true)));
Darin Petkov36275772010-10-01 11:40:57 -0700518 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700519 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800520 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700521 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700522 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700523}
524
525TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800526 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700527 .WillOnce(Return(false))
Ben Chan672c1f52017-10-23 15:41:39 -0700528 .WillOnce(DoAll(SetArgPointee<1>(-1), Return(true)))
529 .WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800530 .WillOnce(
531 DoAll(SetArgPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
532 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800533 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700534 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800535 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700536 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800537 EXPECT_CALL(*prefs_,
538 SetInt64(kPrefsDeltaUpdateFailures,
539 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
540 for (int i = 0; i < 4; i++)
Darin Petkov36275772010-10-01 11:40:57 -0700541 attempter_.MarkDeltaUpdateFailure();
542}
543
Darin Petkov1b003102010-11-30 10:18:36 -0800544TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
545 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
546 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700547 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800548 .Times(0);
549 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700550 string url1 = "http://url1";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800551 response.packages.push_back({.payload_urls = {url1, "https://url"}});
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700552 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700553 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700554 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800555 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700556 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800557}
558
559TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
560 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700561 EnqueueAction(Pointee(Property(
562 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700563 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700564 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700565 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800566 attempter_.error_event_.reset(new OmahaEvent(
567 OmahaEvent::kTypeUpdateComplete, OmahaEvent::kResultError, err));
Darin Petkov1b003102010-11-30 10:18:36 -0800568 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700569 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800570}
571
Darin Petkove6ef2f82011-03-07 17:31:11 -0800572namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700573// Actions that will be built as part of an update check.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200574vector<string> GetUpdateActionTypes() {
575 return {OmahaRequestAction::StaticType(),
576 OmahaResponseHandlerAction::StaticType(),
577 UpdateBootFlagsAction::StaticType(),
578 OmahaRequestAction::StaticType(),
579 DownloadAction::StaticType(),
580 OmahaRequestAction::StaticType(),
581 FilesystemVerifierAction::StaticType(),
582 PostinstallRunnerAction::StaticType(),
583 OmahaRequestAction::StaticType()};
584}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700585
586// Actions that will be built as part of a user-initiated rollback.
Askar Aitzhan570ca872019-04-24 11:16:12 +0200587vector<string> GetRollbackActionTypes() {
588 return {InstallPlanAction::StaticType(),
589 PostinstallRunnerAction::StaticType()};
590}
Chris Sosa76a29ae2013-07-11 17:59:24 -0700591
Adolfo Victoria497044c2018-07-18 07:51:42 -0700592const StagingSchedule kValidStagingSchedule = {
593 {4, 10}, {10, 40}, {19, 70}, {26, 100}};
594
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700595} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800596
597void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700598 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700599
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700600 // Expect that the device policy is loaded by the |UpdateAttempter| at some
601 // point by calling |RefreshDevicePolicy()|.
Igor9fd76b62017-12-11 15:24:18 +0100602 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Alex Deymo749ecf12014-10-21 20:06:57 -0700603 EXPECT_CALL(*device_policy, LoadPolicy())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800604 .Times(testing::AtLeast(1))
605 .WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100606 attempter_.policy_provider_.reset(
607 new policy::PolicyProvider(std::move(device_policy)));
Alex Deymo749ecf12014-10-21 20:06:57 -0700608
609 {
610 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200611 for (const auto& update_action_type : GetUpdateActionTypes()) {
Alex Deymo749ecf12014-10-21 20:06:57 -0700612 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700613 EnqueueAction(Pointee(
Askar Aitzhan570ca872019-04-24 11:16:12 +0200614 Property(&AbstractAction::Type, update_action_type))));
Alex Deymo749ecf12014-10-21 20:06:57 -0700615 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700616 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700617 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700618
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800619 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700620 loop_.PostTask(FROM_HERE,
621 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
622 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800623}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700624
Darin Petkove6ef2f82011-03-07 17:31:11 -0800625void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700626 EXPECT_EQ(0, attempter_.http_response_code());
627 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700628 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700629 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800630}
631
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800632void UpdateAttempterTest::RollbackTestStart(bool enterprise_rollback,
633 bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700634 // Create a device policy so that we can change settings.
Igor9fd76b62017-12-11 15:24:18 +0100635 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700636 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100637 fake_system_state_.set_device_policy(device_policy.get());
638 if (enterprise_rollback) {
639 // We return an empty owner as this is an enterprise.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800640 EXPECT_CALL(*device_policy, GetOwner(_))
641 .WillRepeatedly(DoAll(SetArgPointee<0>(string("")), Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100642 } else {
643 // We return a fake owner as this is an owned consumer device.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800644 EXPECT_CALL(*device_policy, GetOwner(_))
645 .WillRepeatedly(DoAll(SetArgPointee<0>(string("fake.mail@fake.com")),
646 Return(true)));
Igor9fd76b62017-12-11 15:24:18 +0100647 }
648
649 attempter_.policy_provider_.reset(
650 new policy::PolicyProvider(std::move(device_policy)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700651
Alex Deymo763e7db2015-08-27 21:08:08 -0700652 if (valid_slot) {
653 BootControlInterface::Slot rollback_slot = 1;
654 LOG(INFO) << "Test Mark Bootable: "
655 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700656 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
657 true);
Don Garrett6646b442013-11-13 15:29:11 -0800658 }
659
Chris Sosa28e479c2013-07-12 11:39:53 -0700660 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700661
Chris Sosad38b1132014-03-25 10:43:59 -0700662 // We only allow rollback on devices that are not enterprise enrolled and
663 // which have a valid slot to rollback to.
664 if (!enterprise_rollback && valid_slot) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800665 is_rollback_allowed = true;
Chris Sosa28e479c2013-07-12 11:39:53 -0700666 }
667
Chris Sosa28e479c2013-07-12 11:39:53 -0700668 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700669 InSequence s;
Askar Aitzhan570ca872019-04-24 11:16:12 +0200670 for (const auto& rollback_action_type : GetRollbackActionTypes()) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700671 EXPECT_CALL(*processor_,
Askar Aitzhan570ca872019-04-24 11:16:12 +0200672 EnqueueAction(Pointee(
673 Property(&AbstractAction::Type, rollback_action_type))));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700674 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700675 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700676
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700677 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700678 loop_.PostTask(FROM_HERE,
679 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
680 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700681 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700682 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700683 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700684 }
685}
686
687void UpdateAttempterTest::RollbackTestVerify() {
688 // Verifies the actions that were enqueued.
689 EXPECT_EQ(&attempter_, processor_->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700690 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700691 EXPECT_EQ(0U, attempter_.install_plan_->partitions.size());
692 EXPECT_EQ(attempter_.install_plan_->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700693 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700694}
695
Darin Petkove6ef2f82011-03-07 17:31:11 -0800696TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700697 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700698 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700699}
700
Chris Sosa76a29ae2013-07-11 17:59:24 -0700701TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700702 loop_.PostTask(FROM_HERE,
703 base::Bind(&UpdateAttempterTest::RollbackTestStart,
704 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800705 false,
706 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700707 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700708}
709
Don Garrett6646b442013-11-13 15:29:11 -0800710TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700711 loop_.PostTask(FROM_HERE,
712 base::Bind(&UpdateAttempterTest::RollbackTestStart,
713 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800714 false,
715 false));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700716 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800717}
718
Chris Sosa76a29ae2013-07-11 17:59:24 -0700719TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700720 loop_.PostTask(FROM_HERE,
721 base::Bind(&UpdateAttempterTest::RollbackTestStart,
722 base::Unretained(this),
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800723 true,
724 true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700725 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700726}
727
Thieu Le116fda32011-04-19 11:01:54 -0700728void UpdateAttempterTest::PingOmahaTestStart() {
729 EXPECT_CALL(*processor_,
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700730 EnqueueAction(Pointee(Property(
731 &AbstractAction::Type, OmahaRequestAction::StaticType()))));
Gilad Arnold74b5f552014-10-07 08:17:16 -0700732 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700733 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700734 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700735}
736
737TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700738 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
739 EXPECT_FALSE(attempter_.schedule_updates_called());
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700740 // Disable scheduling of subsequnet checks; we're using the |DefaultPolicy| in
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700741 // testing, which is more permissive than we want to handle here.
742 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700743 loop_.PostTask(FROM_HERE,
744 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
745 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700746 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700747 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700748 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700749}
750
Darin Petkov18c7bce2011-06-16 14:07:00 -0700751TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800752 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700753 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700754 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700755 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700756 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
757 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700758 EXPECT_EQ(
759 static_cast<ErrorCode>(static_cast<int>(kCode) |
760 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
761 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700762}
763
764TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700765 attempter_.install_plan_.reset(new InstallPlan);
766 attempter_.install_plan_->is_resume = true;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800767 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700768 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700769 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700770 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700771 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
772 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700773 EXPECT_EQ(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800774 static_cast<ErrorCode>(static_cast<int>(kCode) |
775 static_cast<int>(ErrorCode::kResumedFlag) |
776 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700777 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700778}
779
David Zeuthen8f191b22013-08-06 12:27:50 -0700780TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
781 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700782 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700783 mock_p2p_manager.fake().SetP2PEnabled(false);
784 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
785 attempter_.UpdateEngineStarted();
786}
787
788TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
789 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700790 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700791 mock_p2p_manager.fake().SetP2PEnabled(true);
792 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
793 attempter_.UpdateEngineStarted();
794}
795
796TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
797 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700798 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700799 mock_p2p_manager.fake().SetP2PEnabled(true);
800 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700801 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700802 attempter_.UpdateEngineStarted();
803}
804
805TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700806 loop_.PostTask(FROM_HERE,
807 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
808 base::Unretained(this)));
809 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700810}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700811
David Zeuthen8f191b22013-08-06 12:27:50 -0700812void UpdateAttempterTest::P2PNotEnabledStart() {
813 // If P2P is not enabled, check that we do not attempt housekeeping
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700814 // and do not convey that P2P is to be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700815 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700816 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700817 mock_p2p_manager.fake().SetP2PEnabled(false);
818 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800819 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700820 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700821 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700822 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700823}
824
825TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700826 loop_.PostTask(FROM_HERE,
827 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
828 base::Unretained(this)));
829 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700830}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700831
David Zeuthen8f191b22013-08-06 12:27:50 -0700832void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700833 // If P2P is enabled, but starting it fails ensure we don't do
834 // any housekeeping and do not convey that P2P should be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700835 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700836 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700837 mock_p2p_manager.fake().SetP2PEnabled(true);
838 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
839 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
840 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800841 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700842 EXPECT_FALSE(actual_using_p2p_for_downloading());
843 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700844 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700845}
846
847TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700848 loop_.PostTask(
849 FROM_HERE,
850 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
851 base::Unretained(this)));
852 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700853}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700854
David Zeuthen8f191b22013-08-06 12:27:50 -0700855void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700856 // If P2P is enabled, starting it works but housekeeping fails, ensure
857 // we do not convey P2P is to be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700858 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700859 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700860 mock_p2p_manager.fake().SetP2PEnabled(true);
861 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
862 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700863 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800864 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700865 EXPECT_FALSE(actual_using_p2p_for_downloading());
866 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700867 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700868}
869
870TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700871 loop_.PostTask(FROM_HERE,
872 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
873 base::Unretained(this)));
874 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700875}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700876
David Zeuthen8f191b22013-08-06 12:27:50 -0700877void UpdateAttempterTest::P2PEnabledStart() {
878 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700879 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700880 // If P2P is enabled and starting it works, check that we performed
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700881 // housekeeping and that we convey P2P should be used.
David Zeuthen8f191b22013-08-06 12:27:50 -0700882 mock_p2p_manager.fake().SetP2PEnabled(true);
883 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
884 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700885 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800886 attempter_.Update("", "", "", "", false, false, 0, false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700887 EXPECT_TRUE(actual_using_p2p_for_downloading());
888 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700889 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700890}
891
892TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700893 loop_.PostTask(FROM_HERE,
894 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
895 base::Unretained(this)));
896 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700897}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700898
David Zeuthen8f191b22013-08-06 12:27:50 -0700899void UpdateAttempterTest::P2PEnabledInteractiveStart() {
900 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700901 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700902 // For an interactive check, if P2P is enabled and starting it
903 // works, check that we performed housekeeping and that we convey
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700904 // P2P should be used for sharing but NOT for downloading.
David Zeuthen8f191b22013-08-06 12:27:50 -0700905 mock_p2p_manager.fake().SetP2PEnabled(true);
906 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
907 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700908 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200909 attempter_.Update("",
910 "",
911 "",
912 "",
913 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800914 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700915 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200916 false,
917 /*interactive=*/true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700918 EXPECT_FALSE(actual_using_p2p_for_downloading());
919 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700920 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700921}
922
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700923TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700924 loop_.PostTask(
925 FROM_HERE,
926 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
927 base::Unretained(this)));
928 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700929}
930
931// Tests that the scatter_factor_in_seconds value is properly fetched
932// from the device policy.
933void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700934 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700935
Igor9fd76b62017-12-11 15:24:18 +0100936 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700937 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100938 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700939
940 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800941 .WillRepeatedly(
942 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700943
Igor9fd76b62017-12-11 15:24:18 +0100944 attempter_.policy_provider_.reset(
945 new policy::PolicyProvider(std::move(device_policy)));
946
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800947 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700948 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
949
Alex Deymo60ca1a72015-06-18 18:19:15 -0700950 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700951}
952
953TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700954 loop_.PostTask(
955 FROM_HERE,
956 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
957 base::Unretained(this)));
958 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700959}
960
961void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
962 // Tests that the scatter_factor_in_seconds value is properly fetched
963 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700964 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800965 FakePrefs fake_prefs;
966 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700967
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700968 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700969
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800970 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700971
Ben Chan9abb7632014-08-07 00:10:53 -0700972 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700973
Igor9fd76b62017-12-11 15:24:18 +0100974 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700975 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +0100976 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700977
978 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800979 .WillRepeatedly(
980 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700981
Igor9fd76b62017-12-11 15:24:18 +0100982 attempter_.policy_provider_.reset(
983 new policy::PolicyProvider(std::move(device_policy)));
984
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800985 attempter_.Update("", "", "", "", false, false, 0, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700986 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
987
988 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800989 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700990
Ben Chan9abb7632014-08-07 00:10:53 -0700991 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800992 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700993 EXPECT_EQ(initial_value - 1, new_value);
994
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700995 EXPECT_TRUE(
996 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700997
998 // However, if the count is already 0, it's not decremented. Test that.
999 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001000 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001001 attempter_.Update("", "", "", "", false, false, 0, false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001002 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
1003 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001004 EXPECT_EQ(initial_value, new_value);
1005
Alex Deymo60ca1a72015-06-18 18:19:15 -07001006 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001007}
1008
Jay Srinivasan08fce042012-06-07 16:31:01 -07001009TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001010 loop_.PostTask(
1011 FROM_HERE,
1012 base::Bind(
1013 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
1014 base::Unretained(this)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001015 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001016}
1017
1018void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
1019 // Tests that no scattering logic is enabled if the update check
1020 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -07001021 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001022 FakePrefs fake_prefs;
1023 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001024
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001025 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001026 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001027
Adolfo Victoriad3a1e352018-07-16 11:40:47 -07001028 EXPECT_TRUE(
1029 fake_prefs.SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001030 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001031
1032 // make sure scatter_factor is non-zero as scattering is disabled
1033 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -07001034 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001035
Igor9fd76b62017-12-11 15:24:18 +01001036 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001037 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Igor9fd76b62017-12-11 15:24:18 +01001038 fake_system_state_.set_device_policy(device_policy.get());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001039
1040 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001041 .WillRepeatedly(
1042 DoAll(SetArgPointee<0>(scatter_factor_in_seconds), Return(true)));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001043
Igor9fd76b62017-12-11 15:24:18 +01001044 attempter_.policy_provider_.reset(
1045 new policy::PolicyProvider(std::move(device_policy)));
1046
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001047 // Trigger an interactive check so we can test that scattering is disabled.
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001048 attempter_.Update("",
1049 "",
1050 "",
1051 "",
1052 false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001053 false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001054 /*rollback_allowed_milestones=*/0,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001055 false,
1056 /*interactive=*/true);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001057 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1058
1059 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001060 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001061 EXPECT_FALSE(
1062 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Adolfo Victoriad3a1e352018-07-16 11:40:47 -07001063 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001064 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1065 EXPECT_FALSE(
1066 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001067 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001068
Alex Deymo60ca1a72015-06-18 18:19:15 -07001069 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -07001070}
1071
Adolfo Victoria497044c2018-07-18 07:51:42 -07001072void UpdateAttempterTest::SetUpStagingTest(const StagingSchedule& schedule,
1073 FakePrefs* prefs) {
1074 attempter_.prefs_ = prefs;
1075 fake_system_state_.set_prefs(prefs);
1076
1077 int64_t initial_value = 8;
1078 EXPECT_TRUE(
1079 prefs->SetInt64(kPrefsWallClockScatteringWaitPeriod, initial_value));
1080 EXPECT_TRUE(prefs->SetInt64(kPrefsUpdateCheckCount, initial_value));
1081 attempter_.scatter_factor_ = TimeDelta::FromSeconds(20);
1082
1083 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1084 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
1085 fake_system_state_.set_device_policy(device_policy.get());
1086 EXPECT_CALL(*device_policy, GetDeviceUpdateStagingSchedule(_))
1087 .WillRepeatedly(DoAll(SetArgPointee<0>(schedule), Return(true)));
1088
1089 attempter_.policy_provider_.reset(
1090 new policy::PolicyProvider(std::move(device_policy)));
1091}
1092
1093TEST_F(UpdateAttempterTest, StagingSetsPrefsAndTurnsOffScattering) {
1094 loop_.PostTask(
1095 FROM_HERE,
1096 base::Bind(
1097 &UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart,
1098 base::Unretained(this)));
1099 loop_.Run();
1100}
1101
1102void UpdateAttempterTest::StagingSetsPrefsAndTurnsOffScatteringStart() {
1103 // Tests that staging sets its prefs properly and turns off scattering.
1104 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1105 FakePrefs fake_prefs;
1106 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1107
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001108 attempter_.Update("", "", "", "", false, false, 0, false, false);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001109 // Check that prefs have the correct values.
1110 int64_t update_count;
1111 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &update_count));
1112 int64_t waiting_time_days;
1113 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsWallClockStagingWaitPeriod,
1114 &waiting_time_days));
1115 EXPECT_GT(waiting_time_days, 0);
1116 // Update count should have been decremented.
1117 EXPECT_EQ(7, update_count);
1118 // Check that Omaha parameters were updated correctly.
1119 EXPECT_TRUE(
1120 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1121 EXPECT_TRUE(
1122 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1123 EXPECT_EQ(waiting_time_days,
1124 attempter_.omaha_request_params_->waiting_period().InDays());
1125 // Check class variables.
1126 EXPECT_EQ(waiting_time_days, attempter_.staging_wait_time_.InDays());
1127 EXPECT_EQ(kValidStagingSchedule, attempter_.staging_schedule_);
1128 // Check that scattering is turned off
1129 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1130 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockScatteringWaitPeriod));
1131
1132 ScheduleQuitMainLoop();
1133}
1134
1135void UpdateAttempterTest::CheckStagingOff() {
1136 // Check that all prefs were removed.
1137 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsUpdateCheckCount));
1138 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockScatteringWaitPeriod));
1139 EXPECT_FALSE(attempter_.prefs_->Exists(kPrefsWallClockStagingWaitPeriod));
1140 // Check that the Omaha parameters have the correct value.
1141 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InDays());
1142 EXPECT_EQ(attempter_.omaha_request_params_->waiting_period(),
1143 attempter_.staging_wait_time_);
1144 EXPECT_FALSE(
1145 attempter_.omaha_request_params_->update_check_count_wait_enabled());
1146 EXPECT_FALSE(
1147 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
1148 // Check that scattering is turned off too.
1149 EXPECT_EQ(0, attempter_.scatter_factor_.InSeconds());
1150}
1151
1152TEST_F(UpdateAttempterTest, StagingOffIfInteractive) {
1153 loop_.PostTask(FROM_HERE,
1154 base::Bind(&UpdateAttempterTest::StagingOffIfInteractiveStart,
1155 base::Unretained(this)));
1156 loop_.Run();
1157}
1158
1159void UpdateAttempterTest::StagingOffIfInteractiveStart() {
1160 // Tests that staging is turned off when an interactive update is requested.
1161 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
1162 FakePrefs fake_prefs;
1163 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1164
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001165 attempter_.Update(
1166 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001167 CheckStagingOff();
1168
1169 ScheduleQuitMainLoop();
1170}
1171
1172TEST_F(UpdateAttempterTest, StagingOffIfOobe) {
1173 loop_.PostTask(FROM_HERE,
1174 base::Bind(&UpdateAttempterTest::StagingOffIfOobeStart,
1175 base::Unretained(this)));
1176 loop_.Run();
1177}
1178
1179void UpdateAttempterTest::StagingOffIfOobeStart() {
1180 // Tests that staging is turned off if OOBE hasn't been completed.
1181 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
1182 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1183 FakePrefs fake_prefs;
1184 SetUpStagingTest(kValidStagingSchedule, &fake_prefs);
1185
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001186 attempter_.Update(
1187 "", "", "", "", false, false, 0, false, /* interactive = */ true);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001188 CheckStagingOff();
1189
1190 ScheduleQuitMainLoop();
1191}
1192
David Zeuthen985b1122013-10-09 12:13:15 -07001193// Checks that we only report daily metrics at most every 24 hours.
1194TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1195 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001196 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -07001197
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001198 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -08001199 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001200
1201 Time epoch = Time::FromInternalValue(0);
1202 fake_clock.SetWallclockTime(epoch);
1203
1204 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1205 // we should report.
1206 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1207 // We should not report again if no time has passed.
1208 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1209
1210 // We should not report if only 10 hours has passed.
1211 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1212 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1213
1214 // We should not report if only 24 hours - 1 sec has passed.
1215 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1216 TimeDelta::FromSeconds(1));
1217 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1218
1219 // We should report if 24 hours has passed.
1220 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1221 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1222
1223 // But then we should not report again..
1224 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1225
1226 // .. until another 24 hours has passed
1227 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1228 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1229 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1230 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1231 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1232
1233 // .. and another 24 hours
1234 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1235 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1236 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1237 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1238 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1239
1240 // If the span between time of reporting and present time is
1241 // negative, we report. This is in order to reset the timestamp and
1242 // avoid an edge condition whereby a distant point in the future is
1243 // in the state variable resulting in us never ever reporting again.
1244 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1245 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1246 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1247
1248 // In this case we should not update until the clock reads 71 + 24 = 95.
1249 // Check that.
1250 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1251 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1252 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1253 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1254 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001255}
1256
David Zeuthen3c55abd2013-10-14 12:48:03 -07001257TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -07001258 FakeClock fake_clock;
1259 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001260 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -07001261 FakePrefs fake_prefs;
1262 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001263 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001264
1265 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001266 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001267
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001268 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -07001269
Sen Jiangaeeb2e02016-06-09 15:00:16 -07001270 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -07001271 EXPECT_EQ(boot_time.ToTimeT(), 42);
1272}
1273
David Pursell02c18642014-11-06 11:26:11 -08001274TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1275 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1276 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1277}
1278
1279TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1280 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001281 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -08001282 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1283}
1284
1285TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1286 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001287 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -08001288 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1289}
1290
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001291TEST_F(UpdateAttempterTest, CheckForUpdateAUDlcTest) {
1292 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1293 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1294
1295 const string dlc_module_id = "a_dlc_module_id";
1296 vector<string> dlc_module_ids = {dlc_module_id};
1297 ON_CALL(mock_dlcservice_, GetInstalled(testing::_))
1298 .WillByDefault(DoAll(testing::SetArgPointee<0>(dlc_module_ids),
1299 testing::Return(true)));
1300
1301 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08001302 EXPECT_EQ(attempter_.dlc_module_ids_.size(), 1);
1303 EXPECT_EQ(attempter_.dlc_module_ids_[0], dlc_module_id);
Xiaochu Liu8ba486f2018-11-06 11:14:10 -08001304}
1305
David Pursell02c18642014-11-06 11:26:11 -08001306TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1307 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001308 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001309 attempter_.CheckForUpdate("", "autest", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001310 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001311}
1312
1313TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
1314 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -07001315 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
Aaron Wood081c0232017-10-19 17:14:58 -07001316 attempter_.CheckForUpdate("", "autest-scheduled", UpdateAttemptFlags::kNone);
Alex Deymoac41a822015-09-15 20:52:53 -07001317 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -08001318}
1319
Xiaochu Liu88d90382018-08-29 16:09:11 -07001320TEST_F(UpdateAttempterTest, CheckForInstallTest) {
1321 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1322 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
1323 attempter_.CheckForInstall({}, "autest");
1324 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1325
1326 attempter_.CheckForInstall({}, "autest-scheduled");
1327 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
1328
1329 attempter_.CheckForInstall({}, "http://omaha.phishing");
1330 EXPECT_EQ("", attempter_.forced_omaha_url());
1331}
1332
Colin Howesac170d92018-11-20 16:29:28 -08001333TEST_F(UpdateAttempterTest, InstallSetsStatusIdle) {
1334 attempter_.CheckForInstall({}, "http://foo.bar");
1335 attempter_.status_ = UpdateStatus::DOWNLOADING;
1336 EXPECT_TRUE(attempter_.is_install_);
1337 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1338 UpdateEngineStatus status;
1339 attempter_.GetStatus(&status);
1340 // Should set status to idle after an install operation.
1341 EXPECT_EQ(UpdateStatus::IDLE, status.status);
1342}
1343
Colin Howes978c1082018-12-03 11:46:12 -08001344TEST_F(UpdateAttempterTest, RollbackAfterInstall) {
1345 attempter_.is_install_ = true;
1346 attempter_.Rollback(false);
1347 EXPECT_FALSE(attempter_.is_install_);
1348}
1349
1350TEST_F(UpdateAttempterTest, UpdateAfterInstall) {
1351 attempter_.is_install_ = true;
1352 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1353 EXPECT_FALSE(attempter_.is_install_);
1354}
1355
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001356TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001357 attempter_.CalculateUpdateParams(
1358 "", "", "", "1234", false, false, 4, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001359 EXPECT_EQ("1234",
1360 fake_system_state_.request_params()->target_version_prefix());
1361
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001362 attempter_.CalculateUpdateParams(
1363 "", "", "", "", false, 4, false, false, false);
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -08001364 EXPECT_TRUE(
1365 fake_system_state_.request_params()->target_version_prefix().empty());
1366}
1367
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001368TEST_F(UpdateAttempterTest, RollbackAllowedSetAndReset) {
1369 attempter_.CalculateUpdateParams("",
1370 "",
1371 "",
1372 "1234",
1373 /*rollback_allowed=*/true,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001374 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001375 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001376 false,
1377 false);
1378 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001379 EXPECT_EQ(4,
1380 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001381
1382 attempter_.CalculateUpdateParams("",
1383 "",
1384 "",
1385 "1234",
1386 /*rollback_allowed=*/false,
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001387 /*rollback_data_save_requested=*/false,
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001388 /*rollback_allowed_milestones=*/4,
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001389 false,
1390 false);
1391 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07001392 EXPECT_EQ(4,
1393 fake_system_state_.request_params()->rollback_allowed_milestones());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001394}
1395
Aaron Wood23bd3392017-10-06 14:48:25 -07001396TEST_F(UpdateAttempterTest, UpdateDeferredByPolicyTest) {
1397 // Construct an OmahaResponseHandlerAction that has processed an InstallPlan,
1398 // but the update is being deferred by the Policy.
Amin Hassani68512d42018-07-31 23:52:33 -07001399 OmahaResponseHandlerAction response_action(&fake_system_state_);
1400 response_action.install_plan_.version = "a.b.c.d";
1401 response_action.install_plan_.system_version = "b.c.d.e";
1402 response_action.install_plan_.payloads.push_back(
Aaron Wood23bd3392017-10-06 14:48:25 -07001403 {.size = 1234ULL, .type = InstallPayloadType::kFull});
Aaron Wood23bd3392017-10-06 14:48:25 -07001404 // Inform the UpdateAttempter that the OmahaResponseHandlerAction has
1405 // completed, with the deferred-update error code.
1406 attempter_.ActionCompleted(
Amin Hassani68512d42018-07-31 23:52:33 -07001407 nullptr, &response_action, ErrorCode::kOmahaUpdateDeferredPerPolicy);
Aaron Wood23bd3392017-10-06 14:48:25 -07001408 {
1409 UpdateEngineStatus status;
1410 attempter_.GetStatus(&status);
1411 EXPECT_EQ(UpdateStatus::UPDATE_AVAILABLE, status.status);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001412 EXPECT_TRUE(attempter_.install_plan_);
1413 EXPECT_EQ(attempter_.install_plan_->version, status.new_version);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001414 EXPECT_EQ(attempter_.install_plan_->payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001415 status.new_size_bytes);
1416 }
1417 // An "error" event should have been created to tell Omaha that the update is
1418 // being deferred.
1419 EXPECT_TRUE(nullptr != attempter_.error_event_);
1420 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
1421 EXPECT_EQ(OmahaEvent::kResultUpdateDeferred, attempter_.error_event_->result);
1422 ErrorCode expected_code = static_cast<ErrorCode>(
1423 static_cast<int>(ErrorCode::kOmahaUpdateDeferredPerPolicy) |
1424 static_cast<int>(ErrorCode::kTestOmahaUrlFlag));
1425 EXPECT_EQ(expected_code, attempter_.error_event_->error_code);
1426 // End the processing
1427 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1428 // Validate the state of the attempter.
1429 {
1430 UpdateEngineStatus status;
1431 attempter_.GetStatus(&status);
1432 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, status.status);
Amin Hassani68512d42018-07-31 23:52:33 -07001433 EXPECT_EQ(response_action.install_plan_.version, status.new_version);
Amin Hassani68512d42018-07-31 23:52:33 -07001434 EXPECT_EQ(response_action.install_plan_.payloads[0].size,
Aaron Wood23bd3392017-10-06 14:48:25 -07001435 status.new_size_bytes);
1436 }
1437}
1438
1439TEST_F(UpdateAttempterTest, UpdateIsNotRunningWhenUpdateAvailable) {
1440 EXPECT_FALSE(attempter_.IsUpdateRunningOrScheduled());
1441 // Verify in-progress update with UPDATE_AVAILABLE is running
1442 attempter_.status_ = UpdateStatus::UPDATE_AVAILABLE;
1443 EXPECT_TRUE(attempter_.IsUpdateRunningOrScheduled());
1444}
1445
Aaron Woodbf5a2522017-10-04 10:58:36 -07001446TEST_F(UpdateAttempterTest, UpdateAttemptFlagsCachedAtUpdateStart) {
1447 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1448
1449 UpdateCheckParams params = {.updates_enabled = true};
1450 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1451
1452 EXPECT_EQ(UpdateAttemptFlags::kFlagRestrictDownload,
1453 attempter_.GetCurrentUpdateAttemptFlags());
1454}
1455
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001456TEST_F(UpdateAttempterTest, RollbackNotAllowed) {
1457 UpdateCheckParams params = {.updates_enabled = true,
1458 .rollback_allowed = false};
1459 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1460 EXPECT_FALSE(fake_system_state_.request_params()->rollback_allowed());
1461}
1462
1463TEST_F(UpdateAttempterTest, RollbackAllowed) {
1464 UpdateCheckParams params = {.updates_enabled = true,
1465 .rollback_allowed = true};
1466 attempter_.OnUpdateScheduled(EvalStatus::kSucceeded, params);
1467 EXPECT_TRUE(fake_system_state_.request_params()->rollback_allowed());
1468}
1469
Aaron Wood081c0232017-10-19 17:14:58 -07001470TEST_F(UpdateAttempterTest, InteractiveUpdateUsesPassedRestrictions) {
1471 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kFlagRestrictDownload);
1472
1473 attempter_.CheckForUpdate("", "", UpdateAttemptFlags::kNone);
1474 EXPECT_EQ(UpdateAttemptFlags::kNone,
1475 attempter_.GetCurrentUpdateAttemptFlags());
1476}
1477
1478TEST_F(UpdateAttempterTest, NonInteractiveUpdateUsesSetRestrictions) {
1479 attempter_.SetUpdateAttemptFlags(UpdateAttemptFlags::kNone);
1480
1481 // This tests that when CheckForUpdate() is called with the non-interactive
1482 // flag set, that it doesn't change the current UpdateAttemptFlags.
1483 attempter_.CheckForUpdate("",
1484 "",
1485 UpdateAttemptFlags::kFlagNonInteractive |
1486 UpdateAttemptFlags::kFlagRestrictDownload);
1487 EXPECT_EQ(UpdateAttemptFlags::kNone,
1488 attempter_.GetCurrentUpdateAttemptFlags());
1489}
1490
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001491void UpdateAttempterTest::ResetRollbackHappenedStart(bool is_consumer,
1492 bool is_policy_loaded,
1493 bool expected_reset) {
1494 EXPECT_CALL(*fake_system_state_.mock_payload_state(), GetRollbackHappened())
1495 .WillRepeatedly(Return(true));
1496 auto mock_policy_provider =
1497 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
1498 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
1499 .WillRepeatedly(Return(is_consumer));
1500 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
1501 .WillRepeatedly(Return(is_policy_loaded));
1502 const policy::MockDevicePolicy device_policy;
1503 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
1504 .WillRepeatedly(ReturnRef(device_policy));
1505 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1506 SetRollbackHappened(false))
1507 .Times(expected_reset ? 1 : 0);
1508 attempter_.policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -08001509 attempter_.Update("", "", "", "", false, false, 0, false, false);
Marton Hunyadye58bddb2018-04-10 20:27:26 +02001510 ScheduleQuitMainLoop();
1511}
1512
1513TEST_F(UpdateAttempterTest, ResetRollbackHappenedOobe) {
1514 loop_.PostTask(FROM_HERE,
1515 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1516 base::Unretained(this),
1517 /*is_consumer=*/false,
1518 /*is_policy_loaded=*/false,
1519 /*expected_reset=*/false));
1520 loop_.Run();
1521}
1522
1523TEST_F(UpdateAttempterTest, ResetRollbackHappenedConsumer) {
1524 loop_.PostTask(FROM_HERE,
1525 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1526 base::Unretained(this),
1527 /*is_consumer=*/true,
1528 /*is_policy_loaded=*/false,
1529 /*expected_reset=*/true));
1530 loop_.Run();
1531}
1532
1533TEST_F(UpdateAttempterTest, ResetRollbackHappenedEnterprise) {
1534 loop_.PostTask(FROM_HERE,
1535 base::Bind(&UpdateAttempterTest::ResetRollbackHappenedStart,
1536 base::Unretained(this),
1537 /*is_consumer=*/false,
1538 /*is_policy_loaded=*/true,
1539 /*expected_reset=*/true));
1540 loop_.Run();
1541}
1542
Marton Hunyady199152d2018-05-07 19:08:48 +02001543TEST_F(UpdateAttempterTest, SetRollbackHappenedRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001544 attempter_.install_plan_.reset(new InstallPlan);
1545 attempter_.install_plan_->is_rollback = true;
Marton Hunyady199152d2018-05-07 19:08:48 +02001546
1547 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1548 SetRollbackHappened(true))
1549 .Times(1);
1550 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1551}
1552
1553TEST_F(UpdateAttempterTest, SetRollbackHappenedNotRollback) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001554 attempter_.install_plan_.reset(new InstallPlan);
1555 attempter_.install_plan_->is_rollback = false;
Marton Hunyady199152d2018-05-07 19:08:48 +02001556
1557 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
1558 SetRollbackHappened(true))
1559 .Times(0);
1560 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1561}
1562
Marton Hunyadya0302682018-05-16 18:52:13 +02001563TEST_F(UpdateAttempterTest, RollbackMetricsRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001564 attempter_.install_plan_.reset(new InstallPlan);
1565 attempter_.install_plan_->is_rollback = true;
1566 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001567
1568 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1569 ReportEnterpriseRollbackMetrics(true, kRollbackVersion))
1570 .Times(1);
1571 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1572}
1573
1574TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackSuccess) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001575 attempter_.install_plan_.reset(new InstallPlan);
1576 attempter_.install_plan_->is_rollback = false;
1577 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001578
1579 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1580 ReportEnterpriseRollbackMetrics(_, _))
1581 .Times(0);
1582 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1583}
1584
1585TEST_F(UpdateAttempterTest, RollbackMetricsRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001586 attempter_.install_plan_.reset(new InstallPlan);
1587 attempter_.install_plan_->is_rollback = true;
1588 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001589
1590 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1591 ReportEnterpriseRollbackMetrics(false, kRollbackVersion))
1592 .Times(1);
1593 MockAction action;
1594 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1595 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1596}
1597
1598TEST_F(UpdateAttempterTest, RollbackMetricsNotRollbackFailure) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001599 attempter_.install_plan_.reset(new InstallPlan);
1600 attempter_.install_plan_->is_rollback = false;
1601 attempter_.install_plan_->version = kRollbackVersion;
Marton Hunyadya0302682018-05-16 18:52:13 +02001602
1603 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1604 ReportEnterpriseRollbackMetrics(_, _))
1605 .Times(0);
1606 MockAction action;
1607 attempter_.CreatePendingErrorEvent(&action, ErrorCode::kRollbackNotPossible);
1608 attempter_.ProcessingDone(nullptr, ErrorCode::kRollbackNotPossible);
1609}
1610
May Lippert60aa3ca2018-08-15 16:55:29 -07001611TEST_F(UpdateAttempterTest, TimeToUpdateAppliedMetricFailure) {
1612 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1613 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1614 .Times(0);
1615 attempter_.ProcessingDone(nullptr, ErrorCode::kOmahaUpdateDeferredPerPolicy);
1616}
1617
1618TEST_F(UpdateAttempterTest, TimeToUpdateAppliedOnNonEnterprise) {
1619 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1620 fake_system_state_.set_device_policy(device_policy.get());
1621 // Make device policy return that this is not enterprise enrolled
1622 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(false));
1623
1624 // Ensure that the metric is not recorded.
1625 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1626 ReportEnterpriseUpdateSeenToDownloadDays(_, _))
1627 .Times(0);
1628 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1629}
1630
1631TEST_F(UpdateAttempterTest,
1632 TimeToUpdateAppliedWithTimeRestrictionMetricSuccess) {
1633 constexpr int kDaysToUpdate = 15;
1634 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1635 fake_system_state_.set_device_policy(device_policy.get());
1636 // Make device policy return that this is enterprise enrolled
1637 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1638 // Pretend that there's a time restriction policy in place
1639 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1640 .WillOnce(Return(true));
1641
1642 FakePrefs fake_prefs;
1643 Time update_first_seen_at = Time::Now();
1644 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1645 update_first_seen_at.ToInternalValue());
1646
1647 FakeClock fake_clock;
1648 Time update_finished_at =
1649 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1650 fake_clock.SetWallclockTime(update_finished_at);
1651
1652 fake_system_state_.set_clock(&fake_clock);
1653 fake_system_state_.set_prefs(&fake_prefs);
1654
1655 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1656 ReportEnterpriseUpdateSeenToDownloadDays(true, kDaysToUpdate))
1657 .Times(1);
1658 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1659}
1660
1661TEST_F(UpdateAttempterTest,
1662 TimeToUpdateAppliedWithoutTimeRestrictionMetricSuccess) {
1663 constexpr int kDaysToUpdate = 15;
1664 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1665 fake_system_state_.set_device_policy(device_policy.get());
1666 // Make device policy return that this is enterprise enrolled
1667 EXPECT_CALL(*device_policy, IsEnterpriseEnrolled()).WillOnce(Return(true));
1668 // Pretend that there's no time restriction policy in place
1669 EXPECT_CALL(*device_policy, GetDisallowedTimeIntervals(_))
1670 .WillOnce(Return(false));
1671
1672 FakePrefs fake_prefs;
1673 Time update_first_seen_at = Time::Now();
1674 fake_prefs.SetInt64(kPrefsUpdateFirstSeenAt,
1675 update_first_seen_at.ToInternalValue());
1676
1677 FakeClock fake_clock;
1678 Time update_finished_at =
1679 update_first_seen_at + TimeDelta::FromDays(kDaysToUpdate);
1680 fake_clock.SetWallclockTime(update_finished_at);
1681
1682 fake_system_state_.set_clock(&fake_clock);
1683 fake_system_state_.set_prefs(&fake_prefs);
1684
1685 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
1686 ReportEnterpriseUpdateSeenToDownloadDays(false, kDaysToUpdate))
1687 .Times(1);
1688 attempter_.ProcessingDone(nullptr, ErrorCode::kSuccess);
1689}
1690
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001691void UpdateAttempterTest::UpdateToQuickFixBuildStart(bool set_token) {
1692 // Tests that checks if |device_quick_fix_build_token| arrives when
1693 // policy is set and the device is enterprise enrolled based on |set_token|.
1694 string token = set_token ? "some_token" : "";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001695 auto device_policy = std::make_unique<policy::MockDevicePolicy>();
1696 fake_system_state_.set_device_policy(device_policy.get());
Askar Aitzhan570ca872019-04-24 11:16:12 +02001697 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001698
1699 if (set_token)
1700 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
1701 .WillOnce(DoAll(SetArgPointee<0>(token), Return(true)));
1702 else
1703 EXPECT_CALL(*device_policy, GetDeviceQuickFixBuildToken(_))
1704 .WillOnce(Return(false));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001705 attempter_.policy_provider_.reset(
1706 new policy::PolicyProvider(std::move(device_policy)));
1707 attempter_.Update("", "", "", "", false, false, 0, false, false);
1708
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001709 EXPECT_EQ(token, attempter_.omaha_request_params_->autoupdate_token());
Askar Aitzhan570ca872019-04-24 11:16:12 +02001710 ScheduleQuitMainLoop();
1711}
1712
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001713TEST_F(UpdateAttempterTest,
1714 QuickFixTokenWhenDeviceIsEnterpriseEnrolledAndPolicyIsSet) {
Askar Aitzhan570ca872019-04-24 11:16:12 +02001715 loop_.PostTask(FROM_HERE,
1716 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
Amr Aboelkher21ac9962019-05-15 14:50:05 +02001717 base::Unretained(this),
1718 /*set_token=*/true));
1719 loop_.Run();
1720}
1721
1722TEST_F(UpdateAttempterTest, EmptyQuickFixToken) {
1723 loop_.PostTask(FROM_HERE,
1724 base::Bind(&UpdateAttempterTest::UpdateToQuickFixBuildStart,
1725 base::Unretained(this),
1726 /*set_token=*/false));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001727 loop_.Run();
1728}
1729
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001730} // namespace chromeos_update_engine