Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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" |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 16 | #include "update_engine/prefs.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 17 | #include "update_engine/prefs_mock.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 18 | #include "update_engine/test_utils.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 19 | #include "update_engine/update_attempter.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 20 | #include "update_engine/update_check_scheduler.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 21 | |
| 22 | using std::string; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 23 | using testing::_; |
| 24 | using testing::DoAll; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 25 | using testing::InSequence; |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 26 | using testing::Ne; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 27 | using testing::NiceMock; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 28 | using testing::Property; |
| 29 | using testing::Return; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 30 | using testing::SetArgumentPointee; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 31 | |
| 32 | namespace chromeos_update_engine { |
| 33 | |
| 34 | // 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] | 35 | // 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] | 36 | // methods. |
| 37 | class UpdateAttempterUnderTest : public UpdateAttempter { |
| 38 | public: |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 39 | explicit UpdateAttempterUnderTest(MockDbusGlib* dbus) |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 40 | : UpdateAttempter(NULL, NULL, dbus, NULL) {} |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class UpdateAttempterTest : public ::testing::Test { |
| 44 | protected: |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 45 | UpdateAttempterTest() : attempter_(&dbus_), loop_(NULL) {} |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 46 | virtual void SetUp() { |
| 47 | EXPECT_EQ(NULL, attempter_.dbus_service_); |
| 48 | EXPECT_EQ(NULL, attempter_.prefs_); |
| 49 | EXPECT_EQ(NULL, attempter_.metrics_lib_); |
| 50 | EXPECT_EQ(NULL, attempter_.update_check_scheduler_); |
| 51 | EXPECT_EQ(0, attempter_.http_response_code_); |
| 52 | EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_); |
| 53 | EXPECT_EQ(NULL, attempter_.manage_priority_source_); |
| 54 | EXPECT_FALSE(attempter_.download_active_); |
| 55 | EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_); |
| 56 | EXPECT_EQ(0.0, attempter_.download_progress_); |
| 57 | EXPECT_EQ(0, attempter_.last_checked_time_); |
| 58 | EXPECT_EQ("0.0.0.0", attempter_.new_version_); |
| 59 | EXPECT_EQ(0, attempter_.new_size_); |
| 60 | processor_ = new ActionProcessorMock(); |
| 61 | attempter_.processor_.reset(processor_); // Transfers ownership. |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 62 | attempter_.prefs_ = &prefs_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 65 | void QuitMainLoop(); |
| 66 | static gboolean StaticQuitMainLoop(gpointer data); |
| 67 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 68 | void UpdateTestStart(); |
| 69 | void UpdateTestVerify(); |
| 70 | static gboolean StaticUpdateTestStart(gpointer data); |
| 71 | static gboolean StaticUpdateTestVerify(gpointer data); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 72 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 73 | void PingOmahaTestStart(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 74 | static gboolean StaticPingOmahaTestStart(gpointer data); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 75 | |
| 76 | void ReadTrackFromPolicyTestStart(); |
| 77 | static gboolean StaticReadTrackFromPolicyTestStart(gpointer data); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 78 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 79 | void ReadUpdateDisabledFromPolicyTestStart(); |
| 80 | static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data); |
| 81 | |
| 82 | void ReadTargetVersionPrefixFromPolicyTestStart(); |
| 83 | static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart( |
| 84 | gpointer data); |
| 85 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 86 | void ReadScatterFactorFromPolicyTestStart(); |
| 87 | static gboolean StaticReadScatterFactorFromPolicyTestStart( |
| 88 | gpointer data); |
| 89 | |
| 90 | void DecrementUpdateCheckCountTestStart(); |
| 91 | static gboolean StaticDecrementUpdateCheckCountTestStart( |
| 92 | gpointer data); |
| 93 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 94 | MockDbusGlib dbus_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 95 | UpdateAttempterUnderTest attempter_; |
| 96 | ActionProcessorMock* processor_; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 97 | NiceMock<PrefsMock> prefs_; |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 98 | GMainLoop* loop_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 101 | TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) { |
| 102 | scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL)); |
| 103 | fetcher->FailTransfer(503); // Sets the HTTP response code. |
| 104 | DownloadAction action(&prefs_, fetcher.release()); |
| 105 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0); |
| 106 | attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess); |
| 107 | EXPECT_EQ(503, attempter_.http_response_code()); |
| 108 | EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status()); |
| 109 | ASSERT_TRUE(attempter_.error_event_.get() == NULL); |
| 110 | } |
| 111 | |
| 112 | TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) { |
| 113 | ActionMock action; |
| 114 | EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock")); |
| 115 | attempter_.status_ = UPDATE_STATUS_DOWNLOADING; |
| 116 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 117 | .WillOnce(Return(false)); |
| 118 | attempter_.ActionCompleted(NULL, &action, kActionCodeError); |
| 119 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 120 | } |
| 121 | |
| 122 | TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) { |
| 123 | scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL)); |
| 124 | fetcher->FailTransfer(500); // Sets the HTTP response code. |
| 125 | OmahaRequestParams params; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 126 | OmahaRequestAction action(&prefs_, ¶ms, NULL, fetcher.release(), false); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 127 | ObjectCollectorAction<OmahaResponse> collector_action; |
| 128 | BondActions(&action, &collector_action); |
| 129 | OmahaResponse response; |
| 130 | response.poll_interval = 234; |
| 131 | action.SetOutputObject(response); |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 132 | UpdateCheckScheduler scheduler(&attempter_, NULL); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 133 | attempter_.set_update_check_scheduler(&scheduler); |
| 134 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0); |
| 135 | attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess); |
| 136 | EXPECT_EQ(500, attempter_.http_response_code()); |
| 137 | EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status()); |
| 138 | EXPECT_EQ(234, scheduler.poll_interval()); |
| 139 | ASSERT_TRUE(attempter_.error_event_.get() == NULL); |
| 140 | } |
| 141 | |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 142 | TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 143 | extern const char* kUpdateCompletedMarker; |
| 144 | const FilePath kMarker(kUpdateCompletedMarker); |
| 145 | EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0)); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 146 | MockDbusGlib dbus; |
| 147 | UpdateAttempterUnderTest attempter(&dbus); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 148 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status()); |
| 149 | EXPECT_TRUE(file_util::Delete(kMarker, false)); |
| 150 | } |
| 151 | |
| 152 | TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) { |
| 153 | extern ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 154 | ActionExitCode code); |
| 155 | EXPECT_EQ(kActionCodeSuccess, |
| 156 | GetErrorCodeForAction(NULL, kActionCodeSuccess)); |
| 157 | |
| 158 | OmahaRequestParams params; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 159 | OmahaRequestAction omaha_request_action(NULL, ¶ms, NULL, NULL, false); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 160 | EXPECT_EQ(kActionCodeOmahaRequestError, |
| 161 | GetErrorCodeForAction(&omaha_request_action, kActionCodeError)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 162 | OmahaResponseHandlerAction omaha_response_handler_action(&prefs_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 163 | EXPECT_EQ(kActionCodeOmahaResponseHandlerError, |
| 164 | GetErrorCodeForAction(&omaha_response_handler_action, |
| 165 | kActionCodeError)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 166 | FilesystemCopierAction filesystem_copier_action(false, false); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 167 | EXPECT_EQ(kActionCodeFilesystemCopierError, |
| 168 | GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 169 | PostinstallRunnerAction postinstall_runner_action; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 170 | EXPECT_EQ(kActionCodePostinstallRunnerError, |
| 171 | GetErrorCodeForAction(&postinstall_runner_action, |
| 172 | kActionCodeError)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 173 | ActionMock action_mock; |
| 174 | EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock")); |
| 175 | EXPECT_EQ(kActionCodeError, |
| 176 | GetErrorCodeForAction(&action_mock, kActionCodeError)); |
| 177 | } |
| 178 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 179 | TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) { |
| 180 | attempter_.omaha_request_params_.delta_okay = true; |
| 181 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 182 | .WillOnce(Return(false)); |
| 183 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 184 | EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); |
| 185 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 186 | .WillOnce(DoAll( |
| 187 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1), |
| 188 | Return(true))); |
| 189 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 190 | EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); |
| 191 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 192 | .WillOnce(DoAll( |
| 193 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 194 | Return(true))); |
| 195 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 196 | EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); |
| 197 | EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0); |
| 198 | attempter_.DisableDeltaUpdateIfNeeded(); |
| 199 | EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); |
| 200 | } |
| 201 | |
| 202 | TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) { |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 203 | EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
| 204 | .WillOnce(Return(false)) |
| 205 | .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true))) |
| 206 | .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true))) |
| 207 | .WillOnce(DoAll( |
| 208 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 209 | Return(true))); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 210 | EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _)) |
| 211 | .WillRepeatedly(Return(true)); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 212 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2); |
| 213 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1); |
| 214 | EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, |
| 215 | UpdateAttempter::kMaxDeltaUpdateFailures + 1)) |
| 216 | .Times(1); |
| 217 | for (int i = 0; i < 4; i ++) |
| 218 | attempter_.MarkDeltaUpdateFailure(); |
| 219 | } |
| 220 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 221 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) { |
| 222 | EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0); |
| 223 | EXPECT_CALL(*processor_, StartProcessing()).Times(0); |
| 224 | attempter_.ScheduleErrorEventAction(); |
| 225 | } |
| 226 | |
| 227 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) { |
| 228 | EXPECT_CALL(*processor_, |
| 229 | EnqueueAction(Property(&AbstractAction::Type, |
| 230 | OmahaRequestAction::StaticType()))) |
| 231 | .Times(1); |
| 232 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 233 | attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 234 | OmahaEvent::kResultError, |
| 235 | kActionCodeError)); |
| 236 | attempter_.ScheduleErrorEventAction(); |
| 237 | EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status()); |
| 238 | } |
| 239 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 240 | TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) { |
| 241 | extern const char* UpdateStatusToString(UpdateStatus); |
| 242 | EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE)); |
| 243 | EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE", |
| 244 | UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE)); |
| 245 | EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE", |
| 246 | UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE)); |
| 247 | EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING", |
| 248 | UpdateStatusToString(UPDATE_STATUS_DOWNLOADING)); |
| 249 | EXPECT_STREQ("UPDATE_STATUS_VERIFYING", |
| 250 | UpdateStatusToString(UPDATE_STATUS_VERIFYING)); |
| 251 | EXPECT_STREQ("UPDATE_STATUS_FINALIZING", |
| 252 | UpdateStatusToString(UPDATE_STATUS_FINALIZING)); |
| 253 | EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT", |
| 254 | UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT)); |
| 255 | EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT", |
| 256 | UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT)); |
| 257 | EXPECT_STREQ("unknown status", |
| 258 | UpdateStatusToString(static_cast<UpdateStatus>(-1))); |
| 259 | } |
| 260 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 261 | void UpdateAttempterTest::QuitMainLoop() { |
| 262 | g_main_loop_quit(loop_); |
| 263 | } |
| 264 | |
| 265 | gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) { |
| 266 | reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop(); |
| 267 | return FALSE; |
| 268 | } |
| 269 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 270 | gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) { |
| 271 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart(); |
| 272 | return FALSE; |
| 273 | } |
| 274 | |
| 275 | gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) { |
| 276 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify(); |
| 277 | return FALSE; |
| 278 | } |
| 279 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 280 | gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) { |
| 281 | reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart(); |
| 282 | return FALSE; |
| 283 | } |
| 284 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 285 | gboolean UpdateAttempterTest::StaticReadTrackFromPolicyTestStart( |
| 286 | gpointer data) { |
| 287 | reinterpret_cast<UpdateAttempterTest*>(data)->ReadTrackFromPolicyTestStart(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 288 | return FALSE; |
| 289 | } |
| 290 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 291 | gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart( |
| 292 | gpointer data) { |
| 293 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 294 | ua_test->ReadUpdateDisabledFromPolicyTestStart(); |
| 295 | return FALSE; |
| 296 | } |
| 297 | |
| 298 | gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart( |
| 299 | gpointer data) { |
| 300 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 301 | ua_test->ReadTargetVersionPrefixFromPolicyTestStart(); |
| 302 | return FALSE; |
| 303 | } |
| 304 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 305 | gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart( |
| 306 | gpointer data) { |
| 307 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 308 | ua_test->ReadScatterFactorFromPolicyTestStart(); |
| 309 | return FALSE; |
| 310 | } |
| 311 | |
| 312 | gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart( |
| 313 | gpointer data) { |
| 314 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 315 | ua_test->DecrementUpdateCheckCountTestStart(); |
| 316 | return FALSE; |
| 317 | } |
| 318 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 319 | namespace { |
| 320 | const string kActionTypes[] = { |
| 321 | OmahaRequestAction::StaticType(), |
| 322 | OmahaResponseHandlerAction::StaticType(), |
| 323 | FilesystemCopierAction::StaticType(), |
| 324 | FilesystemCopierAction::StaticType(), |
| 325 | OmahaRequestAction::StaticType(), |
| 326 | DownloadAction::StaticType(), |
| 327 | OmahaRequestAction::StaticType(), |
| 328 | FilesystemCopierAction::StaticType(), |
| 329 | FilesystemCopierAction::StaticType(), |
| 330 | PostinstallRunnerAction::StaticType(), |
| 331 | OmahaRequestAction::StaticType() |
| 332 | }; |
| 333 | } // namespace {} |
| 334 | |
| 335 | void UpdateAttempterTest::UpdateTestStart() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 336 | attempter_.set_http_response_code(200); |
| 337 | InSequence s; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 338 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 339 | EXPECT_CALL(*processor_, |
| 340 | EnqueueAction(Property(&AbstractAction::Type, |
| 341 | kActionTypes[i]))).Times(1); |
| 342 | } |
| 343 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 344 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 345 | attempter_.Update("", "", false, false, false); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 346 | g_idle_add(&StaticUpdateTestVerify, this); |
| 347 | } |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 348 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 349 | void UpdateAttempterTest::UpdateTestVerify() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 350 | EXPECT_EQ(0, attempter_.http_response_code()); |
| 351 | EXPECT_EQ(&attempter_, processor_->delegate()); |
| 352 | EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size()); |
| 353 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 354 | EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type()); |
| 355 | } |
| 356 | EXPECT_EQ(attempter_.response_handler_action_.get(), |
| 357 | attempter_.actions_[1].get()); |
| 358 | DownloadAction* download_action = |
| 359 | dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); |
| 360 | ASSERT_TRUE(download_action != NULL); |
| 361 | EXPECT_EQ(&attempter_, download_action->delegate()); |
| 362 | EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 363 | g_main_loop_quit(loop_); |
| 364 | } |
| 365 | |
| 366 | TEST_F(UpdateAttempterTest, UpdateTest) { |
| 367 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 368 | g_idle_add(&StaticUpdateTestStart, this); |
| 369 | g_main_loop_run(loop_); |
| 370 | g_main_loop_unref(loop_); |
| 371 | loop_ = NULL; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 374 | void UpdateAttempterTest::PingOmahaTestStart() { |
| 375 | EXPECT_CALL(*processor_, |
| 376 | EnqueueAction(Property(&AbstractAction::Type, |
| 377 | OmahaRequestAction::StaticType()))) |
| 378 | .Times(1); |
| 379 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 380 | attempter_.PingOmaha(); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 381 | g_idle_add(&StaticQuitMainLoop, this); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | TEST_F(UpdateAttempterTest, PingOmahaTest) { |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 385 | UpdateCheckScheduler scheduler(&attempter_, NULL); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 386 | scheduler.enabled_ = true; |
Andrew de los Reyes | e05fc28 | 2011-06-02 09:50:08 -0700 | [diff] [blame] | 387 | EXPECT_FALSE(scheduler.scheduled_); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 388 | attempter_.set_update_check_scheduler(&scheduler); |
| 389 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 390 | g_idle_add(&StaticPingOmahaTestStart, this); |
| 391 | g_main_loop_run(loop_); |
| 392 | g_main_loop_unref(loop_); |
| 393 | loop_ = NULL; |
| 394 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status()); |
| 395 | EXPECT_EQ(true, scheduler.scheduled_); |
| 396 | } |
| 397 | |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 398 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) { |
| 399 | ActionMock action; |
| 400 | const ActionExitCode kCode = kActionCodeDownloadTransferError; |
| 401 | attempter_.CreatePendingErrorEvent(&action, kCode); |
| 402 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 403 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 404 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
| 405 | EXPECT_EQ(kCode, attempter_.error_event_->error_code); |
| 406 | } |
| 407 | |
| 408 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) { |
| 409 | OmahaResponseHandlerAction *response_action = |
| 410 | new OmahaResponseHandlerAction(&prefs_); |
| 411 | response_action->install_plan_.is_resume = true; |
| 412 | attempter_.response_handler_action_.reset(response_action); |
| 413 | ActionMock action; |
| 414 | const ActionExitCode kCode = kActionCodeInstallDeviceOpenError; |
| 415 | attempter_.CreatePendingErrorEvent(&action, kCode); |
| 416 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 417 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 418 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
| 419 | EXPECT_EQ(kCode | kActionCodeResumedFlag, |
| 420 | attempter_.error_event_->error_code); |
| 421 | } |
| 422 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 423 | TEST_F(UpdateAttempterTest, ReadTrackFromPolicy) { |
| 424 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 425 | g_idle_add(&StaticReadTrackFromPolicyTestStart, this); |
| 426 | g_main_loop_run(loop_); |
| 427 | g_main_loop_unref(loop_); |
| 428 | loop_ = NULL; |
| 429 | } |
| 430 | |
| 431 | void UpdateAttempterTest::ReadTrackFromPolicyTestStart() { |
| 432 | // Tests that the update track (aka release channel) is properly fetched |
| 433 | // from the device policy. |
| 434 | |
| 435 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 436 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 437 | |
| 438 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 439 | |
| 440 | EXPECT_CALL(*device_policy, GetReleaseChannel(_)) |
| 441 | .WillRepeatedly(DoAll( |
| 442 | SetArgumentPointee<0>(std::string("canary-channel")), |
| 443 | Return(true))); |
| 444 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 445 | attempter_.Update("", "", false, false, false); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 446 | EXPECT_EQ("canary-channel", attempter_.omaha_request_params_.app_track); |
| 447 | |
| 448 | g_idle_add(&StaticQuitMainLoop, this); |
| 449 | } |
| 450 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 451 | TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) { |
| 452 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 453 | g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this); |
| 454 | g_main_loop_run(loop_); |
| 455 | g_main_loop_unref(loop_); |
| 456 | loop_ = NULL; |
| 457 | } |
| 458 | |
| 459 | void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() { |
| 460 | // Tests that the update_disbled flag is properly fetched |
| 461 | // from the device policy. |
| 462 | |
| 463 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 464 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 465 | |
| 466 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 467 | |
| 468 | EXPECT_CALL(*device_policy, GetUpdateDisabled(_)) |
| 469 | .WillRepeatedly(DoAll( |
| 470 | SetArgumentPointee<0>(true), |
| 471 | Return(true))); |
| 472 | |
| 473 | attempter_.Update("", "", false, false, false); |
| 474 | EXPECT_TRUE(attempter_.omaha_request_params_.update_disabled); |
| 475 | |
| 476 | g_idle_add(&StaticQuitMainLoop, this); |
| 477 | } |
| 478 | |
| 479 | TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) { |
| 480 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 481 | g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this); |
| 482 | g_main_loop_run(loop_); |
| 483 | g_main_loop_unref(loop_); |
| 484 | loop_ = NULL; |
| 485 | } |
| 486 | |
| 487 | void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() { |
| 488 | // Tests that the target_version_prefix value is properly fetched |
| 489 | // from the device policy. |
| 490 | |
| 491 | const std::string target_version_prefix = "1412."; |
| 492 | |
| 493 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 494 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 495 | |
| 496 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 497 | |
| 498 | EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_)) |
| 499 | .WillRepeatedly(DoAll( |
| 500 | SetArgumentPointee<0>(target_version_prefix), |
| 501 | Return(true))); |
| 502 | |
| 503 | attempter_.Update("", "", false, false, false); |
| 504 | EXPECT_EQ(target_version_prefix.c_str(), |
| 505 | attempter_.omaha_request_params_.target_version_prefix); |
| 506 | |
| 507 | g_idle_add(&StaticQuitMainLoop, this); |
| 508 | } |
| 509 | |
| 510 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 511 | TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) { |
| 512 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 513 | g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this); |
| 514 | g_main_loop_run(loop_); |
| 515 | g_main_loop_unref(loop_); |
| 516 | loop_ = NULL; |
| 517 | } |
| 518 | |
| 519 | // Tests that the scatter_factor_in_seconds value is properly fetched |
| 520 | // from the device policy. |
| 521 | void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() { |
| 522 | int64 scatter_factor_in_seconds = 36000; |
| 523 | |
| 524 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 525 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 526 | |
| 527 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 528 | |
| 529 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
| 530 | .WillRepeatedly(DoAll( |
| 531 | SetArgumentPointee<0>(scatter_factor_in_seconds), |
| 532 | Return(true))); |
| 533 | |
| 534 | attempter_.Update("", "", false, false, false); |
| 535 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 536 | |
| 537 | g_idle_add(&StaticQuitMainLoop, this); |
| 538 | } |
| 539 | |
| 540 | TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) { |
| 541 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 542 | g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this); |
| 543 | g_main_loop_run(loop_); |
| 544 | g_main_loop_unref(loop_); |
| 545 | loop_ = NULL; |
| 546 | } |
| 547 | |
| 548 | void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() { |
| 549 | // Tests that the scatter_factor_in_seconds value is properly fetched |
| 550 | // from the device policy and is decremented if value > 0. |
| 551 | int64 initial_value = 5; |
| 552 | Prefs prefs; |
| 553 | attempter_.prefs_ = &prefs; |
| 554 | |
| 555 | string prefs_dir; |
| 556 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 557 | &prefs_dir)); |
| 558 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 559 | |
| 560 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 561 | << "Failed to initialize preferences."; |
| 562 | EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value)); |
| 563 | |
| 564 | int64 scatter_factor_in_seconds = 10; |
| 565 | |
| 566 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 567 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 568 | |
| 569 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 570 | |
| 571 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
| 572 | .WillRepeatedly(DoAll( |
| 573 | SetArgumentPointee<0>(scatter_factor_in_seconds), |
| 574 | Return(true))); |
| 575 | |
| 576 | attempter_.Update("", "", false, false, false); |
| 577 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 578 | |
| 579 | // Make sure the file still exists. |
| 580 | EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount)); |
| 581 | |
| 582 | int64 new_value; |
| 583 | EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value)); |
| 584 | EXPECT_EQ(initial_value - 1, new_value); |
| 585 | |
| 586 | EXPECT_TRUE(attempter_.omaha_request_params_.update_check_count_wait_enabled); |
| 587 | |
| 588 | // However, if the count is already 0, it's not decremented. Test that. |
| 589 | initial_value = 0; |
| 590 | EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value)); |
| 591 | attempter_.Update("", "", false, false, false); |
| 592 | EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount)); |
| 593 | EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value)); |
| 594 | EXPECT_EQ(initial_value, new_value); |
| 595 | |
| 596 | g_idle_add(&StaticQuitMainLoop, this); |
| 597 | } |
| 598 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 599 | } // namespace chromeos_update_engine |