blob: d2e131eab81c7c0d52bce65b6e22236611c49a29 [file] [log] [blame]
Clark DuVall385b5a52018-06-14 21:33:321// Copyright 2018 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 "services/network/cookie_settings.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace network {
10namespace {
11
12constexpr char kURL[] = "http://foo.com";
13constexpr char kOtherURL[] = "http://other.com";
14
15ContentSettingPatternSource CreateSetting(const std::string& url,
16 const std::string& secondary_url,
17 ContentSetting setting) {
18 return ContentSettingPatternSource(
19 ContentSettingsPattern::FromString(url),
20 ContentSettingsPattern::FromString(secondary_url), base::Value(setting),
21 std::string(), false);
22}
23
24TEST(CookieSettingsTest, GetCookieSettingDefault) {
25 CookieSettings settings;
26 ContentSetting setting;
27 settings.GetCookieSetting(GURL(kURL), GURL(kURL), nullptr, &setting);
28 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
29}
30
31TEST(CookieSettingsTest, GetCookieSetting) {
32 CookieSettings settings;
33 settings.set_content_settings(
34 {CreateSetting(kURL, kURL, CONTENT_SETTING_BLOCK)});
35 ContentSetting setting;
36 settings.GetCookieSetting(GURL(kURL), GURL(kURL), nullptr, &setting);
37 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
38}
39
40TEST(CookieSettingsTest, GetCookieSettingMustMatchBothPatterns) {
41 CookieSettings settings;
42 // This setting needs kOtherURL as the secondary pattern.
43 settings.set_content_settings(
44 {CreateSetting(kURL, kOtherURL, CONTENT_SETTING_BLOCK)});
45 ContentSetting setting;
46 settings.GetCookieSetting(GURL(kURL), GURL(kURL), nullptr, &setting);
47 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
Clark DuVallc57280be2018-06-19 03:46:2348
49 settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr, &setting);
50 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
Clark DuVall385b5a52018-06-14 21:33:3251}
52
53TEST(CookieSettingsTest, GetCookieSettingGetsFirstSetting) {
54 CookieSettings settings;
55 settings.set_content_settings(
56 {CreateSetting(kURL, kURL, CONTENT_SETTING_BLOCK),
57 CreateSetting(kURL, kURL, CONTENT_SETTING_SESSION_ONLY)});
58 ContentSetting setting;
59 settings.GetCookieSetting(GURL(kURL), GURL(kURL), nullptr, &setting);
60 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
61}
62
Clark DuVallc57280be2018-06-19 03:46:2363TEST(CookieSettingsTest, GetCookieSettingDontBlockThirdParty) {
64 CookieSettings settings;
65 settings.set_content_settings(
66 {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
67 settings.set_block_third_party_cookies(false);
68 ContentSetting setting;
69 settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr, &setting);
70 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
71}
72
73TEST(CookieSettingsTest, GetCookieSettingBlockThirdParty) {
74 CookieSettings settings;
75 settings.set_content_settings(
76 {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
77 settings.set_block_third_party_cookies(true);
78 ContentSetting setting;
79 settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr, &setting);
80 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
81}
82
83TEST(CookieSettingsTest, GetCookieSettingDontBlockThirdPartyWithException) {
84 CookieSettings settings;
85 settings.set_content_settings(
86 {CreateSetting(kURL, kOtherURL, CONTENT_SETTING_ALLOW)});
87 settings.set_block_third_party_cookies(true);
88 ContentSetting setting;
89 settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr, &setting);
90 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
91}
92
Clark DuVall385b5a52018-06-14 21:33:3293TEST(CookieSettingsTest, CreateDeleteCookieOnExitPredicateNoSettings) {
94 CookieSettings settings;
95 EXPECT_FALSE(settings.CreateDeleteCookieOnExitPredicate());
96}
97
98TEST(CookieSettingsTest, CreateDeleteCookieOnExitPredicateNoSessionOnly) {
99 CookieSettings settings;
100 settings.set_content_settings(
101 {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
102 EXPECT_FALSE(settings.CreateDeleteCookieOnExitPredicate());
103}
104
105TEST(CookieSettingsTest, CreateDeleteCookieOnExitPredicateSessionOnly) {
106 CookieSettings settings;
107 settings.set_content_settings(
108 {CreateSetting("*", "*", CONTENT_SETTING_SESSION_ONLY)});
109 EXPECT_TRUE(settings.CreateDeleteCookieOnExitPredicate().Run(kURL, false));
110}
111
112TEST(CookieSettingsTest, CreateDeleteCookieOnExitPredicateAllow) {
113 CookieSettings settings;
114 settings.set_content_settings(
115 {CreateSetting("*", "*", CONTENT_SETTING_ALLOW),
116 CreateSetting("*", "*", CONTENT_SETTING_SESSION_ONLY)});
117 EXPECT_FALSE(settings.CreateDeleteCookieOnExitPredicate().Run(kURL, false));
118}
119
Clark DuValle8737642018-08-31 17:26:34120TEST(CookieSettingsTest, GetCookieSettingSecureOriginCookiesAllowed) {
121 CookieSettings settings;
122 settings.set_secure_origin_cookies_allowed_schemes({"chrome"});
123 settings.set_block_third_party_cookies(true);
124
125 ContentSetting setting;
Clark DuVall042756582018-12-06 19:17:42126 settings.GetCookieSetting(GURL("https://foo.com") /* url */,
127 GURL("chrome://foo") /* first_party_url */,
128 nullptr /* source */, &setting);
Clark DuValle8737642018-08-31 17:26:34129 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
130
Clark DuVall042756582018-12-06 19:17:42131 settings.GetCookieSetting(GURL("chrome://foo") /* url */,
132 GURL("https://foo.com") /* first_party_url */,
133 nullptr /* source */, &setting);
Clark DuValle8737642018-08-31 17:26:34134 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
135
Clark DuVall042756582018-12-06 19:17:42136 settings.GetCookieSetting(GURL("http://foo.com") /* url */,
137 GURL("chrome://foo") /* first_party_url */,
138 nullptr /* source */, &setting);
139 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
140}
141
142TEST(CookieSettingsTest, GetCookieSettingWithThirdPartyCookiesAllowedScheme) {
143 CookieSettings settings;
144 settings.set_third_party_cookies_allowed_schemes({"chrome-extension"});
145 settings.set_block_third_party_cookies(true);
146
147 ContentSetting setting;
148 settings.GetCookieSetting(
149 GURL("http://foo.com") /* url */,
150 GURL("chrome-extension://foo") /* first_party_url */,
151 nullptr /* source */, &setting);
152 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
153
154 settings.GetCookieSetting(GURL("http://foo.com") /* url */,
155 GURL("other-scheme://foo") /* first_party_url */,
156 nullptr /* source */, &setting);
157 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
158
159 settings.GetCookieSetting(GURL("chrome-extension://foo") /* url */,
160 GURL("http://foo.com") /* first_party_url */,
161 nullptr /* source */, &setting);
Clark DuValle8737642018-08-31 17:26:34162 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
163}
164
165TEST(CookieSettingsTest, GetCookieSettingMatchingSchemeCookiesAllowed) {
166 CookieSettings settings;
167 settings.set_matching_scheme_cookies_allowed_schemes({"chrome-extension"});
168 settings.set_block_third_party_cookies(true);
169
170 ContentSetting setting;
Clark DuVall042756582018-12-06 19:17:42171 settings.GetCookieSetting(
172 GURL("chrome-extension://bar") /* url */,
173 GURL("chrome-extension://foo") /* first_party_url */,
174 nullptr /* source */, &setting);
Clark DuValle8737642018-08-31 17:26:34175 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
176
Clark DuVall042756582018-12-06 19:17:42177 settings.GetCookieSetting(
178 GURL("http://foo.com") /* url */,
179 GURL("chrome-extension://foo") /* first_party_url */,
180 nullptr /* source */, &setting);
Clark DuValle8737642018-08-31 17:26:34181 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
182
Clark DuVall042756582018-12-06 19:17:42183 settings.GetCookieSetting(GURL("chrome-extension://foo") /* url */,
184 GURL("http://foo.com") /* first_party_url */,
185 nullptr /* source */, &setting);
Clark DuValle8737642018-08-31 17:26:34186 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
187}
188
Clark DuVall385b5a52018-06-14 21:33:32189} // namespace
190} // namespace network