blob: d27f96369f5f1ed70bce5911c660bc40e2a405a7 [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"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070027#include "update_engine/update_manager/fake_update_manager.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000028#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000029
30namespace chromeos_update_engine {
31
Alex Deymof329b932014-10-30 01:37:48 -070032using base::FilePath;
33using base::ReadFileToString;
34using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000035using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070036using std::unique_ptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000037using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070038using testing::AtLeast;
39using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070040using testing::Return;
Alex Deymof329b932014-10-30 01:37:48 -070041using testing::_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000042
43class DownloadActionTest : public ::testing::Test { };
44
45namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070046class DownloadActionDelegateMock : public DownloadActionDelegate {
47 public:
48 MOCK_METHOD1(SetDownloadStatus, void(bool active));
49 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
50};
51
rspangler@google.com49fdf182009-10-10 00:57:34 +000052class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
53 public:
David Zeuthena99981f2013-04-29 13:42:47 -070054 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Vakulenko88b591f2014-08-28 16:48:57 -070055 : loop_(nullptr),
Darin Petkovc97435c2010-07-20 12:37:43 -070056 processing_done_called_(false),
57 expected_code_(expected_code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000058 virtual ~DownloadActionTestProcessorDelegate() {
59 EXPECT_TRUE(processing_done_called_);
60 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070061 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070062 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000063 ASSERT_TRUE(loop_);
64 g_main_loop_quit(loop_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000065 vector<char> found_data;
66 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070067 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080068 ASSERT_EQ(expected_data_.size(), found_data.size());
69 for (unsigned i = 0; i < expected_data_.size(); i++) {
70 EXPECT_EQ(expected_data_[i], found_data[i]);
71 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000072 }
73 processing_done_called_ = true;
74 }
75
adlr@google.comc98a7ed2009-12-04 18:54:03 +000076 virtual void ActionCompleted(ActionProcessor* processor,
77 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070078 ErrorCode code) {
Darin Petkovc97435c2010-07-20 12:37:43 -070079 const string type = action->Type();
80 if (type == DownloadAction::StaticType()) {
81 EXPECT_EQ(expected_code_, code);
82 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070083 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070084 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000085 }
86
87 GMainLoop *loop_;
88 string path_;
89 vector<char> expected_data_;
90 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070091 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000092};
93
Darin Petkov9ce452b2010-11-17 14:33:28 -080094class TestDirectFileWriter : public DirectFileWriter {
95 public:
96 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
97 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
98
Don Garrette410e0f2011-11-10 15:39:01 -080099 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800100 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800101 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800102 }
103 return DirectFileWriter::Write(bytes, count);
104 }
105
106 private:
107 // If positive, fail on the |fail_write_| call to Write.
108 int fail_write_;
109 int current_write_;
110};
111
rspangler@google.com49fdf182009-10-10 00:57:34 +0000112struct EntryPointArgs {
113 const vector<char> *data;
114 GMainLoop *loop;
115 ActionProcessor *processor;
116};
117
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700118struct StartProcessorInRunLoopArgs {
119 ActionProcessor* processor;
120 MockHttpFetcher* http_fetcher;
121};
122
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700124 ActionProcessor* processor =
125 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700127 MockHttpFetcher* http_fetcher =
128 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher;
129 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130 return FALSE;
131}
132
Darin Petkov9d911fa2010-08-19 09:36:08 -0700133void TestWithData(const vector<char>& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800134 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700135 bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
137
138 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700139 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800140 TestDirectFileWriter writer;
141 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700142
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700143 // We pull off the first byte from data and seek past it.
144
Darin Petkov7ed561b2011-10-04 02:59:03 -0700145 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700146 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700147 uint64_t size = data.size();
148 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700149 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700150 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700151 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700152 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700153 0,
154 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700155 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700156 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700157 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000158 ObjectFeederAction<InstallPlan> feeder_action;
159 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800160 MockPrefs prefs;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800161 MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0],
162 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700163 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700164 // takes ownership of passed in HttpFetcher
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700165 DownloadAction download_action(&prefs, nullptr, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700166 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000167 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700168 DownloadActionDelegateMock download_delegate;
169 if (use_download_delegate) {
170 InSequence s;
171 download_action.set_delegate(&download_delegate);
172 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700173 if (data.size() > kMockHttpFetcherChunkSize)
174 EXPECT_CALL(download_delegate,
175 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800176 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700177 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
178 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700179 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700180 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700181 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700182 DownloadActionTestProcessorDelegate delegate(expected_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000183 delegate.loop_ = loop;
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700184 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700185 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000186 ActionProcessor processor;
187 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000188 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000189 processor.EnqueueAction(&download_action);
190
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700191 StartProcessorInRunLoopArgs args;
192 args.processor = &processor;
193 args.http_fetcher = http_fetcher;
194 g_timeout_add(0, &StartProcessorInRunLoop, &args);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000195 g_main_loop_run(loop);
196 g_main_loop_unref(loop);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000197}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700198} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000199
200TEST(DownloadActionTest, SimpleTest) {
201 vector<char> small;
202 const char* foo = "foo";
203 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700204 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800205 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700206 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000207}
208
209TEST(DownloadActionTest, LargeTest) {
210 vector<char> big(5 * kMockHttpFetcherChunkSize);
211 char c = '0';
212 for (unsigned int i = 0; i < big.size(); i++) {
213 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800214 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000215 }
Darin Petkov50332f12010-09-24 11:44:47 -0700216 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800217 0, // fail_write
218 true); // use_download_delegate
219}
220
221TEST(DownloadActionTest, FailWriteTest) {
222 vector<char> big(5 * kMockHttpFetcherChunkSize);
223 char c = '0';
224 for (unsigned int i = 0; i < big.size(); i++) {
225 big[i] = c;
226 c = ('9' == c) ? '0' : c + 1;
227 }
228 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800229 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700230 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700231}
232
Darin Petkov9d911fa2010-08-19 09:36:08 -0700233TEST(DownloadActionTest, NoDownloadDelegateTest) {
234 vector<char> small;
235 const char* foo = "foofoo";
236 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700237 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800238 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700239 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000240}
241
242namespace {
243class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
244 public:
245 void ProcessingStopped(const ActionProcessor* processor) {
246 ASSERT_TRUE(loop_);
247 g_main_loop_quit(loop_);
248 }
249 GMainLoop *loop_;
250};
251
252gboolean TerminateEarlyTestStarter(gpointer data) {
253 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
254 processor->StartProcessing();
255 CHECK(processor->IsRunning());
256 processor->StopProcessing();
257 return FALSE;
258}
259
Darin Petkov9d911fa2010-08-19 09:36:08 -0700260void TestTerminateEarly(bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000261 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
262
263 vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
264 memset(&data[0], 0, data.size());
265
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700266 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000267 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700268 DirectFileWriter writer;
269
rspangler@google.com49fdf182009-10-10 00:57:34 +0000270 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000271 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700272 InstallPlan install_plan(false, false, "", 0, "", 0, "",
David Zeuthene7f89172013-10-31 10:21:04 -0700273 temp_file.GetPath(), "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000274 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800275 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700276 DownloadAction download_action(&prefs, nullptr,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800277 new MockHttpFetcher(&data[0],
278 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700279 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700280 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700281 DownloadActionDelegateMock download_delegate;
282 if (use_download_delegate) {
283 InSequence s;
284 download_action.set_delegate(&download_delegate);
285 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
286 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
287 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000288 TerminateEarlyTestProcessorDelegate delegate;
289 delegate.loop_ = loop;
290 ActionProcessor processor;
291 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000292 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000293 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000294 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000295
296 g_timeout_add(0, &TerminateEarlyTestStarter, &processor);
297 g_main_loop_run(loop);
298 g_main_loop_unref(loop);
299 }
300
301 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700302 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
303 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000304 if (resulting_file_size != 0)
305 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
306}
307
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700308} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700309
310TEST(DownloadActionTest, TerminateEarlyTest) {
311 TestTerminateEarly(true);
312}
313
314TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
315 TestTerminateEarly(false);
316}
317
rspangler@google.com49fdf182009-10-10 00:57:34 +0000318class DownloadActionTestAction;
319
320template<>
321class ActionTraits<DownloadActionTestAction> {
322 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000323 typedef InstallPlan OutputObjectType;
324 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000325};
326
327// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700328class DownloadActionTestAction : public Action<DownloadActionTestAction> {
329 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000330 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000331 typedef InstallPlan InputObjectType;
332 typedef InstallPlan OutputObjectType;
333 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
334 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000335 ActionProcessor* processor() { return processor_; }
336 void PerformAction() {
337 did_run_ = true;
338 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000339 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000340 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700341 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000342 }
343 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000344 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000345 bool did_run_;
346};
347
348namespace {
349// This class is an ActionProcessorDelegate that simply terminates the
350// run loop when the ActionProcessor has completed processing. It's used
351// only by the test PassObjectOutTest.
352class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
353 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700354 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000355 ASSERT_TRUE(loop_);
356 g_main_loop_quit(loop_);
357 }
358 GMainLoop *loop_;
359};
360
361gboolean PassObjectOutTestStarter(gpointer data) {
362 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
363 processor->StartProcessing();
364 return FALSE;
365}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700366} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000367
368TEST(DownloadActionTest, PassObjectOutTest) {
369 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
370
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700371 DirectFileWriter writer;
372
rspangler@google.com49fdf182009-10-10 00:57:34 +0000373 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700374 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700375 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700376 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700377 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800378 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700379 0,
380 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700381 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700382 "/dev/null",
383 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000384 ObjectFeederAction<InstallPlan> feeder_action;
385 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800386 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700387 DownloadAction download_action(&prefs, nullptr,
388 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700389 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000390
391 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000392 test_action.expected_input_object_ = install_plan;
393 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000394 BondActions(&download_action, &test_action);
395
396 ActionProcessor processor;
397 PassObjectOutTestProcessorDelegate delegate;
398 delegate.loop_ = loop;
399 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000400 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000401 processor.EnqueueAction(&download_action);
402 processor.EnqueueAction(&test_action);
403
404 g_timeout_add(0, &PassObjectOutTestStarter, &processor);
405 g_main_loop_run(loop);
406 g_main_loop_unref(loop);
407
408 EXPECT_EQ(true, test_action.did_run_);
409}
410
411TEST(DownloadActionTest, BadOutFileTest) {
412 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
413
414 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700415 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000416
417 // takes ownership of passed in HttpFetcher
David Zeuthene7f89172013-10-31 10:21:04 -0700418 InstallPlan install_plan(false, false, "", 0, "", 0, "", path, "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000419 ObjectFeederAction<InstallPlan> feeder_action;
420 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800421 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700422 DownloadAction download_action(&prefs, nullptr,
423 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700424 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700425
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000426 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000427
428 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000429 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000430 processor.EnqueueAction(&download_action);
431 processor.StartProcessing();
432 ASSERT_FALSE(processor.IsRunning());
433
434 g_main_loop_unref(loop);
435}
436
David Zeuthen8f191b22013-08-06 12:27:50 -0700437gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
438 ActionProcessor* processor = reinterpret_cast<ActionProcessor*>(user_data);
439 processor->StartProcessing();
440 return FALSE;
441}
442
443// Test fixture for P2P tests.
444class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700445 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700446 P2PDownloadActionTest()
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700447 : loop_(nullptr),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700448 start_at_offset_(0),
449 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700450
451 virtual ~P2PDownloadActionTest() {}
452
453 // Derived from testing::Test.
454 virtual void SetUp() {
455 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
456 }
457
458 // Derived from testing::Test.
459 virtual void TearDown() {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700460 if (loop_ != nullptr)
David Zeuthen8f191b22013-08-06 12:27:50 -0700461 g_main_loop_unref(loop_);
462 }
463
464 // To be called by tests to setup the download. The
465 // |starting_offset| parameter is for where to resume.
466 void SetupDownload(off_t starting_offset) {
467 start_at_offset_ = starting_offset;
468 // Prepare data 10 kB of data.
469 data_.clear();
470 for (unsigned int i = 0; i < 10 * 1000; i++)
471 data_ += 'a' + (i % 25);
472
473 // Setup p2p.
474 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500475 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700476 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500477 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700478 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700479 }
480
481 // To be called by tests to perform the download. The
482 // |use_p2p_to_share| parameter is used to indicate whether the
483 // payload should be shared via p2p.
484 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700485 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
486 GetUsingP2PForSharing())
487 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700488
489 ScopedTempFile output_temp_file;
490 TestDirectFileWriter writer;
491 InstallPlan install_plan(false,
492 false,
493 "",
494 data_.length(),
495 "1234hash",
496 0,
497 "",
498 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700499 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700500 "");
501 ObjectFeederAction<InstallPlan> feeder_action;
502 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800503 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700504 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
505 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700506 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700507 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700508 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700509 http_fetcher_));
510 download_action_->SetTestFileWriter(&writer);
511 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700512 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
David Zeuthen8f191b22013-08-06 12:27:50 -0700513 delegate.loop_ = loop_;
514 delegate.expected_data_ = vector<char>(data_.begin() + start_at_offset_,
515 data_.end());
516 delegate.path_ = output_temp_file.GetPath();
517 processor_.set_delegate(&delegate);
518 processor_.EnqueueAction(&feeder_action);
519 processor_.EnqueueAction(download_action_.get());
520
521 g_timeout_add(0, &StartProcessorInRunLoopForP2P, this);
522 g_main_loop_run(loop_);
523 }
524
525 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700526 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700527
528 // The HttpFetcher used in the test.
529 MockHttpFetcher* http_fetcher_;
530
531 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700532 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700533
534 // The ActionProcessor used for running the actions.
535 ActionProcessor processor_;
536
537 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700538 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700539
540 // The data being downloaded.
541 string data_;
542
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700543 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700544 // Callback used in StartDownload() method.
545 static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
546 class P2PDownloadActionTest *test =
547 reinterpret_cast<P2PDownloadActionTest*>(user_data);
548 test->processor_.StartProcessing();
549 test->http_fetcher_->SetOffset(test->start_at_offset_);
550 return FALSE;
551 }
552
553 // Mainloop used to make StartDownload() synchronous.
554 GMainLoop *loop_;
555
556 // The requested starting offset passed to SetupDownload().
557 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700558
559 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700560};
561
562TEST_F(P2PDownloadActionTest, IsWrittenTo) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700563 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
564 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
565 << "Please update your system to support this feature.";
566 return;
567 }
568
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700569 SetupDownload(0); // starting_offset
570 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700571
572 // Check the p2p file and its content matches what was sent.
573 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700574 EXPECT_NE("", file_id);
575 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
576 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700577 string p2p_file_contents;
578 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
579 &p2p_file_contents));
580 EXPECT_EQ(data_, p2p_file_contents);
581}
582
583TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700584 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
585 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
586 << "Please update your system to support this feature.";
587 return;
588 }
589
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700590 SetupDownload(1000); // starting_offset
591 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700592
593 // DownloadAction should convey that the file is not being shared.
594 // and that we don't have any p2p files.
595 EXPECT_EQ(download_action_->p2p_file_id(), "");
596 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
597}
598
599TEST_F(P2PDownloadActionTest, CanAppend) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700600 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
601 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
602 << "Please update your system to support this feature.";
603 return;
604 }
605
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700606 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700607
608 // Prepare the file with existing data before starting to write to
609 // it via DownloadAction.
610 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
611 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
612 string existing_data;
613 for (unsigned int i = 0; i < 1000; i++)
614 existing_data += '0' + (i % 10);
615 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
616 1000), 1000);
617
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700618 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700619
620 // DownloadAction should convey the same file_id and the file should
621 // have the expected size.
622 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
623 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
624 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
625 string p2p_file_contents;
626 // Check that the first 1000 bytes wasn't touched and that we
627 // appended the remaining as appropriate.
628 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
629 &p2p_file_contents));
630 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
631 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
632}
633
David Zeuthen8f191b22013-08-06 12:27:50 -0700634TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700635 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
636 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
637 << "Please update your system to support this feature.";
638 return;
639 }
640
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700641 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700642
643 // Prepare the file with all existing data before starting to write
644 // to it via DownloadAction.
645 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
646 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
647 string existing_data;
648 for (unsigned int i = 0; i < 1000; i++)
649 existing_data += '0' + (i % 10);
650 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
651 1000), 1000);
652
653 // Check that the file is there.
654 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
655 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
656
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700657 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700658
659 // DownloadAction should have deleted the p2p file. Check that it's gone.
660 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
661 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
662}
663
rspangler@google.com49fdf182009-10-10 00:57:34 +0000664} // namespace chromeos_update_engine