blob: a3ad5b962bb5bd1d0f790a88c58e026e93df03ed [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:
David Zeuthena99981f2013-04-29 13:42:47 -070068 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Deymo60ca1a72015-06-18 18:19:15 -070069 : processing_done_called_(false),
Darin Petkovc97435c2010-07-20 12:37:43 -070070 expected_code_(expected_code) {}
Alex Deymo610277e2014-11-11 21:18:11 -080071 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000072 EXPECT_TRUE(processing_done_called_);
73 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080074 void ProcessingDone(const ActionProcessor* processor,
75 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070076 brillo::MessageLoop::current()->BreakLoop();
77 brillo::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000078 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070079 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080080 ASSERT_EQ(expected_data_.size(), found_data.size());
81 for (unsigned i = 0; i < expected_data_.size(); i++) {
82 EXPECT_EQ(expected_data_[i], found_data[i]);
83 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 }
85 processing_done_called_ = true;
86 }
87
Yunlian Jiang35866ed2015-01-29 13:09:20 -080088 void ActionCompleted(ActionProcessor* processor,
89 AbstractAction* action,
90 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070091 const string type = action->Type();
92 if (type == DownloadAction::StaticType()) {
93 EXPECT_EQ(expected_code_, code);
94 } 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_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000103};
104
Darin Petkov9ce452b2010-11-17 14:33:28 -0800105class TestDirectFileWriter : public DirectFileWriter {
106 public:
107 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
108 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
109
Don Garrette410e0f2011-11-10 15:39:01 -0800110 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800111 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800112 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800113 }
114 return DirectFileWriter::Write(bytes, count);
115 }
116
117 private:
118 // If positive, fail on the |fail_write_| call to Write.
119 int fail_write_;
120 int current_write_;
121};
122
Alex Deymo60ca1a72015-06-18 18:19:15 -0700123void StartProcessorInRunLoop(ActionProcessor* processor,
124 MockHttpFetcher* http_fetcher) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700126 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127}
128
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700129void TestWithData(const brillo::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800130 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700131 bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700132 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700133 loop.SetAsCurrent();
Alex Deymo5ed695e2015-10-05 16:59:23 -0700134 FakeSystemState fake_system_state;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135
136 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700137 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800138 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800139 EXPECT_EQ(
140 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800141 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700142
Sen Jiang5ae865b2017-04-18 14:24:40 -0700143 uint64_t size = data.size() - 1;
Alex Deymo64d98782016-02-05 18:03:48 -0800144 InstallPlan install_plan;
Sen Jiangcdd52062017-05-18 15:33:10 -0700145 install_plan.payloads.push_back(
146 {.size = size, .type = InstallPayloadType::kDelta});
Sen Jiang2703ef42017-03-16 13:36:21 -0700147 // We pull off the first byte from data and seek past it.
148 EXPECT_TRUE(HashCalculator::RawHashOfBytes(
Sen Jiang0affc2c2017-02-10 15:55:05 -0800149 &data[1], data.size() - 1, &install_plan.payloads[0].hash));
Alex Deymo5ed695e2015-10-05 16:59:23 -0700150 install_plan.source_slot = 0;
151 install_plan.target_slot = 1;
152 // We mark both slots as bootable. Only the target slot should be unbootable
153 // after the download starts.
154 fake_system_state.fake_boot_control()->SetSlotBootable(
155 install_plan.source_slot, true);
156 fake_system_state.fake_boot_control()->SetSlotBootable(
157 install_plan.target_slot, true);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000158 ObjectFeederAction<InstallPlan> feeder_action;
159 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800160 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800161 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800162 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700163 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700164 // takes ownership of passed in HttpFetcher
Alex Deymo1b3556c2016-02-03 09:54:02 -0800165 DownloadAction download_action(&prefs,
166 fake_system_state.boot_control(),
167 fake_system_state.hardware(),
168 &fake_system_state,
Amin Hassani7ecda262017-07-11 17:10:50 -0700169 http_fetcher,
Amin Hassanied37d682018-04-06 13:22:00 -0700170 false /* interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700171 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000172 BondActions(&feeder_action, &download_action);
Alex Deymo542c19b2015-12-03 07:43:31 -0300173 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700174 if (use_download_delegate) {
175 InSequence s;
176 download_action.set_delegate(&download_delegate);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700177 if (data.size() > kMockHttpFetcherChunkSize)
178 EXPECT_CALL(download_delegate,
Sen Jiang5ae865b2017-04-18 14:24:40 -0700179 BytesReceived(_, kMockHttpFetcherChunkSize, _));
Alex Deymo542c19b2015-12-03 07:43:31 -0300180 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(AtLeast(1));
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700181 EXPECT_CALL(download_delegate, DownloadComplete())
182 .Times(fail_write == 0 ? 1 : 0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700183 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700184 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700185 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700186 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700187 DownloadActionTestProcessorDelegate delegate(expected_code);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700188 delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800189 delegate.path_ = output_temp_file.path();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000190 ActionProcessor processor;
191 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000192 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193 processor.EnqueueAction(&download_action);
194
Alex Deymo60ca1a72015-06-18 18:19:15 -0700195 loop.PostTask(FROM_HERE,
196 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
197 loop.Run();
198 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo5ed695e2015-10-05 16:59:23 -0700199
200 EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable(
201 install_plan.source_slot));
202 EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable(
203 install_plan.target_slot));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000204}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700205} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000206
207TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700208 brillo::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000209 const char* foo = "foo";
210 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700211 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800212 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700213 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000214}
215
216TEST(DownloadActionTest, LargeTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700217 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000218 char c = '0';
219 for (unsigned int i = 0; i < big.size(); i++) {
220 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800221 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000222 }
Darin Petkov50332f12010-09-24 11:44:47 -0700223 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800224 0, // fail_write
225 true); // use_download_delegate
226}
227
228TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700229 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800230 char c = '0';
231 for (unsigned int i = 0; i < big.size(); i++) {
232 big[i] = c;
233 c = ('9' == c) ? '0' : c + 1;
234 }
235 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800236 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700237 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700238}
239
Darin Petkov9d911fa2010-08-19 09:36:08 -0700240TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700241 brillo::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700242 const char* foo = "foofoo";
243 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700244 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800245 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700246 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000247}
248
Aaron Wood9321f502017-09-07 11:18:54 -0700249TEST(DownloadActionTest, MultiPayloadProgressTest) {
250 std::vector<brillo::Blob> payload_datas;
251 // the first payload must be the largest, as it's the actual payload used by
252 // the MockHttpFetcher for all downloaded data.
253 payload_datas.emplace_back(4 * kMockHttpFetcherChunkSize + 256);
254 payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize);
255 brillo::FakeMessageLoop loop(nullptr);
256 loop.SetAsCurrent();
257 FakeSystemState fake_system_state;
258 EXPECT_CALL(*fake_system_state.mock_payload_state(), NextPayload())
259 .WillOnce(Return(true));
260
261 MockFileWriter mock_file_writer;
262 EXPECT_CALL(mock_file_writer, Close()).WillRepeatedly(Return(0));
263 EXPECT_CALL(mock_file_writer, Write(_, _, _))
264 .WillRepeatedly(
265 DoAll(SetArgPointee<2>(ErrorCode::kSuccess), Return(true)));
266
267 InstallPlan install_plan;
268 uint64_t total_expected_download_size{0};
269 for (const auto& data : payload_datas) {
270 uint64_t size = data.size();
271 install_plan.payloads.push_back(
272 {.size = size, .type = InstallPayloadType::kFull});
273 total_expected_download_size += size;
274 }
275 ObjectFeederAction<InstallPlan> feeder_action;
276 feeder_action.set_obj(install_plan);
277 MockPrefs prefs;
278 MockHttpFetcher* http_fetcher = new MockHttpFetcher(
279 payload_datas[0].data(), payload_datas[0].size(), nullptr);
280 // takes ownership of passed in HttpFetcher
281 DownloadAction download_action(&prefs,
282 fake_system_state.boot_control(),
283 fake_system_state.hardware(),
284 &fake_system_state,
Sen Jiang18414082018-01-11 14:50:36 -0800285 http_fetcher,
Amin Hassanied37d682018-04-06 13:22:00 -0700286 false /* interactive */);
Aaron Wood9321f502017-09-07 11:18:54 -0700287 download_action.SetTestFileWriter(&mock_file_writer);
288 BondActions(&feeder_action, &download_action);
289 MockDownloadActionDelegate download_delegate;
290 {
291 InSequence s;
292 download_action.set_delegate(&download_delegate);
293 // these are hand-computed based on the payloads specified above
294 EXPECT_CALL(download_delegate,
295 BytesReceived(kMockHttpFetcherChunkSize,
296 kMockHttpFetcherChunkSize,
297 total_expected_download_size));
298 EXPECT_CALL(download_delegate,
299 BytesReceived(kMockHttpFetcherChunkSize,
300 kMockHttpFetcherChunkSize * 2,
301 total_expected_download_size));
302 EXPECT_CALL(download_delegate,
303 BytesReceived(kMockHttpFetcherChunkSize,
304 kMockHttpFetcherChunkSize * 3,
305 total_expected_download_size));
306 EXPECT_CALL(download_delegate,
307 BytesReceived(kMockHttpFetcherChunkSize,
308 kMockHttpFetcherChunkSize * 4,
309 total_expected_download_size));
310 EXPECT_CALL(download_delegate,
311 BytesReceived(256,
312 kMockHttpFetcherChunkSize * 4 + 256,
313 total_expected_download_size));
314 EXPECT_CALL(download_delegate,
315 BytesReceived(kMockHttpFetcherChunkSize,
316 kMockHttpFetcherChunkSize * 5 + 256,
317 total_expected_download_size));
318 EXPECT_CALL(download_delegate,
319 BytesReceived(kMockHttpFetcherChunkSize,
320 total_expected_download_size,
321 total_expected_download_size));
322 }
323 ActionProcessor processor;
324 processor.EnqueueAction(&feeder_action);
325 processor.EnqueueAction(&download_action);
326
327 loop.PostTask(
328 FROM_HERE,
329 base::Bind(
330 [](ActionProcessor* processor) { processor->StartProcessing(); },
331 base::Unretained(&processor)));
332 loop.Run();
333 EXPECT_FALSE(loop.PendingTasks());
334}
335
rspangler@google.com49fdf182009-10-10 00:57:34 +0000336namespace {
337class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
338 public:
339 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700340 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000342};
343
Alex Deymo60ca1a72015-06-18 18:19:15 -0700344void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000345 processor->StartProcessing();
346 CHECK(processor->IsRunning());
347 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000348}
349
Darin Petkov9d911fa2010-08-19 09:36:08 -0700350void TestTerminateEarly(bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700351 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700352 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000353
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700354 brillo::Blob data(kMockHttpFetcherChunkSize +
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800355 kMockHttpFetcherChunkSize / 2);
356 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000357
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700358 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000359 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700360 DirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800361 EXPECT_EQ(0, writer.Open(temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700362
rspangler@google.com49fdf182009-10-10 00:57:34 +0000363 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000364 ObjectFeederAction<InstallPlan> feeder_action;
Alex Deymo64d98782016-02-05 18:03:48 -0800365 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800366 install_plan.payloads.resize(1);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000367 feeder_action.set_obj(install_plan);
Alex Deymo5ed695e2015-10-05 16:59:23 -0700368 FakeSystemState fake_system_state_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800369 MockPrefs prefs;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800370 DownloadAction download_action(
371 &prefs,
372 fake_system_state_.boot_control(),
373 fake_system_state_.hardware(),
374 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700375 new MockHttpFetcher(data.data(), data.size(), nullptr),
Amin Hassanied37d682018-04-06 13:22:00 -0700376 false /* interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700377 download_action.SetTestFileWriter(&writer);
Alex Deymo542c19b2015-12-03 07:43:31 -0300378 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700379 if (use_download_delegate) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700380 download_action.set_delegate(&download_delegate);
Alex Deymo542c19b2015-12-03 07:43:31 -0300381 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700382 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000383 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000384 ActionProcessor processor;
385 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000386 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000387 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000388 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000389
Alex Deymo60ca1a72015-06-18 18:19:15 -0700390 loop.PostTask(FROM_HERE,
391 base::Bind(&TerminateEarlyTestStarter, &processor));
392 loop.Run();
393 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000394 }
395
396 // 1 or 0 chunks should have come through
Alex Deymobffa0602016-02-12 17:16:29 -0800397 const off_t resulting_file_size(utils::FileSize(temp_file.path()));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700398 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000399 if (resulting_file_size != 0)
Alex Deymo5fe0c4e2016-02-16 18:46:24 -0800400 EXPECT_EQ(kMockHttpFetcherChunkSize,
401 static_cast<size_t>(resulting_file_size));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000402}
403
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700404} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700405
406TEST(DownloadActionTest, TerminateEarlyTest) {
407 TestTerminateEarly(true);
408}
409
410TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
411 TestTerminateEarly(false);
412}
413
rspangler@google.com49fdf182009-10-10 00:57:34 +0000414class DownloadActionTestAction;
415
416template<>
417class ActionTraits<DownloadActionTestAction> {
418 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000419 typedef InstallPlan OutputObjectType;
420 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000421};
422
423// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700424class DownloadActionTestAction : public Action<DownloadActionTestAction> {
425 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000426 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000427 typedef InstallPlan InputObjectType;
428 typedef InstallPlan OutputObjectType;
429 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
430 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000431 ActionProcessor* processor() { return processor_; }
432 void PerformAction() {
433 did_run_ = true;
434 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000435 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000436 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700437 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000438 }
439 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000440 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000441 bool did_run_;
442};
443
444namespace {
445// This class is an ActionProcessorDelegate that simply terminates the
446// run loop when the ActionProcessor has completed processing. It's used
447// only by the test PassObjectOutTest.
448class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
449 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700450 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700451 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000452 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000453};
454
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700455} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000456
457TEST(DownloadActionTest, PassObjectOutTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700458 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700459 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000460
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700461 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700462 EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700463
rspangler@google.com49fdf182009-10-10 00:57:34 +0000464 // takes ownership of passed in HttpFetcher
Alex Deymo64d98782016-02-05 18:03:48 -0800465 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800466 install_plan.payloads.push_back({.size = 1});
467 EXPECT_TRUE(
468 HashCalculator::RawHashOfData({'x'}, &install_plan.payloads[0].hash));
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000469 ObjectFeederAction<InstallPlan> feeder_action;
470 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800471 MockPrefs prefs;
Alex Deymo5ed695e2015-10-05 16:59:23 -0700472 FakeSystemState fake_system_state_;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800473 DownloadAction download_action(&prefs,
474 fake_system_state_.boot_control(),
475 fake_system_state_.hardware(),
476 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700477 new MockHttpFetcher("x", 1, nullptr),
Amin Hassanied37d682018-04-06 13:22:00 -0700478 false /* interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700479 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000480
481 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000482 test_action.expected_input_object_ = install_plan;
483 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000484 BondActions(&download_action, &test_action);
485
486 ActionProcessor processor;
487 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000488 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000489 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000490 processor.EnqueueAction(&download_action);
491 processor.EnqueueAction(&test_action);
492
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700493 loop.PostTask(
494 FROM_HERE,
495 base::Bind(
496 [](ActionProcessor* processor) { processor->StartProcessing(); },
497 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700498 loop.Run();
499 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000500
501 EXPECT_EQ(true, test_action.did_run_);
502}
503
David Zeuthen8f191b22013-08-06 12:27:50 -0700504// Test fixture for P2P tests.
505class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700506 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700507 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700508 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700509 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700510
Alex Deymo610277e2014-11-11 21:18:11 -0800511 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700512
513 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800514 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700515 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700516 }
517
518 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800519 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700520 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700521 }
522
523 // To be called by tests to setup the download. The
524 // |starting_offset| parameter is for where to resume.
525 void SetupDownload(off_t starting_offset) {
526 start_at_offset_ = starting_offset;
527 // Prepare data 10 kB of data.
528 data_.clear();
529 for (unsigned int i = 0; i < 10 * 1000; i++)
530 data_ += 'a' + (i % 25);
531
532 // Setup p2p.
533 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500534 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700535 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500536 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700537 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700538 }
539
540 // To be called by tests to perform the download. The
541 // |use_p2p_to_share| parameter is used to indicate whether the
542 // payload should be shared via p2p.
543 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700544 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
545 GetUsingP2PForSharing())
546 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700547
548 ScopedTempFile output_temp_file;
549 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800550 EXPECT_EQ(
551 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Alex Deymo64d98782016-02-05 18:03:48 -0800552 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800553 install_plan.payloads.push_back(
554 {.size = data_.length(),
555 .hash = {'1', '2', '3', '4', 'h', 'a', 's', 'h'}});
David Zeuthen8f191b22013-08-06 12:27:50 -0700556 ObjectFeederAction<InstallPlan> feeder_action;
557 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800558 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700559 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
560 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700561 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700562 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Alex Deymo1b3556c2016-02-03 09:54:02 -0800563 download_action_.reset(new DownloadAction(&prefs,
564 fake_system_state_.boot_control(),
565 fake_system_state_.hardware(),
566 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700567 http_fetcher_,
Amin Hassanied37d682018-04-06 13:22:00 -0700568 false /* interactive */));
David Zeuthen8f191b22013-08-06 12:27:50 -0700569 download_action_->SetTestFileWriter(&writer);
570 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700571 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700572 delegate.expected_data_ = brillo::Blob(data_.begin() + start_at_offset_,
573 data_.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800574 delegate.path_ = output_temp_file.path();
David Zeuthen8f191b22013-08-06 12:27:50 -0700575 processor_.set_delegate(&delegate);
576 processor_.EnqueueAction(&feeder_action);
577 processor_.EnqueueAction(download_action_.get());
578
Alex Deymo60ca1a72015-06-18 18:19:15 -0700579 loop_.PostTask(FROM_HERE, base::Bind(
580 &P2PDownloadActionTest::StartProcessorInRunLoopForP2P,
581 base::Unretained(this)));
582 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700583 }
584
Alex Deymo60ca1a72015-06-18 18:19:15 -0700585 // Mainloop used to make StartDownload() synchronous.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700586 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700587
David Zeuthen8f191b22013-08-06 12:27:50 -0700588 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700589 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700590
591 // The HttpFetcher used in the test.
592 MockHttpFetcher* http_fetcher_;
593
594 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700595 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700596
597 // The ActionProcessor used for running the actions.
598 ActionProcessor processor_;
599
600 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700601 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700602
603 // The data being downloaded.
604 string data_;
605
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700606 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700607 // Callback used in StartDownload() method.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700608 void StartProcessorInRunLoopForP2P() {
609 processor_.StartProcessing();
Sen Jiangba2213a2018-02-27 16:28:20 -0800610 download_action_->http_fetcher()->SetOffset(start_at_offset_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700611 }
612
David Zeuthen8f191b22013-08-06 12:27:50 -0700613 // The requested starting offset passed to SetupDownload().
614 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700615
616 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700617};
618
619TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800620 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700621 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
622 << "Please update your system to support this feature.";
623 return;
624 }
625
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700626 SetupDownload(0); // starting_offset
627 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700628
629 // Check the p2p file and its content matches what was sent.
630 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700631 EXPECT_NE("", file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800632 EXPECT_EQ(static_cast<int>(data_.length()),
633 p2p_manager_->FileGetSize(file_id));
634 EXPECT_EQ(static_cast<int>(data_.length()),
635 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700636 string p2p_file_contents;
637 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
638 &p2p_file_contents));
639 EXPECT_EQ(data_, p2p_file_contents);
640}
641
642TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800643 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700644 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
645 << "Please update your system to support this feature.";
646 return;
647 }
648
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700649 SetupDownload(1000); // starting_offset
650 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700651
652 // DownloadAction should convey that the file is not being shared.
653 // and that we don't have any p2p files.
654 EXPECT_EQ(download_action_->p2p_file_id(), "");
655 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
656}
657
658TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800659 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700660 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
661 << "Please update your system to support this feature.";
662 return;
663 }
664
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700665 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700666
667 // Prepare the file with existing data before starting to write to
668 // it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700669 string file_id = utils::CalculateP2PFileId(
670 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700671 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
672 string existing_data;
673 for (unsigned int i = 0; i < 1000; i++)
674 existing_data += '0' + (i % 10);
675 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
676 1000), 1000);
677
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700678 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700679
680 // DownloadAction should convey the same file_id and the file should
681 // have the expected size.
682 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800683 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
684 p2p_manager_->FileGetSize(file_id));
685 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
686 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700687 string p2p_file_contents;
688 // Check that the first 1000 bytes wasn't touched and that we
689 // appended the remaining as appropriate.
690 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
691 &p2p_file_contents));
692 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
693 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
694}
695
David Zeuthen8f191b22013-08-06 12:27:50 -0700696TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800697 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700698 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
699 << "Please update your system to support this feature.";
700 return;
701 }
702
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700703 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700704
705 // Prepare the file with all existing data before starting to write
706 // to it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700707 string file_id = utils::CalculateP2PFileId(
708 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700709 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
710 string existing_data;
711 for (unsigned int i = 0; i < 1000; i++)
712 existing_data += '0' + (i % 10);
713 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
714 1000), 1000);
715
716 // Check that the file is there.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800717 EXPECT_EQ(1000, p2p_manager_->FileGetSize(file_id));
718 EXPECT_EQ(1, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700719
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700720 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700721
722 // DownloadAction should have deleted the p2p file. Check that it's gone.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800723 EXPECT_EQ(-1, p2p_manager_->FileGetSize(file_id));
724 EXPECT_EQ(0, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700725}
726
rspangler@google.com49fdf182009-10-10 00:57:34 +0000727} // namespace chromeos_update_engine