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