blob: 0bdeacfea57db905033f9f3bbca986c51dc530df [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
rspangler@google.com49fdf182009-10-10 00:57:34 +00005#include <glib.h>
Darin Petkov9d911fa2010-08-19 09:36:08 -07006#include <gmock/gmock.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00007#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -07008
Ben Chan02f7c1d2014-10-18 15:18:02 -07009#include <memory>
David Zeuthen8f191b22013-08-06 12:27:50 -070010#include <string>
11#include <utility>
12#include <vector>
13
Alex Vakulenko75039d72014-03-25 12:36:28 -070014#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070015#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070016#include <base/strings/stringprintf.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070017
rspangler@google.com49fdf182009-10-10 00:57:34 +000018#include "update_engine/action_pipe.h"
19#include "update_engine/download_action.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070020#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070021#include "update_engine/fake_system_state.h"
22#include "update_engine/mock_http_fetcher.h"
23#include "update_engine/omaha_hash_calculator.h"
Darin Petkov73058b42010-10-06 16:32:19 -070024#include "update_engine/prefs_mock.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000025#include "update_engine/test_utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000026#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000027
28namespace chromeos_update_engine {
29
30using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070031using std::unique_ptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000032using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070033using testing::_;
34using testing::AtLeast;
35using testing::InSequence;
David Zeuthen8f191b22013-08-06 12:27:50 -070036using base::FilePath;
Alex Vakulenko75039d72014-03-25 12:36:28 -070037using base::ReadFileToString;
David Zeuthen8f191b22013-08-06 12:27:50 -070038using base::StringPrintf;
Ben Chan736fcb52014-05-21 18:28:22 -070039using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000040
41class DownloadActionTest : public ::testing::Test { };
42
43namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070044class DownloadActionDelegateMock : public DownloadActionDelegate {
45 public:
46 MOCK_METHOD1(SetDownloadStatus, void(bool active));
47 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
48};
49
rspangler@google.com49fdf182009-10-10 00:57:34 +000050class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
51 public:
David Zeuthena99981f2013-04-29 13:42:47 -070052 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Vakulenko88b591f2014-08-28 16:48:57 -070053 : loop_(nullptr),
Darin Petkovc97435c2010-07-20 12:37:43 -070054 processing_done_called_(false),
55 expected_code_(expected_code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000056 virtual ~DownloadActionTestProcessorDelegate() {
57 EXPECT_TRUE(processing_done_called_);
58 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070059 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070060 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 ASSERT_TRUE(loop_);
62 g_main_loop_quit(loop_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000063 vector<char> found_data;
64 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070065 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080066 ASSERT_EQ(expected_data_.size(), found_data.size());
67 for (unsigned i = 0; i < expected_data_.size(); i++) {
68 EXPECT_EQ(expected_data_[i], found_data[i]);
69 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 }
71 processing_done_called_ = true;
72 }
73
adlr@google.comc98a7ed2009-12-04 18:54:03 +000074 virtual void ActionCompleted(ActionProcessor* processor,
75 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070076 ErrorCode code) {
Darin Petkovc97435c2010-07-20 12:37:43 -070077 const string type = action->Type();
78 if (type == DownloadAction::StaticType()) {
79 EXPECT_EQ(expected_code_, code);
80 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070081 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070082 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 }
84
85 GMainLoop *loop_;
86 string path_;
87 vector<char> expected_data_;
88 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070089 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000090};
91
Darin Petkov9ce452b2010-11-17 14:33:28 -080092class TestDirectFileWriter : public DirectFileWriter {
93 public:
94 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
95 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
96
Don Garrette410e0f2011-11-10 15:39:01 -080097 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080098 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -080099 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800100 }
101 return DirectFileWriter::Write(bytes, count);
102 }
103
104 private:
105 // If positive, fail on the |fail_write_| call to Write.
106 int fail_write_;
107 int current_write_;
108};
109
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110struct EntryPointArgs {
111 const vector<char> *data;
112 GMainLoop *loop;
113 ActionProcessor *processor;
114};
115
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700116struct StartProcessorInRunLoopArgs {
117 ActionProcessor* processor;
118 MockHttpFetcher* http_fetcher;
119};
120
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700122 ActionProcessor* processor =
123 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000124 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700125 MockHttpFetcher* http_fetcher =
126 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher;
127 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128 return FALSE;
129}
130
Darin Petkov9d911fa2010-08-19 09:36:08 -0700131void TestWithData(const vector<char>& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800132 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700133 bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
135
136 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700137 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800138 TestDirectFileWriter writer;
139 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700140
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700141 // We pull off the first byte from data and seek past it.
142
Darin Petkov7ed561b2011-10-04 02:59:03 -0700143 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700144 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700145 uint64_t size = data.size();
146 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700147 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700148 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700149 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700150 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700151 0,
152 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700153 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700154 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700155 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000156 ObjectFeederAction<InstallPlan> feeder_action;
157 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700158 PrefsMock prefs;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800159 MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0],
160 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700161 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700162 // takes ownership of passed in HttpFetcher
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700163 DownloadAction download_action(&prefs, nullptr, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700164 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000165 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700166 DownloadActionDelegateMock download_delegate;
167 if (use_download_delegate) {
168 InSequence s;
169 download_action.set_delegate(&download_delegate);
170 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700171 if (data.size() > kMockHttpFetcherChunkSize)
172 EXPECT_CALL(download_delegate,
173 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800174 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700175 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
176 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700177 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700178 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700179 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700180 DownloadActionTestProcessorDelegate delegate(expected_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000181 delegate.loop_ = loop;
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700182 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700183 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000184 ActionProcessor processor;
185 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000186 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000187 processor.EnqueueAction(&download_action);
188
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700189 StartProcessorInRunLoopArgs args;
190 args.processor = &processor;
191 args.http_fetcher = http_fetcher;
192 g_timeout_add(0, &StartProcessorInRunLoop, &args);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193 g_main_loop_run(loop);
194 g_main_loop_unref(loop);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000195}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700196} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000197
198TEST(DownloadActionTest, SimpleTest) {
199 vector<char> small;
200 const char* foo = "foo";
201 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700202 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800203 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700204 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000205}
206
207TEST(DownloadActionTest, LargeTest) {
208 vector<char> big(5 * kMockHttpFetcherChunkSize);
209 char c = '0';
210 for (unsigned int i = 0; i < big.size(); i++) {
211 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800212 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000213 }
Darin Petkov50332f12010-09-24 11:44:47 -0700214 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800215 0, // fail_write
216 true); // use_download_delegate
217}
218
219TEST(DownloadActionTest, FailWriteTest) {
220 vector<char> big(5 * kMockHttpFetcherChunkSize);
221 char c = '0';
222 for (unsigned int i = 0; i < big.size(); i++) {
223 big[i] = c;
224 c = ('9' == c) ? '0' : c + 1;
225 }
226 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800227 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700228 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700229}
230
Darin Petkov9d911fa2010-08-19 09:36:08 -0700231TEST(DownloadActionTest, NoDownloadDelegateTest) {
232 vector<char> small;
233 const char* foo = "foofoo";
234 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700235 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800236 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700237 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000238}
239
240namespace {
241class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
242 public:
243 void ProcessingStopped(const ActionProcessor* processor) {
244 ASSERT_TRUE(loop_);
245 g_main_loop_quit(loop_);
246 }
247 GMainLoop *loop_;
248};
249
250gboolean TerminateEarlyTestStarter(gpointer data) {
251 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
252 processor->StartProcessing();
253 CHECK(processor->IsRunning());
254 processor->StopProcessing();
255 return FALSE;
256}
257
Darin Petkov9d911fa2010-08-19 09:36:08 -0700258void TestTerminateEarly(bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000259 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
260
261 vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
262 memset(&data[0], 0, data.size());
263
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700264 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000265 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700266 DirectFileWriter writer;
267
rspangler@google.com49fdf182009-10-10 00:57:34 +0000268 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000269 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700270 InstallPlan install_plan(false, false, "", 0, "", 0, "",
David Zeuthene7f89172013-10-31 10:21:04 -0700271 temp_file.GetPath(), "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000272 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700273 PrefsMock prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700274 DownloadAction download_action(&prefs, nullptr,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800275 new MockHttpFetcher(&data[0],
276 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700277 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700278 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700279 DownloadActionDelegateMock download_delegate;
280 if (use_download_delegate) {
281 InSequence s;
282 download_action.set_delegate(&download_delegate);
283 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
284 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
285 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000286 TerminateEarlyTestProcessorDelegate delegate;
287 delegate.loop_ = loop;
288 ActionProcessor processor;
289 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000290 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000291 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000292 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000293
294 g_timeout_add(0, &TerminateEarlyTestStarter, &processor);
295 g_main_loop_run(loop);
296 g_main_loop_unref(loop);
297 }
298
299 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700300 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
301 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000302 if (resulting_file_size != 0)
303 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
304}
305
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700306} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700307
308TEST(DownloadActionTest, TerminateEarlyTest) {
309 TestTerminateEarly(true);
310}
311
312TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
313 TestTerminateEarly(false);
314}
315
rspangler@google.com49fdf182009-10-10 00:57:34 +0000316class DownloadActionTestAction;
317
318template<>
319class ActionTraits<DownloadActionTestAction> {
320 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000321 typedef InstallPlan OutputObjectType;
322 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000323};
324
325// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700326class DownloadActionTestAction : public Action<DownloadActionTestAction> {
327 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000328 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000329 typedef InstallPlan InputObjectType;
330 typedef InstallPlan OutputObjectType;
331 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
332 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000333 ActionProcessor* processor() { return processor_; }
334 void PerformAction() {
335 did_run_ = true;
336 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000337 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000338 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700339 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000340 }
341 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000342 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000343 bool did_run_;
344};
345
346namespace {
347// This class is an ActionProcessorDelegate that simply terminates the
348// run loop when the ActionProcessor has completed processing. It's used
349// only by the test PassObjectOutTest.
350class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
351 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700352 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000353 ASSERT_TRUE(loop_);
354 g_main_loop_quit(loop_);
355 }
356 GMainLoop *loop_;
357};
358
359gboolean PassObjectOutTestStarter(gpointer data) {
360 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
361 processor->StartProcessing();
362 return FALSE;
363}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700364} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000365
366TEST(DownloadActionTest, PassObjectOutTest) {
367 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
368
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700369 DirectFileWriter writer;
370
rspangler@google.com49fdf182009-10-10 00:57:34 +0000371 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700372 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700373 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700374 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700375 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800376 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700377 0,
378 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700379 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700380 "/dev/null",
381 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000382 ObjectFeederAction<InstallPlan> feeder_action;
383 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700384 PrefsMock prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700385 DownloadAction download_action(&prefs, nullptr,
386 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700387 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000388
389 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000390 test_action.expected_input_object_ = install_plan;
391 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000392 BondActions(&download_action, &test_action);
393
394 ActionProcessor processor;
395 PassObjectOutTestProcessorDelegate delegate;
396 delegate.loop_ = loop;
397 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000398 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000399 processor.EnqueueAction(&download_action);
400 processor.EnqueueAction(&test_action);
401
402 g_timeout_add(0, &PassObjectOutTestStarter, &processor);
403 g_main_loop_run(loop);
404 g_main_loop_unref(loop);
405
406 EXPECT_EQ(true, test_action.did_run_);
407}
408
409TEST(DownloadActionTest, BadOutFileTest) {
410 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
411
412 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700413 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000414
415 // takes ownership of passed in HttpFetcher
David Zeuthene7f89172013-10-31 10:21:04 -0700416 InstallPlan install_plan(false, false, "", 0, "", 0, "", path, "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000417 ObjectFeederAction<InstallPlan> feeder_action;
418 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700419 PrefsMock prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700420 DownloadAction download_action(&prefs, nullptr,
421 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700422 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700423
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000424 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000425
426 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000427 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000428 processor.EnqueueAction(&download_action);
429 processor.StartProcessing();
430 ASSERT_FALSE(processor.IsRunning());
431
432 g_main_loop_unref(loop);
433}
434
David Zeuthen8f191b22013-08-06 12:27:50 -0700435gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
436 ActionProcessor* processor = reinterpret_cast<ActionProcessor*>(user_data);
437 processor->StartProcessing();
438 return FALSE;
439}
440
441// Test fixture for P2P tests.
442class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700443 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700444 P2PDownloadActionTest()
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700445 : loop_(nullptr),
David Zeuthen8f191b22013-08-06 12:27:50 -0700446 start_at_offset_(0) {}
447
448 virtual ~P2PDownloadActionTest() {}
449
450 // Derived from testing::Test.
451 virtual void SetUp() {
452 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
453 }
454
455 // Derived from testing::Test.
456 virtual void TearDown() {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700457 if (loop_ != nullptr)
David Zeuthen8f191b22013-08-06 12:27:50 -0700458 g_main_loop_unref(loop_);
459 }
460
461 // To be called by tests to setup the download. The
462 // |starting_offset| parameter is for where to resume.
463 void SetupDownload(off_t starting_offset) {
464 start_at_offset_ = starting_offset;
465 // Prepare data 10 kB of data.
466 data_.clear();
467 for (unsigned int i = 0; i < 10 * 1000; i++)
468 data_ += 'a' + (i % 25);
469
470 // Setup p2p.
471 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700472 p2p_manager_.reset(P2PManager::Construct(test_conf, nullptr, "cros_au", 3));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700473 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700474 }
475
476 // To be called by tests to perform the download. The
477 // |use_p2p_to_share| parameter is used to indicate whether the
478 // payload should be shared via p2p.
479 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700480 fake_system_state_.request_params()->set_use_p2p_for_sharing(
David Zeuthen8f191b22013-08-06 12:27:50 -0700481 use_p2p_to_share);
482
483 ScopedTempFile output_temp_file;
484 TestDirectFileWriter writer;
485 InstallPlan install_plan(false,
486 false,
487 "",
488 data_.length(),
489 "1234hash",
490 0,
491 "",
492 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700493 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700494 "");
495 ObjectFeederAction<InstallPlan> feeder_action;
496 feeder_action.set_obj(install_plan);
497 PrefsMock prefs;
498 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
499 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700500 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700501 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700502 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700503 http_fetcher_));
504 download_action_->SetTestFileWriter(&writer);
505 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700506 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
David Zeuthen8f191b22013-08-06 12:27:50 -0700507 delegate.loop_ = loop_;
508 delegate.expected_data_ = vector<char>(data_.begin() + start_at_offset_,
509 data_.end());
510 delegate.path_ = output_temp_file.GetPath();
511 processor_.set_delegate(&delegate);
512 processor_.EnqueueAction(&feeder_action);
513 processor_.EnqueueAction(download_action_.get());
514
515 g_timeout_add(0, &StartProcessorInRunLoopForP2P, this);
516 g_main_loop_run(loop_);
517 }
518
519 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700520 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700521
522 // The HttpFetcher used in the test.
523 MockHttpFetcher* http_fetcher_;
524
525 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700526 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700527
528 // The ActionProcessor used for running the actions.
529 ActionProcessor processor_;
530
531 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700532 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700533
534 // The data being downloaded.
535 string data_;
536
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700537 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700538 // Callback used in StartDownload() method.
539 static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
540 class P2PDownloadActionTest *test =
541 reinterpret_cast<P2PDownloadActionTest*>(user_data);
542 test->processor_.StartProcessing();
543 test->http_fetcher_->SetOffset(test->start_at_offset_);
544 return FALSE;
545 }
546
547 // Mainloop used to make StartDownload() synchronous.
548 GMainLoop *loop_;
549
550 // The requested starting offset passed to SetupDownload().
551 off_t start_at_offset_;
552};
553
554TEST_F(P2PDownloadActionTest, IsWrittenTo) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700555 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
556 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
557 << "Please update your system to support this feature.";
558 return;
559 }
560
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700561 SetupDownload(0); // starting_offset
562 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700563
564 // Check the p2p file and its content matches what was sent.
565 string file_id = download_action_->p2p_file_id();
566 EXPECT_NE(file_id, "");
567 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
568 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
569 string p2p_file_contents;
570 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
571 &p2p_file_contents));
572 EXPECT_EQ(data_, p2p_file_contents);
573}
574
575TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700576 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
577 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
583 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700584
585 // DownloadAction should convey that the file is not being shared.
586 // and that we don't have any p2p files.
587 EXPECT_EQ(download_action_->p2p_file_id(), "");
588 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
589}
590
591TEST_F(P2PDownloadActionTest, CanAppend) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700592 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
593 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
594 << "Please update your system to support this feature.";
595 return;
596 }
597
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700598 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700599
600 // Prepare the file with existing data before starting to write to
601 // it via DownloadAction.
602 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
603 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
604 string existing_data;
605 for (unsigned int i = 0; i < 1000; i++)
606 existing_data += '0' + (i % 10);
607 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
608 1000), 1000);
609
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700610 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700611
612 // DownloadAction should convey the same file_id and the file should
613 // have the expected size.
614 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
615 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
616 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
617 string p2p_file_contents;
618 // Check that the first 1000 bytes wasn't touched and that we
619 // appended the remaining as appropriate.
620 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
621 &p2p_file_contents));
622 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
623 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
624}
625
David Zeuthen8f191b22013-08-06 12:27:50 -0700626TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700627 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
628 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
629 << "Please update your system to support this feature.";
630 return;
631 }
632
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700633 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700634
635 // Prepare the file with all existing data before starting to write
636 // to it via DownloadAction.
637 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
638 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
639 string existing_data;
640 for (unsigned int i = 0; i < 1000; i++)
641 existing_data += '0' + (i % 10);
642 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
643 1000), 1000);
644
645 // Check that the file is there.
646 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
647 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
648
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700649 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700650
651 // DownloadAction should have deleted the p2p file. Check that it's gone.
652 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
653 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
654}
655
rspangler@google.com49fdf182009-10-10 00:57:34 +0000656} // namespace chromeos_update_engine