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