blob: ac01ee3e58d102adc5e552a2de62e7a1e80037c0 [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();
Don Garrett6646b442013-11-13 15:29:11 -080098 void RollbackTestStart(bool enterprise_rollback,
99 bool stable_channel,
100 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700101 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800102 static gboolean StaticUpdateTestStart(gpointer data);
103 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700104 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800105 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700106 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
Chris Sosa28e479c2013-07-12 11:39:53 -0700107 static gboolean StaticStableChannelRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700108 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200109
Thieu Le116fda32011-04-19 11:01:54 -0700110 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700111 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200112
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113 void ReadChannelFromPolicyTestStart();
114 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800115
Jay Srinivasan0a708742012-03-20 11:26:12 -0700116 void ReadUpdateDisabledFromPolicyTestStart();
117 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
118
119 void ReadTargetVersionPrefixFromPolicyTestStart();
120 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
121 gpointer data);
122
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700123 void ReadScatterFactorFromPolicyTestStart();
124 static gboolean StaticReadScatterFactorFromPolicyTestStart(
125 gpointer data);
126
127 void DecrementUpdateCheckCountTestStart();
128 static gboolean StaticDecrementUpdateCheckCountTestStart(
129 gpointer data);
130
Jay Srinivasan08fce042012-06-07 16:31:01 -0700131 void NoScatteringDoneDuringManualUpdateTestStart();
132 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
133 gpointer data);
134
David Zeuthen8f191b22013-08-06 12:27:50 -0700135 void P2PNotEnabledStart();
136 static gboolean StaticP2PNotEnabled(gpointer data);
137
138 void P2PEnabledStart();
139 static gboolean StaticP2PEnabled(gpointer data);
140
141 void P2PEnabledInteractiveStart();
142 static gboolean StaticP2PEnabledInteractive(gpointer data);
143
144 void P2PEnabledStartingFailsStart();
145 static gboolean StaticP2PEnabledStartingFails(gpointer data);
146
147 void P2PEnabledHousekeepingFailsStart();
148 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
149
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800150 NiceMock<MockSystemState> mock_system_state_;
151 NiceMock<MockDbusGlib> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700152 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800153 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800154 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800155 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800156 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700157
158 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700159};
160
Darin Petkov1b003102010-11-30 10:18:36 -0800161TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
162 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
163 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800164 DownloadAction action(prefs_, NULL, fetcher.release());
165 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700166 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800167 EXPECT_EQ(503, attempter_.http_response_code());
168 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
169 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
170}
171
172TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
173 ActionMock action;
174 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
175 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800176 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800177 .WillOnce(Return(false));
David Zeuthena99981f2013-04-29 13:42:47 -0700178 attempter_.ActionCompleted(NULL, &action, kErrorCodeError);
Darin Petkov1b003102010-11-30 10:18:36 -0800179 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
180}
181
182TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
183 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
184 fetcher->FailTransfer(500); // Sets the HTTP response code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700185 OmahaRequestAction action(&mock_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800186 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800187 ObjectCollectorAction<OmahaResponse> collector_action;
188 BondActions(&action, &collector_action);
189 OmahaResponse response;
190 response.poll_interval = 234;
191 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800192 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800193 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800194 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700195 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800196 EXPECT_EQ(500, attempter_.http_response_code());
197 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
198 EXPECT_EQ(234, scheduler.poll_interval());
199 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
200}
201
Darin Petkovcd1666f2010-09-23 09:53:44 -0700202TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700203 string test_update_completed_marker;
204 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800205 "update_attempter_unittest-update_completed_marker-XXXXXX",
Gilad Arnold70e476e2013-07-30 16:01:13 -0700206 &test_update_completed_marker, NULL));
207 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
208 const FilePath marker(test_update_completed_marker);
209 EXPECT_EQ(0, file_util::WriteFile(marker, "", 0));
210 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
211 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700212 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700213}
214
215TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700216 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
217 ErrorCode code);
218 EXPECT_EQ(kErrorCodeSuccess,
219 GetErrorCodeForAction(NULL, kErrorCodeSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700220
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800221 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700222 OmahaRequestAction omaha_request_action(&mock_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800223 NULL, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700224 EXPECT_EQ(kErrorCodeOmahaRequestError,
225 GetErrorCodeForAction(&omaha_request_action, kErrorCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800226 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
David Zeuthena99981f2013-04-29 13:42:47 -0700227 EXPECT_EQ(kErrorCodeOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700228 GetErrorCodeForAction(&omaha_response_handler_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700229 kErrorCodeError));
Alex Deymo42432912013-07-12 20:21:15 -0700230 FilesystemCopierAction filesystem_copier_action(
231 &mock_system_state_, false, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700232 EXPECT_EQ(kErrorCodeFilesystemCopierError,
233 GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800234 PostinstallRunnerAction postinstall_runner_action;
David Zeuthena99981f2013-04-29 13:42:47 -0700235 EXPECT_EQ(kErrorCodePostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700236 GetErrorCodeForAction(&postinstall_runner_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700237 kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700238 ActionMock action_mock;
239 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
David Zeuthena99981f2013-04-29 13:42:47 -0700240 EXPECT_EQ(kErrorCodeError,
241 GetErrorCodeForAction(&action_mock, kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700242}
243
Darin Petkov36275772010-10-01 11:40:57 -0700244TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700245 attempter_.omaha_request_params_->set_delta_okay(true);
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 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700249 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800250 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700251 .WillOnce(DoAll(
252 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
253 Return(true)));
254 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700255 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800256 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700257 .WillOnce(DoAll(
258 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
259 Return(true)));
260 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700261 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800262 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700263 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700264 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700265}
266
267TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800268 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700269 .WillOnce(Return(false))
270 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
271 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
272 .WillOnce(DoAll(
273 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
274 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800275 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700276 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800277 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
278 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
279 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700280 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
281 .Times(1);
282 for (int i = 0; i < 4; i ++)
283 attempter_.MarkDeltaUpdateFailure();
284}
285
Darin Petkov1b003102010-11-30 10:18:36 -0800286TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
287 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
288 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800289 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
290 .Times(0);
291 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700292 string url1 = "http://url1";
293 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800294 response.payload_urls.push_back("https://url");
Jay Srinivasan53173b92013-05-17 17:13:01 -0700295 EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl())
296 .WillRepeatedly(Return(url1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800297 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800298 attempter_.ScheduleErrorEventAction();
Jay Srinivasan53173b92013-05-17 17:13:01 -0700299 EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800300}
301
302TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
303 EXPECT_CALL(*processor_,
304 EnqueueAction(Property(&AbstractAction::Type,
305 OmahaRequestAction::StaticType())))
306 .Times(1);
307 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
David Zeuthena99981f2013-04-29 13:42:47 -0700308 ErrorCode err = kErrorCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800309 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800310 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
311 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800312 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800313 attempter_.ScheduleErrorEventAction();
314 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
315}
316
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200317void UpdateAttempterTest::QuitMainLoop() {
318 g_main_loop_quit(loop_);
319}
320
321gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
322 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
323 return FALSE;
324}
325
Darin Petkove6ef2f82011-03-07 17:31:11 -0800326gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
327 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
328 return FALSE;
329}
330
331gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
332 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
333 return FALSE;
334}
335
Chris Sosa76a29ae2013-07-11 17:59:24 -0700336gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800337 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
338 false, false, true);
339 return FALSE;
340}
341
342gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
343 gpointer data) {
344 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
345 false, false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700346 return FALSE;
347}
348
349gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800350 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
351 true, false, true);
Chris Sosa28e479c2013-07-12 11:39:53 -0700352 return FALSE;
353}
354
355gboolean UpdateAttempterTest::StaticStableChannelRollbackTestStart(
356 gpointer data) {
357 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Don Garrett6646b442013-11-13 15:29:11 -0800358 false, true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700359 return FALSE;
360}
361
362gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
363 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
364 return FALSE;
365}
366
Thieu Le116fda32011-04-19 11:01:54 -0700367gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
368 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
369 return FALSE;
370}
371
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700372gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200373 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700374 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
375 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700376 return FALSE;
377}
378
Jay Srinivasan0a708742012-03-20 11:26:12 -0700379gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
380 gpointer data) {
381 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
382 ua_test->ReadUpdateDisabledFromPolicyTestStart();
383 return FALSE;
384}
385
386gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
387 gpointer data) {
388 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
389 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
390 return FALSE;
391}
392
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700393gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
394 gpointer data) {
395 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
396 ua_test->ReadScatterFactorFromPolicyTestStart();
397 return FALSE;
398}
399
400gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
401 gpointer data) {
402 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
403 ua_test->DecrementUpdateCheckCountTestStart();
404 return FALSE;
405}
406
Jay Srinivasan08fce042012-06-07 16:31:01 -0700407gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
408 gpointer data) {
409 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
410 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
411 return FALSE;
412}
413
Darin Petkove6ef2f82011-03-07 17:31:11 -0800414namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700415// Actions that will be built as part of an update check.
416const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800417 OmahaRequestAction::StaticType(),
418 OmahaResponseHandlerAction::StaticType(),
419 FilesystemCopierAction::StaticType(),
420 FilesystemCopierAction::StaticType(),
421 OmahaRequestAction::StaticType(),
422 DownloadAction::StaticType(),
423 OmahaRequestAction::StaticType(),
424 FilesystemCopierAction::StaticType(),
425 FilesystemCopierAction::StaticType(),
426 PostinstallRunnerAction::StaticType(),
427 OmahaRequestAction::StaticType()
428};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700429
430// Actions that will be built as part of a user-initiated rollback.
431const string kRollbackActionTypes[] = {
432 InstallPlanAction::StaticType(),
433 PostinstallRunnerAction::StaticType(),
434};
435
Darin Petkove6ef2f82011-03-07 17:31:11 -0800436} // namespace {}
437
438void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700439 attempter_.set_http_response_code(200);
440 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700441 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700442 EXPECT_CALL(*processor_,
443 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700444 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700445 }
446 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
447
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800448 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800449 g_idle_add(&StaticUpdateTestVerify, this);
450}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700451
Darin Petkove6ef2f82011-03-07 17:31:11 -0800452void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700453 EXPECT_EQ(0, attempter_.http_response_code());
454 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700455 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
456 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
457 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700458 }
459 EXPECT_EQ(attempter_.response_handler_action_.get(),
460 attempter_.actions_[1].get());
461 DownloadAction* download_action =
462 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
463 ASSERT_TRUE(download_action != NULL);
464 EXPECT_EQ(&attempter_, download_action->delegate());
465 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800466 g_main_loop_quit(loop_);
467}
468
Chris Sosa28e479c2013-07-12 11:39:53 -0700469void UpdateAttempterTest::RollbackTestStart(
Don Garrett6646b442013-11-13 15:29:11 -0800470 bool enterprise_rollback, bool stable_channel, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700471 // Create a device policy so that we can change settings.
472 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
473 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
474
475 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
476 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
477 Return(device_policy));
478
Don Garrett6646b442013-11-13 15:29:11 -0800479 if (!valid_slot) {
480 string rollback_kernel = "/dev/sda2";
481 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
482 mock_system_state_.get_fake_hardware()->MarkKernelUnbootable(
483 rollback_kernel);
484 }
485
Chris Sosa76a29ae2013-07-11 17:59:24 -0700486 string install_path = "/dev/sda3";
Chris Sosa28e479c2013-07-12 11:39:53 -0700487 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700488
Chris Sosa28e479c2013-07-12 11:39:53 -0700489 // We only allow rollback on devices that are neither enterprise enrolled or
Don Garrett6646b442013-11-13 15:29:11 -0800490 // not on the stable channel, and which have a valid slot to rollback too.
491 if (!(enterprise_rollback || stable_channel) && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700492 is_rollback_allowed = true;
493 }
494
495 // Set up the policy for the test given our args.
496 if (stable_channel) {
497 attempter_.omaha_request_params_->set_current_channel(
498 string("stable-channel"));
Don Garrett6646b442013-11-13 15:29:11 -0800499 }
500
501 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700502 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800503 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa28e479c2013-07-12 11:39:53 -0700504 DoAll(SetArgumentPointee<0>(std::string("")),
505 Return(true)));
506 } else {
507 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800508 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa76a29ae2013-07-11 17:59:24 -0700509 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
510 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700511 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700512
Chris Sosa28e479c2013-07-12 11:39:53 -0700513 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700514 InSequence s;
515 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
516 EXPECT_CALL(*processor_,
517 EnqueueAction(Property(&AbstractAction::Type,
518 kRollbackActionTypes[i]))).Times(1);
519 }
520 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
521
522 EXPECT_TRUE(attempter_.Rollback(true, &install_path));
523 g_idle_add(&StaticRollbackTestVerify, this);
524 } else {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700525 EXPECT_FALSE(attempter_.Rollback(true, &install_path));
526 g_main_loop_quit(loop_);
527 }
528}
529
530void UpdateAttempterTest::RollbackTestVerify() {
531 // Verifies the actions that were enqueued.
532 EXPECT_EQ(&attempter_, processor_->delegate());
533 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
534 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
535 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
536 }
537 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
538 InstallPlanAction* install_plan_action =
539 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
540 InstallPlan* install_plan = install_plan_action->install_plan();
541 EXPECT_EQ(install_plan->install_path, string("/dev/sda3"));
542 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sda2"));
543 EXPECT_EQ(install_plan->powerwash_required, true);
544 g_main_loop_quit(loop_);
545}
546
Darin Petkove6ef2f82011-03-07 17:31:11 -0800547TEST_F(UpdateAttempterTest, UpdateTest) {
548 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
549 g_idle_add(&StaticUpdateTestStart, this);
550 g_main_loop_run(loop_);
551 g_main_loop_unref(loop_);
552 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700553}
554
Chris Sosa76a29ae2013-07-11 17:59:24 -0700555TEST_F(UpdateAttempterTest, RollbackTest) {
556 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
557 g_idle_add(&StaticRollbackTestStart, this);
558 g_main_loop_run(loop_);
559 g_main_loop_unref(loop_);
560 loop_ = NULL;
561}
562
Don Garrett6646b442013-11-13 15:29:11 -0800563TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
564 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
565 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
566 g_main_loop_run(loop_);
567 g_main_loop_unref(loop_);
568 loop_ = NULL;
569}
570
Chris Sosa28e479c2013-07-12 11:39:53 -0700571TEST_F(UpdateAttempterTest, StableChannelRollbackTest) {
572 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
573 g_idle_add(&StaticStableChannelRollbackTestStart, this);
574 g_main_loop_run(loop_);
575 g_main_loop_unref(loop_);
576 loop_ = NULL;
577}
578
Chris Sosa76a29ae2013-07-11 17:59:24 -0700579TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
580 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
581 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
582 g_main_loop_run(loop_);
583 g_main_loop_unref(loop_);
584 loop_ = NULL;
585}
586
Thieu Le116fda32011-04-19 11:01:54 -0700587void UpdateAttempterTest::PingOmahaTestStart() {
588 EXPECT_CALL(*processor_,
589 EnqueueAction(Property(&AbstractAction::Type,
590 OmahaRequestAction::StaticType())))
591 .Times(1);
592 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
593 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200594 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700595}
596
597TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800598 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700599 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700600 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700601 attempter_.set_update_check_scheduler(&scheduler);
602 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
603 g_idle_add(&StaticPingOmahaTestStart, this);
604 g_main_loop_run(loop_);
605 g_main_loop_unref(loop_);
606 loop_ = NULL;
607 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
608 EXPECT_EQ(true, scheduler.scheduled_);
609}
610
Darin Petkov18c7bce2011-06-16 14:07:00 -0700611TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
612 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700613 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700614 attempter_.CreatePendingErrorEvent(&action, kCode);
615 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
616 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
617 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700618 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800619 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700620}
621
622TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
623 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800624 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700625 response_action->install_plan_.is_resume = true;
626 attempter_.response_handler_action_.reset(response_action);
627 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700628 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700629 attempter_.CreatePendingErrorEvent(&action, kCode);
630 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
631 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
632 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700633 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700634 attempter_.error_event_->error_code);
635}
636
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700637TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200638 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700639 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200640 g_main_loop_run(loop_);
641 g_main_loop_unref(loop_);
642 loop_ = NULL;
643}
644
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700645void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
646 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200647 // from the device policy.
648
649 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
650 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
651
652 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700653 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
654 Return(device_policy));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200655
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700656 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
657 DoAll(SetArgumentPointee<0>(bool(false)),
658 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200659
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700660 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
661 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
662 Return(true)));
663
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700664 ASSERT_FALSE(test_dir_.empty());
665 attempter_.omaha_request_params_->set_root(test_dir_);
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800666 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700667 EXPECT_EQ("beta-channel",
668 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200669
670 g_idle_add(&StaticQuitMainLoop, this);
671}
672
Jay Srinivasan0a708742012-03-20 11:26:12 -0700673TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
674 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
675 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
676 g_main_loop_run(loop_);
677 g_main_loop_unref(loop_);
678 loop_ = NULL;
679}
680
681void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
682 // Tests that the update_disbled flag is properly fetched
683 // from the device policy.
684
685 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
686 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
687
688 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700689 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
690 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700691
692 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
693 .WillRepeatedly(DoAll(
694 SetArgumentPointee<0>(true),
695 Return(true)));
696
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800697 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700698 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700699
700 g_idle_add(&StaticQuitMainLoop, this);
701}
702
David Zeuthen8f191b22013-08-06 12:27:50 -0700703TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
704 MockP2PManager mock_p2p_manager;
705 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
706 mock_p2p_manager.fake().SetP2PEnabled(false);
707 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
708 attempter_.UpdateEngineStarted();
709}
710
711TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
712 MockP2PManager mock_p2p_manager;
713 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
714 mock_p2p_manager.fake().SetP2PEnabled(true);
715 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
716 attempter_.UpdateEngineStarted();
717}
718
719TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
720 MockP2PManager mock_p2p_manager;
721 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
722 mock_p2p_manager.fake().SetP2PEnabled(true);
723 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
724 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
725 attempter_.UpdateEngineStarted();
726}
727
728TEST_F(UpdateAttempterTest, P2PNotEnabled) {
729 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
730 g_idle_add(&StaticP2PNotEnabled, this);
731 g_main_loop_run(loop_);
732 g_main_loop_unref(loop_);
733 loop_ = NULL;
734}
735gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
736 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
737 ua_test->P2PNotEnabledStart();
738 return FALSE;
739}
740void UpdateAttempterTest::P2PNotEnabledStart() {
741 // If P2P is not enabled, check that we do not attempt housekeeping
742 // and do not convey that p2p is to be used.
743 MockP2PManager mock_p2p_manager;
744 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
745 mock_p2p_manager.fake().SetP2PEnabled(false);
746 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
747 attempter_.Update("", "", false, false, false);
748 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
749 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
750 g_idle_add(&StaticQuitMainLoop, this);
751}
752
753TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
754 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
755 g_idle_add(&StaticP2PEnabledStartingFails, this);
756 g_main_loop_run(loop_);
757 g_main_loop_unref(loop_);
758 loop_ = NULL;
759}
760gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
761 gpointer data) {
762 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
763 ua_test->P2PEnabledStartingFailsStart();
764 return FALSE;
765}
766void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
767 // If p2p is enabled, but starting it fails ensure we don't do
768 // any housekeeping and do not convey that p2p should be used.
769 MockP2PManager mock_p2p_manager;
770 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
771 mock_p2p_manager.fake().SetP2PEnabled(true);
772 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
773 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
774 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
775 attempter_.Update("", "", false, false, false);
776 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
777 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
778 g_idle_add(&StaticQuitMainLoop, this);
779}
780
781TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
782 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
783 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
784 g_main_loop_run(loop_);
785 g_main_loop_unref(loop_);
786 loop_ = NULL;
787}
788gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
789 gpointer data) {
790 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
791 ua_test->P2PEnabledHousekeepingFailsStart();
792 return FALSE;
793}
794void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
795 // If p2p is enabled, starting it works but housekeeping fails, ensure
796 // we do not convey p2p is to be used.
797 MockP2PManager mock_p2p_manager;
798 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
799 mock_p2p_manager.fake().SetP2PEnabled(true);
800 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
801 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
802 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
803 attempter_.Update("", "", false, false, false);
804 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
805 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
806 g_idle_add(&StaticQuitMainLoop, this);
807}
808
809TEST_F(UpdateAttempterTest, P2PEnabled) {
810 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
811 g_idle_add(&StaticP2PEnabled, this);
812 g_main_loop_run(loop_);
813 g_main_loop_unref(loop_);
814 loop_ = NULL;
815}
816gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
817 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
818 ua_test->P2PEnabledStart();
819 return FALSE;
820}
821void UpdateAttempterTest::P2PEnabledStart() {
822 MockP2PManager mock_p2p_manager;
823 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
824 // If P2P is enabled and starting it works, check that we performed
825 // housekeeping and that we convey p2p should be used.
826 mock_p2p_manager.fake().SetP2PEnabled(true);
827 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
828 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
829 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
830 attempter_.Update("", "", false, false, false);
831 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
832 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
833 g_idle_add(&StaticQuitMainLoop, this);
834}
835
836TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
837 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
838 g_idle_add(&StaticP2PEnabledInteractive, this);
839 g_main_loop_run(loop_);
840 g_main_loop_unref(loop_);
841 loop_ = NULL;
842}
843gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
844 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
845 ua_test->P2PEnabledInteractiveStart();
846 return FALSE;
847}
848void UpdateAttempterTest::P2PEnabledInteractiveStart() {
849 MockP2PManager mock_p2p_manager;
850 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
851 // For an interactive check, if P2P is enabled and starting it
852 // works, check that we performed housekeeping and that we convey
853 // p2p should be used for sharing but NOT for downloading.
854 mock_p2p_manager.fake().SetP2PEnabled(true);
855 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
856 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
857 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
858 attempter_.Update("", "", false, true /* interactive */, false);
859 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
860 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
861 g_idle_add(&StaticQuitMainLoop, this);
862}
863
Jay Srinivasan0a708742012-03-20 11:26:12 -0700864TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
865 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
866 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
867 g_main_loop_run(loop_);
868 g_main_loop_unref(loop_);
869 loop_ = NULL;
870}
871
872void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
873 // Tests that the target_version_prefix value is properly fetched
874 // from the device policy.
875
876 const std::string target_version_prefix = "1412.";
877
878 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
879 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
880
881 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700882 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
883 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700884
885 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
886 .WillRepeatedly(DoAll(
887 SetArgumentPointee<0>(target_version_prefix),
888 Return(true)));
889
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800890 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700891 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700892 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700893
894 g_idle_add(&StaticQuitMainLoop, this);
895}
896
897
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700898TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
899 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
900 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
901 g_main_loop_run(loop_);
902 g_main_loop_unref(loop_);
903 loop_ = NULL;
904}
905
906// Tests that the scatter_factor_in_seconds value is properly fetched
907// from the device policy.
908void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
909 int64 scatter_factor_in_seconds = 36000;
910
911 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
912 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
913
914 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800915 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700916 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700917
918 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
919 .WillRepeatedly(DoAll(
920 SetArgumentPointee<0>(scatter_factor_in_seconds),
921 Return(true)));
922
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800923 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700924 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
925
926 g_idle_add(&StaticQuitMainLoop, this);
927}
928
929TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
930 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
931 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
932 g_main_loop_run(loop_);
933 g_main_loop_unref(loop_);
934 loop_ = NULL;
935}
936
937void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
938 // Tests that the scatter_factor_in_seconds value is properly fetched
939 // from the device policy and is decremented if value > 0.
940 int64 initial_value = 5;
941 Prefs prefs;
942 attempter_.prefs_ = &prefs;
943
Jay Srinivasan08fce042012-06-07 16:31:01 -0700944 EXPECT_CALL(mock_system_state_,
945 IsOOBEComplete()).WillRepeatedly(Return(true));
946
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700947 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800948 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700949 &prefs_dir));
950 ScopedDirRemover temp_dir_remover(prefs_dir);
951
952 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
953 << "Failed to initialize preferences.";
954 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
955
956 int64 scatter_factor_in_seconds = 10;
957
958 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
959 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
960
961 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800962 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700963 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700964
965 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
966 .WillRepeatedly(DoAll(
967 SetArgumentPointee<0>(scatter_factor_in_seconds),
968 Return(true)));
969
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800970 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700971 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
972
973 // Make sure the file still exists.
974 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
975
976 int64 new_value;
977 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
978 EXPECT_EQ(initial_value - 1, new_value);
979
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700980 EXPECT_TRUE(
981 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700982
983 // However, if the count is already 0, it's not decremented. Test that.
984 initial_value = 0;
985 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800986 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700987 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
988 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
989 EXPECT_EQ(initial_value, new_value);
990
991 g_idle_add(&StaticQuitMainLoop, this);
992}
993
Jay Srinivasan08fce042012-06-07 16:31:01 -0700994TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
995 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
996 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
997 g_main_loop_run(loop_);
998 g_main_loop_unref(loop_);
999 loop_ = NULL;
1000}
1001
1002void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
1003 // Tests that no scattering logic is enabled if the update check
1004 // is manually done (as opposed to a scheduled update check)
1005 int64 initial_value = 8;
1006 Prefs prefs;
1007 attempter_.prefs_ = &prefs;
1008
1009 EXPECT_CALL(mock_system_state_,
1010 IsOOBEComplete()).WillRepeatedly(Return(true));
1011
1012 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -08001013 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -07001014 &prefs_dir));
1015 ScopedDirRemover temp_dir_remover(prefs_dir);
1016
1017 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
1018 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -07001019 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001020 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
1021
1022 // make sure scatter_factor is non-zero as scattering is disabled
1023 // otherwise.
1024 int64 scatter_factor_in_seconds = 50;
1025
1026 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
1027 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
1028
1029 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001030 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -07001031 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001032
1033 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
1034 .WillRepeatedly(DoAll(
1035 SetArgumentPointee<0>(scatter_factor_in_seconds),
1036 Return(true)));
1037
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001038 // Trigger an interactive check so we can test that scattering is disabled.
1039 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001040 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1041
1042 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001043 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001044 EXPECT_FALSE(
1045 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001046 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001047 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1048 EXPECT_FALSE(
1049 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001050 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
1051
1052 g_idle_add(&StaticQuitMainLoop, this);
1053}
1054
David Zeuthen985b1122013-10-09 12:13:15 -07001055// Checks that we only report daily metrics at most every 24 hours.
1056TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1057 FakeClock fake_clock;
1058 Prefs prefs;
1059 string temp_dir;
1060
1061 // We need persistent preferences for this test
Gilad Arnolda6742b32014-01-11 00:18:34 -08001062 EXPECT_TRUE(utils::MakeTempDirectory("UpdateCheckScheduler.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -07001063 &temp_dir));
1064 prefs.Init(FilePath(temp_dir));
1065 mock_system_state_.set_clock(&fake_clock);
1066 mock_system_state_.set_prefs(&prefs);
1067
1068 Time epoch = Time::FromInternalValue(0);
1069 fake_clock.SetWallclockTime(epoch);
1070
1071 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1072 // we should report.
1073 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1074 // We should not report again if no time has passed.
1075 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1076
1077 // We should not report if only 10 hours has passed.
1078 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1079 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1080
1081 // We should not report if only 24 hours - 1 sec has passed.
1082 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1083 TimeDelta::FromSeconds(1));
1084 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1085
1086 // We should report if 24 hours has passed.
1087 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1088 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1089
1090 // But then we should not report again..
1091 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1092
1093 // .. until another 24 hours has passed
1094 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1095 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1096 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1097 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1098 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1099
1100 // .. and another 24 hours
1101 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1102 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1103 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1104 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1105 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1106
1107 // If the span between time of reporting and present time is
1108 // negative, we report. This is in order to reset the timestamp and
1109 // avoid an edge condition whereby a distant point in the future is
1110 // in the state variable resulting in us never ever reporting again.
1111 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1112 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1113 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1114
1115 // In this case we should not update until the clock reads 71 + 24 = 95.
1116 // Check that.
1117 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1118 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1119 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1120 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1121 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1122
1123 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1124}
1125
David Zeuthen3c55abd2013-10-14 12:48:03 -07001126TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1127 const string update_completed_marker = test_dir_ + "/update-completed-marker";
1128 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
1129 update_completed_marker);
1130
1131 FakeClock fake_clock;
1132 fake_clock.SetBootTime(Time::FromTimeT(42));
1133 mock_system_state_.set_clock(&fake_clock);
1134
1135 Time boot_time;
1136 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1137
1138 attempter.WriteUpdateCompletedMarker();
1139
1140 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1141 EXPECT_EQ(boot_time.ToTimeT(), 42);
1142}
1143
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001144} // namespace chromeos_update_engine