blob: a505ea89df33f9537ea68d4f1ce2d755fb27524d [file] [log] [blame]
[email protected]aa84a7e2012-03-15 21:29:061// Copyright (c) 2012 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]ee611372011-11-29 05:41:176#include "base/bind_helpers.h"
[email protected]9d01a6a2010-11-30 12:03:337#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]30fde822011-10-28 09:49:058#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:339#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1010#include "chrome/browser/ui/browser.h"
[email protected]88509ab2012-08-27 15:04:1411#include "chrome/browser/ui/tab_contents/tab_contents.h"
[email protected]59253a652012-11-20 00:17:2612#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]30fde822011-10-28 09:49:0513#include "chrome/common/pref_names.h"
[email protected]af44e7fb2011-07-29 18:32:3214#include "chrome/test/base/in_process_browser_test.h"
15#include "chrome/test/base/ui_test_utils.h"
[email protected]88509ab2012-08-27 15:04:1416#include "content/public/test/browser_test_utils.h"
[email protected]c4ff4952010-01-08 19:12:4717#include "net/base/mock_host_resolver.h"
[email protected]3985ba82010-07-29 21:44:1218#include "net/test/test_server.h"
[email protected]c145edad2009-11-18 02:14:2719
[email protected]631bb742011-11-02 11:29:3920using content::BrowserThread;
21
[email protected]9eaa18e2010-06-29 20:51:0122namespace {
23
[email protected]9eaa18e2010-06-29 20:51:0124class CookiePolicyBrowserTest : public InProcessBrowserTest {
25 protected:
[email protected]c145edad2009-11-18 02:14:2726 CookiePolicyBrowserTest() {}
27
[email protected]c145edad2009-11-18 02:14:2728 private:
29 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest);
30};
31
32// Visits a page that sets a first-party cookie.
33IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) {
[email protected]95409e12010-08-17 20:07:1134 ASSERT_TRUE(test_server()->Start());
[email protected]c145edad2009-11-18 02:14:2735
[email protected]30fde822011-10-28 09:49:0536 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies,
37 true);
[email protected]c145edad2009-11-18 02:14:2738
[email protected]95409e12010-08-17 20:07:1139 GURL url(test_server()->GetURL("set-cookie?cookie1"));
[email protected]c145edad2009-11-18 02:14:2740
[email protected]59253a652012-11-20 00:17:2641 TabContents* tab = browser()->tab_strip_model()->GetActiveTabContents();
[email protected]88509ab2012-08-27 15:04:1442 std::string cookie = content::GetCookies(tab->profile(), url);
[email protected]c145edad2009-11-18 02:14:2743 ASSERT_EQ("", cookie);
44
45 ui_test_utils::NavigateToURL(browser(), url);
46
[email protected]88509ab2012-08-27 15:04:1447 cookie = content::GetCookies(tab->profile(), url);
[email protected]c145edad2009-11-18 02:14:2748 EXPECT_EQ("cookie1", cookie);
49}
50
[email protected]c145edad2009-11-18 02:14:2751// Visits a page that is a redirect across domain boundary to a page that sets
52// a first-party cookie.
53IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
54 AllowFirstPartyCookiesRedirect) {
[email protected]95409e12010-08-17 20:07:1155 ASSERT_TRUE(test_server()->Start());
[email protected]c145edad2009-11-18 02:14:2756
[email protected]30fde822011-10-28 09:49:0557 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies,
58 true);
[email protected]c145edad2009-11-18 02:14:2759
[email protected]95409e12010-08-17 20:07:1160 GURL url(test_server()->GetURL("server-redirect?"));
61 GURL redirected_url(test_server()->GetURL("set-cookie?cookie2"));
[email protected]c145edad2009-11-18 02:14:2762
[email protected]95409e12010-08-17 20:07:1163 // Change the host name from 127.0.0.1 to www.example.com so it triggers
[email protected]c145edad2009-11-18 02:14:2764 // third-party cookie blocking if the first party for cookies URL is not
65 // changed when we follow a redirect.
[email protected]95409e12010-08-17 20:07:1166 ASSERT_EQ("127.0.0.1", redirected_url.host());
[email protected]c145edad2009-11-18 02:14:2767 GURL::Replacements replacements;
68 std::string new_host("www.example.com");
69 replacements.SetHostStr(new_host);
70 redirected_url = redirected_url.ReplaceComponents(replacements);
71
[email protected]59253a652012-11-20 00:17:2672 TabContents* tab = browser()->tab_strip_model()->GetActiveTabContents();
[email protected]88509ab2012-08-27 15:04:1473 std::string cookie = content::GetCookies(tab->profile(), redirected_url);
[email protected]c145edad2009-11-18 02:14:2774 ASSERT_EQ("", cookie);
75
76 host_resolver()->AddRule("www.example.com", "127.0.0.1");
77
78 ui_test_utils::NavigateToURL(browser(),
79 GURL(url.spec() + redirected_url.spec()));
80
[email protected]88509ab2012-08-27 15:04:1481 cookie = content::GetCookies(tab->profile(), redirected_url);
[email protected]c145edad2009-11-18 02:14:2782 EXPECT_EQ("cookie2", cookie);
83}
[email protected]9eaa18e2010-06-29 20:51:0184
85} // namespace