blob: 21ce4610dd3803b61103f6ecafa769451c98b4cf [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>
David Zeuthen8f191b22013-08-06 12:27:50 -070025
Alex Deymo60ca1a72015-06-18 18:19:15 -070026#include <base/bind.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070027#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070028#include <base/files/file_util.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070029#include <base/location.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070030#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/message_loops/fake_message_loop.h>
32#include <brillo/message_loops/message_loop.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070033
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/action_pipe.h"
35#include "update_engine/common/hash_calculator.h"
36#include "update_engine/common/mock_http_fetcher.h"
37#include "update_engine/common/mock_prefs.h"
38#include "update_engine/common/test_utils.h"
39#include "update_engine/common/utils.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070040#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070041#include "update_engine/fake_system_state.h"
Aaron Wood9321f502017-09-07 11:18:54 -070042#include "update_engine/mock_file_writer.h"
Alex Deymo542c19b2015-12-03 07:43:31 -030043#include "update_engine/payload_consumer/mock_download_action.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070044#include "update_engine/update_manager/fake_update_manager.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000045
46namespace chromeos_update_engine {
47
Alex Deymof329b932014-10-30 01:37:48 -070048using base::FilePath;
49using base::ReadFileToString;
50using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000051using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070052using std::unique_ptr;
Alex Deymoe5e5fe92015-10-05 09:28:19 -070053using test_utils::ScopedTempFile;
Darin Petkov9d911fa2010-08-19 09:36:08 -070054using testing::AtLeast;
55using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070056using testing::Return;
Aaron Wood9321f502017-09-07 11:18:54 -070057using testing::SetArgPointee;
Alex Deymof329b932014-10-30 01:37:48 -070058using testing::_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000059
60class DownloadActionTest : public ::testing::Test { };
61
62namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070063
rspangler@google.com49fdf182009-10-10 00:57:34 +000064class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
65 public:
David Zeuthena99981f2013-04-29 13:42:47 -070066 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Deymo60ca1a72015-06-18 18:19:15 -070067 : processing_done_called_(false),
Darin Petkovc97435c2010-07-20 12:37:43 -070068 expected_code_(expected_code) {}
Alex Deymo610277e2014-11-11 21:18:11 -080069 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 EXPECT_TRUE(processing_done_called_);
71 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080072 void ProcessingDone(const ActionProcessor* processor,
73 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070074 brillo::MessageLoop::current()->BreakLoop();
75 brillo::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000076 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070077 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080078 ASSERT_EQ(expected_data_.size(), found_data.size());
79 for (unsigned i = 0; i < expected_data_.size(); i++) {
80 EXPECT_EQ(expected_data_[i], found_data[i]);
81 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 }
83 processing_done_called_ = true;
84 }
85
Yunlian Jiang35866ed2015-01-29 13:09:20 -080086 void ActionCompleted(ActionProcessor* processor,
87 AbstractAction* action,
88 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070089 const string type = action->Type();
90 if (type == DownloadAction::StaticType()) {
91 EXPECT_EQ(expected_code_, code);
92 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070093 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070094 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000095 }
96
rspangler@google.com49fdf182009-10-10 00:57:34 +000097 string path_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070098 brillo::Blob expected_data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000099 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -0700100 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101};
102
Darin Petkov9ce452b2010-11-17 14:33:28 -0800103class TestDirectFileWriter : public DirectFileWriter {
104 public:
105 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
106 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
107
Don Garrette410e0f2011-11-10 15:39:01 -0800108 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800109 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800110 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800111 }
112 return DirectFileWriter::Write(bytes, count);
113 }
114
115 private:
116 // If positive, fail on the |fail_write_| call to Write.
117 int fail_write_;
118 int current_write_;
119};
120
Alex Deymo60ca1a72015-06-18 18:19:15 -0700121void StartProcessorInRunLoop(ActionProcessor* processor,
122 MockHttpFetcher* http_fetcher) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700124 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125}
126
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700127void TestWithData(const brillo::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800128 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700129 bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700130 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700131 loop.SetAsCurrent();
Alex Deymo5ed695e2015-10-05 16:59:23 -0700132 FakeSystemState fake_system_state;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133
134 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700135 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800136 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800137 EXPECT_EQ(
138 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800139 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700140
Sen Jiang5ae865b2017-04-18 14:24:40 -0700141 uint64_t size = data.size() - 1;
Alex Deymo64d98782016-02-05 18:03:48 -0800142 InstallPlan install_plan;
Sen Jiangcdd52062017-05-18 15:33:10 -0700143 install_plan.payloads.push_back(
144 {.size = size, .type = InstallPayloadType::kDelta});
Sen Jiang2703ef42017-03-16 13:36:21 -0700145 // We pull off the first byte from data and seek past it.
146 EXPECT_TRUE(HashCalculator::RawHashOfBytes(
Sen Jiang0affc2c2017-02-10 15:55:05 -0800147 &data[1], data.size() - 1, &install_plan.payloads[0].hash));
Alex Deymo5ed695e2015-10-05 16:59:23 -0700148 install_plan.source_slot = 0;
149 install_plan.target_slot = 1;
150 // We mark both slots as bootable. Only the target slot should be unbootable
151 // after the download starts.
152 fake_system_state.fake_boot_control()->SetSlotBootable(
153 install_plan.source_slot, true);
154 fake_system_state.fake_boot_control()->SetSlotBootable(
155 install_plan.target_slot, true);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000156 ObjectFeederAction<InstallPlan> feeder_action;
157 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800158 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800159 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800160 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700161 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700162 // takes ownership of passed in HttpFetcher
Alex Deymo1b3556c2016-02-03 09:54:02 -0800163 DownloadAction download_action(&prefs,
164 fake_system_state.boot_control(),
165 fake_system_state.hardware(),
166 &fake_system_state,
Amin Hassani7ecda262017-07-11 17:10:50 -0700167 http_fetcher,
168 false /* is_interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700169 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000170 BondActions(&feeder_action, &download_action);
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;
174 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));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700179 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700180 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700181 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700182 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700183 DownloadActionTestProcessorDelegate delegate(expected_code);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700184 delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800185 delegate.path_ = output_temp_file.path();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000186 ActionProcessor processor;
187 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000188 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000189 processor.EnqueueAction(&download_action);
190
Alex Deymo60ca1a72015-06-18 18:19:15 -0700191 loop.PostTask(FROM_HERE,
192 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
193 loop.Run();
194 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo5ed695e2015-10-05 16:59:23 -0700195
196 EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable(
197 install_plan.source_slot));
198 EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable(
199 install_plan.target_slot));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000200}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700201} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000202
203TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700204 brillo::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000205 const char* foo = "foo";
206 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700207 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800208 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700209 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000210}
211
212TEST(DownloadActionTest, LargeTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700213 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000214 char c = '0';
215 for (unsigned int i = 0; i < big.size(); i++) {
216 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800217 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000218 }
Darin Petkov50332f12010-09-24 11:44:47 -0700219 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800220 0, // fail_write
221 true); // use_download_delegate
222}
223
224TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700225 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800226 char c = '0';
227 for (unsigned int i = 0; i < big.size(); i++) {
228 big[i] = c;
229 c = ('9' == c) ? '0' : c + 1;
230 }
231 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800232 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700233 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700234}
235
Darin Petkov9d911fa2010-08-19 09:36:08 -0700236TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700237 brillo::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700238 const char* foo = "foofoo";
239 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700240 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800241 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700242 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000243}
244
Aaron Wood9321f502017-09-07 11:18:54 -0700245TEST(DownloadActionTest, MultiPayloadProgressTest) {
246 std::vector<brillo::Blob> payload_datas;
247 // the first payload must be the largest, as it's the actual payload used by
248 // the MockHttpFetcher for all downloaded data.
249 payload_datas.emplace_back(4 * kMockHttpFetcherChunkSize + 256);
250 payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize);
251 brillo::FakeMessageLoop loop(nullptr);
252 loop.SetAsCurrent();
253 FakeSystemState fake_system_state;
254 EXPECT_CALL(*fake_system_state.mock_payload_state(), NextPayload())
255 .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 }
271 ObjectFeederAction<InstallPlan> feeder_action;
272 feeder_action.set_obj(install_plan);
273 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
277 DownloadAction download_action(&prefs,
278 fake_system_state.boot_control(),
279 fake_system_state.hardware(),
280 &fake_system_state,
Sen Jiang18414082018-01-11 14:50:36 -0800281 http_fetcher,
282 false /* is_interactive */);
Aaron Wood9321f502017-09-07 11:18:54 -0700283 download_action.SetTestFileWriter(&mock_file_writer);
284 BondActions(&feeder_action, &download_action);
285 MockDownloadActionDelegate download_delegate;
286 {
287 InSequence s;
288 download_action.set_delegate(&download_delegate);
289 // 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;
320 processor.EnqueueAction(&feeder_action);
321 processor.EnqueueAction(&download_action);
322
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) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700347 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700348 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000349
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700350 brillo::Blob data(kMockHttpFetcherChunkSize +
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800351 kMockHttpFetcherChunkSize / 2);
352 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
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000360 ObjectFeederAction<InstallPlan> feeder_action;
Alex Deymo64d98782016-02-05 18:03:48 -0800361 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800362 install_plan.payloads.resize(1);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000363 feeder_action.set_obj(install_plan);
Alex Deymo5ed695e2015-10-05 16:59:23 -0700364 FakeSystemState fake_system_state_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800365 MockPrefs prefs;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800366 DownloadAction download_action(
367 &prefs,
368 fake_system_state_.boot_control(),
369 fake_system_state_.hardware(),
370 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700371 new MockHttpFetcher(data.data(), data.size(), nullptr),
372 false /* is_interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -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) {
Darin Petkov9d911fa2010-08-19 09:36:08 -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);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000382 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000383 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000384 BondActions(&feeder_action, &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
412template<>
413class 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:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000422 DownloadActionTestAction() : did_run_(false) {}
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() {
429 did_run_ = true;
430 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000431 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000432 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700433 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000434 }
435 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000436 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000437 bool did_run_;
438};
439
440namespace {
441// This class is an ActionProcessorDelegate that simply terminates the
442// run loop when the ActionProcessor has completed processing. It's used
443// only by the test PassObjectOutTest.
444class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
445 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700446 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700447 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000448 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000449};
450
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700451} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000452
453TEST(DownloadActionTest, PassObjectOutTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700454 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700455 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000456
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700457 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700458 EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700459
rspangler@google.com49fdf182009-10-10 00:57:34 +0000460 // takes ownership of passed in HttpFetcher
Alex Deymo64d98782016-02-05 18:03:48 -0800461 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800462 install_plan.payloads.push_back({.size = 1});
463 EXPECT_TRUE(
464 HashCalculator::RawHashOfData({'x'}, &install_plan.payloads[0].hash));
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000465 ObjectFeederAction<InstallPlan> feeder_action;
466 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800467 MockPrefs prefs;
Alex Deymo5ed695e2015-10-05 16:59:23 -0700468 FakeSystemState fake_system_state_;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800469 DownloadAction download_action(&prefs,
470 fake_system_state_.boot_control(),
471 fake_system_state_.hardware(),
472 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700473 new MockHttpFetcher("x", 1, nullptr),
474 false /* is_interactive */);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700475 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000476
477 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000478 test_action.expected_input_object_ = install_plan;
479 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000480 BondActions(&download_action, &test_action);
481
482 ActionProcessor processor;
483 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000484 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000485 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000486 processor.EnqueueAction(&download_action);
487 processor.EnqueueAction(&test_action);
488
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700489 loop.PostTask(
490 FROM_HERE,
491 base::Bind(
492 [](ActionProcessor* processor) { processor->StartProcessing(); },
493 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700494 loop.Run();
495 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000496
497 EXPECT_EQ(true, test_action.did_run_);
498}
499
David Zeuthen8f191b22013-08-06 12:27:50 -0700500// Test fixture for P2P tests.
501class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700502 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700503 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700504 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700505 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700506
Alex Deymo610277e2014-11-11 21:18:11 -0800507 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700508
509 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800510 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700511 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700512 }
513
514 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800515 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700516 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700517 }
518
519 // To be called by tests to setup the download. The
520 // |starting_offset| parameter is for where to resume.
521 void SetupDownload(off_t starting_offset) {
522 start_at_offset_ = starting_offset;
523 // Prepare data 10 kB of data.
524 data_.clear();
525 for (unsigned int i = 0; i < 10 * 1000; i++)
526 data_ += 'a' + (i % 25);
527
528 // Setup p2p.
529 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500530 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700531 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500532 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700533 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700534 }
535
536 // To be called by tests to perform the download. The
537 // |use_p2p_to_share| parameter is used to indicate whether the
538 // payload should be shared via p2p.
539 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700540 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
541 GetUsingP2PForSharing())
542 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700543
544 ScopedTempFile output_temp_file;
545 TestDirectFileWriter writer;
Alex Deymobffa0602016-02-12 17:16:29 -0800546 EXPECT_EQ(
547 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
Alex Deymo64d98782016-02-05 18:03:48 -0800548 InstallPlan install_plan;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800549 install_plan.payloads.push_back(
550 {.size = data_.length(),
551 .hash = {'1', '2', '3', '4', 'h', 'a', 's', 'h'}});
David Zeuthen8f191b22013-08-06 12:27:50 -0700552 ObjectFeederAction<InstallPlan> feeder_action;
553 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800554 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700555 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
556 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700557 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700558 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Alex Deymo1b3556c2016-02-03 09:54:02 -0800559 download_action_.reset(new DownloadAction(&prefs,
560 fake_system_state_.boot_control(),
561 fake_system_state_.hardware(),
562 &fake_system_state_,
Amin Hassani7ecda262017-07-11 17:10:50 -0700563 http_fetcher_,
564 false /* is_interactive */));
David Zeuthen8f191b22013-08-06 12:27:50 -0700565 download_action_->SetTestFileWriter(&writer);
566 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700567 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700568 delegate.expected_data_ = brillo::Blob(data_.begin() + start_at_offset_,
569 data_.end());
Alex Deymobffa0602016-02-12 17:16:29 -0800570 delegate.path_ = output_temp_file.path();
David Zeuthen8f191b22013-08-06 12:27:50 -0700571 processor_.set_delegate(&delegate);
572 processor_.EnqueueAction(&feeder_action);
573 processor_.EnqueueAction(download_action_.get());
574
Alex Deymo60ca1a72015-06-18 18:19:15 -0700575 loop_.PostTask(FROM_HERE, base::Bind(
576 &P2PDownloadActionTest::StartProcessorInRunLoopForP2P,
577 base::Unretained(this)));
578 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700579 }
580
Alex Deymo60ca1a72015-06-18 18:19:15 -0700581 // Mainloop used to make StartDownload() synchronous.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700582 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700583
David Zeuthen8f191b22013-08-06 12:27:50 -0700584 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700585 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700586
587 // The HttpFetcher used in the test.
588 MockHttpFetcher* http_fetcher_;
589
590 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700591 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700592
593 // The ActionProcessor used for running the actions.
594 ActionProcessor processor_;
595
596 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700597 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700598
599 // 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 // Callback used in StartDownload() method.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700604 void StartProcessorInRunLoopForP2P() {
605 processor_.StartProcessing();
606 http_fetcher_->SetOffset(start_at_offset_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700607 }
608
David Zeuthen8f191b22013-08-06 12:27:50 -0700609 // The requested starting offset passed to SetupDownload().
610 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700611
612 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700613};
614
615TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800616 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700617 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
618 << "Please update your system to support this feature.";
619 return;
620 }
621
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700622 SetupDownload(0); // starting_offset
623 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700624
625 // Check the p2p file and its content matches what was sent.
626 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700627 EXPECT_NE("", file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800628 EXPECT_EQ(static_cast<int>(data_.length()),
629 p2p_manager_->FileGetSize(file_id));
630 EXPECT_EQ(static_cast<int>(data_.length()),
631 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700632 string p2p_file_contents;
633 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
634 &p2p_file_contents));
635 EXPECT_EQ(data_, p2p_file_contents);
636}
637
638TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800639 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700640 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
641 << "Please update your system to support this feature.";
642 return;
643 }
644
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700645 SetupDownload(1000); // starting_offset
646 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700647
648 // DownloadAction should convey that the file is not being shared.
649 // and that we don't have any p2p files.
650 EXPECT_EQ(download_action_->p2p_file_id(), "");
651 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
652}
653
654TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800655 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700656 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
657 << "Please update your system to support this feature.";
658 return;
659 }
660
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700661 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700662
663 // Prepare the file with existing data before starting to write to
664 // it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700665 string file_id = utils::CalculateP2PFileId(
666 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700667 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
668 string existing_data;
669 for (unsigned int i = 0; i < 1000; i++)
670 existing_data += '0' + (i % 10);
671 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
672 1000), 1000);
673
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700674 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700675
676 // DownloadAction should convey the same file_id and the file should
677 // have the expected size.
678 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
Alex Deymo80f70ff2016-02-10 16:08:11 -0800679 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
680 p2p_manager_->FileGetSize(file_id));
681 EXPECT_EQ(static_cast<ssize_t>(data_.length()),
682 p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700683 string p2p_file_contents;
684 // Check that the first 1000 bytes wasn't touched and that we
685 // appended the remaining as appropriate.
686 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
687 &p2p_file_contents));
688 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
689 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
690}
691
David Zeuthen8f191b22013-08-06 12:27:50 -0700692TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800693 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700694 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
695 << "Please update your system to support this feature.";
696 return;
697 }
698
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700699 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700700
701 // Prepare the file with all existing data before starting to write
702 // to it via DownloadAction.
Sen Jiang2703ef42017-03-16 13:36:21 -0700703 string file_id = utils::CalculateP2PFileId(
704 {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
David Zeuthen8f191b22013-08-06 12:27:50 -0700705 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
706 string existing_data;
707 for (unsigned int i = 0; i < 1000; i++)
708 existing_data += '0' + (i % 10);
709 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
710 1000), 1000);
711
712 // Check that the file is there.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800713 EXPECT_EQ(1000, p2p_manager_->FileGetSize(file_id));
714 EXPECT_EQ(1, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700715
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700716 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700717
718 // DownloadAction should have deleted the p2p file. Check that it's gone.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800719 EXPECT_EQ(-1, p2p_manager_->FileGetSize(file_id));
720 EXPECT_EQ(0, p2p_manager_->CountSharedFiles());
David Zeuthen8f191b22013-08-06 12:27:50 -0700721}
722
rspangler@google.com49fdf182009-10-10 00:57:34 +0000723} // namespace chromeos_update_engine