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 "content/public/test/browser_test_utils.h"
16 #include "net/dns/mock_host_resolver.h"
17 #include "net/test/spawned_test_server/spawned_test_server.h"
19 using content::BrowserThread
;
23 class CookiePolicyBrowserTest
: public InProcessBrowserTest
{
25 CookiePolicyBrowserTest() {}
28 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest
);
31 // Visits a page that sets a first-party cookie.
32 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest
, AllowFirstPartyCookies
) {
33 ASSERT_TRUE(test_server()->Start());
35 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
,
38 GURL
url(test_server()->GetURL("set-cookie?cookie1"));
40 std::string cookie
= content::GetCookies(browser()->profile(), url
);
41 ASSERT_EQ("", cookie
);
43 ui_test_utils::NavigateToURL(browser(), url
);
45 cookie
= content::GetCookies(browser()->profile(), url
);
46 EXPECT_EQ("cookie1", cookie
);
49 // Visits a page that is a redirect across domain boundary to a page that sets
50 // a first-party cookie.
51 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest
,
52 AllowFirstPartyCookiesRedirect
) {
53 ASSERT_TRUE(test_server()->Start());
55 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies
,
58 GURL
url(test_server()->GetURL("server-redirect?"));
59 GURL
redirected_url(test_server()->GetURL("set-cookie?cookie2"));
61 // Change the host name from 127.0.0.1 to www.example.com so it triggers
62 // third-party cookie blocking if the first party for cookies URL is not
63 // changed when we follow a redirect.
64 ASSERT_EQ("127.0.0.1", redirected_url
.host());
65 GURL::Replacements replacements
;
66 replacements
.SetHostStr("www.example.com");
67 redirected_url
= redirected_url
.ReplaceComponents(replacements
);
70 content::GetCookies(browser()->profile(), redirected_url
);
71 ASSERT_EQ("", cookie
);
73 host_resolver()->AddRule("www.example.com", "127.0.0.1");
75 ui_test_utils::NavigateToURL(browser(),
76 GURL(url
.spec() + redirected_url
.spec()));
78 cookie
= content::GetCookies(browser()->profile(), redirected_url
);
79 EXPECT_EQ("cookie2", cookie
);