blob: 10af01e6b68fe1a4fa33d2be9a72df10094e7f6d [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovf42cc1c2010-09-01 09:03:02 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <base/file_util.h>
6#include <gtest/gtest.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +02007#include <policy/libpolicy.h>
8#include <policy/mock_device_policy.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -07009
10#include "update_engine/action_mock.h"
11#include "update_engine/action_processor_mock.h"
David Zeuthen985b1122013-10-09 12:13:15 -070012#include "update_engine/fake_clock.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070013#include "update_engine/filesystem_copier_action.h"
Chris Sosa76a29ae2013-07-11 17:59:24 -070014#include "update_engine/install_plan.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080015#include "update_engine/mock_dbus_interface.h"
Darin Petkov1b003102010-11-30 10:18:36 -080016#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070017#include "update_engine/mock_p2p_manager.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080018#include "update_engine/mock_payload_state.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070019#include "update_engine/mock_system_state.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070020#include "update_engine/postinstall_runner_action.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070021#include "update_engine/prefs.h"
Darin Petkov36275772010-10-01 11:40:57 -070022#include "update_engine/prefs_mock.h"
Darin Petkov1b003102010-11-30 10:18:36 -080023#include "update_engine/test_utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024#include "update_engine/update_attempter.h"
Darin Petkov1b003102010-11-30 10:18:36 -080025#include "update_engine/update_check_scheduler.h"
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070026#include "update_engine/utils.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070027
David Zeuthen985b1122013-10-09 12:13:15 -070028using base::Time;
29using base::TimeDelta;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070031using testing::_;
32using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070034using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080035using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070036using testing::Property;
37using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070038using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070039
40namespace chromeos_update_engine {
41
42// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070043// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070044// methods.
45class UpdateAttempterUnderTest : public UpdateAttempter {
46 public:
Gilad Arnold70e476e2013-07-30 16:01:13 -070047 // We always feed an explicit update completed marker name; however, unless
48 // explicitly specified, we feed an empty string, which causes the
49 // UpdateAttempter class to ignore / not write the marker file.
50 UpdateAttempterUnderTest(MockSystemState* mock_system_state,
51 MockDbusGlib* dbus)
52 : UpdateAttempter(mock_system_state, dbus, "") {}
53
54 UpdateAttempterUnderTest(MockSystemState* mock_system_state,
55 MockDbusGlib* dbus,
56 const string& update_completed_marker)
57 : UpdateAttempter(mock_system_state, dbus, update_completed_marker) {}
Darin Petkovf42cc1c2010-09-01 09:03:02 -070058};
59
60class UpdateAttempterTest : public ::testing::Test {
61 protected:
Jay Srinivasan43488792012-06-19 00:25:31 -070062 UpdateAttempterTest()
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080063 : attempter_(&mock_system_state_, &dbus_),
Jay Srinivasan43488792012-06-19 00:25:31 -070064 mock_connection_manager(&mock_system_state_),
65 loop_(NULL) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080066 mock_system_state_.set_connection_manager(&mock_connection_manager);
Jay Srinivasan43488792012-06-19 00:25:31 -070067 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070068
Darin Petkovf42cc1c2010-09-01 09:03:02 -070069 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070070 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
71
Darin Petkovf42cc1c2010-09-01 09:03:02 -070072 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080073 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070074 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
75 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080076 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
77 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070078 EXPECT_FALSE(attempter_.download_active_);
79 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
80 EXPECT_EQ(0.0, attempter_.download_progress_);
81 EXPECT_EQ(0, attempter_.last_checked_time_);
82 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070083 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080084 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070085 attempter_.processor_.reset(processor_); // Transfers ownership.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080086 prefs_ = mock_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070087 }
88
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070089 virtual void TearDown() {
90 utils::RecursiveUnlinkDir(test_dir_);
91 }
92
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020093 void QuitMainLoop();
94 static gboolean StaticQuitMainLoop(gpointer data);
95
Darin Petkove6ef2f82011-03-07 17:31:11 -080096 void UpdateTestStart();
97 void UpdateTestVerify();
Chris Sosa28e479c2013-07-12 11:39:53 -070098 void RollbackTestStart(bool enterprise_rollback, bool stable_channel);
Chris Sosa76a29ae2013-07-11 17:59:24 -070099 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800100 static gboolean StaticUpdateTestStart(gpointer data);
101 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700102 static gboolean StaticRollbackTestStart(gpointer data);
103 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
Chris Sosa28e479c2013-07-12 11:39:53 -0700104 static gboolean StaticStableChannelRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700105 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200106
Thieu Le116fda32011-04-19 11:01:54 -0700107 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700108 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200109
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700110 void ReadChannelFromPolicyTestStart();
111 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800112
Jay Srinivasan0a708742012-03-20 11:26:12 -0700113 void ReadUpdateDisabledFromPolicyTestStart();
114 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
115
116 void ReadTargetVersionPrefixFromPolicyTestStart();
117 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
118 gpointer data);
119
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700120 void ReadScatterFactorFromPolicyTestStart();
121 static gboolean StaticReadScatterFactorFromPolicyTestStart(
122 gpointer data);
123
124 void DecrementUpdateCheckCountTestStart();
125 static gboolean StaticDecrementUpdateCheckCountTestStart(
126 gpointer data);
127
Jay Srinivasan08fce042012-06-07 16:31:01 -0700128 void NoScatteringDoneDuringManualUpdateTestStart();
129 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
130 gpointer data);
131
David Zeuthen8f191b22013-08-06 12:27:50 -0700132 void P2PNotEnabledStart();
133 static gboolean StaticP2PNotEnabled(gpointer data);
134
135 void P2PEnabledStart();
136 static gboolean StaticP2PEnabled(gpointer data);
137
138 void P2PEnabledInteractiveStart();
139 static gboolean StaticP2PEnabledInteractive(gpointer data);
140
141 void P2PEnabledStartingFailsStart();
142 static gboolean StaticP2PEnabledStartingFails(gpointer data);
143
144 void P2PEnabledHousekeepingFailsStart();
145 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
146
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800147 NiceMock<MockSystemState> mock_system_state_;
148 NiceMock<MockDbusGlib> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700149 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800150 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800151 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800152 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800153 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700154
155 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700156};
157
Darin Petkov1b003102010-11-30 10:18:36 -0800158TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
159 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
160 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800161 DownloadAction action(prefs_, NULL, fetcher.release());
162 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700163 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800164 EXPECT_EQ(503, attempter_.http_response_code());
165 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
166 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
167}
168
169TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
170 ActionMock action;
171 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
172 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800173 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800174 .WillOnce(Return(false));
David Zeuthena99981f2013-04-29 13:42:47 -0700175 attempter_.ActionCompleted(NULL, &action, kErrorCodeError);
Darin Petkov1b003102010-11-30 10:18:36 -0800176 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
177}
178
179TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
180 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
181 fetcher->FailTransfer(500); // Sets the HTTP response code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700182 OmahaRequestAction action(&mock_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800183 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800184 ObjectCollectorAction<OmahaResponse> collector_action;
185 BondActions(&action, &collector_action);
186 OmahaResponse response;
187 response.poll_interval = 234;
188 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800189 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800190 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800191 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700192 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800193 EXPECT_EQ(500, attempter_.http_response_code());
194 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
195 EXPECT_EQ(234, scheduler.poll_interval());
196 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
197}
198
Darin Petkovcd1666f2010-09-23 09:53:44 -0700199TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700200 string test_update_completed_marker;
201 CHECK(utils::MakeTempFile(
202 "/tmp/update_attempter_unittest-update_completed_marker-XXXXXX",
203 &test_update_completed_marker, NULL));
204 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
205 const FilePath marker(test_update_completed_marker);
206 EXPECT_EQ(0, file_util::WriteFile(marker, "", 0));
207 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
208 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700209 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700210}
211
212TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700213 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
214 ErrorCode code);
215 EXPECT_EQ(kErrorCodeSuccess,
216 GetErrorCodeForAction(NULL, kErrorCodeSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700217
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800218 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700219 OmahaRequestAction omaha_request_action(&mock_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800220 NULL, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700221 EXPECT_EQ(kErrorCodeOmahaRequestError,
222 GetErrorCodeForAction(&omaha_request_action, kErrorCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800223 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
David Zeuthena99981f2013-04-29 13:42:47 -0700224 EXPECT_EQ(kErrorCodeOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700225 GetErrorCodeForAction(&omaha_response_handler_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700226 kErrorCodeError));
Alex Deymo42432912013-07-12 20:21:15 -0700227 FilesystemCopierAction filesystem_copier_action(
228 &mock_system_state_, false, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700229 EXPECT_EQ(kErrorCodeFilesystemCopierError,
230 GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800231 PostinstallRunnerAction postinstall_runner_action;
David Zeuthena99981f2013-04-29 13:42:47 -0700232 EXPECT_EQ(kErrorCodePostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700233 GetErrorCodeForAction(&postinstall_runner_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700234 kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700235 ActionMock action_mock;
236 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
David Zeuthena99981f2013-04-29 13:42:47 -0700237 EXPECT_EQ(kErrorCodeError,
238 GetErrorCodeForAction(&action_mock, kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700239}
240
Darin Petkov36275772010-10-01 11:40:57 -0700241TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700242 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800243 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700244 .WillOnce(Return(false));
245 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700246 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800247 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700248 .WillOnce(DoAll(
249 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
250 Return(true)));
251 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700252 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800253 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700254 .WillOnce(DoAll(
255 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
256 Return(true)));
257 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700258 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800259 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700260 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700261 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700262}
263
264TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800265 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700266 .WillOnce(Return(false))
267 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
268 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
269 .WillOnce(DoAll(
270 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
271 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800272 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700273 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800274 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
275 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
276 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700277 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
278 .Times(1);
279 for (int i = 0; i < 4; i ++)
280 attempter_.MarkDeltaUpdateFailure();
281}
282
Darin Petkov1b003102010-11-30 10:18:36 -0800283TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
284 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
285 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800286 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
287 .Times(0);
288 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700289 string url1 = "http://url1";
290 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800291 response.payload_urls.push_back("https://url");
Jay Srinivasan53173b92013-05-17 17:13:01 -0700292 EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl())
293 .WillRepeatedly(Return(url1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800294 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800295 attempter_.ScheduleErrorEventAction();
Jay Srinivasan53173b92013-05-17 17:13:01 -0700296 EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800297}
298
299TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
300 EXPECT_CALL(*processor_,
301 EnqueueAction(Property(&AbstractAction::Type,
302 OmahaRequestAction::StaticType())))
303 .Times(1);
304 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
David Zeuthena99981f2013-04-29 13:42:47 -0700305 ErrorCode err = kErrorCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800306 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800307 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
308 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800309 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800310 attempter_.ScheduleErrorEventAction();
311 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
312}
313
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200314void UpdateAttempterTest::QuitMainLoop() {
315 g_main_loop_quit(loop_);
316}
317
318gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
319 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
320 return FALSE;
321}
322
Darin Petkove6ef2f82011-03-07 17:31:11 -0800323gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
324 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
325 return FALSE;
326}
327
328gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
329 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
330 return FALSE;
331}
332
Chris Sosa76a29ae2013-07-11 17:59:24 -0700333gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700334 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700335 return FALSE;
336}
337
338gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700339 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(true, false);
340 return FALSE;
341}
342
343gboolean UpdateAttempterTest::StaticStableChannelRollbackTestStart(
344 gpointer data) {
345 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
346 false, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700347 return FALSE;
348}
349
350gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
351 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
352 return FALSE;
353}
354
Thieu Le116fda32011-04-19 11:01:54 -0700355gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
356 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
357 return FALSE;
358}
359
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700360gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200361 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700362 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
363 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700364 return FALSE;
365}
366
Jay Srinivasan0a708742012-03-20 11:26:12 -0700367gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
368 gpointer data) {
369 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
370 ua_test->ReadUpdateDisabledFromPolicyTestStart();
371 return FALSE;
372}
373
374gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
375 gpointer data) {
376 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
377 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
378 return FALSE;
379}
380
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700381gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
382 gpointer data) {
383 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
384 ua_test->ReadScatterFactorFromPolicyTestStart();
385 return FALSE;
386}
387
388gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
389 gpointer data) {
390 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
391 ua_test->DecrementUpdateCheckCountTestStart();
392 return FALSE;
393}
394
Jay Srinivasan08fce042012-06-07 16:31:01 -0700395gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
396 gpointer data) {
397 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
398 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
399 return FALSE;
400}
401
Darin Petkove6ef2f82011-03-07 17:31:11 -0800402namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700403// Actions that will be built as part of an update check.
404const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800405 OmahaRequestAction::StaticType(),
406 OmahaResponseHandlerAction::StaticType(),
407 FilesystemCopierAction::StaticType(),
408 FilesystemCopierAction::StaticType(),
409 OmahaRequestAction::StaticType(),
410 DownloadAction::StaticType(),
411 OmahaRequestAction::StaticType(),
412 FilesystemCopierAction::StaticType(),
413 FilesystemCopierAction::StaticType(),
414 PostinstallRunnerAction::StaticType(),
415 OmahaRequestAction::StaticType()
416};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700417
418// Actions that will be built as part of a user-initiated rollback.
419const string kRollbackActionTypes[] = {
420 InstallPlanAction::StaticType(),
421 PostinstallRunnerAction::StaticType(),
422};
423
Darin Petkove6ef2f82011-03-07 17:31:11 -0800424} // namespace {}
425
426void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700427 attempter_.set_http_response_code(200);
428 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700429 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700430 EXPECT_CALL(*processor_,
431 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700432 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700433 }
434 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
435
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800436 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800437 g_idle_add(&StaticUpdateTestVerify, this);
438}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700439
Darin Petkove6ef2f82011-03-07 17:31:11 -0800440void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700441 EXPECT_EQ(0, attempter_.http_response_code());
442 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700443 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
444 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
445 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700446 }
447 EXPECT_EQ(attempter_.response_handler_action_.get(),
448 attempter_.actions_[1].get());
449 DownloadAction* download_action =
450 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
451 ASSERT_TRUE(download_action != NULL);
452 EXPECT_EQ(&attempter_, download_action->delegate());
453 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800454 g_main_loop_quit(loop_);
455}
456
Chris Sosa28e479c2013-07-12 11:39:53 -0700457void UpdateAttempterTest::RollbackTestStart(
458 bool enterprise_rollback, bool stable_channel) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700459 // Create a device policy so that we can change settings.
460 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
461 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
462
463 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
464 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
465 Return(device_policy));
466
467 string install_path = "/dev/sda3";
Chris Sosa28e479c2013-07-12 11:39:53 -0700468 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700469
Chris Sosa28e479c2013-07-12 11:39:53 -0700470 // We only allow rollback on devices that are neither enterprise enrolled or
471 // not on the stable channel.
472 if (!(enterprise_rollback || stable_channel)) {
473 is_rollback_allowed = true;
474 }
475
476 // Set up the policy for the test given our args.
477 if (stable_channel) {
478 attempter_.omaha_request_params_->set_current_channel(
479 string("stable-channel"));
480 } else if (enterprise_rollback) {
481 // We return an empty owner as this is an enterprise.
482 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
483 DoAll(SetArgumentPointee<0>(std::string("")),
484 Return(true)));
485 } else {
486 // We return a fake owner as this is an owned consumer device.
Chris Sosa76a29ae2013-07-11 17:59:24 -0700487 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
488 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
489 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700490 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700491
Chris Sosa28e479c2013-07-12 11:39:53 -0700492 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700493 InSequence s;
494 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
495 EXPECT_CALL(*processor_,
496 EnqueueAction(Property(&AbstractAction::Type,
497 kRollbackActionTypes[i]))).Times(1);
498 }
499 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
500
501 EXPECT_TRUE(attempter_.Rollback(true, &install_path));
502 g_idle_add(&StaticRollbackTestVerify, this);
503 } else {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700504 EXPECT_FALSE(attempter_.Rollback(true, &install_path));
505 g_main_loop_quit(loop_);
506 }
507}
508
509void UpdateAttempterTest::RollbackTestVerify() {
510 // Verifies the actions that were enqueued.
511 EXPECT_EQ(&attempter_, processor_->delegate());
512 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
513 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
514 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
515 }
516 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
517 InstallPlanAction* install_plan_action =
518 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
519 InstallPlan* install_plan = install_plan_action->install_plan();
520 EXPECT_EQ(install_plan->install_path, string("/dev/sda3"));
521 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sda2"));
522 EXPECT_EQ(install_plan->powerwash_required, true);
523 g_main_loop_quit(loop_);
524}
525
Darin Petkove6ef2f82011-03-07 17:31:11 -0800526TEST_F(UpdateAttempterTest, UpdateTest) {
527 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
528 g_idle_add(&StaticUpdateTestStart, this);
529 g_main_loop_run(loop_);
530 g_main_loop_unref(loop_);
531 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700532}
533
Chris Sosa76a29ae2013-07-11 17:59:24 -0700534TEST_F(UpdateAttempterTest, RollbackTest) {
535 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
536 g_idle_add(&StaticRollbackTestStart, this);
537 g_main_loop_run(loop_);
538 g_main_loop_unref(loop_);
539 loop_ = NULL;
540}
541
Chris Sosa28e479c2013-07-12 11:39:53 -0700542TEST_F(UpdateAttempterTest, StableChannelRollbackTest) {
543 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
544 g_idle_add(&StaticStableChannelRollbackTestStart, this);
545 g_main_loop_run(loop_);
546 g_main_loop_unref(loop_);
547 loop_ = NULL;
548}
549
Chris Sosa76a29ae2013-07-11 17:59:24 -0700550TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
551 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
552 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
553 g_main_loop_run(loop_);
554 g_main_loop_unref(loop_);
555 loop_ = NULL;
556}
557
Thieu Le116fda32011-04-19 11:01:54 -0700558void UpdateAttempterTest::PingOmahaTestStart() {
559 EXPECT_CALL(*processor_,
560 EnqueueAction(Property(&AbstractAction::Type,
561 OmahaRequestAction::StaticType())))
562 .Times(1);
563 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
564 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200565 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700566}
567
568TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800569 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700570 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700571 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700572 attempter_.set_update_check_scheduler(&scheduler);
573 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
574 g_idle_add(&StaticPingOmahaTestStart, this);
575 g_main_loop_run(loop_);
576 g_main_loop_unref(loop_);
577 loop_ = NULL;
578 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
579 EXPECT_EQ(true, scheduler.scheduled_);
580}
581
Darin Petkov18c7bce2011-06-16 14:07:00 -0700582TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
583 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700584 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700585 attempter_.CreatePendingErrorEvent(&action, kCode);
586 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
587 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
588 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700589 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800590 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700591}
592
593TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
594 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800595 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700596 response_action->install_plan_.is_resume = true;
597 attempter_.response_handler_action_.reset(response_action);
598 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700599 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700600 attempter_.CreatePendingErrorEvent(&action, kCode);
601 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
602 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
603 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700604 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700605 attempter_.error_event_->error_code);
606}
607
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700608TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200609 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700610 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200611 g_main_loop_run(loop_);
612 g_main_loop_unref(loop_);
613 loop_ = NULL;
614}
615
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700616void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
617 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200618 // from the device policy.
619
620 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
621 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
622
623 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700624 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
625 Return(device_policy));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200626
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700627 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
628 DoAll(SetArgumentPointee<0>(bool(false)),
629 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200630
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700631 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
632 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
633 Return(true)));
634
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700635 ASSERT_FALSE(test_dir_.empty());
636 attempter_.omaha_request_params_->set_root(test_dir_);
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800637 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700638 EXPECT_EQ("beta-channel",
639 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200640
641 g_idle_add(&StaticQuitMainLoop, this);
642}
643
Jay Srinivasan0a708742012-03-20 11:26:12 -0700644TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
645 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
646 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
647 g_main_loop_run(loop_);
648 g_main_loop_unref(loop_);
649 loop_ = NULL;
650}
651
652void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
653 // Tests that the update_disbled flag is properly fetched
654 // from the device policy.
655
656 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
657 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
658
659 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700660 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
661 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700662
663 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
664 .WillRepeatedly(DoAll(
665 SetArgumentPointee<0>(true),
666 Return(true)));
667
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800668 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700669 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700670
671 g_idle_add(&StaticQuitMainLoop, this);
672}
673
David Zeuthen8f191b22013-08-06 12:27:50 -0700674TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
675 MockP2PManager mock_p2p_manager;
676 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
677 mock_p2p_manager.fake().SetP2PEnabled(false);
678 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
679 attempter_.UpdateEngineStarted();
680}
681
682TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
683 MockP2PManager mock_p2p_manager;
684 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
685 mock_p2p_manager.fake().SetP2PEnabled(true);
686 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
687 attempter_.UpdateEngineStarted();
688}
689
690TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
691 MockP2PManager mock_p2p_manager;
692 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
693 mock_p2p_manager.fake().SetP2PEnabled(true);
694 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
695 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
696 attempter_.UpdateEngineStarted();
697}
698
699TEST_F(UpdateAttempterTest, P2PNotEnabled) {
700 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
701 g_idle_add(&StaticP2PNotEnabled, this);
702 g_main_loop_run(loop_);
703 g_main_loop_unref(loop_);
704 loop_ = NULL;
705}
706gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
707 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
708 ua_test->P2PNotEnabledStart();
709 return FALSE;
710}
711void UpdateAttempterTest::P2PNotEnabledStart() {
712 // If P2P is not enabled, check that we do not attempt housekeeping
713 // and do not convey that p2p is to be used.
714 MockP2PManager mock_p2p_manager;
715 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
716 mock_p2p_manager.fake().SetP2PEnabled(false);
717 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
718 attempter_.Update("", "", false, false, false);
719 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
720 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
721 g_idle_add(&StaticQuitMainLoop, this);
722}
723
724TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
725 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
726 g_idle_add(&StaticP2PEnabledStartingFails, this);
727 g_main_loop_run(loop_);
728 g_main_loop_unref(loop_);
729 loop_ = NULL;
730}
731gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
732 gpointer data) {
733 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
734 ua_test->P2PEnabledStartingFailsStart();
735 return FALSE;
736}
737void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
738 // If p2p is enabled, but starting it fails ensure we don't do
739 // any housekeeping and do not convey that p2p should be used.
740 MockP2PManager mock_p2p_manager;
741 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
742 mock_p2p_manager.fake().SetP2PEnabled(true);
743 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
744 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
745 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
746 attempter_.Update("", "", false, false, false);
747 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
748 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
749 g_idle_add(&StaticQuitMainLoop, this);
750}
751
752TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
753 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
754 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
755 g_main_loop_run(loop_);
756 g_main_loop_unref(loop_);
757 loop_ = NULL;
758}
759gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
760 gpointer data) {
761 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
762 ua_test->P2PEnabledHousekeepingFailsStart();
763 return FALSE;
764}
765void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
766 // If p2p is enabled, starting it works but housekeeping fails, ensure
767 // we do not convey p2p is to be used.
768 MockP2PManager mock_p2p_manager;
769 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
770 mock_p2p_manager.fake().SetP2PEnabled(true);
771 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
772 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
773 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
774 attempter_.Update("", "", false, false, false);
775 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
776 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
777 g_idle_add(&StaticQuitMainLoop, this);
778}
779
780TEST_F(UpdateAttempterTest, P2PEnabled) {
781 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
782 g_idle_add(&StaticP2PEnabled, this);
783 g_main_loop_run(loop_);
784 g_main_loop_unref(loop_);
785 loop_ = NULL;
786}
787gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
788 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
789 ua_test->P2PEnabledStart();
790 return FALSE;
791}
792void UpdateAttempterTest::P2PEnabledStart() {
793 MockP2PManager mock_p2p_manager;
794 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
795 // If P2P is enabled and starting it works, check that we performed
796 // housekeeping and that we convey p2p should be used.
797 mock_p2p_manager.fake().SetP2PEnabled(true);
798 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
799 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
800 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
801 attempter_.Update("", "", false, false, false);
802 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
803 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
804 g_idle_add(&StaticQuitMainLoop, this);
805}
806
807TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
808 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
809 g_idle_add(&StaticP2PEnabledInteractive, this);
810 g_main_loop_run(loop_);
811 g_main_loop_unref(loop_);
812 loop_ = NULL;
813}
814gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
815 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
816 ua_test->P2PEnabledInteractiveStart();
817 return FALSE;
818}
819void UpdateAttempterTest::P2PEnabledInteractiveStart() {
820 MockP2PManager mock_p2p_manager;
821 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
822 // For an interactive check, if P2P is enabled and starting it
823 // works, check that we performed housekeeping and that we convey
824 // p2p should be used for sharing but NOT for downloading.
825 mock_p2p_manager.fake().SetP2PEnabled(true);
826 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
827 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
828 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
829 attempter_.Update("", "", false, true /* interactive */, false);
830 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
831 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
832 g_idle_add(&StaticQuitMainLoop, this);
833}
834
Jay Srinivasan0a708742012-03-20 11:26:12 -0700835TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
836 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
837 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
838 g_main_loop_run(loop_);
839 g_main_loop_unref(loop_);
840 loop_ = NULL;
841}
842
843void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
844 // Tests that the target_version_prefix value is properly fetched
845 // from the device policy.
846
847 const std::string target_version_prefix = "1412.";
848
849 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
850 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
851
852 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700853 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
854 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700855
856 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
857 .WillRepeatedly(DoAll(
858 SetArgumentPointee<0>(target_version_prefix),
859 Return(true)));
860
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800861 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700862 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700863 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700864
865 g_idle_add(&StaticQuitMainLoop, this);
866}
867
868
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700869TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
870 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
871 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
872 g_main_loop_run(loop_);
873 g_main_loop_unref(loop_);
874 loop_ = NULL;
875}
876
877// Tests that the scatter_factor_in_seconds value is properly fetched
878// from the device policy.
879void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
880 int64 scatter_factor_in_seconds = 36000;
881
882 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
883 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
884
885 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800886 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700887 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700888
889 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
890 .WillRepeatedly(DoAll(
891 SetArgumentPointee<0>(scatter_factor_in_seconds),
892 Return(true)));
893
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800894 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700895 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
896
897 g_idle_add(&StaticQuitMainLoop, this);
898}
899
900TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
901 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
902 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
903 g_main_loop_run(loop_);
904 g_main_loop_unref(loop_);
905 loop_ = NULL;
906}
907
908void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
909 // Tests that the scatter_factor_in_seconds value is properly fetched
910 // from the device policy and is decremented if value > 0.
911 int64 initial_value = 5;
912 Prefs prefs;
913 attempter_.prefs_ = &prefs;
914
Jay Srinivasan08fce042012-06-07 16:31:01 -0700915 EXPECT_CALL(mock_system_state_,
916 IsOOBEComplete()).WillRepeatedly(Return(true));
917
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700918 string prefs_dir;
919 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
920 &prefs_dir));
921 ScopedDirRemover temp_dir_remover(prefs_dir);
922
923 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
924 << "Failed to initialize preferences.";
925 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
926
927 int64 scatter_factor_in_seconds = 10;
928
929 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
930 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
931
932 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800933 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700934 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700935
936 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
937 .WillRepeatedly(DoAll(
938 SetArgumentPointee<0>(scatter_factor_in_seconds),
939 Return(true)));
940
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800941 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700942 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
943
944 // Make sure the file still exists.
945 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
946
947 int64 new_value;
948 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
949 EXPECT_EQ(initial_value - 1, new_value);
950
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700951 EXPECT_TRUE(
952 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700953
954 // However, if the count is already 0, it's not decremented. Test that.
955 initial_value = 0;
956 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800957 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700958 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
959 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
960 EXPECT_EQ(initial_value, new_value);
961
962 g_idle_add(&StaticQuitMainLoop, this);
963}
964
Jay Srinivasan08fce042012-06-07 16:31:01 -0700965TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
966 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
967 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
968 g_main_loop_run(loop_);
969 g_main_loop_unref(loop_);
970 loop_ = NULL;
971}
972
973void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
974 // Tests that no scattering logic is enabled if the update check
975 // is manually done (as opposed to a scheduled update check)
976 int64 initial_value = 8;
977 Prefs prefs;
978 attempter_.prefs_ = &prefs;
979
980 EXPECT_CALL(mock_system_state_,
981 IsOOBEComplete()).WillRepeatedly(Return(true));
982
983 string prefs_dir;
984 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
985 &prefs_dir));
986 ScopedDirRemover temp_dir_remover(prefs_dir);
987
988 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
989 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700990 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700991 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
992
993 // make sure scatter_factor is non-zero as scattering is disabled
994 // otherwise.
995 int64 scatter_factor_in_seconds = 50;
996
997 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
998 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
999
1000 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001001 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -07001002 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001003
1004 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
1005 .WillRepeatedly(DoAll(
1006 SetArgumentPointee<0>(scatter_factor_in_seconds),
1007 Return(true)));
1008
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001009 // Trigger an interactive check so we can test that scattering is disabled.
1010 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001011 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1012
1013 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001014 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001015 EXPECT_FALSE(
1016 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001017 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001018 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1019 EXPECT_FALSE(
1020 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001021 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
1022
1023 g_idle_add(&StaticQuitMainLoop, this);
1024}
1025
David Zeuthen985b1122013-10-09 12:13:15 -07001026// Checks that we only report daily metrics at most every 24 hours.
1027TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1028 FakeClock fake_clock;
1029 Prefs prefs;
1030 string temp_dir;
1031
1032 // We need persistent preferences for this test
1033 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/UpdateCheckScheduler.XXXXXX",
1034 &temp_dir));
1035 prefs.Init(FilePath(temp_dir));
1036 mock_system_state_.set_clock(&fake_clock);
1037 mock_system_state_.set_prefs(&prefs);
1038
1039 Time epoch = Time::FromInternalValue(0);
1040 fake_clock.SetWallclockTime(epoch);
1041
1042 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1043 // we should report.
1044 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1045 // We should not report again if no time has passed.
1046 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1047
1048 // We should not report if only 10 hours has passed.
1049 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1050 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1051
1052 // We should not report if only 24 hours - 1 sec has passed.
1053 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1054 TimeDelta::FromSeconds(1));
1055 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1056
1057 // We should report if 24 hours has passed.
1058 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1059 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1060
1061 // But then we should not report again..
1062 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1063
1064 // .. until another 24 hours has passed
1065 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1066 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1067 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1068 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1069 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1070
1071 // .. and another 24 hours
1072 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1073 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1074 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1075 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1076 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1077
1078 // If the span between time of reporting and present time is
1079 // negative, we report. This is in order to reset the timestamp and
1080 // avoid an edge condition whereby a distant point in the future is
1081 // in the state variable resulting in us never ever reporting again.
1082 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1083 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1084 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1085
1086 // In this case we should not update until the clock reads 71 + 24 = 95.
1087 // Check that.
1088 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1089 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1090 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1091 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1092 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1093
1094 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1095}
1096
David Zeuthen3c55abd2013-10-14 12:48:03 -07001097TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1098 const string update_completed_marker = test_dir_ + "/update-completed-marker";
1099 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
1100 update_completed_marker);
1101
1102 FakeClock fake_clock;
1103 fake_clock.SetBootTime(Time::FromTimeT(42));
1104 mock_system_state_.set_clock(&fake_clock);
1105
1106 Time boot_time;
1107 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1108
1109 attempter.WriteUpdateCompletedMarker();
1110
1111 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1112 EXPECT_EQ(boot_time.ToTimeT(), 42);
1113}
1114
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001115} // namespace chromeos_update_engine