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