OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | 9 |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "gtest/gtest.h" | 11 #include "gtest/gtest.h" |
12 #include "update_engine/action_pipe.h" | 12 #include "update_engine/action_pipe.h" |
13 #include "update_engine/mock_http_fetcher.h" | 13 #include "update_engine/mock_http_fetcher.h" |
14 #include "update_engine/omaha_hash_calculator.h" | 14 #include "update_engine/omaha_hash_calculator.h" |
15 #include "update_engine/omaha_request_action.h" | 15 #include "update_engine/omaha_request_action.h" |
| 16 #include "update_engine/omaha_request_params.h" |
16 #include "update_engine/test_utils.h" | 17 #include "update_engine/test_utils.h" |
17 | 18 |
18 using std::string; | 19 using std::string; |
19 using std::vector; | 20 using std::vector; |
20 | 21 |
21 namespace chromeos_update_engine { | 22 namespace chromeos_update_engine { |
22 | 23 |
23 class OmahaRequestActionTest : public ::testing::Test { }; | 24 class OmahaRequestActionTest : public ::testing::Test { }; |
24 | 25 |
25 namespace { | 26 namespace { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // out_post_data may be null; if non-null, the post-data received by the | 123 // out_post_data may be null; if non-null, the post-data received by the |
123 // mock HttpFetcher is returned. | 124 // mock HttpFetcher is returned. |
124 bool TestUpdateCheck(const OmahaRequestParams& params, | 125 bool TestUpdateCheck(const OmahaRequestParams& params, |
125 const string& http_response, | 126 const string& http_response, |
126 bool expected_success, | 127 bool expected_success, |
127 OmahaResponse* out_response, | 128 OmahaResponse* out_response, |
128 vector<char>* out_post_data) { | 129 vector<char>* out_post_data) { |
129 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); | 130 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
130 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), | 131 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
131 http_response.size()); | 132 http_response.size()); |
132 ObjectFeederAction<OmahaRequestParams> feeder_action; | 133 OmahaRequestAction action(params, NULL, fetcher); |
133 OmahaRequestAction action(NULL, fetcher); // takes ownership of fetcher | |
134 OmahaRequestActionTestProcessorDelegate delegate; | 134 OmahaRequestActionTestProcessorDelegate delegate; |
135 delegate.loop_ = loop; | 135 delegate.loop_ = loop; |
136 delegate.expected_success_ = expected_success; | 136 delegate.expected_success_ = expected_success; |
| 137 |
137 ActionProcessor processor; | 138 ActionProcessor processor; |
138 feeder_action.set_obj(params); | |
139 processor.set_delegate(&delegate); | 139 processor.set_delegate(&delegate); |
140 processor.EnqueueAction(&feeder_action); | |
141 processor.EnqueueAction(&action); | 140 processor.EnqueueAction(&action); |
142 | 141 |
143 OutputObjectCollectorAction collector_action; | 142 OutputObjectCollectorAction collector_action; |
144 | |
145 BondActions(&feeder_action, &action); | |
146 BondActions(&action, &collector_action); | 143 BondActions(&action, &collector_action); |
147 processor.EnqueueAction(&collector_action); | 144 processor.EnqueueAction(&collector_action); |
148 | 145 |
149 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 146 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
150 g_main_loop_run(loop); | 147 g_main_loop_run(loop); |
151 g_main_loop_unref(loop); | 148 g_main_loop_unref(loop); |
152 if (collector_action.has_input_object_ && out_response) | 149 if (collector_action.has_input_object_ && out_response) |
153 *out_response = collector_action.omaha_response_; | 150 *out_response = collector_action.omaha_response_; |
154 if (out_post_data) | 151 if (out_post_data) |
155 *out_post_data = fetcher->post_data(); | 152 *out_post_data = fetcher->post_data(); |
156 return collector_action.has_input_object_; | 153 return collector_action.has_input_object_; |
157 } | 154 } |
158 | 155 |
159 // Tests Event requests -- they should always succeed. |out_post_data| | 156 // Tests Event requests -- they should always succeed. |out_post_data| |
160 // may be null; if non-null, the post-data received by the mock | 157 // may be null; if non-null, the post-data received by the mock |
161 // HttpFetcher is returned. | 158 // HttpFetcher is returned. |
162 void TestEvent(const OmahaRequestParams& params, | 159 void TestEvent(const OmahaRequestParams& params, |
163 OmahaEvent* event, | 160 OmahaEvent* event, |
164 const string& http_response, | 161 const string& http_response, |
165 vector<char>* out_post_data) { | 162 vector<char>* out_post_data) { |
166 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); | 163 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
167 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), | 164 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
168 http_response.size()); | 165 http_response.size()); |
169 ObjectFeederAction<OmahaRequestParams> feeder_action; | 166 OmahaRequestAction action(params, event, fetcher); |
170 OmahaRequestAction action(event, fetcher); // takes ownership of fetcher | |
171 OmahaRequestActionTestProcessorDelegate delegate; | 167 OmahaRequestActionTestProcessorDelegate delegate; |
172 delegate.loop_ = loop; | 168 delegate.loop_ = loop; |
173 ActionProcessor processor; | 169 ActionProcessor processor; |
174 feeder_action.set_obj(params); | |
175 processor.set_delegate(&delegate); | 170 processor.set_delegate(&delegate); |
176 processor.EnqueueAction(&feeder_action); | |
177 processor.EnqueueAction(&action); | 171 processor.EnqueueAction(&action); |
178 | 172 |
179 BondActions(&feeder_action, &action); | |
180 | |
181 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 173 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
182 g_main_loop_run(loop); | 174 g_main_loop_run(loop); |
183 g_main_loop_unref(loop); | 175 g_main_loop_unref(loop); |
184 if (out_post_data) | 176 if (out_post_data) |
185 *out_post_data = fetcher->post_data(); | 177 *out_post_data = fetcher->post_data(); |
186 } | 178 } |
187 | 179 |
188 TEST(OmahaRequestActionTest, NoUpdateTest) { | 180 TEST(OmahaRequestActionTest, NoUpdateTest) { |
189 OmahaRequestParams params("", // machine_id | 181 OmahaRequestParams params("", // machine_id |
190 "", // user_id | 182 "", // user_id |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 "", // os_board | 244 "", // os_board |
253 OmahaRequestParams::kAppId, | 245 OmahaRequestParams::kAppId, |
254 "0.1.0.0", | 246 "0.1.0.0", |
255 "en-US", | 247 "en-US", |
256 "unittest", | 248 "unittest", |
257 ""); // url | 249 ""); // url |
258 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId)); | 250 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId)); |
259 | 251 |
260 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 252 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
261 | 253 |
262 ObjectFeederAction<OmahaRequestParams> feeder_action; | 254 OmahaRequestAction action(params, NULL, |
263 feeder_action.set_obj(params); | |
264 OmahaRequestAction action(NULL, | |
265 new MockHttpFetcher(http_response.data(), | 255 new MockHttpFetcher(http_response.data(), |
266 http_response.size())); | 256 http_response.size())); |
267 OmahaRequestActionTestProcessorDelegate delegate; | 257 OmahaRequestActionTestProcessorDelegate delegate; |
268 delegate.loop_ = loop; | 258 delegate.loop_ = loop; |
269 ActionProcessor processor; | 259 ActionProcessor processor; |
270 processor.set_delegate(&delegate); | 260 processor.set_delegate(&delegate); |
271 processor.EnqueueAction(&feeder_action); | |
272 processor.EnqueueAction(&action); | 261 processor.EnqueueAction(&action); |
273 BondActions(&feeder_action, &action); | |
274 | 262 |
275 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 263 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
276 g_main_loop_run(loop); | 264 g_main_loop_run(loop); |
277 g_main_loop_unref(loop); | 265 g_main_loop_unref(loop); |
278 EXPECT_FALSE(processor.IsRunning()); | 266 EXPECT_FALSE(processor.IsRunning()); |
279 } | 267 } |
280 | 268 |
281 TEST(OmahaRequestActionTest, InvalidXmlTest) { | 269 TEST(OmahaRequestActionTest, InvalidXmlTest) { |
282 OmahaRequestParams params("machine_id", | 270 OmahaRequestParams params("machine_id", |
283 "user_id", | 271 "user_id", |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 "", // os_sp | 431 "", // os_sp |
444 "", // os_board | 432 "", // os_board |
445 OmahaRequestParams::kAppId, | 433 OmahaRequestParams::kAppId, |
446 "0.1.0.0", | 434 "0.1.0.0", |
447 "en-US", | 435 "en-US", |
448 "unittest", | 436 "unittest", |
449 "http://url"); | 437 "http://url"); |
450 string http_response("doesn't matter"); | 438 string http_response("doesn't matter"); |
451 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 439 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
452 | 440 |
453 ObjectFeederAction<OmahaRequestParams> feeder_action; | 441 OmahaRequestAction action(params, NULL, |
454 feeder_action.set_obj(params); | |
455 OmahaRequestAction action(NULL, | |
456 new MockHttpFetcher(http_response.data(), | 442 new MockHttpFetcher(http_response.data(), |
457 http_response.size())); | 443 http_response.size())); |
458 TerminateEarlyTestProcessorDelegate delegate; | 444 TerminateEarlyTestProcessorDelegate delegate; |
459 delegate.loop_ = loop; | 445 delegate.loop_ = loop; |
460 ActionProcessor processor; | 446 ActionProcessor processor; |
461 processor.set_delegate(&delegate); | 447 processor.set_delegate(&delegate); |
462 processor.EnqueueAction(&feeder_action); | |
463 processor.EnqueueAction(&action); | 448 processor.EnqueueAction(&action); |
464 BondActions(&feeder_action, &action); | |
465 | 449 |
466 g_timeout_add(0, &TerminateTransferTestStarter, &processor); | 450 g_timeout_add(0, &TerminateTransferTestStarter, &processor); |
467 g_main_loop_run(loop); | 451 g_main_loop_run(loop); |
468 g_main_loop_unref(loop); | 452 g_main_loop_unref(loop); |
469 } | 453 } |
470 | 454 |
471 TEST(OmahaRequestActionTest, XmlEncodeTest) { | 455 TEST(OmahaRequestActionTest, XmlEncodeTest) { |
472 EXPECT_EQ("ab", XmlEncode("ab")); | 456 EXPECT_EQ("ab", XmlEncode("ab")); |
473 EXPECT_EQ("a<b", XmlEncode("a<b")); | 457 EXPECT_EQ("a<b", XmlEncode("a<b")); |
474 EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); | 458 EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 "errorcode=\"%d\"></o:event>\n", | 606 "errorcode=\"%d\"></o:event>\n", |
623 OmahaEvent::kTypeDownloadComplete, | 607 OmahaEvent::kTypeDownloadComplete, |
624 OmahaEvent::kResultError, | 608 OmahaEvent::kResultError, |
625 5); | 609 5); |
626 EXPECT_NE(post_str.find(expected_event), string::npos); | 610 EXPECT_NE(post_str.find(expected_event), string::npos); |
627 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos); | 611 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos); |
628 } | 612 } |
629 | 613 |
630 TEST(OmahaRequestActionTest, IsEventTest) { | 614 TEST(OmahaRequestActionTest, IsEventTest) { |
631 string http_response("doesn't matter"); | 615 string http_response("doesn't matter"); |
| 616 OmahaRequestParams params("machine_id", |
| 617 "user_id", |
| 618 OmahaRequestParams::kOsPlatform, |
| 619 OmahaRequestParams::kOsVersion, |
| 620 "service_pack", |
| 621 "x86-generic", |
| 622 OmahaRequestParams::kAppId, |
| 623 "0.1.0.0", |
| 624 "en-US", |
| 625 "unittest_track", |
| 626 "http://url"); |
632 | 627 |
633 OmahaRequestAction update_check_action( | 628 OmahaRequestAction update_check_action( |
| 629 params, |
634 NULL, | 630 NULL, |
635 new MockHttpFetcher(http_response.data(), | 631 new MockHttpFetcher(http_response.data(), |
636 http_response.size())); | 632 http_response.size())); |
637 EXPECT_FALSE(update_check_action.IsEvent()); | 633 EXPECT_FALSE(update_check_action.IsEvent()); |
638 | 634 |
639 OmahaRequestAction event_action( | 635 OmahaRequestAction event_action( |
| 636 params, |
640 new OmahaEvent(OmahaEvent::kTypeInstallComplete, | 637 new OmahaEvent(OmahaEvent::kTypeInstallComplete, |
641 OmahaEvent::kResultError, | 638 OmahaEvent::kResultError, |
642 0), | 639 0), |
643 new MockHttpFetcher(http_response.data(), | 640 new MockHttpFetcher(http_response.data(), |
644 http_response.size())); | 641 http_response.size())); |
645 EXPECT_TRUE(event_action.IsEvent()); | 642 EXPECT_TRUE(event_action.IsEvent()); |
646 } | 643 } |
647 | 644 |
648 } // namespace chromeos_update_engine | 645 } // namespace chromeos_update_engine |
OLD | NEW |