• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/task.h"
6 #include "base/synchronization/waitable_event.h"
7 #include "chrome/browser/content_settings/host_content_settings_map.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/test/in_process_browser_test.h"
11 #include "chrome/test/ui_test_utils.h"
12 #include "net/base/cookie_store.h"
13 #include "net/base/mock_host_resolver.h"
14 #include "net/test/test_server.h"
15 #include "net/url_request/url_request_context.cc"
16 #include "net/url_request/url_request_context_getter.h"
17 
18 namespace {
19 
20 class GetCookiesTask : public Task {
21  public:
GetCookiesTask(const GURL & url,net::URLRequestContextGetter * context_getter,base::WaitableEvent * event,std::string * cookies)22   GetCookiesTask(const GURL& url,
23                  net::URLRequestContextGetter* context_getter,
24                  base::WaitableEvent* event,
25                  std::string* cookies)
26       : url_(url),
27         context_getter_(context_getter),
28         event_(event),
29         cookies_(cookies) {}
30 
Run()31   virtual void Run() {
32     *cookies_ =
33         context_getter_->GetURLRequestContext()->cookie_store()->
34         GetCookies(url_);
35     event_->Signal();
36   }
37 
38  private:
39   const GURL& url_;
40   net::URLRequestContextGetter* const context_getter_;
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:
CookiePolicyBrowserTest()49   CookiePolicyBrowserTest() {}
50 
GetCookies(const GURL & url)51   std::string GetCookies(const GURL& url) {
52     std::string cookies;
53     base::WaitableEvent event(true /* manual reset */,
54                               false /* not initially signaled */);
55     net::URLRequestContextGetter* context_getter =
56         browser()->profile()->GetRequestContext();
57     EXPECT_TRUE(
58         BrowserThread::PostTask(
59             BrowserThread::IO, FROM_HERE,
60             new GetCookiesTask(url, context_getter, &event, &cookies)));
61     EXPECT_TRUE(event.Wait());
62     return cookies;
63   }
64 
65  private:
66   DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest);
67 };
68 
69 // Visits a page that sets a first-party cookie.
IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,AllowFirstPartyCookies)70 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) {
71   ASSERT_TRUE(test_server()->Start());
72 
73   browser()->profile()->GetHostContentSettingsMap()->
74       SetBlockThirdPartyCookies(true);
75 
76   GURL url(test_server()->GetURL("set-cookie?cookie1"));
77 
78   std::string cookie = GetCookies(url);
79   ASSERT_EQ("", cookie);
80 
81   ui_test_utils::NavigateToURL(browser(), url);
82 
83   cookie = GetCookies(url);
84   EXPECT_EQ("cookie1", cookie);
85 }
86 
87 // Visits a page that is a redirect across domain boundary to a page that sets
88 // a first-party cookie.
IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,AllowFirstPartyCookiesRedirect)89 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
90                        AllowFirstPartyCookiesRedirect) {
91   ASSERT_TRUE(test_server()->Start());
92 
93   browser()->profile()->GetHostContentSettingsMap()->
94       SetBlockThirdPartyCookies(true);
95 
96   GURL url(test_server()->GetURL("server-redirect?"));
97   GURL redirected_url(test_server()->GetURL("set-cookie?cookie2"));
98 
99   // Change the host name from 127.0.0.1 to www.example.com so it triggers
100   // third-party cookie blocking if the first party for cookies URL is not
101   // changed when we follow a redirect.
102   ASSERT_EQ("127.0.0.1", redirected_url.host());
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 
108   std::string cookie = GetCookies(redirected_url);
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 
116   cookie = GetCookies(redirected_url);
117   EXPECT_EQ("cookie2", cookie);
118 }
119 
120 }  // namespace
121