blob: be9d1706be0b03660da391de66141986d980d251 [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(),
152 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000153 ObjectFeederAction<InstallPlan> feeder_action;
154 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700155 PrefsMock prefs;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800156 MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0],
157 data.size(),
158 NULL);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700159 // takes ownership of passed in HttpFetcher
Jay Srinivasanf0572052012-10-23 18:12:56 -0700160 DownloadAction download_action(&prefs, NULL, http_fetcher);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700161 download_action.SetTestFileWriter(&writer);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000162 BondActions(&feeder_action, &download_action);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700163 DownloadActionDelegateMock download_delegate;
164 if (use_download_delegate) {
165 InSequence s;
166 download_action.set_delegate(&download_delegate);
167 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700168 if (data.size() > kMockHttpFetcherChunkSize)
169 EXPECT_CALL(download_delegate,
170 BytesReceived(1 + kMockHttpFetcherChunkSize, _));
Darin Petkov9ce452b2010-11-17 14:33:28 -0800171 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1));
Darin Petkov9d911fa2010-08-19 09:36:08 -0700172 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
173 }
David Zeuthena99981f2013-04-29 13:42:47 -0700174 ErrorCode expected_code = kErrorCodeSuccess;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700175 if (fail_write > 0)
David Zeuthena99981f2013-04-29 13:42:47 -0700176 expected_code = kErrorCodeDownloadWriteError;
Darin Petkov50332f12010-09-24 11:44:47 -0700177 DownloadActionTestProcessorDelegate delegate(expected_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000178 delegate.loop_ = loop;
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700179 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700180 delegate.path_ = output_temp_file.GetPath();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000181 ActionProcessor processor;
182 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000183 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000184 processor.EnqueueAction(&download_action);
185
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700186 StartProcessorInRunLoopArgs args;
187 args.processor = &processor;
188 args.http_fetcher = http_fetcher;
189 g_timeout_add(0, &StartProcessorInRunLoop, &args);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000190 g_main_loop_run(loop);
191 g_main_loop_unref(loop);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000192}
193} // namespace {}
194
195TEST(DownloadActionTest, SimpleTest) {
196 vector<char> small;
197 const char* foo = "foo";
198 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700199 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800200 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700201 true); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000202}
203
204TEST(DownloadActionTest, LargeTest) {
205 vector<char> big(5 * kMockHttpFetcherChunkSize);
206 char c = '0';
207 for (unsigned int i = 0; i < big.size(); i++) {
208 big[i] = c;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800209 c = ('9' == c) ? '0' : c + 1;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000210 }
Darin Petkov50332f12010-09-24 11:44:47 -0700211 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800212 0, // fail_write
213 true); // use_download_delegate
214}
215
216TEST(DownloadActionTest, FailWriteTest) {
217 vector<char> big(5 * kMockHttpFetcherChunkSize);
218 char c = '0';
219 for (unsigned int i = 0; i < big.size(); i++) {
220 big[i] = c;
221 c = ('9' == c) ? '0' : c + 1;
222 }
223 TestWithData(big,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800224 2, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700225 true); // use_download_delegate
Darin Petkovc97435c2010-07-20 12:37:43 -0700226}
227
Darin Petkov9d911fa2010-08-19 09:36:08 -0700228TEST(DownloadActionTest, NoDownloadDelegateTest) {
229 vector<char> small;
230 const char* foo = "foofoo";
231 small.insert(small.end(), foo, foo + strlen(foo));
Darin Petkov50332f12010-09-24 11:44:47 -0700232 TestWithData(small,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800233 0, // fail_write
Darin Petkov50332f12010-09-24 11:44:47 -0700234 false); // use_download_delegate
rspangler@google.com49fdf182009-10-10 00:57:34 +0000235}
236
237namespace {
238class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
239 public:
240 void ProcessingStopped(const ActionProcessor* processor) {
241 ASSERT_TRUE(loop_);
242 g_main_loop_quit(loop_);
243 }
244 GMainLoop *loop_;
245};
246
247gboolean TerminateEarlyTestStarter(gpointer data) {
248 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
249 processor->StartProcessing();
250 CHECK(processor->IsRunning());
251 processor->StopProcessing();
252 return FALSE;
253}
254
Darin Petkov9d911fa2010-08-19 09:36:08 -0700255void TestTerminateEarly(bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000256 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
257
258 vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
259 memset(&data[0], 0, data.size());
260
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700261 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000262 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700263 DirectFileWriter writer;
264
rspangler@google.com49fdf182009-10-10 00:57:34 +0000265 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000266 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700267 InstallPlan install_plan(false, false, "", 0, "", 0, "",
268 temp_file.GetPath(), "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000269 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700270 PrefsMock prefs;
Jay Srinivasanf0572052012-10-23 18:12:56 -0700271 DownloadAction download_action(&prefs, NULL,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800272 new MockHttpFetcher(&data[0],
273 data.size(),
274 NULL));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700275 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700276 DownloadActionDelegateMock download_delegate;
277 if (use_download_delegate) {
278 InSequence s;
279 download_action.set_delegate(&download_delegate);
280 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
281 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
282 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000283 TerminateEarlyTestProcessorDelegate delegate;
284 delegate.loop_ = loop;
285 ActionProcessor processor;
286 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000287 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000288 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000289 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000290
291 g_timeout_add(0, &TerminateEarlyTestStarter, &processor);
292 g_main_loop_run(loop);
293 g_main_loop_unref(loop);
294 }
295
296 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700297 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
298 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000299 if (resulting_file_size != 0)
300 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
301}
302
Darin Petkov9d911fa2010-08-19 09:36:08 -0700303} // namespace {}
304
305TEST(DownloadActionTest, TerminateEarlyTest) {
306 TestTerminateEarly(true);
307}
308
309TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
310 TestTerminateEarly(false);
311}
312
rspangler@google.com49fdf182009-10-10 00:57:34 +0000313class DownloadActionTestAction;
314
315template<>
316class ActionTraits<DownloadActionTestAction> {
317 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000318 typedef InstallPlan OutputObjectType;
319 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000320};
321
322// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700323class DownloadActionTestAction : public Action<DownloadActionTestAction> {
324 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000325 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000326 typedef InstallPlan InputObjectType;
327 typedef InstallPlan OutputObjectType;
328 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
329 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000330 ActionProcessor* processor() { return processor_; }
331 void PerformAction() {
332 did_run_ = true;
333 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000334 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000335 ASSERT_TRUE(processor());
David Zeuthena99981f2013-04-29 13:42:47 -0700336 processor()->ActionComplete(this, kErrorCodeSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000337 }
338 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000339 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000340 bool did_run_;
341};
342
343namespace {
344// This class is an ActionProcessorDelegate that simply terminates the
345// run loop when the ActionProcessor has completed processing. It's used
346// only by the test PassObjectOutTest.
347class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
348 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700349 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000350 ASSERT_TRUE(loop_);
351 g_main_loop_quit(loop_);
352 }
353 GMainLoop *loop_;
354};
355
356gboolean PassObjectOutTestStarter(gpointer data) {
357 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
358 processor->StartProcessing();
359 return FALSE;
360}
361}
362
363TEST(DownloadActionTest, PassObjectOutTest) {
364 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
365
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700366 DirectFileWriter writer;
367
rspangler@google.com49fdf182009-10-10 00:57:34 +0000368 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700369 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700370 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700371 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700372 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800373 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700374 0,
375 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700376 "/dev/null",
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800377 "/dev/null");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000378 ObjectFeederAction<InstallPlan> feeder_action;
379 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700380 PrefsMock prefs;
Jay Srinivasanf0572052012-10-23 18:12:56 -0700381 DownloadAction download_action(&prefs, NULL,
382 new MockHttpFetcher("x", 1, NULL));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700383 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000384
385 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000386 test_action.expected_input_object_ = install_plan;
387 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000388 BondActions(&download_action, &test_action);
389
390 ActionProcessor processor;
391 PassObjectOutTestProcessorDelegate delegate;
392 delegate.loop_ = loop;
393 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000394 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000395 processor.EnqueueAction(&download_action);
396 processor.EnqueueAction(&test_action);
397
398 g_timeout_add(0, &PassObjectOutTestStarter, &processor);
399 g_main_loop_run(loop);
400 g_main_loop_unref(loop);
401
402 EXPECT_EQ(true, test_action.did_run_);
403}
404
405TEST(DownloadActionTest, BadOutFileTest) {
406 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
407
408 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700409 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000410
411 // takes ownership of passed in HttpFetcher
Gilad Arnold21504f02013-05-24 08:51:22 -0700412 InstallPlan install_plan(false, false, "", 0, "", 0, "", path, "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000413 ObjectFeederAction<InstallPlan> feeder_action;
414 feeder_action.set_obj(install_plan);
Darin Petkov73058b42010-10-06 16:32:19 -0700415 PrefsMock prefs;
Jay Srinivasanf0572052012-10-23 18:12:56 -0700416 DownloadAction download_action(&prefs, NULL,
417 new MockHttpFetcher("x", 1, NULL));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700418 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700419
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000420 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000421
422 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000423 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000424 processor.EnqueueAction(&download_action);
425 processor.StartProcessing();
426 ASSERT_FALSE(processor.IsRunning());
427
428 g_main_loop_unref(loop);
429}
430
David Zeuthen8f191b22013-08-06 12:27:50 -0700431gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
432 ActionProcessor* processor = reinterpret_cast<ActionProcessor*>(user_data);
433 processor->StartProcessing();
434 return FALSE;
435}
436
437// Test fixture for P2P tests.
438class P2PDownloadActionTest : public testing::Test {
439protected:
440 P2PDownloadActionTest()
441 : loop_(NULL),
442 start_at_offset_(0) {}
443
444 virtual ~P2PDownloadActionTest() {}
445
446 // Derived from testing::Test.
447 virtual void SetUp() {
448 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
449 }
450
451 // Derived from testing::Test.
452 virtual void TearDown() {
453 if (loop_ != NULL)
454 g_main_loop_unref(loop_);
455 }
456
457 // To be called by tests to setup the download. The
458 // |starting_offset| parameter is for where to resume.
459 void SetupDownload(off_t starting_offset) {
460 start_at_offset_ = starting_offset;
461 // Prepare data 10 kB of data.
462 data_.clear();
463 for (unsigned int i = 0; i < 10 * 1000; i++)
464 data_ += 'a' + (i % 25);
465
466 // Setup p2p.
467 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
468 p2p_manager_.reset(P2PManager::Construct(test_conf, NULL, "cros_au", 3));
469 mock_system_state_.set_p2p_manager(p2p_manager_.get());
470 }
471
472 // To be called by tests to perform the download. The
473 // |use_p2p_to_share| parameter is used to indicate whether the
474 // payload should be shared via p2p.
475 void StartDownload(bool use_p2p_to_share) {
476 mock_system_state_.request_params()->set_use_p2p_for_sharing(
477 use_p2p_to_share);
478
479 ScopedTempFile output_temp_file;
480 TestDirectFileWriter writer;
481 InstallPlan install_plan(false,
482 false,
483 "",
484 data_.length(),
485 "1234hash",
486 0,
487 "",
488 output_temp_file.GetPath(),
489 "");
490 ObjectFeederAction<InstallPlan> feeder_action;
491 feeder_action.set_obj(install_plan);
492 PrefsMock prefs;
493 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
494 data_.length(),
495 NULL);
496 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
497 download_action_.reset(new DownloadAction(&prefs, &mock_system_state_,
498 http_fetcher_));
499 download_action_->SetTestFileWriter(&writer);
500 BondActions(&feeder_action, download_action_.get());
501 DownloadActionTestProcessorDelegate delegate(kErrorCodeSuccess);
502 delegate.loop_ = loop_;
503 delegate.expected_data_ = vector<char>(data_.begin() + start_at_offset_,
504 data_.end());
505 delegate.path_ = output_temp_file.GetPath();
506 processor_.set_delegate(&delegate);
507 processor_.EnqueueAction(&feeder_action);
508 processor_.EnqueueAction(download_action_.get());
509
510 g_timeout_add(0, &StartProcessorInRunLoopForP2P, this);
511 g_main_loop_run(loop_);
512 }
513
514 // The DownloadAction instance under test.
515 scoped_ptr<DownloadAction> download_action_;
516
517 // The HttpFetcher used in the test.
518 MockHttpFetcher* http_fetcher_;
519
520 // The P2PManager used in the test.
521 scoped_ptr<P2PManager> p2p_manager_;
522
523 // The ActionProcessor used for running the actions.
524 ActionProcessor processor_;
525
526 // A fake system state.
527 MockSystemState mock_system_state_;
528
529 // The data being downloaded.
530 string data_;
531
532private:
533 // Callback used in StartDownload() method.
534 static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
535 class P2PDownloadActionTest *test =
536 reinterpret_cast<P2PDownloadActionTest*>(user_data);
537 test->processor_.StartProcessing();
538 test->http_fetcher_->SetOffset(test->start_at_offset_);
539 return FALSE;
540 }
541
542 // Mainloop used to make StartDownload() synchronous.
543 GMainLoop *loop_;
544
545 // The requested starting offset passed to SetupDownload().
546 off_t start_at_offset_;
547};
548
549TEST_F(P2PDownloadActionTest, IsWrittenTo) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700550 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
551 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
552 << "Please update your system to support this feature.";
553 return;
554 }
555
David Zeuthen8f191b22013-08-06 12:27:50 -0700556 SetupDownload(0); // starting_offset
557 StartDownload(true); // use_p2p_to_share
558
559 // Check the p2p file and its content matches what was sent.
560 string file_id = download_action_->p2p_file_id();
561 EXPECT_NE(file_id, "");
562 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
563 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
564 string p2p_file_contents;
565 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
566 &p2p_file_contents));
567 EXPECT_EQ(data_, p2p_file_contents);
568}
569
570TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700571 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
572 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
573 << "Please update your system to support this feature.";
574 return;
575 }
576
David Zeuthen8f191b22013-08-06 12:27:50 -0700577 SetupDownload(1000); // starting_offset
578 StartDownload(true); // use_p2p_to_share
579
580 // DownloadAction should convey that the file is not being shared.
581 // and that we don't have any p2p files.
582 EXPECT_EQ(download_action_->p2p_file_id(), "");
583 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
584}
585
586TEST_F(P2PDownloadActionTest, CanAppend) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700587 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
588 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
589 << "Please update your system to support this feature.";
590 return;
591 }
592
David Zeuthen8f191b22013-08-06 12:27:50 -0700593 SetupDownload(1000); // starting_offset
594
595 // Prepare the file with existing data before starting to write to
596 // it via DownloadAction.
597 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
598 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
599 string existing_data;
600 for (unsigned int i = 0; i < 1000; i++)
601 existing_data += '0' + (i % 10);
602 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
603 1000), 1000);
604
605 StartDownload(true); // use_p2p_to_share
606
607 // DownloadAction should convey the same file_id and the file should
608 // have the expected size.
609 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
610 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
611 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
612 string p2p_file_contents;
613 // Check that the first 1000 bytes wasn't touched and that we
614 // appended the remaining as appropriate.
615 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
616 &p2p_file_contents));
617 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
618 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
619}
620
David Zeuthen8f191b22013-08-06 12:27:50 -0700621TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700622 if (!utils::IsXAttrSupported(FilePath("/tmp"))) {
623 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
624 << "Please update your system to support this feature.";
625 return;
626 }
627
David Zeuthen8f191b22013-08-06 12:27:50 -0700628 SetupDownload(1000); // starting_offset
629
630 // Prepare the file with all existing data before starting to write
631 // to it via DownloadAction.
632 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
633 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
634 string existing_data;
635 for (unsigned int i = 0; i < 1000; i++)
636 existing_data += '0' + (i % 10);
637 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
638 1000), 1000);
639
640 // Check that the file is there.
641 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
642 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
643
644 StartDownload(false); // use_p2p_to_share
645
646 // DownloadAction should have deleted the p2p file. Check that it's gone.
647 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
648 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
649}
650
rspangler@google.com49fdf182009-10-10 00:57:34 +0000651} // namespace chromeos_update_engine