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