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