~james-page/ubuntu/natty/tomcat6/fix-662588

« back to all changes in this revision

Viewing changes to test/org/apache/tomcat/util/http/TestCookies.java

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Claude, Ludovic Claude, Jason Brittain
  • Date: 2010-02-09 23:06:51 UTC
  • mfrom: (2.2.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100209230651-qiv9397g7txhrh99
Tags: 6.0.24-1
[ Ludovic Claude ]
* New upstream version
* Update the POM files for the new version of Tomcat
* Bump up Standards-Version to 3.8.4
* Refresh patches deploy-webapps-build-xml.patch and var_loaders.patch
* Remove patch fix_context_name.patch as it has been applied upstream
* Fix the installation of servlet-api-2.5.jar: the jar
  goes to /usr/share/java as in older versions (6.0.20-2)
  and links to the jar are added to /usr/share/maven-repo
* Moved NEWS.Debian into README.Debian
* Add a link from /usr/share/doc/tomcat6-common/README.Debian to
  /usr/share/doc/tomcat6/README.Debian to include a minimum of
  documentation in the tomcat6 package and add some useful notes. 
  (Closes: #563937, #563939)
* Remove poms from the Debian packaging, use upstream pom files

[ Jason Brittain ]
* Fixed a bug in the init script: When a start fails, the PID file was
  being left in place.  Now the init script makes sure it is deleted.
* Fixed a packaging bug that results in the ROOT webapp not being properly
  installed after an uninstall, then a reinstall.
* control: Corrected a couple of comments (no functional change).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 *  contributor license agreements.  See the NOTICE file distributed with
 
4
 *  this work for additional information regarding copyright ownership.
 
5
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 *  (the "License"); you may not use this file except in compliance with
 
7
 *  the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *  Unless required by applicable law or agreed to in writing, software
 
12
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *  See the License for the specific language governing permissions and
 
15
 *  limitations under the License.
 
16
 */
 
17
 
 
18
package org.apache.tomcat.util.http; 
 
19
 
 
20
import org.apache.tomcat.util.http.Cookies;
 
21
import org.apache.tomcat.util.http.ServerCookie;
 
22
 
 
23
import junit.framework.Test;
 
24
import junit.framework.TestCase;
 
25
import junit.framework.TestSuite;
 
26
import junit.textui.TestRunner;
 
27
 
 
28
import java.lang.Exception;
 
29
 
 
30
 
 
31
public class TestCookies extends TestCase {
 
32
    public static void main( String args[] ) {
 
33
       TestRunner.run(suite());
 
34
    }
 
35
    public static Test suite() {
 
36
       TestSuite suite = new TestSuite();
 
37
       suite.addTest(new TestSuite(TestCookies.class));
 
38
       return suite;
 
39
    }
 
40
/*
 
41
       int i = 10000000;
 
42
          // These tests are not really representative 
 
43
         while (i-- > 0) { 
 
44
             test("session=1234567890;name=\"John Q. Public\";");
 
45
        }
 
46
//        runtests();
 
47
    } 
 
48
 */
 
49
    
 
50
    public void testCookies() throws Exception {
 
51
        test("foo=bar; a=b", "foo", "bar", "a", "b");
 
52
        test("foo=bar;a=b", "foo", "bar", "a", "b");
 
53
        test("foo=bar;a=b;", "foo", "bar", "a", "b");
 
54
        test("foo=bar;a=b; ", "foo", "bar", "a", "b");
 
55
        test("foo=bar;a=b; ;", "foo", "bar", "a", "b");
 
56
        test("foo=;a=b; ;", "foo", "", "a", "b");
 
57
        test("foo;a=b; ;", "foo", "", "a", "b");
 
58
        // v1 
 
59
        test("$Version=1; foo=bar;a=b", "foo", "bar", "a", "b"); 
 
60
 
 
61
        // OK
 
62
        test("$Version=1;foo=bar;a=b; ; ",  "foo", "bar", "a", "b");
 
63
        test("$Version=1;foo=;a=b; ; ",  "foo", "", "a", "b");
 
64
        test("$Version=1;foo= ;a=b; ; ",  "foo", "", "a", "b");
 
65
        test("$Version=1;foo;a=b; ; ", "foo", "", "a", "b");
 
66
        test("$Version=1;foo=\"bar\";a=b; ; ", "foo", "bar", "a", "b");
 
67
 
 
68
        test("$Version=1;foo=\"bar\";$Domain=apache.org;a=b", "foo", "bar", "a", "b");
 
69
        test("$Version=1;foo=\"bar\";$Domain=apache.org;a=b;$Domain=yahoo.com", "foo", "bar", "a", "b");
 
70
        // rfc2965
 
71
        test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b", "foo", "bar", "a", "b");
 
72
 
 
73
        // make sure these never split into two cookies - JVK
 
74
        test("$Version=1;foo=\"b\"ar\";$Domain=apache.org;$Port=8080;a=b",  "foo", "b", "a", "b"); // Incorrectly escaped.
 
75
        test("$Version=1;foo=\"b\\\"ar\";$Domain=apache.org;$Port=8080;a=b", "foo", "b\"ar", "a", "b"); // correctly escaped.
 
76
        test("$Version=1;foo=\"b'ar\";$Domain=apache.org;$Port=8080;a=b", "foo", "b'ar", "a", "b");
 
77
        // ba'r is OK - ' is not a separator
 
78
        test("$Version=1;foo=b'ar;$Domain=apache.org;$Port=8080;a=b", "foo", "b'ar", "a", "b");
 
79
 
 
80
        // Ends in quoted value
 
81
        test("foo=bar;a=\"b\"",  "foo", "bar", "a", "b");
 
82
        test("foo=bar;a=\"b\";",  "foo", "bar", "a", "b");
 
83
 
 
84
        // Last character is an escape character
 
85
        test("$Version=1;foo=b'ar;$Domain=\"apache.org\";$Port=8080;a=\"b\\\"", "foo", "b'ar");
 
86
        test("$Version=1;foo=b'ar;$Domain=\"apache.org\";$Port=8080;a=\"b\\",  "foo", "b'ar");
 
87
        
 
88
        // A token cannot be quoted with ' chars - they should be treated as part of the value
 
89
        test("$Version=\"1\"; foo='bar'; $Path=/path; $Domain=\"localhost\"", "foo", "'bar'");
 
90
    
 
91
        // wrong, path should not have '/' JVK
 
92
        test("$Version=1;foo=\"bar\";$Path=/examples;a=b; ; ", "foo", "bar", "a", "b");
 
93
 
 
94
        // wrong
 
95
        test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b", "foo", "bar", "a", "b");
 
96
 
 
97
        // Test name-only at the end of the header
 
98
        test("foo;a=b;bar", "foo", "", "a", "b", "bar", "");
 
99
        test("foo;a=b;bar;", "foo", "", "a", "b", "bar", "");
 
100
        test("foo;a=b;bar ", "foo", "", "a", "b", "bar", "");
 
101
        test("foo;a=b;bar ;", "foo", "", "a", "b", "bar", "");
 
102
 
 
103
        // Multiple delimiters next to each other
 
104
 
 
105
        // BUG -- the ' ' needs to be skipped.
 
106
        test("foo;a=b; ;bar", "foo", "", "a", "b", "bar", "");
 
107
        // BUG -- ';' needs skipping
 
108
        test("foo;a=b;;bar", "foo", "", "a", "b", "bar", "");
 
109
        test("foo;a=b; ;;bar=rab", "foo", "", "a", "b", "bar", "rab");
 
110
        // These pass currently
 
111
        test("foo;a=b;; ;bar=rab", "foo", "", "a", "b", "bar", "rab");
 
112
 
 
113
        // '#' is a valid cookie name (not a separator)
 
114
        test("foo;a=b;;#;bar=rab","foo", "", "a", "b", "#", "", "bar", "rab");
 
115
 
 
116
        
 
117
        test("foo;a=b;;\\;bar=rab", "foo", "", "a", "b", "bar", "rab");
 
118
 
 
119
        // Try all the separators of version1 in version0 cookie.
 
120
        // Won't work we only parse version1 cookie result 1 cookie.
 
121
        test("a=()<>@:\\\"/[]?={}\t; foo=bar", "foo", "bar");
 
122
 
 
123
        // Test the version.
 
124
        test("$Version=1;foo=bar", 1);
 
125
        test("$Version=0;foo=bar", 0);
 
126
    }
 
127
 
 
128
    public static void test( String s, int val ) throws Exception {
 
129
        System.out.println("Processing [" + s + "]");
 
130
        Cookies cs=new Cookies(null);
 
131
        cs.processCookieHeader( s.getBytes(), 0, s.length());
 
132
        int num = cs.getCookieCount();
 
133
        if (num != 1)
 
134
          throw new Exception("wrong number of cookies " + num);
 
135
        ServerCookie co = cs.getCookie(0);
 
136
        System.out.println("One Cookie: " + co);
 
137
        if (co.getVersion() != val)
 
138
          throw new Exception("wrong version " + co.getVersion() + " != " + val);
 
139
    }
 
140
    public static void test( String s ) throws Exception {
 
141
        System.out.println("Processing [" + s + "]");
 
142
        Cookies cs=new Cookies(null);
 
143
        cs.processCookieHeader( s.getBytes(), 0, s.length());
 
144
 
 
145
        int num = cs.getCookieCount();
 
146
        for( int i=0; i< num ; i++ ) {
 
147
            System.out.println("Cookie: " + cs.getCookie( i ));
 
148
        }
 
149
        if (num != 0)
 
150
          throw new Exception("wrong number of cookies " + num);
 
151
    }
 
152
    public static void test( String s, String name, String val ) throws Exception {
 
153
        System.out.println("Processing [" + s + "]");
 
154
        Cookies cs=new Cookies(null);
 
155
        cs.processCookieHeader( s.getBytes(), 0, s.length());
 
156
 
 
157
        int num = cs.getCookieCount();
 
158
        if (num != 1)
 
159
          throw new Exception("wrong number of cookies " + num);
 
160
        ServerCookie co = cs.getCookie(0);
 
161
        System.out.println("One Cookie: " + co);
 
162
        String coname = co.getName().toString();
 
163
        String coval  = co.getValue().toString();
 
164
        if ( ! name.equals(coname))
 
165
          throw new Exception("wrong name " + coname + " != " + name);
 
166
        if ( ! val.equals(coval))
 
167
          throw new Exception("wrong value " + coval + " != " + val);
 
168
    }
 
169
    public static void test( String s, String name, String val, String name2, String val2 ) throws Exception {
 
170
        System.out.println("Processing [" + s + "]");
 
171
        Cookies cs=new Cookies(null);
 
172
        cs.processCookieHeader( s.getBytes(), 0, s.length());
 
173
 
 
174
        int num = cs.getCookieCount();
 
175
        if (num != 2)
 
176
          throw new Exception("wrong number of cookies " + num);
 
177
        ServerCookie co = cs.getCookie(0);
 
178
        System.out.println("1 - Cookie: " + co);
 
179
        ServerCookie co2 = cs.getCookie(1);
 
180
        System.out.println("2 - Cookie: " + co2);
 
181
 
 
182
        String coname = co.getName().toString();
 
183
        String coval  = co.getValue().toString();
 
184
        if ( ! name.equals(coname))
 
185
          throw new Exception("1 - wrong name " + coname + " != " + name);
 
186
        if ( ! val.equals(coval))
 
187
          throw new Exception("1 - wrong value " + coval + " != " + val);
 
188
 
 
189
        String coname2 = co2.getName().toString();
 
190
        String coval2  = co2.getValue().toString();
 
191
        if ( ! name2.equals(coname2))
 
192
          throw new Exception("2 - wrong name " + coname2 + " != " + name2);
 
193
        if ( ! val2.equals(coval2))
 
194
          throw new Exception("2 - wrong value " + coval2 + " != " + val2);
 
195
    }
 
196
    public static void test( String s, String name, String val, String name2,
 
197
                             String val2, String name3, String val3 ) throws Exception {
 
198
        System.out.println("Processing [" + s + "]");
 
199
        Cookies cs=new Cookies(null);
 
200
        cs.processCookieHeader( s.getBytes(), 0, s.length());
 
201
 
 
202
        int num = cs.getCookieCount();
 
203
        if (num != 3)
 
204
          throw new Exception("wrong number of cookies " + num);
 
205
        ServerCookie co = cs.getCookie(0);
 
206
        System.out.println("1 - Cookie: " + co);
 
207
        ServerCookie co2 = cs.getCookie(1);
 
208
        System.out.println("2 - Cookie: " + co2);
 
209
        ServerCookie co3 = cs.getCookie(2);
 
210
        System.out.println("3 - Cookie: " + co3);
 
211
 
 
212
        String coname = co.getName().toString();
 
213
        String coval  = co.getValue().toString();
 
214
        if ( ! name.equals(coname))
 
215
          throw new Exception("1 - wrong name " + coname + " != " + name);
 
216
        if ( ! val.equals(coval))
 
217
          throw new Exception("1 - wrong value " + coval + " != " + val);
 
218
 
 
219
        String coname2 = co2.getName().toString();
 
220
        String coval2  = co2.getValue().toString();
 
221
        if ( ! name2.equals(coname2))
 
222
          throw new Exception("2 - wrong name " + coname2 + " != " + name2);
 
223
        if ( ! val2.equals(coval2))
 
224
          throw new Exception("2 - wrong value " + coval2 + " != " + val2);
 
225
 
 
226
        String coname3 = co3.getName().toString();
 
227
        String coval3  = co3.getValue().toString();
 
228
        if ( ! name3.equals(coname3))
 
229
          throw new Exception("3 - wrong name " + coname3 + " != " + name3);
 
230
        if ( ! val2.equals(coval2))
 
231
          throw new Exception("3 - wrong value " + coval3 + " != " + val3);
 
232
    }
 
233
    public static void test( String s, String name, String val, String name2,
 
234
                             String val2, String name3, String val3,
 
235
                             String name4, String val4 ) throws Exception {
 
236
        System.out.println("Processing [" + s + "]");
 
237
        Cookies cs=new Cookies(null);
 
238
        cs.processCookieHeader( s.getBytes(), 0, s.length());
 
239
 
 
240
        int num = cs.getCookieCount();
 
241
        if (num != 4)
 
242
          throw new Exception("wrong number of cookies " + num);
 
243
        ServerCookie co = cs.getCookie(0);
 
244
        System.out.println("1 - Cookie: " + co);
 
245
        ServerCookie co2 = cs.getCookie(1);
 
246
        System.out.println("2 - Cookie: " + co2);
 
247
        ServerCookie co3 = cs.getCookie(2);
 
248
        System.out.println("3 - Cookie: " + co3);
 
249
        ServerCookie co4 = cs.getCookie(3);
 
250
        System.out.println("4 - Cookie: " + co4);
 
251
 
 
252
        String coname = co.getName().toString();
 
253
        String coval  = co.getValue().toString();
 
254
        if ( ! name.equals(coname))
 
255
          throw new Exception("1 - wrong name " + coname + " != " + name);
 
256
        if ( ! val.equals(coval))
 
257
          throw new Exception("1 - wrong value " + coval + " != " + val);
 
258
 
 
259
        String coname2 = co2.getName().toString();
 
260
        String coval2  = co2.getValue().toString();
 
261
        if ( ! name2.equals(coname2))
 
262
          throw new Exception("2 - wrong name " + coname2 + " != " + name2);
 
263
        if ( ! val2.equals(coval2))
 
264
          throw new Exception("2 - wrong value " + coval2 + " != " + val2);
 
265
 
 
266
        String coname3 = co3.getName().toString();
 
267
        String coval3  = co3.getValue().toString();
 
268
        if ( ! name3.equals(coname3))
 
269
          throw new Exception("3 - wrong name " + coname3 + " != " + name3);
 
270
        if ( ! val3.equals(coval3))
 
271
          throw new Exception("3 - wrong value " + coval3 + " != " + val3);
 
272
 
 
273
        String coname4 = co4.getName().toString();
 
274
        String coval4  = co4.getValue().toString();
 
275
        if ( ! name4.equals(coname4))
 
276
          throw new Exception("4 - wrong name " + coname4 + " != " + name4);
 
277
        if ( ! val4.equals(coval4))
 
278
          throw new Exception("4 - wrong value " + coval4 + " != " + val4);
 
279
    }
 
280
}