blob: 1a520959da63a9d68d4257933d881acb11c81a73 [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"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070024#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070025
26using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070027using testing::_;
28using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070029using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070030using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080031using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070032using testing::Property;
33using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070034using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070035
36namespace chromeos_update_engine {
37
38// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070039// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070040// methods.
41class UpdateAttempterUnderTest : public UpdateAttempter {
42 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070043 // We always feed an explicit update completed marker name; however, unless
44 // explicitly specified, we feed an empty string, which causes the
45 // UpdateAttempter class to ignore / not write the marker file.
46 UpdateAttempterUnderTest(MockSystemState* mock_system_state,
47 MockDbusGlib* dbus)
48 : UpdateAttempter(mock_system_state, dbus, "") {}
49
50 UpdateAttempterUnderTest(MockSystemState* mock_system_state,
51 MockDbusGlib* dbus,
52 const string& update_completed_marker)
53 : UpdateAttempter(mock_system_state, dbus, update_completed_marker) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070054};
55
56class UpdateAttempterTest : public ::testing::Test {
57 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070058 UpdateAttempterTest()
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080059 : attempter_(&mock_system_state_, &dbus_),
Jay Srinivasan43488792012-06-19 00:25:31 -070060 mock_connection_manager(&mock_system_state_),
61 loop_(NULL) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080062 mock_system_state_.set_connection_manager(&mock_connection_manager);
Jay Srinivasan43488792012-06-19 00:25:31 -070063 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070064
Darin Petkovf42cc1c2010-09-01 09:03:02 -070065 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070066 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
67
Darin Petkovf42cc1c2010-09-01 09:03:02 -070068 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080069 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070070 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
71 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080072 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
73 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070074 EXPECT_FALSE(attempter_.download_active_);
75 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
76 EXPECT_EQ(0.0, attempter_.download_progress_);
77 EXPECT_EQ(0, attempter_.last_checked_time_);
78 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070079 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080080 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070081 attempter_.processor_.reset(processor_); // Transfers ownership.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080082 prefs_ = mock_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070083 }
84
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070085 virtual void TearDown() {
86 utils::RecursiveUnlinkDir(test_dir_);
87 }
88
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020089 void QuitMainLoop();
90 static gboolean StaticQuitMainLoop(gpointer data);
91
Darin Petkove6ef2f82011-03-07 17:31:11 -080092 void UpdateTestStart();
93 void UpdateTestVerify();
Chris Sosa28e479c2013-07-12 11:39:53 -070094 void RollbackTestStart(bool enterprise_rollback, bool stable_channel);
Chris Sosa76a29ae2013-07-11 17:59:24 -070095 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -080096 static gboolean StaticUpdateTestStart(gpointer data);
97 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -070098 static gboolean StaticRollbackTestStart(gpointer data);
99 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
Chris Sosa28e479c2013-07-12 11:39:53 -0700100 static gboolean StaticStableChannelRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700101 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200102
Thieu Le116fda32011-04-19 11:01:54 -0700103 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700104 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200105
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700106 void ReadChannelFromPolicyTestStart();
107 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800108
Jay Srinivasan0a708742012-03-20 11:26:12 -0700109 void ReadUpdateDisabledFromPolicyTestStart();
110 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
111
112 void ReadTargetVersionPrefixFromPolicyTestStart();
113 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
114 gpointer data);
115
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700116 void ReadScatterFactorFromPolicyTestStart();
117 static gboolean StaticReadScatterFactorFromPolicyTestStart(
118 gpointer data);
119
120 void DecrementUpdateCheckCountTestStart();
121 static gboolean StaticDecrementUpdateCheckCountTestStart(
122 gpointer data);
123
Jay Srinivasan08fce042012-06-07 16:31:01 -0700124 void NoScatteringDoneDuringManualUpdateTestStart();
125 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
126 gpointer data);
127
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800128 NiceMock<MockSystemState> mock_system_state_;
129 NiceMock<MockDbusGlib> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700130 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800131 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800132 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800133 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800134 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700135
136 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700137};
138
Darin Petkov1b003102010-11-30 10:18:36 -0800139TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
140 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
141 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800142 DownloadAction action(prefs_, NULL, fetcher.release());
143 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700144 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800145 EXPECT_EQ(503, attempter_.http_response_code());
146 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
147 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
148}
149
150TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
151 ActionMock action;
152 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
153 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800154 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800155 .WillOnce(Return(false));
David Zeuthena99981f2013-04-29 13:42:47 -0700156 attempter_.ActionCompleted(NULL, &action, kErrorCodeError);
Darin Petkov1b003102010-11-30 10:18:36 -0800157 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
158}
159
160TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
161 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
162 fetcher->FailTransfer(500); // Sets the HTTP response code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700163 OmahaRequestAction action(&mock_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800164 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800165 ObjectCollectorAction<OmahaResponse> collector_action;
166 BondActions(&action, &collector_action);
167 OmahaResponse response;
168 response.poll_interval = 234;
169 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800170 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800171 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800172 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700173 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800174 EXPECT_EQ(500, attempter_.http_response_code());
175 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
176 EXPECT_EQ(234, scheduler.poll_interval());
177 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
178}
179
Darin Petkovcd1666f2010-09-23 09:53:44 -0700180TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700181 string test_update_completed_marker;
182 CHECK(utils::MakeTempFile(
183 "/tmp/update_attempter_unittest-update_completed_marker-XXXXXX",
184 &test_update_completed_marker, NULL));
185 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
186 const FilePath marker(test_update_completed_marker);
187 EXPECT_EQ(0, file_util::WriteFile(marker, "", 0));
188 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
189 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700190 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700191}
192
193TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700194 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
195 ErrorCode code);
196 EXPECT_EQ(kErrorCodeSuccess,
197 GetErrorCodeForAction(NULL, kErrorCodeSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700198
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800199 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700200 OmahaRequestAction omaha_request_action(&mock_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800201 NULL, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700202 EXPECT_EQ(kErrorCodeOmahaRequestError,
203 GetErrorCodeForAction(&omaha_request_action, kErrorCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800204 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
David Zeuthena99981f2013-04-29 13:42:47 -0700205 EXPECT_EQ(kErrorCodeOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700206 GetErrorCodeForAction(&omaha_response_handler_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700207 kErrorCodeError));
Alex Deymo42432912013-07-12 20:21:15 -0700208 FilesystemCopierAction filesystem_copier_action(
209 &mock_system_state_, false, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700210 EXPECT_EQ(kErrorCodeFilesystemCopierError,
211 GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800212 PostinstallRunnerAction postinstall_runner_action;
David Zeuthena99981f2013-04-29 13:42:47 -0700213 EXPECT_EQ(kErrorCodePostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700214 GetErrorCodeForAction(&postinstall_runner_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700215 kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700216 ActionMock action_mock;
217 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
David Zeuthena99981f2013-04-29 13:42:47 -0700218 EXPECT_EQ(kErrorCodeError,
219 GetErrorCodeForAction(&action_mock, kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700220}
221
Darin Petkov36275772010-10-01 11:40:57 -0700222TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700223 attempter_.omaha_request_params_->set_delta_okay(true);
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 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700227 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800228 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700229 .WillOnce(DoAll(
230 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
231 Return(true)));
232 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700233 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800234 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700235 .WillOnce(DoAll(
236 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
237 Return(true)));
238 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700239 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800240 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700241 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700242 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700243}
244
245TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800246 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700247 .WillOnce(Return(false))
248 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
249 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
250 .WillOnce(DoAll(
251 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
252 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800253 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700254 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800255 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
256 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
257 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700258 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
259 .Times(1);
260 for (int i = 0; i < 4; i ++)
261 attempter_.MarkDeltaUpdateFailure();
262}
263
Darin Petkov1b003102010-11-30 10:18:36 -0800264TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
265 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
266 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800267 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
268 .Times(0);
269 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700270 string url1 = "http://url1";
271 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800272 response.payload_urls.push_back("https://url");
Jay Srinivasan53173b92013-05-17 17:13:01 -0700273 EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl())
274 .WillRepeatedly(Return(url1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800275 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800276 attempter_.ScheduleErrorEventAction();
Jay Srinivasan53173b92013-05-17 17:13:01 -0700277 EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800278}
279
280TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
281 EXPECT_CALL(*processor_,
282 EnqueueAction(Property(&AbstractAction::Type,
283 OmahaRequestAction::StaticType())))
284 .Times(1);
285 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
David Zeuthena99981f2013-04-29 13:42:47 -0700286 ErrorCode err = kErrorCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800287 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800288 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
289 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800290 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800291 attempter_.ScheduleErrorEventAction();
292 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
293}
294
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700295TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
296 extern const char* UpdateStatusToString(UpdateStatus);
297 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
298 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
299 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
300 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
301 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
302 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
303 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
304 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
305 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
306 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
307 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
308 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
309 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
310 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
311 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
312 EXPECT_STREQ("unknown status",
313 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
314}
315
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200316void UpdateAttempterTest::QuitMainLoop() {
317 g_main_loop_quit(loop_);
318}
319
320gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
321 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
322 return FALSE;
323}
324
Darin Petkove6ef2f82011-03-07 17:31:11 -0800325gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
326 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
327 return FALSE;
328}
329
330gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
331 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
332 return FALSE;
333}
334
Chris Sosa76a29ae2013-07-11 17:59:24 -0700335gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700336 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700337 return FALSE;
338}
339
340gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700341 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(true, false);
342 return FALSE;
343}
344
345gboolean UpdateAttempterTest::StaticStableChannelRollbackTestStart(
346 gpointer data) {
347 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
348 false, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700349 return FALSE;
350}
351
352gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
353 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
354 return FALSE;
355}
356
Thieu Le116fda32011-04-19 11:01:54 -0700357gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
358 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
359 return FALSE;
360}
361
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700362gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200363 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700364 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
365 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700366 return FALSE;
367}
368
Jay Srinivasan0a708742012-03-20 11:26:12 -0700369gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
370 gpointer data) {
371 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
372 ua_test->ReadUpdateDisabledFromPolicyTestStart();
373 return FALSE;
374}
375
376gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
377 gpointer data) {
378 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
379 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
380 return FALSE;
381}
382
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700383gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
384 gpointer data) {
385 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
386 ua_test->ReadScatterFactorFromPolicyTestStart();
387 return FALSE;
388}
389
390gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
391 gpointer data) {
392 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
393 ua_test->DecrementUpdateCheckCountTestStart();
394 return FALSE;
395}
396
Jay Srinivasan08fce042012-06-07 16:31:01 -0700397gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
398 gpointer data) {
399 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
400 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
401 return FALSE;
402}
403
Darin Petkove6ef2f82011-03-07 17:31:11 -0800404namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700405// Actions that will be built as part of an update check.
406const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800407 OmahaRequestAction::StaticType(),
408 OmahaResponseHandlerAction::StaticType(),
409 FilesystemCopierAction::StaticType(),
410 FilesystemCopierAction::StaticType(),
411 OmahaRequestAction::StaticType(),
412 DownloadAction::StaticType(),
413 OmahaRequestAction::StaticType(),
414 FilesystemCopierAction::StaticType(),
415 FilesystemCopierAction::StaticType(),
416 PostinstallRunnerAction::StaticType(),
417 OmahaRequestAction::StaticType()
418};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700419
420// Actions that will be built as part of a user-initiated rollback.
421const string kRollbackActionTypes[] = {
422 InstallPlanAction::StaticType(),
423 PostinstallRunnerAction::StaticType(),
424};
425
Darin Petkove6ef2f82011-03-07 17:31:11 -0800426} // namespace {}
427
428void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700429 attempter_.set_http_response_code(200);
430 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700431 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700432 EXPECT_CALL(*processor_,
433 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700434 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700435 }
436 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
437
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800438 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800439 g_idle_add(&StaticUpdateTestVerify, this);
440}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700441
Darin Petkove6ef2f82011-03-07 17:31:11 -0800442void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700443 EXPECT_EQ(0, attempter_.http_response_code());
444 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700445 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
446 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
447 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700448 }
449 EXPECT_EQ(attempter_.response_handler_action_.get(),
450 attempter_.actions_[1].get());
451 DownloadAction* download_action =
452 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
453 ASSERT_TRUE(download_action != NULL);
454 EXPECT_EQ(&attempter_, download_action->delegate());
455 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800456 g_main_loop_quit(loop_);
457}
458
Chris Sosa28e479c2013-07-12 11:39:53 -0700459void UpdateAttempterTest::RollbackTestStart(
460 bool enterprise_rollback, bool stable_channel) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700461 // Create a device policy so that we can change settings.
462 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
463 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
464
465 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
466 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
467 Return(device_policy));
468
469 string install_path = "/dev/sda3";
Chris Sosa28e479c2013-07-12 11:39:53 -0700470 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700471
Chris Sosa28e479c2013-07-12 11:39:53 -0700472 // We only allow rollback on devices that are neither enterprise enrolled or
473 // not on the stable channel.
474 if (!(enterprise_rollback || stable_channel)) {
475 is_rollback_allowed = true;
476 }
477
478 // Set up the policy for the test given our args.
479 if (stable_channel) {
480 attempter_.omaha_request_params_->set_current_channel(
481 string("stable-channel"));
482 } else if (enterprise_rollback) {
483 // We return an empty owner as this is an enterprise.
484 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
485 DoAll(SetArgumentPointee<0>(std::string("")),
486 Return(true)));
487 } else {
488 // We return a fake owner as this is an owned consumer device.
Chris Sosa76a29ae2013-07-11 17:59:24 -0700489 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
490 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
491 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700492 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700493
Chris Sosa28e479c2013-07-12 11:39:53 -0700494 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700495 InSequence s;
496 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
497 EXPECT_CALL(*processor_,
498 EnqueueAction(Property(&AbstractAction::Type,
499 kRollbackActionTypes[i]))).Times(1);
500 }
501 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
502
503 EXPECT_TRUE(attempter_.Rollback(true, &install_path));
504 g_idle_add(&StaticRollbackTestVerify, this);
505 } else {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700506 EXPECT_FALSE(attempter_.Rollback(true, &install_path));
507 g_main_loop_quit(loop_);
508 }
509}
510
511void UpdateAttempterTest::RollbackTestVerify() {
512 // Verifies the actions that were enqueued.
513 EXPECT_EQ(&attempter_, processor_->delegate());
514 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
515 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
516 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
517 }
518 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
519 InstallPlanAction* install_plan_action =
520 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
521 InstallPlan* install_plan = install_plan_action->install_plan();
522 EXPECT_EQ(install_plan->install_path, string("/dev/sda3"));
523 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sda2"));
524 EXPECT_EQ(install_plan->powerwash_required, true);
525 g_main_loop_quit(loop_);
526}
527
Darin Petkove6ef2f82011-03-07 17:31:11 -0800528TEST_F(UpdateAttempterTest, UpdateTest) {
529 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
530 g_idle_add(&StaticUpdateTestStart, this);
531 g_main_loop_run(loop_);
532 g_main_loop_unref(loop_);
533 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700534}
535
Chris Sosa76a29ae2013-07-11 17:59:24 -0700536TEST_F(UpdateAttempterTest, RollbackTest) {
537 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
538 g_idle_add(&StaticRollbackTestStart, this);
539 g_main_loop_run(loop_);
540 g_main_loop_unref(loop_);
541 loop_ = NULL;
542}
543
Chris Sosa28e479c2013-07-12 11:39:53 -0700544TEST_F(UpdateAttempterTest, StableChannelRollbackTest) {
545 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
546 g_idle_add(&StaticStableChannelRollbackTestStart, this);
547 g_main_loop_run(loop_);
548 g_main_loop_unref(loop_);
549 loop_ = NULL;
550}
551
Chris Sosa76a29ae2013-07-11 17:59:24 -0700552TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
553 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
554 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
555 g_main_loop_run(loop_);
556 g_main_loop_unref(loop_);
557 loop_ = NULL;
558}
559
Thieu Le116fda32011-04-19 11:01:54 -0700560void UpdateAttempterTest::PingOmahaTestStart() {
561 EXPECT_CALL(*processor_,
562 EnqueueAction(Property(&AbstractAction::Type,
563 OmahaRequestAction::StaticType())))
564 .Times(1);
565 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
566 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200567 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700568}
569
570TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800571 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700572 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700573 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700574 attempter_.set_update_check_scheduler(&scheduler);
575 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
576 g_idle_add(&StaticPingOmahaTestStart, this);
577 g_main_loop_run(loop_);
578 g_main_loop_unref(loop_);
579 loop_ = NULL;
580 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
581 EXPECT_EQ(true, scheduler.scheduled_);
582}
583
Darin Petkov18c7bce2011-06-16 14:07:00 -0700584TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
585 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700586 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700587 attempter_.CreatePendingErrorEvent(&action, kCode);
588 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
589 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
590 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700591 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800592 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700593}
594
595TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
596 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800597 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700598 response_action->install_plan_.is_resume = true;
599 attempter_.response_handler_action_.reset(response_action);
600 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700601 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700602 attempter_.CreatePendingErrorEvent(&action, kCode);
603 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
604 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
605 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700606 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700607 attempter_.error_event_->error_code);
608}
609
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700610TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200611 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700612 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200613 g_main_loop_run(loop_);
614 g_main_loop_unref(loop_);
615 loop_ = NULL;
616}
617
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700618void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
619 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200620 // from the device policy.
621
622 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
623 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
624
625 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700626 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
627 Return(device_policy));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200628
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700629 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
630 DoAll(SetArgumentPointee<0>(bool(false)),
631 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200632
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700633 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
634 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
635 Return(true)));
636
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700637 ASSERT_FALSE(test_dir_.empty());
638 attempter_.omaha_request_params_->set_root(test_dir_);
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800639 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700640 EXPECT_EQ("beta-channel",
641 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200642
643 g_idle_add(&StaticQuitMainLoop, this);
644}
645
Jay Srinivasan0a708742012-03-20 11:26:12 -0700646TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
647 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
648 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
649 g_main_loop_run(loop_);
650 g_main_loop_unref(loop_);
651 loop_ = NULL;
652}
653
654void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
655 // Tests that the update_disbled flag is properly fetched
656 // from the device policy.
657
658 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
659 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
660
661 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700662 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
663 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700664
665 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
666 .WillRepeatedly(DoAll(
667 SetArgumentPointee<0>(true),
668 Return(true)));
669
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800670 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700671 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700672
673 g_idle_add(&StaticQuitMainLoop, this);
674}
675
676TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
677 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
678 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
679 g_main_loop_run(loop_);
680 g_main_loop_unref(loop_);
681 loop_ = NULL;
682}
683
684void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
685 // Tests that the target_version_prefix value is properly fetched
686 // from the device policy.
687
688 const std::string target_version_prefix = "1412.";
689
690 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
691 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
692
693 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700694 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
695 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700696
697 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
698 .WillRepeatedly(DoAll(
699 SetArgumentPointee<0>(target_version_prefix),
700 Return(true)));
701
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800702 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700703 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700704 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700705
706 g_idle_add(&StaticQuitMainLoop, this);
707}
708
709
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700710TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
711 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
712 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
713 g_main_loop_run(loop_);
714 g_main_loop_unref(loop_);
715 loop_ = NULL;
716}
717
718// Tests that the scatter_factor_in_seconds value is properly fetched
719// from the device policy.
720void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
721 int64 scatter_factor_in_seconds = 36000;
722
723 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
724 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
725
726 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800727 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700728 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700729
730 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
731 .WillRepeatedly(DoAll(
732 SetArgumentPointee<0>(scatter_factor_in_seconds),
733 Return(true)));
734
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800735 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700736 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
737
738 g_idle_add(&StaticQuitMainLoop, this);
739}
740
741TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
742 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
743 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
744 g_main_loop_run(loop_);
745 g_main_loop_unref(loop_);
746 loop_ = NULL;
747}
748
749void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
750 // Tests that the scatter_factor_in_seconds value is properly fetched
751 // from the device policy and is decremented if value > 0.
752 int64 initial_value = 5;
753 Prefs prefs;
754 attempter_.prefs_ = &prefs;
755
Jay Srinivasan08fce042012-06-07 16:31:01 -0700756 EXPECT_CALL(mock_system_state_,
757 IsOOBEComplete()).WillRepeatedly(Return(true));
758
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700759 string prefs_dir;
760 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
761 &prefs_dir));
762 ScopedDirRemover temp_dir_remover(prefs_dir);
763
764 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
765 << "Failed to initialize preferences.";
766 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
767
768 int64 scatter_factor_in_seconds = 10;
769
770 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
771 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
772
773 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800774 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700775 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700776
777 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
778 .WillRepeatedly(DoAll(
779 SetArgumentPointee<0>(scatter_factor_in_seconds),
780 Return(true)));
781
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800782 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700783 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
784
785 // Make sure the file still exists.
786 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
787
788 int64 new_value;
789 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
790 EXPECT_EQ(initial_value - 1, new_value);
791
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700792 EXPECT_TRUE(
793 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700794
795 // However, if the count is already 0, it's not decremented. Test that.
796 initial_value = 0;
797 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800798 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700799 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
800 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
801 EXPECT_EQ(initial_value, new_value);
802
803 g_idle_add(&StaticQuitMainLoop, this);
804}
805
Jay Srinivasan08fce042012-06-07 16:31:01 -0700806TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
807 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
808 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
809 g_main_loop_run(loop_);
810 g_main_loop_unref(loop_);
811 loop_ = NULL;
812}
813
814void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
815 // Tests that no scattering logic is enabled if the update check
816 // is manually done (as opposed to a scheduled update check)
817 int64 initial_value = 8;
818 Prefs prefs;
819 attempter_.prefs_ = &prefs;
820
821 EXPECT_CALL(mock_system_state_,
822 IsOOBEComplete()).WillRepeatedly(Return(true));
823
824 string prefs_dir;
825 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
826 &prefs_dir));
827 ScopedDirRemover temp_dir_remover(prefs_dir);
828
829 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
830 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700831 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700832 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
833
834 // make sure scatter_factor is non-zero as scattering is disabled
835 // otherwise.
836 int64 scatter_factor_in_seconds = 50;
837
838 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
839 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
840
841 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800842 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700843 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700844
845 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
846 .WillRepeatedly(DoAll(
847 SetArgumentPointee<0>(scatter_factor_in_seconds),
848 Return(true)));
849
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800850 // Trigger an interactive check so we can test that scattering is disabled.
851 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -0700852 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
853
854 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -0700855 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700856 EXPECT_FALSE(
857 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700858 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700859 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
860 EXPECT_FALSE(
861 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700862 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
863
864 g_idle_add(&StaticQuitMainLoop, this);
865}
866
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700867} // namespace chromeos_update_engine