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