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