blob: 486f2c555104a85b12d4cc48632a164a3227683d [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 Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/download_action.h"
18
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 Deymo60ca1a72015-06-18 18:19:15 -070032#include <chromeos/bind_lambda.h>
33#include <chromeos/message_loops/fake_message_loop.h>
34#include <chromeos/message_loops/message_loop.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070035
rspangler@google.com49fdf182009-10-10 00:57:34 +000036#include "update_engine/action_pipe.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070037#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070038#include "update_engine/fake_system_state.h"
39#include "update_engine/mock_http_fetcher.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080040#include "update_engine/mock_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070041#include "update_engine/omaha_hash_calculator.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000042#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070043#include "update_engine/update_manager/fake_update_manager.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000044#include "update_engine/utils.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;
rspangler@google.com49fdf182009-10-10 00:57:34 +000053using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070054using testing::AtLeast;
55using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070056using testing::Return;
Alex Deymof329b932014-10-30 01:37:48 -070057using testing::_;
Alex Deymo10875d92014-11-10 21:52:57 -080058using test_utils::ScopedTempFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000059
60class DownloadActionTest : public ::testing::Test { };
61
62namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070063class DownloadActionDelegateMock : public DownloadActionDelegate {
64 public:
65 MOCK_METHOD1(SetDownloadStatus, void(bool active));
66 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
67};
68
rspangler@google.com49fdf182009-10-10 00:57:34 +000069class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
70 public:
David Zeuthena99981f2013-04-29 13:42:47 -070071 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Deymo60ca1a72015-06-18 18:19:15 -070072 : processing_done_called_(false),
Darin Petkovc97435c2010-07-20 12:37:43 -070073 expected_code_(expected_code) {}
Alex Deymo610277e2014-11-11 21:18:11 -080074 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 EXPECT_TRUE(processing_done_called_);
76 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080077 void ProcessingDone(const ActionProcessor* processor,
78 ErrorCode code) override {
Alex Deymo60ca1a72015-06-18 18:19:15 -070079 chromeos::MessageLoop::current()->BreakLoop();
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080080 chromeos::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000081 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070082 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080083 ASSERT_EQ(expected_data_.size(), found_data.size());
84 for (unsigned i = 0; i < expected_data_.size(); i++) {
85 EXPECT_EQ(expected_data_[i], found_data[i]);
86 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000087 }
88 processing_done_called_ = true;
89 }
90
Yunlian Jiang35866ed2015-01-29 13:09:20 -080091 void ActionCompleted(ActionProcessor* processor,
92 AbstractAction* action,
93 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070094 const string type = action->Type();
95 if (type == DownloadAction::StaticType()) {
96 EXPECT_EQ(expected_code_, code);
97 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070098 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070099 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 }
101
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102 string path_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800103 chromeos::Blob expected_data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -0700105 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106};
107
Darin Petkov9ce452b2010-11-17 14:33:28 -0800108class TestDirectFileWriter : public DirectFileWriter {
109 public:
110 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
111 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
112
Don Garrette410e0f2011-11-10 15:39:01 -0800113 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800114 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800115 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800116 }
117 return DirectFileWriter::Write(bytes, count);
118 }
119
120 private:
121 // If positive, fail on the |fail_write_| call to Write.
122 int fail_write_;
123 int current_write_;
124};
125
Alex Deymo60ca1a72015-06-18 18:19:15 -0700126void StartProcessorInRunLoop(ActionProcessor* processor,
127 MockHttpFetcher* http_fetcher) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700129 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130}
131
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800132void TestWithData(const chromeos::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800133 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700134 bool use_download_delegate) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700135 chromeos::FakeMessageLoop loop(nullptr);
136 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137
138 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700139 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800140 TestDirectFileWriter writer;
141 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.
144
Darin Petkov7ed561b2011-10-04 02:59:03 -0700145 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700146 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700147 uint64_t size = data.size();
148 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700149 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700150 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700151 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700152 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700153 0,
154 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700155 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700156 "",
Allie Woodfdf00512015-03-02 13:34:55 -0800157 "",
158 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700159 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000160 ObjectFeederAction<InstallPlan> feeder_action;
161 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800162 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800163 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800164 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700165 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700166 // takes ownership of passed in HttpFetcher
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700167 DownloadAction download_action(&prefs, nullptr, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700168 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000169 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700170 DownloadActionDelegateMock download_delegate;
171 if (use_download_delegate) {
172 InSequence s;
173 download_action.set_delegate(&download_delegate);
174 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700175 if (data.size() > kMockHttpFetcherChunkSize)
176 EXPECT_CALL(download_delegate,
177 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800178 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700179 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
180 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700181 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700182 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700183 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700184 DownloadActionTestProcessorDelegate delegate(expected_code);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800185 delegate.expected_data_ = chromeos::Blob(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700186 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000187 ActionProcessor processor;
188 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000189 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000190 processor.EnqueueAction(&download_action);
191
Alex Deymo60ca1a72015-06-18 18:19:15 -0700192 loop.PostTask(FROM_HERE,
193 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
194 loop.Run();
195 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000196}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700197} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000198
199TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800200 chromeos::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000201 const char* foo = "foo";
202 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700203 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800204 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700205 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000206}
207
208TEST(DownloadActionTest, LargeTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800209 chromeos::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000210 char c = '0';
211 for (unsigned int i = 0; i < big.size(); i++) {
212 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800213 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000214 }
Darin Petkov50332f12010-09-24 11:44:47 -0700215 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800216 0, // fail_write
217 true); // use_download_delegate
218}
219
220TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800221 chromeos::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800222 char c = '0';
223 for (unsigned int i = 0; i < big.size(); i++) {
224 big[i] = c;
225 c = ('9' == c) ? '0' : c + 1;
226 }
227 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800228 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700229 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700230}
231
Darin Petkov9d911fa2010-08-19 09:36:08 -0700232TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800233 chromeos::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700234 const char* foo = "foofoo";
235 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700236 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800237 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700238 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000239}
240
241namespace {
242class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
243 public:
244 void ProcessingStopped(const ActionProcessor* processor) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700245 chromeos::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000246 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000247};
248
Alex Deymo60ca1a72015-06-18 18:19:15 -0700249void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000250 processor->StartProcessing();
251 CHECK(processor->IsRunning());
252 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000253}
254
Darin Petkov9d911fa2010-08-19 09:36:08 -0700255void TestTerminateEarly(bool use_download_delegate) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700256 chromeos::FakeMessageLoop loop(nullptr);
257 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000258
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800259 chromeos::Blob data(kMockHttpFetcherChunkSize +
260 kMockHttpFetcherChunkSize / 2);
261 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000262
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700263 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000264 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700265 DirectFileWriter writer;
266
rspangler@google.com49fdf182009-10-10 00:57:34 +0000267 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000268 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700269 InstallPlan install_plan(false, false, "", 0, "", 0, "",
Allie Woodfdf00512015-03-02 13:34:55 -0800270 temp_file.GetPath(), "", "", "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000271 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800272 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700273 DownloadAction download_action(&prefs, nullptr,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800274 new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800275 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700276 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700277 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700278 DownloadActionDelegateMock download_delegate;
279 if (use_download_delegate) {
280 InSequence s;
281 download_action.set_delegate(&download_delegate);
282 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
283 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
284 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000285 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000286 ActionProcessor processor;
287 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000288 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000289 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000290 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000291
Alex Deymo60ca1a72015-06-18 18:19:15 -0700292 loop.PostTask(FROM_HERE,
293 base::Bind(&TerminateEarlyTestStarter, &processor));
294 loop.Run();
295 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000296 }
297
298 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700299 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
300 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000301 if (resulting_file_size != 0)
302 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
303}
304
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700305} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700306
307TEST(DownloadActionTest, TerminateEarlyTest) {
308 TestTerminateEarly(true);
309}
310
311TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
312 TestTerminateEarly(false);
313}
314
rspangler@google.com49fdf182009-10-10 00:57:34 +0000315class DownloadActionTestAction;
316
317template<>
318class ActionTraits<DownloadActionTestAction> {
319 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000320 typedef InstallPlan OutputObjectType;
321 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000322};
323
324// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700325class DownloadActionTestAction : public Action<DownloadActionTestAction> {
326 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000327 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000328 typedef InstallPlan InputObjectType;
329 typedef InstallPlan OutputObjectType;
330 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
331 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000332 ActionProcessor* processor() { return processor_; }
333 void PerformAction() {
334 did_run_ = true;
335 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000336 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000337 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700338 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000339 }
340 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000341 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000342 bool did_run_;
343};
344
345namespace {
346// This class is an ActionProcessorDelegate that simply terminates the
347// run loop when the ActionProcessor has completed processing. It's used
348// only by the test PassObjectOutTest.
349class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
350 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700351 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700352 chromeos::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000353 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000354};
355
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700356} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000357
358TEST(DownloadActionTest, PassObjectOutTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700359 chromeos::FakeMessageLoop loop(nullptr);
360 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000361
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700362 DirectFileWriter writer;
363
rspangler@google.com49fdf182009-10-10 00:57:34 +0000364 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700365 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700366 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700367 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700368 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800369 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700370 0,
371 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700372 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700373 "/dev/null",
Allie Woodfdf00512015-03-02 13:34:55 -0800374 "/dev/null",
375 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700376 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000377 ObjectFeederAction<InstallPlan> feeder_action;
378 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800379 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700380 DownloadAction download_action(&prefs, nullptr,
381 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700382 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000383
384 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000385 test_action.expected_input_object_ = install_plan;
386 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000387 BondActions(&download_action, &test_action);
388
389 ActionProcessor processor;
390 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000391 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000392 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000393 processor.EnqueueAction(&download_action);
394 processor.EnqueueAction(&test_action);
395
Alex Deymo60ca1a72015-06-18 18:19:15 -0700396 loop.PostTask(FROM_HERE,
397 base::Bind([&processor] { processor.StartProcessing(); }));
398 loop.Run();
399 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000400
401 EXPECT_EQ(true, test_action.did_run_);
402}
403
404TEST(DownloadActionTest, BadOutFileTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700405 chromeos::FakeMessageLoop loop(nullptr);
406 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000407
408 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700409 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000410
411 // takes ownership of passed in HttpFetcher
Allie Woodfdf00512015-03-02 13:34:55 -0800412 InstallPlan install_plan(
413 false, false, "", 0, "", 0, "", path, "", "", "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000414 ObjectFeederAction<InstallPlan> feeder_action;
415 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800416 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700417 DownloadAction download_action(&prefs, nullptr,
418 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700419 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700420
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000421 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000422
423 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000424 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000425 processor.EnqueueAction(&download_action);
426 processor.StartProcessing();
427 ASSERT_FALSE(processor.IsRunning());
428
Alex Deymo60ca1a72015-06-18 18:19:15 -0700429 loop.Run();
430 EXPECT_FALSE(loop.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700431}
432
433// Test fixture for P2P tests.
434class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700435 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700436 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700437 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700438 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700439
Alex Deymo610277e2014-11-11 21:18:11 -0800440 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700441
442 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800443 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700444 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700445 }
446
447 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800448 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700449 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700450 }
451
452 // To be called by tests to setup the download. The
453 // |starting_offset| parameter is for where to resume.
454 void SetupDownload(off_t starting_offset) {
455 start_at_offset_ = starting_offset;
456 // Prepare data 10 kB of data.
457 data_.clear();
458 for (unsigned int i = 0; i < 10 * 1000; i++)
459 data_ += 'a' + (i % 25);
460
461 // Setup p2p.
462 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500463 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700464 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500465 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700466 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700467 }
468
469 // To be called by tests to perform the download. The
470 // |use_p2p_to_share| parameter is used to indicate whether the
471 // payload should be shared via p2p.
472 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700473 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
474 GetUsingP2PForSharing())
475 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700476
477 ScopedTempFile output_temp_file;
478 TestDirectFileWriter writer;
479 InstallPlan install_plan(false,
480 false,
481 "",
482 data_.length(),
483 "1234hash",
484 0,
485 "",
486 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700487 "",
Allie Woodfdf00512015-03-02 13:34:55 -0800488 "",
489 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700490 "");
491 ObjectFeederAction<InstallPlan> feeder_action;
492 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800493 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700494 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
495 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700496 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700497 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700498 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700499 http_fetcher_));
500 download_action_->SetTestFileWriter(&writer);
501 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700502 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800503 delegate.expected_data_ = chromeos::Blob(data_.begin() + start_at_offset_,
504 data_.end());
David Zeuthen8f191b22013-08-06 12:27:50 -0700505 delegate.path_ = output_temp_file.GetPath();
506 processor_.set_delegate(&delegate);
507 processor_.EnqueueAction(&feeder_action);
508 processor_.EnqueueAction(download_action_.get());
509
Alex Deymo60ca1a72015-06-18 18:19:15 -0700510 loop_.PostTask(FROM_HERE, base::Bind(
511 &P2PDownloadActionTest::StartProcessorInRunLoopForP2P,
512 base::Unretained(this)));
513 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700514 }
515
Alex Deymo60ca1a72015-06-18 18:19:15 -0700516 // Mainloop used to make StartDownload() synchronous.
517 chromeos::FakeMessageLoop loop_{nullptr};
518
David Zeuthen8f191b22013-08-06 12:27:50 -0700519 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700520 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700521
522 // The HttpFetcher used in the test.
523 MockHttpFetcher* http_fetcher_;
524
525 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700526 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700527
528 // The ActionProcessor used for running the actions.
529 ActionProcessor processor_;
530
531 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700532 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700533
534 // The data being downloaded.
535 string data_;
536
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700537 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700538 // Callback used in StartDownload() method.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700539 void StartProcessorInRunLoopForP2P() {
540 processor_.StartProcessing();
541 http_fetcher_->SetOffset(start_at_offset_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700542 }
543
David Zeuthen8f191b22013-08-06 12:27:50 -0700544 // The requested starting offset passed to SetupDownload().
545 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700546
547 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700548};
549
550TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800551 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700552 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
553 << "Please update your system to support this feature.";
554 return;
555 }
556
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700557 SetupDownload(0); // starting_offset
558 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700559
560 // Check the p2p file and its content matches what was sent.
561 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700562 EXPECT_NE("", file_id);
563 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
564 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700565 string p2p_file_contents;
566 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
567 &p2p_file_contents));
568 EXPECT_EQ(data_, p2p_file_contents);
569}
570
571TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800572 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700573 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
574 << "Please update your system to support this feature.";
575 return;
576 }
577
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700578 SetupDownload(1000); // starting_offset
579 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700580
581 // DownloadAction should convey that the file is not being shared.
582 // and that we don't have any p2p files.
583 EXPECT_EQ(download_action_->p2p_file_id(), "");
584 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
585}
586
587TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800588 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700589 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
590 << "Please update your system to support this feature.";
591 return;
592 }
593
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700594 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700595
596 // Prepare the file with existing data before starting to write to
597 // it via DownloadAction.
598 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
599 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
600 string existing_data;
601 for (unsigned int i = 0; i < 1000; i++)
602 existing_data += '0' + (i % 10);
603 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
604 1000), 1000);
605
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700606 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700607
608 // DownloadAction should convey the same file_id and the file should
609 // have the expected size.
610 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
611 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
612 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
613 string p2p_file_contents;
614 // Check that the first 1000 bytes wasn't touched and that we
615 // appended the remaining as appropriate.
616 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
617 &p2p_file_contents));
618 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
619 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
620}
621
David Zeuthen8f191b22013-08-06 12:27:50 -0700622TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800623 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700624 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
625 << "Please update your system to support this feature.";
626 return;
627 }
628
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700629 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700630
631 // Prepare the file with all existing data before starting to write
632 // to it via DownloadAction.
633 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
634 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
635 string existing_data;
636 for (unsigned int i = 0; i < 1000; i++)
637 existing_data += '0' + (i % 10);
638 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
639 1000), 1000);
640
641 // Check that the file is there.
642 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
643 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
644
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700645 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700646
647 // DownloadAction should have deleted the p2p file. Check that it's gone.
648 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
649 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
650}
651
rspangler@google.com49fdf182009-10-10 00:57:34 +0000652} // namespace chromeos_update_engine