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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 bool processing_done_called_; | 73 bool processing_done_called_; |
74 ActionExitCode expected_code_; | 74 ActionExitCode expected_code_; |
75 }; | 75 }; |
76 | 76 |
77 struct EntryPointArgs { | 77 struct EntryPointArgs { |
78 const vector<char> *data; | 78 const vector<char> *data; |
79 GMainLoop *loop; | 79 GMainLoop *loop; |
80 ActionProcessor *processor; | 80 ActionProcessor *processor; |
81 }; | 81 }; |
82 | 82 |
| 83 struct StartProcessorInRunLoopArgs { |
| 84 ActionProcessor* processor; |
| 85 MockHttpFetcher* http_fetcher; |
| 86 }; |
| 87 |
83 gboolean StartProcessorInRunLoop(gpointer data) { | 88 gboolean StartProcessorInRunLoop(gpointer data) { |
84 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 89 ActionProcessor* processor = |
| 90 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->processor; |
85 processor->StartProcessing(); | 91 processor->StartProcessing(); |
| 92 MockHttpFetcher* http_fetcher = |
| 93 reinterpret_cast<StartProcessorInRunLoopArgs*>(data)->http_fetcher; |
| 94 http_fetcher->SetOffset(1); |
86 return FALSE; | 95 return FALSE; |
87 } | 96 } |
88 | 97 |
89 void TestWithData(const vector<char>& data, | 98 void TestWithData(const vector<char>& data, |
90 bool hash_test, | 99 bool hash_test, |
91 bool size_test, | 100 bool size_test, |
92 bool use_download_delegate) { | 101 bool use_download_delegate) { |
93 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 102 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
94 | 103 |
95 // TODO(adlr): see if we need a different file for build bots | 104 // TODO(adlr): see if we need a different file for build bots |
96 ScopedTempFile output_temp_file; | 105 ScopedTempFile output_temp_file; |
97 DirectFileWriter writer; | 106 DirectFileWriter writer; |
98 | 107 |
99 // takes ownership of passed in HttpFetcher | 108 // We pull off the first byte from data and seek past it. |
| 109 |
100 string hash = hash_test ? | 110 string hash = hash_test ? |
101 OmahaHashCalculator::OmahaHashOfString("random string") : | 111 OmahaHashCalculator::OmahaHashOfString("random string") : |
102 OmahaHashCalculator::OmahaHashOfData(data); | 112 OmahaHashCalculator::OmahaHashOfBytes(&data[1], data.size() - 1); |
103 uint64_t size = data.size() + (size_test ? 1 : 0); | 113 uint64_t size = data.size() + (size_test ? 1 : 0); |
104 InstallPlan install_plan(true, | 114 InstallPlan install_plan(true, |
105 false, | 115 false, |
106 "", | 116 "", |
107 size, | 117 size, |
108 hash, | 118 hash, |
109 output_temp_file.GetPath(), | 119 output_temp_file.GetPath(), |
110 ""); | 120 ""); |
111 ObjectFeederAction<InstallPlan> feeder_action; | 121 ObjectFeederAction<InstallPlan> feeder_action; |
112 feeder_action.set_obj(install_plan); | 122 feeder_action.set_obj(install_plan); |
113 PrefsMock prefs; | 123 PrefsMock prefs; |
114 DownloadAction download_action(&prefs, new MockHttpFetcher(&data[0], | 124 MockHttpFetcher* http_fetcher = new MockHttpFetcher(&data[0], data.size()); |
115 data.size())); | 125 // takes ownership of passed in HttpFetcher |
| 126 DownloadAction download_action(&prefs, http_fetcher); |
116 download_action.SetTestFileWriter(&writer); | 127 download_action.SetTestFileWriter(&writer); |
117 BondActions(&feeder_action, &download_action); | 128 BondActions(&feeder_action, &download_action); |
118 DownloadActionDelegateMock download_delegate; | 129 DownloadActionDelegateMock download_delegate; |
119 if (use_download_delegate) { | 130 if (use_download_delegate) { |
120 InSequence s; | 131 InSequence s; |
121 download_action.set_delegate(&download_delegate); | 132 download_action.set_delegate(&download_delegate); |
122 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); | 133 EXPECT_CALL(download_delegate, SetDownloadStatus(true)).Times(1); |
123 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); | 134 if (data.size() > kMockHttpFetcherChunkSize) |
| 135 EXPECT_CALL(download_delegate, |
| 136 BytesReceived(1 + kMockHttpFetcherChunkSize, _)); |
| 137 |
| 138 EXPECT_CALL(download_delegate, BytesReceived(_, _)).Times(AtLeast(1)); |
124 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); | 139 EXPECT_CALL(download_delegate, SetDownloadStatus(false)).Times(1); |
125 } | 140 } |
126 ActionExitCode expected_code = kActionCodeSuccess; | 141 ActionExitCode expected_code = kActionCodeSuccess; |
127 if (hash_test) | 142 if (hash_test) |
128 expected_code = kActionCodeDownloadHashMismatchError; | 143 expected_code = kActionCodeDownloadHashMismatchError; |
129 else if (size_test) | 144 else if (size_test) |
130 expected_code = kActionCodeDownloadSizeMismatchError; | 145 expected_code = kActionCodeDownloadSizeMismatchError; |
131 DownloadActionTestProcessorDelegate delegate(expected_code); | 146 DownloadActionTestProcessorDelegate delegate(expected_code); |
132 delegate.loop_ = loop; | 147 delegate.loop_ = loop; |
133 delegate.expected_data_ = data; | 148 delegate.expected_data_ = vector<char>(data.begin() + 1, data.end()); |
134 delegate.path_ = output_temp_file.GetPath(); | 149 delegate.path_ = output_temp_file.GetPath(); |
135 ActionProcessor processor; | 150 ActionProcessor processor; |
136 processor.set_delegate(&delegate); | 151 processor.set_delegate(&delegate); |
137 processor.EnqueueAction(&feeder_action); | 152 processor.EnqueueAction(&feeder_action); |
138 processor.EnqueueAction(&download_action); | 153 processor.EnqueueAction(&download_action); |
139 | 154 |
140 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 155 StartProcessorInRunLoopArgs args; |
| 156 args.processor = &processor; |
| 157 args.http_fetcher = http_fetcher; |
| 158 g_timeout_add(0, &StartProcessorInRunLoop, &args); |
141 g_main_loop_run(loop); | 159 g_main_loop_run(loop); |
142 g_main_loop_unref(loop); | 160 g_main_loop_unref(loop); |
143 } | 161 } |
144 } // namespace {} | 162 } // namespace {} |
145 | 163 |
146 TEST(DownloadActionTest, SimpleTest) { | 164 TEST(DownloadActionTest, SimpleTest) { |
147 vector<char> small; | 165 vector<char> small; |
148 const char* foo = "foo"; | 166 const char* foo = "foo"; |
149 small.insert(small.end(), foo, foo + strlen(foo)); | 167 small.insert(small.end(), foo, foo + strlen(foo)); |
150 TestWithData(small, | 168 TestWithData(small, |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 ActionProcessor processor; | 396 ActionProcessor processor; |
379 processor.EnqueueAction(&feeder_action); | 397 processor.EnqueueAction(&feeder_action); |
380 processor.EnqueueAction(&download_action); | 398 processor.EnqueueAction(&download_action); |
381 processor.StartProcessing(); | 399 processor.StartProcessing(); |
382 ASSERT_FALSE(processor.IsRunning()); | 400 ASSERT_FALSE(processor.IsRunning()); |
383 | 401 |
384 g_main_loop_unref(loop); | 402 g_main_loop_unref(loop); |
385 } | 403 } |
386 | 404 |
387 } // namespace chromeos_update_engine | 405 } // namespace chromeos_update_engine |
OLD | NEW |