Path: blob/master/test/jdk/java/net/CookieHandler/TestHttpCookie.java
41149 views
/*1* Copyright (c) 2005, 2013, 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* @summary Unit test for java.net.HttpCookie26* @bug 6244040 6277796 6277801 6277808 6294071 6692802 6790677 6901170 802075827* @author Edward Wang28*/2930import java.net.HttpCookie;31import java.util.List;3233public class TestHttpCookie {34private static int testCount = 0;3536private String cHeader = null;37private List<HttpCookie> cookies = null;3839// test case here expressed as a string, which represents40// the header string to be parsed into HttpCookie instance.41// A TestHttpCookie instance will be created to hold such a HttpCookie42// object, and TestHttpCookie class has utility methods to check equality43// between HttpCookie's real property and expected property.44static TestHttpCookie test(String cookieHeader) {45testCount++;46return new TestHttpCookie(cookieHeader);47}4849TestHttpCookie(String cHeader) {50this.cHeader = cHeader;5152try {53List<HttpCookie> cookies = HttpCookie.parse(cHeader);54this.cookies = cookies;55} catch (IllegalArgumentException ignored) {56cookies = null;57}58}5960// check name61TestHttpCookie n(int index, String n) {62HttpCookie cookie = cookies.get(index);63if (cookie == null || !n.equalsIgnoreCase(cookie.getName())) {64raiseError("name", cookie.getName(), n);65}6667return this;68}69TestHttpCookie n(String n) { return n(0, n); }7071// check value72TestHttpCookie v(int index, String v) {73HttpCookie cookie = cookies.get(index);74if (cookie == null || !v.equals(cookie.getValue())) {75raiseError("value", cookie.getValue(), v);76}7778return this;79}80TestHttpCookie v(String v) { return v(0, v); }8182// check version83TestHttpCookie ver(int index, int ver) {84HttpCookie cookie = cookies.get(index);85if (cookie == null || (ver != cookie.getVersion())) {86raiseError("version", Integer.toString(cookie.getVersion()), Integer.toString(ver));87}8889return this;90}91TestHttpCookie ver(int ver) { return ver(0, ver); }9293// check path94TestHttpCookie p(int index, String p) {95HttpCookie cookie = cookies.get(index);96if (cookie == null || !p.equals(cookie.getPath())) {97raiseError("path", cookie.getPath(), p);98}99100return this;101}102TestHttpCookie p(String p) { return p(0, p); }103104// check null-ability105TestHttpCookie nil() {106if (cookies != null) {107raiseError("Check null-ability fail");108}109110return this;111}112113// check comment114TestHttpCookie c(int index, String c) {115HttpCookie cookie = cookies.get(index);116if (cookie == null || !c.equals(cookie.getComment())) {117raiseError("comment", cookie.getComment(), c);118}119120return this;121}122TestHttpCookie c(String c) { return c(0, c); }123124// check comment url125TestHttpCookie cu(int index, String cu) {126HttpCookie cookie = cookies.get(index);127if (cookie == null || !cu.equals(cookie.getCommentURL())) {128raiseError("comment url", cookie.getCommentURL(), cu);129}130131return this;132}133TestHttpCookie cu(String cu) { return cu(0, cu); }134135// check discard136TestHttpCookie dsc(int index, boolean dsc) {137HttpCookie cookie = cookies.get(index);138if (cookie == null || (dsc != cookie.getDiscard())) {139raiseError("discard", Boolean.toString(cookie.getDiscard()), Boolean.toString(dsc));140}141142return this;143}144TestHttpCookie dsc(boolean dsc) { return dsc(0, dsc); }145146// check domain147TestHttpCookie d(int index, String d) {148HttpCookie cookie = cookies.get(index);149if (cookie == null || !d.equalsIgnoreCase(cookie.getDomain())) {150raiseError("domain", cookie.getDomain(), d);151}152153return this;154}155TestHttpCookie d(String d) { return d(0, d); }156157// check max-age158TestHttpCookie a(int index, long a) {159HttpCookie cookie = cookies.get(index);160if (cookie == null || (a != cookie.getMaxAge())) {161raiseError("max-age", Long.toString(cookie.getMaxAge()), Long.toString(a));162}163164return this;165}166TestHttpCookie a(long a) { return a(0, a); }167168// check port list169TestHttpCookie port(int index, String p) {170HttpCookie cookie = cookies.get(index);171if (cookie == null || !p.equals(cookie.getPortlist())) {172raiseError("portlist", cookie.getPortlist(), p);173}174175return this;176}177TestHttpCookie port(String p) { return port(0, p); }178179// check http only180TestHttpCookie httpOnly(int index, boolean b) {181HttpCookie cookie = cookies.get(index);182if (cookie == null || b != cookie.isHttpOnly()) {183raiseError("HttpOnly", String.valueOf(cookie.isHttpOnly()), String.valueOf(b));184}185return this;186}187188TestHttpCookie httpOnly(boolean b) {189return httpOnly(0, b);190}191192// check equality193static void eq(HttpCookie ck1, HttpCookie ck2, boolean same) {194testCount++;195if (ck1.equals(ck2) != same) {196raiseError("Comparison inconsistent: " + ck1 + " " + ck2197+ " should " + (same ? "equal" : "not equal"));198}199200int h1 = ck1.hashCode();201int h2 = ck2.hashCode();202if ((h1 == h2) != same) {203raiseError("Comparison inconsistent: hashCode for " + ck1 + " " + ck2204+ " should " + (same ? "equal" : "not equal"));205}206}207208// check domainMatches()209static void dm(String domain, String host, boolean matches) {210testCount++;211if (HttpCookie.domainMatches(domain, host) != matches) {212raiseError("Host " + host + (matches?" should ":" should not ") +213"domain-match with domain " + domain);214}215}216217void raiseError(String attr, String realValue, String expectedValue) {218StringBuilder sb = new StringBuilder();219sb.append("Cookie ").append(attr).append(" is ").append(realValue).220append(", should be ").append(expectedValue).221append(" (").append(cHeader).append(")");222throw new RuntimeException(sb.toString());223}224225static void raiseError(String prompt) {226throw new RuntimeException(prompt);227}228229static void runTests() {230rfc2965();231netscape();232misc();233}234235static void rfc2965() {236header("Test using rfc 2965 syntax");237238test("set-cookie2: Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"")239.n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");240241// whitespace between attr and = sign242test("set-cookie2: Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")243.n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");244245// $NAME is reserved; result should be null246test("set-cookie2: $Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")247.nil();248249// a 'full' cookie250test("set-cookie2: Customer=\"WILE_E_COYOTE\"" +251";Version=\"1\"" +252";Path=\"/acme\"" +253";Comment=\"this is a coyote\"" +254";CommentURL=\"http://www.coyote.org\"" +255";Discard" +256";Domain=\".coyote.org\"" +257";Max-Age=\"3600\"" +258";Port=\"80\"" +259";Secure")260.n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")261.c("this is a coyote").cu("http://www.coyote.org").dsc(true)262.d(".coyote.org").a(3600).port("80");263264// a 'full' cookie, without leading set-cookie2 token265test("Customer=\"WILE_E_COYOTE\"" +266";Version=\"1\"" +267";Path=\"/acme\"" +268";Comment=\"this is a coyote\"" +269";CommentURL=\"http://www.coyote.org\"" +270";Discard" +271";Domain=\".coyote.org\"" +272";Max-Age=\"3600\"" +273";Port=\"80\"" +274";Secure")275.n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")276.c("this is a coyote").cu("http://www.coyote.org").dsc(true)277.d(".coyote.org").a(3600).port("80");278279// empty set-cookie string280test("").nil();281282// NullPointerException expected283try {284test(null);285} catch (NullPointerException ignored) {286// no-op287}288289// bug 6277796290test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\"; path=\"/www\"; Port=\"80\"")291.n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);292293// bug 6277801294test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\"; path=\"/www\"; Port=\"80\"" +295";Domain=\".java.sun.com\"; Max-Age=\"200\"; path=\"/javadoc\"; Port=\"8080\"")296.n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);297298// bug 6294071299test("Set-Cookie2:Customer=\"dtftest\";Discard; Secure; Domain=\"sun.com\"; Max-Age=\"100\";Version=\"1\"; Path=\"/www\"; Port=\"80,8080\"")300.n("Customer").v("dtftest").ver(1).d("sun.com").p("/www").port("80,8080").dsc(true).a(100);301test("Set-Cookie2:Customer=\"developer\";Domain=\"sun.com\";Max-Age=\"100\";Path=\"/www\";Port=\"80,8080\";CommentURL=\"http://www.sun.com/java1,000,000.html\"")302.n("Customer").v("developer").d("sun.com").p("/www").port("80,8080").a(100).cu("http://www.sun.com/java1,000,000.html");303304// a header string contains 2 cookies305test("Set-Cookie2:C1=\"V1\";Domain=\".sun1.com\";path=\"/www1\";Max-Age=\"100\",C2=\"V2\";Domain=\".sun2.com\";path=\"/www2\";Max-Age=\"200\"")306.n(0, "C1").v(0, "V1").p(0, "/www1").a(0, 100).d(0, ".sun1.com")307.n(1, "C2").v(1, "V2").p(1, "/www2").a(1, 200).d(1, ".sun2.com");308309// Bug 6790677: Should ignore bogus attributes310test("Set-Cookie2:C1=\"V1\";foobar").n(0, "C1").v(0, "V1");311}312313static void netscape() {314header("Test using netscape cookie syntax");315316test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")317.n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);318319// a Netscape cookie, without set-cookie leading token320test("CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")321.n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);322323// a 'google' cookie324test("Set-Cookie: PREF=ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU;" +325"expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com")326.n("PREF").v("ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU")327.p("/").d(".google.com").ver(0);328329// bug 6277796330test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; Secure")331.n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);332333// bug 6277801334test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; path=\"/acme\"")335.n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);336337// bug 6901170338test("set-cookie: CUSTOMER=WILE_E_COYOTE; version='1'").ver(1);339}340341static void misc() {342header("Test equals()");343344// test equals()345HttpCookie c1 = new HttpCookie("Customer", "WILE_E_COYOTE");346c1.setDomain(".coyote.org");347c1.setPath("/acme");348HttpCookie c2 = (HttpCookie)c1.clone();349eq(c1, c2, true);350351// test equals() when domain and path are null352c1 = new HttpCookie("Customer", "WILE_E_COYOTE");353c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");354eq(c1, c2, true);355356// path is case-sensitive357c1 = new HttpCookie("Customer", "WILE_E_COYOTE");358c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");359c1.setPath("/acme");360c2.setPath("/ACME");361eq(c1, c2, false);362363header("Test domainMatches()");364dm(".foo.com", "y.x.foo.com", false);365dm(".foo.com", "x.foo.com", true);366dm(".com", "whatever.com", false);367dm(".com.", "whatever.com", false);368dm(".ajax.com", "ajax.com", true);369dm(".local", "example.local", true);370dm("example.local", "example", true);371372// bug 6277808373testCount++;374try {375c1 = new HttpCookie("", "whatever");376} catch (IllegalArgumentException ignored) {377// expected exception; no-op378}379380// CR 6692802: HttpOnly flag381test("set-cookie: CUSTOMER=WILE_E_COYOTE;HttpOnly").httpOnly(true);382test("set-cookie: CUSTOMER=WILE_E_COYOTE").httpOnly(false);383384// space disallowed in name (both Netscape and RFC2965)385test("set-cookie: CUST OMER=WILE_E_COYOTE").nil();386}387388static void header(String prompt) {389System.out.println("== " + prompt + " ==");390}391392public static void main(String[] args) {393runTests();394395System.out.println("Succeeded in running " + testCount + " tests.");396}397}398399400