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