blob: b623aecac694d3842936a12def1bffd724cf55a6 [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"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080015#include "update_engine/mock_dbus_wrapper.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,
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080051 MockDBusWrapper* dbus)
Gilad Arnold70e476e2013-07-30 16:01:13 -070052 : UpdateAttempter(mock_system_state, dbus, "") {}
53
54 UpdateAttempterUnderTest(MockSystemState* mock_system_state,
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080055 MockDBusWrapper* dbus,
Gilad Arnold70e476e2013-07-30 16:01:13 -070056 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,
Don Garrett6646b442013-11-13 15:29:11 -080099 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700100 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800101 static gboolean StaticUpdateTestStart(gpointer data);
102 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700103 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800104 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700105 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
106 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200107
Thieu Le116fda32011-04-19 11:01:54 -0700108 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700109 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200110
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700111 void ReadChannelFromPolicyTestStart();
112 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800113
Jay Srinivasan0a708742012-03-20 11:26:12 -0700114 void ReadUpdateDisabledFromPolicyTestStart();
115 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
116
117 void ReadTargetVersionPrefixFromPolicyTestStart();
118 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
119 gpointer data);
120
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700121 void ReadScatterFactorFromPolicyTestStart();
122 static gboolean StaticReadScatterFactorFromPolicyTestStart(
123 gpointer data);
124
125 void DecrementUpdateCheckCountTestStart();
126 static gboolean StaticDecrementUpdateCheckCountTestStart(
127 gpointer data);
128
Jay Srinivasan08fce042012-06-07 16:31:01 -0700129 void NoScatteringDoneDuringManualUpdateTestStart();
130 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
131 gpointer data);
132
David Zeuthen8f191b22013-08-06 12:27:50 -0700133 void P2PNotEnabledStart();
134 static gboolean StaticP2PNotEnabled(gpointer data);
135
136 void P2PEnabledStart();
137 static gboolean StaticP2PEnabled(gpointer data);
138
139 void P2PEnabledInteractiveStart();
140 static gboolean StaticP2PEnabledInteractive(gpointer data);
141
142 void P2PEnabledStartingFailsStart();
143 static gboolean StaticP2PEnabledStartingFails(gpointer data);
144
145 void P2PEnabledHousekeepingFailsStart();
146 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
147
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800148 NiceMock<MockSystemState> mock_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800149 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700150 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800151 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800152 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800153 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800154 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700155
156 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700157};
158
Darin Petkov1b003102010-11-30 10:18:36 -0800159TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
160 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
161 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800162 DownloadAction action(prefs_, NULL, fetcher.release());
163 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700164 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800165 EXPECT_EQ(503, attempter_.http_response_code());
166 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
167 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
168}
169
170TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
171 ActionMock action;
172 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
173 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800174 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800175 .WillOnce(Return(false));
David Zeuthena99981f2013-04-29 13:42:47 -0700176 attempter_.ActionCompleted(NULL, &action, kErrorCodeError);
Darin Petkov1b003102010-11-30 10:18:36 -0800177 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
178}
179
180TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
181 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
182 fetcher->FailTransfer(500); // Sets the HTTP response code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700183 OmahaRequestAction action(&mock_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800184 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800185 ObjectCollectorAction<OmahaResponse> collector_action;
186 BondActions(&action, &collector_action);
187 OmahaResponse response;
188 response.poll_interval = 234;
189 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800190 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800191 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800192 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700193 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800194 EXPECT_EQ(500, attempter_.http_response_code());
195 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
196 EXPECT_EQ(234, scheduler.poll_interval());
197 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
198}
199
Darin Petkovcd1666f2010-09-23 09:53:44 -0700200TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700201 string test_update_completed_marker;
202 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800203 "update_attempter_unittest-update_completed_marker-XXXXXX",
Gilad Arnold70e476e2013-07-30 16:01:13 -0700204 &test_update_completed_marker, NULL));
205 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700206 const base::FilePath marker(test_update_completed_marker);
Gilad Arnold70e476e2013-07-30 16:01:13 -0700207 EXPECT_EQ(0, file_util::WriteFile(marker, "", 0));
208 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
209 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700210 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700211}
212
213TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700214 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
215 ErrorCode code);
216 EXPECT_EQ(kErrorCodeSuccess,
217 GetErrorCodeForAction(NULL, kErrorCodeSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700218
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800219 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700220 OmahaRequestAction omaha_request_action(&mock_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800221 NULL, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700222 EXPECT_EQ(kErrorCodeOmahaRequestError,
223 GetErrorCodeForAction(&omaha_request_action, kErrorCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800224 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
David Zeuthena99981f2013-04-29 13:42:47 -0700225 EXPECT_EQ(kErrorCodeOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700226 GetErrorCodeForAction(&omaha_response_handler_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700227 kErrorCodeError));
Alex Deymo42432912013-07-12 20:21:15 -0700228 FilesystemCopierAction filesystem_copier_action(
229 &mock_system_state_, false, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700230 EXPECT_EQ(kErrorCodeFilesystemCopierError,
231 GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800232 PostinstallRunnerAction postinstall_runner_action;
David Zeuthena99981f2013-04-29 13:42:47 -0700233 EXPECT_EQ(kErrorCodePostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700234 GetErrorCodeForAction(&postinstall_runner_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700235 kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700236 ActionMock action_mock;
237 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
David Zeuthena99981f2013-04-29 13:42:47 -0700238 EXPECT_EQ(kErrorCodeError,
239 GetErrorCodeForAction(&action_mock, kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700240}
241
Darin Petkov36275772010-10-01 11:40:57 -0700242TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700243 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800244 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700245 .WillOnce(Return(false));
246 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700247 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800248 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700249 .WillOnce(DoAll(
250 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
251 Return(true)));
252 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700253 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800254 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700255 .WillOnce(DoAll(
256 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
257 Return(true)));
258 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700259 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800260 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700261 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700262 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700263}
264
265TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800266 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700267 .WillOnce(Return(false))
268 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
269 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
270 .WillOnce(DoAll(
271 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
272 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800273 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700274 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800275 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
276 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
277 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700278 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
279 .Times(1);
280 for (int i = 0; i < 4; i ++)
281 attempter_.MarkDeltaUpdateFailure();
282}
283
Darin Petkov1b003102010-11-30 10:18:36 -0800284TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
285 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
286 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800287 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
288 .Times(0);
289 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700290 string url1 = "http://url1";
291 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800292 response.payload_urls.push_back("https://url");
Jay Srinivasan53173b92013-05-17 17:13:01 -0700293 EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl())
294 .WillRepeatedly(Return(url1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800295 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800296 attempter_.ScheduleErrorEventAction();
Jay Srinivasan53173b92013-05-17 17:13:01 -0700297 EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800298}
299
300TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
301 EXPECT_CALL(*processor_,
302 EnqueueAction(Property(&AbstractAction::Type,
303 OmahaRequestAction::StaticType())))
304 .Times(1);
305 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
David Zeuthena99981f2013-04-29 13:42:47 -0700306 ErrorCode err = kErrorCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800307 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800308 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
309 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800310 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800311 attempter_.ScheduleErrorEventAction();
312 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
313}
314
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200315void UpdateAttempterTest::QuitMainLoop() {
316 g_main_loop_quit(loop_);
317}
318
319gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
320 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
321 return FALSE;
322}
323
Darin Petkove6ef2f82011-03-07 17:31:11 -0800324gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
325 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
326 return FALSE;
327}
328
329gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
330 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
331 return FALSE;
332}
333
Chris Sosa76a29ae2013-07-11 17:59:24 -0700334gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800335 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700336 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800337 return FALSE;
338}
339
340gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
341 gpointer data) {
342 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700343 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700344 return FALSE;
345}
346
347gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800348 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700349 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700350 return FALSE;
351}
352
353gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
354 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
355 return FALSE;
356}
357
Thieu Le116fda32011-04-19 11:01:54 -0700358gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
359 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
360 return FALSE;
361}
362
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700363gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200364 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700365 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
366 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700367 return FALSE;
368}
369
Jay Srinivasan0a708742012-03-20 11:26:12 -0700370gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
371 gpointer data) {
372 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
373 ua_test->ReadUpdateDisabledFromPolicyTestStart();
374 return FALSE;
375}
376
377gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
378 gpointer data) {
379 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
380 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
381 return FALSE;
382}
383
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700384gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
385 gpointer data) {
386 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
387 ua_test->ReadScatterFactorFromPolicyTestStart();
388 return FALSE;
389}
390
391gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
392 gpointer data) {
393 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
394 ua_test->DecrementUpdateCheckCountTestStart();
395 return FALSE;
396}
397
Jay Srinivasan08fce042012-06-07 16:31:01 -0700398gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
399 gpointer data) {
400 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
401 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
402 return FALSE;
403}
404
Darin Petkove6ef2f82011-03-07 17:31:11 -0800405namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700406// Actions that will be built as part of an update check.
407const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800408 OmahaRequestAction::StaticType(),
409 OmahaResponseHandlerAction::StaticType(),
410 FilesystemCopierAction::StaticType(),
411 FilesystemCopierAction::StaticType(),
412 OmahaRequestAction::StaticType(),
413 DownloadAction::StaticType(),
414 OmahaRequestAction::StaticType(),
415 FilesystemCopierAction::StaticType(),
416 FilesystemCopierAction::StaticType(),
417 PostinstallRunnerAction::StaticType(),
418 OmahaRequestAction::StaticType()
419};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700420
421// Actions that will be built as part of a user-initiated rollback.
422const string kRollbackActionTypes[] = {
423 InstallPlanAction::StaticType(),
424 PostinstallRunnerAction::StaticType(),
425};
426
Darin Petkove6ef2f82011-03-07 17:31:11 -0800427} // namespace {}
428
429void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700430 attempter_.set_http_response_code(200);
431 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700432 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700433 EXPECT_CALL(*processor_,
434 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700435 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700436 }
437 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
438
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800439 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800440 g_idle_add(&StaticUpdateTestVerify, this);
441}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700442
Darin Petkove6ef2f82011-03-07 17:31:11 -0800443void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700444 EXPECT_EQ(0, attempter_.http_response_code());
445 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700446 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
447 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
448 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700449 }
450 EXPECT_EQ(attempter_.response_handler_action_.get(),
451 attempter_.actions_[1].get());
452 DownloadAction* download_action =
453 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
454 ASSERT_TRUE(download_action != NULL);
455 EXPECT_EQ(&attempter_, download_action->delegate());
456 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800457 g_main_loop_quit(loop_);
458}
459
Chris Sosa28e479c2013-07-12 11:39:53 -0700460void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700461 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700462 // Create a device policy so that we can change settings.
463 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
464 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
465
466 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
467 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
468 Return(device_policy));
469
Don Garrett6646b442013-11-13 15:29:11 -0800470 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700471 // References bootable kernels in fake_hardware.h
472 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800473 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
474 mock_system_state_.get_fake_hardware()->MarkKernelUnbootable(
475 rollback_kernel);
476 }
477
Chris Sosa28e479c2013-07-12 11:39:53 -0700478 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700479
Chris Sosad38b1132014-03-25 10:43:59 -0700480 // We only allow rollback on devices that are not enterprise enrolled and
481 // which have a valid slot to rollback to.
482 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700483 is_rollback_allowed = true;
484 }
485
Don Garrett6646b442013-11-13 15:29:11 -0800486 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700487 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800488 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa28e479c2013-07-12 11:39:53 -0700489 DoAll(SetArgumentPointee<0>(std::string("")),
490 Return(true)));
491 } else {
492 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800493 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa76a29ae2013-07-11 17:59:24 -0700494 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
495 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700496 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700497
Chris Sosa28e479c2013-07-12 11:39:53 -0700498 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700499 InSequence s;
500 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
501 EXPECT_CALL(*processor_,
502 EnqueueAction(Property(&AbstractAction::Type,
503 kRollbackActionTypes[i]))).Times(1);
504 }
505 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
506
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700507 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700508 g_idle_add(&StaticRollbackTestVerify, this);
509 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700510 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700511 g_main_loop_quit(loop_);
512 }
513}
514
515void UpdateAttempterTest::RollbackTestVerify() {
516 // Verifies the actions that were enqueued.
517 EXPECT_EQ(&attempter_, processor_->delegate());
518 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
519 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
520 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
521 }
522 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
523 InstallPlanAction* install_plan_action =
524 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
525 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700526 // Matches fake_hardware.h -> rollback should move from kernel/boot device
527 // pair to other pair.
528 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
529 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700530 EXPECT_EQ(install_plan->powerwash_required, true);
531 g_main_loop_quit(loop_);
532}
533
Darin Petkove6ef2f82011-03-07 17:31:11 -0800534TEST_F(UpdateAttempterTest, UpdateTest) {
535 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
536 g_idle_add(&StaticUpdateTestStart, this);
537 g_main_loop_run(loop_);
538 g_main_loop_unref(loop_);
539 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700540}
541
Chris Sosa76a29ae2013-07-11 17:59:24 -0700542TEST_F(UpdateAttempterTest, RollbackTest) {
543 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
544 g_idle_add(&StaticRollbackTestStart, this);
545 g_main_loop_run(loop_);
546 g_main_loop_unref(loop_);
547 loop_ = NULL;
548}
549
Don Garrett6646b442013-11-13 15:29:11 -0800550TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
551 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
552 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
553 g_main_loop_run(loop_);
554 g_main_loop_unref(loop_);
555 loop_ = NULL;
556}
557
Chris Sosa76a29ae2013-07-11 17:59:24 -0700558TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
559 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
560 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
561 g_main_loop_run(loop_);
562 g_main_loop_unref(loop_);
563 loop_ = NULL;
564}
565
Thieu Le116fda32011-04-19 11:01:54 -0700566void UpdateAttempterTest::PingOmahaTestStart() {
567 EXPECT_CALL(*processor_,
568 EnqueueAction(Property(&AbstractAction::Type,
569 OmahaRequestAction::StaticType())))
570 .Times(1);
571 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
572 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200573 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700574}
575
576TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800577 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700578 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700579 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700580 attempter_.set_update_check_scheduler(&scheduler);
581 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
582 g_idle_add(&StaticPingOmahaTestStart, this);
583 g_main_loop_run(loop_);
584 g_main_loop_unref(loop_);
585 loop_ = NULL;
586 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
587 EXPECT_EQ(true, scheduler.scheduled_);
588}
589
Darin Petkov18c7bce2011-06-16 14:07:00 -0700590TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
591 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700592 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700593 attempter_.CreatePendingErrorEvent(&action, kCode);
594 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
595 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
596 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700597 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800598 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700599}
600
601TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
602 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800603 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700604 response_action->install_plan_.is_resume = true;
605 attempter_.response_handler_action_.reset(response_action);
606 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700607 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700608 attempter_.CreatePendingErrorEvent(&action, kCode);
609 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
610 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
611 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700612 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700613 attempter_.error_event_->error_code);
614}
615
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700616TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200617 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700618 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200619 g_main_loop_run(loop_);
620 g_main_loop_unref(loop_);
621 loop_ = NULL;
622}
623
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700624void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
625 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200626 // from the device policy.
627
628 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
629 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
630
631 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700632 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
633 Return(device_policy));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200634
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700635 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
636 DoAll(SetArgumentPointee<0>(bool(false)),
637 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200638
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700639 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
640 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
641 Return(true)));
642
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700643 ASSERT_FALSE(test_dir_.empty());
644 attempter_.omaha_request_params_->set_root(test_dir_);
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800645 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700646 EXPECT_EQ("beta-channel",
647 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200648
649 g_idle_add(&StaticQuitMainLoop, this);
650}
651
Jay Srinivasan0a708742012-03-20 11:26:12 -0700652TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
653 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
654 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
655 g_main_loop_run(loop_);
656 g_main_loop_unref(loop_);
657 loop_ = NULL;
658}
659
660void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
661 // Tests that the update_disbled flag is properly fetched
662 // from the device policy.
663
664 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
665 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
666
667 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700668 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
669 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700670
671 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
672 .WillRepeatedly(DoAll(
673 SetArgumentPointee<0>(true),
674 Return(true)));
675
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800676 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700677 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700678
679 g_idle_add(&StaticQuitMainLoop, this);
680}
681
David Zeuthen8f191b22013-08-06 12:27:50 -0700682TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
683 MockP2PManager mock_p2p_manager;
684 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
685 mock_p2p_manager.fake().SetP2PEnabled(false);
686 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
687 attempter_.UpdateEngineStarted();
688}
689
690TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
691 MockP2PManager mock_p2p_manager;
692 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
693 mock_p2p_manager.fake().SetP2PEnabled(true);
694 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
695 attempter_.UpdateEngineStarted();
696}
697
698TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
699 MockP2PManager mock_p2p_manager;
700 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
701 mock_p2p_manager.fake().SetP2PEnabled(true);
702 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
703 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
704 attempter_.UpdateEngineStarted();
705}
706
707TEST_F(UpdateAttempterTest, P2PNotEnabled) {
708 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
709 g_idle_add(&StaticP2PNotEnabled, this);
710 g_main_loop_run(loop_);
711 g_main_loop_unref(loop_);
712 loop_ = NULL;
713}
714gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
715 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
716 ua_test->P2PNotEnabledStart();
717 return FALSE;
718}
719void UpdateAttempterTest::P2PNotEnabledStart() {
720 // If P2P is not enabled, check that we do not attempt housekeeping
721 // and do not convey that p2p is to be used.
722 MockP2PManager mock_p2p_manager;
723 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
724 mock_p2p_manager.fake().SetP2PEnabled(false);
725 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
726 attempter_.Update("", "", false, false, false);
727 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
728 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
729 g_idle_add(&StaticQuitMainLoop, this);
730}
731
732TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
733 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
734 g_idle_add(&StaticP2PEnabledStartingFails, this);
735 g_main_loop_run(loop_);
736 g_main_loop_unref(loop_);
737 loop_ = NULL;
738}
739gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
740 gpointer data) {
741 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
742 ua_test->P2PEnabledStartingFailsStart();
743 return FALSE;
744}
745void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
746 // If p2p is enabled, but starting it fails ensure we don't do
747 // any housekeeping and do not convey that p2p should be used.
748 MockP2PManager mock_p2p_manager;
749 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
750 mock_p2p_manager.fake().SetP2PEnabled(true);
751 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
752 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
753 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
754 attempter_.Update("", "", false, false, false);
755 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
756 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
757 g_idle_add(&StaticQuitMainLoop, this);
758}
759
760TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
761 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
762 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
763 g_main_loop_run(loop_);
764 g_main_loop_unref(loop_);
765 loop_ = NULL;
766}
767gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
768 gpointer data) {
769 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
770 ua_test->P2PEnabledHousekeepingFailsStart();
771 return FALSE;
772}
773void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
774 // If p2p is enabled, starting it works but housekeeping fails, ensure
775 // we do not convey p2p is to be used.
776 MockP2PManager mock_p2p_manager;
777 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
778 mock_p2p_manager.fake().SetP2PEnabled(true);
779 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
780 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
781 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
782 attempter_.Update("", "", false, false, false);
783 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
784 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
785 g_idle_add(&StaticQuitMainLoop, this);
786}
787
788TEST_F(UpdateAttempterTest, P2PEnabled) {
789 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
790 g_idle_add(&StaticP2PEnabled, this);
791 g_main_loop_run(loop_);
792 g_main_loop_unref(loop_);
793 loop_ = NULL;
794}
795gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
796 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
797 ua_test->P2PEnabledStart();
798 return FALSE;
799}
800void UpdateAttempterTest::P2PEnabledStart() {
801 MockP2PManager mock_p2p_manager;
802 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
803 // If P2P is enabled and starting it works, check that we performed
804 // housekeeping and that we convey p2p should be used.
805 mock_p2p_manager.fake().SetP2PEnabled(true);
806 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
807 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
808 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
809 attempter_.Update("", "", false, false, false);
810 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
811 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
812 g_idle_add(&StaticQuitMainLoop, this);
813}
814
815TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
816 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
817 g_idle_add(&StaticP2PEnabledInteractive, this);
818 g_main_loop_run(loop_);
819 g_main_loop_unref(loop_);
820 loop_ = NULL;
821}
822gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
823 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
824 ua_test->P2PEnabledInteractiveStart();
825 return FALSE;
826}
827void UpdateAttempterTest::P2PEnabledInteractiveStart() {
828 MockP2PManager mock_p2p_manager;
829 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
830 // For an interactive check, if P2P is enabled and starting it
831 // works, check that we performed housekeeping and that we convey
832 // p2p should be used for sharing but NOT for downloading.
833 mock_p2p_manager.fake().SetP2PEnabled(true);
834 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
835 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
836 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
837 attempter_.Update("", "", false, true /* interactive */, false);
838 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
839 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
840 g_idle_add(&StaticQuitMainLoop, this);
841}
842
Jay Srinivasan0a708742012-03-20 11:26:12 -0700843TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
844 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
845 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
846 g_main_loop_run(loop_);
847 g_main_loop_unref(loop_);
848 loop_ = NULL;
849}
850
851void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
852 // Tests that the target_version_prefix value is properly fetched
853 // from the device policy.
854
855 const std::string target_version_prefix = "1412.";
856
857 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
858 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
859
860 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700861 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
862 Return(device_policy));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700863
864 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
865 .WillRepeatedly(DoAll(
866 SetArgumentPointee<0>(target_version_prefix),
867 Return(true)));
868
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800869 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700870 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700871 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700872
873 g_idle_add(&StaticQuitMainLoop, this);
874}
875
876
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700877TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
878 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
879 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
880 g_main_loop_run(loop_);
881 g_main_loop_unref(loop_);
882 loop_ = NULL;
883}
884
885// Tests that the scatter_factor_in_seconds value is properly fetched
886// from the device policy.
887void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
888 int64 scatter_factor_in_seconds = 36000;
889
890 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
891 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
892
893 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800894 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700895 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700896
897 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
898 .WillRepeatedly(DoAll(
899 SetArgumentPointee<0>(scatter_factor_in_seconds),
900 Return(true)));
901
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800902 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700903 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
904
905 g_idle_add(&StaticQuitMainLoop, this);
906}
907
908TEST_F(UpdateAttempterTest, DecrementUpdateCheckCountTest) {
909 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
910 g_idle_add(&StaticDecrementUpdateCheckCountTestStart, this);
911 g_main_loop_run(loop_);
912 g_main_loop_unref(loop_);
913 loop_ = NULL;
914}
915
916void UpdateAttempterTest::DecrementUpdateCheckCountTestStart() {
917 // Tests that the scatter_factor_in_seconds value is properly fetched
918 // from the device policy and is decremented if value > 0.
919 int64 initial_value = 5;
920 Prefs prefs;
921 attempter_.prefs_ = &prefs;
922
Jay Srinivasan08fce042012-06-07 16:31:01 -0700923 EXPECT_CALL(mock_system_state_,
David Zeuthen639aa362014-02-03 16:23:44 -0800924 IsOOBEComplete(_))
925 .WillRepeatedly(DoAll(SetArgumentPointee<0>(Time::UnixEpoch()),
926 Return(true)));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700927
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700928 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800929 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700930 &prefs_dir));
931 ScopedDirRemover temp_dir_remover(prefs_dir);
932
Alex Vakulenko75039d72014-03-25 12:36:28 -0700933 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700934 << "Failed to initialize preferences.";
935 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
936
937 int64 scatter_factor_in_seconds = 10;
938
939 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
940 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
941
942 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800943 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -0700944 Return(device_policy));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700945
946 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
947 .WillRepeatedly(DoAll(
948 SetArgumentPointee<0>(scatter_factor_in_seconds),
949 Return(true)));
950
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800951 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700952 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
953
954 // Make sure the file still exists.
955 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
956
957 int64 new_value;
958 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
959 EXPECT_EQ(initial_value - 1, new_value);
960
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700961 EXPECT_TRUE(
962 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700963
964 // However, if the count is already 0, it's not decremented. Test that.
965 initial_value = 0;
966 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800967 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700968 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
969 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
970 EXPECT_EQ(initial_value, new_value);
971
972 g_idle_add(&StaticQuitMainLoop, this);
973}
974
Jay Srinivasan08fce042012-06-07 16:31:01 -0700975TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
976 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
977 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
978 g_main_loop_run(loop_);
979 g_main_loop_unref(loop_);
980 loop_ = NULL;
981}
982
983void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
984 // Tests that no scattering logic is enabled if the update check
985 // is manually done (as opposed to a scheduled update check)
986 int64 initial_value = 8;
987 Prefs prefs;
988 attempter_.prefs_ = &prefs;
989
990 EXPECT_CALL(mock_system_state_,
David Zeuthen639aa362014-02-03 16:23:44 -0800991 IsOOBEComplete(_))
992 .WillRepeatedly(DoAll(SetArgumentPointee<0>(Time::UnixEpoch()),
993 Return(true)));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700994
995 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800996 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -0700997 &prefs_dir));
998 ScopedDirRemover temp_dir_remover(prefs_dir);
999
Alex Vakulenko75039d72014-03-25 12:36:28 -07001000 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -07001001 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -07001002 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001003 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
1004
1005 // make sure scatter_factor is non-zero as scattering is disabled
1006 // otherwise.
1007 int64 scatter_factor_in_seconds = 50;
1008
1009 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
1010 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
1011
1012 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001013 EXPECT_CALL(mock_system_state_, device_policy()).WillRepeatedly(
Jay Srinivasan21be0752012-07-25 15:44:56 -07001014 Return(device_policy));
Jay Srinivasan08fce042012-06-07 16:31:01 -07001015
1016 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
1017 .WillRepeatedly(DoAll(
1018 SetArgumentPointee<0>(scatter_factor_in_seconds),
1019 Return(true)));
1020
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001021 // Trigger an interactive check so we can test that scattering is disabled.
1022 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001023 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1024
1025 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001026 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001027 EXPECT_FALSE(
1028 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001029 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001030 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1031 EXPECT_FALSE(
1032 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001033 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
1034
1035 g_idle_add(&StaticQuitMainLoop, this);
1036}
1037
David Zeuthen985b1122013-10-09 12:13:15 -07001038// Checks that we only report daily metrics at most every 24 hours.
1039TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1040 FakeClock fake_clock;
1041 Prefs prefs;
1042 string temp_dir;
1043
1044 // We need persistent preferences for this test
Gilad Arnolda6742b32014-01-11 00:18:34 -08001045 EXPECT_TRUE(utils::MakeTempDirectory("UpdateCheckScheduler.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -07001046 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001047 prefs.Init(base::FilePath(temp_dir));
David Zeuthen985b1122013-10-09 12:13:15 -07001048 mock_system_state_.set_clock(&fake_clock);
1049 mock_system_state_.set_prefs(&prefs);
1050
1051 Time epoch = Time::FromInternalValue(0);
1052 fake_clock.SetWallclockTime(epoch);
1053
1054 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1055 // we should report.
1056 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1057 // We should not report again if no time has passed.
1058 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1059
1060 // We should not report if only 10 hours has passed.
1061 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1062 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1063
1064 // We should not report if only 24 hours - 1 sec has passed.
1065 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1066 TimeDelta::FromSeconds(1));
1067 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1068
1069 // We should report if 24 hours has passed.
1070 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1071 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1072
1073 // But then we should not report again..
1074 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1075
1076 // .. until another 24 hours has passed
1077 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1078 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1079 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1080 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1081 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1082
1083 // .. and another 24 hours
1084 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1085 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1086 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1087 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1088 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1089
1090 // If the span between time of reporting and present time is
1091 // negative, we report. This is in order to reset the timestamp and
1092 // avoid an edge condition whereby a distant point in the future is
1093 // in the state variable resulting in us never ever reporting again.
1094 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1095 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1096 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1097
1098 // In this case we should not update until the clock reads 71 + 24 = 95.
1099 // Check that.
1100 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1101 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1102 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1103 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1104 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1105
1106 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1107}
1108
David Zeuthen3c55abd2013-10-14 12:48:03 -07001109TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1110 const string update_completed_marker = test_dir_ + "/update-completed-marker";
1111 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
1112 update_completed_marker);
1113
1114 FakeClock fake_clock;
1115 fake_clock.SetBootTime(Time::FromTimeT(42));
1116 mock_system_state_.set_clock(&fake_clock);
1117
1118 Time boot_time;
1119 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1120
1121 attempter.WriteUpdateCompletedMarker();
1122
1123 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1124 EXPECT_EQ(boot_time.ToTimeT(), 42);
1125}
1126
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001127} // namespace chromeos_update_engine