blob: 19f208198e797a47e786627e50e1375c366d8caf [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovf42cc1c2010-09-01 09:03:02 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo2c0db7b2014-11-04 12:23:39 -08005#include "update_engine/update_attempter.h"
6
Ben Chan9abb7632014-08-07 00:10:53 -07007#include <stdint.h>
8
Ben Chan02f7c1d2014-10-18 15:18:02 -07009#include <memory>
10
Ben Chan06c76a42014-09-05 08:21:06 -070011#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070012#include <base/message_loop/message_loop.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070013#include <chromeos/bind_lambda.h>
David Pursell02c18642014-11-06 11:26:11 -080014#include <chromeos/dbus/service_constants.h>
Alex Deymo30534502015-07-20 15:06:33 -070015#include <chromeos/make_unique_ptr.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070016#include <chromeos/message_loops/base_message_loop.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070017#include <chromeos/message_loops/message_loop.h>
18#include <chromeos/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070019#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020020#include <policy/libpolicy.h>
21#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070022
David Zeuthen985b1122013-10-09 12:13:15 -070023#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080024#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070025#include "update_engine/fake_system_state.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070026#include "update_engine/filesystem_verifier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070027#include "update_engine/install_plan.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080028#include "update_engine/mock_action.h"
29#include "update_engine/mock_action_processor.h"
Darin Petkov1b003102010-11-30 10:18:36 -080030#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070031#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080032#include "update_engine/mock_payload_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080033#include "update_engine/mock_prefs.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070034#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070035#include "update_engine/prefs.h"
Darin Petkov1b003102010-11-30 10:18:36 -080036#include "update_engine/test_utils.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070037#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070038
David Zeuthen985b1122013-10-09 12:13:15 -070039using base::Time;
40using base::TimeDelta;
Alex Deymo60ca1a72015-06-18 18:19:15 -070041using chromeos::MessageLoop;
Alex Deymo30534502015-07-20 15:06:33 -070042using org::chromium::LibCrosServiceInterfaceProxyMock;
43using org::chromium::UpdateEngineLibcrosProxyResolvedInterfaceProxyMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070044using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070045using std::unique_ptr;
Darin Petkov36275772010-10-01 11:40:57 -070046using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070047using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070048using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080049using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070050using testing::Property;
51using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070052using testing::ReturnPointee;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080053using testing::SaveArg;
Darin Petkov36275772010-10-01 11:40:57 -070054using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070055using testing::_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070056
57namespace chromeos_update_engine {
58
59// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070060// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061// methods.
62class UpdateAttempterUnderTest : public UpdateAttempter {
63 public:
Gilad Arnold1f847232014-04-07 12:07:49 -070064 UpdateAttempterUnderTest(SystemState* system_state,
Alex Deymo30534502015-07-20 15:06:33 -070065 LibCrosProxy* libcros_proxy,
66 org::chromium::debugdProxyInterface* debugd_proxy,
Alex Deymof329b932014-10-30 01:37:48 -070067 const string& update_completed_marker)
Alex Deymo30534502015-07-20 15:06:33 -070068 : UpdateAttempter(system_state, libcros_proxy, debugd_proxy,
69 update_completed_marker) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070070
71 // Wrap the update scheduling method, allowing us to opt out of scheduled
72 // updates for testing purposes.
73 void ScheduleUpdates() override {
74 schedule_updates_called_ = true;
75 if (do_schedule_updates_) {
76 UpdateAttempter::ScheduleUpdates();
77 } else {
78 LOG(INFO) << "[TEST] Update scheduling disabled.";
79 }
80 }
81 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
82 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
83
84 // Indicates whether ScheduleUpdates() was called.
85 bool schedule_updates_called() const { return schedule_updates_called_; }
86
David Pursell02c18642014-11-06 11:26:11 -080087 // Need to expose forced_omaha_url_ so we can test it.
Alex Deymo60ca1a72015-06-18 18:19:15 -070088 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -080089
Gilad Arnoldec7f9162014-07-15 13:24:46 -070090 private:
91 bool schedule_updates_called_ = false;
92 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070093};
94
95class UpdateAttempterTest : public ::testing::Test {
96 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070097 UpdateAttempterTest()
Alex Deymo30534502015-07-20 15:06:33 -070098 : service_interface_mock_(new LibCrosServiceInterfaceProxyMock()),
99 ue_proxy_resolved_interface_mock_(
100 new NiceMock<UpdateEngineLibcrosProxyResolvedInterfaceProxyMock>()),
101 libcros_proxy_(
102 chromeos::make_unique_ptr(service_interface_mock_),
103 chromeos::make_unique_ptr(ue_proxy_resolved_interface_mock_)) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700104 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700105 fake_system_state_.set_connection_manager(&mock_connection_manager);
106 fake_system_state_.set_update_attempter(&attempter_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700107 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700108
109 // Finish initializing the attempter.
110 attempter_.Init();
Alex Deymo461b2592015-07-24 20:10:52 -0700111 // Don't run setgoodkernel command.
112 attempter_.skip_set_good_kernel_ = true;
Jay Srinivasan43488792012-06-19 00:25:31 -0700113 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700114
Alex Deymo610277e2014-11-11 21:18:11 -0800115 void SetUp() override {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700116 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
117
Alex Deymob7ca0962014-10-01 17:58:07 -0700118 EXPECT_EQ(nullptr, attempter_.dbus_adaptor_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700119 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700120 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800121 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700122 EXPECT_EQ(MessageLoop::kTaskIdNull, attempter_.manage_shares_id_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700123 EXPECT_FALSE(attempter_.download_active_);
124 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
125 EXPECT_EQ(0.0, attempter_.download_progress_);
126 EXPECT_EQ(0, attempter_.last_checked_time_);
127 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700128 EXPECT_EQ(0, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800129 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700130 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700131 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700132
133 // Set up store/load semantics of P2P properties via the mock PayloadState.
134 actual_using_p2p_for_downloading_ = false;
135 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
136 SetUsingP2PForDownloading(_))
137 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
138 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
139 GetUsingP2PForDownloading())
140 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
141 actual_using_p2p_for_sharing_ = false;
142 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
143 SetUsingP2PForSharing(_))
144 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
145 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
146 GetUsingP2PForDownloading())
147 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700148 }
149
Alex Deymo610277e2014-11-11 21:18:11 -0800150 void TearDown() override {
Alex Deymo10875d92014-11-10 21:52:57 -0800151 test_utils::RecursiveUnlinkDir(test_dir_);
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700152 }
153
Alex Deymo60ca1a72015-06-18 18:19:15 -0700154 public:
155 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200156
Alex Deymo60ca1a72015-06-18 18:19:15 -0700157 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800158 void UpdateTestStart();
159 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700160 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700161 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700162 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700163 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700164 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700165 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700166 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700167 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700168 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700169 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700170 void P2PEnabledHousekeepingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700171
Gilad Arnold74b5f552014-10-07 08:17:16 -0700172 bool actual_using_p2p_for_downloading() {
173 return actual_using_p2p_for_downloading_;
174 }
175 bool actual_using_p2p_for_sharing() {
176 return actual_using_p2p_for_sharing_;
177 }
178
Alex Deymo0b3db6b2015-08-10 15:19:37 -0700179 base::MessageLoopForIO base_loop_;
180 chromeos::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700181
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700182 FakeSystemState fake_system_state_;
Alex Deymo30534502015-07-20 15:06:33 -0700183 org::chromium::debugdProxyMock debugd_proxy_mock_;
184 LibCrosServiceInterfaceProxyMock* service_interface_mock_;
185 UpdateEngineLibcrosProxyResolvedInterfaceProxyMock*
186 ue_proxy_resolved_interface_mock_;
187 LibCrosProxy libcros_proxy_;
188 UpdateAttempterUnderTest attempter_{&fake_system_state_,
189 &libcros_proxy_,
190 &debugd_proxy_mock_,
191 ""};
192
Alex Deymo8427b4a2014-11-05 14:00:32 -0800193 NiceMock<MockActionProcessor>* processor_;
194 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800195 NiceMock<MockConnectionManager> mock_connection_manager;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700196
197 string test_dir_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700198
199 bool actual_using_p2p_for_downloading_;
200 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700201};
202
Alex Deymo60ca1a72015-06-18 18:19:15 -0700203void UpdateAttempterTest::ScheduleQuitMainLoop() {
204 loop_.PostTask(FROM_HERE, base::Bind([this] { this->loop_.BreakLoop(); }));
205}
206
Darin Petkov1b003102010-11-30 10:18:36 -0800207TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700208 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800209 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700210 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800211 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700212 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800213 EXPECT_EQ(503, attempter_.http_response_code());
214 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700215 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800216}
217
218TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800219 MockAction action;
220 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Darin Petkov1b003102010-11-30 10:18:36 -0800221 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800222 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800223 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700224 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
225 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800226}
227
228TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700229 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800230 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700231 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800232 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800233 ObjectCollectorAction<OmahaResponse> collector_action;
234 BondActions(&action, &collector_action);
235 OmahaResponse response;
236 response.poll_interval = 234;
237 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800238 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700239 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800240 EXPECT_EQ(500, attempter_.http_response_code());
241 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700242 EXPECT_EQ(234, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700243 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800244}
245
Alex Deymo30534502015-07-20 15:06:33 -0700246TEST_F(UpdateAttempterTest, ConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700247 string test_update_completed_marker;
248 CHECK(utils::MakeTempFile(
Alex Deymo30534502015-07-20 15:06:33 -0700249 "update_attempter_unittest-update_completed_marker-XXXXXX",
250 &test_update_completed_marker,
251 nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700252 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700253 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700254 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Alex Deymo30534502015-07-20 15:06:33 -0700255 UpdateAttempterUnderTest attempter(&fake_system_state_,
256 nullptr,
257 &debugd_proxy_mock_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700258 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700259 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700260}
261
262TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700263 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
264 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700265 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700266 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700267
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700268 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700269 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
270 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700271 EXPECT_EQ(ErrorCode::kOmahaRequestError,
272 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700273 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700274 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700275 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700276 ErrorCode::kError));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700277 FilesystemVerifierAction filesystem_verifier_action(
278 &fake_system_state_, PartitionType::kRootfs);
279 EXPECT_EQ(ErrorCode::kFilesystemVerifierError,
280 GetErrorCodeForAction(&filesystem_verifier_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700281 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800282 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700283 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700284 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700285 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800286 MockAction action_mock;
287 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700288 EXPECT_EQ(ErrorCode::kError,
289 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700290}
291
Darin Petkov36275772010-10-01 11:40:57 -0700292TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700293 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800294 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700295 .WillOnce(Return(false));
296 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700297 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800298 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700299 .WillOnce(DoAll(
300 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
301 Return(true)));
302 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700303 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800304 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700305 .WillOnce(DoAll(
306 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
307 Return(true)));
308 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700309 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800310 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700311 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700312 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700313}
314
315TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800316 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700317 .WillOnce(Return(false))
318 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
319 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
320 .WillOnce(DoAll(
321 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
322 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800323 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700324 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800325 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700326 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800327 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700328 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700329 for (int i = 0; i < 4; i ++)
330 attempter_.MarkDeltaUpdateFailure();
331}
332
Darin Petkov1b003102010-11-30 10:18:36 -0800333TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
334 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
335 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700336 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800337 .Times(0);
338 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700339 string url1 = "http://url1";
340 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800341 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700342 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700343 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700344 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800345 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700346 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800347}
348
349TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
350 EXPECT_CALL(*processor_,
351 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700352 OmahaRequestAction::StaticType())));
353 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700354 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700355 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800356 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
357 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800358 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800359 attempter_.ScheduleErrorEventAction();
360 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
361}
362
Darin Petkove6ef2f82011-03-07 17:31:11 -0800363namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700364// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700365const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800366 OmahaRequestAction::StaticType(),
367 OmahaResponseHandlerAction::StaticType(),
Allie Woodeb9e6d82015-04-17 13:55:30 -0700368 FilesystemVerifierAction::StaticType(),
369 FilesystemVerifierAction::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(),
374 FilesystemVerifierAction::StaticType(),
Darin Petkove6ef2f82011-03-07 17:31:11 -0800375 PostinstallRunnerAction::StaticType(),
376 OmahaRequestAction::StaticType()
377};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700378
379// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700380const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700381 InstallPlanAction::StaticType(),
382 PostinstallRunnerAction::StaticType(),
383};
384
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700385} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800386
387void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700388 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700389
390 // Expect that the device policy is loaded by the UpdateAttempter at some
391 // point by calling RefreshDevicePolicy.
392 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
393 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
394 EXPECT_CALL(*device_policy, LoadPolicy())
395 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
396
397 {
398 InSequence s;
399 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
400 EXPECT_CALL(*processor_,
401 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700402 kUpdateActionTypes[i])));
Alex Deymo749ecf12014-10-21 20:06:57 -0700403 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700404 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700405 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700406
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700407 attempter_.Update("", "", "", "", false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700408 loop_.PostTask(FROM_HERE,
409 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
410 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800411}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700412
Darin Petkove6ef2f82011-03-07 17:31:11 -0800413void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700414 EXPECT_EQ(0, attempter_.http_response_code());
415 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700416 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
417 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
418 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700419 }
420 EXPECT_EQ(attempter_.response_handler_action_.get(),
421 attempter_.actions_[1].get());
422 DownloadAction* download_action =
423 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700424 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700425 EXPECT_EQ(&attempter_, download_action->delegate());
426 EXPECT_EQ(UPDATE_STATUS_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
Don Garrett6646b442013-11-13 15:29:11 -0800439 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700440 // References bootable kernels in fake_hardware.h
441 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800442 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700443 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800444 rollback_kernel);
445 }
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 }
493 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
494 InstallPlanAction* install_plan_action =
495 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
496 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700497 // Matches fake_hardware.h -> rollback should move from kernel/boot device
498 // pair to other pair.
499 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
500 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
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)));
552 chromeos::MessageLoopRunMaxIterations(&loop_, 100);
Thieu Le116fda32011-04-19 11:01:54 -0700553 EXPECT_EQ(UPDATE_STATUS_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) {
927 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Alex Deymo30534502015-07-20 15:06:33 -0700928 UpdateAttempterUnderTest attempter{&fake_system_state_,
929 nullptr, // libcros_proxy
930 &debugd_proxy_mock_,
931 update_completed_marker};
David Zeuthen3c55abd2013-10-14 12:48:03 -0700932
933 FakeClock fake_clock;
934 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700935 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -0700936
937 Time boot_time;
938 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
939
940 attempter.WriteUpdateCompletedMarker();
941
942 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
943 EXPECT_EQ(boot_time.ToTimeT(), 42);
944}
945
David Pursell02c18642014-11-06 11:26:11 -0800946TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
947 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
948 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
949}
950
951TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
952 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
953 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
Alex Deymo30534502015-07-20 15:06:33 -0700954 EXPECT_CALL(debugd_proxy_mock_, QueryDevFeatures(_, _, _))
955 .WillRepeatedly(DoAll(SetArgumentPointee<0>(0), Return(true)));
David Pursell02c18642014-11-06 11:26:11 -0800956 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
957}
958
959TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
960 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
961 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
962 // debugd should not be queried in this case.
Alex Deymo30534502015-07-20 15:06:33 -0700963 EXPECT_CALL(debugd_proxy_mock_, QueryDevFeatures(_, _, _)).Times(0);
David Pursell02c18642014-11-06 11:26:11 -0800964 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
965}
966
967TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedDebugdDisabled) {
968 using debugd::DEV_FEATURES_DISABLED;
969 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
970 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
Alex Deymo30534502015-07-20 15:06:33 -0700971 EXPECT_CALL(debugd_proxy_mock_, QueryDevFeatures(_, _, _))
972 .WillRepeatedly(
973 DoAll(SetArgumentPointee<0>(DEV_FEATURES_DISABLED), Return(true)));
David Pursell02c18642014-11-06 11:26:11 -0800974 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
975}
976
977TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedDebugdFailure) {
978 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
979 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
Alex Deymo30534502015-07-20 15:06:33 -0700980 EXPECT_CALL(debugd_proxy_mock_, QueryDevFeatures(_, _, _))
David Pursell02c18642014-11-06 11:26:11 -0800981 .WillRepeatedly(Return(false));
982 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
983}
984
985TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
986 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
987 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
988 attempter_.CheckForUpdate("", "autest", true);
989 EXPECT_EQ(chromeos_update_engine::kAUTestOmahaUrl,
990 attempter_.forced_omaha_url());
991}
992
993TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
994 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
995 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
996 attempter_.CheckForUpdate("", "autest-scheduled", true);
997 EXPECT_EQ(chromeos_update_engine::kAUTestOmahaUrl,
998 attempter_.forced_omaha_url());
999}
1000
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001001} // namespace chromeos_update_engine