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: |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 39 | explicit DownloadActionTestProcessorDelegate(ActionExitCode expected_code) |
| 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, |
| 47 | ActionExitCode 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)); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 52 | if (expected_code_ != kActionCodeDownloadWriteError) { |
| 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, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 63 | ActionExitCode 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 { |
| 68 | EXPECT_EQ(kActionCodeSuccess, code); |
| 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_; |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 76 | ActionExitCode 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 |
| 148 | DownloadAction download_action(&prefs, 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 | } |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 162 | ActionExitCode expected_code = kActionCodeSuccess; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 163 | if (fail_write > 0) |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 164 | expected_code = kActionCodeDownloadWriteError; |
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; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -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; |
| 258 | DownloadAction download_action(&prefs, |
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. |
| 310 | struct DownloadActionTestAction : public Action<DownloadActionTestAction> { |
| 311 | DownloadActionTestAction() : did_run_(false) {} |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 312 | typedef InstallPlan InputObjectType; |
| 313 | typedef InstallPlan OutputObjectType; |
| 314 | ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); } |
| 315 | ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 316 | ActionProcessor* processor() { return processor_; } |
| 317 | void PerformAction() { |
| 318 | did_run_ = true; |
| 319 | ASSERT_TRUE(HasInputObject()); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 320 | EXPECT_TRUE(expected_input_object_ == GetInputObject()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 321 | ASSERT_TRUE(processor()); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 322 | processor()->ActionComplete(this, kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 323 | } |
| 324 | string Type() const { return "DownloadActionTestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 325 | InstallPlan expected_input_object_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 326 | bool did_run_; |
| 327 | }; |
| 328 | |
| 329 | namespace { |
| 330 | // This class is an ActionProcessorDelegate that simply terminates the |
| 331 | // run loop when the ActionProcessor has completed processing. It's used |
| 332 | // only by the test PassObjectOutTest. |
| 333 | class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate { |
| 334 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 335 | void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 336 | ASSERT_TRUE(loop_); |
| 337 | g_main_loop_quit(loop_); |
| 338 | } |
| 339 | GMainLoop *loop_; |
| 340 | }; |
| 341 | |
| 342 | gboolean PassObjectOutTestStarter(gpointer data) { |
| 343 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 344 | processor->StartProcessing(); |
| 345 | return FALSE; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | TEST(DownloadActionTest, PassObjectOutTest) { |
| 350 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 351 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 352 | DirectFileWriter writer; |
| 353 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 354 | // takes ownership of passed in HttpFetcher |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 355 | InstallPlan install_plan(false, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 356 | "", |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 357 | 1, |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 358 | OmahaHashCalculator::OmahaHashOfString("x"), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame^] | 359 | 0, |
| 360 | "", |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 361 | "/dev/null", |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 362 | "/dev/null"); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 363 | ObjectFeederAction<InstallPlan> feeder_action; |
| 364 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 365 | PrefsMock prefs; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 366 | DownloadAction download_action(&prefs, new MockHttpFetcher("x", 1, NULL)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 367 | download_action.SetTestFileWriter(&writer); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 368 | |
| 369 | DownloadActionTestAction test_action; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 370 | test_action.expected_input_object_ = install_plan; |
| 371 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 372 | BondActions(&download_action, &test_action); |
| 373 | |
| 374 | ActionProcessor processor; |
| 375 | PassObjectOutTestProcessorDelegate delegate; |
| 376 | delegate.loop_ = loop; |
| 377 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 378 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 379 | processor.EnqueueAction(&download_action); |
| 380 | processor.EnqueueAction(&test_action); |
| 381 | |
| 382 | g_timeout_add(0, &PassObjectOutTestStarter, &processor); |
| 383 | g_main_loop_run(loop); |
| 384 | g_main_loop_unref(loop); |
| 385 | |
| 386 | EXPECT_EQ(true, test_action.did_run_); |
| 387 | } |
| 388 | |
| 389 | TEST(DownloadActionTest, BadOutFileTest) { |
| 390 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 391 | |
| 392 | 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] | 393 | DirectFileWriter writer; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 394 | |
| 395 | // takes ownership of passed in HttpFetcher |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame^] | 396 | InstallPlan install_plan(false, "", 0, "", 0, "", path, ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 397 | ObjectFeederAction<InstallPlan> feeder_action; |
| 398 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 399 | PrefsMock prefs; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 400 | DownloadAction download_action(&prefs, new MockHttpFetcher("x", 1, NULL)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 401 | download_action.SetTestFileWriter(&writer); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 402 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 403 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 404 | |
| 405 | ActionProcessor processor; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 406 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 407 | processor.EnqueueAction(&download_action); |
| 408 | processor.StartProcessing(); |
| 409 | ASSERT_FALSE(processor.IsRunning()); |
| 410 | |
| 411 | g_main_loop_unref(loop); |
| 412 | } |
| 413 | |
| 414 | } // namespace chromeos_update_engine |