[email protected] | 277ec26 | 2011-03-30 21:09:40 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 6 | #include "base/task.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 7 | #include "base/synchronization/waitable_event.h" |
[email protected] | 9d01a6a | 2010-11-30 12:03:33 | [diff] [blame] | 8 | #include "chrome/browser/content_settings/host_content_settings_map.h" |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame^] | 9 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser.h" |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame^] | 12 | #include "chrome/common/pref_names.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 13 | #include "chrome/test/base/in_process_browser_test.h" |
| 14 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 15 | #include "net/base/cookie_store.h" |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 16 | #include "net/base/mock_host_resolver.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 17 | #include "net/test/test_server.h" |
[email protected] | ad94d34 | 2011-06-03 22:19:35 | [diff] [blame] | 18 | #include "net/url_request/url_request_context.h" |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 19 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 20 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | class GetCookiesTask : public Task { |
[email protected] | 34d18e4 | 2010-06-21 16:04:50 | [diff] [blame] | 24 | public: |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 25 | GetCookiesTask(const GURL& url, |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 26 | net::URLRequestContextGetter* context_getter, |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 27 | base::WaitableEvent* event, |
| 28 | std::string* cookies) |
| 29 | : url_(url), |
| 30 | context_getter_(context_getter), |
| 31 | event_(event), |
| 32 | cookies_(cookies) {} |
| 33 | |
| 34 | virtual void Run() { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 35 | net::CookieOptions options; |
| 36 | context_getter_->GetURLRequestContext()->cookie_store() |
| 37 | ->GetCookiesWithOptionsAsync( |
| 38 | url_, options, base::Bind(&GetCookiesTask::GetCookiesCallback, |
| 39 | base::Unretained(cookies_), |
| 40 | base::Unretained(event_))); |
| 41 | } |
| 42 | |
| 43 | static void GetCookiesCallback(std::string* cookies_out, |
| 44 | base::WaitableEvent* event, |
| 45 | const std::string& cookies) { |
| 46 | *cookies_out = cookies; |
| 47 | event->Signal(); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | private: |
| 51 | const GURL& url_; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 52 | net::URLRequestContextGetter* const context_getter_; |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 53 | base::WaitableEvent* const event_; |
| 54 | std::string* const cookies_; |
| 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(GetCookiesTask); |
| 57 | }; |
| 58 | |
| 59 | class CookiePolicyBrowserTest : public InProcessBrowserTest { |
| 60 | protected: |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 61 | CookiePolicyBrowserTest() {} |
| 62 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 63 | std::string GetCookies(const GURL& url) { |
| 64 | std::string cookies; |
| 65 | base::WaitableEvent event(true /* manual reset */, |
| 66 | false /* not initially signaled */); |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 67 | net::URLRequestContextGetter* context_getter = |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 68 | browser()->profile()->GetRequestContext(); |
| 69 | EXPECT_TRUE( |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 70 | BrowserThread::PostTask( |
| 71 | BrowserThread::IO, FROM_HERE, |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 72 | new GetCookiesTask(url, context_getter, &event, &cookies))); |
[email protected] | 866cf33 | 2011-10-12 03:09:42 | [diff] [blame] | 73 | event.Wait(); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 74 | return cookies; |
| 75 | } |
| 76 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 77 | private: |
| 78 | DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); |
| 79 | }; |
| 80 | |
| 81 | // Visits a page that sets a first-party cookie. |
| 82 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 83 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 84 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame^] | 85 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 86 | true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 87 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 88 | GURL url(test_server()->GetURL("set-cookie?cookie1")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 89 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 90 | std::string cookie = GetCookies(url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 91 | ASSERT_EQ("", cookie); |
| 92 | |
| 93 | ui_test_utils::NavigateToURL(browser(), url); |
| 94 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 95 | cookie = GetCookies(url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 96 | EXPECT_EQ("cookie1", cookie); |
| 97 | } |
| 98 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 99 | // Visits a page that is a redirect across domain boundary to a page that sets |
| 100 | // a first-party cookie. |
| 101 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 102 | AllowFirstPartyCookiesRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 103 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 104 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame^] | 105 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 106 | true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 107 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 108 | GURL url(test_server()->GetURL("server-redirect?")); |
| 109 | GURL redirected_url(test_server()->GetURL("set-cookie?cookie2")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 110 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 111 | // Change the host name from 127.0.0.1 to www.example.com so it triggers |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 112 | // third-party cookie blocking if the first party for cookies URL is not |
| 113 | // changed when we follow a redirect. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 114 | ASSERT_EQ("127.0.0.1", redirected_url.host()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 115 | GURL::Replacements replacements; |
| 116 | std::string new_host("www.example.com"); |
| 117 | replacements.SetHostStr(new_host); |
| 118 | redirected_url = redirected_url.ReplaceComponents(replacements); |
| 119 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 120 | std::string cookie = GetCookies(redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 121 | ASSERT_EQ("", cookie); |
| 122 | |
| 123 | host_resolver()->AddRule("www.example.com", "127.0.0.1"); |
| 124 | |
| 125 | ui_test_utils::NavigateToURL(browser(), |
| 126 | GURL(url.spec() + redirected_url.spec())); |
| 127 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 128 | cookie = GetCookies(redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 129 | EXPECT_EQ("cookie2", cookie); |
| 130 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 131 | |
| 132 | } // namespace |