jccollet | baa356e | 2008-04-17 11:05:33 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6644726 |
| 27 | * @summary Cookie management issues |
| 28 | */ |
| 29 | |
| 30 | import java.net.*; |
| 31 | import java.util.*; |
| 32 | |
| 33 | public class B6644726 { |
| 34 | public static void main(String[] args) throws Exception { |
| 35 | testCookieStore(); |
| 36 | } |
| 37 | |
| 38 | private static void testCookieStore() throws Exception { |
| 39 | CookieManager cm = new CookieManager(); |
| 40 | CookieStore cs = cm.getCookieStore(); |
| 41 | URI uri = new URI("http://www.s1.sun.com/dir/foo/doc.html"); |
| 42 | URI suri = new URI("https://www.s1.sun.com/dir/foo/index.html"); |
| 43 | cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL); |
| 44 | |
| 45 | ArrayList<String> lst = new ArrayList<String>(); |
| 46 | // Let's test the default path |
| 47 | lst.add("myCookie1=foo"); |
| 48 | // Then some alternate expires format |
| 49 | lst.add("myCookie2=bar; path=/dir; expires=Tue, 19 Aug 2025 16:00:00 GMT"); |
| 50 | lst.add("myCookie3=test; path=/dir; expires=Tue Aug 19 2025 16:00:00 GMT-0100"); |
| 51 | // Then Netscape draft cookies and domains |
| 52 | lst.add("myCookie4=test; domain=.sun.com; path=/dir/foo"); |
| 53 | HashMap<String, List<String>> map = new HashMap<String, List<String>>(); |
| 54 | map.put("Set-Cookie", lst); |
| 55 | cm.put(uri, map); |
| 56 | map.clear(); |
| 57 | lst.clear(); |
| 58 | // Test for secure tag |
| 59 | lst.add("myCookie5=test; secure"); |
| 60 | // Test for passing cookies between http and https |
| 61 | map.put("Set-Cookie", lst); |
| 62 | cm.put(suri, map); |
| 63 | |
| 64 | List<HttpCookie> cookies = cs.getCookies(); |
| 65 | // There should be 5 cookies if all dates parsed correctly |
| 66 | if (cookies.size() != 5) { |
| 67 | fail("Should have 5 cookies. Got only "+ cookies.size() + ", expires probably didn't parse correctly"); |
| 68 | } |
| 69 | // Check Path for first Cookie |
| 70 | for (HttpCookie c : cookies) { |
| 71 | if (c.getName().equals("myCookie1")) { |
| 72 | if (!"/dir/foo/".equals(c.getPath())) { |
| 73 | fail("Default path for myCookie1 is " + c.getPath()); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | HashMap<String, List<String>> emptyMap = new HashMap<String, List<String>>(); |
| 79 | // We should get 1 Cookie: MyCookie4, because of the domain |
| 80 | Map<String, List<String>>m = cm.get(new URI("http://www.s2.sun.com/dir/foo/doc2.html"), |
| 81 | emptyMap); |
| 82 | List<String> clst = m.get("Cookie"); |
| 83 | if (clst.size() != 1) { |
| 84 | fail("We should have only 1 cookie, not " + clst.size()); |
| 85 | } else { |
| 86 | if (!clst.get(0).startsWith("myCookie4")) { |
| 87 | fail("The cookie should be myCookie4, not " + clst.get(0)); |
| 88 | } |
| 89 | } |
| 90 | // We should get 4 cookies for non secure URI, and 5 for the secure one |
| 91 | m = cm.get(suri, emptyMap); |
| 92 | clst = m.get("Cookie"); |
| 93 | if (clst.size() != 5) { |
| 94 | fail("Cookies didn't cross from http to https. Got only " + clst.size()); |
| 95 | } |
| 96 | |
| 97 | m = cm.get(uri, emptyMap); |
| 98 | clst = m.get("Cookie"); |
| 99 | if (clst.size() != 4) { |
| 100 | fail("We should have gotten only 4 cookies over http (non secure), got " + |
| 101 | clst.size()); |
| 102 | } |
| 103 | if (isIn(clst, "myCookie5=")) { |
| 104 | // myCookie5 (the secure one) shouldn't be here |
| 105 | fail("Got the secure cookie over a non secure link"); |
| 106 | } |
| 107 | |
| 108 | // Let's check that empty path is treated correctly |
| 109 | uri = new URI("http://www.sun.com/"); |
| 110 | lst.clear(); |
| 111 | lst.add("myCookie6=foo"); |
| 112 | map.clear(); |
| 113 | map.put("Set-Cookie", lst); |
| 114 | cm.put(uri, map); |
| 115 | uri = new URI("http://www.sun.com"); |
| 116 | m = cm.get(uri, emptyMap); |
| 117 | clst = m.get("Cookie"); |
| 118 | if (clst.size() != 1) { |
| 119 | fail("Missing a cookie when using an empty path"); |
| 120 | } |
| 121 | |
| 122 | // And now, the other way around: |
| 123 | |
| 124 | uri = new URI("http://www.sun.com"); |
| 125 | lst.clear(); |
| 126 | lst.add("myCookie7=foo"); |
| 127 | map.clear(); |
| 128 | map.put("Set-Cookie", lst); |
| 129 | cm.put(uri, map); |
| 130 | uri = new URI("http://www.sun.com/"); |
| 131 | m = cm.get(uri, emptyMap); |
| 132 | clst = m.get("Cookie"); |
| 133 | if (!isIn(clst, "myCookie7=")) { |
| 134 | fail("Missing a cookie when using an empty path"); |
| 135 | } |
| 136 | |
| 137 | // Let's make sure the 'Port' optional attributes is enforced |
| 138 | |
| 139 | lst.clear(); |
| 140 | lst.add("myCookie8=porttest; port"); |
| 141 | lst.add("myCookie9=porttest; port=\"80,8000\""); |
| 142 | lst.add("myCookie10=porttest; port=\"8000\""); |
| 143 | map.clear(); |
| 144 | map.put("Set-Cookie", lst); |
| 145 | uri = new URI("http://www.sun.com/"); |
| 146 | cm.put(uri, map); |
| 147 | |
| 148 | // myCookie10 should have been rejected |
| 149 | cookies = cs.getCookies(); |
| 150 | for (HttpCookie c : cookies) { |
| 151 | if (c.getName().equals("myCookie10")) { |
| 152 | fail("A cookie with an invalid port list was accepted"); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | uri = new URI("http://www.sun.com:80/"); |
| 157 | m = cm.get(uri, emptyMap); |
| 158 | clst = m.get("Cookie"); |
| 159 | // We should find both myCookie8 and myCookie9 but not myCookie10 |
| 160 | if (!isIn(clst, "myCookie8=") || !isIn(clst, "myCookie9=")) { |
| 161 | fail("Missing a cookie on port 80"); |
| 162 | } |
| 163 | uri = new URI("http://www.sun.com:8000/"); |
| 164 | m = cm.get(uri, emptyMap); |
| 165 | clst = m.get("Cookie"); |
| 166 | // We should find only myCookie9 |
| 167 | if (!isIn(clst, "myCookie9=")) { |
| 168 | fail("Missing a cookie on port 80"); |
| 169 | } |
| 170 | if (isIn(clst, "myCookie8=")) { |
| 171 | fail("A cookie with an invalid port list was returned"); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private static boolean isIn(List<String> lst, String cookie) { |
| 176 | if (lst == null || lst.isEmpty()) { |
| 177 | return false; |
| 178 | } |
| 179 | for (String s : lst) { |
| 180 | if (s.startsWith(cookie)) |
| 181 | return true; |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | private static void fail(String msg) throws Exception { |
| 187 | throw new RuntimeException(msg); |
| 188 | } |
| 189 | } |