blob: a65ffea68f3003ee0a39ab973a86b38cb28dc91d [file] [log] [blame]
Darin Petkovf42cc1c2010-09-01 09:03:02 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// 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>
7
8#include "update_engine/action_mock.h"
9#include "update_engine/action_processor_mock.h"
10#include "update_engine/filesystem_copier_action.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080011#include "update_engine/mock_dbus_interface.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070012#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070013#include "update_engine/prefs_mock.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070014#include "update_engine/update_attempter.h"
15
16using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070017using testing::_;
18using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070019using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070020using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080021using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070022using testing::Property;
23using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070024using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070025
26namespace chromeos_update_engine {
27
28// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070029// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070030// methods.
31class UpdateAttempterUnderTest : public UpdateAttempter {
32 public:
33 UpdateAttempterUnderTest()
Andrew de los Reyes45168102010-11-22 11:13:50 -080034 : UpdateAttempter(NULL, NULL, &dbus_) {}
35 MockDbusGlib dbus_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070036};
37
38class UpdateAttempterTest : public ::testing::Test {
39 protected:
40 virtual void SetUp() {
41 EXPECT_EQ(NULL, attempter_.dbus_service_);
42 EXPECT_EQ(NULL, attempter_.prefs_);
43 EXPECT_EQ(NULL, attempter_.metrics_lib_);
44 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
45 EXPECT_EQ(0, attempter_.http_response_code_);
46 EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_);
47 EXPECT_EQ(NULL, attempter_.manage_priority_source_);
48 EXPECT_FALSE(attempter_.download_active_);
49 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
50 EXPECT_EQ(0.0, attempter_.download_progress_);
51 EXPECT_EQ(0, attempter_.last_checked_time_);
52 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
53 EXPECT_EQ(0, attempter_.new_size_);
Darin Petkov36275772010-10-01 11:40:57 -070054 EXPECT_FALSE(attempter_.is_full_update_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070055 processor_ = new ActionProcessorMock();
56 attempter_.processor_.reset(processor_); // Transfers ownership.
Darin Petkov36275772010-10-01 11:40:57 -070057 attempter_.prefs_ = &prefs_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070058 }
59
60 UpdateAttempterUnderTest attempter_;
61 ActionProcessorMock* processor_;
Darin Petkov9c096d62010-11-17 14:49:04 -080062 NiceMock<PrefsMock> prefs_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070063};
64
Darin Petkovcd1666f2010-09-23 09:53:44 -070065TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -070066 extern const char* kUpdateCompletedMarker;
67 const FilePath kMarker(kUpdateCompletedMarker);
68 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
69 UpdateAttempterUnderTest attempter;
70 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
71 EXPECT_TRUE(file_util::Delete(kMarker, false));
72}
73
74TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
75 extern ActionExitCode GetErrorCodeForAction(AbstractAction* action,
76 ActionExitCode code);
77 EXPECT_EQ(kActionCodeSuccess,
78 GetErrorCodeForAction(NULL, kActionCodeSuccess));
79
80 OmahaRequestParams params;
81 OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL);
82 EXPECT_EQ(kActionCodeOmahaRequestError,
83 GetErrorCodeForAction(&omaha_request_action, kActionCodeError));
Darin Petkov73058b42010-10-06 16:32:19 -070084 OmahaResponseHandlerAction omaha_response_handler_action(&prefs_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070085 EXPECT_EQ(kActionCodeOmahaResponseHandlerError,
86 GetErrorCodeForAction(&omaha_response_handler_action,
87 kActionCodeError));
88 FilesystemCopierAction filesystem_copier_action(false);
89 EXPECT_EQ(kActionCodeFilesystemCopierError,
90 GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -080091 PostinstallRunnerAction postinstall_runner_action;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070092 EXPECT_EQ(kActionCodePostinstallRunnerError,
93 GetErrorCodeForAction(&postinstall_runner_action,
94 kActionCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -070095 ActionMock action_mock;
96 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
97 EXPECT_EQ(kActionCodeError,
98 GetErrorCodeForAction(&action_mock, kActionCodeError));
99}
100
Darin Petkov36275772010-10-01 11:40:57 -0700101TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
102 attempter_.omaha_request_params_.delta_okay = true;
103 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
104 .WillOnce(Return(false));
105 attempter_.DisableDeltaUpdateIfNeeded();
106 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
107 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
108 .WillOnce(DoAll(
109 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
110 Return(true)));
111 attempter_.DisableDeltaUpdateIfNeeded();
112 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
113 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
114 .WillOnce(DoAll(
115 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
116 Return(true)));
117 attempter_.DisableDeltaUpdateIfNeeded();
118 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
119 EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0);
120 attempter_.DisableDeltaUpdateIfNeeded();
121 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
122}
123
124TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
125 attempter_.is_full_update_ = false;
126 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
127 .WillOnce(Return(false))
128 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
129 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
130 .WillOnce(DoAll(
131 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
132 Return(true)));
Darin Petkov2dd01092010-10-08 15:43:05 -0700133 EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
134 .WillRepeatedly(Return(true));
Darin Petkov36275772010-10-01 11:40:57 -0700135 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
136 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
137 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures,
138 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
139 .Times(1);
140 for (int i = 0; i < 4; i ++)
141 attempter_.MarkDeltaUpdateFailure();
142}
143
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700144TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
145 extern const char* UpdateStatusToString(UpdateStatus);
146 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
147 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
148 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
149 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
150 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
151 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
152 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
153 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
154 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
155 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
156 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
157 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
158 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
159 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
160 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
161 EXPECT_STREQ("unknown status",
162 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
163}
164
165TEST_F(UpdateAttempterTest, UpdateTest) {
166 attempter_.set_http_response_code(200);
167 InSequence s;
168 const string kActionTypes[] = {
169 OmahaRequestAction::StaticType(),
170 OmahaResponseHandlerAction::StaticType(),
171 FilesystemCopierAction::StaticType(),
172 FilesystemCopierAction::StaticType(),
173 OmahaRequestAction::StaticType(),
174 DownloadAction::StaticType(),
175 OmahaRequestAction::StaticType(),
176 PostinstallRunnerAction::StaticType(),
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700177 OmahaRequestAction::StaticType()
178 };
179 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
180 EXPECT_CALL(*processor_,
181 EnqueueAction(Property(&AbstractAction::Type,
182 kActionTypes[i]))).Times(1);
183 }
184 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
185
Andrew de los Reyes45168102010-11-22 11:13:50 -0800186 attempter_.Update("", "", false);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700187
188 EXPECT_EQ(0, attempter_.http_response_code());
189 EXPECT_EQ(&attempter_, processor_->delegate());
190 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
191 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
192 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
193 }
194 EXPECT_EQ(attempter_.response_handler_action_.get(),
195 attempter_.actions_[1].get());
196 DownloadAction* download_action =
197 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
198 ASSERT_TRUE(download_action != NULL);
199 EXPECT_EQ(&attempter_, download_action->delegate());
200 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
201}
202
203} // namespace chromeos_update_engine