blob: 93c39ff47adfe3cedee6825f4716bbcbab280cec [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
Darin Petkov9d911fa2010-08-19 09:36:08 -070017#include <gmock/gmock.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000018#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -070019
Ben Chan02f7c1d2014-10-18 15:18:02 -070020#include <memory>
David Zeuthen8f191b22013-08-06 12:27:50 -070021#include <string>
22#include <utility>
Sen Jiangba2213a2018-02-27 16:28:20 -080023#include <vector>
David Zeuthen8f191b22013-08-06 12:27:50 -070024
Alex Deymo60ca1a72015-06-18 18:19:15 -070025#include <base/bind.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070027#include <base/files/file_util.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070028#include <base/location.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070029#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/message_loops/fake_message_loop.h>
31#include <brillo/message_loops/message_loop.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070032
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/action_pipe.h"
34#include "update_engine/common/hash_calculator.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070035#include "update_engine/common/mock_download_action.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/mock_http_fetcher.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/test_utils.h"
38#include "update_engine/common/utils.h"
Kelvin Zhang97cb0582020-12-02 16:42:15 -050039#include "update_engine/cros/download_action_chromeos.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070040#include "update_engine/cros/fake_p2p_manager_configuration.h"
41#include "update_engine/cros/fake_system_state.h"
42#include "update_engine/payload_consumer/mock_file_writer.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070043#include "update_engine/update_manager/fake_update_manager.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000044
45namespace chromeos_update_engine {
46
Alex Deymof329b932014-10-30 01:37:48 -070047using base::FilePath;
48using base::ReadFileToString;
49using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000050using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070051using std::unique_ptr;
Amin Hassani008c4582019-01-13 16:22:47 -080052using testing::_;
Darin Petkov9d911fa2010-08-19 09:36:08 -070053using testing::AtLeast;
54using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070055using testing::Return;
Aaron Wood9321f502017-09-07 11:18:54 -070056using testing::SetArgPointee;
rspangler@google.com49fdf182009-10-10 00:57:34 +000057
Kelvin Zhang97cb0582020-12-02 16:42:15 -050058class DownloadActionChromeosTest : public ::testing::Test {
Amin Hassani538bd592020-11-04 20:46:08 -080059 void SetUp() { FakeSystemState::CreateInstance(); }
60};
rspangler@google.com49fdf182009-10-10 00:57:34 +000061
62namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070063
rspangler@google.com49fdf182009-10-10 00:57:34 +000064class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
65 public:
Amin Hassanid3f4bea2018-04-30 14:52:40 -070066 DownloadActionTestProcessorDelegate()
67 : processing_done_called_(false), expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -080068 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 EXPECT_TRUE(processing_done_called_);
70 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080071 void ProcessingDone(const ActionProcessor* processor,
72 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070073 brillo::MessageLoop::current()->BreakLoop();
74 brillo::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000075 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070076 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080077 ASSERT_EQ(expected_data_.size(), found_data.size());
78 for (unsigned i = 0; i < expected_data_.size(); i++) {
79 EXPECT_EQ(expected_data_[i], found_data[i]);
80 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 }
82 processing_done_called_ = true;
83 }
84
Yunlian Jiang35866ed2015-01-29 13:09:20 -080085 void ActionCompleted(ActionProcessor* processor,
86 AbstractAction* action,
87 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070088 const string type = action->Type();
Kelvin Zhang97cb0582020-12-02 16:42:15 -050089 if (type == DownloadActionChromeos::StaticType()) {
Darin Petkovc97435c2010-07-20 12:37:43 -070090 EXPECT_EQ(expected_code_, code);
Kelvin Zhang97cb0582020-12-02 16:42:15 -050091 p2p_file_id_ =
92 static_cast<DownloadActionChromeos*>(action)->p2p_file_id();
Darin Petkovc97435c2010-07-20 12:37:43 -070093 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070094 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070095 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000096 }
97
rspangler@google.com49fdf182009-10-10 00:57:34 +000098 string path_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070099 brillo::Blob expected_data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -0700101 ErrorCode expected_code_;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700102 string p2p_file_id_;
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) {
Amin Hassani538bd592020-11-04 20:46:08 -0800132 FakeSystemState::CreateInstance();
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700133 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700134 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700136 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800137 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800138 EXPECT_EQ(
139 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800140 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700141
Sen Jiang5ae865b2017-04-18 14:24:40 -0700142 uint64_t size = data.size() - 1;
Alex Deymo64d98782016-02-05 18:03:48 -0800143 InstallPlan install_plan;
Sen Jiangcdd52062017-05-18 15:33:10 -0700144 install_plan.payloads.push_back(
145 {.size = size, .type = InstallPayloadType::kDelta});
Sen Jiang2703ef42017-03-16 13:36:21 -0700146 // We pull off the first byte from data and seek past it.
147 EXPECT_TRUE(HashCalculator::RawHashOfBytes(
Sen Jiang0affc2c2017-02-10 15:55:05 -0800148 &data[1], data.size() - 1, &install_plan.payloads[0].hash));
Alex Deymo5ed695e2015-10-05 16:59:23 -0700149 install_plan.source_slot = 0;
150 install_plan.target_slot = 1;
151 // We mark both slots as bootable. Only the target slot should be unbootable
152 // after the download starts.
Amin Hassani538bd592020-11-04 20:46:08 -0800153 FakeSystemState::Get()->fake_boot_control()->SetSlotBootable(
Alex Deymo5ed695e2015-10-05 16:59:23 -0700154 install_plan.source_slot, true);
Amin Hassani538bd592020-11-04 20:46:08 -0800155 FakeSystemState::Get()->fake_boot_control()->SetSlotBootable(
Alex Deymo5ed695e2015-10-05 16:59:23 -0700156 install_plan.target_slot, true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700157 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
158 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800159 MockPrefs prefs;
Amin Hassani008c4582019-01-13 16:22:47 -0800160 MockHttpFetcher* http_fetcher =
161 new MockHttpFetcher(data.data(), data.size(), nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700162 // takes ownership of passed in HttpFetcher
Kelvin Zhang97cb0582020-12-02 16:42:15 -0500163 auto download_action = std::make_unique<DownloadActionChromeos>(
164 &prefs,
165 FakeSystemState::Get()->boot_control(),
166 FakeSystemState::Get()->hardware(),
167 http_fetcher,
168 false /* interactive */);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700169 download_action->SetTestFileWriter(&writer);
170 BondActions(feeder_action.get(), download_action.get());
Alex Deymo542c19b2015-12-03 07:43:31 -0300171 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700172 if (use_download_delegate) {
173 InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700174 download_action->set_delegate(&download_delegate);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700175 if (data.size() > kMockHttpFetcherChunkSize)
176 EXPECT_CALL(download_delegate,
Sen Jiang5ae865b2017-04-18 14:24:40 -0700177 BytesReceived(_, kMockHttpFetcherChunkSize, _));
Alex Deymo542c19b2015-12-03 07:43:31 -0300178 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(AtLeast(1));
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700179 EXPECT_CALL(download_delegate, DownloadComplete())
180 .Times(fail_write == 0 ? 1 : 0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700181 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700182 DownloadActionTestProcessorDelegate delegate;
183 delegate.expected_code_ =
184 (fail_write > 0) ? ErrorCode::kDownloadWriteError : ErrorCode::kSuccess;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700185 delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800186 delegate.path_ = output_temp_file.path();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000187 ActionProcessor processor;
188 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700189 processor.EnqueueAction(std::move(feeder_action));
190 processor.EnqueueAction(std::move(download_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191
Alex Deymo60ca1a72015-06-18 18:19:15 -0700192 loop.PostTask(FROM_HERE,
193 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
194 loop.Run();
195 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo5ed695e2015-10-05 16:59:23 -0700196
Amin Hassani538bd592020-11-04 20:46:08 -0800197 EXPECT_TRUE(FakeSystemState::Get()->fake_boot_control()->IsSlotBootable(
Alex Deymo5ed695e2015-10-05 16:59:23 -0700198 install_plan.source_slot));
Amin Hassani538bd592020-11-04 20:46:08 -0800199 EXPECT_FALSE(FakeSystemState::Get()->fake_boot_control()->IsSlotBootable(
Alex Deymo5ed695e2015-10-05 16:59:23 -0700200 install_plan.target_slot));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000201}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700202} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000203
204TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700205 brillo::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000206 const char* foo = "foo";
207 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700208 TestWithData(small,
Amin Hassani008c4582019-01-13 16:22:47 -0800209 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700210 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000211}
212
213TEST(DownloadActionTest, LargeTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700214 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000215 char c = '0';
216 for (unsigned int i = 0; i < big.size(); i++) {
217 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800218 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000219 }
Darin Petkov50332f12010-09-24 11:44:47 -0700220 TestWithData(big,
Amin Hassani008c4582019-01-13 16:22:47 -0800221 0, // fail_write
Darin Petkov9ce452b2010-11-17 14:33:28 -0800222 true); // use_download_delegate
223}
224
225TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700226 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800227 char c = '0';
228 for (unsigned int i = 0; i < big.size(); i++) {
229 big[i] = c;
230 c = ('9' == c) ? '0' : c + 1;
231 }
232 TestWithData(big,
Amin Hassani008c4582019-01-13 16:22:47 -0800233 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700234 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700235}
236
Darin Petkov9d911fa2010-08-19 09:36:08 -0700237TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700238 brillo::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700239 const char* foo = "foofoo";
240 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700241 TestWithData(small,
Amin Hassani008c4582019-01-13 16:22:47 -0800242 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700243 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000244}
245
Aaron Wood9321f502017-09-07 11:18:54 -0700246TEST(DownloadActionTest, MultiPayloadProgressTest) {
247 std::vector<brillo::Blob> payload_datas;
248 // the first payload must be the largest, as it's the actual payload used by
249 // the MockHttpFetcher for all downloaded data.
250 payload_datas.emplace_back(4 * kMockHttpFetcherChunkSize + 256);
251 payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize);
252 brillo::FakeMessageLoop loop(nullptr);
253 loop.SetAsCurrent();
Amin Hassani538bd592020-11-04 20:46:08 -0800254 EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), NextPayload())
Aaron Wood9321f502017-09-07 11:18:54 -0700255 .WillOnce(Return(true));
256
257 MockFileWriter mock_file_writer;
258 EXPECT_CALL(mock_file_writer, Close()).WillRepeatedly(Return(0));
259 EXPECT_CALL(mock_file_writer, Write(_, _, _))
260 .WillRepeatedly(
261 DoAll(SetArgPointee<2>(ErrorCode::kSuccess), Return(true)));
262
263 InstallPlan install_plan;
264 uint64_t total_expected_download_size{0};
265 for (const auto& data : payload_datas) {
266 uint64_t size = data.size();
267 install_plan.payloads.push_back(
268 {.size = size, .type = InstallPayloadType::kFull});
269 total_expected_download_size += size;
270 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700271 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
272 feeder_action->set_obj(install_plan);
Aaron Wood9321f502017-09-07 11:18:54 -0700273 MockPrefs prefs;
274 MockHttpFetcher* http_fetcher = new MockHttpFetcher(
275 payload_datas[0].data(), payload_datas[0].size(), nullptr);
276 // takes ownership of passed in HttpFetcher
Kelvin Zhang97cb0582020-12-02 16:42:15 -0500277 auto download_action = std::make_unique<DownloadActionChromeos>(
278 &prefs,
279 FakeSystemState::Get()->boot_control(),
280 FakeSystemState::Get()->hardware(),
281 http_fetcher,
282 false /* interactive */);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700283 download_action->SetTestFileWriter(&mock_file_writer);
284 BondActions(feeder_action.get(), download_action.get());
Aaron Wood9321f502017-09-07 11:18:54 -0700285 MockDownloadActionDelegate download_delegate;
286 {
287 InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700288 download_action->set_delegate(&download_delegate);
Aaron Wood9321f502017-09-07 11:18:54 -0700289 // these are hand-computed based on the payloads specified above
290 EXPECT_CALL(download_delegate,
291 BytesReceived(kMockHttpFetcherChunkSize,
292 kMockHttpFetcherChunkSize,
293 total_expected_download_size));
294 EXPECT_CALL(download_delegate,
295 BytesReceived(kMockHttpFetcherChunkSize,
296 kMockHttpFetcherChunkSize * 2,
297 total_expected_download_size));
298 EXPECT_CALL(download_delegate,
299 BytesReceived(kMockHttpFetcherChunkSize,
300 kMockHttpFetcherChunkSize * 3,
301 total_expected_download_size));
302 EXPECT_CALL(download_delegate,
303 BytesReceived(kMockHttpFetcherChunkSize,
304 kMockHttpFetcherChunkSize * 4,
305 total_expected_download_size));
306 EXPECT_CALL(download_delegate,
307 BytesReceived(256,
308 kMockHttpFetcherChunkSize * 4 + 256,
309 total_expected_download_size));
310 EXPECT_CALL(download_delegate,
311 BytesReceived(kMockHttpFetcherChunkSize,
312 kMockHttpFetcherChunkSize * 5 + 256,
313 total_expected_download_size));
314 EXPECT_CALL(download_delegate,
315 BytesReceived(kMockHttpFetcherChunkSize,
316 total_expected_download_size,
317 total_expected_download_size));
318 }
319 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700320 processor.EnqueueAction(std::move(feeder_action));
321 processor.EnqueueAction(std::move(download_action));
Aaron Wood9321f502017-09-07 11:18:54 -0700322
323 loop.PostTask(
324 FROM_HERE,
325 base::Bind(
326 [](ActionProcessor* processor) { processor->StartProcessing(); },
327 base::Unretained(&processor)));
328 loop.Run();
329 EXPECT_FALSE(loop.PendingTasks());
330}
331
rspangler@google.com49fdf182009-10-10 00:57:34 +0000332namespace {
333class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
334 public:
335 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700336 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000337 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000338};
339
Alex Deymo60ca1a72015-06-18 18:19:15 -0700340void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341 processor->StartProcessing();
342 CHECK(processor->IsRunning());
343 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000344}
345
Darin Petkov9d911fa2010-08-19 09:36:08 -0700346void TestTerminateEarly(bool use_download_delegate) {
Amin Hassani538bd592020-11-04 20:46:08 -0800347 FakeSystemState::CreateInstance();
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700348 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700349 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000350
Amin Hassani008c4582019-01-13 16:22:47 -0800351 brillo::Blob data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800352 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000353
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700354 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000355 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700356 DirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800357 EXPECT_EQ(0, writer.Open(temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700358
rspangler@google.com49fdf182009-10-10 00:57:34 +0000359 // takes ownership of passed in HttpFetcher
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700360 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
Alex Deymo64d98782016-02-05 18:03:48 -0800361 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800362 install_plan.payloads.resize(1);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700363 feeder_action->set_obj(install_plan);
Amin Hassani538bd592020-11-04 20:46:08 -0800364
Alex Deymo8427b4a2014-11-05 14:00:32 -0800365 MockPrefs prefs;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700366 auto download_action = std::make_unique<DownloadAction>(
Alex Deymo1b3556c2016-02-03 09:54:02 -0800367 &prefs,
Amin Hassani538bd592020-11-04 20:46:08 -0800368 FakeSystemState::Get()->boot_control(),
369 FakeSystemState::Get()->hardware(),
Amin Hassani7ecda262017-07-11 17:10:50 -0700370 new MockHttpFetcher(data.data(), data.size(), nullptr),
Amin Hassanied37d682018-04-06 13:22:00 -0700371 false /* interactive */);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700372 download_action->SetTestFileWriter(&writer);
Alex Deymo542c19b2015-12-03 07:43:31 -0300373 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700374 if (use_download_delegate) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700375 download_action->set_delegate(&download_delegate);
Alex Deymo542c19b2015-12-03 07:43:31 -0300376 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700377 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000378 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000379 ActionProcessor processor;
380 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700381 BondActions(feeder_action.get(), download_action.get());
382 processor.EnqueueAction(std::move(feeder_action));
383 processor.EnqueueAction(std::move(download_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000384
Alex Deymo60ca1a72015-06-18 18:19:15 -0700385 loop.PostTask(FROM_HERE,
386 base::Bind(&TerminateEarlyTestStarter, &processor));
387 loop.Run();
388 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000389 }
390
391 // 1 or 0 chunks should have come through
Alex Deymobffa0602016-02-12 17:16:29 -0800392 const off_t resulting_file_size(utils::FileSize(temp_file.path()));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700393 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000394 if (resulting_file_size != 0)
Alex Deymo5fe0c4e2016-02-16 18:46:24 -0800395 EXPECT_EQ(kMockHttpFetcherChunkSize,
396 static_cast<size_t>(resulting_file_size));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000397}
398
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700399} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700400
401TEST(DownloadActionTest, TerminateEarlyTest) {
402 TestTerminateEarly(true);
403}
404
405TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
406 TestTerminateEarly(false);
407}
408
rspangler@google.com49fdf182009-10-10 00:57:34 +0000409class DownloadActionTestAction;
410
Amin Hassani008c4582019-01-13 16:22:47 -0800411template <>
rspangler@google.com49fdf182009-10-10 00:57:34 +0000412class ActionTraits<DownloadActionTestAction> {
413 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000414 typedef InstallPlan OutputObjectType;
415 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000416};
417
418// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700419class DownloadActionTestAction : public Action<DownloadActionTestAction> {
420 public:
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700421 DownloadActionTestAction() = default;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000422 typedef InstallPlan InputObjectType;
423 typedef InstallPlan OutputObjectType;
424 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
425 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000426 ActionProcessor* processor() { return processor_; }
427 void PerformAction() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000428 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000429 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000430 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700431 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000432 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700433 static std::string StaticType() { return "DownloadActionTestAction"; }
434 string Type() const { return StaticType(); }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000435 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000436};
437
438namespace {
439// This class is an ActionProcessorDelegate that simply terminates the
440// run loop when the ActionProcessor has completed processing. It's used
441// only by the test PassObjectOutTest.
442class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
443 public:
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700444 void ProcessingDone(const ActionProcessor* processor,
445 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700446 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000447 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700448 void ActionCompleted(ActionProcessor* processor,
449 AbstractAction* action,
450 ErrorCode code) override {
451 if (action->Type() == DownloadActionTestAction::StaticType()) {
452 did_test_action_run_ = true;
453 }
454 }
455
456 bool did_test_action_run_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000457};
458
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700459} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000460
461TEST(DownloadActionTest, PassObjectOutTest) {
Amin Hassani538bd592020-11-04 20:46:08 -0800462 FakeSystemState::CreateInstance();
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700463 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700464 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000465
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700466 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700467 EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700468
rspangler@google.com49fdf182009-10-10 00:57:34 +0000469 // takes ownership of passed in HttpFetcher
Alex Deymo64d98782016-02-05 18:03:48 -0800470 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800471 install_plan.payloads.push_back({.size = 1});
472 EXPECT_TRUE(
473 HashCalculator::RawHashOfData({'x'}, &install_plan.payloads[0].hash));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700474 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
475 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800476 MockPrefs prefs;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700477 auto download_action =
478 std::make_unique<DownloadAction>(&prefs,
Amin Hassani538bd592020-11-04 20:46:08 -0800479 FakeSystemState::Get()->boot_control(),
480 FakeSystemState::Get()->hardware(),
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700481 new MockHttpFetcher("x", 1, nullptr),
482 false /* interactive */);
483 download_action->SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000484
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700485 auto test_action = std::make_unique<DownloadActionTestAction>();
486 test_action->expected_input_object_ = install_plan;
487 BondActions(feeder_action.get(), download_action.get());
488 BondActions(download_action.get(), test_action.get());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000489
490 ActionProcessor processor;
491 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000492 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700493 processor.EnqueueAction(std::move(feeder_action));
494 processor.EnqueueAction(std::move(download_action));
495 processor.EnqueueAction(std::move(test_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000496
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700497 loop.PostTask(
498 FROM_HERE,
499 base::Bind(
500 [](ActionProcessor* processor) { processor->StartProcessing(); },
501 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700502 loop.Run();
503 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000504
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700505 EXPECT_EQ(true, delegate.did_test_action_run_);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000506}
507
David Zeuthen8f191b22013-08-06 12:27:50 -0700508// Test fixture for P2P tests.
509class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700510 protected:
Amin Hassani0468a762020-11-17 23:53:48 -0800511 P2PDownloadActionTest() : start_at_offset_(0) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700512
Alex Deymo610277e2014-11-11 21:18:11 -0800513 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700514
515 // Derived from testing::Test.
Amin Hassani538bd592020-11-04 20:46:08 -0800516 void SetUp() override {
517 loop_.SetAsCurrent();
518 FakeSystemState::CreateInstance();
519 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700520
521 // Derived from testing::Test.
Amin Hassani008c4582019-01-13 16:22:47 -0800522 void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
David Zeuthen8f191b22013-08-06 12:27:50 -0700523
524 // To be called by tests to setup the download. The
525 // |starting_offset| parameter is for where to resume.
526 void SetupDownload(off_t starting_offset) {
527 start_at_offset_ = starting_offset;
528 // Prepare data 10 kB of data.
529 data_.clear();
530 for (unsigned int i = 0; i < 10 * 1000; i++)
531 data_ += 'a' + (i % 25);
532
533 // Setup p2p.
Amin Hassani008c4582019-01-13 16:22:47 -0800534 FakeP2PManagerConfiguration* test_conf = new FakeP2PManagerConfiguration();
Kelvin Zhang97cb0582020-12-02 16:42:15 -0500535 p2p_manager_.reset(P2PManager::Construct(
536 test_conf, &fake_um_, "cros_au", 3, base::TimeDelta::FromDays(5)));
David Zeuthen8f191b22013-08-06 12:27:50 -0700537 }
538
539 // To be called by tests to perform the download. The
540 // |use_p2p_to_share| parameter is used to indicate whether the
541 // payload should be shared via p2p.
542 void StartDownload(bool use_p2p_to_share) {
Amin Hassani538bd592020-11-04 20:46:08 -0800543 EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(),
Gilad Arnold74b5f552014-10-07 08:17:16 -0700544 GetUsingP2PForSharing())
545 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700546
547 ScopedTempFile output_temp_file;
548 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800549 EXPECT_EQ(
550 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Alex Deymo64d98782016-02-05 18:03:48 -0800551 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800552 install_plan.payloads.push_back(
553 {.size = data_.length(),
554 .hash = {'1', '2', '3', '4', 'h', 'a', 's', 'h'}});
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700555 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
556 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800557 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700558 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700559 auto download_action = std::make_unique<DownloadAction>(
560 &prefs,
Amin Hassani538bd592020-11-04 20:46:08 -0800561 FakeSystemState::Get()->boot_control(),
562 FakeSystemState::Get()->hardware(),
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700563 new MockHttpFetcher(data_.c_str(), data_.length(), nullptr),
564 false /* interactive */);
565 auto http_fetcher = download_action->http_fetcher();
566 download_action->SetTestFileWriter(&writer);
567 BondActions(feeder_action.get(), download_action.get());
568 delegate_.expected_data_ =
569 brillo::Blob(data_.begin() + start_at_offset_, data_.end());
570 delegate_.path_ = output_temp_file.path();
571 processor_.set_delegate(&delegate_);
572 processor_.EnqueueAction(std::move(feeder_action));
573 processor_.EnqueueAction(std::move(download_action));
David Zeuthen8f191b22013-08-06 12:27:50 -0700574
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700575 loop_.PostTask(
576 FROM_HERE,
577 base::Bind(
578 [](P2PDownloadActionTest* action_test, HttpFetcher* http_fetcher) {
579 action_test->processor_.StartProcessing();
580 http_fetcher->SetOffset(action_test->start_at_offset_);
581 },
582 base::Unretained(this),
583 base::Unretained(http_fetcher)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700584 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700585 }
586
Alex Deymo60ca1a72015-06-18 18:19:15 -0700587 // Mainloop used to make StartDownload() synchronous.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700588 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700589
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700590 // Delegate that is passed to the ActionProcessor.
591 DownloadActionTestProcessorDelegate delegate_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700592
593 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700594 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700595
596 // The ActionProcessor used for running the actions.
597 ActionProcessor processor_;
598
David Zeuthen8f191b22013-08-06 12:27:50 -0700599 // The data being downloaded.
600 string data_;
601
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700602 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700603 // The requested starting offset passed to SetupDownload().
604 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700605
606 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700607};
608
609TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700610 SetupDownload(0); // starting_offset
611 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700612
613 // Check the p2p file and its content matches what was sent.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700614 string file_id = delegate_.p2p_file_id_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700615 EXPECT_NE("", file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800616 EXPECT_EQ(static_cast<int>(data_.length()),
617 p2p_manager_->FileGetSize(file_id));
618 EXPECT_EQ(static_cast<int>(data_.length()),
619 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700620 string p2p_file_contents;
Amin Hassani008c4582019-01-13 16:22:47 -0800621 EXPECT_TRUE(
622 ReadFileToString(p2p_manager_->FileGetPath(file_id), &p2p_file_contents));
David Zeuthen8f191b22013-08-06 12:27:50 -0700623 EXPECT_EQ(data_, p2p_file_contents);
624}
625
626TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700627 SetupDownload(1000); // starting_offset
628 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700629
630 // DownloadAction should convey that the file is not being shared.
631 // and that we don't have any p2p files.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700632 EXPECT_EQ(delegate_.p2p_file_id_, "");
David Zeuthen8f191b22013-08-06 12:27:50 -0700633 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
634}
635
636TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700637 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700638
639 // Prepare the file with existing data before starting to write to
640 // it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700641 string file_id = utils::CalculateP2PFileId(
642 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700643 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
644 string existing_data;
645 for (unsigned int i = 0; i < 1000; i++)
646 existing_data += '0' + (i % 10);
Amin Hassani008c4582019-01-13 16:22:47 -0800647 ASSERT_EQ(
648 WriteFile(
649 p2p_manager_->FileGetPath(file_id), existing_data.c_str(), 1000),
650 1000);
David Zeuthen8f191b22013-08-06 12:27:50 -0700651
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700652 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700653
654 // DownloadAction should convey the same file_id and the file should
655 // have the expected size.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700656 EXPECT_EQ(delegate_.p2p_file_id_, file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800657 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
658 p2p_manager_->FileGetSize(file_id));
659 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
660 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700661 string p2p_file_contents;
662 // Check that the first 1000 bytes wasn't touched and that we
663 // appended the remaining as appropriate.
Amin Hassani008c4582019-01-13 16:22:47 -0800664 EXPECT_TRUE(
665 ReadFileToString(p2p_manager_->FileGetPath(file_id), &p2p_file_contents));
David Zeuthen8f191b22013-08-06 12:27:50 -0700666 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
667 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
668}
669
David Zeuthen8f191b22013-08-06 12:27:50 -0700670TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700671 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700672
673 // Prepare the file with all existing data before starting to write
674 // to it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700675 string file_id = utils::CalculateP2PFileId(
676 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700677 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
678 string existing_data;
679 for (unsigned int i = 0; i < 1000; i++)
680 existing_data += '0' + (i % 10);
Amin Hassani008c4582019-01-13 16:22:47 -0800681 ASSERT_EQ(
682 WriteFile(
683 p2p_manager_->FileGetPath(file_id), existing_data.c_str(), 1000),
684 1000);
David Zeuthen8f191b22013-08-06 12:27:50 -0700685
686 // Check that the file is there.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800687 EXPECT_EQ(1000, p2p_manager_->FileGetSize(file_id));
688 EXPECT_EQ(1, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700689
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700690 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700691
692 // DownloadAction should have deleted the p2p file. Check that it's gone.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800693 EXPECT_EQ(-1, p2p_manager_->FileGetSize(file_id));
694 EXPECT_EQ(0, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700695}
696
rspangler@google.com49fdf182009-10-10 00:57:34 +0000697} // namespace chromeos_update_engine