blob: 93bcc5cb519c94385fa50266c2389127909f031e [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/update_attempter.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Ben Chan02f7c1d2014-10-18 15:18:02 -070021#include <memory>
22
Ben Chan06c76a42014-09-05 08:21:06 -070023#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070024#include <base/message_loop/message_loop.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/bind_lambda.h>
26#include <brillo/make_unique_ptr.h>
27#include <brillo/message_loops/base_message_loop.h>
28#include <brillo/message_loops/message_loop.h>
29#include <brillo/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020031#include <policy/libpolicy.h>
32#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033
Sen Jiangc92195c2016-06-13 15:48:44 -070034#if USE_LIBCROS
Daniel Erate5f6f252017-04-20 12:09:58 -060035#include "network_proxy/dbus-proxies.h"
36#include "network_proxy/dbus-proxy-mocks.h"
Sen Jiangc92195c2016-06-13 15:48:44 -070037#endif // USE_LIBCROS
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/fake_clock.h"
39#include "update_engine/common/fake_prefs.h"
Alex Deymo14fd1ec2016-02-24 22:03:57 -080040#include "update_engine/common/mock_action.h"
41#include "update_engine/common/mock_action_processor.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080042#include "update_engine/common/mock_http_fetcher.h"
43#include "update_engine/common/mock_prefs.h"
44#include "update_engine/common/platform_constants.h"
45#include "update_engine/common/prefs.h"
46#include "update_engine/common/test_utils.h"
47#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070048#include "update_engine/fake_system_state.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070049#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050#include "update_engine/mock_payload_state.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"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070055
Daniel Erate5f6f252017-04-20 12:09:58 -060056namespace org {
57namespace chromium {
58class NetworkProxyServiceInterfaceProxyMock;
59} // namespace chromium
60} // namespace org
61
David Zeuthen985b1122013-10-09 12:13:15 -070062using base::Time;
63using base::TimeDelta;
Daniel Erate5f6f252017-04-20 12:09:58 -060064using org::chromium::NetworkProxyServiceInterfaceProxyInterface;
65using org::chromium::NetworkProxyServiceInterfaceProxyMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070066using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070067using std::unique_ptr;
Darin Petkov36275772010-10-01 11:40:57 -070068using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070069using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070070using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080071using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070072using testing::Property;
73using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070074using testing::ReturnPointee;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080075using testing::SaveArg;
Darin Petkov36275772010-10-01 11:40:57 -070076using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070077using testing::_;
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -070078using update_engine::UpdateStatus;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070079
80namespace chromeos_update_engine {
81
82// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070083// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070084// methods.
85class UpdateAttempterUnderTest : public UpdateAttempter {
86 public:
Daniel Erate5f6f252017-04-20 12:09:58 -060087 UpdateAttempterUnderTest(
88 SystemState* system_state,
89 NetworkProxyServiceInterfaceProxyInterface* network_proxy_service_proxy)
90 : UpdateAttempter(system_state, nullptr, network_proxy_service_proxy) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070091
92 // Wrap the update scheduling method, allowing us to opt out of scheduled
93 // updates for testing purposes.
94 void ScheduleUpdates() override {
95 schedule_updates_called_ = true;
96 if (do_schedule_updates_) {
97 UpdateAttempter::ScheduleUpdates();
98 } else {
99 LOG(INFO) << "[TEST] Update scheduling disabled.";
100 }
101 }
102 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
103 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
104
105 // Indicates whether ScheduleUpdates() was called.
106 bool schedule_updates_called() const { return schedule_updates_called_; }
107
David Pursell02c18642014-11-06 11:26:11 -0800108 // Need to expose forced_omaha_url_ so we can test it.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700109 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -0800110
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700111 private:
112 bool schedule_updates_called_ = false;
113 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700114};
115
116class UpdateAttempterTest : public ::testing::Test {
117 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700118 UpdateAttempterTest()
Daniel Erate5f6f252017-04-20 12:09:58 -0600119 : certificate_checker_(fake_system_state_.mock_prefs(),
Alex Deymo33e91e72015-12-01 18:26:08 -0300120 &openssl_wrapper_) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700121 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700122 fake_system_state_.set_connection_manager(&mock_connection_manager);
123 fake_system_state_.set_update_attempter(&attempter_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700124 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700125
Alex Deymo33e91e72015-12-01 18:26:08 -0300126 certificate_checker_.Init();
127
Sen Jiange67bb5b2016-06-20 15:53:56 -0700128 // Finish initializing the attempter.
Gilad Arnold1f847232014-04-07 12:07:49 -0700129 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -0700130 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700131
Alex Deymo610277e2014-11-11 21:18:11 -0800132 void SetUp() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700133 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700134 EXPECT_EQ(0, attempter_.http_response_code_);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700135 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700136 EXPECT_EQ(0.0, attempter_.download_progress_);
137 EXPECT_EQ(0, attempter_.last_checked_time_);
138 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700139 EXPECT_EQ(0, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800140 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700141 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700142 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700143
144 // Set up store/load semantics of P2P properties via the mock PayloadState.
145 actual_using_p2p_for_downloading_ = false;
146 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
147 SetUsingP2PForDownloading(_))
148 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
149 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
150 GetUsingP2PForDownloading())
151 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
152 actual_using_p2p_for_sharing_ = false;
153 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
154 SetUsingP2PForSharing(_))
155 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
156 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
157 GetUsingP2PForDownloading())
158 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700159 }
160
Alex Deymo60ca1a72015-06-18 18:19:15 -0700161 public:
162 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200163
Alex Deymo60ca1a72015-06-18 18:19:15 -0700164 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800165 void UpdateTestStart();
166 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700167 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700168 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700169 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700170 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700171 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700172 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700173 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700174 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700175 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700176 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700177 void P2PEnabledHousekeepingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700178
Gilad Arnold74b5f552014-10-07 08:17:16 -0700179 bool actual_using_p2p_for_downloading() {
180 return actual_using_p2p_for_downloading_;
181 }
182 bool actual_using_p2p_for_sharing() {
183 return actual_using_p2p_for_sharing_;
184 }
185
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700186 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700187 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700188
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700189 FakeSystemState fake_system_state_;
Sen Jiangc92195c2016-06-13 15:48:44 -0700190#if USE_LIBCROS
Daniel Erate5f6f252017-04-20 12:09:58 -0600191 NetworkProxyServiceInterfaceProxyMock network_proxy_service_proxy_mock_;
192 UpdateAttempterUnderTest attempter_{&fake_system_state_,
193 &network_proxy_service_proxy_mock_};
Sen Jiangc92195c2016-06-13 15:48:44 -0700194#else
195 UpdateAttempterUnderTest attempter_{&fake_system_state_, nullptr};
196#endif // USE_LIBCROS
Alex Deymo33e91e72015-12-01 18:26:08 -0300197 OpenSSLWrapper openssl_wrapper_;
198 CertificateChecker certificate_checker_;
Alex Deymo30534502015-07-20 15:06:33 -0700199
Alex Deymo8427b4a2014-11-05 14:00:32 -0800200 NiceMock<MockActionProcessor>* processor_;
201 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800202 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700203
Gilad Arnold74b5f552014-10-07 08:17:16 -0700204 bool actual_using_p2p_for_downloading_;
205 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700206};
207
Alex Deymo60ca1a72015-06-18 18:19:15 -0700208void UpdateAttempterTest::ScheduleQuitMainLoop() {
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700209 loop_.PostTask(
210 FROM_HERE,
211 base::Bind([](brillo::BaseMessageLoop* loop) { loop->BreakLoop(); },
212 base::Unretained(&loop_)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700213}
214
Darin Petkov1b003102010-11-30 10:18:36 -0800215TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700216 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800217 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Deymo1b3556c2016-02-03 09:54:02 -0800218 DownloadAction action(prefs_, nullptr, nullptr, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800219 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700220 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800221 EXPECT_EQ(503, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700222 EXPECT_EQ(UpdateStatus::FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700223 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800224}
225
226TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800227 MockAction action;
228 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700229 attempter_.status_ = UpdateStatus::DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800230 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800231 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700232 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
233 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800234}
235
236TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700237 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800238 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700239 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300240 std::move(fetcher), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800241 ObjectCollectorAction<OmahaResponse> collector_action;
242 BondActions(&action, &collector_action);
243 OmahaResponse response;
244 response.poll_interval = 234;
245 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800246 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700247 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800248 EXPECT_EQ(500, attempter_.http_response_code());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700249 EXPECT_EQ(UpdateStatus::IDLE, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800250 EXPECT_EQ(234U, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700251 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800252}
253
Alex Deymo30534502015-07-20 15:06:33 -0700254TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Alex Deymo906191f2015-10-12 12:22:44 -0700255 FakePrefs fake_prefs;
256 string boot_id;
257 EXPECT_TRUE(utils::GetBootId(&boot_id));
258 fake_prefs.SetString(kPrefsUpdateCompletedOnBootId, boot_id);
259 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700260 attempter_.Init();
261 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700262}
263
264TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700265 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
266 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700267 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700268 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700269
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700270 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700271 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
272 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700273 EXPECT_EQ(ErrorCode::kOmahaRequestError,
274 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700275 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700276 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700277 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700278 ErrorCode::kError));
Sen Jiange6e4bb92016-04-05 14:59:12 -0700279 FilesystemVerifierAction filesystem_verifier_action;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700280 EXPECT_EQ(ErrorCode::kFilesystemVerifierError,
281 GetErrorCodeForAction(&filesystem_verifier_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700282 ErrorCode::kError));
Alex Deymob15a0b82015-11-25 20:30:40 -0300283 PostinstallRunnerAction postinstall_runner_action(
Alex Deymofb905d92016-06-03 19:26:58 -0700284 fake_system_state.fake_boot_control(), fake_system_state.fake_hardware());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700285 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700286 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700287 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800288 MockAction action_mock;
289 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700290 EXPECT_EQ(ErrorCode::kError,
291 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700292}
293
Darin Petkov36275772010-10-01 11:40:57 -0700294TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700295 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700297 .WillOnce(Return(false));
298 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700299 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800300 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700301 .WillOnce(DoAll(
302 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
303 Return(true)));
304 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700305 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800306 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700307 .WillOnce(DoAll(
308 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
309 Return(true)));
310 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700311 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800312 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700313 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700314 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700315}
316
317TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800318 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700319 .WillOnce(Return(false))
320 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
321 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
322 .WillOnce(DoAll(
323 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
324 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800325 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700326 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800327 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700328 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800329 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700330 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700331 for (int i = 0; i < 4; i ++)
332 attempter_.MarkDeltaUpdateFailure();
333}
334
Darin Petkov1b003102010-11-30 10:18:36 -0800335TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
336 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
337 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700338 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800339 .Times(0);
340 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700341 string url1 = "http://url1";
342 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800343 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700344 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700345 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700346 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800347 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700348 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800349}
350
351TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
352 EXPECT_CALL(*processor_,
353 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700354 OmahaRequestAction::StaticType())));
355 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700356 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700357 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800358 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
359 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800360 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800361 attempter_.ScheduleErrorEventAction();
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700362 EXPECT_EQ(UpdateStatus::REPORTING_ERROR_EVENT, attempter_.status());
Darin Petkov1b003102010-11-30 10:18:36 -0800363}
364
Darin Petkove6ef2f82011-03-07 17:31:11 -0800365namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700366// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700367const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800368 OmahaRequestAction::StaticType(),
369 OmahaResponseHandlerAction::StaticType(),
Darin Petkove6ef2f82011-03-07 17:31:11 -0800370 OmahaRequestAction::StaticType(),
371 DownloadAction::StaticType(),
372 OmahaRequestAction::StaticType(),
Allie Woodeb9e6d82015-04-17 13:55:30 -0700373 FilesystemVerifierAction::StaticType(),
Darin Petkove6ef2f82011-03-07 17:31:11 -0800374 PostinstallRunnerAction::StaticType(),
375 OmahaRequestAction::StaticType()
376};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700377
378// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700379const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700380 InstallPlanAction::StaticType(),
381 PostinstallRunnerAction::StaticType(),
382};
383
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700384} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800385
386void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700387 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700388
389 // Expect that the device policy is loaded by the UpdateAttempter at some
390 // point by calling RefreshDevicePolicy.
391 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
392 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
393 EXPECT_CALL(*device_policy, LoadPolicy())
394 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
395
396 {
397 InSequence s;
398 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
399 EXPECT_CALL(*processor_,
400 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700401 kUpdateActionTypes[i])));
Alex Deymo749ecf12014-10-21 20:06:57 -0700402 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700403 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700404 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700405
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700406 attempter_.Update("", "", "", "", false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700407 loop_.PostTask(FROM_HERE,
408 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
409 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800410}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700411
Darin Petkove6ef2f82011-03-07 17:31:11 -0800412void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700413 EXPECT_EQ(0, attempter_.http_response_code());
414 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700415 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
416 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
417 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700418 }
419 EXPECT_EQ(attempter_.response_handler_action_.get(),
420 attempter_.actions_[1].get());
Sen Jiangfef85fd2016-03-25 15:32:49 -0700421 AbstractAction* action_3 = attempter_.actions_[3].get();
422 ASSERT_NE(nullptr, action_3);
423 ASSERT_EQ(DownloadAction::StaticType(), action_3->Type());
424 DownloadAction* download_action = static_cast<DownloadAction*>(action_3);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700425 EXPECT_EQ(&attempter_, download_action->delegate());
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700426 EXPECT_EQ(UpdateStatus::CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700427 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800428}
429
Chris Sosa28e479c2013-07-12 11:39:53 -0700430void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700431 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700432 // Create a device policy so that we can change settings.
433 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
434 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
435
436 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700437 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700438
Alex Deymo763e7db2015-08-27 21:08:08 -0700439 if (valid_slot) {
440 BootControlInterface::Slot rollback_slot = 1;
441 LOG(INFO) << "Test Mark Bootable: "
442 << BootControlInterface::SlotName(rollback_slot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700443 fake_system_state_.fake_boot_control()->SetSlotBootable(rollback_slot,
444 true);
Don Garrett6646b442013-11-13 15:29:11 -0800445 }
446
Chris Sosa28e479c2013-07-12 11:39:53 -0700447 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700448
Chris Sosad38b1132014-03-25 10:43:59 -0700449 // We only allow rollback on devices that are not enterprise enrolled and
450 // which have a valid slot to rollback to.
451 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700452 is_rollback_allowed = true;
453 }
454
Don Garrett6646b442013-11-13 15:29:11 -0800455 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700456 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800457 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700458 DoAll(SetArgumentPointee<0>(string("")),
Chris Sosa28e479c2013-07-12 11:39:53 -0700459 Return(true)));
460 } else {
461 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800462 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700463 DoAll(SetArgumentPointee<0>(string("fake.mail@fake.com")),
Chris Sosa76a29ae2013-07-11 17:59:24 -0700464 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700465 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700466
Chris Sosa28e479c2013-07-12 11:39:53 -0700467 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700468 InSequence s;
469 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
470 EXPECT_CALL(*processor_,
471 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700472 kRollbackActionTypes[i])));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700473 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700474 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700475
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700476 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700477 loop_.PostTask(FROM_HERE,
478 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
479 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700480 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700481 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700482 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700483 }
484}
485
486void UpdateAttempterTest::RollbackTestVerify() {
487 // Verifies the actions that were enqueued.
488 EXPECT_EQ(&attempter_, processor_->delegate());
489 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
490 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
491 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
492 }
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700493 EXPECT_EQ(UpdateStatus::ATTEMPTING_ROLLBACK, attempter_.status());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800494 AbstractAction* action_0 = attempter_.actions_[0].get();
495 ASSERT_NE(nullptr, action_0);
496 ASSERT_EQ(InstallPlanAction::StaticType(), action_0->Type());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700497 InstallPlanAction* install_plan_action =
Alex Deymo80f70ff2016-02-10 16:08:11 -0800498 static_cast<InstallPlanAction*>(action_0);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700499 InstallPlan* install_plan = install_plan_action->install_plan();
Alex Deymo80f70ff2016-02-10 16:08:11 -0800500 EXPECT_EQ(0U, install_plan->partitions.size());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700501 EXPECT_EQ(install_plan->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700502 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700503}
504
Darin Petkove6ef2f82011-03-07 17:31:11 -0800505TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700506 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700507 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700508}
509
Chris Sosa76a29ae2013-07-11 17:59:24 -0700510TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700511 loop_.PostTask(FROM_HERE,
512 base::Bind(&UpdateAttempterTest::RollbackTestStart,
513 base::Unretained(this),
514 false, true));
515 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700516}
517
Don Garrett6646b442013-11-13 15:29:11 -0800518TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700519 loop_.PostTask(FROM_HERE,
520 base::Bind(&UpdateAttempterTest::RollbackTestStart,
521 base::Unretained(this),
522 false, false));
523 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800524}
525
Chris Sosa76a29ae2013-07-11 17:59:24 -0700526TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700527 loop_.PostTask(FROM_HERE,
528 base::Bind(&UpdateAttempterTest::RollbackTestStart,
529 base::Unretained(this),
530 true, true));
531 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700532}
533
Thieu Le116fda32011-04-19 11:01:54 -0700534void UpdateAttempterTest::PingOmahaTestStart() {
535 EXPECT_CALL(*processor_,
536 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700537 OmahaRequestAction::StaticType())));
538 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700539 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700540 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700541}
542
543TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700544 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
545 EXPECT_FALSE(attempter_.schedule_updates_called());
546 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
547 // testing, which is more permissive than we want to handle here.
548 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700549 loop_.PostTask(FROM_HERE,
550 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
551 base::Unretained(this)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700552 brillo::MessageLoopRunMaxIterations(&loop_, 100);
Christopher Wiley6b6cc1b2015-10-05 17:50:07 -0700553 EXPECT_EQ(UpdateStatus::UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700554 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700555}
556
Darin Petkov18c7bce2011-06-16 14:07:00 -0700557TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800558 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700559 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700560 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700561 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700562 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
563 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700564 EXPECT_EQ(
565 static_cast<ErrorCode>(static_cast<int>(kCode) |
566 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
567 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700568}
569
570TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
571 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700572 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700573 response_action->install_plan_.is_resume = true;
574 attempter_.response_handler_action_.reset(response_action);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800575 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700576 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700577 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700578 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700579 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
580 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700581 EXPECT_EQ(
582 static_cast<ErrorCode>(
583 static_cast<int>(kCode) |
584 static_cast<int>(ErrorCode::kResumedFlag) |
585 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
586 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700587}
588
David Zeuthen8f191b22013-08-06 12:27:50 -0700589TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
590 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700591 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700592 mock_p2p_manager.fake().SetP2PEnabled(false);
593 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
594 attempter_.UpdateEngineStarted();
595}
596
597TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
598 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700599 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700600 mock_p2p_manager.fake().SetP2PEnabled(true);
601 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
602 attempter_.UpdateEngineStarted();
603}
604
605TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
606 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700607 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700608 mock_p2p_manager.fake().SetP2PEnabled(true);
609 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700610 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700611 attempter_.UpdateEngineStarted();
612}
613
614TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700615 loop_.PostTask(FROM_HERE,
616 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
617 base::Unretained(this)));
618 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700619}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700620
David Zeuthen8f191b22013-08-06 12:27:50 -0700621void UpdateAttempterTest::P2PNotEnabledStart() {
622 // If P2P is not enabled, check that we do not attempt housekeeping
623 // and do not convey that p2p is to be used.
624 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700625 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700626 mock_p2p_manager.fake().SetP2PEnabled(false);
627 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700628 attempter_.Update("", "", "", "", false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700629 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700630 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700631 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700632}
633
634TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700635 loop_.PostTask(FROM_HERE,
636 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
637 base::Unretained(this)));
638 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700639}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700640
David Zeuthen8f191b22013-08-06 12:27:50 -0700641void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
642 // If p2p is enabled, but starting it fails ensure we don't do
643 // any housekeeping and do not convey that p2p should be used.
644 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700645 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700646 mock_p2p_manager.fake().SetP2PEnabled(true);
647 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
648 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
649 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700650 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700651 EXPECT_FALSE(actual_using_p2p_for_downloading());
652 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700653 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700654}
655
656TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700657 loop_.PostTask(
658 FROM_HERE,
659 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
660 base::Unretained(this)));
661 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700662}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700663
David Zeuthen8f191b22013-08-06 12:27:50 -0700664void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
665 // If p2p is enabled, starting it works but housekeeping fails, ensure
666 // we do not convey p2p is to be used.
667 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700668 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700669 mock_p2p_manager.fake().SetP2PEnabled(true);
670 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
671 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700672 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700673 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700674 EXPECT_FALSE(actual_using_p2p_for_downloading());
675 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700676 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700677}
678
679TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700680 loop_.PostTask(FROM_HERE,
681 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
682 base::Unretained(this)));
683 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700684}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700685
David Zeuthen8f191b22013-08-06 12:27:50 -0700686void UpdateAttempterTest::P2PEnabledStart() {
687 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700688 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700689 // If P2P is enabled and starting it works, check that we performed
690 // housekeeping and that we convey p2p should be used.
691 mock_p2p_manager.fake().SetP2PEnabled(true);
692 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
693 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700694 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700695 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700696 EXPECT_TRUE(actual_using_p2p_for_downloading());
697 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700698 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700699}
700
701TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700702 loop_.PostTask(FROM_HERE,
703 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
704 base::Unretained(this)));
705 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700706}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700707
David Zeuthen8f191b22013-08-06 12:27:50 -0700708void UpdateAttempterTest::P2PEnabledInteractiveStart() {
709 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700710 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700711 // For an interactive check, if P2P is enabled and starting it
712 // works, check that we performed housekeeping and that we convey
713 // p2p should be used for sharing but NOT for downloading.
714 mock_p2p_manager.fake().SetP2PEnabled(true);
715 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
716 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700717 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700718 attempter_.Update("", "", "", "", false, true /* interactive */);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700719 EXPECT_FALSE(actual_using_p2p_for_downloading());
720 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700721 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700722}
723
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700724TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700725 loop_.PostTask(
726 FROM_HERE,
727 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
728 base::Unretained(this)));
729 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700730}
731
732// Tests that the scatter_factor_in_seconds value is properly fetched
733// from the device policy.
734void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700735 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700736
737 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
738 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
739
740 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700741 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700742
743 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
744 .WillRepeatedly(DoAll(
745 SetArgumentPointee<0>(scatter_factor_in_seconds),
746 Return(true)));
747
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700748 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700749 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
750
Alex Deymo60ca1a72015-06-18 18:19:15 -0700751 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700752}
753
754TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700755 loop_.PostTask(
756 FROM_HERE,
757 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
758 base::Unretained(this)));
759 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700760}
761
762void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
763 // Tests that the scatter_factor_in_seconds value is properly fetched
764 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700765 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800766 FakePrefs fake_prefs;
767 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700768
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700769 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700770
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800771 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700772
Ben Chan9abb7632014-08-07 00:10:53 -0700773 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700774
775 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
776 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
777
778 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700779 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700780
781 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
782 .WillRepeatedly(DoAll(
783 SetArgumentPointee<0>(scatter_factor_in_seconds),
784 Return(true)));
785
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700786 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700787 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
788
789 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800790 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700791
Ben Chan9abb7632014-08-07 00:10:53 -0700792 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800793 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700794 EXPECT_EQ(initial_value - 1, new_value);
795
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700796 EXPECT_TRUE(
797 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700798
799 // However, if the count is already 0, it's not decremented. Test that.
800 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800801 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700802 attempter_.Update("", "", "", "", false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800803 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
804 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700805 EXPECT_EQ(initial_value, new_value);
806
Alex Deymo60ca1a72015-06-18 18:19:15 -0700807 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700808}
809
Jay Srinivasan08fce042012-06-07 16:31:01 -0700810TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700811 loop_.PostTask(FROM_HERE, base::Bind(
812 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
813 base::Unretained(this)));
814 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700815}
816
817void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
818 // Tests that no scattering logic is enabled if the update check
819 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700820 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800821 FakePrefs fake_prefs;
822 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700823
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700824 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800825 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700826
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800827 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
828 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700829
830 // make sure scatter_factor is non-zero as scattering is disabled
831 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700832 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700833
834 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
835 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
836
837 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700838 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700839
840 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
841 .WillRepeatedly(DoAll(
842 SetArgumentPointee<0>(scatter_factor_in_seconds),
843 Return(true)));
844
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800845 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700846 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700847 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
848
849 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700850 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700851 EXPECT_FALSE(
852 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800853 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700854 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
855 EXPECT_FALSE(
856 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800857 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700858
Alex Deymo60ca1a72015-06-18 18:19:15 -0700859 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700860}
861
David Zeuthen985b1122013-10-09 12:13:15 -0700862// Checks that we only report daily metrics at most every 24 hours.
863TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
864 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800865 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -0700866
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700867 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800868 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700869
870 Time epoch = Time::FromInternalValue(0);
871 fake_clock.SetWallclockTime(epoch);
872
873 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
874 // we should report.
875 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
876 // We should not report again if no time has passed.
877 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
878
879 // We should not report if only 10 hours has passed.
880 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
881 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
882
883 // We should not report if only 24 hours - 1 sec has passed.
884 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
885 TimeDelta::FromSeconds(1));
886 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
887
888 // We should report if 24 hours has passed.
889 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
890 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
891
892 // But then we should not report again..
893 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
894
895 // .. until another 24 hours has passed
896 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
897 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
898 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
899 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
900 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
901
902 // .. and another 24 hours
903 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
904 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
905 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
906 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
907 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
908
909 // If the span between time of reporting and present time is
910 // negative, we report. This is in order to reset the timestamp and
911 // avoid an edge condition whereby a distant point in the future is
912 // in the state variable resulting in us never ever reporting again.
913 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
914 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
915 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
916
917 // In this case we should not update until the clock reads 71 + 24 = 95.
918 // Check that.
919 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
920 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
921 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
922 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
923 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -0700924}
925
David Zeuthen3c55abd2013-10-14 12:48:03 -0700926TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
David Zeuthen3c55abd2013-10-14 12:48:03 -0700927 FakeClock fake_clock;
928 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700929 fake_system_state_.set_clock(&fake_clock);
Alex Deymo906191f2015-10-12 12:22:44 -0700930 FakePrefs fake_prefs;
931 fake_system_state_.set_prefs(&fake_prefs);
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700932 attempter_.Init();
David Zeuthen3c55abd2013-10-14 12:48:03 -0700933
934 Time boot_time;
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700935 EXPECT_FALSE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -0700936
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700937 attempter_.WriteUpdateCompletedMarker();
David Zeuthen3c55abd2013-10-14 12:48:03 -0700938
Sen Jiangaeeb2e02016-06-09 15:00:16 -0700939 EXPECT_TRUE(attempter_.GetBootTimeAtUpdate(&boot_time));
David Zeuthen3c55abd2013-10-14 12:48:03 -0700940 EXPECT_EQ(boot_time.ToTimeT(), 42);
941}
942
David Pursell02c18642014-11-06 11:26:11 -0800943TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
944 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
945 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
946}
947
948TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
949 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -0700950 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(true);
David Pursell02c18642014-11-06 11:26:11 -0800951 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
952}
953
954TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
955 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -0700956 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -0800957 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
958}
959
960TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
961 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -0700962 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -0800963 attempter_.CheckForUpdate("", "autest", true);
Alex Deymoac41a822015-09-15 20:52:53 -0700964 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -0800965}
966
967TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
968 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
Sen Jiange67bb5b2016-06-20 15:53:56 -0700969 fake_system_state_.fake_hardware()->SetAreDevFeaturesEnabled(false);
David Pursell02c18642014-11-06 11:26:11 -0800970 attempter_.CheckForUpdate("", "autest-scheduled", true);
Alex Deymoac41a822015-09-15 20:52:53 -0700971 EXPECT_EQ(constants::kOmahaDefaultAUTestURL, attempter_.forced_omaha_url());
David Pursell02c18642014-11-06 11:26:11 -0800972}
973
Xiyuan Xiac0e8f9a2017-02-22 13:19:35 -0800974TEST_F(UpdateAttempterTest, TargetVersionPrefixSetAndReset) {
975 attempter_.CalculateUpdateParams("", "", "", "1234", false, false);
976 EXPECT_EQ("1234",
977 fake_system_state_.request_params()->target_version_prefix());
978
979 attempter_.CalculateUpdateParams("", "", "", "", false, false);
980 EXPECT_TRUE(
981 fake_system_state_.request_params()->target_version_prefix().empty());
982}
983
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700984} // namespace chromeos_update_engine