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