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