Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 5 | #include "update_engine/download_action.h" |
| 6 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | #include <glib.h> |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 8 | #include <gmock/gmock.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 10 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 11 | #include <memory> |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 16 | #include <base/files/file_path.h> |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 17 | #include <base/files/file_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 18 | #include <base/strings/stringprintf.h> |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 19 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | #include "update_engine/action_pipe.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 21 | #include "update_engine/fake_p2p_manager_configuration.h" |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 22 | #include "update_engine/fake_system_state.h" |
| 23 | #include "update_engine/mock_http_fetcher.h" |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 24 | #include "update_engine/mock_prefs.h" |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 25 | #include "update_engine/omaha_hash_calculator.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 | #include "update_engine/test_utils.h" |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 27 | #include "update_engine/update_manager/fake_update_manager.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 28 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 29 | |
| 30 | namespace chromeos_update_engine { |
| 31 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 32 | using base::FilePath; |
| 33 | using base::ReadFileToString; |
| 34 | using base::WriteFile; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 36 | using std::unique_ptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | using std::vector; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 38 | using testing::AtLeast; |
| 39 | using testing::InSequence; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 40 | using testing::Return; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 41 | using testing::_; |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 42 | using test_utils::ScopedTempFile; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 43 | |
| 44 | class DownloadActionTest : public ::testing::Test { }; |
| 45 | |
| 46 | namespace { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 47 | class 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 54 | public: |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 55 | explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 56 | : loop_(nullptr), |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 57 | processing_done_called_(false), |
| 58 | expected_code_(expected_code) {} |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 59 | ~DownloadActionTestProcessorDelegate() override { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 60 | EXPECT_TRUE(processing_done_called_); |
| 61 | } |
Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 62 | void ProcessingDone(const ActionProcessor* processor, |
| 63 | ErrorCode code) override { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | ASSERT_TRUE(loop_); |
| 65 | g_main_loop_quit(loop_); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 66 | chromeos::Blob found_data; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 67 | ASSERT_TRUE(utils::ReadFile(path_, &found_data)); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 68 | if (expected_code_ != ErrorCode::kDownloadWriteError) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 69 | 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | } |
| 74 | processing_done_called_ = true; |
| 75 | } |
| 76 | |
Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 77 | void ActionCompleted(ActionProcessor* processor, |
| 78 | AbstractAction* action, |
| 79 | ErrorCode code) override { |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 80 | const string type = action->Type(); |
| 81 | if (type == DownloadAction::StaticType()) { |
| 82 | EXPECT_EQ(expected_code_, code); |
| 83 | } else { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 84 | EXPECT_EQ(ErrorCode::kSuccess, code); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 85 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | GMainLoop *loop_; |
| 89 | string path_; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 90 | chromeos::Blob expected_data_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 91 | bool processing_done_called_; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 92 | ErrorCode expected_code_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 95 | class 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 Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 100 | virtual bool Write(const void* bytes, size_t count) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 101 | if (++current_write_ == fail_write_) { |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 102 | return false; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 103 | } |
| 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 Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 113 | struct StartProcessorInRunLoopArgs { |
| 114 | ActionProcessor* processor; |
| 115 | MockHttpFetcher* http_fetcher; |
| 116 | }; |
| 117 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 118 | gboolean StartProcessorInRunLoop(gpointer data) { |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 119 | ActionProcessor* processor = |
| 120 | reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 121 | processor->StartProcessing(); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 122 | MockHttpFetcher* http_fetcher = |
| 123 | reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher; |
| 124 | http_fetcher->SetOffset(1); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 125 | return FALSE; |
| 126 | } |
| 127 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 128 | void TestWithData(const chromeos::Blob& data, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 129 | int fail_write, |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 130 | bool use_download_delegate) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 131 | 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 Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 134 | ScopedTempFile output_temp_file; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 135 | TestDirectFileWriter writer; |
| 136 | writer.set_fail_write(fail_write); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 137 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 138 | // We pull off the first byte from data and seek past it. |
| 139 | |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 140 | string hash = |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 141 | OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1); |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 142 | uint64_t size = data.size(); |
| 143 | InstallPlan install_plan(false, |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 144 | false, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 145 | "", |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 146 | size, |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 147 | hash, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 148 | 0, |
| 149 | "", |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 150 | output_temp_file.GetPath(), |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 151 | "", |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 152 | ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 153 | ObjectFeederAction<InstallPlan> feeder_action; |
| 154 | feeder_action.set_obj(install_plan); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 155 | MockPrefs prefs; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 156 | MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 157 | data.size(), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 158 | nullptr); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 159 | // takes ownership of passed in HttpFetcher |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 160 | DownloadAction download_action(&prefs, nullptr, http_fetcher); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 161 | download_action.SetTestFileWriter(&writer); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 162 | BondActions(&feeder_action, &download_action); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 163 | 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 Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 168 | if (data.size() > kMockHttpFetcherChunkSize) |
| 169 | EXPECT_CALL(download_delegate, |
| 170 | BytesReceived(1 + kMockHttpFetcherChunkSize, _)); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 171 | EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 172 | EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); |
| 173 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 174 | ErrorCode expected_code = ErrorCode::kSuccess; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 175 | if (fail_write > 0) |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 176 | expected_code = ErrorCode::kDownloadWriteError; |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 177 | DownloadActionTestProcessorDelegate delegate(expected_code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 178 | delegate.loop_ = loop; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 179 | delegate.expected_data_ = chromeos::Blob(data.begin() + 1, data.end()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 180 | delegate.path_ = output_temp_file.GetPath(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 181 | ActionProcessor processor; |
| 182 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 183 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 184 | processor.EnqueueAction(&download_action); |
| 185 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 186 | StartProcessorInRunLoopArgs args; |
| 187 | args.processor = &processor; |
| 188 | args.http_fetcher = http_fetcher; |
| 189 | g_timeout_add(0, &StartProcessorInRunLoop, &args); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 190 | g_main_loop_run(loop); |
| 191 | g_main_loop_unref(loop); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 192 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 193 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 194 | |
| 195 | TEST(DownloadActionTest, SimpleTest) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 196 | chromeos::Blob small; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 197 | const char* foo = "foo"; |
| 198 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 199 | TestWithData(small, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 200 | 0, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 201 | true); // use_download_delegate |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST(DownloadActionTest, LargeTest) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 205 | chromeos::Blob big(5 * kMockHttpFetcherChunkSize); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 206 | char c = '0'; |
| 207 | for (unsigned int i = 0; i < big.size(); i++) { |
| 208 | big[i] = c; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 209 | c = ('9' == c) ? '0' : c + 1; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 210 | } |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 211 | TestWithData(big, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 212 | 0, // fail_write |
| 213 | true); // use_download_delegate |
| 214 | } |
| 215 | |
| 216 | TEST(DownloadActionTest, FailWriteTest) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 217 | chromeos::Blob big(5 * kMockHttpFetcherChunkSize); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 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 Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 224 | 2, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 225 | true); // use_download_delegate |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 228 | TEST(DownloadActionTest, NoDownloadDelegateTest) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 229 | chromeos::Blob small; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 230 | const char* foo = "foofoo"; |
| 231 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 232 | TestWithData(small, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 233 | 0, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 234 | false); // use_download_delegate |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | namespace { |
| 238 | class 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 | |
| 247 | gboolean 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 Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 255 | void TestTerminateEarly(bool use_download_delegate) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 256 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 257 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 258 | chromeos::Blob data(kMockHttpFetcherChunkSize + |
| 259 | kMockHttpFetcherChunkSize / 2); |
| 260 | memset(data.data(), 0, data.size()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 261 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 262 | ScopedTempFile temp_file; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 263 | { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 264 | DirectFileWriter writer; |
| 265 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 266 | // takes ownership of passed in HttpFetcher |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 267 | ObjectFeederAction<InstallPlan> feeder_action; |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 268 | InstallPlan install_plan(false, false, "", 0, "", 0, "", |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 269 | temp_file.GetPath(), "", ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 270 | feeder_action.set_obj(install_plan); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 271 | MockPrefs prefs; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 272 | DownloadAction download_action(&prefs, nullptr, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 273 | new MockHttpFetcher(data.data(), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 274 | data.size(), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 275 | nullptr)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 276 | download_action.SetTestFileWriter(&writer); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 277 | 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 284 | TerminateEarlyTestProcessorDelegate delegate; |
| 285 | delegate.loop_ = loop; |
| 286 | ActionProcessor processor; |
| 287 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 288 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 289 | processor.EnqueueAction(&download_action); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 290 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 291 | |
| 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 Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 298 | const off_t resulting_file_size(utils::FileSize(temp_file.GetPath())); |
| 299 | EXPECT_GE(resulting_file_size, 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 300 | if (resulting_file_size != 0) |
| 301 | EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size); |
| 302 | } |
| 303 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 304 | } // namespace |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 305 | |
| 306 | TEST(DownloadActionTest, TerminateEarlyTest) { |
| 307 | TestTerminateEarly(true); |
| 308 | } |
| 309 | |
| 310 | TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) { |
| 311 | TestTerminateEarly(false); |
| 312 | } |
| 313 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 314 | class DownloadActionTestAction; |
| 315 | |
| 316 | template<> |
| 317 | class ActionTraits<DownloadActionTestAction> { |
| 318 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 319 | typedef InstallPlan OutputObjectType; |
| 320 | typedef InstallPlan InputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | // This is a simple Action class for testing. |
Yunlian Jiang | 2dac576 | 2013-04-12 09:53:09 -0700 | [diff] [blame] | 324 | class DownloadActionTestAction : public Action<DownloadActionTestAction> { |
| 325 | public: |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 326 | DownloadActionTestAction() : did_run_(false) {} |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 327 | 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 331 | ActionProcessor* processor() { return processor_; } |
| 332 | void PerformAction() { |
| 333 | did_run_ = true; |
| 334 | ASSERT_TRUE(HasInputObject()); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 335 | EXPECT_TRUE(expected_input_object_ == GetInputObject()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 336 | ASSERT_TRUE(processor()); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 337 | processor()->ActionComplete(this, ErrorCode::kSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 338 | } |
| 339 | string Type() const { return "DownloadActionTestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 340 | InstallPlan expected_input_object_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 341 | bool did_run_; |
| 342 | }; |
| 343 | |
| 344 | namespace { |
| 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. |
| 348 | class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate { |
| 349 | public: |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 350 | void ProcessingDone(const ActionProcessor* processor, ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 351 | ASSERT_TRUE(loop_); |
| 352 | g_main_loop_quit(loop_); |
| 353 | } |
| 354 | GMainLoop *loop_; |
| 355 | }; |
| 356 | |
| 357 | gboolean PassObjectOutTestStarter(gpointer data) { |
| 358 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 359 | processor->StartProcessing(); |
| 360 | return FALSE; |
| 361 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 362 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 363 | |
| 364 | TEST(DownloadActionTest, PassObjectOutTest) { |
| 365 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 366 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 367 | DirectFileWriter writer; |
| 368 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 369 | // takes ownership of passed in HttpFetcher |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 370 | InstallPlan install_plan(false, |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 371 | false, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 372 | "", |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 373 | 1, |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 374 | OmahaHashCalculator::OmahaHashOfString("x"), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 375 | 0, |
| 376 | "", |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 377 | "/dev/null", |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 378 | "/dev/null", |
| 379 | ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 380 | ObjectFeederAction<InstallPlan> feeder_action; |
| 381 | feeder_action.set_obj(install_plan); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 382 | MockPrefs prefs; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 383 | DownloadAction download_action(&prefs, nullptr, |
| 384 | new MockHttpFetcher("x", 1, nullptr)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 385 | download_action.SetTestFileWriter(&writer); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 386 | |
| 387 | DownloadActionTestAction test_action; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 388 | test_action.expected_input_object_ = install_plan; |
| 389 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 390 | BondActions(&download_action, &test_action); |
| 391 | |
| 392 | ActionProcessor processor; |
| 393 | PassObjectOutTestProcessorDelegate delegate; |
| 394 | delegate.loop_ = loop; |
| 395 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 396 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 397 | 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 | |
| 407 | TEST(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 Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 411 | DirectFileWriter writer; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 412 | |
| 413 | // takes ownership of passed in HttpFetcher |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 414 | InstallPlan install_plan(false, false, "", 0, "", 0, "", path, "", ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 415 | ObjectFeederAction<InstallPlan> feeder_action; |
| 416 | feeder_action.set_obj(install_plan); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 417 | MockPrefs prefs; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 418 | DownloadAction download_action(&prefs, nullptr, |
| 419 | new MockHttpFetcher("x", 1, nullptr)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 420 | download_action.SetTestFileWriter(&writer); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 421 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 422 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 423 | |
| 424 | ActionProcessor processor; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 425 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 426 | processor.EnqueueAction(&download_action); |
| 427 | processor.StartProcessing(); |
| 428 | ASSERT_FALSE(processor.IsRunning()); |
| 429 | |
| 430 | g_main_loop_unref(loop); |
| 431 | } |
| 432 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 433 | gboolean 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. |
| 440 | class P2PDownloadActionTest : public testing::Test { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 441 | protected: |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 442 | P2PDownloadActionTest() |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 443 | : loop_(nullptr), |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 444 | start_at_offset_(0), |
| 445 | fake_um_(fake_system_state_.fake_clock()) {} |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 446 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 447 | ~P2PDownloadActionTest() override {} |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 448 | |
| 449 | // Derived from testing::Test. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 450 | void SetUp() override { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 451 | loop_ = g_main_loop_new(g_main_context_default(), FALSE); |
| 452 | } |
| 453 | |
| 454 | // Derived from testing::Test. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 455 | void TearDown() override { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 456 | if (loop_ != nullptr) |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 457 | g_main_loop_unref(loop_); |
| 458 | } |
| 459 | |
| 460 | // To be called by tests to setup the download. The |
| 461 | // |starting_offset| parameter is for where to resume. |
| 462 | void SetupDownload(off_t starting_offset) { |
| 463 | start_at_offset_ = starting_offset; |
| 464 | // Prepare data 10 kB of data. |
| 465 | data_.clear(); |
| 466 | for (unsigned int i = 0; i < 10 * 1000; i++) |
| 467 | data_ += 'a' + (i % 25); |
| 468 | |
| 469 | // Setup p2p. |
| 470 | FakeP2PManagerConfiguration *test_conf = new FakeP2PManagerConfiguration(); |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 471 | p2p_manager_.reset(P2PManager::Construct( |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 472 | test_conf, nullptr, &fake_um_, "cros_au", 3, |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 473 | base::TimeDelta::FromDays(5))); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 474 | fake_system_state_.set_p2p_manager(p2p_manager_.get()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // To be called by tests to perform the download. The |
| 478 | // |use_p2p_to_share| parameter is used to indicate whether the |
| 479 | // payload should be shared via p2p. |
| 480 | void StartDownload(bool use_p2p_to_share) { |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 481 | EXPECT_CALL(*fake_system_state_.mock_payload_state(), |
| 482 | GetUsingP2PForSharing()) |
| 483 | .WillRepeatedly(Return(use_p2p_to_share)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 484 | |
| 485 | ScopedTempFile output_temp_file; |
| 486 | TestDirectFileWriter writer; |
| 487 | InstallPlan install_plan(false, |
| 488 | false, |
| 489 | "", |
| 490 | data_.length(), |
| 491 | "1234hash", |
| 492 | 0, |
| 493 | "", |
| 494 | output_temp_file.GetPath(), |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 495 | "", |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 496 | ""); |
| 497 | ObjectFeederAction<InstallPlan> feeder_action; |
| 498 | feeder_action.set_obj(install_plan); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 499 | MockPrefs prefs; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 500 | http_fetcher_ = new MockHttpFetcher(data_.c_str(), |
| 501 | data_.length(), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 502 | nullptr); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 503 | // Note that DownloadAction takes ownership of the passed in HttpFetcher. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 504 | download_action_.reset(new DownloadAction(&prefs, &fake_system_state_, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 505 | http_fetcher_)); |
| 506 | download_action_->SetTestFileWriter(&writer); |
| 507 | BondActions(&feeder_action, download_action_.get()); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 508 | DownloadActionTestProcessorDelegate delegate(ErrorCode::kSuccess); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 509 | delegate.loop_ = loop_; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 510 | delegate.expected_data_ = chromeos::Blob(data_.begin() + start_at_offset_, |
| 511 | data_.end()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 512 | delegate.path_ = output_temp_file.GetPath(); |
| 513 | processor_.set_delegate(&delegate); |
| 514 | processor_.EnqueueAction(&feeder_action); |
| 515 | processor_.EnqueueAction(download_action_.get()); |
| 516 | |
| 517 | g_timeout_add(0, &StartProcessorInRunLoopForP2P, this); |
| 518 | g_main_loop_run(loop_); |
| 519 | } |
| 520 | |
| 521 | // The DownloadAction instance under test. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 522 | unique_ptr<DownloadAction> download_action_; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 523 | |
| 524 | // The HttpFetcher used in the test. |
| 525 | MockHttpFetcher* http_fetcher_; |
| 526 | |
| 527 | // The P2PManager used in the test. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 528 | unique_ptr<P2PManager> p2p_manager_; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 529 | |
| 530 | // The ActionProcessor used for running the actions. |
| 531 | ActionProcessor processor_; |
| 532 | |
| 533 | // A fake system state. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 534 | FakeSystemState fake_system_state_; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 535 | |
| 536 | // The data being downloaded. |
| 537 | string data_; |
| 538 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 539 | private: |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 540 | // Callback used in StartDownload() method. |
| 541 | static gboolean StartProcessorInRunLoopForP2P(gpointer user_data) { |
| 542 | class P2PDownloadActionTest *test = |
| 543 | reinterpret_cast<P2PDownloadActionTest*>(user_data); |
| 544 | test->processor_.StartProcessing(); |
| 545 | test->http_fetcher_->SetOffset(test->start_at_offset_); |
| 546 | return FALSE; |
| 547 | } |
| 548 | |
| 549 | // Mainloop used to make StartDownload() synchronous. |
| 550 | GMainLoop *loop_; |
| 551 | |
| 552 | // The requested starting offset passed to SetupDownload(). |
| 553 | off_t start_at_offset_; |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 554 | |
| 555 | chromeos_update_manager::FakeUpdateManager fake_um_; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 556 | }; |
| 557 | |
| 558 | TEST_F(P2PDownloadActionTest, IsWrittenTo) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 559 | if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 560 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 561 | << "Please update your system to support this feature."; |
| 562 | return; |
| 563 | } |
| 564 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 565 | SetupDownload(0); // starting_offset |
| 566 | StartDownload(true); // use_p2p_to_share |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 567 | |
| 568 | // Check the p2p file and its content matches what was sent. |
| 569 | string file_id = download_action_->p2p_file_id(); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 570 | EXPECT_NE("", file_id); |
| 571 | EXPECT_EQ(data_.length(), p2p_manager_->FileGetSize(file_id)); |
| 572 | EXPECT_EQ(data_.length(), p2p_manager_->FileGetExpectedSize(file_id)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 573 | string p2p_file_contents; |
| 574 | EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id), |
| 575 | &p2p_file_contents)); |
| 576 | EXPECT_EQ(data_, p2p_file_contents); |
| 577 | } |
| 578 | |
| 579 | TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 580 | if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 581 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 582 | << "Please update your system to support this feature."; |
| 583 | return; |
| 584 | } |
| 585 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 586 | SetupDownload(1000); // starting_offset |
| 587 | StartDownload(true); // use_p2p_to_share |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 588 | |
| 589 | // DownloadAction should convey that the file is not being shared. |
| 590 | // and that we don't have any p2p files. |
| 591 | EXPECT_EQ(download_action_->p2p_file_id(), ""); |
| 592 | EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0); |
| 593 | } |
| 594 | |
| 595 | TEST_F(P2PDownloadActionTest, CanAppend) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 596 | if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 597 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 598 | << "Please update your system to support this feature."; |
| 599 | return; |
| 600 | } |
| 601 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 602 | SetupDownload(1000); // starting_offset |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 603 | |
| 604 | // Prepare the file with existing data before starting to write to |
| 605 | // it via DownloadAction. |
| 606 | string file_id = utils::CalculateP2PFileId("1234hash", data_.length()); |
| 607 | ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length())); |
| 608 | string existing_data; |
| 609 | for (unsigned int i = 0; i < 1000; i++) |
| 610 | existing_data += '0' + (i % 10); |
| 611 | ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(), |
| 612 | 1000), 1000); |
| 613 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 614 | StartDownload(true); // use_p2p_to_share |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 615 | |
| 616 | // DownloadAction should convey the same file_id and the file should |
| 617 | // have the expected size. |
| 618 | EXPECT_EQ(download_action_->p2p_file_id(), file_id); |
| 619 | EXPECT_EQ(p2p_manager_->FileGetSize(file_id), data_.length()); |
| 620 | EXPECT_EQ(p2p_manager_->FileGetExpectedSize(file_id), data_.length()); |
| 621 | string p2p_file_contents; |
| 622 | // Check that the first 1000 bytes wasn't touched and that we |
| 623 | // appended the remaining as appropriate. |
| 624 | EXPECT_TRUE(ReadFileToString(p2p_manager_->FileGetPath(file_id), |
| 625 | &p2p_file_contents)); |
| 626 | EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000)); |
| 627 | EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000)); |
| 628 | } |
| 629 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 630 | TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 631 | if (!test_utils::IsXAttrSupported(FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 632 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 633 | << "Please update your system to support this feature."; |
| 634 | return; |
| 635 | } |
| 636 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 637 | SetupDownload(1000); // starting_offset |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 638 | |
| 639 | // Prepare the file with all existing data before starting to write |
| 640 | // to it via DownloadAction. |
| 641 | string file_id = utils::CalculateP2PFileId("1234hash", data_.length()); |
| 642 | ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length())); |
| 643 | string existing_data; |
| 644 | for (unsigned int i = 0; i < 1000; i++) |
| 645 | existing_data += '0' + (i % 10); |
| 646 | ASSERT_EQ(WriteFile(p2p_manager_->FileGetPath(file_id), existing_data.c_str(), |
| 647 | 1000), 1000); |
| 648 | |
| 649 | // Check that the file is there. |
| 650 | EXPECT_EQ(p2p_manager_->FileGetSize(file_id), 1000); |
| 651 | EXPECT_EQ(p2p_manager_->CountSharedFiles(), 1); |
| 652 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 653 | StartDownload(false); // use_p2p_to_share |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 654 | |
| 655 | // DownloadAction should have deleted the p2p file. Check that it's gone. |
| 656 | EXPECT_EQ(p2p_manager_->FileGetSize(file_id), -1); |
| 657 | EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0); |
| 658 | } |
| 659 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 660 | } // namespace chromeos_update_engine |