Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 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> |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 7 | #include <policy/libpolicy.h> |
| 8 | #include <policy/mock_device_policy.h> |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 9 | |
| 10 | #include "update_engine/action_mock.h" |
| 11 | #include "update_engine/action_processor_mock.h" |
| 12 | #include "update_engine/filesystem_copier_action.h" |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 13 | #include "update_engine/mock_dbus_interface.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 14 | #include "update_engine/mock_http_fetcher.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 15 | #include "update_engine/postinstall_runner_action.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 16 | #include "update_engine/prefs_mock.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 17 | #include "update_engine/test_utils.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 18 | #include "update_engine/update_attempter.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 19 | #include "update_engine/update_check_scheduler.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 20 | |
| 21 | using std::string; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 22 | using testing::_; |
| 23 | using testing::DoAll; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 24 | using testing::InSequence; |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 25 | using testing::Ne; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 26 | using testing::NiceMock; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 27 | using testing::Property; |
| 28 | using testing::Return; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 29 | using testing::SetArgumentPointee; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | // 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] | 34 | // 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] | 35 | // methods. |
| 36 | class UpdateAttempterUnderTest : public UpdateAttempter { |
| 37 | public: |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 38 | explicit UpdateAttempterUnderTest(MockDbusGlib* dbus) |
| 39 | : UpdateAttempter(NULL, NULL, dbus) {} |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class UpdateAttempterTest : public ::testing::Test { |
| 43 | protected: |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 44 | UpdateAttempterTest() : attempter_(&dbus_), loop_(NULL) {} |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 45 | virtual void SetUp() { |
| 46 | EXPECT_EQ(NULL, attempter_.dbus_service_); |
| 47 | EXPECT_EQ(NULL, attempter_.prefs_); |
| 48 | EXPECT_EQ(NULL, attempter_.metrics_lib_); |
| 49 | EXPECT_EQ(NULL, attempter_.update_check_scheduler_); |
| 50 | EXPECT_EQ(0, attempter_.http_response_code_); |
| 51 | EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_); |
| 52 | EXPECT_EQ(NULL, attempter_.manage_priority_source_); |
| 53 | EXPECT_FALSE(attempter_.download_active_); |
| 54 | EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_); |
| 55 | EXPECT_EQ(0.0, attempter_.download_progress_); |
| 56 | EXPECT_EQ(0, attempter_.last_checked_time_); |
| 57 | EXPECT_EQ("0.0.0.0", attempter_.new_version_); |
| 58 | EXPECT_EQ(0, attempter_.new_size_); |
| 59 | processor_ = new ActionProcessorMock(); |
| 60 | attempter_.processor_.reset(processor_); // Transfers ownership. |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 61 | attempter_.prefs_ = &prefs_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 64 | void QuitMainLoop(); |
| 65 | static gboolean StaticQuitMainLoop(gpointer data); |
| 66 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 67 | void UpdateTestStart(); |
| 68 | void UpdateTestVerify(); |
| 69 | static gboolean StaticUpdateTestStart(gpointer data); |
| 70 | static gboolean StaticUpdateTestVerify(gpointer data); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 71 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 72 | void PingOmahaTestStart(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 73 | static gboolean StaticPingOmahaTestStart(gpointer data); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 74 | |
| 75 | void ReadTrackFromPolicyTestStart(); |
| 76 | static gboolean StaticReadTrackFromPolicyTestStart(gpointer data); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 77 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 78 | MockDbusGlib dbus_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 79 | UpdateAttempterUnderTest attempter_; |
| 80 | ActionProcessorMock* processor_; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 81 | NiceMock<PrefsMock> prefs_; |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 82 | GMainLoop* loop_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 85 | TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) { |
| 86 | scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL)); |
| 87 | fetcher->FailTransfer(503); // Sets the HTTP response code. |
| 88 | DownloadAction action(&prefs_, fetcher.release()); |
| 89 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0); |
| 90 | attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess); |
| 91 | EXPECT_EQ(503, attempter_.http_response_code()); |
| 92 | EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status()); |
| 93 | ASSERT_TRUE(attempter_.error_event_.get() == NULL); |
| 94 | } |
| 95 | |
| 96 | TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) { |
| 97 | ActionMock action; |
| 98 | EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock")); |
| 99 | attempter_.status_ = UPDATE_STATUS_DOWNLOADING; |
| 100 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 101 | .WillOnce(Return(false)); |
| 102 | attempter_.ActionCompleted(NULL, &action, kActionCodeError); |
| 103 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 104 | } |
| 105 | |
| 106 | TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) { |
| 107 | scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL)); |
| 108 | fetcher->FailTransfer(500); // Sets the HTTP response code. |
| 109 | OmahaRequestParams params; |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 110 | OmahaRequestAction action(&prefs_, params, NULL, fetcher.release(), false); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 111 | ObjectCollectorAction<OmahaResponse> collector_action; |
| 112 | BondActions(&action, &collector_action); |
| 113 | OmahaResponse response; |
| 114 | response.poll_interval = 234; |
| 115 | action.SetOutputObject(response); |
| 116 | UpdateCheckScheduler scheduler(&attempter_); |
| 117 | attempter_.set_update_check_scheduler(&scheduler); |
| 118 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0); |
| 119 | attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess); |
| 120 | EXPECT_EQ(500, attempter_.http_response_code()); |
| 121 | EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status()); |
| 122 | EXPECT_EQ(234, scheduler.poll_interval()); |
| 123 | ASSERT_TRUE(attempter_.error_event_.get() == NULL); |
| 124 | } |
| 125 | |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 126 | TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 127 | extern const char* kUpdateCompletedMarker; |
| 128 | const FilePath kMarker(kUpdateCompletedMarker); |
| 129 | EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0)); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 130 | MockDbusGlib dbus; |
| 131 | UpdateAttempterUnderTest attempter(&dbus); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 132 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status()); |
| 133 | EXPECT_TRUE(file_util::Delete(kMarker, false)); |
| 134 | } |
| 135 | |
| 136 | TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) { |
| 137 | extern ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 138 | ActionExitCode code); |
| 139 | EXPECT_EQ(kActionCodeSuccess, |
| 140 | GetErrorCodeForAction(NULL, kActionCodeSuccess)); |
| 141 | |
| 142 | OmahaRequestParams params; |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 143 | OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL, false); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 144 | EXPECT_EQ(kActionCodeOmahaRequestError, |
| 145 | GetErrorCodeForAction(&omaha_request_action, kActionCodeError)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 146 | OmahaResponseHandlerAction omaha_response_handler_action(&prefs_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 147 | EXPECT_EQ(kActionCodeOmahaResponseHandlerError, |
| 148 | GetErrorCodeForAction(&omaha_response_handler_action, |
| 149 | kActionCodeError)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 150 | FilesystemCopierAction filesystem_copier_action(false, false); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 151 | EXPECT_EQ(kActionCodeFilesystemCopierError, |
| 152 | GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 153 | PostinstallRunnerAction postinstall_runner_action; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 154 | EXPECT_EQ(kActionCodePostinstallRunnerError, |
| 155 | GetErrorCodeForAction(&postinstall_runner_action, |
| 156 | kActionCodeError)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 157 | ActionMock action_mock; |
| 158 | EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock")); |
| 159 | EXPECT_EQ(kActionCodeError, |
| 160 | GetErrorCodeForAction(&action_mock, kActionCodeError)); |
| 161 | } |
| 162 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 163 | TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) { |
| 164 | attempter_.omaha_request_params_.delta_okay = true; |
| 165 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 166 | .WillOnce(Return(false)); |
| 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 - 1), |
| 172 | Return(true))); |
| 173 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 174 | EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); |
| 175 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 176 | .WillOnce(DoAll( |
| 177 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 178 | Return(true))); |
| 179 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 180 | EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); |
| 181 | EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0); |
| 182 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 183 | EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); |
| 184 | } |
| 185 | |
| 186 | TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) { |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 187 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 188 | .WillOnce(Return(false)) |
| 189 | .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true))) |
| 190 | .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true))) |
| 191 | .WillOnce(DoAll( |
| 192 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 193 | Return(true))); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 194 | EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _)) |
| 195 | .WillRepeatedly(Return(true)); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 196 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2); |
| 197 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1); |
| 198 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, |
| 199 | UpdateAttempter::kMaxDeltaUpdateFailures + 1)) |
| 200 | .Times(1); |
| 201 | for (int i = 0; i < 4; i ++) |
| 202 | attempter_.MarkDeltaUpdateFailure(); |
| 203 | } |
| 204 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 205 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) { |
| 206 | EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0); |
| 207 | EXPECT_CALL(*processor_, StartProcessing()).Times(0); |
| 208 | attempter_.ScheduleErrorEventAction(); |
| 209 | } |
| 210 | |
| 211 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) { |
| 212 | EXPECT_CALL(*processor_, |
| 213 | EnqueueAction(Property(&AbstractAction::Type, |
| 214 | OmahaRequestAction::StaticType()))) |
| 215 | .Times(1); |
| 216 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 217 | attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 218 | OmahaEvent::kResultError, |
| 219 | kActionCodeError)); |
| 220 | attempter_.ScheduleErrorEventAction(); |
| 221 | EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status()); |
| 222 | } |
| 223 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 224 | TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) { |
| 225 | extern const char* UpdateStatusToString(UpdateStatus); |
| 226 | EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE)); |
| 227 | EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE", |
| 228 | UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE)); |
| 229 | EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE", |
| 230 | UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE)); |
| 231 | EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING", |
| 232 | UpdateStatusToString(UPDATE_STATUS_DOWNLOADING)); |
| 233 | EXPECT_STREQ("UPDATE_STATUS_VERIFYING", |
| 234 | UpdateStatusToString(UPDATE_STATUS_VERIFYING)); |
| 235 | EXPECT_STREQ("UPDATE_STATUS_FINALIZING", |
| 236 | UpdateStatusToString(UPDATE_STATUS_FINALIZING)); |
| 237 | EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT", |
| 238 | UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT)); |
| 239 | EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT", |
| 240 | UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT)); |
| 241 | EXPECT_STREQ("unknown status", |
| 242 | UpdateStatusToString(static_cast<UpdateStatus>(-1))); |
| 243 | } |
| 244 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 245 | void UpdateAttempterTest::QuitMainLoop() { |
| 246 | g_main_loop_quit(loop_); |
| 247 | } |
| 248 | |
| 249 | gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) { |
| 250 | reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop(); |
| 251 | return FALSE; |
| 252 | } |
| 253 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 254 | gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) { |
| 255 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart(); |
| 256 | return FALSE; |
| 257 | } |
| 258 | |
| 259 | gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) { |
| 260 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify(); |
| 261 | return FALSE; |
| 262 | } |
| 263 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 264 | gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) { |
| 265 | reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart(); |
| 266 | return FALSE; |
| 267 | } |
| 268 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 269 | gboolean UpdateAttempterTest::StaticReadTrackFromPolicyTestStart( |
| 270 | gpointer data) { |
| 271 | reinterpret_cast<UpdateAttempterTest*>(data)->ReadTrackFromPolicyTestStart(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 272 | return FALSE; |
| 273 | } |
| 274 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 275 | namespace { |
| 276 | const string kActionTypes[] = { |
| 277 | OmahaRequestAction::StaticType(), |
| 278 | OmahaResponseHandlerAction::StaticType(), |
| 279 | FilesystemCopierAction::StaticType(), |
| 280 | FilesystemCopierAction::StaticType(), |
| 281 | OmahaRequestAction::StaticType(), |
| 282 | DownloadAction::StaticType(), |
| 283 | OmahaRequestAction::StaticType(), |
| 284 | FilesystemCopierAction::StaticType(), |
| 285 | FilesystemCopierAction::StaticType(), |
| 286 | PostinstallRunnerAction::StaticType(), |
| 287 | OmahaRequestAction::StaticType() |
| 288 | }; |
| 289 | } // namespace {} |
| 290 | |
| 291 | void UpdateAttempterTest::UpdateTestStart() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 292 | attempter_.set_http_response_code(200); |
| 293 | InSequence s; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 294 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 295 | EXPECT_CALL(*processor_, |
| 296 | EnqueueAction(Property(&AbstractAction::Type, |
| 297 | kActionTypes[i]))).Times(1); |
| 298 | } |
| 299 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 300 | |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 301 | attempter_.Update("", "", false, false); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 302 | g_idle_add(&StaticUpdateTestVerify, this); |
| 303 | } |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 304 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 305 | void UpdateAttempterTest::UpdateTestVerify() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 306 | EXPECT_EQ(0, attempter_.http_response_code()); |
| 307 | EXPECT_EQ(&attempter_, processor_->delegate()); |
| 308 | EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size()); |
| 309 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 310 | EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type()); |
| 311 | } |
| 312 | EXPECT_EQ(attempter_.response_handler_action_.get(), |
| 313 | attempter_.actions_[1].get()); |
| 314 | DownloadAction* download_action = |
| 315 | dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); |
| 316 | ASSERT_TRUE(download_action != NULL); |
| 317 | EXPECT_EQ(&attempter_, download_action->delegate()); |
| 318 | EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 319 | g_main_loop_quit(loop_); |
| 320 | } |
| 321 | |
| 322 | TEST_F(UpdateAttempterTest, UpdateTest) { |
| 323 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 324 | g_idle_add(&StaticUpdateTestStart, this); |
| 325 | g_main_loop_run(loop_); |
| 326 | g_main_loop_unref(loop_); |
| 327 | loop_ = NULL; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 330 | void UpdateAttempterTest::PingOmahaTestStart() { |
| 331 | EXPECT_CALL(*processor_, |
| 332 | EnqueueAction(Property(&AbstractAction::Type, |
| 333 | OmahaRequestAction::StaticType()))) |
| 334 | .Times(1); |
| 335 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 336 | attempter_.PingOmaha(); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 337 | g_idle_add(&StaticQuitMainLoop, this); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | TEST_F(UpdateAttempterTest, PingOmahaTest) { |
| 341 | UpdateCheckScheduler scheduler(&attempter_); |
| 342 | scheduler.enabled_ = true; |
Andrew de los Reyes | e05fc28 | 2011-06-02 09:50:08 -0700 | [diff] [blame] | 343 | EXPECT_FALSE(scheduler.scheduled_); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 344 | attempter_.set_update_check_scheduler(&scheduler); |
| 345 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 346 | g_idle_add(&StaticPingOmahaTestStart, this); |
| 347 | g_main_loop_run(loop_); |
| 348 | g_main_loop_unref(loop_); |
| 349 | loop_ = NULL; |
| 350 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status()); |
| 351 | EXPECT_EQ(true, scheduler.scheduled_); |
| 352 | } |
| 353 | |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 354 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) { |
| 355 | ActionMock action; |
| 356 | const ActionExitCode kCode = kActionCodeDownloadTransferError; |
| 357 | attempter_.CreatePendingErrorEvent(&action, kCode); |
| 358 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 359 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 360 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
| 361 | EXPECT_EQ(kCode, attempter_.error_event_->error_code); |
| 362 | } |
| 363 | |
| 364 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) { |
| 365 | OmahaResponseHandlerAction *response_action = |
| 366 | new OmahaResponseHandlerAction(&prefs_); |
| 367 | response_action->install_plan_.is_resume = true; |
| 368 | attempter_.response_handler_action_.reset(response_action); |
| 369 | ActionMock action; |
| 370 | const ActionExitCode kCode = kActionCodeInstallDeviceOpenError; |
| 371 | attempter_.CreatePendingErrorEvent(&action, kCode); |
| 372 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 373 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 374 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
| 375 | EXPECT_EQ(kCode | kActionCodeResumedFlag, |
| 376 | attempter_.error_event_->error_code); |
| 377 | } |
| 378 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 379 | TEST_F(UpdateAttempterTest, ReadTrackFromPolicy) { |
| 380 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 381 | g_idle_add(&StaticReadTrackFromPolicyTestStart, this); |
| 382 | g_main_loop_run(loop_); |
| 383 | g_main_loop_unref(loop_); |
| 384 | loop_ = NULL; |
| 385 | } |
| 386 | |
| 387 | void UpdateAttempterTest::ReadTrackFromPolicyTestStart() { |
| 388 | // Tests that the update track (aka release channel) is properly fetched |
| 389 | // from the device policy. |
| 390 | |
| 391 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 392 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 393 | |
| 394 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 395 | |
| 396 | EXPECT_CALL(*device_policy, GetReleaseChannel(_)) |
| 397 | .WillRepeatedly(DoAll( |
| 398 | SetArgumentPointee<0>(std::string("canary-channel")), |
| 399 | Return(true))); |
| 400 | |
| 401 | attempter_.Update("", "", false, false); |
| 402 | EXPECT_EQ("canary-channel", attempter_.omaha_request_params_.app_track); |
| 403 | |
| 404 | g_idle_add(&StaticQuitMainLoop, this); |
| 405 | } |
| 406 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 407 | } // namespace chromeos_update_engine |