blob: 251ae6907db4dddcc26c761488cabe734889e3b7 [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]9eaa18e2010-06-29 20:51:015#include "base/task.h"
[email protected]44f9c952011-01-02 06:05:396#include "base/synchronization/waitable_event.h"
[email protected]9d01a6a2010-11-30 12:03:337#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]8ecad5e2010-12-02 21:18:338#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:109#include "chrome/browser/ui/browser.h"
[email protected]af44e7fb2011-07-29 18:32:3210#include "chrome/test/base/in_process_browser_test.h"
11#include "chrome/test/base/ui_test_utils.h"
[email protected]3985ba82010-07-29 21:44:1212#include "net/base/cookie_store.h"
[email protected]c4ff4952010-01-08 19:12:4713#include "net/base/mock_host_resolver.h"
[email protected]3985ba82010-07-29 21:44:1214#include "net/test/test_server.h"
[email protected]ad94d342011-06-03 22:19:3515#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3416#include "net/url_request/url_request_context_getter.h"
[email protected]c145edad2009-11-18 02:14:2717
[email protected]9eaa18e2010-06-29 20:51:0118namespace {
19
20class GetCookiesTask : public Task {
[email protected]34d18e42010-06-21 16:04:5021 public:
[email protected]9eaa18e2010-06-29 20:51:0122 GetCookiesTask(const GURL& url,
[email protected]abe2c032011-03-31 18:49:3423 net::URLRequestContextGetter* context_getter,
[email protected]9eaa18e2010-06-29 20:51:0124 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]277ec262011-03-30 21:09:4032 *cookies_ =
33 context_getter_->GetURLRequestContext()->cookie_store()->
34 GetCookies(url_);
[email protected]9eaa18e2010-06-29 20:51:0135 event_->Signal();
36 }
37
38 private:
39 const GURL& url_;
[email protected]abe2c032011-03-31 18:49:3440 net::URLRequestContextGetter* const context_getter_;
[email protected]9eaa18e2010-06-29 20:51:0141 base::WaitableEvent* const event_;
42 std::string* const cookies_;
43
44 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
45};
46
47class CookiePolicyBrowserTest : public InProcessBrowserTest {
48 protected:
[email protected]c145edad2009-11-18 02:14:2749 CookiePolicyBrowserTest() {}
50
[email protected]9eaa18e2010-06-29 20:51:0151 std::string GetCookies(const GURL& url) {
52 std::string cookies;
53 base::WaitableEvent event(true /* manual reset */,
54 false /* not initially signaled */);
[email protected]abe2c032011-03-31 18:49:3455 net::URLRequestContextGetter* context_getter =
[email protected]9eaa18e2010-06-29 20:51:0156 browser()->profile()->GetRequestContext();
57 EXPECT_TRUE(
[email protected]ba4f1132010-10-09 02:02:3558 BrowserThread::PostTask(
59 BrowserThread::IO, FROM_HERE,
[email protected]9eaa18e2010-06-29 20:51:0160 new GetCookiesTask(url, context_getter, &event, &cookies)));
61 EXPECT_TRUE(event.Wait());
62 return cookies;
63 }
64
[email protected]c145edad2009-11-18 02:14:2765 private:
66 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest);
67};
68
69// Visits a page that sets a first-party cookie.
70IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) {
[email protected]95409e12010-08-17 20:07:1171 ASSERT_TRUE(test_server()->Start());
[email protected]c145edad2009-11-18 02:14:2772
[email protected]b281b6c2010-01-31 08:14:0973 browser()->profile()->GetHostContentSettingsMap()->
74 SetBlockThirdPartyCookies(true);
[email protected]c145edad2009-11-18 02:14:2775
[email protected]95409e12010-08-17 20:07:1176 GURL url(test_server()->GetURL("set-cookie?cookie1"));
[email protected]c145edad2009-11-18 02:14:2777
[email protected]9eaa18e2010-06-29 20:51:0178 std::string cookie = GetCookies(url);
[email protected]c145edad2009-11-18 02:14:2779 ASSERT_EQ("", cookie);
80
81 ui_test_utils::NavigateToURL(browser(), url);
82
[email protected]9eaa18e2010-06-29 20:51:0183 cookie = GetCookies(url);
[email protected]c145edad2009-11-18 02:14:2784 EXPECT_EQ("cookie1", cookie);
85}
86
[email protected]c145edad2009-11-18 02:14:2787// Visits a page that is a redirect across domain boundary to a page that sets
88// a first-party cookie.
89IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
90 AllowFirstPartyCookiesRedirect) {
[email protected]95409e12010-08-17 20:07:1191 ASSERT_TRUE(test_server()->Start());
[email protected]c145edad2009-11-18 02:14:2792
[email protected]b281b6c2010-01-31 08:14:0993 browser()->profile()->GetHostContentSettingsMap()->
94 SetBlockThirdPartyCookies(true);
[email protected]c145edad2009-11-18 02:14:2795
[email protected]95409e12010-08-17 20:07:1196 GURL url(test_server()->GetURL("server-redirect?"));
97 GURL redirected_url(test_server()->GetURL("set-cookie?cookie2"));
[email protected]c145edad2009-11-18 02:14:2798
[email protected]95409e12010-08-17 20:07:1199 // Change the host name from 127.0.0.1 to www.example.com so it triggers
[email protected]c145edad2009-11-18 02:14:27100 // third-party cookie blocking if the first party for cookies URL is not
101 // changed when we follow a redirect.
[email protected]95409e12010-08-17 20:07:11102 ASSERT_EQ("127.0.0.1", redirected_url.host());
[email protected]c145edad2009-11-18 02:14:27103 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]9eaa18e2010-06-29 20:51:01108 std::string cookie = GetCookies(redirected_url);
[email protected]c145edad2009-11-18 02:14:27109 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]9eaa18e2010-06-29 20:51:01116 cookie = GetCookies(redirected_url);
[email protected]c145edad2009-11-18 02:14:27117 EXPECT_EQ("cookie2", cookie);
118}
[email protected]9eaa18e2010-06-29 20:51:01119
120} // namespace