blob: 52861694df0da6ab564e19ea470834e7cc4e7e95 [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 Chan06c76a42014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -07008#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +02009#include <policy/libpolicy.h>
10#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070011
12#include "update_engine/action_mock.h"
13#include "update_engine/action_processor_mock.h"
David Zeuthen985b1122013-10-09 12:13:15 -070014#include "update_engine/fake_clock.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070015#include "update_engine/fake_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070017#include "update_engine/install_plan.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080018#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080019#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070020#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021#include "update_engine/mock_payload_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070022#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070023#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070024#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080025#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070026#include "update_engine/update_attempter.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070027#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028
David Zeuthen985b1122013-10-09 12:13:15 -070029using base::Time;
30using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070031using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070032using testing::_;
33using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070034using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070035using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080036using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070037using testing::Property;
38using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070039using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070040
41namespace chromeos_update_engine {
42
43// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070044// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070045// methods.
46class UpdateAttempterUnderTest : public UpdateAttempter {
47 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070048 // We always feed an explicit update completed marker name; however, unless
49 // explicitly specified, we feed an empty string, which causes the
50 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070051 UpdateAttempterUnderTest(SystemState* system_state,
52 DBusWrapperInterface* dbus_iface)
53 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070054
Gilad Arnold1f847232014-04-07 12:07:49 -070055 UpdateAttempterUnderTest(SystemState* system_state,
56 DBusWrapperInterface* dbus_iface,
57 const std::string& update_completed_marker)
58 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070059
60 // Wrap the update scheduling method, allowing us to opt out of scheduled
61 // updates for testing purposes.
62 void ScheduleUpdates() override {
63 schedule_updates_called_ = true;
64 if (do_schedule_updates_) {
65 UpdateAttempter::ScheduleUpdates();
66 } else {
67 LOG(INFO) << "[TEST] Update scheduling disabled.";
68 }
69 }
70 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
71 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
72
73 // Indicates whether ScheduleUpdates() was called.
74 bool schedule_updates_called() const { return schedule_updates_called_; }
75
76 private:
77 bool schedule_updates_called_ = false;
78 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070079};
80
81class UpdateAttempterTest : public ::testing::Test {
82 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070083 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -070084 : attempter_(&fake_system_state_, &dbus_),
85 mock_connection_manager(&fake_system_state_),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070086 loop_(nullptr) {
Gilad Arnold1f847232014-04-07 12:07:49 -070087 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070088 fake_system_state_.set_connection_manager(&mock_connection_manager);
89 fake_system_state_.set_update_attempter(&attempter_);
Gilad Arnold1f847232014-04-07 12:07:49 -070090
91 // Finish initializing the attempter.
92 attempter_.Init();
Alex Deymo3e0b53e2014-08-12 23:12:25 -070093
94 // We set the set_good_kernel command to a non-existent path so it fails to
95 // run it. This avoids the async call to the command and continues the
96 // update process right away. Tests testing that behavior can override the
97 // default set_good_kernel command if needed.
98 attempter_.set_good_kernel_cmd_ = "/path/to/non-existent/command";
Jay Srinivasan43488792012-06-19 00:25:31 -070099 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700100
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700101 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700102 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
103
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700104 EXPECT_EQ(nullptr, attempter_.dbus_service_);
105 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700106 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800107 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700108 EXPECT_EQ(nullptr, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700109 EXPECT_FALSE(attempter_.download_active_);
110 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
111 EXPECT_EQ(0.0, attempter_.download_progress_);
112 EXPECT_EQ(0, attempter_.last_checked_time_);
113 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700114 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800115 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700116 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700117 prefs_ = fake_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700118 }
119
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700120 virtual void TearDown() {
121 utils::RecursiveUnlinkDir(test_dir_);
122 }
123
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200124 void QuitMainLoop();
125 static gboolean StaticQuitMainLoop(gpointer data);
126
Darin Petkove6ef2f82011-03-07 17:31:11 -0800127 void UpdateTestStart();
128 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800129 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800130 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700131 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800132 static gboolean StaticUpdateTestStart(gpointer data);
133 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700134 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800135 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700136 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
137 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200138
Thieu Le116fda32011-04-19 11:01:54 -0700139 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700140 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200141
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700142 void ReadScatterFactorFromPolicyTestStart();
143 static gboolean StaticReadScatterFactorFromPolicyTestStart(
144 gpointer data);
145
146 void DecrementUpdateCheckCountTestStart();
147 static gboolean StaticDecrementUpdateCheckCountTestStart(
148 gpointer data);
149
Jay Srinivasan08fce042012-06-07 16:31:01 -0700150 void NoScatteringDoneDuringManualUpdateTestStart();
151 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
152 gpointer data);
153
David Zeuthen8f191b22013-08-06 12:27:50 -0700154 void P2PNotEnabledStart();
155 static gboolean StaticP2PNotEnabled(gpointer data);
156
157 void P2PEnabledStart();
158 static gboolean StaticP2PEnabled(gpointer data);
159
160 void P2PEnabledInteractiveStart();
161 static gboolean StaticP2PEnabledInteractive(gpointer data);
162
163 void P2PEnabledStartingFailsStart();
164 static gboolean StaticP2PEnabledStartingFails(gpointer data);
165
166 void P2PEnabledHousekeepingFailsStart();
167 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
168
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700169 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800170 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700171 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800172 NiceMock<ActionProcessorMock>* processor_;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700173 NiceMock<PrefsMock>* prefs_; // shortcut to fake_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800174 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800175 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700176
177 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700178};
179
Darin Petkov1b003102010-11-30 10:18:36 -0800180TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700181 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800182 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700183 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800184 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700185 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800186 EXPECT_EQ(503, attempter_.http_response_code());
187 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700188 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800189}
190
191TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
192 ActionMock action;
193 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
194 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800195 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800196 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700197 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
198 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800199}
200
201TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700202 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800203 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700204 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800205 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800206 ObjectCollectorAction<OmahaResponse> collector_action;
207 BondActions(&action, &collector_action);
208 OmahaResponse response;
209 response.poll_interval = 234;
210 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800211 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700212 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800213 EXPECT_EQ(500, attempter_.http_response_code());
214 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700215 EXPECT_EQ(234, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700216 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800217}
218
Darin Petkovcd1666f2010-09-23 09:53:44 -0700219TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700220 string test_update_completed_marker;
221 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800222 "update_attempter_unittest-update_completed_marker-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700223 &test_update_completed_marker, nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700224 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700225 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700226 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700227 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700228 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700229 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700230}
231
232TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700233 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
234 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700235 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700236 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700237
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700238 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700239 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
240 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700241 EXPECT_EQ(ErrorCode::kOmahaRequestError,
242 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700243 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700244 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700245 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700246 ErrorCode::kError));
Alex Deymo42432912013-07-12 20:21:15 -0700247 FilesystemCopierAction filesystem_copier_action(
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700248 &fake_system_state_, false, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700249 EXPECT_EQ(ErrorCode::kFilesystemCopierError,
250 GetErrorCodeForAction(&filesystem_copier_action,
251 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800252 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700253 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700254 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700255 ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700256 ActionMock action_mock;
257 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700258 EXPECT_EQ(ErrorCode::kError,
259 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700260}
261
Darin Petkov36275772010-10-01 11:40:57 -0700262TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700263 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800264 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700265 .WillOnce(Return(false));
266 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700267 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800268 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700269 .WillOnce(DoAll(
270 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
271 Return(true)));
272 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700273 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800274 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700275 .WillOnce(DoAll(
276 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
277 Return(true)));
278 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700279 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800280 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700281 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700282 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700283}
284
285TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800286 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700287 .WillOnce(Return(false))
288 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
289 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
290 .WillOnce(DoAll(
291 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
292 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800293 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700294 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800295 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
296 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
297 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700298 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
299 .Times(1);
300 for (int i = 0; i < 4; i ++)
301 attempter_.MarkDeltaUpdateFailure();
302}
303
Darin Petkov1b003102010-11-30 10:18:36 -0800304TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
305 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
306 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700307 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800308 .Times(0);
309 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700310 string url1 = "http://url1";
311 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800312 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700313 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700314 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700315 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800316 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700317 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800318}
319
320TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
321 EXPECT_CALL(*processor_,
322 EnqueueAction(Property(&AbstractAction::Type,
323 OmahaRequestAction::StaticType())))
324 .Times(1);
325 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700326 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700327 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800328 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
329 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800330 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800331 attempter_.ScheduleErrorEventAction();
332 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
333}
334
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200335void UpdateAttempterTest::QuitMainLoop() {
336 g_main_loop_quit(loop_);
337}
338
339gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
340 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
341 return FALSE;
342}
343
Darin Petkove6ef2f82011-03-07 17:31:11 -0800344gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
345 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
346 return FALSE;
347}
348
349gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
350 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
351 return FALSE;
352}
353
Chris Sosa76a29ae2013-07-11 17:59:24 -0700354gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800355 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700356 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800357 return FALSE;
358}
359
360gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
361 gpointer data) {
362 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700363 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700364 return FALSE;
365}
366
367gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800368 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700369 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700370 return FALSE;
371}
372
373gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
374 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
375 return FALSE;
376}
377
Thieu Le116fda32011-04-19 11:01:54 -0700378gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
379 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
380 return FALSE;
381}
382
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700383gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
384 gpointer data) {
385 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
386 ua_test->ReadScatterFactorFromPolicyTestStart();
387 return FALSE;
388}
389
390gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
391 gpointer data) {
392 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
393 ua_test->DecrementUpdateCheckCountTestStart();
394 return FALSE;
395}
396
Jay Srinivasan08fce042012-06-07 16:31:01 -0700397gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
398 gpointer data) {
399 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
400 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
401 return FALSE;
402}
403
Darin Petkove6ef2f82011-03-07 17:31:11 -0800404namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700405// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700406const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800407 OmahaRequestAction::StaticType(),
408 OmahaResponseHandlerAction::StaticType(),
409 FilesystemCopierAction::StaticType(),
410 FilesystemCopierAction::StaticType(),
411 OmahaRequestAction::StaticType(),
412 DownloadAction::StaticType(),
413 OmahaRequestAction::StaticType(),
414 FilesystemCopierAction::StaticType(),
415 FilesystemCopierAction::StaticType(),
416 PostinstallRunnerAction::StaticType(),
417 OmahaRequestAction::StaticType()
418};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700419
420// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700421const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700422 InstallPlanAction::StaticType(),
423 PostinstallRunnerAction::StaticType(),
424};
425
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700426} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800427
428void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700429 attempter_.set_http_response_code(200);
430 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700431 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700432 EXPECT_CALL(*processor_,
433 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700434 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700435 }
436 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
437
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700438 attempter_.Update("", "", "", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800439 g_idle_add(&StaticUpdateTestVerify, this);
440}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700441
Darin Petkove6ef2f82011-03-07 17:31:11 -0800442void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700443 EXPECT_EQ(0, attempter_.http_response_code());
444 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700445 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
446 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
447 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700448 }
449 EXPECT_EQ(attempter_.response_handler_action_.get(),
450 attempter_.actions_[1].get());
451 DownloadAction* download_action =
452 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700453 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700454 EXPECT_EQ(&attempter_, download_action->delegate());
455 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800456 g_main_loop_quit(loop_);
457}
458
Chris Sosa28e479c2013-07-12 11:39:53 -0700459void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700460 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700461 // Create a device policy so that we can change settings.
462 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
463 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
464
465 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700466 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700467
Don Garrett6646b442013-11-13 15:29:11 -0800468 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700469 // References bootable kernels in fake_hardware.h
470 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800471 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700472 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800473 rollback_kernel);
474 }
475
Chris Sosa28e479c2013-07-12 11:39:53 -0700476 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700477
Chris Sosad38b1132014-03-25 10:43:59 -0700478 // We only allow rollback on devices that are not enterprise enrolled and
479 // which have a valid slot to rollback to.
480 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700481 is_rollback_allowed = true;
482 }
483
Don Garrett6646b442013-11-13 15:29:11 -0800484 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700485 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800486 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa28e479c2013-07-12 11:39:53 -0700487 DoAll(SetArgumentPointee<0>(std::string("")),
488 Return(true)));
489 } else {
490 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800491 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa76a29ae2013-07-11 17:59:24 -0700492 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
493 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700494 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700495
Chris Sosa28e479c2013-07-12 11:39:53 -0700496 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700497 InSequence s;
498 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
499 EXPECT_CALL(*processor_,
500 EnqueueAction(Property(&AbstractAction::Type,
501 kRollbackActionTypes[i]))).Times(1);
502 }
503 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
504
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700505 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700506 g_idle_add(&StaticRollbackTestVerify, this);
507 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700508 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700509 g_main_loop_quit(loop_);
510 }
511}
512
513void UpdateAttempterTest::RollbackTestVerify() {
514 // Verifies the actions that were enqueued.
515 EXPECT_EQ(&attempter_, processor_->delegate());
516 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
517 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
518 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
519 }
520 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
521 InstallPlanAction* install_plan_action =
522 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
523 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700524 // Matches fake_hardware.h -> rollback should move from kernel/boot device
525 // pair to other pair.
526 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
527 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700528 EXPECT_EQ(install_plan->powerwash_required, true);
529 g_main_loop_quit(loop_);
530}
531
Darin Petkove6ef2f82011-03-07 17:31:11 -0800532TEST_F(UpdateAttempterTest, UpdateTest) {
533 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
534 g_idle_add(&StaticUpdateTestStart, this);
535 g_main_loop_run(loop_);
536 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700537 loop_ = nullptr;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700538}
539
Chris Sosa76a29ae2013-07-11 17:59:24 -0700540TEST_F(UpdateAttempterTest, RollbackTest) {
541 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
542 g_idle_add(&StaticRollbackTestStart, this);
543 g_main_loop_run(loop_);
544 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700545 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700546}
547
Don Garrett6646b442013-11-13 15:29:11 -0800548TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
549 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
550 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
551 g_main_loop_run(loop_);
552 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700553 loop_ = nullptr;
Don Garrett6646b442013-11-13 15:29:11 -0800554}
555
Chris Sosa76a29ae2013-07-11 17:59:24 -0700556TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
557 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
558 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
559 g_main_loop_run(loop_);
560 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700561 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700562}
563
Thieu Le116fda32011-04-19 11:01:54 -0700564void UpdateAttempterTest::PingOmahaTestStart() {
565 EXPECT_CALL(*processor_,
566 EnqueueAction(Property(&AbstractAction::Type,
567 OmahaRequestAction::StaticType())))
568 .Times(1);
569 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
570 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200571 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700572}
573
574TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700575 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
576 EXPECT_FALSE(attempter_.schedule_updates_called());
577 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
578 // testing, which is more permissive than we want to handle here.
579 attempter_.DisableScheduleUpdates();
Thieu Le116fda32011-04-19 11:01:54 -0700580 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
581 g_idle_add(&StaticPingOmahaTestStart, this);
582 g_main_loop_run(loop_);
583 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700584 loop_ = nullptr;
Thieu Le116fda32011-04-19 11:01:54 -0700585 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700586 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700587}
588
Darin Petkov18c7bce2011-06-16 14:07:00 -0700589TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
590 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700591 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700592 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700593 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700594 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
595 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700596 EXPECT_EQ(
597 static_cast<ErrorCode>(static_cast<int>(kCode) |
598 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
599 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700600}
601
602TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
603 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700604 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700605 response_action->install_plan_.is_resume = true;
606 attempter_.response_handler_action_.reset(response_action);
607 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700608 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700609 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700610 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700611 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
612 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700613 EXPECT_EQ(
614 static_cast<ErrorCode>(
615 static_cast<int>(kCode) |
616 static_cast<int>(ErrorCode::kResumedFlag) |
617 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
618 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700619}
620
David Zeuthen8f191b22013-08-06 12:27:50 -0700621TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
622 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700623 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700624 mock_p2p_manager.fake().SetP2PEnabled(false);
625 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
626 attempter_.UpdateEngineStarted();
627}
628
629TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
630 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700631 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700632 mock_p2p_manager.fake().SetP2PEnabled(true);
633 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
634 attempter_.UpdateEngineStarted();
635}
636
637TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
638 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700639 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700640 mock_p2p_manager.fake().SetP2PEnabled(true);
641 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
642 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
643 attempter_.UpdateEngineStarted();
644}
645
646TEST_F(UpdateAttempterTest, P2PNotEnabled) {
647 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
648 g_idle_add(&StaticP2PNotEnabled, this);
649 g_main_loop_run(loop_);
650 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700651 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700652}
653gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
654 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
655 ua_test->P2PNotEnabledStart();
656 return FALSE;
657}
658void UpdateAttempterTest::P2PNotEnabledStart() {
659 // If P2P is not enabled, check that we do not attempt housekeeping
660 // and do not convey that p2p is to be used.
661 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700662 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700663 mock_p2p_manager.fake().SetP2PEnabled(false);
664 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700665 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700666 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
667 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
668 g_idle_add(&StaticQuitMainLoop, this);
669}
670
671TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
672 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
673 g_idle_add(&StaticP2PEnabledStartingFails, this);
674 g_main_loop_run(loop_);
675 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700676 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700677}
678gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
679 gpointer data) {
680 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
681 ua_test->P2PEnabledStartingFailsStart();
682 return FALSE;
683}
684void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
685 // If p2p is enabled, but starting it fails ensure we don't do
686 // any housekeeping and do not convey that p2p should be used.
687 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700688 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700689 mock_p2p_manager.fake().SetP2PEnabled(true);
690 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
691 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
692 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700693 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700694 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
695 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
696 g_idle_add(&StaticQuitMainLoop, this);
697}
698
699TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
700 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
701 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
702 g_main_loop_run(loop_);
703 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700704 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700705}
706gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
707 gpointer data) {
708 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
709 ua_test->P2PEnabledHousekeepingFailsStart();
710 return FALSE;
711}
712void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
713 // If p2p is enabled, starting it works but housekeeping fails, ensure
714 // we do not convey p2p is to be used.
715 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700716 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700717 mock_p2p_manager.fake().SetP2PEnabled(true);
718 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
719 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
720 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700721 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700722 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
723 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
724 g_idle_add(&StaticQuitMainLoop, this);
725}
726
727TEST_F(UpdateAttempterTest, P2PEnabled) {
728 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
729 g_idle_add(&StaticP2PEnabled, this);
730 g_main_loop_run(loop_);
731 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700732 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700733}
734gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
735 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
736 ua_test->P2PEnabledStart();
737 return FALSE;
738}
739void UpdateAttempterTest::P2PEnabledStart() {
740 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700741 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700742 // If P2P is enabled and starting it works, check that we performed
743 // housekeeping and that we convey p2p should be used.
744 mock_p2p_manager.fake().SetP2PEnabled(true);
745 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
746 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
747 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700748 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700749 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
750 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
751 g_idle_add(&StaticQuitMainLoop, this);
752}
753
754TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
755 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
756 g_idle_add(&StaticP2PEnabledInteractive, this);
757 g_main_loop_run(loop_);
758 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700759 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700760}
761gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
762 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
763 ua_test->P2PEnabledInteractiveStart();
764 return FALSE;
765}
766void UpdateAttempterTest::P2PEnabledInteractiveStart() {
767 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700768 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700769 // For an interactive check, if P2P is enabled and starting it
770 // works, check that we performed housekeeping and that we convey
771 // p2p should be used for sharing but NOT for downloading.
772 mock_p2p_manager.fake().SetP2PEnabled(true);
773 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
774 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
775 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700776 attempter_.Update("", "", "", "", false, true /* interactive */);
David Zeuthen8f191b22013-08-06 12:27:50 -0700777 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
778 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
779 g_idle_add(&StaticQuitMainLoop, this);
780}
781
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700782TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
783 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
784 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
785 g_main_loop_run(loop_);
786 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700787 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700788}
789
790// Tests that the scatter_factor_in_seconds value is properly fetched
791// from the device policy.
792void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700793 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700794
795 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
796 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
797
798 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700799 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700800
801 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
802 .WillRepeatedly(DoAll(
803 SetArgumentPointee<0>(scatter_factor_in_seconds),
804 Return(true)));
805
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700806 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700807 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
808
809 g_idle_add(&StaticQuitMainLoop, this);
810}
811
812TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
813 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
814 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
815 g_main_loop_run(loop_);
816 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700817 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700818}
819
820void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
821 // Tests that the scatter_factor_in_seconds value is properly fetched
822 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700823 int64_t initial_value = 5;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700824 Prefs prefs;
825 attempter_.prefs_ = &prefs;
826
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700827 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700828
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700829 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800830 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700831 &prefs_dir));
832 ScopedDirRemover temp_dir_remover(prefs_dir);
833
Alex Vakulenko75039d72014-03-25 12:36:28 -0700834 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700835 << "Failed to initialize preferences.";
836 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
837
Ben Chan9abb7632014-08-07 00:10:53 -0700838 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700839
840 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
841 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
842
843 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700844 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700845
846 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
847 .WillRepeatedly(DoAll(
848 SetArgumentPointee<0>(scatter_factor_in_seconds),
849 Return(true)));
850
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700851 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700852 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
853
854 // Make sure the file still exists.
855 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
856
Ben Chan9abb7632014-08-07 00:10:53 -0700857 int64_t new_value;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700858 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
859 EXPECT_EQ(initial_value - 1, new_value);
860
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700861 EXPECT_TRUE(
862 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700863
864 // However, if the count is already 0, it's not decremented. Test that.
865 initial_value = 0;
866 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700867 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700868 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
869 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
870 EXPECT_EQ(initial_value, new_value);
871
872 g_idle_add(&StaticQuitMainLoop, this);
873}
874
Jay Srinivasan08fce042012-06-07 16:31:01 -0700875TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
876 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
877 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
878 g_main_loop_run(loop_);
879 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700880 loop_ = nullptr;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700881}
882
883void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
884 // Tests that no scattering logic is enabled if the update check
885 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700886 int64_t initial_value = 8;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700887 Prefs prefs;
888 attempter_.prefs_ = &prefs;
889
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700890 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700891
892 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800893 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -0700894 &prefs_dir));
895 ScopedDirRemover temp_dir_remover(prefs_dir);
896
Alex Vakulenko75039d72014-03-25 12:36:28 -0700897 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -0700898 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700899 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700900 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
901
902 // make sure scatter_factor is non-zero as scattering is disabled
903 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700904 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700905
906 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
907 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
908
909 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700910 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700911
912 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
913 .WillRepeatedly(DoAll(
914 SetArgumentPointee<0>(scatter_factor_in_seconds),
915 Return(true)));
916
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800917 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700918 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700919 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
920
921 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700922 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700923 EXPECT_FALSE(
924 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700925 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700926 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
927 EXPECT_FALSE(
928 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700929 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
930
931 g_idle_add(&StaticQuitMainLoop, this);
932}
933
David Zeuthen985b1122013-10-09 12:13:15 -0700934// Checks that we only report daily metrics at most every 24 hours.
935TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
936 FakeClock fake_clock;
937 Prefs prefs;
938 string temp_dir;
939
940 // We need persistent preferences for this test
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700941 EXPECT_TRUE(utils::MakeTempDirectory("UpdateAttempterTest.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -0700942 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700943 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700944 fake_system_state_.set_clock(&fake_clock);
945 fake_system_state_.set_prefs(&prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700946
947 Time epoch = Time::FromInternalValue(0);
948 fake_clock.SetWallclockTime(epoch);
949
950 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
951 // we should report.
952 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
953 // We should not report again if no time has passed.
954 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
955
956 // We should not report if only 10 hours has passed.
957 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
958 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
959
960 // We should not report if only 24 hours - 1 sec has passed.
961 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
962 TimeDelta::FromSeconds(1));
963 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
964
965 // We should report if 24 hours has passed.
966 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
967 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
968
969 // But then we should not report again..
970 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
971
972 // .. until another 24 hours has passed
973 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
974 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
975 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
976 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
977 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
978
979 // .. and another 24 hours
980 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
981 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
982 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
983 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
984 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
985
986 // If the span between time of reporting and present time is
987 // negative, we report. This is in order to reset the timestamp and
988 // avoid an edge condition whereby a distant point in the future is
989 // in the state variable resulting in us never ever reporting again.
990 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
991 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
992 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
993
994 // In this case we should not update until the clock reads 71 + 24 = 95.
995 // Check that.
996 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
997 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
998 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
999 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1000 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1001
1002 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1003}
1004
David Zeuthen3c55abd2013-10-14 12:48:03 -07001005TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1006 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001007 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001008 update_completed_marker);
1009
1010 FakeClock fake_clock;
1011 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001012 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001013
1014 Time boot_time;
1015 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1016
1017 attempter.WriteUpdateCompletedMarker();
1018
1019 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1020 EXPECT_EQ(boot_time.ToTimeT(), 42);
1021}
1022
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001023} // namespace chromeos_update_engine