Path: blob/master/test/jdk/java/net/CookieHandler/B6644726.java
41149 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6644726 687354326* @summary Cookie management issues27*/2829import java.net.*;30import java.util.*;3132public class B6644726 {33public static void main(String[] args) throws Exception {34testCookieStore();35}3637private static void testCookieStore() throws Exception {38CookieManager cm = new CookieManager();39CookieStore cs = cm.getCookieStore();40URI uri = new URI("http://www.s1.sun.com/dir/foo/doc.html");41URI suri = new URI("https://www.s1.sun.com/dir/foo/index.html");42cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);4344ArrayList<String> lst = new ArrayList<String>();45// Let's test the default path46lst.add("myCookie1=foo");47// Then some alternate expires format48lst.add("myCookie2=bar; path=/dir; expires=Tue, 19 Aug 2025 16:00:00 GMT");49lst.add("myCookie3=test; path=/dir; expires=Tue Aug 19 2025 16:00:00 GMT-0100");50// Then Netscape draft cookies and domains51lst.add("myCookie4=test; domain=.sun.com; path=/dir/foo");52HashMap<String, List<String>> map = new HashMap<String, List<String>>();53map.put("Set-Cookie", lst);54cm.put(uri, map);55map.clear();56lst.clear();57// Test for secure tag58lst.add("myCookie5=test; secure");59// Test for passing cookies between http and https60map.put("Set-Cookie", lst);61cm.put(suri, map);6263List<HttpCookie> cookies = cs.getCookies();64// There should be 5 cookies if all dates parsed correctly65if (cookies.size() != 5) {66fail("Should have 5 cookies. Got only "+ cookies.size() + ", expires probably didn't parse correctly");67}68// Check Path for first Cookie69for (HttpCookie c : cookies) {70if (c.getName().equals("myCookie1")) {71if (!"/dir/foo/".equals(c.getPath())) {72fail("Default path for myCookie1 is " + c.getPath());73}74}75}7677HashMap<String, List<String>> emptyMap = new HashMap<String, List<String>>();78// We should get 1 Cookie: MyCookie4, because of the domain79Map<String, List<String>>m = cm.get(new URI("http://www.s2.sun.com/dir/foo/doc2.html"),80emptyMap);81List<String> clst = m.get("Cookie");82if (clst.size() != 1) {83fail("We should have only 1 cookie, not " + clst.size());84} else {85if (!clst.get(0).startsWith("myCookie4")) {86fail("The cookie should be myCookie4, not " + clst.get(0));87}88}89// We should get 4 cookies for non secure URI, and 5 for the secure one90m = cm.get(suri, emptyMap);91clst = m.get("Cookie");92if (clst.size() != 5) {93fail("Cookies didn't cross from http to https. Got only " + clst.size());94}9596m = cm.get(uri, emptyMap);97clst = m.get("Cookie");98if (clst.size() != 4) {99fail("We should have gotten only 4 cookies over http (non secure), got " +100clst.size());101}102if (isIn(clst, "myCookie5=")) {103// myCookie5 (the secure one) shouldn't be here104fail("Got the secure cookie over a non secure link");105}106107// Let's check that empty path is treated correctly108uri = new URI("http://www.sun.com/");109lst.clear();110lst.add("myCookie6=foo");111map.clear();112map.put("Set-Cookie", lst);113cm.put(uri, map);114uri = new URI("http://www.sun.com");115m = cm.get(uri, emptyMap);116clst = m.get("Cookie");117if (clst.size() != 1) {118fail("Missing a cookie when using an empty path");119}120121// And now, the other way around:122123uri = new URI("http://www.sun.com");124lst.clear();125lst.add("myCookie7=foo");126map.clear();127map.put("Set-Cookie", lst);128cm.put(uri, map);129uri = new URI("http://www.sun.com/");130m = cm.get(uri, emptyMap);131clst = m.get("Cookie");132if (!isIn(clst, "myCookie7=")) {133fail("Missing a cookie when using an empty path");134}135136// Let's make sure the 'Port' optional attributes is enforced137138lst.clear();139lst.add("myCookie8=porttest; port");140lst.add("myCookie9=porttest; port=\"80,8000\"");141lst.add("myCookie10=porttest; port=\"8000\"");142map.clear();143map.put("Set-Cookie", lst);144uri = new URI("http://www.sun.com/");145cm.put(uri, map);146147// myCookie10 should have been rejected148cookies = cs.getCookies();149for (HttpCookie c : cookies) {150if (c.getName().equals("myCookie10")) {151fail("A cookie with an invalid port list was accepted");152}153}154155uri = new URI("http://www.sun.com:80/");156m = cm.get(uri, emptyMap);157clst = m.get("Cookie");158// We should find both myCookie8 and myCookie9 but not myCookie10159if (!isIn(clst, "myCookie8=") || !isIn(clst, "myCookie9=")) {160fail("Missing a cookie on port 80");161}162uri = new URI("http://www.sun.com:8000/");163m = cm.get(uri, emptyMap);164clst = m.get("Cookie");165// We should find only myCookie9166if (!isIn(clst, "myCookie9=")) {167fail("Missing a cookie on port 80");168}169if (isIn(clst, "myCookie8=")) {170fail("A cookie with an invalid port list was returned");171}172173// Test httpOnly flag (CR# 6873543)174lst.clear();175map.clear();176cm.getCookieStore().removeAll();177lst.add("myCookie11=httpOnlyTest; httpOnly");178map.put("Set-Cookie", lst);179uri = new URI("http://www.sun.com/");180cm.put(uri, map);181m = cm.get(uri, emptyMap);182clst = m.get("Cookie");183// URI scheme was http: so we should get the cookie184if (!isIn(clst, "myCookie11=")) {185fail("Missing cookie with httpOnly flag");186}187uri = new URI("javascript://www.sun.com/");188m = cm.get(uri, emptyMap);189clst = m.get("Cookie");190// URI scheme was neither http or https so we shouldn't get the cookie191if (isIn(clst, "myCookie11=")) {192fail("Should get the cookie with httpOnly when scheme is javascript:");193}194}195196private static boolean isIn(List<String> lst, String cookie) {197if (lst == null || lst.isEmpty()) {198return false;199}200for (String s : lst) {201if (s.startsWith(cookie))202return true;203}204return false;205}206207private static void fail(String msg) throws Exception {208throw new RuntimeException(msg);209}210}211212213