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

Side by Side Diff: chrome/browser/policy/policy_browsertest.cc

Issue 11293252: Change Interceptors into URLRequestJobFactory::ProtocolHandlers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 8 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include <algorithm> 5 #include <algorithm>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 }; 141 };
142 142
143 // Filters requests to the hosts in |urls| and redirects them to the test data 143 // Filters requests to the hosts in |urls| and redirects them to the test data
144 // dir through URLRequestMockHTTPJobs. 144 // dir through URLRequestMockHTTPJobs.
145 void RedirectHostsToTestData(const char* const urls[], size_t size) { 145 void RedirectHostsToTestData(const char* const urls[], size_t size) {
146 // Map the given hosts to the test data dir. 146 // Map the given hosts to the test data dir.
147 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 147 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
148 for (size_t i = 0; i < size; ++i) { 148 for (size_t i = 0; i < size; ++i) {
149 const GURL url(urls[i]); 149 const GURL url(urls[i]);
150 EXPECT_TRUE(url.is_valid()); 150 EXPECT_TRUE(url.is_valid());
151 filter->AddHostnameHandler(url.scheme(), url.host(), 151 filter->AddUrlHandler(url, URLRequestMockHTTPJob::Factory);
152 URLRequestMockHTTPJob::Factory);
153 } 152 }
154 } 153 }
155 154
155 // Remove filters for requests to the hosts in |urls|.
156 void UndoRedirectHostsToTestData(const char* const urls[], size_t size) {
157 // Map the given hosts to the test data dir.
158 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
159 for (size_t i = 0; i < size; ++i) {
160 const GURL url(urls[i]);
161 EXPECT_TRUE(url.is_valid());
162 filter->RemoveUrlHandler(url);
163 }
164 }
165
156 // Fails requests using ERR_CONNECTION_RESET. 166 // Fails requests using ERR_CONNECTION_RESET.
157 net::URLRequestJob* FailedJobFactory( 167 net::URLRequestJob* FailedJobFactory(
158 net::URLRequest* request, 168 net::URLRequest* request,
159 net::NetworkDelegate* network_delegate, 169 net::NetworkDelegate* network_delegate,
160 const std::string& scheme) { 170 const std::string& scheme) {
161 return new content::URLRequestFailedJob( 171 return new content::URLRequestFailedJob(
162 request, network_delegate, net::ERR_CONNECTION_RESET); 172 request, network_delegate, net::ERR_CONNECTION_RESET);
163 } 173 }
164 174
165 // Filters requests to the |host| such that they fail. Run on IO thread. 175 // While |MakeRequestFail| is in scope URLRequests to |host| will fail.
166 void MakeRequestFailOnIO(const std::string& host) { 176 class MakeRequestFail {
167 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 177 public:
168 filter->AddHostnameHandler("http", host, &FailedJobFactory); 178 // Sets up the filter on IO thread such that requests to |host| fail.
169 filter->AddHostnameHandler("https", host, &FailedJobFactory); 179 explicit MakeRequestFail(const std::string& host) : host_(host) {
170 } 180 BrowserThread::PostTaskAndReply(
181 BrowserThread::IO, FROM_HERE,
182 base::Bind(MakeRequestFailOnIO, host_),
183 MessageLoop::QuitClosure());
184 content::RunMessageLoop();
185 }
186 ~MakeRequestFail() {
187 BrowserThread::PostTaskAndReply(
188 BrowserThread::IO, FROM_HERE,
189 base::Bind(UndoMakeRequestFailOnIO, host_),
190 MessageLoop::QuitClosure());
191 content::RunMessageLoop();
192 }
171 193
172 // Sets up the filter on IO thread such that requests to |host| fail. 194 private:
173 void MakeRequestFail(const std::string& host) { 195 // Filters requests to the |host| such that they fail. Run on IO thread.
174 BrowserThread::PostTaskAndReply( 196 static void MakeRequestFailOnIO(const std::string& host) {
175 BrowserThread::IO, FROM_HERE, 197 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
176 base::Bind(MakeRequestFailOnIO, host), 198 filter->AddHostnameHandler("http", host, &FailedJobFactory);
177 MessageLoop::QuitClosure()); 199 filter->AddHostnameHandler("https", host, &FailedJobFactory);
178 content::RunMessageLoop(); 200 }
179 } 201
202 // Remove filters for requests to the |host|. Run on IO thread.
203 static void UndoMakeRequestFailOnIO(const std::string& host) {
204 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
205 filter->RemoveHostnameHandler("http", host);
206 filter->RemoveHostnameHandler("https", host);
207 }
208
209 const std::string host_;
210 };
180 211
181 // Verifies that the given url |spec| can be opened. This assumes that |spec| 212 // Verifies that the given url |spec| can be opened. This assumes that |spec|
182 // points at empty.html in the test data dir. 213 // points at empty.html in the test data dir.
183 void CheckCanOpenURL(Browser* browser, const char* spec) { 214 void CheckCanOpenURL(Browser* browser, const char* spec) {
184 GURL url(spec); 215 GURL url(spec);
185 ui_test_utils::NavigateToURL(browser, url); 216 ui_test_utils::NavigateToURL(browser, url);
186 content::WebContents* contents = chrome::GetActiveWebContents(browser); 217 content::WebContents* contents = chrome::GetActiveWebContents(browser);
187 EXPECT_EQ(url, contents->GetURL()); 218 EXPECT_EQ(url, contents->GetURL());
188 EXPECT_EQ(net::FormatUrl(url, std::string()), contents->GetTitle()); 219 EXPECT_EQ(net::FormatUrl(url, std::string()), contents->GetTitle());
189 } 220 }
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true)); 639 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true));
609 provider_.UpdateChromePolicy(policies); 640 provider_.UpdateChromePolicy(policies);
610 } 641 }
611 642
612 IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) { 643 IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) {
613 // Verify that the cookie is gone. 644 // Verify that the cookie is gone.
614 EXPECT_TRUE(GetCookies(browser()->profile(), GURL(kURL)).empty()); 645 EXPECT_TRUE(GetCookies(browser()->profile(), GURL(kURL)).empty());
615 } 646 }
616 647
617 IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) { 648 IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) {
618 MakeRequestFail("search.example"); 649 MakeRequestFail make_request_fail("search.example");
619 650
620 // Verifies that a default search is made using the provider configured via 651 // Verifies that a default search is made using the provider configured via
621 // policy. Also checks that default search can be completely disabled. 652 // policy. Also checks that default search can be completely disabled.
622 const string16 kKeyword(ASCIIToUTF16("testsearch")); 653 const string16 kKeyword(ASCIIToUTF16("testsearch"));
623 const std::string kSearchURL("http://search.example/search?q={searchTerms}"); 654 const std::string kSearchURL("http://search.example/search?q={searchTerms}");
624 const std::string kAlternateURL0( 655 const std::string kAlternateURL0(
625 "http://search.example/search#q={searchTerms}"); 656 "http://search.example/search#q={searchTerms}");
626 const std::string kAlternateURL1("http://search.example/#q={searchTerms}"); 657 const std::string kAlternateURL1("http://search.example/#q={searchTerms}");
627 658
628 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile( 659 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 EXPECT_FALSE(service->GetDefaultSearchProvider()); 709 EXPECT_FALSE(service->GetDefaultSearchProvider());
679 ui_test_utils::SendToOmniboxAndSubmit(location_bar, "should not work"); 710 ui_test_utils::SendToOmniboxAndSubmit(location_bar, "should not work");
680 // This means that submitting won't trigger any action. 711 // This means that submitting won't trigger any action.
681 EXPECT_FALSE(model->CurrentMatch().destination_url.is_valid()); 712 EXPECT_FALSE(model->CurrentMatch().destination_url.is_valid());
682 EXPECT_EQ(GURL(chrome::kAboutBlankURL), web_contents->GetURL()); 713 EXPECT_EQ(GURL(chrome::kAboutBlankURL), web_contents->GetURL());
683 } 714 }
684 715
685 IN_PROC_BROWSER_TEST_F(PolicyTest, ForceSafeSearch) { 716 IN_PROC_BROWSER_TEST_F(PolicyTest, ForceSafeSearch) {
686 // Makes the requests fail since all we want to check is that the redirection 717 // Makes the requests fail since all we want to check is that the redirection
687 // is done properly. 718 // is done properly.
688 MakeRequestFail("google.com"); 719 MakeRequestFail make_request_fail("google.com");
689 720
690 // Verifies that requests to Google Search engine with the SafeSearch 721 // Verifies that requests to Google Search engine with the SafeSearch
691 // enabled set the safe=active&ssui=on parameters at the end of the query. 722 // enabled set the safe=active&ssui=on parameters at the end of the query.
692 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile( 723 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
693 browser()->profile()); 724 browser()->profile());
694 ui_test_utils::WaitForTemplateURLServiceToLoad(service); 725 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
695 726
696 // First check that nothing happens. 727 // First check that nothing happens.
697 content::TestNavigationObserver no_safesearch_observer( 728 content::TestNavigationObserver no_safesearch_observer(
698 content::NotificationService::AllSources()); 729 content::NotificationService::AllSources());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 EXPECT_TRUE(model->CurrentMatch().destination_url.is_valid()); 762 EXPECT_TRUE(model->CurrentMatch().destination_url.is_valid());
732 web_contents = chrome::GetActiveWebContents(browser()); 763 web_contents = chrome::GetActiveWebContents(browser());
733 std::string expected_url("http://google.com/?"); 764 std::string expected_url("http://google.com/?");
734 expected_url += std::string(chrome::kSafeSearchSafeParameter) + "&" + 765 expected_url += std::string(chrome::kSafeSearchSafeParameter) + "&" +
735 chrome::kSafeSearchSsuiParameter; 766 chrome::kSafeSearchSsuiParameter;
736 GURL expected_with_parameters(expected_url); 767 GURL expected_with_parameters(expected_url);
737 EXPECT_EQ(expected_with_parameters, web_contents->GetURL()); 768 EXPECT_EQ(expected_with_parameters, web_contents->GetURL());
738 } 769 }
739 770
740 IN_PROC_BROWSER_TEST_F(PolicyTest, ReplaceSearchTerms) { 771 IN_PROC_BROWSER_TEST_F(PolicyTest, ReplaceSearchTerms) {
741 MakeRequestFail("search.example"); 772 MakeRequestFail make_request_fail("search.example");
742 773
743 chrome::search::EnableInstantExtendedAPIForTesting(); 774 chrome::search::EnableInstantExtendedAPIForTesting();
744 775
745 // Verifies that a default search is made using the provider configured via 776 // Verifies that a default search is made using the provider configured via
746 // policy. Also checks that default search can be completely disabled. 777 // policy. Also checks that default search can be completely disabled.
747 const string16 kKeyword(ASCIIToUTF16("testsearch")); 778 const string16 kKeyword(ASCIIToUTF16("testsearch"));
748 const std::string kSearchURL("https://www.google.com/search?q={searchTerms}"); 779 const std::string kSearchURL("https://www.google.com/search?q={searchTerms}");
749 const std::string kAlternateURL0( 780 const std::string kAlternateURL0(
750 "https://www.google.com/search#q={searchTerms}"); 781 "https://www.google.com/search#q={searchTerms}");
751 const std::string kAlternateURL1("https://www.google.com/#q={searchTerms}"); 782 const std::string kAlternateURL1("https://www.google.com/#q={searchTerms}");
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
1409 whitelist.Append(base::Value::CreateStringValue("sub.bbb.com")); 1440 whitelist.Append(base::Value::CreateStringValue("sub.bbb.com"));
1410 whitelist.Append(base::Value::CreateStringValue("bbb.com/policy")); 1441 whitelist.Append(base::Value::CreateStringValue("bbb.com/policy"));
1411 policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY, 1442 policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY,
1412 POLICY_SCOPE_USER, whitelist.DeepCopy()); 1443 POLICY_SCOPE_USER, whitelist.DeepCopy());
1413 provider_.UpdateChromePolicy(policies); 1444 provider_.UpdateChromePolicy(policies);
1414 FlushBlacklistPolicy(); 1445 FlushBlacklistPolicy();
1415 CheckCanOpenURL(browser(), kURLS[0]); 1446 CheckCanOpenURL(browser(), kURLS[0]);
1416 CheckURLIsBlocked(browser(), kURLS[1]); 1447 CheckURLIsBlocked(browser(), kURLS[1]);
1417 CheckCanOpenURL(browser(), kURLS[2]); 1448 CheckCanOpenURL(browser(), kURLS[2]);
1418 CheckCanOpenURL(browser(), kURLS[3]); 1449 CheckCanOpenURL(browser(), kURLS[3]);
1450
1451 BrowserThread::PostTaskAndReply(
1452 BrowserThread::IO, FROM_HERE,
1453 base::Bind(UndoRedirectHostsToTestData, kURLS, arraysize(kURLS)),
1454 MessageLoop::QuitClosure());
1455 content::RunMessageLoop();
1419 } 1456 }
1420 1457
1421 // Flaky on Linux. http://crbug.com/155459 1458 // Flaky on Linux. http://crbug.com/155459
1422 #if defined(OS_LINUX) 1459 #if defined(OS_LINUX)
1423 #define MAYBE_DisableScreenshotsFeedback DISABLED_DisableScreenshotsFeedback 1460 #define MAYBE_DisableScreenshotsFeedback DISABLED_DisableScreenshotsFeedback
1424 #else 1461 #else
1425 #define MAYBE_DisableScreenshotsFeedback DisableScreenshotsFeedback 1462 #define MAYBE_DisableScreenshotsFeedback DisableScreenshotsFeedback
1426 #endif 1463 #endif
1427 IN_PROC_BROWSER_TEST_F(PolicyTest, MAYBE_DisableScreenshotsFeedback) { 1464 IN_PROC_BROWSER_TEST_F(PolicyTest, MAYBE_DisableScreenshotsFeedback) {
1428 // Make sure current screenshot can be taken and displayed on feedback page. 1465 // Make sure current screenshot can be taken and displayed on feedback page.
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1696 EXPECT_EQ(kExpectedLabel, text.substr(0, kExpectedLabel.size())); 1733 EXPECT_EQ(kExpectedLabel, text.substr(0, kExpectedLabel.size()));
1697 // HomepageLocation has policy ID 1. 1734 // HomepageLocation has policy ID 1.
1698 EXPECT_NE(std::string::npos, text.find("<br>1 ---")); 1735 EXPECT_NE(std::string::npos, text.find("<br>1 ---"));
1699 // ShowHomeButton has policy ID 35. 1736 // ShowHomeButton has policy ID 35.
1700 EXPECT_NE(std::string::npos, text.find("<br>35 ---")); 1737 EXPECT_NE(std::string::npos, text.find("<br>35 ---"));
1701 // BookmarkBarEnabled has policy ID 82. 1738 // BookmarkBarEnabled has policy ID 82.
1702 EXPECT_NE(std::string::npos, text.find("<br>82 ---")); 1739 EXPECT_NE(std::string::npos, text.find("<br>82 ---"));
1703 } 1740 }
1704 1741
1705 } // namespace policy 1742 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698