Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2011 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/download_action.h" |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 18 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 19 | #include <gmock/gmock.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 21 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 22 | #include <memory> |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <utility> |
Sen Jiang | ba2213a | 2018-02-27 16:28:20 -0800 | [diff] [blame] | 25 | #include <vector> |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 26 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 27 | #include <base/bind.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 28 | #include <base/files/file_path.h> |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 29 | #include <base/files/file_util.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 30 | #include <base/location.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 31 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 32 | #include <brillo/message_loops/fake_message_loop.h> |
| 33 | #include <brillo/message_loops/message_loop.h> |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 34 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 35 | #include "update_engine/common/action_pipe.h" |
| 36 | #include "update_engine/common/hash_calculator.h" |
| 37 | #include "update_engine/common/mock_http_fetcher.h" |
| 38 | #include "update_engine/common/mock_prefs.h" |
| 39 | #include "update_engine/common/test_utils.h" |
| 40 | #include "update_engine/common/utils.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 41 | #include "update_engine/fake_p2p_manager_configuration.h" |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 42 | #include "update_engine/fake_system_state.h" |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 43 | #include "update_engine/mock_file_writer.h" |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 44 | #include "update_engine/payload_consumer/mock_download_action.h" |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 45 | #include "update_engine/update_manager/fake_update_manager.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 46 | |
| 47 | namespace chromeos_update_engine { |
| 48 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 49 | using base::FilePath; |
| 50 | using base::ReadFileToString; |
| 51 | using base::WriteFile; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 53 | using std::unique_ptr; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 54 | using test_utils::ScopedTempFile; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 55 | using testing::_; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 56 | using testing::AtLeast; |
| 57 | using testing::InSequence; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 58 | using testing::Return; |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 59 | using testing::SetArgPointee; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 60 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 61 | class DownloadActionTest : public ::testing::Test {}; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 62 | |
| 63 | namespace { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 64 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 65 | class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 66 | public: |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 67 | DownloadActionTestProcessorDelegate() |
| 68 | : processing_done_called_(false), expected_code_(ErrorCode::kSuccess) {} |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 69 | ~DownloadActionTestProcessorDelegate() override { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | EXPECT_TRUE(processing_done_called_); |
| 71 | } |
Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 72 | void ProcessingDone(const ActionProcessor* processor, |
| 73 | ErrorCode code) override { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 74 | brillo::MessageLoop::current()->BreakLoop(); |
| 75 | brillo::Blob found_data; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 76 | ASSERT_TRUE(utils::ReadFile(path_, &found_data)); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 77 | if (expected_code_ != ErrorCode::kDownloadWriteError) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 78 | ASSERT_EQ(expected_data_.size(), found_data.size()); |
| 79 | for (unsigned i = 0; i < expected_data_.size(); i++) { |
| 80 | EXPECT_EQ(expected_data_[i], found_data[i]); |
| 81 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 82 | } |
| 83 | processing_done_called_ = true; |
| 84 | } |
| 85 | |
Yunlian Jiang | 35866ed | 2015-01-29 13:09:20 -0800 | [diff] [blame] | 86 | void ActionCompleted(ActionProcessor* processor, |
| 87 | AbstractAction* action, |
| 88 | ErrorCode code) override { |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 89 | const string type = action->Type(); |
| 90 | if (type == DownloadAction::StaticType()) { |
| 91 | EXPECT_EQ(expected_code_, code); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 92 | p2p_file_id_ = static_cast<DownloadAction*>(action)->p2p_file_id(); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 93 | } else { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 94 | EXPECT_EQ(ErrorCode::kSuccess, code); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 95 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 96 | } |
| 97 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 98 | string path_; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 99 | brillo::Blob expected_data_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 100 | bool processing_done_called_; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 101 | ErrorCode expected_code_; |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 102 | string p2p_file_id_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 105 | class TestDirectFileWriter : public DirectFileWriter { |
| 106 | public: |
| 107 | TestDirectFileWriter() : fail_write_(0), current_write_(0) {} |
| 108 | void set_fail_write(int fail_write) { fail_write_ = fail_write; } |
| 109 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 110 | virtual bool Write(const void* bytes, size_t count) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 111 | if (++current_write_ == fail_write_) { |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 112 | return false; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 113 | } |
| 114 | return DirectFileWriter::Write(bytes, count); |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | // If positive, fail on the |fail_write_| call to Write. |
| 119 | int fail_write_; |
| 120 | int current_write_; |
| 121 | }; |
| 122 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 123 | void StartProcessorInRunLoop(ActionProcessor* processor, |
| 124 | MockHttpFetcher* http_fetcher) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 125 | processor->StartProcessing(); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 126 | http_fetcher->SetOffset(1); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 129 | void TestWithData(const brillo::Blob& data, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 130 | int fail_write, |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 131 | bool use_download_delegate) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 132 | brillo::FakeMessageLoop loop(nullptr); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 133 | loop.SetAsCurrent(); |
Alex Deymo | 5ed695e | 2015-10-05 16:59:23 -0700 | [diff] [blame] | 134 | FakeSystemState fake_system_state; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 135 | |
| 136 | // 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] | 137 | ScopedTempFile output_temp_file; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 138 | TestDirectFileWriter writer; |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 139 | EXPECT_EQ( |
| 140 | 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0)); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 141 | writer.set_fail_write(fail_write); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 142 | |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 143 | uint64_t size = data.size() - 1; |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 144 | InstallPlan install_plan; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 145 | install_plan.payloads.push_back( |
| 146 | {.size = size, .type = InstallPayloadType::kDelta}); |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 147 | // We pull off the first byte from data and seek past it. |
| 148 | EXPECT_TRUE(HashCalculator::RawHashOfBytes( |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 149 | &data[1], data.size() - 1, &install_plan.payloads[0].hash)); |
Alex Deymo | 5ed695e | 2015-10-05 16:59:23 -0700 | [diff] [blame] | 150 | install_plan.source_slot = 0; |
| 151 | install_plan.target_slot = 1; |
| 152 | // We mark both slots as bootable. Only the target slot should be unbootable |
| 153 | // after the download starts. |
| 154 | fake_system_state.fake_boot_control()->SetSlotBootable( |
| 155 | install_plan.source_slot, true); |
| 156 | fake_system_state.fake_boot_control()->SetSlotBootable( |
| 157 | install_plan.target_slot, true); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 158 | auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>(); |
| 159 | feeder_action->set_obj(install_plan); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 160 | MockPrefs prefs; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 161 | MockHttpFetcher* http_fetcher = |
| 162 | new MockHttpFetcher(data.data(), data.size(), nullptr); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 163 | // takes ownership of passed in HttpFetcher |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 164 | auto download_action = |
| 165 | std::make_unique<DownloadAction>(&prefs, |
| 166 | fake_system_state.boot_control(), |
| 167 | fake_system_state.hardware(), |
| 168 | &fake_system_state, |
| 169 | http_fetcher, |
| 170 | false /* interactive */); |
| 171 | download_action->SetTestFileWriter(&writer); |
| 172 | BondActions(feeder_action.get(), download_action.get()); |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 173 | MockDownloadActionDelegate download_delegate; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 174 | if (use_download_delegate) { |
| 175 | InSequence s; |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 176 | download_action->set_delegate(&download_delegate); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 177 | if (data.size() > kMockHttpFetcherChunkSize) |
| 178 | EXPECT_CALL(download_delegate, |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 179 | BytesReceived(_, kMockHttpFetcherChunkSize, _)); |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 180 | EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(AtLeast(1)); |
Sen Jiang | 7ebfccf | 2018-03-15 13:55:55 -0700 | [diff] [blame] | 181 | EXPECT_CALL(download_delegate, DownloadComplete()) |
| 182 | .Times(fail_write == 0 ? 1 : 0); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 183 | } |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 184 | DownloadActionTestProcessorDelegate delegate; |
| 185 | delegate.expected_code_ = |
| 186 | (fail_write > 0) ? ErrorCode::kDownloadWriteError : ErrorCode::kSuccess; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 187 | delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end()); |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 188 | delegate.path_ = output_temp_file.path(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 189 | ActionProcessor processor; |
| 190 | processor.set_delegate(&delegate); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 191 | processor.EnqueueAction(std::move(feeder_action)); |
| 192 | processor.EnqueueAction(std::move(download_action)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 193 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 194 | loop.PostTask(FROM_HERE, |
| 195 | base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher)); |
| 196 | loop.Run(); |
| 197 | EXPECT_FALSE(loop.PendingTasks()); |
Alex Deymo | 5ed695e | 2015-10-05 16:59:23 -0700 | [diff] [blame] | 198 | |
| 199 | EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable( |
| 200 | install_plan.source_slot)); |
| 201 | EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable( |
| 202 | install_plan.target_slot)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 203 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 204 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 205 | |
| 206 | TEST(DownloadActionTest, SimpleTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 207 | brillo::Blob small; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 208 | const char* foo = "foo"; |
| 209 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 210 | TestWithData(small, |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 211 | 0, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 212 | true); // use_download_delegate |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | TEST(DownloadActionTest, LargeTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 216 | brillo::Blob big(5 * kMockHttpFetcherChunkSize); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 217 | char c = '0'; |
| 218 | for (unsigned int i = 0; i < big.size(); i++) { |
| 219 | big[i] = c; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 220 | c = ('9' == c) ? '0' : c + 1; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 221 | } |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 222 | TestWithData(big, |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 223 | 0, // fail_write |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 224 | true); // use_download_delegate |
| 225 | } |
| 226 | |
| 227 | TEST(DownloadActionTest, FailWriteTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 228 | brillo::Blob big(5 * kMockHttpFetcherChunkSize); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 229 | char c = '0'; |
| 230 | for (unsigned int i = 0; i < big.size(); i++) { |
| 231 | big[i] = c; |
| 232 | c = ('9' == c) ? '0' : c + 1; |
| 233 | } |
| 234 | TestWithData(big, |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 235 | 2, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 236 | true); // use_download_delegate |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 239 | TEST(DownloadActionTest, NoDownloadDelegateTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 240 | brillo::Blob small; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 241 | const char* foo = "foofoo"; |
| 242 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 243 | TestWithData(small, |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 244 | 0, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 245 | false); // use_download_delegate |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 248 | TEST(DownloadActionTest, MultiPayloadProgressTest) { |
| 249 | std::vector<brillo::Blob> payload_datas; |
| 250 | // the first payload must be the largest, as it's the actual payload used by |
| 251 | // the MockHttpFetcher for all downloaded data. |
| 252 | payload_datas.emplace_back(4 * kMockHttpFetcherChunkSize + 256); |
| 253 | payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize); |
| 254 | brillo::FakeMessageLoop loop(nullptr); |
| 255 | loop.SetAsCurrent(); |
| 256 | FakeSystemState fake_system_state; |
| 257 | EXPECT_CALL(*fake_system_state.mock_payload_state(), NextPayload()) |
| 258 | .WillOnce(Return(true)); |
| 259 | |
| 260 | MockFileWriter mock_file_writer; |
| 261 | EXPECT_CALL(mock_file_writer, Close()).WillRepeatedly(Return(0)); |
| 262 | EXPECT_CALL(mock_file_writer, Write(_, _, _)) |
| 263 | .WillRepeatedly( |
| 264 | DoAll(SetArgPointee<2>(ErrorCode::kSuccess), Return(true))); |
| 265 | |
| 266 | InstallPlan install_plan; |
| 267 | uint64_t total_expected_download_size{0}; |
| 268 | for (const auto& data : payload_datas) { |
| 269 | uint64_t size = data.size(); |
| 270 | install_plan.payloads.push_back( |
| 271 | {.size = size, .type = InstallPayloadType::kFull}); |
| 272 | total_expected_download_size += size; |
| 273 | } |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 274 | auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>(); |
| 275 | feeder_action->set_obj(install_plan); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 276 | MockPrefs prefs; |
| 277 | MockHttpFetcher* http_fetcher = new MockHttpFetcher( |
| 278 | payload_datas[0].data(), payload_datas[0].size(), nullptr); |
| 279 | // takes ownership of passed in HttpFetcher |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 280 | auto download_action = |
| 281 | std::make_unique<DownloadAction>(&prefs, |
| 282 | fake_system_state.boot_control(), |
| 283 | fake_system_state.hardware(), |
| 284 | &fake_system_state, |
| 285 | http_fetcher, |
| 286 | false /* interactive */); |
| 287 | download_action->SetTestFileWriter(&mock_file_writer); |
| 288 | BondActions(feeder_action.get(), download_action.get()); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 289 | MockDownloadActionDelegate download_delegate; |
| 290 | { |
| 291 | InSequence s; |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 292 | download_action->set_delegate(&download_delegate); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 293 | // these are hand-computed based on the payloads specified above |
| 294 | EXPECT_CALL(download_delegate, |
| 295 | BytesReceived(kMockHttpFetcherChunkSize, |
| 296 | kMockHttpFetcherChunkSize, |
| 297 | total_expected_download_size)); |
| 298 | EXPECT_CALL(download_delegate, |
| 299 | BytesReceived(kMockHttpFetcherChunkSize, |
| 300 | kMockHttpFetcherChunkSize * 2, |
| 301 | total_expected_download_size)); |
| 302 | EXPECT_CALL(download_delegate, |
| 303 | BytesReceived(kMockHttpFetcherChunkSize, |
| 304 | kMockHttpFetcherChunkSize * 3, |
| 305 | total_expected_download_size)); |
| 306 | EXPECT_CALL(download_delegate, |
| 307 | BytesReceived(kMockHttpFetcherChunkSize, |
| 308 | kMockHttpFetcherChunkSize * 4, |
| 309 | total_expected_download_size)); |
| 310 | EXPECT_CALL(download_delegate, |
| 311 | BytesReceived(256, |
| 312 | kMockHttpFetcherChunkSize * 4 + 256, |
| 313 | total_expected_download_size)); |
| 314 | EXPECT_CALL(download_delegate, |
| 315 | BytesReceived(kMockHttpFetcherChunkSize, |
| 316 | kMockHttpFetcherChunkSize * 5 + 256, |
| 317 | total_expected_download_size)); |
| 318 | EXPECT_CALL(download_delegate, |
| 319 | BytesReceived(kMockHttpFetcherChunkSize, |
| 320 | total_expected_download_size, |
| 321 | total_expected_download_size)); |
| 322 | } |
| 323 | ActionProcessor processor; |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 324 | processor.EnqueueAction(std::move(feeder_action)); |
| 325 | processor.EnqueueAction(std::move(download_action)); |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 326 | |
| 327 | loop.PostTask( |
| 328 | FROM_HERE, |
| 329 | base::Bind( |
| 330 | [](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 331 | base::Unretained(&processor))); |
| 332 | loop.Run(); |
| 333 | EXPECT_FALSE(loop.PendingTasks()); |
| 334 | } |
| 335 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 336 | namespace { |
| 337 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 338 | public: |
| 339 | void ProcessingStopped(const ActionProcessor* processor) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 340 | brillo::MessageLoop::current()->BreakLoop(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 341 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 344 | void TerminateEarlyTestStarter(ActionProcessor* processor) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 345 | processor->StartProcessing(); |
| 346 | CHECK(processor->IsRunning()); |
| 347 | processor->StopProcessing(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 350 | void TestTerminateEarly(bool use_download_delegate) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 351 | brillo::FakeMessageLoop loop(nullptr); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 352 | loop.SetAsCurrent(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 353 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 354 | brillo::Blob data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 355 | memset(data.data(), 0, data.size()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 356 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 357 | ScopedTempFile temp_file; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 358 | { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 359 | DirectFileWriter writer; |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 360 | EXPECT_EQ(0, writer.Open(temp_file.path().c_str(), O_WRONLY | O_CREAT, 0)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 361 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 362 | // takes ownership of passed in HttpFetcher |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 363 | auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>(); |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 364 | InstallPlan install_plan; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 365 | install_plan.payloads.resize(1); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 366 | feeder_action->set_obj(install_plan); |
Alex Deymo | 5ed695e | 2015-10-05 16:59:23 -0700 | [diff] [blame] | 367 | FakeSystemState fake_system_state_; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 368 | MockPrefs prefs; |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 369 | auto download_action = std::make_unique<DownloadAction>( |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 370 | &prefs, |
| 371 | fake_system_state_.boot_control(), |
| 372 | fake_system_state_.hardware(), |
| 373 | &fake_system_state_, |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 374 | new MockHttpFetcher(data.data(), data.size(), nullptr), |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 375 | false /* interactive */); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 376 | download_action->SetTestFileWriter(&writer); |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 377 | MockDownloadActionDelegate download_delegate; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 378 | if (use_download_delegate) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 379 | download_action->set_delegate(&download_delegate); |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 380 | EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(0); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 381 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 382 | TerminateEarlyTestProcessorDelegate delegate; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 383 | ActionProcessor processor; |
| 384 | processor.set_delegate(&delegate); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 385 | BondActions(feeder_action.get(), download_action.get()); |
| 386 | processor.EnqueueAction(std::move(feeder_action)); |
| 387 | processor.EnqueueAction(std::move(download_action)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 388 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 389 | loop.PostTask(FROM_HERE, |
| 390 | base::Bind(&TerminateEarlyTestStarter, &processor)); |
| 391 | loop.Run(); |
| 392 | EXPECT_FALSE(loop.PendingTasks()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // 1 or 0 chunks should have come through |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 396 | const off_t resulting_file_size(utils::FileSize(temp_file.path())); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 397 | EXPECT_GE(resulting_file_size, 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 398 | if (resulting_file_size != 0) |
Alex Deymo | 5fe0c4e | 2016-02-16 18:46:24 -0800 | [diff] [blame] | 399 | EXPECT_EQ(kMockHttpFetcherChunkSize, |
| 400 | static_cast<size_t>(resulting_file_size)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 403 | } // namespace |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 404 | |
| 405 | TEST(DownloadActionTest, TerminateEarlyTest) { |
| 406 | TestTerminateEarly(true); |
| 407 | } |
| 408 | |
| 409 | TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) { |
| 410 | TestTerminateEarly(false); |
| 411 | } |
| 412 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 413 | class DownloadActionTestAction; |
| 414 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 415 | template <> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 416 | class ActionTraits<DownloadActionTestAction> { |
| 417 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 418 | typedef InstallPlan OutputObjectType; |
| 419 | typedef InstallPlan InputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | // This is a simple Action class for testing. |
Yunlian Jiang | 2dac576 | 2013-04-12 09:53:09 -0700 | [diff] [blame] | 423 | class DownloadActionTestAction : public Action<DownloadActionTestAction> { |
| 424 | public: |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 425 | DownloadActionTestAction() = default; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 426 | typedef InstallPlan InputObjectType; |
| 427 | typedef InstallPlan OutputObjectType; |
| 428 | ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); } |
| 429 | ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 430 | ActionProcessor* processor() { return processor_; } |
| 431 | void PerformAction() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 432 | ASSERT_TRUE(HasInputObject()); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 433 | EXPECT_TRUE(expected_input_object_ == GetInputObject()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 434 | ASSERT_TRUE(processor()); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 435 | processor()->ActionComplete(this, ErrorCode::kSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 436 | } |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 437 | static std::string StaticType() { return "DownloadActionTestAction"; } |
| 438 | string Type() const { return StaticType(); } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 439 | InstallPlan expected_input_object_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
| 442 | namespace { |
| 443 | // This class is an ActionProcessorDelegate that simply terminates the |
| 444 | // run loop when the ActionProcessor has completed processing. It's used |
| 445 | // only by the test PassObjectOutTest. |
| 446 | class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate { |
| 447 | public: |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 448 | void ProcessingDone(const ActionProcessor* processor, |
| 449 | ErrorCode code) override { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 450 | brillo::MessageLoop::current()->BreakLoop(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 451 | } |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 452 | void ActionCompleted(ActionProcessor* processor, |
| 453 | AbstractAction* action, |
| 454 | ErrorCode code) override { |
| 455 | if (action->Type() == DownloadActionTestAction::StaticType()) { |
| 456 | did_test_action_run_ = true; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | bool did_test_action_run_ = false; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 461 | }; |
| 462 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 463 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 464 | |
| 465 | TEST(DownloadActionTest, PassObjectOutTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 466 | brillo::FakeMessageLoop loop(nullptr); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 467 | loop.SetAsCurrent(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 468 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 469 | DirectFileWriter writer; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 470 | EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 471 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 472 | // takes ownership of passed in HttpFetcher |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 473 | InstallPlan install_plan; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 474 | install_plan.payloads.push_back({.size = 1}); |
| 475 | EXPECT_TRUE( |
| 476 | HashCalculator::RawHashOfData({'x'}, &install_plan.payloads[0].hash)); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 477 | auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>(); |
| 478 | feeder_action->set_obj(install_plan); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 479 | MockPrefs prefs; |
Alex Deymo | 5ed695e | 2015-10-05 16:59:23 -0700 | [diff] [blame] | 480 | FakeSystemState fake_system_state_; |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 481 | auto download_action = |
| 482 | std::make_unique<DownloadAction>(&prefs, |
| 483 | fake_system_state_.boot_control(), |
| 484 | fake_system_state_.hardware(), |
| 485 | &fake_system_state_, |
| 486 | new MockHttpFetcher("x", 1, nullptr), |
| 487 | false /* interactive */); |
| 488 | download_action->SetTestFileWriter(&writer); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 489 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 490 | auto test_action = std::make_unique<DownloadActionTestAction>(); |
| 491 | test_action->expected_input_object_ = install_plan; |
| 492 | BondActions(feeder_action.get(), download_action.get()); |
| 493 | BondActions(download_action.get(), test_action.get()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 494 | |
| 495 | ActionProcessor processor; |
| 496 | PassObjectOutTestProcessorDelegate delegate; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 497 | processor.set_delegate(&delegate); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 498 | processor.EnqueueAction(std::move(feeder_action)); |
| 499 | processor.EnqueueAction(std::move(download_action)); |
| 500 | processor.EnqueueAction(std::move(test_action)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 501 | |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 502 | loop.PostTask( |
| 503 | FROM_HERE, |
| 504 | base::Bind( |
| 505 | [](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 506 | base::Unretained(&processor))); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 507 | loop.Run(); |
| 508 | EXPECT_FALSE(loop.PendingTasks()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 509 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 510 | EXPECT_EQ(true, delegate.did_test_action_run_); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 511 | } |
| 512 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 513 | // Test fixture for P2P tests. |
| 514 | class P2PDownloadActionTest : public testing::Test { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 515 | protected: |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 516 | P2PDownloadActionTest() |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 517 | : start_at_offset_(0), fake_um_(fake_system_state_.fake_clock()) {} |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 518 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 519 | ~P2PDownloadActionTest() override {} |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 520 | |
| 521 | // Derived from testing::Test. |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 522 | void SetUp() override { loop_.SetAsCurrent(); } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 523 | |
| 524 | // Derived from testing::Test. |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 525 | void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 526 | |
| 527 | // To be called by tests to setup the download. The |
| 528 | // |starting_offset| parameter is for where to resume. |
| 529 | void SetupDownload(off_t starting_offset) { |
| 530 | start_at_offset_ = starting_offset; |
| 531 | // Prepare data 10 kB of data. |
| 532 | data_.clear(); |
| 533 | for (unsigned int i = 0; i < 10 * 1000; i++) |
| 534 | data_ += 'a' + (i % 25); |
| 535 | |
| 536 | // Setup p2p. |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 537 | FakeP2PManagerConfiguration* test_conf = new FakeP2PManagerConfiguration(); |
| 538 | p2p_manager_.reset(P2PManager::Construct(test_conf, |
| 539 | nullptr, |
| 540 | &fake_um_, |
| 541 | "cros_au", |
| 542 | 3, |
| 543 | base::TimeDelta::FromDays(5))); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 544 | fake_system_state_.set_p2p_manager(p2p_manager_.get()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // To be called by tests to perform the download. The |
| 548 | // |use_p2p_to_share| parameter is used to indicate whether the |
| 549 | // payload should be shared via p2p. |
| 550 | void StartDownload(bool use_p2p_to_share) { |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 551 | EXPECT_CALL(*fake_system_state_.mock_payload_state(), |
| 552 | GetUsingP2PForSharing()) |
| 553 | .WillRepeatedly(Return(use_p2p_to_share)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 554 | |
| 555 | ScopedTempFile output_temp_file; |
| 556 | TestDirectFileWriter writer; |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 557 | EXPECT_EQ( |
| 558 | 0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0)); |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 559 | InstallPlan install_plan; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 560 | install_plan.payloads.push_back( |
| 561 | {.size = data_.length(), |
| 562 | .hash = {'1', '2', '3', '4', 'h', 'a', 's', 'h'}}); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 563 | auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>(); |
| 564 | feeder_action->set_obj(install_plan); |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 565 | MockPrefs prefs; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 566 | // Note that DownloadAction takes ownership of the passed in HttpFetcher. |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 567 | auto download_action = std::make_unique<DownloadAction>( |
| 568 | &prefs, |
| 569 | fake_system_state_.boot_control(), |
| 570 | fake_system_state_.hardware(), |
| 571 | &fake_system_state_, |
| 572 | new MockHttpFetcher(data_.c_str(), data_.length(), nullptr), |
| 573 | false /* interactive */); |
| 574 | auto http_fetcher = download_action->http_fetcher(); |
| 575 | download_action->SetTestFileWriter(&writer); |
| 576 | BondActions(feeder_action.get(), download_action.get()); |
| 577 | delegate_.expected_data_ = |
| 578 | brillo::Blob(data_.begin() + start_at_offset_, data_.end()); |
| 579 | delegate_.path_ = output_temp_file.path(); |
| 580 | processor_.set_delegate(&delegate_); |
| 581 | processor_.EnqueueAction(std::move(feeder_action)); |
| 582 | processor_.EnqueueAction(std::move(download_action)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 583 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 584 | loop_.PostTask( |
| 585 | FROM_HERE, |
| 586 | base::Bind( |
| 587 | [](P2PDownloadActionTest* action_test, HttpFetcher* http_fetcher) { |
| 588 | action_test->processor_.StartProcessing(); |
| 589 | http_fetcher->SetOffset(action_test->start_at_offset_); |
| 590 | }, |
| 591 | base::Unretained(this), |
| 592 | base::Unretained(http_fetcher))); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 593 | loop_.Run(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 596 | // Mainloop used to make StartDownload() synchronous. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 597 | brillo::FakeMessageLoop loop_{nullptr}; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 598 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 599 | // Delegate that is passed to the ActionProcessor. |
| 600 | DownloadActionTestProcessorDelegate delegate_; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 601 | |
| 602 | // The P2PManager used in the test. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 603 | unique_ptr<P2PManager> p2p_manager_; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 604 | |
| 605 | // The ActionProcessor used for running the actions. |
| 606 | ActionProcessor processor_; |
| 607 | |
| 608 | // A fake system state. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 609 | FakeSystemState fake_system_state_; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 610 | |
| 611 | // The data being downloaded. |
| 612 | string data_; |
| 613 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 614 | private: |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 615 | // The requested starting offset passed to SetupDownload(). |
| 616 | off_t start_at_offset_; |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 617 | |
| 618 | chromeos_update_manager::FakeUpdateManager fake_um_; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 619 | }; |
| 620 | |
| 621 | TEST_F(P2PDownloadActionTest, IsWrittenTo) { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 622 | SetupDownload(0); // starting_offset |
| 623 | StartDownload(true); // use_p2p_to_share |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 624 | |
| 625 | // Check the p2p file and its content matches what was sent. |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 626 | string file_id = delegate_.p2p_file_id_; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 627 | EXPECT_NE("", file_id); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 628 | EXPECT_EQ(static_cast<int>(data_.length()), |
| 629 | p2p_manager_->FileGetSize(file_id)); |
| 630 | EXPECT_EQ(static_cast<int>(data_.length()), |
| 631 | p2p_manager_->FileGetExpectedSize(file_id)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 632 | string p2p_file_contents; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 633 | EXPECT_TRUE( |
| 634 | ReadFileToString(p2p_manager_->FileGetPath(file_id), &p2p_file_contents)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 635 | EXPECT_EQ(data_, p2p_file_contents); |
| 636 | } |
| 637 | |
| 638 | TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 639 | SetupDownload(1000); // starting_offset |
| 640 | StartDownload(true); // use_p2p_to_share |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 641 | |
| 642 | // DownloadAction should convey that the file is not being shared. |
| 643 | // and that we don't have any p2p files. |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 644 | EXPECT_EQ(delegate_.p2p_file_id_, ""); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 645 | EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0); |
| 646 | } |
| 647 | |
| 648 | TEST_F(P2PDownloadActionTest, CanAppend) { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 649 | SetupDownload(1000); // starting_offset |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 650 | |
| 651 | // Prepare the file with existing data before starting to write to |
| 652 | // it via DownloadAction. |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 653 | string file_id = utils::CalculateP2PFileId( |
| 654 | {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 655 | ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length())); |
| 656 | string existing_data; |
| 657 | for (unsigned int i = 0; i < 1000; i++) |
| 658 | existing_data += '0' + (i % 10); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 659 | ASSERT_EQ( |
| 660 | WriteFile( |
| 661 | p2p_manager_->FileGetPath(file_id), existing_data.c_str(), 1000), |
| 662 | 1000); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 663 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 664 | StartDownload(true); // use_p2p_to_share |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 665 | |
| 666 | // DownloadAction should convey the same file_id and the file should |
| 667 | // have the expected size. |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 668 | EXPECT_EQ(delegate_.p2p_file_id_, file_id); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 669 | EXPECT_EQ(static_cast<ssize_t>(data_.length()), |
| 670 | p2p_manager_->FileGetSize(file_id)); |
| 671 | EXPECT_EQ(static_cast<ssize_t>(data_.length()), |
| 672 | p2p_manager_->FileGetExpectedSize(file_id)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 673 | string p2p_file_contents; |
| 674 | // Check that the first 1000 bytes wasn't touched and that we |
| 675 | // appended the remaining as appropriate. |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 676 | EXPECT_TRUE( |
| 677 | ReadFileToString(p2p_manager_->FileGetPath(file_id), &p2p_file_contents)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 678 | EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000)); |
| 679 | EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000)); |
| 680 | } |
| 681 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 682 | TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 683 | SetupDownload(1000); // starting_offset |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 684 | |
| 685 | // Prepare the file with all existing data before starting to write |
| 686 | // to it via DownloadAction. |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 687 | string file_id = utils::CalculateP2PFileId( |
| 688 | {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 689 | ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length())); |
| 690 | string existing_data; |
| 691 | for (unsigned int i = 0; i < 1000; i++) |
| 692 | existing_data += '0' + (i % 10); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 693 | ASSERT_EQ( |
| 694 | WriteFile( |
| 695 | p2p_manager_->FileGetPath(file_id), existing_data.c_str(), 1000), |
| 696 | 1000); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 697 | |
| 698 | // Check that the file is there. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 699 | EXPECT_EQ(1000, p2p_manager_->FileGetSize(file_id)); |
| 700 | EXPECT_EQ(1, p2p_manager_->CountSharedFiles()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 701 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 702 | StartDownload(false); // use_p2p_to_share |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 703 | |
| 704 | // DownloadAction should have deleted the p2p file. Check that it's gone. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 705 | EXPECT_EQ(-1, p2p_manager_->FileGetSize(file_id)); |
| 706 | EXPECT_EQ(0, p2p_manager_->CountSharedFiles()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 707 | } |
| 708 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 709 | } // namespace chromeos_update_engine |