From b13c997af51a173f61f6093eec5e7799f70abbef Mon Sep 17 00:00:00 2001 From: jfclere Date: Fri, 26 Oct 2007 14:51:23 +0000 Subject: [PATCH] Add the tests of the cookies. git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@588673 13f79535-47bb-0310-9956-ffa450edef68 --- STATUS | 4 - test/build.xml | 69 ++++++ .../catalina/tomcat/util/http/TestCookies.java | 258 +++++++++++++++++++++ webapps/docs/changelog.xml | 7 + 4 files changed, 334 insertions(+), 4 deletions(-) create mode 100644 test/build.xml create mode 100644 test/org/apache/catalina/tomcat/util/http/TestCookies.java diff --git a/STATUS b/STATUS index 5a9a98647..260c350a9 100644 --- a/STATUS +++ b/STATUS @@ -26,10 +26,6 @@ PATCHES PROPOSED TO BACKPORT: [ New proposals should be added at the end of the list ] -* Tests for unit tests for the cookie issues. http://people.apache.org/~jfclere/patches/CookiesTest.patch - +1: fhanik, funkman, pero, jim - -1: - * Guess java location from the PATH environment. http://people.apache.org/~jfclere/patches/setclasspath.sh.patch And improve fix for 37284. +1: fhanik, remm, funkman, pero, jim diff --git a/test/build.xml b/test/build.xml new file mode 100644 index 000000000..54a017282 --- /dev/null +++ b/test/build.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/org/apache/catalina/tomcat/util/http/TestCookies.java b/test/org/apache/catalina/tomcat/util/http/TestCookies.java new file mode 100644 index 000000000..4feb0accd --- /dev/null +++ b/test/org/apache/catalina/tomcat/util/http/TestCookies.java @@ -0,0 +1,258 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.tomcat.util.http.Cookies; +import org.apache.tomcat.util.http.ServerCookie; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +import java.lang.Exception; + + +public class TestCookies extends TestCase { + public static void main( String args[] ) { + TestRunner.run(suite()); + } + public static Test suite() { + TestSuite suite = new TestSuite(); + suite.addTest(new TestSuite(TestCookies.class)); + return suite; + } +/* + int i = 10000000; + // These tests are not really representative + while (i-- > 0) { + test("session=1234567890;name=\"John Q. Public\";"); + } +// runtests(); + } + */ + + public void testCookies() throws Exception { + test("foo=bar; a=b", "foo", "bar", "a", "b"); + test("foo=bar;a=b", "foo", "bar", "a", "b"); + test("foo=bar;a=b;", "foo", "bar", "a", "b"); + test("foo=bar;a=b; ", "foo", "bar", "a", "b"); + test("foo=bar;a=b; ;", "foo", "bar", "a", "b"); + test("foo=;a=b; ;", "foo", "", "a", "b"); + test("foo;a=b; ;", "foo", "", "a", "b"); + // v1 + test("$Version=1; foo=bar;a=b", "foo", "bar", "a", "b"); + + // OK + test("$Version=1;foo=bar;a=b; ; ", "foo", "bar", "a", "b"); + test("$Version=1;foo=;a=b; ; ", "foo", "", "a", "b"); + test("$Version=1;foo= ;a=b; ; ", "foo", "", "a", "b"); + test("$Version=1;foo;a=b; ; ", "foo", "", "a", "b"); + test("$Version=1;foo=\"bar\";a=b; ; ", "foo", "bar", "a", "b"); + + test("$Version=1;foo=\"bar\";$Domain=apache.org;a=b", "foo", "bar", "a", "b"); + test("$Version=1;foo=\"bar\";$Domain=apache.org;a=b;$Domain=yahoo.com", "foo", "bar", "a", "b"); + // rfc2965 + test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b", "foo", "bar", "a", "b"); + + // make sure these never split into two cookies - JVK + test("$Version=1;foo=\"b\"ar\";$Domain=apache.org;$Port=8080;a=b", "foo", "b", "a", "b"); + test("$Version=1;foo=\"b\\\"ar\";$Domain=apache.org;$Port=8080;a=b", "foo", "b\\\"ar", "a", "b"); + test("$Version=1;foo=\"b'ar\";$Domain=apache.org;$Port=8080;a=b", "foo", "b'ar", "a", "b"); + // JFC: sure it is "b" and not b'ar ? + test("$Version=1;foo=b'ar;$Domain=apache.org;$Port=8080;a=b", "foo", "b", "a", "b"); + + // Ends in quoted value + test("foo=bar;a=\"b\"", "foo", "bar", "a", "b"); + test("foo=bar;a=\"b\";", "foo", "bar", "a", "b"); + + // Last character is an escape character + test("$Version=1;foo=b'ar;$Domain=\"apache.org\";$Port=8080;a=\"b\\\"", "foo", "b"); + test("$Version=1;foo=b'ar;$Domain=\"apache.org\";$Port=8080;a=\"b\\", "foo", "b"); + + // Bad... a token cannot be quoted with ' chars + test("$Version=\"1\"; foo='bar'; $Path=/path; $Domain=\"localhost\""); + + // wrong, path should not have '/' JVK + test("$Version=1;foo=\"bar\";$Path=/examples;a=b; ; ", "foo", "bar", "a", "b"); + + // wrong + test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b", "foo", "bar", "a", "b"); + + // Test name-only at the end of the header + test("foo;a=b;bar", "foo", "", "a", "b", "bar", ""); + test("foo;a=b;bar;", "foo", "", "a", "b", "bar", ""); + test("foo;a=b;bar ", "foo", "", "a", "b", "bar", ""); + test("foo;a=b;bar ;", "foo", "", "a", "b", "bar", ""); + + // Multiple delimiters next to each other + + // BUG -- the ' ' needs to be skipped. + test("foo;a=b; ;bar", "foo", "", "a", "b", "bar", ""); + // BUG -- ';' needs skipping + test("foo;a=b;;bar", "foo", "", "a", "b", "bar", ""); + test("foo;a=b; ;;bar=rab", "foo", "", "a", "b", "bar", "rab"); + // These pass currently + test("foo;a=b;; ;bar=rab", "foo", "", "a", "b", "bar", "rab"); + + // '#' is a valid cookie name (not a separator) + test("foo;a=b;;#;bar=rab","foo", "", "a", "b", "#", "", "bar", "rab"); + + + test("foo;a=b;;\\;bar=rab", "foo", "", "a", "b", "bar", "rab"); + } + + public static void test( String s ) throws Exception { + System.out.println("Processing [" + s + "]"); + Cookies cs=new Cookies(null); + cs.processCookieHeader( s.getBytes(), 0, s.length()); + + int num = cs.getCookieCount(); + for( int i=0; i< num ; i++ ) { + System.out.println("Cookie: " + cs.getCookie( i )); + } + if (num != 0) + throw new Exception("wrong number of cookies " + num); + } + public static void test( String s, String name, String val ) throws Exception { + System.out.println("Processing [" + s + "]"); + Cookies cs=new Cookies(null); + cs.processCookieHeader( s.getBytes(), 0, s.length()); + + int num = cs.getCookieCount(); + if (num != 1) + throw new Exception("wrong number of cookies " + num); + ServerCookie co = cs.getCookie(0); + System.out.println("One Cookie: " + co); + String coname = co.getName().toString(); + String coval = co.getValue().toString(); + if ( ! name.equals(coname)) + throw new Exception("wrong name " + coname + " != " + name); + if ( ! val.equals(coval)) + throw new Exception("wrong value " + coval + " != " + val); + } + public static void test( String s, String name, String val, String name2, String val2 ) throws Exception { + System.out.println("Processing [" + s + "]"); + Cookies cs=new Cookies(null); + cs.processCookieHeader( s.getBytes(), 0, s.length()); + + int num = cs.getCookieCount(); + if (num != 2) + throw new Exception("wrong number of cookies " + num); + ServerCookie co = cs.getCookie(0); + System.out.println("1 - Cookie: " + co); + ServerCookie co2 = cs.getCookie(1); + System.out.println("2 - Cookie: " + co2); + + String coname = co.getName().toString(); + String coval = co.getValue().toString(); + if ( ! name.equals(coname)) + throw new Exception("1 - wrong name " + coname + " != " + name); + if ( ! val.equals(coval)) + throw new Exception("1 - wrong value " + coval + " != " + val); + + String coname2 = co2.getName().toString(); + String coval2 = co2.getValue().toString(); + if ( ! name2.equals(coname2)) + throw new Exception("2 - wrong name " + coname2 + " != " + name2); + if ( ! val2.equals(coval2)) + throw new Exception("2 - wrong value " + coval2 + " != " + val2); + } + public static void test( String s, String name, String val, String name2, + String val2, String name3, String val3 ) throws Exception { + System.out.println("Processing [" + s + "]"); + Cookies cs=new Cookies(null); + cs.processCookieHeader( s.getBytes(), 0, s.length()); + + int num = cs.getCookieCount(); + if (num != 3) + throw new Exception("wrong number of cookies " + num); + ServerCookie co = cs.getCookie(0); + System.out.println("1 - Cookie: " + co); + ServerCookie co2 = cs.getCookie(1); + System.out.println("2 - Cookie: " + co2); + ServerCookie co3 = cs.getCookie(2); + System.out.println("3 - Cookie: " + co3); + + String coname = co.getName().toString(); + String coval = co.getValue().toString(); + if ( ! name.equals(coname)) + throw new Exception("1 - wrong name " + coname + " != " + name); + if ( ! val.equals(coval)) + throw new Exception("1 - wrong value " + coval + " != " + val); + + String coname2 = co2.getName().toString(); + String coval2 = co2.getValue().toString(); + if ( ! name2.equals(coname2)) + throw new Exception("2 - wrong name " + coname2 + " != " + name2); + if ( ! val2.equals(coval2)) + throw new Exception("2 - wrong value " + coval2 + " != " + val2); + + String coname3 = co3.getName().toString(); + String coval3 = co3.getValue().toString(); + if ( ! name3.equals(coname3)) + throw new Exception("3 - wrong name " + coname3 + " != " + name3); + if ( ! val2.equals(coval2)) + throw new Exception("3 - wrong value " + coval3 + " != " + val3); + } + public static void test( String s, String name, String val, String name2, + String val2, String name3, String val3, + String name4, String val4 ) throws Exception { + System.out.println("Processing [" + s + "]"); + Cookies cs=new Cookies(null); + cs.processCookieHeader( s.getBytes(), 0, s.length()); + + int num = cs.getCookieCount(); + if (num != 4) + throw new Exception("wrong number of cookies " + num); + ServerCookie co = cs.getCookie(0); + System.out.println("1 - Cookie: " + co); + ServerCookie co2 = cs.getCookie(1); + System.out.println("2 - Cookie: " + co2); + ServerCookie co3 = cs.getCookie(2); + System.out.println("3 - Cookie: " + co3); + ServerCookie co4 = cs.getCookie(3); + System.out.println("4 - Cookie: " + co4); + + String coname = co.getName().toString(); + String coval = co.getValue().toString(); + if ( ! name.equals(coname)) + throw new Exception("1 - wrong name " + coname + " != " + name); + if ( ! val.equals(coval)) + throw new Exception("1 - wrong value " + coval + " != " + val); + + String coname2 = co2.getName().toString(); + String coval2 = co2.getValue().toString(); + if ( ! name2.equals(coname2)) + throw new Exception("2 - wrong name " + coname2 + " != " + name2); + if ( ! val2.equals(coval2)) + throw new Exception("2 - wrong value " + coval2 + " != " + val2); + + String coname3 = co3.getName().toString(); + String coval3 = co3.getValue().toString(); + if ( ! name3.equals(coname3)) + throw new Exception("3 - wrong name " + coname3 + " != " + name3); + if ( ! val3.equals(coval3)) + throw new Exception("3 - wrong value " + coval3 + " != " + val3); + + String coname4 = co4.getName().toString(); + String coval4 = co4.getValue().toString(); + if ( ! name4.equals(coname4)) + throw new Exception("4 - wrong name " + coname4 + " != " + name4); + if ( ! val4.equals(coval4)) + throw new Exception("4 - wrong value " + coval4 + " != " + val4); + } +} diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 952d1b87f..103ff2947 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -197,6 +197,13 @@ + + + + Tests for unit tests for the cookie issues. (jfclere) + + +
-- 2.11.0