1 // Copyright (c) 2012 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.
6 #include "base/bind_helpers.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "components/content_settings/core/common/pref_names.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/test/spawned_test_server/spawned_test_server.h"
20 using content::BrowserThread
;
24 class CookiePolicyBrowserTest
: public InProcessBrowserTest
{
26 CookiePolicyBrowserTest() {}
29 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest
);
32 // Visits a page that sets a first-party cookie.
33 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest
, AllowFirstPartyCookies
) {
34 ASSERT_TRUE(test_server()->Start());
36 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
,
39 GURL
url(test_server()->GetURL("set-cookie?cookie1"));
41 std::string cookie
= content::GetCookies(browser()->profile(), url
);
42 ASSERT_EQ("", cookie
);
44 ui_test_utils::NavigateToURL(browser(), url
);
46 cookie
= content::GetCookies(browser()->profile(), url
);
47 EXPECT_EQ("cookie1", cookie
);
50 // Visits a page that is a redirect across domain boundary to a page that sets
51 // a first-party cookie.
52 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest
,
53 AllowFirstPartyCookiesRedirect
) {
54 ASSERT_TRUE(test_server()->Start());
56 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
,
59 GURL
url(test_server()->GetURL("server-redirect?"));
60 GURL
redirected_url(test_server()->GetURL("set-cookie?cookie2"));
62 // Change the host name from 127.0.0.1 to www.example.com so it triggers
63 // third-party cookie blocking if the first party for cookies URL is not
64 // changed when we follow a redirect.
65 ASSERT_EQ("127.0.0.1", redirected_url
.host());
66 GURL::Replacements replacements
;
67 replacements
.SetHostStr("www.example.com");
68 redirected_url
= redirected_url
.ReplaceComponents(replacements
);
71 content::GetCookies(browser()->profile(), redirected_url
);
72 ASSERT_EQ("", cookie
);
74 host_resolver()->AddRule("www.example.com", "127.0.0.1");
76 ui_test_utils::NavigateToURL(browser(),
77 GURL(url
.spec() + redirected_url
.spec()));
79 cookie
= content::GetCookies(browser()->profile(), redirected_url
);
80 EXPECT_EQ("cookie2", cookie
);