blob: 254dbd7a1517e4cf2377022f1aeac8e59c7195e5 [file] [log] [blame]
Darin Petkov7ed561b2011-10-04 02:59:03 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo8427b4a2014-11-05 14:00:32 -08005#include "update_engine/download_action.h"
6
rspangler@google.com49fdf182009-10-10 00:57:34 +00007#include <glib.h>
Darin Petkov9d911fa2010-08-19 09:36:08 -07008#include <gmock/gmock.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00009#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -070010
Ben Chan02f7c1d2014-10-18 15:18:02 -070011#include <memory>
David Zeuthen8f191b22013-08-06 12:27:50 -070012#include <string>
13#include <utility>
14#include <vector>
15
Alex Deymo60ca1a72015-06-18 18:19:15 -070016#include <base/bind.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070017#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070018#include <base/files/file_util.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070019#include <base/location.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070020#include <base/strings/stringprintf.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070021#include <chromeos/bind_lambda.h>
22#include <chromeos/message_loops/fake_message_loop.h>
23#include <chromeos/message_loops/message_loop.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070024
rspangler@google.com49fdf182009-10-10 00:57:34 +000025#include "update_engine/action_pipe.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070026#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070027#include "update_engine/fake_system_state.h"
28#include "update_engine/mock_http_fetcher.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080029#include "update_engine/mock_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070030#include "update_engine/omaha_hash_calculator.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000031#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070032#include "update_engine/update_manager/fake_update_manager.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000033#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000034
35namespace chromeos_update_engine {
36
Alex Deymof329b932014-10-30 01:37:48 -070037using base::FilePath;
38using base::ReadFileToString;
39using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000040using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070041using std::unique_ptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000042using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070043using testing::AtLeast;
44using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070045using testing::Return;
Alex Deymof329b932014-10-30 01:37:48 -070046using testing::_;
Alex Deymo10875d92014-11-10 21:52:57 -080047using test_utils::ScopedTempFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000048
49class DownloadActionTest : public ::testing::Test { };
50
51namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070052class DownloadActionDelegateMock : public DownloadActionDelegate {
53 public:
54 MOCK_METHOD1(SetDownloadStatus, void(bool active));
55 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
56};
57
rspangler@google.com49fdf182009-10-10 00:57:34 +000058class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
59 public:
David Zeuthena99981f2013-04-29 13:42:47 -070060 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Deymo60ca1a72015-06-18 18:19:15 -070061 : processing_done_called_(false),
Darin Petkovc97435c2010-07-20 12:37:43 -070062 expected_code_(expected_code) {}
Alex Deymo610277e2014-11-11 21:18:11 -080063 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000064 EXPECT_TRUE(processing_done_called_);
65 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080066 void ProcessingDone(const ActionProcessor* processor,
67 ErrorCode code) override {
Alex Deymo60ca1a72015-06-18 18:19:15 -070068 chromeos::MessageLoop::current()->BreakLoop();
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080069 chromeos::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000070 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070071 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080072 ASSERT_EQ(expected_data_.size(), found_data.size());
73 for (unsigned i = 0; i < expected_data_.size(); i++) {
74 EXPECT_EQ(expected_data_[i], found_data[i]);
75 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000076 }
77 processing_done_called_ = true;
78 }
79
Yunlian Jiang35866ed2015-01-29 13:09:20 -080080 void ActionCompleted(ActionProcessor* processor,
81 AbstractAction* action,
82 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070083 const string type = action->Type();
84 if (type == DownloadAction::StaticType()) {
85 EXPECT_EQ(expected_code_, code);
86 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070087 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070088 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000089 }
90
rspangler@google.com49fdf182009-10-10 00:57:34 +000091 string path_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080092 chromeos::Blob expected_data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000093 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070094 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000095};
96
Darin Petkov9ce452b2010-11-17 14:33:28 -080097class TestDirectFileWriter : public DirectFileWriter {
98 public:
99 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
100 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
101
Don Garrette410e0f2011-11-10 15:39:01 -0800102 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800103 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800104 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800105 }
106 return DirectFileWriter::Write(bytes, count);
107 }
108
109 private:
110 // If positive, fail on the |fail_write_| call to Write.
111 int fail_write_;
112 int current_write_;
113};
114
Alex Deymo60ca1a72015-06-18 18:19:15 -0700115void StartProcessorInRunLoop(ActionProcessor* processor,
116 MockHttpFetcher* http_fetcher) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700118 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119}
120
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800121void TestWithData(const chromeos::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800122 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700123 bool use_download_delegate) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700124 chromeos::FakeMessageLoop loop(nullptr);
125 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126
127 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700128 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800129 TestDirectFileWriter writer;
130 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700131
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700132 // We pull off the first byte from data and seek past it.
133
Darin Petkov7ed561b2011-10-04 02:59:03 -0700134 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700135 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700136 uint64_t size = data.size();
137 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700138 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700139 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700140 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700141 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700142 0,
143 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700144 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700145 "",
Allie Woodfdf00512015-03-02 13:34:55 -0800146 "",
147 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700148 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000149 ObjectFeederAction<InstallPlan> feeder_action;
150 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800151 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800152 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800153 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700154 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700155 // takes ownership of passed in HttpFetcher
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700156 DownloadAction download_action(&prefs, nullptr, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700157 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000158 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700159 DownloadActionDelegateMock download_delegate;
160 if (use_download_delegate) {
161 InSequence s;
162 download_action.set_delegate(&download_delegate);
163 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700164 if (data.size() > kMockHttpFetcherChunkSize)
165 EXPECT_CALL(download_delegate,
166 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800167 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700168 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
169 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700170 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700171 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700172 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700173 DownloadActionTestProcessorDelegate delegate(expected_code);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800174 delegate.expected_data_ = chromeos::Blob(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700175 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000176 ActionProcessor processor;
177 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000178 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000179 processor.EnqueueAction(&download_action);
180
Alex Deymo60ca1a72015-06-18 18:19:15 -0700181 loop.PostTask(FROM_HERE,
182 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
183 loop.Run();
184 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000185}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700186} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000187
188TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800189 chromeos::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000190 const char* foo = "foo";
191 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700192 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800193 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700194 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000195}
196
197TEST(DownloadActionTest, LargeTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800198 chromeos::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000199 char c = '0';
200 for (unsigned int i = 0; i < big.size(); i++) {
201 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800202 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000203 }
Darin Petkov50332f12010-09-24 11:44:47 -0700204 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800205 0, // fail_write
206 true); // use_download_delegate
207}
208
209TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800210 chromeos::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800211 char c = '0';
212 for (unsigned int i = 0; i < big.size(); i++) {
213 big[i] = c;
214 c = ('9' == c) ? '0' : c + 1;
215 }
216 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800217 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700218 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700219}
220
Darin Petkov9d911fa2010-08-19 09:36:08 -0700221TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800222 chromeos::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700223 const char* foo = "foofoo";
224 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700225 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800226 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700227 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000228}
229
230namespace {
231class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
232 public:
233 void ProcessingStopped(const ActionProcessor* processor) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700234 chromeos::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000235 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000236};
237
Alex Deymo60ca1a72015-06-18 18:19:15 -0700238void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000239 processor->StartProcessing();
240 CHECK(processor->IsRunning());
241 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000242}
243
Darin Petkov9d911fa2010-08-19 09:36:08 -0700244void TestTerminateEarly(bool use_download_delegate) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700245 chromeos::FakeMessageLoop loop(nullptr);
246 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000247
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800248 chromeos::Blob data(kMockHttpFetcherChunkSize +
249 kMockHttpFetcherChunkSize / 2);
250 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000251
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700252 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000253 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700254 DirectFileWriter writer;
255
rspangler@google.com49fdf182009-10-10 00:57:34 +0000256 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000257 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700258 InstallPlan install_plan(false, false, "", 0, "", 0, "",
Allie Woodfdf00512015-03-02 13:34:55 -0800259 temp_file.GetPath(), "", "", "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000260 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800261 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700262 DownloadAction download_action(&prefs, nullptr,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800263 new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800264 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700265 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700266 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700267 DownloadActionDelegateMock download_delegate;
268 if (use_download_delegate) {
269 InSequence s;
270 download_action.set_delegate(&download_delegate);
271 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
272 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
273 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000274 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000275 ActionProcessor processor;
276 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000277 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000278 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000279 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000280
Alex Deymo60ca1a72015-06-18 18:19:15 -0700281 loop.PostTask(FROM_HERE,
282 base::Bind(&TerminateEarlyTestStarter, &processor));
283 loop.Run();
284 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000285 }
286
287 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700288 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
289 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000290 if (resulting_file_size != 0)
291 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
292}
293
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700294} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700295
296TEST(DownloadActionTest, TerminateEarlyTest) {
297 TestTerminateEarly(true);
298}
299
300TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
301 TestTerminateEarly(false);
302}
303
rspangler@google.com49fdf182009-10-10 00:57:34 +0000304class DownloadActionTestAction;
305
306template<>
307class ActionTraits<DownloadActionTestAction> {
308 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000309 typedef InstallPlan OutputObjectType;
310 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000311};
312
313// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700314class DownloadActionTestAction : public Action<DownloadActionTestAction> {
315 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000316 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000317 typedef InstallPlan InputObjectType;
318 typedef InstallPlan OutputObjectType;
319 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
320 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000321 ActionProcessor* processor() { return processor_; }
322 void PerformAction() {
323 did_run_ = true;
324 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000325 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000326 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700327 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000328 }
329 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000330 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000331 bool did_run_;
332};
333
334namespace {
335// This class is an ActionProcessorDelegate that simply terminates the
336// run loop when the ActionProcessor has completed processing. It's used
337// only by the test PassObjectOutTest.
338class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
339 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700340 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700341 chromeos::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000342 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000343};
344
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700345} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000346
347TEST(DownloadActionTest, PassObjectOutTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700348 chromeos::FakeMessageLoop loop(nullptr);
349 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000350
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700351 DirectFileWriter writer;
352
rspangler@google.com49fdf182009-10-10 00:57:34 +0000353 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700354 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700355 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700356 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700357 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800358 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700359 0,
360 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700361 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700362 "/dev/null",
Allie Woodfdf00512015-03-02 13:34:55 -0800363 "/dev/null",
364 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700365 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000366 ObjectFeederAction<InstallPlan> feeder_action;
367 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800368 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700369 DownloadAction download_action(&prefs, nullptr,
370 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700371 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000372
373 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000374 test_action.expected_input_object_ = install_plan;
375 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000376 BondActions(&download_action, &test_action);
377
378 ActionProcessor processor;
379 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000380 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000381 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000382 processor.EnqueueAction(&download_action);
383 processor.EnqueueAction(&test_action);
384
Alex Deymo60ca1a72015-06-18 18:19:15 -0700385 loop.PostTask(FROM_HERE,
386 base::Bind([&processor] { processor.StartProcessing(); }));
387 loop.Run();
388 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000389
390 EXPECT_EQ(true, test_action.did_run_);
391}
392
393TEST(DownloadActionTest, BadOutFileTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700394 chromeos::FakeMessageLoop loop(nullptr);
395 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000396
397 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700398 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000399
400 // takes ownership of passed in HttpFetcher
Allie Woodfdf00512015-03-02 13:34:55 -0800401 InstallPlan install_plan(
402 false, false, "", 0, "", 0, "", path, "", "", "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000403 ObjectFeederAction<InstallPlan> feeder_action;
404 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800405 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700406 DownloadAction download_action(&prefs, nullptr,
407 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700408 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700409
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000410 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000411
412 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000413 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000414 processor.EnqueueAction(&download_action);
415 processor.StartProcessing();
416 ASSERT_FALSE(processor.IsRunning());
417
Alex Deymo60ca1a72015-06-18 18:19:15 -0700418 loop.Run();
419 EXPECT_FALSE(loop.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700420}
421
422// Test fixture for P2P tests.
423class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700424 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700425 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700426 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700427 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700428
Alex Deymo610277e2014-11-11 21:18:11 -0800429 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700430
431 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800432 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700433 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700434 }
435
436 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800437 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700438 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700439 }
440
441 // To be called by tests to setup the download. The
442 // |starting_offset| parameter is for where to resume.
443 void SetupDownload(off_t starting_offset) {
444 start_at_offset_ = starting_offset;
445 // Prepare data 10 kB of data.
446 data_.clear();
447 for (unsigned int i = 0; i < 10 * 1000; i++)
448 data_ += 'a' + (i % 25);
449
450 // Setup p2p.
451 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500452 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700453 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500454 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700455 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700456 }
457
458 // To be called by tests to perform the download. The
459 // |use_p2p_to_share| parameter is used to indicate whether the
460 // payload should be shared via p2p.
461 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700462 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
463 GetUsingP2PForSharing())
464 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700465
466 ScopedTempFile output_temp_file;
467 TestDirectFileWriter writer;
468 InstallPlan install_plan(false,
469 false,
470 "",
471 data_.length(),
472 "1234hash",
473 0,
474 "",
475 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700476 "",
Allie Woodfdf00512015-03-02 13:34:55 -0800477 "",
478 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700479 "");
480 ObjectFeederAction<InstallPlan> feeder_action;
481 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800482 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700483 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
484 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700485 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700486 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700487 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700488 http_fetcher_));
489 download_action_->SetTestFileWriter(&writer);
490 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700491 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800492 delegate.expected_data_ = chromeos::Blob(data_.begin() + start_at_offset_,
493 data_.end());
David Zeuthen8f191b22013-08-06 12:27:50 -0700494 delegate.path_ = output_temp_file.GetPath();
495 processor_.set_delegate(&delegate);
496 processor_.EnqueueAction(&feeder_action);
497 processor_.EnqueueAction(download_action_.get());
498
Alex Deymo60ca1a72015-06-18 18:19:15 -0700499 loop_.PostTask(FROM_HERE, base::Bind(
500 &P2PDownloadActionTest::StartProcessorInRunLoopForP2P,
501 base::Unretained(this)));
502 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700503 }
504
Alex Deymo60ca1a72015-06-18 18:19:15 -0700505 // Mainloop used to make StartDownload() synchronous.
506 chromeos::FakeMessageLoop loop_{nullptr};
507
David Zeuthen8f191b22013-08-06 12:27:50 -0700508 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700509 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700510
511 // The HttpFetcher used in the test.
512 MockHttpFetcher* http_fetcher_;
513
514 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700515 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700516
517 // The ActionProcessor used for running the actions.
518 ActionProcessor processor_;
519
520 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700521 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700522
523 // The data being downloaded.
524 string data_;
525
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700526 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700527 // Callback used in StartDownload() method.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700528 void StartProcessorInRunLoopForP2P() {
529 processor_.StartProcessing();
530 http_fetcher_->SetOffset(start_at_offset_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700531 }
532
David Zeuthen8f191b22013-08-06 12:27:50 -0700533 // The requested starting offset passed to SetupDownload().
534 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700535
536 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700537};
538
539TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800540 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700541 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
542 << "Please update your system to support this feature.";
543 return;
544 }
545
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700546 SetupDownload(0); // starting_offset
547 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700548
549 // Check the p2p file and its content matches what was sent.
550 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700551 EXPECT_NE("", file_id);
552 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
553 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700554 string p2p_file_contents;
555 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
556 &p2p_file_contents));
557 EXPECT_EQ(data_, p2p_file_contents);
558}
559
560TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800561 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700562 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
563 << "Please update your system to support this feature.";
564 return;
565 }
566
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700567 SetupDownload(1000); // starting_offset
568 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700569
570 // DownloadAction should convey that the file is not being shared.
571 // and that we don't have any p2p files.
572 EXPECT_EQ(download_action_->p2p_file_id(), "");
573 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
574}
575
576TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800577 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700578 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
579 << "Please update your system to support this feature.";
580 return;
581 }
582
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700583 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700584
585 // Prepare the file with existing data before starting to write to
586 // it via DownloadAction.
587 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
588 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
589 string existing_data;
590 for (unsigned int i = 0; i < 1000; i++)
591 existing_data += '0' + (i % 10);
592 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
593 1000), 1000);
594
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700595 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700596
597 // DownloadAction should convey the same file_id and the file should
598 // have the expected size.
599 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
600 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
601 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
602 string p2p_file_contents;
603 // Check that the first 1000 bytes wasn't touched and that we
604 // appended the remaining as appropriate.
605 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
606 &p2p_file_contents));
607 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
608 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
609}
610
David Zeuthen8f191b22013-08-06 12:27:50 -0700611TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800612 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700613 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
614 << "Please update your system to support this feature.";
615 return;
616 }
617
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700618 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700619
620 // Prepare the file with all existing data before starting to write
621 // to it via DownloadAction.
622 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
623 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
624 string existing_data;
625 for (unsigned int i = 0; i < 1000; i++)
626 existing_data += '0' + (i % 10);
627 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
628 1000), 1000);
629
630 // Check that the file is there.
631 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
632 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
633
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700634 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700635
636 // DownloadAction should have deleted the p2p file. Check that it's gone.
637 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
638 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
639}
640
rspangler@google.com49fdf182009-10-10 00:57:34 +0000641} // namespace chromeos_update_engine