Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / android_webview / browser / aw_cookie_access_policy_unittest.cc
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 "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"
11
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace content {
15 class ResourceContext;
16 }
17
18 namespace net {
19 class CookieOptions;
20 }
21
22 class GURL;
23
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;
31 using testing::Test;
32
33 class AwCookieAccessPolicyTest : public Test {
34  public:
35   static const GURL kUrlFirstParty;
36   static const GURL kUrlThirdParty;
37
38   AwCookieAccessPolicyTest()
39       : loop_(),
40         context_(),
41         url_request_delegate_(),
42         network_delegate_(),
43         first_party_request_() {}
44
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_,
50                                                   &context_));
51     first_party_request_->set_method("GET");
52
53     third_party_request_.reset(new TestURLRequest(kUrlThirdParty,
54                                                   net::DEFAULT_PRIORITY,
55                                                   &url_request_delegate_,
56                                                   &context_));
57     third_party_request_->set_method("GET");
58     third_party_request_->set_first_party_for_cookies(kUrlFirstParty);
59   }
60
61   AwCookieAccessPolicy* policy() {
62     return AwCookieAccessPolicy::GetInstance();
63   }
64
65   bool GetGlobalAllowAccess() {
66     return policy()->GetGlobalAllowAccess();
67   }
68
69   void SetGlobalAllowAccess(bool allow) {
70     policy()->SetGlobalAllowAccess(allow);
71   }
72
73   bool GetThirdPartyAllowAccess() {
74     return policy()->GetThirdPartyAllowAccess();
75   }
76
77   void SetThirdPartyAllowAccess(bool allow) {
78     policy()->SetThirdPartyAllowAccess(allow);
79   }
80
81   bool OnCanGetCookies(const net::URLRequest& request) {
82     return policy()->OnCanGetCookies(request, CookieList());
83   }
84
85   bool OnCanSetCookie(const net::URLRequest& request) {
86     return policy()->OnCanSetCookie(request, "", NULL);
87   }
88
89   bool AllowGetCookie(const GURL& url, const GURL& first_party) {
90     return policy()->AllowGetCookie(url, first_party, CookieList(), NULL, 0, 0);
91   }
92
93   bool AllowSetCookie(const GURL& url, const GURL& first_party) {
94     return policy()->AllowSetCookie(url, first_party, "", NULL, 0, 0, NULL);
95   }
96
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_));
102   }
103
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_));
109   }
110
111  protected:
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_;
118 };
119
120 const GURL AwCookieAccessPolicyTest::kUrlFirstParty =
121     GURL("http://first.example");
122 const GURL AwCookieAccessPolicyTest::kUrlThirdParty =
123     GURL("http://third.example");
124
125 TEST_F(AwCookieAccessPolicyTest, BlockAllCookies) {
126   SetGlobalAllowAccess(false);
127   SetThirdPartyAllowAccess(false);
128   expectFirstPartyAccess(false);
129   expectThirdPartyAccess(false);
130 }
131
132 TEST_F(AwCookieAccessPolicyTest, BlockAllCookiesWithThirdPartySet) {
133   SetGlobalAllowAccess(false);
134   SetThirdPartyAllowAccess(true);
135   expectFirstPartyAccess(false);
136   expectThirdPartyAccess(false);
137 }
138
139 TEST_F(AwCookieAccessPolicyTest, FirstPartyCookiesOnly) {
140   SetGlobalAllowAccess(true);
141   SetThirdPartyAllowAccess(false);
142   expectFirstPartyAccess(true);
143   expectThirdPartyAccess(false);
144 }
145
146 TEST_F(AwCookieAccessPolicyTest, AllowAllCookies) {
147   SetGlobalAllowAccess(true);
148   SetThirdPartyAllowAccess(true);
149   expectFirstPartyAccess(true);
150   expectThirdPartyAccess(true);
151 }