blob: 3bef1962003b279975ed103dbce49b7425adfcee [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/download_action.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080018
Darin Petkov9d911fa2010-08-19 09:36:08 -070019#include <gmock/gmock.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -070021
Ben Chan02f7c1d2014-10-18 15:18:02 -070022#include <memory>
David Zeuthen8f191b22013-08-06 12:27:50 -070023#include <string>
24#include <utility>
25#include <vector>
26
Alex Deymo60ca1a72015-06-18 18:19:15 -070027#include <base/bind.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070029#include <base/files/file_util.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070030#include <base/location.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070031#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070032#include <brillo/bind_lambda.h>
33#include <brillo/message_loops/fake_message_loop.h>
34#include <brillo/message_loops/message_loop.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070035
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/action_pipe.h"
37#include "update_engine/common/hash_calculator.h"
38#include "update_engine/common/mock_http_fetcher.h"
39#include "update_engine/common/mock_prefs.h"
40#include "update_engine/common/test_utils.h"
41#include "update_engine/common/utils.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070042#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070043#include "update_engine/fake_system_state.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070044#include "update_engine/update_manager/fake_update_manager.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000045
46namespace chromeos_update_engine {
47
Alex Deymof329b932014-10-30 01:37:48 -070048using base::FilePath;
49using base::ReadFileToString;
50using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000051using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070052using std::unique_ptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000053using std::vector;
Alex Deymoe5e5fe92015-10-05 09:28:19 -070054using test_utils::ScopedTempFile;
Darin Petkov9d911fa2010-08-19 09:36:08 -070055using testing::AtLeast;
56using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070057using testing::Return;
Alex Deymof329b932014-10-30 01:37:48 -070058using testing::_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000059
60class DownloadActionTest : public ::testing::Test { };
61
62namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070063class DownloadActionDelegateMock : public DownloadActionDelegate {
64 public:
65 MOCK_METHOD1(SetDownloadStatus, void(bool active));
66 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
67};
68
rspangler@google.com49fdf182009-10-10 00:57:34 +000069class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
70 public:
David Zeuthena99981f2013-04-29 13:42:47 -070071 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Deymo60ca1a72015-06-18 18:19:15 -070072 : processing_done_called_(false),
Darin Petkovc97435c2010-07-20 12:37:43 -070073 expected_code_(expected_code) {}
Alex Deymo610277e2014-11-11 21:18:11 -080074 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 EXPECT_TRUE(processing_done_called_);
76 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080077 void ProcessingDone(const ActionProcessor* processor,
78 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070079 brillo::MessageLoop::current()->BreakLoop();
80 brillo::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000081 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070082 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080083 ASSERT_EQ(expected_data_.size(), found_data.size());
84 for (unsigned i = 0; i < expected_data_.size(); i++) {
85 EXPECT_EQ(expected_data_[i], found_data[i]);
86 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000087 }
88 processing_done_called_ = true;
89 }
90
Yunlian Jiang35866ed2015-01-29 13:09:20 -080091 void ActionCompleted(ActionProcessor* processor,
92 AbstractAction* action,
93 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070094 const string type = action->Type();
95 if (type == DownloadAction::StaticType()) {
96 EXPECT_EQ(expected_code_, code);
97 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070098 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070099 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 }
101
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102 string path_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700103 brillo::Blob expected_data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -0700105 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106};
107
Darin Petkov9ce452b2010-11-17 14:33:28 -0800108class TestDirectFileWriter : public DirectFileWriter {
109 public:
110 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
111 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
112
Don Garrette410e0f2011-11-10 15:39:01 -0800113 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800114 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800115 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800116 }
117 return DirectFileWriter::Write(bytes, count);
118 }
119
120 private:
121 // If positive, fail on the |fail_write_| call to Write.
122 int fail_write_;
123 int current_write_;
124};
125
Alex Deymo60ca1a72015-06-18 18:19:15 -0700126void StartProcessorInRunLoop(ActionProcessor* processor,
127 MockHttpFetcher* http_fetcher) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700129 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130}
131
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700132void TestWithData(const brillo::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800133 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700134 bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700135 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700136 loop.SetAsCurrent();
Alex Deymo5ed695e2015-10-05 16:59:23 -0700137 FakeSystemState fake_system_state;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138
139 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700140 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800141 TestDirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700142 EXPECT_EQ(0, writer.Open(output_temp_file.GetPath().c_str(),
143 O_WRONLY | O_CREAT,
144 0));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800145 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700146
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700147 // We pull off the first byte from data and seek past it.
Alex Deymo39910dc2015-11-09 17:04:30 -0800148 string hash = HashCalculator::HashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700149 uint64_t size = data.size();
150 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700151 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700152 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700153 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700154 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700155 0,
156 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700157 "");
Alex Deymo5ed695e2015-10-05 16:59:23 -0700158 install_plan.source_slot = 0;
159 install_plan.target_slot = 1;
160 // We mark both slots as bootable. Only the target slot should be unbootable
161 // after the download starts.
162 fake_system_state.fake_boot_control()->SetSlotBootable(
163 install_plan.source_slot, true);
164 fake_system_state.fake_boot_control()->SetSlotBootable(
165 install_plan.target_slot, true);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000166 ObjectFeederAction<InstallPlan> feeder_action;
167 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800168 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800169 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800170 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700171 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700172 // takes ownership of passed in HttpFetcher
Alex Deymo5ed695e2015-10-05 16:59:23 -0700173 DownloadAction download_action(&prefs, &fake_system_state, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700174 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000175 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700176 DownloadActionDelegateMock download_delegate;
177 if (use_download_delegate) {
178 InSequence s;
179 download_action.set_delegate(&download_delegate);
180 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700181 if (data.size() > kMockHttpFetcherChunkSize)
182 EXPECT_CALL(download_delegate,
183 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800184 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700185 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
186 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700187 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700188 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700189 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700190 DownloadActionTestProcessorDelegate delegate(expected_code);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700191 delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700192 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193 ActionProcessor processor;
194 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000195 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000196 processor.EnqueueAction(&download_action);
197
Alex Deymo60ca1a72015-06-18 18:19:15 -0700198 loop.PostTask(FROM_HERE,
199 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
200 loop.Run();
201 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo5ed695e2015-10-05 16:59:23 -0700202
203 EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable(
204 install_plan.source_slot));
205 EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable(
206 install_plan.target_slot));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000207}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700208} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000209
210TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700211 brillo::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000212 const char* foo = "foo";
213 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700214 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800215 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700216 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000217}
218
219TEST(DownloadActionTest, LargeTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700220 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000221 char c = '0';
222 for (unsigned int i = 0; i < big.size(); i++) {
223 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800224 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000225 }
Darin Petkov50332f12010-09-24 11:44:47 -0700226 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800227 0, // fail_write
228 true); // use_download_delegate
229}
230
231TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700232 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800233 char c = '0';
234 for (unsigned int i = 0; i < big.size(); i++) {
235 big[i] = c;
236 c = ('9' == c) ? '0' : c + 1;
237 }
238 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800239 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700240 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700241}
242
Darin Petkov9d911fa2010-08-19 09:36:08 -0700243TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700244 brillo::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700245 const char* foo = "foofoo";
246 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700247 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800248 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700249 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000250}
251
252namespace {
253class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
254 public:
255 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700256 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000257 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000258};
259
Alex Deymo60ca1a72015-06-18 18:19:15 -0700260void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000261 processor->StartProcessing();
262 CHECK(processor->IsRunning());
263 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000264}
265
Darin Petkov9d911fa2010-08-19 09:36:08 -0700266void TestTerminateEarly(bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700267 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700268 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000269
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700270 brillo::Blob data(kMockHttpFetcherChunkSize +
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800271 kMockHttpFetcherChunkSize / 2);
272 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000273
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700274 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000275 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700276 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700277 EXPECT_EQ(0, writer.Open(temp_file.GetPath().c_str(),
278 O_WRONLY | O_CREAT,
279 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700280
rspangler@google.com49fdf182009-10-10 00:57:34 +0000281 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000282 ObjectFeederAction<InstallPlan> feeder_action;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700283 InstallPlan install_plan(false, false, "", 0, "", 0, "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000284 feeder_action.set_obj(install_plan);
Alex Deymo5ed695e2015-10-05 16:59:23 -0700285 FakeSystemState fake_system_state_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800286 MockPrefs prefs;
Alex Deymo5ed695e2015-10-05 16:59:23 -0700287 DownloadAction download_action(&prefs, &fake_system_state_,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800288 new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800289 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700290 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700291 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700292 DownloadActionDelegateMock download_delegate;
293 if (use_download_delegate) {
294 InSequence s;
295 download_action.set_delegate(&download_delegate);
296 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
297 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
298 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000299 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000300 ActionProcessor processor;
301 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000302 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000303 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000304 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000305
Alex Deymo60ca1a72015-06-18 18:19:15 -0700306 loop.PostTask(FROM_HERE,
307 base::Bind(&TerminateEarlyTestStarter, &processor));
308 loop.Run();
309 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000310 }
311
312 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700313 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
314 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000315 if (resulting_file_size != 0)
316 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
317}
318
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700319} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700320
321TEST(DownloadActionTest, TerminateEarlyTest) {
322 TestTerminateEarly(true);
323}
324
325TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
326 TestTerminateEarly(false);
327}
328
rspangler@google.com49fdf182009-10-10 00:57:34 +0000329class DownloadActionTestAction;
330
331template<>
332class ActionTraits<DownloadActionTestAction> {
333 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000334 typedef InstallPlan OutputObjectType;
335 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000336};
337
338// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700339class DownloadActionTestAction : public Action<DownloadActionTestAction> {
340 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000342 typedef InstallPlan InputObjectType;
343 typedef InstallPlan OutputObjectType;
344 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
345 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000346 ActionProcessor* processor() { return processor_; }
347 void PerformAction() {
348 did_run_ = true;
349 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000350 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000351 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700352 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000353 }
354 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000355 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000356 bool did_run_;
357};
358
359namespace {
360// This class is an ActionProcessorDelegate that simply terminates the
361// run loop when the ActionProcessor has completed processing. It's used
362// only by the test PassObjectOutTest.
363class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
364 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700365 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700366 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000367 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000368};
369
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700370} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000371
372TEST(DownloadActionTest, PassObjectOutTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700373 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700374 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000375
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700376 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700377 EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700378
rspangler@google.com49fdf182009-10-10 00:57:34 +0000379 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700380 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700381 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700382 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700383 1,
Alex Deymo39910dc2015-11-09 17:04:30 -0800384 HashCalculator::HashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700385 0,
386 "",
David Zeuthene7f89172013-10-31 10:21:04 -0700387 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000388 ObjectFeederAction<InstallPlan> feeder_action;
389 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800390 MockPrefs prefs;
Alex Deymo5ed695e2015-10-05 16:59:23 -0700391 FakeSystemState fake_system_state_;
392 DownloadAction download_action(&prefs, &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700393 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700394 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000395
396 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000397 test_action.expected_input_object_ = install_plan;
398 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000399 BondActions(&download_action, &test_action);
400
401 ActionProcessor processor;
402 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000403 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000404 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000405 processor.EnqueueAction(&download_action);
406 processor.EnqueueAction(&test_action);
407
Alex Deymo60ca1a72015-06-18 18:19:15 -0700408 loop.PostTask(FROM_HERE,
409 base::Bind([&processor] { processor.StartProcessing(); }));
410 loop.Run();
411 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000412
413 EXPECT_EQ(true, test_action.did_run_);
414}
415
David Zeuthen8f191b22013-08-06 12:27:50 -0700416// Test fixture for P2P tests.
417class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700418 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700419 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700420 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700421 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700422
Alex Deymo610277e2014-11-11 21:18:11 -0800423 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700424
425 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800426 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700427 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700428 }
429
430 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800431 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700432 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700433 }
434
435 // To be called by tests to setup the download. The
436 // |starting_offset| parameter is for where to resume.
437 void SetupDownload(off_t starting_offset) {
438 start_at_offset_ = starting_offset;
439 // Prepare data 10 kB of data.
440 data_.clear();
441 for (unsigned int i = 0; i < 10 * 1000; i++)
442 data_ += 'a' + (i % 25);
443
444 // Setup p2p.
445 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500446 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700447 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500448 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700449 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700450 }
451
452 // To be called by tests to perform the download. The
453 // |use_p2p_to_share| parameter is used to indicate whether the
454 // payload should be shared via p2p.
455 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700456 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
457 GetUsingP2PForSharing())
458 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700459
460 ScopedTempFile output_temp_file;
461 TestDirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700462 EXPECT_EQ(0, writer.Open(output_temp_file.GetPath().c_str(),
463 O_WRONLY | O_CREAT,
464 0));
David Zeuthen8f191b22013-08-06 12:27:50 -0700465 InstallPlan install_plan(false,
466 false,
467 "",
468 data_.length(),
469 "1234hash",
470 0,
471 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700472 "");
473 ObjectFeederAction<InstallPlan> feeder_action;
474 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800475 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700476 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
477 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700478 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700479 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700480 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700481 http_fetcher_));
482 download_action_->SetTestFileWriter(&writer);
483 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700484 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700485 delegate.expected_data_ = brillo::Blob(data_.begin() + start_at_offset_,
486 data_.end());
David Zeuthen8f191b22013-08-06 12:27:50 -0700487 delegate.path_ = output_temp_file.GetPath();
488 processor_.set_delegate(&delegate);
489 processor_.EnqueueAction(&feeder_action);
490 processor_.EnqueueAction(download_action_.get());
491
Alex Deymo60ca1a72015-06-18 18:19:15 -0700492 loop_.PostTask(FROM_HERE, base::Bind(
493 &P2PDownloadActionTest::StartProcessorInRunLoopForP2P,
494 base::Unretained(this)));
495 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700496 }
497
Alex Deymo60ca1a72015-06-18 18:19:15 -0700498 // Mainloop used to make StartDownload() synchronous.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700499 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700500
David Zeuthen8f191b22013-08-06 12:27:50 -0700501 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700502 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700503
504 // The HttpFetcher used in the test.
505 MockHttpFetcher* http_fetcher_;
506
507 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700508 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700509
510 // The ActionProcessor used for running the actions.
511 ActionProcessor processor_;
512
513 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700514 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700515
516 // The data being downloaded.
517 string data_;
518
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700519 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700520 // Callback used in StartDownload() method.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700521 void StartProcessorInRunLoopForP2P() {
522 processor_.StartProcessing();
523 http_fetcher_->SetOffset(start_at_offset_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700524 }
525
David Zeuthen8f191b22013-08-06 12:27:50 -0700526 // The requested starting offset passed to SetupDownload().
527 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700528
529 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700530};
531
532TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800533 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700534 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
535 << "Please update your system to support this feature.";
536 return;
537 }
538
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700539 SetupDownload(0); // starting_offset
540 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700541
542 // Check the p2p file and its content matches what was sent.
543 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700544 EXPECT_NE("", file_id);
545 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
546 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700547 string p2p_file_contents;
548 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
549 &p2p_file_contents));
550 EXPECT_EQ(data_, p2p_file_contents);
551}
552
553TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800554 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700555 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
556 << "Please update your system to support this feature.";
557 return;
558 }
559
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700560 SetupDownload(1000); // starting_offset
561 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700562
563 // DownloadAction should convey that the file is not being shared.
564 // and that we don't have any p2p files.
565 EXPECT_EQ(download_action_->p2p_file_id(), "");
566 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
567}
568
569TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800570 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700571 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
572 << "Please update your system to support this feature.";
573 return;
574 }
575
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700576 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700577
578 // Prepare the file with existing data before starting to write to
579 // it via DownloadAction.
580 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
581 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
582 string existing_data;
583 for (unsigned int i = 0; i < 1000; i++)
584 existing_data += '0' + (i % 10);
585 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
586 1000), 1000);
587
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700588 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700589
590 // DownloadAction should convey the same file_id and the file should
591 // have the expected size.
592 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
593 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
594 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
595 string p2p_file_contents;
596 // Check that the first 1000 bytes wasn't touched and that we
597 // appended the remaining as appropriate.
598 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
599 &p2p_file_contents));
600 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
601 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
602}
603
David Zeuthen8f191b22013-08-06 12:27:50 -0700604TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800605 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700606 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
607 << "Please update your system to support this feature.";
608 return;
609 }
610
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700611 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700612
613 // Prepare the file with all existing data before starting to write
614 // to it via DownloadAction.
615 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
616 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
617 string existing_data;
618 for (unsigned int i = 0; i < 1000; i++)
619 existing_data += '0' + (i % 10);
620 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
621 1000), 1000);
622
623 // Check that the file is there.
624 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
625 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
626
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700627 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700628
629 // DownloadAction should have deleted the p2p file. Check that it's gone.
630 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
631 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
632}
633
rspangler@google.com49fdf182009-10-10 00:57:34 +0000634} // namespace chromeos_update_engine