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