blob: b5df68cd2dd339d1c9ed2564d02a80ddc09bd0fd [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"
Chris Sosa76a29ae2013-07-11 17:59:24 -070013#include "update_engine/install_plan.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080014#include "update_engine/mock_dbus_interface.h"
Darin Petkov1b003102010-11-30 10:18:36 -080015#include "update_engine/mock_http_fetcher.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016#include "update_engine/mock_payload_state.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070017#include "update_engine/mock_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070018#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070019#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070020#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080021#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070022#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080023#include "update_engine/update_check_scheduler.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024
25using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070026using testing::_;
27using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070028using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070029using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080030using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070031using testing::Property;
32using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070033using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070034
35namespace chromeos_update_engine {
36
37// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070038// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070039// methods.
40class UpdateAttempterUnderTest : public UpdateAttempter {
41 public:
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042 explicit UpdateAttempterUnderTest(MockSystemState* mock_system_state,
43 MockDbusGlib* dbus)
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080044 : UpdateAttempter(mock_system_state, dbus) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070045};
46
47class UpdateAttempterTest : public ::testing::Test {
48 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070049 UpdateAttempterTest()
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050 : attempter_(&mock_system_state_, &dbus_),
Jay Srinivasan43488792012-06-19 00:25:31 -070051 mock_connection_manager(&mock_system_state_),
52 loop_(NULL) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080053 mock_system_state_.set_connection_manager(&mock_connection_manager);
Jay Srinivasan43488792012-06-19 00:25:31 -070054 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -070055 virtual void SetUp() {
56 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080057 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070058 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
59 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080060 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
61 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070062 EXPECT_FALSE(attempter_.download_active_);
63 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
64 EXPECT_EQ(0.0, attempter_.download_progress_);
65 EXPECT_EQ(0, attempter_.last_checked_time_);
66 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070067 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080068 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070069 attempter_.processor_.reset(processor_); // Transfers ownership.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080070 prefs_ = mock_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070071 }
72
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020073 void QuitMainLoop();
74 static gboolean StaticQuitMainLoop(gpointer data);
75
Darin Petkove6ef2f82011-03-07 17:31:11 -080076 void UpdateTestStart();
77 void UpdateTestVerify();
Chris Sosa28e479c2013-07-12 11:39:53 -070078 void RollbackTestStart(bool enterprise_rollback, bool stable_channel);
Chris Sosa76a29ae2013-07-11 17:59:24 -070079 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -080080 static gboolean StaticUpdateTestStart(gpointer data);
81 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -070082 static gboolean StaticRollbackTestStart(gpointer data);
83 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
Chris Sosa28e479c2013-07-12 11:39:53 -070084 static gboolean StaticStableChannelRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -070085 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020086
Thieu Le116fda32011-04-19 11:01:54 -070087 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -070088 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020089
Jay Srinivasanae4697c2013-03-18 17:08:08 -070090 void ReadChannelFromPolicyTestStart();
91 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -080092
Jay Srinivasan0a708742012-03-20 11:26:12 -070093 void ReadUpdateDisabledFromPolicyTestStart();
94 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
95
96 void ReadTargetVersionPrefixFromPolicyTestStart();
97 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
98 gpointer data);
99
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700100 void ReadScatterFactorFromPolicyTestStart();
101 static gboolean StaticReadScatterFactorFromPolicyTestStart(
102 gpointer data);
103
104 void DecrementUpdateCheckCountTestStart();
105 static gboolean StaticDecrementUpdateCheckCountTestStart(
106 gpointer data);
107
Jay Srinivasan08fce042012-06-07 16:31:01 -0700108 void NoScatteringDoneDuringManualUpdateTestStart();
109 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
110 gpointer data);
111
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800112 NiceMock<MockSystemState> mock_system_state_;
113 NiceMock<MockDbusGlib> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700114 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800115 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800116 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800117 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800118 GMainLoop* loop_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700119};
120
Darin Petkov1b003102010-11-30 10:18:36 -0800121TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
122 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
123 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800124 DownloadAction action(prefs_, NULL, fetcher.release());
125 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700126 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800127 EXPECT_EQ(503, attempter_.http_response_code());
128 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
129 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
130}
131
132TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
133 ActionMock action;
134 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
135 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800136 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800137 .WillOnce(Return(false));
David Zeuthena99981f2013-04-29 13:42:47 -0700138 attempter_.ActionCompleted(NULL, &action, kErrorCodeError);
Darin Petkov1b003102010-11-30 10:18:36 -0800139 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
140}
141
142TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
143 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
144 fetcher->FailTransfer(500); // Sets the HTTP response code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700145 OmahaRequestAction action(&mock_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800146 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800147 ObjectCollectorAction<OmahaResponse> collector_action;
148 BondActions(&action, &collector_action);
149 OmahaResponse response;
150 response.poll_interval = 234;
151 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800152 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800153 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800154 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700155 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800156 EXPECT_EQ(500, attempter_.http_response_code());
157 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
158 EXPECT_EQ(234, scheduler.poll_interval());
159 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
160}
161
Darin Petkovcd1666f2010-09-23 09:53:44 -0700162TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700163 extern const char* kUpdateCompletedMarker;
164 const FilePath kMarker(kUpdateCompletedMarker);
165 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800166 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700167 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
168 EXPECT_TRUE(file_util::Delete(kMarker, false));
169}
170
171TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700172 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
173 ErrorCode code);
174 EXPECT_EQ(kErrorCodeSuccess,
175 GetErrorCodeForAction(NULL, kErrorCodeSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700176
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800177 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700178 OmahaRequestAction omaha_request_action(&mock_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800179 NULL, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700180 EXPECT_EQ(kErrorCodeOmahaRequestError,
181 GetErrorCodeForAction(&omaha_request_action, kErrorCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800182 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
David Zeuthena99981f2013-04-29 13:42:47 -0700183 EXPECT_EQ(kErrorCodeOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700184 GetErrorCodeForAction(&omaha_response_handler_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700185 kErrorCodeError));
Alex Deymo42432912013-07-12 20:21:15 -0700186 FilesystemCopierAction filesystem_copier_action(
187 &mock_system_state_, false, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700188 EXPECT_EQ(kErrorCodeFilesystemCopierError,
189 GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800190 PostinstallRunnerAction postinstall_runner_action;
David Zeuthena99981f2013-04-29 13:42:47 -0700191 EXPECT_EQ(kErrorCodePostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700192 GetErrorCodeForAction(&postinstall_runner_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700193 kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700194 ActionMock action_mock;
195 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
David Zeuthena99981f2013-04-29 13:42:47 -0700196 EXPECT_EQ(kErrorCodeError,
197 GetErrorCodeForAction(&action_mock, kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700198}
199
Darin Petkov36275772010-10-01 11:40:57 -0700200TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700201 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800202 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700203 .WillOnce(Return(false));
204 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700205 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800206 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700207 .WillOnce(DoAll(
208 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
209 Return(true)));
210 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700211 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800212 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700213 .WillOnce(DoAll(
214 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
215 Return(true)));
216 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700217 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800218 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700219 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700220 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700221}
222
223TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800224 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700225 .WillOnce(Return(false))
226 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
227 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
228 .WillOnce(DoAll(
229 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
230 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800231 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700232 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800233 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
234 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
235 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700236 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
237 .Times(1);
238 for (int i = 0; i < 4; i ++)
239 attempter_.MarkDeltaUpdateFailure();
240}
241
Darin Petkov1b003102010-11-30 10:18:36 -0800242TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
243 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
244 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800245 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
246 .Times(0);
247 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700248 string url1 = "http://url1";
249 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800250 response.payload_urls.push_back("https://url");
Jay Srinivasan53173b92013-05-17 17:13:01 -0700251 EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl())
252 .WillRepeatedly(Return(url1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800253 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800254 attempter_.ScheduleErrorEventAction();
Jay Srinivasan53173b92013-05-17 17:13:01 -0700255 EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800256}
257
258TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
259 EXPECT_CALL(*processor_,
260 EnqueueAction(Property(&AbstractAction::Type,
261 OmahaRequestAction::StaticType())))
262 .Times(1);
263 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
David Zeuthena99981f2013-04-29 13:42:47 -0700264 ErrorCode err = kErrorCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800265 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800266 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
267 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800268 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800269 attempter_.ScheduleErrorEventAction();
270 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
271}
272
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700273TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
274 extern const char* UpdateStatusToString(UpdateStatus);
275 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
276 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
277 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
278 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
279 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
280 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
281 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
282 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
283 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
284 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
285 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
286 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
287 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
288 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
289 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
290 EXPECT_STREQ("unknown status",
291 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
292}
293
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200294void UpdateAttempterTest::QuitMainLoop() {
295 g_main_loop_quit(loop_);
296}
297
298gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
299 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
300 return FALSE;
301}
302
Darin Petkove6ef2f82011-03-07 17:31:11 -0800303gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
304 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
305 return FALSE;
306}
307
308gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
309 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
310 return FALSE;
311}
312
Chris Sosa76a29ae2013-07-11 17:59:24 -0700313gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700314 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700315 return FALSE;
316}
317
318gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700319 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(true, false);
320 return FALSE;
321}
322
323gboolean UpdateAttempterTest::StaticStableChannelRollbackTestStart(
324 gpointer data) {
325 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
326 false, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700327 return FALSE;
328}
329
330gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
331 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
332 return FALSE;
333}
334
Thieu Le116fda32011-04-19 11:01:54 -0700335gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
336 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
337 return FALSE;
338}
339
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700340gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200341 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700342 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
343 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700344 return FALSE;
345}
346
Jay Srinivasan0a708742012-03-20 11:26:12 -0700347gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
348 gpointer data) {
349 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
350 ua_test->ReadUpdateDisabledFromPolicyTestStart();
351 return FALSE;
352}
353
354gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
355 gpointer data) {
356 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
357 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
358 return FALSE;
359}
360
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700361gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
362 gpointer data) {
363 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
364 ua_test->ReadScatterFactorFromPolicyTestStart();
365 return FALSE;
366}
367
368gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
369 gpointer data) {
370 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
371 ua_test->DecrementUpdateCheckCountTestStart();
372 return FALSE;
373}
374
Jay Srinivasan08fce042012-06-07 16:31:01 -0700375gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
376 gpointer data) {
377 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
378 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
379 return FALSE;
380}
381
Darin Petkove6ef2f82011-03-07 17:31:11 -0800382namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700383// Actions that will be built as part of an update check.
384const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800385 OmahaRequestAction::StaticType(),
386 OmahaResponseHandlerAction::StaticType(),
387 FilesystemCopierAction::StaticType(),
388 FilesystemCopierAction::StaticType(),
389 OmahaRequestAction::StaticType(),
390 DownloadAction::StaticType(),
391 OmahaRequestAction::StaticType(),
392 FilesystemCopierAction::StaticType(),
393 FilesystemCopierAction::StaticType(),
394 PostinstallRunnerAction::StaticType(),
395 OmahaRequestAction::StaticType()
396};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700397
398// Actions that will be built as part of a user-initiated rollback.
399const string kRollbackActionTypes[] = {
400 InstallPlanAction::StaticType(),
401 PostinstallRunnerAction::StaticType(),
402};
403
Darin Petkove6ef2f82011-03-07 17:31:11 -0800404} // namespace {}
405
406void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700407 attempter_.set_http_response_code(200);
408 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700409 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700410 EXPECT_CALL(*processor_,
411 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700412 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700413 }
414 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
415
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800416 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800417 g_idle_add(&StaticUpdateTestVerify, this);
418}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700419
Darin Petkove6ef2f82011-03-07 17:31:11 -0800420void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700421 EXPECT_EQ(0, attempter_.http_response_code());
422 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700423 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
424 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
425 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700426 }
427 EXPECT_EQ(attempter_.response_handler_action_.get(),
428 attempter_.actions_[1].get());
429 DownloadAction* download_action =
430 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
431 ASSERT_TRUE(download_action != NULL);
432 EXPECT_EQ(&attempter_, download_action->delegate());
433 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800434 g_main_loop_quit(loop_);
435}
436
Chris Sosa28e479c2013-07-12 11:39:53 -0700437void UpdateAttempterTest::RollbackTestStart(
438 bool enterprise_rollback, bool stable_channel) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700439 // Create a device policy so that we can change settings.
440 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
441 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
442
443 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
444 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
445 Return(device_policy));
446
447 string install_path = "/dev/sda3";
Chris Sosa28e479c2013-07-12 11:39:53 -0700448 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700449
Chris Sosa28e479c2013-07-12 11:39:53 -0700450 // We only allow rollback on devices that are neither enterprise enrolled or
451 // not on the stable channel.
452 if (!(enterprise_rollback || stable_channel)) {
453 is_rollback_allowed = true;
454 }
455
456 // Set up the policy for the test given our args.
457 if (stable_channel) {
458 attempter_.omaha_request_params_->set_current_channel(
459 string("stable-channel"));
460 } else if (enterprise_rollback) {
461 // We return an empty owner as this is an enterprise.
462 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
463 DoAll(SetArgumentPointee<0>(std::string("")),
464 Return(true)));
465 } else {
466 // We return a fake owner as this is an owned consumer device.
Chris Sosa76a29ae2013-07-11 17:59:24 -0700467 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
468 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
469 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700470 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700471
Chris Sosa28e479c2013-07-12 11:39:53 -0700472 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700473 InSequence s;
474 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
475 EXPECT_CALL(*processor_,
476 EnqueueAction(Property(&AbstractAction::Type,
477 kRollbackActionTypes[i]))).Times(1);
478 }
479 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
480
481 EXPECT_TRUE(attempter_.Rollback(true, &install_path));
482 g_idle_add(&StaticRollbackTestVerify, this);
483 } else {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700484 EXPECT_FALSE(attempter_.Rollback(true, &install_path));
485 g_main_loop_quit(loop_);
486 }
487}
488
489void UpdateAttempterTest::RollbackTestVerify() {
490 // Verifies the actions that were enqueued.
491 EXPECT_EQ(&attempter_, processor_->delegate());
492 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
493 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
494 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
495 }
496 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
497 InstallPlanAction* install_plan_action =
498 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
499 InstallPlan* install_plan = install_plan_action->install_plan();
500 EXPECT_EQ(install_plan->install_path, string("/dev/sda3"));
501 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sda2"));
502 EXPECT_EQ(install_plan->powerwash_required, true);
503 g_main_loop_quit(loop_);
504}
505
Darin Petkove6ef2f82011-03-07 17:31:11 -0800506TEST_F(UpdateAttempterTest, UpdateTest) {
507 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
508 g_idle_add(&StaticUpdateTestStart, this);
509 g_main_loop_run(loop_);
510 g_main_loop_unref(loop_);
511 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700512}
513
Chris Sosa76a29ae2013-07-11 17:59:24 -0700514TEST_F(UpdateAttempterTest, RollbackTest) {
515 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
516 g_idle_add(&StaticRollbackTestStart, this);
517 g_main_loop_run(loop_);
518 g_main_loop_unref(loop_);
519 loop_ = NULL;
520}
521
Chris Sosa28e479c2013-07-12 11:39:53 -0700522TEST_F(UpdateAttempterTest, StableChannelRollbackTest) {
523 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
524 g_idle_add(&StaticStableChannelRollbackTestStart, this);
525 g_main_loop_run(loop_);
526 g_main_loop_unref(loop_);
527 loop_ = NULL;
528}
529
Chris Sosa76a29ae2013-07-11 17:59:24 -0700530TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
531 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
532 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
533 g_main_loop_run(loop_);
534 g_main_loop_unref(loop_);
535 loop_ = NULL;
536}
537
Thieu Le116fda32011-04-19 11:01:54 -0700538void UpdateAttempterTest::PingOmahaTestStart() {
539 EXPECT_CALL(*processor_,
540 EnqueueAction(Property(&AbstractAction::Type,
541 OmahaRequestAction::StaticType())))
542 .Times(1);
543 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
544 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200545 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700546}
547
548TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800549 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700550 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700551 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700552 attempter_.set_update_check_scheduler(&scheduler);
553 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
554 g_idle_add(&StaticPingOmahaTestStart, this);
555 g_main_loop_run(loop_);
556 g_main_loop_unref(loop_);
557 loop_ = NULL;
558 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
559 EXPECT_EQ(true, scheduler.scheduled_);
560}
561
Darin Petkov18c7bce2011-06-16 14:07:00 -0700562TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
563 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700564 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700565 attempter_.CreatePendingErrorEvent(&action, kCode);
566 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
567 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
568 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700569 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800570 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700571}
572
573TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
574 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800575 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700576 response_action->install_plan_.is_resume = true;
577 attempter_.response_handler_action_.reset(response_action);
578 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700579 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700580 attempter_.CreatePendingErrorEvent(&action, kCode);
581 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
582 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
583 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700584 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700585 attempter_.error_event_->error_code);
586}
587
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700588TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200589 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700590 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200591 g_main_loop_run(loop_);
592 g_main_loop_unref(loop_);
593 loop_ = NULL;
594}
595
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700596void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
597 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200598 // from the device policy.
599
600 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
601 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
602
603 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700604 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
605 Return(device_policy));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200606
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700607 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
608 DoAll(SetArgumentPointee<0>(bool(false)),
609 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200610
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700611 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
612 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
613 Return(true)));
614
615 attempter_.omaha_request_params_->set_root("./UpdateAttempterTest");
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800616 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700617 EXPECT_EQ("beta-channel",
618 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200619
620 g_idle_add(&StaticQuitMainLoop, this);
621}
622
Jay Srinivasan0a708742012-03-20 11:26:12 -0700623TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
624 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
625 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
626 g_main_loop_run(loop_);
627 g_main_loop_unref(loop_);
628 loop_ = NULL;
629}
630
631void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
632 // Tests that the update_disbled flag is properly fetched
633 // from the device policy.
634
635 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
636 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
637
638 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700639 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
640 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700641
642 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
643 .WillRepeatedly(DoAll(
644 SetArgumentPointee<0>(true),
645 Return(true)));
646
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800647 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700648 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700649
650 g_idle_add(&StaticQuitMainLoop, this);
651}
652
653TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
654 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
655 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
656 g_main_loop_run(loop_);
657 g_main_loop_unref(loop_);
658 loop_ = NULL;
659}
660
661void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
662 // Tests that the target_version_prefix value is properly fetched
663 // from the device policy.
664
665 const std::string target_version_prefix = "1412.";
666
667 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
668 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
669
670 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700671 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
672 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700673
674 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
675 .WillRepeatedly(DoAll(
676 SetArgumentPointee<0>(target_version_prefix),
677 Return(true)));
678
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800679 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700680 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700681 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700682
683 g_idle_add(&StaticQuitMainLoop, this);
684}
685
686
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700687TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
688 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
689 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
690 g_main_loop_run(loop_);
691 g_main_loop_unref(loop_);
692 loop_ = NULL;
693}
694
695// Tests that the scatter_factor_in_seconds value is properly fetched
696// from the device policy.
697void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
698 int64 scatter_factor_in_seconds = 36000;
699
700 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
701 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
702
703 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800704 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700705 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700706
707 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
708 .WillRepeatedly(DoAll(
709 SetArgumentPointee<0>(scatter_factor_in_seconds),
710 Return(true)));
711
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800712 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700713 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
714
715 g_idle_add(&StaticQuitMainLoop, this);
716}
717
718TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
719 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
720 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
721 g_main_loop_run(loop_);
722 g_main_loop_unref(loop_);
723 loop_ = NULL;
724}
725
726void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
727 // Tests that the scatter_factor_in_seconds value is properly fetched
728 // from the device policy and is decremented if value > 0.
729 int64 initial_value = 5;
730 Prefs prefs;
731 attempter_.prefs_ = &prefs;
732
Jay Srinivasan08fce042012-06-07 16:31:01 -0700733 EXPECT_CALL(mock_system_state_,
734 IsOOBEComplete()).WillRepeatedly(Return(true));
735
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700736 string prefs_dir;
737 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
738 &prefs_dir));
739 ScopedDirRemover temp_dir_remover(prefs_dir);
740
741 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
742 << "Failed to initialize preferences.";
743 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
744
745 int64 scatter_factor_in_seconds = 10;
746
747 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
748 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
749
750 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800751 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700752 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700753
754 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
755 .WillRepeatedly(DoAll(
756 SetArgumentPointee<0>(scatter_factor_in_seconds),
757 Return(true)));
758
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800759 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700760 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
761
762 // Make sure the file still exists.
763 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
764
765 int64 new_value;
766 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
767 EXPECT_EQ(initial_value - 1, new_value);
768
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700769 EXPECT_TRUE(
770 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700771
772 // However, if the count is already 0, it's not decremented. Test that.
773 initial_value = 0;
774 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800775 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700776 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
777 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
778 EXPECT_EQ(initial_value, new_value);
779
780 g_idle_add(&StaticQuitMainLoop, this);
781}
782
Jay Srinivasan08fce042012-06-07 16:31:01 -0700783TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
784 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
785 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
786 g_main_loop_run(loop_);
787 g_main_loop_unref(loop_);
788 loop_ = NULL;
789}
790
791void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
792 // Tests that no scattering logic is enabled if the update check
793 // is manually done (as opposed to a scheduled update check)
794 int64 initial_value = 8;
795 Prefs prefs;
796 attempter_.prefs_ = &prefs;
797
798 EXPECT_CALL(mock_system_state_,
799 IsOOBEComplete()).WillRepeatedly(Return(true));
800
801 string prefs_dir;
802 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
803 &prefs_dir));
804 ScopedDirRemover temp_dir_remover(prefs_dir);
805
806 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
807 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700808 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700809 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
810
811 // make sure scatter_factor is non-zero as scattering is disabled
812 // otherwise.
813 int64 scatter_factor_in_seconds = 50;
814
815 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
816 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
817
818 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800819 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700820 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700821
822 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
823 .WillRepeatedly(DoAll(
824 SetArgumentPointee<0>(scatter_factor_in_seconds),
825 Return(true)));
826
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800827 // Trigger an interactive check so we can test that scattering is disabled.
828 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700829 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
830
831 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700832 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700833 EXPECT_FALSE(
834 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700835 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700836 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
837 EXPECT_FALSE(
838 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700839 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
840
841 g_idle_add(&StaticQuitMainLoop, this);
842}
843
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700844} // namespace chromeos_update_engine