blob: 095de23b5e8c1284e251a31e1d7e471832c084dd [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"
12#include "update_engine/filesystem_copier_action.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080013#include "update_engine/mock_dbus_interface.h"
Darin Petkov1b003102010-11-30 10:18:36 -080014#include "update_engine/mock_http_fetcher.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080015#include "update_engine/mock_payload_state.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070016#include "update_engine/mock_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070017#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070018#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070019#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080020#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070021#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080022#include "update_engine/update_check_scheduler.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070023
24using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070025using testing::_;
26using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070027using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070028using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080029using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030using testing::Property;
31using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070032using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033
34namespace chromeos_update_engine {
35
36// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070037// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070038// methods.
39class UpdateAttempterUnderTest : public UpdateAttempter {
40 public:
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080041 explicit UpdateAttempterUnderTest(MockSystemState* mock_system_state,
42 MockDbusGlib* dbus)
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080043 : UpdateAttempter(mock_system_state, dbus) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070044};
45
46class UpdateAttempterTest : public ::testing::Test {
47 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070048 UpdateAttempterTest()
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049 : attempter_(&mock_system_state_, &dbus_),
Jay Srinivasan43488792012-06-19 00:25:31 -070050 mock_connection_manager(&mock_system_state_),
51 loop_(NULL) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052 mock_system_state_.set_connection_manager(&mock_connection_manager);
Jay Srinivasan43488792012-06-19 00:25:31 -070053 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -070054 virtual void SetUp() {
55 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080056 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070057 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
58 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080059 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
60 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061 EXPECT_FALSE(attempter_.download_active_);
62 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
63 EXPECT_EQ(0.0, attempter_.download_progress_);
64 EXPECT_EQ(0, attempter_.last_checked_time_);
65 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070066 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080067 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070068 attempter_.processor_.reset(processor_); // Transfers ownership.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080069 prefs_ = mock_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070070 }
71
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020072 void QuitMainLoop();
73 static gboolean StaticQuitMainLoop(gpointer data);
74
Darin Petkove6ef2f82011-03-07 17:31:11 -080075 void UpdateTestStart();
76 void UpdateTestVerify();
77 static gboolean StaticUpdateTestStart(gpointer data);
78 static gboolean StaticUpdateTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020079
Thieu Le116fda32011-04-19 11:01:54 -070080 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -070081 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020082
Jay Srinivasanae4697c2013-03-18 17:08:08 -070083 void ReadChannelFromPolicyTestStart();
84 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -080085
Jay Srinivasan0a708742012-03-20 11:26:12 -070086 void ReadUpdateDisabledFromPolicyTestStart();
87 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
88
89 void ReadTargetVersionPrefixFromPolicyTestStart();
90 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
91 gpointer data);
92
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070093 void ReadScatterFactorFromPolicyTestStart();
94 static gboolean StaticReadScatterFactorFromPolicyTestStart(
95 gpointer data);
96
97 void DecrementUpdateCheckCountTestStart();
98 static gboolean StaticDecrementUpdateCheckCountTestStart(
99 gpointer data);
100
Jay Srinivasan08fce042012-06-07 16:31:01 -0700101 void NoScatteringDoneDuringManualUpdateTestStart();
102 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
103 gpointer data);
104
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800105 NiceMock<MockSystemState> mock_system_state_;
106 NiceMock<MockDbusGlib> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700107 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800108 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800109 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800110 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800111 GMainLoop* loop_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700112};
113
Darin Petkov1b003102010-11-30 10:18:36 -0800114TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
115 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
116 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800117 DownloadAction action(prefs_, NULL, fetcher.release());
118 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700119 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800120 EXPECT_EQ(503, attempter_.http_response_code());
121 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
122 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
123}
124
125TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
126 ActionMock action;
127 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
128 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800129 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800130 .WillOnce(Return(false));
David Zeuthena99981f2013-04-29 13:42:47 -0700131 attempter_.ActionCompleted(NULL, &action, kErrorCodeError);
Darin Petkov1b003102010-11-30 10:18:36 -0800132 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
133}
134
135TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
136 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
137 fetcher->FailTransfer(500); // Sets the HTTP response code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700138 OmahaRequestAction action(&mock_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800139 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800140 ObjectCollectorAction<OmahaResponse> collector_action;
141 BondActions(&action, &collector_action);
142 OmahaResponse response;
143 response.poll_interval = 234;
144 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800145 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800146 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800147 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700148 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800149 EXPECT_EQ(500, attempter_.http_response_code());
150 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
151 EXPECT_EQ(234, scheduler.poll_interval());
152 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
153}
154
Darin Petkovcd1666f2010-09-23 09:53:44 -0700155TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700156 extern const char* kUpdateCompletedMarker;
157 const FilePath kMarker(kUpdateCompletedMarker);
158 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800159 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700160 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
161 EXPECT_TRUE(file_util::Delete(kMarker, false));
162}
163
164TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700165 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
166 ErrorCode code);
167 EXPECT_EQ(kErrorCodeSuccess,
168 GetErrorCodeForAction(NULL, kErrorCodeSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700169
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800170 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700171 OmahaRequestAction omaha_request_action(&mock_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800172 NULL, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700173 EXPECT_EQ(kErrorCodeOmahaRequestError,
174 GetErrorCodeForAction(&omaha_request_action, kErrorCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800175 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
David Zeuthena99981f2013-04-29 13:42:47 -0700176 EXPECT_EQ(kErrorCodeOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700177 GetErrorCodeForAction(&omaha_response_handler_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700178 kErrorCodeError));
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700179 FilesystemCopierAction filesystem_copier_action(false, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700180 EXPECT_EQ(kErrorCodeFilesystemCopierError,
181 GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800182 PostinstallRunnerAction postinstall_runner_action;
David Zeuthena99981f2013-04-29 13:42:47 -0700183 EXPECT_EQ(kErrorCodePostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700184 GetErrorCodeForAction(&postinstall_runner_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700185 kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700186 ActionMock action_mock;
187 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
David Zeuthena99981f2013-04-29 13:42:47 -0700188 EXPECT_EQ(kErrorCodeError,
189 GetErrorCodeForAction(&action_mock, kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700190}
191
Darin Petkov36275772010-10-01 11:40:57 -0700192TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700193 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800194 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700195 .WillOnce(Return(false));
196 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700197 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800198 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700199 .WillOnce(DoAll(
200 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
201 Return(true)));
202 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700203 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800204 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700205 .WillOnce(DoAll(
206 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
207 Return(true)));
208 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700209 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800210 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700211 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700212 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700213}
214
215TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800216 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700217 .WillOnce(Return(false))
218 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
219 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
220 .WillOnce(DoAll(
221 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
222 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800223 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700224 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800225 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
226 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
227 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700228 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
229 .Times(1);
230 for (int i = 0; i < 4; i ++)
231 attempter_.MarkDeltaUpdateFailure();
232}
233
Darin Petkov1b003102010-11-30 10:18:36 -0800234TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
235 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
236 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800237 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
238 .Times(0);
239 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700240 string url1 = "http://url1";
241 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800242 response.payload_urls.push_back("https://url");
Jay Srinivasan53173b92013-05-17 17:13:01 -0700243 EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl())
244 .WillRepeatedly(Return(url1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800245 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800246 attempter_.ScheduleErrorEventAction();
Jay Srinivasan53173b92013-05-17 17:13:01 -0700247 EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800248}
249
250TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
251 EXPECT_CALL(*processor_,
252 EnqueueAction(Property(&AbstractAction::Type,
253 OmahaRequestAction::StaticType())))
254 .Times(1);
255 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
David Zeuthena99981f2013-04-29 13:42:47 -0700256 ErrorCode err = kErrorCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800257 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800258 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
259 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800260 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800261 attempter_.ScheduleErrorEventAction();
262 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
263}
264
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700265TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
266 extern const char* UpdateStatusToString(UpdateStatus);
267 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
268 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
269 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
270 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
271 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
272 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
273 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
274 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
275 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
276 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
277 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
278 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
279 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
280 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
281 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
282 EXPECT_STREQ("unknown status",
283 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
284}
285
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200286void UpdateAttempterTest::QuitMainLoop() {
287 g_main_loop_quit(loop_);
288}
289
290gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
291 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
292 return FALSE;
293}
294
Darin Petkove6ef2f82011-03-07 17:31:11 -0800295gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
296 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
297 return FALSE;
298}
299
300gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
301 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
302 return FALSE;
303}
304
Thieu Le116fda32011-04-19 11:01:54 -0700305gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
306 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
307 return FALSE;
308}
309
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700310gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200311 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700312 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
313 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700314 return FALSE;
315}
316
Jay Srinivasan0a708742012-03-20 11:26:12 -0700317gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
318 gpointer data) {
319 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
320 ua_test->ReadUpdateDisabledFromPolicyTestStart();
321 return FALSE;
322}
323
324gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
325 gpointer data) {
326 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
327 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
328 return FALSE;
329}
330
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700331gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
332 gpointer data) {
333 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
334 ua_test->ReadScatterFactorFromPolicyTestStart();
335 return FALSE;
336}
337
338gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
339 gpointer data) {
340 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
341 ua_test->DecrementUpdateCheckCountTestStart();
342 return FALSE;
343}
344
Jay Srinivasan08fce042012-06-07 16:31:01 -0700345gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
346 gpointer data) {
347 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
348 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
349 return FALSE;
350}
351
Darin Petkove6ef2f82011-03-07 17:31:11 -0800352namespace {
353const string kActionTypes[] = {
354 OmahaRequestAction::StaticType(),
355 OmahaResponseHandlerAction::StaticType(),
356 FilesystemCopierAction::StaticType(),
357 FilesystemCopierAction::StaticType(),
358 OmahaRequestAction::StaticType(),
359 DownloadAction::StaticType(),
360 OmahaRequestAction::StaticType(),
361 FilesystemCopierAction::StaticType(),
362 FilesystemCopierAction::StaticType(),
363 PostinstallRunnerAction::StaticType(),
364 OmahaRequestAction::StaticType()
365};
366} // namespace {}
367
368void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700369 attempter_.set_http_response_code(200);
370 InSequence s;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700371 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
372 EXPECT_CALL(*processor_,
373 EnqueueAction(Property(&AbstractAction::Type,
374 kActionTypes[i]))).Times(1);
375 }
376 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
377
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800378 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800379 g_idle_add(&StaticUpdateTestVerify, this);
380}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700381
Darin Petkove6ef2f82011-03-07 17:31:11 -0800382void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700383 EXPECT_EQ(0, attempter_.http_response_code());
384 EXPECT_EQ(&attempter_, processor_->delegate());
385 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
386 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
387 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
388 }
389 EXPECT_EQ(attempter_.response_handler_action_.get(),
390 attempter_.actions_[1].get());
391 DownloadAction* download_action =
392 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
393 ASSERT_TRUE(download_action != NULL);
394 EXPECT_EQ(&attempter_, download_action->delegate());
395 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800396 g_main_loop_quit(loop_);
397}
398
399TEST_F(UpdateAttempterTest, UpdateTest) {
400 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
401 g_idle_add(&StaticUpdateTestStart, this);
402 g_main_loop_run(loop_);
403 g_main_loop_unref(loop_);
404 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700405}
406
Thieu Le116fda32011-04-19 11:01:54 -0700407void UpdateAttempterTest::PingOmahaTestStart() {
408 EXPECT_CALL(*processor_,
409 EnqueueAction(Property(&AbstractAction::Type,
410 OmahaRequestAction::StaticType())))
411 .Times(1);
412 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
413 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200414 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700415}
416
417TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800418 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700419 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700420 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700421 attempter_.set_update_check_scheduler(&scheduler);
422 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
423 g_idle_add(&StaticPingOmahaTestStart, this);
424 g_main_loop_run(loop_);
425 g_main_loop_unref(loop_);
426 loop_ = NULL;
427 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
428 EXPECT_EQ(true, scheduler.scheduled_);
429}
430
Darin Petkov18c7bce2011-06-16 14:07:00 -0700431TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
432 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700433 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700434 attempter_.CreatePendingErrorEvent(&action, kCode);
435 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
436 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
437 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700438 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800439 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700440}
441
442TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
443 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800444 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700445 response_action->install_plan_.is_resume = true;
446 attempter_.response_handler_action_.reset(response_action);
447 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700448 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700449 attempter_.CreatePendingErrorEvent(&action, kCode);
450 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
451 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
452 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700453 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700454 attempter_.error_event_->error_code);
455}
456
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700457TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200458 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700459 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200460 g_main_loop_run(loop_);
461 g_main_loop_unref(loop_);
462 loop_ = NULL;
463}
464
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700465void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
466 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200467 // from the device policy.
468
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));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700473 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
474 Return(device_policy));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200475
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700476 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
477 DoAll(SetArgumentPointee<0>(bool(false)),
478 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200479
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700480 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
481 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
482 Return(true)));
483
484 attempter_.omaha_request_params_->set_root("./UpdateAttempterTest");
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800485 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700486 EXPECT_EQ("beta-channel",
487 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200488
489 g_idle_add(&StaticQuitMainLoop, this);
490}
491
Jay Srinivasan0a708742012-03-20 11:26:12 -0700492TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
493 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
494 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
495 g_main_loop_run(loop_);
496 g_main_loop_unref(loop_);
497 loop_ = NULL;
498}
499
500void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
501 // Tests that the update_disbled flag is properly fetched
502 // from the device policy.
503
504 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
505 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
506
507 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700508 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
509 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700510
511 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
512 .WillRepeatedly(DoAll(
513 SetArgumentPointee<0>(true),
514 Return(true)));
515
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800516 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700517 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700518
519 g_idle_add(&StaticQuitMainLoop, this);
520}
521
522TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
523 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
524 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
525 g_main_loop_run(loop_);
526 g_main_loop_unref(loop_);
527 loop_ = NULL;
528}
529
530void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
531 // Tests that the target_version_prefix value is properly fetched
532 // from the device policy.
533
534 const std::string target_version_prefix = "1412.";
535
536 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
537 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
538
539 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700540 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
541 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700542
543 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
544 .WillRepeatedly(DoAll(
545 SetArgumentPointee<0>(target_version_prefix),
546 Return(true)));
547
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800548 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700549 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700550 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700551
552 g_idle_add(&StaticQuitMainLoop, this);
553}
554
555
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700556TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
557 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
558 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
559 g_main_loop_run(loop_);
560 g_main_loop_unref(loop_);
561 loop_ = NULL;
562}
563
564// Tests that the scatter_factor_in_seconds value is properly fetched
565// from the device policy.
566void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
567 int64 scatter_factor_in_seconds = 36000;
568
569 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
570 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
571
572 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800573 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700574 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700575
576 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
577 .WillRepeatedly(DoAll(
578 SetArgumentPointee<0>(scatter_factor_in_seconds),
579 Return(true)));
580
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800581 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700582 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
583
584 g_idle_add(&StaticQuitMainLoop, this);
585}
586
587TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
588 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
589 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
590 g_main_loop_run(loop_);
591 g_main_loop_unref(loop_);
592 loop_ = NULL;
593}
594
595void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
596 // Tests that the scatter_factor_in_seconds value is properly fetched
597 // from the device policy and is decremented if value > 0.
598 int64 initial_value = 5;
599 Prefs prefs;
600 attempter_.prefs_ = &prefs;
601
Jay Srinivasan08fce042012-06-07 16:31:01 -0700602 EXPECT_CALL(mock_system_state_,
603 IsOOBEComplete()).WillRepeatedly(Return(true));
604
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700605 string prefs_dir;
606 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
607 &prefs_dir));
608 ScopedDirRemover temp_dir_remover(prefs_dir);
609
610 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
611 << "Failed to initialize preferences.";
612 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
613
614 int64 scatter_factor_in_seconds = 10;
615
616 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
617 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
618
619 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800620 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700621 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700622
623 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
624 .WillRepeatedly(DoAll(
625 SetArgumentPointee<0>(scatter_factor_in_seconds),
626 Return(true)));
627
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800628 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700629 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
630
631 // Make sure the file still exists.
632 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
633
634 int64 new_value;
635 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
636 EXPECT_EQ(initial_value - 1, new_value);
637
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700638 EXPECT_TRUE(
639 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700640
641 // However, if the count is already 0, it's not decremented. Test that.
642 initial_value = 0;
643 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800644 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700645 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
646 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
647 EXPECT_EQ(initial_value, new_value);
648
649 g_idle_add(&StaticQuitMainLoop, this);
650}
651
Jay Srinivasan08fce042012-06-07 16:31:01 -0700652TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
653 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
654 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
655 g_main_loop_run(loop_);
656 g_main_loop_unref(loop_);
657 loop_ = NULL;
658}
659
660void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
661 // Tests that no scattering logic is enabled if the update check
662 // is manually done (as opposed to a scheduled update check)
663 int64 initial_value = 8;
664 Prefs prefs;
665 attempter_.prefs_ = &prefs;
666
667 EXPECT_CALL(mock_system_state_,
668 IsOOBEComplete()).WillRepeatedly(Return(true));
669
670 string prefs_dir;
671 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
672 &prefs_dir));
673 ScopedDirRemover temp_dir_remover(prefs_dir);
674
675 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
676 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700677 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700678 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
679
680 // make sure scatter_factor is non-zero as scattering is disabled
681 // otherwise.
682 int64 scatter_factor_in_seconds = 50;
683
684 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
685 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
686
687 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800688 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700689 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700690
691 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
692 .WillRepeatedly(DoAll(
693 SetArgumentPointee<0>(scatter_factor_in_seconds),
694 Return(true)));
695
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800696 // Trigger an interactive check so we can test that scattering is disabled.
697 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700698 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
699
700 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700701 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700702 EXPECT_FALSE(
703 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700704 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700705 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
706 EXPECT_FALSE(
707 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700708 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
709
710 g_idle_add(&StaticQuitMainLoop, this);
711}
712
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700713} // namespace chromeos_update_engine