blob: aa0af3e157cb2d44f9e8dd1ac7c540eaddd75f2a [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
5#include <base/file_util.h>
6#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +02007#include <policy/libpolicy.h>
8#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -07009
10#include "update_engine/action_mock.h"
11#include "update_engine/action_processor_mock.h"
David Zeuthen985b1122013-10-09 12:13:15 -070012#include "update_engine/fake_clock.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070013#include "update_engine/fake_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070014#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070015#include "update_engine/install_plan.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080016#include "update_engine/mock_dbus_wrapper.h"
Darin Petkov1b003102010-11-30 10:18:36 -080017#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070018#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019#include "update_engine/mock_payload_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070020#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070021#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070022#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080023#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080025#include "update_engine/update_check_scheduler.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070026#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070027
David Zeuthen985b1122013-10-09 12:13:15 -070028using base::Time;
29using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070031using testing::_;
32using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070034using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080035using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070036using testing::Property;
37using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070038using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070039
40namespace chromeos_update_engine {
41
42// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070043// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070044// methods.
45class UpdateAttempterUnderTest : public UpdateAttempter {
46 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070047 // We always feed an explicit update completed marker name; however, unless
48 // explicitly specified, we feed an empty string, which causes the
49 // UpdateAttempter class to ignore / not write the marker file.
Gilad Arnold1f847232014-04-07 12:07:49 -070050 UpdateAttempterUnderTest(SystemState* system_state,
51 DBusWrapperInterface* dbus_iface)
52 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070053
Gilad Arnold1f847232014-04-07 12:07:49 -070054 UpdateAttempterUnderTest(SystemState* system_state,
55 DBusWrapperInterface* dbus_iface,
56 const std::string& update_completed_marker)
57 : UpdateAttempter(system_state, dbus_iface, update_completed_marker) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070058};
59
60class UpdateAttempterTest : public ::testing::Test {
61 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070062 UpdateAttempterTest()
Gilad Arnold5bb4c902014-04-10 12:32:13 -070063 : attempter_(&fake_system_state_, &dbus_),
64 mock_connection_manager(&fake_system_state_),
Jay Srinivasan43488792012-06-19 00:25:31 -070065 loop_(NULL) {
Gilad Arnold1f847232014-04-07 12:07:49 -070066 // Override system state members.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070067 fake_system_state_.set_connection_manager(&mock_connection_manager);
68 fake_system_state_.set_update_attempter(&attempter_);
Gilad Arnold1f847232014-04-07 12:07:49 -070069
70 // Finish initializing the attempter.
71 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -070072 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070073
Darin Petkovf42cc1c2010-09-01 09:03:02 -070074 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070075 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
76
Darin Petkovf42cc1c2010-09-01 09:03:02 -070077 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080078 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070079 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
80 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080081 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
82 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070083 EXPECT_FALSE(attempter_.download_active_);
84 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
85 EXPECT_EQ(0.0, attempter_.download_progress_);
86 EXPECT_EQ(0, attempter_.last_checked_time_);
87 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070088 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080089 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070090 attempter_.processor_.reset(processor_); // Transfers ownership.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070091 prefs_ = fake_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070092 }
93
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070094 virtual void TearDown() {
95 utils::RecursiveUnlinkDir(test_dir_);
96 }
97
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020098 void QuitMainLoop();
99 static gboolean StaticQuitMainLoop(gpointer data);
100
Darin Petkove6ef2f82011-03-07 17:31:11 -0800101 void UpdateTestStart();
102 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800103 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800104 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700105 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800106 static gboolean StaticUpdateTestStart(gpointer data);
107 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700108 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800109 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700110 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
111 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200112
Thieu Le116fda32011-04-19 11:01:54 -0700113 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700114 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200115
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700116 void ReadChannelFromPolicyTestStart();
117 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800118
Jay Srinivasan0a708742012-03-20 11:26:12 -0700119 void ReadUpdateDisabledFromPolicyTestStart();
120 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
121
122 void ReadTargetVersionPrefixFromPolicyTestStart();
123 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
124 gpointer data);
125
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700126 void ReadScatterFactorFromPolicyTestStart();
127 static gboolean StaticReadScatterFactorFromPolicyTestStart(
128 gpointer data);
129
130 void DecrementUpdateCheckCountTestStart();
131 static gboolean StaticDecrementUpdateCheckCountTestStart(
132 gpointer data);
133
Jay Srinivasan08fce042012-06-07 16:31:01 -0700134 void NoScatteringDoneDuringManualUpdateTestStart();
135 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
136 gpointer data);
137
David Zeuthen8f191b22013-08-06 12:27:50 -0700138 void P2PNotEnabledStart();
139 static gboolean StaticP2PNotEnabled(gpointer data);
140
141 void P2PEnabledStart();
142 static gboolean StaticP2PEnabled(gpointer data);
143
144 void P2PEnabledInteractiveStart();
145 static gboolean StaticP2PEnabledInteractive(gpointer data);
146
147 void P2PEnabledStartingFailsStart();
148 static gboolean StaticP2PEnabledStartingFails(gpointer data);
149
150 void P2PEnabledHousekeepingFailsStart();
151 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
152
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700153 FakeSystemState fake_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800154 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700155 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800156 NiceMock<ActionProcessorMock>* processor_;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700157 NiceMock<PrefsMock>* prefs_; // shortcut to fake_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800158 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800159 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700160
161 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700162};
163
Darin Petkov1b003102010-11-30 10:18:36 -0800164TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
165 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
166 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800167 DownloadAction action(prefs_, NULL, fetcher.release());
168 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700169 attempter_.ActionCompleted(NULL, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800170 EXPECT_EQ(503, attempter_.http_response_code());
171 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
172 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
173}
174
175TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
176 ActionMock action;
177 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
178 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800179 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800180 .WillOnce(Return(false));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700181 attempter_.ActionCompleted(NULL, &action, ErrorCode::kError);
Darin Petkov1b003102010-11-30 10:18:36 -0800182 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
183}
184
185TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
186 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
187 fetcher->FailTransfer(500); // Sets the HTTP response code.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700188 OmahaRequestAction action(&fake_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800189 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800190 ObjectCollectorAction<OmahaResponse> collector_action;
191 BondActions(&action, &collector_action);
192 OmahaResponse response;
193 response.poll_interval = 234;
194 action.SetOutputObject(response);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700195 UpdateCheckScheduler scheduler(&attempter_, &fake_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800196 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800197 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700198 attempter_.ActionCompleted(NULL, &action, ErrorCode::kSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800199 EXPECT_EQ(500, attempter_.http_response_code());
200 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
201 EXPECT_EQ(234, scheduler.poll_interval());
202 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
203}
204
Darin Petkovcd1666f2010-09-23 09:53:44 -0700205TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700206 string test_update_completed_marker;
207 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800208 "update_attempter_unittest-update_completed_marker-XXXXXX",
Gilad Arnold70e476e2013-07-30 16:01:13 -0700209 &test_update_completed_marker, NULL));
210 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700211 const base::FilePath marker(test_update_completed_marker);
Ben Chan736fcb52014-05-21 18:28:22 -0700212 EXPECT_EQ(0, base::WriteFile(marker, "", 0));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700213 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
Gilad Arnold70e476e2013-07-30 16:01:13 -0700214 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700215 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700216}
217
218TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700219 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
220 ErrorCode code);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700221 EXPECT_EQ(ErrorCode::kSuccess,
222 GetErrorCodeForAction(NULL, ErrorCode::kSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700223
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700224 FakeSystemState fake_system_state;
225 OmahaRequestAction omaha_request_action(&fake_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800226 NULL, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700227 EXPECT_EQ(ErrorCode::kOmahaRequestError,
228 GetErrorCodeForAction(&omaha_request_action, ErrorCode::kError));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700229 OmahaResponseHandlerAction omaha_response_handler_action(&fake_system_state_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700230 EXPECT_EQ(ErrorCode::kOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700231 GetErrorCodeForAction(&omaha_response_handler_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700232 ErrorCode::kError));
Alex Deymo42432912013-07-12 20:21:15 -0700233 FilesystemCopierAction filesystem_copier_action(
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700234 &fake_system_state_, false, false);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700235 EXPECT_EQ(ErrorCode::kFilesystemCopierError,
236 GetErrorCodeForAction(&filesystem_copier_action,
237 ErrorCode::kError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800238 PostinstallRunnerAction postinstall_runner_action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700239 EXPECT_EQ(ErrorCode::kPostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700240 GetErrorCodeForAction(&postinstall_runner_action,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700241 ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700242 ActionMock action_mock;
243 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700244 EXPECT_EQ(ErrorCode::kError,
245 GetErrorCodeForAction(&action_mock, ErrorCode::kError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700246}
247
Darin Petkov36275772010-10-01 11:40:57 -0700248TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700249 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800250 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700251 .WillOnce(Return(false));
252 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700253 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800254 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700255 .WillOnce(DoAll(
256 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
257 Return(true)));
258 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700259 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800260 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700261 .WillOnce(DoAll(
262 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
263 Return(true)));
264 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700265 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800266 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700267 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700268 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700269}
270
271TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800272 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700273 .WillOnce(Return(false))
274 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
275 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
276 .WillOnce(DoAll(
277 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
278 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800279 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700280 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800281 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
282 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
283 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700284 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
285 .Times(1);
286 for (int i = 0; i < 4; i ++)
287 attempter_.MarkDeltaUpdateFailure();
288}
289
Darin Petkov1b003102010-11-30 10:18:36 -0800290TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
291 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
292 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700293 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(_))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800294 .Times(0);
295 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700296 string url1 = "http://url1";
297 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800298 response.payload_urls.push_back("https://url");
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700299 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetCurrentUrl())
Jay Srinivasan53173b92013-05-17 17:13:01 -0700300 .WillRepeatedly(Return(url1));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700301 fake_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800302 attempter_.ScheduleErrorEventAction();
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700303 EXPECT_EQ(url1, fake_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800304}
305
306TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
307 EXPECT_CALL(*processor_,
308 EnqueueAction(Property(&AbstractAction::Type,
309 OmahaRequestAction::StaticType())))
310 .Times(1);
311 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700312 ErrorCode err = ErrorCode::kError;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700313 EXPECT_CALL(*fake_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800314 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
315 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800316 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800317 attempter_.ScheduleErrorEventAction();
318 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
319}
320
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200321void UpdateAttempterTest::QuitMainLoop() {
322 g_main_loop_quit(loop_);
323}
324
325gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
326 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
327 return FALSE;
328}
329
Darin Petkove6ef2f82011-03-07 17:31:11 -0800330gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
331 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
332 return FALSE;
333}
334
335gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
336 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
337 return FALSE;
338}
339
Chris Sosa76a29ae2013-07-11 17:59:24 -0700340gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800341 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700342 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800343 return FALSE;
344}
345
346gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
347 gpointer data) {
348 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700349 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700350 return FALSE;
351}
352
353gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800354 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700355 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700356 return FALSE;
357}
358
359gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
360 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
361 return FALSE;
362}
363
Thieu Le116fda32011-04-19 11:01:54 -0700364gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
365 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
366 return FALSE;
367}
368
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700369gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200370 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700371 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
372 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700373 return FALSE;
374}
375
Jay Srinivasan0a708742012-03-20 11:26:12 -0700376gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
377 gpointer data) {
378 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
379 ua_test->ReadUpdateDisabledFromPolicyTestStart();
380 return FALSE;
381}
382
383gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
384 gpointer data) {
385 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
386 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
387 return FALSE;
388}
389
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700390gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
391 gpointer data) {
392 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
393 ua_test->ReadScatterFactorFromPolicyTestStart();
394 return FALSE;
395}
396
397gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
398 gpointer data) {
399 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
400 ua_test->DecrementUpdateCheckCountTestStart();
401 return FALSE;
402}
403
Jay Srinivasan08fce042012-06-07 16:31:01 -0700404gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
405 gpointer data) {
406 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
407 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
408 return FALSE;
409}
410
Darin Petkove6ef2f82011-03-07 17:31:11 -0800411namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700412// Actions that will be built as part of an update check.
413const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800414 OmahaRequestAction::StaticType(),
415 OmahaResponseHandlerAction::StaticType(),
416 FilesystemCopierAction::StaticType(),
417 FilesystemCopierAction::StaticType(),
418 OmahaRequestAction::StaticType(),
419 DownloadAction::StaticType(),
420 OmahaRequestAction::StaticType(),
421 FilesystemCopierAction::StaticType(),
422 FilesystemCopierAction::StaticType(),
423 PostinstallRunnerAction::StaticType(),
424 OmahaRequestAction::StaticType()
425};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700426
427// Actions that will be built as part of a user-initiated rollback.
428const string kRollbackActionTypes[] = {
429 InstallPlanAction::StaticType(),
430 PostinstallRunnerAction::StaticType(),
431};
432
Darin Petkove6ef2f82011-03-07 17:31:11 -0800433} // namespace {}
434
435void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700436 attempter_.set_http_response_code(200);
437 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700438 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700439 EXPECT_CALL(*processor_,
440 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700441 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700442 }
443 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
444
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700445 attempter_.Update("", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800446 g_idle_add(&StaticUpdateTestVerify, this);
447}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700448
Darin Petkove6ef2f82011-03-07 17:31:11 -0800449void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700450 EXPECT_EQ(0, attempter_.http_response_code());
451 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700452 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
453 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
454 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700455 }
456 EXPECT_EQ(attempter_.response_handler_action_.get(),
457 attempter_.actions_[1].get());
458 DownloadAction* download_action =
459 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
460 ASSERT_TRUE(download_action != NULL);
461 EXPECT_EQ(&attempter_, download_action->delegate());
462 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800463 g_main_loop_quit(loop_);
464}
465
Chris Sosa28e479c2013-07-12 11:39:53 -0700466void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700467 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700468 // Create a device policy so that we can change settings.
469 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
470 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
471
472 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700473 fake_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700474
Don Garrett6646b442013-11-13 15:29:11 -0800475 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700476 // References bootable kernels in fake_hardware.h
477 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800478 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700479 fake_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800480 rollback_kernel);
481 }
482
Chris Sosa28e479c2013-07-12 11:39:53 -0700483 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700484
Chris Sosad38b1132014-03-25 10:43:59 -0700485 // We only allow rollback on devices that are not enterprise enrolled and
486 // which have a valid slot to rollback to.
487 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700488 is_rollback_allowed = true;
489 }
490
Don Garrett6646b442013-11-13 15:29:11 -0800491 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700492 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800493 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa28e479c2013-07-12 11:39:53 -0700494 DoAll(SetArgumentPointee<0>(std::string("")),
495 Return(true)));
496 } else {
497 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800498 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa76a29ae2013-07-11 17:59:24 -0700499 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
500 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700501 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700502
Chris Sosa28e479c2013-07-12 11:39:53 -0700503 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700504 InSequence s;
505 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
506 EXPECT_CALL(*processor_,
507 EnqueueAction(Property(&AbstractAction::Type,
508 kRollbackActionTypes[i]))).Times(1);
509 }
510 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
511
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700512 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700513 g_idle_add(&StaticRollbackTestVerify, this);
514 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700515 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700516 g_main_loop_quit(loop_);
517 }
518}
519
520void UpdateAttempterTest::RollbackTestVerify() {
521 // Verifies the actions that were enqueued.
522 EXPECT_EQ(&attempter_, processor_->delegate());
523 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
524 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
525 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
526 }
527 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
528 InstallPlanAction* install_plan_action =
529 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
530 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700531 // Matches fake_hardware.h -> rollback should move from kernel/boot device
532 // pair to other pair.
533 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
534 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700535 EXPECT_EQ(install_plan->powerwash_required, true);
536 g_main_loop_quit(loop_);
537}
538
Darin Petkove6ef2f82011-03-07 17:31:11 -0800539TEST_F(UpdateAttempterTest, UpdateTest) {
540 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
541 g_idle_add(&StaticUpdateTestStart, this);
542 g_main_loop_run(loop_);
543 g_main_loop_unref(loop_);
544 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700545}
546
Chris Sosa76a29ae2013-07-11 17:59:24 -0700547TEST_F(UpdateAttempterTest, RollbackTest) {
548 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
549 g_idle_add(&StaticRollbackTestStart, this);
550 g_main_loop_run(loop_);
551 g_main_loop_unref(loop_);
552 loop_ = NULL;
553}
554
Don Garrett6646b442013-11-13 15:29:11 -0800555TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
556 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
557 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
558 g_main_loop_run(loop_);
559 g_main_loop_unref(loop_);
560 loop_ = NULL;
561}
562
Chris Sosa76a29ae2013-07-11 17:59:24 -0700563TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
564 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
565 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
566 g_main_loop_run(loop_);
567 g_main_loop_unref(loop_);
568 loop_ = NULL;
569}
570
Thieu Le116fda32011-04-19 11:01:54 -0700571void UpdateAttempterTest::PingOmahaTestStart() {
572 EXPECT_CALL(*processor_,
573 EnqueueAction(Property(&AbstractAction::Type,
574 OmahaRequestAction::StaticType())))
575 .Times(1);
576 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
577 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200578 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700579}
580
581TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700582 UpdateCheckScheduler scheduler(&attempter_, &fake_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700583 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700584 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700585 attempter_.set_update_check_scheduler(&scheduler);
586 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
587 g_idle_add(&StaticPingOmahaTestStart, this);
588 g_main_loop_run(loop_);
589 g_main_loop_unref(loop_);
590 loop_ = NULL;
591 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
592 EXPECT_EQ(true, scheduler.scheduled_);
593}
594
Darin Petkov18c7bce2011-06-16 14:07:00 -0700595TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
596 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700597 const ErrorCode kCode = ErrorCode::kDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700598 attempter_.CreatePendingErrorEvent(&action, kCode);
599 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
600 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
601 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700602 EXPECT_EQ(
603 static_cast<ErrorCode>(static_cast<int>(kCode) |
604 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
605 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700606}
607
608TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
609 OmahaResponseHandlerAction *response_action =
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700610 new OmahaResponseHandlerAction(&fake_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700611 response_action->install_plan_.is_resume = true;
612 attempter_.response_handler_action_.reset(response_action);
613 ActionMock action;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700614 const ErrorCode kCode = ErrorCode::kInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700615 attempter_.CreatePendingErrorEvent(&action, kCode);
616 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
617 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
618 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700619 EXPECT_EQ(
620 static_cast<ErrorCode>(
621 static_cast<int>(kCode) |
622 static_cast<int>(ErrorCode::kResumedFlag) |
623 static_cast<int>(ErrorCode::kTestOmahaUrlFlag)),
624 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700625}
626
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700627TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200628 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700629 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200630 g_main_loop_run(loop_);
631 g_main_loop_unref(loop_);
632 loop_ = NULL;
633}
634
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700635void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
636 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200637 // from the device policy.
638
639 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
640 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
641
642 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700643 fake_system_state_.set_device_policy(device_policy);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200644
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700645 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
646 DoAll(SetArgumentPointee<0>(bool(false)),
647 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200648
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700649 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
650 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
651 Return(true)));
652
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700653 ASSERT_FALSE(test_dir_.empty());
654 attempter_.omaha_request_params_->set_root(test_dir_);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700655 attempter_.Update("", "", false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700656 EXPECT_EQ("beta-channel",
657 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200658
659 g_idle_add(&StaticQuitMainLoop, this);
660}
661
Jay Srinivasan0a708742012-03-20 11:26:12 -0700662TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
663 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
664 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
665 g_main_loop_run(loop_);
666 g_main_loop_unref(loop_);
667 loop_ = NULL;
668}
669
670void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
671 // Tests that the update_disbled flag is properly fetched
672 // from the device policy.
673
674 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
675 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
676
677 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700678 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700679
680 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
681 .WillRepeatedly(DoAll(
682 SetArgumentPointee<0>(true),
683 Return(true)));
684
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700685 attempter_.Update("", "", false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700686 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700687
688 g_idle_add(&StaticQuitMainLoop, this);
689}
690
David Zeuthen8f191b22013-08-06 12:27:50 -0700691TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
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(false);
695 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
696 attempter_.UpdateEngineStarted();
697}
698
699TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
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 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
704 attempter_.UpdateEngineStarted();
705}
706
707TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
708 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700709 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700710 mock_p2p_manager.fake().SetP2PEnabled(true);
711 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
712 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
713 attempter_.UpdateEngineStarted();
714}
715
716TEST_F(UpdateAttempterTest, P2PNotEnabled) {
717 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
718 g_idle_add(&StaticP2PNotEnabled, this);
719 g_main_loop_run(loop_);
720 g_main_loop_unref(loop_);
721 loop_ = NULL;
722}
723gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
724 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
725 ua_test->P2PNotEnabledStart();
726 return FALSE;
727}
728void UpdateAttempterTest::P2PNotEnabledStart() {
729 // If P2P is not enabled, check that we do not attempt housekeeping
730 // and do not convey that p2p is to be used.
731 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700732 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700733 mock_p2p_manager.fake().SetP2PEnabled(false);
734 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700735 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700736 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
737 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
738 g_idle_add(&StaticQuitMainLoop, this);
739}
740
741TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
742 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
743 g_idle_add(&StaticP2PEnabledStartingFails, this);
744 g_main_loop_run(loop_);
745 g_main_loop_unref(loop_);
746 loop_ = NULL;
747}
748gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
749 gpointer data) {
750 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
751 ua_test->P2PEnabledStartingFailsStart();
752 return FALSE;
753}
754void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
755 // If p2p is enabled, but starting it fails ensure we don't do
756 // any housekeeping and do not convey that p2p should be used.
757 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700758 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700759 mock_p2p_manager.fake().SetP2PEnabled(true);
760 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
761 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
762 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700763 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700764 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
765 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
766 g_idle_add(&StaticQuitMainLoop, this);
767}
768
769TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
770 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
771 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
772 g_main_loop_run(loop_);
773 g_main_loop_unref(loop_);
774 loop_ = NULL;
775}
776gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
777 gpointer data) {
778 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
779 ua_test->P2PEnabledHousekeepingFailsStart();
780 return FALSE;
781}
782void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
783 // If p2p is enabled, starting it works but housekeeping fails, ensure
784 // we do not convey p2p is to be used.
785 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700786 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700787 mock_p2p_manager.fake().SetP2PEnabled(true);
788 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
789 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
790 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700791 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700792 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
793 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
794 g_idle_add(&StaticQuitMainLoop, this);
795}
796
797TEST_F(UpdateAttempterTest, P2PEnabled) {
798 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
799 g_idle_add(&StaticP2PEnabled, this);
800 g_main_loop_run(loop_);
801 g_main_loop_unref(loop_);
802 loop_ = NULL;
803}
804gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
805 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
806 ua_test->P2PEnabledStart();
807 return FALSE;
808}
809void UpdateAttempterTest::P2PEnabledStart() {
810 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700811 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700812 // If P2P is enabled and starting it works, check that we performed
813 // housekeeping and that we convey p2p should be used.
814 mock_p2p_manager.fake().SetP2PEnabled(true);
815 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
816 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
817 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700818 attempter_.Update("", "", false, false);
David Zeuthen8f191b22013-08-06 12:27:50 -0700819 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
820 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
821 g_idle_add(&StaticQuitMainLoop, this);
822}
823
824TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
825 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
826 g_idle_add(&StaticP2PEnabledInteractive, this);
827 g_main_loop_run(loop_);
828 g_main_loop_unref(loop_);
829 loop_ = NULL;
830}
831gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
832 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
833 ua_test->P2PEnabledInteractiveStart();
834 return FALSE;
835}
836void UpdateAttempterTest::P2PEnabledInteractiveStart() {
837 MockP2PManager mock_p2p_manager;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700838 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -0700839 // For an interactive check, if P2P is enabled and starting it
840 // works, check that we performed housekeeping and that we convey
841 // p2p should be used for sharing but NOT for downloading.
842 mock_p2p_manager.fake().SetP2PEnabled(true);
843 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
844 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
845 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700846 attempter_.Update("", "", false, true /* interactive */);
David Zeuthen8f191b22013-08-06 12:27:50 -0700847 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
848 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
849 g_idle_add(&StaticQuitMainLoop, this);
850}
851
Jay Srinivasan0a708742012-03-20 11:26:12 -0700852TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
853 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
854 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
855 g_main_loop_run(loop_);
856 g_main_loop_unref(loop_);
857 loop_ = NULL;
858}
859
860void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
861 // Tests that the target_version_prefix value is properly fetched
862 // from the device policy.
863
864 const std::string target_version_prefix = "1412.";
865
866 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
867 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
868
869 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700870 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700871
872 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
873 .WillRepeatedly(DoAll(
874 SetArgumentPointee<0>(target_version_prefix),
875 Return(true)));
876
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700877 attempter_.Update("", "", false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700878 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700879 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700880
881 g_idle_add(&StaticQuitMainLoop, this);
882}
883
884
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700885TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
886 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
887 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
888 g_main_loop_run(loop_);
889 g_main_loop_unref(loop_);
890 loop_ = NULL;
891}
892
893// Tests that the scatter_factor_in_seconds value is properly fetched
894// from the device policy.
895void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
896 int64 scatter_factor_in_seconds = 36000;
897
898 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
899 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
900
901 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700902 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700903
904 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
905 .WillRepeatedly(DoAll(
906 SetArgumentPointee<0>(scatter_factor_in_seconds),
907 Return(true)));
908
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700909 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700910 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
911
912 g_idle_add(&StaticQuitMainLoop, this);
913}
914
915TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
916 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
917 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
918 g_main_loop_run(loop_);
919 g_main_loop_unref(loop_);
920 loop_ = NULL;
921}
922
923void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
924 // Tests that the scatter_factor_in_seconds value is properly fetched
925 // from the device policy and is decremented if value > 0.
926 int64 initial_value = 5;
927 Prefs prefs;
928 attempter_.prefs_ = &prefs;
929
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700930 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700931
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700932 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800933 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700934 &prefs_dir));
935 ScopedDirRemover temp_dir_remover(prefs_dir);
936
Alex Vakulenko75039d72014-03-25 12:36:28 -0700937 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700938 << "Failed to initialize preferences.";
939 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
940
941 int64 scatter_factor_in_seconds = 10;
942
943 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
944 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
945
946 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700947 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700948
949 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
950 .WillRepeatedly(DoAll(
951 SetArgumentPointee<0>(scatter_factor_in_seconds),
952 Return(true)));
953
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700954 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700955 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
956
957 // Make sure the file still exists.
958 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
959
960 int64 new_value;
961 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
962 EXPECT_EQ(initial_value - 1, new_value);
963
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700964 EXPECT_TRUE(
965 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700966
967 // However, if the count is already 0, it's not decremented. Test that.
968 initial_value = 0;
969 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700970 attempter_.Update("", "", false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700971 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
972 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
973 EXPECT_EQ(initial_value, new_value);
974
975 g_idle_add(&StaticQuitMainLoop, this);
976}
977
Jay Srinivasan08fce042012-06-07 16:31:01 -0700978TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
979 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
980 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
981 g_main_loop_run(loop_);
982 g_main_loop_unref(loop_);
983 loop_ = NULL;
984}
985
986void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
987 // Tests that no scattering logic is enabled if the update check
988 // is manually done (as opposed to a scheduled update check)
989 int64 initial_value = 8;
990 Prefs prefs;
991 attempter_.prefs_ = &prefs;
992
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700993 fake_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700994
995 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800996 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -0700997 &prefs_dir));
998 ScopedDirRemover temp_dir_remover(prefs_dir);
999
Alex Vakulenko75039d72014-03-25 12:36:28 -07001000 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -07001001 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -07001002 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001003 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
1004
1005 // make sure scatter_factor is non-zero as scattering is disabled
1006 // otherwise.
1007 int64 scatter_factor_in_seconds = 50;
1008
1009 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
1010 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
1011
1012 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001013 fake_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001014
1015 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
1016 .WillRepeatedly(DoAll(
1017 SetArgumentPointee<0>(scatter_factor_in_seconds),
1018 Return(true)));
1019
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001020 // Trigger an interactive check so we can test that scattering is disabled.
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -07001021 attempter_.Update("", "", false, true);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001022 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1023
1024 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001025 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001026 EXPECT_FALSE(
1027 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001028 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001029 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1030 EXPECT_FALSE(
1031 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001032 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
1033
1034 g_idle_add(&StaticQuitMainLoop, this);
1035}
1036
David Zeuthen985b1122013-10-09 12:13:15 -07001037// Checks that we only report daily metrics at most every 24 hours.
1038TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1039 FakeClock fake_clock;
1040 Prefs prefs;
1041 string temp_dir;
1042
1043 // We need persistent preferences for this test
Gilad Arnolda6742b32014-01-11 00:18:34 -08001044 EXPECT_TRUE(utils::MakeTempDirectory("UpdateCheckScheduler.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -07001045 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001046 prefs.Init(base::FilePath(temp_dir));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001047 fake_system_state_.set_clock(&fake_clock);
1048 fake_system_state_.set_prefs(&prefs);
David Zeuthen985b1122013-10-09 12:13:15 -07001049
1050 Time epoch = Time::FromInternalValue(0);
1051 fake_clock.SetWallclockTime(epoch);
1052
1053 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1054 // we should report.
1055 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1056 // We should not report again if no time has passed.
1057 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1058
1059 // We should not report if only 10 hours has passed.
1060 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1061 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1062
1063 // We should not report if only 24 hours - 1 sec has passed.
1064 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1065 TimeDelta::FromSeconds(1));
1066 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1067
1068 // We should report if 24 hours has passed.
1069 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1070 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1071
1072 // But then we should not report again..
1073 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1074
1075 // .. until another 24 hours has passed
1076 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1077 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1078 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1079 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1080 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1081
1082 // .. and another 24 hours
1083 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1084 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1085 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1086 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1087 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1088
1089 // If the span between time of reporting and present time is
1090 // negative, we report. This is in order to reset the timestamp and
1091 // avoid an edge condition whereby a distant point in the future is
1092 // in the state variable resulting in us never ever reporting again.
1093 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1094 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1095 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1096
1097 // In this case we should not update until the clock reads 71 + 24 = 95.
1098 // Check that.
1099 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1100 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1101 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1102 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1103 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1104
1105 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1106}
1107
David Zeuthen3c55abd2013-10-14 12:48:03 -07001108TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1109 const string update_completed_marker = test_dir_ + "/update-completed-marker";
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001110 UpdateAttempterUnderTest attempter(&fake_system_state_, &dbus_,
David Zeuthen3c55abd2013-10-14 12:48:03 -07001111 update_completed_marker);
1112
1113 FakeClock fake_clock;
1114 fake_clock.SetBootTime(Time::FromTimeT(42));
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001115 fake_system_state_.set_clock(&fake_clock);
David Zeuthen3c55abd2013-10-14 12:48:03 -07001116
1117 Time boot_time;
1118 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1119
1120 attempter.WriteUpdateCompletedMarker();
1121
1122 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1123 EXPECT_EQ(boot_time.ToTimeT(), 42);
1124}
1125
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001126} // namespace chromeos_update_engine