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" |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 13 | #include "update_engine/install_plan.h" |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 14 | #include "update_engine/mock_dbus_interface.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 15 | #include "update_engine/mock_http_fetcher.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame^] | 16 | #include "update_engine/mock_p2p_manager.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 17 | #include "update_engine/mock_payload_state.h" |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 18 | #include "update_engine/mock_system_state.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 19 | #include "update_engine/postinstall_runner_action.h" |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 20 | #include "update_engine/prefs.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 21 | #include "update_engine/prefs_mock.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 22 | #include "update_engine/test_utils.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 23 | #include "update_engine/update_attempter.h" |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 24 | #include "update_engine/update_check_scheduler.h" |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 25 | #include "update_engine/utils.h" |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 26 | |
| 27 | using std::string; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 28 | using testing::_; |
| 29 | using testing::DoAll; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 30 | using testing::InSequence; |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 31 | using testing::Ne; |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame] | 32 | using testing::NiceMock; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 33 | using testing::Property; |
| 34 | using testing::Return; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 35 | using testing::SetArgumentPointee; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | // 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] | 40 | // 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] | 41 | // methods. |
| 42 | class UpdateAttempterUnderTest : public UpdateAttempter { |
| 43 | public: |
Gilad Arnold | 70e476e | 2013-07-30 16:01:13 -0700 | [diff] [blame] | 44 | // We always feed an explicit update completed marker name; however, unless |
| 45 | // explicitly specified, we feed an empty string, which causes the |
| 46 | // UpdateAttempter class to ignore / not write the marker file. |
| 47 | UpdateAttempterUnderTest(MockSystemState* mock_system_state, |
| 48 | MockDbusGlib* dbus) |
| 49 | : UpdateAttempter(mock_system_state, dbus, "") {} |
| 50 | |
| 51 | UpdateAttempterUnderTest(MockSystemState* mock_system_state, |
| 52 | MockDbusGlib* dbus, |
| 53 | const string& update_completed_marker) |
| 54 | : UpdateAttempter(mock_system_state, dbus, update_completed_marker) {} |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | class UpdateAttempterTest : public ::testing::Test { |
| 58 | protected: |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 59 | UpdateAttempterTest() |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 60 | : attempter_(&mock_system_state_, &dbus_), |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 61 | mock_connection_manager(&mock_system_state_), |
| 62 | loop_(NULL) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 63 | mock_system_state_.set_connection_manager(&mock_connection_manager); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 64 | } |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 65 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 66 | virtual void SetUp() { |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 67 | CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_)); |
| 68 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 69 | EXPECT_EQ(NULL, attempter_.dbus_service_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 70 | EXPECT_TRUE(attempter_.system_state_ != NULL); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 71 | EXPECT_EQ(NULL, attempter_.update_check_scheduler_); |
| 72 | EXPECT_EQ(0, attempter_.http_response_code_); |
Chris Sosa | 4f8ee27 | 2012-11-30 13:01:54 -0800 | [diff] [blame] | 73 | EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_); |
| 74 | EXPECT_EQ(NULL, attempter_.manage_shares_source_); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 75 | EXPECT_FALSE(attempter_.download_active_); |
| 76 | EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_); |
| 77 | EXPECT_EQ(0.0, attempter_.download_progress_); |
| 78 | EXPECT_EQ(0, attempter_.last_checked_time_); |
| 79 | EXPECT_EQ("0.0.0.0", attempter_.new_version_); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 80 | EXPECT_EQ(0, attempter_.new_payload_size_); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 81 | processor_ = new NiceMock<ActionProcessorMock>(); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 82 | attempter_.processor_.reset(processor_); // Transfers ownership. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 83 | prefs_ = mock_system_state_.mock_prefs(); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 86 | virtual void TearDown() { |
| 87 | utils::RecursiveUnlinkDir(test_dir_); |
| 88 | } |
| 89 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 90 | void QuitMainLoop(); |
| 91 | static gboolean StaticQuitMainLoop(gpointer data); |
| 92 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 93 | void UpdateTestStart(); |
| 94 | void UpdateTestVerify(); |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 95 | void RollbackTestStart(bool enterprise_rollback, bool stable_channel); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 96 | void RollbackTestVerify(); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 97 | static gboolean StaticUpdateTestStart(gpointer data); |
| 98 | static gboolean StaticUpdateTestVerify(gpointer data); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 99 | static gboolean StaticRollbackTestStart(gpointer data); |
| 100 | static gboolean StaticEnterpriseRollbackTestStart(gpointer data); |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 101 | static gboolean StaticStableChannelRollbackTestStart(gpointer data); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 102 | static gboolean StaticRollbackTestVerify(gpointer data); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 103 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 104 | void PingOmahaTestStart(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 105 | static gboolean StaticPingOmahaTestStart(gpointer data); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 106 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 107 | void ReadChannelFromPolicyTestStart(); |
| 108 | static gboolean StaticReadChannelFromPolicyTestStart(gpointer data); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 109 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 110 | void ReadUpdateDisabledFromPolicyTestStart(); |
| 111 | static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data); |
| 112 | |
| 113 | void ReadTargetVersionPrefixFromPolicyTestStart(); |
| 114 | static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart( |
| 115 | gpointer data); |
| 116 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 117 | void ReadScatterFactorFromPolicyTestStart(); |
| 118 | static gboolean StaticReadScatterFactorFromPolicyTestStart( |
| 119 | gpointer data); |
| 120 | |
| 121 | void DecrementUpdateCheckCountTestStart(); |
| 122 | static gboolean StaticDecrementUpdateCheckCountTestStart( |
| 123 | gpointer data); |
| 124 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 125 | void NoScatteringDoneDuringManualUpdateTestStart(); |
| 126 | static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart( |
| 127 | gpointer data); |
| 128 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame^] | 129 | void P2PNotEnabledStart(); |
| 130 | static gboolean StaticP2PNotEnabled(gpointer data); |
| 131 | |
| 132 | void P2PEnabledStart(); |
| 133 | static gboolean StaticP2PEnabled(gpointer data); |
| 134 | |
| 135 | void P2PEnabledInteractiveStart(); |
| 136 | static gboolean StaticP2PEnabledInteractive(gpointer data); |
| 137 | |
| 138 | void P2PEnabledStartingFailsStart(); |
| 139 | static gboolean StaticP2PEnabledStartingFails(gpointer data); |
| 140 | |
| 141 | void P2PEnabledHousekeepingFailsStart(); |
| 142 | static gboolean StaticP2PEnabledHousekeepingFails(gpointer data); |
| 143 | |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 144 | NiceMock<MockSystemState> mock_system_state_; |
| 145 | NiceMock<MockDbusGlib> dbus_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 146 | UpdateAttempterUnderTest attempter_; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 147 | NiceMock<ActionProcessorMock>* processor_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 148 | NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs() |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 149 | NiceMock<MockConnectionManager> mock_connection_manager; |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 150 | GMainLoop* loop_; |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 151 | |
| 152 | string test_dir_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 155 | TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) { |
| 156 | scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL)); |
| 157 | fetcher->FailTransfer(503); // Sets the HTTP response code. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 158 | DownloadAction action(prefs_, NULL, fetcher.release()); |
| 159 | EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 160 | attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 161 | EXPECT_EQ(503, attempter_.http_response_code()); |
| 162 | EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status()); |
| 163 | ASSERT_TRUE(attempter_.error_event_.get() == NULL); |
| 164 | } |
| 165 | |
| 166 | TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) { |
| 167 | ActionMock action; |
| 168 | EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock")); |
| 169 | attempter_.status_ = UPDATE_STATUS_DOWNLOADING; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 170 | EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 171 | .WillOnce(Return(false)); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 172 | attempter_.ActionCompleted(NULL, &action, kErrorCodeError); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 173 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 174 | } |
| 175 | |
| 176 | TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) { |
| 177 | scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL)); |
| 178 | fetcher->FailTransfer(500); // Sets the HTTP response code. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 179 | OmahaRequestAction action(&mock_system_state_, NULL, |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 180 | fetcher.release(), false); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 181 | ObjectCollectorAction<OmahaResponse> collector_action; |
| 182 | BondActions(&action, &collector_action); |
| 183 | OmahaResponse response; |
| 184 | response.poll_interval = 234; |
| 185 | action.SetOutputObject(response); |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 186 | UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 187 | attempter_.set_update_check_scheduler(&scheduler); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 188 | EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 189 | attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 190 | EXPECT_EQ(500, attempter_.http_response_code()); |
| 191 | EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status()); |
| 192 | EXPECT_EQ(234, scheduler.poll_interval()); |
| 193 | ASSERT_TRUE(attempter_.error_event_.get() == NULL); |
| 194 | } |
| 195 | |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 196 | TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) { |
Gilad Arnold | 70e476e | 2013-07-30 16:01:13 -0700 | [diff] [blame] | 197 | string test_update_completed_marker; |
| 198 | CHECK(utils::MakeTempFile( |
| 199 | "/tmp/update_attempter_unittest-update_completed_marker-XXXXXX", |
| 200 | &test_update_completed_marker, NULL)); |
| 201 | ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker); |
| 202 | const FilePath marker(test_update_completed_marker); |
| 203 | EXPECT_EQ(0, file_util::WriteFile(marker, "", 0)); |
| 204 | UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_, |
| 205 | test_update_completed_marker); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 206 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status()); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 210 | extern ErrorCode GetErrorCodeForAction(AbstractAction* action, |
| 211 | ErrorCode code); |
| 212 | EXPECT_EQ(kErrorCodeSuccess, |
| 213 | GetErrorCodeForAction(NULL, kErrorCodeSuccess)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 214 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 215 | MockSystemState mock_system_state; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 216 | OmahaRequestAction omaha_request_action(&mock_system_state, NULL, |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 217 | NULL, false); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 218 | EXPECT_EQ(kErrorCodeOmahaRequestError, |
| 219 | GetErrorCodeForAction(&omaha_request_action, kErrorCodeError)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 220 | OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 221 | EXPECT_EQ(kErrorCodeOmahaResponseHandlerError, |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 222 | GetErrorCodeForAction(&omaha_response_handler_action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 223 | kErrorCodeError)); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 224 | FilesystemCopierAction filesystem_copier_action( |
| 225 | &mock_system_state_, false, false); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 226 | EXPECT_EQ(kErrorCodeFilesystemCopierError, |
| 227 | GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError)); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 228 | PostinstallRunnerAction postinstall_runner_action; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 229 | EXPECT_EQ(kErrorCodePostinstallRunnerError, |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 230 | GetErrorCodeForAction(&postinstall_runner_action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 231 | kErrorCodeError)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 232 | ActionMock action_mock; |
| 233 | EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock")); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 234 | EXPECT_EQ(kErrorCodeError, |
| 235 | GetErrorCodeForAction(&action_mock, kErrorCodeError)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 238 | TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 239 | attempter_.omaha_request_params_->set_delta_okay(true); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 240 | EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 241 | .WillOnce(Return(false)); |
| 242 | attempter_.DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 243 | EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 244 | EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 245 | .WillOnce(DoAll( |
| 246 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1), |
| 247 | Return(true))); |
| 248 | attempter_.DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 249 | EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 250 | EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 251 | .WillOnce(DoAll( |
| 252 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 253 | Return(true))); |
| 254 | attempter_.DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 255 | EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay()); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 256 | EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 257 | attempter_.DisableDeltaUpdateIfNeeded(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 258 | EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay()); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 262 | EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 263 | .WillOnce(Return(false)) |
| 264 | .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true))) |
| 265 | .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true))) |
| 266 | .WillOnce(DoAll( |
| 267 | SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), |
| 268 | Return(true))); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 269 | EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _)) |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 270 | .WillRepeatedly(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 271 | EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2); |
| 272 | EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1); |
| 273 | EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 274 | UpdateAttempter::kMaxDeltaUpdateFailures + 1)) |
| 275 | .Times(1); |
| 276 | for (int i = 0; i < 4; i ++) |
| 277 | attempter_.MarkDeltaUpdateFailure(); |
| 278 | } |
| 279 | |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 280 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) { |
| 281 | EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0); |
| 282 | EXPECT_CALL(*processor_, StartProcessing()).Times(0); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 283 | EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_)) |
| 284 | .Times(0); |
| 285 | OmahaResponse response; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 286 | string url1 = "http://url1"; |
| 287 | response.payload_urls.push_back(url1); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 288 | response.payload_urls.push_back("https://url"); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 289 | EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl()) |
| 290 | .WillRepeatedly(Return(url1)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 291 | mock_system_state_.mock_payload_state()->SetResponse(response); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 292 | attempter_.ScheduleErrorEventAction(); |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 293 | EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl()); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) { |
| 297 | EXPECT_CALL(*processor_, |
| 298 | EnqueueAction(Property(&AbstractAction::Type, |
| 299 | OmahaRequestAction::StaticType()))) |
| 300 | .Times(1); |
| 301 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 302 | ErrorCode err = kErrorCodeError; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 303 | EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err)); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 304 | attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 305 | OmahaEvent::kResultError, |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 306 | err)); |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 307 | attempter_.ScheduleErrorEventAction(); |
| 308 | EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status()); |
| 309 | } |
| 310 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 311 | TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) { |
| 312 | extern const char* UpdateStatusToString(UpdateStatus); |
| 313 | EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE)); |
| 314 | EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE", |
| 315 | UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE)); |
| 316 | EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE", |
| 317 | UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE)); |
| 318 | EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING", |
| 319 | UpdateStatusToString(UPDATE_STATUS_DOWNLOADING)); |
| 320 | EXPECT_STREQ("UPDATE_STATUS_VERIFYING", |
| 321 | UpdateStatusToString(UPDATE_STATUS_VERIFYING)); |
| 322 | EXPECT_STREQ("UPDATE_STATUS_FINALIZING", |
| 323 | UpdateStatusToString(UPDATE_STATUS_FINALIZING)); |
| 324 | EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT", |
| 325 | UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT)); |
| 326 | EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT", |
| 327 | UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT)); |
| 328 | EXPECT_STREQ("unknown status", |
| 329 | UpdateStatusToString(static_cast<UpdateStatus>(-1))); |
| 330 | } |
| 331 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 332 | void UpdateAttempterTest::QuitMainLoop() { |
| 333 | g_main_loop_quit(loop_); |
| 334 | } |
| 335 | |
| 336 | gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) { |
| 337 | reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop(); |
| 338 | return FALSE; |
| 339 | } |
| 340 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 341 | gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) { |
| 342 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart(); |
| 343 | return FALSE; |
| 344 | } |
| 345 | |
| 346 | gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) { |
| 347 | reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify(); |
| 348 | return FALSE; |
| 349 | } |
| 350 | |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 351 | gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) { |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 352 | reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(false, false); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 353 | return FALSE; |
| 354 | } |
| 355 | |
| 356 | gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) { |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 357 | reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(true, false); |
| 358 | return FALSE; |
| 359 | } |
| 360 | |
| 361 | gboolean UpdateAttempterTest::StaticStableChannelRollbackTestStart( |
| 362 | gpointer data) { |
| 363 | reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart( |
| 364 | false, true); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 365 | return FALSE; |
| 366 | } |
| 367 | |
| 368 | gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) { |
| 369 | reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify(); |
| 370 | return FALSE; |
| 371 | } |
| 372 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 373 | gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) { |
| 374 | reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart(); |
| 375 | return FALSE; |
| 376 | } |
| 377 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 378 | gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart( |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 379 | gpointer data) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 380 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 381 | ua_test->ReadChannelFromPolicyTestStart(); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 382 | return FALSE; |
| 383 | } |
| 384 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 385 | gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart( |
| 386 | gpointer data) { |
| 387 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 388 | ua_test->ReadUpdateDisabledFromPolicyTestStart(); |
| 389 | return FALSE; |
| 390 | } |
| 391 | |
| 392 | gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart( |
| 393 | gpointer data) { |
| 394 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 395 | ua_test->ReadTargetVersionPrefixFromPolicyTestStart(); |
| 396 | return FALSE; |
| 397 | } |
| 398 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 399 | gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart( |
| 400 | gpointer data) { |
| 401 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 402 | ua_test->ReadScatterFactorFromPolicyTestStart(); |
| 403 | return FALSE; |
| 404 | } |
| 405 | |
| 406 | gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart( |
| 407 | gpointer data) { |
| 408 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 409 | ua_test->DecrementUpdateCheckCountTestStart(); |
| 410 | return FALSE; |
| 411 | } |
| 412 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 413 | gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart( |
| 414 | gpointer data) { |
| 415 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 416 | ua_test->NoScatteringDoneDuringManualUpdateTestStart(); |
| 417 | return FALSE; |
| 418 | } |
| 419 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 420 | namespace { |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 421 | // Actions that will be built as part of an update check. |
| 422 | const string kUpdateActionTypes[] = { |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 423 | OmahaRequestAction::StaticType(), |
| 424 | OmahaResponseHandlerAction::StaticType(), |
| 425 | FilesystemCopierAction::StaticType(), |
| 426 | FilesystemCopierAction::StaticType(), |
| 427 | OmahaRequestAction::StaticType(), |
| 428 | DownloadAction::StaticType(), |
| 429 | OmahaRequestAction::StaticType(), |
| 430 | FilesystemCopierAction::StaticType(), |
| 431 | FilesystemCopierAction::StaticType(), |
| 432 | PostinstallRunnerAction::StaticType(), |
| 433 | OmahaRequestAction::StaticType() |
| 434 | }; |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 435 | |
| 436 | // Actions that will be built as part of a user-initiated rollback. |
| 437 | const string kRollbackActionTypes[] = { |
| 438 | InstallPlanAction::StaticType(), |
| 439 | PostinstallRunnerAction::StaticType(), |
| 440 | }; |
| 441 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 442 | } // namespace {} |
| 443 | |
| 444 | void UpdateAttempterTest::UpdateTestStart() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 445 | attempter_.set_http_response_code(200); |
| 446 | InSequence s; |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 447 | for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 448 | EXPECT_CALL(*processor_, |
| 449 | EnqueueAction(Property(&AbstractAction::Type, |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 450 | kUpdateActionTypes[i]))).Times(1); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 451 | } |
| 452 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 453 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 454 | attempter_.Update("", "", false, false, false); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 455 | g_idle_add(&StaticUpdateTestVerify, this); |
| 456 | } |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 457 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 458 | void UpdateAttempterTest::UpdateTestVerify() { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 459 | EXPECT_EQ(0, attempter_.http_response_code()); |
| 460 | EXPECT_EQ(&attempter_, processor_->delegate()); |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 461 | EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size()); |
| 462 | for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) { |
| 463 | EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type()); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 464 | } |
| 465 | EXPECT_EQ(attempter_.response_handler_action_.get(), |
| 466 | attempter_.actions_[1].get()); |
| 467 | DownloadAction* download_action = |
| 468 | dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); |
| 469 | ASSERT_TRUE(download_action != NULL); |
| 470 | EXPECT_EQ(&attempter_, download_action->delegate()); |
| 471 | EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 472 | g_main_loop_quit(loop_); |
| 473 | } |
| 474 | |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 475 | void UpdateAttempterTest::RollbackTestStart( |
| 476 | bool enterprise_rollback, bool stable_channel) { |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 477 | // Create a device policy so that we can change settings. |
| 478 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 479 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 480 | |
| 481 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
| 482 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
| 483 | Return(device_policy)); |
| 484 | |
| 485 | string install_path = "/dev/sda3"; |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 486 | bool is_rollback_allowed = false; |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 487 | |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 488 | // We only allow rollback on devices that are neither enterprise enrolled or |
| 489 | // not on the stable channel. |
| 490 | if (!(enterprise_rollback || stable_channel)) { |
| 491 | is_rollback_allowed = true; |
| 492 | } |
| 493 | |
| 494 | // Set up the policy for the test given our args. |
| 495 | if (stable_channel) { |
| 496 | attempter_.omaha_request_params_->set_current_channel( |
| 497 | string("stable-channel")); |
| 498 | } else if (enterprise_rollback) { |
| 499 | // We return an empty owner as this is an enterprise. |
| 500 | EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce( |
| 501 | DoAll(SetArgumentPointee<0>(std::string("")), |
| 502 | Return(true))); |
| 503 | } else { |
| 504 | // We return a fake owner as this is an owned consumer device. |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 505 | EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce( |
| 506 | DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")), |
| 507 | Return(true))); |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 508 | } |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 509 | |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 510 | if (is_rollback_allowed) { |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 511 | InSequence s; |
| 512 | for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) { |
| 513 | EXPECT_CALL(*processor_, |
| 514 | EnqueueAction(Property(&AbstractAction::Type, |
| 515 | kRollbackActionTypes[i]))).Times(1); |
| 516 | } |
| 517 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 518 | |
| 519 | EXPECT_TRUE(attempter_.Rollback(true, &install_path)); |
| 520 | g_idle_add(&StaticRollbackTestVerify, this); |
| 521 | } else { |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 522 | EXPECT_FALSE(attempter_.Rollback(true, &install_path)); |
| 523 | g_main_loop_quit(loop_); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void UpdateAttempterTest::RollbackTestVerify() { |
| 528 | // Verifies the actions that were enqueued. |
| 529 | EXPECT_EQ(&attempter_, processor_->delegate()); |
| 530 | EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size()); |
| 531 | for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) { |
| 532 | EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type()); |
| 533 | } |
| 534 | EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status()); |
| 535 | InstallPlanAction* install_plan_action = |
| 536 | dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get()); |
| 537 | InstallPlan* install_plan = install_plan_action->install_plan(); |
| 538 | EXPECT_EQ(install_plan->install_path, string("/dev/sda3")); |
| 539 | EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sda2")); |
| 540 | EXPECT_EQ(install_plan->powerwash_required, true); |
| 541 | g_main_loop_quit(loop_); |
| 542 | } |
| 543 | |
Darin Petkov | e6ef2f8 | 2011-03-07 17:31:11 -0800 | [diff] [blame] | 544 | TEST_F(UpdateAttempterTest, UpdateTest) { |
| 545 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 546 | g_idle_add(&StaticUpdateTestStart, this); |
| 547 | g_main_loop_run(loop_); |
| 548 | g_main_loop_unref(loop_); |
| 549 | loop_ = NULL; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 552 | TEST_F(UpdateAttempterTest, RollbackTest) { |
| 553 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 554 | g_idle_add(&StaticRollbackTestStart, this); |
| 555 | g_main_loop_run(loop_); |
| 556 | g_main_loop_unref(loop_); |
| 557 | loop_ = NULL; |
| 558 | } |
| 559 | |
Chris Sosa | 28e479c | 2013-07-12 11:39:53 -0700 | [diff] [blame] | 560 | TEST_F(UpdateAttempterTest, StableChannelRollbackTest) { |
| 561 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 562 | g_idle_add(&StaticStableChannelRollbackTestStart, this); |
| 563 | g_main_loop_run(loop_); |
| 564 | g_main_loop_unref(loop_); |
| 565 | loop_ = NULL; |
| 566 | } |
| 567 | |
Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 568 | TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) { |
| 569 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 570 | g_idle_add(&StaticEnterpriseRollbackTestStart, this); |
| 571 | g_main_loop_run(loop_); |
| 572 | g_main_loop_unref(loop_); |
| 573 | loop_ = NULL; |
| 574 | } |
| 575 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 576 | void UpdateAttempterTest::PingOmahaTestStart() { |
| 577 | EXPECT_CALL(*processor_, |
| 578 | EnqueueAction(Property(&AbstractAction::Type, |
| 579 | OmahaRequestAction::StaticType()))) |
| 580 | .Times(1); |
| 581 | EXPECT_CALL(*processor_, StartProcessing()).Times(1); |
| 582 | attempter_.PingOmaha(); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 583 | g_idle_add(&StaticQuitMainLoop, this); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | TEST_F(UpdateAttempterTest, PingOmahaTest) { |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 587 | UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 588 | scheduler.enabled_ = true; |
Andrew de los Reyes | e05fc28 | 2011-06-02 09:50:08 -0700 | [diff] [blame] | 589 | EXPECT_FALSE(scheduler.scheduled_); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 590 | attempter_.set_update_check_scheduler(&scheduler); |
| 591 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 592 | g_idle_add(&StaticPingOmahaTestStart, this); |
| 593 | g_main_loop_run(loop_); |
| 594 | g_main_loop_unref(loop_); |
| 595 | loop_ = NULL; |
| 596 | EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status()); |
| 597 | EXPECT_EQ(true, scheduler.scheduled_); |
| 598 | } |
| 599 | |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 600 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) { |
| 601 | ActionMock action; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 602 | const ErrorCode kCode = kErrorCodeDownloadTransferError; |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 603 | attempter_.CreatePendingErrorEvent(&action, kCode); |
| 604 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 605 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 606 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 607 | EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag, |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 608 | attempter_.error_event_->error_code); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) { |
| 612 | OmahaResponseHandlerAction *response_action = |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 613 | new OmahaResponseHandlerAction(&mock_system_state_); |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 614 | response_action->install_plan_.is_resume = true; |
| 615 | attempter_.response_handler_action_.reset(response_action); |
| 616 | ActionMock action; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 617 | const ErrorCode kCode = kErrorCodeInstallDeviceOpenError; |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 618 | attempter_.CreatePendingErrorEvent(&action, kCode); |
| 619 | ASSERT_TRUE(attempter_.error_event_.get() != NULL); |
| 620 | EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type); |
| 621 | EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 622 | EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag, |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 623 | attempter_.error_event_->error_code); |
| 624 | } |
| 625 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 626 | TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) { |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 627 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 628 | g_idle_add(&StaticReadChannelFromPolicyTestStart, this); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 629 | g_main_loop_run(loop_); |
| 630 | g_main_loop_unref(loop_); |
| 631 | loop_ = NULL; |
| 632 | } |
| 633 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 634 | void UpdateAttempterTest::ReadChannelFromPolicyTestStart() { |
| 635 | // Tests that the update channel (aka release channel) is properly fetched |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 636 | // from the device policy. |
| 637 | |
| 638 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 639 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 640 | |
| 641 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 642 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
| 643 | Return(device_policy)); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 644 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 645 | EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly( |
| 646 | DoAll(SetArgumentPointee<0>(bool(false)), |
| 647 | Return(true))); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 648 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 649 | EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly( |
| 650 | DoAll(SetArgumentPointee<0>(std::string("beta-channel")), |
| 651 | Return(true))); |
| 652 | |
Gilad Arnold | eff87cc | 2013-07-22 18:32:09 -0700 | [diff] [blame] | 653 | ASSERT_FALSE(test_dir_.empty()); |
| 654 | attempter_.omaha_request_params_->set_root(test_dir_); |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 655 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 656 | EXPECT_EQ("beta-channel", |
| 657 | attempter_.omaha_request_params_->target_channel()); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 658 | |
| 659 | g_idle_add(&StaticQuitMainLoop, this); |
| 660 | } |
| 661 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 662 | TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) { |
| 663 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 664 | g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this); |
| 665 | g_main_loop_run(loop_); |
| 666 | g_main_loop_unref(loop_); |
| 667 | loop_ = NULL; |
| 668 | } |
| 669 | |
| 670 | void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() { |
| 671 | // Tests that the update_disbled flag is properly fetched |
| 672 | // from the device policy. |
| 673 | |
| 674 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 675 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 676 | |
| 677 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 678 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
| 679 | Return(device_policy)); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 680 | |
| 681 | EXPECT_CALL(*device_policy, GetUpdateDisabled(_)) |
| 682 | .WillRepeatedly(DoAll( |
| 683 | SetArgumentPointee<0>(true), |
| 684 | Return(true))); |
| 685 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 686 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 687 | EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled()); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 688 | |
| 689 | g_idle_add(&StaticQuitMainLoop, this); |
| 690 | } |
| 691 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame^] | 692 | TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) { |
| 693 | MockP2PManager mock_p2p_manager; |
| 694 | mock_system_state_.set_p2p_manager(&mock_p2p_manager); |
| 695 | mock_p2p_manager.fake().SetP2PEnabled(false); |
| 696 | EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0); |
| 697 | attempter_.UpdateEngineStarted(); |
| 698 | } |
| 699 | |
| 700 | TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) { |
| 701 | MockP2PManager mock_p2p_manager; |
| 702 | mock_system_state_.set_p2p_manager(&mock_p2p_manager); |
| 703 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 704 | EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0); |
| 705 | attempter_.UpdateEngineStarted(); |
| 706 | } |
| 707 | |
| 708 | TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) { |
| 709 | MockP2PManager mock_p2p_manager; |
| 710 | mock_system_state_.set_p2p_manager(&mock_p2p_manager); |
| 711 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 712 | mock_p2p_manager.fake().SetCountSharedFilesResult(1); |
| 713 | EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1); |
| 714 | attempter_.UpdateEngineStarted(); |
| 715 | } |
| 716 | |
| 717 | TEST_F(UpdateAttempterTest, P2PNotEnabled) { |
| 718 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 719 | g_idle_add(&StaticP2PNotEnabled, this); |
| 720 | g_main_loop_run(loop_); |
| 721 | g_main_loop_unref(loop_); |
| 722 | loop_ = NULL; |
| 723 | } |
| 724 | gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) { |
| 725 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 726 | ua_test->P2PNotEnabledStart(); |
| 727 | return FALSE; |
| 728 | } |
| 729 | void UpdateAttempterTest::P2PNotEnabledStart() { |
| 730 | // If P2P is not enabled, check that we do not attempt housekeeping |
| 731 | // and do not convey that p2p is to be used. |
| 732 | MockP2PManager mock_p2p_manager; |
| 733 | mock_system_state_.set_p2p_manager(&mock_p2p_manager); |
| 734 | mock_p2p_manager.fake().SetP2PEnabled(false); |
| 735 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0); |
| 736 | attempter_.Update("", "", false, false, false); |
| 737 | EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading()); |
| 738 | EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing()); |
| 739 | g_idle_add(&StaticQuitMainLoop, this); |
| 740 | } |
| 741 | |
| 742 | TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) { |
| 743 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 744 | g_idle_add(&StaticP2PEnabledStartingFails, this); |
| 745 | g_main_loop_run(loop_); |
| 746 | g_main_loop_unref(loop_); |
| 747 | loop_ = NULL; |
| 748 | } |
| 749 | gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails( |
| 750 | gpointer data) { |
| 751 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 752 | ua_test->P2PEnabledStartingFailsStart(); |
| 753 | return FALSE; |
| 754 | } |
| 755 | void UpdateAttempterTest::P2PEnabledStartingFailsStart() { |
| 756 | // If p2p is enabled, but starting it fails ensure we don't do |
| 757 | // any housekeeping and do not convey that p2p should be used. |
| 758 | MockP2PManager mock_p2p_manager; |
| 759 | mock_system_state_.set_p2p_manager(&mock_p2p_manager); |
| 760 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 761 | mock_p2p_manager.fake().SetEnsureP2PRunningResult(false); |
| 762 | mock_p2p_manager.fake().SetPerformHousekeepingResult(false); |
| 763 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0); |
| 764 | attempter_.Update("", "", false, false, false); |
| 765 | EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading()); |
| 766 | EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing()); |
| 767 | g_idle_add(&StaticQuitMainLoop, this); |
| 768 | } |
| 769 | |
| 770 | TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) { |
| 771 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 772 | g_idle_add(&StaticP2PEnabledHousekeepingFails, this); |
| 773 | g_main_loop_run(loop_); |
| 774 | g_main_loop_unref(loop_); |
| 775 | loop_ = NULL; |
| 776 | } |
| 777 | gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails( |
| 778 | gpointer data) { |
| 779 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 780 | ua_test->P2PEnabledHousekeepingFailsStart(); |
| 781 | return FALSE; |
| 782 | } |
| 783 | void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() { |
| 784 | // If p2p is enabled, starting it works but housekeeping fails, ensure |
| 785 | // we do not convey p2p is to be used. |
| 786 | MockP2PManager mock_p2p_manager; |
| 787 | mock_system_state_.set_p2p_manager(&mock_p2p_manager); |
| 788 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 789 | mock_p2p_manager.fake().SetEnsureP2PRunningResult(true); |
| 790 | mock_p2p_manager.fake().SetPerformHousekeepingResult(false); |
| 791 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1); |
| 792 | attempter_.Update("", "", false, false, false); |
| 793 | EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading()); |
| 794 | EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing()); |
| 795 | g_idle_add(&StaticQuitMainLoop, this); |
| 796 | } |
| 797 | |
| 798 | TEST_F(UpdateAttempterTest, P2PEnabled) { |
| 799 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 800 | g_idle_add(&StaticP2PEnabled, this); |
| 801 | g_main_loop_run(loop_); |
| 802 | g_main_loop_unref(loop_); |
| 803 | loop_ = NULL; |
| 804 | } |
| 805 | gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) { |
| 806 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 807 | ua_test->P2PEnabledStart(); |
| 808 | return FALSE; |
| 809 | } |
| 810 | void UpdateAttempterTest::P2PEnabledStart() { |
| 811 | MockP2PManager mock_p2p_manager; |
| 812 | mock_system_state_.set_p2p_manager(&mock_p2p_manager); |
| 813 | // If P2P is enabled and starting it works, check that we performed |
| 814 | // housekeeping and that we convey p2p should be used. |
| 815 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 816 | mock_p2p_manager.fake().SetEnsureP2PRunningResult(true); |
| 817 | mock_p2p_manager.fake().SetPerformHousekeepingResult(true); |
| 818 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1); |
| 819 | attempter_.Update("", "", false, false, false); |
| 820 | EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading()); |
| 821 | EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing()); |
| 822 | g_idle_add(&StaticQuitMainLoop, this); |
| 823 | } |
| 824 | |
| 825 | TEST_F(UpdateAttempterTest, P2PEnabledInteractive) { |
| 826 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 827 | g_idle_add(&StaticP2PEnabledInteractive, this); |
| 828 | g_main_loop_run(loop_); |
| 829 | g_main_loop_unref(loop_); |
| 830 | loop_ = NULL; |
| 831 | } |
| 832 | gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) { |
| 833 | UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data); |
| 834 | ua_test->P2PEnabledInteractiveStart(); |
| 835 | return FALSE; |
| 836 | } |
| 837 | void UpdateAttempterTest::P2PEnabledInteractiveStart() { |
| 838 | MockP2PManager mock_p2p_manager; |
| 839 | mock_system_state_.set_p2p_manager(&mock_p2p_manager); |
| 840 | // For an interactive check, if P2P is enabled and starting it |
| 841 | // works, check that we performed housekeeping and that we convey |
| 842 | // p2p should be used for sharing but NOT for downloading. |
| 843 | mock_p2p_manager.fake().SetP2PEnabled(true); |
| 844 | mock_p2p_manager.fake().SetEnsureP2PRunningResult(true); |
| 845 | mock_p2p_manager.fake().SetPerformHousekeepingResult(true); |
| 846 | EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1); |
| 847 | attempter_.Update("", "", false, true /* interactive */, false); |
| 848 | EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading()); |
| 849 | EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing()); |
| 850 | g_idle_add(&StaticQuitMainLoop, this); |
| 851 | } |
| 852 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 853 | TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) { |
| 854 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 855 | g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this); |
| 856 | g_main_loop_run(loop_); |
| 857 | g_main_loop_unref(loop_); |
| 858 | loop_ = NULL; |
| 859 | } |
| 860 | |
| 861 | void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() { |
| 862 | // Tests that the target_version_prefix value is properly fetched |
| 863 | // from the device policy. |
| 864 | |
| 865 | const std::string target_version_prefix = "1412."; |
| 866 | |
| 867 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 868 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 869 | |
| 870 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 871 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
| 872 | Return(device_policy)); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 873 | |
| 874 | EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_)) |
| 875 | .WillRepeatedly(DoAll( |
| 876 | SetArgumentPointee<0>(target_version_prefix), |
| 877 | Return(true))); |
| 878 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 879 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 880 | EXPECT_EQ(target_version_prefix.c_str(), |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 881 | attempter_.omaha_request_params_->target_version_prefix()); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 882 | |
| 883 | g_idle_add(&StaticQuitMainLoop, this); |
| 884 | } |
| 885 | |
| 886 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 887 | TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) { |
| 888 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 889 | g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this); |
| 890 | g_main_loop_run(loop_); |
| 891 | g_main_loop_unref(loop_); |
| 892 | loop_ = NULL; |
| 893 | } |
| 894 | |
| 895 | // Tests that the scatter_factor_in_seconds value is properly fetched |
| 896 | // from the device policy. |
| 897 | void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() { |
| 898 | int64 scatter_factor_in_seconds = 36000; |
| 899 | |
| 900 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 901 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 902 | |
| 903 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 904 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 905 | Return(device_policy)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 906 | |
| 907 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
| 908 | .WillRepeatedly(DoAll( |
| 909 | SetArgumentPointee<0>(scatter_factor_in_seconds), |
| 910 | Return(true))); |
| 911 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 912 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 913 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 914 | |
| 915 | g_idle_add(&StaticQuitMainLoop, this); |
| 916 | } |
| 917 | |
| 918 | TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) { |
| 919 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 920 | g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this); |
| 921 | g_main_loop_run(loop_); |
| 922 | g_main_loop_unref(loop_); |
| 923 | loop_ = NULL; |
| 924 | } |
| 925 | |
| 926 | void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() { |
| 927 | // Tests that the scatter_factor_in_seconds value is properly fetched |
| 928 | // from the device policy and is decremented if value > 0. |
| 929 | int64 initial_value = 5; |
| 930 | Prefs prefs; |
| 931 | attempter_.prefs_ = &prefs; |
| 932 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 933 | EXPECT_CALL(mock_system_state_, |
| 934 | IsOOBEComplete()).WillRepeatedly(Return(true)); |
| 935 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 936 | string prefs_dir; |
| 937 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 938 | &prefs_dir)); |
| 939 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 940 | |
| 941 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 942 | << "Failed to initialize preferences."; |
| 943 | EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value)); |
| 944 | |
| 945 | int64 scatter_factor_in_seconds = 10; |
| 946 | |
| 947 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 948 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 949 | |
| 950 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 951 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 952 | Return(device_policy)); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 953 | |
| 954 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
| 955 | .WillRepeatedly(DoAll( |
| 956 | SetArgumentPointee<0>(scatter_factor_in_seconds), |
| 957 | Return(true))); |
| 958 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 959 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 960 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 961 | |
| 962 | // Make sure the file still exists. |
| 963 | EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount)); |
| 964 | |
| 965 | int64 new_value; |
| 966 | EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value)); |
| 967 | EXPECT_EQ(initial_value - 1, new_value); |
| 968 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 969 | EXPECT_TRUE( |
| 970 | attempter_.omaha_request_params_->update_check_count_wait_enabled()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 971 | |
| 972 | // However, if the count is already 0, it's not decremented. Test that. |
| 973 | initial_value = 0; |
| 974 | EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value)); |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 975 | attempter_.Update("", "", false, false, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 976 | EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount)); |
| 977 | EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value)); |
| 978 | EXPECT_EQ(initial_value, new_value); |
| 979 | |
| 980 | g_idle_add(&StaticQuitMainLoop, this); |
| 981 | } |
| 982 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 983 | TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) { |
| 984 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 985 | g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this); |
| 986 | g_main_loop_run(loop_); |
| 987 | g_main_loop_unref(loop_); |
| 988 | loop_ = NULL; |
| 989 | } |
| 990 | |
| 991 | void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() { |
| 992 | // Tests that no scattering logic is enabled if the update check |
| 993 | // is manually done (as opposed to a scheduled update check) |
| 994 | int64 initial_value = 8; |
| 995 | Prefs prefs; |
| 996 | attempter_.prefs_ = &prefs; |
| 997 | |
| 998 | EXPECT_CALL(mock_system_state_, |
| 999 | IsOOBEComplete()).WillRepeatedly(Return(true)); |
| 1000 | |
| 1001 | string prefs_dir; |
| 1002 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX", |
| 1003 | &prefs_dir)); |
| 1004 | ScopedDirRemover temp_dir_remover(prefs_dir); |
| 1005 | |
| 1006 | LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir))) |
| 1007 | << "Failed to initialize preferences."; |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 1008 | EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value)); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1009 | EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value)); |
| 1010 | |
| 1011 | // make sure scatter_factor is non-zero as scattering is disabled |
| 1012 | // otherwise. |
| 1013 | int64 scatter_factor_in_seconds = 50; |
| 1014 | |
| 1015 | policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy(); |
| 1016 | attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy)); |
| 1017 | |
| 1018 | EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1019 | EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly( |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 1020 | Return(device_policy)); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1021 | |
| 1022 | EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_)) |
| 1023 | .WillRepeatedly(DoAll( |
| 1024 | SetArgumentPointee<0>(scatter_factor_in_seconds), |
| 1025 | Return(true))); |
| 1026 | |
Gilad Arnold | b92f0df | 2013-01-10 16:32:45 -0800 | [diff] [blame] | 1027 | // Trigger an interactive check so we can test that scattering is disabled. |
| 1028 | attempter_.Update("", "", false, true, false); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1029 | EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds()); |
| 1030 | |
| 1031 | // Make sure scattering is disabled for manual (i.e. user initiated) update |
Jay Srinivasan | 21be075 | 2012-07-25 15:44:56 -0700 | [diff] [blame] | 1032 | // checks and all artifacts are removed. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1033 | EXPECT_FALSE( |
| 1034 | attempter_.omaha_request_params_->wall_clock_based_wait_enabled()); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1035 | EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod)); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1036 | EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds()); |
| 1037 | EXPECT_FALSE( |
| 1038 | attempter_.omaha_request_params_->update_check_count_wait_enabled()); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1039 | EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount)); |
| 1040 | |
| 1041 | g_idle_add(&StaticQuitMainLoop, this); |
| 1042 | } |
| 1043 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 1044 | } // namespace chromeos_update_engine |