Author: jfrederic.clere(a)jboss.com
Date: 2009-04-16 12:31:35 -0400 (Thu, 16 Apr 2009)
New Revision: 1011
Added:
trunk/test/build.properties.default
trunk/test/build.xml
trunk/test/java/org/jboss/web/cookies/
trunk/test/java/org/jboss/web/cookies/CookiesTestCase.java
trunk/test/webapps/cookies/
trunk/test/webapps/cookies/test.jsp
Log:
Add some tests for the cookie fixes.
Added: trunk/test/build.properties.default
===================================================================
--- trunk/test/build.properties.default (rev 0)
+++ trunk/test/build.properties.default 2009-04-16 16:31:35 UTC (rev 1011)
@@ -0,0 +1,5 @@
+# junit stuff
+junit.version=4.5
+junit.jar.loc=http://kent.dl.sourceforge.net/sourceforge/junit/junit-${junit.version}.jar
+junit.jar=${base.path}/junit-${junit.version}/junit-${junit.version}.jar
+base-junit.home=${base.path}/junit-${junit.version}
Added: trunk/test/build.xml
===================================================================
--- trunk/test/build.xml (rev 0)
+++ trunk/test/build.xml 2009-04-16 16:31:35 UTC (rev 1011)
@@ -0,0 +1,116 @@
+<?xml version="1.0"?>
+<!--
+ * Copyright(c) 2009 Red Hat Middleware, LLC,
+ * and individual contributors as indicated by the @authors tag.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * @author Jean-Frederic Clere
+ * @version $Revision$
+-->
+<project name="jbossweb_test" default="all"
basedir=".">
+
+ <!-- See "build.properties.sample" in the top level directory for all
-->
+ <!-- property values you must customize for successful building!!! -->
+ <property file="${user.home}/build.properties"/>
+ <property file="build.properties"/>
+
+ <property file="build.properties.default"/>
+
+ <property name="test.classes"
value="${basedir}/output/classes"/>
+
+ <property name="compile.source" value="1.5"/>
+
+ <property name="test.runner"
value="junit.textui.TestRunner"/>
+
+ <path id="jbossweb.test.classpath">
+ <pathelement location="${test.classes}"/>
+ <pathelement location="${junit.jar}"/>
+ </path>
+
+ <target name="compile" depends="download">
+
+ <mkdir dir="${test.classes}"/>
+ <mkdir dir="${test.classes}/conf"/>
+
+ <!-- Compile -->
+ <javac srcdir="." destdir="${test.classes}"
+ debug="on" debuglevel="lines,vars,source"
+ deprecation="${compile.deprecation}"
+ source="${compile.source}"
+ optimize="${compile.optimize}">
+ <classpath refid="jbossweb.test.classpath" />
+ <include name="java/org/jboss/web/cookies/**" />
+ </javac>
+ </target>
+
+ <!-- Run the tests -->
+ <target name="all" depends="compile">
+ <java dir="${test.classes}" classname="${test.runner}"
fork="yes">
+ <arg value="org.jboss.web.cookies.CookiesTestCase"/>
+ <jvmarg line="-Dcluster=${cluster}"/>
+ <classpath refid="jbossweb.test.classpath"/>
+ </java>
+ </target>
+
+ <!-- Download and dependency building -->
+ <target name="proxyflags">
+ <!-- check proxy parameters. -->
+ <condition property="useproxy">
+ <equals arg1="${proxy.use}" arg2="on" />
+ </condition>
+ </target>
+
+ <target name="setproxy" depends="proxyflags"
if="useproxy">
+ <taskdef name="setproxy"
+ classname="org.apache.tools.ant.taskdefs.optional.net.SetProxy"
/>
+ <setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"
+ proxyuser="${proxy.user}"
proxypassword="${proxy.password}" />
+ <echo message="Using ${proxy.host}:${proxy.port} to download
${sourcefile}"/>
+ </target>
+
+ <target name="testexist">
+ <echo message="Testing for ${destfile}"/>
+ <available file="${destfile}" property="exist"/>
+ </target>
+
+ <target name="downloadgz" unless="exist"
depends="setproxy,testexist">
+ <!-- Download and extract the package -->
+ <mkdir dir="${base.path}" />
+ <get src="${sourcefile}" dest="${base.path}/file.tar.gz"
/>
+ <gunzip src="${base.path}/file.tar.gz"
dest="${base.path}/file.tar"/>
+ <untar src="${base.path}/file.tar" dest="${base.path}"/>
+ <delete file="${base.path}/file.tar"/>
+ <delete file="${base.path}/file.tar.gz"/>
+ </target>
+
+ <target name="downloadfile" unless="exist"
depends="setproxy,testexist">
+ <!-- Download extract the file -->
+ <mkdir dir="${destdir}" />
+ <get src="${sourcefile}" dest="${destfile}" />
+ </target>
+
+ <target name="download" description="Builds and download dependent
components">
+ <antcall target="downloadfile">
+ <param name="sourcefile" value="${junit.jar.loc}"/>
+ <param name="destfile" value="${junit.jar}"/>
+ <param name="destdir" value="${base-junit.home}"/>
+ </antcall>
+ </target>
+
+</project>
Added: trunk/test/java/org/jboss/web/cookies/CookiesTestCase.java
===================================================================
--- trunk/test/java/org/jboss/web/cookies/CookiesTestCase.java
(rev 0)
+++ trunk/test/java/org/jboss/web/cookies/CookiesTestCase.java 2009-04-16 16:31:35 UTC
(rev 1011)
@@ -0,0 +1,191 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ * @author Jean-Frederic Clere
+ */
+
+
+package org.jboss.web.cookies;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.Socket;
+
+public class CookiesTestCase extends TestCase {
+
+ /**
+ * Construct a new instance of this test case.
+ *
+ * @param name Name of the test case
+ */
+ public CookiesTestCase(String name) {
+ super(name);
+ }
+
+ /**
+ * Set up instance variables required by this test case.
+ */
+ public void setUp() {
+ }
+
+ /**
+ * Return the tests included in this test suite.
+ */
+ public static Test suite() {
+ return (new TestSuite(CookiesTestCase.class));
+ }
+
+ /**
+ * Tear down instance variables required by this test case.
+ */
+ public void tearDown() {
+ }
+
+ /* should create foo (bar) and a (b) */
+ public void testTest1() { mytest(1, "foo=bar; a=b"); }
+ public void testTest2() { mytest(2, "foo=bar;a=b"); }
+ public void testTest3() { mytest(3, "foo=bar;a=b;"); }
+ public void testTest4() { mytest(4, "foo=bar;a=b; "); }
+ public void testTest5() { mytest(5, "foo=bar;a=b; ;"); }
+
+ /* should create foo () and a (b) */
+ public void testTest6() { mytest(6, "foo=;a=b; ;"); }
+ public void testTest7() { mytest(7, "foo;a=b; ;"); }
+
+ /* v1 create foo (bar) and a (b) */
+ public void testTest8() { mytest(8, "$Version=1; foo=bar;a=b"); }
+ public void testTest9() { mytest(9, "$Version=1;foo=bar;a=b; ; "); }
+ /* v1 create foo () and a (b) */
+ public void testTest10() { mytest(10, "$Version=1;foo=;a=b; ; "); }
+ public void testTest11() { mytest(11, "$Version=1;foo= ;a=b; ; "); }
+ public void testTest12() { mytest(12, "$Version=1;foo;a=b; ; "); }
+
+ /* v1 create foo (bar) and a (b) */
+ public void testTest13() { mytest(13, "$Version=1;foo=\"bar\";a=b; ;
"); }
+ /* use domain */
+ public void testTest14() { mytest(14,
"$Version=1;foo=\"bar\";$Domain=apache.org;a=b"); }
+ public void testTest15() { mytest(15,
"$Version=1;foo=\"bar\";$Domain=apache.org;a=b;$Domain=yahoo.com"); }
+ /* rfc2965 */
+ public void testTest16() { mytest(16,
"$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b"); }
+ // make sure these never split into two cookies - JVK
+ public void testTest17() { mytest(17,
"$Version=1;foo=\"b\"ar\";$Domain=apache.org;$Port=8080;a=b"); }
+ public void testTest18() { mytest(18,
"$Version=1;foo=\"b\\\"ar\";$Domain=apache.org;$Port=8080;a=b");
}
+ public void testTest19() { mytest(19,
"$Version=1;foo=\"b'ar\";$Domain=apache.org;$Port=8080;a=b"); }
+ // JFC: sure it is "b" and not b'ar ?
+ public void testTest20() { mytest(20,
"$Version=1;foo=b'ar;$Domain=apache.org;$Port=8080;a=b"); }
+ // Ends in quoted value
+ public void testTest21() { mytest(21, "foo=bar;a=\"b\""); }
+ public void testTest22() { mytest(22, "foo=bar;a=\"b\";"); }
+
+ // Testing bad stuff
+ public void testTest23() { mytest(23, "$Version=\"1\";
foo='bar'; $Path=/path; $Domain=\"localhost\""); }
+
+ // wrong, path should not have '/' JVK ???
+ public void testTest24() { mytest(24,
"$Version=1;foo=\"bar\";$Path=/examples;a=b; ; "); }
+ // Test name-only at the end of the header
+ public void testTest25() { mytest(25, "foo;a=b;bar"); }
+ public void testTest26() { mytest(26, "foo;a=b;bar;"); }
+ public void testTest27() { mytest(27, "foo;a=b;bar "); }
+ public void testTest28() { mytest(28, "foo;a=b;bar ;"); }
+ // BUG -- the ' ' needs to be skipped.
+ public void testTest29() { mytest(29, "foo;a=b; ;bar"); }
+ // BUG -- ';' needs skipping
+ public void testTest30() { mytest(30, "foo;a=b;;bar"); }
+ public void testTest31() { mytest(31, "foo;a=b; ;;bar=rab"); }
+ public void testTest32() { mytest(32, "foo;a=b;; ;bar=rab"); }
+
+ public void testTest33() { mytest(33, "a=b;#;bar=rab"); }
+ public void testTest34() { mytest(34, "a=b;;\\;bar=rab"); }
+
+ // Try all the separators of version1 in version0 cookie.
+ public void testTest35() { mytest(35, "a=()<>@:\\\"/[]?={}\t;
foo=bar; a=b"); }
+
+ // Just test the version.
+ public void testTest36() { mytest(36, "$Version=1;foo=bar"); }
+ public void testTest37() { mytest(37, "$Version=0;foo=bar"); }
+
+ public void mytest(int test, String cookie) {
+ try {
+ String result = Mytest(test, cookie);
+ if (result != null)
+ fail(result);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ fail("Test failed because of " + ex);
+ }
+ }
+ public String Mytest(int test, String cookie) throws Exception {
+ Socket socket = new Socket("localhost", 8080);
+ BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
+ BufferedReader reader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
+ // writer.write("GET /cookies/test.jsp HTTP/1.0");
+ writer.write("GET /myapp/test.jsp HTTP/1.0\r\n");
+ writer.write("User-Agent: CookiesTestCase/1.0\r\n");
+ writer.write("Connection: Keep-Alive\r\n");
+ writer.write("TEST: " + test + "\r\n");
+ writer.write("ACTION: CREATE\r\n");
+ writer.write("Cookie: " + cookie + "\r\n");
+ writer.write("\r\n");
+ writer.flush();
+
+ String responseStatus = reader.readLine();
+ if (responseStatus == null) {
+ return "Can't read answer" ;
+ }
+ responseStatus = responseStatus.substring(responseStatus.indexOf(' ') +
1, responseStatus.indexOf(' ', responseStatus.indexOf(' ') + 1));
+ int status = Integer.parseInt(responseStatus);
+ if (status != 200) {
+ return "Error " + status + " from Servlet";
+ }
+ // read all the headers.
+ String header = reader.readLine();
+ int contentLength = 0;
+ while (!"".equals(header)) {
+ int colon = header.indexOf(':');
+ String headerName = header.substring(0, colon).trim();
+ String headerValue = header.substring(colon + 1).trim();
+ if ("content-length".equalsIgnoreCase(headerName)) {
+ contentLength = Integer.parseInt(headerValue);
+ }
+ header = reader.readLine();
+ }
+ if (contentLength > 0) {
+ char[] buf = new char[512];
+ while (contentLength > 0) {
+ int thisTime = (contentLength > buf.length) ? buf.length :
contentLength;
+ int n = reader.read(buf, 0, thisTime);
+ if (n <= 0) {
+ return "Read content failed";
+ } else {
+ contentLength -= n;
+ }
+ }
+ }
+ return null;
+ }
+
+}
Added: trunk/test/webapps/cookies/test.jsp
===================================================================
--- trunk/test/webapps/cookies/test.jsp (rev 0)
+++ trunk/test/webapps/cookies/test.jsp 2009-04-16 16:31:35 UTC (rev 1011)
@@ -0,0 +1,153 @@
+<%@ page pageEncoding="UTF-8"%>
+<%@ page import="java.io.*"%>
+<HTML>
+<HEAD>
+<META http-equiv="Content-Type" content="text/html;
charset=UTF-8">
+<TITLE>cookies#test.</TITLE>
+</HEAD>
+<BODY>
+<%
+ /*
+ * Just read the header and process the corresponding tests
+ */
+ String test = request.getHeader("TEST");
+ String action = request.getHeader("ACTION");
+ int ntest = Integer.parseInt(test);
+
+ response.setContentType("text/html; charset=UTF-8");
+
+ if (action.compareTo("CREATE") == 0) {
+ switch (ntest) {
+ case 1: test(response, request, out, "foo", "bar",
"a", "b"); break;
+ case 2: test(response, request, out, "foo", "bar",
"a", "b"); break;
+ case 3: test(response, request, out, "foo", "bar",
"a", "b"); break;
+ case 4: test(response, request, out, "foo", "bar",
"a", "b"); break;
+ case 5: test(response, request, out, "foo", "bar",
"a", "b"); break;
+
+ case 6: test(response, request, out, "foo", "", "a",
"b"); break;
+ case 7: test(response, request, out, "foo", "", "a",
"b"); break;
+
+ case 8: test(response, request, out, "foo", "bar",
"a", "b"); break;
+ case 9: test(response, request, out, "foo", "bar",
"a", "b"); break;
+
+ case 10: test(response, request, out, "foo", "", "a",
"b"); break;
+ case 11: test(response, request, out, "foo", "", "a",
"b"); break;
+ case 12: test(response, request, out, "foo", "", "a",
"b"); break;
+
+ case 13: test(response, request, out, "foo", "bar",
"a", "b"); break;
+
+ case 14: test(response, request, out, "foo", "bar",
"a", "b"); break;
+ case 15: test(response, request, out, "foo", "bar",
"a", "b"); break;
+
+ case 16: test(response, request, out, "foo", "bar",
"a", "b"); break;
+
+ case 17: test(response, request, out, "foo", "b",
"a", "b"); break;
+ case 18: test(response, request, out, "foo", "b\"ar",
"a", "b"); break;
+ case 19: test(response, request, out, "foo", "b'ar",
"a", "b"); break;
+
+ case 20: test(response, request, out, "foo", "b",
"a", "b"); break;
+
+ case 21: test(response, request, out, "foo", "bar",
"a", "b"); break;
+ case 22: test(response, request, out, "foo", "bar",
"a", "b"); break;
+
+ case 23: test(response, request, out); break;
+
+ case 24: test(response, request, out, "foo", "bar",
"a", "b"); break;
+
+ case 25: test(response, request, out, "foo", "", "a",
"b", "bar", ""); break;
+ case 26: test(response, request, out, "foo", "", "a",
"b", "bar", ""); break;
+ case 27: test(response, request, out, "foo", "", "a",
"b", "bar", ""); break;
+ case 28: test(response, request, out, "foo", "", "a",
"b", "bar", ""); break;
+
+ case 29: test(response, request, out, "foo", "", "a",
"b", "bar", ""); break;
+ case 30: test(response, request, out, "foo", "", "a",
"b", "bar", ""); break;
+ case 31: test(response, request, out, "foo", "", "a",
"b", "bar", "rab"); break;
+ case 32: test(response, request, out, "foo", "", "a",
"b", "bar", "rab"); break;
+
+ case 33: test(response, request, out, "a", "b", "#",
"", "bar", "rab"); break;
+
+ case 34: test(response, request, out, "a", "b",
"bar", "rab"); break;
+
+ case 35: test(response, request, out, "foo", "bar",
"a", "b"); break;
+
+ case 36: test(response, request, out, 1); break;
+ case 37: test(response, request, out, 0); break;
+
+ default: response.sendError(500, "Unknown test");break;
+ }
+ } else if (action.compareTo("READ") == 0) {
+ switch (ntest) {
+ case 1: out.println("OK");break;
+ default: response.sendError(500, "Unknown test");break;
+ }
+ } else {
+ response.sendError(500, "Unknown command");
+ }
+%>
+</BODY>
+</HTML>
+<%!
+void test(HttpServletResponse response, HttpServletRequest request, JspWriter out, String
name1, String val1, String name2, String val2, String name3, String val3) throws Exception
{
+ Cookie[] cookies = request.getCookies();
+ if (cookies != null) {
+ if (cookies.length != 3) {
+ response.sendError(500, "Wrong number of cookies");
+ return;
+ }
+ if (name1.compareTo(cookies[0].getName()) == 0 &&
+ val1.compareTo(cookies[0].getValue()) == 0 &&
+ name2.compareTo(cookies[1].getName()) == 0 &&
+ val2.compareTo(cookies[1].getValue()) == 0 &&
+ name3.compareTo(cookies[2].getName()) == 0 &&
+ val3.compareTo(cookies[2].getValue()) == 0 )
+ out.println("OK");
+ else {
+ response.sendError(500, "Value or name don't match");
+ return;
+ }
+ } else {
+ response.sendError(500, "No cookies");
+ }
+ }
+void test(HttpServletResponse response, HttpServletRequest request, JspWriter out, String
name1, String val1, String name2, String val2) throws Exception {
+ Cookie[] cookies = request.getCookies();
+ if (cookies != null) {
+ if (cookies.length != 2) {
+ response.sendError(500, "Wrong number of cookies");
+ return;
+ }
+ if (name1.compareTo(cookies[0].getName()) == 0 &&
+ val1.compareTo(cookies[0].getValue()) == 0 &&
+ name2.compareTo(cookies[1].getName()) == 0 &&
+ val2.compareTo(cookies[1].getValue()) == 0)
+ out.println("OK");
+ else {
+ response.sendError(500, "Value or name don't match");
+ return;
+ }
+ } else {
+ response.sendError(500, "No cookies");
+ }
+ }
+void test(HttpServletResponse response, HttpServletRequest request, JspWriter out) throws
Exception {
+ Cookie[] cookies = request.getCookies();
+ if (cookies != null) {
+ if (cookies.length != 0) {
+ response.sendError(500, "Wrong number of cookies");
+ return;
+ }
+ }
+ out.println("OK");
+ }
+void test(HttpServletResponse response, HttpServletRequest request, JspWriter out, int
version) throws Exception {
+ Cookie[] cookies = request.getCookies();
+ if (cookies != null) {
+ if (cookies.length == 1) {
+ if (cookies[0].getVersion() == version) {
+ out.println("OK");
+ return;
+ }
+ }
+ }
+ response.sendError(500, "Wrong number of cookies");
+ }%>