blob: 565c678251ff54f7fe5b48e5f35940b044c790e0 [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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/common/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>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070032#include <brillo/message_loops/fake_message_loop.h>
33#include <brillo/message_loops/message_loop.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070034
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/action_pipe.h"
36#include "update_engine/common/hash_calculator.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070037#include "update_engine/common/mock_download_action.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080038#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"
Amin Hassaniec7bc112020-10-29 16:47:58 -070042#include "update_engine/cros/fake_p2p_manager_configuration.h"
43#include "update_engine/cros/fake_system_state.h"
44#include "update_engine/payload_consumer/mock_file_writer.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070045#include "update_engine/update_manager/fake_update_manager.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000046
47namespace chromeos_update_engine {
48
Alex Deymof329b932014-10-30 01:37:48 -070049using base::FilePath;
50using base::ReadFileToString;
51using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000052using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070053using std::unique_ptr;
Amin Hassani008c4582019-01-13 16:22:47 -080054using testing::_;
Darin Petkov9d911fa2010-08-19 09:36:08 -070055using testing::AtLeast;
56using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070057using testing::Return;
Aaron Wood9321f502017-09-07 11:18:54 -070058using testing::SetArgPointee;
rspangler@google.com49fdf182009-10-10 00:57:34 +000059
Amin Hassani538bd592020-11-04 20:46:08 -080060class DownloadActionTest : public ::testing::Test {
61 void SetUp() { FakeSystemState::CreateInstance(); }
62};
rspangler@google.com49fdf182009-10-10 00:57:34 +000063
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) {
Amin Hassani538bd592020-11-04 20:46:08 -0800133 FakeSystemState::CreateInstance();
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700134 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700135 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136
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.
Amin Hassani538bd592020-11-04 20:46:08 -0800154 FakeSystemState::Get()->fake_boot_control()->SetSlotBootable(
Alex Deymo5ed695e2015-10-05 16:59:23 -0700155 install_plan.source_slot, true);
Amin Hassani538bd592020-11-04 20:46:08 -0800156 FakeSystemState::Get()->fake_boot_control()->SetSlotBootable(
Alex Deymo5ed695e2015-10-05 16:59:23 -0700157 install_plan.target_slot, true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700158 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
159 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800160 MockPrefs prefs;
Amin Hassani008c4582019-01-13 16:22:47 -0800161 MockHttpFetcher* http_fetcher =
162 new MockHttpFetcher(data.data(), data.size(), nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700163 // takes ownership of passed in HttpFetcher
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700164 auto download_action =
165 std::make_unique<DownloadAction>(&prefs,
Amin Hassani538bd592020-11-04 20:46:08 -0800166 FakeSystemState::Get()->boot_control(),
167 FakeSystemState::Get()->hardware(),
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700168 http_fetcher,
169 false /* interactive */);
170 download_action->SetTestFileWriter(&writer);
171 BondActions(feeder_action.get(), download_action.get());
Alex Deymo542c19b2015-12-03 07:43:31 -0300172 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700173 if (use_download_delegate) {
174 InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700175 download_action->set_delegate(&download_delegate);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700176 if (data.size() > kMockHttpFetcherChunkSize)
177 EXPECT_CALL(download_delegate,
Sen Jiang5ae865b2017-04-18 14:24:40 -0700178 BytesReceived(_, kMockHttpFetcherChunkSize, _));
Alex Deymo542c19b2015-12-03 07:43:31 -0300179 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(AtLeast(1));
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700180 EXPECT_CALL(download_delegate, DownloadComplete())
181 .Times(fail_write == 0 ? 1 : 0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700182 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700183 DownloadActionTestProcessorDelegate delegate;
184 delegate.expected_code_ =
185 (fail_write > 0) ? ErrorCode::kDownloadWriteError : ErrorCode::kSuccess;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700186 delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800187 delegate.path_ = output_temp_file.path();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000188 ActionProcessor processor;
189 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700190 processor.EnqueueAction(std::move(feeder_action));
191 processor.EnqueueAction(std::move(download_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000192
Alex Deymo60ca1a72015-06-18 18:19:15 -0700193 loop.PostTask(FROM_HERE,
194 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
195 loop.Run();
196 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo5ed695e2015-10-05 16:59:23 -0700197
Amin Hassani538bd592020-11-04 20:46:08 -0800198 EXPECT_TRUE(FakeSystemState::Get()->fake_boot_control()->IsSlotBootable(
Alex Deymo5ed695e2015-10-05 16:59:23 -0700199 install_plan.source_slot));
Amin Hassani538bd592020-11-04 20:46:08 -0800200 EXPECT_FALSE(FakeSystemState::Get()->fake_boot_control()->IsSlotBootable(
Alex Deymo5ed695e2015-10-05 16:59:23 -0700201 install_plan.target_slot));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000202}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700203} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000204
205TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700206 brillo::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000207 const char* foo = "foo";
208 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700209 TestWithData(small,
Amin Hassani008c4582019-01-13 16:22:47 -0800210 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700211 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000212}
213
214TEST(DownloadActionTest, LargeTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700215 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000216 char c = '0';
217 for (unsigned int i = 0; i < big.size(); i++) {
218 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800219 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000220 }
Darin Petkov50332f12010-09-24 11:44:47 -0700221 TestWithData(big,
Amin Hassani008c4582019-01-13 16:22:47 -0800222 0, // fail_write
Darin Petkov9ce452b2010-11-17 14:33:28 -0800223 true); // use_download_delegate
224}
225
226TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700227 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800228 char c = '0';
229 for (unsigned int i = 0; i < big.size(); i++) {
230 big[i] = c;
231 c = ('9' == c) ? '0' : c + 1;
232 }
233 TestWithData(big,
Amin Hassani008c4582019-01-13 16:22:47 -0800234 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700235 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700236}
237
Darin Petkov9d911fa2010-08-19 09:36:08 -0700238TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700239 brillo::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700240 const char* foo = "foofoo";
241 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700242 TestWithData(small,
Amin Hassani008c4582019-01-13 16:22:47 -0800243 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700244 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000245}
246
Aaron Wood9321f502017-09-07 11:18:54 -0700247TEST(DownloadActionTest, MultiPayloadProgressTest) {
248 std::vector<brillo::Blob> payload_datas;
249 // the first payload must be the largest, as it's the actual payload used by
250 // the MockHttpFetcher for all downloaded data.
251 payload_datas.emplace_back(4 * kMockHttpFetcherChunkSize + 256);
252 payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize);
253 brillo::FakeMessageLoop loop(nullptr);
254 loop.SetAsCurrent();
Amin Hassani538bd592020-11-04 20:46:08 -0800255 EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), NextPayload())
Aaron Wood9321f502017-09-07 11:18:54 -0700256 .WillOnce(Return(true));
257
258 MockFileWriter mock_file_writer;
259 EXPECT_CALL(mock_file_writer, Close()).WillRepeatedly(Return(0));
260 EXPECT_CALL(mock_file_writer, Write(_, _, _))
261 .WillRepeatedly(
262 DoAll(SetArgPointee<2>(ErrorCode::kSuccess), Return(true)));
263
264 InstallPlan install_plan;
265 uint64_t total_expected_download_size{0};
266 for (const auto& data : payload_datas) {
267 uint64_t size = data.size();
268 install_plan.payloads.push_back(
269 {.size = size, .type = InstallPayloadType::kFull});
270 total_expected_download_size += size;
271 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700272 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
273 feeder_action->set_obj(install_plan);
Aaron Wood9321f502017-09-07 11:18:54 -0700274 MockPrefs prefs;
275 MockHttpFetcher* http_fetcher = new MockHttpFetcher(
276 payload_datas[0].data(), payload_datas[0].size(), nullptr);
277 // takes ownership of passed in HttpFetcher
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700278 auto download_action =
279 std::make_unique<DownloadAction>(&prefs,
Amin Hassani538bd592020-11-04 20:46:08 -0800280 FakeSystemState::Get()->boot_control(),
281 FakeSystemState::Get()->hardware(),
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700282 http_fetcher,
283 false /* interactive */);
284 download_action->SetTestFileWriter(&mock_file_writer);
285 BondActions(feeder_action.get(), download_action.get());
Aaron Wood9321f502017-09-07 11:18:54 -0700286 MockDownloadActionDelegate download_delegate;
287 {
288 InSequence s;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700289 download_action->set_delegate(&download_delegate);
Aaron Wood9321f502017-09-07 11:18:54 -0700290 // these are hand-computed based on the payloads specified above
291 EXPECT_CALL(download_delegate,
292 BytesReceived(kMockHttpFetcherChunkSize,
293 kMockHttpFetcherChunkSize,
294 total_expected_download_size));
295 EXPECT_CALL(download_delegate,
296 BytesReceived(kMockHttpFetcherChunkSize,
297 kMockHttpFetcherChunkSize * 2,
298 total_expected_download_size));
299 EXPECT_CALL(download_delegate,
300 BytesReceived(kMockHttpFetcherChunkSize,
301 kMockHttpFetcherChunkSize * 3,
302 total_expected_download_size));
303 EXPECT_CALL(download_delegate,
304 BytesReceived(kMockHttpFetcherChunkSize,
305 kMockHttpFetcherChunkSize * 4,
306 total_expected_download_size));
307 EXPECT_CALL(download_delegate,
308 BytesReceived(256,
309 kMockHttpFetcherChunkSize * 4 + 256,
310 total_expected_download_size));
311 EXPECT_CALL(download_delegate,
312 BytesReceived(kMockHttpFetcherChunkSize,
313 kMockHttpFetcherChunkSize * 5 + 256,
314 total_expected_download_size));
315 EXPECT_CALL(download_delegate,
316 BytesReceived(kMockHttpFetcherChunkSize,
317 total_expected_download_size,
318 total_expected_download_size));
319 }
320 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700321 processor.EnqueueAction(std::move(feeder_action));
322 processor.EnqueueAction(std::move(download_action));
Aaron Wood9321f502017-09-07 11:18:54 -0700323
324 loop.PostTask(
325 FROM_HERE,
326 base::Bind(
327 [](ActionProcessor* processor) { processor->StartProcessing(); },
328 base::Unretained(&processor)));
329 loop.Run();
330 EXPECT_FALSE(loop.PendingTasks());
331}
332
rspangler@google.com49fdf182009-10-10 00:57:34 +0000333namespace {
334class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
335 public:
336 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700337 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000338 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000339};
340
Alex Deymo60ca1a72015-06-18 18:19:15 -0700341void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000342 processor->StartProcessing();
343 CHECK(processor->IsRunning());
344 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000345}
346
Darin Petkov9d911fa2010-08-19 09:36:08 -0700347void TestTerminateEarly(bool use_download_delegate) {
Amin Hassani538bd592020-11-04 20:46:08 -0800348 FakeSystemState::CreateInstance();
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700349 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700350 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000351
Amin Hassani008c4582019-01-13 16:22:47 -0800352 brillo::Blob data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800353 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000354
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700355 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000356 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700357 DirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800358 EXPECT_EQ(0, writer.Open(temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700359
rspangler@google.com49fdf182009-10-10 00:57:34 +0000360 // takes ownership of passed in HttpFetcher
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700361 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
Alex Deymo64d98782016-02-05 18:03:48 -0800362 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800363 install_plan.payloads.resize(1);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700364 feeder_action->set_obj(install_plan);
Amin Hassani538bd592020-11-04 20:46:08 -0800365
Alex Deymo8427b4a2014-11-05 14:00:32 -0800366 MockPrefs prefs;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700367 auto download_action = std::make_unique<DownloadAction>(
Alex Deymo1b3556c2016-02-03 09:54:02 -0800368 &prefs,
Amin Hassani538bd592020-11-04 20:46:08 -0800369 FakeSystemState::Get()->boot_control(),
370 FakeSystemState::Get()->hardware(),
Amin Hassani7ecda262017-07-11 17:10:50 -0700371 new MockHttpFetcher(data.data(), data.size(), nullptr),
Amin Hassanied37d682018-04-06 13:22:00 -0700372 false /* interactive */);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700373 download_action->SetTestFileWriter(&writer);
Alex Deymo542c19b2015-12-03 07:43:31 -0300374 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700375 if (use_download_delegate) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700376 download_action->set_delegate(&download_delegate);
Alex Deymo542c19b2015-12-03 07:43:31 -0300377 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700378 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000379 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000380 ActionProcessor processor;
381 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700382 BondActions(feeder_action.get(), download_action.get());
383 processor.EnqueueAction(std::move(feeder_action));
384 processor.EnqueueAction(std::move(download_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000385
Alex Deymo60ca1a72015-06-18 18:19:15 -0700386 loop.PostTask(FROM_HERE,
387 base::Bind(&TerminateEarlyTestStarter, &processor));
388 loop.Run();
389 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000390 }
391
392 // 1 or 0 chunks should have come through
Alex Deymobffa0602016-02-12 17:16:29 -0800393 const off_t resulting_file_size(utils::FileSize(temp_file.path()));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700394 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000395 if (resulting_file_size != 0)
Alex Deymo5fe0c4e2016-02-16 18:46:24 -0800396 EXPECT_EQ(kMockHttpFetcherChunkSize,
397 static_cast<size_t>(resulting_file_size));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000398}
399
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700400} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700401
402TEST(DownloadActionTest, TerminateEarlyTest) {
403 TestTerminateEarly(true);
404}
405
406TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
407 TestTerminateEarly(false);
408}
409
rspangler@google.com49fdf182009-10-10 00:57:34 +0000410class DownloadActionTestAction;
411
Amin Hassani008c4582019-01-13 16:22:47 -0800412template <>
rspangler@google.com49fdf182009-10-10 00:57:34 +0000413class ActionTraits<DownloadActionTestAction> {
414 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000415 typedef InstallPlan OutputObjectType;
416 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000417};
418
419// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700420class DownloadActionTestAction : public Action<DownloadActionTestAction> {
421 public:
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700422 DownloadActionTestAction() = default;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000423 typedef InstallPlan InputObjectType;
424 typedef InstallPlan OutputObjectType;
425 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
426 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000427 ActionProcessor* processor() { return processor_; }
428 void PerformAction() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000429 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000430 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000431 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700432 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000433 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700434 static std::string StaticType() { return "DownloadActionTestAction"; }
435 string Type() const { return StaticType(); }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000436 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000437};
438
439namespace {
440// This class is an ActionProcessorDelegate that simply terminates the
441// run loop when the ActionProcessor has completed processing. It's used
442// only by the test PassObjectOutTest.
443class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
444 public:
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700445 void ProcessingDone(const ActionProcessor* processor,
446 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700447 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000448 }
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700449 void ActionCompleted(ActionProcessor* processor,
450 AbstractAction* action,
451 ErrorCode code) override {
452 if (action->Type() == DownloadActionTestAction::StaticType()) {
453 did_test_action_run_ = true;
454 }
455 }
456
457 bool did_test_action_run_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000458};
459
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700460} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000461
462TEST(DownloadActionTest, PassObjectOutTest) {
Amin Hassani538bd592020-11-04 20:46:08 -0800463 FakeSystemState::CreateInstance();
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700464 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700465 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000466
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700467 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700468 EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700469
rspangler@google.com49fdf182009-10-10 00:57:34 +0000470 // takes ownership of passed in HttpFetcher
Alex Deymo64d98782016-02-05 18:03:48 -0800471 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800472 install_plan.payloads.push_back({.size = 1});
473 EXPECT_TRUE(
474 HashCalculator::RawHashOfData({'x'}, &install_plan.payloads[0].hash));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700475 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
476 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800477 MockPrefs prefs;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700478 auto download_action =
479 std::make_unique<DownloadAction>(&prefs,
Amin Hassani538bd592020-11-04 20:46:08 -0800480 FakeSystemState::Get()->boot_control(),
481 FakeSystemState::Get()->hardware(),
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700482 new MockHttpFetcher("x", 1, nullptr),
483 false /* interactive */);
484 download_action->SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000485
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700486 auto test_action = std::make_unique<DownloadActionTestAction>();
487 test_action->expected_input_object_ = install_plan;
488 BondActions(feeder_action.get(), download_action.get());
489 BondActions(download_action.get(), test_action.get());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000490
491 ActionProcessor processor;
492 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000493 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700494 processor.EnqueueAction(std::move(feeder_action));
495 processor.EnqueueAction(std::move(download_action));
496 processor.EnqueueAction(std::move(test_action));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000497
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700498 loop.PostTask(
499 FROM_HERE,
500 base::Bind(
501 [](ActionProcessor* processor) { processor->StartProcessing(); },
502 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700503 loop.Run();
504 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000505
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700506 EXPECT_EQ(true, delegate.did_test_action_run_);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000507}
508
David Zeuthen8f191b22013-08-06 12:27:50 -0700509// Test fixture for P2P tests.
510class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700511 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700512 P2PDownloadActionTest()
Amin Hassani538bd592020-11-04 20:46:08 -0800513 : start_at_offset_(0), fake_um_(FakeSystemState::Get()->fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700514
Alex Deymo610277e2014-11-11 21:18:11 -0800515 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700516
517 // Derived from testing::Test.
Amin Hassani538bd592020-11-04 20:46:08 -0800518 void SetUp() override {
519 loop_.SetAsCurrent();
520 FakeSystemState::CreateInstance();
521 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700522
523 // Derived from testing::Test.
Amin Hassani008c4582019-01-13 16:22:47 -0800524 void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
David Zeuthen8f191b22013-08-06 12:27:50 -0700525
526 // To be called by tests to setup the download. The
527 // |starting_offset| parameter is for where to resume.
528 void SetupDownload(off_t starting_offset) {
529 start_at_offset_ = starting_offset;
530 // Prepare data 10 kB of data.
531 data_.clear();
532 for (unsigned int i = 0; i < 10 * 1000; i++)
533 data_ += 'a' + (i % 25);
534
535 // Setup p2p.
Amin Hassani008c4582019-01-13 16:22:47 -0800536 FakeP2PManagerConfiguration* test_conf = new FakeP2PManagerConfiguration();
537 p2p_manager_.reset(P2PManager::Construct(test_conf,
538 nullptr,
539 &fake_um_,
540 "cros_au",
541 3,
542 base::TimeDelta::FromDays(5)));
Amin Hassani538bd592020-11-04 20:46:08 -0800543 FakeSystemState::Get()->set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700544 }
545
546 // To be called by tests to perform the download. The
547 // |use_p2p_to_share| parameter is used to indicate whether the
548 // payload should be shared via p2p.
549 void StartDownload(bool use_p2p_to_share) {
Amin Hassani538bd592020-11-04 20:46:08 -0800550 EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(),
Gilad Arnold74b5f552014-10-07 08:17:16 -0700551 GetUsingP2PForSharing())
552 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700553
554 ScopedTempFile output_temp_file;
555 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800556 EXPECT_EQ(
557 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Alex Deymo64d98782016-02-05 18:03:48 -0800558 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800559 install_plan.payloads.push_back(
560 {.size = data_.length(),
561 .hash = {'1', '2', '3', '4', 'h', 'a', 's', 'h'}});
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700562 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
563 feeder_action->set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800564 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700565 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700566 auto download_action = std::make_unique<DownloadAction>(
567 &prefs,
Amin Hassani538bd592020-11-04 20:46:08 -0800568 FakeSystemState::Get()->boot_control(),
569 FakeSystemState::Get()->hardware(),
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700570 new MockHttpFetcher(data_.c_str(), data_.length(), nullptr),
571 false /* interactive */);
572 auto http_fetcher = download_action->http_fetcher();
573 download_action->SetTestFileWriter(&writer);
574 BondActions(feeder_action.get(), download_action.get());
575 delegate_.expected_data_ =
576 brillo::Blob(data_.begin() + start_at_offset_, data_.end());
577 delegate_.path_ = output_temp_file.path();
578 processor_.set_delegate(&delegate_);
579 processor_.EnqueueAction(std::move(feeder_action));
580 processor_.EnqueueAction(std::move(download_action));
David Zeuthen8f191b22013-08-06 12:27:50 -0700581
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700582 loop_.PostTask(
583 FROM_HERE,
584 base::Bind(
585 [](P2PDownloadActionTest* action_test, HttpFetcher* http_fetcher) {
586 action_test->processor_.StartProcessing();
587 http_fetcher->SetOffset(action_test->start_at_offset_);
588 },
589 base::Unretained(this),
590 base::Unretained(http_fetcher)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700591 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700592 }
593
Alex Deymo60ca1a72015-06-18 18:19:15 -0700594 // Mainloop used to make StartDownload() synchronous.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700595 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700596
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700597 // Delegate that is passed to the ActionProcessor.
598 DownloadActionTestProcessorDelegate delegate_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700599
600 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700601 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700602
603 // The ActionProcessor used for running the actions.
604 ActionProcessor processor_;
605
David Zeuthen8f191b22013-08-06 12:27:50 -0700606 // The data being downloaded.
607 string data_;
608
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700609 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700610 // The requested starting offset passed to SetupDownload().
611 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700612
613 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700614};
615
616TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700617 SetupDownload(0); // starting_offset
618 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700619
620 // Check the p2p file and its content matches what was sent.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700621 string file_id = delegate_.p2p_file_id_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700622 EXPECT_NE("", file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800623 EXPECT_EQ(static_cast<int>(data_.length()),
624 p2p_manager_->FileGetSize(file_id));
625 EXPECT_EQ(static_cast<int>(data_.length()),
626 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700627 string p2p_file_contents;
Amin Hassani008c4582019-01-13 16:22:47 -0800628 EXPECT_TRUE(
629 ReadFileToString(p2p_manager_->FileGetPath(file_id), &p2p_file_contents));
David Zeuthen8f191b22013-08-06 12:27:50 -0700630 EXPECT_EQ(data_, p2p_file_contents);
631}
632
633TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700634 SetupDownload(1000); // starting_offset
635 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700636
637 // DownloadAction should convey that the file is not being shared.
638 // and that we don't have any p2p files.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700639 EXPECT_EQ(delegate_.p2p_file_id_, "");
David Zeuthen8f191b22013-08-06 12:27:50 -0700640 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
641}
642
643TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700644 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700645
646 // Prepare the file with existing data before starting to write to
647 // it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700648 string file_id = utils::CalculateP2PFileId(
649 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700650 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
651 string existing_data;
652 for (unsigned int i = 0; i < 1000; i++)
653 existing_data += '0' + (i % 10);
Amin Hassani008c4582019-01-13 16:22:47 -0800654 ASSERT_EQ(
655 WriteFile(
656 p2p_manager_->FileGetPath(file_id), existing_data.c_str(), 1000),
657 1000);
David Zeuthen8f191b22013-08-06 12:27:50 -0700658
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700659 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700660
661 // DownloadAction should convey the same file_id and the file should
662 // have the expected size.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700663 EXPECT_EQ(delegate_.p2p_file_id_, file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800664 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
665 p2p_manager_->FileGetSize(file_id));
666 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
667 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700668 string p2p_file_contents;
669 // Check that the first 1000 bytes wasn't touched and that we
670 // appended the remaining as appropriate.
Amin Hassani008c4582019-01-13 16:22:47 -0800671 EXPECT_TRUE(
672 ReadFileToString(p2p_manager_->FileGetPath(file_id), &p2p_file_contents));
David Zeuthen8f191b22013-08-06 12:27:50 -0700673 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
674 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
675}
676
David Zeuthen8f191b22013-08-06 12:27:50 -0700677TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700678 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700679
680 // Prepare the file with all existing data before starting to write
681 // to it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700682 string file_id = utils::CalculateP2PFileId(
683 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700684 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
685 string existing_data;
686 for (unsigned int i = 0; i < 1000; i++)
687 existing_data += '0' + (i % 10);
Amin Hassani008c4582019-01-13 16:22:47 -0800688 ASSERT_EQ(
689 WriteFile(
690 p2p_manager_->FileGetPath(file_id), existing_data.c_str(), 1000),
691 1000);
David Zeuthen8f191b22013-08-06 12:27:50 -0700692
693 // Check that the file is there.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800694 EXPECT_EQ(1000, p2p_manager_->FileGetSize(file_id));
695 EXPECT_EQ(1, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700696
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700697 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700698
699 // DownloadAction should have deleted the p2p file. Check that it's gone.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800700 EXPECT_EQ(-1, p2p_manager_->FileGetSize(file_id));
701 EXPECT_EQ(0, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700702}
703
rspangler@google.com49fdf182009-10-10 00:57:34 +0000704} // namespace chromeos_update_engine