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" |
| 11 | #include "update_engine/postinstall_runner_action.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 12 | #include "update_engine/prefs_mock.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 13 | #include "update_engine/set_bootable_flag_action.h" |
| 14 | #include "update_engine/update_attempter.h" |
| 15 | |
| 16 | using std::string; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 17 | using testing::_; |
| 18 | using testing::DoAll; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 19 | using testing::InSequence; |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 20 | using testing::Ne; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 21 | using testing::Property; |
| 22 | using testing::Return; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 23 | using testing::SetArgumentPointee; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 24 | |
| 25 | namespace chromeos_update_engine { |
| 26 | |
| 27 | // 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] | 28 | // 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] | 29 | // methods. |
| 30 | class UpdateAttempterUnderTest : public UpdateAttempter { |
| 31 | public: |
| 32 | UpdateAttempterUnderTest() |
| 33 | : UpdateAttempter(NULL, NULL) {} |
| 34 | }; |
| 35 | |
| 36 | class UpdateAttempterTest : public ::testing::Test { |
| 37 | protected: |
| 38 | virtual void SetUp() { |
| 39 | EXPECT_EQ(NULL, attempter_.dbus_service_); |
| 40 | EXPECT_EQ(NULL, attempter_.prefs_); |
| 41 | EXPECT_EQ(NULL, attempter_.metrics_lib_); |
| 42 | EXPECT_EQ(NULL, attempter_.update_check_scheduler_); |
| 43 | EXPECT_EQ(0, attempter_.http_response_code_); |
| 44 | EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_); |
| 45 | EXPECT_EQ(NULL, attempter_.manage_priority_source_); |
| 46 | EXPECT_FALSE(attempter_.download_active_); |
| 47 | EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_); |
| 48 | EXPECT_EQ(0.0, attempter_.download_progress_); |
| 49 | EXPECT_EQ(0, attempter_.last_checked_time_); |
| 50 | EXPECT_EQ("0.0.0.0", attempter_.new_version_); |
| 51 | EXPECT_EQ(0, attempter_.new_size_); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 52 | EXPECT_FALSE(attempter_.is_full_update_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 53 | processor_ = new ActionProcessorMock(); |
| 54 | attempter_.processor_.reset(processor_); // Transfers ownership. |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 55 | attempter_.prefs_ = &prefs_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | UpdateAttempterUnderTest attempter_; |
| 59 | ActionProcessorMock* processor_; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 60 | PrefsMock prefs_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 63 | TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 64 | extern const char* kUpdateCompletedMarker; |
| 65 | const FilePath kMarker(kUpdateCompletedMarker); |
| 66 | EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0)); |
| 67 | UpdateAttempterUnderTest attempter; |
| 68 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status()); |
| 69 | EXPECT_TRUE(file_util::Delete(kMarker, false)); |
| 70 | } |
| 71 | |
| 72 | TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) { |
| 73 | extern ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 74 | ActionExitCode code); |
| 75 | EXPECT_EQ(kActionCodeSuccess, |
| 76 | GetErrorCodeForAction(NULL, kActionCodeSuccess)); |
| 77 | |
| 78 | OmahaRequestParams params; |
| 79 | OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL); |
| 80 | EXPECT_EQ(kActionCodeOmahaRequestError, |
| 81 | GetErrorCodeForAction(&omaha_request_action, kActionCodeError)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 82 | OmahaResponseHandlerAction omaha_response_handler_action(&prefs_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 83 | EXPECT_EQ(kActionCodeOmahaResponseHandlerError, |
| 84 | GetErrorCodeForAction(&omaha_response_handler_action, |
| 85 | kActionCodeError)); |
| 86 | FilesystemCopierAction filesystem_copier_action(false); |
| 87 | EXPECT_EQ(kActionCodeFilesystemCopierError, |
| 88 | GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError)); |
| 89 | PostinstallRunnerAction postinstall_runner_action(true); |
| 90 | EXPECT_EQ(kActionCodePostinstallRunnerError, |
| 91 | GetErrorCodeForAction(&postinstall_runner_action, |
| 92 | kActionCodeError)); |
| 93 | SetBootableFlagAction set_bootable_flag_action; |
| 94 | EXPECT_EQ(kActionCodeSetBootableFlagError, |
| 95 | GetErrorCodeForAction(&set_bootable_flag_action, |
| 96 | kActionCodeError)); |
| 97 | ActionMock action_mock; |
| 98 | EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock")); |
| 99 | EXPECT_EQ(kActionCodeError, |
| 100 | GetErrorCodeForAction(&action_mock, kActionCodeError)); |
| 101 | } |
| 102 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 103 | TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) { |
| 104 | attempter_.omaha_request_params_.delta_okay = true; |
| 105 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 106 | .WillOnce(Return(false)); |
| 107 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 108 | EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); |
| 109 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 110 | .WillOnce(DoAll( |
| 111 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1), |
| 112 | Return(true))); |
| 113 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 114 | EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); |
| 115 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 116 | .WillOnce(DoAll( |
| 117 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 118 | Return(true))); |
| 119 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 120 | EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); |
| 121 | EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0); |
| 122 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 123 | EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); |
| 124 | } |
| 125 | |
| 126 | TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) { |
| 127 | attempter_.is_full_update_ = false; |
| 128 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 129 | .WillOnce(Return(false)) |
| 130 | .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true))) |
| 131 | .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true))) |
| 132 | .WillOnce(DoAll( |
| 133 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 134 | Return(true))); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 135 | EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _)) |
| 136 | .WillRepeatedly(Return(true)); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 137 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2); |
| 138 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1); |
| 139 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, |
| 140 | UpdateAttempter::kMaxDeltaUpdateFailures + 1)) |
| 141 | .Times(1); |
| 142 | for (int i = 0; i < 4; i ++) |
| 143 | attempter_.MarkDeltaUpdateFailure(); |
| 144 | } |
| 145 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 146 | TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) { |
| 147 | extern const char* UpdateStatusToString(UpdateStatus); |
| 148 | EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE)); |
| 149 | EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE", |
| 150 | UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE)); |
| 151 | EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE", |
| 152 | UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE)); |
| 153 | EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING", |
| 154 | UpdateStatusToString(UPDATE_STATUS_DOWNLOADING)); |
| 155 | EXPECT_STREQ("UPDATE_STATUS_VERIFYING", |
| 156 | UpdateStatusToString(UPDATE_STATUS_VERIFYING)); |
| 157 | EXPECT_STREQ("UPDATE_STATUS_FINALIZING", |
| 158 | UpdateStatusToString(UPDATE_STATUS_FINALIZING)); |
| 159 | EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT", |
| 160 | UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT)); |
| 161 | EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT", |
| 162 | UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT)); |
| 163 | EXPECT_STREQ("unknown status", |
| 164 | UpdateStatusToString(static_cast<UpdateStatus>(-1))); |
| 165 | } |
| 166 | |
| 167 | TEST_F(UpdateAttempterTest, UpdateTest) { |
| 168 | attempter_.set_http_response_code(200); |
| 169 | InSequence s; |
| 170 | const string kActionTypes[] = { |
| 171 | OmahaRequestAction::StaticType(), |
| 172 | OmahaResponseHandlerAction::StaticType(), |
| 173 | FilesystemCopierAction::StaticType(), |
| 174 | FilesystemCopierAction::StaticType(), |
| 175 | OmahaRequestAction::StaticType(), |
| 176 | DownloadAction::StaticType(), |
| 177 | OmahaRequestAction::StaticType(), |
| 178 | PostinstallRunnerAction::StaticType(), |
| 179 | SetBootableFlagAction::StaticType(), |
| 180 | PostinstallRunnerAction::StaticType(), |
| 181 | OmahaRequestAction::StaticType() |
| 182 | }; |
| 183 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 184 | EXPECT_CALL(*processor_, |
| 185 | EnqueueAction(Property(&AbstractAction::Type, |
| 186 | kActionTypes[i]))).Times(1); |
| 187 | } |
| 188 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 189 | |
| 190 | attempter_.Update("", ""); |
| 191 | |
| 192 | EXPECT_EQ(0, attempter_.http_response_code()); |
| 193 | EXPECT_EQ(&attempter_, processor_->delegate()); |
| 194 | EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size()); |
| 195 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 196 | EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type()); |
| 197 | } |
| 198 | EXPECT_EQ(attempter_.response_handler_action_.get(), |
| 199 | attempter_.actions_[1].get()); |
| 200 | DownloadAction* download_action = |
| 201 | dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); |
| 202 | ASSERT_TRUE(download_action != NULL); |
| 203 | EXPECT_EQ(&attempter_, download_action->delegate()); |
| 204 | EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); |
| 205 | } |
| 206 | |
| 207 | } // namespace chromeos_update_engine |