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