| 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 <glib.h> | 7 #include <glib.h> |
| 7 #include <gtest/gtest.h> | 8 #include <gtest/gtest.h> |
| 8 #include "update_engine/action_pipe.h" | 9 #include "update_engine/action_pipe.h" |
| 9 #include "update_engine/update_check_action.h" | 10 #include "update_engine/update_check_action.h" |
| 10 #include "update_engine/mock_http_fetcher.h" | 11 #include "update_engine/mock_http_fetcher.h" |
| 11 #include "update_engine/omaha_hash_calculator.h" | 12 #include "update_engine/omaha_hash_calculator.h" |
| 12 #include "update_engine/test_utils.h" | 13 #include "update_engine/test_utils.h" |
| 13 | 14 |
| 15 using std::string; |
| 16 using std::vector; |
| 17 |
| 14 namespace chromeos_update_engine { | 18 namespace chromeos_update_engine { |
| 15 | 19 |
| 16 using std::string; | |
| 17 | |
| 18 class UpdateCheckActionTest : public ::testing::Test { }; | 20 class UpdateCheckActionTest : public ::testing::Test { }; |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 string GetNoUpdateResponse(const string& app_id) { | 23 string GetNoUpdateResponse(const string& app_id) { |
| 22 return string( | 24 return string( |
| 23 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " | 25 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 24 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " | 26 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 25 "appid=\"") + app_id + "\" status=\"ok\"><ping " | 27 "appid=\"") + app_id + "\" status=\"ok\"><ping " |
| 26 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>"; | 28 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>"; |
| 27 } | 29 } |
| 28 | 30 |
| 29 string GetUpdateResponse(const string& app_id, | 31 string GetUpdateResponse(const string& app_id, |
| 30 const string& display_version, | 32 const string& display_version, |
| 31 const string& more_info_url, | 33 const string& more_info_url, |
| 32 const string& prompt, | 34 const string& prompt, |
| 33 const string& codebase, | 35 const string& codebase, |
| 34 const string& hash, | 36 const string& hash, |
| 35 const string& needsadmin, | 37 const string& needsadmin, |
| 36 const string& size) { | 38 const string& size) { |
| 37 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " | 39 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 38 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " | 40 "xmlns=\"http://www.google.com/update2/response\" " |
| 39 "appid=\"") + app_id + "\" status=\"ok\"><ping " | 41 "protocol=\"2.0\"><app " |
| 42 "appid=\"") + app_id + "\" status=\"ok\"><ping " |
| 40 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" " | 43 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" " |
| 41 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" " | 44 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" " |
| 42 "codebase=\"" + codebase + "\" " | 45 "codebase=\"" + codebase + "\" " |
| 43 "hash=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" " | 46 "hash=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" " |
| 44 "size=\"" + size + "\" status=\"ok\"/></app></gupdate>"; | 47 "size=\"" + size + "\" status=\"ok\"/></app></gupdate>"; |
| 45 } | 48 } |
| 46 | 49 |
| 47 class UpdateCheckActionTestProcessorDelegate : public ActionProcessorDelegate { | 50 class UpdateCheckActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 48 public: | 51 public: |
| 49 UpdateCheckActionTestProcessorDelegate() | 52 UpdateCheckActionTestProcessorDelegate() |
| 50 : loop_(NULL), | 53 : loop_(NULL), |
| 51 expected_success_(true) {} | 54 expected_success_(true) {} |
| 52 virtual ~UpdateCheckActionTestProcessorDelegate() { | 55 virtual ~UpdateCheckActionTestProcessorDelegate() { |
| 53 } | 56 } |
| 54 virtual void ProcessingDone(const ActionProcessor* processor) { | 57 virtual void ProcessingDone(const ActionProcessor* processor) { |
| 55 ASSERT_TRUE(loop_); | 58 ASSERT_TRUE(loop_); |
| 56 g_main_loop_quit(loop_); | 59 g_main_loop_quit(loop_); |
| 57 } | 60 } |
| 58 | 61 |
| 59 virtual void ActionCompleted(const ActionProcessor* processor, | 62 virtual void ActionCompleted(ActionProcessor* processor, |
| 60 const AbstractAction* action, | 63 AbstractAction* action, |
| 61 bool success) { | 64 bool success) { |
| 62 // make sure actions always succeed | 65 // make sure actions always succeed |
| 63 if (action->Type() == "UpdateCheckAction") | 66 if (action->Type() == UpdateCheckAction::StaticType()) |
| 64 EXPECT_EQ(expected_success_, success); | 67 EXPECT_EQ(expected_success_, success); |
| 65 else | 68 else |
| 66 EXPECT_TRUE(success); | 69 EXPECT_TRUE(success); |
| 67 } | 70 } |
| 68 GMainLoop *loop_; | 71 GMainLoop *loop_; |
| 69 bool expected_success_; | 72 bool expected_success_; |
| 70 }; | 73 }; |
| 71 | 74 |
| 72 gboolean StartProcessorInRunLoop(gpointer data) { | 75 gboolean StartProcessorInRunLoop(gpointer data) { |
| 73 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 76 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 95 has_input_object_ = HasInputObject(); | 98 has_input_object_ = HasInputObject(); |
| 96 if (has_input_object_) | 99 if (has_input_object_) |
| 97 update_check_response_ = GetInputObject(); | 100 update_check_response_ = GetInputObject(); |
| 98 processor_->ActionComplete(this, true); | 101 processor_->ActionComplete(this, true); |
| 99 } | 102 } |
| 100 // Should never be called | 103 // Should never be called |
| 101 void TerminateProcessing() { | 104 void TerminateProcessing() { |
| 102 CHECK(false); | 105 CHECK(false); |
| 103 } | 106 } |
| 104 // Debugging/logging | 107 // Debugging/logging |
| 105 std::string Type() const { return "OutputObjectCollectorAction"; } | 108 static std::string StaticType() { |
| 109 return "OutputObjectCollectorAction"; |
| 110 } |
| 111 std::string Type() const { return StaticType(); } |
| 106 bool has_input_object_; | 112 bool has_input_object_; |
| 107 UpdateCheckResponse update_check_response_; | 113 UpdateCheckResponse update_check_response_; |
| 108 }; | 114 }; |
| 109 | 115 |
| 110 // returns true iff an output response was obtained from the | 116 // returns true iff an output response was obtained from the |
| 111 // UpdateCheckAction. out_response may be NULL. | 117 // UpdateCheckAction. out_response may be NULL. |
| 112 // out_post_data may be null; if non-null, the post-data received by the | 118 // out_post_data may be null; if non-null, the post-data received by the |
| 113 // mock HttpFetcher is returned. | 119 // mock HttpFetcher is returned. |
| 114 bool TestUpdateCheckAction(const UpdateCheckParams& params, | 120 bool TestUpdateCheckAction(const UpdateCheckParams& params, |
| 115 const string& http_response, | 121 const string& http_response, |
| 116 bool expected_success, | 122 bool expected_success, |
| 117 UpdateCheckResponse* out_response, | 123 UpdateCheckResponse* out_response, |
| 118 vector<char> *out_post_data) { | 124 vector<char> *out_post_data) { |
| 119 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 125 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 120 MockHttpFetcher *fetcher = new MockHttpFetcher(http_response.data(), | 126 MockHttpFetcher *fetcher = new MockHttpFetcher(http_response.data(), |
| 121 http_response.size()); | 127 http_response.size()); |
| 122 | 128 ObjectFeederAction<UpdateCheckParams> feeder_action; |
| 123 UpdateCheckAction action(params, fetcher); // takes ownership of fetcher | 129 UpdateCheckAction action(fetcher); // takes ownership of fetcher |
| 124 UpdateCheckActionTestProcessorDelegate delegate; | 130 UpdateCheckActionTestProcessorDelegate delegate; |
| 125 delegate.loop_ = loop; | 131 delegate.loop_ = loop; |
| 126 delegate.expected_success_ = expected_success; | 132 delegate.expected_success_ = expected_success; |
| 127 ActionProcessor processor; | 133 ActionProcessor processor; |
| 134 feeder_action.set_obj(params); |
| 128 processor.set_delegate(&delegate); | 135 processor.set_delegate(&delegate); |
| 136 processor.EnqueueAction(&feeder_action); |
| 129 processor.EnqueueAction(&action); | 137 processor.EnqueueAction(&action); |
| 130 | 138 |
| 131 OutputObjectCollectorAction collector_action; | 139 OutputObjectCollectorAction collector_action; |
| 132 | 140 |
| 141 BondActions(&feeder_action, &action); |
| 133 BondActions(&action, &collector_action); | 142 BondActions(&action, &collector_action); |
| 134 processor.EnqueueAction(&collector_action); | 143 processor.EnqueueAction(&collector_action); |
| 135 | 144 |
| 136 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 145 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 137 g_main_loop_run(loop); | 146 g_main_loop_run(loop); |
| 138 g_main_loop_unref(loop); | 147 g_main_loop_unref(loop); |
| 139 if (collector_action.has_input_object_ && out_response) | 148 if (collector_action.has_input_object_ && out_response) |
| 140 *out_response = collector_action.update_check_response_; | 149 *out_response = collector_action.update_check_response_; |
| 141 if (out_post_data) | 150 if (out_post_data) |
| 142 *out_post_data = fetcher->post_data(); | 151 *out_post_data = fetcher->post_data(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 UpdateCheckParams::kOsVersion, | 213 UpdateCheckParams::kOsVersion, |
| 205 "", // os_sp | 214 "", // os_sp |
| 206 UpdateCheckParams::kAppId, | 215 UpdateCheckParams::kAppId, |
| 207 "0.1.0.0", | 216 "0.1.0.0", |
| 208 "en-US", | 217 "en-US", |
| 209 "unittest"); | 218 "unittest"); |
| 210 const string http_response(GetNoUpdateResponse(UpdateCheckParams::kAppId)); | 219 const string http_response(GetNoUpdateResponse(UpdateCheckParams::kAppId)); |
| 211 | 220 |
| 212 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 221 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 213 | 222 |
| 214 UpdateCheckAction action(params, | 223 ObjectFeederAction<UpdateCheckParams> feeder_action; |
| 215 new MockHttpFetcher(http_response.data(), | 224 feeder_action.set_obj(params); |
| 225 UpdateCheckAction action(new MockHttpFetcher(http_response.data(), |
| 216 http_response.size())); | 226 http_response.size())); |
| 217 UpdateCheckActionTestProcessorDelegate delegate; | 227 UpdateCheckActionTestProcessorDelegate delegate; |
| 218 delegate.loop_ = loop; | 228 delegate.loop_ = loop; |
| 219 ActionProcessor processor; | 229 ActionProcessor processor; |
| 220 processor.set_delegate(&delegate); | 230 processor.set_delegate(&delegate); |
| 231 processor.EnqueueAction(&feeder_action); |
| 221 processor.EnqueueAction(&action); | 232 processor.EnqueueAction(&action); |
| 233 BondActions(&feeder_action, &action); |
| 222 | 234 |
| 223 g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 235 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 224 g_main_loop_run(loop); | 236 g_main_loop_run(loop); |
| 225 g_main_loop_unref(loop); | 237 g_main_loop_unref(loop); |
| 226 EXPECT_FALSE(processor.IsRunning()); | 238 EXPECT_FALSE(processor.IsRunning()); |
| 227 } | 239 } |
| 228 | 240 |
| 229 TEST(UpdateCheckActionTest, InvalidXmlTest) { | 241 TEST(UpdateCheckActionTest, InvalidXmlTest) { |
| 230 UpdateCheckParams params("machine_id", | 242 UpdateCheckParams params("machine_id", |
| 231 "user_id", | 243 "user_id", |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 "user_id", | 332 "user_id", |
| 321 UpdateCheckParams::kOsPlatform, | 333 UpdateCheckParams::kOsPlatform, |
| 322 UpdateCheckParams::kOsVersion, | 334 UpdateCheckParams::kOsVersion, |
| 323 "service_pack", | 335 "service_pack", |
| 324 UpdateCheckParams::kAppId, | 336 UpdateCheckParams::kAppId, |
| 325 "0.1.0.0", | 337 "0.1.0.0", |
| 326 "en-US", | 338 "en-US", |
| 327 "unittest_track"); | 339 "unittest_track"); |
| 328 UpdateCheckResponse response; | 340 UpdateCheckResponse response; |
| 329 ASSERT_TRUE(TestUpdateCheckAction(params, | 341 ASSERT_TRUE(TestUpdateCheckAction(params, |
| 330 string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " | 342 string("<?xml version=\"1.0\" " |
| 331 "xmlns=\"http://www.google.com/update2/response\" " | 343 "encoding=\"UTF-8\"?><gupdate " |
| 332 "protocol=\"2.0\"><app appid=\"") + | 344 "xmlns=\"http://www.google.com/" |
| 333 UpdateCheckParams::kAppId + "\" status=\"ok\"><ping " | 345 "update2/response\" " |
| 334 "status=\"ok\"/><updatecheck DisplayVersion=\"1.2.3.4\" " | 346 "protocol=\"2.0\"><app appid=\"") + |
| 335 "Prompt=\"false\" " | 347 UpdateCheckParams::kAppId |
| 336 "codebase=\"http://code/base\" " | 348 + "\" status=\"ok\"><ping " |
| 337 "hash=\"HASH1234=\" needsadmin=\"true\" " | 349 "status=\"ok\"/><updatecheck " |
| 338 "size=\"123\" status=\"ok\"/></app></gupdate>", | 350 "DisplayVersion=\"1.2.3.4\" " |
| 339 true, | 351 "Prompt=\"false\" " |
| 340 &response, | 352 "codebase=\"http://code/base\" " |
| 341 NULL)); | 353 "hash=\"HASH1234=\" needsadmin=\"true\" " |
| 354 "size=\"123\" " |
| 355 "status=\"ok\"/></app></gupdate>", |
| 356 true, |
| 357 &response, |
| 358 NULL)); |
| 342 EXPECT_TRUE(response.update_exists); | 359 EXPECT_TRUE(response.update_exists); |
| 343 EXPECT_EQ("1.2.3.4", response.display_version); | 360 EXPECT_EQ("1.2.3.4", response.display_version); |
| 344 EXPECT_EQ("http://code/base", response.codebase); | 361 EXPECT_EQ("http://code/base", response.codebase); |
| 345 EXPECT_EQ("", response.more_info_url); | 362 EXPECT_EQ("", response.more_info_url); |
| 346 EXPECT_EQ("HASH1234=", response.hash); | 363 EXPECT_EQ("HASH1234=", response.hash); |
| 347 EXPECT_EQ(123, response.size); | 364 EXPECT_EQ(123, response.size); |
| 348 EXPECT_TRUE(response.needs_admin); | 365 EXPECT_TRUE(response.needs_admin); |
| 349 EXPECT_FALSE(response.prompt); | 366 EXPECT_FALSE(response.prompt); |
| 350 } | 367 } |
| 351 | 368 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 374 UpdateCheckParams::kOsPlatform, | 391 UpdateCheckParams::kOsPlatform, |
| 375 UpdateCheckParams::kOsVersion, | 392 UpdateCheckParams::kOsVersion, |
| 376 "", // os_sp | 393 "", // os_sp |
| 377 UpdateCheckParams::kAppId, | 394 UpdateCheckParams::kAppId, |
| 378 "0.1.0.0", | 395 "0.1.0.0", |
| 379 "en-US", | 396 "en-US", |
| 380 "unittest"); | 397 "unittest"); |
| 381 string http_response("doesn't matter"); | 398 string http_response("doesn't matter"); |
| 382 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 399 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 383 | 400 |
| 384 UpdateCheckAction action(params, | 401 ObjectFeederAction<UpdateCheckParams> feeder_action; |
| 385 new MockHttpFetcher(http_response.data(), | 402 feeder_action.set_obj(params); |
| 403 UpdateCheckAction action(new MockHttpFetcher(http_response.data(), |
| 386 http_response.size())); | 404 http_response.size())); |
| 387 TerminateEarlyTestProcessorDelegate delegate; | 405 TerminateEarlyTestProcessorDelegate delegate; |
| 388 delegate.loop_ = loop; | 406 delegate.loop_ = loop; |
| 389 ActionProcessor processor; | 407 ActionProcessor processor; |
| 390 processor.set_delegate(&delegate); | 408 processor.set_delegate(&delegate); |
| 409 processor.EnqueueAction(&feeder_action); |
| 391 processor.EnqueueAction(&action); | 410 processor.EnqueueAction(&action); |
| 411 BondActions(&feeder_action, &action); |
| 392 | 412 |
| 393 g_timeout_add(0, &TerminateTransferTestStarter, &processor); | 413 g_timeout_add(0, &TerminateTransferTestStarter, &processor); |
| 394 g_main_loop_run(loop); | 414 g_main_loop_run(loop); |
| 395 g_main_loop_unref(loop); | 415 g_main_loop_unref(loop); |
| 396 } | 416 } |
| 397 | 417 |
| 398 TEST(UpdateCheckActionTest, XmlEncodeTest) { | 418 TEST(UpdateCheckActionTest, XmlEncodeTest) { |
| 399 EXPECT_EQ("ab", XmlEncode("ab")); | 419 EXPECT_EQ("ab", XmlEncode("ab")); |
| 400 EXPECT_EQ("a<b", XmlEncode("a<b")); | 420 EXPECT_EQ("a<b", XmlEncode("a<b")); |
| 401 EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); | 421 EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 // overflows int32: | 503 // overflows int32: |
| 484 "123123123123123"), // size | 504 "123123123123123"), // size |
| 485 true, | 505 true, |
| 486 &response, | 506 &response, |
| 487 NULL)); | 507 NULL)); |
| 488 | 508 |
| 489 EXPECT_EQ(response.size, 123123123123123ll); | 509 EXPECT_EQ(response.size, 123123123123123ll); |
| 490 } | 510 } |
| 491 | 511 |
| 492 } // namespace chromeos_update_engine | 512 } // namespace chromeos_update_engine |
| OLD | NEW |