blob: 0bfc31d56b46e2eec0adf38712cda20c90b066dc [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovf42cc1c2010-09-01 09:03:02 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan9abb7632014-08-07 00:10:53 -07005#include <stdint.h>
6
Ben Chan02f7c1d2014-10-18 15:18:02 -07007#include <memory>
8
Ben Chan06c76a42014-09-05 08:21:06 -07009#include <base/files/file_util.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070010#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020011#include <policy/libpolicy.h>
12#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070013
14#include "update_engine/action_mock.h"
15#include "update_engine/action_processor_mock.h"
David Zeuthen985b1122013-10-09 12:13:15 -070016#include "update_engine/fake_clock.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070017#include "update_engine/fake_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070018#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070019#include "update_engine/install_plan.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080020#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080021#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070022#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080023#include "update_engine/mock_payload_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070025#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070026#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080027#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028#include "update_engine/update_attempter.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070029#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030
David Zeuthen985b1122013-10-09 12:13:15 -070031using base::Time;
32using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070034using std::unique_ptr;
Darin Petkov36275772010-10-01 11:40:57 -070035using testing::_;
36using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070037using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070038using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080039using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070040using testing::Property;
41using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070042using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070043
44namespace chromeos_update_engine {
45
46// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070047// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070048// methods.
49class UpdateAttempterUnderTest : public UpdateAttempter {
50 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070051 // We always feed an explicit update completed marker name; however, unless
52 // explicitly specified, we feed an empty string, which causes the
53 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070054 UpdateAttempterUnderTest(SystemState* system_state,
55 DBusWrapperInterface* dbus_iface)
56 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070057
Gilad Arnold1f847232014-04-07 12:07:49 -070058 UpdateAttempterUnderTest(SystemState* system_state,
59 DBusWrapperInterface* dbus_iface,
60 const std::string& update_completed_marker)
61 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070062
63 // Wrap the update scheduling method, allowing us to opt out of scheduled
64 // updates for testing purposes.
65 void ScheduleUpdates() override {
66 schedule_updates_called_ = true;
67 if (do_schedule_updates_) {
68 UpdateAttempter::ScheduleUpdates();
69 } else {
70 LOG(INFO) << "[TEST] Update scheduling disabled.";
71 }
72 }
73 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
74 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
75
76 // Indicates whether ScheduleUpdates() was called.
77 bool schedule_updates_called() const { return schedule_updates_called_; }
78
79 private:
80 bool schedule_updates_called_ = false;
81 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070082};
83
84class UpdateAttempterTest : public ::testing::Test {
85 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070086 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -070087 : attempter_(&fake_system_state_, &dbus_),
88 mock_connection_manager(&fake_system_state_),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070089 loop_(nullptr) {
Gilad Arnold1f847232014-04-07 12:07:49 -070090 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070091 fake_system_state_.set_connection_manager(&mock_connection_manager);
92 fake_system_state_.set_update_attempter(&attempter_);
Gilad Arnold1f847232014-04-07 12:07:49 -070093
94 // Finish initializing the attempter.
95 attempter_.Init();
Alex Deymo3e0b53e2014-08-12 23:12:25 -070096
97 // We set the set_good_kernel command to a non-existent path so it fails to
98 // run it. This avoids the async call to the command and continues the
99 // update process right away. Tests testing that behavior can override the
100 // default set_good_kernel command if needed.
101 attempter_.set_good_kernel_cmd_ = "/path/to/non-existent/command";
Jay Srinivasan43488792012-06-19 00:25:31 -0700102 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700103
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700104 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700105 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
106
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700107 EXPECT_EQ(nullptr, attempter_.dbus_service_);
108 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700109 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800110 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700111 EXPECT_EQ(nullptr, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700112 EXPECT_FALSE(attempter_.download_active_);
113 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
114 EXPECT_EQ(0.0, attempter_.download_progress_);
115 EXPECT_EQ(0, attempter_.last_checked_time_);
116 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700117 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800118 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700119 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700120 prefs_ = fake_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700121 }
122
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700123 virtual void TearDown() {
124 utils::RecursiveUnlinkDir(test_dir_);
125 }
126
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200127 void QuitMainLoop();
128 static gboolean StaticQuitMainLoop(gpointer data);
129
Darin Petkove6ef2f82011-03-07 17:31:11 -0800130 void UpdateTestStart();
131 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800132 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800133 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700134 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800135 static gboolean StaticUpdateTestStart(gpointer data);
136 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700137 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800138 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700139 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
140 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200141
Thieu Le116fda32011-04-19 11:01:54 -0700142 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700143 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200144
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700145 void ReadScatterFactorFromPolicyTestStart();
146 static gboolean StaticReadScatterFactorFromPolicyTestStart(
147 gpointer data);
148
149 void DecrementUpdateCheckCountTestStart();
150 static gboolean StaticDecrementUpdateCheckCountTestStart(
151 gpointer data);
152
Jay Srinivasan08fce042012-06-07 16:31:01 -0700153 void NoScatteringDoneDuringManualUpdateTestStart();
154 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
155 gpointer data);
156
David Zeuthen8f191b22013-08-06 12:27:50 -0700157 void P2PNotEnabledStart();
158 static gboolean StaticP2PNotEnabled(gpointer data);
159
160 void P2PEnabledStart();
161 static gboolean StaticP2PEnabled(gpointer data);
162
163 void P2PEnabledInteractiveStart();
164 static gboolean StaticP2PEnabledInteractive(gpointer data);
165
166 void P2PEnabledStartingFailsStart();
167 static gboolean StaticP2PEnabledStartingFails(gpointer data);
168
169 void P2PEnabledHousekeepingFailsStart();
170 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
171
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700172 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800173 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700174 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800175 NiceMock<ActionProcessorMock>* processor_;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700176 NiceMock<PrefsMock>* prefs_; // shortcut to fake_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800177 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800178 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700179
180 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700181};
182
Darin Petkov1b003102010-11-30 10:18:36 -0800183TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700184 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800185 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700186 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800187 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700188 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800189 EXPECT_EQ(503, attempter_.http_response_code());
190 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700191 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800192}
193
194TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
195 ActionMock action;
196 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
197 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800198 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800199 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700200 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
201 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800202}
203
204TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700205 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800206 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700207 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800208 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800209 ObjectCollectorAction<OmahaResponse> collector_action;
210 BondActions(&action, &collector_action);
211 OmahaResponse response;
212 response.poll_interval = 234;
213 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800214 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700215 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800216 EXPECT_EQ(500, attempter_.http_response_code());
217 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700218 EXPECT_EQ(234, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700219 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800220}
221
Darin Petkovcd1666f2010-09-23 09:53:44 -0700222TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700223 string test_update_completed_marker;
224 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800225 "update_attempter_unittest-update_completed_marker-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700226 &test_update_completed_marker, nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700227 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700228 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700229 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700230 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700231 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700232 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700233}
234
235TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700236 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
237 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700238 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700239 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700240
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700241 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700242 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
243 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700244 EXPECT_EQ(ErrorCode::kOmahaRequestError,
245 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700246 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700247 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700248 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700249 ErrorCode::kError));
Alex Deymo42432912013-07-12 20:21:15 -0700250 FilesystemCopierAction filesystem_copier_action(
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700251 &fake_system_state_, false, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700252 EXPECT_EQ(ErrorCode::kFilesystemCopierError,
253 GetErrorCodeForAction(&filesystem_copier_action,
254 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800255 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700256 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700257 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700258 ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700259 ActionMock action_mock;
260 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700261 EXPECT_EQ(ErrorCode::kError,
262 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700263}
264
Darin Petkov36275772010-10-01 11:40:57 -0700265TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700266 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800267 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700268 .WillOnce(Return(false));
269 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700270 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800271 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700272 .WillOnce(DoAll(
273 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
274 Return(true)));
275 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700276 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800277 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700278 .WillOnce(DoAll(
279 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
280 Return(true)));
281 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700282 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800283 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700284 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700285 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700286}
287
288TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800289 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700290 .WillOnce(Return(false))
291 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
292 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
293 .WillOnce(DoAll(
294 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
295 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700297 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800298 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
299 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
300 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700301 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
302 .Times(1);
303 for (int i = 0; i < 4; i ++)
304 attempter_.MarkDeltaUpdateFailure();
305}
306
Darin Petkov1b003102010-11-30 10:18:36 -0800307TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
308 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
309 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700310 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800311 .Times(0);
312 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700313 string url1 = "http://url1";
314 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800315 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700316 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700317 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700318 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800319 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700320 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800321}
322
323TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
324 EXPECT_CALL(*processor_,
325 EnqueueAction(Property(&AbstractAction::Type,
326 OmahaRequestAction::StaticType())))
327 .Times(1);
328 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700329 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700330 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800331 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
332 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800333 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800334 attempter_.ScheduleErrorEventAction();
335 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
336}
337
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200338void UpdateAttempterTest::QuitMainLoop() {
339 g_main_loop_quit(loop_);
340}
341
342gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
343 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
344 return FALSE;
345}
346
Darin Petkove6ef2f82011-03-07 17:31:11 -0800347gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
348 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
349 return FALSE;
350}
351
352gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
353 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
354 return FALSE;
355}
356
Chris Sosa76a29ae2013-07-11 17:59:24 -0700357gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800358 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700359 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800360 return FALSE;
361}
362
363gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
364 gpointer data) {
365 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700366 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700367 return FALSE;
368}
369
370gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800371 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700372 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700373 return FALSE;
374}
375
376gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
377 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
378 return FALSE;
379}
380
Thieu Le116fda32011-04-19 11:01:54 -0700381gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
382 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
383 return FALSE;
384}
385
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700386gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
387 gpointer data) {
388 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
389 ua_test->ReadScatterFactorFromPolicyTestStart();
390 return FALSE;
391}
392
393gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
394 gpointer data) {
395 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
396 ua_test->DecrementUpdateCheckCountTestStart();
397 return FALSE;
398}
399
Jay Srinivasan08fce042012-06-07 16:31:01 -0700400gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
401 gpointer data) {
402 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
403 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
404 return FALSE;
405}
406
Darin Petkove6ef2f82011-03-07 17:31:11 -0800407namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700408// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700409const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800410 OmahaRequestAction::StaticType(),
411 OmahaResponseHandlerAction::StaticType(),
412 FilesystemCopierAction::StaticType(),
413 FilesystemCopierAction::StaticType(),
414 OmahaRequestAction::StaticType(),
415 DownloadAction::StaticType(),
416 OmahaRequestAction::StaticType(),
417 FilesystemCopierAction::StaticType(),
418 FilesystemCopierAction::StaticType(),
419 PostinstallRunnerAction::StaticType(),
420 OmahaRequestAction::StaticType()
421};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700422
423// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700424const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700425 InstallPlanAction::StaticType(),
426 PostinstallRunnerAction::StaticType(),
427};
428
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700429} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800430
431void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700432 attempter_.set_http_response_code(200);
433 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700434 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700435 EXPECT_CALL(*processor_,
436 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700437 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700438 }
439 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
440
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700441 attempter_.Update("", "", "", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800442 g_idle_add(&StaticUpdateTestVerify, this);
443}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700444
Darin Petkove6ef2f82011-03-07 17:31:11 -0800445void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700446 EXPECT_EQ(0, attempter_.http_response_code());
447 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700448 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
449 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
450 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700451 }
452 EXPECT_EQ(attempter_.response_handler_action_.get(),
453 attempter_.actions_[1].get());
454 DownloadAction* download_action =
455 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700456 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700457 EXPECT_EQ(&attempter_, download_action->delegate());
458 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800459 g_main_loop_quit(loop_);
460}
461
Chris Sosa28e479c2013-07-12 11:39:53 -0700462void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700463 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700464 // Create a device policy so that we can change settings.
465 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
466 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
467
468 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700469 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700470
Don Garrett6646b442013-11-13 15:29:11 -0800471 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700472 // References bootable kernels in fake_hardware.h
473 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800474 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700475 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800476 rollback_kernel);
477 }
478
Chris Sosa28e479c2013-07-12 11:39:53 -0700479 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700480
Chris Sosad38b1132014-03-25 10:43:59 -0700481 // We only allow rollback on devices that are not enterprise enrolled and
482 // which have a valid slot to rollback to.
483 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700484 is_rollback_allowed = true;
485 }
486
Don Garrett6646b442013-11-13 15:29:11 -0800487 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700488 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800489 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa28e479c2013-07-12 11:39:53 -0700490 DoAll(SetArgumentPointee<0>(std::string("")),
491 Return(true)));
492 } else {
493 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800494 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa76a29ae2013-07-11 17:59:24 -0700495 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
496 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700497 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700498
Chris Sosa28e479c2013-07-12 11:39:53 -0700499 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700500 InSequence s;
501 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
502 EXPECT_CALL(*processor_,
503 EnqueueAction(Property(&AbstractAction::Type,
504 kRollbackActionTypes[i]))).Times(1);
505 }
506 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
507
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700508 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700509 g_idle_add(&StaticRollbackTestVerify, this);
510 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700511 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700512 g_main_loop_quit(loop_);
513 }
514}
515
516void UpdateAttempterTest::RollbackTestVerify() {
517 // Verifies the actions that were enqueued.
518 EXPECT_EQ(&attempter_, processor_->delegate());
519 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
520 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
521 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
522 }
523 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
524 InstallPlanAction* install_plan_action =
525 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
526 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700527 // Matches fake_hardware.h -> rollback should move from kernel/boot device
528 // pair to other pair.
529 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
530 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700531 EXPECT_EQ(install_plan->powerwash_required, true);
532 g_main_loop_quit(loop_);
533}
534
Darin Petkove6ef2f82011-03-07 17:31:11 -0800535TEST_F(UpdateAttempterTest, UpdateTest) {
536 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
537 g_idle_add(&StaticUpdateTestStart, this);
538 g_main_loop_run(loop_);
539 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700540 loop_ = nullptr;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700541}
542
Chris Sosa76a29ae2013-07-11 17:59:24 -0700543TEST_F(UpdateAttempterTest, RollbackTest) {
544 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
545 g_idle_add(&StaticRollbackTestStart, this);
546 g_main_loop_run(loop_);
547 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700548 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700549}
550
Don Garrett6646b442013-11-13 15:29:11 -0800551TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
552 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
553 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
554 g_main_loop_run(loop_);
555 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700556 loop_ = nullptr;
Don Garrett6646b442013-11-13 15:29:11 -0800557}
558
Chris Sosa76a29ae2013-07-11 17:59:24 -0700559TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
560 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
561 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
562 g_main_loop_run(loop_);
563 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700564 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700565}
566
Thieu Le116fda32011-04-19 11:01:54 -0700567void UpdateAttempterTest::PingOmahaTestStart() {
568 EXPECT_CALL(*processor_,
569 EnqueueAction(Property(&AbstractAction::Type,
570 OmahaRequestAction::StaticType())))
571 .Times(1);
572 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
573 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200574 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700575}
576
577TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700578 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
579 EXPECT_FALSE(attempter_.schedule_updates_called());
580 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
581 // testing, which is more permissive than we want to handle here.
582 attempter_.DisableScheduleUpdates();
Thieu Le116fda32011-04-19 11:01:54 -0700583 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
584 g_idle_add(&StaticPingOmahaTestStart, this);
585 g_main_loop_run(loop_);
586 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700587 loop_ = nullptr;
Thieu Le116fda32011-04-19 11:01:54 -0700588 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700589 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700590}
591
Darin Petkov18c7bce2011-06-16 14:07:00 -0700592TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
593 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700594 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700595 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700596 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700597 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
598 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700599 EXPECT_EQ(
600 static_cast<ErrorCode>(static_cast<int>(kCode) |
601 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
602 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700603}
604
605TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
606 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700607 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700608 response_action->install_plan_.is_resume = true;
609 attempter_.response_handler_action_.reset(response_action);
610 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700611 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700612 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700613 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700614 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
615 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700616 EXPECT_EQ(
617 static_cast<ErrorCode>(
618 static_cast<int>(kCode) |
619 static_cast<int>(ErrorCode::kResumedFlag) |
620 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
621 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700622}
623
David Zeuthen8f191b22013-08-06 12:27:50 -0700624TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
625 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700626 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700627 mock_p2p_manager.fake().SetP2PEnabled(false);
628 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
629 attempter_.UpdateEngineStarted();
630}
631
632TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
633 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700634 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700635 mock_p2p_manager.fake().SetP2PEnabled(true);
636 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
637 attempter_.UpdateEngineStarted();
638}
639
640TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
641 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700642 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700643 mock_p2p_manager.fake().SetP2PEnabled(true);
644 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
645 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
646 attempter_.UpdateEngineStarted();
647}
648
649TEST_F(UpdateAttempterTest, P2PNotEnabled) {
650 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
651 g_idle_add(&StaticP2PNotEnabled, this);
652 g_main_loop_run(loop_);
653 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700654 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700655}
656gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
657 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
658 ua_test->P2PNotEnabledStart();
659 return FALSE;
660}
661void UpdateAttempterTest::P2PNotEnabledStart() {
662 // If P2P is not enabled, check that we do not attempt housekeeping
663 // and do not convey that p2p is to be used.
664 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700665 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700666 mock_p2p_manager.fake().SetP2PEnabled(false);
667 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700668 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700669 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
670 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
671 g_idle_add(&StaticQuitMainLoop, this);
672}
673
674TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
675 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
676 g_idle_add(&StaticP2PEnabledStartingFails, this);
677 g_main_loop_run(loop_);
678 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700679 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700680}
681gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
682 gpointer data) {
683 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
684 ua_test->P2PEnabledStartingFailsStart();
685 return FALSE;
686}
687void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
688 // If p2p is enabled, but starting it fails ensure we don't do
689 // any housekeeping and do not convey that p2p should be used.
690 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700691 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700692 mock_p2p_manager.fake().SetP2PEnabled(true);
693 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
694 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
695 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700696 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700697 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
698 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
699 g_idle_add(&StaticQuitMainLoop, this);
700}
701
702TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
703 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
704 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
705 g_main_loop_run(loop_);
706 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700707 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700708}
709gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
710 gpointer data) {
711 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
712 ua_test->P2PEnabledHousekeepingFailsStart();
713 return FALSE;
714}
715void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
716 // If p2p is enabled, starting it works but housekeeping fails, ensure
717 // we do not convey p2p is to be used.
718 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700719 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700720 mock_p2p_manager.fake().SetP2PEnabled(true);
721 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
722 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
723 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700724 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700725 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
726 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
727 g_idle_add(&StaticQuitMainLoop, this);
728}
729
730TEST_F(UpdateAttempterTest, P2PEnabled) {
731 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
732 g_idle_add(&StaticP2PEnabled, this);
733 g_main_loop_run(loop_);
734 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700735 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700736}
737gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
738 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
739 ua_test->P2PEnabledStart();
740 return FALSE;
741}
742void UpdateAttempterTest::P2PEnabledStart() {
743 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700744 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700745 // If P2P is enabled and starting it works, check that we performed
746 // housekeeping and that we convey p2p should be used.
747 mock_p2p_manager.fake().SetP2PEnabled(true);
748 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
749 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
750 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700751 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700752 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
753 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
754 g_idle_add(&StaticQuitMainLoop, this);
755}
756
757TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
758 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
759 g_idle_add(&StaticP2PEnabledInteractive, this);
760 g_main_loop_run(loop_);
761 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700762 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700763}
764gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
765 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
766 ua_test->P2PEnabledInteractiveStart();
767 return FALSE;
768}
769void UpdateAttempterTest::P2PEnabledInteractiveStart() {
770 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700771 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700772 // For an interactive check, if P2P is enabled and starting it
773 // works, check that we performed housekeeping and that we convey
774 // p2p should be used for sharing but NOT for downloading.
775 mock_p2p_manager.fake().SetP2PEnabled(true);
776 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
777 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
778 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700779 attempter_.Update("", "", "", "", false, true /* interactive */);
David Zeuthen8f191b22013-08-06 12:27:50 -0700780 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
781 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
782 g_idle_add(&StaticQuitMainLoop, this);
783}
784
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700785TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
786 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
787 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
788 g_main_loop_run(loop_);
789 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700790 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700791}
792
793// Tests that the scatter_factor_in_seconds value is properly fetched
794// from the device policy.
795void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700796 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700797
798 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
799 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
800
801 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700802 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700803
804 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
805 .WillRepeatedly(DoAll(
806 SetArgumentPointee<0>(scatter_factor_in_seconds),
807 Return(true)));
808
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700809 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700810 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
811
812 g_idle_add(&StaticQuitMainLoop, this);
813}
814
815TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
816 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
817 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
818 g_main_loop_run(loop_);
819 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700820 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700821}
822
823void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
824 // Tests that the scatter_factor_in_seconds value is properly fetched
825 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700826 int64_t initial_value = 5;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700827 Prefs prefs;
828 attempter_.prefs_ = &prefs;
829
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700830 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700831
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700832 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800833 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700834 &prefs_dir));
835 ScopedDirRemover temp_dir_remover(prefs_dir);
836
Alex Vakulenko75039d72014-03-25 12:36:28 -0700837 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700838 << "Failed to initialize preferences.";
839 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
840
Ben Chan9abb7632014-08-07 00:10:53 -0700841 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700842
843 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
844 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
845
846 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700847 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700848
849 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
850 .WillRepeatedly(DoAll(
851 SetArgumentPointee<0>(scatter_factor_in_seconds),
852 Return(true)));
853
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700854 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700855 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
856
857 // Make sure the file still exists.
858 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
859
Ben Chan9abb7632014-08-07 00:10:53 -0700860 int64_t new_value;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700861 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
862 EXPECT_EQ(initial_value - 1, new_value);
863
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700864 EXPECT_TRUE(
865 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700866
867 // However, if the count is already 0, it's not decremented. Test that.
868 initial_value = 0;
869 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700870 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700871 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
872 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
873 EXPECT_EQ(initial_value, new_value);
874
875 g_idle_add(&StaticQuitMainLoop, this);
876}
877
Jay Srinivasan08fce042012-06-07 16:31:01 -0700878TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
879 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
880 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
881 g_main_loop_run(loop_);
882 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700883 loop_ = nullptr;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700884}
885
886void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
887 // Tests that no scattering logic is enabled if the update check
888 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700889 int64_t initial_value = 8;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700890 Prefs prefs;
891 attempter_.prefs_ = &prefs;
892
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700893 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700894
895 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800896 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -0700897 &prefs_dir));
898 ScopedDirRemover temp_dir_remover(prefs_dir);
899
Alex Vakulenko75039d72014-03-25 12:36:28 -0700900 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -0700901 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700902 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700903 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
904
905 // make sure scatter_factor is non-zero as scattering is disabled
906 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700907 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700908
909 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
910 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
911
912 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700913 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700914
915 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
916 .WillRepeatedly(DoAll(
917 SetArgumentPointee<0>(scatter_factor_in_seconds),
918 Return(true)));
919
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800920 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700921 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700922 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
923
924 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700925 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700926 EXPECT_FALSE(
927 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700928 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700929 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
930 EXPECT_FALSE(
931 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700932 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
933
934 g_idle_add(&StaticQuitMainLoop, this);
935}
936
David Zeuthen985b1122013-10-09 12:13:15 -0700937// Checks that we only report daily metrics at most every 24 hours.
938TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
939 FakeClock fake_clock;
940 Prefs prefs;
941 string temp_dir;
942
943 // We need persistent preferences for this test
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700944 EXPECT_TRUE(utils::MakeTempDirectory("UpdateAttempterTest.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -0700945 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700946 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700947 fake_system_state_.set_clock(&fake_clock);
948 fake_system_state_.set_prefs(&prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700949
950 Time epoch = Time::FromInternalValue(0);
951 fake_clock.SetWallclockTime(epoch);
952
953 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
954 // we should report.
955 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
956 // We should not report again if no time has passed.
957 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
958
959 // We should not report if only 10 hours has passed.
960 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
961 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
962
963 // We should not report if only 24 hours - 1 sec has passed.
964 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
965 TimeDelta::FromSeconds(1));
966 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
967
968 // We should report if 24 hours has passed.
969 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
970 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
971
972 // But then we should not report again..
973 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
974
975 // .. until another 24 hours has passed
976 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
977 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
978 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
979 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
980 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
981
982 // .. and another 24 hours
983 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
984 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
985 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
986 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
987 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
988
989 // If the span between time of reporting and present time is
990 // negative, we report. This is in order to reset the timestamp and
991 // avoid an edge condition whereby a distant point in the future is
992 // in the state variable resulting in us never ever reporting again.
993 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
994 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
995 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
996
997 // In this case we should not update until the clock reads 71 + 24 = 95.
998 // Check that.
999 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1000 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1001 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1002 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1003 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1004
1005 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1006}
1007
David Zeuthen3c55abd2013-10-14 12:48:03 -07001008TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1009 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001010 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001011 update_completed_marker);
1012
1013 FakeClock fake_clock;
1014 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001015 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001016
1017 Time boot_time;
1018 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1019
1020 attempter.WriteUpdateCompletedMarker();
1021
1022 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1023 EXPECT_EQ(boot_time.ToTimeT(), 42);
1024}
1025
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001026} // namespace chromeos_update_engine