blob: a8306f377986d94bab152a7fe2b7429bef67e659 [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
Darin Petkov9d911fa2010-08-19 09:36:08 -07007#include <gmock/gmock.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -07009
Ben Chan02f7c1d2014-10-18 15:18:02 -070010#include <memory>
David Zeuthen8f191b22013-08-06 12:27:50 -070011#include <string>
12#include <utility>
13#include <vector>
14
Alex Deymo60ca1a72015-06-18 18:19:15 -070015#include <base/bind.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070016#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070017#include <base/files/file_util.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070018#include <base/location.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/strings/stringprintf.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070020#include <chromeos/bind_lambda.h>
21#include <chromeos/message_loops/fake_message_loop.h>
22#include <chromeos/message_loops/message_loop.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070023
rspangler@google.com49fdf182009-10-10 00:57:34 +000024#include "update_engine/action_pipe.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070025#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070026#include "update_engine/fake_system_state.h"
27#include "update_engine/mock_http_fetcher.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080028#include "update_engine/mock_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070029#include "update_engine/omaha_hash_calculator.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000030#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070031#include "update_engine/update_manager/fake_update_manager.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000032#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000033
34namespace chromeos_update_engine {
35
Alex Deymof329b932014-10-30 01:37:48 -070036using base::FilePath;
37using base::ReadFileToString;
38using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000039using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070040using std::unique_ptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000041using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070042using testing::AtLeast;
43using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070044using testing::Return;
Alex Deymof329b932014-10-30 01:37:48 -070045using testing::_;
Alex Deymo10875d92014-11-10 21:52:57 -080046using test_utils::ScopedTempFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000047
48class DownloadActionTest : public ::testing::Test { };
49
50namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070051class DownloadActionDelegateMock : public DownloadActionDelegate {
52 public:
53 MOCK_METHOD1(SetDownloadStatus, void(bool active));
54 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
55};
56
rspangler@google.com49fdf182009-10-10 00:57:34 +000057class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
58 public:
David Zeuthena99981f2013-04-29 13:42:47 -070059 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Deymo60ca1a72015-06-18 18:19:15 -070060 : processing_done_called_(false),
Darin Petkovc97435c2010-07-20 12:37:43 -070061 expected_code_(expected_code) {}
Alex Deymo610277e2014-11-11 21:18:11 -080062 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000063 EXPECT_TRUE(processing_done_called_);
64 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080065 void ProcessingDone(const ActionProcessor* processor,
66 ErrorCode code) override {
Alex Deymo60ca1a72015-06-18 18:19:15 -070067 chromeos::MessageLoop::current()->BreakLoop();
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080068 chromeos::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000069 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070070 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080071 ASSERT_EQ(expected_data_.size(), found_data.size());
72 for (unsigned i = 0; i < expected_data_.size(); i++) {
73 EXPECT_EQ(expected_data_[i], found_data[i]);
74 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 }
76 processing_done_called_ = true;
77 }
78
Yunlian Jiang35866ed2015-01-29 13:09:20 -080079 void ActionCompleted(ActionProcessor* processor,
80 AbstractAction* action,
81 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070082 const string type = action->Type();
83 if (type == DownloadAction::StaticType()) {
84 EXPECT_EQ(expected_code_, code);
85 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070086 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070087 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000088 }
89
rspangler@google.com49fdf182009-10-10 00:57:34 +000090 string path_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080091 chromeos::Blob expected_data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000092 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070093 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000094};
95
Darin Petkov9ce452b2010-11-17 14:33:28 -080096class TestDirectFileWriter : public DirectFileWriter {
97 public:
98 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
99 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
100
Don Garrette410e0f2011-11-10 15:39:01 -0800101 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800102 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800103 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800104 }
105 return DirectFileWriter::Write(bytes, count);
106 }
107
108 private:
109 // If positive, fail on the |fail_write_| call to Write.
110 int fail_write_;
111 int current_write_;
112};
113
Alex Deymo60ca1a72015-06-18 18:19:15 -0700114void StartProcessorInRunLoop(ActionProcessor* processor,
115 MockHttpFetcher* http_fetcher) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700117 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000118}
119
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800120void TestWithData(const chromeos::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800121 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700122 bool use_download_delegate) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700123 chromeos::FakeMessageLoop loop(nullptr);
124 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125
126 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700127 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800128 TestDirectFileWriter writer;
129 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700130
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700131 // We pull off the first byte from data and seek past it.
132
Darin Petkov7ed561b2011-10-04 02:59:03 -0700133 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700134 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700135 uint64_t size = data.size();
136 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700137 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700138 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700139 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700140 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700141 0,
142 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700143 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700144 "",
Allie Woodfdf00512015-03-02 13:34:55 -0800145 "",
146 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700147 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000148 ObjectFeederAction<InstallPlan> feeder_action;
149 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800150 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800151 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800152 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700153 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700154 // takes ownership of passed in HttpFetcher
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700155 DownloadAction download_action(&prefs, nullptr, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700156 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000157 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700158 DownloadActionDelegateMock download_delegate;
159 if (use_download_delegate) {
160 InSequence s;
161 download_action.set_delegate(&download_delegate);
162 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700163 if (data.size() > kMockHttpFetcherChunkSize)
164 EXPECT_CALL(download_delegate,
165 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800166 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700167 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
168 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700169 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700170 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700171 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700172 DownloadActionTestProcessorDelegate delegate(expected_code);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800173 delegate.expected_data_ = chromeos::Blob(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700174 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000175 ActionProcessor processor;
176 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000177 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000178 processor.EnqueueAction(&download_action);
179
Alex Deymo60ca1a72015-06-18 18:19:15 -0700180 loop.PostTask(FROM_HERE,
181 base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
182 loop.Run();
183 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000184}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700185} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000186
187TEST(DownloadActionTest, SimpleTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800188 chromeos::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000189 const char* foo = "foo";
190 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700191 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800192 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700193 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000194}
195
196TEST(DownloadActionTest, LargeTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800197 chromeos::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000198 char c = '0';
199 for (unsigned int i = 0; i < big.size(); i++) {
200 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800201 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000202 }
Darin Petkov50332f12010-09-24 11:44:47 -0700203 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800204 0, // fail_write
205 true); // use_download_delegate
206}
207
208TEST(DownloadActionTest, FailWriteTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800209 chromeos::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800210 char c = '0';
211 for (unsigned int i = 0; i < big.size(); i++) {
212 big[i] = c;
213 c = ('9' == c) ? '0' : c + 1;
214 }
215 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800216 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700217 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700218}
219
Darin Petkov9d911fa2010-08-19 09:36:08 -0700220TEST(DownloadActionTest, NoDownloadDelegateTest) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800221 chromeos::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700222 const char* foo = "foofoo";
223 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700224 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800225 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700226 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000227}
228
229namespace {
230class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
231 public:
232 void ProcessingStopped(const ActionProcessor* processor) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700233 chromeos::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000234 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000235};
236
Alex Deymo60ca1a72015-06-18 18:19:15 -0700237void TerminateEarlyTestStarter(ActionProcessor* processor) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000238 processor->StartProcessing();
239 CHECK(processor->IsRunning());
240 processor->StopProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000241}
242
Darin Petkov9d911fa2010-08-19 09:36:08 -0700243void TestTerminateEarly(bool use_download_delegate) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700244 chromeos::FakeMessageLoop loop(nullptr);
245 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000246
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800247 chromeos::Blob data(kMockHttpFetcherChunkSize +
248 kMockHttpFetcherChunkSize / 2);
249 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000250
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700251 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000252 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700253 DirectFileWriter writer;
254
rspangler@google.com49fdf182009-10-10 00:57:34 +0000255 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000256 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700257 InstallPlan install_plan(false, false, "", 0, "", 0, "",
Allie Woodfdf00512015-03-02 13:34:55 -0800258 temp_file.GetPath(), "", "", "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000259 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800260 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700261 DownloadAction download_action(&prefs, nullptr,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800262 new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800263 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700264 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700265 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700266 DownloadActionDelegateMock download_delegate;
267 if (use_download_delegate) {
268 InSequence s;
269 download_action.set_delegate(&download_delegate);
270 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
271 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
272 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000273 TerminateEarlyTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000274 ActionProcessor processor;
275 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000276 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000277 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000278 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000279
Alex Deymo60ca1a72015-06-18 18:19:15 -0700280 loop.PostTask(FROM_HERE,
281 base::Bind(&TerminateEarlyTestStarter, &processor));
282 loop.Run();
283 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000284 }
285
286 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700287 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
288 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000289 if (resulting_file_size != 0)
290 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
291}
292
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700293} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700294
295TEST(DownloadActionTest, TerminateEarlyTest) {
296 TestTerminateEarly(true);
297}
298
299TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
300 TestTerminateEarly(false);
301}
302
rspangler@google.com49fdf182009-10-10 00:57:34 +0000303class DownloadActionTestAction;
304
305template<>
306class ActionTraits<DownloadActionTestAction> {
307 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000308 typedef InstallPlan OutputObjectType;
309 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000310};
311
312// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700313class DownloadActionTestAction : public Action<DownloadActionTestAction> {
314 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000315 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000316 typedef InstallPlan InputObjectType;
317 typedef InstallPlan OutputObjectType;
318 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
319 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000320 ActionProcessor* processor() { return processor_; }
321 void PerformAction() {
322 did_run_ = true;
323 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000324 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000325 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700326 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000327 }
328 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000329 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000330 bool did_run_;
331};
332
333namespace {
334// This class is an ActionProcessorDelegate that simply terminates the
335// run loop when the ActionProcessor has completed processing. It's used
336// only by the test PassObjectOutTest.
337class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
338 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700339 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700340 chromeos::MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000342};
343
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700344} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000345
346TEST(DownloadActionTest, PassObjectOutTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700347 chromeos::FakeMessageLoop loop(nullptr);
348 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000349
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700350 DirectFileWriter writer;
351
rspangler@google.com49fdf182009-10-10 00:57:34 +0000352 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700353 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700354 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700355 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700356 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800357 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700358 0,
359 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700360 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700361 "/dev/null",
Allie Woodfdf00512015-03-02 13:34:55 -0800362 "/dev/null",
363 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700364 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000365 ObjectFeederAction<InstallPlan> feeder_action;
366 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800367 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700368 DownloadAction download_action(&prefs, nullptr,
369 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700370 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000371
372 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000373 test_action.expected_input_object_ = install_plan;
374 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000375 BondActions(&download_action, &test_action);
376
377 ActionProcessor processor;
378 PassObjectOutTestProcessorDelegate delegate;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000379 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000380 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000381 processor.EnqueueAction(&download_action);
382 processor.EnqueueAction(&test_action);
383
Alex Deymo60ca1a72015-06-18 18:19:15 -0700384 loop.PostTask(FROM_HERE,
385 base::Bind([&processor] { processor.StartProcessing(); }));
386 loop.Run();
387 EXPECT_FALSE(loop.PendingTasks());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000388
389 EXPECT_EQ(true, test_action.did_run_);
390}
391
392TEST(DownloadActionTest, BadOutFileTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700393 chromeos::FakeMessageLoop loop(nullptr);
394 loop.SetAsCurrent();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000395
396 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700397 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000398
399 // takes ownership of passed in HttpFetcher
Allie Woodfdf00512015-03-02 13:34:55 -0800400 InstallPlan install_plan(
401 false, false, "", 0, "", 0, "", path, "", "", "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000402 ObjectFeederAction<InstallPlan> feeder_action;
403 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800404 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700405 DownloadAction download_action(&prefs, nullptr,
406 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700407 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700408
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000409 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000410
411 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000412 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000413 processor.EnqueueAction(&download_action);
414 processor.StartProcessing();
415 ASSERT_FALSE(processor.IsRunning());
416
Alex Deymo60ca1a72015-06-18 18:19:15 -0700417 loop.Run();
418 EXPECT_FALSE(loop.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700419}
420
421// Test fixture for P2P tests.
422class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700423 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700424 P2PDownloadActionTest()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700425 : start_at_offset_(0),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700426 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700427
Alex Deymo610277e2014-11-11 21:18:11 -0800428 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700429
430 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800431 void SetUp() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700432 loop_.SetAsCurrent();
David Zeuthen8f191b22013-08-06 12:27:50 -0700433 }
434
435 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800436 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700437 EXPECT_FALSE(loop_.PendingTasks());
David Zeuthen8f191b22013-08-06 12:27:50 -0700438 }
439
440 // To be called by tests to setup the download. The
441 // |starting_offset| parameter is for where to resume.
442 void SetupDownload(off_t starting_offset) {
443 start_at_offset_ = starting_offset;
444 // Prepare data 10 kB of data.
445 data_.clear();
446 for (unsigned int i = 0; i < 10 * 1000; i++)
447 data_ += 'a' + (i % 25);
448
449 // Setup p2p.
450 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500451 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700452 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500453 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700454 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700455 }
456
457 // To be called by tests to perform the download. The
458 // |use_p2p_to_share| parameter is used to indicate whether the
459 // payload should be shared via p2p.
460 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700461 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
462 GetUsingP2PForSharing())
463 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700464
465 ScopedTempFile output_temp_file;
466 TestDirectFileWriter writer;
467 InstallPlan install_plan(false,
468 false,
469 "",
470 data_.length(),
471 "1234hash",
472 0,
473 "",
474 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700475 "",
Allie Woodfdf00512015-03-02 13:34:55 -0800476 "",
477 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700478 "");
479 ObjectFeederAction<InstallPlan> feeder_action;
480 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800481 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700482 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
483 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700484 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700485 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700486 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700487 http_fetcher_));
488 download_action_->SetTestFileWriter(&writer);
489 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700490 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800491 delegate.expected_data_ = chromeos::Blob(data_.begin() + start_at_offset_,
492 data_.end());
David Zeuthen8f191b22013-08-06 12:27:50 -0700493 delegate.path_ = output_temp_file.GetPath();
494 processor_.set_delegate(&delegate);
495 processor_.EnqueueAction(&feeder_action);
496 processor_.EnqueueAction(download_action_.get());
497
Alex Deymo60ca1a72015-06-18 18:19:15 -0700498 loop_.PostTask(FROM_HERE, base::Bind(
499 &P2PDownloadActionTest::StartProcessorInRunLoopForP2P,
500 base::Unretained(this)));
501 loop_.Run();
David Zeuthen8f191b22013-08-06 12:27:50 -0700502 }
503
Alex Deymo60ca1a72015-06-18 18:19:15 -0700504 // Mainloop used to make StartDownload() synchronous.
505 chromeos::FakeMessageLoop loop_{nullptr};
506
David Zeuthen8f191b22013-08-06 12:27:50 -0700507 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700508 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700509
510 // The HttpFetcher used in the test.
511 MockHttpFetcher* http_fetcher_;
512
513 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700514 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700515
516 // The ActionProcessor used for running the actions.
517 ActionProcessor processor_;
518
519 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700520 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700521
522 // The data being downloaded.
523 string data_;
524
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700525 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700526 // Callback used in StartDownload() method.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700527 void StartProcessorInRunLoopForP2P() {
528 processor_.StartProcessing();
529 http_fetcher_->SetOffset(start_at_offset_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700530 }
531
David Zeuthen8f191b22013-08-06 12:27:50 -0700532 // The requested starting offset passed to SetupDownload().
533 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700534
535 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700536};
537
538TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800539 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700540 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
541 << "Please update your system to support this feature.";
542 return;
543 }
544
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700545 SetupDownload(0); // starting_offset
546 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700547
548 // Check the p2p file and its content matches what was sent.
549 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700550 EXPECT_NE("", file_id);
551 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
552 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700553 string p2p_file_contents;
554 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
555 &p2p_file_contents));
556 EXPECT_EQ(data_, p2p_file_contents);
557}
558
559TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800560 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700561 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
562 << "Please update your system to support this feature.";
563 return;
564 }
565
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700566 SetupDownload(1000); // starting_offset
567 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700568
569 // DownloadAction should convey that the file is not being shared.
570 // and that we don't have any p2p files.
571 EXPECT_EQ(download_action_->p2p_file_id(), "");
572 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
573}
574
575TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800576 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700577 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
578 << "Please update your system to support this feature.";
579 return;
580 }
581
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700582 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700583
584 // Prepare the file with existing data before starting to write to
585 // it via DownloadAction.
586 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
587 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
588 string existing_data;
589 for (unsigned int i = 0; i < 1000; i++)
590 existing_data += '0' + (i % 10);
591 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
592 1000), 1000);
593
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700594 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700595
596 // DownloadAction should convey the same file_id and the file should
597 // have the expected size.
598 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
599 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
600 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
601 string p2p_file_contents;
602 // Check that the first 1000 bytes wasn't touched and that we
603 // appended the remaining as appropriate.
604 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
605 &p2p_file_contents));
606 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
607 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
608}
609
David Zeuthen8f191b22013-08-06 12:27:50 -0700610TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800611 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700612 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
613 << "Please update your system to support this feature.";
614 return;
615 }
616
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700617 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700618
619 // Prepare the file with all existing data before starting to write
620 // to it via DownloadAction.
621 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
622 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
623 string existing_data;
624 for (unsigned int i = 0; i < 1000; i++)
625 existing_data += '0' + (i % 10);
626 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
627 1000), 1000);
628
629 // Check that the file is there.
630 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
631 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
632
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700633 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700634
635 // DownloadAction should have deleted the p2p file. Check that it's gone.
636 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
637 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
638}
639
rspangler@google.com49fdf182009-10-10 00:57:34 +0000640} // namespace chromeos_update_engine