blob: 2128a15a4528ea4b37de53833bab8d12dd31ceaf [file] [log] [blame]
[email protected]277ec262011-03-30 21:09:401// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c145edad2009-11-18 02:14:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]218aa6a12011-09-13 17:38:385#include "base/bind.h"
[email protected]9eaa18e2010-06-29 20:51:016#include "base/task.h"
[email protected]44f9c952011-01-02 06:05:397#include "base/synchronization/waitable_event.h"
[email protected]9d01a6a2010-11-30 12:03:338#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]30fde822011-10-28 09:49:059#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3310#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1011#include "chrome/browser/ui/browser.h"
[email protected]30fde822011-10-28 09:49:0512#include "chrome/common/pref_names.h"
[email protected]af44e7fb2011-07-29 18:32:3213#include "chrome/test/base/in_process_browser_test.h"
14#include "chrome/test/base/ui_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1215#include "net/base/cookie_store.h"
[email protected]c4ff4952010-01-08 19:12:4716#include "net/base/mock_host_resolver.h"
[email protected]3985ba82010-07-29 21:44:1217#include "net/test/test_server.h"
[email protected]ad94d342011-06-03 22:19:3518#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3419#include "net/url_request/url_request_context_getter.h"
[email protected]c145edad2009-11-18 02:14:2720
[email protected]9eaa18e2010-06-29 20:51:0121namespace {
22
23class GetCookiesTask : public Task {
[email protected]34d18e42010-06-21 16:04:5024 public:
[email protected]9eaa18e2010-06-29 20:51:0125 GetCookiesTask(const GURL& url,
[email protected]abe2c032011-03-31 18:49:3426 net::URLRequestContextGetter* context_getter,
[email protected]9eaa18e2010-06-29 20:51:0127 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]218aa6a12011-09-13 17:38:3835 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]9eaa18e2010-06-29 20:51:0148 }
49
50 private:
51 const GURL& url_;
[email protected]abe2c032011-03-31 18:49:3452 net::URLRequestContextGetter* const context_getter_;
[email protected]9eaa18e2010-06-29 20:51:0153 base::WaitableEvent* const event_;
54 std::string* const cookies_;
55
56 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
57};
58
59class CookiePolicyBrowserTest : public InProcessBrowserTest {
60 protected:
[email protected]c145edad2009-11-18 02:14:2761 CookiePolicyBrowserTest() {}
62
[email protected]9eaa18e2010-06-29 20:51:0163 std::string GetCookies(const GURL& url) {
64 std::string cookies;
65 base::WaitableEvent event(true /* manual reset */,
66 false /* not initially signaled */);
[email protected]abe2c032011-03-31 18:49:3467 net::URLRequestContextGetter* context_getter =
[email protected]9eaa18e2010-06-29 20:51:0168 browser()->profile()->GetRequestContext();
69 EXPECT_TRUE(
[email protected]ba4f1132010-10-09 02:02:3570 BrowserThread::PostTask(
71 BrowserThread::IO, FROM_HERE,
[email protected]9eaa18e2010-06-29 20:51:0172 new GetCookiesTask(url, context_getter, &event, &cookies)));
[email protected]866cf332011-10-12 03:09:4273 event.Wait();
[email protected]9eaa18e2010-06-29 20:51:0174 return cookies;
75 }
76
[email protected]c145edad2009-11-18 02:14:2777 private:
78 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest);
79};
80
81// Visits a page that sets a first-party cookie.
82IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) {
[email protected]95409e12010-08-17 20:07:1183 ASSERT_TRUE(test_server()->Start());
[email protected]c145edad2009-11-18 02:14:2784
[email protected]30fde822011-10-28 09:49:0585 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies,
86 true);
[email protected]c145edad2009-11-18 02:14:2787
[email protected]95409e12010-08-17 20:07:1188 GURL url(test_server()->GetURL("set-cookie?cookie1"));
[email protected]c145edad2009-11-18 02:14:2789
[email protected]9eaa18e2010-06-29 20:51:0190 std::string cookie = GetCookies(url);
[email protected]c145edad2009-11-18 02:14:2791 ASSERT_EQ("", cookie);
92
93 ui_test_utils::NavigateToURL(browser(), url);
94
[email protected]9eaa18e2010-06-29 20:51:0195 cookie = GetCookies(url);
[email protected]c145edad2009-11-18 02:14:2796 EXPECT_EQ("cookie1", cookie);
97}
98
[email protected]c145edad2009-11-18 02:14:2799// Visits a page that is a redirect across domain boundary to a page that sets
100// a first-party cookie.
101IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
102 AllowFirstPartyCookiesRedirect) {
[email protected]95409e12010-08-17 20:07:11103 ASSERT_TRUE(test_server()->Start());
[email protected]c145edad2009-11-18 02:14:27104
[email protected]30fde822011-10-28 09:49:05105 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies,
106 true);
[email protected]c145edad2009-11-18 02:14:27107
[email protected]95409e12010-08-17 20:07:11108 GURL url(test_server()->GetURL("server-redirect?"));
109 GURL redirected_url(test_server()->GetURL("set-cookie?cookie2"));
[email protected]c145edad2009-11-18 02:14:27110
[email protected]95409e12010-08-17 20:07:11111 // Change the host name from 127.0.0.1 to www.example.com so it triggers
[email protected]c145edad2009-11-18 02:14:27112 // third-party cookie blocking if the first party for cookies URL is not
113 // changed when we follow a redirect.
[email protected]95409e12010-08-17 20:07:11114 ASSERT_EQ("127.0.0.1", redirected_url.host());
[email protected]c145edad2009-11-18 02:14:27115 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]9eaa18e2010-06-29 20:51:01120 std::string cookie = GetCookies(redirected_url);
[email protected]c145edad2009-11-18 02:14:27121 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]9eaa18e2010-06-29 20:51:01128 cookie = GetCookies(redirected_url);
[email protected]c145edad2009-11-18 02:14:27129 EXPECT_EQ("cookie2", cookie);
130}
[email protected]9eaa18e2010-06-29 20:51:01131
132} // namespace