blob: 6dc921bc66cd936c158031bca4ceb244bc73b1f2 [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
Ben Chan9abb7632014-08-07 00:10:53 -07005#include <stdint.h>
6
Ben Chan02f7c1d2014-10-18 15:18:02 -07007#include <memory>
8
Ben Chan06c76a42014-09-05 08:21:06 -07009#include <base/files/file_util.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070010#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020011#include <policy/libpolicy.h>
12#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070013
14#include "update_engine/action_mock.h"
15#include "update_engine/action_processor_mock.h"
David Zeuthen985b1122013-10-09 12:13:15 -070016#include "update_engine/fake_clock.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070017#include "update_engine/fake_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070018#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070019#include "update_engine/install_plan.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080020#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080021#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070022#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080023#include "update_engine/mock_payload_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070025#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070026#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080027#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028#include "update_engine/update_attempter.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070029#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030
David Zeuthen985b1122013-10-09 12:13:15 -070031using base::Time;
32using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070034using std::unique_ptr;
Darin Petkov36275772010-10-01 11:40:57 -070035using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070036using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070037using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080038using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070039using testing::Property;
Gilad Arnold74b5f552014-10-07 08:17:16 -070040using testing::SaveArg;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070041using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070042using testing::ReturnPointee;
Darin Petkov36275772010-10-01 11:40:57 -070043using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070044using testing::_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070045
46namespace chromeos_update_engine {
47
48// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070049// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070050// methods.
51class UpdateAttempterUnderTest : public UpdateAttempter {
52 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070053 // We always feed an explicit update completed marker name; however, unless
54 // explicitly specified, we feed an empty string, which causes the
55 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070056 UpdateAttempterUnderTest(SystemState* system_state,
57 DBusWrapperInterface* dbus_iface)
58 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070059
Gilad Arnold1f847232014-04-07 12:07:49 -070060 UpdateAttempterUnderTest(SystemState* system_state,
61 DBusWrapperInterface* dbus_iface,
Alex Deymof329b932014-10-30 01:37:48 -070062 const string& update_completed_marker)
Gilad Arnold1f847232014-04-07 12:07:49 -070063 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070064
65 // Wrap the update scheduling method, allowing us to opt out of scheduled
66 // updates for testing purposes.
67 void ScheduleUpdates() override {
68 schedule_updates_called_ = true;
69 if (do_schedule_updates_) {
70 UpdateAttempter::ScheduleUpdates();
71 } else {
72 LOG(INFO) << "[TEST] Update scheduling disabled.";
73 }
74 }
75 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
76 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
77
78 // Indicates whether ScheduleUpdates() was called.
79 bool schedule_updates_called() const { return schedule_updates_called_; }
80
81 private:
82 bool schedule_updates_called_ = false;
83 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070084};
85
86class UpdateAttempterTest : public ::testing::Test {
87 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070088 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -070089 : attempter_(&fake_system_state_, &dbus_),
90 mock_connection_manager(&fake_system_state_),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070091 loop_(nullptr) {
Gilad Arnold1f847232014-04-07 12:07:49 -070092 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070093 fake_system_state_.set_connection_manager(&mock_connection_manager);
94 fake_system_state_.set_update_attempter(&attempter_);
Gilad Arnold1f847232014-04-07 12:07:49 -070095
96 // Finish initializing the attempter.
97 attempter_.Init();
Alex Deymo3e0b53e2014-08-12 23:12:25 -070098
99 // We set the set_good_kernel command to a non-existent path so it fails to
100 // run it. This avoids the async call to the command and continues the
101 // update process right away. Tests testing that behavior can override the
102 // default set_good_kernel command if needed.
103 attempter_.set_good_kernel_cmd_ = "/path/to/non-existent/command";
Jay Srinivasan43488792012-06-19 00:25:31 -0700104 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700105
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700106 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700107 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
108
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700109 EXPECT_EQ(nullptr, attempter_.dbus_service_);
110 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700111 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800112 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700113 EXPECT_EQ(nullptr, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700114 EXPECT_FALSE(attempter_.download_active_);
115 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
116 EXPECT_EQ(0.0, attempter_.download_progress_);
117 EXPECT_EQ(0, attempter_.last_checked_time_);
118 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700119 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800120 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700121 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700122 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700123
124 // Set up store/load semantics of P2P properties via the mock PayloadState.
125 actual_using_p2p_for_downloading_ = false;
126 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
127 SetUsingP2PForDownloading(_))
128 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
129 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
130 GetUsingP2PForDownloading())
131 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
132 actual_using_p2p_for_sharing_ = false;
133 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
134 SetUsingP2PForSharing(_))
135 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
136 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
137 GetUsingP2PForDownloading())
138 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700139 }
140
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700141 virtual void TearDown() {
142 utils::RecursiveUnlinkDir(test_dir_);
143 }
144
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200145 void QuitMainLoop();
146 static gboolean StaticQuitMainLoop(gpointer data);
147
Darin Petkove6ef2f82011-03-07 17:31:11 -0800148 void UpdateTestStart();
149 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800150 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800151 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700152 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800153 static gboolean StaticUpdateTestStart(gpointer data);
154 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700155 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800156 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700157 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
158 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200159
Thieu Le116fda32011-04-19 11:01:54 -0700160 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700161 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200162
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700163 void ReadScatterFactorFromPolicyTestStart();
164 static gboolean StaticReadScatterFactorFromPolicyTestStart(
165 gpointer data);
166
167 void DecrementUpdateCheckCountTestStart();
168 static gboolean StaticDecrementUpdateCheckCountTestStart(
169 gpointer data);
170
Jay Srinivasan08fce042012-06-07 16:31:01 -0700171 void NoScatteringDoneDuringManualUpdateTestStart();
172 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
173 gpointer data);
174
David Zeuthen8f191b22013-08-06 12:27:50 -0700175 void P2PNotEnabledStart();
176 static gboolean StaticP2PNotEnabled(gpointer data);
177
178 void P2PEnabledStart();
179 static gboolean StaticP2PEnabled(gpointer data);
180
181 void P2PEnabledInteractiveStart();
182 static gboolean StaticP2PEnabledInteractive(gpointer data);
183
184 void P2PEnabledStartingFailsStart();
185 static gboolean StaticP2PEnabledStartingFails(gpointer data);
186
187 void P2PEnabledHousekeepingFailsStart();
188 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
189
Gilad Arnold74b5f552014-10-07 08:17:16 -0700190 bool actual_using_p2p_for_downloading() {
191 return actual_using_p2p_for_downloading_;
192 }
193 bool actual_using_p2p_for_sharing() {
194 return actual_using_p2p_for_sharing_;
195 }
196
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700197 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800198 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700199 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800200 NiceMock<ActionProcessorMock>* processor_;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700201 NiceMock<PrefsMock>* prefs_; // shortcut to fake_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800202 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800203 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700204
205 string test_dir_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700206
207 bool actual_using_p2p_for_downloading_;
208 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700209};
210
Darin Petkov1b003102010-11-30 10:18:36 -0800211TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700212 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800213 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700214 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800215 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700216 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800217 EXPECT_EQ(503, attempter_.http_response_code());
218 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700219 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800220}
221
222TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
223 ActionMock action;
224 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
225 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800226 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800227 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700228 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
229 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800230}
231
232TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700233 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800234 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700235 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800236 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800237 ObjectCollectorAction<OmahaResponse> collector_action;
238 BondActions(&action, &collector_action);
239 OmahaResponse response;
240 response.poll_interval = 234;
241 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800242 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700243 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800244 EXPECT_EQ(500, attempter_.http_response_code());
245 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700246 EXPECT_EQ(234, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700247 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800248}
249
Darin Petkovcd1666f2010-09-23 09:53:44 -0700250TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700251 string test_update_completed_marker;
252 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800253 "update_attempter_unittest-update_completed_marker-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700254 &test_update_completed_marker, nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700255 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700256 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700257 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700258 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700259 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700260 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700261}
262
263TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700264 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
265 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700266 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700267 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700268
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700269 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700270 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
271 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700272 EXPECT_EQ(ErrorCode::kOmahaRequestError,
273 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700274 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700275 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700276 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700277 ErrorCode::kError));
Alex Deymo42432912013-07-12 20:21:15 -0700278 FilesystemCopierAction filesystem_copier_action(
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700279 &fake_system_state_, false, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700280 EXPECT_EQ(ErrorCode::kFilesystemCopierError,
281 GetErrorCodeForAction(&filesystem_copier_action,
282 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800283 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700284 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700285 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700286 ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700287 ActionMock action_mock;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700288 EXPECT_CALL(action_mock, Type()).WillOnce(Return("ActionMock"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700289 EXPECT_EQ(ErrorCode::kError,
290 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700291}
292
Darin Petkov36275772010-10-01 11:40:57 -0700293TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700294 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800295 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700296 .WillOnce(Return(false));
297 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700298 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800299 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700300 .WillOnce(DoAll(
301 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
302 Return(true)));
303 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700304 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800305 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700306 .WillOnce(DoAll(
307 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
308 Return(true)));
309 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700310 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800311 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700312 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700313 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700314}
315
316TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800317 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700318 .WillOnce(Return(false))
319 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
320 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
321 .WillOnce(DoAll(
322 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
323 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800324 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700325 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800326 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700327 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800328 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700329 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700330 for (int i = 0; i < 4; i ++)
331 attempter_.MarkDeltaUpdateFailure();
332}
333
Darin Petkov1b003102010-11-30 10:18:36 -0800334TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
335 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
336 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700337 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800338 .Times(0);
339 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700340 string url1 = "http://url1";
341 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800342 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700343 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700344 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700345 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800346 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700347 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800348}
349
350TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
351 EXPECT_CALL(*processor_,
352 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700353 OmahaRequestAction::StaticType())));
354 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700355 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700356 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800357 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
358 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800359 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800360 attempter_.ScheduleErrorEventAction();
361 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
362}
363
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200364void UpdateAttempterTest::QuitMainLoop() {
365 g_main_loop_quit(loop_);
366}
367
368gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
369 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
370 return FALSE;
371}
372
Darin Petkove6ef2f82011-03-07 17:31:11 -0800373gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
374 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
375 return FALSE;
376}
377
378gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
379 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
380 return FALSE;
381}
382
Chris Sosa76a29ae2013-07-11 17:59:24 -0700383gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800384 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700385 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800386 return FALSE;
387}
388
389gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
390 gpointer data) {
391 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700392 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700393 return FALSE;
394}
395
396gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800397 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700398 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700399 return FALSE;
400}
401
402gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
403 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
404 return FALSE;
405}
406
Thieu Le116fda32011-04-19 11:01:54 -0700407gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
408 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
409 return FALSE;
410}
411
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700412gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
413 gpointer data) {
414 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
415 ua_test->ReadScatterFactorFromPolicyTestStart();
416 return FALSE;
417}
418
419gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
420 gpointer data) {
421 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
422 ua_test->DecrementUpdateCheckCountTestStart();
423 return FALSE;
424}
425
Jay Srinivasan08fce042012-06-07 16:31:01 -0700426gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
427 gpointer data) {
428 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
429 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
430 return FALSE;
431}
432
Darin Petkove6ef2f82011-03-07 17:31:11 -0800433namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700434// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700435const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800436 OmahaRequestAction::StaticType(),
437 OmahaResponseHandlerAction::StaticType(),
438 FilesystemCopierAction::StaticType(),
439 FilesystemCopierAction::StaticType(),
440 OmahaRequestAction::StaticType(),
441 DownloadAction::StaticType(),
442 OmahaRequestAction::StaticType(),
443 FilesystemCopierAction::StaticType(),
444 FilesystemCopierAction::StaticType(),
445 PostinstallRunnerAction::StaticType(),
446 OmahaRequestAction::StaticType()
447};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700448
449// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700450const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700451 InstallPlanAction::StaticType(),
452 PostinstallRunnerAction::StaticType(),
453};
454
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700455} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800456
457void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700458 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700459
460 // Expect that the device policy is loaded by the UpdateAttempter at some
461 // point by calling RefreshDevicePolicy.
462 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
463 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
464 EXPECT_CALL(*device_policy, LoadPolicy())
465 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
466
467 {
468 InSequence s;
469 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
470 EXPECT_CALL(*processor_,
471 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700472 kUpdateActionTypes[i])));
Alex Deymo749ecf12014-10-21 20:06:57 -0700473 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700474 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700475 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700476
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700477 attempter_.Update("", "", "", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800478 g_idle_add(&StaticUpdateTestVerify, this);
479}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700480
Darin Petkove6ef2f82011-03-07 17:31:11 -0800481void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700482 EXPECT_EQ(0, attempter_.http_response_code());
483 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700484 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
485 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
486 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700487 }
488 EXPECT_EQ(attempter_.response_handler_action_.get(),
489 attempter_.actions_[1].get());
490 DownloadAction* download_action =
491 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700492 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700493 EXPECT_EQ(&attempter_, download_action->delegate());
494 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800495 g_main_loop_quit(loop_);
496}
497
Chris Sosa28e479c2013-07-12 11:39:53 -0700498void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700499 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700500 // Create a device policy so that we can change settings.
501 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
502 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
503
504 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700505 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700506
Don Garrett6646b442013-11-13 15:29:11 -0800507 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700508 // References bootable kernels in fake_hardware.h
509 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800510 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700511 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800512 rollback_kernel);
513 }
514
Chris Sosa28e479c2013-07-12 11:39:53 -0700515 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700516
Chris Sosad38b1132014-03-25 10:43:59 -0700517 // We only allow rollback on devices that are not enterprise enrolled and
518 // which have a valid slot to rollback to.
519 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700520 is_rollback_allowed = true;
521 }
522
Don Garrett6646b442013-11-13 15:29:11 -0800523 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700524 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800525 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700526 DoAll(SetArgumentPointee<0>(string("")),
Chris Sosa28e479c2013-07-12 11:39:53 -0700527 Return(true)));
528 } else {
529 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800530 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700531 DoAll(SetArgumentPointee<0>(string("fake.mail@fake.com")),
Chris Sosa76a29ae2013-07-11 17:59:24 -0700532 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700533 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700534
Chris Sosa28e479c2013-07-12 11:39:53 -0700535 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700536 InSequence s;
537 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
538 EXPECT_CALL(*processor_,
539 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700540 kRollbackActionTypes[i])));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700541 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700542 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700543
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700544 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700545 g_idle_add(&StaticRollbackTestVerify, this);
546 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700547 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700548 g_main_loop_quit(loop_);
549 }
550}
551
552void UpdateAttempterTest::RollbackTestVerify() {
553 // Verifies the actions that were enqueued.
554 EXPECT_EQ(&attempter_, processor_->delegate());
555 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
556 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
557 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
558 }
559 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
560 InstallPlanAction* install_plan_action =
561 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
562 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700563 // Matches fake_hardware.h -> rollback should move from kernel/boot device
564 // pair to other pair.
565 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
566 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700567 EXPECT_EQ(install_plan->powerwash_required, true);
568 g_main_loop_quit(loop_);
569}
570
Darin Petkove6ef2f82011-03-07 17:31:11 -0800571TEST_F(UpdateAttempterTest, UpdateTest) {
572 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
573 g_idle_add(&StaticUpdateTestStart, this);
574 g_main_loop_run(loop_);
575 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700576 loop_ = nullptr;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700577}
578
Chris Sosa76a29ae2013-07-11 17:59:24 -0700579TEST_F(UpdateAttempterTest, RollbackTest) {
580 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
581 g_idle_add(&StaticRollbackTestStart, this);
582 g_main_loop_run(loop_);
583 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700584 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700585}
586
Don Garrett6646b442013-11-13 15:29:11 -0800587TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
588 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
589 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
590 g_main_loop_run(loop_);
591 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700592 loop_ = nullptr;
Don Garrett6646b442013-11-13 15:29:11 -0800593}
594
Chris Sosa76a29ae2013-07-11 17:59:24 -0700595TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
596 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
597 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
598 g_main_loop_run(loop_);
599 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700600 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700601}
602
Thieu Le116fda32011-04-19 11:01:54 -0700603void UpdateAttempterTest::PingOmahaTestStart() {
604 EXPECT_CALL(*processor_,
605 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700606 OmahaRequestAction::StaticType())));
607 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700608 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200609 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700610}
611
612TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700613 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
614 EXPECT_FALSE(attempter_.schedule_updates_called());
615 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
616 // testing, which is more permissive than we want to handle here.
617 attempter_.DisableScheduleUpdates();
Thieu Le116fda32011-04-19 11:01:54 -0700618 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
619 g_idle_add(&StaticPingOmahaTestStart, this);
620 g_main_loop_run(loop_);
621 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700622 loop_ = nullptr;
Thieu Le116fda32011-04-19 11:01:54 -0700623 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700624 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700625}
626
Darin Petkov18c7bce2011-06-16 14:07:00 -0700627TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
628 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700629 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700630 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700631 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700632 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
633 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700634 EXPECT_EQ(
635 static_cast<ErrorCode>(static_cast<int>(kCode) |
636 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
637 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700638}
639
640TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
641 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700642 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700643 response_action->install_plan_.is_resume = true;
644 attempter_.response_handler_action_.reset(response_action);
645 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700646 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700647 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700648 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700649 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
650 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700651 EXPECT_EQ(
652 static_cast<ErrorCode>(
653 static_cast<int>(kCode) |
654 static_cast<int>(ErrorCode::kResumedFlag) |
655 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
656 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700657}
658
David Zeuthen8f191b22013-08-06 12:27:50 -0700659TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
660 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700661 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700662 mock_p2p_manager.fake().SetP2PEnabled(false);
663 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
664 attempter_.UpdateEngineStarted();
665}
666
667TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
668 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700669 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700670 mock_p2p_manager.fake().SetP2PEnabled(true);
671 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
672 attempter_.UpdateEngineStarted();
673}
674
675TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
676 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700677 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700678 mock_p2p_manager.fake().SetP2PEnabled(true);
679 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700680 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700681 attempter_.UpdateEngineStarted();
682}
683
684TEST_F(UpdateAttempterTest, P2PNotEnabled) {
685 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
686 g_idle_add(&StaticP2PNotEnabled, this);
687 g_main_loop_run(loop_);
688 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700689 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700690}
691gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
692 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
693 ua_test->P2PNotEnabledStart();
694 return FALSE;
695}
696void UpdateAttempterTest::P2PNotEnabledStart() {
697 // If P2P is not enabled, check that we do not attempt housekeeping
698 // and do not convey that p2p is to be used.
699 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700700 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700701 mock_p2p_manager.fake().SetP2PEnabled(false);
702 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700703 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700704 EXPECT_FALSE(actual_using_p2p_for_downloading());
705 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700706 g_idle_add(&StaticQuitMainLoop, this);
707}
708
709TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
710 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
711 g_idle_add(&StaticP2PEnabledStartingFails, this);
712 g_main_loop_run(loop_);
713 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700714 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700715}
716gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
717 gpointer data) {
718 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
719 ua_test->P2PEnabledStartingFailsStart();
720 return FALSE;
721}
722void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
723 // If p2p is enabled, but starting it fails ensure we don't do
724 // any housekeeping and do not convey that p2p should be used.
725 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700726 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700727 mock_p2p_manager.fake().SetP2PEnabled(true);
728 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
729 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
730 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700731 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700732 EXPECT_FALSE(actual_using_p2p_for_downloading());
733 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700734 g_idle_add(&StaticQuitMainLoop, this);
735}
736
737TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
738 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
739 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
740 g_main_loop_run(loop_);
741 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700742 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700743}
744gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
745 gpointer data) {
746 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
747 ua_test->P2PEnabledHousekeepingFailsStart();
748 return FALSE;
749}
750void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
751 // If p2p is enabled, starting it works but housekeeping fails, ensure
752 // we do not convey p2p is to be used.
753 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700754 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700755 mock_p2p_manager.fake().SetP2PEnabled(true);
756 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
757 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700758 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700759 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700760 EXPECT_FALSE(actual_using_p2p_for_downloading());
761 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700762 g_idle_add(&StaticQuitMainLoop, this);
763}
764
765TEST_F(UpdateAttempterTest, P2PEnabled) {
766 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
767 g_idle_add(&StaticP2PEnabled, this);
768 g_main_loop_run(loop_);
769 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700770 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700771}
772gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
773 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
774 ua_test->P2PEnabledStart();
775 return FALSE;
776}
777void UpdateAttempterTest::P2PEnabledStart() {
778 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700779 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700780 // If P2P is enabled and starting it works, check that we performed
781 // housekeeping and that we convey p2p should be used.
782 mock_p2p_manager.fake().SetP2PEnabled(true);
783 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
784 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700785 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700786 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700787 EXPECT_TRUE(actual_using_p2p_for_downloading());
788 EXPECT_TRUE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700789 g_idle_add(&StaticQuitMainLoop, this);
790}
791
792TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
793 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
794 g_idle_add(&StaticP2PEnabledInteractive, this);
795 g_main_loop_run(loop_);
796 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700797 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700798}
799gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
800 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
801 ua_test->P2PEnabledInteractiveStart();
802 return FALSE;
803}
804void UpdateAttempterTest::P2PEnabledInteractiveStart() {
805 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700806 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700807 // For an interactive check, if P2P is enabled and starting it
808 // works, check that we performed housekeeping and that we convey
809 // p2p should be used for sharing but NOT for downloading.
810 mock_p2p_manager.fake().SetP2PEnabled(true);
811 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
812 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700813 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700814 attempter_.Update("", "", "", "", false, true /* interactive */);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700815 EXPECT_FALSE(actual_using_p2p_for_downloading());
816 EXPECT_TRUE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700817 g_idle_add(&StaticQuitMainLoop, this);
818}
819
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700820TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
821 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
822 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
823 g_main_loop_run(loop_);
824 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700825 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700826}
827
828// Tests that the scatter_factor_in_seconds value is properly fetched
829// from the device policy.
830void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700831 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700832
833 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
834 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
835
836 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700837 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700838
839 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
840 .WillRepeatedly(DoAll(
841 SetArgumentPointee<0>(scatter_factor_in_seconds),
842 Return(true)));
843
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700844 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700845 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
846
847 g_idle_add(&StaticQuitMainLoop, this);
848}
849
850TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
851 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
852 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
853 g_main_loop_run(loop_);
854 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700855 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700856}
857
858void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
859 // Tests that the scatter_factor_in_seconds value is properly fetched
860 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700861 int64_t initial_value = 5;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700862 Prefs prefs;
863 attempter_.prefs_ = &prefs;
864
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700865 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700866
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700867 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800868 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700869 &prefs_dir));
870 ScopedDirRemover temp_dir_remover(prefs_dir);
871
Alex Vakulenko75039d72014-03-25 12:36:28 -0700872 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700873 << "Failed to initialize preferences.";
874 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
875
Ben Chan9abb7632014-08-07 00:10:53 -0700876 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700877
878 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
879 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
880
881 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700882 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700883
884 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
885 .WillRepeatedly(DoAll(
886 SetArgumentPointee<0>(scatter_factor_in_seconds),
887 Return(true)));
888
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700889 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700890 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
891
892 // Make sure the file still exists.
893 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
894
Ben Chan9abb7632014-08-07 00:10:53 -0700895 int64_t new_value;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700896 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
897 EXPECT_EQ(initial_value - 1, new_value);
898
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700899 EXPECT_TRUE(
900 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700901
902 // However, if the count is already 0, it's not decremented. Test that.
903 initial_value = 0;
904 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700905 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700906 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
907 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
908 EXPECT_EQ(initial_value, new_value);
909
910 g_idle_add(&StaticQuitMainLoop, this);
911}
912
Jay Srinivasan08fce042012-06-07 16:31:01 -0700913TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
914 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
915 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
916 g_main_loop_run(loop_);
917 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700918 loop_ = nullptr;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700919}
920
921void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
922 // Tests that no scattering logic is enabled if the update check
923 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700924 int64_t initial_value = 8;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700925 Prefs prefs;
926 attempter_.prefs_ = &prefs;
927
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700928 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700929
930 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800931 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -0700932 &prefs_dir));
933 ScopedDirRemover temp_dir_remover(prefs_dir);
934
Alex Vakulenko75039d72014-03-25 12:36:28 -0700935 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -0700936 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700937 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700938 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
939
940 // make sure scatter_factor is non-zero as scattering is disabled
941 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700942 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700943
944 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
945 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
946
947 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700948 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700949
950 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
951 .WillRepeatedly(DoAll(
952 SetArgumentPointee<0>(scatter_factor_in_seconds),
953 Return(true)));
954
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800955 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700956 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700957 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
958
959 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700960 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700961 EXPECT_FALSE(
962 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700963 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700964 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
965 EXPECT_FALSE(
966 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700967 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
968
969 g_idle_add(&StaticQuitMainLoop, this);
970}
971
David Zeuthen985b1122013-10-09 12:13:15 -0700972// Checks that we only report daily metrics at most every 24 hours.
973TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
974 FakeClock fake_clock;
975 Prefs prefs;
976 string temp_dir;
977
978 // We need persistent preferences for this test
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700979 EXPECT_TRUE(utils::MakeTempDirectory("UpdateAttempterTest.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -0700980 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700981 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700982 fake_system_state_.set_clock(&fake_clock);
983 fake_system_state_.set_prefs(&prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700984
985 Time epoch = Time::FromInternalValue(0);
986 fake_clock.SetWallclockTime(epoch);
987
988 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
989 // we should report.
990 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
991 // We should not report again if no time has passed.
992 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
993
994 // We should not report if only 10 hours has passed.
995 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
996 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
997
998 // We should not report if only 24 hours - 1 sec has passed.
999 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1000 TimeDelta::FromSeconds(1));
1001 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1002
1003 // We should report if 24 hours has passed.
1004 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1005 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1006
1007 // But then we should not report again..
1008 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1009
1010 // .. until another 24 hours has passed
1011 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1012 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1013 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1014 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1015 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1016
1017 // .. and another 24 hours
1018 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1019 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1020 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1021 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1022 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1023
1024 // If the span between time of reporting and present time is
1025 // negative, we report. This is in order to reset the timestamp and
1026 // avoid an edge condition whereby a distant point in the future is
1027 // in the state variable resulting in us never ever reporting again.
1028 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1029 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1030 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1031
1032 // In this case we should not update until the clock reads 71 + 24 = 95.
1033 // Check that.
1034 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1035 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1036 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1037 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1038 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1039
1040 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1041}
1042
David Zeuthen3c55abd2013-10-14 12:48:03 -07001043TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1044 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001045 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001046 update_completed_marker);
1047
1048 FakeClock fake_clock;
1049 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001050 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001051
1052 Time boot_time;
1053 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1054
1055 attempter.WriteUpdateCompletedMarker();
1056
1057 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1058 EXPECT_EQ(boot_time.ToTimeT(), 42);
1059}
1060
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001061} // namespace chromeos_update_engine