blob: f241a32e536a28ce300e3a07b634cf845dd5dc71 [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 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 Vakulenko75039d72014-03-25 12:36:28 -070018#include <base/strings/stringprintf.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070019
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include "update_engine/action_pipe.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070021#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070022#include "update_engine/fake_system_state.h"
23#include "update_engine/mock_http_fetcher.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080024#include "update_engine/mock_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070025#include "update_engine/omaha_hash_calculator.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000026#include "update_engine/test_utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000027#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000028
29namespace chromeos_update_engine {
30
Alex Deymof329b932014-10-30 01:37:48 -070031using base::FilePath;
32using base::ReadFileToString;
33using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000034using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070035using std::unique_ptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000036using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070037using testing::AtLeast;
38using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070039using testing::Return;
Alex Deymof329b932014-10-30 01:37:48 -070040using testing::_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000041
42class DownloadActionTest : public ::testing::Test { };
43
44namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070045class DownloadActionDelegateMock : public DownloadActionDelegate {
46 public:
47 MOCK_METHOD1(SetDownloadStatus, void(bool active));
48 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
49};
50
rspangler@google.com49fdf182009-10-10 00:57:34 +000051class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
52 public:
David Zeuthena99981f2013-04-29 13:42:47 -070053 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Vakulenko88b591f2014-08-28 16:48:57 -070054 : loop_(nullptr),
Darin Petkovc97435c2010-07-20 12:37:43 -070055 processing_done_called_(false),
56 expected_code_(expected_code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000057 virtual ~DownloadActionTestProcessorDelegate() {
58 EXPECT_TRUE(processing_done_called_);
59 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070060 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070061 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000062 ASSERT_TRUE(loop_);
63 g_main_loop_quit(loop_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000064 vector<char> found_data;
65 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070066 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080067 ASSERT_EQ(expected_data_.size(), found_data.size());
68 for (unsigned i = 0; i < expected_data_.size(); i++) {
69 EXPECT_EQ(expected_data_[i], found_data[i]);
70 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 }
72 processing_done_called_ = true;
73 }
74
adlr@google.comc98a7ed2009-12-04 18:54:03 +000075 virtual void ActionCompleted(ActionProcessor* processor,
76 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070077 ErrorCode code) {
Darin Petkovc97435c2010-07-20 12:37:43 -070078 const string type = action->Type();
79 if (type == DownloadAction::StaticType()) {
80 EXPECT_EQ(expected_code_, code);
81 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070082 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070083 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 }
85
86 GMainLoop *loop_;
87 string path_;
88 vector<char> expected_data_;
89 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070090 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000091};
92
Darin Petkov9ce452b2010-11-17 14:33:28 -080093class TestDirectFileWriter : public DirectFileWriter {
94 public:
95 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
96 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
97
Don Garrette410e0f2011-11-10 15:39:01 -080098 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080099 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800100 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800101 }
102 return DirectFileWriter::Write(bytes, count);
103 }
104
105 private:
106 // If positive, fail on the |fail_write_| call to Write.
107 int fail_write_;
108 int current_write_;
109};
110
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111struct EntryPointArgs {
112 const vector<char> *data;
113 GMainLoop *loop;
114 ActionProcessor *processor;
115};
116
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700117struct StartProcessorInRunLoopArgs {
118 ActionProcessor* processor;
119 MockHttpFetcher* http_fetcher;
120};
121
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700123 ActionProcessor* processor =
124 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700126 MockHttpFetcher* http_fetcher =
127 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher;
128 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000129 return FALSE;
130}
131
Darin Petkov9d911fa2010-08-19 09:36:08 -0700132void TestWithData(const vector<char>& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800133 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700134 bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
136
137 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700138 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800139 TestDirectFileWriter writer;
140 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700141
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700142 // We pull off the first byte from data and seek past it.
143
Darin Petkov7ed561b2011-10-04 02:59:03 -0700144 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700145 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700146 uint64_t size = data.size();
147 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700148 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700149 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700150 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700151 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700152 0,
153 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700154 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700155 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700156 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000157 ObjectFeederAction<InstallPlan> feeder_action;
158 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800159 MockPrefs prefs;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800160 MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0],
161 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700162 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700163 // takes ownership of passed in HttpFetcher
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700164 DownloadAction download_action(&prefs, nullptr, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700165 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000166 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700167 DownloadActionDelegateMock download_delegate;
168 if (use_download_delegate) {
169 InSequence s;
170 download_action.set_delegate(&download_delegate);
171 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700172 if (data.size() > kMockHttpFetcherChunkSize)
173 EXPECT_CALL(download_delegate,
174 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800175 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700176 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
177 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700178 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700179 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700180 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700181 DownloadActionTestProcessorDelegate delegate(expected_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000182 delegate.loop_ = loop;
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700183 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700184 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000185 ActionProcessor processor;
186 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000187 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000188 processor.EnqueueAction(&download_action);
189
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700190 StartProcessorInRunLoopArgs args;
191 args.processor = &processor;
192 args.http_fetcher = http_fetcher;
193 g_timeout_add(0, &StartProcessorInRunLoop, &args);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000194 g_main_loop_run(loop);
195 g_main_loop_unref(loop);
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) {
200 vector<char> small;
201 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) {
209 vector<char> big(5 * kMockHttpFetcherChunkSize);
210 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) {
221 vector<char> big(5 * kMockHttpFetcherChunkSize);
222 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) {
233 vector<char> small;
234 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) {
245 ASSERT_TRUE(loop_);
246 g_main_loop_quit(loop_);
247 }
248 GMainLoop *loop_;
249};
250
251gboolean TerminateEarlyTestStarter(gpointer data) {
252 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
253 processor->StartProcessing();
254 CHECK(processor->IsRunning());
255 processor->StopProcessing();
256 return FALSE;
257}
258
Darin Petkov9d911fa2010-08-19 09:36:08 -0700259void TestTerminateEarly(bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000260 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
261
262 vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
263 memset(&data[0], 0, data.size());
264
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700265 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000266 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700267 DirectFileWriter writer;
268
rspangler@google.com49fdf182009-10-10 00:57:34 +0000269 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000270 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700271 InstallPlan install_plan(false, false, "", 0, "", 0, "",
David Zeuthene7f89172013-10-31 10:21:04 -0700272 temp_file.GetPath(), "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000273 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800274 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700275 DownloadAction download_action(&prefs, nullptr,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800276 new MockHttpFetcher(&data[0],
277 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700278 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700279 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700280 DownloadActionDelegateMock download_delegate;
281 if (use_download_delegate) {
282 InSequence s;
283 download_action.set_delegate(&download_delegate);
284 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
285 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
286 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000287 TerminateEarlyTestProcessorDelegate delegate;
288 delegate.loop_ = loop;
289 ActionProcessor processor;
290 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000291 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000292 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000293 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000294
295 g_timeout_add(0, &TerminateEarlyTestStarter, &processor);
296 g_main_loop_run(loop);
297 g_main_loop_unref(loop);
298 }
299
300 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700301 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
302 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000303 if (resulting_file_size != 0)
304 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
305}
306
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700307} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700308
309TEST(DownloadActionTest, TerminateEarlyTest) {
310 TestTerminateEarly(true);
311}
312
313TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
314 TestTerminateEarly(false);
315}
316
rspangler@google.com49fdf182009-10-10 00:57:34 +0000317class DownloadActionTestAction;
318
319template<>
320class ActionTraits<DownloadActionTestAction> {
321 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000322 typedef InstallPlan OutputObjectType;
323 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000324};
325
326// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700327class DownloadActionTestAction : public Action<DownloadActionTestAction> {
328 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000329 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000330 typedef InstallPlan InputObjectType;
331 typedef InstallPlan OutputObjectType;
332 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
333 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000334 ActionProcessor* processor() { return processor_; }
335 void PerformAction() {
336 did_run_ = true;
337 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000338 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000339 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700340 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341 }
342 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000343 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000344 bool did_run_;
345};
346
347namespace {
348// This class is an ActionProcessorDelegate that simply terminates the
349// run loop when the ActionProcessor has completed processing. It's used
350// only by the test PassObjectOutTest.
351class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
352 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700353 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000354 ASSERT_TRUE(loop_);
355 g_main_loop_quit(loop_);
356 }
357 GMainLoop *loop_;
358};
359
360gboolean PassObjectOutTestStarter(gpointer data) {
361 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
362 processor->StartProcessing();
363 return FALSE;
364}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700365} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000366
367TEST(DownloadActionTest, PassObjectOutTest) {
368 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
369
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700370 DirectFileWriter writer;
371
rspangler@google.com49fdf182009-10-10 00:57:34 +0000372 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700373 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700374 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700375 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700376 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800377 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700378 0,
379 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700380 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700381 "/dev/null",
382 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000383 ObjectFeederAction<InstallPlan> feeder_action;
384 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800385 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700386 DownloadAction download_action(&prefs, nullptr,
387 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700388 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000389
390 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000391 test_action.expected_input_object_ = install_plan;
392 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000393 BondActions(&download_action, &test_action);
394
395 ActionProcessor processor;
396 PassObjectOutTestProcessorDelegate delegate;
397 delegate.loop_ = loop;
398 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000399 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000400 processor.EnqueueAction(&download_action);
401 processor.EnqueueAction(&test_action);
402
403 g_timeout_add(0, &PassObjectOutTestStarter, &processor);
404 g_main_loop_run(loop);
405 g_main_loop_unref(loop);
406
407 EXPECT_EQ(true, test_action.did_run_);
408}
409
410TEST(DownloadActionTest, BadOutFileTest) {
411 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
412
413 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700414 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000415
416 // takes ownership of passed in HttpFetcher
David Zeuthene7f89172013-10-31 10:21:04 -0700417 InstallPlan install_plan(false, false, "", 0, "", 0, "", path, "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000418 ObjectFeederAction<InstallPlan> feeder_action;
419 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800420 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700421 DownloadAction download_action(&prefs, nullptr,
422 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700423 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700424
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000425 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000426
427 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000428 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000429 processor.EnqueueAction(&download_action);
430 processor.StartProcessing();
431 ASSERT_FALSE(processor.IsRunning());
432
433 g_main_loop_unref(loop);
434}
435
David Zeuthen8f191b22013-08-06 12:27:50 -0700436gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
437 ActionProcessor* processor = reinterpret_cast<ActionProcessor*>(user_data);
438 processor->StartProcessing();
439 return FALSE;
440}
441
442// Test fixture for P2P tests.
443class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700444 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700445 P2PDownloadActionTest()
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700446 : loop_(nullptr),
David Zeuthen8f191b22013-08-06 12:27:50 -0700447 start_at_offset_(0) {}
448
449 virtual ~P2PDownloadActionTest() {}
450
451 // Derived from testing::Test.
452 virtual void SetUp() {
453 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
454 }
455
456 // Derived from testing::Test.
457 virtual void TearDown() {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700458 if (loop_ != nullptr)
David Zeuthen8f191b22013-08-06 12:27:50 -0700459 g_main_loop_unref(loop_);
460 }
461
462 // To be called by tests to setup the download. The
463 // |starting_offset| parameter is for where to resume.
464 void SetupDownload(off_t starting_offset) {
465 start_at_offset_ = starting_offset;
466 // Prepare data 10 kB of data.
467 data_.clear();
468 for (unsigned int i = 0; i < 10 * 1000; i++)
469 data_ += 'a' + (i % 25);
470
471 // Setup p2p.
472 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700473 p2p_manager_.reset(P2PManager::Construct(test_conf, nullptr, "cros_au", 3));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700474 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700475 }
476
477 // To be called by tests to perform the download. The
478 // |use_p2p_to_share| parameter is used to indicate whether the
479 // payload should be shared via p2p.
480 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700481 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
482 GetUsingP2PForSharing())
483 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700484
485 ScopedTempFile output_temp_file;
486 TestDirectFileWriter writer;
487 InstallPlan install_plan(false,
488 false,
489 "",
490 data_.length(),
491 "1234hash",
492 0,
493 "",
494 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700495 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700496 "");
497 ObjectFeederAction<InstallPlan> feeder_action;
498 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800499 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700500 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
501 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700502 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700503 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700504 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700505 http_fetcher_));
506 download_action_->SetTestFileWriter(&writer);
507 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700508 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
David Zeuthen8f191b22013-08-06 12:27:50 -0700509 delegate.loop_ = loop_;
510 delegate.expected_data_ = vector<char>(data_.begin() + start_at_offset_,
511 data_.end());
512 delegate.path_ = output_temp_file.GetPath();
513 processor_.set_delegate(&delegate);
514 processor_.EnqueueAction(&feeder_action);
515 processor_.EnqueueAction(download_action_.get());
516
517 g_timeout_add(0, &StartProcessorInRunLoopForP2P, this);
518 g_main_loop_run(loop_);
519 }
520
521 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700522 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700523
524 // The HttpFetcher used in the test.
525 MockHttpFetcher* http_fetcher_;
526
527 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700528 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700529
530 // The ActionProcessor used for running the actions.
531 ActionProcessor processor_;
532
533 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700534 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700535
536 // The data being downloaded.
537 string data_;
538
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700539 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700540 // Callback used in StartDownload() method.
541 static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
542 class P2PDownloadActionTest *test =
543 reinterpret_cast<P2PDownloadActionTest*>(user_data);
544 test->processor_.StartProcessing();
545 test->http_fetcher_->SetOffset(test->start_at_offset_);
546 return FALSE;
547 }
548
549 // Mainloop used to make StartDownload() synchronous.
550 GMainLoop *loop_;
551
552 // The requested starting offset passed to SetupDownload().
553 off_t start_at_offset_;
554};
555
556TEST_F(P2PDownloadActionTest, IsWrittenTo) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700557 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
558 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
559 << "Please update your system to support this feature.";
560 return;
561 }
562
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700563 SetupDownload(0); // starting_offset
564 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700565
566 // Check the p2p file and its content matches what was sent.
567 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700568 EXPECT_NE("", file_id);
569 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
570 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700571 string p2p_file_contents;
572 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
573 &p2p_file_contents));
574 EXPECT_EQ(data_, p2p_file_contents);
575}
576
577TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700578 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
579 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
580 << "Please update your system to support this feature.";
581 return;
582 }
583
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700584 SetupDownload(1000); // starting_offset
585 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700586
587 // DownloadAction should convey that the file is not being shared.
588 // and that we don't have any p2p files.
589 EXPECT_EQ(download_action_->p2p_file_id(), "");
590 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
591}
592
593TEST_F(P2PDownloadActionTest, CanAppend) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700594 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
595 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
596 << "Please update your system to support this feature.";
597 return;
598 }
599
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700600 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700601
602 // Prepare the file with existing data before starting to write to
603 // it via DownloadAction.
604 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
605 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
606 string existing_data;
607 for (unsigned int i = 0; i < 1000; i++)
608 existing_data += '0' + (i % 10);
609 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
610 1000), 1000);
611
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700612 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700613
614 // DownloadAction should convey the same file_id and the file should
615 // have the expected size.
616 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
617 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
618 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
619 string p2p_file_contents;
620 // Check that the first 1000 bytes wasn't touched and that we
621 // appended the remaining as appropriate.
622 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
623 &p2p_file_contents));
624 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
625 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
626}
627
David Zeuthen8f191b22013-08-06 12:27:50 -0700628TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700629 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
630 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
631 << "Please update your system to support this feature.";
632 return;
633 }
634
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700635 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700636
637 // Prepare the file with all existing data before starting to write
638 // to it via DownloadAction.
639 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
640 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
641 string existing_data;
642 for (unsigned int i = 0; i < 1000; i++)
643 existing_data += '0' + (i % 10);
644 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
645 1000), 1000);
646
647 // Check that the file is there.
648 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
649 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
650
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700651 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700652
653 // DownloadAction should have deleted the p2p file. Check that it's gone.
654 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
655 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
656}
657
rspangler@google.com49fdf182009-10-10 00:57:34 +0000658} // namespace chromeos_update_engine