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