blob: 705d7590defcf8dff6fcec65261a7637cbf74458 [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;
Alex Deymof329b932014-10-30 01:37:48 -070038using testing::_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000039
40class DownloadActionTest : public ::testing::Test { };
41
42namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070043class DownloadActionDelegateMock : public DownloadActionDelegate {
44 public:
45 MOCK_METHOD1(SetDownloadStatus, void(bool active));
46 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
47};
48
rspangler@google.com49fdf182009-10-10 00:57:34 +000049class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
50 public:
David Zeuthena99981f2013-04-29 13:42:47 -070051 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Vakulenko88b591f2014-08-28 16:48:57 -070052 : loop_(nullptr),
Darin Petkovc97435c2010-07-20 12:37:43 -070053 processing_done_called_(false),
54 expected_code_(expected_code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000055 virtual ~DownloadActionTestProcessorDelegate() {
56 EXPECT_TRUE(processing_done_called_);
57 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070058 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070059 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000060 ASSERT_TRUE(loop_);
61 g_main_loop_quit(loop_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000062 vector<char> found_data;
63 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070064 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080065 ASSERT_EQ(expected_data_.size(), found_data.size());
66 for (unsigned i = 0; i < expected_data_.size(); i++) {
67 EXPECT_EQ(expected_data_[i], found_data[i]);
68 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 }
70 processing_done_called_ = true;
71 }
72
adlr@google.comc98a7ed2009-12-04 18:54:03 +000073 virtual void ActionCompleted(ActionProcessor* processor,
74 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070075 ErrorCode code) {
Darin Petkovc97435c2010-07-20 12:37:43 -070076 const string type = action->Type();
77 if (type == DownloadAction::StaticType()) {
78 EXPECT_EQ(expected_code_, code);
79 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070080 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070081 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 }
83
84 GMainLoop *loop_;
85 string path_;
86 vector<char> expected_data_;
87 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070088 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000089};
90
Darin Petkov9ce452b2010-11-17 14:33:28 -080091class TestDirectFileWriter : public DirectFileWriter {
92 public:
93 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
94 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
95
Don Garrette410e0f2011-11-10 15:39:01 -080096 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080097 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -080098 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -080099 }
100 return DirectFileWriter::Write(bytes, count);
101 }
102
103 private:
104 // If positive, fail on the |fail_write_| call to Write.
105 int fail_write_;
106 int current_write_;
107};
108
rspangler@google.com49fdf182009-10-10 00:57:34 +0000109struct EntryPointArgs {
110 const vector<char> *data;
111 GMainLoop *loop;
112 ActionProcessor *processor;
113};
114
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700115struct StartProcessorInRunLoopArgs {
116 ActionProcessor* processor;
117 MockHttpFetcher* http_fetcher;
118};
119
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700121 ActionProcessor* processor =
122 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700124 MockHttpFetcher* http_fetcher =
125 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher;
126 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127 return FALSE;
128}
129
Darin Petkov9d911fa2010-08-19 09:36:08 -0700130void TestWithData(const vector<char>& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800131 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700132 bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
134
135 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700136 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800137 TestDirectFileWriter writer;
138 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700139
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700140 // We pull off the first byte from data and seek past it.
141
Darin Petkov7ed561b2011-10-04 02:59:03 -0700142 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700143 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700144 uint64_t size = data.size();
145 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700146 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700147 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700148 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700149 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700150 0,
151 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700152 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700153 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700154 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000155 ObjectFeederAction<InstallPlan> feeder_action;
156 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700157 PrefsMock prefs;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800158 MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0],
159 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700160 nullptr);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700161 // takes ownership of passed in HttpFetcher
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700162 DownloadAction download_action(&prefs, nullptr, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700163 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000164 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700165 DownloadActionDelegateMock download_delegate;
166 if (use_download_delegate) {
167 InSequence s;
168 download_action.set_delegate(&download_delegate);
169 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700170 if (data.size() > kMockHttpFetcherChunkSize)
171 EXPECT_CALL(download_delegate,
172 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800173 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700174 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
175 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700176 ErrorCode expected_code = ErrorCode::kSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700177 if (fail_write > 0)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700178 expected_code = ErrorCode::kDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700179 DownloadActionTestProcessorDelegate delegate(expected_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000180 delegate.loop_ = loop;
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700181 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700182 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000183 ActionProcessor processor;
184 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000185 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000186 processor.EnqueueAction(&download_action);
187
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700188 StartProcessorInRunLoopArgs args;
189 args.processor = &processor;
190 args.http_fetcher = http_fetcher;
191 g_timeout_add(0, &StartProcessorInRunLoop, &args);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000192 g_main_loop_run(loop);
193 g_main_loop_unref(loop);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000194}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700195} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000196
197TEST(DownloadActionTest, SimpleTest) {
198 vector<char> small;
199 const char* foo = "foo";
200 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700201 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800202 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700203 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000204}
205
206TEST(DownloadActionTest, LargeTest) {
207 vector<char> big(5 * kMockHttpFetcherChunkSize);
208 char c = '0';
209 for (unsigned int i = 0; i < big.size(); i++) {
210 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800211 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000212 }
Darin Petkov50332f12010-09-24 11:44:47 -0700213 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800214 0, // fail_write
215 true); // use_download_delegate
216}
217
218TEST(DownloadActionTest, FailWriteTest) {
219 vector<char> big(5 * kMockHttpFetcherChunkSize);
220 char c = '0';
221 for (unsigned int i = 0; i < big.size(); i++) {
222 big[i] = c;
223 c = ('9' == c) ? '0' : c + 1;
224 }
225 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800226 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700227 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700228}
229
Darin Petkov9d911fa2010-08-19 09:36:08 -0700230TEST(DownloadActionTest, NoDownloadDelegateTest) {
231 vector<char> small;
232 const char* foo = "foofoo";
233 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700234 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800235 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700236 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000237}
238
239namespace {
240class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
241 public:
242 void ProcessingStopped(const ActionProcessor* processor) {
243 ASSERT_TRUE(loop_);
244 g_main_loop_quit(loop_);
245 }
246 GMainLoop *loop_;
247};
248
249gboolean TerminateEarlyTestStarter(gpointer data) {
250 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
251 processor->StartProcessing();
252 CHECK(processor->IsRunning());
253 processor->StopProcessing();
254 return FALSE;
255}
256
Darin Petkov9d911fa2010-08-19 09:36:08 -0700257void TestTerminateEarly(bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000258 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
259
260 vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
261 memset(&data[0], 0, data.size());
262
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700263 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000264 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700265 DirectFileWriter writer;
266
rspangler@google.com49fdf182009-10-10 00:57:34 +0000267 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000268 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700269 InstallPlan install_plan(false, false, "", 0, "", 0, "",
David Zeuthene7f89172013-10-31 10:21:04 -0700270 temp_file.GetPath(), "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000271 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700272 PrefsMock prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700273 DownloadAction download_action(&prefs, nullptr,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800274 new MockHttpFetcher(&data[0],
275 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700276 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700277 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700278 DownloadActionDelegateMock download_delegate;
279 if (use_download_delegate) {
280 InSequence s;
281 download_action.set_delegate(&download_delegate);
282 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
283 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
284 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000285 TerminateEarlyTestProcessorDelegate delegate;
286 delegate.loop_ = loop;
287 ActionProcessor processor;
288 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000289 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000290 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000291 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000292
293 g_timeout_add(0, &TerminateEarlyTestStarter, &processor);
294 g_main_loop_run(loop);
295 g_main_loop_unref(loop);
296 }
297
298 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700299 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
300 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000301 if (resulting_file_size != 0)
302 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
303}
304
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700305} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700306
307TEST(DownloadActionTest, TerminateEarlyTest) {
308 TestTerminateEarly(true);
309}
310
311TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
312 TestTerminateEarly(false);
313}
314
rspangler@google.com49fdf182009-10-10 00:57:34 +0000315class DownloadActionTestAction;
316
317template<>
318class ActionTraits<DownloadActionTestAction> {
319 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000320 typedef InstallPlan OutputObjectType;
321 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000322};
323
324// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700325class DownloadActionTestAction : public Action<DownloadActionTestAction> {
326 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000327 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000328 typedef InstallPlan InputObjectType;
329 typedef InstallPlan OutputObjectType;
330 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
331 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000332 ActionProcessor* processor() { return processor_; }
333 void PerformAction() {
334 did_run_ = true;
335 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000336 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000337 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700338 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000339 }
340 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000341 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000342 bool did_run_;
343};
344
345namespace {
346// This class is an ActionProcessorDelegate that simply terminates the
347// run loop when the ActionProcessor has completed processing. It's used
348// only by the test PassObjectOutTest.
349class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
350 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700351 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000352 ASSERT_TRUE(loop_);
353 g_main_loop_quit(loop_);
354 }
355 GMainLoop *loop_;
356};
357
358gboolean PassObjectOutTestStarter(gpointer data) {
359 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
360 processor->StartProcessing();
361 return FALSE;
362}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700363} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000364
365TEST(DownloadActionTest, PassObjectOutTest) {
366 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
367
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700368 DirectFileWriter writer;
369
rspangler@google.com49fdf182009-10-10 00:57:34 +0000370 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700371 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700372 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700373 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700374 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800375 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700376 0,
377 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700378 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700379 "/dev/null",
380 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000381 ObjectFeederAction<InstallPlan> feeder_action;
382 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700383 PrefsMock prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700384 DownloadAction download_action(&prefs, nullptr,
385 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700386 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000387
388 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000389 test_action.expected_input_object_ = install_plan;
390 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000391 BondActions(&download_action, &test_action);
392
393 ActionProcessor processor;
394 PassObjectOutTestProcessorDelegate delegate;
395 delegate.loop_ = loop;
396 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000397 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000398 processor.EnqueueAction(&download_action);
399 processor.EnqueueAction(&test_action);
400
401 g_timeout_add(0, &PassObjectOutTestStarter, &processor);
402 g_main_loop_run(loop);
403 g_main_loop_unref(loop);
404
405 EXPECT_EQ(true, test_action.did_run_);
406}
407
408TEST(DownloadActionTest, BadOutFileTest) {
409 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
410
411 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700412 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000413
414 // takes ownership of passed in HttpFetcher
David Zeuthene7f89172013-10-31 10:21:04 -0700415 InstallPlan install_plan(false, false, "", 0, "", 0, "", path, "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000416 ObjectFeederAction<InstallPlan> feeder_action;
417 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700418 PrefsMock prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700419 DownloadAction download_action(&prefs, nullptr,
420 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700421 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700422
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000423 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000424
425 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000426 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000427 processor.EnqueueAction(&download_action);
428 processor.StartProcessing();
429 ASSERT_FALSE(processor.IsRunning());
430
431 g_main_loop_unref(loop);
432}
433
David Zeuthen8f191b22013-08-06 12:27:50 -0700434gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
435 ActionProcessor* processor = reinterpret_cast<ActionProcessor*>(user_data);
436 processor->StartProcessing();
437 return FALSE;
438}
439
440// Test fixture for P2P tests.
441class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700442 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700443 P2PDownloadActionTest()
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700444 : loop_(nullptr),
David Zeuthen8f191b22013-08-06 12:27:50 -0700445 start_at_offset_(0) {}
446
447 virtual ~P2PDownloadActionTest() {}
448
449 // Derived from testing::Test.
450 virtual void SetUp() {
451 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
452 }
453
454 // Derived from testing::Test.
455 virtual void TearDown() {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700456 if (loop_ != nullptr)
David Zeuthen8f191b22013-08-06 12:27:50 -0700457 g_main_loop_unref(loop_);
458 }
459
460 // To be called by tests to setup the download. The
461 // |starting_offset| parameter is for where to resume.
462 void SetupDownload(off_t starting_offset) {
463 start_at_offset_ = starting_offset;
464 // Prepare data 10 kB of data.
465 data_.clear();
466 for (unsigned int i = 0; i < 10 * 1000; i++)
467 data_ += 'a' + (i % 25);
468
469 // Setup p2p.
470 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700471 p2p_manager_.reset(P2PManager::Construct(test_conf, nullptr, "cros_au", 3));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700472 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700473 }
474
475 // To be called by tests to perform the download. The
476 // |use_p2p_to_share| parameter is used to indicate whether the
477 // payload should be shared via p2p.
478 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700479 fake_system_state_.request_params()->set_use_p2p_for_sharing(
David Zeuthen8f191b22013-08-06 12:27:50 -0700480 use_p2p_to_share);
481
482 ScopedTempFile output_temp_file;
483 TestDirectFileWriter writer;
484 InstallPlan install_plan(false,
485 false,
486 "",
487 data_.length(),
488 "1234hash",
489 0,
490 "",
491 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700492 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700493 "");
494 ObjectFeederAction<InstallPlan> feeder_action;
495 feeder_action.set_obj(install_plan);
496 PrefsMock prefs;
497 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
498 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700499 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700500 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700501 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700502 http_fetcher_));
503 download_action_->SetTestFileWriter(&writer);
504 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700505 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
David Zeuthen8f191b22013-08-06 12:27:50 -0700506 delegate.loop_ = loop_;
507 delegate.expected_data_ = vector<char>(data_.begin() + start_at_offset_,
508 data_.end());
509 delegate.path_ = output_temp_file.GetPath();
510 processor_.set_delegate(&delegate);
511 processor_.EnqueueAction(&feeder_action);
512 processor_.EnqueueAction(download_action_.get());
513
514 g_timeout_add(0, &StartProcessorInRunLoopForP2P, this);
515 g_main_loop_run(loop_);
516 }
517
518 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700519 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700520
521 // The HttpFetcher used in the test.
522 MockHttpFetcher* http_fetcher_;
523
524 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700525 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700526
527 // The ActionProcessor used for running the actions.
528 ActionProcessor processor_;
529
530 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700531 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700532
533 // The data being downloaded.
534 string data_;
535
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700536 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700537 // Callback used in StartDownload() method.
538 static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
539 class P2PDownloadActionTest *test =
540 reinterpret_cast<P2PDownloadActionTest*>(user_data);
541 test->processor_.StartProcessing();
542 test->http_fetcher_->SetOffset(test->start_at_offset_);
543 return FALSE;
544 }
545
546 // Mainloop used to make StartDownload() synchronous.
547 GMainLoop *loop_;
548
549 // The requested starting offset passed to SetupDownload().
550 off_t start_at_offset_;
551};
552
553TEST_F(P2PDownloadActionTest, IsWrittenTo) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700554 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
555 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
556 << "Please update your system to support this feature.";
557 return;
558 }
559
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700560 SetupDownload(0); // starting_offset
561 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700562
563 // Check the p2p file and its content matches what was sent.
564 string file_id = download_action_->p2p_file_id();
565 EXPECT_NE(file_id, "");
566 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
567 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
568 string p2p_file_contents;
569 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
570 &p2p_file_contents));
571 EXPECT_EQ(data_, p2p_file_contents);
572}
573
574TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700575 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
576 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
577 << "Please update your system to support this feature.";
578 return;
579 }
580
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700581 SetupDownload(1000); // starting_offset
582 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700583
584 // DownloadAction should convey that the file is not being shared.
585 // and that we don't have any p2p files.
586 EXPECT_EQ(download_action_->p2p_file_id(), "");
587 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
588}
589
590TEST_F(P2PDownloadActionTest, CanAppend) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700591 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
592 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
593 << "Please update your system to support this feature.";
594 return;
595 }
596
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700597 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700598
599 // Prepare the file with existing data before starting to write to
600 // it via DownloadAction.
601 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
602 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
603 string existing_data;
604 for (unsigned int i = 0; i < 1000; i++)
605 existing_data += '0' + (i % 10);
606 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
607 1000), 1000);
608
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700609 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700610
611 // DownloadAction should convey the same file_id and the file should
612 // have the expected size.
613 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
614 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
615 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
616 string p2p_file_contents;
617 // Check that the first 1000 bytes wasn't touched and that we
618 // appended the remaining as appropriate.
619 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
620 &p2p_file_contents));
621 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
622 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
623}
624
David Zeuthen8f191b22013-08-06 12:27:50 -0700625TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700626 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
627 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
628 << "Please update your system to support this feature.";
629 return;
630 }
631
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700632 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700633
634 // Prepare the file with all existing data before starting to write
635 // to it via DownloadAction.
636 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
637 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
638 string existing_data;
639 for (unsigned int i = 0; i < 1000; i++)
640 existing_data += '0' + (i % 10);
641 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
642 1000), 1000);
643
644 // Check that the file is there.
645 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
646 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
647
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700648 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700649
650 // DownloadAction should have deleted the p2p file. Check that it's gone.
651 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
652 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
653}
654
rspangler@google.com49fdf182009-10-10 00:57:34 +0000655} // namespace chromeos_update_engine