blob: 5dac5507559aa835e15096e702dcb53968ed6dc4 [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>
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"
37#include "update_engine/common/mock_http_fetcher.h"
38#include "update_engine/common/mock_prefs.h"
39#include "update_engine/common/test_utils.h"
40#include "update_engine/common/utils.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070041#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070042#include "update_engine/fake_system_state.h"
Aaron Wood9321f502017-09-07 11:18:54 -070043#include "update_engine/mock_file_writer.h"
Alex Deymo542c19b2015-12-03 07:43:31 -030044#include "update_engine/payload_consumer/mock_download_action.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;
Alex Deymoe5e5fe92015-10-05 09:28:19 -070054using test_utils::ScopedTempFile;
Darin Petkov9d911fa2010-08-19 09:36:08 -070055using testing::AtLeast;
56using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070057using testing::Return;
Aaron Wood9321f502017-09-07 11:18:54 -070058using testing::SetArgPointee;
Alex Deymof329b932014-10-30 01:37:48 -070059using testing::_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000060
61class DownloadActionTest : public ::testing::Test { };
62
63namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070064
rspangler@google.com49fdf182009-10-10 00:57:34 +000065class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
66 public:
David Zeuthena99981f2013-04-29 13:42:47 -070067 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Deymo60ca1a72015-06-18 18:19:15 -070068 : processing_done_called_(false),
Darin Petkovc97435c2010-07-20 12:37:43 -070069 expected_code_(expected_code) {}
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);
93 } 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_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102};
103
Darin Petkov9ce452b2010-11-17 14:33:28 -0800104class TestDirectFileWriter : public DirectFileWriter {
105 public:
106 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
107 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
108
Don Garrette410e0f2011-11-10 15:39:01 -0800109 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800110 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800111 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800112 }
113 return DirectFileWriter::Write(bytes, count);
114 }
115
116 private:
117 // If positive, fail on the |fail_write_| call to Write.
118 int fail_write_;
119 int current_write_;
120};
121
Alex Deymo60ca1a72015-06-18 18:19:15 -0700122void StartProcessorInRunLoop(ActionProcessor* processor,
123 MockHttpFetcher* http_fetcher) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000124 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700125 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126}
127
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700128void TestWithData(const brillo::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800129 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700130 bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700131 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700132 loop.SetAsCurrent();
Alex Deymo5ed695e2015-10-05 16:59:23 -0700133 FakeSystemState fake_system_state;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134
135 // TODO(adlr): see if we need a different file for build bots
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.
153 fake_system_state.fake_boot_control()->SetSlotBootable(
154 install_plan.source_slot, true);
155 fake_system_state.fake_boot_control()->SetSlotBootable(
156 install_plan.target_slot, true);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000157 ObjectFeederAction<InstallPlan> feeder_action;
158 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800159 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800160 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800161 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700162 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700163 // takes ownership of passed in HttpFetcher
Alex Deymo1b3556c2016-02-03 09:54:02 -0800164 DownloadAction download_action(&prefs,
165 fake_system_state.boot_control(),
166 fake_system_state.hardware(),
167 &fake_system_state,
Amin Hassani7ecda262017-07-11 17:10:50 -0700168 http_fetcher,
Amin Hassanied37d682018-04-06 13:22:00 -0700169 false /* interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700170 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000171 BondActions(&feeder_action, &download_action);
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;
175 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 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700183 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700184 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700185 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700186 DownloadActionTestProcessorDelegate delegate(expected_code);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700187 delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800188 delegate.path_ = output_temp_file.path();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000189 ActionProcessor processor;
190 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000191 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000192 processor.EnqueueAction(&download_action);
193
Alex Deymo60ca1a72015-06-18 18:19:15 -0700194 loop.PostTask(FROM_HERE,
195 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
196 loop.Run();
197 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo5ed695e2015-10-05 16:59:23 -0700198
199 EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable(
200 install_plan.source_slot));
201 EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable(
202 install_plan.target_slot));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000203}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700204} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000205
206TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700207 brillo::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000208 const char* foo = "foo";
209 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700210 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800211 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700212 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000213}
214
215TEST(DownloadActionTest, LargeTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700216 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000217 char c = '0';
218 for (unsigned int i = 0; i < big.size(); i++) {
219 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800220 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000221 }
Darin Petkov50332f12010-09-24 11:44:47 -0700222 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800223 0, // fail_write
224 true); // use_download_delegate
225}
226
227TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700228 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800229 char c = '0';
230 for (unsigned int i = 0; i < big.size(); i++) {
231 big[i] = c;
232 c = ('9' == c) ? '0' : c + 1;
233 }
234 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800235 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700236 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700237}
238
Darin Petkov9d911fa2010-08-19 09:36:08 -0700239TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700240 brillo::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700241 const char* foo = "foofoo";
242 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700243 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800244 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700245 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000246}
247
Aaron Wood9321f502017-09-07 11:18:54 -0700248TEST(DownloadActionTest, MultiPayloadProgressTest) {
249 std::vector<brillo::Blob> payload_datas;
250 // the first payload must be the largest, as it's the actual payload used by
251 // the MockHttpFetcher for all downloaded data.
252 payload_datas.emplace_back(4 * kMockHttpFetcherChunkSize + 256);
253 payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize);
254 brillo::FakeMessageLoop loop(nullptr);
255 loop.SetAsCurrent();
256 FakeSystemState fake_system_state;
257 EXPECT_CALL(*fake_system_state.mock_payload_state(), NextPayload())
258 .WillOnce(Return(true));
259
260 MockFileWriter mock_file_writer;
261 EXPECT_CALL(mock_file_writer, Close()).WillRepeatedly(Return(0));
262 EXPECT_CALL(mock_file_writer, Write(_, _, _))
263 .WillRepeatedly(
264 DoAll(SetArgPointee<2>(ErrorCode::kSuccess), Return(true)));
265
266 InstallPlan install_plan;
267 uint64_t total_expected_download_size{0};
268 for (const auto& data : payload_datas) {
269 uint64_t size = data.size();
270 install_plan.payloads.push_back(
271 {.size = size, .type = InstallPayloadType::kFull});
272 total_expected_download_size += size;
273 }
274 ObjectFeederAction<InstallPlan> feeder_action;
275 feeder_action.set_obj(install_plan);
276 MockPrefs prefs;
277 MockHttpFetcher* http_fetcher = new MockHttpFetcher(
278 payload_datas[0].data(), payload_datas[0].size(), nullptr);
279 // takes ownership of passed in HttpFetcher
280 DownloadAction download_action(&prefs,
281 fake_system_state.boot_control(),
282 fake_system_state.hardware(),
283 &fake_system_state,
Sen Jiang18414082018-01-11 14:50:36 -0800284 http_fetcher,
Amin Hassanied37d682018-04-06 13:22:00 -0700285 false /* interactive */);
Aaron Wood9321f502017-09-07 11:18:54 -0700286 download_action.SetTestFileWriter(&mock_file_writer);
287 BondActions(&feeder_action, &download_action);
288 MockDownloadActionDelegate download_delegate;
289 {
290 InSequence s;
291 download_action.set_delegate(&download_delegate);
292 // these are hand-computed based on the payloads specified above
293 EXPECT_CALL(download_delegate,
294 BytesReceived(kMockHttpFetcherChunkSize,
295 kMockHttpFetcherChunkSize,
296 total_expected_download_size));
297 EXPECT_CALL(download_delegate,
298 BytesReceived(kMockHttpFetcherChunkSize,
299 kMockHttpFetcherChunkSize * 2,
300 total_expected_download_size));
301 EXPECT_CALL(download_delegate,
302 BytesReceived(kMockHttpFetcherChunkSize,
303 kMockHttpFetcherChunkSize * 3,
304 total_expected_download_size));
305 EXPECT_CALL(download_delegate,
306 BytesReceived(kMockHttpFetcherChunkSize,
307 kMockHttpFetcherChunkSize * 4,
308 total_expected_download_size));
309 EXPECT_CALL(download_delegate,
310 BytesReceived(256,
311 kMockHttpFetcherChunkSize * 4 + 256,
312 total_expected_download_size));
313 EXPECT_CALL(download_delegate,
314 BytesReceived(kMockHttpFetcherChunkSize,
315 kMockHttpFetcherChunkSize * 5 + 256,
316 total_expected_download_size));
317 EXPECT_CALL(download_delegate,
318 BytesReceived(kMockHttpFetcherChunkSize,
319 total_expected_download_size,
320 total_expected_download_size));
321 }
322 ActionProcessor processor;
323 processor.EnqueueAction(&feeder_action);
324 processor.EnqueueAction(&download_action);
325
326 loop.PostTask(
327 FROM_HERE,
328 base::Bind(
329 [](ActionProcessor* processor) { processor->StartProcessing(); },
330 base::Unretained(&processor)));
331 loop.Run();
332 EXPECT_FALSE(loop.PendingTasks());
333}
334
rspangler@google.com49fdf182009-10-10 00:57:34 +0000335namespace {
336class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
337 public:
338 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700339 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000340 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341};
342
Alex Deymo60ca1a72015-06-18 18:19:15 -0700343void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000344 processor->StartProcessing();
345 CHECK(processor->IsRunning());
346 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000347}
348
Darin Petkov9d911fa2010-08-19 09:36:08 -0700349void TestTerminateEarly(bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700350 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700351 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000352
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700353 brillo::Blob data(kMockHttpFetcherChunkSize +
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800354 kMockHttpFetcherChunkSize / 2);
355 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000356
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700357 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000358 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700359 DirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800360 EXPECT_EQ(0, writer.Open(temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700361
rspangler@google.com49fdf182009-10-10 00:57:34 +0000362 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000363 ObjectFeederAction<InstallPlan> feeder_action;
Alex Deymo64d98782016-02-05 18:03:48 -0800364 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800365 install_plan.payloads.resize(1);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000366 feeder_action.set_obj(install_plan);
Alex Deymo5ed695e2015-10-05 16:59:23 -0700367 FakeSystemState fake_system_state_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800368 MockPrefs prefs;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800369 DownloadAction download_action(
370 &prefs,
371 fake_system_state_.boot_control(),
372 fake_system_state_.hardware(),
373 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700374 new MockHttpFetcher(data.data(), data.size(), nullptr),
Amin Hassanied37d682018-04-06 13:22:00 -0700375 false /* interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700376 download_action.SetTestFileWriter(&writer);
Alex Deymo542c19b2015-12-03 07:43:31 -0300377 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700378 if (use_download_delegate) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700379 download_action.set_delegate(&download_delegate);
Alex Deymo542c19b2015-12-03 07:43:31 -0300380 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700381 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000382 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000383 ActionProcessor processor;
384 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000385 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000386 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000387 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000388
Alex Deymo60ca1a72015-06-18 18:19:15 -0700389 loop.PostTask(FROM_HERE,
390 base::Bind(&TerminateEarlyTestStarter, &processor));
391 loop.Run();
392 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000393 }
394
395 // 1 or 0 chunks should have come through
Alex Deymobffa0602016-02-12 17:16:29 -0800396 const off_t resulting_file_size(utils::FileSize(temp_file.path()));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700397 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000398 if (resulting_file_size != 0)
Alex Deymo5fe0c4e2016-02-16 18:46:24 -0800399 EXPECT_EQ(kMockHttpFetcherChunkSize,
400 static_cast<size_t>(resulting_file_size));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000401}
402
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700403} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700404
405TEST(DownloadActionTest, TerminateEarlyTest) {
406 TestTerminateEarly(true);
407}
408
409TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
410 TestTerminateEarly(false);
411}
412
rspangler@google.com49fdf182009-10-10 00:57:34 +0000413class DownloadActionTestAction;
414
415template<>
416class ActionTraits<DownloadActionTestAction> {
417 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000418 typedef InstallPlan OutputObjectType;
419 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000420};
421
422// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700423class DownloadActionTestAction : public Action<DownloadActionTestAction> {
424 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000425 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000426 typedef InstallPlan InputObjectType;
427 typedef InstallPlan OutputObjectType;
428 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
429 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000430 ActionProcessor* processor() { return processor_; }
431 void PerformAction() {
432 did_run_ = true;
433 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000434 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000435 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700436 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000437 }
438 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000439 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000440 bool did_run_;
441};
442
443namespace {
444// This class is an ActionProcessorDelegate that simply terminates the
445// run loop when the ActionProcessor has completed processing. It's used
446// only by the test PassObjectOutTest.
447class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
448 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700449 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700450 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000451 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000452};
453
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700454} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000455
456TEST(DownloadActionTest, PassObjectOutTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700457 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700458 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000459
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700460 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700461 EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700462
rspangler@google.com49fdf182009-10-10 00:57:34 +0000463 // takes ownership of passed in HttpFetcher
Alex Deymo64d98782016-02-05 18:03:48 -0800464 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800465 install_plan.payloads.push_back({.size = 1});
466 EXPECT_TRUE(
467 HashCalculator::RawHashOfData({'x'}, &install_plan.payloads[0].hash));
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000468 ObjectFeederAction<InstallPlan> feeder_action;
469 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800470 MockPrefs prefs;
Alex Deymo5ed695e2015-10-05 16:59:23 -0700471 FakeSystemState fake_system_state_;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800472 DownloadAction download_action(&prefs,
473 fake_system_state_.boot_control(),
474 fake_system_state_.hardware(),
475 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700476 new MockHttpFetcher("x", 1, nullptr),
Amin Hassanied37d682018-04-06 13:22:00 -0700477 false /* interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700478 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000479
480 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000481 test_action.expected_input_object_ = install_plan;
482 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000483 BondActions(&download_action, &test_action);
484
485 ActionProcessor processor;
486 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000487 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000488 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000489 processor.EnqueueAction(&download_action);
490 processor.EnqueueAction(&test_action);
491
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700492 loop.PostTask(
493 FROM_HERE,
494 base::Bind(
495 [](ActionProcessor* processor) { processor->StartProcessing(); },
496 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700497 loop.Run();
498 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000499
500 EXPECT_EQ(true, test_action.did_run_);
501}
502
David Zeuthen8f191b22013-08-06 12:27:50 -0700503// Test fixture for P2P tests.
504class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700505 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700506 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700507 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700508 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700509
Alex Deymo610277e2014-11-11 21:18:11 -0800510 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700511
512 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800513 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700514 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700515 }
516
517 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800518 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700519 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700520 }
521
522 // To be called by tests to setup the download. The
523 // |starting_offset| parameter is for where to resume.
524 void SetupDownload(off_t starting_offset) {
525 start_at_offset_ = starting_offset;
526 // Prepare data 10 kB of data.
527 data_.clear();
528 for (unsigned int i = 0; i < 10 * 1000; i++)
529 data_ += 'a' + (i % 25);
530
531 // Setup p2p.
532 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500533 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700534 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500535 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700536 fake_system_state_.set_p2p_manager(p2p_manager_.get());
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) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700543 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
544 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'}});
David Zeuthen8f191b22013-08-06 12:27:50 -0700555 ObjectFeederAction<InstallPlan> feeder_action;
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 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
559 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700560 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700561 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Alex Deymo1b3556c2016-02-03 09:54:02 -0800562 download_action_.reset(new DownloadAction(&prefs,
563 fake_system_state_.boot_control(),
564 fake_system_state_.hardware(),
565 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700566 http_fetcher_,
Amin Hassanied37d682018-04-06 13:22:00 -0700567 false /* interactive */));
David Zeuthen8f191b22013-08-06 12:27:50 -0700568 download_action_->SetTestFileWriter(&writer);
569 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700570 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700571 delegate.expected_data_ = brillo::Blob(data_.begin() + start_at_offset_,
572 data_.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800573 delegate.path_ = output_temp_file.path();
David Zeuthen8f191b22013-08-06 12:27:50 -0700574 processor_.set_delegate(&delegate);
575 processor_.EnqueueAction(&feeder_action);
576 processor_.EnqueueAction(download_action_.get());
577
Alex Deymo60ca1a72015-06-18 18:19:15 -0700578 loop_.PostTask(FROM_HERE, base::Bind(
579 &P2PDownloadActionTest::StartProcessorInRunLoopForP2P,
580 base::Unretained(this)));
581 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700582 }
583
Alex Deymo60ca1a72015-06-18 18:19:15 -0700584 // Mainloop used to make StartDownload() synchronous.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700585 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700586
David Zeuthen8f191b22013-08-06 12:27:50 -0700587 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700588 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700589
590 // The HttpFetcher used in the test.
591 MockHttpFetcher* http_fetcher_;
592
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
599 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700600 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700601
602 // The data being downloaded.
603 string data_;
604
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700605 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700606 // Callback used in StartDownload() method.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700607 void StartProcessorInRunLoopForP2P() {
608 processor_.StartProcessing();
Sen Jiangba2213a2018-02-27 16:28:20 -0800609 download_action_->http_fetcher()->SetOffset(start_at_offset_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700610 }
611
David Zeuthen8f191b22013-08-06 12:27:50 -0700612 // The requested starting offset passed to SetupDownload().
613 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700614
615 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700616};
617
618TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800619 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700620 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
621 << "Please update your system to support this feature.";
622 return;
623 }
624
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700625 SetupDownload(0); // starting_offset
626 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700627
628 // Check the p2p file and its content matches what was sent.
629 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700630 EXPECT_NE("", file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800631 EXPECT_EQ(static_cast<int>(data_.length()),
632 p2p_manager_->FileGetSize(file_id));
633 EXPECT_EQ(static_cast<int>(data_.length()),
634 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700635 string p2p_file_contents;
636 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
637 &p2p_file_contents));
638 EXPECT_EQ(data_, p2p_file_contents);
639}
640
641TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800642 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700643 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
644 << "Please update your system to support this feature.";
645 return;
646 }
647
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700648 SetupDownload(1000); // starting_offset
649 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700650
651 // DownloadAction should convey that the file is not being shared.
652 // and that we don't have any p2p files.
653 EXPECT_EQ(download_action_->p2p_file_id(), "");
654 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
655}
656
657TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800658 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700659 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
660 << "Please update your system to support this feature.";
661 return;
662 }
663
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700664 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700665
666 // Prepare the file with existing data before starting to write to
667 // it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700668 string file_id = utils::CalculateP2PFileId(
669 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700670 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
671 string existing_data;
672 for (unsigned int i = 0; i < 1000; i++)
673 existing_data += '0' + (i % 10);
674 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
675 1000), 1000);
676
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700677 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700678
679 // DownloadAction should convey the same file_id and the file should
680 // have the expected size.
681 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800682 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
683 p2p_manager_->FileGetSize(file_id));
684 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
685 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700686 string p2p_file_contents;
687 // Check that the first 1000 bytes wasn't touched and that we
688 // appended the remaining as appropriate.
689 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
690 &p2p_file_contents));
691 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
692 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
693}
694
David Zeuthen8f191b22013-08-06 12:27:50 -0700695TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800696 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700697 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
698 << "Please update your system to support this feature.";
699 return;
700 }
701
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700702 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700703
704 // Prepare the file with all existing data before starting to write
705 // to it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700706 string file_id = utils::CalculateP2PFileId(
707 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700708 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
709 string existing_data;
710 for (unsigned int i = 0; i < 1000; i++)
711 existing_data += '0' + (i % 10);
712 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
713 1000), 1000);
714
715 // Check that the file is there.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800716 EXPECT_EQ(1000, p2p_manager_->FileGetSize(file_id));
717 EXPECT_EQ(1, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700718
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700719 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700720
721 // DownloadAction should have deleted the p2p file. Check that it's gone.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800722 EXPECT_EQ(-1, p2p_manager_->FileGetSize(file_id));
723 EXPECT_EQ(0, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700724}
725
rspangler@google.com49fdf182009-10-10 00:57:34 +0000726} // namespace chromeos_update_engine