blob: 93a72aed1b9479b61140a919947cec55600126ed [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
Alex Deymof329b932014-10-30 01:37:48 -070030using base::FilePath;
31using base::ReadFileToString;
32using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000033using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070034using std::unique_ptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000035using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070036using testing::AtLeast;
37using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070038using testing::Return;
Alex Deymof329b932014-10-30 01:37:48 -070039using testing::_;
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 Arnold74b5f552014-10-07 08:17:16 -0700480 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
481 GetUsingP2PForSharing())
482 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700483
484 ScopedTempFile output_temp_file;
485 TestDirectFileWriter writer;
486 InstallPlan install_plan(false,
487 false,
488 "",
489 data_.length(),
490 "1234hash",
491 0,
492 "",
493 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700494 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700495 "");
496 ObjectFeederAction<InstallPlan> feeder_action;
497 feeder_action.set_obj(install_plan);
498 PrefsMock prefs;
499 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
500 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700501 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700502 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700503 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700504 http_fetcher_));
505 download_action_->SetTestFileWriter(&writer);
506 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700507 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
David Zeuthen8f191b22013-08-06 12:27:50 -0700508 delegate.loop_ = loop_;
509 delegate.expected_data_ = vector<char>(data_.begin() + start_at_offset_,
510 data_.end());
511 delegate.path_ = output_temp_file.GetPath();
512 processor_.set_delegate(&delegate);
513 processor_.EnqueueAction(&feeder_action);
514 processor_.EnqueueAction(download_action_.get());
515
516 g_timeout_add(0, &StartProcessorInRunLoopForP2P, this);
517 g_main_loop_run(loop_);
518 }
519
520 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700521 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700522
523 // The HttpFetcher used in the test.
524 MockHttpFetcher* http_fetcher_;
525
526 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700527 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700528
529 // The ActionProcessor used for running the actions.
530 ActionProcessor processor_;
531
532 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700533 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700534
535 // The data being downloaded.
536 string data_;
537
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700538 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700539 // Callback used in StartDownload() method.
540 static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
541 class P2PDownloadActionTest *test =
542 reinterpret_cast<P2PDownloadActionTest*>(user_data);
543 test->processor_.StartProcessing();
544 test->http_fetcher_->SetOffset(test->start_at_offset_);
545 return FALSE;
546 }
547
548 // Mainloop used to make StartDownload() synchronous.
549 GMainLoop *loop_;
550
551 // The requested starting offset passed to SetupDownload().
552 off_t start_at_offset_;
553};
554
555TEST_F(P2PDownloadActionTest, IsWrittenTo) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700556 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
557 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
558 << "Please update your system to support this feature.";
559 return;
560 }
561
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700562 SetupDownload(0); // starting_offset
563 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700564
565 // Check the p2p file and its content matches what was sent.
566 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700567 EXPECT_NE("", file_id);
568 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
569 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700570 string p2p_file_contents;
571 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
572 &p2p_file_contents));
573 EXPECT_EQ(data_, p2p_file_contents);
574}
575
576TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700577 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
578 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
579 << "Please update your system to support this feature.";
580 return;
581 }
582
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700583 SetupDownload(1000); // starting_offset
584 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700585
586 // DownloadAction should convey that the file is not being shared.
587 // and that we don't have any p2p files.
588 EXPECT_EQ(download_action_->p2p_file_id(), "");
589 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
590}
591
592TEST_F(P2PDownloadActionTest, CanAppend) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700593 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
594 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
595 << "Please update your system to support this feature.";
596 return;
597 }
598
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700599 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700600
601 // Prepare the file with existing data before starting to write to
602 // it via DownloadAction.
603 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
604 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
605 string existing_data;
606 for (unsigned int i = 0; i < 1000; i++)
607 existing_data += '0' + (i % 10);
608 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
609 1000), 1000);
610
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700611 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700612
613 // DownloadAction should convey the same file_id and the file should
614 // have the expected size.
615 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
616 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
617 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
618 string p2p_file_contents;
619 // Check that the first 1000 bytes wasn't touched and that we
620 // appended the remaining as appropriate.
621 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
622 &p2p_file_contents));
623 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
624 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
625}
626
David Zeuthen8f191b22013-08-06 12:27:50 -0700627TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700628 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
629 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
630 << "Please update your system to support this feature.";
631 return;
632 }
633
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700634 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700635
636 // Prepare the file with all existing data before starting to write
637 // to it via DownloadAction.
638 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
639 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
640 string existing_data;
641 for (unsigned int i = 0; i < 1000; i++)
642 existing_data += '0' + (i % 10);
643 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
644 1000), 1000);
645
646 // Check that the file is there.
647 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
648 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
649
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700650 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700651
652 // DownloadAction should have deleted the p2p file. Check that it's gone.
653 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
654 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
655}
656
rspangler@google.com49fdf182009-10-10 00:57:34 +0000657} // namespace chromeos_update_engine