[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] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 5 | #include "base/task.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 6 | #include "base/synchronization/waitable_event.h" |
[email protected] | 9d01a6a | 2010-11-30 12:03:33 | [diff] [blame] | 7 | #include "chrome/browser/content_settings/host_content_settings_map.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 8 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 9 | #include "chrome/browser/ui/browser.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame^] | 10 | #include "chrome/test/base/in_process_browser_test.h" |
| 11 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 12 | #include "net/base/cookie_store.h" |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 13 | #include "net/base/mock_host_resolver.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 14 | #include "net/test/test_server.h" |
[email protected] | ad94d34 | 2011-06-03 22:19:35 | [diff] [blame] | 15 | #include "net/url_request/url_request_context.h" |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 16 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 17 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | class GetCookiesTask : public Task { |
[email protected] | 34d18e4 | 2010-06-21 16:04:50 | [diff] [blame] | 21 | public: |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 22 | GetCookiesTask(const GURL& url, |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 23 | net::URLRequestContextGetter* context_getter, |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 24 | base::WaitableEvent* event, |
| 25 | std::string* cookies) |
| 26 | : url_(url), |
| 27 | context_getter_(context_getter), |
| 28 | event_(event), |
| 29 | cookies_(cookies) {} |
| 30 | |
| 31 | virtual void Run() { |
[email protected] | 277ec26 | 2011-03-30 21:09:40 | [diff] [blame] | 32 | *cookies_ = |
| 33 | context_getter_->GetURLRequestContext()->cookie_store()-> |
| 34 | GetCookies(url_); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 35 | event_->Signal(); |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | const GURL& url_; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 40 | net::URLRequestContextGetter* const context_getter_; |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 41 | base::WaitableEvent* const event_; |
| 42 | std::string* const cookies_; |
| 43 | |
| 44 | DISALLOW_COPY_AND_ASSIGN(GetCookiesTask); |
| 45 | }; |
| 46 | |
| 47 | class CookiePolicyBrowserTest : public InProcessBrowserTest { |
| 48 | protected: |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 49 | CookiePolicyBrowserTest() {} |
| 50 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 51 | std::string GetCookies(const GURL& url) { |
| 52 | std::string cookies; |
| 53 | base::WaitableEvent event(true /* manual reset */, |
| 54 | false /* not initially signaled */); |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 55 | net::URLRequestContextGetter* context_getter = |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 56 | browser()->profile()->GetRequestContext(); |
| 57 | EXPECT_TRUE( |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 58 | BrowserThread::PostTask( |
| 59 | BrowserThread::IO, FROM_HERE, |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 60 | new GetCookiesTask(url, context_getter, &event, &cookies))); |
| 61 | EXPECT_TRUE(event.Wait()); |
| 62 | return cookies; |
| 63 | } |
| 64 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 65 | private: |
| 66 | DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); |
| 67 | }; |
| 68 | |
| 69 | // Visits a page that sets a first-party cookie. |
| 70 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 71 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 72 | |
[email protected] | b281b6c | 2010-01-31 08:14:09 | [diff] [blame] | 73 | browser()->profile()->GetHostContentSettingsMap()-> |
| 74 | SetBlockThirdPartyCookies(true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 75 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 76 | GURL url(test_server()->GetURL("set-cookie?cookie1")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 77 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 78 | std::string cookie = GetCookies(url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 79 | ASSERT_EQ("", cookie); |
| 80 | |
| 81 | ui_test_utils::NavigateToURL(browser(), url); |
| 82 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 83 | cookie = GetCookies(url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 84 | EXPECT_EQ("cookie1", cookie); |
| 85 | } |
| 86 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 87 | // Visits a page that is a redirect across domain boundary to a page that sets |
| 88 | // a first-party cookie. |
| 89 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 90 | AllowFirstPartyCookiesRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 91 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 92 | |
[email protected] | b281b6c | 2010-01-31 08:14:09 | [diff] [blame] | 93 | browser()->profile()->GetHostContentSettingsMap()-> |
| 94 | SetBlockThirdPartyCookies(true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 95 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 96 | GURL url(test_server()->GetURL("server-redirect?")); |
| 97 | GURL redirected_url(test_server()->GetURL("set-cookie?cookie2")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 98 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 99 | // 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] | 100 | // third-party cookie blocking if the first party for cookies URL is not |
| 101 | // changed when we follow a redirect. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 102 | ASSERT_EQ("127.0.0.1", redirected_url.host()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 103 | GURL::Replacements replacements; |
| 104 | std::string new_host("www.example.com"); |
| 105 | replacements.SetHostStr(new_host); |
| 106 | redirected_url = redirected_url.ReplaceComponents(replacements); |
| 107 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 108 | std::string cookie = GetCookies(redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 109 | ASSERT_EQ("", cookie); |
| 110 | |
| 111 | host_resolver()->AddRule("www.example.com", "127.0.0.1"); |
| 112 | |
| 113 | ui_test_utils::NavigateToURL(browser(), |
| 114 | GURL(url.spec() + redirected_url.spec())); |
| 115 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 116 | cookie = GetCookies(redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 117 | EXPECT_EQ("cookie2", cookie); |
| 118 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 119 | |
| 120 | } // namespace |