blob: 6d2a9f0bfee7d3475fbf2c0cd737f501315e8ce9 [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>
Sen Jiangba2213a2018-02-27 16:28:20 -080025#include <vector>
David Zeuthen8f191b22013-08-06 12:27:50 -070026
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>
Amin Hassani71818c82018-03-06 13:25:40 -080032#include <brillo/bind_lambda.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070033#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"
Aaron Wood9321f502017-09-07 11:18:54 -070044#include "update_engine/mock_file_writer.h"
Alex Deymo542c19b2015-12-03 07:43:31 -030045#include "update_engine/payload_consumer/mock_download_action.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070046#include "update_engine/update_manager/fake_update_manager.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000047
48namespace chromeos_update_engine {
49
Alex Deymof329b932014-10-30 01:37:48 -070050using base::FilePath;
51using base::ReadFileToString;
52using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000053using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070054using std::unique_ptr;
Alex Deymoe5e5fe92015-10-05 09:28:19 -070055using test_utils::ScopedTempFile;
Darin Petkov9d911fa2010-08-19 09:36:08 -070056using testing::AtLeast;
57using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070058using testing::Return;
Aaron Wood9321f502017-09-07 11:18:54 -070059using testing::SetArgPointee;
Alex Deymof329b932014-10-30 01:37:48 -070060using testing::_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000061
62class DownloadActionTest : public ::testing::Test { };
63
64namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070065
rspangler@google.com49fdf182009-10-10 00:57:34 +000066class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
67 public:
Amin Hassanid3f4bea2018-04-30 14:52:40 -070068 DownloadActionTestProcessorDelegate()
69 : processing_done_called_(false), expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -080070 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 EXPECT_TRUE(processing_done_called_);
72 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080073 void ProcessingDone(const ActionProcessor* processor,
74 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070075 brillo::MessageLoop::current()->BreakLoop();
76 brillo::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000077 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070078 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080079 ASSERT_EQ(expected_data_.size(), found_data.size());
80 for (unsigned i = 0; i < expected_data_.size(); i++) {
81 EXPECT_EQ(expected_data_[i], found_data[i]);
82 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 }
84 processing_done_called_ = true;
85 }
86
Yunlian Jiang35866ed2015-01-29 13:09:20 -080087 void ActionCompleted(ActionProcessor* processor,
88 AbstractAction* action,
89 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070090 const string type = action->Type();
91 if (type == DownloadAction::StaticType()) {
92 EXPECT_EQ(expected_code_, code);
Amin Hassanid3f4bea2018-04-30 14:52:40 -070093 p2p_file_id_ = static_cast<DownloadAction*>(action)->p2p_file_id();
Darin Petkovc97435c2010-07-20 12:37:43 -070094 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070095 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070096 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000097 }
98
rspangler@google.com49fdf182009-10-10 00:57:34 +000099 string path_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700100 brillo::Blob expected_data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -0700102 ErrorCode expected_code_;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700103 string p2p_file_id_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104};
105
Darin Petkov9ce452b2010-11-17 14:33:28 -0800106class TestDirectFileWriter : public DirectFileWriter {
107 public:
108 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
109 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
110
Don Garrette410e0f2011-11-10 15:39:01 -0800111 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800112 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800113 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800114 }
115 return DirectFileWriter::Write(bytes, count);
116 }
117
118 private:
119 // If positive, fail on the |fail_write_| call to Write.
120 int fail_write_;
121 int current_write_;
122};
123
Alex Deymo60ca1a72015-06-18 18:19:15 -0700124void StartProcessorInRunLoop(ActionProcessor* processor,
125 MockHttpFetcher* http_fetcher) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700127 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128}
129
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700130void TestWithData(const brillo::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800131 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700132 bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700133 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700134 loop.SetAsCurrent();
Alex Deymo5ed695e2015-10-05 16:59:23 -0700135 FakeSystemState fake_system_state;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136
137 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700138 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800139 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800140 EXPECT_EQ(
141 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800142 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700143
Sen Jiang5ae865b2017-04-18 14:24:40 -0700144 uint64_t size = data.size() - 1;
Alex Deymo64d98782016-02-05 18:03:48 -0800145 InstallPlan install_plan;
Sen Jiangcdd52062017-05-18 15:33:10 -0700146 install_plan.payloads.push_back(
147 {.size = size, .type = InstallPayloadType::kDelta});
Sen Jiang2703ef42017-03-16 13:36:21 -0700148 // We pull off the first byte from data and seek past it.
149 EXPECT_TRUE(HashCalculator::RawHashOfBytes(
Sen Jiang0affc2c2017-02-10 15:55:05 -0800150 &data[1], data.size() - 1, &install_plan.payloads[0].hash));
Alex Deymo5ed695e2015-10-05 16:59:23 -0700151 install_plan.source_slot = 0;
152 install_plan.target_slot = 1;
153 // We mark both slots as bootable. Only the target slot should be unbootable
154 // after the download starts.
155 fake_system_state.fake_boot_control()->SetSlotBootable(
156 install_plan.source_slot, true);
157 fake_system_state.fake_boot_control()->SetSlotBootable(
158 install_plan.target_slot, true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700159 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
160 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800161 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800162 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800163 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700164 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700165 // takes ownership of passed in HttpFetcher
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700166 auto download_action =
167 std::make_unique<DownloadAction>(&prefs,
168 fake_system_state.boot_control(),
169 fake_system_state.hardware(),
170 &fake_system_state,
171 http_fetcher,
172 false /* interactive */);
173 download_action->SetTestFileWriter(&writer);
174 BondActions(feeder_action.get(), download_action.get());
Alex Deymo542c19b2015-12-03 07:43:31 -0300175 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700176 if (use_download_delegate) {
177 InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700178 download_action->set_delegate(&download_delegate);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700179 if (data.size() > kMockHttpFetcherChunkSize)
180 EXPECT_CALL(download_delegate,
Sen Jiang5ae865b2017-04-18 14:24:40 -0700181 BytesReceived(_, kMockHttpFetcherChunkSize, _));
Alex Deymo542c19b2015-12-03 07:43:31 -0300182 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(AtLeast(1));
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700183 EXPECT_CALL(download_delegate, DownloadComplete())
184 .Times(fail_write == 0 ? 1 : 0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700185 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700186 DownloadActionTestProcessorDelegate delegate;
187 delegate.expected_code_ =
188 (fail_write > 0) ? ErrorCode::kDownloadWriteError : ErrorCode::kSuccess;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700189 delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800190 delegate.path_ = output_temp_file.path();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191 ActionProcessor processor;
192 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700193 processor.EnqueueAction(std::move(feeder_action));
194 processor.EnqueueAction(std::move(download_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000195
Alex Deymo60ca1a72015-06-18 18:19:15 -0700196 loop.PostTask(FROM_HERE,
197 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
198 loop.Run();
199 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo5ed695e2015-10-05 16:59:23 -0700200
201 EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable(
202 install_plan.source_slot));
203 EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable(
204 install_plan.target_slot));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000205}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700206} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000207
208TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700209 brillo::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000210 const char* foo = "foo";
211 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700212 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800213 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700214 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000215}
216
217TEST(DownloadActionTest, LargeTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700218 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000219 char c = '0';
220 for (unsigned int i = 0; i < big.size(); i++) {
221 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800222 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000223 }
Darin Petkov50332f12010-09-24 11:44:47 -0700224 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800225 0, // fail_write
226 true); // use_download_delegate
227}
228
229TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700230 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800231 char c = '0';
232 for (unsigned int i = 0; i < big.size(); i++) {
233 big[i] = c;
234 c = ('9' == c) ? '0' : c + 1;
235 }
236 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800237 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700238 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700239}
240
Darin Petkov9d911fa2010-08-19 09:36:08 -0700241TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700242 brillo::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700243 const char* foo = "foofoo";
244 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700245 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800246 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700247 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000248}
249
Aaron Wood9321f502017-09-07 11:18:54 -0700250TEST(DownloadActionTest, MultiPayloadProgressTest) {
251 std::vector<brillo::Blob> payload_datas;
252 // the first payload must be the largest, as it's the actual payload used by
253 // the MockHttpFetcher for all downloaded data.
254 payload_datas.emplace_back(4 * kMockHttpFetcherChunkSize + 256);
255 payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize);
256 brillo::FakeMessageLoop loop(nullptr);
257 loop.SetAsCurrent();
258 FakeSystemState fake_system_state;
259 EXPECT_CALL(*fake_system_state.mock_payload_state(), NextPayload())
260 .WillOnce(Return(true));
261
262 MockFileWriter mock_file_writer;
263 EXPECT_CALL(mock_file_writer, Close()).WillRepeatedly(Return(0));
264 EXPECT_CALL(mock_file_writer, Write(_, _, _))
265 .WillRepeatedly(
266 DoAll(SetArgPointee<2>(ErrorCode::kSuccess), Return(true)));
267
268 InstallPlan install_plan;
269 uint64_t total_expected_download_size{0};
270 for (const auto& data : payload_datas) {
271 uint64_t size = data.size();
272 install_plan.payloads.push_back(
273 {.size = size, .type = InstallPayloadType::kFull});
274 total_expected_download_size += size;
275 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700276 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
277 feeder_action->set_obj(install_plan);
Aaron Wood9321f502017-09-07 11:18:54 -0700278 MockPrefs prefs;
279 MockHttpFetcher* http_fetcher = new MockHttpFetcher(
280 payload_datas[0].data(), payload_datas[0].size(), nullptr);
281 // takes ownership of passed in HttpFetcher
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700282 auto download_action =
283 std::make_unique<DownloadAction>(&prefs,
284 fake_system_state.boot_control(),
285 fake_system_state.hardware(),
286 &fake_system_state,
287 http_fetcher,
288 false /* interactive */);
289 download_action->SetTestFileWriter(&mock_file_writer);
290 BondActions(feeder_action.get(), download_action.get());
Aaron Wood9321f502017-09-07 11:18:54 -0700291 MockDownloadActionDelegate download_delegate;
292 {
293 InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700294 download_action->set_delegate(&download_delegate);
Aaron Wood9321f502017-09-07 11:18:54 -0700295 // these are hand-computed based on the payloads specified above
296 EXPECT_CALL(download_delegate,
297 BytesReceived(kMockHttpFetcherChunkSize,
298 kMockHttpFetcherChunkSize,
299 total_expected_download_size));
300 EXPECT_CALL(download_delegate,
301 BytesReceived(kMockHttpFetcherChunkSize,
302 kMockHttpFetcherChunkSize * 2,
303 total_expected_download_size));
304 EXPECT_CALL(download_delegate,
305 BytesReceived(kMockHttpFetcherChunkSize,
306 kMockHttpFetcherChunkSize * 3,
307 total_expected_download_size));
308 EXPECT_CALL(download_delegate,
309 BytesReceived(kMockHttpFetcherChunkSize,
310 kMockHttpFetcherChunkSize * 4,
311 total_expected_download_size));
312 EXPECT_CALL(download_delegate,
313 BytesReceived(256,
314 kMockHttpFetcherChunkSize * 4 + 256,
315 total_expected_download_size));
316 EXPECT_CALL(download_delegate,
317 BytesReceived(kMockHttpFetcherChunkSize,
318 kMockHttpFetcherChunkSize * 5 + 256,
319 total_expected_download_size));
320 EXPECT_CALL(download_delegate,
321 BytesReceived(kMockHttpFetcherChunkSize,
322 total_expected_download_size,
323 total_expected_download_size));
324 }
325 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700326 processor.EnqueueAction(std::move(feeder_action));
327 processor.EnqueueAction(std::move(download_action));
Aaron Wood9321f502017-09-07 11:18:54 -0700328
329 loop.PostTask(
330 FROM_HERE,
331 base::Bind(
332 [](ActionProcessor* processor) { processor->StartProcessing(); },
333 base::Unretained(&processor)));
334 loop.Run();
335 EXPECT_FALSE(loop.PendingTasks());
336}
337
rspangler@google.com49fdf182009-10-10 00:57:34 +0000338namespace {
339class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
340 public:
341 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700342 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000343 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000344};
345
Alex Deymo60ca1a72015-06-18 18:19:15 -0700346void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000347 processor->StartProcessing();
348 CHECK(processor->IsRunning());
349 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000350}
351
Darin Petkov9d911fa2010-08-19 09:36:08 -0700352void TestTerminateEarly(bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700353 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700354 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000355
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700356 brillo::Blob data(kMockHttpFetcherChunkSize +
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800357 kMockHttpFetcherChunkSize / 2);
358 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000359
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700360 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000361 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700362 DirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800363 EXPECT_EQ(0, writer.Open(temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700364
rspangler@google.com49fdf182009-10-10 00:57:34 +0000365 // takes ownership of passed in HttpFetcher
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700366 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
Alex Deymo64d98782016-02-05 18:03:48 -0800367 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800368 install_plan.payloads.resize(1);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700369 feeder_action->set_obj(install_plan);
Alex Deymo5ed695e2015-10-05 16:59:23 -0700370 FakeSystemState fake_system_state_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800371 MockPrefs prefs;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700372 auto download_action = std::make_unique<DownloadAction>(
Alex Deymo1b3556c2016-02-03 09:54:02 -0800373 &prefs,
374 fake_system_state_.boot_control(),
375 fake_system_state_.hardware(),
376 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700377 new MockHttpFetcher(data.data(), data.size(), nullptr),
Amin Hassanied37d682018-04-06 13:22:00 -0700378 false /* interactive */);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700379 download_action->SetTestFileWriter(&writer);
Alex Deymo542c19b2015-12-03 07:43:31 -0300380 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700381 if (use_download_delegate) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700382 download_action->set_delegate(&download_delegate);
Alex Deymo542c19b2015-12-03 07:43:31 -0300383 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700384 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000385 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000386 ActionProcessor processor;
387 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700388 BondActions(feeder_action.get(), download_action.get());
389 processor.EnqueueAction(std::move(feeder_action));
390 processor.EnqueueAction(std::move(download_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000391
Alex Deymo60ca1a72015-06-18 18:19:15 -0700392 loop.PostTask(FROM_HERE,
393 base::Bind(&TerminateEarlyTestStarter, &processor));
394 loop.Run();
395 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000396 }
397
398 // 1 or 0 chunks should have come through
Alex Deymobffa0602016-02-12 17:16:29 -0800399 const off_t resulting_file_size(utils::FileSize(temp_file.path()));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700400 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000401 if (resulting_file_size != 0)
Alex Deymo5fe0c4e2016-02-16 18:46:24 -0800402 EXPECT_EQ(kMockHttpFetcherChunkSize,
403 static_cast<size_t>(resulting_file_size));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000404}
405
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700406} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700407
408TEST(DownloadActionTest, TerminateEarlyTest) {
409 TestTerminateEarly(true);
410}
411
412TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
413 TestTerminateEarly(false);
414}
415
rspangler@google.com49fdf182009-10-10 00:57:34 +0000416class DownloadActionTestAction;
417
418template<>
419class ActionTraits<DownloadActionTestAction> {
420 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000421 typedef InstallPlan OutputObjectType;
422 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000423};
424
425// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700426class DownloadActionTestAction : public Action<DownloadActionTestAction> {
427 public:
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700428 DownloadActionTestAction() = default;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000429 typedef InstallPlan InputObjectType;
430 typedef InstallPlan OutputObjectType;
431 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
432 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000433 ActionProcessor* processor() { return processor_; }
434 void PerformAction() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000435 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000436 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000437 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700438 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000439 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700440 static std::string StaticType() { return "DownloadActionTestAction"; }
441 string Type() const { return StaticType(); }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000442 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000443};
444
445namespace {
446// This class is an ActionProcessorDelegate that simply terminates the
447// run loop when the ActionProcessor has completed processing. It's used
448// only by the test PassObjectOutTest.
449class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
450 public:
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700451 void ProcessingDone(const ActionProcessor* processor,
452 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700453 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000454 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700455 void ActionCompleted(ActionProcessor* processor,
456 AbstractAction* action,
457 ErrorCode code) override {
458 if (action->Type() == DownloadActionTestAction::StaticType()) {
459 did_test_action_run_ = true;
460 }
461 }
462
463 bool did_test_action_run_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000464};
465
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700466} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000467
468TEST(DownloadActionTest, PassObjectOutTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700469 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700470 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000471
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700472 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700473 EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700474
rspangler@google.com49fdf182009-10-10 00:57:34 +0000475 // takes ownership of passed in HttpFetcher
Alex Deymo64d98782016-02-05 18:03:48 -0800476 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800477 install_plan.payloads.push_back({.size = 1});
478 EXPECT_TRUE(
479 HashCalculator::RawHashOfData({'x'}, &install_plan.payloads[0].hash));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700480 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
481 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800482 MockPrefs prefs;
Alex Deymo5ed695e2015-10-05 16:59:23 -0700483 FakeSystemState fake_system_state_;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700484 auto download_action =
485 std::make_unique<DownloadAction>(&prefs,
486 fake_system_state_.boot_control(),
487 fake_system_state_.hardware(),
488 &fake_system_state_,
489 new MockHttpFetcher("x", 1, nullptr),
490 false /* interactive */);
491 download_action->SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000492
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700493 auto test_action = std::make_unique<DownloadActionTestAction>();
494 test_action->expected_input_object_ = install_plan;
495 BondActions(feeder_action.get(), download_action.get());
496 BondActions(download_action.get(), test_action.get());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000497
498 ActionProcessor processor;
499 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000500 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700501 processor.EnqueueAction(std::move(feeder_action));
502 processor.EnqueueAction(std::move(download_action));
503 processor.EnqueueAction(std::move(test_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000504
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700505 loop.PostTask(
506 FROM_HERE,
507 base::Bind(
508 [](ActionProcessor* processor) { processor->StartProcessing(); },
509 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700510 loop.Run();
511 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000512
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700513 EXPECT_EQ(true, delegate.did_test_action_run_);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000514}
515
David Zeuthen8f191b22013-08-06 12:27:50 -0700516// Test fixture for P2P tests.
517class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700518 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700519 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700520 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700521 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700522
Alex Deymo610277e2014-11-11 21:18:11 -0800523 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700524
525 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800526 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700527 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700528 }
529
530 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800531 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700532 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700533 }
534
535 // To be called by tests to setup the download. The
536 // |starting_offset| parameter is for where to resume.
537 void SetupDownload(off_t starting_offset) {
538 start_at_offset_ = starting_offset;
539 // Prepare data 10 kB of data.
540 data_.clear();
541 for (unsigned int i = 0; i < 10 * 1000; i++)
542 data_ += 'a' + (i % 25);
543
544 // Setup p2p.
545 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500546 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700547 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500548 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700549 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700550 }
551
552 // To be called by tests to perform the download. The
553 // |use_p2p_to_share| parameter is used to indicate whether the
554 // payload should be shared via p2p.
555 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700556 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
557 GetUsingP2PForSharing())
558 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700559
560 ScopedTempFile output_temp_file;
561 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800562 EXPECT_EQ(
563 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Alex Deymo64d98782016-02-05 18:03:48 -0800564 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800565 install_plan.payloads.push_back(
566 {.size = data_.length(),
567 .hash = {'1', '2', '3', '4', 'h', 'a', 's', 'h'}});
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700568 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
569 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800570 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700571 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700572 auto download_action = std::make_unique<DownloadAction>(
573 &prefs,
574 fake_system_state_.boot_control(),
575 fake_system_state_.hardware(),
576 &fake_system_state_,
577 new MockHttpFetcher(data_.c_str(), data_.length(), nullptr),
578 false /* interactive */);
579 auto http_fetcher = download_action->http_fetcher();
580 download_action->SetTestFileWriter(&writer);
581 BondActions(feeder_action.get(), download_action.get());
582 delegate_.expected_data_ =
583 brillo::Blob(data_.begin() + start_at_offset_, data_.end());
584 delegate_.path_ = output_temp_file.path();
585 processor_.set_delegate(&delegate_);
586 processor_.EnqueueAction(std::move(feeder_action));
587 processor_.EnqueueAction(std::move(download_action));
David Zeuthen8f191b22013-08-06 12:27:50 -0700588
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700589 loop_.PostTask(
590 FROM_HERE,
591 base::Bind(
592 [](P2PDownloadActionTest* action_test, HttpFetcher* http_fetcher) {
593 action_test->processor_.StartProcessing();
594 http_fetcher->SetOffset(action_test->start_at_offset_);
595 },
596 base::Unretained(this),
597 base::Unretained(http_fetcher)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700598 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700599 }
600
Alex Deymo60ca1a72015-06-18 18:19:15 -0700601 // Mainloop used to make StartDownload() synchronous.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700602 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700603
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700604 // Delegate that is passed to the ActionProcessor.
605 DownloadActionTestProcessorDelegate delegate_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700606
607 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700608 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700609
610 // The ActionProcessor used for running the actions.
611 ActionProcessor processor_;
612
613 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700614 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700615
616 // The data being downloaded.
617 string data_;
618
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700619 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700620 // The requested starting offset passed to SetupDownload().
621 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700622
623 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700624};
625
626TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800627 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700628 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
629 << "Please update your system to support this feature.";
630 return;
631 }
632
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700633 SetupDownload(0); // starting_offset
634 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700635
636 // Check the p2p file and its content matches what was sent.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700637 string file_id = delegate_.p2p_file_id_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700638 EXPECT_NE("", file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800639 EXPECT_EQ(static_cast<int>(data_.length()),
640 p2p_manager_->FileGetSize(file_id));
641 EXPECT_EQ(static_cast<int>(data_.length()),
642 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700643 string p2p_file_contents;
644 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
645 &p2p_file_contents));
646 EXPECT_EQ(data_, p2p_file_contents);
647}
648
649TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800650 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700651 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
652 << "Please update your system to support this feature.";
653 return;
654 }
655
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700656 SetupDownload(1000); // starting_offset
657 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700658
659 // DownloadAction should convey that the file is not being shared.
660 // and that we don't have any p2p files.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700661 EXPECT_EQ(delegate_.p2p_file_id_, "");
David Zeuthen8f191b22013-08-06 12:27:50 -0700662 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
663}
664
665TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800666 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700667 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
668 << "Please update your system to support this feature.";
669 return;
670 }
671
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700672 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700673
674 // Prepare the file with existing data before starting to write to
675 // it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700676 string file_id = utils::CalculateP2PFileId(
677 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700678 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
679 string existing_data;
680 for (unsigned int i = 0; i < 1000; i++)
681 existing_data += '0' + (i % 10);
682 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
683 1000), 1000);
684
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700685 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700686
687 // DownloadAction should convey the same file_id and the file should
688 // have the expected size.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700689 EXPECT_EQ(delegate_.p2p_file_id_, file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800690 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
691 p2p_manager_->FileGetSize(file_id));
692 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
693 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700694 string p2p_file_contents;
695 // Check that the first 1000 bytes wasn't touched and that we
696 // appended the remaining as appropriate.
697 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
698 &p2p_file_contents));
699 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
700 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
701}
702
David Zeuthen8f191b22013-08-06 12:27:50 -0700703TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800704 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700705 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
706 << "Please update your system to support this feature.";
707 return;
708 }
709
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700710 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700711
712 // Prepare the file with all existing data before starting to write
713 // to it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700714 string file_id = utils::CalculateP2PFileId(
715 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700716 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
717 string existing_data;
718 for (unsigned int i = 0; i < 1000; i++)
719 existing_data += '0' + (i % 10);
720 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
721 1000), 1000);
722
723 // Check that the file is there.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800724 EXPECT_EQ(1000, p2p_manager_->FileGetSize(file_id));
725 EXPECT_EQ(1, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700726
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700727 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700728
729 // DownloadAction should have deleted the p2p file. Check that it's gone.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800730 EXPECT_EQ(-1, p2p_manager_->FileGetSize(file_id));
731 EXPECT_EQ(0, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700732}
733
rspangler@google.com49fdf182009-10-10 00:57:34 +0000734} // namespace chromeos_update_engine