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