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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.tomcat.util.http;
20
import org.apache.tomcat.util.http.Cookies;
21
import org.apache.tomcat.util.http.ServerCookie;
23
import junit.framework.Test;
24
import junit.framework.TestCase;
25
import junit.framework.TestSuite;
26
import junit.textui.TestRunner;
28
import java.lang.Exception;
31
public class TestCookies extends TestCase {
32
public static void main( String args[] ) {
33
TestRunner.run(suite());
35
public static Test suite() {
36
TestSuite suite = new TestSuite();
37
suite.addTest(new TestSuite(TestCookies.class));
42
// These tests are not really representative
44
test("session=1234567890;name=\"John Q. Public\";");
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");
59
test("$Version=1; foo=bar;a=b", "foo", "bar", "a", "b");
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");
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");
71
test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b", "foo", "bar", "a", "b");
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");
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");
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");
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'");
91
// wrong, path should not have '/' JVK
92
test("$Version=1;foo=\"bar\";$Path=/examples;a=b; ; ", "foo", "bar", "a", "b");
95
test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b", "foo", "bar", "a", "b");
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", "");
103
// Multiple delimiters next to each other
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");
113
// '#' is a valid cookie name (not a separator)
114
test("foo;a=b;;#;bar=rab","foo", "", "a", "b", "#", "", "bar", "rab");
117
test("foo;a=b;;\\;bar=rab", "foo", "", "a", "b", "bar", "rab");
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");
124
test("$Version=1;foo=bar", 1);
125
test("$Version=0;foo=bar", 0);
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();
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);
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());
145
int num = cs.getCookieCount();
146
for( int i=0; i< num ; i++ ) {
147
System.out.println("Cookie: " + cs.getCookie( i ));
150
throw new Exception("wrong number of cookies " + num);
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());
157
int num = cs.getCookieCount();
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);
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());
174
int num = cs.getCookieCount();
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);
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);
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);
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());
202
int num = cs.getCookieCount();
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);
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);
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);
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);
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());
240
int num = cs.getCookieCount();
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);
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);
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);
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);
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);