1 /* 2 * Copyright (C) 2008 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; 18 19 import android.content.Context; 20 import android.test.AndroidTestCase; 21 import android.util.Log; 22 import android.webkit.CookieManager; 23 import android.webkit.CookieSyncManager; 24 25 public class CookieTest extends AndroidTestCase { 26 27 /** 28 * To run these tests: $ mmm frameworks/base/tests/CoreTests/android && adb 29 * remount && adb sync $ adb shell am instrument -w \ -e class 30 * android.webkit.CookieTest \ 31 * android.core/android.test.InstrumentationTestRunner 32 */ 33 34 private CookieManager mCookieManager; 35 36 @Override setContext(Context context)37 public void setContext(Context context) { 38 assertTrue(mContext == null); 39 super.setContext(context); 40 CookieSyncManager.createInstance(context); 41 mCookieManager = CookieManager.getInstance(); 42 mCookieManager.removeAllCookie(); 43 } 44 testParse()45 public void testParse() { 46 mCookieManager.removeAllCookie(); 47 String url = "http://www.foo.com"; 48 49 // basic 50 mCookieManager.setCookie(url, "a=b"); 51 String cookie = mCookieManager.getCookie(url); 52 assertTrue(cookie.equals("a=b")); 53 54 // quoted 55 mCookieManager.setCookie(url, "c=\"d;\""); 56 cookie = mCookieManager.getCookie(url); 57 assertTrue(cookie.equals("a=b; c=\"d;\"")); 58 59 // empty 60 mCookieManager.setCookie(url, "; path=/"); 61 cookie = mCookieManager.getCookie(url); 62 assertTrue(cookie.equals("a=b; c=\"d;\"")); 63 } 64 testDomain()65 public void testDomain() { 66 mCookieManager.removeAllCookie(); 67 String url = "http://www.foo.com"; 68 69 // basic 70 mCookieManager.setCookie(url, "a=b"); 71 String cookie = mCookieManager.getCookie(url); 72 assertTrue(cookie.equals("a=b")); 73 74 // no cross domain cookie 75 cookie = mCookieManager.getCookie("http://bar.com"); 76 assertTrue(cookie == null); 77 78 // more than one cookie 79 mCookieManager.setCookie(url, "c=d; domain=.foo.com"); 80 cookie = mCookieManager.getCookie(url); 81 assertTrue(cookie.equals("a=b; c=d")); 82 83 // host cookie should not be accessible from a sub-domain. 84 cookie = mCookieManager.getCookie("http://bar.www.foo.com"); 85 assertTrue(cookie.equals("c=d")); 86 87 // test setting a domain= that doesn't start w/ a dot, should 88 // treat it as a domain cookie, as if there was a pre-pended dot. 89 mCookieManager.setCookie(url, "e=f; domain=www.foo.com"); 90 cookie = mCookieManager.getCookie(url); 91 assertTrue(cookie.equals("a=b; c=d; e=f")); 92 cookie = mCookieManager.getCookie("http://sub.www.foo.com"); 93 assertTrue(cookie.equals("c=d; e=f")); 94 cookie = mCookieManager.getCookie("http://foo.com"); 95 assertTrue(cookie.equals("c=d")); 96 } 97 testSubDomain()98 public void testSubDomain() { 99 mCookieManager.removeAllCookie(); 100 String url_abcd = "http://a.b.c.d.com"; 101 String url_bcd = "http://b.c.d.com"; 102 String url_cd = "http://c.d.com"; 103 String url_d = "http://d.com"; 104 105 mCookieManager.setCookie(url_abcd, "a=1; domain=.a.b.c.d.com"); 106 mCookieManager.setCookie(url_abcd, "b=2; domain=.b.c.d.com"); 107 mCookieManager.setCookie(url_abcd, "c=3; domain=.c.d.com"); 108 mCookieManager.setCookie(url_abcd, "d=4; domain=.d.com"); 109 110 String cookie = mCookieManager.getCookie(url_abcd); 111 assertTrue(cookie.equals("a=1; b=2; c=3; d=4")); 112 cookie = mCookieManager.getCookie(url_bcd); 113 assertTrue(cookie.equals("b=2; c=3; d=4")); 114 cookie = mCookieManager.getCookie(url_cd); 115 assertTrue(cookie.equals("c=3; d=4")); 116 cookie = mCookieManager.getCookie(url_d); 117 assertTrue(cookie.equals("d=4")); 118 119 // check that the same cookie can exist on different sub-domains. 120 mCookieManager.setCookie(url_bcd, "x=bcd; domain=.b.c.d.com"); 121 mCookieManager.setCookie(url_bcd, "x=cd; domain=.c.d.com"); 122 cookie = mCookieManager.getCookie(url_bcd); 123 assertTrue(cookie.equals("b=2; c=3; d=4; x=bcd; x=cd")); 124 cookie = mCookieManager.getCookie(url_cd); 125 assertTrue(cookie.equals("c=3; d=4; x=cd")); 126 } 127 testInvalidDomain()128 public void testInvalidDomain() { 129 mCookieManager.removeAllCookie(); 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 testPath()165 public void testPath() { 166 mCookieManager.removeAllCookie(); 167 String url = "http://www.foo.com"; 168 169 mCookieManager.setCookie(url, "a=b; path=/wee"); 170 String cookie = mCookieManager.getCookie(url + "/wee"); 171 assertTrue(cookie.equals("a=b")); 172 cookie = mCookieManager.getCookie(url + "/wee/"); 173 assertTrue(cookie.equals("a=b")); 174 cookie = mCookieManager.getCookie(url + "/wee/hee"); 175 assertTrue(cookie.equals("a=b")); 176 cookie = mCookieManager.getCookie(url + "/wee/hee/more"); 177 assertTrue(cookie.equals("a=b")); 178 cookie = mCookieManager.getCookie(url + "/weehee"); 179 assertTrue(cookie == null); 180 cookie = mCookieManager.getCookie(url); 181 assertTrue(cookie == null); 182 183 mCookieManager.setCookie(url, "a=c; path="); 184 cookie = mCookieManager.getCookie(url + "/wee"); 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