Skip to content

Commit

Permalink
Fix cookie tests after complete switch to RFC 6265
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markt-asf committed Jan 14, 2022
1 parent d7f95ea commit 3ad5d93
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 279 deletions.
Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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(':');
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -414,111 +413,15 @@ 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();
boolean passed = false;
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();

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 3ad5d93

Please sign in to comment.