blob: 11464d4e902caa12bf31da31d46d262b04fb461b [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
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700314TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
315 extern const char* UpdateStatusToString(UpdateStatus);
316 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
317 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
318 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
319 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
320 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
321 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
322 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
323 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
324 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
325 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
326 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
327 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
328 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
329 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
330 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
331 EXPECT_STREQ("unknown status",
332 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
333}
334
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200335void UpdateAttempterTest::QuitMainLoop() {
336 g_main_loop_quit(loop_);
337}
338
339gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
340 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
341 return FALSE;
342}
343
Darin Petkove6ef2f82011-03-07 17:31:11 -0800344gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
345 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
346 return FALSE;
347}
348
349gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
350 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
351 return FALSE;
352}
353
Chris Sosa76a29ae2013-07-11 17:59:24 -0700354gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700355 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700356 return FALSE;
357}
358
359gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700360 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(true, false);
361 return FALSE;
362}
363
364gboolean UpdateAttempterTest::StaticStableChannelRollbackTestStart(
365 gpointer data) {
366 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
367 false, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700368 return FALSE;
369}
370
371gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
372 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
373 return FALSE;
374}
375
Thieu Le116fda32011-04-19 11:01:54 -0700376gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
377 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
378 return FALSE;
379}
380
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700381gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200382 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700383 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
384 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700385 return FALSE;
386}
387
Jay Srinivasan0a708742012-03-20 11:26:12 -0700388gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
389 gpointer data) {
390 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
391 ua_test->ReadUpdateDisabledFromPolicyTestStart();
392 return FALSE;
393}
394
395gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
396 gpointer data) {
397 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
398 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
399 return FALSE;
400}
401
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700402gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
403 gpointer data) {
404 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
405 ua_test->ReadScatterFactorFromPolicyTestStart();
406 return FALSE;
407}
408
409gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
410 gpointer data) {
411 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
412 ua_test->DecrementUpdateCheckCountTestStart();
413 return FALSE;
414}
415
Jay Srinivasan08fce042012-06-07 16:31:01 -0700416gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
417 gpointer data) {
418 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
419 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
420 return FALSE;
421}
422
Darin Petkove6ef2f82011-03-07 17:31:11 -0800423namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700424// Actions that will be built as part of an update check.
425const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800426 OmahaRequestAction::StaticType(),
427 OmahaResponseHandlerAction::StaticType(),
428 FilesystemCopierAction::StaticType(),
429 FilesystemCopierAction::StaticType(),
430 OmahaRequestAction::StaticType(),
431 DownloadAction::StaticType(),
432 OmahaRequestAction::StaticType(),
433 FilesystemCopierAction::StaticType(),
434 FilesystemCopierAction::StaticType(),
435 PostinstallRunnerAction::StaticType(),
436 OmahaRequestAction::StaticType()
437};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700438
439// Actions that will be built as part of a user-initiated rollback.
440const string kRollbackActionTypes[] = {
441 InstallPlanAction::StaticType(),
442 PostinstallRunnerAction::StaticType(),
443};
444
Darin Petkove6ef2f82011-03-07 17:31:11 -0800445} // namespace {}
446
447void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700448 attempter_.set_http_response_code(200);
449 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700450 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700451 EXPECT_CALL(*processor_,
452 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700453 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700454 }
455 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
456
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800457 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800458 g_idle_add(&StaticUpdateTestVerify, this);
459}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700460
Darin Petkove6ef2f82011-03-07 17:31:11 -0800461void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700462 EXPECT_EQ(0, attempter_.http_response_code());
463 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700464 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
465 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
466 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700467 }
468 EXPECT_EQ(attempter_.response_handler_action_.get(),
469 attempter_.actions_[1].get());
470 DownloadAction* download_action =
471 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
472 ASSERT_TRUE(download_action != NULL);
473 EXPECT_EQ(&attempter_, download_action->delegate());
474 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800475 g_main_loop_quit(loop_);
476}
477
Chris Sosa28e479c2013-07-12 11:39:53 -0700478void UpdateAttempterTest::RollbackTestStart(
479 bool enterprise_rollback, bool stable_channel) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700480 // Create a device policy so that we can change settings.
481 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
482 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
483
484 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
485 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
486 Return(device_policy));
487
488 string install_path = "/dev/sda3";
Chris Sosa28e479c2013-07-12 11:39:53 -0700489 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700490
Chris Sosa28e479c2013-07-12 11:39:53 -0700491 // We only allow rollback on devices that are neither enterprise enrolled or
492 // not on the stable channel.
493 if (!(enterprise_rollback || stable_channel)) {
494 is_rollback_allowed = true;
495 }
496
497 // Set up the policy for the test given our args.
498 if (stable_channel) {
499 attempter_.omaha_request_params_->set_current_channel(
500 string("stable-channel"));
501 } else if (enterprise_rollback) {
502 // We return an empty owner as this is an enterprise.
503 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
504 DoAll(SetArgumentPointee<0>(std::string("")),
505 Return(true)));
506 } else {
507 // We return a fake owner as this is an owned consumer device.
Chris Sosa76a29ae2013-07-11 17:59:24 -0700508 EXPECT_CALL(*device_policy, GetOwner(_)).WillOnce(
509 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
Chris Sosa28e479c2013-07-12 11:39:53 -0700563TEST_F(UpdateAttempterTest, StableChannelRollbackTest) {
564 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
565 g_idle_add(&StaticStableChannelRollbackTestStart, this);
566 g_main_loop_run(loop_);
567 g_main_loop_unref(loop_);
568 loop_ = NULL;
569}
570
Chris Sosa76a29ae2013-07-11 17:59:24 -0700571TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
572 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
573 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
574 g_main_loop_run(loop_);
575 g_main_loop_unref(loop_);
576 loop_ = NULL;
577}
578
Thieu Le116fda32011-04-19 11:01:54 -0700579void UpdateAttempterTest::PingOmahaTestStart() {
580 EXPECT_CALL(*processor_,
581 EnqueueAction(Property(&AbstractAction::Type,
582 OmahaRequestAction::StaticType())))
583 .Times(1);
584 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
585 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200586 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700587}
588
589TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800590 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700591 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700592 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700593 attempter_.set_update_check_scheduler(&scheduler);
594 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
595 g_idle_add(&StaticPingOmahaTestStart, this);
596 g_main_loop_run(loop_);
597 g_main_loop_unref(loop_);
598 loop_ = NULL;
599 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
600 EXPECT_EQ(true, scheduler.scheduled_);
601}
602
Darin Petkov18c7bce2011-06-16 14:07:00 -0700603TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
604 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700605 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700606 attempter_.CreatePendingErrorEvent(&action, kCode);
607 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
608 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
609 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700610 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800611 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700612}
613
614TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
615 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800616 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700617 response_action->install_plan_.is_resume = true;
618 attempter_.response_handler_action_.reset(response_action);
619 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700620 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700621 attempter_.CreatePendingErrorEvent(&action, kCode);
622 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
623 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
624 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700625 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700626 attempter_.error_event_->error_code);
627}
628
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700629TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200630 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700631 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200632 g_main_loop_run(loop_);
633 g_main_loop_unref(loop_);
634 loop_ = NULL;
635}
636
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700637void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
638 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200639 // from the device policy.
640
641 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
642 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
643
644 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700645 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
646 Return(device_policy));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200647
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700648 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
649 DoAll(SetArgumentPointee<0>(bool(false)),
650 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200651
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700652 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
653 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
654 Return(true)));
655
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700656 ASSERT_FALSE(test_dir_.empty());
657 attempter_.omaha_request_params_->set_root(test_dir_);
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800658 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700659 EXPECT_EQ("beta-channel",
660 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200661
662 g_idle_add(&StaticQuitMainLoop, this);
663}
664
Jay Srinivasan0a708742012-03-20 11:26:12 -0700665TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
666 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
667 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
668 g_main_loop_run(loop_);
669 g_main_loop_unref(loop_);
670 loop_ = NULL;
671}
672
673void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
674 // Tests that the update_disbled flag is properly fetched
675 // from the device policy.
676
677 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
678 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
679
680 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700681 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
682 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700683
684 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
685 .WillRepeatedly(DoAll(
686 SetArgumentPointee<0>(true),
687 Return(true)));
688
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800689 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700690 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700691
692 g_idle_add(&StaticQuitMainLoop, this);
693}
694
David Zeuthen8f191b22013-08-06 12:27:50 -0700695TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
696 MockP2PManager mock_p2p_manager;
697 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
698 mock_p2p_manager.fake().SetP2PEnabled(false);
699 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
700 attempter_.UpdateEngineStarted();
701}
702
703TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
704 MockP2PManager mock_p2p_manager;
705 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
706 mock_p2p_manager.fake().SetP2PEnabled(true);
707 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
708 attempter_.UpdateEngineStarted();
709}
710
711TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
712 MockP2PManager mock_p2p_manager;
713 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
714 mock_p2p_manager.fake().SetP2PEnabled(true);
715 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
716 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
717 attempter_.UpdateEngineStarted();
718}
719
720TEST_F(UpdateAttempterTest, P2PNotEnabled) {
721 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
722 g_idle_add(&StaticP2PNotEnabled, this);
723 g_main_loop_run(loop_);
724 g_main_loop_unref(loop_);
725 loop_ = NULL;
726}
727gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
728 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
729 ua_test->P2PNotEnabledStart();
730 return FALSE;
731}
732void UpdateAttempterTest::P2PNotEnabledStart() {
733 // If P2P is not enabled, check that we do not attempt housekeeping
734 // and do not convey that p2p is to be used.
735 MockP2PManager mock_p2p_manager;
736 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
737 mock_p2p_manager.fake().SetP2PEnabled(false);
738 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
739 attempter_.Update("", "", false, false, false);
740 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
741 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
742 g_idle_add(&StaticQuitMainLoop, this);
743}
744
745TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
746 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
747 g_idle_add(&StaticP2PEnabledStartingFails, this);
748 g_main_loop_run(loop_);
749 g_main_loop_unref(loop_);
750 loop_ = NULL;
751}
752gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
753 gpointer data) {
754 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
755 ua_test->P2PEnabledStartingFailsStart();
756 return FALSE;
757}
758void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
759 // If p2p is enabled, but starting it fails ensure we don't do
760 // any housekeeping and do not convey that p2p should be used.
761 MockP2PManager mock_p2p_manager;
762 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
763 mock_p2p_manager.fake().SetP2PEnabled(true);
764 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
765 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
766 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
767 attempter_.Update("", "", false, false, false);
768 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
769 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
770 g_idle_add(&StaticQuitMainLoop, this);
771}
772
773TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
774 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
775 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
776 g_main_loop_run(loop_);
777 g_main_loop_unref(loop_);
778 loop_ = NULL;
779}
780gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
781 gpointer data) {
782 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
783 ua_test->P2PEnabledHousekeepingFailsStart();
784 return FALSE;
785}
786void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
787 // If p2p is enabled, starting it works but housekeeping fails, ensure
788 // we do not convey p2p is to be used.
789 MockP2PManager mock_p2p_manager;
790 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
791 mock_p2p_manager.fake().SetP2PEnabled(true);
792 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
793 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
794 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
795 attempter_.Update("", "", false, false, false);
796 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
797 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
798 g_idle_add(&StaticQuitMainLoop, this);
799}
800
801TEST_F(UpdateAttempterTest, P2PEnabled) {
802 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
803 g_idle_add(&StaticP2PEnabled, this);
804 g_main_loop_run(loop_);
805 g_main_loop_unref(loop_);
806 loop_ = NULL;
807}
808gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
809 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
810 ua_test->P2PEnabledStart();
811 return FALSE;
812}
813void UpdateAttempterTest::P2PEnabledStart() {
814 MockP2PManager mock_p2p_manager;
815 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
816 // If P2P is enabled and starting it works, check that we performed
817 // housekeeping and that we convey p2p should be used.
818 mock_p2p_manager.fake().SetP2PEnabled(true);
819 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
820 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
821 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
822 attempter_.Update("", "", false, false, false);
823 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
824 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
825 g_idle_add(&StaticQuitMainLoop, this);
826}
827
828TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
829 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
830 g_idle_add(&StaticP2PEnabledInteractive, this);
831 g_main_loop_run(loop_);
832 g_main_loop_unref(loop_);
833 loop_ = NULL;
834}
835gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
836 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
837 ua_test->P2PEnabledInteractiveStart();
838 return FALSE;
839}
840void UpdateAttempterTest::P2PEnabledInteractiveStart() {
841 MockP2PManager mock_p2p_manager;
842 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
843 // For an interactive check, if P2P is enabled and starting it
844 // works, check that we performed housekeeping and that we convey
845 // p2p should be used for sharing but NOT for downloading.
846 mock_p2p_manager.fake().SetP2PEnabled(true);
847 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
848 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
849 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
850 attempter_.Update("", "", false, true /* interactive */, false);
851 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
852 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
853 g_idle_add(&StaticQuitMainLoop, this);
854}
855
Jay Srinivasan0a708742012-03-20 11:26:12 -0700856TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
857 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
858 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
859 g_main_loop_run(loop_);
860 g_main_loop_unref(loop_);
861 loop_ = NULL;
862}
863
864void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
865 // Tests that the target_version_prefix value is properly fetched
866 // from the device policy.
867
868 const std::string target_version_prefix = "1412.";
869
870 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
871 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
872
873 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700874 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
875 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700876
877 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
878 .WillRepeatedly(DoAll(
879 SetArgumentPointee<0>(target_version_prefix),
880 Return(true)));
881
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800882 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700883 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700884 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700885
886 g_idle_add(&StaticQuitMainLoop, this);
887}
888
889
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700890TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
891 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
892 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
893 g_main_loop_run(loop_);
894 g_main_loop_unref(loop_);
895 loop_ = NULL;
896}
897
898// Tests that the scatter_factor_in_seconds value is properly fetched
899// from the device policy.
900void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
901 int64 scatter_factor_in_seconds = 36000;
902
903 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
904 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
905
906 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800907 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700908 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700909
910 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
911 .WillRepeatedly(DoAll(
912 SetArgumentPointee<0>(scatter_factor_in_seconds),
913 Return(true)));
914
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800915 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700916 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
917
918 g_idle_add(&StaticQuitMainLoop, this);
919}
920
921TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
922 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
923 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
924 g_main_loop_run(loop_);
925 g_main_loop_unref(loop_);
926 loop_ = NULL;
927}
928
929void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
930 // Tests that the scatter_factor_in_seconds value is properly fetched
931 // from the device policy and is decremented if value > 0.
932 int64 initial_value = 5;
933 Prefs prefs;
934 attempter_.prefs_ = &prefs;
935
Jay Srinivasan08fce042012-06-07 16:31:01 -0700936 EXPECT_CALL(mock_system_state_,
937 IsOOBEComplete()).WillRepeatedly(Return(true));
938
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700939 string prefs_dir;
940 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
941 &prefs_dir));
942 ScopedDirRemover temp_dir_remover(prefs_dir);
943
944 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
945 << "Failed to initialize preferences.";
946 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
947
948 int64 scatter_factor_in_seconds = 10;
949
950 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
951 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
952
953 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800954 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700955 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700956
957 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
958 .WillRepeatedly(DoAll(
959 SetArgumentPointee<0>(scatter_factor_in_seconds),
960 Return(true)));
961
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800962 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700963 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
964
965 // Make sure the file still exists.
966 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
967
968 int64 new_value;
969 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
970 EXPECT_EQ(initial_value - 1, new_value);
971
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700972 EXPECT_TRUE(
973 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700974
975 // However, if the count is already 0, it's not decremented. Test that.
976 initial_value = 0;
977 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800978 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700979 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
980 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
981 EXPECT_EQ(initial_value, new_value);
982
983 g_idle_add(&StaticQuitMainLoop, this);
984}
985
Jay Srinivasan08fce042012-06-07 16:31:01 -0700986TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
987 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
988 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
989 g_main_loop_run(loop_);
990 g_main_loop_unref(loop_);
991 loop_ = NULL;
992}
993
994void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
995 // Tests that no scattering logic is enabled if the update check
996 // is manually done (as opposed to a scheduled update check)
997 int64 initial_value = 8;
998 Prefs prefs;
999 attempter_.prefs_ = &prefs;
1000
1001 EXPECT_CALL(mock_system_state_,
1002 IsOOBEComplete()).WillRepeatedly(Return(true));
1003
1004 string prefs_dir;
1005 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/ue_ut_prefs.XXXXXX",
1006 &prefs_dir));
1007 ScopedDirRemover temp_dir_remover(prefs_dir);
1008
1009 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
1010 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -07001011 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001012 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
1013
1014 // make sure scatter_factor is non-zero as scattering is disabled
1015 // otherwise.
1016 int64 scatter_factor_in_seconds = 50;
1017
1018 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
1019 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
1020
1021 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001022 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -07001023 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001024
1025 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
1026 .WillRepeatedly(DoAll(
1027 SetArgumentPointee<0>(scatter_factor_in_seconds),
1028 Return(true)));
1029
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001030 // Trigger an interactive check so we can test that scattering is disabled.
1031 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001032 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1033
1034 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001035 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001036 EXPECT_FALSE(
1037 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001038 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001039 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1040 EXPECT_FALSE(
1041 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001042 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
1043
1044 g_idle_add(&StaticQuitMainLoop, this);
1045}
1046
David Zeuthen985b1122013-10-09 12:13:15 -07001047// Checks that we only report daily metrics at most every 24 hours.
1048TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1049 FakeClock fake_clock;
1050 Prefs prefs;
1051 string temp_dir;
1052
1053 // We need persistent preferences for this test
1054 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/UpdateCheckScheduler.XXXXXX",
1055 &temp_dir));
1056 prefs.Init(FilePath(temp_dir));
1057 mock_system_state_.set_clock(&fake_clock);
1058 mock_system_state_.set_prefs(&prefs);
1059
1060 Time epoch = Time::FromInternalValue(0);
1061 fake_clock.SetWallclockTime(epoch);
1062
1063 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1064 // we should report.
1065 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1066 // We should not report again if no time has passed.
1067 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1068
1069 // We should not report if only 10 hours has passed.
1070 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1071 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1072
1073 // We should not report if only 24 hours - 1 sec has passed.
1074 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1075 TimeDelta::FromSeconds(1));
1076 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1077
1078 // We should report if 24 hours has passed.
1079 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1080 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1081
1082 // But then we should not report again..
1083 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1084
1085 // .. until another 24 hours has passed
1086 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1087 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1088 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1089 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1090 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1091
1092 // .. and another 24 hours
1093 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1094 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1095 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1096 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1097 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1098
1099 // If the span between time of reporting and present time is
1100 // negative, we report. This is in order to reset the timestamp and
1101 // avoid an edge condition whereby a distant point in the future is
1102 // in the state variable resulting in us never ever reporting again.
1103 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1104 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1105 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1106
1107 // In this case we should not update until the clock reads 71 + 24 = 95.
1108 // Check that.
1109 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1110 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1111 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1112 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1113 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1114
1115 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1116}
1117
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001118} // namespace chromeos_update_engine