blob: 632c2971fbbda3df8756bc45dcf93b86c3e6ddec [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_),
Jay Srinivasan43488792012-06-19 00:25:31 -070067 loop_(NULL) {
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();
Jay Srinivasan43488792012-06-19 00:25:31 -070074 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070075
Darin Petkovf42cc1c2010-09-01 09:03:02 -070076 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070077 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
78
Darin Petkovf42cc1c2010-09-01 09:03:02 -070079 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080080 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070081 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
82 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080083 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
84 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070085 EXPECT_FALSE(attempter_.download_active_);
86 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
87 EXPECT_EQ(0.0, attempter_.download_progress_);
88 EXPECT_EQ(0, attempter_.last_checked_time_);
89 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070090 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080091 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070092 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070093 prefs_ = fake_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070094 }
95
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070096 virtual void TearDown() {
97 utils::RecursiveUnlinkDir(test_dir_);
98 }
99
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200100 void QuitMainLoop();
101 static gboolean StaticQuitMainLoop(gpointer data);
102
Darin Petkove6ef2f82011-03-07 17:31:11 -0800103 void UpdateTestStart();
104 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800105 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800106 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700107 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800108 static gboolean StaticUpdateTestStart(gpointer data);
109 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700110 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800111 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700112 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
113 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200114
Thieu Le116fda32011-04-19 11:01:54 -0700115 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700116 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200117
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700118 void ReadChannelFromPolicyTestStart();
119 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800120
Jay Srinivasan0a708742012-03-20 11:26:12 -0700121 void ReadUpdateDisabledFromPolicyTestStart();
122 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
123
124 void ReadTargetVersionPrefixFromPolicyTestStart();
125 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
126 gpointer data);
127
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700128 void ReadScatterFactorFromPolicyTestStart();
129 static gboolean StaticReadScatterFactorFromPolicyTestStart(
130 gpointer data);
131
132 void DecrementUpdateCheckCountTestStart();
133 static gboolean StaticDecrementUpdateCheckCountTestStart(
134 gpointer data);
135
Jay Srinivasan08fce042012-06-07 16:31:01 -0700136 void NoScatteringDoneDuringManualUpdateTestStart();
137 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
138 gpointer data);
139
David Zeuthen8f191b22013-08-06 12:27:50 -0700140 void P2PNotEnabledStart();
141 static gboolean StaticP2PNotEnabled(gpointer data);
142
143 void P2PEnabledStart();
144 static gboolean StaticP2PEnabled(gpointer data);
145
146 void P2PEnabledInteractiveStart();
147 static gboolean StaticP2PEnabledInteractive(gpointer data);
148
149 void P2PEnabledStartingFailsStart();
150 static gboolean StaticP2PEnabledStartingFails(gpointer data);
151
152 void P2PEnabledHousekeepingFailsStart();
153 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
154
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700155 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800156 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700157 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800158 NiceMock<ActionProcessorMock>* processor_;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700159 NiceMock<PrefsMock>* prefs_; // shortcut to fake_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800160 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800161 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700162
163 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700164};
165
Darin Petkov1b003102010-11-30 10:18:36 -0800166TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
167 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
168 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800169 DownloadAction action(prefs_, NULL, fetcher.release());
170 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700171 attempter_.ActionCompleted(NULL, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800172 EXPECT_EQ(503, attempter_.http_response_code());
173 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
174 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
175}
176
177TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
178 ActionMock action;
179 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
180 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800181 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800182 .WillOnce(Return(false));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700183 attempter_.ActionCompleted(NULL, &action, ErrorCode::kError);
Darin Petkov1b003102010-11-30 10:18:36 -0800184 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
185}
186
187TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
188 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
189 fetcher->FailTransfer(500); // Sets the HTTP response code.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700190 OmahaRequestAction action(&fake_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800191 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800192 ObjectCollectorAction<OmahaResponse> collector_action;
193 BondActions(&action, &collector_action);
194 OmahaResponse response;
195 response.poll_interval = 234;
196 action.SetOutputObject(response);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700197 UpdateCheckScheduler scheduler(&attempter_, &fake_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800198 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800199 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700200 attempter_.ActionCompleted(NULL, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800201 EXPECT_EQ(500, attempter_.http_response_code());
202 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
203 EXPECT_EQ(234, scheduler.poll_interval());
204 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
205}
206
Darin Petkovcd1666f2010-09-23 09:53:44 -0700207TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700208 string test_update_completed_marker;
209 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800210 "update_attempter_unittest-update_completed_marker-XXXXXX",
Gilad Arnold70e476e2013-07-30 16:01:13 -0700211 &test_update_completed_marker, NULL));
212 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700213 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700214 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700215 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700216 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700217 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700218}
219
220TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700221 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
222 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700223 EXPECT_EQ(ErrorCode::kSuccess,
224 GetErrorCodeForAction(NULL, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700225
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700226 FakeSystemState fake_system_state;
227 OmahaRequestAction omaha_request_action(&fake_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800228 NULL, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700229 EXPECT_EQ(ErrorCode::kOmahaRequestError,
230 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700231 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700232 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700233 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700234 ErrorCode::kError));
Alex Deymo42432912013-07-12 20:21:15 -0700235 FilesystemCopierAction filesystem_copier_action(
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700236 &fake_system_state_, false, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700237 EXPECT_EQ(ErrorCode::kFilesystemCopierError,
238 GetErrorCodeForAction(&filesystem_copier_action,
239 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800240 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700241 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700242 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700243 ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700244 ActionMock action_mock;
245 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700246 EXPECT_EQ(ErrorCode::kError,
247 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700248}
249
Darin Petkov36275772010-10-01 11:40:57 -0700250TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700251 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800252 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700253 .WillOnce(Return(false));
254 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700255 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800256 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700257 .WillOnce(DoAll(
258 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
259 Return(true)));
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),
265 Return(true)));
266 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700267 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800268 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700269 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700270 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700271}
272
273TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800274 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700275 .WillOnce(Return(false))
276 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
277 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
278 .WillOnce(DoAll(
279 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
280 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800281 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700282 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800283 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
284 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
285 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700286 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
287 .Times(1);
288 for (int i = 0; i < 4; i ++)
289 attempter_.MarkDeltaUpdateFailure();
290}
291
Darin Petkov1b003102010-11-30 10:18:36 -0800292TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
293 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
294 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700295 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296 .Times(0);
297 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700298 string url1 = "http://url1";
299 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800300 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700301 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700302 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700303 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800304 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700305 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800306}
307
308TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
309 EXPECT_CALL(*processor_,
310 EnqueueAction(Property(&AbstractAction::Type,
311 OmahaRequestAction::StaticType())))
312 .Times(1);
313 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700314 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700315 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800316 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
317 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800318 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800319 attempter_.ScheduleErrorEventAction();
320 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
321}
322
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200323void UpdateAttempterTest::QuitMainLoop() {
324 g_main_loop_quit(loop_);
325}
326
327gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
328 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
329 return FALSE;
330}
331
Darin Petkove6ef2f82011-03-07 17:31:11 -0800332gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
333 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
334 return FALSE;
335}
336
337gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
338 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
339 return FALSE;
340}
341
Chris Sosa76a29ae2013-07-11 17:59:24 -0700342gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800343 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700344 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800345 return FALSE;
346}
347
348gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
349 gpointer data) {
350 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700351 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700352 return FALSE;
353}
354
355gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800356 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700357 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700358 return FALSE;
359}
360
361gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
362 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
363 return FALSE;
364}
365
Thieu Le116fda32011-04-19 11:01:54 -0700366gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
367 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
368 return FALSE;
369}
370
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700371gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200372 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700373 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
374 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700375 return FALSE;
376}
377
Jay Srinivasan0a708742012-03-20 11:26:12 -0700378gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
379 gpointer data) {
380 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
381 ua_test->ReadUpdateDisabledFromPolicyTestStart();
382 return FALSE;
383}
384
385gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
386 gpointer data) {
387 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
388 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
389 return FALSE;
390}
391
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700392gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
393 gpointer data) {
394 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
395 ua_test->ReadScatterFactorFromPolicyTestStart();
396 return FALSE;
397}
398
399gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
400 gpointer data) {
401 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
402 ua_test->DecrementUpdateCheckCountTestStart();
403 return FALSE;
404}
405
Jay Srinivasan08fce042012-06-07 16:31:01 -0700406gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
407 gpointer data) {
408 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
409 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
410 return FALSE;
411}
412
Darin Petkove6ef2f82011-03-07 17:31:11 -0800413namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700414// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700415const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800416 OmahaRequestAction::StaticType(),
417 OmahaResponseHandlerAction::StaticType(),
418 FilesystemCopierAction::StaticType(),
419 FilesystemCopierAction::StaticType(),
420 OmahaRequestAction::StaticType(),
421 DownloadAction::StaticType(),
422 OmahaRequestAction::StaticType(),
423 FilesystemCopierAction::StaticType(),
424 FilesystemCopierAction::StaticType(),
425 PostinstallRunnerAction::StaticType(),
426 OmahaRequestAction::StaticType()
427};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700428
429// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700430const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700431 InstallPlanAction::StaticType(),
432 PostinstallRunnerAction::StaticType(),
433};
434
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700435} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800436
437void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700438 attempter_.set_http_response_code(200);
439 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700440 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700441 EXPECT_CALL(*processor_,
442 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700443 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700444 }
445 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
446
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700447 attempter_.Update("", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800448 g_idle_add(&StaticUpdateTestVerify, this);
449}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700450
Darin Petkove6ef2f82011-03-07 17:31:11 -0800451void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700452 EXPECT_EQ(0, attempter_.http_response_code());
453 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700454 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
455 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
456 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700457 }
458 EXPECT_EQ(attempter_.response_handler_action_.get(),
459 attempter_.actions_[1].get());
460 DownloadAction* download_action =
461 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
462 ASSERT_TRUE(download_action != NULL);
463 EXPECT_EQ(&attempter_, download_action->delegate());
464 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800465 g_main_loop_quit(loop_);
466}
467
Chris Sosa28e479c2013-07-12 11:39:53 -0700468void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700469 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700470 // Create a device policy so that we can change settings.
471 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
472 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
473
474 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700475 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700476
Don Garrett6646b442013-11-13 15:29:11 -0800477 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700478 // References bootable kernels in fake_hardware.h
479 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800480 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700481 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800482 rollback_kernel);
483 }
484
Chris Sosa28e479c2013-07-12 11:39:53 -0700485 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700486
Chris Sosad38b1132014-03-25 10:43:59 -0700487 // We only allow rollback on devices that are not enterprise enrolled and
488 // which have a valid slot to rollback to.
489 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700490 is_rollback_allowed = true;
491 }
492
Don Garrett6646b442013-11-13 15:29:11 -0800493 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700494 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800495 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa28e479c2013-07-12 11:39:53 -0700496 DoAll(SetArgumentPointee<0>(std::string("")),
497 Return(true)));
498 } else {
499 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800500 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa76a29ae2013-07-11 17:59:24 -0700501 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
502 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700503 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700504
Chris Sosa28e479c2013-07-12 11:39:53 -0700505 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700506 InSequence s;
507 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
508 EXPECT_CALL(*processor_,
509 EnqueueAction(Property(&AbstractAction::Type,
510 kRollbackActionTypes[i]))).Times(1);
511 }
512 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
513
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700514 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700515 g_idle_add(&StaticRollbackTestVerify, this);
516 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700517 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700518 g_main_loop_quit(loop_);
519 }
520}
521
522void UpdateAttempterTest::RollbackTestVerify() {
523 // Verifies the actions that were enqueued.
524 EXPECT_EQ(&attempter_, processor_->delegate());
525 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
526 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
527 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
528 }
529 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
530 InstallPlanAction* install_plan_action =
531 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
532 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700533 // Matches fake_hardware.h -> rollback should move from kernel/boot device
534 // pair to other pair.
535 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
536 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700537 EXPECT_EQ(install_plan->powerwash_required, true);
538 g_main_loop_quit(loop_);
539}
540
Darin Petkove6ef2f82011-03-07 17:31:11 -0800541TEST_F(UpdateAttempterTest, UpdateTest) {
542 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
543 g_idle_add(&StaticUpdateTestStart, this);
544 g_main_loop_run(loop_);
545 g_main_loop_unref(loop_);
546 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700547}
548
Chris Sosa76a29ae2013-07-11 17:59:24 -0700549TEST_F(UpdateAttempterTest, RollbackTest) {
550 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
551 g_idle_add(&StaticRollbackTestStart, this);
552 g_main_loop_run(loop_);
553 g_main_loop_unref(loop_);
554 loop_ = NULL;
555}
556
Don Garrett6646b442013-11-13 15:29:11 -0800557TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
558 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
559 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
560 g_main_loop_run(loop_);
561 g_main_loop_unref(loop_);
562 loop_ = NULL;
563}
564
Chris Sosa76a29ae2013-07-11 17:59:24 -0700565TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
566 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
567 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
568 g_main_loop_run(loop_);
569 g_main_loop_unref(loop_);
570 loop_ = NULL;
571}
572
Thieu Le116fda32011-04-19 11:01:54 -0700573void UpdateAttempterTest::PingOmahaTestStart() {
574 EXPECT_CALL(*processor_,
575 EnqueueAction(Property(&AbstractAction::Type,
576 OmahaRequestAction::StaticType())))
577 .Times(1);
578 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
579 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200580 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700581}
582
583TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700584 UpdateCheckScheduler scheduler(&attempter_, &fake_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700585 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700586 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700587 attempter_.set_update_check_scheduler(&scheduler);
588 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
589 g_idle_add(&StaticPingOmahaTestStart, this);
590 g_main_loop_run(loop_);
591 g_main_loop_unref(loop_);
592 loop_ = NULL;
593 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
594 EXPECT_EQ(true, scheduler.scheduled_);
595}
596
Darin Petkov18c7bce2011-06-16 14:07:00 -0700597TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
598 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700599 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700600 attempter_.CreatePendingErrorEvent(&action, kCode);
601 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
602 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
603 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700604 EXPECT_EQ(
605 static_cast<ErrorCode>(static_cast<int>(kCode) |
606 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
607 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700608}
609
610TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
611 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700612 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700613 response_action->install_plan_.is_resume = true;
614 attempter_.response_handler_action_.reset(response_action);
615 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700616 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700617 attempter_.CreatePendingErrorEvent(&action, kCode);
618 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
619 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
620 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700621 EXPECT_EQ(
622 static_cast<ErrorCode>(
623 static_cast<int>(kCode) |
624 static_cast<int>(ErrorCode::kResumedFlag) |
625 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
626 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700627}
628
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700629TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200630 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700631 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200632 g_main_loop_run(loop_);
633 g_main_loop_unref(loop_);
634 loop_ = NULL;
635}
636
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700637void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
638 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200639 // from the device policy.
640
641 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
642 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
643
644 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700645 fake_system_state_.set_device_policy(device_policy);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200646
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700647 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700648 DoAll(SetArgumentPointee<0>(bool(false)), // NOLINT(readability/casting)
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700649 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200650
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700651 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
652 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
653 Return(true)));
654
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700655 ASSERT_FALSE(test_dir_.empty());
656 attempter_.omaha_request_params_->set_root(test_dir_);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700657 attempter_.Update("", "", false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700658 EXPECT_EQ("beta-channel",
659 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200660
661 g_idle_add(&StaticQuitMainLoop, this);
662}
663
Jay Srinivasan0a708742012-03-20 11:26:12 -0700664TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
665 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
666 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
667 g_main_loop_run(loop_);
668 g_main_loop_unref(loop_);
669 loop_ = NULL;
670}
671
672void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
673 // Tests that the update_disbled flag is properly fetched
674 // from the device policy.
675
676 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
677 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
678
679 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700680 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700681
682 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
683 .WillRepeatedly(DoAll(
684 SetArgumentPointee<0>(true),
685 Return(true)));
686
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700687 attempter_.Update("", "", false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700688 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700689
690 g_idle_add(&StaticQuitMainLoop, this);
691}
692
David Zeuthen8f191b22013-08-06 12:27:50 -0700693TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
694 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700695 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700696 mock_p2p_manager.fake().SetP2PEnabled(false);
697 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
698 attempter_.UpdateEngineStarted();
699}
700
701TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
702 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700703 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700704 mock_p2p_manager.fake().SetP2PEnabled(true);
705 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
706 attempter_.UpdateEngineStarted();
707}
708
709TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
710 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700711 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700712 mock_p2p_manager.fake().SetP2PEnabled(true);
713 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
714 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
715 attempter_.UpdateEngineStarted();
716}
717
718TEST_F(UpdateAttempterTest, P2PNotEnabled) {
719 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
720 g_idle_add(&StaticP2PNotEnabled, this);
721 g_main_loop_run(loop_);
722 g_main_loop_unref(loop_);
723 loop_ = NULL;
724}
725gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
726 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
727 ua_test->P2PNotEnabledStart();
728 return FALSE;
729}
730void UpdateAttempterTest::P2PNotEnabledStart() {
731 // If P2P is not enabled, check that we do not attempt housekeeping
732 // and do not convey that p2p is to be used.
733 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700734 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700735 mock_p2p_manager.fake().SetP2PEnabled(false);
736 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700737 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700738 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
739 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
740 g_idle_add(&StaticQuitMainLoop, this);
741}
742
743TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
744 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
745 g_idle_add(&StaticP2PEnabledStartingFails, this);
746 g_main_loop_run(loop_);
747 g_main_loop_unref(loop_);
748 loop_ = NULL;
749}
750gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
751 gpointer data) {
752 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
753 ua_test->P2PEnabledStartingFailsStart();
754 return FALSE;
755}
756void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
757 // If p2p is enabled, but starting it fails ensure we don't do
758 // any housekeeping and do not convey that p2p should be used.
759 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700760 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700761 mock_p2p_manager.fake().SetP2PEnabled(true);
762 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
763 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
764 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700765 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700766 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
767 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
768 g_idle_add(&StaticQuitMainLoop, this);
769}
770
771TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
772 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
773 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
774 g_main_loop_run(loop_);
775 g_main_loop_unref(loop_);
776 loop_ = NULL;
777}
778gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
779 gpointer data) {
780 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
781 ua_test->P2PEnabledHousekeepingFailsStart();
782 return FALSE;
783}
784void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
785 // If p2p is enabled, starting it works but housekeeping fails, ensure
786 // we do not convey p2p is to be used.
787 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700788 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700789 mock_p2p_manager.fake().SetP2PEnabled(true);
790 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
791 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
792 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700793 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700794 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
795 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
796 g_idle_add(&StaticQuitMainLoop, this);
797}
798
799TEST_F(UpdateAttempterTest, P2PEnabled) {
800 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
801 g_idle_add(&StaticP2PEnabled, this);
802 g_main_loop_run(loop_);
803 g_main_loop_unref(loop_);
804 loop_ = NULL;
805}
806gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
807 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
808 ua_test->P2PEnabledStart();
809 return FALSE;
810}
811void UpdateAttempterTest::P2PEnabledStart() {
812 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700813 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700814 // If P2P is enabled and starting it works, check that we performed
815 // housekeeping and that we convey p2p should be used.
816 mock_p2p_manager.fake().SetP2PEnabled(true);
817 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
818 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
819 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700820 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700821 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
822 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
823 g_idle_add(&StaticQuitMainLoop, this);
824}
825
826TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
827 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
828 g_idle_add(&StaticP2PEnabledInteractive, this);
829 g_main_loop_run(loop_);
830 g_main_loop_unref(loop_);
831 loop_ = NULL;
832}
833gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
834 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
835 ua_test->P2PEnabledInteractiveStart();
836 return FALSE;
837}
838void UpdateAttempterTest::P2PEnabledInteractiveStart() {
839 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700840 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700841 // For an interactive check, if P2P is enabled and starting it
842 // works, check that we performed housekeeping and that we convey
843 // p2p should be used for sharing but NOT for downloading.
844 mock_p2p_manager.fake().SetP2PEnabled(true);
845 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
846 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
847 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700848 attempter_.Update("", "", false, true /* interactive */);
David Zeuthen8f191b22013-08-06 12:27:50 -0700849 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
850 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
851 g_idle_add(&StaticQuitMainLoop, this);
852}
853
Jay Srinivasan0a708742012-03-20 11:26:12 -0700854TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
855 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
856 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
857 g_main_loop_run(loop_);
858 g_main_loop_unref(loop_);
859 loop_ = NULL;
860}
861
862void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
863 // Tests that the target_version_prefix value is properly fetched
864 // from the device policy.
865
866 const std::string target_version_prefix = "1412.";
867
868 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
869 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
870
871 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700872 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700873
874 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
875 .WillRepeatedly(DoAll(
876 SetArgumentPointee<0>(target_version_prefix),
877 Return(true)));
878
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700879 attempter_.Update("", "", false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700880 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700881 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700882
883 g_idle_add(&StaticQuitMainLoop, this);
884}
885
886
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700887TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
888 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
889 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
890 g_main_loop_run(loop_);
891 g_main_loop_unref(loop_);
892 loop_ = NULL;
893}
894
895// Tests that the scatter_factor_in_seconds value is properly fetched
896// from the device policy.
897void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700898 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700899
900 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
901 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
902
903 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700904 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700905
906 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
907 .WillRepeatedly(DoAll(
908 SetArgumentPointee<0>(scatter_factor_in_seconds),
909 Return(true)));
910
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700911 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700912 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
913
914 g_idle_add(&StaticQuitMainLoop, this);
915}
916
917TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
918 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
919 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
920 g_main_loop_run(loop_);
921 g_main_loop_unref(loop_);
922 loop_ = NULL;
923}
924
925void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
926 // Tests that the scatter_factor_in_seconds value is properly fetched
927 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700928 int64_t initial_value = 5;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700929 Prefs prefs;
930 attempter_.prefs_ = &prefs;
931
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700932 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700933
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700934 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800935 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700936 &prefs_dir));
937 ScopedDirRemover temp_dir_remover(prefs_dir);
938
Alex Vakulenko75039d72014-03-25 12:36:28 -0700939 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700940 << "Failed to initialize preferences.";
941 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
942
Ben Chan9abb7632014-08-07 00:10:53 -0700943 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700944
945 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
946 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
947
948 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700949 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700950
951 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
952 .WillRepeatedly(DoAll(
953 SetArgumentPointee<0>(scatter_factor_in_seconds),
954 Return(true)));
955
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700956 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700957 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
958
959 // Make sure the file still exists.
960 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
961
Ben Chan9abb7632014-08-07 00:10:53 -0700962 int64_t new_value;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700963 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
964 EXPECT_EQ(initial_value - 1, new_value);
965
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700966 EXPECT_TRUE(
967 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700968
969 // However, if the count is already 0, it's not decremented. Test that.
970 initial_value = 0;
971 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700972 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700973 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
974 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
975 EXPECT_EQ(initial_value, new_value);
976
977 g_idle_add(&StaticQuitMainLoop, this);
978}
979
Jay Srinivasan08fce042012-06-07 16:31:01 -0700980TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
981 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
982 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
983 g_main_loop_run(loop_);
984 g_main_loop_unref(loop_);
985 loop_ = NULL;
986}
987
988void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
989 // Tests that no scattering logic is enabled if the update check
990 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700991 int64_t initial_value = 8;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700992 Prefs prefs;
993 attempter_.prefs_ = &prefs;
994
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700995 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700996
997 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800998 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -0700999 &prefs_dir));
1000 ScopedDirRemover temp_dir_remover(prefs_dir);
1001
Alex Vakulenko75039d72014-03-25 12:36:28 -07001002 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -07001003 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -07001004 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001005 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
1006
1007 // make sure scatter_factor is non-zero as scattering is disabled
1008 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -07001009 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -07001010
1011 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
1012 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
1013
1014 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001015 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001016
1017 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
1018 .WillRepeatedly(DoAll(
1019 SetArgumentPointee<0>(scatter_factor_in_seconds),
1020 Return(true)));
1021
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001022 // Trigger an interactive check so we can test that scattering is disabled.
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -07001023 attempter_.Update("", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001024 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1025
1026 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001027 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001028 EXPECT_FALSE(
1029 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001030 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001031 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1032 EXPECT_FALSE(
1033 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001034 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
1035
1036 g_idle_add(&StaticQuitMainLoop, this);
1037}
1038
David Zeuthen985b1122013-10-09 12:13:15 -07001039// Checks that we only report daily metrics at most every 24 hours.
1040TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1041 FakeClock fake_clock;
1042 Prefs prefs;
1043 string temp_dir;
1044
1045 // We need persistent preferences for this test
Gilad Arnolda6742b32014-01-11 00:18:34 -08001046 EXPECT_TRUE(utils::MakeTempDirectory("UpdateCheckScheduler.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -07001047 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001048 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001049 fake_system_state_.set_clock(&fake_clock);
1050 fake_system_state_.set_prefs(&prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001051
1052 Time epoch = Time::FromInternalValue(0);
1053 fake_clock.SetWallclockTime(epoch);
1054
1055 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1056 // we should report.
1057 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1058 // We should not report again if no time has passed.
1059 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1060
1061 // We should not report if only 10 hours has passed.
1062 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1063 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1064
1065 // We should not report if only 24 hours - 1 sec has passed.
1066 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1067 TimeDelta::FromSeconds(1));
1068 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1069
1070 // We should report if 24 hours has passed.
1071 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1072 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1073
1074 // But then we should not report again..
1075 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1076
1077 // .. until another 24 hours has passed
1078 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1079 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1080 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1081 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1082 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1083
1084 // .. and another 24 hours
1085 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1086 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1087 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1088 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1089 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1090
1091 // If the span between time of reporting and present time is
1092 // negative, we report. This is in order to reset the timestamp and
1093 // avoid an edge condition whereby a distant point in the future is
1094 // in the state variable resulting in us never ever reporting again.
1095 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1096 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1097 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1098
1099 // In this case we should not update until the clock reads 71 + 24 = 95.
1100 // Check that.
1101 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1102 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1103 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1104 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1105 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1106
1107 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1108}
1109
David Zeuthen3c55abd2013-10-14 12:48:03 -07001110TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1111 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001112 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001113 update_completed_marker);
1114
1115 FakeClock fake_clock;
1116 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001117 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001118
1119 Time boot_time;
1120 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1121
1122 attempter.WriteUpdateCompletedMarker();
1123
1124 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1125 EXPECT_EQ(boot_time.ToTimeT(), 42);
1126}
1127
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001128} // namespace chromeos_update_engine