rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 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)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 | ASSERT_EQ(expected_data_.size(), found_data.size()); |
| 53 | for (unsigned i = 0; i < expected_data_.size(); i++) { |
| 54 | EXPECT_EQ(expected_data_[i], found_data[i]); |
| 55 | } |
| 56 | processing_done_called_ = true; |
| 57 | } |
| 58 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 59 | virtual void ActionCompleted(ActionProcessor* processor, |
| 60 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 61 | ActionExitCode code) { |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 62 | const string type = action->Type(); |
| 63 | if (type == DownloadAction::StaticType()) { |
| 64 | EXPECT_EQ(expected_code_, code); |
| 65 | } else { |
| 66 | EXPECT_EQ(kActionCodeSuccess, code); |
| 67 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | GMainLoop *loop_; |
| 71 | string path_; |
| 72 | vector<char> expected_data_; |
| 73 | bool processing_done_called_; |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 74 | ActionExitCode expected_code_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | struct EntryPointArgs { |
| 78 | const vector<char> *data; |
| 79 | GMainLoop *loop; |
| 80 | ActionProcessor *processor; |
| 81 | }; |
| 82 | |
| 83 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 84 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 85 | processor->StartProcessing(); |
| 86 | return FALSE; |
| 87 | } |
| 88 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 89 | void TestWithData(const vector<char>& data, |
| 90 | bool hash_test, |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 91 | bool size_test, |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 92 | bool use_download_delegate) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 94 | |
| 95 | // 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] | 96 | ScopedTempFile output_temp_file; |
| 97 | DirectFileWriter writer; |
| 98 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 99 | // takes ownership of passed in HttpFetcher |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 100 | string hash = hash_test ? |
| 101 | OmahaHashCalculator::OmahaHashOfString("random string") : |
| 102 | OmahaHashCalculator::OmahaHashOfData(data); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 103 | uint64_t size = data.size() + (size_test ? 1 : 0); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 104 | InstallPlan install_plan(true, |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 105 | false, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 106 | "", |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 107 | size, |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 108 | hash, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 109 | output_temp_file.GetPath(), |
| 110 | ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 111 | ObjectFeederAction<InstallPlan> feeder_action; |
| 112 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 113 | PrefsMock prefs; |
| 114 | DownloadAction download_action(&prefs, new MockHttpFetcher(&data[0], |
| 115 | data.size())); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 116 | download_action.SetTestFileWriter(&writer); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 117 | BondActions(&feeder_action, &download_action); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 118 | DownloadActionDelegateMock download_delegate; |
| 119 | if (use_download_delegate) { |
| 120 | InSequence s; |
| 121 | download_action.set_delegate(&download_delegate); |
| 122 | EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); |
| 123 | EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); |
| 124 | EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); |
| 125 | } |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 126 | ActionExitCode expected_code = kActionCodeSuccess; |
| 127 | if (hash_test) |
| 128 | expected_code = kActionCodeDownloadHashMismatchError; |
| 129 | else if (size_test) |
| 130 | expected_code = kActionCodeDownloadSizeMismatchError; |
| 131 | DownloadActionTestProcessorDelegate delegate(expected_code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 132 | delegate.loop_ = loop; |
| 133 | delegate.expected_data_ = data; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 134 | delegate.path_ = output_temp_file.GetPath(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 135 | ActionProcessor processor; |
| 136 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 137 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 138 | processor.EnqueueAction(&download_action); |
| 139 | |
| 140 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 141 | g_main_loop_run(loop); |
| 142 | g_main_loop_unref(loop); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 143 | } |
| 144 | } // namespace {} |
| 145 | |
| 146 | TEST(DownloadActionTest, SimpleTest) { |
| 147 | vector<char> small; |
| 148 | const char* foo = "foo"; |
| 149 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 150 | TestWithData(small, |
| 151 | false, // hash_test |
| 152 | false, // size_test |
| 153 | true); // use_download_delegate |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | TEST(DownloadActionTest, LargeTest) { |
| 157 | vector<char> big(5 * kMockHttpFetcherChunkSize); |
| 158 | char c = '0'; |
| 159 | for (unsigned int i = 0; i < big.size(); i++) { |
| 160 | big[i] = c; |
| 161 | if ('9' == c) |
| 162 | c = '0'; |
| 163 | else |
| 164 | c++; |
| 165 | } |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 166 | TestWithData(big, |
| 167 | false, // hash_test |
| 168 | false, // size_test |
| 169 | true); // use_download_delegate |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | TEST(DownloadActionTest, BadHashTest) { |
| 173 | vector<char> small; |
| 174 | const char* foo = "foo"; |
| 175 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 176 | TestWithData(small, |
| 177 | true, // hash_test |
| 178 | false, // size_test |
| 179 | true); // use_download_delegate |
| 180 | } |
| 181 | |
| 182 | TEST(DownloadActionTest, BadSizeTest) { |
| 183 | const char* something = "something"; |
| 184 | vector<char> small(something, something + strlen(something)); |
| 185 | TestWithData(small, |
| 186 | false, // hash_test |
| 187 | true, // size_test |
| 188 | true); // use_download_delegate |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | TEST(DownloadActionTest, NoDownloadDelegateTest) { |
| 192 | vector<char> small; |
| 193 | const char* foo = "foofoo"; |
| 194 | small.insert(small.end(), foo, foo + strlen(foo)); |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 195 | TestWithData(small, |
| 196 | false, // hash_test |
| 197 | false, // size_test |
| 198 | false); // use_download_delegate |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | namespace { |
| 202 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 203 | public: |
| 204 | void ProcessingStopped(const ActionProcessor* processor) { |
| 205 | ASSERT_TRUE(loop_); |
| 206 | g_main_loop_quit(loop_); |
| 207 | } |
| 208 | GMainLoop *loop_; |
| 209 | }; |
| 210 | |
| 211 | gboolean TerminateEarlyTestStarter(gpointer data) { |
| 212 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 213 | processor->StartProcessing(); |
| 214 | CHECK(processor->IsRunning()); |
| 215 | processor->StopProcessing(); |
| 216 | return FALSE; |
| 217 | } |
| 218 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 219 | void TestTerminateEarly(bool use_download_delegate) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 220 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 221 | |
| 222 | vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2); |
| 223 | memset(&data[0], 0, data.size()); |
| 224 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 225 | ScopedTempFile temp_file; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 226 | { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 227 | DirectFileWriter writer; |
| 228 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 229 | // takes ownership of passed in HttpFetcher |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 230 | ObjectFeederAction<InstallPlan> feeder_action; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 231 | InstallPlan install_plan(true, false, "", 0, "", temp_file.GetPath(), ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 232 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 233 | PrefsMock prefs; |
| 234 | DownloadAction download_action(&prefs, |
| 235 | new MockHttpFetcher(&data[0], data.size())); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 236 | download_action.SetTestFileWriter(&writer); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 237 | DownloadActionDelegateMock download_delegate; |
| 238 | if (use_download_delegate) { |
| 239 | InSequence s; |
| 240 | download_action.set_delegate(&download_delegate); |
| 241 | EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); |
| 242 | EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); |
| 243 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 244 | TerminateEarlyTestProcessorDelegate delegate; |
| 245 | delegate.loop_ = loop; |
| 246 | ActionProcessor processor; |
| 247 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 248 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 249 | processor.EnqueueAction(&download_action); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 250 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 251 | |
| 252 | g_timeout_add(0, &TerminateEarlyTestStarter, &processor); |
| 253 | g_main_loop_run(loop); |
| 254 | g_main_loop_unref(loop); |
| 255 | } |
| 256 | |
| 257 | // 1 or 0 chunks should have come through |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 258 | const off_t resulting_file_size(utils::FileSize(temp_file.GetPath())); |
| 259 | EXPECT_GE(resulting_file_size, 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 260 | if (resulting_file_size != 0) |
| 261 | EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size); |
| 262 | } |
| 263 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 264 | } // namespace {} |
| 265 | |
| 266 | TEST(DownloadActionTest, TerminateEarlyTest) { |
| 267 | TestTerminateEarly(true); |
| 268 | } |
| 269 | |
| 270 | TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) { |
| 271 | TestTerminateEarly(false); |
| 272 | } |
| 273 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 274 | class DownloadActionTestAction; |
| 275 | |
| 276 | template<> |
| 277 | class ActionTraits<DownloadActionTestAction> { |
| 278 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 279 | typedef InstallPlan OutputObjectType; |
| 280 | typedef InstallPlan InputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | // This is a simple Action class for testing. |
| 284 | struct DownloadActionTestAction : public Action<DownloadActionTestAction> { |
| 285 | DownloadActionTestAction() : did_run_(false) {} |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 286 | typedef InstallPlan InputObjectType; |
| 287 | typedef InstallPlan OutputObjectType; |
| 288 | ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); } |
| 289 | ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 290 | ActionProcessor* processor() { return processor_; } |
| 291 | void PerformAction() { |
| 292 | did_run_ = true; |
| 293 | ASSERT_TRUE(HasInputObject()); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 294 | EXPECT_TRUE(expected_input_object_ == GetInputObject()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 295 | ASSERT_TRUE(processor()); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 296 | processor()->ActionComplete(this, kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 297 | } |
| 298 | string Type() const { return "DownloadActionTestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 299 | InstallPlan expected_input_object_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 300 | bool did_run_; |
| 301 | }; |
| 302 | |
| 303 | namespace { |
| 304 | // This class is an ActionProcessorDelegate that simply terminates the |
| 305 | // run loop when the ActionProcessor has completed processing. It's used |
| 306 | // only by the test PassObjectOutTest. |
| 307 | class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate { |
| 308 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 309 | void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 310 | ASSERT_TRUE(loop_); |
| 311 | g_main_loop_quit(loop_); |
| 312 | } |
| 313 | GMainLoop *loop_; |
| 314 | }; |
| 315 | |
| 316 | gboolean PassObjectOutTestStarter(gpointer data) { |
| 317 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 318 | processor->StartProcessing(); |
| 319 | return FALSE; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | TEST(DownloadActionTest, PassObjectOutTest) { |
| 324 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 325 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 326 | DirectFileWriter writer; |
| 327 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 328 | // takes ownership of passed in HttpFetcher |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 329 | InstallPlan install_plan(true, |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 330 | false, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 331 | "", |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 332 | 1, |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 333 | OmahaHashCalculator::OmahaHashOfString("x"), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 334 | "/dev/null", |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 335 | "/dev/null"); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 336 | ObjectFeederAction<InstallPlan> feeder_action; |
| 337 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 338 | PrefsMock prefs; |
| 339 | DownloadAction download_action(&prefs, new MockHttpFetcher("x", 1)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 340 | download_action.SetTestFileWriter(&writer); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 341 | |
| 342 | DownloadActionTestAction test_action; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 343 | test_action.expected_input_object_ = install_plan; |
| 344 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 345 | BondActions(&download_action, &test_action); |
| 346 | |
| 347 | ActionProcessor processor; |
| 348 | PassObjectOutTestProcessorDelegate delegate; |
| 349 | delegate.loop_ = loop; |
| 350 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 351 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 352 | processor.EnqueueAction(&download_action); |
| 353 | processor.EnqueueAction(&test_action); |
| 354 | |
| 355 | g_timeout_add(0, &PassObjectOutTestStarter, &processor); |
| 356 | g_main_loop_run(loop); |
| 357 | g_main_loop_unref(loop); |
| 358 | |
| 359 | EXPECT_EQ(true, test_action.did_run_); |
| 360 | } |
| 361 | |
| 362 | TEST(DownloadActionTest, BadOutFileTest) { |
| 363 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 364 | |
| 365 | 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] | 366 | DirectFileWriter writer; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 367 | |
| 368 | // takes ownership of passed in HttpFetcher |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 369 | InstallPlan install_plan(true, false, "", 0, "", path, ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 370 | ObjectFeederAction<InstallPlan> feeder_action; |
| 371 | feeder_action.set_obj(install_plan); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 372 | PrefsMock prefs; |
| 373 | DownloadAction download_action(&prefs, new MockHttpFetcher("x", 1)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 374 | download_action.SetTestFileWriter(&writer); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 375 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 376 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 377 | |
| 378 | ActionProcessor processor; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 379 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 380 | processor.EnqueueAction(&download_action); |
| 381 | processor.StartProcessing(); |
| 382 | ASSERT_FALSE(processor.IsRunning()); |
| 383 | |
| 384 | g_main_loop_unref(loop); |
| 385 | } |
| 386 | |
| 387 | } // namespace chromeos_update_engine |