[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] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame^] | 21 | using content::BrowserThread; |
| 22 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | class GetCookiesTask : public Task { |
[email protected] | 34d18e4 | 2010-06-21 16:04:50 | [diff] [blame] | 26 | public: |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 27 | GetCookiesTask(const GURL& url, |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 28 | net::URLRequestContextGetter* context_getter, |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 29 | base::WaitableEvent* event, |
| 30 | std::string* cookies) |
| 31 | : url_(url), |
| 32 | context_getter_(context_getter), |
| 33 | event_(event), |
| 34 | cookies_(cookies) {} |
| 35 | |
| 36 | virtual void Run() { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 37 | net::CookieOptions options; |
| 38 | context_getter_->GetURLRequestContext()->cookie_store() |
| 39 | ->GetCookiesWithOptionsAsync( |
| 40 | url_, options, base::Bind(&GetCookiesTask::GetCookiesCallback, |
| 41 | base::Unretained(cookies_), |
| 42 | base::Unretained(event_))); |
| 43 | } |
| 44 | |
| 45 | static void GetCookiesCallback(std::string* cookies_out, |
| 46 | base::WaitableEvent* event, |
| 47 | const std::string& cookies) { |
| 48 | *cookies_out = cookies; |
| 49 | event->Signal(); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private: |
| 53 | const GURL& url_; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 54 | net::URLRequestContextGetter* const context_getter_; |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 55 | base::WaitableEvent* const event_; |
| 56 | std::string* const cookies_; |
| 57 | |
| 58 | DISALLOW_COPY_AND_ASSIGN(GetCookiesTask); |
| 59 | }; |
| 60 | |
| 61 | class CookiePolicyBrowserTest : public InProcessBrowserTest { |
| 62 | protected: |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 63 | CookiePolicyBrowserTest() {} |
| 64 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 65 | std::string GetCookies(const GURL& url) { |
| 66 | std::string cookies; |
| 67 | base::WaitableEvent event(true /* manual reset */, |
| 68 | false /* not initially signaled */); |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 69 | net::URLRequestContextGetter* context_getter = |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 70 | browser()->profile()->GetRequestContext(); |
| 71 | EXPECT_TRUE( |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 72 | BrowserThread::PostTask( |
| 73 | BrowserThread::IO, FROM_HERE, |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 74 | new GetCookiesTask(url, context_getter, &event, &cookies))); |
[email protected] | 866cf33 | 2011-10-12 03:09:42 | [diff] [blame] | 75 | event.Wait(); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 76 | return cookies; |
| 77 | } |
| 78 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 79 | private: |
| 80 | DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); |
| 81 | }; |
| 82 | |
| 83 | // Visits a page that sets a first-party cookie. |
| 84 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 85 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 86 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 87 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 88 | true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 89 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 90 | GURL url(test_server()->GetURL("set-cookie?cookie1")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 91 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 92 | std::string cookie = GetCookies(url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 93 | ASSERT_EQ("", cookie); |
| 94 | |
| 95 | ui_test_utils::NavigateToURL(browser(), url); |
| 96 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 97 | cookie = GetCookies(url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 98 | EXPECT_EQ("cookie1", cookie); |
| 99 | } |
| 100 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 101 | // Visits a page that is a redirect across domain boundary to a page that sets |
| 102 | // a first-party cookie. |
| 103 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 104 | AllowFirstPartyCookiesRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 105 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 106 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 107 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 108 | true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 109 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 110 | GURL url(test_server()->GetURL("server-redirect?")); |
| 111 | GURL redirected_url(test_server()->GetURL("set-cookie?cookie2")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 112 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 113 | // 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] | 114 | // third-party cookie blocking if the first party for cookies URL is not |
| 115 | // changed when we follow a redirect. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 116 | ASSERT_EQ("127.0.0.1", redirected_url.host()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 117 | GURL::Replacements replacements; |
| 118 | std::string new_host("www.example.com"); |
| 119 | replacements.SetHostStr(new_host); |
| 120 | redirected_url = redirected_url.ReplaceComponents(replacements); |
| 121 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 122 | std::string cookie = GetCookies(redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 123 | ASSERT_EQ("", cookie); |
| 124 | |
| 125 | host_resolver()->AddRule("www.example.com", "127.0.0.1"); |
| 126 | |
| 127 | ui_test_utils::NavigateToURL(browser(), |
| 128 | GURL(url.spec() + redirected_url.spec())); |
| 129 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 130 | cookie = GetCookies(redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 131 | EXPECT_EQ("cookie2", cookie); |
| 132 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 133 | |
| 134 | } // namespace |