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