blob: 7bb0fc59f82e4b16c27c8614ff59f6fe5d056d0e [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
David Zeuthen8f191b22013-08-06 12:27:50 -07009#include <string>
10#include <utility>
11#include <vector>
12
13#include <base/file_path.h>
14#include <base/file_util.h>
15#include <base/stringprintf.h>
16
rspangler@google.com49fdf182009-10-10 00:57:34 +000017#include "update_engine/action_pipe.h"
18#include "update_engine/download_action.h"
19#include "update_engine/mock_http_fetcher.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070020#include "update_engine/mock_system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000021#include "update_engine/omaha_hash_calculator.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070022#include "update_engine/fake_p2p_manager_configuration.h"
Darin Petkov73058b42010-10-06 16:32:19 -070023#include "update_engine/prefs_mock.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000024#include "update_engine/test_utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000025#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000026
27namespace chromeos_update_engine {
28
29using std::string;
30using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070031using testing::_;
32using testing::AtLeast;
33using testing::InSequence;
David Zeuthen8f191b22013-08-06 12:27:50 -070034using base::FilePath;
35using base::StringPrintf;
36using file_util::WriteFile;
37using file_util::ReadFileToString;
rspangler@google.com49fdf182009-10-10 00:57:34 +000038
39class DownloadActionTest : public ::testing::Test { };
40
41namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070042class DownloadActionDelegateMock : public DownloadActionDelegate {
43 public:
44 MOCK_METHOD1(SetDownloadStatus, void(bool active));
45 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
46};
47
rspangler@google.com49fdf182009-10-10 00:57:34 +000048class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
49 public:
David Zeuthena99981f2013-04-29 13:42:47 -070050 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Darin Petkovc97435c2010-07-20 12:37:43 -070051 : loop_(NULL),
52 processing_done_called_(false),
53 expected_code_(expected_code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 virtual ~DownloadActionTestProcessorDelegate() {
55 EXPECT_TRUE(processing_done_called_);
56 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070057 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070058 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 ASSERT_TRUE(loop_);
60 g_main_loop_quit(loop_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000061 vector<char> found_data;
62 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
David Zeuthena99981f2013-04-29 13:42:47 -070063 if (expected_code_ != kErrorCodeDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080064 ASSERT_EQ(expected_data_.size(), found_data.size());
65 for (unsigned i = 0; i < expected_data_.size(); i++) {
66 EXPECT_EQ(expected_data_[i], found_data[i]);
67 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000068 }
69 processing_done_called_ = true;
70 }
71
adlr@google.comc98a7ed2009-12-04 18:54:03 +000072 virtual void ActionCompleted(ActionProcessor* processor,
73 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070074 ErrorCode code) {
Darin Petkovc97435c2010-07-20 12:37:43 -070075 const string type = action->Type();
76 if (type == DownloadAction::StaticType()) {
77 EXPECT_EQ(expected_code_, code);
78 } else {
David Zeuthena99981f2013-04-29 13:42:47 -070079 EXPECT_EQ(kErrorCodeSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070080 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 }
82
83 GMainLoop *loop_;
84 string path_;
85 vector<char> expected_data_;
86 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070087 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000088};
89
Darin Petkov9ce452b2010-11-17 14:33:28 -080090class TestDirectFileWriter : public DirectFileWriter {
91 public:
92 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
93 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
94
Don Garrette410e0f2011-11-10 15:39:01 -080095 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080096 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -080097 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -080098 }
99 return DirectFileWriter::Write(bytes, count);
100 }
101
102 private:
103 // If positive, fail on the |fail_write_| call to Write.
104 int fail_write_;
105 int current_write_;
106};
107
rspangler@google.com49fdf182009-10-10 00:57:34 +0000108struct EntryPointArgs {
109 const vector<char> *data;
110 GMainLoop *loop;
111 ActionProcessor *processor;
112};
113
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700114struct StartProcessorInRunLoopArgs {
115 ActionProcessor* processor;
116 MockHttpFetcher* http_fetcher;
117};
118
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700120 ActionProcessor* processor =
121 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700123 MockHttpFetcher* http_fetcher =
124 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher;
125 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126 return FALSE;
127}
128
Darin Petkov9d911fa2010-08-19 09:36:08 -0700129void TestWithData(const vector<char>& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800130 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700131 bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
133
134 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700135 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800136 TestDirectFileWriter writer;
137 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700138
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700139 // We pull off the first byte from data and seek past it.
140
Darin Petkov7ed561b2011-10-04 02:59:03 -0700141 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700142 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700143 uint64_t size = data.size();
144 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700145 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700146 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700147 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700148 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700149 0,
150 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700151 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700152 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700153 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000154 ObjectFeederAction<InstallPlan> feeder_action;
155 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700156 PrefsMock prefs;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800157 MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0],
158 data.size(),
159 NULL);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700160 // takes ownership of passed in HttpFetcher
Jay Srinivasanf0572052012-10-23 18:12:56 -0700161 DownloadAction download_action(&prefs, NULL, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700162 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000163 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700164 DownloadActionDelegateMock download_delegate;
165 if (use_download_delegate) {
166 InSequence s;
167 download_action.set_delegate(&download_delegate);
168 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700169 if (data.size() > kMockHttpFetcherChunkSize)
170 EXPECT_CALL(download_delegate,
171 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800172 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700173 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
174 }
David Zeuthena99981f2013-04-29 13:42:47 -0700175 ErrorCode expected_code = kErrorCodeSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700176 if (fail_write > 0)
David Zeuthena99981f2013-04-29 13:42:47 -0700177 expected_code = kErrorCodeDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700178 DownloadActionTestProcessorDelegate delegate(expected_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000179 delegate.loop_ = loop;
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700180 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700181 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000182 ActionProcessor processor;
183 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000184 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000185 processor.EnqueueAction(&download_action);
186
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700187 StartProcessorInRunLoopArgs args;
188 args.processor = &processor;
189 args.http_fetcher = http_fetcher;
190 g_timeout_add(0, &StartProcessorInRunLoop, &args);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191 g_main_loop_run(loop);
192 g_main_loop_unref(loop);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193}
194} // namespace {}
195
196TEST(DownloadActionTest, SimpleTest) {
197 vector<char> small;
198 const char* foo = "foo";
199 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700200 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800201 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700202 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000203}
204
205TEST(DownloadActionTest, LargeTest) {
206 vector<char> big(5 * kMockHttpFetcherChunkSize);
207 char c = '0';
208 for (unsigned int i = 0; i < big.size(); i++) {
209 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800210 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000211 }
Darin Petkov50332f12010-09-24 11:44:47 -0700212 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800213 0, // fail_write
214 true); // use_download_delegate
215}
216
217TEST(DownloadActionTest, FailWriteTest) {
218 vector<char> big(5 * kMockHttpFetcherChunkSize);
219 char c = '0';
220 for (unsigned int i = 0; i < big.size(); i++) {
221 big[i] = c;
222 c = ('9' == c) ? '0' : c + 1;
223 }
224 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800225 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700226 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700227}
228
Darin Petkov9d911fa2010-08-19 09:36:08 -0700229TEST(DownloadActionTest, NoDownloadDelegateTest) {
230 vector<char> small;
231 const char* foo = "foofoo";
232 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700233 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800234 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700235 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000236}
237
238namespace {
239class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
240 public:
241 void ProcessingStopped(const ActionProcessor* processor) {
242 ASSERT_TRUE(loop_);
243 g_main_loop_quit(loop_);
244 }
245 GMainLoop *loop_;
246};
247
248gboolean TerminateEarlyTestStarter(gpointer data) {
249 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
250 processor->StartProcessing();
251 CHECK(processor->IsRunning());
252 processor->StopProcessing();
253 return FALSE;
254}
255
Darin Petkov9d911fa2010-08-19 09:36:08 -0700256void TestTerminateEarly(bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000257 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
258
259 vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
260 memset(&data[0], 0, data.size());
261
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700262 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000263 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700264 DirectFileWriter writer;
265
rspangler@google.com49fdf182009-10-10 00:57:34 +0000266 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000267 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700268 InstallPlan install_plan(false, false, "", 0, "", 0, "",
David Zeuthene7f89172013-10-31 10:21:04 -0700269 temp_file.GetPath(), "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000270 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700271 PrefsMock prefs;
Jay Srinivasanf0572052012-10-23 18:12:56 -0700272 DownloadAction download_action(&prefs, NULL,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800273 new MockHttpFetcher(&data[0],
274 data.size(),
275 NULL));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700276 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700277 DownloadActionDelegateMock download_delegate;
278 if (use_download_delegate) {
279 InSequence s;
280 download_action.set_delegate(&download_delegate);
281 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
282 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
283 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000284 TerminateEarlyTestProcessorDelegate delegate;
285 delegate.loop_ = loop;
286 ActionProcessor processor;
287 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000288 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000289 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000290 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000291
292 g_timeout_add(0, &TerminateEarlyTestStarter, &processor);
293 g_main_loop_run(loop);
294 g_main_loop_unref(loop);
295 }
296
297 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700298 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
299 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000300 if (resulting_file_size != 0)
301 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
302}
303
Darin Petkov9d911fa2010-08-19 09:36:08 -0700304} // namespace {}
305
306TEST(DownloadActionTest, TerminateEarlyTest) {
307 TestTerminateEarly(true);
308}
309
310TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
311 TestTerminateEarly(false);
312}
313
rspangler@google.com49fdf182009-10-10 00:57:34 +0000314class DownloadActionTestAction;
315
316template<>
317class ActionTraits<DownloadActionTestAction> {
318 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000319 typedef InstallPlan OutputObjectType;
320 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000321};
322
323// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700324class DownloadActionTestAction : public Action<DownloadActionTestAction> {
325 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000326 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000327 typedef InstallPlan InputObjectType;
328 typedef InstallPlan OutputObjectType;
329 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
330 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000331 ActionProcessor* processor() { return processor_; }
332 void PerformAction() {
333 did_run_ = true;
334 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000335 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000336 ASSERT_TRUE(processor());
David Zeuthena99981f2013-04-29 13:42:47 -0700337 processor()->ActionComplete(this, kErrorCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000338 }
339 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000340 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341 bool did_run_;
342};
343
344namespace {
345// This class is an ActionProcessorDelegate that simply terminates the
346// run loop when the ActionProcessor has completed processing. It's used
347// only by the test PassObjectOutTest.
348class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
349 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700350 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000351 ASSERT_TRUE(loop_);
352 g_main_loop_quit(loop_);
353 }
354 GMainLoop *loop_;
355};
356
357gboolean PassObjectOutTestStarter(gpointer data) {
358 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
359 processor->StartProcessing();
360 return FALSE;
361}
362}
363
364TEST(DownloadActionTest, PassObjectOutTest) {
365 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
366
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700367 DirectFileWriter writer;
368
rspangler@google.com49fdf182009-10-10 00:57:34 +0000369 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700370 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700371 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700372 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700373 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800374 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700375 0,
376 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700377 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700378 "/dev/null",
379 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000380 ObjectFeederAction<InstallPlan> feeder_action;
381 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700382 PrefsMock prefs;
Jay Srinivasanf0572052012-10-23 18:12:56 -0700383 DownloadAction download_action(&prefs, NULL,
384 new MockHttpFetcher("x", 1, NULL));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700385 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000386
387 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000388 test_action.expected_input_object_ = install_plan;
389 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000390 BondActions(&download_action, &test_action);
391
392 ActionProcessor processor;
393 PassObjectOutTestProcessorDelegate delegate;
394 delegate.loop_ = loop;
395 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000396 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000397 processor.EnqueueAction(&download_action);
398 processor.EnqueueAction(&test_action);
399
400 g_timeout_add(0, &PassObjectOutTestStarter, &processor);
401 g_main_loop_run(loop);
402 g_main_loop_unref(loop);
403
404 EXPECT_EQ(true, test_action.did_run_);
405}
406
407TEST(DownloadActionTest, BadOutFileTest) {
408 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
409
410 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700411 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000412
413 // takes ownership of passed in HttpFetcher
David Zeuthene7f89172013-10-31 10:21:04 -0700414 InstallPlan install_plan(false, false, "", 0, "", 0, "", path, "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000415 ObjectFeederAction<InstallPlan> feeder_action;
416 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700417 PrefsMock prefs;
Jay Srinivasanf0572052012-10-23 18:12:56 -0700418 DownloadAction download_action(&prefs, NULL,
419 new MockHttpFetcher("x", 1, NULL));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700420 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700421
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000422 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000423
424 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000425 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000426 processor.EnqueueAction(&download_action);
427 processor.StartProcessing();
428 ASSERT_FALSE(processor.IsRunning());
429
430 g_main_loop_unref(loop);
431}
432
David Zeuthen8f191b22013-08-06 12:27:50 -0700433gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
434 ActionProcessor* processor = reinterpret_cast<ActionProcessor*>(user_data);
435 processor->StartProcessing();
436 return FALSE;
437}
438
439// Test fixture for P2P tests.
440class P2PDownloadActionTest : public testing::Test {
441protected:
442 P2PDownloadActionTest()
443 : loop_(NULL),
444 start_at_offset_(0) {}
445
446 virtual ~P2PDownloadActionTest() {}
447
448 // Derived from testing::Test.
449 virtual void SetUp() {
450 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
451 }
452
453 // Derived from testing::Test.
454 virtual void TearDown() {
455 if (loop_ != NULL)
456 g_main_loop_unref(loop_);
457 }
458
459 // To be called by tests to setup the download. The
460 // |starting_offset| parameter is for where to resume.
461 void SetupDownload(off_t starting_offset) {
462 start_at_offset_ = starting_offset;
463 // Prepare data 10 kB of data.
464 data_.clear();
465 for (unsigned int i = 0; i < 10 * 1000; i++)
466 data_ += 'a' + (i % 25);
467
468 // Setup p2p.
469 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
470 p2p_manager_.reset(P2PManager::Construct(test_conf, NULL, "cros_au", 3));
471 mock_system_state_.set_p2p_manager(p2p_manager_.get());
472 }
473
474 // To be called by tests to perform the download. The
475 // |use_p2p_to_share| parameter is used to indicate whether the
476 // payload should be shared via p2p.
477 void StartDownload(bool use_p2p_to_share) {
478 mock_system_state_.request_params()->set_use_p2p_for_sharing(
479 use_p2p_to_share);
480
481 ScopedTempFile output_temp_file;
482 TestDirectFileWriter writer;
483 InstallPlan install_plan(false,
484 false,
485 "",
486 data_.length(),
487 "1234hash",
488 0,
489 "",
490 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700491 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700492 "");
493 ObjectFeederAction<InstallPlan> feeder_action;
494 feeder_action.set_obj(install_plan);
495 PrefsMock prefs;
496 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
497 data_.length(),
498 NULL);
499 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
500 download_action_.reset(new DownloadAction(&prefs, &mock_system_state_,
501 http_fetcher_));
502 download_action_->SetTestFileWriter(&writer);
503 BondActions(&feeder_action, download_action_.get());
504 DownloadActionTestProcessorDelegate delegate(kErrorCodeSuccess);
505 delegate.loop_ = loop_;
506 delegate.expected_data_ = vector<char>(data_.begin() + start_at_offset_,
507 data_.end());
508 delegate.path_ = output_temp_file.GetPath();
509 processor_.set_delegate(&delegate);
510 processor_.EnqueueAction(&feeder_action);
511 processor_.EnqueueAction(download_action_.get());
512
513 g_timeout_add(0, &StartProcessorInRunLoopForP2P, this);
514 g_main_loop_run(loop_);
515 }
516
517 // The DownloadAction instance under test.
518 scoped_ptr<DownloadAction> download_action_;
519
520 // The HttpFetcher used in the test.
521 MockHttpFetcher* http_fetcher_;
522
523 // The P2PManager used in the test.
524 scoped_ptr<P2PManager> p2p_manager_;
525
526 // The ActionProcessor used for running the actions.
527 ActionProcessor processor_;
528
529 // A fake system state.
530 MockSystemState mock_system_state_;
531
532 // The data being downloaded.
533 string data_;
534
535private:
536 // Callback used in StartDownload() method.
537 static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
538 class P2PDownloadActionTest *test =
539 reinterpret_cast<P2PDownloadActionTest*>(user_data);
540 test->processor_.StartProcessing();
541 test->http_fetcher_->SetOffset(test->start_at_offset_);
542 return FALSE;
543 }
544
545 // Mainloop used to make StartDownload() synchronous.
546 GMainLoop *loop_;
547
548 // The requested starting offset passed to SetupDownload().
549 off_t start_at_offset_;
550};
551
552TEST_F(P2PDownloadActionTest, IsWrittenTo) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700553 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
554 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
555 << "Please update your system to support this feature.";
556 return;
557 }
558
David Zeuthen8f191b22013-08-06 12:27:50 -0700559 SetupDownload(0); // starting_offset
560 StartDownload(true); // use_p2p_to_share
561
562 // Check the p2p file and its content matches what was sent.
563 string file_id = download_action_->p2p_file_id();
564 EXPECT_NE(file_id, "");
565 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
566 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
567 string p2p_file_contents;
568 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
569 &p2p_file_contents));
570 EXPECT_EQ(data_, p2p_file_contents);
571}
572
573TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700574 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
575 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
576 << "Please update your system to support this feature.";
577 return;
578 }
579
David Zeuthen8f191b22013-08-06 12:27:50 -0700580 SetupDownload(1000); // starting_offset
581 StartDownload(true); // use_p2p_to_share
582
583 // DownloadAction should convey that the file is not being shared.
584 // and that we don't have any p2p files.
585 EXPECT_EQ(download_action_->p2p_file_id(), "");
586 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
587}
588
589TEST_F(P2PDownloadActionTest, CanAppend) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700590 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
591 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
592 << "Please update your system to support this feature.";
593 return;
594 }
595
David Zeuthen8f191b22013-08-06 12:27:50 -0700596 SetupDownload(1000); // starting_offset
597
598 // Prepare the file with existing data before starting to write to
599 // it via DownloadAction.
600 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
601 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
602 string existing_data;
603 for (unsigned int i = 0; i < 1000; i++)
604 existing_data += '0' + (i % 10);
605 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
606 1000), 1000);
607
608 StartDownload(true); // use_p2p_to_share
609
610 // DownloadAction should convey the same file_id and the file should
611 // have the expected size.
612 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
613 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
614 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
615 string p2p_file_contents;
616 // Check that the first 1000 bytes wasn't touched and that we
617 // appended the remaining as appropriate.
618 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
619 &p2p_file_contents));
620 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
621 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
622}
623
David Zeuthen8f191b22013-08-06 12:27:50 -0700624TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700625 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
626 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
627 << "Please update your system to support this feature.";
628 return;
629 }
630
David Zeuthen8f191b22013-08-06 12:27:50 -0700631 SetupDownload(1000); // starting_offset
632
633 // Prepare the file with all existing data before starting to write
634 // to it via DownloadAction.
635 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
636 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
637 string existing_data;
638 for (unsigned int i = 0; i < 1000; i++)
639 existing_data += '0' + (i % 10);
640 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
641 1000), 1000);
642
643 // Check that the file is there.
644 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
645 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
646
647 StartDownload(false); // use_p2p_to_share
648
649 // DownloadAction should have deleted the p2p file. Check that it's gone.
650 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
651 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
652}
653
rspangler@google.com49fdf182009-10-10 00:57:34 +0000654} // namespace chromeos_update_engine