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) |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 43 | : UpdateAttempter(mock_system_state, dbus) {} |
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_); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 67 | processor_ = new NiceMock<ActionProcessorMock>(); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 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 | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 105 | NiceMock<MockSystemState> mock_system_state_; |
| 106 | NiceMock<MockDbusGlib> dbus_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 107 | UpdateAttempterUnderTest attempter_; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 108 | NiceMock<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 | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 110 | NiceMock<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); |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 146 | UpdateCheckScheduler scheduler(&attempter_, &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 | ActionExitCode err = kActionCodeError; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 256 | EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err)); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 257 | attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 258 | OmahaEvent::kResultError, |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 259 | err)); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 260 | attempter_.ScheduleErrorEventAction(); |
| 261 | EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status()); |
| 262 | } |
| 263 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 264 | TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) { |
| 265 | extern const char* UpdateStatusToString(UpdateStatus); |
| 266 | EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE)); |
| 267 | EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE", |
| 268 | UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE)); |
| 269 | EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE", |
| 270 | UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE)); |
| 271 | EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING", |
| 272 | UpdateStatusToString(UPDATE_STATUS_DOWNLOADING)); |
| 273 | EXPECT_STREQ("UPDATE_STATUS_VERIFYING", |
| 274 | UpdateStatusToString(UPDATE_STATUS_VERIFYING)); |
| 275 | EXPECT_STREQ("UPDATE_STATUS_FINALIZING", |
| 276 | UpdateStatusToString(UPDATE_STATUS_FINALIZING)); |
| 277 | EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT", |
| 278 | UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT)); |
| 279 | EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT", |
| 280 | UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT)); |
| 281 | EXPECT_STREQ("unknown status", |
| 282 | UpdateStatusToString(static_cast<UpdateStatus>(-1))); |
| 283 | } |
| 284 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 285 | void UpdateAttempterTest::QuitMainLoop() { |
| 286 | g_main_loop_quit(loop_); |
| 287 | } |
| 288 | |
| 289 | gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) { |
| 290 | reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop(); |
| 291 | return FALSE; |
| 292 | } |
| 293 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 294 | gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) { |
| 295 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart(); |
| 296 | return FALSE; |
| 297 | } |
| 298 | |
| 299 | gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) { |
| 300 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify(); |
| 301 | return FALSE; |
| 302 | } |
| 303 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 304 | gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) { |
| 305 | reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart(); |
| 306 | return FALSE; |
| 307 | } |
| 308 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 309 | gboolean UpdateAttempterTest::StaticReadTrackFromPolicyTestStart( |
| 310 | gpointer data) { |
| 311 | reinterpret_cast<UpdateAttempterTest*>(data)->ReadTrackFromPolicyTestStart(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 312 | return FALSE; |
| 313 | } |
| 314 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 315 | gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart( |
| 316 | gpointer data) { |
| 317 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 318 | ua_test->ReadUpdateDisabledFromPolicyTestStart(); |
| 319 | return FALSE; |
| 320 | } |
| 321 | |
| 322 | gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart( |
| 323 | gpointer data) { |
| 324 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 325 | ua_test->ReadTargetVersionPrefixFromPolicyTestStart(); |
| 326 | return FALSE; |
| 327 | } |
| 328 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 329 | gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart( |
| 330 | gpointer data) { |
| 331 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 332 | ua_test->ReadScatterFactorFromPolicyTestStart(); |
| 333 | return FALSE; |
| 334 | } |
| 335 | |
| 336 | gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart( |
| 337 | gpointer data) { |
| 338 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 339 | ua_test->DecrementUpdateCheckCountTestStart(); |
| 340 | return FALSE; |
| 341 | } |
| 342 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 343 | gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart( |
| 344 | gpointer data) { |
| 345 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 346 | ua_test->NoScatteringDoneDuringManualUpdateTestStart(); |
| 347 | return FALSE; |
| 348 | } |
| 349 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 350 | namespace { |
| 351 | const string kActionTypes[] = { |
| 352 | OmahaRequestAction::StaticType(), |
| 353 | OmahaResponseHandlerAction::StaticType(), |
| 354 | FilesystemCopierAction::StaticType(), |
| 355 | FilesystemCopierAction::StaticType(), |
| 356 | OmahaRequestAction::StaticType(), |
| 357 | DownloadAction::StaticType(), |
| 358 | OmahaRequestAction::StaticType(), |
| 359 | FilesystemCopierAction::StaticType(), |
| 360 | FilesystemCopierAction::StaticType(), |
| 361 | PostinstallRunnerAction::StaticType(), |
| 362 | OmahaRequestAction::StaticType() |
| 363 | }; |
| 364 | } // namespace {} |
| 365 | |
| 366 | void UpdateAttempterTest::UpdateTestStart() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 367 | attempter_.set_http_response_code(200); |
| 368 | InSequence s; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 369 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 370 | EXPECT_CALL(*processor_, |
| 371 | EnqueueAction(Property(&AbstractAction::Type, |
| 372 | kActionTypes[i]))).Times(1); |
| 373 | } |
| 374 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 375 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 376 | attempter_.Update("", "", false, false, false); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 377 | g_idle_add(&StaticUpdateTestVerify, this); |
| 378 | } |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 379 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 380 | void UpdateAttempterTest::UpdateTestVerify() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 381 | EXPECT_EQ(0, attempter_.http_response_code()); |
| 382 | EXPECT_EQ(&attempter_, processor_->delegate()); |
| 383 | EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size()); |
| 384 | for (size_t i = 0; i < arraysize(kActionTypes); ++i) { |
| 385 | EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type()); |
| 386 | } |
| 387 | EXPECT_EQ(attempter_.response_handler_action_.get(), |
| 388 | attempter_.actions_[1].get()); |
| 389 | DownloadAction* download_action = |
| 390 | dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); |
| 391 | ASSERT_TRUE(download_action != NULL); |
| 392 | EXPECT_EQ(&attempter_, download_action->delegate()); |
| 393 | EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 394 | g_main_loop_quit(loop_); |
| 395 | } |
| 396 | |
| 397 | TEST_F(UpdateAttempterTest, UpdateTest) { |
| 398 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 399 | g_idle_add(&StaticUpdateTestStart, this); |
| 400 | g_main_loop_run(loop_); |
| 401 | g_main_loop_unref(loop_); |
| 402 | loop_ = NULL; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 405 | void UpdateAttempterTest::PingOmahaTestStart() { |
| 406 | EXPECT_CALL(*processor_, |
| 407 | EnqueueAction(Property(&AbstractAction::Type, |
| 408 | OmahaRequestAction::StaticType()))) |
| 409 | .Times(1); |
| 410 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 411 | attempter_.PingOmaha(); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 412 | g_idle_add(&StaticQuitMainLoop, this); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | TEST_F(UpdateAttempterTest, PingOmahaTest) { |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 416 | UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 417 | scheduler.enabled_ = true; |
Andrew de los Reyes | e05fc28 | 2011-06-02 09:50:08 -0700 | [diff] [blame] | 418 | EXPECT_FALSE(scheduler.scheduled_); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 419 | attempter_.set_update_check_scheduler(&scheduler); |
| 420 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 421 | g_idle_add(&StaticPingOmahaTestStart, this); |
| 422 | g_main_loop_run(loop_); |
| 423 | g_main_loop_unref(loop_); |
| 424 | loop_ = NULL; |
| 425 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status()); |
| 426 | EXPECT_EQ(true, scheduler.scheduled_); |
| 427 | } |
| 428 | |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 429 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) { |
| 430 | ActionMock action; |
| 431 | const ActionExitCode kCode = kActionCodeDownloadTransferError; |
| 432 | attempter_.CreatePendingErrorEvent(&action, kCode); |
| 433 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 434 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 435 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 436 | EXPECT_EQ(kCode | kActionCodeTestOmahaUrlFlag, |
| 437 | attempter_.error_event_->error_code); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) { |
| 441 | OmahaResponseHandlerAction *response_action = |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 442 | new OmahaResponseHandlerAction(&mock_system_state_); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 443 | response_action->install_plan_.is_resume = true; |
| 444 | attempter_.response_handler_action_.reset(response_action); |
| 445 | ActionMock action; |
| 446 | const ActionExitCode kCode = kActionCodeInstallDeviceOpenError; |
| 447 | attempter_.CreatePendingErrorEvent(&action, kCode); |
| 448 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 449 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 450 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 451 | EXPECT_EQ(kCode | kActionCodeResumedFlag | kActionCodeTestOmahaUrlFlag, |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 452 | attempter_.error_event_->error_code); |
| 453 | } |
| 454 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 455 | TEST_F(UpdateAttempterTest, ReadTrackFromPolicy) { |
| 456 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 457 | g_idle_add(&StaticReadTrackFromPolicyTestStart, this); |
| 458 | g_main_loop_run(loop_); |
| 459 | g_main_loop_unref(loop_); |
| 460 | loop_ = NULL; |
| 461 | } |
| 462 | |
| 463 | void UpdateAttempterTest::ReadTrackFromPolicyTestStart() { |
| 464 | // Tests that the update track (aka release channel) is properly fetched |
| 465 | // from the device policy. |
| 466 | |
| 467 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 468 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 469 | |
| 470 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 471 | |
| 472 | EXPECT_CALL(*device_policy, GetReleaseChannel(_)) |
| 473 | .WillRepeatedly(DoAll( |
| 474 | SetArgumentPointee<0>(std::string("canary-channel")), |
| 475 | Return(true))); |
| 476 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 477 | attempter_.Update("", "", false, false, false); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 478 | EXPECT_EQ("canary-channel", attempter_.omaha_request_params_.app_track); |
| 479 | |
| 480 | g_idle_add(&StaticQuitMainLoop, this); |
| 481 | } |
| 482 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 483 | TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) { |
| 484 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 485 | g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this); |
| 486 | g_main_loop_run(loop_); |
| 487 | g_main_loop_unref(loop_); |
| 488 | loop_ = NULL; |
| 489 | } |
| 490 | |
| 491 | void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() { |
| 492 | // Tests that the update_disbled flag is properly fetched |
| 493 | // from the device policy. |
| 494 | |
| 495 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 496 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 497 | |
| 498 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 499 | |
| 500 | EXPECT_CALL(*device_policy, GetUpdateDisabled(_)) |
| 501 | .WillRepeatedly(DoAll( |
| 502 | SetArgumentPointee<0>(true), |
| 503 | Return(true))); |
| 504 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 505 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 506 | EXPECT_TRUE(attempter_.omaha_request_params_.update_disabled); |
| 507 | |
| 508 | g_idle_add(&StaticQuitMainLoop, this); |
| 509 | } |
| 510 | |
| 511 | TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) { |
| 512 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 513 | g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this); |
| 514 | g_main_loop_run(loop_); |
| 515 | g_main_loop_unref(loop_); |
| 516 | loop_ = NULL; |
| 517 | } |
| 518 | |
| 519 | void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() { |
| 520 | // Tests that the target_version_prefix value is properly fetched |
| 521 | // from the device policy. |
| 522 | |
| 523 | const std::string target_version_prefix = "1412."; |
| 524 | |
| 525 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 526 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 527 | |
| 528 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 529 | |
| 530 | EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_)) |
| 531 | .WillRepeatedly(DoAll( |
| 532 | SetArgumentPointee<0>(target_version_prefix), |
| 533 | Return(true))); |
| 534 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 535 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 536 | EXPECT_EQ(target_version_prefix.c_str(), |
| 537 | attempter_.omaha_request_params_.target_version_prefix); |
| 538 | |
| 539 | g_idle_add(&StaticQuitMainLoop, this); |
| 540 | } |
| 541 | |
| 542 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 543 | TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) { |
| 544 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 545 | g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this); |
| 546 | g_main_loop_run(loop_); |
| 547 | g_main_loop_unref(loop_); |
| 548 | loop_ = NULL; |
| 549 | } |
| 550 | |
| 551 | // Tests that the scatter_factor_in_seconds value is properly fetched |
| 552 | // from the device policy. |
| 553 | void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() { |
| 554 | int64 scatter_factor_in_seconds = 36000; |
| 555 | |
| 556 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 557 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 558 | |
| 559 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 560 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 561 | Return(device_policy)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 562 | |
| 563 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
| 564 | .WillRepeatedly(DoAll( |
| 565 | SetArgumentPointee<0>(scatter_factor_in_seconds), |
| 566 | Return(true))); |
| 567 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 568 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 569 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 570 | |
| 571 | g_idle_add(&StaticQuitMainLoop, this); |
| 572 | } |
| 573 | |
| 574 | TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) { |
| 575 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 576 | g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this); |
| 577 | g_main_loop_run(loop_); |
| 578 | g_main_loop_unref(loop_); |
| 579 | loop_ = NULL; |
| 580 | } |
| 581 | |
| 582 | void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() { |
| 583 | // Tests that the scatter_factor_in_seconds value is properly fetched |
| 584 | // from the device policy and is decremented if value > 0. |
| 585 | int64 initial_value = 5; |
| 586 | Prefs prefs; |
| 587 | attempter_.prefs_ = &prefs; |
| 588 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 589 | EXPECT_CALL(mock_system_state_, |
| 590 | IsOOBEComplete()).WillRepeatedly(Return(true)); |
| 591 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 592 | string prefs_dir; |
| 593 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 594 | &prefs_dir)); |
| 595 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 596 | |
| 597 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 598 | << "Failed to initialize preferences."; |
| 599 | EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value)); |
| 600 | |
| 601 | int64 scatter_factor_in_seconds = 10; |
| 602 | |
| 603 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 604 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 605 | |
| 606 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 607 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 608 | Return(device_policy)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 609 | |
| 610 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
| 611 | .WillRepeatedly(DoAll( |
| 612 | SetArgumentPointee<0>(scatter_factor_in_seconds), |
| 613 | Return(true))); |
| 614 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 615 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 616 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 617 | |
| 618 | // Make sure the file still exists. |
| 619 | EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount)); |
| 620 | |
| 621 | int64 new_value; |
| 622 | EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value)); |
| 623 | EXPECT_EQ(initial_value - 1, new_value); |
| 624 | |
| 625 | EXPECT_TRUE(attempter_.omaha_request_params_.update_check_count_wait_enabled); |
| 626 | |
| 627 | // However, if the count is already 0, it's not decremented. Test that. |
| 628 | initial_value = 0; |
| 629 | EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value)); |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 630 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 631 | EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount)); |
| 632 | EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value)); |
| 633 | EXPECT_EQ(initial_value, new_value); |
| 634 | |
| 635 | g_idle_add(&StaticQuitMainLoop, this); |
| 636 | } |
| 637 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 638 | TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) { |
| 639 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 640 | g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this); |
| 641 | g_main_loop_run(loop_); |
| 642 | g_main_loop_unref(loop_); |
| 643 | loop_ = NULL; |
| 644 | } |
| 645 | |
| 646 | void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() { |
| 647 | // Tests that no scattering logic is enabled if the update check |
| 648 | // is manually done (as opposed to a scheduled update check) |
| 649 | int64 initial_value = 8; |
| 650 | Prefs prefs; |
| 651 | attempter_.prefs_ = &prefs; |
| 652 | |
| 653 | EXPECT_CALL(mock_system_state_, |
| 654 | IsOOBEComplete()).WillRepeatedly(Return(true)); |
| 655 | |
| 656 | string prefs_dir; |
| 657 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 658 | &prefs_dir)); |
| 659 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 660 | |
| 661 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 662 | << "Failed to initialize preferences."; |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 663 | EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value)); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 664 | EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value)); |
| 665 | |
| 666 | // make sure scatter_factor is non-zero as scattering is disabled |
| 667 | // otherwise. |
| 668 | int64 scatter_factor_in_seconds = 50; |
| 669 | |
| 670 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 671 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 672 | |
| 673 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 674 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 675 | Return(device_policy)); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 676 | |
| 677 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
| 678 | .WillRepeatedly(DoAll( |
| 679 | SetArgumentPointee<0>(scatter_factor_in_seconds), |
| 680 | Return(true))); |
| 681 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame^] | 682 | // Trigger an interactive check so we can test that scattering is disabled. |
| 683 | attempter_.Update("", "", false, true, false); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 684 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 685 | |
| 686 | // Make sure scattering is disabled for manual (i.e. user initiated) update |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 687 | // checks and all artifacts are removed. |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 688 | EXPECT_FALSE(attempter_.omaha_request_params_.wall_clock_based_wait_enabled); |
| 689 | EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod)); |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 690 | EXPECT_TRUE(attempter_.omaha_request_params_.waiting_period.InSeconds() == 0); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 691 | EXPECT_FALSE(attempter_.omaha_request_params_. |
| 692 | update_check_count_wait_enabled); |
| 693 | EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount)); |
| 694 | |
| 695 | g_idle_add(&StaticQuitMainLoop, this); |
| 696 | } |
| 697 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 698 | } // namespace chromeos_update_engine |