blob: bb86af729874930a2d74e2e52e9a1b6790694955 [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/set_bootable_flag_action.h"
14#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 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 Petkov36275772010-10-01 11:40:57 -070060 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));
89 PostinstallRunnerAction postinstall_runner_action(true);
90 EXPECT_EQ(kActionCodePostinstallRunnerError,
91 GetErrorCodeForAction(&postinstall_runner_action,
92 kActionCodeError));
93 SetBootableFlagAction set_bootable_flag_action;
94 EXPECT_EQ(kActionCodeSetBootableFlagError,
95 GetErrorCodeForAction(&set_bootable_flag_action,
96 kActionCodeError));
97 ActionMock action_mock;
98 EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock"));
99 EXPECT_EQ(kActionCodeError,
100 GetErrorCodeForAction(&action_mock, kActionCodeError));
101}
102
Darin Petkov36275772010-10-01 11:40:57 -0700103TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) {
104 attempter_.omaha_request_params_.delta_okay = true;
105 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
106 .WillOnce(Return(false));
107 attempter_.DisableDeltaUpdateIfNeeded();
108 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
109 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
110 .WillOnce(DoAll(
111 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1),
112 Return(true)));
113 attempter_.DisableDeltaUpdateIfNeeded();
114 EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay);
115 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
116 .WillOnce(DoAll(
117 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
118 Return(true)));
119 attempter_.DisableDeltaUpdateIfNeeded();
120 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
121 EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0);
122 attempter_.DisableDeltaUpdateIfNeeded();
123 EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay);
124}
125
126TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) {
127 attempter_.is_full_update_ = false;
128 EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _))
129 .WillOnce(Return(false))
130 .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true)))
131 .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true)))
132 .WillOnce(DoAll(
133 SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures),
134 Return(true)));
Darin Petkov2dd01092010-10-08 15:43:05 -0700135 EXPECT_CALL(prefs_, SetInt64(Ne(kPrefsDeltaUpdateFailures), _))
136 .WillRepeatedly(Return(true));
Darin Petkov36275772010-10-01 11:40:57 -0700137 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2);
138 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1);
139 EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures,
140 UpdateAttempter::kMaxDeltaUpdateFailures + 1))
141 .Times(1);
142 for (int i = 0; i < 4; i ++)
143 attempter_.MarkDeltaUpdateFailure();
144}
145
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700146TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) {
147 extern const char* UpdateStatusToString(UpdateStatus);
148 EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE));
149 EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE",
150 UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE));
151 EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE",
152 UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE));
153 EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING",
154 UpdateStatusToString(UPDATE_STATUS_DOWNLOADING));
155 EXPECT_STREQ("UPDATE_STATUS_VERIFYING",
156 UpdateStatusToString(UPDATE_STATUS_VERIFYING));
157 EXPECT_STREQ("UPDATE_STATUS_FINALIZING",
158 UpdateStatusToString(UPDATE_STATUS_FINALIZING));
159 EXPECT_STREQ("UPDATE_STATUS_UPDATED_NEED_REBOOT",
160 UpdateStatusToString(UPDATE_STATUS_UPDATED_NEED_REBOOT));
161 EXPECT_STREQ("UPDATE_STATUS_REPORTING_ERROR_EVENT",
162 UpdateStatusToString(UPDATE_STATUS_REPORTING_ERROR_EVENT));
163 EXPECT_STREQ("unknown status",
164 UpdateStatusToString(static_cast<UpdateStatus>(-1)));
165}
166
167TEST_F(UpdateAttempterTest, UpdateTest) {
168 attempter_.set_http_response_code(200);
169 InSequence s;
170 const string kActionTypes[] = {
171 OmahaRequestAction::StaticType(),
172 OmahaResponseHandlerAction::StaticType(),
173 FilesystemCopierAction::StaticType(),
174 FilesystemCopierAction::StaticType(),
175 OmahaRequestAction::StaticType(),
176 DownloadAction::StaticType(),
177 OmahaRequestAction::StaticType(),
178 PostinstallRunnerAction::StaticType(),
179 SetBootableFlagAction::StaticType(),
180 PostinstallRunnerAction::StaticType(),
181 OmahaRequestAction::StaticType()
182 };
183 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
184 EXPECT_CALL(*processor_,
185 EnqueueAction(Property(&AbstractAction::Type,
186 kActionTypes[i]))).Times(1);
187 }
188 EXPECT_CALL(*processor_, StartProcessing()).Times(1);
189
190 attempter_.Update("", "");
191
192 EXPECT_EQ(0, attempter_.http_response_code());
193 EXPECT_EQ(&attempter_, processor_->delegate());
194 EXPECT_EQ(arraysize(kActionTypes), attempter_.actions_.size());
195 for (size_t i = 0; i < arraysize(kActionTypes); ++i) {
196 EXPECT_EQ(kActionTypes[i], attempter_.actions_[i]->Type());
197 }
198 EXPECT_EQ(attempter_.response_handler_action_.get(),
199 attempter_.actions_[1].get());
200 DownloadAction* download_action =
201 dynamic_cast<DownloadAction*>(attempter_.actions_[5].get());
202 ASSERT_TRUE(download_action != NULL);
203 EXPECT_EQ(&attempter_, download_action->delegate());
204 EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status());
205}
206
207} // namespace chromeos_update_engine