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