| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 #include <glib.h> | 7 #include <glib.h> |
| 8 #include <gmock/gmock.h> | 8 #include <gmock/gmock.h> |
| 9 #include <gtest/gtest.h> | 9 #include <gtest/gtest.h> |
| 10 #include "update_engine/action_pipe.h" | 10 #include "update_engine/action_pipe.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 gboolean StartProcessorInRunLoop(gpointer data) { | 80 gboolean StartProcessorInRunLoop(gpointer data) { |
| 81 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 81 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 82 processor->StartProcessing(); | 82 processor->StartProcessing(); |
| 83 return FALSE; | 83 return FALSE; |
| 84 } | 84 } |
| 85 | 85 |
| 86 void TestWithData(const vector<char>& data, | 86 void TestWithData(const vector<char>& data, |
| 87 bool hash_test, | 87 bool hash_test, |
| 88 bool size_test, |
| 88 bool use_download_delegate) { | 89 bool use_download_delegate) { |
| 89 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 90 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 90 | 91 |
| 91 // TODO(adlr): see if we need a different file for build bots | 92 // TODO(adlr): see if we need a different file for build bots |
| 92 ScopedTempFile output_temp_file; | 93 ScopedTempFile output_temp_file; |
| 93 DirectFileWriter writer; | 94 DirectFileWriter writer; |
| 94 | 95 |
| 95 // takes ownership of passed in HttpFetcher | 96 // takes ownership of passed in HttpFetcher |
| 96 string hash = hash_test ? | 97 string hash = hash_test ? |
| 97 OmahaHashCalculator::OmahaHashOfString("random string") : | 98 OmahaHashCalculator::OmahaHashOfString("random string") : |
| 98 OmahaHashCalculator::OmahaHashOfData(data); | 99 OmahaHashCalculator::OmahaHashOfData(data); |
| 100 uint64_t size = data.size() + (size_test ? 1 : 0); |
| 99 InstallPlan install_plan(true, | 101 InstallPlan install_plan(true, |
| 100 "", | 102 "", |
| 101 0, | 103 size, |
| 102 hash, | 104 hash, |
| 103 output_temp_file.GetPath(), | 105 output_temp_file.GetPath(), |
| 104 ""); | 106 ""); |
| 105 ObjectFeederAction<InstallPlan> feeder_action; | 107 ObjectFeederAction<InstallPlan> feeder_action; |
| 106 feeder_action.set_obj(install_plan); | 108 feeder_action.set_obj(install_plan); |
| 107 DownloadAction download_action(new MockHttpFetcher(&data[0], | 109 DownloadAction download_action(new MockHttpFetcher(&data[0], |
| 108 data.size())); | 110 data.size())); |
| 109 download_action.SetTestFileWriter(&writer); | 111 download_action.SetTestFileWriter(&writer); |
| 110 BondActions(&feeder_action, &download_action); | 112 BondActions(&feeder_action, &download_action); |
| 111 DownloadActionDelegateMock download_delegate; | 113 DownloadActionDelegateMock download_delegate; |
| 112 if (use_download_delegate) { | 114 if (use_download_delegate) { |
| 113 InSequence s; | 115 InSequence s; |
| 114 download_action.set_delegate(&download_delegate); | 116 download_action.set_delegate(&download_delegate); |
| 115 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); | 117 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); |
| 116 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); | 118 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); |
| 117 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); | 119 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); |
| 118 } | 120 } |
| 119 DownloadActionTestProcessorDelegate delegate( | 121 ActionExitCode expected_code = kActionCodeSuccess; |
| 120 hash_test ? kActionCodeDownloadHashMismatchError : kActionCodeSuccess); | 122 if (hash_test) |
| 123 expected_code = kActionCodeDownloadHashMismatchError; |
| 124 else if (size_test) |
| 125 expected_code = kActionCodeDownloadSizeMismatchError; |
| 126 DownloadActionTestProcessorDelegate delegate(expected_code); |
| 121 delegate.loop_ = loop; | 127 delegate.loop_ = loop; |
| 122 delegate.expected_data_ = data; | 128 delegate.expected_data_ = data; |
| 123 delegate.path_ = output_temp_file.GetPath(); | 129 delegate.path_ = output_temp_file.GetPath(); |
| 124 ActionProcessor processor; | 130 ActionProcessor processor; |
| 125 processor.set_delegate(&delegate); | 131 processor.set_delegate(&delegate); |
| 126 processor.EnqueueAction(&feeder_action); | 132 processor.EnqueueAction(&feeder_action); |
| 127 processor.EnqueueAction(&download_action); | 133 processor.EnqueueAction(&download_action); |
| 128 | 134 |
| 129 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 135 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 130 g_main_loop_run(loop); | 136 g_main_loop_run(loop); |
| 131 g_main_loop_unref(loop); | 137 g_main_loop_unref(loop); |
| 132 } | 138 } |
| 133 } // namespace {} | 139 } // namespace {} |
| 134 | 140 |
| 135 TEST(DownloadActionTest, SimpleTest) { | 141 TEST(DownloadActionTest, SimpleTest) { |
| 136 vector<char> small; | 142 vector<char> small; |
| 137 const char* foo = "foo"; | 143 const char* foo = "foo"; |
| 138 small.insert(small.end(), foo, foo + strlen(foo)); | 144 small.insert(small.end(), foo, foo + strlen(foo)); |
| 139 TestWithData(small, false, true); | 145 TestWithData(small, |
| 146 false, // hash_test |
| 147 false, // size_test |
| 148 true); // use_download_delegate |
| 140 } | 149 } |
| 141 | 150 |
| 142 TEST(DownloadActionTest, LargeTest) { | 151 TEST(DownloadActionTest, LargeTest) { |
| 143 vector<char> big(5 * kMockHttpFetcherChunkSize); | 152 vector<char> big(5 * kMockHttpFetcherChunkSize); |
| 144 char c = '0'; | 153 char c = '0'; |
| 145 for (unsigned int i = 0; i < big.size(); i++) { | 154 for (unsigned int i = 0; i < big.size(); i++) { |
| 146 big[i] = c; | 155 big[i] = c; |
| 147 if ('9' == c) | 156 if ('9' == c) |
| 148 c = '0'; | 157 c = '0'; |
| 149 else | 158 else |
| 150 c++; | 159 c++; |
| 151 } | 160 } |
| 152 TestWithData(big, false, true); | 161 TestWithData(big, |
| 162 false, // hash_test |
| 163 false, // size_test |
| 164 true); // use_download_delegate |
| 153 } | 165 } |
| 154 | 166 |
| 155 TEST(DownloadActionTest, BadHashTest) { | 167 TEST(DownloadActionTest, BadHashTest) { |
| 156 vector<char> small; | 168 vector<char> small; |
| 157 const char* foo = "foo"; | 169 const char* foo = "foo"; |
| 158 small.insert(small.end(), foo, foo + strlen(foo)); | 170 small.insert(small.end(), foo, foo + strlen(foo)); |
| 159 TestWithData(small, true, true); | 171 TestWithData(small, |
| 172 true, // hash_test |
| 173 false, // size_test |
| 174 true); // use_download_delegate |
| 175 } |
| 176 |
| 177 TEST(DownloadActionTest, BadSizeTest) { |
| 178 const char* something = "something"; |
| 179 vector<char> small(something, something + strlen(something)); |
| 180 TestWithData(small, |
| 181 false, // hash_test |
| 182 true, // size_test |
| 183 true); // use_download_delegate |
| 160 } | 184 } |
| 161 | 185 |
| 162 TEST(DownloadActionTest, NoDownloadDelegateTest) { | 186 TEST(DownloadActionTest, NoDownloadDelegateTest) { |
| 163 vector<char> small; | 187 vector<char> small; |
| 164 const char* foo = "foofoo"; | 188 const char* foo = "foofoo"; |
| 165 small.insert(small.end(), foo, foo + strlen(foo)); | 189 small.insert(small.end(), foo, foo + strlen(foo)); |
| 166 TestWithData(small, false, false); | 190 TestWithData(small, |
| 191 false, // hash_test |
| 192 false, // size_test |
| 193 false); // use_download_delegate |
| 167 } | 194 } |
| 168 | 195 |
| 169 namespace { | 196 namespace { |
| 170 class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { | 197 class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 171 public: | 198 public: |
| 172 void ProcessingStopped(const ActionProcessor* processor) { | 199 void ProcessingStopped(const ActionProcessor* processor) { |
| 173 ASSERT_TRUE(loop_); | 200 ASSERT_TRUE(loop_); |
| 174 g_main_loop_quit(loop_); | 201 g_main_loop_quit(loop_); |
| 175 } | 202 } |
| 176 GMainLoop *loop_; | 203 GMainLoop *loop_; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 } | 314 } |
| 288 | 315 |
| 289 TEST(DownloadActionTest, PassObjectOutTest) { | 316 TEST(DownloadActionTest, PassObjectOutTest) { |
| 290 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 317 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 291 | 318 |
| 292 DirectFileWriter writer; | 319 DirectFileWriter writer; |
| 293 | 320 |
| 294 // takes ownership of passed in HttpFetcher | 321 // takes ownership of passed in HttpFetcher |
| 295 InstallPlan install_plan(true, | 322 InstallPlan install_plan(true, |
| 296 "", | 323 "", |
| 297 0, | 324 1, |
| 298 OmahaHashCalculator::OmahaHashOfString("x"), | 325 OmahaHashCalculator::OmahaHashOfString("x"), |
| 299 "/dev/null", | 326 "/dev/null", |
| 300 "/dev/null"); | 327 "/dev/null"); |
| 301 ObjectFeederAction<InstallPlan> feeder_action; | 328 ObjectFeederAction<InstallPlan> feeder_action; |
| 302 feeder_action.set_obj(install_plan); | 329 feeder_action.set_obj(install_plan); |
| 303 DownloadAction download_action(new MockHttpFetcher("x", 1)); | 330 DownloadAction download_action(new MockHttpFetcher("x", 1)); |
| 304 download_action.SetTestFileWriter(&writer); | 331 download_action.SetTestFileWriter(&writer); |
| 305 | 332 |
| 306 DownloadActionTestAction test_action; | 333 DownloadActionTestAction test_action; |
| 307 test_action.expected_input_object_ = install_plan; | 334 test_action.expected_input_object_ = install_plan; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 ActionProcessor processor; | 368 ActionProcessor processor; |
| 342 processor.EnqueueAction(&feeder_action); | 369 processor.EnqueueAction(&feeder_action); |
| 343 processor.EnqueueAction(&download_action); | 370 processor.EnqueueAction(&download_action); |
| 344 processor.StartProcessing(); | 371 processor.StartProcessing(); |
| 345 ASSERT_FALSE(processor.IsRunning()); | 372 ASSERT_FALSE(processor.IsRunning()); |
| 346 | 373 |
| 347 g_main_loop_unref(loop); | 374 g_main_loop_unref(loop); |
| 348 } | 375 } |
| 349 | 376 |
| 350 } // namespace chromeos_update_engine | 377 } // namespace chromeos_update_engine |
| OLD | NEW |