Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 1 | // 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 Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 11 | #include "update_engine/mock_dbus_interface.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 12 | #include "update_engine/mock_http_fetcher.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 13 | #include "update_engine/postinstall_runner_action.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 14 | #include "update_engine/prefs_mock.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 15 | #include "update_engine/test_utils.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 16 | #include "update_engine/update_attempter.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 17 | #include "update_engine/update_check_scheduler.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 18 | |
| 19 | using std::string; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 20 | using testing::_; |
| 21 | using testing::DoAll; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 22 | using testing::InSequence; |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 23 | using testing::Ne; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 24 | using testing::NiceMock; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 25 | using testing::Property; |
| 26 | using testing::Return; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 27 | using testing::SetArgumentPointee; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
| 31 | // Test a subclass rather than the main class directly so that we can mock out |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 32 | // methods within the class. There're explicit unit tests for the mocked out |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 33 | // methods. |
| 34 | class UpdateAttempterUnderTest : public UpdateAttempter { |
| 35 | public: |
| 36 | UpdateAttempterUnderTest() |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 37 | : UpdateAttempter(NULL, NULL, &dbus_) {} |
| 38 | MockDbusGlib dbus_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class 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 Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 57 | EXPECT_FALSE(attempter_.is_full_update_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 58 | processor_ = new ActionProcessorMock(); |
| 59 | attempter_.processor_.reset(processor_); // Transfers ownership. |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 60 | attempter_.prefs_ = &prefs_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | UpdateAttempterUnderTest attempter_; |
| 64 | ActionProcessorMock* processor_; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 65 | NiceMock<PrefsMock> prefs_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 68 | TEST_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 | |
| 79 | TEST_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 | |
| 89 | TEST_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 Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 109 | TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 110 | 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 | |
| 118 | TEST_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 Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 128 | OmahaResponseHandlerAction omaha_response_handler_action(&prefs_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 129 | EXPECT_EQ(kActionCodeOmahaResponseHandlerError, |
| 130 | GetErrorCodeForAction(&omaha_response_handler_action, |
| 131 | kActionCodeError)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame^] | 132 | FilesystemCopierAction filesystem_copier_action(false, false); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 133 | EXPECT_EQ(kActionCodeFilesystemCopierError, |
| 134 | GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 135 | PostinstallRunnerAction postinstall_runner_action; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 136 | EXPECT_EQ(kActionCodePostinstallRunnerError, |
| 137 | GetErrorCodeForAction(&postinstall_runner_action, |
| 138 | kActionCodeError)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 139 | 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 Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 145 | TEST_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 | |
| 168 | TEST_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 Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 177 | EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _)) |
| 178 | .WillRepeatedly(Return(true)); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 179 | 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 Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 188 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) { |
| 189 | EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0); |
| 190 | EXPECT_CALL(*processor_, StartProcessing()).Times(0); |
| 191 | attempter_.ScheduleErrorEventAction(); |
| 192 | } |
| 193 | |
| 194 | TEST_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 Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 207 | TEST_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 | |
| 228 | TEST_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(), |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame^] | 239 | FilesystemCopierAction::StaticType(), |
| 240 | FilesystemCopierAction::StaticType(), |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 241 | PostinstallRunnerAction::StaticType(), |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 242 | OmahaRequestAction::StaticType() |
| 243 | }; |
| 244 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 245 | EXPECT_CALL(*processor_, |
| 246 | EnqueueAction(Property(&AbstractAction::Type, |
| 247 | kActionTypes[i]))).Times(1); |
| 248 | } |
| 249 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 250 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 251 | attempter_.Update("", "", false); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 252 | |
| 253 | EXPECT_EQ(0, attempter_.http_response_code()); |
| 254 | EXPECT_EQ(&attempter_, processor_->delegate()); |
| 255 | EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size()); |
| 256 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 257 | EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type()); |
| 258 | } |
| 259 | EXPECT_EQ(attempter_.response_handler_action_.get(), |
| 260 | attempter_.actions_[1].get()); |
| 261 | DownloadAction* download_action = |
| 262 | dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); |
| 263 | ASSERT_TRUE(download_action != NULL); |
| 264 | EXPECT_EQ(&attempter_, download_action->delegate()); |
| 265 | EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); |
| 266 | } |
| 267 | |
| 268 | } // namespace chromeos_update_engine |