blob: 89adb03b29a102f82328a3c03dce9f3a3006cb03 [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"
11#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070012#include "update_engine/prefs_mock.h"
Darin Petkovf42cc1c2010-09-01 09:03:02 -070013#include "update_engine/update_attempter.h"
14
15using std::string;
Darin Petkov36275772010-10-01 11:40:57 -070016using testing::_;
17using testing::DoAll;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070018using testing::InSequence;
Darin Petkov2dd01092010-10-08 15:43:05 -070019using testing::Ne;
Darin Petkov9c096d62010-11-17 14:49:04 -080020using testing::NiceMock;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070021using testing::Property;
22using testing::Return;
Darin Petkov36275772010-10-01 11:40:57 -070023using testing::SetArgumentPointee;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070024
25namespace chromeos_update_engine {
26
27// Test a subclass rather than the main class directly so that we can mock out
Darin Petkovcd1666f2010-09-23 09:53:44 -070028// methods within the class. There're explicit unit tests for the mocked out
Darin Petkovf42cc1c2010-09-01 09:03:02 -070029// methods.
30class UpdateAttempterUnderTest : public UpdateAttempter {
31 public:
32 UpdateAttempterUnderTest()
33 : UpdateAttempter(NULL, NULL) {}
34};
35
36class UpdateAttempterTest : public ::testing::Test {
37 protected:
38 virtual void SetUp() {
39 EXPECT_EQ(NULL, attempter_.dbus_service_);
40 EXPECT_EQ(NULL, attempter_.prefs_);
41 EXPECT_EQ(NULL, attempter_.metrics_lib_);
42 EXPECT_EQ(NULL, attempter_.update_check_scheduler_);
43 EXPECT_EQ(0, attempter_.http_response_code_);
44 EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_);
45 EXPECT_EQ(NULL, attempter_.manage_priority_source_);
46 EXPECT_FALSE(attempter_.download_active_);
47 EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_);
48 EXPECT_EQ(0.0, attempter_.download_progress_);
49 EXPECT_EQ(0, attempter_.last_checked_time_);
50 EXPECT_EQ("0.0.0.0", attempter_.new_version_);
51 EXPECT_EQ(0, attempter_.new_size_);
Darin Petkov36275772010-10-01 11:40:57 -070052 EXPECT_FALSE(attempter_.is_full_update_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070053 processor_ = new ActionProcessorMock();
54 attempter_.processor_.reset(processor_); // Transfers ownership.
Darin Petkov36275772010-10-01 11:40:57 -070055 attempter_.prefs_ = &prefs_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070056 }
57
58 UpdateAttempterUnderTest attempter_;
59 ActionProcessorMock* processor_;
Darin Petkov9c096d62010-11-17 14:49:04 -080060 NiceMock<PrefsMock> prefs_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061};
62
Darin Petkovcd1666f2010-09-23 09:53:44 -070063TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -070064 extern const char* kUpdateCompletedMarker;
65 const FilePath kMarker(kUpdateCompletedMarker);
66 EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0));
67 UpdateAttempterUnderTest attempter;
68 EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status());
69 EXPECT_TRUE(file_util::Delete(kMarker, false));
70}
71
72TEST_F(UpdateAttempterTest, GetErrorCodeForActionTest) {
73 extern ActionExitCode GetErrorCodeForAction(AbstractAction* action,
74 ActionExitCode code);
75 EXPECT_EQ(kActionCodeSuccess,
76 GetErrorCodeForAction(NULL, kActionCodeSuccess));
77
78 OmahaRequestParams params;
79 OmahaRequestAction omaha_request_action(NULL, params, NULL, NULL);
80 EXPECT_EQ(kActionCodeOmahaRequestError,
81 GetErrorCodeForAction(&omaha_request_action, kActionCodeError));
Darin Petkov73058b42010-10-06 16:32:19 -070082 OmahaResponseHandlerAction omaha_response_handler_action(&prefs_);
Darin Petkovf42cc1c2010-09-01 09:03:02 -070083 EXPECT_EQ(kActionCodeOmahaResponseHandlerError,
84 GetErrorCodeForAction(&omaha_response_handler_action,
85 kActionCodeError));
86 FilesystemCopierAction filesystem_copier_action(false);
87 EXPECT_EQ(kActionCodeFilesystemCopierError,
88 GetErrorCodeForAction(&filesystem_copier_action, kActionCodeError));
Darin Petkov6d5dbf62010-11-08 16:09:55 -080089 PostinstallRunnerAction postinstall_runner_action;
Darin Petkovf42cc1c2010-09-01 09:03:02 -070090 EXPECT_EQ(kActionCodePostinstallRunnerError,
91 GetErrorCodeForAction(&postinstall_runner_action,
92 kActionCodeError));
Darin Petkovf42cc1c2010-09-01 09:03:02 -070093 ActionMock action_mock;
94 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
95 EXPECT_EQ(kActionCodeError,
96 GetErrorCodeForAction(&action_mock, kActionCodeError));
97}
98
Darin Petkov36275772010-10-01 11:40:57 -070099TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
100 attempter_.omaha_request_params_.delta_okay = true;
101 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
102 .WillOnce(Return(false));
103 attempter_.DisableDeltaUpdateIfNeeded();
104 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
105 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
106 .WillOnce(DoAll(
107 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
108 Return(true)));
109 attempter_.DisableDeltaUpdateIfNeeded();
110 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
111 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
112 .WillOnce(DoAll(
113 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
114 Return(true)));
115 attempter_.DisableDeltaUpdateIfNeeded();
116 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
117 EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0);
118 attempter_.DisableDeltaUpdateIfNeeded();
119 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
120}
121
122TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
123 attempter_.is_full_update_ = false;
124 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
125 .WillOnce(Return(false))
126 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
127 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
128 .WillOnce(DoAll(
129 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
130 Return(true)));
Darin Petkov2dd01092010-10-08 15:43:05 -0700131 EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
132 .WillRepeatedly(Return(true));
Darin Petkov36275772010-10-01 11:40:57 -0700133 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
134 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
135 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures,
136 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
137 .Times(1);
138 for (int i = 0; i < 4; i ++)
139 attempter_.MarkDeltaUpdateFailure();
140}
141
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700142TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
143 extern const char* UpdateStatusToString(UpdateStatus);
144 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
145 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
146 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
147 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
148 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
149 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
150 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
151 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
152 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
153 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
154 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
155 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
156 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
157 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
158 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
159 EXPECT_STREQ("unknown status",
160 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
161}
162
163TEST_F(UpdateAttempterTest, UpdateTest) {
164 attempter_.set_http_response_code(200);
165 InSequence s;
166 const string kActionTypes[] = {
167 OmahaRequestAction::StaticType(),
168 OmahaResponseHandlerAction::StaticType(),
169 FilesystemCopierAction::StaticType(),
170 FilesystemCopierAction::StaticType(),
171 OmahaRequestAction::StaticType(),
172 DownloadAction::StaticType(),
173 OmahaRequestAction::StaticType(),
174 PostinstallRunnerAction::StaticType(),
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700175 OmahaRequestAction::StaticType()
176 };
177 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
178 EXPECT_CALL(*processor_,
179 EnqueueAction(Property(&AbstractAction::Type,
180 kActionTypes[i]))).Times(1);
181 }
182 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
183
184 attempter_.Update("", "");
185
186 EXPECT_EQ(0, attempter_.http_response_code());
187 EXPECT_EQ(&attempter_, processor_->delegate());
188 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
189 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
190 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
191 }
192 EXPECT_EQ(attempter_.response_handler_action_.get(),
193 attempter_.actions_[1].get());
194 DownloadAction* download_action =
195 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
196 ASSERT_TRUE(download_action != NULL);
197 EXPECT_EQ(&attempter_, download_action->delegate());
198 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
199}
200
201} // namespace chromeos_update_engine