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

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

Powered by Google App Engine
This is Rietveld 408576698