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