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.
5 #include "android_webview/browser/aw_cookie_access_policy.h"
7 #include "base/run_loop.h"
8 #include "net/base/request_priority.h"
9 #include "net/cookies/canonical_cookie.h"
10 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 class ResourceContext;
24 using android_webview::AwCookieAccessPolicy;
25 using net::CookieList;
26 using net::TestDelegate;
27 using net::TestJobInterceptor;
28 using net::TestNetworkDelegate;
29 using net::TestURLRequestContext;
30 using net::TestURLRequest;
33 class AwCookieAccessPolicyTest : public Test {
35 static const GURL kUrlFirstParty;
36 static const GURL kUrlThirdParty;
38 AwCookieAccessPolicyTest()
41 url_request_delegate_(),
43 first_party_request_() {}
45 virtual void SetUp() {
46 context_.set_network_delegate(&network_delegate_);
47 first_party_request_.reset(new TestURLRequest(kUrlFirstParty,
48 net::DEFAULT_PRIORITY,
49 &url_request_delegate_,
51 first_party_request_->set_method("GET");
53 third_party_request_.reset(new TestURLRequest(kUrlThirdParty,
54 net::DEFAULT_PRIORITY,
55 &url_request_delegate_,
57 third_party_request_->set_method("GET");
58 third_party_request_->set_first_party_for_cookies(kUrlFirstParty);
61 AwCookieAccessPolicy* policy() {
62 return AwCookieAccessPolicy::GetInstance();
65 bool GetGlobalAllowAccess() {
66 return policy()->GetGlobalAllowAccess();
69 void SetGlobalAllowAccess(bool allow) {
70 policy()->SetGlobalAllowAccess(allow);
73 bool GetThirdPartyAllowAccess() {
74 return policy()->GetThirdPartyAllowAccess();
77 void SetThirdPartyAllowAccess(bool allow) {
78 policy()->SetThirdPartyAllowAccess(allow);
81 bool OnCanGetCookies(const net::URLRequest& request) {
82 return policy()->OnCanGetCookies(request, CookieList());
85 bool OnCanSetCookie(const net::URLRequest& request) {
86 return policy()->OnCanSetCookie(request, "", NULL);
89 bool AllowGetCookie(const GURL& url, const GURL& first_party) {
90 return policy()->AllowGetCookie(url, first_party, CookieList(), NULL, 0, 0);
93 bool AllowSetCookie(const GURL& url, const GURL& first_party) {
94 return policy()->AllowSetCookie(url, first_party, "", NULL, 0, 0, NULL);
97 void expectFirstPartyAccess(bool expectedResult) {
98 EXPECT_EQ(expectedResult, AllowSetCookie(kUrlFirstParty, kUrlFirstParty));
99 EXPECT_EQ(expectedResult, AllowGetCookie(kUrlFirstParty, kUrlFirstParty));
100 EXPECT_EQ(expectedResult, OnCanGetCookies(*first_party_request_));
101 EXPECT_EQ(expectedResult, OnCanSetCookie(*first_party_request_));
104 void expectThirdPartyAccess(bool expectedResult) {
105 EXPECT_EQ(expectedResult, AllowSetCookie(kUrlFirstParty, kUrlThirdParty));
106 EXPECT_EQ(expectedResult, AllowGetCookie(kUrlFirstParty, kUrlThirdParty));
107 EXPECT_EQ(expectedResult, OnCanGetCookies(*third_party_request_));
108 EXPECT_EQ(expectedResult, OnCanSetCookie(*third_party_request_));
112 base::MessageLoopForIO loop_;
113 TestURLRequestContext context_;
114 TestDelegate url_request_delegate_;
115 TestNetworkDelegate network_delegate_;
116 scoped_ptr<TestURLRequest> first_party_request_;
117 scoped_ptr<TestURLRequest> third_party_request_;
120 const GURL AwCookieAccessPolicyTest::kUrlFirstParty =
121 GURL("http://first.example");
122 const GURL AwCookieAccessPolicyTest::kUrlThirdParty =
123 GURL("http://third.example");
125 TEST_F(AwCookieAccessPolicyTest, BlockAllCookies) {
126 SetGlobalAllowAccess(false);
127 SetThirdPartyAllowAccess(false);
128 expectFirstPartyAccess(false);
129 expectThirdPartyAccess(false);
132 TEST_F(AwCookieAccessPolicyTest, BlockAllCookiesWithThirdPartySet) {
133 SetGlobalAllowAccess(false);
134 SetThirdPartyAllowAccess(true);
135 expectFirstPartyAccess(false);
136 expectThirdPartyAccess(false);
139 TEST_F(AwCookieAccessPolicyTest, FirstPartyCookiesOnly) {
140 SetGlobalAllowAccess(true);
141 SetThirdPartyAllowAccess(false);
142 expectFirstPartyAccess(true);
143 expectThirdPartyAccess(false);
146 TEST_F(AwCookieAccessPolicyTest, AllowAllCookies) {
147 SetGlobalAllowAccess(true);
148 SetThirdPartyAllowAccess(true);
149 expectFirstPartyAccess(true);
150 expectThirdPartyAccess(true);