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 <unistd.h> | 5 #include <unistd.h> |
| 6 |
6 #include <string> | 7 #include <string> |
7 #include <vector> | 8 #include <vector> |
8 #include <base/scoped_ptr.h> | 9 |
9 #include <glib.h> | |
10 #include <gtest/gtest.h> | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/scoped_ptr.h" |
| 12 #include "base/string_util.h" |
| 13 #include "glib.h" |
| 14 #include "gtest/gtest.h" |
12 #include "update_engine/libcurl_http_fetcher.h" | 15 #include "update_engine/libcurl_http_fetcher.h" |
13 #include "update_engine/mock_http_fetcher.h" | 16 #include "update_engine/mock_http_fetcher.h" |
14 | 17 |
15 using std::string; | 18 using std::string; |
16 using std::vector; | 19 using std::vector; |
17 | 20 |
18 namespace chromeos_update_engine { | 21 namespace chromeos_update_engine { |
19 | 22 |
20 namespace { | 23 namespace { |
21 // WARNING, if you update this, you must also update test_http_server.py | 24 // WARNING, if you update this, you must also update test_http_server.py |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 }; | 446 }; |
444 | 447 |
445 g_timeout_add(0, StartTransfer, &start_xfer_args); | 448 g_timeout_add(0, StartTransfer, &start_xfer_args); |
446 g_main_loop_run(loop); | 449 g_main_loop_run(loop); |
447 | 450 |
448 // Exiting and testing happens in the delegate | 451 // Exiting and testing happens in the delegate |
449 } | 452 } |
450 g_main_loop_unref(loop); | 453 g_main_loop_unref(loop); |
451 } | 454 } |
452 | 455 |
| 456 namespace { |
| 457 const int kRedirectCodes[] = { 301, 302, 303, 307 }; |
| 458 |
| 459 class RedirectHttpFetcherTestDelegate : public HttpFetcherDelegate { |
| 460 public: |
| 461 RedirectHttpFetcherTestDelegate(bool expected_successful) |
| 462 : expected_successful_(expected_successful) {} |
| 463 virtual void ReceivedBytes(HttpFetcher* fetcher, |
| 464 const char* bytes, int length) { |
| 465 data.append(bytes, length); |
| 466 } |
| 467 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { |
| 468 EXPECT_EQ(expected_successful_, successful); |
| 469 g_main_loop_quit(loop_); |
| 470 } |
| 471 bool expected_successful_; |
| 472 string data; |
| 473 GMainLoop* loop_; |
| 474 }; |
| 475 |
| 476 // RedirectTest takes ownership of |http_fetcher|. |
| 477 void RedirectTest(bool expected_successful, |
| 478 const string& url, |
| 479 HttpFetcher* http_fetcher) { |
| 480 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 481 RedirectHttpFetcherTestDelegate delegate(expected_successful); |
| 482 delegate.loop_ = loop; |
| 483 scoped_ptr<HttpFetcher> fetcher(http_fetcher); |
| 484 fetcher->set_delegate(&delegate); |
| 485 |
| 486 StartTransferArgs start_xfer_args = |
| 487 { fetcher.get(), LocalServerUrlForPath(url) }; |
| 488 |
| 489 g_timeout_add(0, StartTransfer, &start_xfer_args); |
| 490 g_main_loop_run(loop); |
| 491 if (expected_successful) { |
| 492 // verify the data we get back |
| 493 ASSERT_EQ(1000, delegate.data.size()); |
| 494 for (int i = 0; i < 1000; i += 10) { |
| 495 // Assert so that we don't flood the screen w/ EXPECT errors on failure. |
| 496 ASSERT_EQ(delegate.data.substr(i, 10), "abcdefghij"); |
| 497 } |
| 498 } |
| 499 g_main_loop_unref(loop); |
| 500 } |
| 501 } // namespace {} |
| 502 |
| 503 TYPED_TEST(HttpFetcherTest, SimpleRedirectTest) { |
| 504 if (this->IsMock()) |
| 505 return; |
| 506 typename TestFixture::HttpServer server; |
| 507 ASSERT_TRUE(server.started_); |
| 508 for (size_t c = 0; c < arraysize(kRedirectCodes); ++c) { |
| 509 const string url = base::StringPrintf("/redirect/%d/medium", |
| 510 kRedirectCodes[c]); |
| 511 RedirectTest(true, url, this->NewLargeFetcher()); |
| 512 } |
| 513 } |
| 514 |
| 515 TYPED_TEST(HttpFetcherTest, MaxRedirectTest) { |
| 516 if (this->IsMock()) |
| 517 return; |
| 518 typename TestFixture::HttpServer server; |
| 519 ASSERT_TRUE(server.started_); |
| 520 string url; |
| 521 for (int r = 0; r < LibcurlHttpFetcher::kMaxRedirects; r++) { |
| 522 url += base::StringPrintf("/redirect/%d", |
| 523 kRedirectCodes[r % arraysize(kRedirectCodes)]); |
| 524 } |
| 525 url += "/medium"; |
| 526 RedirectTest(true, url, this->NewLargeFetcher()); |
| 527 } |
| 528 |
| 529 TYPED_TEST(HttpFetcherTest, BeyondMaxRedirectTest) { |
| 530 if (this->IsMock()) |
| 531 return; |
| 532 typename TestFixture::HttpServer server; |
| 533 ASSERT_TRUE(server.started_); |
| 534 string url; |
| 535 for (int r = 0; r < LibcurlHttpFetcher::kMaxRedirects + 1; r++) { |
| 536 url += base::StringPrintf("/redirect/%d", |
| 537 kRedirectCodes[r % arraysize(kRedirectCodes)]); |
| 538 } |
| 539 url += "/medium"; |
| 540 RedirectTest(false, url, this->NewLargeFetcher()); |
| 541 } |
| 542 |
453 } // namespace chromeos_update_engine | 543 } // namespace chromeos_update_engine |
OLD | NEW |