Chromium Code Reviews| Index: chrome/browser/net/sdch_browsertest.cc |
| diff --git a/chrome/browser/net/sdch_browsertest.cc b/chrome/browser/net/sdch_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..99451f244a9f1275485177de53fdb1b8768e3777 |
| --- /dev/null |
| +++ b/chrome/browser/net/sdch_browsertest.cc |
| @@ -0,0 +1,753 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// End-to-end SDCH tests. Uses the embedded test server to return SDCH |
| +// results |
| + |
| +#include "base/base64.h" |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/command_line.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/path_service.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/browsing_data/browsing_data_helper.h" |
| +#include "chrome/browser/browsing_data/browsing_data_remover.h" |
| +#include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_tabstrip.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "crypto/sha2.h" |
| +#include "net/base/sdch_manager.h" |
| +#include "net/http/http_response_headers.h" |
| +#include "net/test/embedded_test_server/embedded_test_server.h" |
| +#include "net/test/embedded_test_server/http_request.h" |
| +#include "net/test/embedded_test_server/http_response.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| +#include "net/url_request/url_request_context.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "sdch/open-vcdiff/src/google/vcencoder.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +typedef std::vector<net::test_server::HttpRequest> RequestVector; |
| +typedef std::map<std::string, std::string> HttpRequestHeaderMap; |
| + |
| +// Scanns in a case-insensitive way for |header| in |map|, |
| +// returning true if found and setting |*value| to the value |
| +// of that header. Does not handle multiple instances of the same |
| +// header. |
| +bool RequestContainsHeader(const HttpRequestHeaderMap& map, |
|
mef
2014/07/10 17:08:17
Should it be called GetRequestHeader?
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
Done.
|
| + const char* header, |
| + std::string* value) { |
| + for (HttpRequestHeaderMap::const_iterator it = map.begin(); |
| + it != map.end(); ++it) { |
| + |
| + if (!base::strcasecmp(it->first.c_str(), header)) { |
| + *value = it->second; |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +bool ResponseContainsHeaderValue(const net::HttpResponseHeaders& headers, |
| + const char* header, |
| + const char* value) { |
| + std::string iterated_value; |
| + void *iter = NULL; |
| + while (headers.EnumerateHeader(&iter, header, &iterated_value)) { |
| + if (!base::strcasecmp(iterated_value.c_str(), value)) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +// Class that bundles responses for an EmbeddedTestServer(). |
| +// Dictionary is at <domain>/dict, data at <domain>/data. |
| +// The data is sent SDCH encoded if that's allowed by protoocol. |
| +class SdchResponseHandler { |
| + public: |
| + static const char kData[]; |
| + static const char kDictionaryContents[]; |
| + |
| + // Do initial preparation so that SDCH requests can be handled. |
| + explicit SdchResponseHandler(std::string domain) |
| + : cache_sdch_response_(false), |
| + weak_ptr_factory_(this) { |
| + // Dictionary |
| + sdch_dictionary_contents_ = "Domain: "; |
| + sdch_dictionary_contents_ += domain; |
| + sdch_dictionary_contents_ += "\n\n"; |
| + sdch_dictionary_contents_ += kDictionaryContents; |
| + |
| + // Dictionary hash for client and server. |
| + char binary_hash[32]; |
| + crypto::SHA256HashString(sdch_dictionary_contents_, binary_hash, |
| + sizeof(binary_hash)); |
| + base::Base64Encode(std::string(&binary_hash[0], 6), |
| + &dictionary_client_hash_); |
| + for (size_t i = 0; i < dictionary_client_hash_.size(); ++i) { |
| + if (dictionary_client_hash_[i] == '+') |
| + dictionary_client_hash_[i] = '-'; |
| + if (dictionary_client_hash_[i] == '/') |
| + dictionary_client_hash_[i] = '_'; |
| + } |
| + base::Base64Encode(std::string(&binary_hash[6], 6), |
| + &dictionary_server_hash_); |
| + for (size_t i = 0; i < dictionary_server_hash_.size(); ++i) { |
|
mef
2014/07/10 17:08:17
Why do we need this translation? And if we do, I s
Randy Smith (Not in Mondays)
2014/07/21 19:26:45
Sorry, didn't come back and deal with a TODO. I'v
|
| + if (dictionary_client_hash_[i] == '+') |
| + dictionary_client_hash_[i] = '-'; |
| + if (dictionary_client_hash_[i] == '/') |
| + dictionary_client_hash_[i] = '_'; |
| + } |
| + |
| + // Encoded response. |
| + open_vcdiff::HashedDictionary vcdiff_dictionary( |
| + kDictionaryContents, strlen(kDictionaryContents)); |
| + bool result = vcdiff_dictionary.Init(); |
| + DCHECK(result); |
| + open_vcdiff::VCDiffStreamingEncoder encoder(&vcdiff_dictionary, 0, false); |
| + encoded_data_ = dictionary_server_hash_; |
| + encoded_data_ += '\0'; |
| + result = encoder.StartEncoding(&encoded_data_); |
| + DCHECK(result); |
| + result = encoder.EncodeChunk(kData, strlen(kData), &encoded_data_); |
| + DCHECK(result); |
| + result = encoder.FinishEncoding(&encoded_data_); |
| + DCHECK(result); |
| + } |
| + |
| + static bool ClientIsAdvertisingSdchEncoding(const HttpRequestHeaderMap& map) { |
| + std::string value; |
| + if (!RequestContainsHeader(map, "accept-encoding", &value)) |
| + return false; |
| + std::string::iterator word_start = value.begin(); |
| + do { |
| + std::string::iterator it(word_start); |
| + while (it != value.end() && IsAsciiAlpha(*it)) |
|
mef
2014/07/10 17:08:17
This construct with nested loops is a bit hard to
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
Just that. I've added a comment for clarity; let
|
| + ++it; |
| + if (it != word_start) { |
| + if (!base::strcasecmp(std::string(word_start,it).c_str(), "sdch")) |
| + return true; |
| + } |
| + while (it != value.end() && !IsAsciiAlpha(*it)) |
| + ++it; |
| + word_start = it; |
| + } while (word_start != value.end()); |
| + return false; |
| + } |
| + |
| + bool ShouldRespondWithSdchEncoding(const HttpRequestHeaderMap& map) { |
| + std::string value; |
| + if (!RequestContainsHeader(map, "avail-dictionary", &value)) |
| + return false; |
| + return value == dictionary_client_hash_; |
| + } |
| + |
| + static std::string DictionaryClientHash() { |
| + char binary_hash[32]; |
| + crypto::SHA256HashString(kDictionaryContents, |
| + binary_hash, sizeof(binary_hash)); |
| + std::string client_hash; |
| + // TODO(rdsmith): This doesn't appear to be from the spec; figure out |
|
mef
2014/07/10 17:08:17
Umm, so we make 32 byte hash, but only use first 6
Randy Smith (Not in Mondays)
2014/07/21 19:26:45
The spec says that the dictionary is identified by
|
| + // where it comes from. |
| + base::Base64Encode(std::string(&binary_hash[0], 6), &client_hash); |
| + for (size_t i = 0; i < client_hash.size(); ++i) { |
|
mef
2014/07/10 17:08:17
one more translation that deserves a function. :-)
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
Actually, it's redundant with the earlier one :-}.
|
| + if (client_hash[i] == '+') |
| + client_hash[i] = '-'; |
| + else if (client_hash[i] == '/') |
| + client_hash[i] = '_'; |
| + } |
| + return client_hash; |
| + } |
| + |
| + scoped_ptr<net::test_server::HttpResponse> HandleRequest( |
| + const net::test_server::HttpRequest& request) { |
| + // We explicitly allow either GET or POST methods. |
|
mef
2014/07/10 17:08:17
Not sure how about meaning/relevance of this comme
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
It used to be a DCHECK that we only accept GET. O
|
| + request_vector_.push_back(request); |
| + |
| + scoped_ptr<net::test_server::BasicHttpResponse> response( |
| + new net::test_server::BasicHttpResponse); |
| + if (request.relative_url == "/data") { |
| + if (ShouldRespondWithSdchEncoding(request.headers)) { |
| + DCHECK_NE(encoded_data_, ""); |
| + response->set_content_type("text/html"); |
| + response->set_content(encoded_data_); |
| + response->AddCustomHeader("Content-Encoding", "sdch"); |
| + // We allow tests to set caching on the sdch response, |
| + // so that we can force an encoded response with no |
| + // dictionary. |
| + if (cache_sdch_response_) |
| + response->AddCustomHeader("Cache-Control", "max-age=3600"); |
| + else |
| + response->AddCustomHeader("Cache-Control", "no-store"); |
| + } else { |
| + response->set_content_type("text/plain"); |
| + response->set_content(kData); |
| + if (ClientIsAdvertisingSdchEncoding(request.headers)) |
| + response->AddCustomHeader("Get-Dictionary", "/dict"); |
| + // We never cache the plain data response, to make it |
| + // easy to refresh after we get the dictionary. |
| + response->AddCustomHeader("Cache-Control", "no-store"); |
| + } |
| + } else { |
| + DCHECK_EQ(request.relative_url, "/dict"); |
| + DCHECK_NE(sdch_dictionary_contents_, ""); |
| + response->set_content_type("application/x-sdch-dictionary"); |
| + response->set_content(sdch_dictionary_contents_); |
| + } |
| + std::vector<base::Closure> callbacks; |
| + callbacks.swap(callback_vector_); |
|
mef
2014/07/10 17:08:17
why swap?
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
It means that there isn't any racing on access to
|
| + for (std::vector<base::Closure>::iterator it = callbacks.begin(); |
| + it != callbacks.end(); ++it) { |
| + it->Run(); |
| + } |
| + return response.PassAs<net::test_server::HttpResponse>(); |
| + } |
| + |
| + void WaitAndGetRequestVector(int num_requests, |
| + base::Closure callback, |
| + RequestVector* v) { |
| + DCHECK_LT(0, num_requests); |
| + if (static_cast<size_t>(num_requests) > request_vector_.size()) { |
| + callback_vector_.push_back( |
| + base::Bind(&SdchResponseHandler::WaitAndGetRequestVector, |
| + weak_ptr_factory_.GetWeakPtr(), num_requests, |
| + callback, v)); |
| + return; |
| + } |
| + *v = request_vector_; |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, callback); |
| + } |
| + |
| + void set_cache_sdch_response(bool cache_sdch_response) { |
| + cache_sdch_response_ = cache_sdch_response; |
| + } |
| + |
| + private: |
| + bool cache_sdch_response_; |
| + std::string encoded_data_; |
| + std::string sdch_dictionary_contents_; |
| + std::string dictionary_client_hash_; |
| + std::string dictionary_server_hash_; |
| + RequestVector request_vector_; |
| + std::vector<base::Closure> callback_vector_; |
| + base::WeakPtrFactory<SdchResponseHandler> weak_ptr_factory_; |
| +}; |
| + |
| +// Credit Alfred, Lord Tennyson |
| +const char SdchResponseHandler::kData[] = "<html><body><pre>" |
|
mef
2014/07/10 17:08:17
Maybe they don't have to be members of the SdchRes
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
Done.
|
| + "There lies the port; the vessel puffs her sail:\n" |
| + "There gloom the dark, broad seas. My mariners,\n" |
| + "Souls that have toil'd, and wrought, and thought with me—\n" |
| + "That ever with a frolic welcome took\n" |
| + "The thunder and the sunshine, and opposed\n" |
| + "Free hearts, free foreheads—you and I are old;\n" |
| + "Old age hath yet his honour and his toil;\n" |
| + "Death closes all: but something ere the end,\n" |
| + "Some work of noble note, may yet be done,\n" |
| + "Not unbecoming men that strove with Gods.\n" |
| + "The lights begin to twinkle from the rocks:\n" |
| + "The long day wanes: the slow moon climbs: the deep\n" |
| + "Moans round with many voices. Come, my friends,\n" |
| + "'T is not too late to seek a newer world.\n" |
| + "Push off, and sitting well in order smite\n" |
| + "The sounding furrows; for my purpose holds\n" |
| + "To sail beyond the sunset, and the baths\n" |
| + "Of all the western stars, until I die.\n" |
| + "It may be that the gulfs will wash us down:\n" |
| + "It may be we shall touch the Happy Isles,\n" |
| + "And see the great Achilles, whom we knew.\n" |
| + "Tho' much is taken, much abides; and tho'\n" |
| + "We are not now that strength which in old days\n" |
| + "Moved earth and heaven, that which we are, we are;\n" |
| + "One equal temper of heroic hearts,\n" |
| + "Made weak by time and fate, but strong in will\n" |
| + "To strive, to seek, to find, and not to yield.\n" |
| + "</pre></body></html>"; |
| + |
| +// Random selection of lines from above, to allow some encoding, but |
| +// not a trivial encoding. |
| +const char SdchResponseHandler::kDictionaryContents[] = |
| + "The thunder and the sunshine, and opposed\n" |
| + "To sail beyond the sunset, and the baths\n" |
| + "Of all the western stars, until I die.\n" |
| + "Made weak by time and fate, but strong in will\n" |
| + "Moans round with many voices. Come, my friends,\n" |
| + "The lights begin to twinkle from the rocks:"; |
| + |
| +class SdchBrowserTest : public InProcessBrowserTest, net::URLFetcherDelegate { |
| + public: |
| + static const char kTestHost[]; |
| + |
| + SdchBrowserTest() |
| + : response_handler_(kTestHost), |
| + url_request_context_getter_(NULL), |
| + fetcher_response_code_(0), |
| + url_fetch_complete_(false), |
| + waiting_(false) {} |
| + |
| + // ** Helper functions for fetching data. |
| + |
| + void FetchUrlDetailed(GURL url, net::URLRequestContextGetter* getter, |
| + bool use_post) { |
| + url_fetch_complete_ = false; |
| + fetcher_status_ = net::URLRequestStatus(); |
| + fetcher_response_code_ = 0; |
| + fetcher_response_headers_ = NULL; |
| + fetcher_response_contents_ = ""; |
| + fetcher_.reset(net::URLFetcher::Create( |
| + url, use_post ? net::URLFetcher::POST : net::URLFetcher::GET, this)); |
| + if (use_post) |
| + fetcher_->SetUploadData("text/plain", "Simple content"); |
| + fetcher_->SetRequestContext(getter); |
| + fetcher_->Start(); |
| + if (!url_fetch_complete_) { |
| + waiting_ = true; |
| + content::RunMessageLoop(); |
| + waiting_ = false; |
| + } |
| + CHECK(url_fetch_complete_); |
| + } |
| + void FetchUrl(GURL url) { |
| + FetchUrlDetailed(url, url_request_context_getter_, false); |
| + } |
| + int fetcher_response_code() { return fetcher_response_code_; } |
| + const net::URLRequestStatus& fetcher_status() { return fetcher_status_; } |
| + const net::HttpResponseHeaders* fetcher_response_headers() { |
| + return fetcher_response_headers_; |
| + } |
| + std::string fetcher_response_contents() { return fetcher_response_contents_; } |
| + |
| + GURL data_url() { |
| + return GURL(base::StringPrintf( |
| + "http://%s:%d/data", kTestHost, test_server_port())); |
| + } |
| + |
| + // Get the data from the server. Return value is success/failure of the |
| + // data operation, |*sdch_encoding_used| indicates whether or not the |
| + // data was retrieved with sdch encoding. |
| + // This is done through FetchUrl(), so the various helper functions |
| + // will have valid status if it returns successfully. |
| + bool GetDataDetailed(net::URLRequestContextGetter* getter, |
| + bool use_post, |
| + bool* sdch_encoding_used) { |
| + FetchUrlDetailed(data_url(), getter, use_post); |
| + EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher_status().status()) |
| + << "Error code is " << fetcher_status().error(); |
| + EXPECT_EQ(200, fetcher_response_code()); |
| + EXPECT_EQ(SdchResponseHandler::kData, fetcher_response_contents()); |
| + |
| + if (net::URLRequestStatus::SUCCESS != fetcher_status().status() |
|
mef
2014/07/10 17:08:17
nits: I think || goes to first line and constant g
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
Whoops, quite right. Done.
|
| + || 200 != fetcher_response_code()) { |
| + *sdch_encoding_used = false; |
| + return false; |
| + } |
| + |
| + *sdch_encoding_used = ResponseContainsHeaderValue( |
| + *fetcher_response_headers(), |
| + "Content-Encoding", "sdch"); |
|
mef
2014/07/10 17:08:17
nit: would that fit on previous line?
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
Done.
|
| + |
| + if (SdchResponseHandler::kData != fetcher_response_contents()) |
|
mef
2014/07/10 17:08:17
Suggest: return fetcher_response_contents() == Sdc
Randy Smith (Not in Mondays)
2014/07/21 19:26:45
Done.
|
| + return false; |
| + |
| + return true; |
| + } |
| + bool GetData(bool* sdch_encoding_used) { |
| + return GetDataDetailed( |
| + url_request_context_getter_, false, sdch_encoding_used); |
| + } |
| + |
| + // ** Client information and control. |
| + |
| + int GetNumberOfDictionaryFetches() { |
| + int fetches = -1; |
| + content::BrowserThread::PostTaskAndReply( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SdchBrowserTest::GetNumberOfDictionaryFetchesOnIOThread, |
| + url_request_context_getter_, |
| + &fetches), |
| + base::MessageLoopForUI::current()->QuitClosure()); |
|
mef
2014/07/10 17:08:17
I've been told that RunLoops are preferred to Mess
Randy Smith (Not in Mondays)
2014/07/21 19:26:45
Huh. Neat. Done.
|
| + content::RunMessageLoop(); |
| + DCHECK_NE(-1, fetches); |
| + return fetches; |
| + } |
| + |
| + void BrowsingDataRemoveAndWait(int remove_mask) { |
| + BrowsingDataRemover* remover = BrowsingDataRemover::CreateForPeriod( |
| + browser()->profile(), BrowsingDataRemover::LAST_HOUR); |
| + BrowsingDataRemoverCompletionObserver completion_observer(remover); |
| + remover->Remove(remove_mask, BrowsingDataHelper::UNPROTECTED_WEB); |
| + completion_observer.BlockUntilCompletion(); |
| + } |
| + |
| + // Something of a cheat; nuke the dictionaries off the SdchManager without |
| + // touching the cache (which browsing data remover would do). |
| + void NukeSdchDictionaries() { |
| + content::BrowserThread::PostTaskAndReply( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SdchBrowserTest::NukeSdchDictionariesOnIOThread, |
| + url_request_context_getter_), |
| + base::MessageLoopForUI::current()->QuitClosure()); |
| + content::RunMessageLoop(); |
| + } |
| + |
| + // Create a second profile to work within multi-profile. |
| + Profile* CreateSecondProfile() { |
| + base::FilePath user_data_dir; |
| + PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| + |
| + if (!second_profile_data_dir_.CreateUniqueTempDirUnderPath(user_data_dir)) |
|
mef
2014/07/10 17:08:17
Does it ever get cleaned/deleted?
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
second_profile_data_dir_ is a ScopedTempDir, so wh
|
| + return NULL; |
| + |
| + Profile* second_profile = |
| + g_browser_process->profile_manager()->GetProfile( |
| + second_profile_data_dir_.path()); |
| + if (!second_profile) |
|
mef
2014/07/10 17:08:18
it would be NULL anyway...
Randy Smith (Not in Mondays)
2014/07/21 19:26:45
Good point; done.
|
| + return NULL; |
| + |
| + return second_profile; |
| + } |
| + |
| + Browser* CreateBrowserOnProfile(Profile* profile) { |
| + Browser* new_browser = |
|
mef
2014/07/10 17:08:18
DCHECK(profile)?
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
Done.
|
| + new Browser(Browser::CreateParams( |
| + profile, browser()->host_desktop_type())); |
| + chrome::AddSelectedTabWithURL(new_browser, |
| + GURL(url::kAboutBlankURL), |
| + content::PAGE_TRANSITION_AUTO_TOPLEVEL); |
| + content::WaitForLoadStop( |
| + new_browser->tab_strip_model()->GetActiveWebContents()); |
| + new_browser->window()->Show(); |
| + return new_browser; |
| + } |
| + |
| + // ** Server information and control. |
| + |
| + void WaitAndGetTestVector(int num_requests, RequestVector* result) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SdchResponseHandler::WaitAndGetRequestVector, |
| + base::Unretained(&response_handler_), |
| + num_requests, |
| + base::MessageLoopForUI::current()->QuitClosure(), |
| + result)); |
| + content::RunMessageLoop(); |
| + } |
| + |
| + int test_server_port() { return test_server_.port(); } |
| + |
| + void SetSdchCacheability(bool cache_sdch_response) { |
| + content::BrowserThread::PostTaskAndReply( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SdchResponseHandler::set_cache_sdch_response, |
| + base::Unretained(&response_handler_), |
| + cache_sdch_response), |
| + base::MessageLoopForUI::current()->QuitClosure()); |
| + content::RunMessageLoop(); |
| + } |
| + |
| + private: |
| + // InProcessBrowserTest |
| + virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { |
| + command_line->AppendSwitchASCII( |
| + switches::kHostResolverRules, |
| + "MAP " + std::string(kTestHost) + " 127.0.0.1"); |
| + } |
| + |
| + virtual void SetUpOnMainThread() OVERRIDE { |
| + test_server_.RegisterRequestHandler( |
| + base::Bind(&SdchResponseHandler::HandleRequest, |
| + base::Unretained(&response_handler_))); |
| + CHECK(test_server_.InitializeAndWaitUntilReady()); |
| + url_request_context_getter_ = browser()->profile()->GetRequestContext(); |
| + } |
| + |
| + virtual void TearDownOnMainThread() OVERRIDE { |
| + CHECK(test_server_.ShutdownAndWaitUntilComplete()); |
| + } |
| + |
| + static void NukeSdchDictionariesOnIOThread( |
| + net::URLRequestContextGetter* context_getter) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| + net::SdchManager* sdch_manager = |
| + context_getter->GetURLRequestContext()->sdch_manager(); |
| + DCHECK(sdch_manager); |
| + sdch_manager->ClearData(); |
| + } |
| + |
| + static void GetNumberOfDictionaryFetchesOnIOThread( |
| + net::URLRequestContextGetter* url_request_context_getter, |
| + int* result) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| + net::SdchManager* sdch_manager = |
| + url_request_context_getter->GetURLRequestContext()->sdch_manager(); |
| + DCHECK(sdch_manager); |
| + *result = sdch_manager->GetFetchesRequestedForTesting(); |
| + } |
| + |
| + // URLFetcherDelegate |
| + virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { |
| + fetcher_status_ = source->GetStatus(); |
| + if (fetcher_status_.status() == net::URLRequestStatus::SUCCESS) { |
| + fetcher_response_code_ = source->GetResponseCode(); |
| + fetcher_response_headers_ = source->GetResponseHeaders(); |
| + CHECK(source->GetResponseAsString(&fetcher_response_contents_)); |
| + } |
| + url_fetch_complete_ = true; |
| + if (waiting_) |
| + base::MessageLoopForUI::current()->Quit(); |
| + } |
| + |
| + SdchResponseHandler response_handler_; |
| + net::test_server::EmbeddedTestServer test_server_; |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| + scoped_ptr<net::URLFetcher> fetcher_; |
| + net::URLRequestStatus fetcher_status_; |
| + int fetcher_response_code_; |
| + net::HttpResponseHeaders* fetcher_response_headers_; |
| + std::string fetcher_response_contents_; |
| + bool url_fetch_complete_; |
| + bool waiting_; |
| + base::ScopedTempDir second_profile_data_dir_; |
| +}; |
| + |
| +const char SdchBrowserTest::kTestHost[] = "our.test.host.com";; |
| + |
| +// Confirm the simplest SDCH pattern (get with no dictionary |
| +// advertises dictionary, browser then gets dictionary, next |
| +// get of original data uses dictionary) works. |
| +IN_PROC_BROWSER_TEST_F(SdchBrowserTest, Simple) { |
| + bool sdch_encoding_used = true; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| + |
| + // Confirm that we were told to get the dictionary |
| + const net::HttpResponseHeaders* headers = fetcher_response_headers(); |
| + std::string value; |
| + ASSERT_TRUE(headers->EnumerateHeader(NULL, "Get-Dictionary", &value)); |
| + |
| + // If the above didn't result in a dictionary fetch being queued, the |
| + // rest of the test will time out. Avoid that. |
| + ASSERT_EQ(1, GetNumberOfDictionaryFetches()); |
| + |
| + // Wait until the dictionary fetch actually happens. |
| + RequestVector request_vector; |
| + WaitAndGetTestVector(2, &request_vector); |
| + ASSERT_EQ(request_vector[1].relative_url, "/dict"); |
| + |
| + // Do a round trip to the server ignoring the encoding, presuming |
| + // that if we've gotten data to this thread, the dictionary's made |
| + // it into the SdchManager. |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + |
| + // Now data fetches should be SDCH encoded. |
| + sdch_encoding_used = false; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_TRUE(sdch_encoding_used); |
| +} |
| + |
| +// Confirm that after getting a dictionary, calling the browsing |
| +// data remover renders it unusable. |
| +IN_PROC_BROWSER_TEST_F(SdchBrowserTest, BrowsingDataRemover) { |
| + bool sdch_encoding_used = true; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| + |
| + // If the above didn't result in a dictionary fetch being queued, the |
| + // rest of the test will time out. Avoid that. |
| + ASSERT_EQ(1, GetNumberOfDictionaryFetches()); |
| + |
| + // Wait until the dictionary fetch actually happens. |
|
mef
2014/07/11 14:34:36
There seems to be a bit of boilerplate that is rep
Randy Smith (Not in Mondays)
2014/07/21 19:26:46
Done.
|
| + RequestVector request_vector; |
| + WaitAndGetTestVector(2, &request_vector); |
| + ASSERT_EQ(request_vector[1].relative_url, "/dict"); |
| + |
| + // Do a round trip to the server ignoring the encoding, presuming |
| + // that if we've gotten data to this thread, the dictionary's made |
| + // it into the SdchManager. |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + |
| + // Now data fetches should be SDCH encoded. |
| + sdch_encoding_used = false; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_TRUE(sdch_encoding_used); |
| + |
| + // Confirm browsing data remover without removing the cache leaves |
| + // SDCH alone. |
| + BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_ALL & |
| + ~BrowsingDataRemover::REMOVE_CACHE); |
| + sdch_encoding_used = false; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_TRUE(sdch_encoding_used); |
| + |
| + // Confirm browsing data remover removing the cache clears SDCH state. |
| + BrowsingDataRemoveAndWait(BrowsingDataRemover::REMOVE_CACHE); |
| + sdch_encoding_used = false; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| +} |
| + |
| +// Confirm that receiving an SDCH response when we don't have the |
| +// dictionary results in a meta-refresh. |
| +IN_PROC_BROWSER_TEST_F(SdchBrowserTest, MetaRefresh) { |
| + bool sdch_encoding_used = true; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| + |
| + // If the above didn't result in a dictionary fetch being queued, the |
| + // rest of the test will time out. Avoid that. |
| + ASSERT_EQ(1, GetNumberOfDictionaryFetches()); |
| + |
| + // Wait until the dictionary fetch actually happens. |
| + RequestVector request_vector; |
| + WaitAndGetTestVector(2, &request_vector); |
| + ASSERT_EQ(request_vector[1].relative_url, "/dict"); |
| + |
| + // Do a round trip to the server ignoring the encoding, presuming |
| + // that if we've gotten data to this thread, the dictionary's made |
| + // it into the SdchManager. |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + |
| + // We're now sure we have the dictionary. Get a copy of |
| + // the encoded data into our cache. |
| + SetSdchCacheability(true); |
| + sdch_encoding_used = false; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_TRUE(sdch_encoding_used); |
| + ASSERT_TRUE(ResponseContainsHeaderValue(*fetcher_response_headers(), |
| + "Cache-Control", |
| + "max-age=3600")); |
| + |
| + // Confirm the request vector looks at this point as expected. |
| + WaitAndGetTestVector(4, &request_vector); |
| + ASSERT_EQ(4u, request_vector.size()); |
| + ASSERT_EQ(request_vector[2].relative_url, "/data"); |
| + ASSERT_EQ(request_vector[3].relative_url, "/data"); |
| + |
| + NukeSdchDictionaries(); |
| + |
| + // Try to fetch the data. |
| + FetchUrl(data_url()); |
| + ASSERT_EQ(net::URLRequestStatus::SUCCESS, fetcher_status().status()) |
| + << "Error code is " << fetcher_status().error(); |
| + ASSERT_EQ(200, fetcher_response_code()); |
| + ASSERT_NE(SdchResponseHandler::kData, fetcher_response_contents()); |
| + // TODO(rdsmith): Interesting question as to whether the |
| + // fake-out from the network stack in the case of a failed |
| + // decode should claim to be "Content-Encoding: sdch" or not. |
| + EXPECT_TRUE(ResponseContainsHeaderValue(*fetcher_response_headers(), |
| + "Content-Encoding", |
| + "sdch")) |
| + << fetcher_response_contents(); |
| + std::string response_contents(fetcher_response_contents()); |
| + StringToLowerASCII(&response_contents); |
| + EXPECT_NE(string::npos, response_contents.find("meta http-equiv=\"refresh\"")) |
| + << "Actual response is: " << response_contents; |
| +} |
| + |
| +// Confirm dictionaries not visible in other profiles. |
| +IN_PROC_BROWSER_TEST_F(SdchBrowserTest, Isolation) { |
| + bool sdch_encoding_used = true; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| + |
| + // Confirm that we were told to get the dictionary |
| + const net::HttpResponseHeaders* headers = fetcher_response_headers(); |
| + std::string value; |
| + ASSERT_TRUE(headers->EnumerateHeader(NULL, "Get-Dictionary", &value)); |
| + |
| + // If the above didn't result in a dictionary fetch being queued, the |
| + // rest of the test will time out. Avoid that. |
| + ASSERT_EQ(1, GetNumberOfDictionaryFetches()); |
| + |
| + // Wait until the dictionary fetch actually happens. |
| + RequestVector request_vector; |
| + WaitAndGetTestVector(2, &request_vector); |
| + ASSERT_EQ(request_vector[1].relative_url, "/dict"); |
| + |
| + // Do a round trip to the server ignoring the encoding, presuming |
| + // that if we've gotten data to this thread, the dictionary's made |
| + // it into the SdchManager. |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + |
| + // Now data fetches should be SDCH encoded. |
| + sdch_encoding_used = false; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_TRUE(sdch_encoding_used); |
| + |
| + // And data fetches from incognito or separate profiles should not be. |
| + Browser* incognito_browser = CreateIncognitoBrowser(); |
| + sdch_encoding_used = true; |
| + EXPECT_TRUE(GetDataDetailed( |
| + incognito_browser->profile()->GetRequestContext(), |
|
mef
2014/07/11 14:34:36
Would dictionary be retrieved in IncognitoBrowser?
Randy Smith (Not in Mondays)
2014/07/21 19:26:45
It would be retrieved. Incognito profiles and sec
|
| + false, |
| + &sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| + |
| + Browser* new_browser = CreateBrowserOnProfile(CreateSecondProfile()); |
| + sdch_encoding_used = true; |
| + EXPECT_TRUE(GetDataDetailed( |
| + new_browser->profile()->GetRequestContext(), |
| + false, |
| + &sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| +} |
| + |
| +// Confirm we don't advertise dictionaries on posts. |
| +IN_PROC_BROWSER_TEST_F(SdchBrowserTest, NoPost) { |
| + bool sdch_encoding_used = true; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| + |
| + // Confirm that we were told to get the dictionary |
| + const net::HttpResponseHeaders* headers = fetcher_response_headers(); |
| + std::string value; |
| + ASSERT_TRUE(headers->EnumerateHeader(NULL, "Get-Dictionary", &value)); |
| + |
| + // If the above didn't result in a dictionary fetch being queued, the |
| + // rest of the test will time out. Avoid that. |
| + ASSERT_EQ(1, GetNumberOfDictionaryFetches()); |
| + |
| + // Wait until the dictionary fetch actually happens. |
| + RequestVector request_vector; |
| + WaitAndGetTestVector(2, &request_vector); |
| + ASSERT_EQ(request_vector[1].relative_url, "/dict"); |
| + |
| + // Do a round trip to the server ignoring the encoding, presuming |
| + // that if we've gotten data to this thread, the dictionary's made |
| + // it into the SdchManager. |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + |
| + // Now data fetches should be SDCH encoded. |
| + sdch_encoding_used = false; |
| + ASSERT_TRUE(GetData(&sdch_encoding_used)); |
| + EXPECT_TRUE(sdch_encoding_used); |
| + |
| + // And data fetches using post should not be. |
| + sdch_encoding_used = true; |
| + EXPECT_TRUE(GetDataDetailed( |
| + browser()->profile()->GetRequestContext(), |
| + true, |
| + &sdch_encoding_used)); |
| + EXPECT_FALSE(sdch_encoding_used); |
| +} |
| + |
| +} // namespace |