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