| 1 | // Copyright (c) 2012 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 | package org.chromium.android_webview.test; |
| 6 | |
| 7 | import android.test.MoreAsserts; |
| 8 | import android.test.suitebuilder.annotation.MediumTest; |
| 9 | import android.test.suitebuilder.annotation.SmallTest; |
| 10 | import android.util.Pair; |
| 11 | |
| 12 | import org.chromium.android_webview.AwContents; |
| 13 | import org.chromium.android_webview.AwCookieManager; |
| 14 | import org.chromium.android_webview.test.util.JSUtils; |
| 15 | import org.chromium.base.test.util.Feature; |
| 16 | import org.chromium.content.browser.test.util.Criteria; |
| 17 | import org.chromium.content.browser.test.util.CriteriaHelper; |
| 18 | import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper; |
| 19 | import org.chromium.net.test.util.TestWebServer; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Arrays; |
| 23 | import java.util.Date; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.List; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.atomic.AtomicInteger; |
| 28 | |
| 29 | /** |
| 30 | * Tests for the CookieManager. |
| 31 | */ |
| 32 | public class CookieManagerTest extends AwTestBase { |
| 33 | |
| 34 | private AwCookieManager mCookieManager; |
| 35 | private TestAwContentsClient mContentsClient; |
| 36 | private AwContents mAwContents; |
| 37 | |
| 38 | @Override |
| 39 | protected void setUp() throws Exception { |
| 40 | super.setUp(); |
| 41 | |
| 42 | mCookieManager = new AwCookieManager(); |
| 43 | mContentsClient = new TestAwContentsClient(); |
| 44 | final AwTestContainerView testContainerView = |
| 45 | createAwTestContainerViewOnMainSync(mContentsClient); |
| 46 | mAwContents = testContainerView.getAwContents(); |
| 47 | mAwContents.getSettings().setJavaScriptEnabled(true); |
| 48 | assertNotNull(mCookieManager); |
| 49 | } |
| 50 | |
| 51 | @SmallTest |
| 52 | @Feature({"AndroidWebView", "Privacy"}) |
| 53 | public void testAllowFileSchemeCookies() throws Throwable { |
| 54 | assertFalse(mCookieManager.allowFileSchemeCookies()); |
| 55 | mCookieManager.setAcceptFileSchemeCookies(true); |
| 56 | assertTrue(mCookieManager.allowFileSchemeCookies()); |
| 57 | mCookieManager.setAcceptFileSchemeCookies(false); |
| 58 | assertFalse(mCookieManager.allowFileSchemeCookies()); |
| 59 | } |
| 60 | |
| 61 | @MediumTest |
| 62 | @Feature({"AndroidWebView", "Privacy"}) |
| 63 | public void testAcceptCookie() throws Throwable { |
| 64 | TestWebServer webServer = null; |
| 65 | try { |
| 66 | webServer = new TestWebServer(false); |
| 67 | String path = "/cookie_test.html"; |
| 68 | String responseStr = |
| 69 | "<html><head><title>TEST!</title></head><body>HELLO!</body></html>"; |
| 70 | String url = webServer.setResponse(path, responseStr, null); |
| 71 | |
| 72 | mCookieManager.setAcceptCookie(false); |
| 73 | mCookieManager.removeAllCookie(); |
| 74 | assertFalse(mCookieManager.acceptCookie()); |
| 75 | assertFalse(mCookieManager.hasCookies()); |
| 76 | |
| 77 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); |
| 78 | setCookie("test1", "value1"); |
| 79 | assertNull(mCookieManager.getCookie(url)); |
| 80 | |
| 81 | List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>(); |
| 82 | responseHeaders.add( |
| 83 | Pair.create("Set-Cookie", "header-test1=header-value1; path=" + path)); |
| 84 | url = webServer.setResponse(path, responseStr, responseHeaders); |
| 85 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); |
| 86 | assertNull(mCookieManager.getCookie(url)); |
| 87 | |
| 88 | mCookieManager.setAcceptCookie(true); |
| 89 | assertTrue(mCookieManager.acceptCookie()); |
| 90 | |
| 91 | url = webServer.setResponse(path, responseStr, null); |
| 92 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); |
| 93 | setCookie("test2", "value2"); |
| 94 | waitForCookie(url); |
| 95 | String cookie = mCookieManager.getCookie(url); |
| 96 | assertNotNull(cookie); |
| 97 | validateCookies(cookie, "test2"); |
| 98 | |
| 99 | responseHeaders = new ArrayList<Pair<String, String>>(); |
| 100 | responseHeaders.add( |
| 101 | Pair.create("Set-Cookie", "header-test2=header-value2 path=" + path)); |
| 102 | url = webServer.setResponse(path, responseStr, responseHeaders); |
| 103 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); |
| 104 | waitForCookie(url); |
| 105 | cookie = mCookieManager.getCookie(url); |
| 106 | assertNotNull(cookie); |
| 107 | validateCookies(cookie, "test2", "header-test2"); |
| 108 | |
| 109 | // clean up all cookies |
| 110 | mCookieManager.removeAllCookie(); |
| 111 | } finally { |
| 112 | if (webServer != null) webServer.shutdown(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private void setCookie(final String name, final String value) |
| 117 | throws Throwable { |
| 118 | JSUtils.executeJavaScriptAndWaitForResult( |
| 119 | this, mAwContents, |
| 120 | mContentsClient.getOnEvaluateJavaScriptResultHelper(), |
| 121 | "var expirationDate = new Date();" + |
| 122 | "expirationDate.setDate(expirationDate.getDate() + 5);" + |
| 123 | "document.cookie='" + name + "=" + value + |
| 124 | "; expires=' + expirationDate.toUTCString();"); |
| 125 | } |
| 126 | |
| 127 | private void waitForCookie(final String url) throws InterruptedException { |
| 128 | assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 129 | @Override |
| 130 | public boolean isSatisfied() { |
| 131 | return mCookieManager.getCookie(url) != null; |
| 132 | } |
| 133 | })); |
| 134 | } |
| 135 | |
| 136 | private void validateCookies(String responseCookie, String... expectedCookieNames) { |
| 137 | String[] cookies = responseCookie.split(";"); |
| 138 | Set<String> foundCookieNames = new HashSet<String>(); |
| 139 | for (String cookie : cookies) { |
| 140 | foundCookieNames.add(cookie.substring(0, cookie.indexOf("=")).trim()); |
| 141 | } |
| 142 | MoreAsserts.assertEquals( |
| 143 | foundCookieNames, new HashSet<String>(Arrays.asList(expectedCookieNames))); |
| 144 | } |
| 145 | |
| 146 | @MediumTest |
| 147 | @Feature({"AndroidWebView", "Privacy"}) |
| 148 | public void testRemoveAllCookie() throws InterruptedException { |
| 149 | // enable cookie |
| 150 | mCookieManager.setAcceptCookie(true); |
| 151 | assertTrue(mCookieManager.acceptCookie()); |
| 152 | |
| 153 | // first there should be no cookie stored |
| 154 | mCookieManager.removeAllCookie(); |
| 155 | mCookieManager.flushCookieStore(); |
| 156 | assertFalse(mCookieManager.hasCookies()); |
| 157 | |
| 158 | String url = "http://www.example.com"; |
| 159 | String cookie = "name=test"; |
| 160 | mCookieManager.setCookie(url, cookie); |
| 161 | assertEquals(cookie, mCookieManager.getCookie(url)); |
| 162 | |
| 163 | assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 164 | @Override |
| 165 | public boolean isSatisfied() { |
| 166 | return mCookieManager.hasCookies(); |
| 167 | } |
| 168 | })); |
| 169 | |
| 170 | // clean up all cookies |
| 171 | mCookieManager.removeAllCookie(); |
| 172 | assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 173 | @Override |
| 174 | public boolean isSatisfied() { |
| 175 | return !mCookieManager.hasCookies(); |
| 176 | } |
| 177 | })); |
| 178 | } |
| 179 | |
| 180 | @MediumTest |
| 181 | @Feature({"AndroidWebView", "Privacy"}) |
| 182 | @SuppressWarnings("deprecation") |
| 183 | public void testCookieExpiration() throws InterruptedException { |
| 184 | // enable cookie |
| 185 | mCookieManager.setAcceptCookie(true); |
| 186 | assertTrue(mCookieManager.acceptCookie()); |
| 187 | mCookieManager.removeAllCookie(); |
| 188 | assertFalse(mCookieManager.hasCookies()); |
| 189 | |
| 190 | final String url = "http://www.example.com"; |
| 191 | final String cookie1 = "cookie1=peter"; |
| 192 | final String cookie2 = "cookie2=sue"; |
| 193 | final String cookie3 = "cookie3=marc"; |
| 194 | |
| 195 | mCookieManager.setCookie(url, cookie1); // session cookie |
| 196 | |
| 197 | Date date = new Date(); |
| 198 | date.setTime(date.getTime() + 1000 * 600); |
| 199 | String value2 = cookie2 + "; expires=" + date.toGMTString(); |
| 200 | mCookieManager.setCookie(url, value2); // expires in 10min |
| 201 | |
| 202 | long expiration = 3000; |
| 203 | date = new Date(); |
| 204 | date.setTime(date.getTime() + expiration); |
| 205 | String value3 = cookie3 + "; expires=" + date.toGMTString(); |
| 206 | mCookieManager.setCookie(url, value3); // expires in 3s |
| 207 | |
| 208 | String allCookies = mCookieManager.getCookie(url); |
| 209 | assertTrue(allCookies.contains(cookie1)); |
| 210 | assertTrue(allCookies.contains(cookie2)); |
| 211 | assertTrue(allCookies.contains(cookie3)); |
| 212 | |
| 213 | mCookieManager.removeSessionCookie(); |
| 214 | assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 215 | @Override |
| 216 | public boolean isSatisfied() { |
| 217 | String c = mCookieManager.getCookie(url); |
| 218 | return !c.contains(cookie1) && c.contains(cookie2) && c.contains(cookie3); |
| 219 | } |
| 220 | })); |
| 221 | |
| 222 | Thread.sleep(expiration + 1000); // wait for cookie to expire |
| 223 | mCookieManager.removeExpiredCookie(); |
| 224 | assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 225 | @Override |
| 226 | public boolean isSatisfied() { |
| 227 | String c = mCookieManager.getCookie(url); |
| 228 | return !c.contains(cookie1) && c.contains(cookie2) && !c.contains(cookie3); |
| 229 | } |
| 230 | })); |
| 231 | |
| 232 | mCookieManager.removeAllCookie(); |
| 233 | assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 234 | @Override |
| 235 | public boolean isSatisfied() { |
| 236 | return mCookieManager.getCookie(url) == null; |
| 237 | } |
| 238 | })); |
| 239 | } |
| 240 | } |