blob: 39ad1099d353d3f7547fed043367ef2717719b65 [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"
Andrew de los Reyes45168102010-11-22 11:13:50 -080013#include "update_engine/mock_dbus_interface.h"
Darin Petkov1b003102010-11-30 10:18:36 -080014#include "update_engine/mock_http_fetcher.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080015#include "update_engine/mock_payload_state.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070016#include "update_engine/mock_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070017#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070018#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070019#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080020#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070021#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080022#include "update_engine/update_check_scheduler.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070023
24using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070025using testing::_;
26using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070027using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070028using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080029using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030using testing::Property;
31using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070032using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033
34namespace chromeos_update_engine {
35
36// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070037// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070038// methods.
39class UpdateAttempterUnderTest : public UpdateAttempter {
40 public:
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080041 explicit UpdateAttempterUnderTest(MockSystemState* mock_system_state,
42 MockDbusGlib* dbus)
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080043 : UpdateAttempter(mock_system_state, dbus) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070044};
45
46class UpdateAttempterTest : public ::testing::Test {
47 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070048 UpdateAttempterTest()
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049 : attempter_(&mock_system_state_, &dbus_),
Jay Srinivasan43488792012-06-19 00:25:31 -070050 mock_connection_manager(&mock_system_state_),
51 loop_(NULL) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052 mock_system_state_.set_connection_manager(&mock_connection_manager);
Jay Srinivasan43488792012-06-19 00:25:31 -070053 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -070054 virtual void SetUp() {
55 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080056 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070057 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
58 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080059 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
60 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061 EXPECT_FALSE(attempter_.download_active_);
62 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
63 EXPECT_EQ(0.0, attempter_.download_progress_);
64 EXPECT_EQ(0, attempter_.last_checked_time_);
65 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070066 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080067 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070068 attempter_.processor_.reset(processor_); // Transfers ownership.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080069 prefs_ = mock_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070070 }
71
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020072 void QuitMainLoop();
73 static gboolean StaticQuitMainLoop(gpointer data);
74
Darin Petkove6ef2f82011-03-07 17:31:11 -080075 void UpdateTestStart();
76 void UpdateTestVerify();
77 static gboolean StaticUpdateTestStart(gpointer data);
78 static gboolean StaticUpdateTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020079
Thieu Le116fda32011-04-19 11:01:54 -070080 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -070081 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020082
83 void ReadTrackFromPolicyTestStart();
84 static gboolean StaticReadTrackFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -080085
Jay Srinivasan0a708742012-03-20 11:26:12 -070086 void ReadUpdateDisabledFromPolicyTestStart();
87 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
88
89 void ReadTargetVersionPrefixFromPolicyTestStart();
90 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
91 gpointer data);
92
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070093 void ReadScatterFactorFromPolicyTestStart();
94 static gboolean StaticReadScatterFactorFromPolicyTestStart(
95 gpointer data);
96
97 void DecrementUpdateCheckCountTestStart();
98 static gboolean StaticDecrementUpdateCheckCountTestStart(
99 gpointer data);
100
Jay Srinivasan08fce042012-06-07 16:31:01 -0700101 void NoScatteringDoneDuringManualUpdateTestStart();
102 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
103 gpointer data);
104
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800105 NiceMock<MockSystemState> mock_system_state_;
106 NiceMock<MockDbusGlib> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700107 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800108 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800109 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800110 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800111 GMainLoop* loop_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700112};
113
Darin Petkov1b003102010-11-30 10:18:36 -0800114TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
115 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
116 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800117 DownloadAction action(prefs_, NULL, fetcher.release());
118 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Darin Petkov1b003102010-11-30 10:18:36 -0800119 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
120 EXPECT_EQ(503, attempter_.http_response_code());
121 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
122 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
123}
124
125TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
126 ActionMock action;
127 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
128 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800129 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800130 .WillOnce(Return(false));
131 attempter_.ActionCompleted(NULL, &action, kActionCodeError);
132 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
133}
134
135TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
136 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
137 fetcher->FailTransfer(500); // Sets the HTTP response code.
138 OmahaRequestParams params;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800139 OmahaRequestAction action(&mock_system_state_, &params, NULL,
140 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800141 ObjectCollectorAction<OmahaResponse> collector_action;
142 BondActions(&action, &collector_action);
143 OmahaResponse response;
144 response.poll_interval = 234;
145 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800146 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800147 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800148 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Darin Petkov1b003102010-11-30 10:18:36 -0800149 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
150 EXPECT_EQ(500, attempter_.http_response_code());
151 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
152 EXPECT_EQ(234, scheduler.poll_interval());
153 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
154}
155
Darin Petkovcd1666f2010-09-23 09:53:44 -0700156TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700157 extern const char* kUpdateCompletedMarker;
158 const FilePath kMarker(kUpdateCompletedMarker);
159 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800160 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700161 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
162 EXPECT_TRUE(file_util::Delete(kMarker, false));
163}
164
165TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
166 extern ActionExitCode GetErrorCodeForAction(AbstractAction* action,
167 ActionExitCode code);
168 EXPECT_EQ(kActionCodeSuccess,
169 GetErrorCodeForAction(NULL, kActionCodeSuccess));
170
171 OmahaRequestParams params;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800172 MockSystemState mock_system_state;
173 OmahaRequestAction omaha_request_action(&mock_system_state, &params, NULL,
174 NULL, false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700175 EXPECT_EQ(kActionCodeOmahaRequestError,
176 GetErrorCodeForAction(&omaha_request_action, kActionCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800177 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700178 EXPECT_EQ(kActionCodeOmahaResponseHandlerError,
179 GetErrorCodeForAction(&omaha_response_handler_action,
180 kActionCodeError));
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700181 FilesystemCopierAction filesystem_copier_action(false, false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700182 EXPECT_EQ(kActionCodeFilesystemCopierError,
183 GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800184 PostinstallRunnerAction postinstall_runner_action;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700185 EXPECT_EQ(kActionCodePostinstallRunnerError,
186 GetErrorCodeForAction(&postinstall_runner_action,
187 kActionCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700188 ActionMock action_mock;
189 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
190 EXPECT_EQ(kActionCodeError,
191 GetErrorCodeForAction(&action_mock, kActionCodeError));
192}
193
Darin Petkov36275772010-10-01 11:40:57 -0700194TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
195 attempter_.omaha_request_params_.delta_okay = true;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800196 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700197 .WillOnce(Return(false));
198 attempter_.DisableDeltaUpdateIfNeeded();
199 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800200 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700201 .WillOnce(DoAll(
202 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
203 Return(true)));
204 attempter_.DisableDeltaUpdateIfNeeded();
205 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800206 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700207 .WillOnce(DoAll(
208 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
209 Return(true)));
210 attempter_.DisableDeltaUpdateIfNeeded();
211 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800212 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700213 attempter_.DisableDeltaUpdateIfNeeded();
214 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
215}
216
217TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800218 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700219 .WillOnce(Return(false))
220 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
221 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
222 .WillOnce(DoAll(
223 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
224 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800225 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700226 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800227 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
228 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
229 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700230 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
231 .Times(1);
232 for (int i = 0; i < 4; i ++)
233 attempter_.MarkDeltaUpdateFailure();
234}
235
Darin Petkov1b003102010-11-30 10:18:36 -0800236TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
237 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
238 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800239 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
240 .Times(0);
241 OmahaResponse response;
242 response.payload_urls.push_back("http://url");
243 response.payload_urls.push_back("https://url");
244 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800245 attempter_.ScheduleErrorEventAction();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800246 EXPECT_EQ(0, mock_system_state_.mock_payload_state()->GetUrlIndex());
Darin Petkov1b003102010-11-30 10:18:36 -0800247}
248
249TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
250 EXPECT_CALL(*processor_,
251 EnqueueAction(Property(&AbstractAction::Type,
252 OmahaRequestAction::StaticType())))
253 .Times(1);
254 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800255 ActionExitCode err = kActionCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800256 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800257 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
258 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800259 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800260 attempter_.ScheduleErrorEventAction();
261 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
262}
263
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700264TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
265 extern const char* UpdateStatusToString(UpdateStatus);
266 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
267 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
268 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
269 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
270 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
271 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
272 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
273 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
274 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
275 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
276 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
277 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
278 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
279 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
280 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
281 EXPECT_STREQ("unknown status",
282 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
283}
284
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200285void UpdateAttempterTest::QuitMainLoop() {
286 g_main_loop_quit(loop_);
287}
288
289gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
290 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
291 return FALSE;
292}
293
Darin Petkove6ef2f82011-03-07 17:31:11 -0800294gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
295 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
296 return FALSE;
297}
298
299gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
300 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
301 return FALSE;
302}
303
Thieu Le116fda32011-04-19 11:01:54 -0700304gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
305 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
306 return FALSE;
307}
308
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200309gboolean UpdateAttempterTest::StaticReadTrackFromPolicyTestStart(
310 gpointer data) {
311 reinterpret_cast<UpdateAttempterTest*>(data)->ReadTrackFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700312 return FALSE;
313}
314
Jay Srinivasan0a708742012-03-20 11:26:12 -0700315gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
316 gpointer data) {
317 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
318 ua_test->ReadUpdateDisabledFromPolicyTestStart();
319 return FALSE;
320}
321
322gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
323 gpointer data) {
324 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
325 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
326 return FALSE;
327}
328
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700329gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
330 gpointer data) {
331 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
332 ua_test->ReadScatterFactorFromPolicyTestStart();
333 return FALSE;
334}
335
336gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
337 gpointer data) {
338 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
339 ua_test->DecrementUpdateCheckCountTestStart();
340 return FALSE;
341}
342
Jay Srinivasan08fce042012-06-07 16:31:01 -0700343gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
344 gpointer data) {
345 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
346 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
347 return FALSE;
348}
349
Darin Petkove6ef2f82011-03-07 17:31:11 -0800350namespace {
351const string kActionTypes[] = {
352 OmahaRequestAction::StaticType(),
353 OmahaResponseHandlerAction::StaticType(),
354 FilesystemCopierAction::StaticType(),
355 FilesystemCopierAction::StaticType(),
356 OmahaRequestAction::StaticType(),
357 DownloadAction::StaticType(),
358 OmahaRequestAction::StaticType(),
359 FilesystemCopierAction::StaticType(),
360 FilesystemCopierAction::StaticType(),
361 PostinstallRunnerAction::StaticType(),
362 OmahaRequestAction::StaticType()
363};
364} // namespace {}
365
366void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700367 attempter_.set_http_response_code(200);
368 InSequence s;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700369 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
370 EXPECT_CALL(*processor_,
371 EnqueueAction(Property(&AbstractAction::Type,
372 kActionTypes[i]))).Times(1);
373 }
374 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
375
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800376 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800377 g_idle_add(&StaticUpdateTestVerify, this);
378}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700379
Darin Petkove6ef2f82011-03-07 17:31:11 -0800380void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700381 EXPECT_EQ(0, attempter_.http_response_code());
382 EXPECT_EQ(&attempter_, processor_->delegate());
383 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
384 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
385 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
386 }
387 EXPECT_EQ(attempter_.response_handler_action_.get(),
388 attempter_.actions_[1].get());
389 DownloadAction* download_action =
390 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
391 ASSERT_TRUE(download_action != NULL);
392 EXPECT_EQ(&attempter_, download_action->delegate());
393 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800394 g_main_loop_quit(loop_);
395}
396
397TEST_F(UpdateAttempterTest, UpdateTest) {
398 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
399 g_idle_add(&StaticUpdateTestStart, this);
400 g_main_loop_run(loop_);
401 g_main_loop_unref(loop_);
402 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700403}
404
Thieu Le116fda32011-04-19 11:01:54 -0700405void UpdateAttempterTest::PingOmahaTestStart() {
406 EXPECT_CALL(*processor_,
407 EnqueueAction(Property(&AbstractAction::Type,
408 OmahaRequestAction::StaticType())))
409 .Times(1);
410 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
411 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200412 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700413}
414
415TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800416 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700417 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700418 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700419 attempter_.set_update_check_scheduler(&scheduler);
420 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
421 g_idle_add(&StaticPingOmahaTestStart, this);
422 g_main_loop_run(loop_);
423 g_main_loop_unref(loop_);
424 loop_ = NULL;
425 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
426 EXPECT_EQ(true, scheduler.scheduled_);
427}
428
Darin Petkov18c7bce2011-06-16 14:07:00 -0700429TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
430 ActionMock action;
431 const ActionExitCode kCode = kActionCodeDownloadTransferError;
432 attempter_.CreatePendingErrorEvent(&action, kCode);
433 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
434 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
435 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800436 EXPECT_EQ(kCode | kActionCodeTestOmahaUrlFlag,
437 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700438}
439
440TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
441 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800442 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700443 response_action->install_plan_.is_resume = true;
444 attempter_.response_handler_action_.reset(response_action);
445 ActionMock action;
446 const ActionExitCode kCode = kActionCodeInstallDeviceOpenError;
447 attempter_.CreatePendingErrorEvent(&action, kCode);
448 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
449 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
450 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800451 EXPECT_EQ(kCode | kActionCodeResumedFlag | kActionCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700452 attempter_.error_event_->error_code);
453}
454
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200455TEST_F(UpdateAttempterTest, ReadTrackFromPolicy) {
456 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
457 g_idle_add(&StaticReadTrackFromPolicyTestStart, this);
458 g_main_loop_run(loop_);
459 g_main_loop_unref(loop_);
460 loop_ = NULL;
461}
462
463void UpdateAttempterTest::ReadTrackFromPolicyTestStart() {
464 // Tests that the update track (aka release channel) is properly fetched
465 // from the device policy.
466
467 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
468 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
469
470 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
471
472 EXPECT_CALL(*device_policy, GetReleaseChannel(_))
473 .WillRepeatedly(DoAll(
474 SetArgumentPointee<0>(std::string("canary-channel")),
475 Return(true)));
476
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800477 attempter_.Update("", "", false, false, false);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200478 EXPECT_EQ("canary-channel", attempter_.omaha_request_params_.app_track);
479
480 g_idle_add(&StaticQuitMainLoop, this);
481}
482
Jay Srinivasan0a708742012-03-20 11:26:12 -0700483TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
484 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
485 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
486 g_main_loop_run(loop_);
487 g_main_loop_unref(loop_);
488 loop_ = NULL;
489}
490
491void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
492 // Tests that the update_disbled flag is properly fetched
493 // from the device policy.
494
495 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
496 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
497
498 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
499
500 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
501 .WillRepeatedly(DoAll(
502 SetArgumentPointee<0>(true),
503 Return(true)));
504
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800505 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700506 EXPECT_TRUE(attempter_.omaha_request_params_.update_disabled);
507
508 g_idle_add(&StaticQuitMainLoop, this);
509}
510
511TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
512 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
513 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
514 g_main_loop_run(loop_);
515 g_main_loop_unref(loop_);
516 loop_ = NULL;
517}
518
519void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
520 // Tests that the target_version_prefix value is properly fetched
521 // from the device policy.
522
523 const std::string target_version_prefix = "1412.";
524
525 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
526 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
527
528 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
529
530 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
531 .WillRepeatedly(DoAll(
532 SetArgumentPointee<0>(target_version_prefix),
533 Return(true)));
534
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800535 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700536 EXPECT_EQ(target_version_prefix.c_str(),
537 attempter_.omaha_request_params_.target_version_prefix);
538
539 g_idle_add(&StaticQuitMainLoop, this);
540}
541
542
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700543TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
544 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
545 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
546 g_main_loop_run(loop_);
547 g_main_loop_unref(loop_);
548 loop_ = NULL;
549}
550
551// Tests that the scatter_factor_in_seconds value is properly fetched
552// from the device policy.
553void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
554 int64 scatter_factor_in_seconds = 36000;
555
556 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
557 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
558
559 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800560 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700561 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700562
563 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
564 .WillRepeatedly(DoAll(
565 SetArgumentPointee<0>(scatter_factor_in_seconds),
566 Return(true)));
567
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800568 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700569 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
570
571 g_idle_add(&StaticQuitMainLoop, this);
572}
573
574TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
575 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
576 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
577 g_main_loop_run(loop_);
578 g_main_loop_unref(loop_);
579 loop_ = NULL;
580}
581
582void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
583 // Tests that the scatter_factor_in_seconds value is properly fetched
584 // from the device policy and is decremented if value > 0.
585 int64 initial_value = 5;
586 Prefs prefs;
587 attempter_.prefs_ = &prefs;
588
Jay Srinivasan08fce042012-06-07 16:31:01 -0700589 EXPECT_CALL(mock_system_state_,
590 IsOOBEComplete()).WillRepeatedly(Return(true));
591
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700592 string prefs_dir;
593 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
594 &prefs_dir));
595 ScopedDirRemover temp_dir_remover(prefs_dir);
596
597 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
598 << "Failed to initialize preferences.";
599 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
600
601 int64 scatter_factor_in_seconds = 10;
602
603 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
604 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
605
606 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800607 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700608 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700609
610 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
611 .WillRepeatedly(DoAll(
612 SetArgumentPointee<0>(scatter_factor_in_seconds),
613 Return(true)));
614
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800615 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700616 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
617
618 // Make sure the file still exists.
619 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
620
621 int64 new_value;
622 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
623 EXPECT_EQ(initial_value - 1, new_value);
624
625 EXPECT_TRUE(attempter_.omaha_request_params_.update_check_count_wait_enabled);
626
627 // However, if the count is already 0, it's not decremented. Test that.
628 initial_value = 0;
629 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800630 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700631 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
632 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
633 EXPECT_EQ(initial_value, new_value);
634
635 g_idle_add(&StaticQuitMainLoop, this);
636}
637
Jay Srinivasan08fce042012-06-07 16:31:01 -0700638TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
639 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
640 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
641 g_main_loop_run(loop_);
642 g_main_loop_unref(loop_);
643 loop_ = NULL;
644}
645
646void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
647 // Tests that no scattering logic is enabled if the update check
648 // is manually done (as opposed to a scheduled update check)
649 int64 initial_value = 8;
650 Prefs prefs;
651 attempter_.prefs_ = &prefs;
652
653 EXPECT_CALL(mock_system_state_,
654 IsOOBEComplete()).WillRepeatedly(Return(true));
655
656 string prefs_dir;
657 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
658 &prefs_dir));
659 ScopedDirRemover temp_dir_remover(prefs_dir);
660
661 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
662 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700663 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700664 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
665
666 // make sure scatter_factor is non-zero as scattering is disabled
667 // otherwise.
668 int64 scatter_factor_in_seconds = 50;
669
670 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
671 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
672
673 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800674 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700675 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700676
677 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
678 .WillRepeatedly(DoAll(
679 SetArgumentPointee<0>(scatter_factor_in_seconds),
680 Return(true)));
681
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800682 // Trigger an interactive check so we can test that scattering is disabled.
683 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700684 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
685
686 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700687 // checks and all artifacts are removed.
Jay Srinivasan08fce042012-06-07 16:31:01 -0700688 EXPECT_FALSE(attempter_.omaha_request_params_.wall_clock_based_wait_enabled);
689 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasan21be0752012-07-25 15:44:56 -0700690 EXPECT_TRUE(attempter_.omaha_request_params_.waiting_period.InSeconds() == 0);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700691 EXPECT_FALSE(attempter_.omaha_request_params_.
692 update_check_count_wait_enabled);
693 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
694
695 g_idle_add(&StaticQuitMainLoop, this);
696}
697
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700698} // namespace chromeos_update_engine