| 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 | 7 |
| 8 #include <glib.h> | 8 #include <glib.h> |
| 9 #include <gmock/gmock.h> | 9 #include <gmock/gmock.h> |
| 10 #include <gtest/gtest.h> | 10 #include <gtest/gtest.h> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 expected_code_(expected_code) {} | 42 expected_code_(expected_code) {} |
| 43 virtual ~DownloadActionTestProcessorDelegate() { | 43 virtual ~DownloadActionTestProcessorDelegate() { |
| 44 EXPECT_TRUE(processing_done_called_); | 44 EXPECT_TRUE(processing_done_called_); |
| 45 } | 45 } |
| 46 virtual void ProcessingDone(const ActionProcessor* processor, | 46 virtual void ProcessingDone(const ActionProcessor* processor, |
| 47 ActionExitCode code) { | 47 ActionExitCode code) { |
| 48 ASSERT_TRUE(loop_); | 48 ASSERT_TRUE(loop_); |
| 49 g_main_loop_quit(loop_); | 49 g_main_loop_quit(loop_); |
| 50 vector<char> found_data; | 50 vector<char> found_data; |
| 51 ASSERT_TRUE(utils::ReadFile(path_, &found_data)); | 51 ASSERT_TRUE(utils::ReadFile(path_, &found_data)); |
| 52 ASSERT_EQ(expected_data_.size(), found_data.size()); | 52 if (expected_code_ != kActionCodeDownloadWriteError) { |
| 53 for (unsigned i = 0; i < expected_data_.size(); i++) { | 53 ASSERT_EQ(expected_data_.size(), found_data.size()); |
| 54 EXPECT_EQ(expected_data_[i], found_data[i]); | 54 for (unsigned i = 0; i < expected_data_.size(); i++) { |
| 55 EXPECT_EQ(expected_data_[i], found_data[i]); |
| 56 } |
| 55 } | 57 } |
| 56 processing_done_called_ = true; | 58 processing_done_called_ = true; |
| 57 } | 59 } |
| 58 | 60 |
| 59 virtual void ActionCompleted(ActionProcessor* processor, | 61 virtual void ActionCompleted(ActionProcessor* processor, |
| 60 AbstractAction* action, | 62 AbstractAction* action, |
| 61 ActionExitCode code) { | 63 ActionExitCode code) { |
| 62 const string type = action->Type(); | 64 const string type = action->Type(); |
| 63 if (type == DownloadAction::StaticType()) { | 65 if (type == DownloadAction::StaticType()) { |
| 64 EXPECT_EQ(expected_code_, code); | 66 EXPECT_EQ(expected_code_, code); |
| 65 } else { | 67 } else { |
| 66 EXPECT_EQ(kActionCodeSuccess, code); | 68 EXPECT_EQ(kActionCodeSuccess, code); |
| 67 } | 69 } |
| 68 } | 70 } |
| 69 | 71 |
| 70 GMainLoop *loop_; | 72 GMainLoop *loop_; |
| 71 string path_; | 73 string path_; |
| 72 vector<char> expected_data_; | 74 vector<char> expected_data_; |
| 73 bool processing_done_called_; | 75 bool processing_done_called_; |
| 74 ActionExitCode expected_code_; | 76 ActionExitCode expected_code_; |
| 75 }; | 77 }; |
| 76 | 78 |
| 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 |
| 77 struct EntryPointArgs { | 97 struct EntryPointArgs { |
| 78 const vector<char> *data; | 98 const vector<char> *data; |
| 79 GMainLoop *loop; | 99 GMainLoop *loop; |
| 80 ActionProcessor *processor; | 100 ActionProcessor *processor; |
| 81 }; | 101 }; |
| 82 | 102 |
| 83 struct StartProcessorInRunLoopArgs { | 103 struct StartProcessorInRunLoopArgs { |
| 84 ActionProcessor* processor; | 104 ActionProcessor* processor; |
| 85 MockHttpFetcher* http_fetcher; | 105 MockHttpFetcher* http_fetcher; |
| 86 }; | 106 }; |
| 87 | 107 |
| 88 gboolean StartProcessorInRunLoop(gpointer data) { | 108 gboolean StartProcessorInRunLoop(gpointer data) { |
| 89 ActionProcessor* processor = | 109 ActionProcessor* processor = |
| 90 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor; | 110 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor; |
| 91 processor->StartProcessing(); | 111 processor->StartProcessing(); |
| 92 MockHttpFetcher* http_fetcher = | 112 MockHttpFetcher* http_fetcher = |
| 93 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher; | 113 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher; |
| 94 http_fetcher->SetOffset(1); | 114 http_fetcher->SetOffset(1); |
| 95 return FALSE; | 115 return FALSE; |
| 96 } | 116 } |
| 97 | 117 |
| 98 void TestWithData(const vector<char>& data, | 118 void TestWithData(const vector<char>& data, |
| 99 bool hash_test, | 119 bool hash_test, |
| 100 bool size_test, | 120 bool size_test, |
| 121 int fail_write, |
| 101 bool use_download_delegate) { | 122 bool use_download_delegate) { |
| 102 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 123 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 103 | 124 |
| 104 // TODO(adlr): see if we need a different file for build bots | 125 // TODO(adlr): see if we need a different file for build bots |
| 105 ScopedTempFile output_temp_file; | 126 ScopedTempFile output_temp_file; |
| 106 DirectFileWriter writer; | 127 TestDirectFileWriter writer; |
| 128 writer.set_fail_write(fail_write); |
| 107 | 129 |
| 108 // We pull off the first byte from data and seek past it. | 130 // We pull off the first byte from data and seek past it. |
| 109 | 131 |
| 110 string hash = hash_test ? | 132 string hash = hash_test ? |
| 111 OmahaHashCalculator::OmahaHashOfString("random string") : | 133 OmahaHashCalculator::OmahaHashOfString("random string") : |
| 112 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1); | 134 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1); |
| 113 uint64_t size = data.size() + (size_test ? 1 : 0); | 135 uint64_t size = data.size() + (size_test ? 1 : 0); |
| 114 InstallPlan install_plan(true, | 136 InstallPlan install_plan(true, |
| 115 false, | 137 false, |
| 116 "", | 138 "", |
| (...skipping 10 matching lines...) Expand all Loading... |
| 127 download_action.SetTestFileWriter(&writer); | 149 download_action.SetTestFileWriter(&writer); |
| 128 BondActions(&feeder_action, &download_action); | 150 BondActions(&feeder_action, &download_action); |
| 129 DownloadActionDelegateMock download_delegate; | 151 DownloadActionDelegateMock download_delegate; |
| 130 if (use_download_delegate) { | 152 if (use_download_delegate) { |
| 131 InSequence s; | 153 InSequence s; |
| 132 download_action.set_delegate(&download_delegate); | 154 download_action.set_delegate(&download_delegate); |
| 133 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); | 155 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); |
| 134 if (data.size() > kMockHttpFetcherChunkSize) | 156 if (data.size() > kMockHttpFetcherChunkSize) |
| 135 EXPECT_CALL(download_delegate, | 157 EXPECT_CALL(download_delegate, |
| 136 BytesReceived(1 + kMockHttpFetcherChunkSize, _)); | 158 BytesReceived(1 + kMockHttpFetcherChunkSize, _)); |
| 137 | 159 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); |
| 138 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); | |
| 139 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); | 160 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); |
| 140 } | 161 } |
| 141 ActionExitCode expected_code = kActionCodeSuccess; | 162 ActionExitCode expected_code = kActionCodeSuccess; |
| 142 if (hash_test) | 163 if (hash_test) |
| 143 expected_code = kActionCodeDownloadHashMismatchError; | 164 expected_code = kActionCodeDownloadHashMismatchError; |
| 144 else if (size_test) | 165 else if (size_test) |
| 145 expected_code = kActionCodeDownloadSizeMismatchError; | 166 expected_code = kActionCodeDownloadSizeMismatchError; |
| 167 else if (fail_write > 0) |
| 168 expected_code = kActionCodeDownloadWriteError; |
| 146 DownloadActionTestProcessorDelegate delegate(expected_code); | 169 DownloadActionTestProcessorDelegate delegate(expected_code); |
| 147 delegate.loop_ = loop; | 170 delegate.loop_ = loop; |
| 148 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end()); | 171 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end()); |
| 149 delegate.path_ = output_temp_file.GetPath(); | 172 delegate.path_ = output_temp_file.GetPath(); |
| 150 ActionProcessor processor; | 173 ActionProcessor processor; |
| 151 processor.set_delegate(&delegate); | 174 processor.set_delegate(&delegate); |
| 152 processor.EnqueueAction(&feeder_action); | 175 processor.EnqueueAction(&feeder_action); |
| 153 processor.EnqueueAction(&download_action); | 176 processor.EnqueueAction(&download_action); |
| 154 | 177 |
| 155 StartProcessorInRunLoopArgs args; | 178 StartProcessorInRunLoopArgs args; |
| 156 args.processor = &processor; | 179 args.processor = &processor; |
| 157 args.http_fetcher = http_fetcher; | 180 args.http_fetcher = http_fetcher; |
| 158 g_timeout_add(0, &StartProcessorInRunLoop, &args); | 181 g_timeout_add(0, &StartProcessorInRunLoop, &args); |
| 159 g_main_loop_run(loop); | 182 g_main_loop_run(loop); |
| 160 g_main_loop_unref(loop); | 183 g_main_loop_unref(loop); |
| 161 } | 184 } |
| 162 } // namespace {} | 185 } // namespace {} |
| 163 | 186 |
| 164 TEST(DownloadActionTest, SimpleTest) { | 187 TEST(DownloadActionTest, SimpleTest) { |
| 165 vector<char> small; | 188 vector<char> small; |
| 166 const char* foo = "foo"; | 189 const char* foo = "foo"; |
| 167 small.insert(small.end(), foo, foo + strlen(foo)); | 190 small.insert(small.end(), foo, foo + strlen(foo)); |
| 168 TestWithData(small, | 191 TestWithData(small, |
| 169 false, // hash_test | 192 false, // hash_test |
| 170 false, // size_test | 193 false, // size_test |
| 194 0, // fail_write |
| 171 true); // use_download_delegate | 195 true); // use_download_delegate |
| 172 } | 196 } |
| 173 | 197 |
| 174 TEST(DownloadActionTest, LargeTest) { | 198 TEST(DownloadActionTest, LargeTest) { |
| 175 vector<char> big(5 * kMockHttpFetcherChunkSize); | 199 vector<char> big(5 * kMockHttpFetcherChunkSize); |
| 176 char c = '0'; | 200 char c = '0'; |
| 177 for (unsigned int i = 0; i < big.size(); i++) { | 201 for (unsigned int i = 0; i < big.size(); i++) { |
| 178 big[i] = c; | 202 big[i] = c; |
| 179 if ('9' == c) | 203 c = ('9' == c) ? '0' : c + 1; |
| 180 c = '0'; | |
| 181 else | |
| 182 c++; | |
| 183 } | 204 } |
| 184 TestWithData(big, | 205 TestWithData(big, |
| 185 false, // hash_test | 206 false, // hash_test |
| 186 false, // size_test | 207 false, // size_test |
| 208 0, // fail_write |
| 187 true); // use_download_delegate | 209 true); // use_download_delegate |
| 188 } | 210 } |
| 189 | 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 |
| 223 true); // use_download_delegate |
| 224 } |
| 225 |
| 190 TEST(DownloadActionTest, BadHashTest) { | 226 TEST(DownloadActionTest, BadHashTest) { |
| 191 vector<char> small; | 227 vector<char> small; |
| 192 const char* foo = "foo"; | 228 const char* foo = "foo"; |
| 193 small.insert(small.end(), foo, foo + strlen(foo)); | 229 small.insert(small.end(), foo, foo + strlen(foo)); |
| 194 TestWithData(small, | 230 TestWithData(small, |
| 195 true, // hash_test | 231 true, // hash_test |
| 196 false, // size_test | 232 false, // size_test |
| 233 0, // fail_write |
| 197 true); // use_download_delegate | 234 true); // use_download_delegate |
| 198 } | 235 } |
| 199 | 236 |
| 200 TEST(DownloadActionTest, BadSizeTest) { | 237 TEST(DownloadActionTest, BadSizeTest) { |
| 201 const char* something = "something"; | 238 const char* something = "something"; |
| 202 vector<char> small(something, something + strlen(something)); | 239 vector<char> small(something, something + strlen(something)); |
| 203 TestWithData(small, | 240 TestWithData(small, |
| 204 false, // hash_test | 241 false, // hash_test |
| 205 true, // size_test | 242 true, // size_test |
| 243 0, // fail_write |
| 206 true); // use_download_delegate | 244 true); // use_download_delegate |
| 207 } | 245 } |
| 208 | 246 |
| 209 TEST(DownloadActionTest, NoDownloadDelegateTest) { | 247 TEST(DownloadActionTest, NoDownloadDelegateTest) { |
| 210 vector<char> small; | 248 vector<char> small; |
| 211 const char* foo = "foofoo"; | 249 const char* foo = "foofoo"; |
| 212 small.insert(small.end(), foo, foo + strlen(foo)); | 250 small.insert(small.end(), foo, foo + strlen(foo)); |
| 213 TestWithData(small, | 251 TestWithData(small, |
| 214 false, // hash_test | 252 false, // hash_test |
| 215 false, // size_test | 253 false, // size_test |
| 254 0, // fail_write |
| 216 false); // use_download_delegate | 255 false); // use_download_delegate |
| 217 } | 256 } |
| 218 | 257 |
| 219 namespace { | 258 namespace { |
| 220 class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { | 259 class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 221 public: | 260 public: |
| 222 void ProcessingStopped(const ActionProcessor* processor) { | 261 void ProcessingStopped(const ActionProcessor* processor) { |
| 223 ASSERT_TRUE(loop_); | 262 ASSERT_TRUE(loop_); |
| 224 g_main_loop_quit(loop_); | 263 g_main_loop_quit(loop_); |
| 225 } | 264 } |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 ActionProcessor processor; | 435 ActionProcessor processor; |
| 397 processor.EnqueueAction(&feeder_action); | 436 processor.EnqueueAction(&feeder_action); |
| 398 processor.EnqueueAction(&download_action); | 437 processor.EnqueueAction(&download_action); |
| 399 processor.StartProcessing(); | 438 processor.StartProcessing(); |
| 400 ASSERT_FALSE(processor.IsRunning()); | 439 ASSERT_FALSE(processor.IsRunning()); |
| 401 | 440 |
| 402 g_main_loop_unref(loop); | 441 g_main_loop_unref(loop); |
| 403 } | 442 } |
| 404 | 443 |
| 405 } // namespace chromeos_update_engine | 444 } // namespace chromeos_update_engine |
| OLD | NEW |