From 3ad5d93c75bf533fb391c125809170c39cd1ef0a Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Tue, 23 Nov 2021 08:56:36 +0000 Subject: [PATCH] Fix cookie tests after complete switch to RFC 6265 A number of tests issue RFC 2109 cookies. The tests have only been updated to process these as per RFC 6265 which essentially means that the attributes such as $Domain get treated as separate cookies. --- .../cookie/TestServlet.java | 224 +++--------------- .../cookie/URLClient.java | 54 +---- .../cookie/URLClient.java | 47 +--- 3 files changed, 46 insertions(+), 279 deletions(-) diff --git a/src/com/sun/ts/tests/servlet/api/jakarta_servlet_http/cookie/TestServlet.java b/src/com/sun/ts/tests/servlet/api/jakarta_servlet_http/cookie/TestServlet.java index 82daffb857..f7051a0bc0 100644 --- a/src/com/sun/ts/tests/servlet/api/jakarta_servlet_http/cookie/TestServlet.java +++ b/src/com/sun/ts/tests/servlet/api/jakarta_servlet_http/cookie/TestServlet.java @@ -125,8 +125,7 @@ public void constructorTest(HttpServletRequest request, public void constructorIllegalArgumentExceptionTest( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String[] invalidNameValues = { "$test", ",test", ";test", " test", "\ttest", - "\ntest" }; + String[] invalidNameValues = { ",test", ";test", " test", "\ttest", "\ntest" }; PrintWriter pw = response.getWriter(); @@ -157,24 +156,18 @@ public void getCommentTest(HttpServletRequest request, boolean passed = false; Cookie testCookie = new Cookie("name1", "value1"); - String expectedResult = "This is a comment"; // set and get - testCookie.setComment(expectedResult); + testCookie.setComment("This is a comment"); String result = testCookie.getComment(); response.addCookie(testCookie); - if (result != null) { - if (!result.equals(expectedResult)) { - passed = false; - pw.println("getComment() returned an incorrect result"); - pw.println("Expected = " + expectedResult); - pw.println("Actual = |" + result + "| "); - } else { - passed = true; - } + if (result == null) { + passed = true; } else { passed = false; - pw.println("getComment() returned a null result "); + pw.println("getComment() returned an incorrect result"); + pw.println("Expected null value"); + pw.println("Actual = |" + result + "| "); } ServletTestUtil.printResult(pw, passed); @@ -203,7 +196,9 @@ public void getDomainTest(HttpServletRequest request, PrintWriter pw = response.getWriter(); boolean passed = false; Cookie[] cookie = request.getCookies(); - int index = ServletTestUtil.findCookie(cookie, "name1"); + // RFC 6265 treats the domain attribute of an RFC 2109 cookie as a separate + // cookie + int index = ServletTestUtil.findCookie(cookie, "$Domain"); if (index >= 0) { String host = request.getHeader("host"); int col = host.indexOf(':'); @@ -212,7 +207,7 @@ public void getDomainTest(HttpServletRequest request, } // get - String result = cookie[index].getDomain(); + String result = cookie[index].getValue(); if (result != null) { if (!result.equalsIgnoreCase(host)) { passed = false; @@ -288,10 +283,12 @@ public void getPathTest(HttpServletRequest request, PrintWriter pw = response.getWriter(); boolean passed = false; Cookie[] cookie = request.getCookies(); - int index = ServletTestUtil.findCookie(cookie, "name1"); + // RFC 6265 treats the path attribute of an RFC 2109 cookie as a separate + // cookie + int index = ServletTestUtil.findCookie(cookie, "$Path"); if (index >= 0) { String expectedResult = request.getContextPath(); - String result = cookie[index].getPath(); + String result = cookie[index].getValue(); if (result != null) { if (!result.equals(expectedResult)) { passed = false; @@ -367,6 +364,7 @@ public void getValueTest(HttpServletRequest request, public void getVersionVer0Test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // Version should be hard-coded to zero PrintWriter pw = response.getWriter(); boolean passed = false; Cookie[] cookie = request.getCookies(); @@ -392,12 +390,13 @@ public void getVersionVer0Test(HttpServletRequest request, public void getVersionVer1Test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // Version should be hard-coded to zero PrintWriter pw = response.getWriter(); boolean passed = false; Cookie[] cookie = request.getCookies(); int index = ServletTestUtil.findCookie(cookie, "name1"); if (index >= 0) { - int expectedResult = 1; + int expectedResult = 0; int result = cookie[index].getVersion(); if (result != expectedResult) { passed = false; @@ -414,73 +413,7 @@ public void getVersionVer1Test(HttpServletRequest request, ServletTestUtil.printResult(pw, passed); } - public void setCommentVer0Test(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - - PrintWriter pw = response.getWriter(); - boolean passed = false; - Cookie testCookie = new Cookie("name1", "value1"); - testCookie.setVersion(0); - - String expectedResult = "This is a comment"; - // set comment - testCookie.setComment(expectedResult); - - // get and see if it is properly set - String result = testCookie.getComment(); - - response.addCookie(testCookie); - if (result != null) { - if (!result.equals(expectedResult)) { - passed = false; - pw.println("setComment(" + expectedResult - + ") did not set the comment proplerly "); - pw.println("Expected = " + expectedResult + " "); - pw.println("Actual = |" + result + "| "); - } else { - passed = true; - } - } else { - passed = false; - pw.println("getComment() returned a null result "); - } - ServletTestUtil.printResult(pw, passed); - } - - public void setCommentVer1Test(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - - PrintWriter pw = response.getWriter(); - boolean passed = false; - Cookie testCookie = new Cookie("name1", "value1"); - testCookie.setVersion(1); - - String expectedResult = "This is a comment"; - // set comment - testCookie.setComment(expectedResult); - - // get and see if it is properly set - String result = testCookie.getComment(); - - response.addCookie(testCookie); - if (result != null) { - if (!result.equals(expectedResult)) { - passed = false; - pw.println("setComment(" + expectedResult - + ") did not set the comment proplerly "); - pw.println("Expected = " + expectedResult + " "); - pw.println("Actual = |" + result + "| "); - } else { - passed = true; - } - } else { - passed = false; - pw.println("getComment() returned a null result "); - } - ServletTestUtil.printResult(pw, passed); - } - - public void setDomainVer0Test(HttpServletRequest request, + public void setDomainTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); @@ -488,37 +421,7 @@ public void setDomainVer0Test(HttpServletRequest request, Cookie testCookie = new Cookie("name1", "value1"); testCookie.setVersion(0); - String expectedResult = ".ENG.COM"; - testCookie.setDomain(expectedResult); - String result = testCookie.getDomain(); - - response.addCookie(testCookie); - if (result != null) { - if (!result.equalsIgnoreCase(expectedResult)) { - passed = false; - pw.println("setDomain(" + expectedResult - + ") did not set the domain properly "); - pw.println("Expected = " + expectedResult + " "); - pw.println("Actual = |" + result + "| "); - } else { - passed = true; - } - } else { - passed = false; - pw.println("getDomain() returned a null result "); - } - ServletTestUtil.printResult(pw, passed); - } - - public void setDomainVer1Test(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - - PrintWriter pw = response.getWriter(); - boolean passed = false; - Cookie testCookie = new Cookie("name1", "value1"); - testCookie.setVersion(1); - - String expectedResult = ".ENG.COM"; + String expectedResult = "ENG.COM"; testCookie.setDomain(expectedResult); String result = testCookie.getDomain(); @@ -540,7 +443,7 @@ public void setDomainVer1Test(HttpServletRequest request, ServletTestUtil.printResult(pw, passed); } - public void setMaxAgeVer0PositiveTest(HttpServletRequest request, + public void setMaxAgePositiveTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); @@ -560,19 +463,7 @@ public void setMaxAgeVer0PositiveTest(HttpServletRequest request, ServletTestUtil.printResult(pw, true); } - public void setMaxAgeVer1PositiveTest(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - PrintWriter pw = response.getWriter(); - - Cookie testCookie = new Cookie("name1", "value1"); - testCookie.setVersion(1); - testCookie.setMaxAge(2); - response.addCookie(testCookie); - ServletTestUtil.printResult(pw, true); - - } - - public void setMaxAgeZeroVer0Test(HttpServletRequest request, + public void setMaxAgeZeroTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); @@ -585,20 +476,7 @@ public void setMaxAgeZeroVer0Test(HttpServletRequest request, ServletTestUtil.printResult(pw, true); } - public void setMaxAgeZeroVer1Test(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - - PrintWriter pw = response.getWriter(); - Cookie testCookie = new Cookie("name1", "value1"); - testCookie.setVersion(1); - - testCookie.setMaxAge(0); - response.addCookie(testCookie); - - ServletTestUtil.printResult(pw, true); - } - - public void setMaxAgeNegativeVer0Test(HttpServletRequest request, + public void setMaxAgeNegativeTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); @@ -611,20 +489,7 @@ public void setMaxAgeNegativeVer0Test(HttpServletRequest request, ServletTestUtil.printResult(pw, true); } - public void setMaxAgeNegativeVer1Test(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - - PrintWriter pw = response.getWriter(); - Cookie testCookie = new Cookie("name1", "value1"); - testCookie.setVersion(1); - - testCookie.setMaxAge(-1); - response.addCookie(testCookie); - - ServletTestUtil.printResult(pw, true); - } - - public void setPathVer0Test(HttpServletRequest request, + public void setPathTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); @@ -654,36 +519,6 @@ public void setPathVer0Test(HttpServletRequest request, ServletTestUtil.printResult(pw, passed); } - public void setPathVer1Test(HttpServletRequest request, - HttpServletResponse response) throws ServletException, IOException { - - PrintWriter pw = response.getWriter(); - boolean passed = false; - Cookie testCookie = new Cookie("name1", "value1"); - testCookie.setVersion(1); - - String expectedResult = "\"/servlet_jsh_cookie_web\""; - testCookie.setPath(expectedResult); - String result = testCookie.getPath(); - - response.addCookie(testCookie); - if (result != null) { - if (!result.equals(expectedResult)) { - passed = false; - pw.println( - "setPath(" + expectedResult + ") returned an incorrect result"); - pw.println("Expected = " + expectedResult + " "); - pw.println("Actual = |" + result + "| "); - } else { - passed = true; - } - } else { - passed = false; - pw.println("getPath() returned a null result "); - } - ServletTestUtil.printResult(pw, passed); - } - public void setSecureVer0Test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -793,12 +628,13 @@ public void setValueVer1Test(HttpServletRequest request, public void setVersionVer0Test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // Expected to be hard-coded to zero PrintWriter pw = response.getWriter(); boolean passed = false; Cookie testCookie = new Cookie("name1", "value1"); int expectedResult = 0; - testCookie.setVersion(expectedResult); // set according to original Netscape - // specifications + testCookie.setVersion(expectedResult); + int result = testCookie.getVersion(); response.addCookie(testCookie); @@ -819,9 +655,9 @@ public void setVersionVer1Test(HttpServletRequest request, PrintWriter pw = response.getWriter(); boolean passed = false; Cookie testCookie = new Cookie("name1", "value1"); - int expectedResult = 1; - testCookie.setVersion(expectedResult); // set according to original Netscape - // specifications + int expectedResult = 0; + testCookie.setVersion(1); + int result = testCookie.getVersion(); response.addCookie(testCookie); diff --git a/src/com/sun/ts/tests/servlet/api/jakarta_servlet_http/cookie/URLClient.java b/src/com/sun/ts/tests/servlet/api/jakarta_servlet_http/cookie/URLClient.java index f165b9c93b..79cc7080d7 100644 --- a/src/com/sun/ts/tests/servlet/api/jakarta_servlet_http/cookie/URLClient.java +++ b/src/com/sun/ts/tests/servlet/api/jakarta_servlet_http/cookie/URLClient.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2020 Oracle and/or its affiliates and others. + * Copyright (c) 2006, 2021 Oracle and/or its affiliates and others. * All rights reserved. * * This program and the accompanying materials are made available under the @@ -272,20 +272,6 @@ public void getVersionTest() throws Fault { invoke(); } - /* - * @testName: setCommentTest - * - * @assertion_ids: Servlet:JAVADOC:435 - * - * @test_Strategy: Servlet tests method and returns result to client - */ - public void setCommentTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setCommentVer0Test"); - invoke(); - TEST_PROPS.setProperty(APITEST, "setCommentVer1Test"); - invoke(); - } - /* * @testName: setDomainTest * @@ -294,9 +280,7 @@ public void setCommentTest() throws Fault { * @test_Strategy: Servlet tests method and returns result to client */ public void setDomainTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setDomainVer0Test"); - invoke(); - TEST_PROPS.setProperty(APITEST, "setDomainVer1Test"); + TEST_PROPS.setProperty(APITEST, "setDomainTest"); invoke(); } @@ -308,8 +292,7 @@ public void setDomainTest() throws Fault { * @test_Strategy: Servlet sets values and client verifies them */ public void setMaxAgePositiveTest() throws Fault { - // version 0 cookie - String testName = "setMaxAgeVer0PositiveTest"; + String testName = "setMaxAgePositiveTest"; HttpResponse response = null; String dateHeader = null; int index = -1; @@ -349,7 +332,7 @@ public void setMaxAgePositiveTest() throws Fault { if (!foundcookie) throw new Fault("The test cookie was not located in the response"); } catch (Throwable t) { - throw new Fault("Exception occurred:" + t); + throw new Fault("Exception occurred:" + t, t); } // put expiry date into GMT @@ -371,12 +354,6 @@ public void setMaxAgePositiveTest() throws Fault { if (body.indexOf(Data.PASSED) == -1) { throw new Fault("The string: " + Data.PASSED + " not found in response"); } - - // version 1 cookie - TEST_PROPS.setProperty(APITEST, "setMaxAgeVer1PositiveTest"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##Version=1##Max-Age=2"); - invoke(); } /* @@ -387,13 +364,8 @@ public void setMaxAgePositiveTest() throws Fault { * @test_Strategy: Servlet sets values and client verifies them */ public void setMaxAgeZeroTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setMaxAgeZeroVer0Test"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, "Set-Cookie:name1=value1"); - invoke(); - - TEST_PROPS.setProperty(APITEST, "setMaxAgeZeroVer1Test"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##Version=1##Max-Age=0"); + TEST_PROPS.setProperty(APITEST, "setMaxAgeZeroTest"); + TEST_PROPS.setProperty(EXPECTED_HEADERS, "Set-Cookie:name1=value1##Max-Age=0"); invoke(); } @@ -405,13 +377,9 @@ public void setMaxAgeZeroTest() throws Fault { * @test_Strategy: Servlet sets values and client verifies them */ public void setMaxAgeNegativeTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setMaxAgeNegativeVer0Test"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##!Expire"); - invoke(); - TEST_PROPS.setProperty(APITEST, "setMaxAgeNegativeVer1Test"); + TEST_PROPS.setProperty(APITEST, "setMaxAgeNegativeTest"); TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##Version=1##!Max-Age"); + "Set-Cookie:name1=value1##!Expire##!Max-Age"); invoke(); } @@ -423,14 +391,10 @@ public void setMaxAgeNegativeTest() throws Fault { * @test_Strategy: Servlet tests method and returns result to client */ public void setPathTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setPathVer0Test"); + TEST_PROPS.setProperty(APITEST, "setPathTest"); TEST_PROPS.setProperty(EXPECTED_HEADERS, "Set-Cookie:Path=\"/servlet_jsh_cookie_web\""); invoke(); - TEST_PROPS.setProperty(APITEST, "setPathVer1Test"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##Version=1##Path=\"/servlet_jsh_cookie_web\""); - invoke(); } /* diff --git a/src/com/sun/ts/tests/servlet/pluggability/api/jakarta_servlet_http/cookie/URLClient.java b/src/com/sun/ts/tests/servlet/pluggability/api/jakarta_servlet_http/cookie/URLClient.java index c3fc263de6..18e0ecf7f9 100644 --- a/src/com/sun/ts/tests/servlet/pluggability/api/jakarta_servlet_http/cookie/URLClient.java +++ b/src/com/sun/ts/tests/servlet/pluggability/api/jakarta_servlet_http/cookie/URLClient.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2020 Oracle and/or its affiliates and others. + * Copyright (c) 2012, 2021 Oracle and/or its affiliates and others. * All rights reserved. * * This program and the accompanying materials are made available under the @@ -303,22 +303,6 @@ public void getVersionTest() throws Fault { invoke(); } - /* - * @testName: setCommentTest - * - * @assertion_ids: Servlet:JAVADOC:435 - * - * @test_Strategy: Create a web application with no web.xml and one fragment; - * Define everything in web-fragment.xml; Package everything in the fragment; - * Servlet tests method Cookie.setComment - */ - public void setCommentTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setCommentVer0Test"); - invoke(); - TEST_PROPS.setProperty(APITEST, "setCommentVer1Test"); - invoke(); - } - /* * @testName: setDomainTest * @@ -329,9 +313,7 @@ public void setCommentTest() throws Fault { * Servlet tests method Cookie.setDomain */ public void setDomainTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setDomainVer0Test"); - invoke(); - TEST_PROPS.setProperty(APITEST, "setDomainVer1Test"); + TEST_PROPS.setProperty(APITEST, "setDomainTest"); invoke(); } @@ -347,7 +329,7 @@ public void setDomainTest() throws Fault { */ public void setMaxAgePositiveTest() throws Fault { // version 0 cookie - String testName = "setMaxAgeVer0PositiveTest"; + String testName = "setMaxAgePositiveTest"; HttpResponse response = null; String dateHeader = null; int index = -1; @@ -410,12 +392,6 @@ public void setMaxAgePositiveTest() throws Fault { if (body.indexOf(Data.PASSED) == -1) { throw new Fault("The string: " + Data.PASSED + " not found in response"); } - - // version 1 cookie - TEST_PROPS.setProperty(APITEST, "setMaxAgeVer1PositiveTest"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##Version=1##Max-Age=2"); - invoke(); } /* @@ -429,13 +405,8 @@ public void setMaxAgePositiveTest() throws Fault { * sent back to client and client verifies them */ public void setMaxAgeZeroTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setMaxAgeZeroVer0Test"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, "Set-Cookie:name1=value1"); - invoke(); - - TEST_PROPS.setProperty(APITEST, "setMaxAgeZeroVer1Test"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##Version=1##Max-Age=0"); + TEST_PROPS.setProperty(APITEST, "setMaxAgeZeroTest"); + TEST_PROPS.setProperty(EXPECTED_HEADERS, "Set-Cookie:name1=value1##Max-Age=0"); invoke(); } @@ -450,13 +421,9 @@ public void setMaxAgeZeroTest() throws Fault { * sent back to client and client verifies them */ public void setMaxAgeNegativeTest() throws Fault { - TEST_PROPS.setProperty(APITEST, "setMaxAgeNegativeVer0Test"); - TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##!Expire"); - invoke(); - TEST_PROPS.setProperty(APITEST, "setMaxAgeNegativeVer1Test"); + TEST_PROPS.setProperty(APITEST, "setMaxAgeNegativeTest"); TEST_PROPS.setProperty(EXPECTED_HEADERS, - "Set-Cookie:name1=value1##Version=1##!Max-Age"); + "Set-Cookie:name1=value1##!Expire##!Max-Age"); invoke(); }