blob: 765d71d54acb7bc6fe90a9ed986583e11c3effa8 [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
Darin Petkovf42cc1c2010-09-01 09:03:02 -07007#include <base/file_util.h>
8#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +02009#include <policy/libpolicy.h>
10#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070011
12#include "update_engine/action_mock.h"
13#include "update_engine/action_processor_mock.h"
David Zeuthen985b1122013-10-09 12:13:15 -070014#include "update_engine/fake_clock.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070015#include "update_engine/fake_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070017#include "update_engine/install_plan.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080018#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080019#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070020#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021#include "update_engine/mock_payload_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070022#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070023#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070024#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080025#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070026#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080027#include "update_engine/update_check_scheduler.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070028#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070029
David Zeuthen985b1122013-10-09 12:13:15 -070030using base::Time;
31using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070032using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070033using testing::_;
34using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070035using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070036using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080037using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070038using testing::Property;
39using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070040using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070041
42namespace chromeos_update_engine {
43
44// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070045// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070046// methods.
47class UpdateAttempterUnderTest : public UpdateAttempter {
48 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070049 // We always feed an explicit update completed marker name; however, unless
50 // explicitly specified, we feed an empty string, which causes the
51 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070052 UpdateAttempterUnderTest(SystemState* system_state,
53 DBusWrapperInterface* dbus_iface)
54 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070055
Gilad Arnold1f847232014-04-07 12:07:49 -070056 UpdateAttempterUnderTest(SystemState* system_state,
57 DBusWrapperInterface* dbus_iface,
58 const std::string& update_completed_marker)
59 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070060};
61
62class UpdateAttempterTest : public ::testing::Test {
63 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070064 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -070065 : attempter_(&fake_system_state_, &dbus_),
66 mock_connection_manager(&fake_system_state_),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070067 loop_(nullptr) {
Gilad Arnold1f847232014-04-07 12:07:49 -070068 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070069 fake_system_state_.set_connection_manager(&mock_connection_manager);
70 fake_system_state_.set_update_attempter(&attempter_);
Gilad Arnold1f847232014-04-07 12:07:49 -070071
72 // Finish initializing the attempter.
73 attempter_.Init();
Alex Deymo3e0b53e2014-08-12 23:12:25 -070074
75 // We set the set_good_kernel command to a non-existent path so it fails to
76 // run it. This avoids the async call to the command and continues the
77 // update process right away. Tests testing that behavior can override the
78 // default set_good_kernel command if needed.
79 attempter_.set_good_kernel_cmd_ = "/path/to/non-existent/command";
Jay Srinivasan43488792012-06-19 00:25:31 -070080 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070081
Darin Petkovf42cc1c2010-09-01 09:03:02 -070082 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070083 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
84
Alex Vakulenko88b591f2014-08-28 16:48:57 -070085 EXPECT_EQ(nullptr, attempter_.dbus_service_);
86 EXPECT_NE(nullptr, attempter_.system_state_);
87 EXPECT_EQ(nullptr, attempter_.update_check_scheduler_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070088 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080089 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070090 EXPECT_EQ(nullptr, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070091 EXPECT_FALSE(attempter_.download_active_);
92 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
93 EXPECT_EQ(0.0, attempter_.download_progress_);
94 EXPECT_EQ(0, attempter_.last_checked_time_);
95 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070096 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080097 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070098 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070099 prefs_ = fake_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700100 }
101
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700102 virtual void TearDown() {
103 utils::RecursiveUnlinkDir(test_dir_);
104 }
105
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200106 void QuitMainLoop();
107 static gboolean StaticQuitMainLoop(gpointer data);
108
Darin Petkove6ef2f82011-03-07 17:31:11 -0800109 void UpdateTestStart();
110 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800111 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800112 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700113 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800114 static gboolean StaticUpdateTestStart(gpointer data);
115 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700116 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800117 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700118 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
119 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200120
Thieu Le116fda32011-04-19 11:01:54 -0700121 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700122 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200123
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700124 void ReadChannelFromPolicyTestStart();
125 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800126
Jay Srinivasan0a708742012-03-20 11:26:12 -0700127 void ReadUpdateDisabledFromPolicyTestStart();
128 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
129
130 void ReadTargetVersionPrefixFromPolicyTestStart();
131 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
132 gpointer data);
133
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700134 void ReadScatterFactorFromPolicyTestStart();
135 static gboolean StaticReadScatterFactorFromPolicyTestStart(
136 gpointer data);
137
138 void DecrementUpdateCheckCountTestStart();
139 static gboolean StaticDecrementUpdateCheckCountTestStart(
140 gpointer data);
141
Jay Srinivasan08fce042012-06-07 16:31:01 -0700142 void NoScatteringDoneDuringManualUpdateTestStart();
143 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
144 gpointer data);
145
David Zeuthen8f191b22013-08-06 12:27:50 -0700146 void P2PNotEnabledStart();
147 static gboolean StaticP2PNotEnabled(gpointer data);
148
149 void P2PEnabledStart();
150 static gboolean StaticP2PEnabled(gpointer data);
151
152 void P2PEnabledInteractiveStart();
153 static gboolean StaticP2PEnabledInteractive(gpointer data);
154
155 void P2PEnabledStartingFailsStart();
156 static gboolean StaticP2PEnabledStartingFails(gpointer data);
157
158 void P2PEnabledHousekeepingFailsStart();
159 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
160
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700161 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800162 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700163 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800164 NiceMock<ActionProcessorMock>* processor_;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700165 NiceMock<PrefsMock>* prefs_; // shortcut to fake_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800166 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800167 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700168
169 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700170};
171
Darin Petkov1b003102010-11-30 10:18:36 -0800172TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700173 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800174 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700175 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800176 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700177 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800178 EXPECT_EQ(503, attempter_.http_response_code());
179 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700180 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800181}
182
183TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
184 ActionMock action;
185 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
186 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800187 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800188 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700189 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
190 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800191}
192
193TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700194 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800195 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700196 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800197 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800198 ObjectCollectorAction<OmahaResponse> collector_action;
199 BondActions(&action, &collector_action);
200 OmahaResponse response;
201 response.poll_interval = 234;
202 action.SetOutputObject(response);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700203 UpdateCheckScheduler scheduler(&attempter_, &fake_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800204 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800205 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700206 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800207 EXPECT_EQ(500, attempter_.http_response_code());
208 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
209 EXPECT_EQ(234, scheduler.poll_interval());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700210 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800211}
212
Darin Petkovcd1666f2010-09-23 09:53:44 -0700213TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700214 string test_update_completed_marker;
215 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800216 "update_attempter_unittest-update_completed_marker-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700217 &test_update_completed_marker, nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700218 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700219 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700220 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700221 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700222 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700223 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700224}
225
226TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700227 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
228 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700229 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700230 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700231
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700232 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700233 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
234 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700235 EXPECT_EQ(ErrorCode::kOmahaRequestError,
236 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700237 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700238 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700239 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700240 ErrorCode::kError));
Alex Deymo42432912013-07-12 20:21:15 -0700241 FilesystemCopierAction filesystem_copier_action(
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700242 &fake_system_state_, false, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700243 EXPECT_EQ(ErrorCode::kFilesystemCopierError,
244 GetErrorCodeForAction(&filesystem_copier_action,
245 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800246 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700247 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700248 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700249 ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700250 ActionMock action_mock;
251 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700252 EXPECT_EQ(ErrorCode::kError,
253 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700254}
255
Darin Petkov36275772010-10-01 11:40:57 -0700256TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700257 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800258 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700259 .WillOnce(Return(false));
260 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700261 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800262 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700263 .WillOnce(DoAll(
264 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
265 Return(true)));
266 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700267 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800268 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700269 .WillOnce(DoAll(
270 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
271 Return(true)));
272 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700273 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800274 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700275 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700276 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700277}
278
279TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800280 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700281 .WillOnce(Return(false))
282 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
283 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
284 .WillOnce(DoAll(
285 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
286 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800287 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700288 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800289 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
290 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
291 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700292 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
293 .Times(1);
294 for (int i = 0; i < 4; i ++)
295 attempter_.MarkDeltaUpdateFailure();
296}
297
Darin Petkov1b003102010-11-30 10:18:36 -0800298TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
299 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
300 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700301 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800302 .Times(0);
303 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700304 string url1 = "http://url1";
305 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800306 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700307 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700308 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700309 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800310 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700311 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800312}
313
314TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
315 EXPECT_CALL(*processor_,
316 EnqueueAction(Property(&AbstractAction::Type,
317 OmahaRequestAction::StaticType())))
318 .Times(1);
319 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700320 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700321 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800322 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
323 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800324 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800325 attempter_.ScheduleErrorEventAction();
326 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
327}
328
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200329void UpdateAttempterTest::QuitMainLoop() {
330 g_main_loop_quit(loop_);
331}
332
333gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
334 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
335 return FALSE;
336}
337
Darin Petkove6ef2f82011-03-07 17:31:11 -0800338gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
339 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
340 return FALSE;
341}
342
343gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
344 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
345 return FALSE;
346}
347
Chris Sosa76a29ae2013-07-11 17:59:24 -0700348gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800349 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700350 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800351 return FALSE;
352}
353
354gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
355 gpointer data) {
356 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700357 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700358 return FALSE;
359}
360
361gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800362 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700363 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700364 return FALSE;
365}
366
367gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
368 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
369 return FALSE;
370}
371
Thieu Le116fda32011-04-19 11:01:54 -0700372gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
373 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
374 return FALSE;
375}
376
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700377gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200378 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700379 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
380 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700381 return FALSE;
382}
383
Jay Srinivasan0a708742012-03-20 11:26:12 -0700384gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
385 gpointer data) {
386 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
387 ua_test->ReadUpdateDisabledFromPolicyTestStart();
388 return FALSE;
389}
390
391gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
392 gpointer data) {
393 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
394 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
395 return FALSE;
396}
397
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700398gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
399 gpointer data) {
400 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
401 ua_test->ReadScatterFactorFromPolicyTestStart();
402 return FALSE;
403}
404
405gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
406 gpointer data) {
407 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
408 ua_test->DecrementUpdateCheckCountTestStart();
409 return FALSE;
410}
411
Jay Srinivasan08fce042012-06-07 16:31:01 -0700412gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
413 gpointer data) {
414 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
415 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
416 return FALSE;
417}
418
Darin Petkove6ef2f82011-03-07 17:31:11 -0800419namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700420// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700421const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800422 OmahaRequestAction::StaticType(),
423 OmahaResponseHandlerAction::StaticType(),
424 FilesystemCopierAction::StaticType(),
425 FilesystemCopierAction::StaticType(),
426 OmahaRequestAction::StaticType(),
427 DownloadAction::StaticType(),
428 OmahaRequestAction::StaticType(),
429 FilesystemCopierAction::StaticType(),
430 FilesystemCopierAction::StaticType(),
431 PostinstallRunnerAction::StaticType(),
432 OmahaRequestAction::StaticType()
433};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700434
435// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700436const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700437 InstallPlanAction::StaticType(),
438 PostinstallRunnerAction::StaticType(),
439};
440
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700441} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800442
443void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700444 attempter_.set_http_response_code(200);
445 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700446 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700447 EXPECT_CALL(*processor_,
448 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700449 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700450 }
451 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
452
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700453 attempter_.Update("", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800454 g_idle_add(&StaticUpdateTestVerify, this);
455}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700456
Darin Petkove6ef2f82011-03-07 17:31:11 -0800457void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700458 EXPECT_EQ(0, attempter_.http_response_code());
459 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700460 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
461 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
462 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700463 }
464 EXPECT_EQ(attempter_.response_handler_action_.get(),
465 attempter_.actions_[1].get());
466 DownloadAction* download_action =
467 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700468 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700469 EXPECT_EQ(&attempter_, download_action->delegate());
470 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800471 g_main_loop_quit(loop_);
472}
473
Chris Sosa28e479c2013-07-12 11:39:53 -0700474void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700475 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700476 // Create a device policy so that we can change settings.
477 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
478 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
479
480 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700481 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700482
Don Garrett6646b442013-11-13 15:29:11 -0800483 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700484 // References bootable kernels in fake_hardware.h
485 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800486 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700487 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800488 rollback_kernel);
489 }
490
Chris Sosa28e479c2013-07-12 11:39:53 -0700491 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700492
Chris Sosad38b1132014-03-25 10:43:59 -0700493 // We only allow rollback on devices that are not enterprise enrolled and
494 // which have a valid slot to rollback to.
495 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700496 is_rollback_allowed = true;
497 }
498
Don Garrett6646b442013-11-13 15:29:11 -0800499 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700500 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800501 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa28e479c2013-07-12 11:39:53 -0700502 DoAll(SetArgumentPointee<0>(std::string("")),
503 Return(true)));
504 } else {
505 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800506 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa76a29ae2013-07-11 17:59:24 -0700507 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
508 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700509 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700510
Chris Sosa28e479c2013-07-12 11:39:53 -0700511 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700512 InSequence s;
513 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
514 EXPECT_CALL(*processor_,
515 EnqueueAction(Property(&AbstractAction::Type,
516 kRollbackActionTypes[i]))).Times(1);
517 }
518 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
519
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700520 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700521 g_idle_add(&StaticRollbackTestVerify, this);
522 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700523 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700524 g_main_loop_quit(loop_);
525 }
526}
527
528void UpdateAttempterTest::RollbackTestVerify() {
529 // Verifies the actions that were enqueued.
530 EXPECT_EQ(&attempter_, processor_->delegate());
531 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
532 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
533 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
534 }
535 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
536 InstallPlanAction* install_plan_action =
537 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
538 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700539 // Matches fake_hardware.h -> rollback should move from kernel/boot device
540 // pair to other pair.
541 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
542 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700543 EXPECT_EQ(install_plan->powerwash_required, true);
544 g_main_loop_quit(loop_);
545}
546
Darin Petkove6ef2f82011-03-07 17:31:11 -0800547TEST_F(UpdateAttempterTest, UpdateTest) {
548 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
549 g_idle_add(&StaticUpdateTestStart, this);
550 g_main_loop_run(loop_);
551 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700552 loop_ = nullptr;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700553}
554
Chris Sosa76a29ae2013-07-11 17:59:24 -0700555TEST_F(UpdateAttempterTest, RollbackTest) {
556 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
557 g_idle_add(&StaticRollbackTestStart, this);
558 g_main_loop_run(loop_);
559 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700560 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700561}
562
Don Garrett6646b442013-11-13 15:29:11 -0800563TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
564 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
565 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
566 g_main_loop_run(loop_);
567 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700568 loop_ = nullptr;
Don Garrett6646b442013-11-13 15:29:11 -0800569}
570
Chris Sosa76a29ae2013-07-11 17:59:24 -0700571TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
572 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
573 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
574 g_main_loop_run(loop_);
575 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700576 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700577}
578
Thieu Le116fda32011-04-19 11:01:54 -0700579void UpdateAttempterTest::PingOmahaTestStart() {
580 EXPECT_CALL(*processor_,
581 EnqueueAction(Property(&AbstractAction::Type,
582 OmahaRequestAction::StaticType())))
583 .Times(1);
584 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
585 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200586 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700587}
588
589TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700590 UpdateCheckScheduler scheduler(&attempter_, &fake_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700591 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700592 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700593 attempter_.set_update_check_scheduler(&scheduler);
594 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
595 g_idle_add(&StaticPingOmahaTestStart, this);
596 g_main_loop_run(loop_);
597 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700598 loop_ = nullptr;
Thieu Le116fda32011-04-19 11:01:54 -0700599 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
600 EXPECT_EQ(true, scheduler.scheduled_);
601}
602
Darin Petkov18c7bce2011-06-16 14:07:00 -0700603TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
604 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700605 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700606 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700607 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700608 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
609 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700610 EXPECT_EQ(
611 static_cast<ErrorCode>(static_cast<int>(kCode) |
612 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
613 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700614}
615
616TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
617 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700618 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700619 response_action->install_plan_.is_resume = true;
620 attempter_.response_handler_action_.reset(response_action);
621 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700622 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700623 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700624 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700625 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
626 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700627 EXPECT_EQ(
628 static_cast<ErrorCode>(
629 static_cast<int>(kCode) |
630 static_cast<int>(ErrorCode::kResumedFlag) |
631 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
632 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700633}
634
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700635TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200636 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700637 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200638 g_main_loop_run(loop_);
639 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700640 loop_ = nullptr;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200641}
642
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700643void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
644 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200645 // from the device policy.
646
647 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
648 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
649
650 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700651 fake_system_state_.set_device_policy(device_policy);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200652
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700653 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700654 DoAll(SetArgumentPointee<0>(bool(false)), // NOLINT(readability/casting)
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700655 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200656
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700657 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
658 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
659 Return(true)));
660
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700661 ASSERT_FALSE(test_dir_.empty());
662 attempter_.omaha_request_params_->set_root(test_dir_);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700663 attempter_.Update("", "", false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700664 EXPECT_EQ("beta-channel",
665 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200666
667 g_idle_add(&StaticQuitMainLoop, this);
668}
669
Jay Srinivasan0a708742012-03-20 11:26:12 -0700670TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
671 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
672 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
673 g_main_loop_run(loop_);
674 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700675 loop_ = nullptr;
Jay Srinivasan0a708742012-03-20 11:26:12 -0700676}
677
678void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
679 // Tests that the update_disbled flag is properly fetched
680 // from the device policy.
681
682 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
683 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
684
685 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700686 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700687
688 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
689 .WillRepeatedly(DoAll(
690 SetArgumentPointee<0>(true),
691 Return(true)));
692
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700693 attempter_.Update("", "", false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700694 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700695
696 g_idle_add(&StaticQuitMainLoop, this);
697}
698
David Zeuthen8f191b22013-08-06 12:27:50 -0700699TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
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(false);
703 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
704 attempter_.UpdateEngineStarted();
705}
706
707TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
708 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700709 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700710 mock_p2p_manager.fake().SetP2PEnabled(true);
711 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
712 attempter_.UpdateEngineStarted();
713}
714
715TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
716 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700717 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700718 mock_p2p_manager.fake().SetP2PEnabled(true);
719 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
720 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
721 attempter_.UpdateEngineStarted();
722}
723
724TEST_F(UpdateAttempterTest, P2PNotEnabled) {
725 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
726 g_idle_add(&StaticP2PNotEnabled, this);
727 g_main_loop_run(loop_);
728 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700729 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700730}
731gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
732 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
733 ua_test->P2PNotEnabledStart();
734 return FALSE;
735}
736void UpdateAttempterTest::P2PNotEnabledStart() {
737 // If P2P is not enabled, check that we do not attempt housekeeping
738 // and do not convey that p2p is to be used.
739 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700740 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700741 mock_p2p_manager.fake().SetP2PEnabled(false);
742 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700743 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700744 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
745 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
746 g_idle_add(&StaticQuitMainLoop, this);
747}
748
749TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
750 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
751 g_idle_add(&StaticP2PEnabledStartingFails, this);
752 g_main_loop_run(loop_);
753 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700754 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700755}
756gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
757 gpointer data) {
758 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
759 ua_test->P2PEnabledStartingFailsStart();
760 return FALSE;
761}
762void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
763 // If p2p is enabled, but starting it fails ensure we don't do
764 // any housekeeping and do not convey that p2p should be used.
765 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700766 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700767 mock_p2p_manager.fake().SetP2PEnabled(true);
768 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
769 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
770 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700771 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700772 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
773 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
774 g_idle_add(&StaticQuitMainLoop, this);
775}
776
777TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
778 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
779 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
780 g_main_loop_run(loop_);
781 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700782 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700783}
784gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
785 gpointer data) {
786 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
787 ua_test->P2PEnabledHousekeepingFailsStart();
788 return FALSE;
789}
790void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
791 // If p2p is enabled, starting it works but housekeeping fails, ensure
792 // we do not convey p2p is to be used.
793 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700794 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700795 mock_p2p_manager.fake().SetP2PEnabled(true);
796 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
797 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
798 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700799 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700800 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
801 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
802 g_idle_add(&StaticQuitMainLoop, this);
803}
804
805TEST_F(UpdateAttempterTest, P2PEnabled) {
806 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
807 g_idle_add(&StaticP2PEnabled, this);
808 g_main_loop_run(loop_);
809 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700810 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700811}
812gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
813 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
814 ua_test->P2PEnabledStart();
815 return FALSE;
816}
817void UpdateAttempterTest::P2PEnabledStart() {
818 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700819 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700820 // If P2P is enabled and starting it works, check that we performed
821 // housekeeping and that we convey p2p should be used.
822 mock_p2p_manager.fake().SetP2PEnabled(true);
823 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
824 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
825 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700826 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700827 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
828 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
829 g_idle_add(&StaticQuitMainLoop, this);
830}
831
832TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
833 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
834 g_idle_add(&StaticP2PEnabledInteractive, this);
835 g_main_loop_run(loop_);
836 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700837 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700838}
839gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
840 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
841 ua_test->P2PEnabledInteractiveStart();
842 return FALSE;
843}
844void UpdateAttempterTest::P2PEnabledInteractiveStart() {
845 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700846 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700847 // For an interactive check, if P2P is enabled and starting it
848 // works, check that we performed housekeeping and that we convey
849 // p2p should be used for sharing but NOT for downloading.
850 mock_p2p_manager.fake().SetP2PEnabled(true);
851 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
852 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
853 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700854 attempter_.Update("", "", false, true /* interactive */);
David Zeuthen8f191b22013-08-06 12:27:50 -0700855 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
856 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
857 g_idle_add(&StaticQuitMainLoop, this);
858}
859
Jay Srinivasan0a708742012-03-20 11:26:12 -0700860TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
861 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
862 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
863 g_main_loop_run(loop_);
864 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700865 loop_ = nullptr;
Jay Srinivasan0a708742012-03-20 11:26:12 -0700866}
867
868void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
869 // Tests that the target_version_prefix value is properly fetched
870 // from the device policy.
871
872 const std::string target_version_prefix = "1412.";
873
874 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
875 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
876
877 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700878 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700879
880 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
881 .WillRepeatedly(DoAll(
882 SetArgumentPointee<0>(target_version_prefix),
883 Return(true)));
884
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700885 attempter_.Update("", "", false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700886 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700887 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700888
889 g_idle_add(&StaticQuitMainLoop, this);
890}
891
892
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700893TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
894 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
895 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
896 g_main_loop_run(loop_);
897 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700898 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700899}
900
901// Tests that the scatter_factor_in_seconds value is properly fetched
902// from the device policy.
903void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700904 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700905
906 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
907 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
908
909 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700910 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700911
912 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
913 .WillRepeatedly(DoAll(
914 SetArgumentPointee<0>(scatter_factor_in_seconds),
915 Return(true)));
916
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700917 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700918 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
919
920 g_idle_add(&StaticQuitMainLoop, this);
921}
922
923TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
924 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
925 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
926 g_main_loop_run(loop_);
927 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700928 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700929}
930
931void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
932 // Tests that the scatter_factor_in_seconds value is properly fetched
933 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700934 int64_t initial_value = 5;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700935 Prefs prefs;
936 attempter_.prefs_ = &prefs;
937
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700938 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700939
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700940 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800941 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700942 &prefs_dir));
943 ScopedDirRemover temp_dir_remover(prefs_dir);
944
Alex Vakulenko75039d72014-03-25 12:36:28 -0700945 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700946 << "Failed to initialize preferences.";
947 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
948
Ben Chan9abb7632014-08-07 00:10:53 -0700949 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700950
951 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
952 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
953
954 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700955 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700956
957 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
958 .WillRepeatedly(DoAll(
959 SetArgumentPointee<0>(scatter_factor_in_seconds),
960 Return(true)));
961
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700962 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700963 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
964
965 // Make sure the file still exists.
966 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
967
Ben Chan9abb7632014-08-07 00:10:53 -0700968 int64_t new_value;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700969 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
970 EXPECT_EQ(initial_value - 1, new_value);
971
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700972 EXPECT_TRUE(
973 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700974
975 // However, if the count is already 0, it's not decremented. Test that.
976 initial_value = 0;
977 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700978 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700979 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
980 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
981 EXPECT_EQ(initial_value, new_value);
982
983 g_idle_add(&StaticQuitMainLoop, this);
984}
985
Jay Srinivasan08fce042012-06-07 16:31:01 -0700986TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
987 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
988 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
989 g_main_loop_run(loop_);
990 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700991 loop_ = nullptr;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700992}
993
994void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
995 // Tests that no scattering logic is enabled if the update check
996 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700997 int64_t initial_value = 8;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700998 Prefs prefs;
999 attempter_.prefs_ = &prefs;
1000
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001001 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001002
1003 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -08001004 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -07001005 &prefs_dir));
1006 ScopedDirRemover temp_dir_remover(prefs_dir);
1007
Alex Vakulenko75039d72014-03-25 12:36:28 -07001008 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -07001009 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -07001010 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001011 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
1012
1013 // make sure scatter_factor is non-zero as scattering is disabled
1014 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -07001015 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001016
1017 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
1018 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
1019
1020 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001021 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001022
1023 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
1024 .WillRepeatedly(DoAll(
1025 SetArgumentPointee<0>(scatter_factor_in_seconds),
1026 Return(true)));
1027
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001028 // Trigger an interactive check so we can test that scattering is disabled.
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -07001029 attempter_.Update("", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001030 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1031
1032 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001033 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001034 EXPECT_FALSE(
1035 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001036 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001037 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1038 EXPECT_FALSE(
1039 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001040 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
1041
1042 g_idle_add(&StaticQuitMainLoop, this);
1043}
1044
David Zeuthen985b1122013-10-09 12:13:15 -07001045// Checks that we only report daily metrics at most every 24 hours.
1046TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1047 FakeClock fake_clock;
1048 Prefs prefs;
1049 string temp_dir;
1050
1051 // We need persistent preferences for this test
Gilad Arnolda6742b32014-01-11 00:18:34 -08001052 EXPECT_TRUE(utils::MakeTempDirectory("UpdateCheckScheduler.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -07001053 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001054 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001055 fake_system_state_.set_clock(&fake_clock);
1056 fake_system_state_.set_prefs(&prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001057
1058 Time epoch = Time::FromInternalValue(0);
1059 fake_clock.SetWallclockTime(epoch);
1060
1061 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1062 // we should report.
1063 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1064 // We should not report again if no time has passed.
1065 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1066
1067 // We should not report if only 10 hours has passed.
1068 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1069 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1070
1071 // We should not report if only 24 hours - 1 sec has passed.
1072 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1073 TimeDelta::FromSeconds(1));
1074 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1075
1076 // We should report if 24 hours has passed.
1077 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1078 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1079
1080 // But then we should not report again..
1081 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1082
1083 // .. until another 24 hours has passed
1084 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1085 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1086 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1087 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1088 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1089
1090 // .. and another 24 hours
1091 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1092 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1093 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1094 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1095 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1096
1097 // If the span between time of reporting and present time is
1098 // negative, we report. This is in order to reset the timestamp and
1099 // avoid an edge condition whereby a distant point in the future is
1100 // in the state variable resulting in us never ever reporting again.
1101 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1102 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1103 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1104
1105 // In this case we should not update until the clock reads 71 + 24 = 95.
1106 // Check that.
1107 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1108 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1109 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1110 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1111 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1112
1113 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1114}
1115
David Zeuthen3c55abd2013-10-14 12:48:03 -07001116TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1117 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001118 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001119 update_completed_marker);
1120
1121 FakeClock fake_clock;
1122 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001123 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001124
1125 Time boot_time;
1126 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1127
1128 attempter.WriteUpdateCompletedMarker();
1129
1130 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1131 EXPECT_EQ(boot_time.ToTimeT(), 42);
1132}
1133
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001134} // namespace chromeos_update_engine