Skip to content

Commit

Permalink
Update TCK tests for Cookie changes and previous challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Mar 26, 2024
1 parent 7d0d159 commit fdb6d18
Show file tree
Hide file tree
Showing 2 changed files with 366 additions and 178 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2021 Oracle and/or its affiliates and others.
* Copyright (c) 2006, 2024 Oracle and/or its affiliates and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -14,11 +14,6 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

/*
* $Id$
*/

package servlet.tck.api.jakarta_servlet_http.cookie;

import servlet.tck.common.client.AbstractTckTest;
Expand Down Expand Up @@ -133,18 +128,12 @@ public void getCommentNullTest() throws Exception {
*
* @assertion_ids: Servlet:JAVADOC:439
*
* @test_Strategy: Client sends a version 0 and 1 cookie to the servlet.
* Servlet verifies values and returns result to client
* @test_Strategy: Servlet tests method and returns result to client
*/
@Test
public void getDomainTest() throws Exception {
// version 1
TEST_PROPS.get().setProperty(REQUEST_HEADERS,
"Cookie: $Version=1; name1=value1; $Domain=" + _hostname
+ "; $Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getDomainTest");
invoke();

}

/*
Expand All @@ -169,17 +158,10 @@ public void getMaxAgeTest() throws Exception {
*/
@Test
public void getNameTest() throws Exception {
// version 0
TEST_PROPS.get().setProperty(REQUEST_HEADERS, "Cookie: name1=value1; Domain="
+ _hostname + "; Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getNameTest");
invoke();
// version 1
TEST_PROPS.get().setProperty(REQUEST_HEADERS,
"Cookie: $Version=1; name1=value1; $Domain=" + _hostname
+ "; $Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getNameTest");
invoke();
}

/*
Expand All @@ -191,9 +173,6 @@ public void getNameTest() throws Exception {
*/
@Test
public void getPathTest() throws Exception {
TEST_PROPS.get().setProperty(REQUEST_HEADERS,
"Cookie: $Version=1; name1=value1; $Domain=" + _hostname
+ "; $Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getPathTest");
invoke();
}
Expand All @@ -211,6 +190,12 @@ public void getSecureTest() throws Exception {
invoke();
}

@Test
public void getAttributeSecureTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "getAttributeSecureTest");
invoke();
}

/*
* @testName: getValueTest
*
Expand All @@ -220,16 +205,17 @@ public void getSecureTest() throws Exception {
*/
@Test
public void getValueTest() throws Exception {
// version 0
TEST_PROPS.get().setProperty(REQUEST_HEADERS, "Cookie: name1=value1; Domain="
+ _hostname + "; Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getValueTest");
invoke();
// version 1
TEST_PROPS.get().setProperty(REQUEST_HEADERS,
"Cookie: $Version=1; name1=value1; $Domain=" + _hostname
+ "; $Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getValueTest");
}

@Test
public void getValueQuotedTest() throws Exception {
TEST_PROPS.get().setProperty(REQUEST_HEADERS, "Cookie: name1=\"value1\"; Domain="
+ _hostname + "; Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getValueQuotedTest");
invoke();
}

Expand All @@ -242,16 +228,7 @@ public void getValueTest() throws Exception {
*/
@Test
public void getVersionTest() throws Exception {
// version 0
TEST_PROPS.get().setProperty(REQUEST_HEADERS, "Cookie: name1=value1; Domain="
+ _hostname + "; Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getVersionVer0Test");
invoke();
// version 1
TEST_PROPS.get().setProperty(REQUEST_HEADERS,
"Cookie: $Version=1; name1=value1; $Domain=" + _hostname
+ "; $Path=/servlet_jsh_cookie_web");
TEST_PROPS.get().setProperty(APITEST, "getVersionVer1Test");
TEST_PROPS.get().setProperty(APITEST, "getVersionTest");
invoke();
}

Expand Down Expand Up @@ -292,14 +269,13 @@ public void setMaxAgePositiveTest() throws Exception {
dateHeader = response.getResponseHeader("testDate")
.orElseThrow(() -> new NullPointerException("testDate is empty")).getValue();


logger.trace("Found {} set-cookie entry", response.getResponseHeaders("Set-Cookie").size());
logger.trace("Found {} set-cookie entry", String.valueOf(response.getResponseHeaders("Set-Cookie").size()));

boolean foundcookie = false;
List<String> cookiesHeaders = response.getResponseHeaders("Set-Cookie");
int i = 0;
while (i < cookiesHeaders.size()) {
logger.trace("Checking set-cookiei {}:{}", i, cookiesHeaders.get(i));
logger.trace("Checking set-cookiei {}:{}", String.valueOf(i), cookiesHeaders.get(i));
List<HttpCookie> cookies = HttpCookie.parse(cookiesHeaders.get(i));
Optional<HttpCookie> optionalHttpCookie = cookies.stream().filter(httpCookie -> httpCookie.getName().equals("name1"))
.findFirst();
Expand Down Expand Up @@ -335,7 +311,7 @@ public void setMaxAgePositiveTest() throws Exception {
throw new Exception("Exception occurred: " + t);
}

if (!body.contains(Data.PASSED)) {
if (body == null || !body.contains(Data.PASSED)) {
throw new Exception("The string: " + Data.PASSED + " not found in response");
}
}
Expand All @@ -350,10 +326,78 @@ public void setMaxAgePositiveTest() throws Exception {
@Test
public void setMaxAgeZeroTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setMaxAgeZeroTest");
TEST_PROPS.get().setProperty(EXPECTED_HEADERS, "Set-Cookie:name1=value1##Max-Age=0");
// RFC 6265 - server should only send +ve values for Max-Age
TEST_PROPS.get().setProperty(EXPECTED_HEADERS, "Set-Cookie:name1=value1##!Max-Age##Expires=");
invoke();
}

@Test
public void setMaxAgeZeroDateTest() throws Exception {
String testName = "setMaxAgeZeroTest";
HttpResponse response = null;
Date expiryDate = null;
String body = null;

HttpExchange request = new HttpExchange("GET " + getContextRoot() + "/"
+ getServletName() + "?testname=" + testName + " HTTP/1.1", _hostname,
_port);

try {
response = request.execute();

logger.trace("Found {} set-cookie entry", String.valueOf(response.getResponseHeaders("Set-Cookie").size()));

boolean foundcookie = false;
List<String> cookiesHeaders = response.getResponseHeaders("Set-Cookie");
int i = 0;
while (i < cookiesHeaders.size()) {
logger.trace("Checking set-cookie i {}:{}", String.valueOf(i), cookiesHeaders.get(i));
List<HttpCookie> cookies = HttpCookie.parse(cookiesHeaders.get(i));
Optional<HttpCookie> optionalHttpCookie = cookies.stream().filter(httpCookie -> httpCookie.getName().equals("name1"))
.findFirst();
if (optionalHttpCookie.isPresent()) {
HttpCookie httpCookie = optionalHttpCookie.get();
expiryDate = new Date(httpCookie.getMaxAge());
body = response.getResponseBodyAsString();
foundcookie = true;
break;
}
i++;
}

if (!foundcookie)
throw new Exception("The test cookie was not located in the response");
} catch (Throwable t) {
throw new Exception("Exception occurred:" + t, t);
}

// put expiry date into GMT
SimpleDateFormat sdf = new SimpleDateFormat(TestServlet.CUSTOM_HEADER_DATE_FORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String resultStringDate = sdf.format(expiryDate);

try {
Date resultDate = sdf.parse(resultStringDate);
/*
* RFC 6265 does not allow the server to send a Max-Age attribute with value zero. The implication from the client
* requirements is that an expires header should be sent with the earliest representable date. There is a fair
* amount of room for interpretation in all of this so check that whatever has been sent is in the past.
*/
Date expectedDate = new Date(System.currentTimeMillis());
if (resultDate.after(expectedDate)) {
throw new Exception("The expiry date was incorrect, expected ="
+ expectedDate + ", result = " + resultDate);
}
} catch (Throwable t) {
throw new Exception("Exception occurred: " + t);
}

if (body == null || !body.contains(Data.PASSED)) {
throw new Exception("The string: " + Data.PASSED + " not found in response");
}

}

/*
* @testName: setMaxAgeNegativeTest
*
Expand Down Expand Up @@ -393,9 +437,19 @@ public void setPathTest() throws Exception {
*/
@Test
public void setSecureTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setSecureVer0Test");
TEST_PROPS.get().setProperty(APITEST, "setSecureTest");
invoke();
}

@Test
public void setAttributeSecureTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setAttributeSecureTest");
invoke();
TEST_PROPS.get().setProperty(APITEST, "setSecureVer1Test");
}

@Test
public void setAttributeSecureInvalidTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setAttributeSecureInvalidTest");
invoke();
}

Expand All @@ -408,9 +462,7 @@ public void setSecureTest() throws Exception {
*/
@Test
public void setValueTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setValueVer0Test");
invoke();
TEST_PROPS.get().setProperty(APITEST, "setValueVer1Test");
TEST_PROPS.get().setProperty(APITEST, "setValueTest");
invoke();
}

Expand Down Expand Up @@ -454,4 +506,41 @@ public void getAttributesTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "getAttributesTest");
invoke();
}

@Test
public void getHttpOnlyTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "getHttpOnlyTest");
invoke();
}

@Test
public void getAttributeHttpOnlyTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "getAttributeHttpOnlyTest");
invoke();
}

@Test
public void setHttpOnlyTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setHttpOnlyTest");
invoke();
}

@Test
public void setAttributeHttpOnlyTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setAttributeHttpOnlyTest");
invoke();
}

@Test
public void setAttributeHttpOnlyInvalidTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setAttributeHttpOnlyInvalidTest");
invoke();
}

@Test
public void setPartitionedTest() throws Exception {
TEST_PROPS.get().setProperty(APITEST, "setPartitionedTest");
TEST_PROPS.get().setProperty(EXPECTED_HEADERS, "Set-Cookie:name1=value1##Partitioned##!Partitioned=");
invoke();
}
}

0 comments on commit fdb6d18

Please sign in to comment.