blob: b61377b7939b2f261af9a34fd52a50fe03885aba [file] [log] [blame]
Darin Petkov18c7bce2011-06-16 14:07:00 -07001// Copyright (c) 2011 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>
7
8#include "update_engine/action_mock.h"
9#include "update_engine/action_processor_mock.h"
10#include "update_engine/filesystem_copier_action.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080011#include "update_engine/mock_dbus_interface.h"
Darin Petkov1b003102010-11-30 10:18:36 -080012#include "update_engine/mock_http_fetcher.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070013#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070014#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080015#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080017#include "update_engine/update_check_scheduler.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070018
19using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070020using testing::_;
21using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070022using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070023using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080024using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070025using testing::Property;
26using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070027using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028
29namespace chromeos_update_engine {
30
31// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070032// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033// methods.
34class UpdateAttempterUnderTest : public UpdateAttempter {
35 public:
Andrew de los Reyes000d8952011-03-02 15:21:14 -080036 explicit UpdateAttempterUnderTest(MockDbusGlib* dbus)
37 : UpdateAttempter(NULL, NULL, dbus) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070038};
39
40class UpdateAttempterTest : public ::testing::Test {
41 protected:
Darin Petkove6ef2f82011-03-07 17:31:11 -080042 UpdateAttempterTest() : attempter_(&dbus_), loop_(NULL) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070043 virtual void SetUp() {
44 EXPECT_EQ(NULL, attempter_.dbus_service_);
45 EXPECT_EQ(NULL, attempter_.prefs_);
46 EXPECT_EQ(NULL, attempter_.metrics_lib_);
47 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
48 EXPECT_EQ(0, attempter_.http_response_code_);
49 EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_);
50 EXPECT_EQ(NULL, attempter_.manage_priority_source_);
51 EXPECT_FALSE(attempter_.download_active_);
52 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
53 EXPECT_EQ(0.0, attempter_.download_progress_);
54 EXPECT_EQ(0, attempter_.last_checked_time_);
55 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
56 EXPECT_EQ(0, attempter_.new_size_);
Darin Petkov36275772010-10-01 11:40:57 -070057 EXPECT_FALSE(attempter_.is_full_update_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070058 processor_ = new ActionProcessorMock();
59 attempter_.processor_.reset(processor_); // Transfers ownership.
Darin Petkov36275772010-10-01 11:40:57 -070060 attempter_.prefs_ = &prefs_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061 }
62
Darin Petkove6ef2f82011-03-07 17:31:11 -080063 void UpdateTestStart();
64 void UpdateTestVerify();
65 static gboolean StaticUpdateTestStart(gpointer data);
66 static gboolean StaticUpdateTestVerify(gpointer data);
Thieu Le116fda32011-04-19 11:01:54 -070067 void PingOmahaTestStart();
68 void PingOmahaTestDone();
69 static gboolean StaticPingOmahaTestStart(gpointer data);
70 static gboolean StaticPingOmahaTestDone(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -080071
Andrew de los Reyes000d8952011-03-02 15:21:14 -080072 MockDbusGlib dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070073 UpdateAttempterUnderTest attempter_;
74 ActionProcessorMock* processor_;
Darin Petkov9c096d62010-11-17 14:49:04 -080075 NiceMock<PrefsMock> prefs_;
Darin Petkove6ef2f82011-03-07 17:31:11 -080076 GMainLoop* loop_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070077};
78
Darin Petkov1b003102010-11-30 10:18:36 -080079TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
80 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
81 fetcher->FailTransfer(503); // Sets the HTTP response code.
82 DownloadAction action(&prefs_, fetcher.release());
83 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
84 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
85 EXPECT_EQ(503, attempter_.http_response_code());
86 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
87 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
88}
89
90TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
91 ActionMock action;
92 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
93 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
94 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
95 .WillOnce(Return(false));
96 attempter_.ActionCompleted(NULL, &action, kActionCodeError);
97 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
98}
99
100TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
101 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
102 fetcher->FailTransfer(500); // Sets the HTTP response code.
103 OmahaRequestParams params;
Thieu Le116fda32011-04-19 11:01:54 -0700104 OmahaRequestAction action(&prefs_, params, NULL, fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800105 ObjectCollectorAction<OmahaResponse> collector_action;
106 BondActions(&action, &collector_action);
107 OmahaResponse response;
108 response.poll_interval = 234;
109 action.SetOutputObject(response);
110 UpdateCheckScheduler scheduler(&attempter_);
111 attempter_.set_update_check_scheduler(&scheduler);
112 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
113 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
114 EXPECT_EQ(500, attempter_.http_response_code());
115 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
116 EXPECT_EQ(234, scheduler.poll_interval());
117 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
118}
119
Darin Petkovcd1666f2010-09-23 09:53:44 -0700120TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700121 extern const char* kUpdateCompletedMarker;
122 const FilePath kMarker(kUpdateCompletedMarker);
123 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800124 MockDbusGlib dbus;
125 UpdateAttempterUnderTest attempter(&dbus);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700126 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
127 EXPECT_TRUE(file_util::Delete(kMarker, false));
128}
129
130TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
131 extern ActionExitCode GetErrorCodeForAction(AbstractAction* action,
132 ActionExitCode code);
133 EXPECT_EQ(kActionCodeSuccess,
134 GetErrorCodeForAction(NULL, kActionCodeSuccess));
135
136 OmahaRequestParams params;
Thieu Le116fda32011-04-19 11:01:54 -0700137 OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL, false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700138 EXPECT_EQ(kActionCodeOmahaRequestError,
139 GetErrorCodeForAction(&omaha_request_action, kActionCodeError));
Darin Petkov73058b42010-10-06 16:32:19 -0700140 OmahaResponseHandlerAction omaha_response_handler_action(&prefs_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700141 EXPECT_EQ(kActionCodeOmahaResponseHandlerError,
142 GetErrorCodeForAction(&omaha_response_handler_action,
143 kActionCodeError));
Darin Petkov3aefa862010-12-07 14:45:00 -0800144 FilesystemCopierAction filesystem_copier_action(false, false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700145 EXPECT_EQ(kActionCodeFilesystemCopierError,
146 GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800147 PostinstallRunnerAction postinstall_runner_action;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700148 EXPECT_EQ(kActionCodePostinstallRunnerError,
149 GetErrorCodeForAction(&postinstall_runner_action,
150 kActionCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700151 ActionMock action_mock;
152 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
153 EXPECT_EQ(kActionCodeError,
154 GetErrorCodeForAction(&action_mock, kActionCodeError));
155}
156
Darin Petkov36275772010-10-01 11:40:57 -0700157TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
158 attempter_.omaha_request_params_.delta_okay = true;
159 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
160 .WillOnce(Return(false));
161 attempter_.DisableDeltaUpdateIfNeeded();
162 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
163 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
164 .WillOnce(DoAll(
165 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
166 Return(true)));
167 attempter_.DisableDeltaUpdateIfNeeded();
168 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
169 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
170 .WillOnce(DoAll(
171 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
172 Return(true)));
173 attempter_.DisableDeltaUpdateIfNeeded();
174 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
175 EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0);
176 attempter_.DisableDeltaUpdateIfNeeded();
177 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
178}
179
180TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
181 attempter_.is_full_update_ = false;
182 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
183 .WillOnce(Return(false))
184 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
185 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
186 .WillOnce(DoAll(
187 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
188 Return(true)));
Darin Petkov2dd01092010-10-08 15:43:05 -0700189 EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
190 .WillRepeatedly(Return(true));
Darin Petkov36275772010-10-01 11:40:57 -0700191 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
192 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
193 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures,
194 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
195 .Times(1);
196 for (int i = 0; i < 4; i ++)
197 attempter_.MarkDeltaUpdateFailure();
198}
199
Darin Petkov1b003102010-11-30 10:18:36 -0800200TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
201 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
202 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
203 attempter_.ScheduleErrorEventAction();
204}
205
206TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
207 EXPECT_CALL(*processor_,
208 EnqueueAction(Property(&AbstractAction::Type,
209 OmahaRequestAction::StaticType())))
210 .Times(1);
211 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
212 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
213 OmahaEvent::kResultError,
214 kActionCodeError));
215 attempter_.ScheduleErrorEventAction();
216 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
217}
218
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700219TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
220 extern const char* UpdateStatusToString(UpdateStatus);
221 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
222 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
223 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
224 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
225 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
226 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
227 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
228 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
229 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
230 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
231 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
232 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
233 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
234 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
235 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
236 EXPECT_STREQ("unknown status",
237 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
238}
239
Darin Petkove6ef2f82011-03-07 17:31:11 -0800240gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
241 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
242 return FALSE;
243}
244
245gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
246 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
247 return FALSE;
248}
249
Thieu Le116fda32011-04-19 11:01:54 -0700250gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
251 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
252 return FALSE;
253}
254
255gboolean UpdateAttempterTest::StaticPingOmahaTestDone(gpointer data) {
256 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestDone();
257 return FALSE;
258}
259
Darin Petkove6ef2f82011-03-07 17:31:11 -0800260namespace {
261const string kActionTypes[] = {
262 OmahaRequestAction::StaticType(),
263 OmahaResponseHandlerAction::StaticType(),
264 FilesystemCopierAction::StaticType(),
265 FilesystemCopierAction::StaticType(),
266 OmahaRequestAction::StaticType(),
267 DownloadAction::StaticType(),
268 OmahaRequestAction::StaticType(),
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700269 OmahaRequestAction::StaticType(),
Darin Petkove6ef2f82011-03-07 17:31:11 -0800270 FilesystemCopierAction::StaticType(),
271 FilesystemCopierAction::StaticType(),
272 PostinstallRunnerAction::StaticType(),
273 OmahaRequestAction::StaticType()
274};
275} // namespace {}
276
277void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700278 attempter_.set_http_response_code(200);
279 InSequence s;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700280 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
281 EXPECT_CALL(*processor_,
282 EnqueueAction(Property(&AbstractAction::Type,
283 kActionTypes[i]))).Times(1);
284 }
285 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
286
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700287 attempter_.Update("", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800288 g_idle_add(&StaticUpdateTestVerify, this);
289}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700290
Darin Petkove6ef2f82011-03-07 17:31:11 -0800291void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700292 EXPECT_EQ(0, attempter_.http_response_code());
293 EXPECT_EQ(&attempter_, processor_->delegate());
294 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
295 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
296 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
297 }
298 EXPECT_EQ(attempter_.response_handler_action_.get(),
299 attempter_.actions_[1].get());
300 DownloadAction* download_action =
301 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
302 ASSERT_TRUE(download_action != NULL);
303 EXPECT_EQ(&attempter_, download_action->delegate());
304 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800305 g_main_loop_quit(loop_);
306}
307
308TEST_F(UpdateAttempterTest, UpdateTest) {
309 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
310 g_idle_add(&StaticUpdateTestStart, this);
311 g_main_loop_run(loop_);
312 g_main_loop_unref(loop_);
313 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700314}
315
Thieu Le116fda32011-04-19 11:01:54 -0700316void UpdateAttempterTest::PingOmahaTestStart() {
317 EXPECT_CALL(*processor_,
318 EnqueueAction(Property(&AbstractAction::Type,
319 OmahaRequestAction::StaticType())))
320 .Times(1);
321 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
322 attempter_.PingOmaha();
323 g_idle_add(&StaticPingOmahaTestDone, this);
324}
325
326void UpdateAttempterTest::PingOmahaTestDone() {
327 g_main_loop_quit(loop_);
328}
329
330TEST_F(UpdateAttempterTest, PingOmahaTest) {
331 UpdateCheckScheduler scheduler(&attempter_);
332 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700333 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700334 attempter_.set_update_check_scheduler(&scheduler);
335 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
336 g_idle_add(&StaticPingOmahaTestStart, this);
337 g_main_loop_run(loop_);
338 g_main_loop_unref(loop_);
339 loop_ = NULL;
340 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
341 EXPECT_EQ(true, scheduler.scheduled_);
342}
343
Darin Petkov18c7bce2011-06-16 14:07:00 -0700344TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
345 ActionMock action;
346 const ActionExitCode kCode = kActionCodeDownloadTransferError;
347 attempter_.CreatePendingErrorEvent(&action, kCode);
348 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
349 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
350 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
351 EXPECT_EQ(kCode, attempter_.error_event_->error_code);
352}
353
354TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
355 OmahaResponseHandlerAction *response_action =
356 new OmahaResponseHandlerAction(&prefs_);
357 response_action->install_plan_.is_resume = true;
358 attempter_.response_handler_action_.reset(response_action);
359 ActionMock action;
360 const ActionExitCode kCode = kActionCodeInstallDeviceOpenError;
361 attempter_.CreatePendingErrorEvent(&action, kCode);
362 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
363 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
364 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
365 EXPECT_EQ(kCode | kActionCodeResumedFlag,
366 attempter_.error_event_->error_code);
367}
368
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700369} // namespace chromeos_update_engine