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