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