blob: 9098180c61674a4c0ac18179b1702e5b37efd8bb [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovf42cc1c2010-09-01 09:03:02 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan9abb7632014-08-07 00:10:53 -07005#include <stdint.h>
6
Ben Chan02f7c1d2014-10-18 15:18:02 -07007#include <memory>
8
Ben Chan06c76a42014-09-05 08:21:06 -07009#include <base/files/file_util.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070010#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020011#include <policy/libpolicy.h>
12#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070013
14#include "update_engine/action_mock.h"
15#include "update_engine/action_processor_mock.h"
David Zeuthen985b1122013-10-09 12:13:15 -070016#include "update_engine/fake_clock.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070017#include "update_engine/fake_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070018#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070019#include "update_engine/install_plan.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080020#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080021#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070022#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080023#include "update_engine/mock_payload_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070025#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070026#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080027#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028#include "update_engine/update_attempter.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070029#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030
David Zeuthen985b1122013-10-09 12:13:15 -070031using base::Time;
32using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070034using std::unique_ptr;
Darin Petkov36275772010-10-01 11:40:57 -070035using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070036using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070037using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080038using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070039using testing::Property;
40using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070041using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070042using testing::_;
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,
Alex Deymof329b932014-10-30 01:37:48 -070060 const string& update_completed_marker)
Gilad Arnold1f847232014-04-07 12:07:49 -070061 : 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);
Alex Deymo749ecf12014-10-21 20:06:57 -0700433
434 // Expect that the device policy is loaded by the UpdateAttempter at some
435 // point by calling RefreshDevicePolicy.
436 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
437 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
438 EXPECT_CALL(*device_policy, LoadPolicy())
439 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
440
441 {
442 InSequence s;
443 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
444 EXPECT_CALL(*processor_,
445 EnqueueAction(Property(&AbstractAction::Type,
446 kUpdateActionTypes[i]))).Times(1);
447 }
448 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700449 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700450
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700451 attempter_.Update("", "", "", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800452 g_idle_add(&StaticUpdateTestVerify, this);
453}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700454
Darin Petkove6ef2f82011-03-07 17:31:11 -0800455void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700456 EXPECT_EQ(0, attempter_.http_response_code());
457 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700458 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
459 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
460 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700461 }
462 EXPECT_EQ(attempter_.response_handler_action_.get(),
463 attempter_.actions_[1].get());
464 DownloadAction* download_action =
465 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700466 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700467 EXPECT_EQ(&attempter_, download_action->delegate());
468 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800469 g_main_loop_quit(loop_);
470}
471
Chris Sosa28e479c2013-07-12 11:39:53 -0700472void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700473 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700474 // Create a device policy so that we can change settings.
475 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
476 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
477
478 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700479 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700480
Don Garrett6646b442013-11-13 15:29:11 -0800481 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700482 // References bootable kernels in fake_hardware.h
483 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800484 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700485 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800486 rollback_kernel);
487 }
488
Chris Sosa28e479c2013-07-12 11:39:53 -0700489 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700490
Chris Sosad38b1132014-03-25 10:43:59 -0700491 // We only allow rollback on devices that are not enterprise enrolled and
492 // which have a valid slot to rollback to.
493 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700494 is_rollback_allowed = true;
495 }
496
Don Garrett6646b442013-11-13 15:29:11 -0800497 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700498 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800499 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700500 DoAll(SetArgumentPointee<0>(string("")),
Chris Sosa28e479c2013-07-12 11:39:53 -0700501 Return(true)));
502 } else {
503 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800504 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700505 DoAll(SetArgumentPointee<0>(string("fake.mail@fake.com")),
Chris Sosa76a29ae2013-07-11 17:59:24 -0700506 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700507 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700508
Chris Sosa28e479c2013-07-12 11:39:53 -0700509 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700510 InSequence s;
511 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
512 EXPECT_CALL(*processor_,
513 EnqueueAction(Property(&AbstractAction::Type,
514 kRollbackActionTypes[i]))).Times(1);
515 }
516 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
517
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700518 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700519 g_idle_add(&StaticRollbackTestVerify, this);
520 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700521 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700522 g_main_loop_quit(loop_);
523 }
524}
525
526void UpdateAttempterTest::RollbackTestVerify() {
527 // Verifies the actions that were enqueued.
528 EXPECT_EQ(&attempter_, processor_->delegate());
529 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
530 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
531 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
532 }
533 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
534 InstallPlanAction* install_plan_action =
535 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
536 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700537 // Matches fake_hardware.h -> rollback should move from kernel/boot device
538 // pair to other pair.
539 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
540 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700541 EXPECT_EQ(install_plan->powerwash_required, true);
542 g_main_loop_quit(loop_);
543}
544
Darin Petkove6ef2f82011-03-07 17:31:11 -0800545TEST_F(UpdateAttempterTest, UpdateTest) {
546 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
547 g_idle_add(&StaticUpdateTestStart, this);
548 g_main_loop_run(loop_);
549 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700550 loop_ = nullptr;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700551}
552
Chris Sosa76a29ae2013-07-11 17:59:24 -0700553TEST_F(UpdateAttempterTest, RollbackTest) {
554 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
555 g_idle_add(&StaticRollbackTestStart, this);
556 g_main_loop_run(loop_);
557 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700558 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700559}
560
Don Garrett6646b442013-11-13 15:29:11 -0800561TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
562 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
563 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
564 g_main_loop_run(loop_);
565 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700566 loop_ = nullptr;
Don Garrett6646b442013-11-13 15:29:11 -0800567}
568
Chris Sosa76a29ae2013-07-11 17:59:24 -0700569TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
570 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
571 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
572 g_main_loop_run(loop_);
573 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700574 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700575}
576
Thieu Le116fda32011-04-19 11:01:54 -0700577void UpdateAttempterTest::PingOmahaTestStart() {
578 EXPECT_CALL(*processor_,
579 EnqueueAction(Property(&AbstractAction::Type,
580 OmahaRequestAction::StaticType())))
581 .Times(1);
582 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
583 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200584 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700585}
586
587TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700588 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
589 EXPECT_FALSE(attempter_.schedule_updates_called());
590 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
591 // testing, which is more permissive than we want to handle here.
592 attempter_.DisableScheduleUpdates();
Thieu Le116fda32011-04-19 11:01:54 -0700593 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
594 g_idle_add(&StaticPingOmahaTestStart, this);
595 g_main_loop_run(loop_);
596 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700597 loop_ = nullptr;
Thieu Le116fda32011-04-19 11:01:54 -0700598 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700599 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700600}
601
Darin Petkov18c7bce2011-06-16 14:07:00 -0700602TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
603 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700604 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700605 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700606 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700607 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
608 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700609 EXPECT_EQ(
610 static_cast<ErrorCode>(static_cast<int>(kCode) |
611 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
612 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700613}
614
615TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
616 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700617 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700618 response_action->install_plan_.is_resume = true;
619 attempter_.response_handler_action_.reset(response_action);
620 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700621 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700622 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700623 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700624 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
625 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700626 EXPECT_EQ(
627 static_cast<ErrorCode>(
628 static_cast<int>(kCode) |
629 static_cast<int>(ErrorCode::kResumedFlag) |
630 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
631 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700632}
633
David Zeuthen8f191b22013-08-06 12:27:50 -0700634TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
635 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700636 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700637 mock_p2p_manager.fake().SetP2PEnabled(false);
638 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
639 attempter_.UpdateEngineStarted();
640}
641
642TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
643 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700644 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700645 mock_p2p_manager.fake().SetP2PEnabled(true);
646 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
647 attempter_.UpdateEngineStarted();
648}
649
650TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
651 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700652 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700653 mock_p2p_manager.fake().SetP2PEnabled(true);
654 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
655 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
656 attempter_.UpdateEngineStarted();
657}
658
659TEST_F(UpdateAttempterTest, P2PNotEnabled) {
660 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
661 g_idle_add(&StaticP2PNotEnabled, this);
662 g_main_loop_run(loop_);
663 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700664 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700665}
666gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
667 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
668 ua_test->P2PNotEnabledStart();
669 return FALSE;
670}
671void UpdateAttempterTest::P2PNotEnabledStart() {
672 // If P2P is not enabled, check that we do not attempt housekeeping
673 // and do not convey that p2p is to be used.
674 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700675 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700676 mock_p2p_manager.fake().SetP2PEnabled(false);
677 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700678 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700679 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
680 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
681 g_idle_add(&StaticQuitMainLoop, this);
682}
683
684TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
685 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
686 g_idle_add(&StaticP2PEnabledStartingFails, this);
687 g_main_loop_run(loop_);
688 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700689 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700690}
691gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
692 gpointer data) {
693 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
694 ua_test->P2PEnabledStartingFailsStart();
695 return FALSE;
696}
697void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
698 // If p2p is enabled, but starting it fails ensure we don't do
699 // any housekeeping and do not convey that p2p should be used.
700 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700701 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700702 mock_p2p_manager.fake().SetP2PEnabled(true);
703 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
704 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
705 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700706 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700707 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
708 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
709 g_idle_add(&StaticQuitMainLoop, this);
710}
711
712TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
713 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
714 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
715 g_main_loop_run(loop_);
716 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700717 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700718}
719gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
720 gpointer data) {
721 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
722 ua_test->P2PEnabledHousekeepingFailsStart();
723 return FALSE;
724}
725void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
726 // If p2p is enabled, starting it works but housekeeping fails, ensure
727 // we do not convey p2p is to be used.
728 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700729 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700730 mock_p2p_manager.fake().SetP2PEnabled(true);
731 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
732 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
733 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700734 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700735 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
736 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
737 g_idle_add(&StaticQuitMainLoop, this);
738}
739
740TEST_F(UpdateAttempterTest, P2PEnabled) {
741 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
742 g_idle_add(&StaticP2PEnabled, this);
743 g_main_loop_run(loop_);
744 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700745 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700746}
747gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
748 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
749 ua_test->P2PEnabledStart();
750 return FALSE;
751}
752void UpdateAttempterTest::P2PEnabledStart() {
753 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700754 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700755 // If P2P is enabled and starting it works, check that we performed
756 // housekeeping and that we convey p2p should be used.
757 mock_p2p_manager.fake().SetP2PEnabled(true);
758 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
759 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
760 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700761 attempter_.Update("", "", "", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700762 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
763 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
764 g_idle_add(&StaticQuitMainLoop, this);
765}
766
767TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
768 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
769 g_idle_add(&StaticP2PEnabledInteractive, this);
770 g_main_loop_run(loop_);
771 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700772 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700773}
774gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
775 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
776 ua_test->P2PEnabledInteractiveStart();
777 return FALSE;
778}
779void UpdateAttempterTest::P2PEnabledInteractiveStart() {
780 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700781 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700782 // For an interactive check, if P2P is enabled and starting it
783 // works, check that we performed housekeeping and that we convey
784 // p2p should be used for sharing but NOT for downloading.
785 mock_p2p_manager.fake().SetP2PEnabled(true);
786 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
787 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
788 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700789 attempter_.Update("", "", "", "", false, true /* interactive */);
David Zeuthen8f191b22013-08-06 12:27:50 -0700790 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
791 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
792 g_idle_add(&StaticQuitMainLoop, this);
793}
794
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700795TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
796 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
797 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
798 g_main_loop_run(loop_);
799 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700800 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700801}
802
803// Tests that the scatter_factor_in_seconds value is properly fetched
804// from the device policy.
805void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700806 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700807
808 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
809 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
810
811 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700812 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700813
814 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
815 .WillRepeatedly(DoAll(
816 SetArgumentPointee<0>(scatter_factor_in_seconds),
817 Return(true)));
818
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700819 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700820 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
821
822 g_idle_add(&StaticQuitMainLoop, this);
823}
824
825TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
826 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
827 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
828 g_main_loop_run(loop_);
829 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700830 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700831}
832
833void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
834 // Tests that the scatter_factor_in_seconds value is properly fetched
835 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700836 int64_t initial_value = 5;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700837 Prefs prefs;
838 attempter_.prefs_ = &prefs;
839
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700840 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700841
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700842 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800843 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700844 &prefs_dir));
845 ScopedDirRemover temp_dir_remover(prefs_dir);
846
Alex Vakulenko75039d72014-03-25 12:36:28 -0700847 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700848 << "Failed to initialize preferences.";
849 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
850
Ben Chan9abb7632014-08-07 00:10:53 -0700851 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700852
853 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
854 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
855
856 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700857 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700858
859 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
860 .WillRepeatedly(DoAll(
861 SetArgumentPointee<0>(scatter_factor_in_seconds),
862 Return(true)));
863
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700864 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700865 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
866
867 // Make sure the file still exists.
868 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
869
Ben Chan9abb7632014-08-07 00:10:53 -0700870 int64_t new_value;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700871 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
872 EXPECT_EQ(initial_value - 1, new_value);
873
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700874 EXPECT_TRUE(
875 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700876
877 // However, if the count is already 0, it's not decremented. Test that.
878 initial_value = 0;
879 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700880 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700881 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
882 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
883 EXPECT_EQ(initial_value, new_value);
884
885 g_idle_add(&StaticQuitMainLoop, this);
886}
887
Jay Srinivasan08fce042012-06-07 16:31:01 -0700888TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
889 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
890 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
891 g_main_loop_run(loop_);
892 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700893 loop_ = nullptr;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700894}
895
896void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
897 // Tests that no scattering logic is enabled if the update check
898 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700899 int64_t initial_value = 8;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700900 Prefs prefs;
901 attempter_.prefs_ = &prefs;
902
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700903 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700904
905 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800906 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -0700907 &prefs_dir));
908 ScopedDirRemover temp_dir_remover(prefs_dir);
909
Alex Vakulenko75039d72014-03-25 12:36:28 -0700910 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -0700911 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700912 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700913 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
914
915 // make sure scatter_factor is non-zero as scattering is disabled
916 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700917 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700918
919 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
920 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
921
922 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700923 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700924
925 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
926 .WillRepeatedly(DoAll(
927 SetArgumentPointee<0>(scatter_factor_in_seconds),
928 Return(true)));
929
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800930 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700931 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700932 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
933
934 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700935 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700936 EXPECT_FALSE(
937 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700938 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700939 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
940 EXPECT_FALSE(
941 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700942 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
943
944 g_idle_add(&StaticQuitMainLoop, this);
945}
946
David Zeuthen985b1122013-10-09 12:13:15 -0700947// Checks that we only report daily metrics at most every 24 hours.
948TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
949 FakeClock fake_clock;
950 Prefs prefs;
951 string temp_dir;
952
953 // We need persistent preferences for this test
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700954 EXPECT_TRUE(utils::MakeTempDirectory("UpdateAttempterTest.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -0700955 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700956 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700957 fake_system_state_.set_clock(&fake_clock);
958 fake_system_state_.set_prefs(&prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700959
960 Time epoch = Time::FromInternalValue(0);
961 fake_clock.SetWallclockTime(epoch);
962
963 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
964 // we should report.
965 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
966 // We should not report again if no time has passed.
967 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
968
969 // We should not report if only 10 hours has passed.
970 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
971 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
972
973 // We should not report if only 24 hours - 1 sec has passed.
974 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
975 TimeDelta::FromSeconds(1));
976 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
977
978 // We should report if 24 hours has passed.
979 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
980 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
981
982 // But then we should not report again..
983 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
984
985 // .. until another 24 hours has passed
986 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
987 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
988 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
989 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
990 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
991
992 // .. and another 24 hours
993 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
994 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
995 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
996 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
997 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
998
999 // If the span between time of reporting and present time is
1000 // negative, we report. This is in order to reset the timestamp and
1001 // avoid an edge condition whereby a distant point in the future is
1002 // in the state variable resulting in us never ever reporting again.
1003 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1004 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1005 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1006
1007 // In this case we should not update until the clock reads 71 + 24 = 95.
1008 // Check that.
1009 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1010 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1011 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1012 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1013 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1014
1015 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1016}
1017
David Zeuthen3c55abd2013-10-14 12:48:03 -07001018TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1019 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001020 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001021 update_completed_marker);
1022
1023 FakeClock fake_clock;
1024 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001025 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001026
1027 Time boot_time;
1028 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1029
1030 attempter.WriteUpdateCompletedMarker();
1031
1032 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1033 EXPECT_EQ(boot_time.ToTimeT(), 42);
1034}
1035
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001036} // namespace chromeos_update_engine