blob: 82e96dd2b0bb2a2d828fcd8beda6f5ac7d370e5f [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.
Gilad Arnold1f847232014-04-07 12:07:49 -070050 UpdateAttempterUnderTest(SystemState* system_state,
51 DBusWrapperInterface* dbus_iface)
52 : UpdateAttempterUnderTest(system_state, dbus_iface, "") {}
Gilad Arnold70e476e2013-07-30 16:01:13 -070053
Gilad Arnold1f847232014-04-07 12:07:49 -070054 UpdateAttempterUnderTest(SystemState* system_state,
55 DBusWrapperInterface* dbus_iface,
56 const std::string& update_completed_marker)
57 : UpdateAttempter(system_state, dbus_iface, 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) {
Gilad Arnold1f847232014-04-07 12:07:49 -070066 // Override system state members.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080067 mock_system_state_.set_connection_manager(&mock_connection_manager);
Gilad Arnold1f847232014-04-07 12:07:49 -070068 mock_system_state_.set_update_attempter(&attempter_);
69
70 // Finish initializing the attempter.
71 attempter_.Init();
Jay Srinivasan43488792012-06-19 00:25:31 -070072 }
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070073
Darin Petkovf42cc1c2010-09-01 09:03:02 -070074 virtual void SetUp() {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070075 CHECK(utils::MakeTempDirectory("UpdateAttempterTest-XXXXXX", &test_dir_));
76
Darin Petkovf42cc1c2010-09-01 09:03:02 -070077 EXPECT_EQ(NULL, attempter_.dbus_service_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080078 EXPECT_TRUE(attempter_.system_state_ != NULL);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070079 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
80 EXPECT_EQ(0, attempter_.http_response_code_);
Chris Sosa4f8ee272012-11-30 13:01:54 -080081 EXPECT_EQ(utils::kCpuSharesNormal, attempter_.shares_);
82 EXPECT_EQ(NULL, attempter_.manage_shares_source_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070083 EXPECT_FALSE(attempter_.download_active_);
84 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
85 EXPECT_EQ(0.0, attempter_.download_progress_);
86 EXPECT_EQ(0, attempter_.last_checked_time_);
87 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -070088 EXPECT_EQ(0, attempter_.new_payload_size_);
Jay Srinivasan55f50c22013-01-10 19:24:35 -080089 processor_ = new NiceMock<ActionProcessorMock>();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070090 attempter_.processor_.reset(processor_); // Transfers ownership.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080091 prefs_ = mock_system_state_.mock_prefs();
Darin Petkovf42cc1c2010-09-01 09:03:02 -070092 }
93
Gilad Arnoldeff87cc2013-07-22 18:32:09 -070094 virtual void TearDown() {
95 utils::RecursiveUnlinkDir(test_dir_);
96 }
97
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020098 void QuitMainLoop();
99 static gboolean StaticQuitMainLoop(gpointer data);
100
Darin Petkove6ef2f82011-03-07 17:31:11 -0800101 void UpdateTestStart();
102 void UpdateTestVerify();
Don Garrett6646b442013-11-13 15:29:11 -0800103 void RollbackTestStart(bool enterprise_rollback,
Don Garrett6646b442013-11-13 15:29:11 -0800104 bool valid_slot);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700105 void RollbackTestVerify();
Darin Petkove6ef2f82011-03-07 17:31:11 -0800106 static gboolean StaticUpdateTestStart(gpointer data);
107 static gboolean StaticUpdateTestVerify(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700108 static gboolean StaticRollbackTestStart(gpointer data);
Don Garrett6646b442013-11-13 15:29:11 -0800109 static gboolean StaticInvalidSlotRollbackTestStart(gpointer data);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700110 static gboolean StaticEnterpriseRollbackTestStart(gpointer data);
111 static gboolean StaticRollbackTestVerify(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200112
Thieu Le116fda32011-04-19 11:01:54 -0700113 void PingOmahaTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700114 static gboolean StaticPingOmahaTestStart(gpointer data);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200115
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700116 void ReadChannelFromPolicyTestStart();
117 static gboolean StaticReadChannelFromPolicyTestStart(gpointer data);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800118
Jay Srinivasan0a708742012-03-20 11:26:12 -0700119 void ReadUpdateDisabledFromPolicyTestStart();
120 static gboolean StaticReadUpdateDisabledFromPolicyTestStart(gpointer data);
121
122 void ReadTargetVersionPrefixFromPolicyTestStart();
123 static gboolean StaticReadTargetVersionPrefixFromPolicyTestStart(
124 gpointer data);
125
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700126 void ReadScatterFactorFromPolicyTestStart();
127 static gboolean StaticReadScatterFactorFromPolicyTestStart(
128 gpointer data);
129
130 void DecrementUpdateCheckCountTestStart();
131 static gboolean StaticDecrementUpdateCheckCountTestStart(
132 gpointer data);
133
Jay Srinivasan08fce042012-06-07 16:31:01 -0700134 void NoScatteringDoneDuringManualUpdateTestStart();
135 static gboolean StaticNoScatteringDoneDuringManualUpdateTestStart(
136 gpointer data);
137
David Zeuthen8f191b22013-08-06 12:27:50 -0700138 void P2PNotEnabledStart();
139 static gboolean StaticP2PNotEnabled(gpointer data);
140
141 void P2PEnabledStart();
142 static gboolean StaticP2PEnabled(gpointer data);
143
144 void P2PEnabledInteractiveStart();
145 static gboolean StaticP2PEnabledInteractive(gpointer data);
146
147 void P2PEnabledStartingFailsStart();
148 static gboolean StaticP2PEnabledStartingFails(gpointer data);
149
150 void P2PEnabledHousekeepingFailsStart();
151 static gboolean StaticP2PEnabledHousekeepingFails(gpointer data);
152
Gilad Arnold1f847232014-04-07 12:07:49 -0700153 MockSystemState mock_system_state_;
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800154 NiceMock<MockDBusWrapper> dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700155 UpdateAttempterUnderTest attempter_;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800156 NiceMock<ActionProcessorMock>* processor_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800157 NiceMock<PrefsMock>* prefs_; // shortcut to mock_system_state_->mock_prefs()
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800158 NiceMock<MockConnectionManager> mock_connection_manager;
Darin Petkove6ef2f82011-03-07 17:31:11 -0800159 GMainLoop* loop_;
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700160
161 string test_dir_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700162};
163
Darin Petkov1b003102010-11-30 10:18:36 -0800164TEST_F(UpdateAttempterTest, ActionCompletedDownloadTest) {
165 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
166 fetcher->FailTransfer(503); // Sets the HTTP response code.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800167 DownloadAction action(prefs_, NULL, fetcher.release());
168 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700169 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800170 EXPECT_EQ(503, attempter_.http_response_code());
171 EXPECT_EQ(UPDATE_STATUS_FINALIZING, attempter_.status());
172 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
173}
174
175TEST_F(UpdateAttempterTest, ActionCompletedErrorTest) {
176 ActionMock action;
177 EXPECT_CALL(action, Type()).WillRepeatedly(Return("ActionMock"));
178 attempter_.status_ = UPDATE_STATUS_DOWNLOADING;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800179 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov1b003102010-11-30 10:18:36 -0800180 .WillOnce(Return(false));
David Zeuthena99981f2013-04-29 13:42:47 -0700181 attempter_.ActionCompleted(NULL, &action, kErrorCodeError);
Darin Petkov1b003102010-11-30 10:18:36 -0800182 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
183}
184
185TEST_F(UpdateAttempterTest, ActionCompletedOmahaRequestTest) {
186 scoped_ptr<MockHttpFetcher> fetcher(new MockHttpFetcher("", 0, NULL));
187 fetcher->FailTransfer(500); // Sets the HTTP response code.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700188 OmahaRequestAction action(&mock_system_state_, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800189 fetcher.release(), false);
Darin Petkov1b003102010-11-30 10:18:36 -0800190 ObjectCollectorAction<OmahaResponse> collector_action;
191 BondActions(&action, &collector_action);
192 OmahaResponse response;
193 response.poll_interval = 234;
194 action.SetOutputObject(response);
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800195 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Darin Petkov1b003102010-11-30 10:18:36 -0800196 attempter_.set_update_check_scheduler(&scheduler);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800197 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)).Times(0);
David Zeuthena99981f2013-04-29 13:42:47 -0700198 attempter_.ActionCompleted(NULL, &action, kErrorCodeSuccess);
Darin Petkov1b003102010-11-30 10:18:36 -0800199 EXPECT_EQ(500, attempter_.http_response_code());
200 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status());
201 EXPECT_EQ(234, scheduler.poll_interval());
202 ASSERT_TRUE(attempter_.error_event_.get() == NULL);
203}
204
Darin Petkovcd1666f2010-09-23 09:53:44 -0700205TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Gilad Arnold70e476e2013-07-30 16:01:13 -0700206 string test_update_completed_marker;
207 CHECK(utils::MakeTempFile(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800208 "update_attempter_unittest-update_completed_marker-XXXXXX",
Gilad Arnold70e476e2013-07-30 16:01:13 -0700209 &test_update_completed_marker, NULL));
210 ScopedPathUnlinker completed_marker_unlinker(test_update_completed_marker);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700211 const base::FilePath marker(test_update_completed_marker);
Gilad Arnold70e476e2013-07-30 16:01:13 -0700212 EXPECT_EQ(0, file_util::WriteFile(marker, "", 0));
213 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
214 test_update_completed_marker);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700215 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700216}
217
218TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
David Zeuthena99981f2013-04-29 13:42:47 -0700219 extern ErrorCode GetErrorCodeForAction(AbstractAction* action,
220 ErrorCode code);
221 EXPECT_EQ(kErrorCodeSuccess,
222 GetErrorCodeForAction(NULL, kErrorCodeSuccess));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700223
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800224 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700225 OmahaRequestAction omaha_request_action(&mock_system_state, NULL,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800226 NULL, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700227 EXPECT_EQ(kErrorCodeOmahaRequestError,
228 GetErrorCodeForAction(&omaha_request_action, kErrorCodeError));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800229 OmahaResponseHandlerAction omaha_response_handler_action(&mock_system_state_);
David Zeuthena99981f2013-04-29 13:42:47 -0700230 EXPECT_EQ(kErrorCodeOmahaResponseHandlerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700231 GetErrorCodeForAction(&omaha_response_handler_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700232 kErrorCodeError));
Alex Deymo42432912013-07-12 20:21:15 -0700233 FilesystemCopierAction filesystem_copier_action(
234 &mock_system_state_, false, false);
David Zeuthena99981f2013-04-29 13:42:47 -0700235 EXPECT_EQ(kErrorCodeFilesystemCopierError,
236 GetErrorCodeForAction(&filesystem_copier_action, kErrorCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800237 PostinstallRunnerAction postinstall_runner_action;
David Zeuthena99981f2013-04-29 13:42:47 -0700238 EXPECT_EQ(kErrorCodePostinstallRunnerError,
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700239 GetErrorCodeForAction(&postinstall_runner_action,
David Zeuthena99981f2013-04-29 13:42:47 -0700240 kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700241 ActionMock action_mock;
242 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
David Zeuthena99981f2013-04-29 13:42:47 -0700243 EXPECT_EQ(kErrorCodeError,
244 GetErrorCodeForAction(&action_mock, kErrorCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700245}
246
Darin Petkov36275772010-10-01 11:40:57 -0700247TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700248 attempter_.omaha_request_params_->set_delta_okay(true);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800249 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700250 .WillOnce(Return(false));
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 - 1),
256 Return(true)));
257 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700258 EXPECT_TRUE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800259 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700260 .WillOnce(DoAll(
261 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
262 Return(true)));
263 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700264 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800265 EXPECT_CALL(*prefs_, GetInt64(_, _)).Times(0);
Darin Petkov36275772010-10-01 11:40:57 -0700266 attempter_.DisableDeltaUpdateIfNeeded();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700267 EXPECT_FALSE(attempter_.omaha_request_params_->delta_okay());
Darin Petkov36275772010-10-01 11:40:57 -0700268}
269
270TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800271 EXPECT_CALL(*prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
Darin Petkov36275772010-10-01 11:40:57 -0700272 .WillOnce(Return(false))
273 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
274 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
275 .WillOnce(DoAll(
276 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
277 Return(true)));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800278 EXPECT_CALL(*prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
Darin Petkov2dd01092010-10-08 15:43:05 -0700279 .WillRepeatedly(Return(true));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800280 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
281 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
282 EXPECT_CALL(*prefs_, SetInt64(kPrefsDeltaUpdateFailures,
Darin Petkov36275772010-10-01 11:40:57 -0700283 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
284 .Times(1);
285 for (int i = 0; i < 4; i ++)
286 attempter_.MarkDeltaUpdateFailure();
287}
288
Darin Petkov1b003102010-11-30 10:18:36 -0800289TEST_F(UpdateAttempterTest, ScheduleErrorEventActionNoEventTest) {
290 EXPECT_CALL(*processor_, EnqueueAction(_)).Times(0);
291 EXPECT_CALL(*processor_, StartProcessing()).Times(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800292 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(_))
293 .Times(0);
294 OmahaResponse response;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700295 string url1 = "http://url1";
296 response.payload_urls.push_back(url1);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800297 response.payload_urls.push_back("https://url");
Jay Srinivasan53173b92013-05-17 17:13:01 -0700298 EXPECT_CALL(*(mock_system_state_.mock_payload_state()), GetCurrentUrl())
299 .WillRepeatedly(Return(url1));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800300 mock_system_state_.mock_payload_state()->SetResponse(response);
Darin Petkov1b003102010-11-30 10:18:36 -0800301 attempter_.ScheduleErrorEventAction();
Jay Srinivasan53173b92013-05-17 17:13:01 -0700302 EXPECT_EQ(url1, mock_system_state_.mock_payload_state()->GetCurrentUrl());
Darin Petkov1b003102010-11-30 10:18:36 -0800303}
304
305TEST_F(UpdateAttempterTest, ScheduleErrorEventActionTest) {
306 EXPECT_CALL(*processor_,
307 EnqueueAction(Property(&AbstractAction::Type,
308 OmahaRequestAction::StaticType())))
309 .Times(1);
310 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
David Zeuthena99981f2013-04-29 13:42:47 -0700311 ErrorCode err = kErrorCodeError;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800312 EXPECT_CALL(*mock_system_state_.mock_payload_state(), UpdateFailed(err));
Darin Petkov1b003102010-11-30 10:18:36 -0800313 attempter_.error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
314 OmahaEvent::kResultError,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800315 err));
Darin Petkov1b003102010-11-30 10:18:36 -0800316 attempter_.ScheduleErrorEventAction();
317 EXPECT_EQ(UPDATE_STATUS_REPORTING_ERROR_EVENT, attempter_.status());
318}
319
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200320void UpdateAttempterTest::QuitMainLoop() {
321 g_main_loop_quit(loop_);
322}
323
324gboolean UpdateAttempterTest::StaticQuitMainLoop(gpointer data) {
325 reinterpret_cast<UpdateAttempterTest*>(data)->QuitMainLoop();
326 return FALSE;
327}
328
Darin Petkove6ef2f82011-03-07 17:31:11 -0800329gboolean UpdateAttempterTest::StaticUpdateTestStart(gpointer data) {
330 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestStart();
331 return FALSE;
332}
333
334gboolean UpdateAttempterTest::StaticUpdateTestVerify(gpointer data) {
335 reinterpret_cast<UpdateAttempterTest*>(data)->UpdateTestVerify();
336 return FALSE;
337}
338
Chris Sosa76a29ae2013-07-11 17:59:24 -0700339gboolean UpdateAttempterTest::StaticRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800340 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700341 false, true);
Don Garrett6646b442013-11-13 15:29:11 -0800342 return FALSE;
343}
344
345gboolean UpdateAttempterTest::StaticInvalidSlotRollbackTestStart(
346 gpointer data) {
347 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700348 false, false);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700349 return FALSE;
350}
351
352gboolean UpdateAttempterTest::StaticEnterpriseRollbackTestStart(gpointer data) {
Don Garrett6646b442013-11-13 15:29:11 -0800353 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700354 true, true);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700355 return FALSE;
356}
357
358gboolean UpdateAttempterTest::StaticRollbackTestVerify(gpointer data) {
359 reinterpret_cast<UpdateAttempterTest*>(data)->RollbackTestVerify();
360 return FALSE;
361}
362
Thieu Le116fda32011-04-19 11:01:54 -0700363gboolean UpdateAttempterTest::StaticPingOmahaTestStart(gpointer data) {
364 reinterpret_cast<UpdateAttempterTest*>(data)->PingOmahaTestStart();
365 return FALSE;
366}
367
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700368gboolean UpdateAttempterTest::StaticReadChannelFromPolicyTestStart(
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200369 gpointer data) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700370 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
371 ua_test->ReadChannelFromPolicyTestStart();
Thieu Le116fda32011-04-19 11:01:54 -0700372 return FALSE;
373}
374
Jay Srinivasan0a708742012-03-20 11:26:12 -0700375gboolean UpdateAttempterTest::StaticReadUpdateDisabledFromPolicyTestStart(
376 gpointer data) {
377 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
378 ua_test->ReadUpdateDisabledFromPolicyTestStart();
379 return FALSE;
380}
381
382gboolean UpdateAttempterTest::StaticReadTargetVersionPrefixFromPolicyTestStart(
383 gpointer data) {
384 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
385 ua_test->ReadTargetVersionPrefixFromPolicyTestStart();
386 return FALSE;
387}
388
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700389gboolean UpdateAttempterTest::StaticReadScatterFactorFromPolicyTestStart(
390 gpointer data) {
391 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
392 ua_test->ReadScatterFactorFromPolicyTestStart();
393 return FALSE;
394}
395
396gboolean UpdateAttempterTest::StaticDecrementUpdateCheckCountTestStart(
397 gpointer data) {
398 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
399 ua_test->DecrementUpdateCheckCountTestStart();
400 return FALSE;
401}
402
Jay Srinivasan08fce042012-06-07 16:31:01 -0700403gboolean UpdateAttempterTest::StaticNoScatteringDoneDuringManualUpdateTestStart(
404 gpointer data) {
405 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
406 ua_test->NoScatteringDoneDuringManualUpdateTestStart();
407 return FALSE;
408}
409
Darin Petkove6ef2f82011-03-07 17:31:11 -0800410namespace {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700411// Actions that will be built as part of an update check.
412const string kUpdateActionTypes[] = {
Darin Petkove6ef2f82011-03-07 17:31:11 -0800413 OmahaRequestAction::StaticType(),
414 OmahaResponseHandlerAction::StaticType(),
415 FilesystemCopierAction::StaticType(),
416 FilesystemCopierAction::StaticType(),
417 OmahaRequestAction::StaticType(),
418 DownloadAction::StaticType(),
419 OmahaRequestAction::StaticType(),
420 FilesystemCopierAction::StaticType(),
421 FilesystemCopierAction::StaticType(),
422 PostinstallRunnerAction::StaticType(),
423 OmahaRequestAction::StaticType()
424};
Chris Sosa76a29ae2013-07-11 17:59:24 -0700425
426// Actions that will be built as part of a user-initiated rollback.
427const string kRollbackActionTypes[] = {
428 InstallPlanAction::StaticType(),
429 PostinstallRunnerAction::StaticType(),
430};
431
Darin Petkove6ef2f82011-03-07 17:31:11 -0800432} // namespace {}
433
434void UpdateAttempterTest::UpdateTestStart() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700435 attempter_.set_http_response_code(200);
436 InSequence s;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700437 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700438 EXPECT_CALL(*processor_,
439 EnqueueAction(Property(&AbstractAction::Type,
Chris Sosa76a29ae2013-07-11 17:59:24 -0700440 kUpdateActionTypes[i]))).Times(1);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700441 }
442 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
443
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800444 attempter_.Update("", "", false, false, false);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800445 g_idle_add(&StaticUpdateTestVerify, this);
446}
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700447
Darin Petkove6ef2f82011-03-07 17:31:11 -0800448void UpdateAttempterTest::UpdateTestVerify() {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700449 EXPECT_EQ(0, attempter_.http_response_code());
450 EXPECT_EQ(&attempter_, processor_->delegate());
Chris Sosa76a29ae2013-07-11 17:59:24 -0700451 EXPECT_EQ(arraysize(kUpdateActionTypes), attempter_.actions_.size());
452 for (size_t i = 0; i < arraysize(kUpdateActionTypes); ++i) {
453 EXPECT_EQ(kUpdateActionTypes[i], attempter_.actions_[i]->Type());
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700454 }
455 EXPECT_EQ(attempter_.response_handler_action_.get(),
456 attempter_.actions_[1].get());
457 DownloadAction* download_action =
458 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
459 ASSERT_TRUE(download_action != NULL);
460 EXPECT_EQ(&attempter_, download_action->delegate());
461 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
Darin Petkove6ef2f82011-03-07 17:31:11 -0800462 g_main_loop_quit(loop_);
463}
464
Chris Sosa28e479c2013-07-12 11:39:53 -0700465void UpdateAttempterTest::RollbackTestStart(
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700466 bool enterprise_rollback, bool valid_slot) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700467 // Create a device policy so that we can change settings.
468 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
469 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
470
471 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold1f847232014-04-07 12:07:49 -0700472 mock_system_state_.set_device_policy(device_policy);
Chris Sosa76a29ae2013-07-11 17:59:24 -0700473
Don Garrett6646b442013-11-13 15:29:11 -0800474 if (!valid_slot) {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700475 // References bootable kernels in fake_hardware.h
476 string rollback_kernel = "/dev/sdz2";
Don Garrett6646b442013-11-13 15:29:11 -0800477 LOG(INFO) << "Test Mark Unbootable: " << rollback_kernel;
Gilad Arnold1f847232014-04-07 12:07:49 -0700478 mock_system_state_.fake_hardware()->MarkKernelUnbootable(
Don Garrett6646b442013-11-13 15:29:11 -0800479 rollback_kernel);
480 }
481
Chris Sosa28e479c2013-07-12 11:39:53 -0700482 bool is_rollback_allowed = false;
Chris Sosa76a29ae2013-07-11 17:59:24 -0700483
Chris Sosad38b1132014-03-25 10:43:59 -0700484 // We only allow rollback on devices that are not enterprise enrolled and
485 // which have a valid slot to rollback to.
486 if (!enterprise_rollback && valid_slot) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700487 is_rollback_allowed = true;
488 }
489
Don Garrett6646b442013-11-13 15:29:11 -0800490 if (enterprise_rollback) {
Chris Sosa28e479c2013-07-12 11:39:53 -0700491 // We return an empty owner as this is an enterprise.
Don Garrett6646b442013-11-13 15:29:11 -0800492 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa28e479c2013-07-12 11:39:53 -0700493 DoAll(SetArgumentPointee<0>(std::string("")),
494 Return(true)));
495 } else {
496 // We return a fake owner as this is an owned consumer device.
Don Garrett6646b442013-11-13 15:29:11 -0800497 EXPECT_CALL(*device_policy, GetOwner(_)).WillRepeatedly(
Chris Sosa76a29ae2013-07-11 17:59:24 -0700498 DoAll(SetArgumentPointee<0>(std::string("fake.mail@fake.com")),
499 Return(true)));
Chris Sosa28e479c2013-07-12 11:39:53 -0700500 }
Chris Sosa76a29ae2013-07-11 17:59:24 -0700501
Chris Sosa28e479c2013-07-12 11:39:53 -0700502 if (is_rollback_allowed) {
Chris Sosa76a29ae2013-07-11 17:59:24 -0700503 InSequence s;
504 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
505 EXPECT_CALL(*processor_,
506 EnqueueAction(Property(&AbstractAction::Type,
507 kRollbackActionTypes[i]))).Times(1);
508 }
509 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
510
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700511 EXPECT_TRUE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700512 g_idle_add(&StaticRollbackTestVerify, this);
513 } else {
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700514 EXPECT_FALSE(attempter_.Rollback(true));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700515 g_main_loop_quit(loop_);
516 }
517}
518
519void UpdateAttempterTest::RollbackTestVerify() {
520 // Verifies the actions that were enqueued.
521 EXPECT_EQ(&attempter_, processor_->delegate());
522 EXPECT_EQ(arraysize(kRollbackActionTypes), attempter_.actions_.size());
523 for (size_t i = 0; i < arraysize(kRollbackActionTypes); ++i) {
524 EXPECT_EQ(kRollbackActionTypes[i], attempter_.actions_[i]->Type());
525 }
526 EXPECT_EQ(UPDATE_STATUS_ATTEMPTING_ROLLBACK, attempter_.status());
527 InstallPlanAction* install_plan_action =
528 dynamic_cast<InstallPlanAction*>(attempter_.actions_[0].get());
529 InstallPlan* install_plan = install_plan_action->install_plan();
Chris Sosa44b9b7e2014-04-02 13:53:46 -0700530 // Matches fake_hardware.h -> rollback should move from kernel/boot device
531 // pair to other pair.
532 EXPECT_EQ(install_plan->install_path, string("/dev/sdz3"));
533 EXPECT_EQ(install_plan->kernel_install_path, string("/dev/sdz2"));
Chris Sosa76a29ae2013-07-11 17:59:24 -0700534 EXPECT_EQ(install_plan->powerwash_required, true);
535 g_main_loop_quit(loop_);
536}
537
Darin Petkove6ef2f82011-03-07 17:31:11 -0800538TEST_F(UpdateAttempterTest, UpdateTest) {
539 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
540 g_idle_add(&StaticUpdateTestStart, this);
541 g_main_loop_run(loop_);
542 g_main_loop_unref(loop_);
543 loop_ = NULL;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700544}
545
Chris Sosa76a29ae2013-07-11 17:59:24 -0700546TEST_F(UpdateAttempterTest, RollbackTest) {
547 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
548 g_idle_add(&StaticRollbackTestStart, this);
549 g_main_loop_run(loop_);
550 g_main_loop_unref(loop_);
551 loop_ = NULL;
552}
553
Don Garrett6646b442013-11-13 15:29:11 -0800554TEST_F(UpdateAttempterTest, InvalidSlotRollbackTest) {
555 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
556 g_idle_add(&StaticInvalidSlotRollbackTestStart, this);
557 g_main_loop_run(loop_);
558 g_main_loop_unref(loop_);
559 loop_ = NULL;
560}
561
Chris Sosa76a29ae2013-07-11 17:59:24 -0700562TEST_F(UpdateAttempterTest, EnterpriseRollbackTest) {
563 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
564 g_idle_add(&StaticEnterpriseRollbackTestStart, this);
565 g_main_loop_run(loop_);
566 g_main_loop_unref(loop_);
567 loop_ = NULL;
568}
569
Thieu Le116fda32011-04-19 11:01:54 -0700570void UpdateAttempterTest::PingOmahaTestStart() {
571 EXPECT_CALL(*processor_,
572 EnqueueAction(Property(&AbstractAction::Type,
573 OmahaRequestAction::StaticType())))
574 .Times(1);
575 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
576 attempter_.PingOmaha();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200577 g_idle_add(&StaticQuitMainLoop, this);
Thieu Le116fda32011-04-19 11:01:54 -0700578}
579
580TEST_F(UpdateAttempterTest, PingOmahaTest) {
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800581 UpdateCheckScheduler scheduler(&attempter_, &mock_system_state_);
Thieu Le116fda32011-04-19 11:01:54 -0700582 scheduler.enabled_ = true;
Andrew de los Reyese05fc282011-06-02 09:50:08 -0700583 EXPECT_FALSE(scheduler.scheduled_);
Thieu Le116fda32011-04-19 11:01:54 -0700584 attempter_.set_update_check_scheduler(&scheduler);
585 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
586 g_idle_add(&StaticPingOmahaTestStart, this);
587 g_main_loop_run(loop_);
588 g_main_loop_unref(loop_);
589 loop_ = NULL;
590 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter_.status());
591 EXPECT_EQ(true, scheduler.scheduled_);
592}
593
Darin Petkov18c7bce2011-06-16 14:07:00 -0700594TEST_F(UpdateAttempterTest, CreatePendingErrorEventTest) {
595 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700596 const ErrorCode kCode = kErrorCodeDownloadTransferError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700597 attempter_.CreatePendingErrorEvent(&action, kCode);
598 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
599 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
600 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700601 EXPECT_EQ(kCode | kErrorCodeTestOmahaUrlFlag,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800602 attempter_.error_event_->error_code);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700603}
604
605TEST_F(UpdateAttempterTest, CreatePendingErrorEventResumedTest) {
606 OmahaResponseHandlerAction *response_action =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800607 new OmahaResponseHandlerAction(&mock_system_state_);
Darin Petkov18c7bce2011-06-16 14:07:00 -0700608 response_action->install_plan_.is_resume = true;
609 attempter_.response_handler_action_.reset(response_action);
610 ActionMock action;
David Zeuthena99981f2013-04-29 13:42:47 -0700611 const ErrorCode kCode = kErrorCodeInstallDeviceOpenError;
Darin Petkov18c7bce2011-06-16 14:07:00 -0700612 attempter_.CreatePendingErrorEvent(&action, kCode);
613 ASSERT_TRUE(attempter_.error_event_.get() != NULL);
614 EXPECT_EQ(OmahaEvent::kTypeUpdateComplete, attempter_.error_event_->type);
615 EXPECT_EQ(OmahaEvent::kResultError, attempter_.error_event_->result);
David Zeuthena99981f2013-04-29 13:42:47 -0700616 EXPECT_EQ(kCode | kErrorCodeResumedFlag | kErrorCodeTestOmahaUrlFlag,
Darin Petkov18c7bce2011-06-16 14:07:00 -0700617 attempter_.error_event_->error_code);
618}
619
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700620TEST_F(UpdateAttempterTest, ReadChannelFromPolicy) {
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200621 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700622 g_idle_add(&StaticReadChannelFromPolicyTestStart, this);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200623 g_main_loop_run(loop_);
624 g_main_loop_unref(loop_);
625 loop_ = NULL;
626}
627
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700628void UpdateAttempterTest::ReadChannelFromPolicyTestStart() {
629 // Tests that the update channel (aka release channel) is properly fetched
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200630 // from the device policy.
631
632 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
633 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
634
635 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold1f847232014-04-07 12:07:49 -0700636 mock_system_state_.set_device_policy(device_policy);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200637
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700638 EXPECT_CALL(*device_policy, GetReleaseChannelDelegated(_)).WillRepeatedly(
639 DoAll(SetArgumentPointee<0>(bool(false)),
640 Return(true)));
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200641
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700642 EXPECT_CALL(*device_policy, GetReleaseChannel(_)).WillRepeatedly(
643 DoAll(SetArgumentPointee<0>(std::string("beta-channel")),
644 Return(true)));
645
Gilad Arnoldeff87cc2013-07-22 18:32:09 -0700646 ASSERT_FALSE(test_dir_.empty());
647 attempter_.omaha_request_params_->set_root(test_dir_);
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800648 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700649 EXPECT_EQ("beta-channel",
650 attempter_.omaha_request_params_->target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200651
652 g_idle_add(&StaticQuitMainLoop, this);
653}
654
Jay Srinivasan0a708742012-03-20 11:26:12 -0700655TEST_F(UpdateAttempterTest, ReadUpdateDisabledFromPolicy) {
656 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
657 g_idle_add(&StaticReadUpdateDisabledFromPolicyTestStart, this);
658 g_main_loop_run(loop_);
659 g_main_loop_unref(loop_);
660 loop_ = NULL;
661}
662
663void UpdateAttempterTest::ReadUpdateDisabledFromPolicyTestStart() {
664 // Tests that the update_disbled flag is properly fetched
665 // from the device policy.
666
667 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
668 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
669
670 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold1f847232014-04-07 12:07:49 -0700671 mock_system_state_.set_device_policy(device_policy);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700672
673 EXPECT_CALL(*device_policy, GetUpdateDisabled(_))
674 .WillRepeatedly(DoAll(
675 SetArgumentPointee<0>(true),
676 Return(true)));
677
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800678 attempter_.Update("", "", false, false, false);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700679 EXPECT_TRUE(attempter_.omaha_request_params_->update_disabled());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700680
681 g_idle_add(&StaticQuitMainLoop, this);
682}
683
David Zeuthen8f191b22013-08-06 12:27:50 -0700684TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenNotEnabled) {
685 MockP2PManager mock_p2p_manager;
686 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
687 mock_p2p_manager.fake().SetP2PEnabled(false);
688 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
689 attempter_.UpdateEngineStarted();
690}
691
692TEST_F(UpdateAttempterTest, P2PNotStartedAtStartupWhenEnabledButNotSharing) {
693 MockP2PManager mock_p2p_manager;
694 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
695 mock_p2p_manager.fake().SetP2PEnabled(true);
696 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(0);
697 attempter_.UpdateEngineStarted();
698}
699
700TEST_F(UpdateAttempterTest, P2PStartedAtStartupWhenEnabledAndSharing) {
701 MockP2PManager mock_p2p_manager;
702 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
703 mock_p2p_manager.fake().SetP2PEnabled(true);
704 mock_p2p_manager.fake().SetCountSharedFilesResult(1);
705 EXPECT_CALL(mock_p2p_manager, EnsureP2PRunning()).Times(1);
706 attempter_.UpdateEngineStarted();
707}
708
709TEST_F(UpdateAttempterTest, P2PNotEnabled) {
710 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
711 g_idle_add(&StaticP2PNotEnabled, this);
712 g_main_loop_run(loop_);
713 g_main_loop_unref(loop_);
714 loop_ = NULL;
715}
716gboolean UpdateAttempterTest::StaticP2PNotEnabled(gpointer data) {
717 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
718 ua_test->P2PNotEnabledStart();
719 return FALSE;
720}
721void UpdateAttempterTest::P2PNotEnabledStart() {
722 // If P2P is not enabled, check that we do not attempt housekeeping
723 // and do not convey that p2p is to be used.
724 MockP2PManager mock_p2p_manager;
725 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
726 mock_p2p_manager.fake().SetP2PEnabled(false);
727 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
728 attempter_.Update("", "", false, false, false);
729 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
730 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
731 g_idle_add(&StaticQuitMainLoop, this);
732}
733
734TEST_F(UpdateAttempterTest, P2PEnabledStartingFails) {
735 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
736 g_idle_add(&StaticP2PEnabledStartingFails, this);
737 g_main_loop_run(loop_);
738 g_main_loop_unref(loop_);
739 loop_ = NULL;
740}
741gboolean UpdateAttempterTest::StaticP2PEnabledStartingFails(
742 gpointer data) {
743 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
744 ua_test->P2PEnabledStartingFailsStart();
745 return FALSE;
746}
747void UpdateAttempterTest::P2PEnabledStartingFailsStart() {
748 // If p2p is enabled, but starting it fails ensure we don't do
749 // any housekeeping and do not convey that p2p should be used.
750 MockP2PManager mock_p2p_manager;
751 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
752 mock_p2p_manager.fake().SetP2PEnabled(true);
753 mock_p2p_manager.fake().SetEnsureP2PRunningResult(false);
754 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
755 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(0);
756 attempter_.Update("", "", false, false, false);
757 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
758 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
759 g_idle_add(&StaticQuitMainLoop, this);
760}
761
762TEST_F(UpdateAttempterTest, P2PEnabledHousekeepingFails) {
763 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
764 g_idle_add(&StaticP2PEnabledHousekeepingFails, this);
765 g_main_loop_run(loop_);
766 g_main_loop_unref(loop_);
767 loop_ = NULL;
768}
769gboolean UpdateAttempterTest::StaticP2PEnabledHousekeepingFails(
770 gpointer data) {
771 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
772 ua_test->P2PEnabledHousekeepingFailsStart();
773 return FALSE;
774}
775void UpdateAttempterTest::P2PEnabledHousekeepingFailsStart() {
776 // If p2p is enabled, starting it works but housekeeping fails, ensure
777 // we do not convey p2p is to be used.
778 MockP2PManager mock_p2p_manager;
779 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
780 mock_p2p_manager.fake().SetP2PEnabled(true);
781 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
782 mock_p2p_manager.fake().SetPerformHousekeepingResult(false);
783 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
784 attempter_.Update("", "", false, false, false);
785 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
786 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_sharing());
787 g_idle_add(&StaticQuitMainLoop, this);
788}
789
790TEST_F(UpdateAttempterTest, P2PEnabled) {
791 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
792 g_idle_add(&StaticP2PEnabled, this);
793 g_main_loop_run(loop_);
794 g_main_loop_unref(loop_);
795 loop_ = NULL;
796}
797gboolean UpdateAttempterTest::StaticP2PEnabled(gpointer data) {
798 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
799 ua_test->P2PEnabledStart();
800 return FALSE;
801}
802void UpdateAttempterTest::P2PEnabledStart() {
803 MockP2PManager mock_p2p_manager;
804 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
805 // If P2P is enabled and starting it works, check that we performed
806 // housekeeping and that we convey p2p should be used.
807 mock_p2p_manager.fake().SetP2PEnabled(true);
808 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
809 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
810 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
811 attempter_.Update("", "", false, false, false);
812 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_downloading());
813 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
814 g_idle_add(&StaticQuitMainLoop, this);
815}
816
817TEST_F(UpdateAttempterTest, P2PEnabledInteractive) {
818 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
819 g_idle_add(&StaticP2PEnabledInteractive, this);
820 g_main_loop_run(loop_);
821 g_main_loop_unref(loop_);
822 loop_ = NULL;
823}
824gboolean UpdateAttempterTest::StaticP2PEnabledInteractive(gpointer data) {
825 UpdateAttempterTest* ua_test = reinterpret_cast<UpdateAttempterTest*>(data);
826 ua_test->P2PEnabledInteractiveStart();
827 return FALSE;
828}
829void UpdateAttempterTest::P2PEnabledInteractiveStart() {
830 MockP2PManager mock_p2p_manager;
831 mock_system_state_.set_p2p_manager(&mock_p2p_manager);
832 // For an interactive check, if P2P is enabled and starting it
833 // works, check that we performed housekeeping and that we convey
834 // p2p should be used for sharing but NOT for downloading.
835 mock_p2p_manager.fake().SetP2PEnabled(true);
836 mock_p2p_manager.fake().SetEnsureP2PRunningResult(true);
837 mock_p2p_manager.fake().SetPerformHousekeepingResult(true);
838 EXPECT_CALL(mock_p2p_manager, PerformHousekeeping()).Times(1);
839 attempter_.Update("", "", false, true /* interactive */, false);
840 EXPECT_FALSE(attempter_.omaha_request_params_->use_p2p_for_downloading());
841 EXPECT_TRUE(attempter_.omaha_request_params_->use_p2p_for_sharing());
842 g_idle_add(&StaticQuitMainLoop, this);
843}
844
Jay Srinivasan0a708742012-03-20 11:26:12 -0700845TEST_F(UpdateAttempterTest, ReadTargetVersionPrefixFromPolicy) {
846 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
847 g_idle_add(&StaticReadTargetVersionPrefixFromPolicyTestStart, this);
848 g_main_loop_run(loop_);
849 g_main_loop_unref(loop_);
850 loop_ = NULL;
851}
852
853void UpdateAttempterTest::ReadTargetVersionPrefixFromPolicyTestStart() {
854 // Tests that the target_version_prefix value is properly fetched
855 // from the device policy.
856
857 const std::string target_version_prefix = "1412.";
858
859 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
860 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
861
862 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold1f847232014-04-07 12:07:49 -0700863 mock_system_state_.set_device_policy(device_policy);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700864
865 EXPECT_CALL(*device_policy, GetTargetVersionPrefix(_))
866 .WillRepeatedly(DoAll(
867 SetArgumentPointee<0>(target_version_prefix),
868 Return(true)));
869
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800870 attempter_.Update("", "", false, false, false);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700871 EXPECT_EQ(target_version_prefix.c_str(),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700872 attempter_.omaha_request_params_->target_version_prefix());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700873
874 g_idle_add(&StaticQuitMainLoop, this);
875}
876
877
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700878TEST_F(UpdateAttempterTest, ReadScatterFactorFromPolicy) {
879 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
880 g_idle_add(&StaticReadScatterFactorFromPolicyTestStart, this);
881 g_main_loop_run(loop_);
882 g_main_loop_unref(loop_);
883 loop_ = NULL;
884}
885
886// Tests that the scatter_factor_in_seconds value is properly fetched
887// from the device policy.
888void UpdateAttempterTest::ReadScatterFactorFromPolicyTestStart() {
889 int64 scatter_factor_in_seconds = 36000;
890
891 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
892 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
893
894 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold1f847232014-04-07 12:07:49 -0700895 mock_system_state_.set_device_policy(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
Gilad Arnold1f847232014-04-07 12:07:49 -0700923 mock_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700924
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700925 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800926 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700927 &prefs_dir));
928 ScopedDirRemover temp_dir_remover(prefs_dir);
929
Alex Vakulenko75039d72014-03-25 12:36:28 -0700930 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700931 << "Failed to initialize preferences.";
932 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
933
934 int64 scatter_factor_in_seconds = 10;
935
936 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
937 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
938
939 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold1f847232014-04-07 12:07:49 -0700940 mock_system_state_.set_device_policy(device_policy);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700941
942 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
943 .WillRepeatedly(DoAll(
944 SetArgumentPointee<0>(scatter_factor_in_seconds),
945 Return(true)));
946
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800947 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700948 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
949
950 // Make sure the file still exists.
951 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
952
953 int64 new_value;
954 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
955 EXPECT_EQ(initial_value - 1, new_value);
956
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700957 EXPECT_TRUE(
958 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700959
960 // However, if the count is already 0, it's not decremented. Test that.
961 initial_value = 0;
962 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800963 attempter_.Update("", "", false, false, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700964 EXPECT_TRUE(prefs.Exists(kPrefsUpdateCheckCount));
965 EXPECT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &new_value));
966 EXPECT_EQ(initial_value, new_value);
967
968 g_idle_add(&StaticQuitMainLoop, this);
969}
970
Jay Srinivasan08fce042012-06-07 16:31:01 -0700971TEST_F(UpdateAttempterTest, NoScatteringDoneDuringManualUpdateTestStart) {
972 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
973 g_idle_add(&StaticNoScatteringDoneDuringManualUpdateTestStart, this);
974 g_main_loop_run(loop_);
975 g_main_loop_unref(loop_);
976 loop_ = NULL;
977}
978
979void UpdateAttempterTest::NoScatteringDoneDuringManualUpdateTestStart() {
980 // Tests that no scattering logic is enabled if the update check
981 // is manually done (as opposed to a scheduled update check)
982 int64 initial_value = 8;
983 Prefs prefs;
984 attempter_.prefs_ = &prefs;
985
Gilad Arnold1f847232014-04-07 12:07:49 -0700986 mock_system_state_.fake_hardware()->SetIsOOBEComplete(Time::UnixEpoch());
Jay Srinivasan08fce042012-06-07 16:31:01 -0700987
988 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800989 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan08fce042012-06-07 16:31:01 -0700990 &prefs_dir));
991 ScopedDirRemover temp_dir_remover(prefs_dir);
992
Alex Vakulenko75039d72014-03-25 12:36:28 -0700993 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan08fce042012-06-07 16:31:01 -0700994 << "Failed to initialize preferences.";
Jay Srinivasan21be0752012-07-25 15:44:56 -0700995 EXPECT_TRUE(prefs.SetInt64(kPrefsWallClockWaitPeriod, initial_value));
Jay Srinivasan08fce042012-06-07 16:31:01 -0700996 EXPECT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, initial_value));
997
998 // make sure scatter_factor is non-zero as scattering is disabled
999 // otherwise.
1000 int64 scatter_factor_in_seconds = 50;
1001
1002 policy::MockDevicePolicy* device_policy = new policy::MockDevicePolicy();
1003 attempter_.policy_provider_.reset(new policy::PolicyProvider(device_policy));
1004
1005 EXPECT_CALL(*device_policy, LoadPolicy()).WillRepeatedly(Return(true));
Gilad Arnold1f847232014-04-07 12:07:49 -07001006 mock_system_state_.set_device_policy(device_policy);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001007
1008 EXPECT_CALL(*device_policy, GetScatterFactorInSeconds(_))
1009 .WillRepeatedly(DoAll(
1010 SetArgumentPointee<0>(scatter_factor_in_seconds),
1011 Return(true)));
1012
Gilad Arnoldb92f0df2013-01-10 16:32:45 -08001013 // Trigger an interactive check so we can test that scattering is disabled.
1014 attempter_.Update("", "", false, true, false);
Jay Srinivasan08fce042012-06-07 16:31:01 -07001015 EXPECT_EQ(scatter_factor_in_seconds, attempter_.scatter_factor_.InSeconds());
1016
1017 // Make sure scattering is disabled for manual (i.e. user initiated) update
Jay Srinivasan21be0752012-07-25 15:44:56 -07001018 // checks and all artifacts are removed.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001019 EXPECT_FALSE(
1020 attempter_.omaha_request_params_->wall_clock_based_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001021 EXPECT_FALSE(prefs.Exists(kPrefsWallClockWaitPeriod));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001022 EXPECT_EQ(0, attempter_.omaha_request_params_->waiting_period().InSeconds());
1023 EXPECT_FALSE(
1024 attempter_.omaha_request_params_->update_check_count_wait_enabled());
Jay Srinivasan08fce042012-06-07 16:31:01 -07001025 EXPECT_FALSE(prefs.Exists(kPrefsUpdateCheckCount));
1026
1027 g_idle_add(&StaticQuitMainLoop, this);
1028}
1029
David Zeuthen985b1122013-10-09 12:13:15 -07001030// Checks that we only report daily metrics at most every 24 hours.
1031TEST_F(UpdateAttempterTest, ReportDailyMetrics) {
1032 FakeClock fake_clock;
1033 Prefs prefs;
1034 string temp_dir;
1035
1036 // We need persistent preferences for this test
Gilad Arnolda6742b32014-01-11 00:18:34 -08001037 EXPECT_TRUE(utils::MakeTempDirectory("UpdateCheckScheduler.XXXXXX",
David Zeuthen985b1122013-10-09 12:13:15 -07001038 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07001039 prefs.Init(base::FilePath(temp_dir));
David Zeuthen985b1122013-10-09 12:13:15 -07001040 mock_system_state_.set_clock(&fake_clock);
1041 mock_system_state_.set_prefs(&prefs);
1042
1043 Time epoch = Time::FromInternalValue(0);
1044 fake_clock.SetWallclockTime(epoch);
1045
1046 // If there is no kPrefsDailyMetricsLastReportedAt state variable,
1047 // we should report.
1048 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1049 // We should not report again if no time has passed.
1050 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1051
1052 // We should not report if only 10 hours has passed.
1053 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(10));
1054 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1055
1056 // We should not report if only 24 hours - 1 sec has passed.
1057 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24) -
1058 TimeDelta::FromSeconds(1));
1059 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1060
1061 // We should report if 24 hours has passed.
1062 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(24));
1063 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1064
1065 // But then we should not report again..
1066 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1067
1068 // .. until another 24 hours has passed
1069 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(47));
1070 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1071 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(48));
1072 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1073 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1074
1075 // .. and another 24 hours
1076 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1077 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1078 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(72));
1079 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1080 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1081
1082 // If the span between time of reporting and present time is
1083 // negative, we report. This is in order to reset the timestamp and
1084 // avoid an edge condition whereby a distant point in the future is
1085 // in the state variable resulting in us never ever reporting again.
1086 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(71));
1087 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1088 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1089
1090 // In this case we should not update until the clock reads 71 + 24 = 95.
1091 // Check that.
1092 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(94));
1093 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1094 fake_clock.SetWallclockTime(epoch + TimeDelta::FromHours(95));
1095 EXPECT_TRUE(attempter_.CheckAndReportDailyMetrics());
1096 EXPECT_FALSE(attempter_.CheckAndReportDailyMetrics());
1097
1098 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
1099}
1100
David Zeuthen3c55abd2013-10-14 12:48:03 -07001101TEST_F(UpdateAttempterTest, BootTimeInUpdateMarkerFile) {
1102 const string update_completed_marker = test_dir_ + "/update-completed-marker";
1103 UpdateAttempterUnderTest attempter(&mock_system_state_, &dbus_,
1104 update_completed_marker);
1105
1106 FakeClock fake_clock;
1107 fake_clock.SetBootTime(Time::FromTimeT(42));
1108 mock_system_state_.set_clock(&fake_clock);
1109
1110 Time boot_time;
1111 EXPECT_FALSE(attempter.GetBootTimeAtUpdate(&boot_time));
1112
1113 attempter.WriteUpdateCompletedMarker();
1114
1115 EXPECT_TRUE(attempter.GetBootTimeAtUpdate(&boot_time));
1116 EXPECT_EQ(boot_time.ToTimeT(), 42);
1117}
1118
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001119} // namespace chromeos_update_engine