blob: 76068fd1ecd3b7f705410e9165d81fe7716671bf [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:
Andrew de los Reyes000d8952011-03-02 15:21:14 -080042 UpdateAttempterTest() : attempter_(&dbus_) {}
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
Andrew de los Reyes000d8952011-03-02 15:21:14 -080063 MockDbusGlib dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070064 UpdateAttempterUnderTest attempter_;
65 ActionProcessorMock* processor_;
Darin Petkov9c096d62010-11-17 14:49:04 -080066 NiceMock<PrefsMock> prefs_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070067};
68
Darin Petkov1b003102010-11-30 10:18:36 -080069TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
70 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
71 fetcher->FailTransfer(503); // Sets the HTTP response code.
72 DownloadAction action(&prefs_, fetcher.release());
73 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
74 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
75 EXPECT_EQ(503, attempter_.http_response_code());
76 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
77 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
78}
79
80TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
81 ActionMock action;
82 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
83 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
84 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
85 .WillOnce(Return(false));
86 attempter_.ActionCompleted(NULL, &action, kActionCodeError);
87 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
88}
89
90TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
91 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
92 fetcher->FailTransfer(500); // Sets the HTTP response code.
93 OmahaRequestParams params;
94 OmahaRequestAction action(&prefs_, params, NULL, fetcher.release());
95 ObjectCollectorAction<OmahaResponse> collector_action;
96 BondActions(&action, &collector_action);
97 OmahaResponse response;
98 response.poll_interval = 234;
99 action.SetOutputObject(response);
100 UpdateCheckScheduler scheduler(&attempter_);
101 attempter_.set_update_check_scheduler(&scheduler);
102 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
103 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
104 EXPECT_EQ(500, attempter_.http_response_code());
105 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
106 EXPECT_EQ(234, scheduler.poll_interval());
107 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
108}
109
Darin Petkovcd1666f2010-09-23 09:53:44 -0700110TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700111 extern const char* kUpdateCompletedMarker;
112 const FilePath kMarker(kUpdateCompletedMarker);
113 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800114 MockDbusGlib dbus;
115 UpdateAttempterUnderTest attempter(&dbus);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700116 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
117 EXPECT_TRUE(file_util::Delete(kMarker, false));
118}
119
120TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
121 extern ActionExitCode GetErrorCodeForAction(AbstractAction* action,
122 ActionExitCode code);
123 EXPECT_EQ(kActionCodeSuccess,
124 GetErrorCodeForAction(NULL, kActionCodeSuccess));
125
126 OmahaRequestParams params;
127 OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL);
128 EXPECT_EQ(kActionCodeOmahaRequestError,
129 GetErrorCodeForAction(&omaha_request_action, kActionCodeError));
Darin Petkov73058b42010-10-06 16:32:19 -0700130 OmahaResponseHandlerAction omaha_response_handler_action(&prefs_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700131 EXPECT_EQ(kActionCodeOmahaResponseHandlerError,
132 GetErrorCodeForAction(&omaha_response_handler_action,
133 kActionCodeError));
Darin Petkov3aefa862010-12-07 14:45:00 -0800134 FilesystemCopierAction filesystem_copier_action(false, false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700135 EXPECT_EQ(kActionCodeFilesystemCopierError,
136 GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800137 PostinstallRunnerAction postinstall_runner_action;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700138 EXPECT_EQ(kActionCodePostinstallRunnerError,
139 GetErrorCodeForAction(&postinstall_runner_action,
140 kActionCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700141 ActionMock action_mock;
142 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
143 EXPECT_EQ(kActionCodeError,
144 GetErrorCodeForAction(&action_mock, kActionCodeError));
145}
146
Darin Petkov36275772010-10-01 11:40:57 -0700147TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
148 attempter_.omaha_request_params_.delta_okay = true;
149 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
150 .WillOnce(Return(false));
151 attempter_.DisableDeltaUpdateIfNeeded();
152 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
153 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
154 .WillOnce(DoAll(
155 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
156 Return(true)));
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),
162 Return(true)));
163 attempter_.DisableDeltaUpdateIfNeeded();
164 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
165 EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0);
166 attempter_.DisableDeltaUpdateIfNeeded();
167 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
168}
169
170TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
171 attempter_.is_full_update_ = false;
172 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
173 .WillOnce(Return(false))
174 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
175 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
176 .WillOnce(DoAll(
177 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
178 Return(true)));
Darin Petkov2dd01092010-10-08 15:43:05 -0700179 EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
180 .WillRepeatedly(Return(true));
Darin Petkov36275772010-10-01 11:40:57 -0700181 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
182 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
183 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures,
184 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
185 .Times(1);
186 for (int i = 0; i < 4; i ++)
187 attempter_.MarkDeltaUpdateFailure();
188}
189
Darin Petkov1b003102010-11-30 10:18:36 -0800190TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
191 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
192 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
193 attempter_.ScheduleErrorEventAction();
194}
195
196TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
197 EXPECT_CALL(*processor_,
198 EnqueueAction(Property(&AbstractAction::Type,
199 OmahaRequestAction::StaticType())))
200 .Times(1);
201 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
202 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
203 OmahaEvent::kResultError,
204 kActionCodeError));
205 attempter_.ScheduleErrorEventAction();
206 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
207}
208
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700209TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
210 extern const char* UpdateStatusToString(UpdateStatus);
211 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
212 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
213 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
214 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
215 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
216 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
217 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
218 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
219 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
220 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
221 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
222 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
223 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
224 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
225 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
226 EXPECT_STREQ("unknown status",
227 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
228}
229
230TEST_F(UpdateAttempterTest, UpdateTest) {
231 attempter_.set_http_response_code(200);
232 InSequence s;
233 const string kActionTypes[] = {
234 OmahaRequestAction::StaticType(),
235 OmahaResponseHandlerAction::StaticType(),
236 FilesystemCopierAction::StaticType(),
237 FilesystemCopierAction::StaticType(),
238 OmahaRequestAction::StaticType(),
239 DownloadAction::StaticType(),
240 OmahaRequestAction::StaticType(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800241 FilesystemCopierAction::StaticType(),
242 FilesystemCopierAction::StaticType(),
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700243 PostinstallRunnerAction::StaticType(),
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700244 OmahaRequestAction::StaticType()
245 };
246 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
247 EXPECT_CALL(*processor_,
248 EnqueueAction(Property(&AbstractAction::Type,
249 kActionTypes[i]))).Times(1);
250 }
251 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
252
Andrew de los Reyes45168102010-11-22 11:13:50 -0800253 attempter_.Update("", "", false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700254
255 EXPECT_EQ(0, attempter_.http_response_code());
256 EXPECT_EQ(&attempter_, processor_->delegate());
257 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
258 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
259 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
260 }
261 EXPECT_EQ(attempter_.response_handler_action_.get(),
262 attempter_.actions_[1].get());
263 DownloadAction* download_action =
264 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
265 ASSERT_TRUE(download_action != NULL);
266 EXPECT_EQ(&attempter_, download_action->delegate());
267 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
268}
269
270} // namespace chromeos_update_engine