Clark DuVall | 385b5a5 | 2018-06-14 21:33:32 | [diff] [blame] | 1 | // 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 | |
| 9 | namespace network { |
| 10 | namespace { |
| 11 | |
| 12 | constexpr char kURL[] = "http://foo.com"; |
| 13 | constexpr char kOtherURL[] = "http://other.com"; |
| 14 | |
| 15 | ContentSettingPatternSource 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 | |
| 24 | TEST(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 | |
| 31 | TEST(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 | |
| 40 | TEST(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 DuVall | c57280be | 2018-06-19 03:46:23 | [diff] [blame] | 48 | |
| 49 | settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr, &setting); |
| 50 | EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); |
Clark DuVall | 385b5a5 | 2018-06-14 21:33:32 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST(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 DuVall | c57280be | 2018-06-19 03:46:23 | [diff] [blame] | 63 | TEST(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 | |
| 73 | TEST(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 | |
| 83 | TEST(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 DuVall | 385b5a5 | 2018-06-14 21:33:32 | [diff] [blame] | 93 | TEST(CookieSettingsTest, CreateDeleteCookieOnExitPredicateNoSettings) { |
| 94 | CookieSettings settings; |
| 95 | EXPECT_FALSE(settings.CreateDeleteCookieOnExitPredicate()); |
| 96 | } |
| 97 | |
| 98 | TEST(CookieSettingsTest, CreateDeleteCookieOnExitPredicateNoSessionOnly) { |
| 99 | CookieSettings settings; |
| 100 | settings.set_content_settings( |
| 101 | {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)}); |
| 102 | EXPECT_FALSE(settings.CreateDeleteCookieOnExitPredicate()); |
| 103 | } |
| 104 | |
| 105 | TEST(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 | |
| 112 | TEST(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 DuVall | e873764 | 2018-08-31 17:26:34 | [diff] [blame] | 120 | TEST(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 DuVall | 04275658 | 2018-12-06 19:17:42 | [diff] [blame] | 126 | settings.GetCookieSetting(GURL("https://foo.com") /* url */, |
| 127 | GURL("chrome://foo") /* first_party_url */, |
| 128 | nullptr /* source */, &setting); |
Clark DuVall | e873764 | 2018-08-31 17:26:34 | [diff] [blame] | 129 | EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); |
| 130 | |
Clark DuVall | 04275658 | 2018-12-06 19:17:42 | [diff] [blame] | 131 | settings.GetCookieSetting(GURL("chrome://foo") /* url */, |
| 132 | GURL("https://foo.com") /* first_party_url */, |
| 133 | nullptr /* source */, &setting); |
Clark DuVall | e873764 | 2018-08-31 17:26:34 | [diff] [blame] | 134 | EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); |
| 135 | |
Clark DuVall | 04275658 | 2018-12-06 19:17:42 | [diff] [blame] | 136 | 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 | |
| 142 | TEST(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 DuVall | e873764 | 2018-08-31 17:26:34 | [diff] [blame] | 162 | EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); |
| 163 | } |
| 164 | |
| 165 | TEST(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 DuVall | 04275658 | 2018-12-06 19:17:42 | [diff] [blame] | 171 | settings.GetCookieSetting( |
| 172 | GURL("chrome-extension://bar") /* url */, |
| 173 | GURL("chrome-extension://foo") /* first_party_url */, |
| 174 | nullptr /* source */, &setting); |
Clark DuVall | e873764 | 2018-08-31 17:26:34 | [diff] [blame] | 175 | EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); |
| 176 | |
Clark DuVall | 04275658 | 2018-12-06 19:17:42 | [diff] [blame] | 177 | settings.GetCookieSetting( |
| 178 | GURL("http://foo.com") /* url */, |
| 179 | GURL("chrome-extension://foo") /* first_party_url */, |
| 180 | nullptr /* source */, &setting); |
Clark DuVall | e873764 | 2018-08-31 17:26:34 | [diff] [blame] | 181 | EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); |
| 182 | |
Clark DuVall | 04275658 | 2018-12-06 19:17:42 | [diff] [blame] | 183 | settings.GetCookieSetting(GURL("chrome-extension://foo") /* url */, |
| 184 | GURL("http://foo.com") /* first_party_url */, |
| 185 | nullptr /* source */, &setting); |
Clark DuVall | e873764 | 2018-08-31 17:26:34 | [diff] [blame] | 186 | EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); |
| 187 | } |
| 188 | |
Clark DuVall | 385b5a5 | 2018-06-14 21:33:32 | [diff] [blame] | 189 | } // namespace |
| 190 | } // namespace network |