blob: 1dc1bebb34c618c37d33718bb11026666252f77e [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
Alex Deymo8427b4a2014-11-05 14:00:32 -08005#include "update_engine/download_action.h"
6
rspangler@google.com49fdf182009-10-10 00:57:34 +00007#include <glib.h>
Darin Petkov9d911fa2010-08-19 09:36:08 -07008#include <gmock/gmock.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00009#include <gtest/gtest.h>
Darin Petkov73058b42010-10-06 16:32:19 -070010
Ben Chan02f7c1d2014-10-18 15:18:02 -070011#include <memory>
David Zeuthen8f191b22013-08-06 12:27:50 -070012#include <string>
13#include <utility>
14#include <vector>
15
Alex Vakulenko75039d72014-03-25 12:36:28 -070016#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070017#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070018#include <base/strings/stringprintf.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070019
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include "update_engine/action_pipe.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070021#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070022#include "update_engine/fake_system_state.h"
23#include "update_engine/mock_http_fetcher.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080024#include "update_engine/mock_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070025#include "update_engine/omaha_hash_calculator.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000026#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070027#include "update_engine/update_manager/fake_update_manager.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000028#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000029
30namespace chromeos_update_engine {
31
Alex Deymof329b932014-10-30 01:37:48 -070032using base::FilePath;
33using base::ReadFileToString;
34using base::WriteFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000035using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070036using std::unique_ptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000037using std::vector;
Darin Petkov9d911fa2010-08-19 09:36:08 -070038using testing::AtLeast;
39using testing::InSequence;
Gilad Arnold74b5f552014-10-07 08:17:16 -070040using testing::Return;
Alex Deymof329b932014-10-30 01:37:48 -070041using testing::_;
Alex Deymo10875d92014-11-10 21:52:57 -080042using test_utils::ScopedTempFile;
rspangler@google.com49fdf182009-10-10 00:57:34 +000043
44class DownloadActionTest : public ::testing::Test { };
45
46namespace {
Darin Petkov9d911fa2010-08-19 09:36:08 -070047class DownloadActionDelegateMock : public DownloadActionDelegate {
48 public:
49 MOCK_METHOD1(SetDownloadStatus, void(bool active));
50 MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total));
51};
52
rspangler@google.com49fdf182009-10-10 00:57:34 +000053class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
54 public:
David Zeuthena99981f2013-04-29 13:42:47 -070055 explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code)
Alex Vakulenko88b591f2014-08-28 16:48:57 -070056 : loop_(nullptr),
Darin Petkovc97435c2010-07-20 12:37:43 -070057 processing_done_called_(false),
58 expected_code_(expected_code) {}
Alex Deymo610277e2014-11-11 21:18:11 -080059 ~DownloadActionTestProcessorDelegate() override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000060 EXPECT_TRUE(processing_done_called_);
61 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -080062 void ProcessingDone(const ActionProcessor* processor,
63 ErrorCode code) override {
rspangler@google.com49fdf182009-10-10 00:57:34 +000064 ASSERT_TRUE(loop_);
65 g_main_loop_quit(loop_);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080066 chromeos::Blob found_data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000067 ASSERT_TRUE(utils::ReadFile(path_, &found_data));
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070068 if (expected_code_ != ErrorCode::kDownloadWriteError) {
Darin Petkov9ce452b2010-11-17 14:33:28 -080069 ASSERT_EQ(expected_data_.size(), found_data.size());
70 for (unsigned i = 0; i < expected_data_.size(); i++) {
71 EXPECT_EQ(expected_data_[i], found_data[i]);
72 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 }
74 processing_done_called_ = true;
75 }
76
Yunlian Jiang35866ed2015-01-29 13:09:20 -080077 void ActionCompleted(ActionProcessor* processor,
78 AbstractAction* action,
79 ErrorCode code) override {
Darin Petkovc97435c2010-07-20 12:37:43 -070080 const string type = action->Type();
81 if (type == DownloadAction::StaticType()) {
82 EXPECT_EQ(expected_code_, code);
83 } else {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070084 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkovc97435c2010-07-20 12:37:43 -070085 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000086 }
87
88 GMainLoop *loop_;
89 string path_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080090 chromeos::Blob expected_data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000091 bool processing_done_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070092 ErrorCode expected_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000093};
94
Darin Petkov9ce452b2010-11-17 14:33:28 -080095class TestDirectFileWriter : public DirectFileWriter {
96 public:
97 TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
98 void set_fail_write(int fail_write) { fail_write_ = fail_write; }
99
Don Garrette410e0f2011-11-10 15:39:01 -0800100 virtual bool Write(const void* bytes, size_t count) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800101 if (++current_write_ == fail_write_) {
Don Garrette410e0f2011-11-10 15:39:01 -0800102 return false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800103 }
104 return DirectFileWriter::Write(bytes, count);
105 }
106
107 private:
108 // If positive, fail on the |fail_write_| call to Write.
109 int fail_write_;
110 int current_write_;
111};
112
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700113struct StartProcessorInRunLoopArgs {
114 ActionProcessor* processor;
115 MockHttpFetcher* http_fetcher;
116};
117
rspangler@google.com49fdf182009-10-10 00:57:34 +0000118gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700119 ActionProcessor* processor =
120 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121 processor->StartProcessing();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700122 MockHttpFetcher* http_fetcher =
123 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher;
124 http_fetcher->SetOffset(1);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125 return FALSE;
126}
127
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800128void TestWithData(const chromeos::Blob& data,
Darin Petkov9ce452b2010-11-17 14:33:28 -0800129 int fail_write,
Darin Petkov9d911fa2010-08-19 09:36:08 -0700130 bool use_download_delegate) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000131 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
132
133 // TODO(adlr): see if we need a different file for build bots
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700134 ScopedTempFile output_temp_file;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800135 TestDirectFileWriter writer;
136 writer.set_fail_write(fail_write);
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700137
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700138 // We pull off the first byte from data and seek past it.
139
Darin Petkov7ed561b2011-10-04 02:59:03 -0700140 string hash =
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700141 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700142 uint64_t size = data.size();
143 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700144 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700145 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700146 size,
Darin Petkovc97435c2010-07-20 12:37:43 -0700147 hash,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700148 0,
149 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700150 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700151 "",
Allie Woodfdf00512015-03-02 13:34:55 -0800152 "",
153 "",
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);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800157 MockPrefs prefs;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800158 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800159 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;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800181 delegate.expected_data_ = chromeos::Blob(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) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800198 chromeos::Blob small;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000199 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) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800207 chromeos::Blob big(5 * kMockHttpFetcherChunkSize);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000208 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) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800219 chromeos::Blob big(5 * kMockHttpFetcherChunkSize);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800220 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) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800231 chromeos::Blob small;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700232 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
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800260 chromeos::Blob data(kMockHttpFetcherChunkSize +
261 kMockHttpFetcherChunkSize / 2);
262 memset(data.data(), 0, data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000263
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700264 ScopedTempFile temp_file;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000265 {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700266 DirectFileWriter writer;
267
rspangler@google.com49fdf182009-10-10 00:57:34 +0000268 // takes ownership of passed in HttpFetcher
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000269 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold21504f02013-05-24 08:51:22 -0700270 InstallPlan install_plan(false, false, "", 0, "", 0, "",
Allie Woodfdf00512015-03-02 13:34:55 -0800271 temp_file.GetPath(), "", "", "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000272 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800273 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700274 DownloadAction download_action(&prefs, nullptr,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800275 new MockHttpFetcher(data.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800276 data.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700277 nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700278 download_action.SetTestFileWriter(&writer);
Darin Petkov9d911fa2010-08-19 09:36:08 -0700279 DownloadActionDelegateMock download_delegate;
280 if (use_download_delegate) {
281 InSequence s;
282 download_action.set_delegate(&download_delegate);
283 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1);
284 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1);
285 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000286 TerminateEarlyTestProcessorDelegate delegate;
287 delegate.loop_ = loop;
288 ActionProcessor processor;
289 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000290 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000291 processor.EnqueueAction(&download_action);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000292 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000293
294 g_timeout_add(0, &TerminateEarlyTestStarter, &processor);
295 g_main_loop_run(loop);
296 g_main_loop_unref(loop);
297 }
298
299 // 1 or 0 chunks should have come through
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700300 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath()));
301 EXPECT_GE(resulting_file_size, 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000302 if (resulting_file_size != 0)
303 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size);
304}
305
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700306} // namespace
Darin Petkov9d911fa2010-08-19 09:36:08 -0700307
308TEST(DownloadActionTest, TerminateEarlyTest) {
309 TestTerminateEarly(true);
310}
311
312TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
313 TestTerminateEarly(false);
314}
315
rspangler@google.com49fdf182009-10-10 00:57:34 +0000316class DownloadActionTestAction;
317
318template<>
319class ActionTraits<DownloadActionTestAction> {
320 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000321 typedef InstallPlan OutputObjectType;
322 typedef InstallPlan InputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000323};
324
325// This is a simple Action class for testing.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700326class DownloadActionTestAction : public Action<DownloadActionTestAction> {
327 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +0000328 DownloadActionTestAction() : did_run_(false) {}
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000329 typedef InstallPlan InputObjectType;
330 typedef InstallPlan OutputObjectType;
331 ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
332 ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000333 ActionProcessor* processor() { return processor_; }
334 void PerformAction() {
335 did_run_ = true;
336 ASSERT_TRUE(HasInputObject());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000337 EXPECT_TRUE(expected_input_object_ == GetInputObject());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000338 ASSERT_TRUE(processor());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700339 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000340 }
341 string Type() const { return "DownloadActionTestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000342 InstallPlan expected_input_object_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000343 bool did_run_;
344};
345
346namespace {
347// This class is an ActionProcessorDelegate that simply terminates the
348// run loop when the ActionProcessor has completed processing. It's used
349// only by the test PassObjectOutTest.
350class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
351 public:
David Zeuthena99981f2013-04-29 13:42:47 -0700352 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000353 ASSERT_TRUE(loop_);
354 g_main_loop_quit(loop_);
355 }
356 GMainLoop *loop_;
357};
358
359gboolean PassObjectOutTestStarter(gpointer data) {
360 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
361 processor->StartProcessing();
362 return FALSE;
363}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700364} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000365
366TEST(DownloadActionTest, PassObjectOutTest) {
367 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
368
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700369 DirectFileWriter writer;
370
rspangler@google.com49fdf182009-10-10 00:57:34 +0000371 // takes ownership of passed in HttpFetcher
Darin Petkov7ed561b2011-10-04 02:59:03 -0700372 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700373 false,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700374 "",
Darin Petkov50332f12010-09-24 11:44:47 -0700375 1,
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800376 OmahaHashCalculator::OmahaHashOfString("x"),
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700377 0,
378 "",
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700379 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700380 "/dev/null",
Allie Woodfdf00512015-03-02 13:34:55 -0800381 "/dev/null",
382 "/dev/null",
David Zeuthene7f89172013-10-31 10:21:04 -0700383 "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000384 ObjectFeederAction<InstallPlan> feeder_action;
385 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800386 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700387 DownloadAction download_action(&prefs, nullptr,
388 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700389 download_action.SetTestFileWriter(&writer);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000390
391 DownloadActionTestAction test_action;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000392 test_action.expected_input_object_ = install_plan;
393 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000394 BondActions(&download_action, &test_action);
395
396 ActionProcessor processor;
397 PassObjectOutTestProcessorDelegate delegate;
398 delegate.loop_ = loop;
399 processor.set_delegate(&delegate);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000400 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000401 processor.EnqueueAction(&download_action);
402 processor.EnqueueAction(&test_action);
403
404 g_timeout_add(0, &PassObjectOutTestStarter, &processor);
405 g_main_loop_run(loop);
406 g_main_loop_unref(loop);
407
408 EXPECT_EQ(true, test_action.did_run_);
409}
410
411TEST(DownloadActionTest, BadOutFileTest) {
412 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
413
414 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs");
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700415 DirectFileWriter writer;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000416
417 // takes ownership of passed in HttpFetcher
Allie Woodfdf00512015-03-02 13:34:55 -0800418 InstallPlan install_plan(
419 false, false, "", 0, "", 0, "", path, "", "", "", "");
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000420 ObjectFeederAction<InstallPlan> feeder_action;
421 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800422 MockPrefs prefs;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700423 DownloadAction download_action(&prefs, nullptr,
424 new MockHttpFetcher("x", 1, nullptr));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700425 download_action.SetTestFileWriter(&writer);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700426
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000427 BondActions(&feeder_action, &download_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000428
429 ActionProcessor processor;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000430 processor.EnqueueAction(&feeder_action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000431 processor.EnqueueAction(&download_action);
432 processor.StartProcessing();
433 ASSERT_FALSE(processor.IsRunning());
434
435 g_main_loop_unref(loop);
436}
437
David Zeuthen8f191b22013-08-06 12:27:50 -0700438gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
439 ActionProcessor* processor = reinterpret_cast<ActionProcessor*>(user_data);
440 processor->StartProcessing();
441 return FALSE;
442}
443
444// Test fixture for P2P tests.
445class P2PDownloadActionTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700446 protected:
David Zeuthen8f191b22013-08-06 12:27:50 -0700447 P2PDownloadActionTest()
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700448 : loop_(nullptr),
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700449 start_at_offset_(0),
450 fake_um_(fake_system_state_.fake_clock()) {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700451
Alex Deymo610277e2014-11-11 21:18:11 -0800452 ~P2PDownloadActionTest() override {}
David Zeuthen8f191b22013-08-06 12:27:50 -0700453
454 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800455 void SetUp() override {
David Zeuthen8f191b22013-08-06 12:27:50 -0700456 loop_ = g_main_loop_new(g_main_context_default(), FALSE);
457 }
458
459 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -0800460 void TearDown() override {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700461 if (loop_ != nullptr)
David Zeuthen8f191b22013-08-06 12:27:50 -0700462 g_main_loop_unref(loop_);
463 }
464
465 // To be called by tests to setup the download. The
466 // |starting_offset| parameter is for where to resume.
467 void SetupDownload(off_t starting_offset) {
468 start_at_offset_ = starting_offset;
469 // Prepare data 10 kB of data.
470 data_.clear();
471 for (unsigned int i = 0; i < 10 * 1000; i++)
472 data_ += 'a' + (i % 25);
473
474 // Setup p2p.
475 FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration();
David Zeuthen41f2cf52014-11-05 12:29:45 -0500476 p2p_manager_.reset(P2PManager::Construct(
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700477 test_conf, nullptr, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500478 base::TimeDelta::FromDays(5)));
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700479 fake_system_state_.set_p2p_manager(p2p_manager_.get());
David Zeuthen8f191b22013-08-06 12:27:50 -0700480 }
481
482 // To be called by tests to perform the download. The
483 // |use_p2p_to_share| parameter is used to indicate whether the
484 // payload should be shared via p2p.
485 void StartDownload(bool use_p2p_to_share) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700486 EXPECT_CALL(*fake_system_state_.mock_payload_state(),
487 GetUsingP2PForSharing())
488 .WillRepeatedly(Return(use_p2p_to_share));
David Zeuthen8f191b22013-08-06 12:27:50 -0700489
490 ScopedTempFile output_temp_file;
491 TestDirectFileWriter writer;
492 InstallPlan install_plan(false,
493 false,
494 "",
495 data_.length(),
496 "1234hash",
497 0,
498 "",
499 output_temp_file.GetPath(),
David Zeuthene7f89172013-10-31 10:21:04 -0700500 "",
Allie Woodfdf00512015-03-02 13:34:55 -0800501 "",
502 "",
David Zeuthen8f191b22013-08-06 12:27:50 -0700503 "");
504 ObjectFeederAction<InstallPlan> feeder_action;
505 feeder_action.set_obj(install_plan);
Alex Deymo8427b4a2014-11-05 14:00:32 -0800506 MockPrefs prefs;
David Zeuthen8f191b22013-08-06 12:27:50 -0700507 http_fetcher_ = new MockHttpFetcher(data_.c_str(),
508 data_.length(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700509 nullptr);
David Zeuthen8f191b22013-08-06 12:27:50 -0700510 // Note that DownloadAction takes ownership of the passed in HttpFetcher.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700511 download_action_.reset(new DownloadAction(&prefs, &fake_system_state_,
David Zeuthen8f191b22013-08-06 12:27:50 -0700512 http_fetcher_));
513 download_action_->SetTestFileWriter(&writer);
514 BondActions(&feeder_action, download_action_.get());
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700515 DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess);
David Zeuthen8f191b22013-08-06 12:27:50 -0700516 delegate.loop_ = loop_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800517 delegate.expected_data_ = chromeos::Blob(data_.begin() + start_at_offset_,
518 data_.end());
David Zeuthen8f191b22013-08-06 12:27:50 -0700519 delegate.path_ = output_temp_file.GetPath();
520 processor_.set_delegate(&delegate);
521 processor_.EnqueueAction(&feeder_action);
522 processor_.EnqueueAction(download_action_.get());
523
524 g_timeout_add(0, &StartProcessorInRunLoopForP2P, this);
525 g_main_loop_run(loop_);
526 }
527
528 // The DownloadAction instance under test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700529 unique_ptr<DownloadAction> download_action_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700530
531 // The HttpFetcher used in the test.
532 MockHttpFetcher* http_fetcher_;
533
534 // The P2PManager used in the test.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700535 unique_ptr<P2PManager> p2p_manager_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700536
537 // The ActionProcessor used for running the actions.
538 ActionProcessor processor_;
539
540 // A fake system state.
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700541 FakeSystemState fake_system_state_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700542
543 // The data being downloaded.
544 string data_;
545
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700546 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700547 // Callback used in StartDownload() method.
548 static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) {
549 class P2PDownloadActionTest *test =
550 reinterpret_cast<P2PDownloadActionTest*>(user_data);
551 test->processor_.StartProcessing();
552 test->http_fetcher_->SetOffset(test->start_at_offset_);
553 return FALSE;
554 }
555
556 // Mainloop used to make StartDownload() synchronous.
557 GMainLoop *loop_;
558
559 // The requested starting offset passed to SetupDownload().
560 off_t start_at_offset_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700561
562 chromeos_update_manager::FakeUpdateManager fake_um_;
David Zeuthen8f191b22013-08-06 12:27:50 -0700563};
564
565TEST_F(P2PDownloadActionTest, IsWrittenTo) {
Alex Deymo10875d92014-11-10 21:52:57 -0800566 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700567 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
568 << "Please update your system to support this feature.";
569 return;
570 }
571
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700572 SetupDownload(0); // starting_offset
573 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700574
575 // Check the p2p file and its content matches what was sent.
576 string file_id = download_action_->p2p_file_id();
Gilad Arnold74b5f552014-10-07 08:17:16 -0700577 EXPECT_NE("", file_id);
578 EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id));
579 EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id));
David Zeuthen8f191b22013-08-06 12:27:50 -0700580 string p2p_file_contents;
581 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
582 &p2p_file_contents));
583 EXPECT_EQ(data_, p2p_file_contents);
584}
585
586TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
Alex Deymo10875d92014-11-10 21:52:57 -0800587 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700588 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
589 << "Please update your system to support this feature.";
590 return;
591 }
592
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700593 SetupDownload(1000); // starting_offset
594 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700595
596 // DownloadAction should convey that the file is not being shared.
597 // and that we don't have any p2p files.
598 EXPECT_EQ(download_action_->p2p_file_id(), "");
599 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
600}
601
602TEST_F(P2PDownloadActionTest, CanAppend) {
Alex Deymo10875d92014-11-10 21:52:57 -0800603 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700604 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
605 << "Please update your system to support this feature.";
606 return;
607 }
608
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700609 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700610
611 // Prepare the file with existing data before starting to write to
612 // it via DownloadAction.
613 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
614 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
615 string existing_data;
616 for (unsigned int i = 0; i < 1000; i++)
617 existing_data += '0' + (i % 10);
618 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
619 1000), 1000);
620
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700621 StartDownload(true); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700622
623 // DownloadAction should convey the same file_id and the file should
624 // have the expected size.
625 EXPECT_EQ(download_action_->p2p_file_id(), file_id);
626 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length());
627 EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length());
628 string p2p_file_contents;
629 // Check that the first 1000 bytes wasn't touched and that we
630 // appended the remaining as appropriate.
631 EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id),
632 &p2p_file_contents));
633 EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
634 EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
635}
636
David Zeuthen8f191b22013-08-06 12:27:50 -0700637TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
Alex Deymo10875d92014-11-10 21:52:57 -0800638 if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700639 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
640 << "Please update your system to support this feature.";
641 return;
642 }
643
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700644 SetupDownload(1000); // starting_offset
David Zeuthen8f191b22013-08-06 12:27:50 -0700645
646 // Prepare the file with all existing data before starting to write
647 // to it via DownloadAction.
648 string file_id = utils::CalculateP2PFileId("1234hash", data_.length());
649 ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
650 string existing_data;
651 for (unsigned int i = 0; i < 1000; i++)
652 existing_data += '0' + (i % 10);
653 ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(),
654 1000), 1000);
655
656 // Check that the file is there.
657 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000);
658 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1);
659
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700660 StartDownload(false); // use_p2p_to_share
David Zeuthen8f191b22013-08-06 12:27:50 -0700661
662 // DownloadAction should have deleted the p2p file. Check that it's gone.
663 EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1);
664 EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
665}
666
rspangler@google.com49fdf182009-10-10 00:57:34 +0000667} // namespace chromeos_update_engine