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