blob: 8c6fa3d18fb67a10d7938f652c45cda48f3e0c7c [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 Deymo60ca1a72015-06-18 18:19:15 -070012#include <chromeos/bind_lambda.h>
David Pursell02c18642014-11-06 11:26:11 -080013#include <chromeos/dbus/service_constants.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070014#include <chromeos/message_loops/glib_message_loop.h>
15#include <chromeos/message_loops/message_loop.h>
16#include <chromeos/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070017#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020018#include <policy/libpolicy.h>
19#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070020
David Zeuthen985b1122013-10-09 12:13:15 -070021#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080022#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070023#include "update_engine/fake_system_state.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070024#include "update_engine/filesystem_verifier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070025#include "update_engine/install_plan.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080026#include "update_engine/mock_action.h"
27#include "update_engine/mock_action_processor.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080028#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080029#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070030#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080031#include "update_engine/mock_payload_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080032#include "update_engine/mock_prefs.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070034#include "update_engine/prefs.h"
Darin Petkov1b003102010-11-30 10:18:36 -080035#include "update_engine/test_utils.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070036#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070037
David Zeuthen985b1122013-10-09 12:13:15 -070038using base::Time;
39using base::TimeDelta;
Alex Deymo60ca1a72015-06-18 18:19:15 -070040using chromeos::MessageLoop;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070041using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070042using std::unique_ptr;
David Pursell02c18642014-11-06 11:26:11 -080043using testing::A;
Darin Petkov36275772010-10-01 11:40:57 -070044using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070045using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070046using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080047using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070048using testing::Property;
49using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070050using testing::ReturnPointee;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080051using testing::SaveArg;
Darin Petkov36275772010-10-01 11:40:57 -070052using testing::SetArgumentPointee;
David Pursell02c18642014-11-06 11:26:11 -080053using testing::StrEq;
Alex Deymof329b932014-10-30 01:37:48 -070054using testing::_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070055
56namespace chromeos_update_engine {
57
58// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070059// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070060// methods.
61class UpdateAttempterUnderTest : public UpdateAttempter {
62 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070063 // We always feed an explicit update completed marker name; however, unless
64 // explicitly specified, we feed an empty string, which causes the
65 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070066 UpdateAttempterUnderTest(SystemState* system_state,
67 DBusWrapperInterface* dbus_iface)
68 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070069
Gilad Arnold1f847232014-04-07 12:07:49 -070070 UpdateAttempterUnderTest(SystemState* system_state,
71 DBusWrapperInterface* dbus_iface,
Alex Deymof329b932014-10-30 01:37:48 -070072 const string& update_completed_marker)
Gilad Arnold1f847232014-04-07 12:07:49 -070073 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070074
75 // Wrap the update scheduling method, allowing us to opt out of scheduled
76 // updates for testing purposes.
77 void ScheduleUpdates() override {
78 schedule_updates_called_ = true;
79 if (do_schedule_updates_) {
80 UpdateAttempter::ScheduleUpdates();
81 } else {
82 LOG(INFO) << "[TEST] Update scheduling disabled.";
83 }
84 }
85 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
86 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
87
88 // Indicates whether ScheduleUpdates() was called.
89 bool schedule_updates_called() const { return schedule_updates_called_; }
90
David Pursell02c18642014-11-06 11:26:11 -080091 // Need to expose forced_omaha_url_ so we can test it.
Alex Deymo60ca1a72015-06-18 18:19:15 -070092 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -080093
Gilad Arnoldec7f9162014-07-15 13:24:46 -070094 private:
95 bool schedule_updates_called_ = false;
96 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070097};
98
99class UpdateAttempterTest : public ::testing::Test {
100 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700101 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700102 : attempter_(&fake_system_state_, &dbus_),
103 mock_connection_manager(&fake_system_state_),
David Pursell02c18642014-11-06 11:26:11 -0800104 fake_dbus_system_bus_(reinterpret_cast<DBusGConnection*>(1)),
105 fake_dbus_debugd_proxy_(reinterpret_cast<DBusGProxy*>(2)) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700106 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700107 fake_system_state_.set_connection_manager(&mock_connection_manager);
108 fake_system_state_.set_update_attempter(&attempter_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700109 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700110
111 // Finish initializing the attempter.
112 attempter_.Init();
Alex Deymo461b2592015-07-24 20:10:52 -0700113 // Don't run setgoodkernel command.
114 attempter_.skip_set_good_kernel_ = true;
Jay Srinivasan43488792012-06-19 00:25:31 -0700115 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700116
Alex Deymo610277e2014-11-11 21:18:11 -0800117 void SetUp() override {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700118 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
119
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700120 EXPECT_EQ(nullptr, attempter_.dbus_service_);
121 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700122 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800123 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700124 EXPECT_EQ(MessageLoop::kTaskIdNull, attempter_.manage_shares_id_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700125 EXPECT_FALSE(attempter_.download_active_);
126 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
127 EXPECT_EQ(0.0, attempter_.download_progress_);
128 EXPECT_EQ(0, attempter_.last_checked_time_);
129 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700130 EXPECT_EQ(0, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800131 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700132 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700133 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700134
135 // Set up store/load semantics of P2P properties via the mock PayloadState.
136 actual_using_p2p_for_downloading_ = false;
137 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
138 SetUsingP2PForDownloading(_))
139 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
140 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
141 GetUsingP2PForDownloading())
142 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
143 actual_using_p2p_for_sharing_ = false;
144 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
145 SetUsingP2PForSharing(_))
146 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
147 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
148 GetUsingP2PForDownloading())
149 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
David Pursell02c18642014-11-06 11:26:11 -0800150
151 // Set up mock debugd access over the system D-Bus. ProxyCall_0_1() also
152 // needs to be mocked in any test using debugd to provide the desired value.
153 ON_CALL(dbus_, BusGet(DBUS_BUS_SYSTEM, _))
154 .WillByDefault(Return(fake_dbus_system_bus_));
155 ON_CALL(dbus_, ProxyNewForName(fake_dbus_system_bus_,
156 StrEq(debugd::kDebugdServiceName),
157 StrEq(debugd::kDebugdServicePath),
158 StrEq(debugd::kDebugdInterface)))
159 .WillByDefault(Return(fake_dbus_debugd_proxy_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700160 }
161
Alex Deymo610277e2014-11-11 21:18:11 -0800162 void TearDown() override {
Alex Deymo10875d92014-11-10 21:52:57 -0800163 test_utils::RecursiveUnlinkDir(test_dir_);
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700164 }
165
Alex Deymo60ca1a72015-06-18 18:19:15 -0700166 public:
167 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200168
Alex Deymo60ca1a72015-06-18 18:19:15 -0700169 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800170 void UpdateTestStart();
171 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700172 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700173 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700174 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700175 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700176 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700177 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700178 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700179 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700180 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700181 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700182 void P2PEnabledHousekeepingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700183
Gilad Arnold74b5f552014-10-07 08:17:16 -0700184 bool actual_using_p2p_for_downloading() {
185 return actual_using_p2p_for_downloading_;
186 }
187 bool actual_using_p2p_for_sharing() {
188 return actual_using_p2p_for_sharing_;
189 }
190
Alex Deymo60ca1a72015-06-18 18:19:15 -0700191 // TODO(deymo): Replace this with a FakeMessageLoop. Some of these tests use a
192 // real LibcurlHttpFetcher, which still requires a GlibMessageLoop.
193 chromeos::GlibMessageLoop loop_;
194
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700195 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800196 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700197 UpdateAttempterUnderTest attempter_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800198 NiceMock<MockActionProcessor>* processor_;
199 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800200 NiceMock<MockConnectionManager> mock_connection_manager;
David Pursell02c18642014-11-06 11:26:11 -0800201 // fake_dbus_xxx pointers will be non-null for comparison purposes, but won't
202 // be valid objects so don't try to use them.
203 DBusGConnection* fake_dbus_system_bus_;
204 DBusGProxy* fake_dbus_debugd_proxy_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700205
206 string test_dir_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700207
208 bool actual_using_p2p_for_downloading_;
209 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700210};
211
Alex Deymo60ca1a72015-06-18 18:19:15 -0700212void UpdateAttempterTest::ScheduleQuitMainLoop() {
213 loop_.PostTask(FROM_HERE, base::Bind([this] { this->loop_.BreakLoop(); }));
214}
215
Darin Petkov1b003102010-11-30 10:18:36 -0800216TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700217 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800218 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700219 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800220 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700221 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800222 EXPECT_EQ(503, attempter_.http_response_code());
223 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700224 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800225}
226
227TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800228 MockAction action;
229 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Darin Petkov1b003102010-11-30 10:18:36 -0800230 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800231 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800232 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700233 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
234 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800235}
236
237TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700238 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800239 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700240 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800241 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800242 ObjectCollectorAction<OmahaResponse> collector_action;
243 BondActions(&action, &collector_action);
244 OmahaResponse response;
245 response.poll_interval = 234;
246 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800247 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700248 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800249 EXPECT_EQ(500, attempter_.http_response_code());
250 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700251 EXPECT_EQ(234, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700252 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800253}
254
Darin Petkovcd1666f2010-09-23 09:53:44 -0700255TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700256 string test_update_completed_marker;
257 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800258 "update_attempter_unittest-update_completed_marker-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700259 &test_update_completed_marker, nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700260 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700261 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700262 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700263 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700264 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700265 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700266}
267
268TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700269 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
270 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700271 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700272 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700273
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700274 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700275 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
276 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700277 EXPECT_EQ(ErrorCode::kOmahaRequestError,
278 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700279 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700280 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700281 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700282 ErrorCode::kError));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700283 FilesystemVerifierAction filesystem_verifier_action(
284 &fake_system_state_, PartitionType::kRootfs);
285 EXPECT_EQ(ErrorCode::kFilesystemVerifierError,
286 GetErrorCodeForAction(&filesystem_verifier_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700287 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800288 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700289 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700290 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700291 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800292 MockAction action_mock;
293 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700294 EXPECT_EQ(ErrorCode::kError,
295 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700296}
297
Darin Petkov36275772010-10-01 11:40:57 -0700298TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700299 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800300 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700301 .WillOnce(Return(false));
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 - 1),
307 Return(true)));
308 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700309 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800310 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700311 .WillOnce(DoAll(
312 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
313 Return(true)));
314 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700315 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800316 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700317 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700318 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700319}
320
321TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800322 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700323 .WillOnce(Return(false))
324 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
325 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
326 .WillOnce(DoAll(
327 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
328 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800329 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700330 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800331 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700332 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800333 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700334 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700335 for (int i = 0; i < 4; i ++)
336 attempter_.MarkDeltaUpdateFailure();
337}
338
Darin Petkov1b003102010-11-30 10:18:36 -0800339TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
340 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
341 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700342 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800343 .Times(0);
344 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700345 string url1 = "http://url1";
346 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800347 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700348 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700349 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700350 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800351 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700352 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800353}
354
355TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
356 EXPECT_CALL(*processor_,
357 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700358 OmahaRequestAction::StaticType())));
359 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700360 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700361 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800362 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
363 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800364 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800365 attempter_.ScheduleErrorEventAction();
366 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
367}
368
Darin Petkove6ef2f82011-03-07 17:31:11 -0800369namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700370// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700371const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800372 OmahaRequestAction::StaticType(),
373 OmahaResponseHandlerAction::StaticType(),
Allie Woodeb9e6d82015-04-17 13:55:30 -0700374 FilesystemVerifierAction::StaticType(),
375 FilesystemVerifierAction::StaticType(),
Darin Petkove6ef2f82011-03-07 17:31:11 -0800376 OmahaRequestAction::StaticType(),
377 DownloadAction::StaticType(),
378 OmahaRequestAction::StaticType(),
Allie Woodeb9e6d82015-04-17 13:55:30 -0700379 FilesystemVerifierAction::StaticType(),
380 FilesystemVerifierAction::StaticType(),
Darin Petkove6ef2f82011-03-07 17:31:11 -0800381 PostinstallRunnerAction::StaticType(),
382 OmahaRequestAction::StaticType()
383};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700384
385// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700386const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700387 InstallPlanAction::StaticType(),
388 PostinstallRunnerAction::StaticType(),
389};
390
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700391} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800392
393void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700394 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700395
396 // Expect that the device policy is loaded by the UpdateAttempter at some
397 // point by calling RefreshDevicePolicy.
398 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
399 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
400 EXPECT_CALL(*device_policy, LoadPolicy())
401 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
402
403 {
404 InSequence s;
405 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
406 EXPECT_CALL(*processor_,
407 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700408 kUpdateActionTypes[i])));
Alex Deymo749ecf12014-10-21 20:06:57 -0700409 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700410 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700411 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700412
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700413 attempter_.Update("", "", "", "", false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700414 loop_.PostTask(FROM_HERE,
415 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
416 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800417}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700418
Darin Petkove6ef2f82011-03-07 17:31:11 -0800419void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700420 EXPECT_EQ(0, attempter_.http_response_code());
421 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700422 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
423 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
424 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700425 }
426 EXPECT_EQ(attempter_.response_handler_action_.get(),
427 attempter_.actions_[1].get());
428 DownloadAction* download_action =
429 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700430 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700431 EXPECT_EQ(&attempter_, download_action->delegate());
432 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700433 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800434}
435
Chris Sosa28e479c2013-07-12 11:39:53 -0700436void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700437 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700438 // Create a device policy so that we can change settings.
439 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
440 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
441
442 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700443 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700444
Don Garrett6646b442013-11-13 15:29:11 -0800445 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700446 // References bootable kernels in fake_hardware.h
447 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800448 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700449 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800450 rollback_kernel);
451 }
452
Chris Sosa28e479c2013-07-12 11:39:53 -0700453 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700454
Chris Sosad38b1132014-03-25 10:43:59 -0700455 // We only allow rollback on devices that are not enterprise enrolled and
456 // which have a valid slot to rollback to.
457 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700458 is_rollback_allowed = true;
459 }
460
Don Garrett6646b442013-11-13 15:29:11 -0800461 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700462 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800463 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700464 DoAll(SetArgumentPointee<0>(string("")),
Chris Sosa28e479c2013-07-12 11:39:53 -0700465 Return(true)));
466 } else {
467 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800468 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700469 DoAll(SetArgumentPointee<0>(string("fake.mail@fake.com")),
Chris Sosa76a29ae2013-07-11 17:59:24 -0700470 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700471 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700472
Chris Sosa28e479c2013-07-12 11:39:53 -0700473 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700474 InSequence s;
475 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
476 EXPECT_CALL(*processor_,
477 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700478 kRollbackActionTypes[i])));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700479 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700480 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700481
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700482 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700483 loop_.PostTask(FROM_HERE,
484 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
485 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700486 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700487 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700488 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700489 }
490}
491
492void UpdateAttempterTest::RollbackTestVerify() {
493 // Verifies the actions that were enqueued.
494 EXPECT_EQ(&attempter_, processor_->delegate());
495 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
496 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
497 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
498 }
499 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
500 InstallPlanAction* install_plan_action =
501 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
502 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700503 // Matches fake_hardware.h -> rollback should move from kernel/boot device
504 // pair to other pair.
505 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
506 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700507 EXPECT_EQ(install_plan->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700508 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700509}
510
Darin Petkove6ef2f82011-03-07 17:31:11 -0800511TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo461b2592015-07-24 20:10:52 -0700512 UpdateTestStart();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700513 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700514}
515
Chris Sosa76a29ae2013-07-11 17:59:24 -0700516TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700517 loop_.PostTask(FROM_HERE,
518 base::Bind(&UpdateAttempterTest::RollbackTestStart,
519 base::Unretained(this),
520 false, true));
521 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700522}
523
Don Garrett6646b442013-11-13 15:29:11 -0800524TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700525 loop_.PostTask(FROM_HERE,
526 base::Bind(&UpdateAttempterTest::RollbackTestStart,
527 base::Unretained(this),
528 false, false));
529 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800530}
531
Chris Sosa76a29ae2013-07-11 17:59:24 -0700532TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700533 loop_.PostTask(FROM_HERE,
534 base::Bind(&UpdateAttempterTest::RollbackTestStart,
535 base::Unretained(this),
536 true, true));
537 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700538}
539
Thieu Le116fda32011-04-19 11:01:54 -0700540void UpdateAttempterTest::PingOmahaTestStart() {
541 EXPECT_CALL(*processor_,
542 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700543 OmahaRequestAction::StaticType())));
544 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700545 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700546 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700547}
548
549TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700550 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
551 EXPECT_FALSE(attempter_.schedule_updates_called());
552 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
553 // testing, which is more permissive than we want to handle here.
554 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700555 loop_.PostTask(FROM_HERE,
556 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
557 base::Unretained(this)));
558 chromeos::MessageLoopRunMaxIterations(&loop_, 100);
Thieu Le116fda32011-04-19 11:01:54 -0700559 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700560 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700561}
562
Darin Petkov18c7bce2011-06-16 14:07:00 -0700563TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800564 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700565 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700566 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700567 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700568 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
569 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700570 EXPECT_EQ(
571 static_cast<ErrorCode>(static_cast<int>(kCode) |
572 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
573 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700574}
575
576TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
577 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700578 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700579 response_action->install_plan_.is_resume = true;
580 attempter_.response_handler_action_.reset(response_action);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800581 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700582 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700583 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700584 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700585 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
586 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700587 EXPECT_EQ(
588 static_cast<ErrorCode>(
589 static_cast<int>(kCode) |
590 static_cast<int>(ErrorCode::kResumedFlag) |
591 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
592 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700593}
594
David Zeuthen8f191b22013-08-06 12:27:50 -0700595TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
596 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700597 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700598 mock_p2p_manager.fake().SetP2PEnabled(false);
599 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
600 attempter_.UpdateEngineStarted();
601}
602
603TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
604 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700605 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700606 mock_p2p_manager.fake().SetP2PEnabled(true);
607 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
608 attempter_.UpdateEngineStarted();
609}
610
611TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
612 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700613 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700614 mock_p2p_manager.fake().SetP2PEnabled(true);
615 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700616 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700617 attempter_.UpdateEngineStarted();
618}
619
620TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700621 loop_.PostTask(FROM_HERE,
622 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
623 base::Unretained(this)));
624 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700625}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700626
David Zeuthen8f191b22013-08-06 12:27:50 -0700627void UpdateAttempterTest::P2PNotEnabledStart() {
628 // If P2P is not enabled, check that we do not attempt housekeeping
629 // and do not convey that p2p is to be used.
630 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700631 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700632 mock_p2p_manager.fake().SetP2PEnabled(false);
633 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700634 attempter_.Update("", "", "", "", false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700635 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700636 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700637 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700638}
639
640TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700641 loop_.PostTask(FROM_HERE,
642 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
643 base::Unretained(this)));
644 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700645}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700646
David Zeuthen8f191b22013-08-06 12:27:50 -0700647void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
648 // If p2p is enabled, but starting it fails ensure we don't do
649 // any housekeeping and do not convey that p2p should be used.
650 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700651 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700652 mock_p2p_manager.fake().SetP2PEnabled(true);
653 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
654 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
655 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700656 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700657 EXPECT_FALSE(actual_using_p2p_for_downloading());
658 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700659 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700660}
661
662TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700663 loop_.PostTask(
664 FROM_HERE,
665 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
666 base::Unretained(this)));
667 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700668}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700669
David Zeuthen8f191b22013-08-06 12:27:50 -0700670void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
671 // If p2p is enabled, starting it works but housekeeping fails, ensure
672 // we do not convey p2p is to be used.
673 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700674 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700675 mock_p2p_manager.fake().SetP2PEnabled(true);
676 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
677 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700678 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700679 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700680 EXPECT_FALSE(actual_using_p2p_for_downloading());
681 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700682 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700683}
684
685TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700686 loop_.PostTask(FROM_HERE,
687 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
688 base::Unretained(this)));
689 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700690}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700691
David Zeuthen8f191b22013-08-06 12:27:50 -0700692void UpdateAttempterTest::P2PEnabledStart() {
693 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700694 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700695 // If P2P is enabled and starting it works, check that we performed
696 // housekeeping and that we convey p2p should be used.
697 mock_p2p_manager.fake().SetP2PEnabled(true);
698 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
699 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700700 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700701 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700702 EXPECT_TRUE(actual_using_p2p_for_downloading());
703 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700704 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700705}
706
707TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700708 loop_.PostTask(FROM_HERE,
709 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
710 base::Unretained(this)));
711 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700712}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700713
David Zeuthen8f191b22013-08-06 12:27:50 -0700714void UpdateAttempterTest::P2PEnabledInteractiveStart() {
715 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700716 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700717 // For an interactive check, if P2P is enabled and starting it
718 // works, check that we performed housekeeping and that we convey
719 // p2p should be used for sharing but NOT for downloading.
720 mock_p2p_manager.fake().SetP2PEnabled(true);
721 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
722 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700723 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700724 attempter_.Update("", "", "", "", false, true /* interactive */);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700725 EXPECT_FALSE(actual_using_p2p_for_downloading());
726 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700727 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700728}
729
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700730TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700731 loop_.PostTask(
732 FROM_HERE,
733 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
734 base::Unretained(this)));
735 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700736}
737
738// Tests that the scatter_factor_in_seconds value is properly fetched
739// from the device policy.
740void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700741 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700742
743 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
744 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
745
746 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700747 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700748
749 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
750 .WillRepeatedly(DoAll(
751 SetArgumentPointee<0>(scatter_factor_in_seconds),
752 Return(true)));
753
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700754 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700755 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
756
Alex Deymo60ca1a72015-06-18 18:19:15 -0700757 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700758}
759
760TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700761 loop_.PostTask(
762 FROM_HERE,
763 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
764 base::Unretained(this)));
765 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700766}
767
768void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
769 // Tests that the scatter_factor_in_seconds value is properly fetched
770 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700771 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800772 FakePrefs fake_prefs;
773 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700774
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700775 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700776
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800777 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700778
Ben Chan9abb7632014-08-07 00:10:53 -0700779 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700780
781 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
782 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
783
784 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700785 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700786
787 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
788 .WillRepeatedly(DoAll(
789 SetArgumentPointee<0>(scatter_factor_in_seconds),
790 Return(true)));
791
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700792 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700793 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
794
795 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800796 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700797
Ben Chan9abb7632014-08-07 00:10:53 -0700798 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800799 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700800 EXPECT_EQ(initial_value - 1, new_value);
801
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700802 EXPECT_TRUE(
803 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700804
805 // However, if the count is already 0, it's not decremented. Test that.
806 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800807 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700808 attempter_.Update("", "", "", "", false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800809 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
810 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700811 EXPECT_EQ(initial_value, new_value);
812
Alex Deymo60ca1a72015-06-18 18:19:15 -0700813 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700814}
815
Jay Srinivasan08fce042012-06-07 16:31:01 -0700816TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700817 loop_.PostTask(FROM_HERE, base::Bind(
818 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
819 base::Unretained(this)));
820 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700821}
822
823void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
824 // Tests that no scattering logic is enabled if the update check
825 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700826 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800827 FakePrefs fake_prefs;
828 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700829
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700830 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800831 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700832
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800833 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
834 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700835
836 // make sure scatter_factor is non-zero as scattering is disabled
837 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700838 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700839
840 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
841 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
842
843 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700844 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700845
846 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
847 .WillRepeatedly(DoAll(
848 SetArgumentPointee<0>(scatter_factor_in_seconds),
849 Return(true)));
850
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800851 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700852 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700853 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
854
855 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700856 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700857 EXPECT_FALSE(
858 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800859 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700860 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
861 EXPECT_FALSE(
862 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800863 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700864
Alex Deymo60ca1a72015-06-18 18:19:15 -0700865 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700866}
867
David Zeuthen985b1122013-10-09 12:13:15 -0700868// Checks that we only report daily metrics at most every 24 hours.
869TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
870 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800871 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -0700872
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700873 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800874 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700875
876 Time epoch = Time::FromInternalValue(0);
877 fake_clock.SetWallclockTime(epoch);
878
879 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
880 // we should report.
881 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
882 // We should not report again if no time has passed.
883 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
884
885 // We should not report if only 10 hours has passed.
886 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
887 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
888
889 // We should not report if only 24 hours - 1 sec has passed.
890 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
891 TimeDelta::FromSeconds(1));
892 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
893
894 // We should report if 24 hours has passed.
895 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
896 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
897
898 // But then we should not report again..
899 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
900
901 // .. until another 24 hours has passed
902 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
903 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
904 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
905 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
906 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
907
908 // .. and another 24 hours
909 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
910 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
911 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
912 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
913 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
914
915 // If the span between time of reporting and present time is
916 // negative, we report. This is in order to reset the timestamp and
917 // avoid an edge condition whereby a distant point in the future is
918 // in the state variable resulting in us never ever reporting again.
919 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
920 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
921 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
922
923 // In this case we should not update until the clock reads 71 + 24 = 95.
924 // Check that.
925 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
926 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
927 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
928 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
929 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -0700930}
931
David Zeuthen3c55abd2013-10-14 12:48:03 -0700932TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
933 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700934 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -0700935 update_completed_marker);
936
937 FakeClock fake_clock;
938 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700939 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -0700940
941 Time boot_time;
942 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
943
944 attempter.WriteUpdateCompletedMarker();
945
946 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
947 EXPECT_EQ(boot_time.ToTimeT(), 42);
948}
949
David Pursell02c18642014-11-06 11:26:11 -0800950TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
951 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
952 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
953}
954
955TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
956 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
957 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
958 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
959 StrEq(debugd::kQueryDevFeatures),
960 _, A<gint*>()))
961 .WillRepeatedly(DoAll(SetArgumentPointee<3>(0),
962 Return(true)));
963 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
964}
965
966TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
967 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
968 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
969 // debugd should not be queried in this case.
970 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
971 StrEq(debugd::kQueryDevFeatures),
972 _, A<gint*>()))
973 .Times(0);
974 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
975}
976
977TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedDebugdDisabled) {
978 using debugd::DEV_FEATURES_DISABLED;
979 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
980 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
981 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
982 StrEq(debugd::kQueryDevFeatures),
983 _, A<gint*>()))
984 .WillRepeatedly(DoAll(SetArgumentPointee<3>(DEV_FEATURES_DISABLED),
985 Return(true)));
986 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
987}
988
989TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedDebugdFailure) {
990 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
991 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
992 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
993 StrEq(debugd::kQueryDevFeatures),
994 _, A<gint*>()))
995 .WillRepeatedly(Return(false));
996 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
997}
998
999TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1000 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1001 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
1002 attempter_.CheckForUpdate("", "autest", true);
1003 EXPECT_EQ(chromeos_update_engine::kAUTestOmahaUrl,
1004 attempter_.forced_omaha_url());
1005}
1006
1007TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
1008 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1009 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
1010 attempter_.CheckForUpdate("", "autest-scheduled", true);
1011 EXPECT_EQ(chromeos_update_engine::kAUTestOmahaUrl,
1012 attempter_.forced_omaha_url());
1013}
1014
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001015} // namespace chromeos_update_engine