blob: 4f3afac02dccb7d05c572ad9450c18623de02026 [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]631bb742011-11-02 11:29:3921using content::BrowserThread;
22
[email protected]9eaa18e2010-06-29 20:51:0123namespace {
24
25class GetCookiesTask : public Task {
[email protected]34d18e42010-06-21 16:04:5026 public:
[email protected]9eaa18e2010-06-29 20:51:0127 GetCookiesTask(const GURL& url,
[email protected]abe2c032011-03-31 18:49:3428 net::URLRequestContextGetter* context_getter,
[email protected]9eaa18e2010-06-29 20:51:0129 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]218aa6a12011-09-13 17:38:3837 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]9eaa18e2010-06-29 20:51:0150 }
51
52 private:
53 const GURL& url_;
[email protected]abe2c032011-03-31 18:49:3454 net::URLRequestContextGetter* const context_getter_;
[email protected]9eaa18e2010-06-29 20:51:0155 base::WaitableEvent* const event_;
56 std::string* const cookies_;
57
58 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
59};
60
61class CookiePolicyBrowserTest : public InProcessBrowserTest {
62 protected:
[email protected]c145edad2009-11-18 02:14:2763 CookiePolicyBrowserTest() {}
64
[email protected]9eaa18e2010-06-29 20:51:0165 std::string GetCookies(const GURL& url) {
66 std::string cookies;
67 base::WaitableEvent event(true /* manual reset */,
68 false /* not initially signaled */);
[email protected]abe2c032011-03-31 18:49:3469 net::URLRequestContextGetter* context_getter =
[email protected]9eaa18e2010-06-29 20:51:0170 browser()->profile()->GetRequestContext();
71 EXPECT_TRUE(
[email protected]ba4f1132010-10-09 02:02:3572 BrowserThread::PostTask(
73 BrowserThread::IO, FROM_HERE,
[email protected]9eaa18e2010-06-29 20:51:0174 new GetCookiesTask(url, context_getter, &event, &cookies)));
[email protected]866cf332011-10-12 03:09:4275 event.Wait();
[email protected]9eaa18e2010-06-29 20:51:0176 return cookies;
77 }
78
[email protected]c145edad2009-11-18 02:14:2779 private:
80 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest);
81};
82
83// Visits a page that sets a first-party cookie.
84IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) {
[email protected]95409e12010-08-17 20:07:1185 ASSERT_TRUE(test_server()->Start());
[email protected]c145edad2009-11-18 02:14:2786
[email protected]30fde822011-10-28 09:49:0587 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies,
88 true);
[email protected]c145edad2009-11-18 02:14:2789
[email protected]95409e12010-08-17 20:07:1190 GURL url(test_server()->GetURL("set-cookie?cookie1"));
[email protected]c145edad2009-11-18 02:14:2791
[email protected]9eaa18e2010-06-29 20:51:0192 std::string cookie = GetCookies(url);
[email protected]c145edad2009-11-18 02:14:2793 ASSERT_EQ("", cookie);
94
95 ui_test_utils::NavigateToURL(browser(), url);
96
[email protected]9eaa18e2010-06-29 20:51:0197 cookie = GetCookies(url);
[email protected]c145edad2009-11-18 02:14:2798 EXPECT_EQ("cookie1", cookie);
99}
100
[email protected]c145edad2009-11-18 02:14:27101// Visits a page that is a redirect across domain boundary to a page that sets
102// a first-party cookie.
103IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
104 AllowFirstPartyCookiesRedirect) {
[email protected]95409e12010-08-17 20:07:11105 ASSERT_TRUE(test_server()->Start());
[email protected]c145edad2009-11-18 02:14:27106
[email protected]30fde822011-10-28 09:49:05107 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies,
108 true);
[email protected]c145edad2009-11-18 02:14:27109
[email protected]95409e12010-08-17 20:07:11110 GURL url(test_server()->GetURL("server-redirect?"));
111 GURL redirected_url(test_server()->GetURL("set-cookie?cookie2"));
[email protected]c145edad2009-11-18 02:14:27112
[email protected]95409e12010-08-17 20:07:11113 // Change the host name from 127.0.0.1 to www.example.com so it triggers
[email protected]c145edad2009-11-18 02:14:27114 // third-party cookie blocking if the first party for cookies URL is not
115 // changed when we follow a redirect.
[email protected]95409e12010-08-17 20:07:11116 ASSERT_EQ("127.0.0.1", redirected_url.host());
[email protected]c145edad2009-11-18 02:14:27117 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]9eaa18e2010-06-29 20:51:01122 std::string cookie = GetCookies(redirected_url);
[email protected]c145edad2009-11-18 02:14:27123 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]9eaa18e2010-06-29 20:51:01130 cookie = GetCookies(redirected_url);
[email protected]c145edad2009-11-18 02:14:27131 EXPECT_EQ("cookie2", cookie);
132}
[email protected]9eaa18e2010-06-29 20:51:01133
134} // namespace