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

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: Address erikwright's third round of comments 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 const FilePath::CharType kGoodCrxName[] = FILE_PATH_LITERAL("good.crx"); 124 const FilePath::CharType kGoodCrxName[] = FILE_PATH_LITERAL("good.crx");
125 const FilePath::CharType kAdBlockCrxName[] = FILE_PATH_LITERAL("adblock.crx"); 125 const FilePath::CharType kAdBlockCrxName[] = FILE_PATH_LITERAL("adblock.crx");
126 126
127 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; 127 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
128 const char kAdBlockCrxId[] = "dojnnbeimaimaojcialkkgajdnefpgcn"; 128 const char kAdBlockCrxId[] = "dojnnbeimaimaojcialkkgajdnefpgcn";
129 129
130 const FilePath::CharType kGoodCrxManifestName[] = 130 const FilePath::CharType kGoodCrxManifestName[] =
131 FILE_PATH_LITERAL("good_update_manifest.xml"); 131 FILE_PATH_LITERAL("good_update_manifest.xml");
132 132
133 const char* kURLs[] = { 133 const char* kURLs[] = {
134 "http://aaa.com/empty.html", 134 "http://aaaa.com/empty.html",
135 "http://bbb.com/empty.html", 135 "http://bbbb.com/empty.html",
136 "http://ccc.com/empty.html", 136 "http://ccc.com/empty.html",
137 "http://ddd.com/empty.html", 137 "http://ddd.com/empty.html",
138 "http://eee.com/empty.html", 138 "http://eee.com/empty.html",
139 }; 139 };
140 140
141 // Filters requests to the hosts in |urls| and redirects them to the test data 141 // Filters requests to the hosts in |urls| and redirects them to the test data
142 // dir through URLRequestMockHTTPJobs. 142 // dir through URLRequestMockHTTPJobs.
143 void RedirectHostsToTestData(const char* const urls[], size_t size) { 143 void RedirectHostsToTestData(const char* const urls[], size_t size) {
144 // Map the given hosts to the test data dir. 144 // Map the given hosts to the test data dir.
145 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 145 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
146 for (size_t i = 0; i < size; ++i) { 146 for (size_t i = 0; i < size; ++i) {
147 const GURL url(urls[i]); 147 const GURL url(urls[i]);
148 EXPECT_TRUE(url.is_valid()); 148 EXPECT_TRUE(url.is_valid());
149 filter->AddHostnameHandler(url.scheme(), url.host(), 149 filter->AddUrlHandler(url, URLRequestMockHTTPJob::Factory);
150 URLRequestMockHTTPJob::Factory);
151 } 150 }
152 } 151 }
153 152
153 // Remove filters for requests to the hosts in |urls|.
154 void UndoRedirectHostsToTestData(const char* const urls[], size_t size) {
155 // Map the given hosts to the test data dir.
156 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
157 for (size_t i = 0; i < size; ++i) {
158 const GURL url(urls[i]);
159 EXPECT_TRUE(url.is_valid());
160 filter->RemoveUrlHandler(url);
161 }
162 }
163
154 // Fails requests using ERR_CONNECTION_RESET. 164 // Fails requests using ERR_CONNECTION_RESET.
155 net::URLRequestJob* FailedJobFactory( 165 net::URLRequestJob* FailedJobFactory(
156 net::URLRequest* request, 166 net::URLRequest* request,
157 net::NetworkDelegate* network_delegate, 167 net::NetworkDelegate* network_delegate,
158 const std::string& scheme) { 168 const std::string& scheme) {
159 return new content::URLRequestFailedJob( 169 return new content::URLRequestFailedJob(
160 request, network_delegate, net::ERR_CONNECTION_RESET); 170 request, network_delegate, net::ERR_CONNECTION_RESET);
161 } 171 }
162 172
163 // Filters requests to the |host| such that they fail. Run on IO thread. 173 // While |MakeRequestFail| is in scope URLRequests to |host| will fail.
164 void MakeRequestFailOnIO(const std::string& host) { 174 class MakeRequestFail {
165 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 175 public:
166 filter->AddHostnameHandler("http", host, &FailedJobFactory); 176 // Sets up the filter on IO thread such that requests to |host| fail.
167 filter->AddHostnameHandler("https", host, &FailedJobFactory); 177 explicit MakeRequestFail(const std::string& host) : host_(host) {
168 } 178 BrowserThread::PostTaskAndReply(
179 BrowserThread::IO, FROM_HERE,
180 base::Bind(MakeRequestFailOnIO, host_),
181 MessageLoop::QuitClosure());
182 content::RunMessageLoop();
183 }
184 ~MakeRequestFail() {
185 BrowserThread::PostTaskAndReply(
186 BrowserThread::IO, FROM_HERE,
187 base::Bind(UndoMakeRequestFailOnIO, host_),
188 MessageLoop::QuitClosure());
189 content::RunMessageLoop();
190 }
169 191
170 // Sets up the filter on IO thread such that requests to |host| fail. 192 private:
171 void MakeRequestFail(const std::string& host) { 193 // Filters requests to the |host| such that they fail. Run on IO thread.
172 BrowserThread::PostTaskAndReply( 194 static void MakeRequestFailOnIO(const std::string& host) {
173 BrowserThread::IO, FROM_HERE, 195 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
174 base::Bind(MakeRequestFailOnIO, host), 196 filter->AddHostnameHandler("http", host, &FailedJobFactory);
175 MessageLoop::QuitClosure()); 197 filter->AddHostnameHandler("https", host, &FailedJobFactory);
176 content::RunMessageLoop(); 198 }
177 } 199
200 // Remove filters for requests to the |host|. Run on IO thread.
201 static void UndoMakeRequestFailOnIO(const std::string& host) {
202 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
203 filter->RemoveHostnameHandler("http", host);
204 filter->RemoveHostnameHandler("https", host);
205 }
206
207 const std::string host_;
208 };
178 209
179 // Verifies that the given url |spec| can be opened. This assumes that |spec| 210 // Verifies that the given url |spec| can be opened. This assumes that |spec|
180 // points at empty.html in the test data dir. 211 // points at empty.html in the test data dir.
181 void CheckCanOpenURL(Browser* browser, const char* spec) { 212 void CheckCanOpenURL(Browser* browser, const char* spec) {
182 GURL url(spec); 213 GURL url(spec);
183 ui_test_utils::NavigateToURL(browser, url); 214 ui_test_utils::NavigateToURL(browser, url);
184 content::WebContents* contents = chrome::GetActiveWebContents(browser); 215 content::WebContents* contents = chrome::GetActiveWebContents(browser);
185 EXPECT_EQ(url, contents->GetURL()); 216 EXPECT_EQ(url, contents->GetURL());
186 EXPECT_EQ(net::FormatUrl(url, std::string()), contents->GetTitle()); 217 EXPECT_EQ(net::FormatUrl(url, std::string()), contents->GetTitle());
187 } 218 }
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true)); 637 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true));
607 provider_.UpdateChromePolicy(policies); 638 provider_.UpdateChromePolicy(policies);
608 } 639 }
609 640
610 IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) { 641 IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) {
611 // Verify that the cookie is gone. 642 // Verify that the cookie is gone.
612 EXPECT_TRUE(GetCookies(browser()->profile(), GURL(kURL)).empty()); 643 EXPECT_TRUE(GetCookies(browser()->profile(), GURL(kURL)).empty());
613 } 644 }
614 645
615 IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) { 646 IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) {
616 MakeRequestFail("search.example"); 647 MakeRequestFail make_request_fail("search.example");
617 648
618 // Verifies that a default search is made using the provider configured via 649 // Verifies that a default search is made using the provider configured via
619 // policy. Also checks that default search can be completely disabled. 650 // policy. Also checks that default search can be completely disabled.
620 const string16 kKeyword(ASCIIToUTF16("testsearch")); 651 const string16 kKeyword(ASCIIToUTF16("testsearch"));
621 const std::string kSearchURL("http://search.example/search?q={searchTerms}"); 652 const std::string kSearchURL("http://search.example/search?q={searchTerms}");
622 const std::string kAlternateURL0( 653 const std::string kAlternateURL0(
623 "http://search.example/search#q={searchTerms}"); 654 "http://search.example/search#q={searchTerms}");
624 const std::string kAlternateURL1("http://search.example/#q={searchTerms}"); 655 const std::string kAlternateURL1("http://search.example/#q={searchTerms}");
625 656
626 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile( 657 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 EXPECT_FALSE(service->GetDefaultSearchProvider()); 707 EXPECT_FALSE(service->GetDefaultSearchProvider());
677 ui_test_utils::SendToOmniboxAndSubmit(location_bar, "should not work"); 708 ui_test_utils::SendToOmniboxAndSubmit(location_bar, "should not work");
678 // This means that submitting won't trigger any action. 709 // This means that submitting won't trigger any action.
679 EXPECT_FALSE(model->CurrentMatch().destination_url.is_valid()); 710 EXPECT_FALSE(model->CurrentMatch().destination_url.is_valid());
680 EXPECT_EQ(GURL(chrome::kAboutBlankURL), web_contents->GetURL()); 711 EXPECT_EQ(GURL(chrome::kAboutBlankURL), web_contents->GetURL());
681 } 712 }
682 713
683 IN_PROC_BROWSER_TEST_F(PolicyTest, ForceSafeSearch) { 714 IN_PROC_BROWSER_TEST_F(PolicyTest, ForceSafeSearch) {
684 // Makes the requests fail since all we want to check is that the redirection 715 // Makes the requests fail since all we want to check is that the redirection
685 // is done properly. 716 // is done properly.
686 MakeRequestFail("google.com"); 717 MakeRequestFail make_request_fail("google.com");
687 718
688 // Verifies that requests to Google Search engine with the SafeSearch 719 // Verifies that requests to Google Search engine with the SafeSearch
689 // enabled set the safe=active&ssui=on parameters at the end of the query. 720 // enabled set the safe=active&ssui=on parameters at the end of the query.
690 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile( 721 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
691 browser()->profile()); 722 browser()->profile());
692 ui_test_utils::WaitForTemplateURLServiceToLoad(service); 723 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
693 724
694 // First check that nothing happens. 725 // First check that nothing happens.
695 content::TestNavigationObserver no_safesearch_observer( 726 content::TestNavigationObserver no_safesearch_observer(
696 content::NotificationService::AllSources()); 727 content::NotificationService::AllSources());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 EXPECT_TRUE(model->CurrentMatch().destination_url.is_valid()); 760 EXPECT_TRUE(model->CurrentMatch().destination_url.is_valid());
730 web_contents = chrome::GetActiveWebContents(browser()); 761 web_contents = chrome::GetActiveWebContents(browser());
731 std::string expected_url("http://google.com/?"); 762 std::string expected_url("http://google.com/?");
732 expected_url += std::string(chrome::kSafeSearchSafeParameter) + "&" + 763 expected_url += std::string(chrome::kSafeSearchSafeParameter) + "&" +
733 chrome::kSafeSearchSsuiParameter; 764 chrome::kSafeSearchSsuiParameter;
734 GURL expected_with_parameters(expected_url); 765 GURL expected_with_parameters(expected_url);
735 EXPECT_EQ(expected_with_parameters, web_contents->GetURL()); 766 EXPECT_EQ(expected_with_parameters, web_contents->GetURL());
736 } 767 }
737 768
738 IN_PROC_BROWSER_TEST_F(PolicyTest, ReplaceSearchTerms) { 769 IN_PROC_BROWSER_TEST_F(PolicyTest, ReplaceSearchTerms) {
739 MakeRequestFail("search.example"); 770 MakeRequestFail make_request_fail("search.example");
740 771
741 chrome::search::EnableInstantExtendedAPIForTesting(); 772 chrome::search::EnableInstantExtendedAPIForTesting();
742 773
743 // Verifies that a default search is made using the provider configured via 774 // Verifies that a default search is made using the provider configured via
744 // policy. Also checks that default search can be completely disabled. 775 // policy. Also checks that default search can be completely disabled.
745 const string16 kKeyword(ASCIIToUTF16("testsearch")); 776 const string16 kKeyword(ASCIIToUTF16("testsearch"));
746 const std::string kSearchURL("https://www.google.com/search?q={searchTerms}"); 777 const std::string kSearchURL("https://www.google.com/search?q={searchTerms}");
747 const std::string kAlternateURL0( 778 const std::string kAlternateURL0(
748 "https://www.google.com/search#q={searchTerms}"); 779 "https://www.google.com/search#q={searchTerms}");
749 const std::string kAlternateURL1("https://www.google.com/#q={searchTerms}"); 780 const std::string kAlternateURL1("https://www.google.com/#q={searchTerms}");
(...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after
1379 whitelist.Append(base::Value::CreateStringValue("sub.bbb.com")); 1410 whitelist.Append(base::Value::CreateStringValue("sub.bbb.com"));
1380 whitelist.Append(base::Value::CreateStringValue("bbb.com/policy")); 1411 whitelist.Append(base::Value::CreateStringValue("bbb.com/policy"));
1381 policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY, 1412 policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY,
1382 POLICY_SCOPE_USER, whitelist.DeepCopy()); 1413 POLICY_SCOPE_USER, whitelist.DeepCopy());
1383 provider_.UpdateChromePolicy(policies); 1414 provider_.UpdateChromePolicy(policies);
1384 FlushBlacklistPolicy(); 1415 FlushBlacklistPolicy();
1385 CheckCanOpenURL(browser(), kURLS[0]); 1416 CheckCanOpenURL(browser(), kURLS[0]);
1386 CheckURLIsBlocked(browser(), kURLS[1]); 1417 CheckURLIsBlocked(browser(), kURLS[1]);
1387 CheckCanOpenURL(browser(), kURLS[2]); 1418 CheckCanOpenURL(browser(), kURLS[2]);
1388 CheckCanOpenURL(browser(), kURLS[3]); 1419 CheckCanOpenURL(browser(), kURLS[3]);
1420
1421 BrowserThread::PostTaskAndReply(
1422 BrowserThread::IO, FROM_HERE,
1423 base::Bind(UndoRedirectHostsToTestData, kURLS, arraysize(kURLS)),
1424 MessageLoop::QuitClosure());
1425 content::RunMessageLoop();
1389 } 1426 }
1390 1427
1391 // Flaky on Linux. http://crbug.com/155459 1428 // Flaky on Linux. http://crbug.com/155459
1392 #if defined(OS_LINUX) 1429 #if defined(OS_LINUX)
1393 #define MAYBE_DisableScreenshotsFeedback DISABLED_DisableScreenshotsFeedback 1430 #define MAYBE_DisableScreenshotsFeedback DISABLED_DisableScreenshotsFeedback
1394 #else 1431 #else
1395 #define MAYBE_DisableScreenshotsFeedback DisableScreenshotsFeedback 1432 #define MAYBE_DisableScreenshotsFeedback DisableScreenshotsFeedback
1396 #endif 1433 #endif
1397 IN_PROC_BROWSER_TEST_F(PolicyTest, MAYBE_DisableScreenshotsFeedback) { 1434 IN_PROC_BROWSER_TEST_F(PolicyTest, MAYBE_DisableScreenshotsFeedback) {
1398 // Make sure current screenshot can be taken and displayed on feedback page. 1435 // Make sure current screenshot can be taken and displayed on feedback page.
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
1667 EXPECT_EQ(kExpectedLabel, text.substr(0, kExpectedLabel.size())); 1704 EXPECT_EQ(kExpectedLabel, text.substr(0, kExpectedLabel.size()));
1668 // HomepageLocation has policy ID 1. 1705 // HomepageLocation has policy ID 1.
1669 EXPECT_NE(std::string::npos, text.find("<br>1 ---")); 1706 EXPECT_NE(std::string::npos, text.find("<br>1 ---"));
1670 // ShowHomeButton has policy ID 35. 1707 // ShowHomeButton has policy ID 35.
1671 EXPECT_NE(std::string::npos, text.find("<br>35 ---")); 1708 EXPECT_NE(std::string::npos, text.find("<br>35 ---"));
1672 // BookmarkBarEnabled has policy ID 82. 1709 // BookmarkBarEnabled has policy ID 82.
1673 EXPECT_NE(std::string::npos, text.find("<br>82 ---")); 1710 EXPECT_NE(std::string::npos, text.find("<br>82 ---"));
1674 } 1711 }
1675 1712
1676 } // namespace policy 1713 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698