blob: 98fc1a8cf71b430922dc0704bd7fc9504ed643aa [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
5#include <base/file_util.h>
6#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +02007#include <policy/libpolicy.h>
8#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -07009
10#include "update_engine/action_mock.h"
11#include "update_engine/action_processor_mock.h"
12#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070013#include "update_engine/install_plan.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080014#include "update_engine/mock_dbus_interface.h"
Darin Petkov1b003102010-11-30 10:18:36 -080015#include "update_engine/mock_http_fetcher.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016#include "update_engine/mock_payload_state.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070017#include "update_engine/mock_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070018#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070019#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070020#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080021#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070022#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080023#include "update_engine/update_check_scheduler.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024
25using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070026using testing::_;
27using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070029using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080030using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070031using testing::Property;
32using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070033using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070034
35namespace chromeos_update_engine {
36
37// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070038// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070039// methods.
40class UpdateAttempterUnderTest : public UpdateAttempter {
41 public:
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042 explicit UpdateAttempterUnderTest(MockSystemState* mock_system_state,
43 MockDbusGlib* dbus)
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080044 : UpdateAttempter(mock_system_state, dbus) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070045};
46
47class UpdateAttempterTest : public ::testing::Test {
48 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070049 UpdateAttempterTest()
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050 : attempter_(&mock_system_state_, &dbus_),
Jay Srinivasan43488792012-06-19 00:25:31 -070051 mock_connection_manager(&mock_system_state_),
52 loop_(NULL) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080053 mock_system_state_.set_connection_manager(&mock_connection_manager);
Jay Srinivasan43488792012-06-19 00:25:31 -070054 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -070055 virtual void SetUp() {
56 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080057 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070058 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
59 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080060 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
61 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070062 EXPECT_FALSE(attempter_.download_active_);
63 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
64 EXPECT_EQ(0.0, attempter_.download_progress_);
65 EXPECT_EQ(0, attempter_.last_checked_time_);
66 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070067 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080068 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070069 attempter_.processor_.reset(processor_); // Transfers ownership.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080070 prefs_ = mock_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070071 }
72
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020073 void QuitMainLoop();
74 static gboolean StaticQuitMainLoop(gpointer data);
75
Darin Petkove6ef2f82011-03-07 17:31:11 -080076 void UpdateTestStart();
77 void UpdateTestVerify();
Chris Sosa28e479c2013-07-12 11:39:53 -070078 void RollbackTestStart(bool enterprise_rollback, bool stable_channel);
Chris Sosa76a29ae2013-07-11 17:59:24 -070079 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -080080 static gboolean StaticUpdateTestStart(gpointer data);
81 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -070082 static gboolean StaticRollbackTestStart(gpointer data);
83 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
Chris Sosa28e479c2013-07-12 11:39:53 -070084 static gboolean StaticStableChannelRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -070085 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020086
Thieu Le116fda32011-04-19 11:01:54 -070087 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -070088 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020089
Jay Srinivasanae4697c2013-03-18 17:08:08 -070090 void ReadChannelFromPolicyTestStart();
91 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -080092
Jay Srinivasan0a708742012-03-20 11:26:12 -070093 void ReadUpdateDisabledFromPolicyTestStart();
94 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
95
96 void ReadTargetVersionPrefixFromPolicyTestStart();
97 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
98 gpointer data);
99
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700100 void ReadScatterFactorFromPolicyTestStart();
101 static gboolean StaticReadScatterFactorFromPolicyTestStart(
102 gpointer data);
103
104 void DecrementUpdateCheckCountTestStart();
105 static gboolean StaticDecrementUpdateCheckCountTestStart(
106 gpointer data);
107
Jay Srinivasan08fce042012-06-07 16:31:01 -0700108 void NoScatteringDoneDuringManualUpdateTestStart();
109 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
110 gpointer data);
111
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800112 NiceMock<MockSystemState> mock_system_state_;
113 NiceMock<MockDbusGlib> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700114 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800115 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800116 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800117 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800118 GMainLoop* loop_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700119};
120
Darin Petkov1b003102010-11-30 10:18:36 -0800121TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
122 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
123 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800124 DownloadAction action(prefs_, NULL, fetcher.release());
125 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700126 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800127 EXPECT_EQ(503, attempter_.http_response_code());
128 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
129 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
130}
131
132TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
133 ActionMock action;
134 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
135 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800136 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800137 .WillOnce(Return(false));
David Zeuthena99981f2013-04-29 13:42:47 -0700138 attempter_.ActionCompleted(NULL, &action, kErrorCodeError);
Darin Petkov1b003102010-11-30 10:18:36 -0800139 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
140}
141
142TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
143 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
144 fetcher->FailTransfer(500); // Sets the HTTP response code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700145 OmahaRequestAction action(&mock_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800146 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800147 ObjectCollectorAction<OmahaResponse> collector_action;
148 BondActions(&action, &collector_action);
149 OmahaResponse response;
150 response.poll_interval = 234;
151 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800152 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800153 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800154 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700155 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800156 EXPECT_EQ(500, attempter_.http_response_code());
157 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
158 EXPECT_EQ(234, scheduler.poll_interval());
159 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
160}
161
Darin Petkovcd1666f2010-09-23 09:53:44 -0700162TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700163 extern const char* kUpdateCompletedMarker;
164 const FilePath kMarker(kUpdateCompletedMarker);
165 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800166 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700167 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
168 EXPECT_TRUE(file_util::Delete(kMarker, false));
169}
170
171TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700172 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
173 ErrorCode code);
174 EXPECT_EQ(kErrorCodeSuccess,
175 GetErrorCodeForAction(NULL, kErrorCodeSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700176
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800177 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700178 OmahaRequestAction omaha_request_action(&mock_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800179 NULL, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700180 EXPECT_EQ(kErrorCodeOmahaRequestError,
181 GetErrorCodeForAction(&omaha_request_action, kErrorCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800182 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
David Zeuthena99981f2013-04-29 13:42:47 -0700183 EXPECT_EQ(kErrorCodeOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700184 GetErrorCodeForAction(&omaha_response_handler_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700185 kErrorCodeError));
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700186 FilesystemCopierAction filesystem_copier_action(false, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700187 EXPECT_EQ(kErrorCodeFilesystemCopierError,
188 GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800189 PostinstallRunnerAction postinstall_runner_action;
David Zeuthena99981f2013-04-29 13:42:47 -0700190 EXPECT_EQ(kErrorCodePostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700191 GetErrorCodeForAction(&postinstall_runner_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700192 kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700193 ActionMock action_mock;
194 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
David Zeuthena99981f2013-04-29 13:42:47 -0700195 EXPECT_EQ(kErrorCodeError,
196 GetErrorCodeForAction(&action_mock, kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700197}
198
Darin Petkov36275772010-10-01 11:40:57 -0700199TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700200 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800201 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700202 .WillOnce(Return(false));
203 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700204 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800205 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700206 .WillOnce(DoAll(
207 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
208 Return(true)));
209 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700210 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800211 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700212 .WillOnce(DoAll(
213 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
214 Return(true)));
215 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700216 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800217 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700218 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700219 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700220}
221
222TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800223 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700224 .WillOnce(Return(false))
225 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
226 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
227 .WillOnce(DoAll(
228 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
229 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800230 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700231 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800232 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
233 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
234 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700235 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
236 .Times(1);
237 for (int i = 0; i < 4; i ++)
238 attempter_.MarkDeltaUpdateFailure();
239}
240
Darin Petkov1b003102010-11-30 10:18:36 -0800241TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
242 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
243 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800244 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
245 .Times(0);
246 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700247 string url1 = "http://url1";
248 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800249 response.payload_urls.push_back("https://url");
Jay Srinivasan53173b92013-05-17 17:13:01 -0700250 EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl())
251 .WillRepeatedly(Return(url1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800252 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800253 attempter_.ScheduleErrorEventAction();
Jay Srinivasan53173b92013-05-17 17:13:01 -0700254 EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800255}
256
257TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
258 EXPECT_CALL(*processor_,
259 EnqueueAction(Property(&AbstractAction::Type,
260 OmahaRequestAction::StaticType())))
261 .Times(1);
262 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
David Zeuthena99981f2013-04-29 13:42:47 -0700263 ErrorCode err = kErrorCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800264 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800265 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
266 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800267 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800268 attempter_.ScheduleErrorEventAction();
269 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
270}
271
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700272TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
273 extern const char* UpdateStatusToString(UpdateStatus);
274 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
275 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
276 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
277 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
278 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
279 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
280 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
281 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
282 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
283 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
284 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
285 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
286 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
287 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
288 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
289 EXPECT_STREQ("unknown status",
290 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
291}
292
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200293void UpdateAttempterTest::QuitMainLoop() {
294 g_main_loop_quit(loop_);
295}
296
297gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
298 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
299 return FALSE;
300}
301
Darin Petkove6ef2f82011-03-07 17:31:11 -0800302gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
303 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
304 return FALSE;
305}
306
307gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
308 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
309 return FALSE;
310}
311
Chris Sosa76a29ae2013-07-11 17:59:24 -0700312gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700313 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700314 return FALSE;
315}
316
317gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700318 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(true, false);
319 return FALSE;
320}
321
322gboolean UpdateAttempterTest::StaticStableChannelRollbackTestStart(
323 gpointer data) {
324 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
325 false, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700326 return FALSE;
327}
328
329gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
330 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
331 return FALSE;
332}
333
Thieu Le116fda32011-04-19 11:01:54 -0700334gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
335 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
336 return FALSE;
337}
338
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700339gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200340 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700341 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
342 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700343 return FALSE;
344}
345
Jay Srinivasan0a708742012-03-20 11:26:12 -0700346gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
347 gpointer data) {
348 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
349 ua_test->ReadUpdateDisabledFromPolicyTestStart();
350 return FALSE;
351}
352
353gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
354 gpointer data) {
355 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
356 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
357 return FALSE;
358}
359
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700360gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
361 gpointer data) {
362 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
363 ua_test->ReadScatterFactorFromPolicyTestStart();
364 return FALSE;
365}
366
367gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
368 gpointer data) {
369 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
370 ua_test->DecrementUpdateCheckCountTestStart();
371 return FALSE;
372}
373
Jay Srinivasan08fce042012-06-07 16:31:01 -0700374gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
375 gpointer data) {
376 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
377 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
378 return FALSE;
379}
380
Darin Petkove6ef2f82011-03-07 17:31:11 -0800381namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700382// Actions that will be built as part of an update check.
383const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800384 OmahaRequestAction::StaticType(),
385 OmahaResponseHandlerAction::StaticType(),
386 FilesystemCopierAction::StaticType(),
387 FilesystemCopierAction::StaticType(),
388 OmahaRequestAction::StaticType(),
389 DownloadAction::StaticType(),
390 OmahaRequestAction::StaticType(),
391 FilesystemCopierAction::StaticType(),
392 FilesystemCopierAction::StaticType(),
393 PostinstallRunnerAction::StaticType(),
394 OmahaRequestAction::StaticType()
395};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700396
397// Actions that will be built as part of a user-initiated rollback.
398const string kRollbackActionTypes[] = {
399 InstallPlanAction::StaticType(),
400 PostinstallRunnerAction::StaticType(),
401};
402
Darin Petkove6ef2f82011-03-07 17:31:11 -0800403} // namespace {}
404
405void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700406 attempter_.set_http_response_code(200);
407 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700408 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700409 EXPECT_CALL(*processor_,
410 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700411 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700412 }
413 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
414
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800415 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800416 g_idle_add(&StaticUpdateTestVerify, this);
417}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700418
Darin Petkove6ef2f82011-03-07 17:31:11 -0800419void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700420 EXPECT_EQ(0, attempter_.http_response_code());
421 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700422 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
423 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
424 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700425 }
426 EXPECT_EQ(attempter_.response_handler_action_.get(),
427 attempter_.actions_[1].get());
428 DownloadAction* download_action =
429 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
430 ASSERT_TRUE(download_action != NULL);
431 EXPECT_EQ(&attempter_, download_action->delegate());
432 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800433 g_main_loop_quit(loop_);
434}
435
Chris Sosa28e479c2013-07-12 11:39:53 -0700436void UpdateAttempterTest::RollbackTestStart(
437 bool enterprise_rollback, bool stable_channel) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700438 // Create a device policy so that we can change settings.
439 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
440 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
441
442 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
443 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
444 Return(device_policy));
445
446 string install_path = "/dev/sda3";
Chris Sosa28e479c2013-07-12 11:39:53 -0700447 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700448
Chris Sosa28e479c2013-07-12 11:39:53 -0700449 // We only allow rollback on devices that are neither enterprise enrolled or
450 // not on the stable channel.
451 if (!(enterprise_rollback || stable_channel)) {
452 is_rollback_allowed = true;
453 }
454
455 // Set up the policy for the test given our args.
456 if (stable_channel) {
457 attempter_.omaha_request_params_->set_current_channel(
458 string("stable-channel"));
459 } else if (enterprise_rollback) {
460 // We return an empty owner as this is an enterprise.
461 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
462 DoAll(SetArgumentPointee<0>(std::string("")),
463 Return(true)));
464 } else {
465 // We return a fake owner as this is an owned consumer device.
Chris Sosa76a29ae2013-07-11 17:59:24 -0700466 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
467 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
468 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700469 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700470
Chris Sosa28e479c2013-07-12 11:39:53 -0700471 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700472 InSequence s;
473 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
474 EXPECT_CALL(*processor_,
475 EnqueueAction(Property(&AbstractAction::Type,
476 kRollbackActionTypes[i]))).Times(1);
477 }
478 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
479
480 EXPECT_TRUE(attempter_.Rollback(true, &install_path));
481 g_idle_add(&StaticRollbackTestVerify, this);
482 } else {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700483 EXPECT_FALSE(attempter_.Rollback(true, &install_path));
484 g_main_loop_quit(loop_);
485 }
486}
487
488void UpdateAttempterTest::RollbackTestVerify() {
489 // Verifies the actions that were enqueued.
490 EXPECT_EQ(&attempter_, processor_->delegate());
491 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
492 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
493 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
494 }
495 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
496 InstallPlanAction* install_plan_action =
497 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
498 InstallPlan* install_plan = install_plan_action->install_plan();
499 EXPECT_EQ(install_plan->install_path, string("/dev/sda3"));
500 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sda2"));
501 EXPECT_EQ(install_plan->powerwash_required, true);
502 g_main_loop_quit(loop_);
503}
504
Darin Petkove6ef2f82011-03-07 17:31:11 -0800505TEST_F(UpdateAttempterTest, UpdateTest) {
506 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
507 g_idle_add(&StaticUpdateTestStart, this);
508 g_main_loop_run(loop_);
509 g_main_loop_unref(loop_);
510 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700511}
512
Chris Sosa76a29ae2013-07-11 17:59:24 -0700513TEST_F(UpdateAttempterTest, RollbackTest) {
514 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
515 g_idle_add(&StaticRollbackTestStart, this);
516 g_main_loop_run(loop_);
517 g_main_loop_unref(loop_);
518 loop_ = NULL;
519}
520
Chris Sosa28e479c2013-07-12 11:39:53 -0700521TEST_F(UpdateAttempterTest, StableChannelRollbackTest) {
522 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
523 g_idle_add(&StaticStableChannelRollbackTestStart, this);
524 g_main_loop_run(loop_);
525 g_main_loop_unref(loop_);
526 loop_ = NULL;
527}
528
Chris Sosa76a29ae2013-07-11 17:59:24 -0700529TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
530 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
531 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
532 g_main_loop_run(loop_);
533 g_main_loop_unref(loop_);
534 loop_ = NULL;
535}
536
Thieu Le116fda32011-04-19 11:01:54 -0700537void UpdateAttempterTest::PingOmahaTestStart() {
538 EXPECT_CALL(*processor_,
539 EnqueueAction(Property(&AbstractAction::Type,
540 OmahaRequestAction::StaticType())))
541 .Times(1);
542 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
543 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200544 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700545}
546
547TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800548 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700549 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700550 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700551 attempter_.set_update_check_scheduler(&scheduler);
552 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
553 g_idle_add(&StaticPingOmahaTestStart, this);
554 g_main_loop_run(loop_);
555 g_main_loop_unref(loop_);
556 loop_ = NULL;
557 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
558 EXPECT_EQ(true, scheduler.scheduled_);
559}
560
Darin Petkov18c7bce2011-06-16 14:07:00 -0700561TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
562 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700563 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700564 attempter_.CreatePendingErrorEvent(&action, kCode);
565 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
566 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
567 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700568 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800569 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700570}
571
572TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
573 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800574 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700575 response_action->install_plan_.is_resume = true;
576 attempter_.response_handler_action_.reset(response_action);
577 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700578 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700579 attempter_.CreatePendingErrorEvent(&action, kCode);
580 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
581 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
582 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700583 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700584 attempter_.error_event_->error_code);
585}
586
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700587TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200588 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700589 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200590 g_main_loop_run(loop_);
591 g_main_loop_unref(loop_);
592 loop_ = NULL;
593}
594
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700595void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
596 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200597 // from the device policy.
598
599 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
600 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
601
602 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700603 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
604 Return(device_policy));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200605
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700606 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
607 DoAll(SetArgumentPointee<0>(bool(false)),
608 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200609
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700610 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
611 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
612 Return(true)));
613
614 attempter_.omaha_request_params_->set_root("./UpdateAttempterTest");
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800615 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700616 EXPECT_EQ("beta-channel",
617 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200618
619 g_idle_add(&StaticQuitMainLoop, this);
620}
621
Jay Srinivasan0a708742012-03-20 11:26:12 -0700622TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
623 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
624 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
625 g_main_loop_run(loop_);
626 g_main_loop_unref(loop_);
627 loop_ = NULL;
628}
629
630void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
631 // Tests that the update_disbled flag is properly fetched
632 // from the device policy.
633
634 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
635 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
636
637 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700638 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
639 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700640
641 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
642 .WillRepeatedly(DoAll(
643 SetArgumentPointee<0>(true),
644 Return(true)));
645
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800646 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700647 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700648
649 g_idle_add(&StaticQuitMainLoop, this);
650}
651
652TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
653 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
654 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
655 g_main_loop_run(loop_);
656 g_main_loop_unref(loop_);
657 loop_ = NULL;
658}
659
660void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
661 // Tests that the target_version_prefix value is properly fetched
662 // from the device policy.
663
664 const std::string target_version_prefix = "1412.";
665
666 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
667 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
668
669 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700670 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
671 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700672
673 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
674 .WillRepeatedly(DoAll(
675 SetArgumentPointee<0>(target_version_prefix),
676 Return(true)));
677
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800678 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700679 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700680 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700681
682 g_idle_add(&StaticQuitMainLoop, this);
683}
684
685
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700686TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
687 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
688 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
689 g_main_loop_run(loop_);
690 g_main_loop_unref(loop_);
691 loop_ = NULL;
692}
693
694// Tests that the scatter_factor_in_seconds value is properly fetched
695// from the device policy.
696void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
697 int64 scatter_factor_in_seconds = 36000;
698
699 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
700 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
701
702 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800703 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700704 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700705
706 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
707 .WillRepeatedly(DoAll(
708 SetArgumentPointee<0>(scatter_factor_in_seconds),
709 Return(true)));
710
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800711 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700712 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
713
714 g_idle_add(&StaticQuitMainLoop, this);
715}
716
717TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
718 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
719 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
720 g_main_loop_run(loop_);
721 g_main_loop_unref(loop_);
722 loop_ = NULL;
723}
724
725void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
726 // Tests that the scatter_factor_in_seconds value is properly fetched
727 // from the device policy and is decremented if value > 0.
728 int64 initial_value = 5;
729 Prefs prefs;
730 attempter_.prefs_ = &prefs;
731
Jay Srinivasan08fce042012-06-07 16:31:01 -0700732 EXPECT_CALL(mock_system_state_,
733 IsOOBEComplete()).WillRepeatedly(Return(true));
734
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700735 string prefs_dir;
736 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
737 &prefs_dir));
738 ScopedDirRemover temp_dir_remover(prefs_dir);
739
740 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
741 << "Failed to initialize preferences.";
742 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
743
744 int64 scatter_factor_in_seconds = 10;
745
746 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
747 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
748
749 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800750 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700751 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700752
753 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
754 .WillRepeatedly(DoAll(
755 SetArgumentPointee<0>(scatter_factor_in_seconds),
756 Return(true)));
757
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800758 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700759 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
760
761 // Make sure the file still exists.
762 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
763
764 int64 new_value;
765 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
766 EXPECT_EQ(initial_value - 1, new_value);
767
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700768 EXPECT_TRUE(
769 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700770
771 // However, if the count is already 0, it's not decremented. Test that.
772 initial_value = 0;
773 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800774 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700775 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
776 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
777 EXPECT_EQ(initial_value, new_value);
778
779 g_idle_add(&StaticQuitMainLoop, this);
780}
781
Jay Srinivasan08fce042012-06-07 16:31:01 -0700782TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
783 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
784 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
785 g_main_loop_run(loop_);
786 g_main_loop_unref(loop_);
787 loop_ = NULL;
788}
789
790void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
791 // Tests that no scattering logic is enabled if the update check
792 // is manually done (as opposed to a scheduled update check)
793 int64 initial_value = 8;
794 Prefs prefs;
795 attempter_.prefs_ = &prefs;
796
797 EXPECT_CALL(mock_system_state_,
798 IsOOBEComplete()).WillRepeatedly(Return(true));
799
800 string prefs_dir;
801 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
802 &prefs_dir));
803 ScopedDirRemover temp_dir_remover(prefs_dir);
804
805 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
806 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700807 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700808 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
809
810 // make sure scatter_factor is non-zero as scattering is disabled
811 // otherwise.
812 int64 scatter_factor_in_seconds = 50;
813
814 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
815 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
816
817 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800818 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700819 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700820
821 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
822 .WillRepeatedly(DoAll(
823 SetArgumentPointee<0>(scatter_factor_in_seconds),
824 Return(true)));
825
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800826 // Trigger an interactive check so we can test that scattering is disabled.
827 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700828 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
829
830 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700831 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700832 EXPECT_FALSE(
833 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700834 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700835 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
836 EXPECT_FALSE(
837 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700838 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
839
840 g_idle_add(&StaticQuitMainLoop, this);
841}
842
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700843} // namespace chromeos_update_engine