| 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)); | 
| 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 |  | 
|  | 84 | virtual ssize_t Write(const void* bytes, size_t count) { | 
|  | 85 | if (++current_write_ == fail_write_) { | 
|  | 86 | return -EINVAL; | 
|  | 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, | 
|  | 119 | bool hash_test, | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 120 | bool size_test, | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 121 | int fail_write, | 
| Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 122 | bool use_download_delegate) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 123 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 
|  | 124 |  | 
|  | 125 | // 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] | 126 | ScopedTempFile output_temp_file; | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 127 | TestDirectFileWriter writer; | 
|  | 128 | writer.set_fail_write(fail_write); | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 129 |  | 
| Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 130 | // We pull off the first byte from data and seek past it. | 
|  | 131 |  | 
| Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 132 | string hash = hash_test ? | 
|  | 133 | OmahaHashCalculator::OmahaHashOfString("random string") : | 
| Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 134 | OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1); | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 135 | uint64_t size = data.size() + (size_test ? 1 : 0); | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 136 | InstallPlan install_plan(true, | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 137 | false, | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 138 | "", | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 139 | size, | 
| Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 140 | hash, | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 141 | output_temp_file.GetPath(), | 
|  | 142 | ""); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 143 | ObjectFeederAction<InstallPlan> feeder_action; | 
|  | 144 | feeder_action.set_obj(install_plan); | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 145 | PrefsMock prefs; | 
| Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 146 | MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0], data.size()); | 
|  | 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; | 
|  | 163 | if (hash_test) | 
|  | 164 | expected_code = kActionCodeDownloadHashMismatchError; | 
|  | 165 | else if (size_test) | 
|  | 166 | expected_code = kActionCodeDownloadSizeMismatchError; | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 167 | else if (fail_write > 0) | 
|  | 168 | expected_code = kActionCodeDownloadWriteError; | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 169 | DownloadActionTestProcessorDelegate delegate(expected_code); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 170 | delegate.loop_ = loop; | 
| Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 171 | delegate.expected_data_ = vector<char>(data.begin() + 1, data.end()); | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 172 | delegate.path_ = output_temp_file.GetPath(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 173 | ActionProcessor processor; | 
|  | 174 | processor.set_delegate(&delegate); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 175 | processor.EnqueueAction(&feeder_action); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 176 | processor.EnqueueAction(&download_action); | 
|  | 177 |  | 
| Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 178 | StartProcessorInRunLoopArgs args; | 
|  | 179 | args.processor = &processor; | 
|  | 180 | args.http_fetcher = http_fetcher; | 
|  | 181 | g_timeout_add(0, &StartProcessorInRunLoop, &args); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 182 | g_main_loop_run(loop); | 
|  | 183 | g_main_loop_unref(loop); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 184 | } | 
|  | 185 | }  // namespace {} | 
|  | 186 |  | 
|  | 187 | TEST(DownloadActionTest, SimpleTest) { | 
|  | 188 | vector<char> small; | 
|  | 189 | const char* foo = "foo"; | 
|  | 190 | small.insert(small.end(), foo, foo + strlen(foo)); | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 191 | TestWithData(small, | 
|  | 192 | false,  // hash_test | 
|  | 193 | false,  // size_test | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 194 | 0,  // fail_write | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 195 | true);  // use_download_delegate | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
|  | 198 | TEST(DownloadActionTest, LargeTest) { | 
|  | 199 | vector<char> big(5 * kMockHttpFetcherChunkSize); | 
|  | 200 | char c = '0'; | 
|  | 201 | for (unsigned int i = 0; i < big.size(); i++) { | 
|  | 202 | big[i] = c; | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 203 | c = ('9' == c) ? '0' : c + 1; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 204 | } | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 205 | TestWithData(big, | 
|  | 206 | false,  // hash_test | 
|  | 207 | false,  // size_test | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 208 | 0,  // fail_write | 
|  | 209 | true);  // use_download_delegate | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | TEST(DownloadActionTest, FailWriteTest) { | 
|  | 213 | vector<char> big(5 * kMockHttpFetcherChunkSize); | 
|  | 214 | char c = '0'; | 
|  | 215 | for (unsigned int i = 0; i < big.size(); i++) { | 
|  | 216 | big[i] = c; | 
|  | 217 | c = ('9' == c) ? '0' : c + 1; | 
|  | 218 | } | 
|  | 219 | TestWithData(big, | 
|  | 220 | false,  // hash_test | 
|  | 221 | false,  // size_test | 
|  | 222 | 2,  // fail_write | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 223 | true);  // use_download_delegate | 
| Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
|  | 226 | TEST(DownloadActionTest, BadHashTest) { | 
|  | 227 | vector<char> small; | 
|  | 228 | const char* foo = "foo"; | 
|  | 229 | small.insert(small.end(), foo, foo + strlen(foo)); | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 230 | TestWithData(small, | 
|  | 231 | true,  // hash_test | 
|  | 232 | false,  // size_test | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 233 | 0,  // fail_write | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 234 | true);  // use_download_delegate | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | TEST(DownloadActionTest, BadSizeTest) { | 
|  | 238 | const char* something = "something"; | 
|  | 239 | vector<char> small(something, something + strlen(something)); | 
|  | 240 | TestWithData(small, | 
|  | 241 | false,  // hash_test | 
|  | 242 | true,  // size_test | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 243 | 0,  // fail_write | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 244 | true);  // use_download_delegate | 
| Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 245 | } | 
|  | 246 |  | 
|  | 247 | TEST(DownloadActionTest, NoDownloadDelegateTest) { | 
|  | 248 | vector<char> small; | 
|  | 249 | const char* foo = "foofoo"; | 
|  | 250 | small.insert(small.end(), foo, foo + strlen(foo)); | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 251 | TestWithData(small, | 
|  | 252 | false,  // hash_test | 
|  | 253 | false,  // size_test | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 254 | 0,  // fail_write | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 255 | false);  // use_download_delegate | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
|  | 258 | namespace { | 
|  | 259 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { | 
|  | 260 | public: | 
|  | 261 | void ProcessingStopped(const ActionProcessor* processor) { | 
|  | 262 | ASSERT_TRUE(loop_); | 
|  | 263 | g_main_loop_quit(loop_); | 
|  | 264 | } | 
|  | 265 | GMainLoop *loop_; | 
|  | 266 | }; | 
|  | 267 |  | 
|  | 268 | gboolean TerminateEarlyTestStarter(gpointer data) { | 
|  | 269 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 
|  | 270 | processor->StartProcessing(); | 
|  | 271 | CHECK(processor->IsRunning()); | 
|  | 272 | processor->StopProcessing(); | 
|  | 273 | return FALSE; | 
|  | 274 | } | 
|  | 275 |  | 
| Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 276 | void TestTerminateEarly(bool use_download_delegate) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 277 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 
|  | 278 |  | 
|  | 279 | vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2); | 
|  | 280 | memset(&data[0], 0, data.size()); | 
|  | 281 |  | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 282 | ScopedTempFile temp_file; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 283 | { | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 284 | DirectFileWriter writer; | 
|  | 285 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 286 | // takes ownership of passed in HttpFetcher | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 287 | ObjectFeederAction<InstallPlan> feeder_action; | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 288 | InstallPlan install_plan(true, false, "", 0, "", temp_file.GetPath(), ""); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 289 | feeder_action.set_obj(install_plan); | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 290 | PrefsMock prefs; | 
|  | 291 | DownloadAction download_action(&prefs, | 
|  | 292 | new MockHttpFetcher(&data[0], data.size())); | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 293 | download_action.SetTestFileWriter(&writer); | 
| Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 294 | DownloadActionDelegateMock download_delegate; | 
|  | 295 | if (use_download_delegate) { | 
|  | 296 | InSequence s; | 
|  | 297 | download_action.set_delegate(&download_delegate); | 
|  | 298 | EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); | 
|  | 299 | EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); | 
|  | 300 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 301 | TerminateEarlyTestProcessorDelegate delegate; | 
|  | 302 | delegate.loop_ = loop; | 
|  | 303 | ActionProcessor processor; | 
|  | 304 | processor.set_delegate(&delegate); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 305 | processor.EnqueueAction(&feeder_action); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 306 | processor.EnqueueAction(&download_action); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 307 | BondActions(&feeder_action, &download_action); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 308 |  | 
|  | 309 | g_timeout_add(0, &TerminateEarlyTestStarter, &processor); | 
|  | 310 | g_main_loop_run(loop); | 
|  | 311 | g_main_loop_unref(loop); | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | // 1 or 0 chunks should have come through | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 315 | const off_t resulting_file_size(utils::FileSize(temp_file.GetPath())); | 
|  | 316 | EXPECT_GE(resulting_file_size, 0); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 317 | if (resulting_file_size != 0) | 
|  | 318 | EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size); | 
|  | 319 | } | 
|  | 320 |  | 
| Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 321 | }  // namespace {} | 
|  | 322 |  | 
|  | 323 | TEST(DownloadActionTest, TerminateEarlyTest) { | 
|  | 324 | TestTerminateEarly(true); | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) { | 
|  | 328 | TestTerminateEarly(false); | 
|  | 329 | } | 
|  | 330 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 331 | class DownloadActionTestAction; | 
|  | 332 |  | 
|  | 333 | template<> | 
|  | 334 | class ActionTraits<DownloadActionTestAction> { | 
|  | 335 | public: | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 336 | typedef InstallPlan OutputObjectType; | 
|  | 337 | typedef InstallPlan InputObjectType; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 338 | }; | 
|  | 339 |  | 
|  | 340 | // This is a simple Action class for testing. | 
|  | 341 | struct DownloadActionTestAction : public Action<DownloadActionTestAction> { | 
|  | 342 | DownloadActionTestAction() : did_run_(false) {} | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 343 | typedef InstallPlan InputObjectType; | 
|  | 344 | typedef InstallPlan OutputObjectType; | 
|  | 345 | ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); } | 
|  | 346 | ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 347 | ActionProcessor* processor() { return processor_; } | 
|  | 348 | void PerformAction() { | 
|  | 349 | did_run_ = true; | 
|  | 350 | ASSERT_TRUE(HasInputObject()); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 351 | EXPECT_TRUE(expected_input_object_ == GetInputObject()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 352 | ASSERT_TRUE(processor()); | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 353 | processor()->ActionComplete(this, kActionCodeSuccess); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 354 | } | 
|  | 355 | string Type() const { return "DownloadActionTestAction"; } | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 356 | InstallPlan expected_input_object_; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 357 | bool did_run_; | 
|  | 358 | }; | 
|  | 359 |  | 
|  | 360 | namespace { | 
|  | 361 | // This class is an ActionProcessorDelegate that simply terminates the | 
|  | 362 | // run loop when the ActionProcessor has completed processing. It's used | 
|  | 363 | // only by the test PassObjectOutTest. | 
|  | 364 | class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate { | 
|  | 365 | public: | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 366 | void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 367 | ASSERT_TRUE(loop_); | 
|  | 368 | g_main_loop_quit(loop_); | 
|  | 369 | } | 
|  | 370 | GMainLoop *loop_; | 
|  | 371 | }; | 
|  | 372 |  | 
|  | 373 | gboolean PassObjectOutTestStarter(gpointer data) { | 
|  | 374 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 
|  | 375 | processor->StartProcessing(); | 
|  | 376 | return FALSE; | 
|  | 377 | } | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | TEST(DownloadActionTest, PassObjectOutTest) { | 
|  | 381 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 
|  | 382 |  | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 383 | DirectFileWriter writer; | 
|  | 384 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 385 | // takes ownership of passed in HttpFetcher | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 386 | InstallPlan install_plan(true, | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 387 | false, | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 388 | "", | 
| Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame] | 389 | 1, | 
| Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 390 | OmahaHashCalculator::OmahaHashOfString("x"), | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 391 | "/dev/null", | 
| Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 392 | "/dev/null"); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 393 | ObjectFeederAction<InstallPlan> feeder_action; | 
|  | 394 | feeder_action.set_obj(install_plan); | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 395 | PrefsMock prefs; | 
|  | 396 | DownloadAction download_action(&prefs, new MockHttpFetcher("x", 1)); | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 397 | download_action.SetTestFileWriter(&writer); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 398 |  | 
|  | 399 | DownloadActionTestAction test_action; | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 400 | test_action.expected_input_object_ = install_plan; | 
|  | 401 | BondActions(&feeder_action, &download_action); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 402 | BondActions(&download_action, &test_action); | 
|  | 403 |  | 
|  | 404 | ActionProcessor processor; | 
|  | 405 | PassObjectOutTestProcessorDelegate delegate; | 
|  | 406 | delegate.loop_ = loop; | 
|  | 407 | processor.set_delegate(&delegate); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 408 | processor.EnqueueAction(&feeder_action); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 409 | processor.EnqueueAction(&download_action); | 
|  | 410 | processor.EnqueueAction(&test_action); | 
|  | 411 |  | 
|  | 412 | g_timeout_add(0, &PassObjectOutTestStarter, &processor); | 
|  | 413 | g_main_loop_run(loop); | 
|  | 414 | g_main_loop_unref(loop); | 
|  | 415 |  | 
|  | 416 | EXPECT_EQ(true, test_action.did_run_); | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | TEST(DownloadActionTest, BadOutFileTest) { | 
|  | 420 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 
|  | 421 |  | 
|  | 422 | 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] | 423 | DirectFileWriter writer; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 424 |  | 
|  | 425 | // takes ownership of passed in HttpFetcher | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 426 | InstallPlan install_plan(true, false, "", 0, "", path, ""); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 427 | ObjectFeederAction<InstallPlan> feeder_action; | 
|  | 428 | feeder_action.set_obj(install_plan); | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 429 | PrefsMock prefs; | 
|  | 430 | DownloadAction download_action(&prefs, new MockHttpFetcher("x", 1)); | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 431 | download_action.SetTestFileWriter(&writer); | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 432 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 433 | BondActions(&feeder_action, &download_action); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 434 |  | 
|  | 435 | ActionProcessor processor; | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 436 | processor.EnqueueAction(&feeder_action); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 437 | processor.EnqueueAction(&download_action); | 
|  | 438 | processor.StartProcessing(); | 
|  | 439 | ASSERT_FALSE(processor.IsRunning()); | 
|  | 440 |  | 
|  | 441 | g_main_loop_unref(loop); | 
|  | 442 | } | 
|  | 443 |  | 
|  | 444 | }  // namespace chromeos_update_engine |