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 | |
| 5 | #include <string> |
| 6 | #include <vector> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 7 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 8 | #include <glib.h> |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 11 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 12 | #include "update_engine/action_pipe.h" |
| 13 | #include "update_engine/download_action.h" |
| 14 | #include "update_engine/mock_http_fetcher.h" |
| 15 | #include "update_engine/omaha_hash_calculator.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 16 | #include "update_engine/prefs_mock.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 17 | #include "update_engine/test_utils.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 18 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | using std::string; |
| 23 | using std::vector; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 24 | using testing::_; |
| 25 | using testing::AtLeast; |
| 26 | using testing::InSequence; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | |
| 28 | class DownloadActionTest : public ::testing::Test { }; |
| 29 | |
| 30 | namespace { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 31 | class DownloadActionDelegateMock : public DownloadActionDelegate { |
| 32 | public: |
| 33 | MOCK_METHOD1(SetDownloadStatus, void(bool active)); |
| 34 | MOCK_METHOD2(BytesReceived, void(uint64_t bytes_received, uint64_t total)); |
| 35 | }; |
| 36 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 38 | public: |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 39 | explicit DownloadActionTestProcessorDelegate(ErrorCode expected_code) |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 40 | : loop_(NULL), |
| 41 | processing_done_called_(false), |
| 42 | expected_code_(expected_code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 43 | virtual ~DownloadActionTestProcessorDelegate() { |
| 44 | EXPECT_TRUE(processing_done_called_); |
| 45 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 46 | virtual void ProcessingDone(const ActionProcessor* processor, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 47 | ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 48 | ASSERT_TRUE(loop_); |
| 49 | g_main_loop_quit(loop_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 50 | vector<char> found_data; |
| 51 | ASSERT_TRUE(utils::ReadFile(path_, &found_data)); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 52 | if (expected_code_ != kErrorCodeDownloadWriteError) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 53 | ASSERT_EQ(expected_data_.size(), found_data.size()); |
| 54 | for (unsigned i = 0; i < expected_data_.size(); i++) { |
| 55 | EXPECT_EQ(expected_data_[i], found_data[i]); |
| 56 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | } |
| 58 | processing_done_called_ = true; |
| 59 | } |
| 60 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 61 | virtual void ActionCompleted(ActionProcessor* processor, |
| 62 | AbstractAction* action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 63 | ErrorCode code) { |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 64 | const string type = action->Type(); |
| 65 | if (type == DownloadAction::StaticType()) { |
| 66 | EXPECT_EQ(expected_code_, code); |
| 67 | } else { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 68 | EXPECT_EQ(kErrorCodeSuccess, code); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 69 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | GMainLoop *loop_; |
| 73 | string path_; |
| 74 | vector<char> expected_data_; |
| 75 | bool processing_done_called_; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 76 | ErrorCode expected_code_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 79 | class TestDirectFileWriter : public DirectFileWriter { |
| 80 | public: |
| 81 | TestDirectFileWriter() : fail_write_(0), current_write_(0) {} |
| 82 | void set_fail_write(int fail_write) { fail_write_ = fail_write; } |
| 83 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 84 | virtual bool Write(const void* bytes, size_t count) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 85 | if (++current_write_ == fail_write_) { |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 86 | return false; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 87 | } |
| 88 | return DirectFileWriter::Write(bytes, count); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | // If positive, fail on the |fail_write_| call to Write. |
| 93 | int fail_write_; |
| 94 | int current_write_; |
| 95 | }; |
| 96 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 97 | struct EntryPointArgs { |
| 98 | const vector<char> *data; |
| 99 | GMainLoop *loop; |
| 100 | ActionProcessor *processor; |
| 101 | }; |
| 102 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 103 | struct StartProcessorInRunLoopArgs { |
| 104 | ActionProcessor* processor; |
| 105 | MockHttpFetcher* http_fetcher; |
| 106 | }; |
| 107 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 108 | gboolean StartProcessorInRunLoop(gpointer data) { |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 109 | ActionProcessor* processor = |
| 110 | reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | processor->StartProcessing(); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 112 | MockHttpFetcher* http_fetcher = |
| 113 | reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher; |
| 114 | http_fetcher->SetOffset(1); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 115 | return FALSE; |
| 116 | } |
| 117 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 118 | void TestWithData(const vector<char>& data, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 119 | int fail_write, |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 120 | bool use_download_delegate) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 121 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 122 | |
| 123 | // 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] | 124 | ScopedTempFile output_temp_file; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 125 | TestDirectFileWriter writer; |
| 126 | writer.set_fail_write(fail_write); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 127 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 128 | // We pull off the first byte from data and seek past it. |
| 129 | |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 130 | string hash = |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 131 | OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1); |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 132 | uint64_t size = data.size(); |
| 133 | InstallPlan install_plan(false, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 134 | "", |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 135 | size, |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 136 | hash, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 137 | 0, |
| 138 | "", |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 139 | output_temp_file.GetPath(), |
| 140 | ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 141 | ObjectFeederAction<InstallPlan> feeder_action; |
| 142 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 143 | PrefsMock prefs; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 144 | MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0], |
| 145 | data.size(), |
| 146 | NULL); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 147 | // takes ownership of passed in HttpFetcher |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 148 | DownloadAction download_action(&prefs, NULL, http_fetcher); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 149 | download_action.SetTestFileWriter(&writer); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 150 | BondActions(&feeder_action, &download_action); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 151 | DownloadActionDelegateMock download_delegate; |
| 152 | if (use_download_delegate) { |
| 153 | InSequence s; |
| 154 | download_action.set_delegate(&download_delegate); |
| 155 | EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 156 | if (data.size() > kMockHttpFetcherChunkSize) |
| 157 | EXPECT_CALL(download_delegate, |
| 158 | BytesReceived(1 + kMockHttpFetcherChunkSize, _)); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 159 | EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 160 | EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); |
| 161 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 162 | ErrorCode expected_code = kErrorCodeSuccess; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 163 | if (fail_write > 0) |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 164 | expected_code = kErrorCodeDownloadWriteError; |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 165 | DownloadActionTestProcessorDelegate delegate(expected_code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 166 | delegate.loop_ = loop; |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 167 | delegate.expected_data_ = vector<char>(data.begin() + 1, data.end()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 168 | delegate.path_ = output_temp_file.GetPath(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 169 | ActionProcessor processor; |
| 170 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 171 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 172 | processor.EnqueueAction(&download_action); |
| 173 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 174 | StartProcessorInRunLoopArgs args; |
| 175 | args.processor = &processor; |
| 176 | args.http_fetcher = http_fetcher; |
| 177 | g_timeout_add(0, &StartProcessorInRunLoop, &args); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 178 | g_main_loop_run(loop); |
| 179 | g_main_loop_unref(loop); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 180 | } |
| 181 | } // namespace {} |
| 182 | |
| 183 | TEST(DownloadActionTest, SimpleTest) { |
| 184 | vector<char> small; |
| 185 | const char* foo = "foo"; |
| 186 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 187 | TestWithData(small, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 188 | 0, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 189 | true); // use_download_delegate |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TEST(DownloadActionTest, LargeTest) { |
| 193 | vector<char> big(5 * kMockHttpFetcherChunkSize); |
| 194 | char c = '0'; |
| 195 | for (unsigned int i = 0; i < big.size(); i++) { |
| 196 | big[i] = c; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 197 | c = ('9' == c) ? '0' : c + 1; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 198 | } |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 199 | TestWithData(big, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 200 | 0, // fail_write |
| 201 | true); // use_download_delegate |
| 202 | } |
| 203 | |
| 204 | TEST(DownloadActionTest, FailWriteTest) { |
| 205 | vector<char> big(5 * kMockHttpFetcherChunkSize); |
| 206 | char c = '0'; |
| 207 | for (unsigned int i = 0; i < big.size(); i++) { |
| 208 | big[i] = c; |
| 209 | c = ('9' == c) ? '0' : c + 1; |
| 210 | } |
| 211 | TestWithData(big, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 212 | 2, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 213 | true); // use_download_delegate |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 216 | TEST(DownloadActionTest, NoDownloadDelegateTest) { |
| 217 | vector<char> small; |
| 218 | const char* foo = "foofoo"; |
| 219 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 220 | TestWithData(small, |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 221 | 0, // fail_write |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 222 | false); // use_download_delegate |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | namespace { |
| 226 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 227 | public: |
| 228 | void ProcessingStopped(const ActionProcessor* processor) { |
| 229 | ASSERT_TRUE(loop_); |
| 230 | g_main_loop_quit(loop_); |
| 231 | } |
| 232 | GMainLoop *loop_; |
| 233 | }; |
| 234 | |
| 235 | gboolean TerminateEarlyTestStarter(gpointer data) { |
| 236 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 237 | processor->StartProcessing(); |
| 238 | CHECK(processor->IsRunning()); |
| 239 | processor->StopProcessing(); |
| 240 | return FALSE; |
| 241 | } |
| 242 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 243 | void TestTerminateEarly(bool use_download_delegate) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 244 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 245 | |
| 246 | vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2); |
| 247 | memset(&data[0], 0, data.size()); |
| 248 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 249 | ScopedTempFile temp_file; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 250 | { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 251 | DirectFileWriter writer; |
| 252 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 253 | // takes ownership of passed in HttpFetcher |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 254 | ObjectFeederAction<InstallPlan> feeder_action; |
Don Garrett | d1cd325 | 2013-05-22 19:45:17 -0700 | [diff] [blame^] | 255 | InstallPlan install_plan(false, "", 0, "", 0, "", temp_file.GetPath(), ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 256 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 257 | PrefsMock prefs; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 258 | DownloadAction download_action(&prefs, NULL, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 259 | new MockHttpFetcher(&data[0], |
| 260 | data.size(), |
| 261 | NULL)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 262 | download_action.SetTestFileWriter(&writer); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 263 | DownloadActionDelegateMock download_delegate; |
| 264 | if (use_download_delegate) { |
| 265 | InSequence s; |
| 266 | download_action.set_delegate(&download_delegate); |
| 267 | EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); |
| 268 | EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); |
| 269 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 270 | TerminateEarlyTestProcessorDelegate delegate; |
| 271 | delegate.loop_ = loop; |
| 272 | ActionProcessor processor; |
| 273 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 274 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 275 | processor.EnqueueAction(&download_action); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 276 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 277 | |
| 278 | g_timeout_add(0, &TerminateEarlyTestStarter, &processor); |
| 279 | g_main_loop_run(loop); |
| 280 | g_main_loop_unref(loop); |
| 281 | } |
| 282 | |
| 283 | // 1 or 0 chunks should have come through |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 284 | const off_t resulting_file_size(utils::FileSize(temp_file.GetPath())); |
| 285 | EXPECT_GE(resulting_file_size, 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 286 | if (resulting_file_size != 0) |
| 287 | EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size); |
| 288 | } |
| 289 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 290 | } // namespace {} |
| 291 | |
| 292 | TEST(DownloadActionTest, TerminateEarlyTest) { |
| 293 | TestTerminateEarly(true); |
| 294 | } |
| 295 | |
| 296 | TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) { |
| 297 | TestTerminateEarly(false); |
| 298 | } |
| 299 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 300 | class DownloadActionTestAction; |
| 301 | |
| 302 | template<> |
| 303 | class ActionTraits<DownloadActionTestAction> { |
| 304 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 305 | typedef InstallPlan OutputObjectType; |
| 306 | typedef InstallPlan InputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | // This is a simple Action class for testing. |
Yunlian Jiang | 2dac576 | 2013-04-12 09:53:09 -0700 | [diff] [blame] | 310 | class DownloadActionTestAction : public Action<DownloadActionTestAction> { |
| 311 | public: |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 312 | DownloadActionTestAction() : did_run_(false) {} |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 313 | typedef InstallPlan InputObjectType; |
| 314 | typedef InstallPlan OutputObjectType; |
| 315 | ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); } |
| 316 | ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 317 | ActionProcessor* processor() { return processor_; } |
| 318 | void PerformAction() { |
| 319 | did_run_ = true; |
| 320 | ASSERT_TRUE(HasInputObject()); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 321 | EXPECT_TRUE(expected_input_object_ == GetInputObject()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 322 | ASSERT_TRUE(processor()); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 323 | processor()->ActionComplete(this, kErrorCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 324 | } |
| 325 | string Type() const { return "DownloadActionTestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 326 | InstallPlan expected_input_object_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 327 | bool did_run_; |
| 328 | }; |
| 329 | |
| 330 | namespace { |
| 331 | // This class is an ActionProcessorDelegate that simply terminates the |
| 332 | // run loop when the ActionProcessor has completed processing. It's used |
| 333 | // only by the test PassObjectOutTest. |
| 334 | class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate { |
| 335 | public: |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 336 | void ProcessingDone(const ActionProcessor* processor, ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 337 | ASSERT_TRUE(loop_); |
| 338 | g_main_loop_quit(loop_); |
| 339 | } |
| 340 | GMainLoop *loop_; |
| 341 | }; |
| 342 | |
| 343 | gboolean PassObjectOutTestStarter(gpointer data) { |
| 344 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 345 | processor->StartProcessing(); |
| 346 | return FALSE; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | TEST(DownloadActionTest, PassObjectOutTest) { |
| 351 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 352 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 353 | DirectFileWriter writer; |
| 354 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 355 | // takes ownership of passed in HttpFetcher |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 356 | InstallPlan install_plan(false, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 357 | "", |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 358 | 1, |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 359 | OmahaHashCalculator::OmahaHashOfString("x"), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 360 | 0, |
| 361 | "", |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 362 | "/dev/null", |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 363 | "/dev/null"); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 364 | ObjectFeederAction<InstallPlan> feeder_action; |
| 365 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 366 | PrefsMock prefs; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 367 | DownloadAction download_action(&prefs, NULL, |
| 368 | new MockHttpFetcher("x", 1, NULL)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 369 | download_action.SetTestFileWriter(&writer); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 370 | |
| 371 | DownloadActionTestAction test_action; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 372 | test_action.expected_input_object_ = install_plan; |
| 373 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 374 | BondActions(&download_action, &test_action); |
| 375 | |
| 376 | ActionProcessor processor; |
| 377 | PassObjectOutTestProcessorDelegate delegate; |
| 378 | delegate.loop_ = loop; |
| 379 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 380 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 381 | processor.EnqueueAction(&download_action); |
| 382 | processor.EnqueueAction(&test_action); |
| 383 | |
| 384 | g_timeout_add(0, &PassObjectOutTestStarter, &processor); |
| 385 | g_main_loop_run(loop); |
| 386 | g_main_loop_unref(loop); |
| 387 | |
| 388 | EXPECT_EQ(true, test_action.did_run_); |
| 389 | } |
| 390 | |
| 391 | TEST(DownloadActionTest, BadOutFileTest) { |
| 392 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 393 | |
| 394 | 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] | 395 | DirectFileWriter writer; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 396 | |
| 397 | // takes ownership of passed in HttpFetcher |
Don Garrett | d1cd325 | 2013-05-22 19:45:17 -0700 | [diff] [blame^] | 398 | InstallPlan install_plan(false, "", 0, "", 0, "", path, ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 399 | ObjectFeederAction<InstallPlan> feeder_action; |
| 400 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 401 | PrefsMock prefs; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 402 | DownloadAction download_action(&prefs, NULL, |
| 403 | new MockHttpFetcher("x", 1, NULL)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 404 | download_action.SetTestFileWriter(&writer); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 405 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 406 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 407 | |
| 408 | ActionProcessor processor; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 409 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 410 | processor.EnqueueAction(&download_action); |
| 411 | processor.StartProcessing(); |
| 412 | ASSERT_FALSE(processor.IsRunning()); |
| 413 | |
| 414 | g_main_loop_unref(loop); |
| 415 | } |
| 416 | |
| 417 | } // namespace chromeos_update_engine |