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