Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(825)

Side by Side Diff: chrome/browser/net/sdch_browsertest.cc

Issue 664263002: Restructure SDCH layering to allow more separation (observer/1->[0,n] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync'd to 303035. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 : public InProcessBrowserTest,
264 public net::URLFetcherDelegate,
265 public net::SdchObserver {
263 public: 266 public:
264 static const char kTestHost[]; 267 static const char kTestHost[];
265 268
266 SdchBrowserTest() 269 SdchBrowserTest()
267 : response_handler_(kTestHost), 270 : response_handler_(kTestHost),
268 url_request_context_getter_(NULL), 271 url_request_context_getter_(NULL),
269 url_fetch_complete_(false), 272 url_fetch_complete_(false),
270 waiting_(false) {} 273 waiting_(false) {}
271 274
272 // Helper functions for fetching data. 275 // Helper functions for fetching data.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 return GetDataDetailed(url_request_context_getter_.get(), 347 return GetDataDetailed(url_request_context_getter_.get(),
345 sdch_encoding_used); 348 sdch_encoding_used);
346 } 349 }
347 350
348 // Client information and control. 351 // Client information and control.
349 352
350 int GetNumberOfDictionaryFetches(Profile* profile) { 353 int GetNumberOfDictionaryFetches(Profile* profile) {
351 int fetches = -1; 354 int fetches = -1;
352 base::RunLoop run_loop; 355 base::RunLoop run_loop;
353 content::BrowserThread::PostTaskAndReply( 356 content::BrowserThread::PostTaskAndReply(
354 content::BrowserThread::IO, FROM_HERE, 357 content::BrowserThread::IO,
358 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
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,
415 FROM_HERE,
416 base::Bind(&SdchBrowserTest::SubscribeToSdchNotifications,
417 base::Unretained(this),
418 make_scoped_refptr(
419 second_browser_->profile()->GetRequestContext())));
420
421 return true;
422 }
423
424 bool SetupIncognitoBrowser() {
425 incognito_browser_ = CreateIncognitoBrowser();
426
427 if (!incognito_browser_)
428 return false;
429
430 content::BrowserThread::PostTask(
431 content::BrowserThread::IO,
432 FROM_HERE,
433 base::Bind(&SdchBrowserTest::SubscribeToSdchNotifications,
434 base::Unretained(this),
435 make_scoped_refptr(
436 incognito_browser_->profile()->GetRequestContext())));
437
408 return true; 438 return true;
409 } 439 }
410 440
411 Browser* second_browser() { return second_browser_; } 441 Browser* second_browser() { return second_browser_; }
442 Browser* incognito_browser() { return incognito_browser_; }
412 443
413 // Server information and control. 444 // Server information and control.
414 445
415 void WaitAndGetTestVector(int num_requests, RequestVector* result) { 446 void WaitAndGetTestVector(int num_requests, RequestVector* result) {
416 base::RunLoop run_loop; 447 base::RunLoop run_loop;
417 content::BrowserThread::PostTask( 448 content::BrowserThread::PostTask(
418 content::BrowserThread::IO, FROM_HERE, 449 content::BrowserThread::IO, FROM_HERE,
419 base::Bind(&SdchResponseHandler::WaitAndGetRequestVector, 450 base::Bind(&SdchResponseHandler::WaitAndGetRequestVector,
420 base::Unretained(&response_handler_), 451 base::Unretained(&response_handler_),
421 num_requests, 452 num_requests,
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 private: 533 private:
503 static void NukeSdchDictionariesOnIOThread( 534 static void NukeSdchDictionariesOnIOThread(
504 net::URLRequestContextGetter* context_getter) { 535 net::URLRequestContextGetter* context_getter) {
505 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 536 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
506 net::SdchManager* sdch_manager = 537 net::SdchManager* sdch_manager =
507 context_getter->GetURLRequestContext()->sdch_manager(); 538 context_getter->GetURLRequestContext()->sdch_manager();
508 DCHECK(sdch_manager); 539 DCHECK(sdch_manager);
509 sdch_manager->ClearData(); 540 sdch_manager->ClearData();
510 } 541 }
511 542
512 static void GetNumberOfDictionaryFetchesOnIOThread( 543 void GetNumberOfDictionaryFetchesOnIOThread(
513 net::URLRequestContextGetter* url_request_context_getter, 544 net::URLRequestContextGetter* context_getter,
514 int* result) { 545 int* result) {
515 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 546 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
516 net::SdchManager* sdch_manager = 547
517 url_request_context_getter->GetURLRequestContext()->sdch_manager(); 548 net::SdchManager* manager(
518 DCHECK(sdch_manager); 549 context_getter->GetURLRequestContext()->sdch_manager());
519 *result = sdch_manager->GetFetchesCountForTesting(); 550 DCHECK(fetch_counts_.end() != fetch_counts_.find(manager));
551
552 *result = fetch_counts_[manager];
520 } 553 }
521 554
522 // InProcessBrowserTest 555 // InProcessBrowserTest
523 void SetUpCommandLine(base::CommandLine* command_line) override { 556 void SetUpCommandLine(base::CommandLine* command_line) override {
524 command_line->AppendSwitchASCII( 557 command_line->AppendSwitchASCII(
525 switches::kHostResolverRules, 558 switches::kHostResolverRules,
526 "MAP " + std::string(kTestHost) + " 127.0.0.1"); 559 "MAP " + std::string(kTestHost) + " 127.0.0.1");
527 #if defined(OS_CHROMEOS) 560 #if defined(OS_CHROMEOS)
528 command_line->AppendSwitch( 561 command_line->AppendSwitch(
529 chromeos::switches::kIgnoreUserProfileMappingForTests); 562 chromeos::switches::kIgnoreUserProfileMappingForTests);
530 #endif 563 #endif
531 } 564 }
532 565
533 void SetUpOnMainThread() override { 566 void SetUpOnMainThread() override {
534 test_server_.RegisterRequestHandler( 567 test_server_.RegisterRequestHandler(
535 base::Bind(&SdchResponseHandler::HandleRequest, 568 base::Bind(&SdchResponseHandler::HandleRequest,
536 base::Unretained(&response_handler_))); 569 base::Unretained(&response_handler_)));
537 CHECK(test_server_.InitializeAndWaitUntilReady()); 570 CHECK(test_server_.InitializeAndWaitUntilReady());
538 url_request_context_getter_ = browser()->profile()->GetRequestContext(); 571 url_request_context_getter_ = browser()->profile()->GetRequestContext();
572
573 content::BrowserThread::PostTask(
574 content::BrowserThread::IO,
575 FROM_HERE,
576 base::Bind(&SdchBrowserTest::SubscribeToSdchNotifications,
577 base::Unretained(this),
578 url_request_context_getter_));
539 } 579 }
540 580
541 void TearDownOnMainThread() override { 581 void TearDownOnMainThread() override {
542 CHECK(test_server_.ShutdownAndWaitUntilComplete()); 582 CHECK(test_server_.ShutdownAndWaitUntilComplete());
583
584 content::BrowserThread::PostTask(
585 content::BrowserThread::IO,
586 FROM_HERE,
587 base::Bind(&SdchBrowserTest::UnsubscribeFromAllSdchNotifications,
588 base::Unretained(this)));
543 } 589 }
544 590
591 void SubscribeToSdchNotifications(
592 net::URLRequestContextGetter* context_getter) {
593 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
594
595 net::SdchManager* manager =
596 context_getter->GetURLRequestContext()->sdch_manager();
597 DCHECK(fetch_counts_.end() == fetch_counts_.find(manager));
598
599 fetch_counts_[manager] = 0;
600 manager->AddObserver(this);
601 }
602
603 void UnsubscribeFromAllSdchNotifications() {
604 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
605
606 for (auto it = fetch_counts_.begin(); it != fetch_counts_.end(); ++it)
607 it->first->RemoveObserver(this);
608
609 fetch_counts_.clear();
610 }
611
612 // SdchObserver
613 void OnGetDictionary(net::SdchManager* manager,
614 const GURL& request_url,
615 const GURL& dictionary_url) override {
616 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
617 DLOG(ERROR) << "Retrieving count of notifications from manager " << manager;
618 DCHECK(fetch_counts_.end() != fetch_counts_.find(manager));
619 ++fetch_counts_[manager];
620 }
621 void OnClearDictionaries(net::SdchManager* manager) override {}
622
545 // URLFetcherDelegate 623 // URLFetcherDelegate
546 void OnURLFetchComplete(const net::URLFetcher* source) override { 624 void OnURLFetchComplete(const net::URLFetcher* source) override {
547 url_fetch_complete_ = true; 625 url_fetch_complete_ = true;
548 if (waiting_) 626 if (waiting_)
549 base::MessageLoopForUI::current()->Quit(); 627 base::MessageLoopForUI::current()->Quit();
550 } 628 }
551 629
552 SdchResponseHandler response_handler_; 630 SdchResponseHandler response_handler_;
553 net::test_server::EmbeddedTestServer test_server_; 631 net::test_server::EmbeddedTestServer test_server_;
554 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 632 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
555 scoped_ptr<net::URLFetcher> fetcher_; 633 scoped_ptr<net::URLFetcher> fetcher_;
556 bool url_fetch_complete_; 634 bool url_fetch_complete_;
557 bool waiting_; 635 bool waiting_;
558 base::ScopedTempDir second_profile_data_dir_; 636 base::ScopedTempDir second_profile_data_dir_;
559 Profile* second_profile_; 637 Profile* second_profile_;
560 Browser* second_browser_; 638 Browser* second_browser_;
639 Browser* incognito_browser_;
640
641 // IO Thread access only.
642 std::map<net::SdchManager*, int> fetch_counts_;
561 }; 643 };
562 644
563 const char SdchBrowserTest::kTestHost[] = "our.test.host.com"; 645 const char SdchBrowserTest::kTestHost[] = "our.test.host.com";
564 646
565 // Confirm that after getting a dictionary, calling the browsing 647 // Confirm that after getting a dictionary, calling the browsing
566 // data remover renders it unusable. Also (in calling 648 // data remover renders it unusable. Also (in calling
567 // ForceSdchDictionaryLoad()) servers as a smoke test for SDCH. 649 // ForceSdchDictionaryLoad()) servers as a smoke test for SDCH.
568 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, BrowsingDataRemover) { 650 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, BrowsingDataRemover) {
569 ASSERT_TRUE(ForceSdchDictionaryLoad(browser())); 651 ASSERT_TRUE(ForceSdchDictionaryLoad(browser()));
570 652
571 // Confirm browsing data remover without removing the cache leaves 653 // Confirm browsing data remover without removing the cache leaves
572 // SDCH alone. 654 // SDCH alone.
573 BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_ALL & 655 BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_ALL &
574 ~BrowsingDataRemover::REMOVE_CACHE); 656 ~BrowsingDataRemover::REMOVE_CACHE);
575 bool sdch_encoding_used = false; 657 bool sdch_encoding_used = false;
576 ASSERT_TRUE(GetData(&sdch_encoding_used)); 658 ASSERT_TRUE(GetData(&sdch_encoding_used));
577 EXPECT_TRUE(sdch_encoding_used); 659 EXPECT_TRUE(sdch_encoding_used);
578 660
579 // Confirm browsing data remover removing the cache clears SDCH state. 661 // Confirm browsing data remover removing the cache clears SDCH state.
580 BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_CACHE); 662 BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_CACHE);
581 sdch_encoding_used = false; 663 sdch_encoding_used = false;
582 ASSERT_TRUE(GetData(&sdch_encoding_used)); 664 ASSERT_TRUE(GetData(&sdch_encoding_used));
583 EXPECT_FALSE(sdch_encoding_used); 665 EXPECT_FALSE(sdch_encoding_used);
584 } 666 }
585 667
586 // Confirm dictionaries not visible in other profiles. 668 // Confirm dictionaries not visible in other profiles.
587 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, Isolation) { 669 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, Isolation) {
588 ASSERT_TRUE(ForceSdchDictionaryLoad(browser())); 670 ASSERT_TRUE(ForceSdchDictionaryLoad(browser()));
589 ASSERT_TRUE(SetupSecondBrowser()); 671 ASSERT_TRUE(SetupSecondBrowser());
672 ASSERT_TRUE(SetupIncognitoBrowser());
590 673
591 // Data fetches from incognito or separate profiles should not be SDCH 674 // Data fetches from incognito or separate profiles should not be SDCH
592 // encoded. 675 // encoded.
593 bool sdch_encoding_used = true; 676 bool sdch_encoding_used = true;
594 Browser* incognito_browser = CreateIncognitoBrowser(); 677 EXPECT_TRUE(
595 EXPECT_TRUE(GetDataDetailed( 678 GetDataDetailed(incognito_browser()->profile()->GetRequestContext(),
596 incognito_browser->profile()->GetRequestContext(), 679 &sdch_encoding_used));
597 &sdch_encoding_used));
598 EXPECT_FALSE(sdch_encoding_used); 680 EXPECT_FALSE(sdch_encoding_used);
599 681
600 sdch_encoding_used = true; 682 sdch_encoding_used = true;
601 EXPECT_TRUE(GetDataDetailed( 683 EXPECT_TRUE(GetDataDetailed(
602 second_browser()->profile()->GetRequestContext(), &sdch_encoding_used)); 684 second_browser()->profile()->GetRequestContext(), &sdch_encoding_used));
603 EXPECT_FALSE(sdch_encoding_used); 685 EXPECT_FALSE(sdch_encoding_used);
604 } 686 }
605 687
606 // Confirm a dictionary loaded in incognito isn't visible in the main profile. 688 // Confirm a dictionary loaded in incognito isn't visible in the main profile.
607 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, ReverseIsolation) { 689 IN_PROC_BROWSER_TEST_F(SdchBrowserTest, ReverseIsolation) {
608 Browser* incognito_browser = CreateIncognitoBrowser(); 690 ASSERT_TRUE(SetupIncognitoBrowser());
609 ASSERT_TRUE(ForceSdchDictionaryLoad(incognito_browser)); 691 ASSERT_TRUE(ForceSdchDictionaryLoad(incognito_browser()));
610 692
611 // Data fetches on main browser should not be SDCH encoded. 693 // Data fetches on main browser should not be SDCH encoded.
612 bool sdch_encoding_used = true; 694 bool sdch_encoding_used = true;
613 ASSERT_TRUE(GetData(&sdch_encoding_used)); 695 ASSERT_TRUE(GetData(&sdch_encoding_used));
614 EXPECT_FALSE(sdch_encoding_used); 696 EXPECT_FALSE(sdch_encoding_used);
615 } 697 }
616 698
617 } // namespace 699 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/net/chrome_sdch_policy.cc ('k') | chrome/browser/profiles/off_the_record_profile_io_data.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698