1 // Copyright 2014 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 "android_webview/browser/aw_cookie_access_policy.h" 6 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 class GURL; 10 11 using android_webview::AwStaticCookiePolicy; 12 using testing::Test; 13 14 class AwStaticCookiePolicyTest : public Test { 15 public: 16 static const GURL kUrlFirstParty; 17 static const GURL kUrlThirdParty; 18 19 AwStaticCookiePolicyTest() {} 20 21 void expectFirstPartyAccess(const AwStaticCookiePolicy& policy, 22 bool expectedResult) { 23 EXPECT_EQ(expectedResult, policy.AllowSet(kUrlFirstParty, kUrlFirstParty)); 24 EXPECT_EQ(expectedResult, policy.AllowGet(kUrlFirstParty, kUrlFirstParty)); 25 } 26 27 void expectThirdPartyAccess(const AwStaticCookiePolicy& policy, 28 bool expectedResult) { 29 EXPECT_EQ(expectedResult, policy.AllowSet(kUrlFirstParty, kUrlThirdParty)); 30 EXPECT_EQ(expectedResult, policy.AllowGet(kUrlFirstParty, kUrlThirdParty)); 31 } 32 }; 33 34 const GURL AwStaticCookiePolicyTest::kUrlFirstParty = 35 GURL("http://first.example"); 36 const GURL AwStaticCookiePolicyTest::kUrlThirdParty = 37 GURL("http://third.example"); 38 39 TEST_F(AwStaticCookiePolicyTest, BlockAllCookies) { 40 AwStaticCookiePolicy policy(false /* allow_cookies */, 41 false /* allow_third_party_cookies */); 42 expectFirstPartyAccess(policy, false); 43 expectThirdPartyAccess(policy, false); 44 } 45 46 TEST_F(AwStaticCookiePolicyTest, BlockAllCookiesWithThirdPartySet) { 47 AwStaticCookiePolicy policy(false /* allow_cookies */, 48 true /* allow_third_party_cookies */); 49 expectFirstPartyAccess(policy, false); 50 expectThirdPartyAccess(policy, false); 51 } 52 53 TEST_F(AwStaticCookiePolicyTest, FirstPartyCookiesOnly) { 54 AwStaticCookiePolicy policy(true /* allow_cookies */, 55 false /* allow_third_party_cookies */); 56 expectFirstPartyAccess(policy, true); 57 expectThirdPartyAccess(policy, false); 58 } 59 60 TEST_F(AwStaticCookiePolicyTest, AllowAllCookies) { 61 AwStaticCookiePolicy policy(true /* allow_cookies */, 62 true /* allow_third_party_cookies */); 63 expectFirstPartyAccess(policy, true); 64 expectThirdPartyAccess(policy, true); 65 } 66 67