blob: 1d1542b73ce6712de63851338b0521dd2e7ba7f3 [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>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070012#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020013#include <policy/libpolicy.h>
14#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070015
David Zeuthen985b1122013-10-09 12:13:15 -070016#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080017#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070018#include "update_engine/fake_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070019#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070020#include "update_engine/install_plan.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080021#include "update_engine/mock_action.h"
22#include "update_engine/mock_action_processor.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080023#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080024#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070025#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026#include "update_engine/mock_payload_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080027#include "update_engine/mock_prefs.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070029#include "update_engine/prefs.h"
Darin Petkov1b003102010-11-30 10:18:36 -080030#include "update_engine/test_utils.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070031#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070032
David Zeuthen985b1122013-10-09 12:13:15 -070033using base::Time;
34using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070035using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070036using std::unique_ptr;
Darin Petkov36275772010-10-01 11:40:57 -070037using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070038using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070039using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080040using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070041using testing::Property;
42using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070043using testing::ReturnPointee;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080044using testing::SaveArg;
Darin Petkov36275772010-10-01 11:40:57 -070045using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070046using testing::_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070047
48namespace chromeos_update_engine {
49
50// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070051// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070052// methods.
53class UpdateAttempterUnderTest : public UpdateAttempter {
54 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070055 // We always feed an explicit update completed marker name; however, unless
56 // explicitly specified, we feed an empty string, which causes the
57 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070058 UpdateAttempterUnderTest(SystemState* system_state,
59 DBusWrapperInterface* dbus_iface)
60 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070061
Gilad Arnold1f847232014-04-07 12:07:49 -070062 UpdateAttempterUnderTest(SystemState* system_state,
63 DBusWrapperInterface* dbus_iface,
Alex Deymof329b932014-10-30 01:37:48 -070064 const string& update_completed_marker)
Gilad Arnold1f847232014-04-07 12:07:49 -070065 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070066
67 // Wrap the update scheduling method, allowing us to opt out of scheduled
68 // updates for testing purposes.
69 void ScheduleUpdates() override {
70 schedule_updates_called_ = true;
71 if (do_schedule_updates_) {
72 UpdateAttempter::ScheduleUpdates();
73 } else {
74 LOG(INFO) << "[TEST] Update scheduling disabled.";
75 }
76 }
77 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
78 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
79
80 // Indicates whether ScheduleUpdates() was called.
81 bool schedule_updates_called() const { return schedule_updates_called_; }
82
83 private:
84 bool schedule_updates_called_ = false;
85 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070086};
87
88class UpdateAttempterTest : public ::testing::Test {
89 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070090 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -070091 : attempter_(&fake_system_state_, &dbus_),
92 mock_connection_manager(&fake_system_state_),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070093 loop_(nullptr) {
Gilad Arnold1f847232014-04-07 12:07:49 -070094 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070095 fake_system_state_.set_connection_manager(&mock_connection_manager);
96 fake_system_state_.set_update_attempter(&attempter_);
Gilad Arnold1f847232014-04-07 12:07:49 -070097
98 // Finish initializing the attempter.
99 attempter_.Init();
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700100
101 // We set the set_good_kernel command to a non-existent path so it fails to
102 // run it. This avoids the async call to the command and continues the
103 // update process right away. Tests testing that behavior can override the
104 // default set_good_kernel command if needed.
105 attempter_.set_good_kernel_cmd_ = "/path/to/non-existent/command";
Jay Srinivasan43488792012-06-19 00:25:31 -0700106 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700107
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700108 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700109 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
110
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700111 EXPECT_EQ(nullptr, attempter_.dbus_service_);
112 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700113 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800114 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700115 EXPECT_EQ(nullptr, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700116 EXPECT_FALSE(attempter_.download_active_);
117 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
118 EXPECT_EQ(0.0, attempter_.download_progress_);
119 EXPECT_EQ(0, attempter_.last_checked_time_);
120 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700121 EXPECT_EQ(0, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800122 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700123 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700124 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700125
126 // Set up store/load semantics of P2P properties via the mock PayloadState.
127 actual_using_p2p_for_downloading_ = false;
128 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
129 SetUsingP2PForDownloading(_))
130 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
131 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
132 GetUsingP2PForDownloading())
133 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
134 actual_using_p2p_for_sharing_ = false;
135 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
136 SetUsingP2PForSharing(_))
137 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
138 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
139 GetUsingP2PForDownloading())
140 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700141 }
142
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700143 virtual void TearDown() {
Alex Deymo10875d92014-11-10 21:52:57 -0800144 test_utils::RecursiveUnlinkDir(test_dir_);
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700145 }
146
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200147 void QuitMainLoop();
148 static gboolean StaticQuitMainLoop(gpointer data);
149
Darin Petkove6ef2f82011-03-07 17:31:11 -0800150 void UpdateTestStart();
151 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800152 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800153 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700154 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800155 static gboolean StaticUpdateTestStart(gpointer data);
156 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700157 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800158 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700159 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
160 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200161
Thieu Le116fda32011-04-19 11:01:54 -0700162 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700163 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200164
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700165 void ReadScatterFactorFromPolicyTestStart();
166 static gboolean StaticReadScatterFactorFromPolicyTestStart(
167 gpointer data);
168
169 void DecrementUpdateCheckCountTestStart();
170 static gboolean StaticDecrementUpdateCheckCountTestStart(
171 gpointer data);
172
Jay Srinivasan08fce042012-06-07 16:31:01 -0700173 void NoScatteringDoneDuringManualUpdateTestStart();
174 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
175 gpointer data);
176
David Zeuthen8f191b22013-08-06 12:27:50 -0700177 void P2PNotEnabledStart();
178 static gboolean StaticP2PNotEnabled(gpointer data);
179
180 void P2PEnabledStart();
181 static gboolean StaticP2PEnabled(gpointer data);
182
183 void P2PEnabledInteractiveStart();
184 static gboolean StaticP2PEnabledInteractive(gpointer data);
185
186 void P2PEnabledStartingFailsStart();
187 static gboolean StaticP2PEnabledStartingFails(gpointer data);
188
189 void P2PEnabledHousekeepingFailsStart();
190 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
191
Gilad Arnold74b5f552014-10-07 08:17:16 -0700192 bool actual_using_p2p_for_downloading() {
193 return actual_using_p2p_for_downloading_;
194 }
195 bool actual_using_p2p_for_sharing() {
196 return actual_using_p2p_for_sharing_;
197 }
198
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700199 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800200 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700201 UpdateAttempterUnderTest attempter_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800202 NiceMock<MockActionProcessor>* processor_;
203 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800204 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800205 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700206
207 string test_dir_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700208
209 bool actual_using_p2p_for_downloading_;
210 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700211};
212
Darin Petkov1b003102010-11-30 10:18:36 -0800213TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700214 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800215 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700216 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800217 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700218 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800219 EXPECT_EQ(503, attempter_.http_response_code());
220 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700221 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800222}
223
224TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800225 MockAction action;
226 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Darin Petkov1b003102010-11-30 10:18:36 -0800227 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800228 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800229 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700230 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
231 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800232}
233
234TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700235 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800236 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700237 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800238 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800239 ObjectCollectorAction<OmahaResponse> collector_action;
240 BondActions(&action, &collector_action);
241 OmahaResponse response;
242 response.poll_interval = 234;
243 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800244 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700245 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800246 EXPECT_EQ(500, attempter_.http_response_code());
247 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700248 EXPECT_EQ(234, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700249 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800250}
251
Darin Petkovcd1666f2010-09-23 09:53:44 -0700252TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700253 string test_update_completed_marker;
254 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800255 "update_attempter_unittest-update_completed_marker-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700256 &test_update_completed_marker, nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700257 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700258 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700259 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700260 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700261 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700262 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700263}
264
265TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700266 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
267 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700268 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700269 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700270
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700271 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700272 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
273 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700274 EXPECT_EQ(ErrorCode::kOmahaRequestError,
275 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700276 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700277 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700278 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700279 ErrorCode::kError));
Alex Deymo42432912013-07-12 20:21:15 -0700280 FilesystemCopierAction filesystem_copier_action(
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700281 &fake_system_state_, false, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700282 EXPECT_EQ(ErrorCode::kFilesystemCopierError,
283 GetErrorCodeForAction(&filesystem_copier_action,
284 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800285 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700286 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700287 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700288 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800289 MockAction action_mock;
290 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700291 EXPECT_EQ(ErrorCode::kError,
292 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700293}
294
Darin Petkov36275772010-10-01 11:40:57 -0700295TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700296 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800297 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700298 .WillOnce(Return(false));
299 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700300 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800301 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700302 .WillOnce(DoAll(
303 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
304 Return(true)));
305 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700306 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800307 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700308 .WillOnce(DoAll(
309 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
310 Return(true)));
311 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700312 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800313 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700314 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700315 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700316}
317
318TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700320 .WillOnce(Return(false))
321 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
322 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
323 .WillOnce(DoAll(
324 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
325 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800326 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700327 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800328 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700329 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800330 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700331 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700332 for (int i = 0; i < 4; i ++)
333 attempter_.MarkDeltaUpdateFailure();
334}
335
Darin Petkov1b003102010-11-30 10:18:36 -0800336TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
337 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
338 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700339 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800340 .Times(0);
341 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700342 string url1 = "http://url1";
343 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800344 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700345 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700346 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700347 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800348 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700349 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800350}
351
352TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
353 EXPECT_CALL(*processor_,
354 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700355 OmahaRequestAction::StaticType())));
356 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700357 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700358 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800359 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
360 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800361 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800362 attempter_.ScheduleErrorEventAction();
363 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
364}
365
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200366void UpdateAttempterTest::QuitMainLoop() {
367 g_main_loop_quit(loop_);
368}
369
370gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
371 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
372 return FALSE;
373}
374
Darin Petkove6ef2f82011-03-07 17:31:11 -0800375gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
376 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
377 return FALSE;
378}
379
380gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
381 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
382 return FALSE;
383}
384
Chris Sosa76a29ae2013-07-11 17:59:24 -0700385gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800386 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700387 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800388 return FALSE;
389}
390
391gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
392 gpointer data) {
393 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700394 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700395 return FALSE;
396}
397
398gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800399 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700400 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700401 return FALSE;
402}
403
404gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
405 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
406 return FALSE;
407}
408
Thieu Le116fda32011-04-19 11:01:54 -0700409gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
410 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
411 return FALSE;
412}
413
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700414gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
415 gpointer data) {
416 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
417 ua_test->ReadScatterFactorFromPolicyTestStart();
418 return FALSE;
419}
420
421gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
422 gpointer data) {
423 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
424 ua_test->DecrementUpdateCheckCountTestStart();
425 return FALSE;
426}
427
Jay Srinivasan08fce042012-06-07 16:31:01 -0700428gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
429 gpointer data) {
430 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
431 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
432 return FALSE;
433}
434
Darin Petkove6ef2f82011-03-07 17:31:11 -0800435namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700436// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700437const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800438 OmahaRequestAction::StaticType(),
439 OmahaResponseHandlerAction::StaticType(),
440 FilesystemCopierAction::StaticType(),
441 FilesystemCopierAction::StaticType(),
442 OmahaRequestAction::StaticType(),
443 DownloadAction::StaticType(),
444 OmahaRequestAction::StaticType(),
445 FilesystemCopierAction::StaticType(),
446 FilesystemCopierAction::StaticType(),
447 PostinstallRunnerAction::StaticType(),
448 OmahaRequestAction::StaticType()
449};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700450
451// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700452const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700453 InstallPlanAction::StaticType(),
454 PostinstallRunnerAction::StaticType(),
455};
456
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700457} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800458
459void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700460 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700461
462 // Expect that the device policy is loaded by the UpdateAttempter at some
463 // point by calling RefreshDevicePolicy.
464 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
465 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
466 EXPECT_CALL(*device_policy, LoadPolicy())
467 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
468
469 {
470 InSequence s;
471 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
472 EXPECT_CALL(*processor_,
473 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700474 kUpdateActionTypes[i])));
Alex Deymo749ecf12014-10-21 20:06:57 -0700475 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700476 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700477 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700478
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700479 attempter_.Update("", "", "", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800480 g_idle_add(&StaticUpdateTestVerify, this);
481}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700482
Darin Petkove6ef2f82011-03-07 17:31:11 -0800483void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700484 EXPECT_EQ(0, attempter_.http_response_code());
485 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700486 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
487 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
488 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700489 }
490 EXPECT_EQ(attempter_.response_handler_action_.get(),
491 attempter_.actions_[1].get());
492 DownloadAction* download_action =
493 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700494 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700495 EXPECT_EQ(&attempter_, download_action->delegate());
496 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800497 g_main_loop_quit(loop_);
498}
499
Chris Sosa28e479c2013-07-12 11:39:53 -0700500void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700501 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700502 // Create a device policy so that we can change settings.
503 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
504 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
505
506 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700507 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700508
Don Garrett6646b442013-11-13 15:29:11 -0800509 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700510 // References bootable kernels in fake_hardware.h
511 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800512 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700513 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800514 rollback_kernel);
515 }
516
Chris Sosa28e479c2013-07-12 11:39:53 -0700517 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700518
Chris Sosad38b1132014-03-25 10:43:59 -0700519 // We only allow rollback on devices that are not enterprise enrolled and
520 // which have a valid slot to rollback to.
521 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700522 is_rollback_allowed = true;
523 }
524
Don Garrett6646b442013-11-13 15:29:11 -0800525 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700526 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800527 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700528 DoAll(SetArgumentPointee<0>(string("")),
Chris Sosa28e479c2013-07-12 11:39:53 -0700529 Return(true)));
530 } else {
531 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800532 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700533 DoAll(SetArgumentPointee<0>(string("fake.mail@fake.com")),
Chris Sosa76a29ae2013-07-11 17:59:24 -0700534 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700535 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700536
Chris Sosa28e479c2013-07-12 11:39:53 -0700537 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700538 InSequence s;
539 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
540 EXPECT_CALL(*processor_,
541 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700542 kRollbackActionTypes[i])));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700543 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700544 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700545
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700546 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700547 g_idle_add(&StaticRollbackTestVerify, this);
548 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700549 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700550 g_main_loop_quit(loop_);
551 }
552}
553
554void UpdateAttempterTest::RollbackTestVerify() {
555 // Verifies the actions that were enqueued.
556 EXPECT_EQ(&attempter_, processor_->delegate());
557 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
558 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
559 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
560 }
561 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
562 InstallPlanAction* install_plan_action =
563 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
564 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700565 // Matches fake_hardware.h -> rollback should move from kernel/boot device
566 // pair to other pair.
567 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
568 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700569 EXPECT_EQ(install_plan->powerwash_required, true);
570 g_main_loop_quit(loop_);
571}
572
Darin Petkove6ef2f82011-03-07 17:31:11 -0800573TEST_F(UpdateAttempterTest, UpdateTest) {
574 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
575 g_idle_add(&StaticUpdateTestStart, this);
576 g_main_loop_run(loop_);
577 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700578 loop_ = nullptr;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700579}
580
Chris Sosa76a29ae2013-07-11 17:59:24 -0700581TEST_F(UpdateAttempterTest, RollbackTest) {
582 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
583 g_idle_add(&StaticRollbackTestStart, this);
584 g_main_loop_run(loop_);
585 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700586 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700587}
588
Don Garrett6646b442013-11-13 15:29:11 -0800589TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
590 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
591 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
592 g_main_loop_run(loop_);
593 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700594 loop_ = nullptr;
Don Garrett6646b442013-11-13 15:29:11 -0800595}
596
Chris Sosa76a29ae2013-07-11 17:59:24 -0700597TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
598 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
599 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
600 g_main_loop_run(loop_);
601 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700602 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700603}
604
Thieu Le116fda32011-04-19 11:01:54 -0700605void UpdateAttempterTest::PingOmahaTestStart() {
606 EXPECT_CALL(*processor_,
607 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700608 OmahaRequestAction::StaticType())));
609 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700610 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200611 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700612}
613
614TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700615 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
616 EXPECT_FALSE(attempter_.schedule_updates_called());
617 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
618 // testing, which is more permissive than we want to handle here.
619 attempter_.DisableScheduleUpdates();
Thieu Le116fda32011-04-19 11:01:54 -0700620 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
621 g_idle_add(&StaticPingOmahaTestStart, this);
622 g_main_loop_run(loop_);
623 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700624 loop_ = nullptr;
Thieu Le116fda32011-04-19 11:01:54 -0700625 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700626 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700627}
628
Darin Petkov18c7bce2011-06-16 14:07:00 -0700629TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800630 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700631 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700632 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700633 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700634 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
635 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700636 EXPECT_EQ(
637 static_cast<ErrorCode>(static_cast<int>(kCode) |
638 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
639 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700640}
641
642TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
643 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700644 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700645 response_action->install_plan_.is_resume = true;
646 attempter_.response_handler_action_.reset(response_action);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800647 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700648 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700649 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700650 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700651 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
652 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700653 EXPECT_EQ(
654 static_cast<ErrorCode>(
655 static_cast<int>(kCode) |
656 static_cast<int>(ErrorCode::kResumedFlag) |
657 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
658 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700659}
660
David Zeuthen8f191b22013-08-06 12:27:50 -0700661TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
662 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700663 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700664 mock_p2p_manager.fake().SetP2PEnabled(false);
665 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
666 attempter_.UpdateEngineStarted();
667}
668
669TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
670 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700671 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700672 mock_p2p_manager.fake().SetP2PEnabled(true);
673 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
674 attempter_.UpdateEngineStarted();
675}
676
677TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
678 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700679 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700680 mock_p2p_manager.fake().SetP2PEnabled(true);
681 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700682 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700683 attempter_.UpdateEngineStarted();
684}
685
686TEST_F(UpdateAttempterTest, P2PNotEnabled) {
687 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
688 g_idle_add(&StaticP2PNotEnabled, this);
689 g_main_loop_run(loop_);
690 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700691 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700692}
693gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
694 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
695 ua_test->P2PNotEnabledStart();
696 return FALSE;
697}
698void UpdateAttempterTest::P2PNotEnabledStart() {
699 // If P2P is not enabled, check that we do not attempt housekeeping
700 // and do not convey that p2p is to be used.
701 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700702 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700703 mock_p2p_manager.fake().SetP2PEnabled(false);
704 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700705 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700706 EXPECT_FALSE(actual_using_p2p_for_downloading());
707 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700708 g_idle_add(&StaticQuitMainLoop, this);
709}
710
711TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
712 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
713 g_idle_add(&StaticP2PEnabledStartingFails, this);
714 g_main_loop_run(loop_);
715 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700716 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700717}
718gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
719 gpointer data) {
720 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
721 ua_test->P2PEnabledStartingFailsStart();
722 return FALSE;
723}
724void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
725 // If p2p is enabled, but starting it fails ensure we don't do
726 // any housekeeping and do not convey that p2p should be used.
727 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700728 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700729 mock_p2p_manager.fake().SetP2PEnabled(true);
730 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
731 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
732 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700733 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700734 EXPECT_FALSE(actual_using_p2p_for_downloading());
735 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700736 g_idle_add(&StaticQuitMainLoop, this);
737}
738
739TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
740 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
741 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
742 g_main_loop_run(loop_);
743 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700744 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700745}
746gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
747 gpointer data) {
748 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
749 ua_test->P2PEnabledHousekeepingFailsStart();
750 return FALSE;
751}
752void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
753 // If p2p is enabled, starting it works but housekeeping fails, ensure
754 // we do not convey p2p is to be used.
755 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700756 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700757 mock_p2p_manager.fake().SetP2PEnabled(true);
758 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
759 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700760 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700761 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700762 EXPECT_FALSE(actual_using_p2p_for_downloading());
763 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700764 g_idle_add(&StaticQuitMainLoop, this);
765}
766
767TEST_F(UpdateAttempterTest, P2PEnabled) {
768 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
769 g_idle_add(&StaticP2PEnabled, this);
770 g_main_loop_run(loop_);
771 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700772 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700773}
774gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
775 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
776 ua_test->P2PEnabledStart();
777 return FALSE;
778}
779void UpdateAttempterTest::P2PEnabledStart() {
780 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700781 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700782 // If P2P is enabled and starting it works, check that we performed
783 // housekeeping and that we convey p2p should be used.
784 mock_p2p_manager.fake().SetP2PEnabled(true);
785 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
786 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700787 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700788 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700789 EXPECT_TRUE(actual_using_p2p_for_downloading());
790 EXPECT_TRUE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700791 g_idle_add(&StaticQuitMainLoop, this);
792}
793
794TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
795 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
796 g_idle_add(&StaticP2PEnabledInteractive, this);
797 g_main_loop_run(loop_);
798 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700799 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700800}
801gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
802 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
803 ua_test->P2PEnabledInteractiveStart();
804 return FALSE;
805}
806void UpdateAttempterTest::P2PEnabledInteractiveStart() {
807 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700808 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700809 // For an interactive check, if P2P is enabled and starting it
810 // works, check that we performed housekeeping and that we convey
811 // p2p should be used for sharing but NOT for downloading.
812 mock_p2p_manager.fake().SetP2PEnabled(true);
813 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
814 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700815 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700816 attempter_.Update("", "", "", "", false, true /* interactive */);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700817 EXPECT_FALSE(actual_using_p2p_for_downloading());
818 EXPECT_TRUE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700819 g_idle_add(&StaticQuitMainLoop, this);
820}
821
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700822TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
823 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
824 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
825 g_main_loop_run(loop_);
826 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700827 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700828}
829
830// Tests that the scatter_factor_in_seconds value is properly fetched
831// from the device policy.
832void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700833 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700834
835 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
836 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
837
838 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700839 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700840
841 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
842 .WillRepeatedly(DoAll(
843 SetArgumentPointee<0>(scatter_factor_in_seconds),
844 Return(true)));
845
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700846 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700847 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
848
849 g_idle_add(&StaticQuitMainLoop, this);
850}
851
852TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
853 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
854 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
855 g_main_loop_run(loop_);
856 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700857 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700858}
859
860void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
861 // Tests that the scatter_factor_in_seconds value is properly fetched
862 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700863 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800864 FakePrefs fake_prefs;
865 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700866
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700867 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700868
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800869 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700870
Ben Chan9abb7632014-08-07 00:10:53 -0700871 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700872
873 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
874 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
875
876 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700877 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700878
879 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
880 .WillRepeatedly(DoAll(
881 SetArgumentPointee<0>(scatter_factor_in_seconds),
882 Return(true)));
883
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700884 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700885 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
886
887 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800888 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700889
Ben Chan9abb7632014-08-07 00:10:53 -0700890 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800891 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700892 EXPECT_EQ(initial_value - 1, new_value);
893
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700894 EXPECT_TRUE(
895 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700896
897 // However, if the count is already 0, it's not decremented. Test that.
898 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800899 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700900 attempter_.Update("", "", "", "", false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800901 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
902 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700903 EXPECT_EQ(initial_value, new_value);
904
905 g_idle_add(&StaticQuitMainLoop, this);
906}
907
Jay Srinivasan08fce042012-06-07 16:31:01 -0700908TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
909 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
910 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
911 g_main_loop_run(loop_);
912 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700913 loop_ = nullptr;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700914}
915
916void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
917 // Tests that no scattering logic is enabled if the update check
918 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700919 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800920 FakePrefs fake_prefs;
921 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700922
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700923 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800924 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700925
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800926 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
927 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700928
929 // make sure scatter_factor is non-zero as scattering is disabled
930 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700931 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700932
933 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
934 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
935
936 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700937 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700938
939 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
940 .WillRepeatedly(DoAll(
941 SetArgumentPointee<0>(scatter_factor_in_seconds),
942 Return(true)));
943
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800944 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700945 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700946 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
947
948 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700949 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700950 EXPECT_FALSE(
951 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800952 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700953 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
954 EXPECT_FALSE(
955 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800956 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700957
958 g_idle_add(&StaticQuitMainLoop, this);
959}
960
David Zeuthen985b1122013-10-09 12:13:15 -0700961// Checks that we only report daily metrics at most every 24 hours.
962TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
963 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800964 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -0700965
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700966 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800967 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700968
969 Time epoch = Time::FromInternalValue(0);
970 fake_clock.SetWallclockTime(epoch);
971
972 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
973 // we should report.
974 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
975 // We should not report again if no time has passed.
976 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
977
978 // We should not report if only 10 hours has passed.
979 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
980 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
981
982 // We should not report if only 24 hours - 1 sec has passed.
983 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
984 TimeDelta::FromSeconds(1));
985 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
986
987 // We should report if 24 hours has passed.
988 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
989 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
990
991 // But then we should not report again..
992 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
993
994 // .. until another 24 hours has passed
995 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
996 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
997 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
998 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
999 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1000
1001 // .. and another 24 hours
1002 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1003 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1004 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1005 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1006 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1007
1008 // If the span between time of reporting and present time is
1009 // negative, we report. This is in order to reset the timestamp and
1010 // avoid an edge condition whereby a distant point in the future is
1011 // in the state variable resulting in us never ever reporting again.
1012 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1013 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1014 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1015
1016 // In this case we should not update until the clock reads 71 + 24 = 95.
1017 // Check that.
1018 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1019 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1020 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1021 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1022 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001023}
1024
David Zeuthen3c55abd2013-10-14 12:48:03 -07001025TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1026 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001027 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001028 update_completed_marker);
1029
1030 FakeClock fake_clock;
1031 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001032 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001033
1034 Time boot_time;
1035 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1036
1037 attempter.WriteUpdateCompletedMarker();
1038
1039 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1040 EXPECT_EQ(boot_time.ToTimeT(), 42);
1041}
1042
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001043} // namespace chromeos_update_engine