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