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