blob: 5d62cbec99d1af8a88e06280f138c9250d6e8f04 [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovf42cc1c2010-09-01 09:03:02 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo2c0db7b2014-11-04 12:23:39 -08005#include "update_engine/update_attempter.h"
6
Ben Chan9abb7632014-08-07 00:10:53 -07007#include <stdint.h>
8
Ben Chan02f7c1d2014-10-18 15:18:02 -07009#include <memory>
10
Ben Chan06c76a42014-09-05 08:21:06 -070011#include <base/files/file_util.h>
David Pursell02c18642014-11-06 11:26:11 -080012#include <chromeos/dbus/service_constants.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070013#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020014#include <policy/libpolicy.h>
15#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016
David Zeuthen985b1122013-10-09 12:13:15 -070017#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080018#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070019#include "update_engine/fake_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070020#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070021#include "update_engine/install_plan.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080022#include "update_engine/mock_action.h"
23#include "update_engine/mock_action_processor.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080024#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080025#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070026#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080027#include "update_engine/mock_payload_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080028#include "update_engine/mock_prefs.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070029#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070030#include "update_engine/prefs.h"
Darin Petkov1b003102010-11-30 10:18:36 -080031#include "update_engine/test_utils.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070032#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033
David Zeuthen985b1122013-10-09 12:13:15 -070034using base::Time;
35using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070036using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070037using std::unique_ptr;
David Pursell02c18642014-11-06 11:26:11 -080038using testing::A;
Darin Petkov36275772010-10-01 11:40:57 -070039using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070040using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070041using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080042using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070043using testing::Property;
44using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070045using testing::ReturnPointee;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080046using testing::SaveArg;
Darin Petkov36275772010-10-01 11:40:57 -070047using testing::SetArgumentPointee;
David Pursell02c18642014-11-06 11:26:11 -080048using testing::StrEq;
Alex Deymof329b932014-10-30 01:37:48 -070049using testing::_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070050
51namespace chromeos_update_engine {
52
53// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070054// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070055// methods.
56class UpdateAttempterUnderTest : public UpdateAttempter {
57 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070058 // We always feed an explicit update completed marker name; however, unless
59 // explicitly specified, we feed an empty string, which causes the
60 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070061 UpdateAttempterUnderTest(SystemState* system_state,
62 DBusWrapperInterface* dbus_iface)
63 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070064
Gilad Arnold1f847232014-04-07 12:07:49 -070065 UpdateAttempterUnderTest(SystemState* system_state,
66 DBusWrapperInterface* dbus_iface,
Alex Deymof329b932014-10-30 01:37:48 -070067 const string& update_completed_marker)
Gilad Arnold1f847232014-04-07 12:07:49 -070068 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070069
70 // Wrap the update scheduling method, allowing us to opt out of scheduled
71 // updates for testing purposes.
72 void ScheduleUpdates() override {
73 schedule_updates_called_ = true;
74 if (do_schedule_updates_) {
75 UpdateAttempter::ScheduleUpdates();
76 } else {
77 LOG(INFO) << "[TEST] Update scheduling disabled.";
78 }
79 }
80 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
81 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
82
83 // Indicates whether ScheduleUpdates() was called.
84 bool schedule_updates_called() const { return schedule_updates_called_; }
85
David Pursell02c18642014-11-06 11:26:11 -080086 // Need to expose forced_omaha_url_ so we can test it.
87 const std::string& forced_omaha_url() const { return forced_omaha_url_; }
88
Gilad Arnoldec7f9162014-07-15 13:24:46 -070089 private:
90 bool schedule_updates_called_ = false;
91 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070092};
93
94class UpdateAttempterTest : public ::testing::Test {
95 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070096 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -070097 : attempter_(&fake_system_state_, &dbus_),
98 mock_connection_manager(&fake_system_state_),
David Pursell02c18642014-11-06 11:26:11 -080099 loop_(nullptr),
100 fake_dbus_system_bus_(reinterpret_cast<DBusGConnection*>(1)),
101 fake_dbus_debugd_proxy_(reinterpret_cast<DBusGProxy*>(2)) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700102 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700103 fake_system_state_.set_connection_manager(&mock_connection_manager);
104 fake_system_state_.set_update_attempter(&attempter_);
Gilad Arnold1f847232014-04-07 12:07:49 -0700105
106 // Finish initializing the attempter.
107 attempter_.Init();
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700108
109 // We set the set_good_kernel command to a non-existent path so it fails to
110 // run it. This avoids the async call to the command and continues the
111 // update process right away. Tests testing that behavior can override the
112 // default set_good_kernel command if needed.
113 attempter_.set_good_kernel_cmd_ = "/path/to/non-existent/command";
Jay Srinivasan43488792012-06-19 00:25:31 -0700114 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700115
Alex Deymo610277e2014-11-11 21:18:11 -0800116 void SetUp() override {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700117 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
118
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700119 EXPECT_EQ(nullptr, attempter_.dbus_service_);
120 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700121 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800122 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700123 EXPECT_EQ(nullptr, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700124 EXPECT_FALSE(attempter_.download_active_);
125 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
126 EXPECT_EQ(0.0, attempter_.download_progress_);
127 EXPECT_EQ(0, attempter_.last_checked_time_);
128 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700129 EXPECT_EQ(0, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800130 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700131 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700132 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700133
134 // Set up store/load semantics of P2P properties via the mock PayloadState.
135 actual_using_p2p_for_downloading_ = false;
136 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
137 SetUsingP2PForDownloading(_))
138 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
139 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
140 GetUsingP2PForDownloading())
141 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
142 actual_using_p2p_for_sharing_ = false;
143 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
144 SetUsingP2PForSharing(_))
145 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
146 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
147 GetUsingP2PForDownloading())
148 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
David Pursell02c18642014-11-06 11:26:11 -0800149
150 // Set up mock debugd access over the system D-Bus. ProxyCall_0_1() also
151 // needs to be mocked in any test using debugd to provide the desired value.
152 ON_CALL(dbus_, BusGet(DBUS_BUS_SYSTEM, _))
153 .WillByDefault(Return(fake_dbus_system_bus_));
154 ON_CALL(dbus_, ProxyNewForName(fake_dbus_system_bus_,
155 StrEq(debugd::kDebugdServiceName),
156 StrEq(debugd::kDebugdServicePath),
157 StrEq(debugd::kDebugdInterface)))
158 .WillByDefault(Return(fake_dbus_debugd_proxy_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700159 }
160
Alex Deymo610277e2014-11-11 21:18:11 -0800161 void TearDown() override {
Alex Deymo10875d92014-11-10 21:52:57 -0800162 test_utils::RecursiveUnlinkDir(test_dir_);
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700163 }
164
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200165 void QuitMainLoop();
166 static gboolean StaticQuitMainLoop(gpointer data);
167
Darin Petkove6ef2f82011-03-07 17:31:11 -0800168 void UpdateTestStart();
169 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800170 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800171 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700172 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800173 static gboolean StaticUpdateTestStart(gpointer data);
174 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700175 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800176 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700177 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
178 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200179
Thieu Le116fda32011-04-19 11:01:54 -0700180 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700181 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200182
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700183 void ReadScatterFactorFromPolicyTestStart();
184 static gboolean StaticReadScatterFactorFromPolicyTestStart(
185 gpointer data);
186
187 void DecrementUpdateCheckCountTestStart();
188 static gboolean StaticDecrementUpdateCheckCountTestStart(
189 gpointer data);
190
Jay Srinivasan08fce042012-06-07 16:31:01 -0700191 void NoScatteringDoneDuringManualUpdateTestStart();
192 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
193 gpointer data);
194
David Zeuthen8f191b22013-08-06 12:27:50 -0700195 void P2PNotEnabledStart();
196 static gboolean StaticP2PNotEnabled(gpointer data);
197
198 void P2PEnabledStart();
199 static gboolean StaticP2PEnabled(gpointer data);
200
201 void P2PEnabledInteractiveStart();
202 static gboolean StaticP2PEnabledInteractive(gpointer data);
203
204 void P2PEnabledStartingFailsStart();
205 static gboolean StaticP2PEnabledStartingFails(gpointer data);
206
207 void P2PEnabledHousekeepingFailsStart();
208 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
209
Gilad Arnold74b5f552014-10-07 08:17:16 -0700210 bool actual_using_p2p_for_downloading() {
211 return actual_using_p2p_for_downloading_;
212 }
213 bool actual_using_p2p_for_sharing() {
214 return actual_using_p2p_for_sharing_;
215 }
216
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700217 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800218 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700219 UpdateAttempterUnderTest attempter_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800220 NiceMock<MockActionProcessor>* processor_;
221 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800222 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800223 GMainLoop* loop_;
David Pursell02c18642014-11-06 11:26:11 -0800224 // fake_dbus_xxx pointers will be non-null for comparison purposes, but won't
225 // be valid objects so don't try to use them.
226 DBusGConnection* fake_dbus_system_bus_;
227 DBusGProxy* fake_dbus_debugd_proxy_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700228
229 string test_dir_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700230
231 bool actual_using_p2p_for_downloading_;
232 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700233};
234
Darin Petkov1b003102010-11-30 10:18:36 -0800235TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700236 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800237 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700238 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800239 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700240 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800241 EXPECT_EQ(503, attempter_.http_response_code());
242 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700243 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800244}
245
246TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800247 MockAction action;
248 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Darin Petkov1b003102010-11-30 10:18:36 -0800249 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800250 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800251 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700252 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
253 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800254}
255
256TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700257 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800258 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700259 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800260 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800261 ObjectCollectorAction<OmahaResponse> collector_action;
262 BondActions(&action, &collector_action);
263 OmahaResponse response;
264 response.poll_interval = 234;
265 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800266 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700267 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800268 EXPECT_EQ(500, attempter_.http_response_code());
269 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700270 EXPECT_EQ(234, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700271 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800272}
273
Darin Petkovcd1666f2010-09-23 09:53:44 -0700274TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700275 string test_update_completed_marker;
276 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800277 "update_attempter_unittest-update_completed_marker-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700278 &test_update_completed_marker, nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700279 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700280 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700281 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700282 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700283 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700284 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700285}
286
287TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700288 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
289 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700290 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700291 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700292
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700293 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700294 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
295 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700296 EXPECT_EQ(ErrorCode::kOmahaRequestError,
297 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700298 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700299 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700300 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700301 ErrorCode::kError));
Alex Deymo42432912013-07-12 20:21:15 -0700302 FilesystemCopierAction filesystem_copier_action(
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700303 &fake_system_state_, false, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700304 EXPECT_EQ(ErrorCode::kFilesystemCopierError,
305 GetErrorCodeForAction(&filesystem_copier_action,
306 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800307 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700308 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700309 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700310 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800311 MockAction action_mock;
312 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700313 EXPECT_EQ(ErrorCode::kError,
314 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700315}
316
Darin Petkov36275772010-10-01 11:40:57 -0700317TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700318 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800319 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700320 .WillOnce(Return(false));
321 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700322 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800323 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700324 .WillOnce(DoAll(
325 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
326 Return(true)));
327 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700328 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800329 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700330 .WillOnce(DoAll(
331 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
332 Return(true)));
333 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700334 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800335 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700336 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700337 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700338}
339
340TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800341 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700342 .WillOnce(Return(false))
343 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
344 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
345 .WillOnce(DoAll(
346 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
347 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800348 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700349 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800350 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700351 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800352 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700353 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700354 for (int i = 0; i < 4; i ++)
355 attempter_.MarkDeltaUpdateFailure();
356}
357
Darin Petkov1b003102010-11-30 10:18:36 -0800358TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
359 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
360 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700361 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800362 .Times(0);
363 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700364 string url1 = "http://url1";
365 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800366 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700367 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700368 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700369 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800370 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700371 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800372}
373
374TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
375 EXPECT_CALL(*processor_,
376 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700377 OmahaRequestAction::StaticType())));
378 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700379 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700380 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800381 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
382 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800383 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800384 attempter_.ScheduleErrorEventAction();
385 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
386}
387
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200388void UpdateAttempterTest::QuitMainLoop() {
389 g_main_loop_quit(loop_);
390}
391
392gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
393 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
394 return FALSE;
395}
396
Darin Petkove6ef2f82011-03-07 17:31:11 -0800397gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
398 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
399 return FALSE;
400}
401
402gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
403 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
404 return FALSE;
405}
406
Chris Sosa76a29ae2013-07-11 17:59:24 -0700407gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800408 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700409 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800410 return FALSE;
411}
412
413gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
414 gpointer data) {
415 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700416 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700417 return FALSE;
418}
419
420gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800421 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700422 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700423 return FALSE;
424}
425
426gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
427 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
428 return FALSE;
429}
430
Thieu Le116fda32011-04-19 11:01:54 -0700431gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
432 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
433 return FALSE;
434}
435
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700436gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
437 gpointer data) {
438 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
439 ua_test->ReadScatterFactorFromPolicyTestStart();
440 return FALSE;
441}
442
443gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
444 gpointer data) {
445 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
446 ua_test->DecrementUpdateCheckCountTestStart();
447 return FALSE;
448}
449
Jay Srinivasan08fce042012-06-07 16:31:01 -0700450gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
451 gpointer data) {
452 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
453 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
454 return FALSE;
455}
456
Darin Petkove6ef2f82011-03-07 17:31:11 -0800457namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700458// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700459const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800460 OmahaRequestAction::StaticType(),
461 OmahaResponseHandlerAction::StaticType(),
462 FilesystemCopierAction::StaticType(),
463 FilesystemCopierAction::StaticType(),
464 OmahaRequestAction::StaticType(),
465 DownloadAction::StaticType(),
466 OmahaRequestAction::StaticType(),
467 FilesystemCopierAction::StaticType(),
468 FilesystemCopierAction::StaticType(),
469 PostinstallRunnerAction::StaticType(),
470 OmahaRequestAction::StaticType()
471};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700472
473// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700474const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700475 InstallPlanAction::StaticType(),
476 PostinstallRunnerAction::StaticType(),
477};
478
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700479} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800480
481void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700482 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700483
484 // Expect that the device policy is loaded by the UpdateAttempter at some
485 // point by calling RefreshDevicePolicy.
486 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
487 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
488 EXPECT_CALL(*device_policy, LoadPolicy())
489 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
490
491 {
492 InSequence s;
493 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
494 EXPECT_CALL(*processor_,
495 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700496 kUpdateActionTypes[i])));
Alex Deymo749ecf12014-10-21 20:06:57 -0700497 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700498 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700499 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700500
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700501 attempter_.Update("", "", "", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800502 g_idle_add(&StaticUpdateTestVerify, this);
503}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700504
Darin Petkove6ef2f82011-03-07 17:31:11 -0800505void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700506 EXPECT_EQ(0, attempter_.http_response_code());
507 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700508 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
509 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
510 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700511 }
512 EXPECT_EQ(attempter_.response_handler_action_.get(),
513 attempter_.actions_[1].get());
514 DownloadAction* download_action =
515 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700516 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700517 EXPECT_EQ(&attempter_, download_action->delegate());
518 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800519 g_main_loop_quit(loop_);
520}
521
Chris Sosa28e479c2013-07-12 11:39:53 -0700522void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700523 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700524 // Create a device policy so that we can change settings.
525 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
526 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
527
528 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700529 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700530
Don Garrett6646b442013-11-13 15:29:11 -0800531 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700532 // References bootable kernels in fake_hardware.h
533 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800534 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700535 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800536 rollback_kernel);
537 }
538
Chris Sosa28e479c2013-07-12 11:39:53 -0700539 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700540
Chris Sosad38b1132014-03-25 10:43:59 -0700541 // We only allow rollback on devices that are not enterprise enrolled and
542 // which have a valid slot to rollback to.
543 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700544 is_rollback_allowed = true;
545 }
546
Don Garrett6646b442013-11-13 15:29:11 -0800547 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700548 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800549 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700550 DoAll(SetArgumentPointee<0>(string("")),
Chris Sosa28e479c2013-07-12 11:39:53 -0700551 Return(true)));
552 } else {
553 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800554 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700555 DoAll(SetArgumentPointee<0>(string("fake.mail@fake.com")),
Chris Sosa76a29ae2013-07-11 17:59:24 -0700556 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700557 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700558
Chris Sosa28e479c2013-07-12 11:39:53 -0700559 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700560 InSequence s;
561 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
562 EXPECT_CALL(*processor_,
563 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700564 kRollbackActionTypes[i])));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700565 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700566 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700567
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700568 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700569 g_idle_add(&StaticRollbackTestVerify, this);
570 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700571 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700572 g_main_loop_quit(loop_);
573 }
574}
575
576void UpdateAttempterTest::RollbackTestVerify() {
577 // Verifies the actions that were enqueued.
578 EXPECT_EQ(&attempter_, processor_->delegate());
579 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
580 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
581 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
582 }
583 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
584 InstallPlanAction* install_plan_action =
585 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
586 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700587 // Matches fake_hardware.h -> rollback should move from kernel/boot device
588 // pair to other pair.
589 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
590 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700591 EXPECT_EQ(install_plan->powerwash_required, true);
592 g_main_loop_quit(loop_);
593}
594
Darin Petkove6ef2f82011-03-07 17:31:11 -0800595TEST_F(UpdateAttempterTest, UpdateTest) {
596 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
597 g_idle_add(&StaticUpdateTestStart, this);
598 g_main_loop_run(loop_);
599 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700600 loop_ = nullptr;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700601}
602
Chris Sosa76a29ae2013-07-11 17:59:24 -0700603TEST_F(UpdateAttempterTest, RollbackTest) {
604 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
605 g_idle_add(&StaticRollbackTestStart, this);
606 g_main_loop_run(loop_);
607 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700608 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700609}
610
Don Garrett6646b442013-11-13 15:29:11 -0800611TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
612 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
613 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
614 g_main_loop_run(loop_);
615 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700616 loop_ = nullptr;
Don Garrett6646b442013-11-13 15:29:11 -0800617}
618
Chris Sosa76a29ae2013-07-11 17:59:24 -0700619TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
620 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
621 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
622 g_main_loop_run(loop_);
623 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700624 loop_ = nullptr;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700625}
626
Thieu Le116fda32011-04-19 11:01:54 -0700627void UpdateAttempterTest::PingOmahaTestStart() {
628 EXPECT_CALL(*processor_,
629 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700630 OmahaRequestAction::StaticType())));
631 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700632 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200633 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700634}
635
636TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700637 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
638 EXPECT_FALSE(attempter_.schedule_updates_called());
639 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
640 // testing, which is more permissive than we want to handle here.
641 attempter_.DisableScheduleUpdates();
Thieu Le116fda32011-04-19 11:01:54 -0700642 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
643 g_idle_add(&StaticPingOmahaTestStart, this);
644 g_main_loop_run(loop_);
645 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700646 loop_ = nullptr;
Thieu Le116fda32011-04-19 11:01:54 -0700647 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700648 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700649}
650
Darin Petkov18c7bce2011-06-16 14:07:00 -0700651TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800652 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700653 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700654 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700655 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700656 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
657 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700658 EXPECT_EQ(
659 static_cast<ErrorCode>(static_cast<int>(kCode) |
660 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
661 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700662}
663
664TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
665 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700666 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700667 response_action->install_plan_.is_resume = true;
668 attempter_.response_handler_action_.reset(response_action);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800669 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700670 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700671 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700672 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700673 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
674 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700675 EXPECT_EQ(
676 static_cast<ErrorCode>(
677 static_cast<int>(kCode) |
678 static_cast<int>(ErrorCode::kResumedFlag) |
679 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
680 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700681}
682
David Zeuthen8f191b22013-08-06 12:27:50 -0700683TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
684 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700685 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700686 mock_p2p_manager.fake().SetP2PEnabled(false);
687 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
688 attempter_.UpdateEngineStarted();
689}
690
691TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
692 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700693 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700694 mock_p2p_manager.fake().SetP2PEnabled(true);
695 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
696 attempter_.UpdateEngineStarted();
697}
698
699TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
700 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700701 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700702 mock_p2p_manager.fake().SetP2PEnabled(true);
703 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700704 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700705 attempter_.UpdateEngineStarted();
706}
707
708TEST_F(UpdateAttempterTest, P2PNotEnabled) {
709 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
710 g_idle_add(&StaticP2PNotEnabled, this);
711 g_main_loop_run(loop_);
712 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700713 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700714}
715gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
716 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
717 ua_test->P2PNotEnabledStart();
718 return FALSE;
719}
720void UpdateAttempterTest::P2PNotEnabledStart() {
721 // If P2P is not enabled, check that we do not attempt housekeeping
722 // and do not convey that p2p is to be used.
723 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700724 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700725 mock_p2p_manager.fake().SetP2PEnabled(false);
726 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700727 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700728 EXPECT_FALSE(actual_using_p2p_for_downloading());
729 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700730 g_idle_add(&StaticQuitMainLoop, this);
731}
732
733TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
734 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
735 g_idle_add(&StaticP2PEnabledStartingFails, this);
736 g_main_loop_run(loop_);
737 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700738 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700739}
740gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
741 gpointer data) {
742 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
743 ua_test->P2PEnabledStartingFailsStart();
744 return FALSE;
745}
746void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
747 // If p2p is enabled, but starting it fails ensure we don't do
748 // any housekeeping and do not convey that p2p should be used.
749 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700750 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700751 mock_p2p_manager.fake().SetP2PEnabled(true);
752 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
753 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
754 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700755 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700756 EXPECT_FALSE(actual_using_p2p_for_downloading());
757 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700758 g_idle_add(&StaticQuitMainLoop, this);
759}
760
761TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
762 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
763 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
764 g_main_loop_run(loop_);
765 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700766 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700767}
768gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
769 gpointer data) {
770 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
771 ua_test->P2PEnabledHousekeepingFailsStart();
772 return FALSE;
773}
774void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
775 // If p2p is enabled, starting it works but housekeeping fails, ensure
776 // we do not convey p2p is to be used.
777 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700778 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700779 mock_p2p_manager.fake().SetP2PEnabled(true);
780 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
781 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700782 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700783 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700784 EXPECT_FALSE(actual_using_p2p_for_downloading());
785 EXPECT_FALSE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700786 g_idle_add(&StaticQuitMainLoop, this);
787}
788
789TEST_F(UpdateAttempterTest, P2PEnabled) {
790 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
791 g_idle_add(&StaticP2PEnabled, this);
792 g_main_loop_run(loop_);
793 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700794 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700795}
796gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
797 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
798 ua_test->P2PEnabledStart();
799 return FALSE;
800}
801void UpdateAttempterTest::P2PEnabledStart() {
802 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700803 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700804 // If P2P is enabled and starting it works, check that we performed
805 // housekeeping and that we convey p2p should be used.
806 mock_p2p_manager.fake().SetP2PEnabled(true);
807 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
808 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700809 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700810 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700811 EXPECT_TRUE(actual_using_p2p_for_downloading());
812 EXPECT_TRUE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700813 g_idle_add(&StaticQuitMainLoop, this);
814}
815
816TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
817 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
818 g_idle_add(&StaticP2PEnabledInteractive, this);
819 g_main_loop_run(loop_);
820 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700821 loop_ = nullptr;
David Zeuthen8f191b22013-08-06 12:27:50 -0700822}
823gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
824 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
825 ua_test->P2PEnabledInteractiveStart();
826 return FALSE;
827}
828void UpdateAttempterTest::P2PEnabledInteractiveStart() {
829 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700830 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700831 // For an interactive check, if P2P is enabled and starting it
832 // works, check that we performed housekeeping and that we convey
833 // p2p should be used for sharing but NOT for downloading.
834 mock_p2p_manager.fake().SetP2PEnabled(true);
835 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
836 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700837 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700838 attempter_.Update("", "", "", "", false, true /* interactive */);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700839 EXPECT_FALSE(actual_using_p2p_for_downloading());
840 EXPECT_TRUE(actual_using_p2p_for_sharing());
David Zeuthen8f191b22013-08-06 12:27:50 -0700841 g_idle_add(&StaticQuitMainLoop, this);
842}
843
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700844TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
845 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
846 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
847 g_main_loop_run(loop_);
848 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700849 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700850}
851
852// Tests that the scatter_factor_in_seconds value is properly fetched
853// from the device policy.
854void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700855 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700856
857 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
858 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
859
860 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700861 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700862
863 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
864 .WillRepeatedly(DoAll(
865 SetArgumentPointee<0>(scatter_factor_in_seconds),
866 Return(true)));
867
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700868 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700869 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
870
871 g_idle_add(&StaticQuitMainLoop, this);
872}
873
874TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
875 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
876 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
877 g_main_loop_run(loop_);
878 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700879 loop_ = nullptr;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700880}
881
882void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
883 // Tests that the scatter_factor_in_seconds value is properly fetched
884 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700885 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800886 FakePrefs fake_prefs;
887 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700888
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700889 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700890
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800891 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700892
Ben Chan9abb7632014-08-07 00:10:53 -0700893 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700894
895 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
896 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
897
898 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700899 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700900
901 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
902 .WillRepeatedly(DoAll(
903 SetArgumentPointee<0>(scatter_factor_in_seconds),
904 Return(true)));
905
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700906 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700907 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
908
909 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800910 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700911
Ben Chan9abb7632014-08-07 00:10:53 -0700912 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800913 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700914 EXPECT_EQ(initial_value - 1, new_value);
915
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700916 EXPECT_TRUE(
917 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700918
919 // However, if the count is already 0, it's not decremented. Test that.
920 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800921 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700922 attempter_.Update("", "", "", "", false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800923 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
924 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700925 EXPECT_EQ(initial_value, new_value);
926
927 g_idle_add(&StaticQuitMainLoop, this);
928}
929
Jay Srinivasan08fce042012-06-07 16:31:01 -0700930TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
931 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
932 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
933 g_main_loop_run(loop_);
934 g_main_loop_unref(loop_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700935 loop_ = nullptr;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700936}
937
938void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
939 // Tests that no scattering logic is enabled if the update check
940 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700941 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800942 FakePrefs fake_prefs;
943 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700944
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700945 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800946 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700947
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800948 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
949 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700950
951 // make sure scatter_factor is non-zero as scattering is disabled
952 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700953 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700954
955 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
956 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
957
958 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700959 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700960
961 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
962 .WillRepeatedly(DoAll(
963 SetArgumentPointee<0>(scatter_factor_in_seconds),
964 Return(true)));
965
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800966 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700967 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700968 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
969
970 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700971 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700972 EXPECT_FALSE(
973 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800974 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700975 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
976 EXPECT_FALSE(
977 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800978 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700979
980 g_idle_add(&StaticQuitMainLoop, this);
981}
982
David Zeuthen985b1122013-10-09 12:13:15 -0700983// Checks that we only report daily metrics at most every 24 hours.
984TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
985 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800986 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -0700987
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700988 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800989 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700990
991 Time epoch = Time::FromInternalValue(0);
992 fake_clock.SetWallclockTime(epoch);
993
994 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
995 // we should report.
996 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
997 // We should not report again if no time has passed.
998 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
999
1000 // We should not report if only 10 hours has passed.
1001 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1002 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1003
1004 // We should not report if only 24 hours - 1 sec has passed.
1005 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1006 TimeDelta::FromSeconds(1));
1007 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1008
1009 // We should report if 24 hours has passed.
1010 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1011 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1012
1013 // But then we should not report again..
1014 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1015
1016 // .. until another 24 hours has passed
1017 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1018 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1019 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1020 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1021 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1022
1023 // .. and another 24 hours
1024 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1025 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1026 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1027 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1028 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1029
1030 // If the span between time of reporting and present time is
1031 // negative, we report. This is in order to reset the timestamp and
1032 // avoid an edge condition whereby a distant point in the future is
1033 // in the state variable resulting in us never ever reporting again.
1034 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1035 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1036 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1037
1038 // In this case we should not update until the clock reads 71 + 24 = 95.
1039 // Check that.
1040 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1041 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1042 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1043 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1044 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -07001045}
1046
David Zeuthen3c55abd2013-10-14 12:48:03 -07001047TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1048 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001049 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001050 update_completed_marker);
1051
1052 FakeClock fake_clock;
1053 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001054 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001055
1056 Time boot_time;
1057 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1058
1059 attempter.WriteUpdateCompletedMarker();
1060
1061 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1062 EXPECT_EQ(boot_time.ToTimeT(), 42);
1063}
1064
David Pursell02c18642014-11-06 11:26:11 -08001065TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
1066 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
1067 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1068}
1069
1070TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
1071 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1072 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
1073 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
1074 StrEq(debugd::kQueryDevFeatures),
1075 _, A<gint*>()))
1076 .WillRepeatedly(DoAll(SetArgumentPointee<3>(0),
1077 Return(true)));
1078 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
1079}
1080
1081TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
1082 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1083 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
1084 // debugd should not be queried in this case.
1085 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
1086 StrEq(debugd::kQueryDevFeatures),
1087 _, A<gint*>()))
1088 .Times(0);
1089 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1090}
1091
1092TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedDebugdDisabled) {
1093 using debugd::DEV_FEATURES_DISABLED;
1094 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1095 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
1096 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
1097 StrEq(debugd::kQueryDevFeatures),
1098 _, A<gint*>()))
1099 .WillRepeatedly(DoAll(SetArgumentPointee<3>(DEV_FEATURES_DISABLED),
1100 Return(true)));
1101 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1102}
1103
1104TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedDebugdFailure) {
1105 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1106 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
1107 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
1108 StrEq(debugd::kQueryDevFeatures),
1109 _, A<gint*>()))
1110 .WillRepeatedly(Return(false));
1111 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1112}
1113
1114TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1115 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1116 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
1117 attempter_.CheckForUpdate("", "autest", true);
1118 EXPECT_EQ(chromeos_update_engine::kAUTestOmahaUrl,
1119 attempter_.forced_omaha_url());
1120}
1121
1122TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
1123 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1124 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
1125 attempter_.CheckForUpdate("", "autest-scheduled", true);
1126 EXPECT_EQ(chromeos_update_engine::kAUTestOmahaUrl,
1127 attempter_.forced_omaha_url());
1128}
1129
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001130} // namespace chromeos_update_engine