blob: 73addb5a644801777c1064a9eb4ea121445c2387 [file] [log] [blame]
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// 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);
67
Andrew de los Reyes000d8952011-03-02 15:21:14 -080068 MockDbusGlib dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070069 UpdateAttempterUnderTest attempter_;
70 ActionProcessorMock* processor_;
Darin Petkov9c096d62010-11-17 14:49:04 -080071 NiceMock<PrefsMock> prefs_;
Darin Petkove6ef2f82011-03-07 17:31:11 -080072 GMainLoop* loop_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070073};
74
Darin Petkov1b003102010-11-30 10:18:36 -080075TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
76 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
77 fetcher->FailTransfer(503); // Sets the HTTP response code.
78 DownloadAction action(&prefs_, fetcher.release());
79 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
80 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
81 EXPECT_EQ(503, attempter_.http_response_code());
82 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
83 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
84}
85
86TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
87 ActionMock action;
88 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
89 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
90 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
91 .WillOnce(Return(false));
92 attempter_.ActionCompleted(NULL, &action, kActionCodeError);
93 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
94}
95
96TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
97 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
98 fetcher->FailTransfer(500); // Sets the HTTP response code.
99 OmahaRequestParams params;
100 OmahaRequestAction action(&prefs_, params, NULL, fetcher.release());
101 ObjectCollectorAction<OmahaResponse> collector_action;
102 BondActions(&action, &collector_action);
103 OmahaResponse response;
104 response.poll_interval = 234;
105 action.SetOutputObject(response);
106 UpdateCheckScheduler scheduler(&attempter_);
107 attempter_.set_update_check_scheduler(&scheduler);
108 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
109 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
110 EXPECT_EQ(500, attempter_.http_response_code());
111 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
112 EXPECT_EQ(234, scheduler.poll_interval());
113 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
114}
115
Darin Petkovcd1666f2010-09-23 09:53:44 -0700116TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700117 extern const char* kUpdateCompletedMarker;
118 const FilePath kMarker(kUpdateCompletedMarker);
119 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800120 MockDbusGlib dbus;
121 UpdateAttempterUnderTest attempter(&dbus);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700122 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
123 EXPECT_TRUE(file_util::Delete(kMarker, false));
124}
125
126TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
127 extern ActionExitCode GetErrorCodeForAction(AbstractAction* action,
128 ActionExitCode code);
129 EXPECT_EQ(kActionCodeSuccess,
130 GetErrorCodeForAction(NULL, kActionCodeSuccess));
131
132 OmahaRequestParams params;
133 OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL);
134 EXPECT_EQ(kActionCodeOmahaRequestError,
135 GetErrorCodeForAction(&omaha_request_action, kActionCodeError));
Darin Petkov73058b42010-10-06 16:32:19 -0700136 OmahaResponseHandlerAction omaha_response_handler_action(&prefs_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700137 EXPECT_EQ(kActionCodeOmahaResponseHandlerError,
138 GetErrorCodeForAction(&omaha_response_handler_action,
139 kActionCodeError));
Darin Petkov3aefa862010-12-07 14:45:00 -0800140 FilesystemCopierAction filesystem_copier_action(false, false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700141 EXPECT_EQ(kActionCodeFilesystemCopierError,
142 GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800143 PostinstallRunnerAction postinstall_runner_action;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700144 EXPECT_EQ(kActionCodePostinstallRunnerError,
145 GetErrorCodeForAction(&postinstall_runner_action,
146 kActionCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700147 ActionMock action_mock;
148 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
149 EXPECT_EQ(kActionCodeError,
150 GetErrorCodeForAction(&action_mock, kActionCodeError));
151}
152
Darin Petkov36275772010-10-01 11:40:57 -0700153TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
154 attempter_.omaha_request_params_.delta_okay = true;
155 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
156 .WillOnce(Return(false));
157 attempter_.DisableDeltaUpdateIfNeeded();
158 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
159 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
160 .WillOnce(DoAll(
161 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
162 Return(true)));
163 attempter_.DisableDeltaUpdateIfNeeded();
164 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
165 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
166 .WillOnce(DoAll(
167 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
168 Return(true)));
169 attempter_.DisableDeltaUpdateIfNeeded();
170 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
171 EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0);
172 attempter_.DisableDeltaUpdateIfNeeded();
173 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
174}
175
176TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
177 attempter_.is_full_update_ = false;
178 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
179 .WillOnce(Return(false))
180 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
181 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
182 .WillOnce(DoAll(
183 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
184 Return(true)));
Darin Petkov2dd01092010-10-08 15:43:05 -0700185 EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
186 .WillRepeatedly(Return(true));
Darin Petkov36275772010-10-01 11:40:57 -0700187 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
188 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
189 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures,
190 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
191 .Times(1);
192 for (int i = 0; i < 4; i ++)
193 attempter_.MarkDeltaUpdateFailure();
194}
195
Darin Petkov1b003102010-11-30 10:18:36 -0800196TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
197 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
198 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
199 attempter_.ScheduleErrorEventAction();
200}
201
202TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
203 EXPECT_CALL(*processor_,
204 EnqueueAction(Property(&AbstractAction::Type,
205 OmahaRequestAction::StaticType())))
206 .Times(1);
207 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
208 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
209 OmahaEvent::kResultError,
210 kActionCodeError));
211 attempter_.ScheduleErrorEventAction();
212 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
213}
214
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700215TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
216 extern const char* UpdateStatusToString(UpdateStatus);
217 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
218 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
219 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
220 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
221 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
222 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
223 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
224 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
225 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
226 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
227 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
228 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
229 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
230 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
231 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
232 EXPECT_STREQ("unknown status",
233 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
234}
235
Darin Petkove6ef2f82011-03-07 17:31:11 -0800236gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
237 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
238 return FALSE;
239}
240
241gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
242 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
243 return FALSE;
244}
245
246namespace {
247const string kActionTypes[] = {
248 OmahaRequestAction::StaticType(),
249 OmahaResponseHandlerAction::StaticType(),
250 FilesystemCopierAction::StaticType(),
251 FilesystemCopierAction::StaticType(),
252 OmahaRequestAction::StaticType(),
253 DownloadAction::StaticType(),
254 OmahaRequestAction::StaticType(),
255 FilesystemCopierAction::StaticType(),
256 FilesystemCopierAction::StaticType(),
257 PostinstallRunnerAction::StaticType(),
258 OmahaRequestAction::StaticType()
259};
260} // namespace {}
261
262void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700263 attempter_.set_http_response_code(200);
264 InSequence s;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700265 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
266 EXPECT_CALL(*processor_,
267 EnqueueAction(Property(&AbstractAction::Type,
268 kActionTypes[i]))).Times(1);
269 }
270 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
271
Andrew de los Reyes45168102010-11-22 11:13:50 -0800272 attempter_.Update("", "", false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800273 g_idle_add(&StaticUpdateTestVerify, this);
274}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700275
Darin Petkove6ef2f82011-03-07 17:31:11 -0800276void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700277 EXPECT_EQ(0, attempter_.http_response_code());
278 EXPECT_EQ(&attempter_, processor_->delegate());
279 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
280 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
281 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
282 }
283 EXPECT_EQ(attempter_.response_handler_action_.get(),
284 attempter_.actions_[1].get());
285 DownloadAction* download_action =
286 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
287 ASSERT_TRUE(download_action != NULL);
288 EXPECT_EQ(&attempter_, download_action->delegate());
289 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800290 g_main_loop_quit(loop_);
291}
292
293TEST_F(UpdateAttempterTest, UpdateTest) {
294 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
295 g_idle_add(&StaticUpdateTestStart, this);
296 g_main_loop_run(loop_);
297 g_main_loop_unref(loop_);
298 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700299}
300
301} // namespace chromeos_update_engine