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: |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 36 | explicit UpdateAttempterUnderTest(MockDbusGlib* dbus) |
| 37 | : UpdateAttempter(NULL, NULL, dbus) {} |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | class UpdateAttempterTest : public ::testing::Test { |
| 41 | protected: |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 42 | UpdateAttempterTest() : attempter_(&dbus_), loop_(NULL) {} |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 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 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 63 | void UpdateTestStart(); |
| 64 | void UpdateTestVerify(); |
| 65 | static gboolean StaticUpdateTestStart(gpointer data); |
| 66 | static gboolean StaticUpdateTestVerify(gpointer data); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 67 | void PingOmahaTestStart(); |
| 68 | void PingOmahaTestDone(); |
| 69 | static gboolean StaticPingOmahaTestStart(gpointer data); |
| 70 | static gboolean StaticPingOmahaTestDone(gpointer data); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 71 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 72 | MockDbusGlib dbus_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 73 | UpdateAttempterUnderTest attempter_; |
| 74 | ActionProcessorMock* processor_; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 75 | NiceMock<PrefsMock> prefs_; |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 76 | GMainLoop* loop_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 79 | TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) { |
| 80 | scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL)); |
| 81 | fetcher->FailTransfer(503); // Sets the HTTP response code. |
| 82 | DownloadAction action(&prefs_, fetcher.release()); |
| 83 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0); |
| 84 | attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess); |
| 85 | EXPECT_EQ(503, attempter_.http_response_code()); |
| 86 | EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status()); |
| 87 | ASSERT_TRUE(attempter_.error_event_.get() == NULL); |
| 88 | } |
| 89 | |
| 90 | TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) { |
| 91 | ActionMock action; |
| 92 | EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock")); |
| 93 | attempter_.status_ = UPDATE_STATUS_DOWNLOADING; |
| 94 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 95 | .WillOnce(Return(false)); |
| 96 | attempter_.ActionCompleted(NULL, &action, kActionCodeError); |
| 97 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 98 | } |
| 99 | |
| 100 | TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) { |
| 101 | scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL)); |
| 102 | fetcher->FailTransfer(500); // Sets the HTTP response code. |
| 103 | OmahaRequestParams params; |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 104 | OmahaRequestAction action(&prefs_, params, NULL, fetcher.release(), false); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 105 | ObjectCollectorAction<OmahaResponse> collector_action; |
| 106 | BondActions(&action, &collector_action); |
| 107 | OmahaResponse response; |
| 108 | response.poll_interval = 234; |
| 109 | action.SetOutputObject(response); |
| 110 | UpdateCheckScheduler scheduler(&attempter_); |
| 111 | attempter_.set_update_check_scheduler(&scheduler); |
| 112 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0); |
| 113 | attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess); |
| 114 | EXPECT_EQ(500, attempter_.http_response_code()); |
| 115 | EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status()); |
| 116 | EXPECT_EQ(234, scheduler.poll_interval()); |
| 117 | ASSERT_TRUE(attempter_.error_event_.get() == NULL); |
| 118 | } |
| 119 | |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 120 | TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 121 | extern const char* kUpdateCompletedMarker; |
| 122 | const FilePath kMarker(kUpdateCompletedMarker); |
| 123 | EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0)); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 124 | MockDbusGlib dbus; |
| 125 | UpdateAttempterUnderTest attempter(&dbus); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 126 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status()); |
| 127 | EXPECT_TRUE(file_util::Delete(kMarker, false)); |
| 128 | } |
| 129 | |
| 130 | TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) { |
| 131 | extern ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 132 | ActionExitCode code); |
| 133 | EXPECT_EQ(kActionCodeSuccess, |
| 134 | GetErrorCodeForAction(NULL, kActionCodeSuccess)); |
| 135 | |
| 136 | OmahaRequestParams params; |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 137 | OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL, false); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 138 | EXPECT_EQ(kActionCodeOmahaRequestError, |
| 139 | GetErrorCodeForAction(&omaha_request_action, kActionCodeError)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 140 | OmahaResponseHandlerAction omaha_response_handler_action(&prefs_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 141 | EXPECT_EQ(kActionCodeOmahaResponseHandlerError, |
| 142 | GetErrorCodeForAction(&omaha_response_handler_action, |
| 143 | kActionCodeError)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 144 | FilesystemCopierAction filesystem_copier_action(false, false); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 145 | EXPECT_EQ(kActionCodeFilesystemCopierError, |
| 146 | GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 147 | PostinstallRunnerAction postinstall_runner_action; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 148 | EXPECT_EQ(kActionCodePostinstallRunnerError, |
| 149 | GetErrorCodeForAction(&postinstall_runner_action, |
| 150 | kActionCodeError)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 151 | ActionMock action_mock; |
| 152 | EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock")); |
| 153 | EXPECT_EQ(kActionCodeError, |
| 154 | GetErrorCodeForAction(&action_mock, kActionCodeError)); |
| 155 | } |
| 156 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 157 | TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) { |
| 158 | attempter_.omaha_request_params_.delta_okay = true; |
| 159 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 160 | .WillOnce(Return(false)); |
| 161 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 162 | EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); |
| 163 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 164 | .WillOnce(DoAll( |
| 165 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1), |
| 166 | Return(true))); |
| 167 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 168 | EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); |
| 169 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 170 | .WillOnce(DoAll( |
| 171 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 172 | Return(true))); |
| 173 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 174 | EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); |
| 175 | EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0); |
| 176 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 177 | EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); |
| 178 | } |
| 179 | |
| 180 | TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) { |
| 181 | attempter_.is_full_update_ = false; |
| 182 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 183 | .WillOnce(Return(false)) |
| 184 | .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true))) |
| 185 | .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true))) |
| 186 | .WillOnce(DoAll( |
| 187 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 188 | Return(true))); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 189 | EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _)) |
| 190 | .WillRepeatedly(Return(true)); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 191 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2); |
| 192 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1); |
| 193 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, |
| 194 | UpdateAttempter::kMaxDeltaUpdateFailures + 1)) |
| 195 | .Times(1); |
| 196 | for (int i = 0; i < 4; i ++) |
| 197 | attempter_.MarkDeltaUpdateFailure(); |
| 198 | } |
| 199 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 200 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) { |
| 201 | EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0); |
| 202 | EXPECT_CALL(*processor_, StartProcessing()).Times(0); |
| 203 | attempter_.ScheduleErrorEventAction(); |
| 204 | } |
| 205 | |
| 206 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) { |
| 207 | EXPECT_CALL(*processor_, |
| 208 | EnqueueAction(Property(&AbstractAction::Type, |
| 209 | OmahaRequestAction::StaticType()))) |
| 210 | .Times(1); |
| 211 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 212 | attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 213 | OmahaEvent::kResultError, |
| 214 | kActionCodeError)); |
| 215 | attempter_.ScheduleErrorEventAction(); |
| 216 | EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status()); |
| 217 | } |
| 218 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 219 | TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) { |
| 220 | extern const char* UpdateStatusToString(UpdateStatus); |
| 221 | EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE)); |
| 222 | EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE", |
| 223 | UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE)); |
| 224 | EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE", |
| 225 | UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE)); |
| 226 | EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING", |
| 227 | UpdateStatusToString(UPDATE_STATUS_DOWNLOADING)); |
| 228 | EXPECT_STREQ("UPDATE_STATUS_VERIFYING", |
| 229 | UpdateStatusToString(UPDATE_STATUS_VERIFYING)); |
| 230 | EXPECT_STREQ("UPDATE_STATUS_FINALIZING", |
| 231 | UpdateStatusToString(UPDATE_STATUS_FINALIZING)); |
| 232 | EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT", |
| 233 | UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT)); |
| 234 | EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT", |
| 235 | UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT)); |
| 236 | EXPECT_STREQ("unknown status", |
| 237 | UpdateStatusToString(static_cast<UpdateStatus>(-1))); |
| 238 | } |
| 239 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 240 | gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) { |
| 241 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart(); |
| 242 | return FALSE; |
| 243 | } |
| 244 | |
| 245 | gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) { |
| 246 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify(); |
| 247 | return FALSE; |
| 248 | } |
| 249 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 250 | gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) { |
| 251 | reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart(); |
| 252 | return FALSE; |
| 253 | } |
| 254 | |
| 255 | gboolean UpdateAttempterTest::StaticPingOmahaTestDone(gpointer data) { |
| 256 | reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestDone(); |
| 257 | return FALSE; |
| 258 | } |
| 259 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 260 | namespace { |
| 261 | const string kActionTypes[] = { |
| 262 | OmahaRequestAction::StaticType(), |
| 263 | OmahaResponseHandlerAction::StaticType(), |
| 264 | FilesystemCopierAction::StaticType(), |
| 265 | FilesystemCopierAction::StaticType(), |
| 266 | OmahaRequestAction::StaticType(), |
| 267 | DownloadAction::StaticType(), |
| 268 | OmahaRequestAction::StaticType(), |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame] | 269 | OmahaRequestAction::StaticType(), |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 270 | FilesystemCopierAction::StaticType(), |
| 271 | FilesystemCopierAction::StaticType(), |
| 272 | PostinstallRunnerAction::StaticType(), |
| 273 | OmahaRequestAction::StaticType() |
| 274 | }; |
| 275 | } // namespace {} |
| 276 | |
| 277 | void UpdateAttempterTest::UpdateTestStart() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 278 | attempter_.set_http_response_code(200); |
| 279 | InSequence s; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 280 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 281 | EXPECT_CALL(*processor_, |
| 282 | EnqueueAction(Property(&AbstractAction::Type, |
| 283 | kActionTypes[i]))).Times(1); |
| 284 | } |
| 285 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 286 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 287 | attempter_.Update("", "", false); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 288 | g_idle_add(&StaticUpdateTestVerify, this); |
| 289 | } |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 290 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 291 | void UpdateAttempterTest::UpdateTestVerify() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 292 | EXPECT_EQ(0, attempter_.http_response_code()); |
| 293 | EXPECT_EQ(&attempter_, processor_->delegate()); |
| 294 | EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size()); |
| 295 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 296 | EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type()); |
| 297 | } |
| 298 | EXPECT_EQ(attempter_.response_handler_action_.get(), |
| 299 | attempter_.actions_[1].get()); |
| 300 | DownloadAction* download_action = |
| 301 | dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); |
| 302 | ASSERT_TRUE(download_action != NULL); |
| 303 | EXPECT_EQ(&attempter_, download_action->delegate()); |
| 304 | EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 305 | g_main_loop_quit(loop_); |
| 306 | } |
| 307 | |
| 308 | TEST_F(UpdateAttempterTest, UpdateTest) { |
| 309 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 310 | g_idle_add(&StaticUpdateTestStart, this); |
| 311 | g_main_loop_run(loop_); |
| 312 | g_main_loop_unref(loop_); |
| 313 | loop_ = NULL; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 316 | void UpdateAttempterTest::PingOmahaTestStart() { |
| 317 | EXPECT_CALL(*processor_, |
| 318 | EnqueueAction(Property(&AbstractAction::Type, |
| 319 | OmahaRequestAction::StaticType()))) |
| 320 | .Times(1); |
| 321 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 322 | attempter_.PingOmaha(); |
| 323 | g_idle_add(&StaticPingOmahaTestDone, this); |
| 324 | } |
| 325 | |
| 326 | void UpdateAttempterTest::PingOmahaTestDone() { |
| 327 | g_main_loop_quit(loop_); |
| 328 | } |
| 329 | |
| 330 | TEST_F(UpdateAttempterTest, PingOmahaTest) { |
| 331 | UpdateCheckScheduler scheduler(&attempter_); |
| 332 | scheduler.enabled_ = true; |
| 333 | EXPECT_EQ(false, scheduler.scheduled_); |
| 334 | attempter_.set_update_check_scheduler(&scheduler); |
| 335 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 336 | g_idle_add(&StaticPingOmahaTestStart, this); |
| 337 | g_main_loop_run(loop_); |
| 338 | g_main_loop_unref(loop_); |
| 339 | loop_ = NULL; |
| 340 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status()); |
| 341 | EXPECT_EQ(true, scheduler.scheduled_); |
| 342 | } |
| 343 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 344 | } // namespace chromeos_update_engine |