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