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