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