blob: 41a8232b4bd0554a1abad02e14f3b2732a78e90f [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>
Alex Deymo60ca1a72015-06-18 18:19:15 -070012#include <chromeos/bind_lambda.h>
David Pursell02c18642014-11-06 11:26:11 -080013#include <chromeos/dbus/service_constants.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070014#include <chromeos/message_loops/glib_message_loop.h>
15#include <chromeos/message_loops/message_loop.h>
16#include <chromeos/message_loops/message_loop_utils.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070017#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020018#include <policy/libpolicy.h>
19#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070020
David Zeuthen985b1122013-10-09 12:13:15 -070021#include "update_engine/fake_clock.h"
Alex Deymo2c0db7b2014-11-04 12:23:39 -080022#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070023#include "update_engine/fake_system_state.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070024#include "update_engine/filesystem_verifier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070025#include "update_engine/install_plan.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080026#include "update_engine/mock_action.h"
27#include "update_engine/mock_action_processor.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080028#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080029#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070030#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080031#include "update_engine/mock_payload_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080032#include "update_engine/mock_prefs.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070034#include "update_engine/prefs.h"
Darin Petkov1b003102010-11-30 10:18:36 -080035#include "update_engine/test_utils.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070036#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070037
David Zeuthen985b1122013-10-09 12:13:15 -070038using base::Time;
39using base::TimeDelta;
Alex Deymo60ca1a72015-06-18 18:19:15 -070040using chromeos::MessageLoop;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070041using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070042using std::unique_ptr;
David Pursell02c18642014-11-06 11:26:11 -080043using testing::A;
Darin Petkov36275772010-10-01 11:40:57 -070044using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070045using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070046using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080047using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070048using testing::Property;
49using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070050using testing::ReturnPointee;
Alex Deymo2c0db7b2014-11-04 12:23:39 -080051using testing::SaveArg;
Darin Petkov36275772010-10-01 11:40:57 -070052using testing::SetArgumentPointee;
David Pursell02c18642014-11-06 11:26:11 -080053using testing::StrEq;
Alex Deymof329b932014-10-30 01:37:48 -070054using testing::_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070055
56namespace chromeos_update_engine {
57
58// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070059// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070060// methods.
61class UpdateAttempterUnderTest : public UpdateAttempter {
62 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070063 // We always feed an explicit update completed marker name; however, unless
64 // explicitly specified, we feed an empty string, which causes the
65 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070066 UpdateAttempterUnderTest(SystemState* system_state,
67 DBusWrapperInterface* dbus_iface)
68 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070069
Gilad Arnold1f847232014-04-07 12:07:49 -070070 UpdateAttempterUnderTest(SystemState* system_state,
71 DBusWrapperInterface* dbus_iface,
Alex Deymof329b932014-10-30 01:37:48 -070072 const string& update_completed_marker)
Gilad Arnold1f847232014-04-07 12:07:49 -070073 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Gilad Arnoldec7f9162014-07-15 13:24:46 -070074
75 // Wrap the update scheduling method, allowing us to opt out of scheduled
76 // updates for testing purposes.
77 void ScheduleUpdates() override {
78 schedule_updates_called_ = true;
79 if (do_schedule_updates_) {
80 UpdateAttempter::ScheduleUpdates();
81 } else {
82 LOG(INFO) << "[TEST] Update scheduling disabled.";
83 }
84 }
85 void EnableScheduleUpdates() { do_schedule_updates_ = true; }
86 void DisableScheduleUpdates() { do_schedule_updates_ = false; }
87
88 // Indicates whether ScheduleUpdates() was called.
89 bool schedule_updates_called() const { return schedule_updates_called_; }
90
David Pursell02c18642014-11-06 11:26:11 -080091 // Need to expose forced_omaha_url_ so we can test it.
Alex Deymo60ca1a72015-06-18 18:19:15 -070092 const string& forced_omaha_url() const { return forced_omaha_url_; }
David Pursell02c18642014-11-06 11:26:11 -080093
Gilad Arnoldec7f9162014-07-15 13:24:46 -070094 private:
95 bool schedule_updates_called_ = false;
96 bool do_schedule_updates_ = true;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070097};
98
99class UpdateAttempterTest : public ::testing::Test {
100 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -0700101 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700102 : attempter_(&fake_system_state_, &dbus_),
103 mock_connection_manager(&fake_system_state_),
David Pursell02c18642014-11-06 11:26:11 -0800104 fake_dbus_system_bus_(reinterpret_cast<DBusGConnection*>(1)),
105 fake_dbus_debugd_proxy_(reinterpret_cast<DBusGProxy*>(2)) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700106 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700107 fake_system_state_.set_connection_manager(&mock_connection_manager);
108 fake_system_state_.set_update_attempter(&attempter_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700109 loop_.SetAsCurrent();
Gilad Arnold1f847232014-04-07 12:07:49 -0700110
111 // Finish initializing the attempter.
112 attempter_.Init();
Alex Deymo3e0b53e2014-08-12 23:12:25 -0700113
114 // We set the set_good_kernel command to a non-existent path so it fails to
115 // run it. This avoids the async call to the command and continues the
116 // update process right away. Tests testing that behavior can override the
117 // default set_good_kernel command if needed.
118 attempter_.set_good_kernel_cmd_ = "/path/to/non-existent/command";
Jay Srinivasan43488792012-06-19 00:25:31 -0700119 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700120
Alex Deymo610277e2014-11-11 21:18:11 -0800121 void SetUp() override {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700122 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
123
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700124 EXPECT_EQ(nullptr, attempter_.dbus_service_);
125 EXPECT_NE(nullptr, attempter_.system_state_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700126 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -0800127 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700128 EXPECT_EQ(MessageLoop::kTaskIdNull, attempter_.manage_shares_id_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700129 EXPECT_FALSE(attempter_.download_active_);
130 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
131 EXPECT_EQ(0.0, attempter_.download_progress_);
132 EXPECT_EQ(0, attempter_.last_checked_time_);
133 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700134 EXPECT_EQ(0, attempter_.new_payload_size_);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800135 processor_ = new NiceMock<MockActionProcessor>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700136 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700137 prefs_ = fake_system_state_.mock_prefs();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700138
139 // Set up store/load semantics of P2P properties via the mock PayloadState.
140 actual_using_p2p_for_downloading_ = false;
141 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
142 SetUsingP2PForDownloading(_))
143 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_downloading_));
144 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
145 GetUsingP2PForDownloading())
146 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_downloading_));
147 actual_using_p2p_for_sharing_ = false;
148 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
149 SetUsingP2PForSharing(_))
150 .WillRepeatedly(SaveArg<0>(&actual_using_p2p_for_sharing_));
151 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
152 GetUsingP2PForDownloading())
153 .WillRepeatedly(ReturnPointee(&actual_using_p2p_for_sharing_));
David Pursell02c18642014-11-06 11:26:11 -0800154
155 // Set up mock debugd access over the system D-Bus. ProxyCall_0_1() also
156 // needs to be mocked in any test using debugd to provide the desired value.
157 ON_CALL(dbus_, BusGet(DBUS_BUS_SYSTEM, _))
158 .WillByDefault(Return(fake_dbus_system_bus_));
159 ON_CALL(dbus_, ProxyNewForName(fake_dbus_system_bus_,
160 StrEq(debugd::kDebugdServiceName),
161 StrEq(debugd::kDebugdServicePath),
162 StrEq(debugd::kDebugdInterface)))
163 .WillByDefault(Return(fake_dbus_debugd_proxy_));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700164 }
165
Alex Deymo610277e2014-11-11 21:18:11 -0800166 void TearDown() override {
Alex Deymo10875d92014-11-10 21:52:57 -0800167 test_utils::RecursiveUnlinkDir(test_dir_);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700168 EXPECT_EQ(0, MessageLoopRunMaxIterations(&loop_, 1));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700169 }
170
Alex Deymo60ca1a72015-06-18 18:19:15 -0700171 public:
172 void ScheduleQuitMainLoop();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200173
Alex Deymo60ca1a72015-06-18 18:19:15 -0700174 // Callbacks to run the different tests from the main loop.
Darin Petkove6ef2f82011-03-07 17:31:11 -0800175 void UpdateTestStart();
176 void UpdateTestVerify();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700177 void RollbackTestStart(bool enterprise_rollback, bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700178 void RollbackTestVerify();
Thieu Le116fda32011-04-19 11:01:54 -0700179 void PingOmahaTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700180 void ReadScatterFactorFromPolicyTestStart();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700181 void DecrementUpdateCheckCountTestStart();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700182 void NoScatteringDoneDuringManualUpdateTestStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700183 void P2PNotEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700184 void P2PEnabledStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700185 void P2PEnabledInteractiveStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700186 void P2PEnabledStartingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700187 void P2PEnabledHousekeepingFailsStart();
David Zeuthen8f191b22013-08-06 12:27:50 -0700188
Gilad Arnold74b5f552014-10-07 08:17:16 -0700189 bool actual_using_p2p_for_downloading() {
190 return actual_using_p2p_for_downloading_;
191 }
192 bool actual_using_p2p_for_sharing() {
193 return actual_using_p2p_for_sharing_;
194 }
195
Alex Deymo60ca1a72015-06-18 18:19:15 -0700196 // TODO(deymo): Replace this with a FakeMessageLoop. Some of these tests use a
197 // real LibcurlHttpFetcher, which still requires a GlibMessageLoop.
198 chromeos::GlibMessageLoop loop_;
199
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700200 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800201 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700202 UpdateAttempterUnderTest attempter_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800203 NiceMock<MockActionProcessor>* processor_;
204 NiceMock<MockPrefs>* prefs_; // Shortcut to fake_system_state_->mock_prefs().
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800205 NiceMock<MockConnectionManager> mock_connection_manager;
David Pursell02c18642014-11-06 11:26:11 -0800206 // fake_dbus_xxx pointers will be non-null for comparison purposes, but won't
207 // be valid objects so don't try to use them.
208 DBusGConnection* fake_dbus_system_bus_;
209 DBusGProxy* fake_dbus_debugd_proxy_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700210
211 string test_dir_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700212
213 bool actual_using_p2p_for_downloading_;
214 bool actual_using_p2p_for_sharing_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700215};
216
Alex Deymo60ca1a72015-06-18 18:19:15 -0700217void UpdateAttempterTest::ScheduleQuitMainLoop() {
218 loop_.PostTask(FROM_HERE, base::Bind([this] { this->loop_.BreakLoop(); }));
219}
220
Darin Petkov1b003102010-11-30 10:18:36 -0800221TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700222 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800223 fetcher->FailTransfer(503); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700224 DownloadAction action(prefs_, nullptr, fetcher.release());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800225 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700226 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800227 EXPECT_EQ(503, attempter_.http_response_code());
228 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700229 ASSERT_EQ(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800230}
231
232TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800233 MockAction action;
234 EXPECT_CALL(action, Type()).WillRepeatedly(Return("MockAction"));
Darin Petkov1b003102010-11-30 10:18:36 -0800235 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800236 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800237 .WillOnce(Return(false));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700238 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kError);
239 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov1b003102010-11-30 10:18:36 -0800240}
241
242TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700243 unique_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, nullptr));
Darin Petkov1b003102010-11-30 10:18:36 -0800244 fetcher->FailTransfer(500); // Sets the HTTP response code.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700245 OmahaRequestAction action(&fake_system_state_, nullptr,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800246 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800247 ObjectCollectorAction<OmahaResponse> collector_action;
248 BondActions(&action, &collector_action);
249 OmahaResponse response;
250 response.poll_interval = 234;
251 action.SetOutputObject(response);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800252 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700253 attempter_.ActionCompleted(nullptr, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800254 EXPECT_EQ(500, attempter_.http_response_code());
255 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700256 EXPECT_EQ(234, attempter_.server_dictated_poll_interval_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700257 ASSERT_TRUE(attempter_.error_event_.get() == nullptr);
Darin Petkov1b003102010-11-30 10:18:36 -0800258}
259
Darin Petkovcd1666f2010-09-23 09:53:44 -0700260TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700261 string test_update_completed_marker;
262 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800263 "update_attempter_unittest-update_completed_marker-XXXXXX",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700264 &test_update_completed_marker, nullptr));
Gilad Arnold70e476e2013-07-30 16:01:13 -0700265 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700266 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700267 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700268 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700269 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700270 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700271}
272
273TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700274 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
275 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700276 EXPECT_EQ(ErrorCode::kSuccess,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700277 GetErrorCodeForAction(nullptr, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700278
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700279 FakeSystemState fake_system_state;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700280 OmahaRequestAction omaha_request_action(&fake_system_state, nullptr,
281 nullptr, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700282 EXPECT_EQ(ErrorCode::kOmahaRequestError,
283 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700284 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700285 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700286 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700287 ErrorCode::kError));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700288 FilesystemVerifierAction filesystem_verifier_action(
289 &fake_system_state_, PartitionType::kRootfs);
290 EXPECT_EQ(ErrorCode::kFilesystemVerifierError,
291 GetErrorCodeForAction(&filesystem_verifier_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700292 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800293 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700294 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700295 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700296 ErrorCode::kError));
Alex Deymo8427b4a2014-11-05 14:00:32 -0800297 MockAction action_mock;
298 EXPECT_CALL(action_mock, Type()).WillOnce(Return("MockAction"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700299 EXPECT_EQ(ErrorCode::kError,
300 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700301}
302
Darin Petkov36275772010-10-01 11:40:57 -0700303TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700304 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800305 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700306 .WillOnce(Return(false));
307 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700308 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800309 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700310 .WillOnce(DoAll(
311 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
312 Return(true)));
313 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700314 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800315 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700316 .WillOnce(DoAll(
317 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
318 Return(true)));
319 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700320 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800321 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700322 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700323 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700324}
325
326TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800327 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700328 .WillOnce(Return(false))
329 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
330 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
331 .WillOnce(DoAll(
332 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
333 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800334 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700335 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800336 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700337 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800338 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700339 UpdateAttempter::kMaxDeltaUpdateFailures + 1));
Darin Petkov36275772010-10-01 11:40:57 -0700340 for (int i = 0; i < 4; i ++)
341 attempter_.MarkDeltaUpdateFailure();
342}
343
Darin Petkov1b003102010-11-30 10:18:36 -0800344TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
345 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
346 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700347 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800348 .Times(0);
349 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700350 string url1 = "http://url1";
351 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800352 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700353 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700354 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700355 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800356 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700357 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800358}
359
360TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
361 EXPECT_CALL(*processor_,
362 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700363 OmahaRequestAction::StaticType())));
364 EXPECT_CALL(*processor_, StartProcessing());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700365 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700366 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800367 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
368 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800369 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800370 attempter_.ScheduleErrorEventAction();
371 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
372}
373
Darin Petkove6ef2f82011-03-07 17:31:11 -0800374namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700375// Actions that will be built as part of an update check.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700376const string kUpdateActionTypes[] = { // NOLINT(runtime/string)
Darin Petkove6ef2f82011-03-07 17:31:11 -0800377 OmahaRequestAction::StaticType(),
378 OmahaResponseHandlerAction::StaticType(),
Allie Woodeb9e6d82015-04-17 13:55:30 -0700379 FilesystemVerifierAction::StaticType(),
380 FilesystemVerifierAction::StaticType(),
Darin Petkove6ef2f82011-03-07 17:31:11 -0800381 OmahaRequestAction::StaticType(),
382 DownloadAction::StaticType(),
383 OmahaRequestAction::StaticType(),
Allie Woodeb9e6d82015-04-17 13:55:30 -0700384 FilesystemVerifierAction::StaticType(),
385 FilesystemVerifierAction::StaticType(),
Darin Petkove6ef2f82011-03-07 17:31:11 -0800386 PostinstallRunnerAction::StaticType(),
387 OmahaRequestAction::StaticType()
388};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700389
390// Actions that will be built as part of a user-initiated rollback.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700391const string kRollbackActionTypes[] = { // NOLINT(runtime/string)
Chris Sosa76a29ae2013-07-11 17:59:24 -0700392 InstallPlanAction::StaticType(),
393 PostinstallRunnerAction::StaticType(),
394};
395
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700396} // namespace
Darin Petkove6ef2f82011-03-07 17:31:11 -0800397
398void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700399 attempter_.set_http_response_code(200);
Alex Deymo749ecf12014-10-21 20:06:57 -0700400
401 // Expect that the device policy is loaded by the UpdateAttempter at some
402 // point by calling RefreshDevicePolicy.
403 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
404 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
405 EXPECT_CALL(*device_policy, LoadPolicy())
406 .Times(testing::AtLeast(1)).WillRepeatedly(Return(true));
407
408 {
409 InSequence s;
410 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
411 EXPECT_CALL(*processor_,
412 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700413 kUpdateActionTypes[i])));
Alex Deymo749ecf12014-10-21 20:06:57 -0700414 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700415 EXPECT_CALL(*processor_, StartProcessing());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700416 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700417
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700418 attempter_.Update("", "", "", "", false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700419 loop_.PostTask(FROM_HERE,
420 base::Bind(&UpdateAttempterTest::UpdateTestVerify,
421 base::Unretained(this)));
Darin Petkove6ef2f82011-03-07 17:31:11 -0800422}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700423
Darin Petkove6ef2f82011-03-07 17:31:11 -0800424void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700425 EXPECT_EQ(0, attempter_.http_response_code());
426 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700427 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
428 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
429 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700430 }
431 EXPECT_EQ(attempter_.response_handler_action_.get(),
432 attempter_.actions_[1].get());
433 DownloadAction* download_action =
434 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700435 ASSERT_NE(nullptr, download_action);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700436 EXPECT_EQ(&attempter_, download_action->delegate());
437 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700438 loop_.BreakLoop();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800439}
440
Chris Sosa28e479c2013-07-12 11:39:53 -0700441void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700442 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700443 // Create a device policy so that we can change settings.
444 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
445 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
446
447 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700448 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700449
Don Garrett6646b442013-11-13 15:29:11 -0800450 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700451 // References bootable kernels in fake_hardware.h
452 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800453 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700454 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800455 rollback_kernel);
456 }
457
Chris Sosa28e479c2013-07-12 11:39:53 -0700458 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700459
Chris Sosad38b1132014-03-25 10:43:59 -0700460 // We only allow rollback on devices that are not enterprise enrolled and
461 // which have a valid slot to rollback to.
462 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700463 is_rollback_allowed = true;
464 }
465
Don Garrett6646b442013-11-13 15:29:11 -0800466 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700467 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800468 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700469 DoAll(SetArgumentPointee<0>(string("")),
Chris Sosa28e479c2013-07-12 11:39:53 -0700470 Return(true)));
471 } else {
472 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800473 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700474 DoAll(SetArgumentPointee<0>(string("fake.mail@fake.com")),
Chris Sosa76a29ae2013-07-11 17:59:24 -0700475 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700476 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700477
Chris Sosa28e479c2013-07-12 11:39:53 -0700478 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700479 InSequence s;
480 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
481 EXPECT_CALL(*processor_,
482 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700483 kRollbackActionTypes[i])));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700484 }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700485 EXPECT_CALL(*processor_, StartProcessing());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700486
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700487 EXPECT_TRUE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700488 loop_.PostTask(FROM_HERE,
489 base::Bind(&UpdateAttempterTest::RollbackTestVerify,
490 base::Unretained(this)));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700491 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700492 EXPECT_FALSE(attempter_.Rollback(true));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700493 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700494 }
495}
496
497void UpdateAttempterTest::RollbackTestVerify() {
498 // Verifies the actions that were enqueued.
499 EXPECT_EQ(&attempter_, processor_->delegate());
500 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
501 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
502 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
503 }
504 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
505 InstallPlanAction* install_plan_action =
506 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
507 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700508 // Matches fake_hardware.h -> rollback should move from kernel/boot device
509 // pair to other pair.
510 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
511 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700512 EXPECT_EQ(install_plan->powerwash_required, true);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700513 loop_.BreakLoop();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700514}
515
Darin Petkove6ef2f82011-03-07 17:31:11 -0800516TEST_F(UpdateAttempterTest, UpdateTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700517 loop_.PostTask(FROM_HERE,
518 base::Bind(&UpdateAttempterTest::UpdateTestStart,
519 base::Unretained(this)));
520 loop_.Run();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700521}
522
Chris Sosa76a29ae2013-07-11 17:59:24 -0700523TEST_F(UpdateAttempterTest, RollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700524 loop_.PostTask(FROM_HERE,
525 base::Bind(&UpdateAttempterTest::RollbackTestStart,
526 base::Unretained(this),
527 false, true));
528 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700529}
530
Don Garrett6646b442013-11-13 15:29:11 -0800531TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700532 loop_.PostTask(FROM_HERE,
533 base::Bind(&UpdateAttempterTest::RollbackTestStart,
534 base::Unretained(this),
535 false, false));
536 loop_.Run();
Don Garrett6646b442013-11-13 15:29:11 -0800537}
538
Chris Sosa76a29ae2013-07-11 17:59:24 -0700539TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700540 loop_.PostTask(FROM_HERE,
541 base::Bind(&UpdateAttempterTest::RollbackTestStart,
542 base::Unretained(this),
543 true, true));
544 loop_.Run();
Chris Sosa76a29ae2013-07-11 17:59:24 -0700545}
546
Thieu Le116fda32011-04-19 11:01:54 -0700547void UpdateAttempterTest::PingOmahaTestStart() {
548 EXPECT_CALL(*processor_,
549 EnqueueAction(Property(&AbstractAction::Type,
Gilad Arnold74b5f552014-10-07 08:17:16 -0700550 OmahaRequestAction::StaticType())));
551 EXPECT_CALL(*processor_, StartProcessing());
Thieu Le116fda32011-04-19 11:01:54 -0700552 attempter_.PingOmaha();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700553 ScheduleQuitMainLoop();
Thieu Le116fda32011-04-19 11:01:54 -0700554}
555
556TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700557 EXPECT_FALSE(attempter_.waiting_for_scheduled_check_);
558 EXPECT_FALSE(attempter_.schedule_updates_called());
559 // Disable scheduling of subsequnet checks; we're using the DefaultPolicy in
560 // testing, which is more permissive than we want to handle here.
561 attempter_.DisableScheduleUpdates();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700562 loop_.PostTask(FROM_HERE,
563 base::Bind(&UpdateAttempterTest::PingOmahaTestStart,
564 base::Unretained(this)));
565 chromeos::MessageLoopRunMaxIterations(&loop_, 100);
Thieu Le116fda32011-04-19 11:01:54 -0700566 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700567 EXPECT_TRUE(attempter_.schedule_updates_called());
Thieu Le116fda32011-04-19 11:01:54 -0700568}
569
Darin Petkov18c7bce2011-06-16 14:07:00 -0700570TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800571 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700572 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700573 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700574 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700575 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
576 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700577 EXPECT_EQ(
578 static_cast<ErrorCode>(static_cast<int>(kCode) |
579 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
580 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700581}
582
583TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
584 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700585 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700586 response_action->install_plan_.is_resume = true;
587 attempter_.response_handler_action_.reset(response_action);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800588 MockAction action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700589 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700590 attempter_.CreatePendingErrorEvent(&action, kCode);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700591 ASSERT_NE(nullptr, attempter_.error_event_.get());
Darin Petkov18c7bce2011-06-16 14:07:00 -0700592 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
593 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700594 EXPECT_EQ(
595 static_cast<ErrorCode>(
596 static_cast<int>(kCode) |
597 static_cast<int>(ErrorCode::kResumedFlag) |
598 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
599 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700600}
601
David Zeuthen8f191b22013-08-06 12:27:50 -0700602TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
603 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700604 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700605 mock_p2p_manager.fake().SetP2PEnabled(false);
606 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
607 attempter_.UpdateEngineStarted();
608}
609
610TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
611 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700612 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700613 mock_p2p_manager.fake().SetP2PEnabled(true);
614 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
615 attempter_.UpdateEngineStarted();
616}
617
618TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
619 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700620 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700621 mock_p2p_manager.fake().SetP2PEnabled(true);
622 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700623 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning());
David Zeuthen8f191b22013-08-06 12:27:50 -0700624 attempter_.UpdateEngineStarted();
625}
626
627TEST_F(UpdateAttempterTest, P2PNotEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700628 loop_.PostTask(FROM_HERE,
629 base::Bind(&UpdateAttempterTest::P2PNotEnabledStart,
630 base::Unretained(this)));
631 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700632}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700633
David Zeuthen8f191b22013-08-06 12:27:50 -0700634void UpdateAttempterTest::P2PNotEnabledStart() {
635 // If P2P is not enabled, check that we do not attempt housekeeping
636 // and do not convey that p2p is to be used.
637 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700638 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700639 mock_p2p_manager.fake().SetP2PEnabled(false);
640 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700641 attempter_.Update("", "", "", "", false, false);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700642 EXPECT_FALSE(actual_using_p2p_for_downloading_);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700643 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700644 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700645}
646
647TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700648 loop_.PostTask(FROM_HERE,
649 base::Bind(&UpdateAttempterTest::P2PEnabledStartingFailsStart,
650 base::Unretained(this)));
651 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700652}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700653
David Zeuthen8f191b22013-08-06 12:27:50 -0700654void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
655 // If p2p is enabled, but starting it fails ensure we don't do
656 // any housekeeping and do not convey that p2p should be used.
657 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700658 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700659 mock_p2p_manager.fake().SetP2PEnabled(true);
660 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
661 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
662 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700663 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700664 EXPECT_FALSE(actual_using_p2p_for_downloading());
665 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700666 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700667}
668
669TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700670 loop_.PostTask(
671 FROM_HERE,
672 base::Bind(&UpdateAttempterTest::P2PEnabledHousekeepingFailsStart,
673 base::Unretained(this)));
674 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700675}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700676
David Zeuthen8f191b22013-08-06 12:27:50 -0700677void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
678 // If p2p is enabled, starting it works but housekeeping fails, ensure
679 // we do not convey p2p is to be used.
680 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700681 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700682 mock_p2p_manager.fake().SetP2PEnabled(true);
683 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
684 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700685 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700686 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700687 EXPECT_FALSE(actual_using_p2p_for_downloading());
688 EXPECT_FALSE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700689 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700690}
691
692TEST_F(UpdateAttempterTest, P2PEnabled) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700693 loop_.PostTask(FROM_HERE,
694 base::Bind(&UpdateAttempterTest::P2PEnabledStart,
695 base::Unretained(this)));
696 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700697}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700698
David Zeuthen8f191b22013-08-06 12:27:50 -0700699void UpdateAttempterTest::P2PEnabledStart() {
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 // If P2P is enabled and starting it works, check that we performed
703 // housekeeping and that we convey p2p should be used.
704 mock_p2p_manager.fake().SetP2PEnabled(true);
705 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
706 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700707 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700708 attempter_.Update("", "", "", "", false, false);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700709 EXPECT_TRUE(actual_using_p2p_for_downloading());
710 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700711 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700712}
713
714TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700715 loop_.PostTask(FROM_HERE,
716 base::Bind(&UpdateAttempterTest::P2PEnabledInteractiveStart,
717 base::Unretained(this)));
718 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700719}
Alex Deymo60ca1a72015-06-18 18:19:15 -0700720
David Zeuthen8f191b22013-08-06 12:27:50 -0700721void UpdateAttempterTest::P2PEnabledInteractiveStart() {
722 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700723 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700724 // For an interactive check, if P2P is enabled and starting it
725 // works, check that we performed housekeeping and that we convey
726 // p2p should be used for sharing but NOT for downloading.
727 mock_p2p_manager.fake().SetP2PEnabled(true);
728 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
729 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700730 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping());
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700731 attempter_.Update("", "", "", "", false, true /* interactive */);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700732 EXPECT_FALSE(actual_using_p2p_for_downloading());
733 EXPECT_TRUE(actual_using_p2p_for_sharing());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700734 ScheduleQuitMainLoop();
David Zeuthen8f191b22013-08-06 12:27:50 -0700735}
736
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700737TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700738 loop_.PostTask(
739 FROM_HERE,
740 base::Bind(&UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart,
741 base::Unretained(this)));
742 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700743}
744
745// Tests that the scatter_factor_in_seconds value is properly fetched
746// from the device policy.
747void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
Ben Chan9abb7632014-08-07 00:10:53 -0700748 int64_t scatter_factor_in_seconds = 36000;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700749
750 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
751 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
752
753 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700754 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700755
756 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
757 .WillRepeatedly(DoAll(
758 SetArgumentPointee<0>(scatter_factor_in_seconds),
759 Return(true)));
760
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700761 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700762 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
763
Alex Deymo60ca1a72015-06-18 18:19:15 -0700764 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700765}
766
767TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700768 loop_.PostTask(
769 FROM_HERE,
770 base::Bind(&UpdateAttempterTest::DecrementUpdateCheckCountTestStart,
771 base::Unretained(this)));
772 loop_.Run();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700773}
774
775void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
776 // Tests that the scatter_factor_in_seconds value is properly fetched
777 // from the device policy and is decremented if value > 0.
Ben Chan9abb7632014-08-07 00:10:53 -0700778 int64_t initial_value = 5;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800779 FakePrefs fake_prefs;
780 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700781
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700782 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700783
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800784 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700785
Ben Chan9abb7632014-08-07 00:10:53 -0700786 int64_t scatter_factor_in_seconds = 10;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700787
788 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
789 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
790
791 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700792 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700793
794 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
795 .WillRepeatedly(DoAll(
796 SetArgumentPointee<0>(scatter_factor_in_seconds),
797 Return(true)));
798
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700799 attempter_.Update("", "", "", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700800 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
801
802 // Make sure the file still exists.
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800803 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700804
Ben Chan9abb7632014-08-07 00:10:53 -0700805 int64_t new_value;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800806 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700807 EXPECT_EQ(initial_value - 1, new_value);
808
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700809 EXPECT_TRUE(
810 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700811
812 // However, if the count is already 0, it's not decremented. Test that.
813 initial_value = 0;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800814 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700815 attempter_.Update("", "", "", "", false, false);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800816 EXPECT_TRUE(fake_prefs.Exists(kPrefsUpdateCheckCount));
817 EXPECT_TRUE(fake_prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700818 EXPECT_EQ(initial_value, new_value);
819
Alex Deymo60ca1a72015-06-18 18:19:15 -0700820 ScheduleQuitMainLoop();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700821}
822
Jay Srinivasan08fce042012-06-07 16:31:01 -0700823TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700824 loop_.PostTask(FROM_HERE, base::Bind(
825 &UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart,
826 base::Unretained(this)));
827 loop_.Run();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700828}
829
830void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
831 // Tests that no scattering logic is enabled if the update check
832 // is manually done (as opposed to a scheduled update check)
Ben Chan9abb7632014-08-07 00:10:53 -0700833 int64_t initial_value = 8;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800834 FakePrefs fake_prefs;
835 attempter_.prefs_ = &fake_prefs;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700836
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700837 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800838 fake_system_state_.set_prefs(&fake_prefs);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700839
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800840 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
841 EXPECT_TRUE(fake_prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700842
843 // make sure scatter_factor is non-zero as scattering is disabled
844 // otherwise.
Ben Chan9abb7632014-08-07 00:10:53 -0700845 int64_t scatter_factor_in_seconds = 50;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700846
847 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
848 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
849
850 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700851 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700852
853 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
854 .WillRepeatedly(DoAll(
855 SetArgumentPointee<0>(scatter_factor_in_seconds),
856 Return(true)));
857
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800858 // Trigger an interactive check so we can test that scattering is disabled.
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700859 attempter_.Update("", "", "", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700860 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
861
862 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700863 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700864 EXPECT_FALSE(
865 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800866 EXPECT_FALSE(fake_prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700867 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
868 EXPECT_FALSE(
869 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800870 EXPECT_FALSE(fake_prefs.Exists(kPrefsUpdateCheckCount));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700871
Alex Deymo60ca1a72015-06-18 18:19:15 -0700872 ScheduleQuitMainLoop();
Jay Srinivasan08fce042012-06-07 16:31:01 -0700873}
874
David Zeuthen985b1122013-10-09 12:13:15 -0700875// Checks that we only report daily metrics at most every 24 hours.
876TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
877 FakeClock fake_clock;
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800878 FakePrefs fake_prefs;
David Zeuthen985b1122013-10-09 12:13:15 -0700879
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700880 fake_system_state_.set_clock(&fake_clock);
Alex Deymo2c0db7b2014-11-04 12:23:39 -0800881 fake_system_state_.set_prefs(&fake_prefs);
David Zeuthen985b1122013-10-09 12:13:15 -0700882
883 Time epoch = Time::FromInternalValue(0);
884 fake_clock.SetWallclockTime(epoch);
885
886 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
887 // we should report.
888 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
889 // We should not report again if no time has passed.
890 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
891
892 // We should not report if only 10 hours has passed.
893 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
894 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
895
896 // We should not report if only 24 hours - 1 sec has passed.
897 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
898 TimeDelta::FromSeconds(1));
899 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
900
901 // We should report if 24 hours has passed.
902 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
903 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
904
905 // But then we should not report again..
906 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
907
908 // .. until another 24 hours has passed
909 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
910 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
911 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
912 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
913 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
914
915 // .. and another 24 hours
916 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
917 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
918 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
919 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
920 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
921
922 // If the span between time of reporting and present time is
923 // negative, we report. This is in order to reset the timestamp and
924 // avoid an edge condition whereby a distant point in the future is
925 // in the state variable resulting in us never ever reporting again.
926 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
927 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
928 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
929
930 // In this case we should not update until the clock reads 71 + 24 = 95.
931 // Check that.
932 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
933 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
934 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
935 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
936 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
David Zeuthen985b1122013-10-09 12:13:15 -0700937}
938
David Zeuthen3c55abd2013-10-14 12:48:03 -0700939TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
940 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700941 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -0700942 update_completed_marker);
943
944 FakeClock fake_clock;
945 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700946 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -0700947
948 Time boot_time;
949 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
950
951 attempter.WriteUpdateCompletedMarker();
952
953 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
954 EXPECT_EQ(boot_time.ToTimeT(), 42);
955}
956
David Pursell02c18642014-11-06 11:26:11 -0800957TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedUnofficial) {
958 fake_system_state_.fake_hardware()->SetIsOfficialBuild(false);
959 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
960}
961
962TEST_F(UpdateAttempterTest, AnyUpdateSourceAllowedOfficialDevmode) {
963 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
964 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
965 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
966 StrEq(debugd::kQueryDevFeatures),
967 _, A<gint*>()))
968 .WillRepeatedly(DoAll(SetArgumentPointee<3>(0),
969 Return(true)));
970 EXPECT_TRUE(attempter_.IsAnyUpdateSourceAllowed());
971}
972
973TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedOfficialNormal) {
974 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
975 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
976 // debugd should not be queried in this case.
977 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
978 StrEq(debugd::kQueryDevFeatures),
979 _, A<gint*>()))
980 .Times(0);
981 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
982}
983
984TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedDebugdDisabled) {
985 using debugd::DEV_FEATURES_DISABLED;
986 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
987 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
988 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
989 StrEq(debugd::kQueryDevFeatures),
990 _, A<gint*>()))
991 .WillRepeatedly(DoAll(SetArgumentPointee<3>(DEV_FEATURES_DISABLED),
992 Return(true)));
993 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
994}
995
996TEST_F(UpdateAttempterTest, AnyUpdateSourceDisallowedDebugdFailure) {
997 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
998 fake_system_state_.fake_hardware()->SetIsNormalBootMode(false);
999 EXPECT_CALL(dbus_, ProxyCall_0_1(fake_dbus_debugd_proxy_,
1000 StrEq(debugd::kQueryDevFeatures),
1001 _, A<gint*>()))
1002 .WillRepeatedly(Return(false));
1003 EXPECT_FALSE(attempter_.IsAnyUpdateSourceAllowed());
1004}
1005
1006TEST_F(UpdateAttempterTest, CheckForUpdateAUTest) {
1007 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1008 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
1009 attempter_.CheckForUpdate("", "autest", true);
1010 EXPECT_EQ(chromeos_update_engine::kAUTestOmahaUrl,
1011 attempter_.forced_omaha_url());
1012}
1013
1014TEST_F(UpdateAttempterTest, CheckForUpdateScheduledAUTest) {
1015 fake_system_state_.fake_hardware()->SetIsOfficialBuild(true);
1016 fake_system_state_.fake_hardware()->SetIsNormalBootMode(true);
1017 attempter_.CheckForUpdate("", "autest-scheduled", true);
1018 EXPECT_EQ(chromeos_update_engine::kAUTestOmahaUrl,
1019 attempter_.forced_omaha_url());
1020}
1021
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001022} // namespace chromeos_update_engine