| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 // End-to-end SDCH tests. Uses the embedded test server to return SDCH | 5 // End-to-end SDCH tests. Uses the embedded test server to return SDCH |
| 6 // results | 6 // results |
| 7 | 7 |
| 8 #include "base/base64.h" | 8 #include "base/base64.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "chrome/browser/ui/browser_window.h" | 27 #include "chrome/browser/ui/browser_window.h" |
| 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 29 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
| 30 #include "chrome/test/base/in_process_browser_test.h" | 30 #include "chrome/test/base/in_process_browser_test.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/common/content_switches.h" | 32 #include "content/public/common/content_switches.h" |
| 33 #include "content/public/test/browser_test_utils.h" | 33 #include "content/public/test/browser_test_utils.h" |
| 34 #include "content/public/test/test_utils.h" | 34 #include "content/public/test/test_utils.h" |
| 35 #include "crypto/sha2.h" | 35 #include "crypto/sha2.h" |
| 36 #include "net/base/sdch_manager.h" | 36 #include "net/base/sdch_manager.h" |
| 37 #include "net/base/sdch_observer.h" |
| 37 #include "net/http/http_response_headers.h" | 38 #include "net/http/http_response_headers.h" |
| 38 #include "net/test/embedded_test_server/embedded_test_server.h" | 39 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 39 #include "net/test/embedded_test_server/http_request.h" | 40 #include "net/test/embedded_test_server/http_request.h" |
| 40 #include "net/test/embedded_test_server/http_response.h" | 41 #include "net/test/embedded_test_server/http_response.h" |
| 41 #include "net/url_request/url_fetcher.h" | 42 #include "net/url_request/url_fetcher.h" |
| 42 #include "net/url_request/url_fetcher_delegate.h" | 43 #include "net/url_request/url_fetcher_delegate.h" |
| 43 #include "net/url_request/url_request_context.h" | 44 #include "net/url_request/url_request_context.h" |
| 44 #include "net/url_request/url_request_context_getter.h" | 45 #include "net/url_request/url_request_context_getter.h" |
| 45 #include "sdch/open-vcdiff/src/google/vcencoder.h" | 46 #include "sdch/open-vcdiff/src/google/vcencoder.h" |
| 46 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 bool cache_sdch_response_; | 253 bool cache_sdch_response_; |
| 253 std::string encoded_data_; | 254 std::string encoded_data_; |
| 254 std::string sdch_dictionary_contents_; | 255 std::string sdch_dictionary_contents_; |
| 255 std::string dictionary_client_hash_; | 256 std::string dictionary_client_hash_; |
| 256 std::string dictionary_server_hash_; | 257 std::string dictionary_server_hash_; |
| 257 RequestVector request_vector_; | 258 RequestVector request_vector_; |
| 258 std::vector<base::Closure> callback_vector_; | 259 std::vector<base::Closure> callback_vector_; |
| 259 base::WeakPtrFactory<SdchResponseHandler> weak_ptr_factory_; | 260 base::WeakPtrFactory<SdchResponseHandler> weak_ptr_factory_; |
| 260 }; | 261 }; |
| 261 | 262 |
| 262 class SdchBrowserTest : public InProcessBrowserTest, net::URLFetcherDelegate { | 263 class SdchBrowserTest : |
| 264 public InProcessBrowserTest, |
| 265 public net::URLFetcherDelegate, |
| 266 public net::SdchObserver { |
| 263 public: | 267 public: |
| 264 static const char kTestHost[]; | 268 static const char kTestHost[]; |
| 265 | 269 |
| 266 SdchBrowserTest() | 270 SdchBrowserTest() |
| 267 : response_handler_(kTestHost), | 271 : response_handler_(kTestHost), |
| 268 url_request_context_getter_(NULL), | 272 url_request_context_getter_(NULL), |
| 269 url_fetch_complete_(false), | 273 url_fetch_complete_(false), |
| 270 waiting_(false) {} | 274 waiting_(false) {} |
| 271 | 275 |
| 272 // Helper functions for fetching data. | 276 // Helper functions for fetching data. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 } | 350 } |
| 347 | 351 |
| 348 // Client information and control. | 352 // Client information and control. |
| 349 | 353 |
| 350 int GetNumberOfDictionaryFetches(Profile* profile) { | 354 int GetNumberOfDictionaryFetches(Profile* profile) { |
| 351 int fetches = -1; | 355 int fetches = -1; |
| 352 base::RunLoop run_loop; | 356 base::RunLoop run_loop; |
| 353 content::BrowserThread::PostTaskAndReply( | 357 content::BrowserThread::PostTaskAndReply( |
| 354 content::BrowserThread::IO, FROM_HERE, | 358 content::BrowserThread::IO, FROM_HERE, |
| 355 base::Bind(&SdchBrowserTest::GetNumberOfDictionaryFetchesOnIOThread, | 359 base::Bind(&SdchBrowserTest::GetNumberOfDictionaryFetchesOnIOThread, |
| 360 base::Unretained(this), |
| 356 base::Unretained(profile->GetRequestContext()), | 361 base::Unretained(profile->GetRequestContext()), |
| 357 &fetches), | 362 &fetches), |
| 358 run_loop.QuitClosure()); | 363 run_loop.QuitClosure()); |
| 359 run_loop.Run(); | 364 run_loop.Run(); |
| 360 DCHECK_NE(-1, fetches); | 365 DCHECK_NE(-1, fetches); |
| 361 return fetches; | 366 return fetches; |
| 362 } | 367 } |
| 363 | 368 |
| 364 void BrowsingDataRemoveAndWait(int remove_mask) { | 369 void BrowsingDataRemoveAndWait(int remove_mask) { |
| 365 BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod( | 370 BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 second_profile_, browser()->host_desktop_type())); | 403 second_profile_, browser()->host_desktop_type())); |
| 399 if (!second_browser_) return false; | 404 if (!second_browser_) return false; |
| 400 | 405 |
| 401 chrome::AddSelectedTabWithURL(second_browser_, | 406 chrome::AddSelectedTabWithURL(second_browser_, |
| 402 GURL(url::kAboutBlankURL), | 407 GURL(url::kAboutBlankURL), |
| 403 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); | 408 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); |
| 404 content::WaitForLoadStop( | 409 content::WaitForLoadStop( |
| 405 second_browser_->tab_strip_model()->GetActiveWebContents()); | 410 second_browser_->tab_strip_model()->GetActiveWebContents()); |
| 406 second_browser_->window()->Show(); | 411 second_browser_->window()->Show(); |
| 407 | 412 |
| 413 content::BrowserThread::PostTask( |
| 414 content::BrowserThread::IO, FROM_HERE, |
| 415 base::Bind(&SdchBrowserTest::SubscribeToSdchNotifications, |
| 416 base::Unretained(this), make_scoped_refptr( |
| 417 second_browser_->profile()->GetRequestContext()))); |
| 418 |
| 419 return true; |
| 420 } |
| 421 |
| 422 bool SetupIncognitoBrowser() { |
| 423 incognito_browser_ = CreateIncognitoBrowser(); |
| 424 |
| 425 if (!incognito_browser_) return false; |
| 426 |
| 427 content::BrowserThread::PostTask( |
| 428 content::BrowserThread::IO, FROM_HERE, |
| 429 base::Bind(&SdchBrowserTest::SubscribeToSdchNotifications, |
| 430 base::Unretained(this), make_scoped_refptr( |
| 431 incognito_browser_->profile()->GetRequestContext()))); |
| 432 |
| 408 return true; | 433 return true; |
| 409 } | 434 } |
| 410 | 435 |
| 411 Browser* second_browser() { return second_browser_; } | 436 Browser* second_browser() { return second_browser_; } |
| 437 Browser* incognito_browser() { return incognito_browser_; } |
| 412 | 438 |
| 413 // Server information and control. | 439 // Server information and control. |
| 414 | 440 |
| 415 void WaitAndGetTestVector(int num_requests, RequestVector* result) { | 441 void WaitAndGetTestVector(int num_requests, RequestVector* result) { |
| 416 base::RunLoop run_loop; | 442 base::RunLoop run_loop; |
| 417 content::BrowserThread::PostTask( | 443 content::BrowserThread::PostTask( |
| 418 content::BrowserThread::IO, FROM_HERE, | 444 content::BrowserThread::IO, FROM_HERE, |
| 419 base::Bind(&SdchResponseHandler::WaitAndGetRequestVector, | 445 base::Bind(&SdchResponseHandler::WaitAndGetRequestVector, |
| 420 base::Unretained(&response_handler_), | 446 base::Unretained(&response_handler_), |
| 421 num_requests, | 447 num_requests, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 private: | 528 private: |
| 503 static void NukeSdchDictionariesOnIOThread( | 529 static void NukeSdchDictionariesOnIOThread( |
| 504 net::URLRequestContextGetter* context_getter) { | 530 net::URLRequestContextGetter* context_getter) { |
| 505 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 531 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 506 net::SdchManager* sdch_manager = | 532 net::SdchManager* sdch_manager = |
| 507 context_getter->GetURLRequestContext()->sdch_manager(); | 533 context_getter->GetURLRequestContext()->sdch_manager(); |
| 508 DCHECK(sdch_manager); | 534 DCHECK(sdch_manager); |
| 509 sdch_manager->ClearData(); | 535 sdch_manager->ClearData(); |
| 510 } | 536 } |
| 511 | 537 |
| 512 static void GetNumberOfDictionaryFetchesOnIOThread( | 538 void GetNumberOfDictionaryFetchesOnIOThread( |
| 513 net::URLRequestContextGetter* url_request_context_getter, | 539 net::URLRequestContextGetter* context_getter, int* result) { |
| 514 int* result) { | |
| 515 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 540 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 516 net::SdchManager* sdch_manager = | 541 |
| 517 url_request_context_getter->GetURLRequestContext()->sdch_manager(); | 542 net::SdchManager* manager( |
| 518 DCHECK(sdch_manager); | 543 context_getter->GetURLRequestContext()->sdch_manager()); |
| 519 *result = sdch_manager->GetFetchesCountForTesting(); | 544 DCHECK(fetch_counts_.end() != fetch_counts_.find(manager)); |
| 545 |
| 546 *result = fetch_counts_[manager]; |
| 520 } | 547 } |
| 521 | 548 |
| 522 // InProcessBrowserTest | 549 // InProcessBrowserTest |
| 523 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { | 550 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { |
| 524 command_line->AppendSwitchASCII( | 551 command_line->AppendSwitchASCII( |
| 525 switches::kHostResolverRules, | 552 switches::kHostResolverRules, |
| 526 "MAP " + std::string(kTestHost) + " 127.0.0.1"); | 553 "MAP " + std::string(kTestHost) + " 127.0.0.1"); |
| 527 #if defined(OS_CHROMEOS) | 554 #if defined(OS_CHROMEOS) |
| 528 command_line->AppendSwitch( | 555 command_line->AppendSwitch( |
| 529 chromeos::switches::kIgnoreUserProfileMappingForTests); | 556 chromeos::switches::kIgnoreUserProfileMappingForTests); |
| 530 #endif | 557 #endif |
| 531 } | 558 } |
| 532 | 559 |
| 533 virtual void SetUpOnMainThread() OVERRIDE { | 560 virtual void SetUpOnMainThread() OVERRIDE { |
| 534 test_server_.RegisterRequestHandler( | 561 test_server_.RegisterRequestHandler( |
| 535 base::Bind(&SdchResponseHandler::HandleRequest, | 562 base::Bind(&SdchResponseHandler::HandleRequest, |
| 536 base::Unretained(&response_handler_))); | 563 base::Unretained(&response_handler_))); |
| 537 CHECK(test_server_.InitializeAndWaitUntilReady()); | 564 CHECK(test_server_.InitializeAndWaitUntilReady()); |
| 538 url_request_context_getter_ = browser()->profile()->GetRequestContext(); | 565 url_request_context_getter_ = browser()->profile()->GetRequestContext(); |
| 566 |
| 567 content::BrowserThread::PostTask( |
| 568 content::BrowserThread::IO, FROM_HERE, |
| 569 base::Bind(&SdchBrowserTest::SubscribeToSdchNotifications, |
| 570 base::Unretained(this), url_request_context_getter_)); |
| 539 } | 571 } |
| 540 | 572 |
| 541 virtual void TearDownOnMainThread() OVERRIDE { | 573 virtual void TearDownOnMainThread() OVERRIDE { |
| 542 CHECK(test_server_.ShutdownAndWaitUntilComplete()); | 574 CHECK(test_server_.ShutdownAndWaitUntilComplete()); |
| 575 |
| 576 content::BrowserThread::PostTask( |
| 577 content::BrowserThread::IO, FROM_HERE, |
| 578 base::Bind(&SdchBrowserTest::UnsubscribeFromAllSdchNotifications, |
| 579 base::Unretained(this))); |
| 580 } |
| 581 |
| 582 void SubscribeToSdchNotifications( |
| 583 net::URLRequestContextGetter* context_getter) { |
| 584 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 585 |
| 586 net::SdchManager* manager = |
| 587 context_getter->GetURLRequestContext()->sdch_manager(); |
| 588 DCHECK(fetch_counts_.end() == fetch_counts_.find(manager)); |
| 589 |
| 590 fetch_counts_[manager] = 0; |
| 591 manager->AddObserver(this); |
| 592 } |
| 593 |
| 594 void UnsubscribeFromAllSdchNotifications() { |
| 595 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 596 |
| 597 for (auto it = fetch_counts_.begin(); it != fetch_counts_.end(); ++it) |
| 598 it->first->RemoveObserver(this); |
| 599 |
| 600 fetch_counts_.clear(); |
| 601 } |
| 602 |
| 603 // SdchObserver |
| 604 virtual void OnGetDictionary(net::SdchManager* manager, |
| 605 const GURL& request_url, |
| 606 const GURL& dictionary_url) OVERRIDE { |
| 607 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 608 DLOG(ERROR) << "Retrieving count of notifications from manager " << manager; |
| 609 DCHECK(fetch_counts_.end() != fetch_counts_.find(manager)); |
| 610 ++fetch_counts_[manager]; |
| 543 } | 611 } |
| 544 | 612 |
| 545 // URLFetcherDelegate | 613 // URLFetcherDelegate |
| 546 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { | 614 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { |
| 547 url_fetch_complete_ = true; | 615 url_fetch_complete_ = true; |
| 548 if (waiting_) | 616 if (waiting_) |
| 549 base::MessageLoopForUI::current()->Quit(); | 617 base::MessageLoopForUI::current()->Quit(); |
| 550 } | 618 } |
| 551 | 619 |
| 552 SdchResponseHandler response_handler_; | 620 SdchResponseHandler response_handler_; |
| 553 net::test_server::EmbeddedTestServer test_server_; | 621 net::test_server::EmbeddedTestServer test_server_; |
| 554 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 622 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 555 scoped_ptr<net::URLFetcher> fetcher_; | 623 scoped_ptr<net::URLFetcher> fetcher_; |
| 556 bool url_fetch_complete_; | 624 bool url_fetch_complete_; |
| 557 bool waiting_; | 625 bool waiting_; |
| 558 base::ScopedTempDir second_profile_data_dir_; | 626 base::ScopedTempDir second_profile_data_dir_; |
| 559 Profile* second_profile_; | 627 Profile* second_profile_; |
| 560 Browser* second_browser_; | 628 Browser* second_browser_; |
| 629 Browser* incognito_browser_; |
| 630 |
| 631 // IO Thread access only. |
| 632 std::map<net::SdchManager*, int> fetch_counts_; |
| 561 }; | 633 }; |
| 562 | 634 |
| 563 const char SdchBrowserTest::kTestHost[] = "our.test.host.com"; | 635 const char SdchBrowserTest::kTestHost[] = "our.test.host.com"; |
| 564 | 636 |
| 565 // Confirm that after getting a dictionary, calling the browsing | 637 // Confirm that after getting a dictionary, calling the browsing |
| 566 // data remover renders it unusable. Also (in calling | 638 // data remover renders it unusable. Also (in calling |
| 567 // ForceSdchDictionaryLoad()) servers as a smoke test for SDCH. | 639 // ForceSdchDictionaryLoad()) servers as a smoke test for SDCH. |
| 568 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, BrowsingDataRemover) { | 640 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, BrowsingDataRemover) { |
| 569 ASSERT_TRUE(ForceSdchDictionaryLoad(browser())); | 641 ASSERT_TRUE(ForceSdchDictionaryLoad(browser())); |
| 570 | 642 |
| 571 // Confirm browsing data remover without removing the cache leaves | 643 // Confirm browsing data remover without removing the cache leaves |
| 572 // SDCH alone. | 644 // SDCH alone. |
| 573 BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_ALL & | 645 BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_ALL & |
| 574 ~BrowsingDataRemover::REMOVE_CACHE); | 646 ~BrowsingDataRemover::REMOVE_CACHE); |
| 575 bool sdch_encoding_used = false; | 647 bool sdch_encoding_used = false; |
| 576 ASSERT_TRUE(GetData(&sdch_encoding_used)); | 648 ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| 577 EXPECT_TRUE(sdch_encoding_used); | 649 EXPECT_TRUE(sdch_encoding_used); |
| 578 | 650 |
| 579 // Confirm browsing data remover removing the cache clears SDCH state. | 651 // Confirm browsing data remover removing the cache clears SDCH state. |
| 580 BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_CACHE); | 652 BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_CACHE); |
| 581 sdch_encoding_used = false; | 653 sdch_encoding_used = false; |
| 582 ASSERT_TRUE(GetData(&sdch_encoding_used)); | 654 ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| 583 EXPECT_FALSE(sdch_encoding_used); | 655 EXPECT_FALSE(sdch_encoding_used); |
| 584 } | 656 } |
| 585 | 657 |
| 586 // Confirm dictionaries not visible in other profiles. | 658 // Confirm dictionaries not visible in other profiles. |
| 587 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, Isolation) { | 659 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, Isolation) { |
| 588 ASSERT_TRUE(ForceSdchDictionaryLoad(browser())); | 660 ASSERT_TRUE(ForceSdchDictionaryLoad(browser())); |
| 589 ASSERT_TRUE(SetupSecondBrowser()); | 661 ASSERT_TRUE(SetupSecondBrowser()); |
| 662 ASSERT_TRUE(SetupIncognitoBrowser()); |
| 590 | 663 |
| 591 // Data fetches from incognito or separate profiles should not be SDCH | 664 // Data fetches from incognito or separate profiles should not be SDCH |
| 592 // encoded. | 665 // encoded. |
| 593 bool sdch_encoding_used = true; | 666 bool sdch_encoding_used = true; |
| 594 Browser* incognito_browser = CreateIncognitoBrowser(); | |
| 595 EXPECT_TRUE(GetDataDetailed( | 667 EXPECT_TRUE(GetDataDetailed( |
| 596 incognito_browser->profile()->GetRequestContext(), | 668 incognito_browser()->profile()->GetRequestContext(), |
| 597 &sdch_encoding_used)); | 669 &sdch_encoding_used)); |
| 598 EXPECT_FALSE(sdch_encoding_used); | 670 EXPECT_FALSE(sdch_encoding_used); |
| 599 | 671 |
| 600 sdch_encoding_used = true; | 672 sdch_encoding_used = true; |
| 601 EXPECT_TRUE(GetDataDetailed( | 673 EXPECT_TRUE(GetDataDetailed( |
| 602 second_browser()->profile()->GetRequestContext(), &sdch_encoding_used)); | 674 second_browser()->profile()->GetRequestContext(), &sdch_encoding_used)); |
| 603 EXPECT_FALSE(sdch_encoding_used); | 675 EXPECT_FALSE(sdch_encoding_used); |
| 604 } | 676 } |
| 605 | 677 |
| 606 // Confirm a dictionary loaded in incognito isn't visible in the main profile. | 678 // Confirm a dictionary loaded in incognito isn't visible in the main profile. |
| 607 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, ReverseIsolation) { | 679 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, ReverseIsolation) { |
| 608 Browser* incognito_browser = CreateIncognitoBrowser(); | 680 ASSERT_TRUE(SetupIncognitoBrowser()); |
| 609 ASSERT_TRUE(ForceSdchDictionaryLoad(incognito_browser)); | 681 ASSERT_TRUE(ForceSdchDictionaryLoad(incognito_browser())); |
| 610 | 682 |
| 611 // Data fetches on main browser should not be SDCH encoded. | 683 // Data fetches on main browser should not be SDCH encoded. |
| 612 bool sdch_encoding_used = true; | 684 bool sdch_encoding_used = true; |
| 613 ASSERT_TRUE(GetData(&sdch_encoding_used)); | 685 ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| 614 EXPECT_FALSE(sdch_encoding_used); | 686 EXPECT_FALSE(sdch_encoding_used); |
| 615 } | 687 } |
| 616 | 688 |
| 617 } // namespace | 689 } // namespace |
| OLD | NEW |