blob: 6d61c4e11fc1fac582a250fa02af01b17cdbe8b2 [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:
36 UpdateAttempterUnderTest()
Andrew de los Reyes45168102010-11-22 11:13:50 -080037 : UpdateAttempter(NULL, NULL, &dbus_) {}
38 MockDbusGlib dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070039};
40
41class UpdateAttempterTest : public ::testing::Test {
42 protected:
43 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
63 UpdateAttempterUnderTest attempter_;
64 ActionProcessorMock* processor_;
Darin Petkov9c096d62010-11-17 14:49:04 -080065 NiceMock<PrefsMock> prefs_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070066};
67
Darin Petkov1b003102010-11-30 10:18:36 -080068TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
69 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
70 fetcher->FailTransfer(503); // Sets the HTTP response code.
71 DownloadAction action(&prefs_, fetcher.release());
72 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
73 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
74 EXPECT_EQ(503, attempter_.http_response_code());
75 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
76 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
77}
78
79TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
80 ActionMock action;
81 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
82 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
83 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
84 .WillOnce(Return(false));
85 attempter_.ActionCompleted(NULL, &action, kActionCodeError);
86 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
87}
88
89TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
90 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
91 fetcher->FailTransfer(500); // Sets the HTTP response code.
92 OmahaRequestParams params;
93 OmahaRequestAction action(&prefs_, params, NULL, fetcher.release());
94 ObjectCollectorAction<OmahaResponse> collector_action;
95 BondActions(&action, &collector_action);
96 OmahaResponse response;
97 response.poll_interval = 234;
98 action.SetOutputObject(response);
99 UpdateCheckScheduler scheduler(&attempter_);
100 attempter_.set_update_check_scheduler(&scheduler);
101 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
102 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
103 EXPECT_EQ(500, attempter_.http_response_code());
104 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
105 EXPECT_EQ(234, scheduler.poll_interval());
106 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
107}
108
Darin Petkovcd1666f2010-09-23 09:53:44 -0700109TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700110 extern const char* kUpdateCompletedMarker;
111 const FilePath kMarker(kUpdateCompletedMarker);
112 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
113 UpdateAttempterUnderTest attempter;
114 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
115 EXPECT_TRUE(file_util::Delete(kMarker, false));
116}
117
118TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
119 extern ActionExitCode GetErrorCodeForAction(AbstractAction* action,
120 ActionExitCode code);
121 EXPECT_EQ(kActionCodeSuccess,
122 GetErrorCodeForAction(NULL, kActionCodeSuccess));
123
124 OmahaRequestParams params;
125 OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL);
126 EXPECT_EQ(kActionCodeOmahaRequestError,
127 GetErrorCodeForAction(&omaha_request_action, kActionCodeError));
Darin Petkov73058b42010-10-06 16:32:19 -0700128 OmahaResponseHandlerAction omaha_response_handler_action(&prefs_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700129 EXPECT_EQ(kActionCodeOmahaResponseHandlerError,
130 GetErrorCodeForAction(&omaha_response_handler_action,
131 kActionCodeError));
132 FilesystemCopierAction filesystem_copier_action(false);
133 EXPECT_EQ(kActionCodeFilesystemCopierError,
134 GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800135 PostinstallRunnerAction postinstall_runner_action;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700136 EXPECT_EQ(kActionCodePostinstallRunnerError,
137 GetErrorCodeForAction(&postinstall_runner_action,
138 kActionCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700139 ActionMock action_mock;
140 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
141 EXPECT_EQ(kActionCodeError,
142 GetErrorCodeForAction(&action_mock, kActionCodeError));
143}
144
Darin Petkov36275772010-10-01 11:40:57 -0700145TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
146 attempter_.omaha_request_params_.delta_okay = true;
147 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
148 .WillOnce(Return(false));
149 attempter_.DisableDeltaUpdateIfNeeded();
150 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
151 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
152 .WillOnce(DoAll(
153 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
154 Return(true)));
155 attempter_.DisableDeltaUpdateIfNeeded();
156 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
157 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
158 .WillOnce(DoAll(
159 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
160 Return(true)));
161 attempter_.DisableDeltaUpdateIfNeeded();
162 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
163 EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0);
164 attempter_.DisableDeltaUpdateIfNeeded();
165 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
166}
167
168TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
169 attempter_.is_full_update_ = false;
170 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
171 .WillOnce(Return(false))
172 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
173 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
174 .WillOnce(DoAll(
175 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
176 Return(true)));
Darin Petkov2dd01092010-10-08 15:43:05 -0700177 EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
178 .WillRepeatedly(Return(true));
Darin Petkov36275772010-10-01 11:40:57 -0700179 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
180 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
181 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures,
182 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
183 .Times(1);
184 for (int i = 0; i < 4; i ++)
185 attempter_.MarkDeltaUpdateFailure();
186}
187
Darin Petkov1b003102010-11-30 10:18:36 -0800188TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
189 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
190 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
191 attempter_.ScheduleErrorEventAction();
192}
193
194TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
195 EXPECT_CALL(*processor_,
196 EnqueueAction(Property(&AbstractAction::Type,
197 OmahaRequestAction::StaticType())))
198 .Times(1);
199 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
200 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
201 OmahaEvent::kResultError,
202 kActionCodeError));
203 attempter_.ScheduleErrorEventAction();
204 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
205}
206
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700207TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
208 extern const char* UpdateStatusToString(UpdateStatus);
209 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
210 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
211 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
212 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
213 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
214 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
215 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
216 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
217 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
218 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
219 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
220 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
221 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
222 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
223 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
224 EXPECT_STREQ("unknown status",
225 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
226}
227
228TEST_F(UpdateAttempterTest, UpdateTest) {
229 attempter_.set_http_response_code(200);
230 InSequence s;
231 const string kActionTypes[] = {
232 OmahaRequestAction::StaticType(),
233 OmahaResponseHandlerAction::StaticType(),
234 FilesystemCopierAction::StaticType(),
235 FilesystemCopierAction::StaticType(),
236 OmahaRequestAction::StaticType(),
237 DownloadAction::StaticType(),
238 OmahaRequestAction::StaticType(),
239 PostinstallRunnerAction::StaticType(),
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700240 OmahaRequestAction::StaticType()
241 };
242 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
243 EXPECT_CALL(*processor_,
244 EnqueueAction(Property(&AbstractAction::Type,
245 kActionTypes[i]))).Times(1);
246 }
247 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
248
Andrew de los Reyes45168102010-11-22 11:13:50 -0800249 attempter_.Update("", "", false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700250
251 EXPECT_EQ(0, attempter_.http_response_code());
252 EXPECT_EQ(&attempter_, processor_->delegate());
253 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
254 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
255 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
256 }
257 EXPECT_EQ(attempter_.response_handler_action_.get(),
258 attempter_.actions_[1].get());
259 DownloadAction* download_action =
260 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
261 ASSERT_TRUE(download_action != NULL);
262 EXPECT_EQ(&attempter_, download_action->delegate());
263 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
264}
265
266} // namespace chromeos_update_engine