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