blob: 6c5b5a215f771f5edf860e3b8e695571fca74225 [file] [log] [blame]
Darin Petkov18c7bce2011-06-16 14:07:00 -07001// Copyright (c) 2011 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"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070015#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070016#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080017#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070018#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080019#include "update_engine/update_check_scheduler.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070020
21using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070022using testing::_;
23using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070025using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080026using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070027using testing::Property;
28using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070029using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030
31namespace chromeos_update_engine {
32
33// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070034// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070035// methods.
36class UpdateAttempterUnderTest : public UpdateAttempter {
37 public:
Andrew de los Reyes000d8952011-03-02 15:21:14 -080038 explicit UpdateAttempterUnderTest(MockDbusGlib* dbus)
39 : UpdateAttempter(NULL, NULL, dbus) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070040};
41
42class UpdateAttempterTest : public ::testing::Test {
43 protected:
Darin Petkove6ef2f82011-03-07 17:31:11 -080044 UpdateAttempterTest() : attempter_(&dbus_), loop_(NULL) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070045 virtual void SetUp() {
46 EXPECT_EQ(NULL, attempter_.dbus_service_);
47 EXPECT_EQ(NULL, attempter_.prefs_);
48 EXPECT_EQ(NULL, attempter_.metrics_lib_);
49 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
50 EXPECT_EQ(0, attempter_.http_response_code_);
51 EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_);
52 EXPECT_EQ(NULL, attempter_.manage_priority_source_);
53 EXPECT_FALSE(attempter_.download_active_);
54 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
55 EXPECT_EQ(0.0, attempter_.download_progress_);
56 EXPECT_EQ(0, attempter_.last_checked_time_);
57 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
58 EXPECT_EQ(0, attempter_.new_size_);
59 processor_ = new ActionProcessorMock();
60 attempter_.processor_.reset(processor_); // Transfers ownership.
Darin Petkov36275772010-10-01 11:40:57 -070061 attempter_.prefs_ = &prefs_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070062 }
63
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020064 void QuitMainLoop();
65 static gboolean StaticQuitMainLoop(gpointer data);
66
Darin Petkove6ef2f82011-03-07 17:31:11 -080067 void UpdateTestStart();
68 void UpdateTestVerify();
69 static gboolean StaticUpdateTestStart(gpointer data);
70 static gboolean StaticUpdateTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020071
Thieu Le116fda32011-04-19 11:01:54 -070072 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -070073 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020074
75 void ReadTrackFromPolicyTestStart();
76 static gboolean StaticReadTrackFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -080077
Andrew de los Reyes000d8952011-03-02 15:21:14 -080078 MockDbusGlib dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070079 UpdateAttempterUnderTest attempter_;
80 ActionProcessorMock* processor_;
Darin Petkov9c096d62010-11-17 14:49:04 -080081 NiceMock<PrefsMock> prefs_;
Darin Petkove6ef2f82011-03-07 17:31:11 -080082 GMainLoop* loop_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070083};
84
Darin Petkov1b003102010-11-30 10:18:36 -080085TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
86 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
87 fetcher->FailTransfer(503); // Sets the HTTP response code.
88 DownloadAction action(&prefs_, fetcher.release());
89 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
90 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
91 EXPECT_EQ(503, attempter_.http_response_code());
92 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
93 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
94}
95
96TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
97 ActionMock action;
98 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
99 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
100 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
101 .WillOnce(Return(false));
102 attempter_.ActionCompleted(NULL, &action, kActionCodeError);
103 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
104}
105
106TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
107 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
108 fetcher->FailTransfer(500); // Sets the HTTP response code.
109 OmahaRequestParams params;
Thieu Le116fda32011-04-19 11:01:54 -0700110 OmahaRequestAction action(&prefs_, params, NULL, fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800111 ObjectCollectorAction<OmahaResponse> collector_action;
112 BondActions(&action, &collector_action);
113 OmahaResponse response;
114 response.poll_interval = 234;
115 action.SetOutputObject(response);
116 UpdateCheckScheduler scheduler(&attempter_);
117 attempter_.set_update_check_scheduler(&scheduler);
118 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
119 attempter_.ActionCompleted(NULL, &action, kActionCodeSuccess);
120 EXPECT_EQ(500, attempter_.http_response_code());
121 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
122 EXPECT_EQ(234, scheduler.poll_interval());
123 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
124}
125
Darin Petkovcd1666f2010-09-23 09:53:44 -0700126TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700127 extern const char* kUpdateCompletedMarker;
128 const FilePath kMarker(kUpdateCompletedMarker);
129 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800130 MockDbusGlib dbus;
131 UpdateAttempterUnderTest attempter(&dbus);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700132 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
133 EXPECT_TRUE(file_util::Delete(kMarker, false));
134}
135
136TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
137 extern ActionExitCode GetErrorCodeForAction(AbstractAction* action,
138 ActionExitCode code);
139 EXPECT_EQ(kActionCodeSuccess,
140 GetErrorCodeForAction(NULL, kActionCodeSuccess));
141
142 OmahaRequestParams params;
Thieu Le116fda32011-04-19 11:01:54 -0700143 OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL, false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700144 EXPECT_EQ(kActionCodeOmahaRequestError,
145 GetErrorCodeForAction(&omaha_request_action, kActionCodeError));
Darin Petkov73058b42010-10-06 16:32:19 -0700146 OmahaResponseHandlerAction omaha_response_handler_action(&prefs_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700147 EXPECT_EQ(kActionCodeOmahaResponseHandlerError,
148 GetErrorCodeForAction(&omaha_response_handler_action,
149 kActionCodeError));
Darin Petkov3aefa862010-12-07 14:45:00 -0800150 FilesystemCopierAction filesystem_copier_action(false, false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700151 EXPECT_EQ(kActionCodeFilesystemCopierError,
152 GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800153 PostinstallRunnerAction postinstall_runner_action;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700154 EXPECT_EQ(kActionCodePostinstallRunnerError,
155 GetErrorCodeForAction(&postinstall_runner_action,
156 kActionCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700157 ActionMock action_mock;
158 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
159 EXPECT_EQ(kActionCodeError,
160 GetErrorCodeForAction(&action_mock, kActionCodeError));
161}
162
Darin Petkov36275772010-10-01 11:40:57 -0700163TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
164 attempter_.omaha_request_params_.delta_okay = true;
165 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
166 .WillOnce(Return(false));
167 attempter_.DisableDeltaUpdateIfNeeded();
168 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
169 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
170 .WillOnce(DoAll(
171 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
172 Return(true)));
173 attempter_.DisableDeltaUpdateIfNeeded();
174 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
175 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
176 .WillOnce(DoAll(
177 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
178 Return(true)));
179 attempter_.DisableDeltaUpdateIfNeeded();
180 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
181 EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0);
182 attempter_.DisableDeltaUpdateIfNeeded();
183 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
184}
185
186TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Darin Petkov36275772010-10-01 11:40:57 -0700187 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
188 .WillOnce(Return(false))
189 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
190 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
191 .WillOnce(DoAll(
192 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
193 Return(true)));
Darin Petkov2dd01092010-10-08 15:43:05 -0700194 EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
195 .WillRepeatedly(Return(true));
Darin Petkov36275772010-10-01 11:40:57 -0700196 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
197 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
198 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures,
199 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
200 .Times(1);
201 for (int i = 0; i < 4; i ++)
202 attempter_.MarkDeltaUpdateFailure();
203}
204
Darin Petkov1b003102010-11-30 10:18:36 -0800205TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
206 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
207 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
208 attempter_.ScheduleErrorEventAction();
209}
210
211TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
212 EXPECT_CALL(*processor_,
213 EnqueueAction(Property(&AbstractAction::Type,
214 OmahaRequestAction::StaticType())))
215 .Times(1);
216 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
217 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
218 OmahaEvent::kResultError,
219 kActionCodeError));
220 attempter_.ScheduleErrorEventAction();
221 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
222}
223
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700224TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
225 extern const char* UpdateStatusToString(UpdateStatus);
226 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
227 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
228 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
229 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
230 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
231 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
232 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
233 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
234 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
235 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
236 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
237 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
238 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
239 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
240 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
241 EXPECT_STREQ("unknown status",
242 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
243}
244
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200245void UpdateAttempterTest::QuitMainLoop() {
246 g_main_loop_quit(loop_);
247}
248
249gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
250 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
251 return FALSE;
252}
253
Darin Petkove6ef2f82011-03-07 17:31:11 -0800254gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
255 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
256 return FALSE;
257}
258
259gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
260 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
261 return FALSE;
262}
263
Thieu Le116fda32011-04-19 11:01:54 -0700264gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
265 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
266 return FALSE;
267}
268
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200269gboolean UpdateAttempterTest::StaticReadTrackFromPolicyTestStart(
270 gpointer data) {
271 reinterpret_cast<UpdateAttempterTest*>(data)->ReadTrackFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700272 return FALSE;
273}
274
Darin Petkove6ef2f82011-03-07 17:31:11 -0800275namespace {
276const string kActionTypes[] = {
277 OmahaRequestAction::StaticType(),
278 OmahaResponseHandlerAction::StaticType(),
279 FilesystemCopierAction::StaticType(),
280 FilesystemCopierAction::StaticType(),
281 OmahaRequestAction::StaticType(),
282 DownloadAction::StaticType(),
283 OmahaRequestAction::StaticType(),
284 FilesystemCopierAction::StaticType(),
285 FilesystemCopierAction::StaticType(),
286 PostinstallRunnerAction::StaticType(),
287 OmahaRequestAction::StaticType()
288};
289} // namespace {}
290
291void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700292 attempter_.set_http_response_code(200);
293 InSequence s;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700294 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
295 EXPECT_CALL(*processor_,
296 EnqueueAction(Property(&AbstractAction::Type,
297 kActionTypes[i]))).Times(1);
298 }
299 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
300
Andrew de los Reyesfb2f4612011-06-09 18:21:49 -0700301 attempter_.Update("", "", false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800302 g_idle_add(&StaticUpdateTestVerify, this);
303}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700304
Darin Petkove6ef2f82011-03-07 17:31:11 -0800305void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700306 EXPECT_EQ(0, attempter_.http_response_code());
307 EXPECT_EQ(&attempter_, processor_->delegate());
308 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
309 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
310 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
311 }
312 EXPECT_EQ(attempter_.response_handler_action_.get(),
313 attempter_.actions_[1].get());
314 DownloadAction* download_action =
315 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
316 ASSERT_TRUE(download_action != NULL);
317 EXPECT_EQ(&attempter_, download_action->delegate());
318 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800319 g_main_loop_quit(loop_);
320}
321
322TEST_F(UpdateAttempterTest, UpdateTest) {
323 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
324 g_idle_add(&StaticUpdateTestStart, this);
325 g_main_loop_run(loop_);
326 g_main_loop_unref(loop_);
327 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700328}
329
Thieu Le116fda32011-04-19 11:01:54 -0700330void UpdateAttempterTest::PingOmahaTestStart() {
331 EXPECT_CALL(*processor_,
332 EnqueueAction(Property(&AbstractAction::Type,
333 OmahaRequestAction::StaticType())))
334 .Times(1);
335 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
336 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200337 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700338}
339
340TEST_F(UpdateAttempterTest, PingOmahaTest) {
341 UpdateCheckScheduler scheduler(&attempter_);
342 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700343 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700344 attempter_.set_update_check_scheduler(&scheduler);
345 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
346 g_idle_add(&StaticPingOmahaTestStart, this);
347 g_main_loop_run(loop_);
348 g_main_loop_unref(loop_);
349 loop_ = NULL;
350 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
351 EXPECT_EQ(true, scheduler.scheduled_);
352}
353
Darin Petkov18c7bce2011-06-16 14:07:00 -0700354TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
355 ActionMock action;
356 const ActionExitCode kCode = kActionCodeDownloadTransferError;
357 attempter_.CreatePendingErrorEvent(&action, kCode);
358 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
359 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
360 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
361 EXPECT_EQ(kCode, attempter_.error_event_->error_code);
362}
363
364TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
365 OmahaResponseHandlerAction *response_action =
366 new OmahaResponseHandlerAction(&prefs_);
367 response_action->install_plan_.is_resume = true;
368 attempter_.response_handler_action_.reset(response_action);
369 ActionMock action;
370 const ActionExitCode kCode = kActionCodeInstallDeviceOpenError;
371 attempter_.CreatePendingErrorEvent(&action, kCode);
372 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
373 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
374 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
375 EXPECT_EQ(kCode | kActionCodeResumedFlag,
376 attempter_.error_event_->error_code);
377}
378
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200379TEST_F(UpdateAttempterTest, ReadTrackFromPolicy) {
380 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
381 g_idle_add(&StaticReadTrackFromPolicyTestStart, this);
382 g_main_loop_run(loop_);
383 g_main_loop_unref(loop_);
384 loop_ = NULL;
385}
386
387void UpdateAttempterTest::ReadTrackFromPolicyTestStart() {
388 // Tests that the update track (aka release channel) is properly fetched
389 // from the device policy.
390
391 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
392 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
393
394 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
395
396 EXPECT_CALL(*device_policy, GetReleaseChannel(_))
397 .WillRepeatedly(DoAll(
398 SetArgumentPointee<0>(std::string("canary-channel")),
399 Return(true)));
400
401 attempter_.Update("", "", false, false);
402 EXPECT_EQ("canary-channel", attempter_.omaha_request_params_.app_track);
403
404 g_idle_add(&StaticQuitMainLoop, this);
405}
406
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700407} // namespace chromeos_update_engine