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