blob: 60cc6f2f81a8c0113d2dac0b03555dade40d225f [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>
25#include <vector>
26
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/bind_lambda.h>
33#include <brillo/message_loops/fake_message_loop.h>
34#include <brillo/message_loops/message_loop.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070035
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/action_pipe.h"
37#include "update_engine/common/hash_calculator.h"
38#include "update_engine/common/mock_http_fetcher.h"
39#include "update_engine/common/mock_prefs.h"
40#include "update_engine/common/test_utils.h"
41#include "update_engine/common/utils.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070042#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070043#include "update_engine/fake_system_state.h"
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;
rspangler@google.com49fdf182009-10-10 00:57:34 +000054using std::vector;
Alex Deymoe5e5fe92015-10-05 09:28:19 -070055using test_utils::ScopedTempFile;
Darin Petkov9d911fa2010-08-19 09:36:08 -070056using testing::AtLeast;
57using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070058using testing::Return;
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 Deymoe5e5fe92015-10-05 09:28:19 -0700138 EXPECT_EQ(0, writer.Open(output_temp_file.GetPath().c_str(),
139 O_WRONLY | O_CREAT,
140 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
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700143 // We pull off the first byte from data and seek past it.
Alex Deymo39910dc2015-11-09 17:04:30 -0800144 string hash = HashCalculator::HashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700145 uint64_t size = data.size();
146 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700147 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700148 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700149 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700150 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700151 0,
152 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700153 "");
Alex Deymo5ed695e2015-10-05 16:59:23 -0700154 install_plan.source_slot = 0;
155 install_plan.target_slot = 1;
156 // We mark both slots as bootable. Only the target slot should be unbootable
157 // after the download starts.
158 fake_system_state.fake_boot_control()->SetSlotBootable(
159 install_plan.source_slot, true);
160 fake_system_state.fake_boot_control()->SetSlotBootable(
161 install_plan.target_slot, true);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000162 ObjectFeederAction<InstallPlan> feeder_action;
163 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800164 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800165 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800166 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700167 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700168 // takes ownership of passed in HttpFetcher
Alex Deymo1b3556c2016-02-03 09:54:02 -0800169 DownloadAction download_action(&prefs,
170 fake_system_state.boot_control(),
171 fake_system_state.hardware(),
172 &fake_system_state,
173 http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700174 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000175 BondActions(&feeder_action, &download_action);
Alex Deymo542c19b2015-12-03 07:43:31 -0300176 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700177 if (use_download_delegate) {
178 InSequence s;
179 download_action.set_delegate(&download_delegate);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700180 if (data.size() > kMockHttpFetcherChunkSize)
181 EXPECT_CALL(download_delegate,
Alex Deymo542c19b2015-12-03 07:43:31 -0300182 BytesReceived(_, 1 + kMockHttpFetcherChunkSize, _));
183 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700184 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700185 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700186 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700187 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700188 DownloadActionTestProcessorDelegate delegate(expected_code);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700189 delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700190 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191 ActionProcessor processor;
192 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000193 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000194 processor.EnqueueAction(&download_action);
195
Alex Deymo60ca1a72015-06-18 18:19:15 -0700196 loop.PostTask(FROM_HERE,
197 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
198 loop.Run();
199 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo5ed695e2015-10-05 16:59:23 -0700200
201 EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable(
202 install_plan.source_slot));
203 EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable(
204 install_plan.target_slot));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000205}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700206} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000207
208TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700209 brillo::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000210 const char* foo = "foo";
211 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700212 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800213 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700214 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000215}
216
217TEST(DownloadActionTest, LargeTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700218 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000219 char c = '0';
220 for (unsigned int i = 0; i < big.size(); i++) {
221 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800222 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000223 }
Darin Petkov50332f12010-09-24 11:44:47 -0700224 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800225 0, // fail_write
226 true); // use_download_delegate
227}
228
229TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700230 brillo::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800231 char c = '0';
232 for (unsigned int i = 0; i < big.size(); i++) {
233 big[i] = c;
234 c = ('9' == c) ? '0' : c + 1;
235 }
236 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800237 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700238 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700239}
240
Darin Petkov9d911fa2010-08-19 09:36:08 -0700241TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700242 brillo::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700243 const char* foo = "foofoo";
244 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700245 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800246 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700247 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000248}
249
250namespace {
251class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
252 public:
253 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700254 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000255 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000256};
257
Alex Deymo60ca1a72015-06-18 18:19:15 -0700258void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000259 processor->StartProcessing();
260 CHECK(processor->IsRunning());
261 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000262}
263
Darin Petkov9d911fa2010-08-19 09:36:08 -0700264void TestTerminateEarly(bool use_download_delegate) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700265 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700266 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000267
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700268 brillo::Blob data(kMockHttpFetcherChunkSize +
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800269 kMockHttpFetcherChunkSize / 2);
270 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000271
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700272 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000273 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700274 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700275 EXPECT_EQ(0, writer.Open(temp_file.GetPath().c_str(),
276 O_WRONLY | O_CREAT,
277 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700278
rspangler@google.com49fdf182009-10-10 00:57:34 +0000279 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000280 ObjectFeederAction<InstallPlan> feeder_action;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700281 InstallPlan install_plan(false, false, "", 0, "", 0, "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000282 feeder_action.set_obj(install_plan);
Alex Deymo5ed695e2015-10-05 16:59:23 -0700283 FakeSystemState fake_system_state_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800284 MockPrefs prefs;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800285 DownloadAction download_action(
286 &prefs,
287 fake_system_state_.boot_control(),
288 fake_system_state_.hardware(),
289 &fake_system_state_,
290 new MockHttpFetcher(data.data(), data.size(), nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700291 download_action.SetTestFileWriter(&writer);
Alex Deymo542c19b2015-12-03 07:43:31 -0300292 MockDownloadActionDelegate download_delegate;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700293 if (use_download_delegate) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700294 download_action.set_delegate(&download_delegate);
Alex Deymo542c19b2015-12-03 07:43:31 -0300295 EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(0);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700296 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000297 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000298 ActionProcessor processor;
299 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000300 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000301 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000302 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000303
Alex Deymo60ca1a72015-06-18 18:19:15 -0700304 loop.PostTask(FROM_HERE,
305 base::Bind(&TerminateEarlyTestStarter, &processor));
306 loop.Run();
307 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000308 }
309
310 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700311 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
312 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000313 if (resulting_file_size != 0)
314 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
315}
316
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700317} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700318
319TEST(DownloadActionTest, TerminateEarlyTest) {
320 TestTerminateEarly(true);
321}
322
323TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
324 TestTerminateEarly(false);
325}
326
rspangler@google.com49fdf182009-10-10 00:57:34 +0000327class DownloadActionTestAction;
328
329template<>
330class ActionTraits<DownloadActionTestAction> {
331 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000332 typedef InstallPlan OutputObjectType;
333 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000334};
335
336// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700337class DownloadActionTestAction : public Action<DownloadActionTestAction> {
338 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000339 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000340 typedef InstallPlan InputObjectType;
341 typedef InstallPlan OutputObjectType;
342 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
343 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000344 ActionProcessor* processor() { return processor_; }
345 void PerformAction() {
346 did_run_ = true;
347 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000348 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000349 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700350 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000351 }
352 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000353 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000354 bool did_run_;
355};
356
357namespace {
358// This class is an ActionProcessorDelegate that simply terminates the
359// run loop when the ActionProcessor has completed processing. It's used
360// only by the test PassObjectOutTest.
361class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
362 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700363 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700364 brillo::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000365 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000366};
367
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700368} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000369
370TEST(DownloadActionTest, PassObjectOutTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700371 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700372 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000373
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700374 DirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700375 EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700376
rspangler@google.com49fdf182009-10-10 00:57:34 +0000377 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700378 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700379 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700380 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700381 1,
Alex Deymo39910dc2015-11-09 17:04:30 -0800382 HashCalculator::HashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700383 0,
384 "",
David Zeuthene7f89172013-10-31 10:21:04 -0700385 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000386 ObjectFeederAction<InstallPlan> feeder_action;
387 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800388 MockPrefs prefs;
Alex Deymo5ed695e2015-10-05 16:59:23 -0700389 FakeSystemState fake_system_state_;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800390 DownloadAction download_action(&prefs,
391 fake_system_state_.boot_control(),
392 fake_system_state_.hardware(),
393 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700394 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700395 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000396
397 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000398 test_action.expected_input_object_ = install_plan;
399 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000400 BondActions(&download_action, &test_action);
401
402 ActionProcessor processor;
403 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000404 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000405 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000406 processor.EnqueueAction(&download_action);
407 processor.EnqueueAction(&test_action);
408
Alex Deymo60ca1a72015-06-18 18:19:15 -0700409 loop.PostTask(FROM_HERE,
410 base::Bind([&processor] { processor.StartProcessing(); }));
411 loop.Run();
412 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000413
414 EXPECT_EQ(true, test_action.did_run_);
415}
416
David Zeuthen8f191b22013-08-06 12:27:50 -0700417// Test fixture for P2P tests.
418class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700419 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700420 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700421 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700422 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700423
Alex Deymo610277e2014-11-11 21:18:11 -0800424 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700425
426 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800427 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700428 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700429 }
430
431 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800432 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700433 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700434 }
435
436 // To be called by tests to setup the download. The
437 // |starting_offset| parameter is for where to resume.
438 void SetupDownload(off_t starting_offset) {
439 start_at_offset_ = starting_offset;
440 // Prepare data 10 kB of data.
441 data_.clear();
442 for (unsigned int i = 0; i < 10 * 1000; i++)
443 data_ += 'a' + (i % 25);
444
445 // Setup p2p.
446 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500447 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700448 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500449 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700450 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700451 }
452
453 // To be called by tests to perform the download. The
454 // |use_p2p_to_share| parameter is used to indicate whether the
455 // payload should be shared via p2p.
456 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700457 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
458 GetUsingP2PForSharing())
459 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700460
461 ScopedTempFile output_temp_file;
462 TestDirectFileWriter writer;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700463 EXPECT_EQ(0, writer.Open(output_temp_file.GetPath().c_str(),
464 O_WRONLY | O_CREAT,
465 0));
David Zeuthen8f191b22013-08-06 12:27:50 -0700466 InstallPlan install_plan(false,
467 false,
468 "",
469 data_.length(),
470 "1234hash",
471 0,
472 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700473 "");
474 ObjectFeederAction<InstallPlan> feeder_action;
475 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800476 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700477 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
478 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700479 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700480 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Alex Deymo1b3556c2016-02-03 09:54:02 -0800481 download_action_.reset(new DownloadAction(&prefs,
482 fake_system_state_.boot_control(),
483 fake_system_state_.hardware(),
484 &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700485 http_fetcher_));
486 download_action_->SetTestFileWriter(&writer);
487 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700488 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700489 delegate.expected_data_ = brillo::Blob(data_.begin() + start_at_offset_,
490 data_.end());
David Zeuthen8f191b22013-08-06 12:27:50 -0700491 delegate.path_ = output_temp_file.GetPath();
492 processor_.set_delegate(&delegate);
493 processor_.EnqueueAction(&feeder_action);
494 processor_.EnqueueAction(download_action_.get());
495
Alex Deymo60ca1a72015-06-18 18:19:15 -0700496 loop_.PostTask(FROM_HERE, base::Bind(
497 &P2PDownloadActionTest::StartProcessorInRunLoopForP2P,
498 base::Unretained(this)));
499 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700500 }
501
Alex Deymo60ca1a72015-06-18 18:19:15 -0700502 // Mainloop used to make StartDownload() synchronous.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700503 brillo::FakeMessageLoop loop_{nullptr};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700504
David Zeuthen8f191b22013-08-06 12:27:50 -0700505 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700506 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700507
508 // The HttpFetcher used in the test.
509 MockHttpFetcher* http_fetcher_;
510
511 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700512 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700513
514 // The ActionProcessor used for running the actions.
515 ActionProcessor processor_;
516
517 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700518 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700519
520 // The data being downloaded.
521 string data_;
522
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700523 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700524 // Callback used in StartDownload() method.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700525 void StartProcessorInRunLoopForP2P() {
526 processor_.StartProcessing();
527 http_fetcher_->SetOffset(start_at_offset_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700528 }
529
David Zeuthen8f191b22013-08-06 12:27:50 -0700530 // The requested starting offset passed to SetupDownload().
531 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700532
533 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700534};
535
536TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800537 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700538 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
539 << "Please update your system to support this feature.";
540 return;
541 }
542
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700543 SetupDownload(0); // starting_offset
544 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700545
546 // Check the p2p file and its content matches what was sent.
547 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700548 EXPECT_NE("", file_id);
549 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
550 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700551 string p2p_file_contents;
552 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
553 &p2p_file_contents));
554 EXPECT_EQ(data_, p2p_file_contents);
555}
556
557TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800558 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700559 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
560 << "Please update your system to support this feature.";
561 return;
562 }
563
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700564 SetupDownload(1000); // starting_offset
565 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700566
567 // DownloadAction should convey that the file is not being shared.
568 // and that we don't have any p2p files.
569 EXPECT_EQ(download_action_->p2p_file_id(), "");
570 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
571}
572
573TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800574 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700575 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
576 << "Please update your system to support this feature.";
577 return;
578 }
579
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700580 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700581
582 // Prepare the file with existing data before starting to write to
583 // it via DownloadAction.
584 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
585 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
586 string existing_data;
587 for (unsigned int i = 0; i < 1000; i++)
588 existing_data += '0' + (i % 10);
589 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
590 1000), 1000);
591
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700592 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700593
594 // DownloadAction should convey the same file_id and the file should
595 // have the expected size.
596 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
597 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
598 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
599 string p2p_file_contents;
600 // Check that the first 1000 bytes wasn't touched and that we
601 // appended the remaining as appropriate.
602 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
603 &p2p_file_contents));
604 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
605 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
606}
607
David Zeuthen8f191b22013-08-06 12:27:50 -0700608TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800609 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700610 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
611 << "Please update your system to support this feature.";
612 return;
613 }
614
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700615 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700616
617 // Prepare the file with all existing data before starting to write
618 // to it via DownloadAction.
619 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
620 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
621 string existing_data;
622 for (unsigned int i = 0; i < 1000; i++)
623 existing_data += '0' + (i % 10);
624 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
625 1000), 1000);
626
627 // Check that the file is there.
628 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
629 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
630
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700631 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700632
633 // DownloadAction should have deleted the p2p file. Check that it's gone.
634 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
635 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
636}
637
rspangler@google.com49fdf182009-10-10 00:57:34 +0000638} // namespace chromeos_update_engine