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