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