1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.webkit.cts; 18 19 import android.cts.util.NullWebViewUtils; 20 import android.test.ActivityInstrumentationTestCase2; 21 import android.webkit.CookieManager; 22 import android.webkit.CookieSyncManager; 23 24 /** 25 * Original framework tests for CookieManager 26 */ 27 public class CookieTest extends ActivityInstrumentationTestCase2<CookieSyncManagerCtsActivity> { 28 29 private CookieManager mCookieManager; 30 private static final long WAIT_TIME = 50; 31 32 public CookieTest() { 33 super("com.android.cts.webkit", CookieSyncManagerCtsActivity.class); 34 } 35 36 @Override 37 protected void setUp() throws Exception { 38 super.setUp(); 39 40 if (getActivity().getWebView() == null) { 41 return; 42 } 43 44 mCookieManager = CookieManager.getInstance(); 45 mCookieManager.removeAllCookie(); 46 // cookies are removed asynchronously, wait a bit for cookies to be removed 47 int maxWait = 10; 48 for (int i=0; i < maxWait; i++) { 49 // this is unfortuately non-deterministic, but ensure sleep a least once to limit 50 // chance of remove thread running after test has begun 51 Thread.sleep(WAIT_TIME); 52 if (!mCookieManager.hasCookies()) { 53 break; 54 } 55 } 56 assertFalse(mCookieManager.hasCookies()); 57 } 58 59 public void testDomain() { 60 if (!NullWebViewUtils.isWebViewAvailable()) { 61 return; 62 } 63 String url = "http://www.foo.com"; 64 65 // basic 66 mCookieManager.setCookie(url, "a=b"); 67 String cookie = mCookieManager.getCookie(url); 68 assertTrue(cookie.equals("a=b")); 69 70 // no cross domain cookie 71 cookie = mCookieManager.getCookie("http://bar.com"); 72 assertTrue(cookie == null); 73 74 // more than one cookie 75 mCookieManager.setCookie(url, "c=d; domain=.foo.com"); 76 cookie = mCookieManager.getCookie(url); 77 assertTrue(cookie.contains("a=b;")); 78 assertTrue(cookie.contains("c=d")); 79 80 // host cookie should not be accessible from a sub-domain. 81 cookie = mCookieManager.getCookie("http://bar.www.foo.com"); 82 assertTrue(cookie.equals("c=d")); 83 84 // test setting a domain= that doesn't start w/ a dot, should 85 // treat it as a domain cookie, as if there was a pre-pended dot. 86 mCookieManager.setCookie(url, "e=f; domain=www.foo.com"); 87 cookie = mCookieManager.getCookie(url); 88 assertTrue(cookie.contains("a=b")); 89 assertTrue(cookie.contains("c=d")); 90 assertTrue(cookie.contains("e=f")); 91 92 cookie = mCookieManager.getCookie("http://sub.www.foo.com"); 93 assertTrue(cookie.contains("c=d")); 94 assertTrue(cookie.contains("e=f")); 95 96 cookie = mCookieManager.getCookie("http://foo.com"); 97 assertTrue(cookie.equals("c=d")); 98 } 99 100 public void testSubDomain() { 101 if (!NullWebViewUtils.isWebViewAvailable()) { 102 return; 103 } 104 String url_abcd = "http://a.b.c.d.com"; 105 String url_bcd = "http://b.c.d.com"; 106 String url_cd = "http://c.d.com"; 107 String url_d = "http://d.com"; 108 109 mCookieManager.setCookie(url_abcd, "a=1; domain=.a.b.c.d.com"); 110 mCookieManager.setCookie(url_abcd, "b=2; domain=.b.c.d.com"); 111 mCookieManager.setCookie(url_abcd, "c=3; domain=.c.d.com"); 112 mCookieManager.setCookie(url_abcd, "d=4; domain=.d.com"); 113 114 String cookie = mCookieManager.getCookie(url_abcd); 115 assertTrue(cookie.contains("a=1")); 116 assertTrue(cookie.contains("b=2")); 117 assertTrue(cookie.contains("c=3")); 118 assertTrue(cookie.contains("d=4")); 119 cookie = mCookieManager.getCookie(url_bcd); 120 assertTrue(cookie.contains("b=2")); 121 assertTrue(cookie.contains("c=3")); 122 assertTrue(cookie.contains("d=4")); 123 cookie = mCookieManager.getCookie(url_cd); 124 assertTrue(cookie.contains("c=3")); 125 assertTrue(cookie.contains("d=4")); 126 cookie = mCookieManager.getCookie(url_d); 127 assertTrue(cookie.equals("d=4")); 128 129 // check that the same cookie can exist on different sub-domains. 130 mCookieManager.setCookie(url_bcd, "x=bcd; domain=.b.c.d.com"); 131 mCookieManager.setCookie(url_bcd, "x=cd; domain=.c.d.com"); 132 cookie = mCookieManager.getCookie(url_bcd); 133 assertTrue(cookie.contains("b=2")); 134 assertTrue(cookie.contains("c=3")); 135 assertTrue(cookie.contains("d=4")); 136 assertTrue(cookie.contains("x=bcd")); 137 assertTrue(cookie.contains("x=cd")); 138 cookie = mCookieManager.getCookie(url_cd); 139 assertTrue(cookie.contains("c=3")); 140 assertTrue(cookie.contains("d=4")); 141 assertTrue(cookie.contains("x=cd")); 142 } 143 144 public void testInvalidDomain() { 145 if (!NullWebViewUtils.isWebViewAvailable()) { 146 return; 147 } 148 String url = "http://foo.bar.com"; 149 150 mCookieManager.setCookie(url, "a=1; domain=.yo.foo.bar.com"); 151 String cookie = mCookieManager.getCookie(url); 152 assertTrue(cookie == null); 153 154 mCookieManager.setCookie(url, "b=2; domain=.foo.com"); 155 cookie = mCookieManager.getCookie(url); 156 assertTrue(cookie == null); 157 158 mCookieManager.setCookie(url, "c=3; domain=.bar.foo.com"); 159 cookie = mCookieManager.getCookie(url); 160 assertTrue(cookie == null); 161 162 mCookieManager.setCookie(url, "d=4; domain=.foo.bar.com.net"); 163 cookie = mCookieManager.getCookie(url); 164 assertTrue(cookie == null); 165 166 mCookieManager.setCookie(url, "e=5; domain=.ar.com"); 167 cookie = mCookieManager.getCookie(url); 168 assertTrue(cookie == null); 169 170 mCookieManager.setCookie(url, "f=6; domain=.com"); 171 cookie = mCookieManager.getCookie(url); 172 assertTrue(cookie == null); 173 174 mCookieManager.setCookie(url, "g=7; domain=.co.uk"); 175 cookie = mCookieManager.getCookie(url); 176 assertTrue(cookie == null); 177 178 mCookieManager.setCookie(url, "h=8; domain=.foo.bar.com.com"); 179 cookie = mCookieManager.getCookie(url); 180 assertTrue(cookie == null); 181 } 182 183 public void testPath() { 184 if (!NullWebViewUtils.isWebViewAvailable()) { 185 return; 186 } 187 String url = "http://www.foo.com"; 188 189 mCookieManager.setCookie(url, "a=b; path=/wee"); 190 String cookie = mCookieManager.getCookie(url + "/wee"); 191 assertTrue(cookie.equals("a=b")); 192 cookie = mCookieManager.getCookie(url + "/wee/"); 193 assertTrue(cookie.equals("a=b")); 194 cookie = mCookieManager.getCookie(url + "/wee/hee"); 195 assertTrue(cookie.equals("a=b")); 196 cookie = mCookieManager.getCookie(url + "/wee/hee/more"); 197 assertTrue(cookie.equals("a=b")); 198 cookie = mCookieManager.getCookie(url + "/weehee"); 199 assertTrue(cookie == null); 200 cookie = mCookieManager.getCookie(url); 201 assertTrue(cookie == null); 202 203 mCookieManager.setCookie(url, "a=c; path="); 204 cookie = mCookieManager.getCookie(url + "/wee"); 205 // order of contents matters in this case, per spec 206 assertTrue(cookie.equals("a=b; a=c")); 207 cookie = mCookieManager.getCookie(url); 208 assertTrue(cookie.equals("a=c")); 209 210 mCookieManager.setCookie(url, "a=d"); 211 cookie = mCookieManager.getCookie(url + "/wee"); 212 assertTrue(cookie.equals("a=b; a=d")); 213 } 214 215 public void testEmptyValue() { 216 if (!NullWebViewUtils.isWebViewAvailable()) { 217 return; 218 } 219 String url = "http://www.foobar.com"; 220 221 mCookieManager.setCookie(url, "bar="); 222 String cookie = mCookieManager.getCookie(url); 223 assertTrue(cookie.equals("bar=")); 224 225 mCookieManager.setCookie(url, "foobar=;"); 226 cookie = mCookieManager.getCookie(url); 227 assertTrue(cookie.equals("bar=; foobar=")); 228 229 mCookieManager.setCookie(url, "baz=; path=/wee"); 230 cookie = mCookieManager.getCookie(url + "/wee"); 231 assertTrue(cookie.equals("baz=; bar=; foobar=")); 232 } 233 } 234