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

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

Issue 11186002: Add a SafeSearch preference, policy and implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased with the latest tree version Created 8 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 | 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 #include "content/public/browser/notification_types.h" 72 #include "content/public/browser/notification_types.h"
73 #include "content/public/browser/plugin_service.h" 73 #include "content/public/browser/plugin_service.h"
74 #include "content/public/browser/render_view_host.h" 74 #include "content/public/browser/render_view_host.h"
75 #include "content/public/browser/web_contents.h" 75 #include "content/public/browser/web_contents.h"
76 #include "content/public/common/content_paths.h" 76 #include "content/public/common/content_paths.h"
77 #include "content/public/common/page_transition_types.h" 77 #include "content/public/common/page_transition_types.h"
78 #include "content/public/common/process_type.h" 78 #include "content/public/common/process_type.h"
79 #include "content/public/common/url_constants.h" 79 #include "content/public/common/url_constants.h"
80 #include "content/public/test/browser_test_utils.h" 80 #include "content/public/test/browser_test_utils.h"
81 #include "content/public/test/download_test_observer.h" 81 #include "content/public/test/download_test_observer.h"
82 #include "content/public/test/test_navigation_observer.h"
82 #include "content/public/test/test_utils.h" 83 #include "content/public/test/test_utils.h"
84 #include "content/test/net/url_request_failed_job.h"
83 #include "content/test/net/url_request_mock_http_job.h" 85 #include "content/test/net/url_request_mock_http_job.h"
84 #include "googleurl/src/gurl.h" 86 #include "googleurl/src/gurl.h"
85 #include "grit/generated_resources.h" 87 #include "grit/generated_resources.h"
88 #include "net/base/net_errors.h"
86 #include "net/base/net_util.h" 89 #include "net/base/net_util.h"
87 #include "net/http/http_stream_factory.h" 90 #include "net/http/http_stream_factory.h"
88 #include "net/url_request/url_request.h" 91 #include "net/url_request/url_request.h"
89 #include "net/url_request/url_request_filter.h" 92 #include "net/url_request/url_request_filter.h"
90 #include "policy/policy_constants.h" 93 #include "policy/policy_constants.h"
91 #include "testing/gmock/include/gmock/gmock.h" 94 #include "testing/gmock/include/gmock/gmock.h"
92 #include "testing/gtest/include/gtest/gtest.h" 95 #include "testing/gtest/include/gtest/gtest.h"
93 #include "ui/base/l10n/l10n_util.h" 96 #include "ui/base/l10n/l10n_util.h"
94 #include "ui/base/resource/resource_bundle.h" 97 #include "ui/base/resource/resource_bundle.h"
95 #include "webkit/plugins/npapi/plugin_utils.h" 98 #include "webkit/plugins/npapi/plugin_utils.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 // Map the given hosts to the test data dir. 144 // Map the given hosts to the test data dir.
142 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 145 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
143 for (size_t i = 0; i < size; ++i) { 146 for (size_t i = 0; i < size; ++i) {
144 const GURL* url = urls[i]; 147 const GURL* url = urls[i];
145 EXPECT_TRUE(url->is_valid()); 148 EXPECT_TRUE(url->is_valid());
146 filter->AddHostnameHandler(url->scheme(), url->host(), 149 filter->AddHostnameHandler(url->scheme(), url->host(),
147 URLRequestMockHTTPJob::Factory); 150 URLRequestMockHTTPJob::Factory);
148 } 151 }
149 } 152 }
150 153
154 // Fails requests using ERR_CONNECTION_RESET.
155 net::URLRequestJob* FailedJobFactory(
156 net::URLRequest* request,
157 net::NetworkDelegate* network_delegate,
158 const std::string& scheme) {
159 return new content::URLRequestFailedJob(
160 request, network_delegate, net::ERR_CONNECTION_RESET);
161 }
162
163 // Filters requests to the |host| such that they fail. Run on IO thread.
164 void MakeRequestFailOnIO(const std::string& host) {
165 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
166 filter->AddHostnameHandler("http", host, &FailedJobFactory);
167 filter->AddHostnameHandler("https", host, &FailedJobFactory);
168 }
169
170 // Sets up the filter on IO thread such that requests to |host| fail.
171 void MakeRequestFail(const std::string& host) {
172 BrowserThread::PostTaskAndReply(
173 BrowserThread::IO, FROM_HERE,
174 base::Bind(MakeRequestFailOnIO, host),
175 MessageLoop::QuitClosure());
176 content::RunMessageLoop();
177 }
178
151 // Verifies that the given |url| can be opened. This assumes that |url| points 179 // Verifies that the given |url| can be opened. This assumes that |url| points
152 // at empty.html in the test data dir. 180 // at empty.html in the test data dir.
153 void CheckCanOpenURL(Browser* browser, const GURL& url) { 181 void CheckCanOpenURL(Browser* browser, const GURL& url) {
154 ui_test_utils::NavigateToURL(browser, url); 182 ui_test_utils::NavigateToURL(browser, url);
155 content::WebContents* contents = chrome::GetActiveWebContents(browser); 183 content::WebContents* contents = chrome::GetActiveWebContents(browser);
156 EXPECT_EQ(url, contents->GetURL()); 184 EXPECT_EQ(url, contents->GetURL());
157 EXPECT_EQ(net::FormatUrl(url, std::string()), contents->GetTitle()); 185 EXPECT_EQ(net::FormatUrl(url, std::string()), contents->GetTitle());
158 } 186 }
159 187
160 // Verifies that access to the given |url| is blocked. 188 // Verifies that access to the given |url| is blocked.
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true)); 604 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true));
577 provider_.UpdateChromePolicy(policies); 605 provider_.UpdateChromePolicy(policies);
578 } 606 }
579 607
580 IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) { 608 IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) {
581 // Verify that the cookie is gone. 609 // Verify that the cookie is gone.
582 EXPECT_TRUE(GetCookies(browser()->profile(), GURL(kURL)).empty()); 610 EXPECT_TRUE(GetCookies(browser()->profile(), GURL(kURL)).empty());
583 } 611 }
584 612
585 IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) { 613 IN_PROC_BROWSER_TEST_F(PolicyTest, DefaultSearchProvider) {
614 MakeRequestFail("search.example");
615
586 // Verifies that a default search is made using the provider configured via 616 // Verifies that a default search is made using the provider configured via
587 // policy. Also checks that default search can be completely disabled. 617 // policy. Also checks that default search can be completely disabled.
588 const string16 kKeyword(ASCIIToUTF16("testsearch")); 618 const string16 kKeyword(ASCIIToUTF16("testsearch"));
589 const std::string kSearchURL("http://search.example/search?q={searchTerms}"); 619 const std::string kSearchURL("http://search.example/search?q={searchTerms}");
590 const std::string kAlternateURL0( 620 const std::string kAlternateURL0(
591 "http://search.example/search#q={searchTerms}"); 621 "http://search.example/search#q={searchTerms}");
592 const std::string kAlternateURL1("http://search.example/#q={searchTerms}"); 622 const std::string kAlternateURL1("http://search.example/#q={searchTerms}");
593 623
594 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile( 624 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
595 browser()->profile()); 625 browser()->profile());
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false)); 671 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false));
642 EXPECT_TRUE(service->GetDefaultSearchProvider()); 672 EXPECT_TRUE(service->GetDefaultSearchProvider());
643 provider_.UpdateChromePolicy(policies); 673 provider_.UpdateChromePolicy(policies);
644 EXPECT_FALSE(service->GetDefaultSearchProvider()); 674 EXPECT_FALSE(service->GetDefaultSearchProvider());
645 ui_test_utils::SendToOmniboxAndSubmit(location_bar, "should not work"); 675 ui_test_utils::SendToOmniboxAndSubmit(location_bar, "should not work");
646 // This means that submitting won't trigger any action. 676 // This means that submitting won't trigger any action.
647 EXPECT_FALSE(model->CurrentMatch().destination_url.is_valid()); 677 EXPECT_FALSE(model->CurrentMatch().destination_url.is_valid());
648 EXPECT_EQ(GURL(chrome::kAboutBlankURL), web_contents->GetURL()); 678 EXPECT_EQ(GURL(chrome::kAboutBlankURL), web_contents->GetURL());
649 } 679 }
650 680
681 IN_PROC_BROWSER_TEST_F(PolicyTest, ForceSafeSearch) {
682 // Makes the requests fail since all we want to check is that the redirection
683 // is done properly.
684 MakeRequestFail("google.com");
685
686 // Verifies that requests to Google Search engine with the SafeSearch
687 // enabled set the safe=active&ssui=on parameters at the end of the query.
688 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
689 browser()->profile());
690 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
691
692 // First check that nothing happens.
693 content::TestNavigationObserver no_safesearch_observer(
694 content::NotificationService::AllSources());
695 chrome::FocusLocationBar(browser());
696 LocationBar* location_bar = browser()->window()->GetLocationBar();
697 ui_test_utils::SendToOmniboxAndSubmit(location_bar, "http://google.com/");
698 OmniboxEditModel* model = location_bar->GetLocationEntry()->model();
699 no_safesearch_observer.Wait();
700 EXPECT_TRUE(model->CurrentMatch().destination_url.is_valid());
701 content::WebContents* web_contents = chrome::GetActiveWebContents(browser());
702 GURL expected_without("http://google.com/");
703 EXPECT_EQ(expected_without, web_contents->GetURL());
704
705 PrefService* prefs = browser()->profile()->GetPrefs();
706 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kForceSafeSearch));
707 EXPECT_FALSE(prefs->GetBoolean(prefs::kForceSafeSearch));
708
709 // Override the default SafeSearch setting using policies.
710 PolicyMap policies;
711 policies.Set(key::kForceSafeSearch, POLICY_LEVEL_MANDATORY,
712 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true));
713 provider_.UpdateChromePolicy(policies);
714
715 EXPECT_TRUE(prefs->IsManagedPreference(prefs::kForceSafeSearch));
716 EXPECT_TRUE(prefs->GetBoolean(prefs::kForceSafeSearch));
717
718 content::TestNavigationObserver safesearch_observer(
719 content::NotificationService::AllSources());
720
721 // Verify that searching from google.com works.
722 chrome::FocusLocationBar(browser());
723 location_bar = browser()->window()->GetLocationBar();
724 ui_test_utils::SendToOmniboxAndSubmit(location_bar, "http://google.com/");
725 safesearch_observer.Wait();
726 model = location_bar->GetLocationEntry()->model();
727 EXPECT_TRUE(model->CurrentMatch().destination_url.is_valid());
728 web_contents = chrome::GetActiveWebContents(browser());
729 std::string expected_url("http://google.com/?");
730 expected_url += std::string(chrome::kSafeSearchSafeParameter) + "&" +
731 chrome::kSafeSearchSsuiParameter;
732 GURL expected_with_parameters(expected_url);
733 EXPECT_EQ(expected_with_parameters, web_contents->GetURL());
734 }
735
651 IN_PROC_BROWSER_TEST_F(PolicyTest, ReplaceSearchTerms) { 736 IN_PROC_BROWSER_TEST_F(PolicyTest, ReplaceSearchTerms) {
737 MakeRequestFail("search.example");
738
652 CommandLine::ForCurrentProcess()->AppendSwitch( 739 CommandLine::ForCurrentProcess()->AppendSwitch(
653 switches::kEnableInstantExtendedAPI); 740 switches::kEnableInstantExtendedAPI);
654 741
655 // Adding the kEnableInstantExtendedAPI is not enough since 742 // Adding the kEnableInstantExtendedAPI is not enough since
656 // IsInstantExtendedAPIEnabled does not return true on CHANNEL_DEV. 743 // IsInstantExtendedAPIEnabled does not return true on CHANNEL_DEV.
657 if (!chrome::search::IsInstantExtendedAPIEnabled(browser()->profile())) 744 if (!chrome::search::IsInstantExtendedAPIEnabled(browser()->profile()))
658 return; 745 return;
659 746
660 // Verifies that a default search is made using the provider configured via 747 // Verifies that a default search is made using the provider configured via
661 // policy. Also checks that default search can be completely disabled. 748 // policy. Also checks that default search can be completely disabled.
(...skipping 912 matching lines...) Expand 10 before | Expand all | Expand 10 after
1574 EXPECT_EQ(kExpectedLabel, text.substr(0, kExpectedLabel.size())); 1661 EXPECT_EQ(kExpectedLabel, text.substr(0, kExpectedLabel.size()));
1575 // HomepageLocation has policy ID 1. 1662 // HomepageLocation has policy ID 1.
1576 EXPECT_NE(std::string::npos, text.find("<br>1 ---")); 1663 EXPECT_NE(std::string::npos, text.find("<br>1 ---"));
1577 // ShowHomeButton has policy ID 35. 1664 // ShowHomeButton has policy ID 35.
1578 EXPECT_NE(std::string::npos, text.find("<br>35 ---")); 1665 EXPECT_NE(std::string::npos, text.find("<br>35 ---"));
1579 // BookmarkBarEnabled has policy ID 82. 1666 // BookmarkBarEnabled has policy ID 82.
1580 EXPECT_NE(std::string::npos, text.find("<br>82 ---")); 1667 EXPECT_NE(std::string::npos, text.find("<br>82 ---"));
1581 } 1668 }
1582 1669
1583 } // namespace policy 1670 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698