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.test.AndroidTestCase; 20 import android.webkit.CookieManager; 21 import android.webkit.CookieSyncManager; 22 23 /** 24 * Original framework tests for CookieManager 25 */ 26 public class CookieTest extends AndroidTestCase { 27 28 private CookieManager mCookieManager; 29 private static final long WAIT_TIME = 50; 30 31 @Override 32 protected void setUp() throws Exception { 33 super.setUp(); 34 CookieSyncManager.createInstance(getContext()); 35 mCookieManager = CookieManager.getInstance(); 36 mCookieManager.removeAllCookie(); 37 // cookies are removed asynchronously, wait a bit for cookies to be removed 38 int maxWait = 10; 39 for (int i=0; i < maxWait; i++) { 40 // this is unfortuately non-deterministic, but ensure sleep a least once to limit 41 // chance of remove thread running after test has begun 42 Thread.sleep(WAIT_TIME); 43 if (!mCookieManager.hasCookies()) { 44 break; 45 } 46 } 47 assertFalse(mCookieManager.hasCookies()); 48 } 49 50 public void testDomain() { 51 String url = "http://www.foo.com"; 52 53 // basic 54 mCookieManager.setCookie(url, "a=b"); 55 String cookie = mCookieManager.getCookie(url); 56 assertTrue(cookie.equals("a=b")); 57 58 // no cross domain cookie 59 cookie = mCookieManager.getCookie("http://bar.com"); 60 assertTrue(cookie == null); 61 62 // more than one cookie 63 mCookieManager.setCookie(url, "c=d; domain=.foo.com"); 64 cookie = mCookieManager.getCookie(url); 65 assertTrue(cookie.contains("a=b;")); 66 assertTrue(cookie.contains("c=d")); 67 68 // host cookie should not be accessible from a sub-domain. 69 cookie = mCookieManager.getCookie("http://bar.www.foo.com"); 70 assertTrue(cookie.equals("c=d")); 71 72 // test setting a domain= that doesn't start w/ a dot, should 73 // treat it as a domain cookie, as if there was a pre-pended dot. 74 mCookieManager.setCookie(url, "e=f; domain=www.foo.com"); 75 cookie = mCookieManager.getCookie(url); 76 assertTrue(cookie.contains("a=b")); 77 assertTrue(cookie.contains("c=d")); 78 assertTrue(cookie.contains("e=f")); 79 80 cookie = mCookieManager.getCookie("http://sub.www.foo.com"); 81 assertTrue(cookie.contains("c=d")); 82 assertTrue(cookie.contains("e=f")); 83 84 cookie = mCookieManager.getCookie("http://foo.com"); 85 assertTrue(cookie.equals("c=d")); 86 } 87 88 public void testSubDomain() { 89 String url_abcd = "http://a.b.c.d.com"; 90 String url_bcd = "http://b.c.d.com"; 91 String url_cd = "http://c.d.com"; 92 String url_d = "http://d.com"; 93 94 mCookieManager.setCookie(url_abcd, "a=1; domain=.a.b.c.d.com"); 95 mCookieManager.setCookie(url_abcd, "b=2; domain=.b.c.d.com"); 96 mCookieManager.setCookie(url_abcd, "c=3; domain=.c.d.com"); 97 mCookieManager.setCookie(url_abcd, "d=4; domain=.d.com"); 98 99 String cookie = mCookieManager.getCookie(url_abcd); 100 assertTrue(cookie.contains("a=1")); 101 assertTrue(cookie.contains("b=2")); 102 assertTrue(cookie.contains("c=3")); 103 assertTrue(cookie.contains("d=4")); 104 cookie = mCookieManager.getCookie(url_bcd); 105 assertTrue(cookie.contains("b=2")); 106 assertTrue(cookie.contains("c=3")); 107 assertTrue(cookie.contains("d=4")); 108 cookie = mCookieManager.getCookie(url_cd); 109 assertTrue(cookie.contains("c=3")); 110 assertTrue(cookie.contains("d=4")); 111 cookie = mCookieManager.getCookie(url_d); 112 assertTrue(cookie.equals("d=4")); 113 114 // check that the same cookie can exist on different sub-domains. 115 mCookieManager.setCookie(url_bcd, "x=bcd; domain=.b.c.d.com"); 116 mCookieManager.setCookie(url_bcd, "x=cd; domain=.c.d.com"); 117 cookie = mCookieManager.getCookie(url_bcd); 118 assertTrue(cookie.contains("b=2")); 119 assertTrue(cookie.contains("c=3")); 120 assertTrue(cookie.contains("d=4")); 121 assertTrue(cookie.contains("x=bcd")); 122 assertTrue(cookie.contains("x=cd")); 123 cookie = mCookieManager.getCookie(url_cd); 124 assertTrue(cookie.contains("c=3")); 125 assertTrue(cookie.contains("d=4")); 126 assertTrue(cookie.contains("x=cd")); 127 } 128 129 public void testInvalidDomain() { 130 String url = "http://foo.bar.com"; 131 132 mCookieManager.setCookie(url, "a=1; domain=.yo.foo.bar.com"); 133 String cookie = mCookieManager.getCookie(url); 134 assertTrue(cookie == null); 135 136 mCookieManager.setCookie(url, "b=2; domain=.foo.com"); 137 cookie = mCookieManager.getCookie(url); 138 assertTrue(cookie == null); 139 140 mCookieManager.setCookie(url, "c=3; domain=.bar.foo.com"); 141 cookie = mCookieManager.getCookie(url); 142 assertTrue(cookie == null); 143 144 mCookieManager.setCookie(url, "d=4; domain=.foo.bar.com.net"); 145 cookie = mCookieManager.getCookie(url); 146 assertTrue(cookie == null); 147 148 mCookieManager.setCookie(url, "e=5; domain=.ar.com"); 149 cookie = mCookieManager.getCookie(url); 150 assertTrue(cookie == null); 151 152 mCookieManager.setCookie(url, "f=6; domain=.com"); 153 cookie = mCookieManager.getCookie(url); 154 assertTrue(cookie == null); 155 156 mCookieManager.setCookie(url, "g=7; domain=.co.uk"); 157 cookie = mCookieManager.getCookie(url); 158 assertTrue(cookie == null); 159 160 mCookieManager.setCookie(url, "h=8; domain=.foo.bar.com.com"); 161 cookie = mCookieManager.getCookie(url); 162 assertTrue(cookie == null); 163 } 164 165 public void testPath() { 166 String url = "http://www.foo.com"; 167 168 mCookieManager.setCookie(url, "a=b; path=/wee"); 169 String cookie = mCookieManager.getCookie(url + "/wee"); 170 assertTrue(cookie.equals("a=b")); 171 cookie = mCookieManager.getCookie(url + "/wee/"); 172 assertTrue(cookie.equals("a=b")); 173 cookie = mCookieManager.getCookie(url + "/wee/hee"); 174 assertTrue(cookie.equals("a=b")); 175 cookie = mCookieManager.getCookie(url + "/wee/hee/more"); 176 assertTrue(cookie.equals("a=b")); 177 cookie = mCookieManager.getCookie(url + "/weehee"); 178 assertTrue(cookie == null); 179 cookie = mCookieManager.getCookie(url); 180 assertTrue(cookie == null); 181 182 mCookieManager.setCookie(url, "a=c; path="); 183 cookie = mCookieManager.getCookie(url + "/wee"); 184 // order of contents matters in this case, per spec 185 assertTrue(cookie.equals("a=b; a=c")); 186 cookie = mCookieManager.getCookie(url); 187 assertTrue(cookie.equals("a=c")); 188 189 mCookieManager.setCookie(url, "a=d"); 190 cookie = mCookieManager.getCookie(url + "/wee"); 191 assertTrue(cookie.equals("a=b; a=d")); 192 } 193 194 public void testEmptyValue() { 195 String url = "http://www.foobar.com"; 196 197 mCookieManager.setCookie(url, "bar="); 198 String cookie = mCookieManager.getCookie(url); 199 assertTrue(cookie.equals("bar=")); 200 201 mCookieManager.setCookie(url, "foobar=;"); 202 cookie = mCookieManager.getCookie(url); 203 assertTrue(cookie.equals("bar=; foobar=")); 204 205 mCookieManager.setCookie(url, "baz=; path=/wee"); 206 cookie = mCookieManager.getCookie(url + "/wee"); 207 assertTrue(cookie.equals("baz=; bar=; foobar=")); 208 } 209 } 210