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 <gtest/gtest.h> | 8 #include <gtest/gtest.h> |
9 #include "update_engine/action_pipe.h" | 9 #include "update_engine/action_pipe.h" |
10 #include "update_engine/download_action.h" | 10 #include "update_engine/download_action.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 GMainLoop *loop; | 58 GMainLoop *loop; |
59 ActionProcessor *processor; | 59 ActionProcessor *processor; |
60 }; | 60 }; |
61 | 61 |
62 gboolean StartProcessorInRunLoop(gpointer data) { | 62 gboolean StartProcessorInRunLoop(gpointer data) { |
63 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 63 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
64 processor->StartProcessing(); | 64 processor->StartProcessing(); |
65 return FALSE; | 65 return FALSE; |
66 } | 66 } |
67 | 67 |
68 void TestWithData(const vector<char>& data, bool compress) { | 68 void TestWithData(const vector<char>& data) { |
69 vector<char> use_data; | |
70 if (compress) { | |
71 use_data = GzipCompressData(data); | |
72 } else { | |
73 use_data = data; | |
74 } | |
75 | |
76 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 69 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
77 | 70 |
78 // TODO(adlr): see if we need a different file for build bots | 71 // TODO(adlr): see if we need a different file for build bots |
79 const string path("/tmp/DownloadActionTest"); | 72 ScopedTempFile output_temp_file; |
| 73 DirectFileWriter writer; |
| 74 |
80 // takes ownership of passed in HttpFetcher | 75 // takes ownership of passed in HttpFetcher |
81 InstallPlan install_plan(compress, "", | 76 InstallPlan install_plan(true, |
82 OmahaHashCalculator::OmahaHashOfData(use_data), | 77 "", |
83 path); | 78 OmahaHashCalculator::OmahaHashOfData(data), |
| 79 output_temp_file.GetPath(), |
| 80 ""); |
84 ObjectFeederAction<InstallPlan> feeder_action; | 81 ObjectFeederAction<InstallPlan> feeder_action; |
85 feeder_action.set_obj(install_plan); | 82 feeder_action.set_obj(install_plan); |
86 DownloadAction download_action(new MockHttpFetcher(&use_data[0], | 83 DownloadAction download_action(new MockHttpFetcher(&data[0], |
87 use_data.size())); | 84 data.size())); |
| 85 download_action.SetTestFileWriter(&writer); |
88 BondActions(&feeder_action, &download_action); | 86 BondActions(&feeder_action, &download_action); |
89 | 87 |
90 DownloadActionTestProcessorDelegate delegate; | 88 DownloadActionTestProcessorDelegate delegate; |
91 delegate.loop_ = loop; | 89 delegate.loop_ = loop; |
92 delegate.expected_data_ = data; | 90 delegate.expected_data_ = data; |
93 delegate.path_ = path; | 91 delegate.path_ = output_temp_file.GetPath(); |
94 ActionProcessor processor; | 92 ActionProcessor processor; |
95 processor.set_delegate(&delegate); | 93 processor.set_delegate(&delegate); |
96 processor.EnqueueAction(&feeder_action); | 94 processor.EnqueueAction(&feeder_action); |
97 processor.EnqueueAction(&download_action); | 95 processor.EnqueueAction(&download_action); |
98 | 96 |
99 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 97 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
100 g_main_loop_run(loop); | 98 g_main_loop_run(loop); |
101 g_main_loop_unref(loop); | 99 g_main_loop_unref(loop); |
102 | |
103 // remove temp file; don't care if there are errors here | |
104 unlink(path.c_str()); | |
105 } | 100 } |
106 } // namespace {} | 101 } // namespace {} |
107 | 102 |
108 TEST(DownloadActionTest, SimpleTest) { | 103 TEST(DownloadActionTest, SimpleTest) { |
109 vector<char> small; | 104 vector<char> small; |
110 const char* foo = "foo"; | 105 const char* foo = "foo"; |
111 small.insert(small.end(), foo, foo + strlen(foo)); | 106 small.insert(small.end(), foo, foo + strlen(foo)); |
112 TestWithData(small, false); | 107 TestWithData(small); |
113 TestWithData(small, true); | |
114 } | 108 } |
115 | 109 |
116 TEST(DownloadActionTest, LargeTest) { | 110 TEST(DownloadActionTest, LargeTest) { |
117 vector<char> big(5 * kMockHttpFetcherChunkSize); | 111 vector<char> big(5 * kMockHttpFetcherChunkSize); |
118 char c = '0'; | 112 char c = '0'; |
119 for (unsigned int i = 0; i < big.size(); i++) { | 113 for (unsigned int i = 0; i < big.size(); i++) { |
120 big[i] = c; | 114 big[i] = c; |
121 if ('9' == c) | 115 if ('9' == c) |
122 c = '0'; | 116 c = '0'; |
123 else | 117 else |
124 c++; | 118 c++; |
125 } | 119 } |
126 TestWithData(big, false); | 120 TestWithData(big); |
127 TestWithData(big, true); | |
128 } | 121 } |
129 | 122 |
130 namespace { | 123 namespace { |
131 class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { | 124 class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
132 public: | 125 public: |
133 void ProcessingStopped(const ActionProcessor* processor) { | 126 void ProcessingStopped(const ActionProcessor* processor) { |
134 ASSERT_TRUE(loop_); | 127 ASSERT_TRUE(loop_); |
135 g_main_loop_quit(loop_); | 128 g_main_loop_quit(loop_); |
136 } | 129 } |
137 GMainLoop *loop_; | 130 GMainLoop *loop_; |
138 }; | 131 }; |
139 | 132 |
140 gboolean TerminateEarlyTestStarter(gpointer data) { | 133 gboolean TerminateEarlyTestStarter(gpointer data) { |
141 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 134 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
142 processor->StartProcessing(); | 135 processor->StartProcessing(); |
143 CHECK(processor->IsRunning()); | 136 CHECK(processor->IsRunning()); |
144 processor->StopProcessing(); | 137 processor->StopProcessing(); |
145 return FALSE; | 138 return FALSE; |
146 } | 139 } |
147 | 140 |
148 } // namespace {} | 141 } // namespace {} |
149 | 142 |
150 TEST(DownloadActionTest, TerminateEarlyTest) { | 143 TEST(DownloadActionTest, TerminateEarlyTest) { |
151 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 144 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
152 | 145 |
153 vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2); | 146 vector<char> data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2); |
154 memset(&data[0], 0, data.size()); | 147 memset(&data[0], 0, data.size()); |
155 | 148 |
156 const string path("/tmp/DownloadActionTest"); | 149 ScopedTempFile temp_file; |
157 { | 150 { |
| 151 DirectFileWriter writer; |
| 152 |
158 // takes ownership of passed in HttpFetcher | 153 // takes ownership of passed in HttpFetcher |
159 ObjectFeederAction<InstallPlan> feeder_action; | 154 ObjectFeederAction<InstallPlan> feeder_action; |
160 InstallPlan install_plan(false, "", "", path); | 155 InstallPlan install_plan(true, "", "", temp_file.GetPath(), ""); |
161 feeder_action.set_obj(install_plan); | 156 feeder_action.set_obj(install_plan); |
162 DownloadAction download_action(new MockHttpFetcher(&data[0], data.size())); | 157 DownloadAction download_action(new MockHttpFetcher(&data[0], data.size())); |
| 158 download_action.SetTestFileWriter(&writer); |
163 TerminateEarlyTestProcessorDelegate delegate; | 159 TerminateEarlyTestProcessorDelegate delegate; |
164 delegate.loop_ = loop; | 160 delegate.loop_ = loop; |
165 ActionProcessor processor; | 161 ActionProcessor processor; |
166 processor.set_delegate(&delegate); | 162 processor.set_delegate(&delegate); |
167 processor.EnqueueAction(&feeder_action); | 163 processor.EnqueueAction(&feeder_action); |
168 processor.EnqueueAction(&download_action); | 164 processor.EnqueueAction(&download_action); |
169 BondActions(&feeder_action, &download_action); | 165 BondActions(&feeder_action, &download_action); |
170 | 166 |
171 g_timeout_add(0, &TerminateEarlyTestStarter, &processor); | 167 g_timeout_add(0, &TerminateEarlyTestStarter, &processor); |
172 g_main_loop_run(loop); | 168 g_main_loop_run(loop); |
173 g_main_loop_unref(loop); | 169 g_main_loop_unref(loop); |
174 } | 170 } |
175 | 171 |
176 // 1 or 0 chunks should have come through | 172 // 1 or 0 chunks should have come through |
177 const off_t resulting_file_size(utils::FileSize(path)); | 173 const off_t resulting_file_size(utils::FileSize(temp_file.GetPath())); |
| 174 EXPECT_GE(resulting_file_size, 0); |
178 if (resulting_file_size != 0) | 175 if (resulting_file_size != 0) |
179 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size); | 176 EXPECT_EQ(kMockHttpFetcherChunkSize, resulting_file_size); |
180 } | 177 } |
181 | 178 |
182 class DownloadActionTestAction; | 179 class DownloadActionTestAction; |
183 | 180 |
184 template<> | 181 template<> |
185 class ActionTraits<DownloadActionTestAction> { | 182 class ActionTraits<DownloadActionTestAction> { |
186 public: | 183 public: |
187 typedef InstallPlan OutputObjectType; | 184 typedef InstallPlan OutputObjectType; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 gboolean PassObjectOutTestStarter(gpointer data) { | 221 gboolean PassObjectOutTestStarter(gpointer data) { |
225 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 222 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
226 processor->StartProcessing(); | 223 processor->StartProcessing(); |
227 return FALSE; | 224 return FALSE; |
228 } | 225 } |
229 } | 226 } |
230 | 227 |
231 TEST(DownloadActionTest, PassObjectOutTest) { | 228 TEST(DownloadActionTest, PassObjectOutTest) { |
232 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 229 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
233 | 230 |
| 231 DirectFileWriter writer; |
| 232 |
234 // takes ownership of passed in HttpFetcher | 233 // takes ownership of passed in HttpFetcher |
235 InstallPlan install_plan(false, "", | 234 InstallPlan install_plan(true, |
| 235 "", |
236 OmahaHashCalculator::OmahaHashOfString("x"), | 236 OmahaHashCalculator::OmahaHashOfString("x"), |
| 237 "/dev/null", |
237 "/dev/null"); | 238 "/dev/null"); |
238 ObjectFeederAction<InstallPlan> feeder_action; | 239 ObjectFeederAction<InstallPlan> feeder_action; |
239 feeder_action.set_obj(install_plan); | 240 feeder_action.set_obj(install_plan); |
240 DownloadAction download_action(new MockHttpFetcher("x", 1)); | 241 DownloadAction download_action(new MockHttpFetcher("x", 1)); |
| 242 download_action.SetTestFileWriter(&writer); |
241 | 243 |
242 DownloadActionTestAction test_action; | 244 DownloadActionTestAction test_action; |
243 test_action.expected_input_object_ = install_plan; | 245 test_action.expected_input_object_ = install_plan; |
244 BondActions(&feeder_action, &download_action); | 246 BondActions(&feeder_action, &download_action); |
245 BondActions(&download_action, &test_action); | 247 BondActions(&download_action, &test_action); |
246 | 248 |
247 ActionProcessor processor; | 249 ActionProcessor processor; |
248 PassObjectOutTestProcessorDelegate delegate; | 250 PassObjectOutTestProcessorDelegate delegate; |
249 delegate.loop_ = loop; | 251 delegate.loop_ = loop; |
250 processor.set_delegate(&delegate); | 252 processor.set_delegate(&delegate); |
251 processor.EnqueueAction(&feeder_action); | 253 processor.EnqueueAction(&feeder_action); |
252 processor.EnqueueAction(&download_action); | 254 processor.EnqueueAction(&download_action); |
253 processor.EnqueueAction(&test_action); | 255 processor.EnqueueAction(&test_action); |
254 | 256 |
255 g_timeout_add(0, &PassObjectOutTestStarter, &processor); | 257 g_timeout_add(0, &PassObjectOutTestStarter, &processor); |
256 g_main_loop_run(loop); | 258 g_main_loop_run(loop); |
257 g_main_loop_unref(loop); | 259 g_main_loop_unref(loop); |
258 | 260 |
259 EXPECT_EQ(true, test_action.did_run_); | 261 EXPECT_EQ(true, test_action.did_run_); |
260 } | 262 } |
261 | 263 |
262 TEST(DownloadActionTest, BadOutFileTest) { | 264 TEST(DownloadActionTest, BadOutFileTest) { |
263 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 265 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
264 | 266 |
265 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs"); | 267 const string path("/fake/path/that/cant/be/created/because/of/missing/dirs"); |
| 268 DirectFileWriter writer; |
266 | 269 |
267 // takes ownership of passed in HttpFetcher | 270 // takes ownership of passed in HttpFetcher |
268 InstallPlan install_plan(false, "", "", path); | 271 InstallPlan install_plan(true, "", "", path, ""); |
269 ObjectFeederAction<InstallPlan> feeder_action; | 272 ObjectFeederAction<InstallPlan> feeder_action; |
270 feeder_action.set_obj(install_plan); | 273 feeder_action.set_obj(install_plan); |
271 DownloadAction download_action(new MockHttpFetcher("x", 1)); | 274 DownloadAction download_action(new MockHttpFetcher("x", 1)); |
| 275 download_action.SetTestFileWriter(&writer); |
| 276 |
272 BondActions(&feeder_action, &download_action); | 277 BondActions(&feeder_action, &download_action); |
273 | 278 |
274 ActionProcessor processor; | 279 ActionProcessor processor; |
275 processor.EnqueueAction(&feeder_action); | 280 processor.EnqueueAction(&feeder_action); |
276 processor.EnqueueAction(&download_action); | 281 processor.EnqueueAction(&download_action); |
277 processor.StartProcessing(); | 282 processor.StartProcessing(); |
278 ASSERT_FALSE(processor.IsRunning()); | 283 ASSERT_FALSE(processor.IsRunning()); |
279 | 284 |
280 g_main_loop_unref(loop); | 285 g_main_loop_unref(loop); |
281 } | 286 } |
282 | 287 |
283 } // namespace chromeos_update_engine | 288 } // namespace chromeos_update_engine |
OLD | NEW |