1 // Copyright (c) 2010 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 "net/base/net_errors.h" 6 #include "net/base/static_cookie_policy.h" 7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "googleurl/src/gurl.h" 9 10 class StaticCookiePolicyTest : public testing::Test { 11 public: 12 StaticCookiePolicyTest() 13 : url_google_("http://www.google.izzle"), 14 url_google_secure_("https://www.google.izzle"), 15 url_google_mail_("http://mail.google.izzle"), 16 url_google_analytics_("http://www.googleanalytics.izzle") { 17 } 18 void SetPolicyType(net::StaticCookiePolicy::Type type) { 19 policy_.set_type(type); 20 } 21 int CanGetCookies(const GURL& url, const GURL& first_party) { 22 return policy_.CanGetCookies(url, first_party, NULL); 23 } 24 int CanSetCookie(const GURL& url, const GURL& first_party) { 25 return policy_.CanSetCookie(url, first_party, std::string(), NULL); 26 } 27 protected: 28 net::StaticCookiePolicy policy_; 29 GURL url_google_; 30 GURL url_google_secure_; 31 GURL url_google_mail_; 32 GURL url_google_analytics_; 33 }; 34 35 TEST_F(StaticCookiePolicyTest, DefaultPolicyTest) { 36 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_)); 37 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_secure_)); 38 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_mail_)); 39 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_analytics_)); 40 EXPECT_EQ(net::OK, CanGetCookies(url_google_, GURL())); 41 42 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_)); 43 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_secure_)); 44 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_mail_)); 45 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_analytics_)); 46 EXPECT_EQ(net::OK, CanSetCookie(url_google_, GURL())); 47 } 48 49 TEST_F(StaticCookiePolicyTest, AllowAllCookiesTest) { 50 SetPolicyType(net::StaticCookiePolicy::ALLOW_ALL_COOKIES); 51 52 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_)); 53 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_secure_)); 54 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_mail_)); 55 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_analytics_)); 56 EXPECT_EQ(net::OK, CanGetCookies(url_google_, GURL())); 57 58 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_)); 59 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_secure_)); 60 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_mail_)); 61 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_analytics_)); 62 EXPECT_EQ(net::OK, CanSetCookie(url_google_, GURL())); 63 } 64 65 TEST_F(StaticCookiePolicyTest, BlockThirdPartyCookiesTest) { 66 SetPolicyType(net::StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES); 67 68 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_)); 69 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_secure_)); 70 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_mail_)); 71 EXPECT_EQ(net::OK, CanGetCookies(url_google_, url_google_analytics_)); 72 EXPECT_EQ(net::OK, CanGetCookies(url_google_, GURL())); 73 74 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_)); 75 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_secure_)); 76 EXPECT_EQ(net::OK, CanSetCookie(url_google_, url_google_mail_)); 77 EXPECT_NE(net::OK, CanSetCookie(url_google_, url_google_analytics_)); 78 EXPECT_EQ(net::OK, CanSetCookie(url_google_, GURL())); 79 } 80 81 TEST_F(StaticCookiePolicyTest, BlockAllCookiesTest) { 82 SetPolicyType(net::StaticCookiePolicy::BLOCK_ALL_COOKIES); 83 84 EXPECT_NE(net::OK, CanGetCookies(url_google_, url_google_)); 85 EXPECT_NE(net::OK, CanGetCookies(url_google_, url_google_secure_)); 86 EXPECT_NE(net::OK, CanGetCookies(url_google_, url_google_mail_)); 87 EXPECT_NE(net::OK, CanGetCookies(url_google_, url_google_analytics_)); 88 EXPECT_NE(net::OK, CanGetCookies(url_google_, GURL())); 89 90 EXPECT_NE(net::OK, CanSetCookie(url_google_, url_google_)); 91 EXPECT_NE(net::OK, CanSetCookie(url_google_, url_google_secure_)); 92 EXPECT_NE(net::OK, CanSetCookie(url_google_, url_google_mail_)); 93 EXPECT_NE(net::OK, CanSetCookie(url_google_, url_google_analytics_)); 94 EXPECT_NE(net::OK, CanSetCookie(url_google_, GURL())); 95 } 96