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> |
| 7 | #include <glib.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | #include "update_engine/action_pipe.h" |
| 10 | #include "update_engine/download_action.h" |
| 11 | #include "update_engine/mock_http_fetcher.h" |
| 12 | #include "update_engine/omaha_hash_calculator.h" |
| 13 | #include "update_engine/test_utils.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 14 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
| 18 | using std::string; |
| 19 | using std::vector; |
| 20 | |
| 21 | class DownloadActionTest : public ::testing::Test { }; |
| 22 | |
| 23 | namespace { |
| 24 | class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 25 | public: |
| 26 | DownloadActionTestProcessorDelegate() |
| 27 | : loop_(NULL), processing_done_called_(false) {} |
| 28 | virtual ~DownloadActionTestProcessorDelegate() { |
| 29 | EXPECT_TRUE(processing_done_called_); |
| 30 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 31 | virtual void ProcessingDone(const ActionProcessor* processor, |
| 32 | ActionExitCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | ASSERT_TRUE(loop_); |
| 34 | g_main_loop_quit(loop_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 35 | vector<char> found_data; |
| 36 | ASSERT_TRUE(utils::ReadFile(path_, &found_data)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | ASSERT_EQ(expected_data_.size(), found_data.size()); |
| 38 | for (unsigned i = 0; i < expected_data_.size(); i++) { |
| 39 | EXPECT_EQ(expected_data_[i], found_data[i]); |
| 40 | } |
| 41 | processing_done_called_ = true; |
| 42 | } |
| 43 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 44 | virtual void ActionCompleted(ActionProcessor* processor, |
| 45 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 46 | ActionExitCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 | // make sure actions always succeed |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 48 | EXPECT_EQ(kActionCodeSuccess, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | GMainLoop *loop_; |
| 52 | string path_; |
| 53 | vector<char> expected_data_; |
| 54 | bool processing_done_called_; |
| 55 | }; |
| 56 | |
| 57 | struct EntryPointArgs { |
| 58 | const vector<char> *data; |
| 59 | GMainLoop *loop; |
| 60 | ActionProcessor *processor; |
| 61 | }; |
| 62 | |
| 63 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 64 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 65 | processor->StartProcessing(); |
| 66 | return FALSE; |
| 67 | } |
| 68 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 69 | void TestWithData(const vector<char>& data) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 71 | |
| 72 | // 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] | 73 | ScopedTempFile output_temp_file; |
| 74 | DirectFileWriter writer; |
| 75 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 76 | // takes ownership of passed in HttpFetcher |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 77 | InstallPlan install_plan(true, |
| 78 | "", |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 79 | 0, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 80 | OmahaHashCalculator::OmahaHashOfData(data), |
| 81 | output_temp_file.GetPath(), |
| 82 | ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 83 | ObjectFeederAction<InstallPlan> feeder_action; |
| 84 | feeder_action.set_obj(install_plan); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 85 | DownloadAction download_action(new MockHttpFetcher(&data[0], |
| 86 | data.size())); |
| 87 | download_action.SetTestFileWriter(&writer); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 88 | BondActions(&feeder_action, &download_action); |
| 89 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | DownloadActionTestProcessorDelegate delegate; |
| 91 | delegate.loop_ = loop; |
| 92 | delegate.expected_data_ = data; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 93 | delegate.path_ = output_temp_file.GetPath(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 94 | ActionProcessor processor; |
| 95 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 96 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 97 | processor.EnqueueAction(&download_action); |
| 98 | |
| 99 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 100 | g_main_loop_run(loop); |
| 101 | g_main_loop_unref(loop); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 102 | } |
| 103 | } // namespace {} |
| 104 | |
| 105 | TEST(DownloadActionTest, SimpleTest) { |
| 106 | vector<char> small; |
| 107 | const char* foo = "foo"; |
| 108 | small.insert(small.end(), foo, foo + strlen(foo)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 109 | TestWithData(small); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST(DownloadActionTest, LargeTest) { |
| 113 | vector<char> big(5 * kMockHttpFetcherChunkSize); |
| 114 | char c = '0'; |
| 115 | for (unsigned int i = 0; i < big.size(); i++) { |
| 116 | big[i] = c; |
| 117 | if ('9' == c) |
| 118 | c = '0'; |
| 119 | else |
| 120 | c++; |
| 121 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 122 | TestWithData(big); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | namespace { |
| 126 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 127 | public: |
| 128 | void ProcessingStopped(const ActionProcessor* processor) { |
| 129 | ASSERT_TRUE(loop_); |
| 130 | g_main_loop_quit(loop_); |
| 131 | } |
| 132 | GMainLoop *loop_; |
| 133 | }; |
| 134 | |
| 135 | gboolean TerminateEarlyTestStarter(gpointer data) { |
| 136 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 137 | processor->StartProcessing(); |
| 138 | CHECK(processor->IsRunning()); |
| 139 | processor->StopProcessing(); |
| 140 | return FALSE; |
| 141 | } |
| 142 | |
| 143 | } // namespace {} |
| 144 | |
| 145 | TEST(DownloadActionTest, TerminateEarlyTest) { |
| 146 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 147 | |
| 148 | vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2); |
| 149 | memset(&data[0], 0, data.size()); |
| 150 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 151 | ScopedTempFile temp_file; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 152 | { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 153 | DirectFileWriter writer; |
| 154 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 155 | // takes ownership of passed in HttpFetcher |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 156 | ObjectFeederAction<InstallPlan> feeder_action; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 157 | InstallPlan install_plan(true, "", 0, "", temp_file.GetPath(), ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 158 | feeder_action.set_obj(install_plan); |
| 159 | DownloadAction download_action(new MockHttpFetcher(&data[0], data.size())); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 160 | download_action.SetTestFileWriter(&writer); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 161 | TerminateEarlyTestProcessorDelegate delegate; |
| 162 | delegate.loop_ = loop; |
| 163 | ActionProcessor processor; |
| 164 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 165 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 166 | processor.EnqueueAction(&download_action); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 167 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 168 | |
| 169 | g_timeout_add(0, &TerminateEarlyTestStarter, &processor); |
| 170 | g_main_loop_run(loop); |
| 171 | g_main_loop_unref(loop); |
| 172 | } |
| 173 | |
| 174 | // 1 or 0 chunks should have come through |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 175 | const off_t resulting_file_size(utils::FileSize(temp_file.GetPath())); |
| 176 | EXPECT_GE(resulting_file_size, 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 177 | if (resulting_file_size != 0) |
| 178 | EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size); |
| 179 | } |
| 180 | |
| 181 | class DownloadActionTestAction; |
| 182 | |
| 183 | template<> |
| 184 | class ActionTraits<DownloadActionTestAction> { |
| 185 | public: |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 186 | typedef InstallPlan OutputObjectType; |
| 187 | typedef InstallPlan InputObjectType; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | // This is a simple Action class for testing. |
| 191 | struct DownloadActionTestAction : public Action<DownloadActionTestAction> { |
| 192 | DownloadActionTestAction() : did_run_(false) {} |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 193 | typedef InstallPlan InputObjectType; |
| 194 | typedef InstallPlan OutputObjectType; |
| 195 | ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); } |
| 196 | ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 197 | ActionProcessor* processor() { return processor_; } |
| 198 | void PerformAction() { |
| 199 | did_run_ = true; |
| 200 | ASSERT_TRUE(HasInputObject()); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 201 | EXPECT_TRUE(expected_input_object_ == GetInputObject()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 202 | ASSERT_TRUE(processor()); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 203 | processor()->ActionComplete(this, kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 204 | } |
| 205 | string Type() const { return "DownloadActionTestAction"; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 206 | InstallPlan expected_input_object_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 207 | bool did_run_; |
| 208 | }; |
| 209 | |
| 210 | namespace { |
| 211 | // This class is an ActionProcessorDelegate that simply terminates the |
| 212 | // run loop when the ActionProcessor has completed processing. It's used |
| 213 | // only by the test PassObjectOutTest. |
| 214 | class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate { |
| 215 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 216 | void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 217 | ASSERT_TRUE(loop_); |
| 218 | g_main_loop_quit(loop_); |
| 219 | } |
| 220 | GMainLoop *loop_; |
| 221 | }; |
| 222 | |
| 223 | gboolean PassObjectOutTestStarter(gpointer data) { |
| 224 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 225 | processor->StartProcessing(); |
| 226 | return FALSE; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | TEST(DownloadActionTest, PassObjectOutTest) { |
| 231 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 232 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 233 | DirectFileWriter writer; |
| 234 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 235 | // takes ownership of passed in HttpFetcher |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 236 | InstallPlan install_plan(true, |
| 237 | "", |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 238 | 0, |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 239 | OmahaHashCalculator::OmahaHashOfString("x"), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 240 | "/dev/null", |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 241 | "/dev/null"); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 242 | ObjectFeederAction<InstallPlan> feeder_action; |
| 243 | feeder_action.set_obj(install_plan); |
| 244 | DownloadAction download_action(new MockHttpFetcher("x", 1)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 245 | download_action.SetTestFileWriter(&writer); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 246 | |
| 247 | DownloadActionTestAction test_action; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 248 | test_action.expected_input_object_ = install_plan; |
| 249 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 250 | BondActions(&download_action, &test_action); |
| 251 | |
| 252 | ActionProcessor processor; |
| 253 | PassObjectOutTestProcessorDelegate delegate; |
| 254 | delegate.loop_ = loop; |
| 255 | processor.set_delegate(&delegate); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 256 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 257 | processor.EnqueueAction(&download_action); |
| 258 | processor.EnqueueAction(&test_action); |
| 259 | |
| 260 | g_timeout_add(0, &PassObjectOutTestStarter, &processor); |
| 261 | g_main_loop_run(loop); |
| 262 | g_main_loop_unref(loop); |
| 263 | |
| 264 | EXPECT_EQ(true, test_action.did_run_); |
| 265 | } |
| 266 | |
| 267 | TEST(DownloadActionTest, BadOutFileTest) { |
| 268 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 269 | |
| 270 | 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] | 271 | DirectFileWriter writer; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 272 | |
| 273 | // takes ownership of passed in HttpFetcher |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 274 | InstallPlan install_plan(true, "", 0, "", path, ""); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 275 | ObjectFeederAction<InstallPlan> feeder_action; |
| 276 | feeder_action.set_obj(install_plan); |
| 277 | DownloadAction download_action(new MockHttpFetcher("x", 1)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 278 | download_action.SetTestFileWriter(&writer); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 279 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 280 | BondActions(&feeder_action, &download_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 281 | |
| 282 | ActionProcessor processor; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 283 | processor.EnqueueAction(&feeder_action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 284 | processor.EnqueueAction(&download_action); |
| 285 | processor.StartProcessing(); |
| 286 | ASSERT_FALSE(processor.IsRunning()); |
| 287 | |
| 288 | g_main_loop_unref(loop); |
| 289 | } |
| 290 | |
| 291 | } // namespace chromeos_update_engine |