This commit is contained in:
2024-11-30 19:03:49 +08:00
commit 1e6763c160
3806 changed files with 737676 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
/*
* 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.
*/
package org.apache.tomcat.util;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.core.StandardContext;
public class TestIntrospectionUtils {
// Test for all the classes and interfaces in StandardContext's type hierarchy
@Test
public void testIsInstanceStandardContext01() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.core.StandardContext"));
}
@Test
public void testIsInstanceStandardContext02() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.util.LifecycleMBeanBase"));
}
@Test
public void testIsInstanceStandardContext03() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.util.LifecycleBase"));
}
@Test
public void testIsInstanceStandardContext04() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "java.lang.Object"));
}
@Test
public void testIsInstanceStandardContext05() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.Lifecycle"));
}
@Test
public void testIsInstanceStandardContext06() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.JmxEnabled"));
}
@Test
public void testIsInstanceStandardContext07() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "javax.management.MBeanRegistration"));
}
@Test
public void testIsInstanceStandardContext08() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.Container"));
}
@Test
public void testIsInstanceStandardContext09() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.tomcat.ContextBind"));
}
@Test
public void testIsInstanceStandardContext10() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "javax.management.NotificationEmitter"));
}
@Test
public void testIsInstanceStandardContext11() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "javax.management.NotificationBroadcaster"));
}
// And one to check that non-matches return false
@Test
public void testIsInstanceStandardContext12() {
Assert.assertFalse(IntrospectionUtils.isInstance(
StandardContext.class, "com.example.Other"));
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.
*/
package org.apache.tomcat.util.bcel;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.Jar;
import org.apache.tomcat.util.bcel.classfile.ClassParser;
import org.apache.tomcat.util.scan.JarFactory;
public class TesterPerformance {
private static final String JAR_LOCATION = "/tmp/jira-libs";
@Test
public void testClassParserPerformance() throws IOException {
File libDir = new File(JAR_LOCATION);
String[] libs = libDir.list();
Assert.assertNotNull(libs);
Set<URL> jarURLs = new HashSet<>();
for (String lib : libs) {
if (!lib.toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
continue;
}
jarURLs.add(new URL("jar:" + new File (libDir, lib).toURI().toURL().toExternalForm() + "!/"));
}
long duration = 0;
for (URL jarURL : jarURLs) {
try (Jar jar = JarFactory.newInstance(jarURL)) {
jar.nextEntry();
String jarEntryName = jar.getEntryName();
while (jarEntryName != null) {
if (jarEntryName.endsWith(".class")) {
InputStream is = jar.getEntryInputStream();
long start = System.nanoTime();
ClassParser cp = new ClassParser(is);
cp.parse();
duration += System.nanoTime() - start;
}
jar.nextEntry();
jarEntryName = jar.getEntryName();
}
}
}
System.out.println("ClassParser performance test took: " + duration + " ns");
}
}

View File

@@ -0,0 +1,65 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
public class TestAscii {
@Test
public void testParseLong1() {
String value = "9223372036854775807"; // Long.MAX_VALUE
byte[] bytes = value.getBytes();
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.assertEquals(value, String.valueOf(result));
}
@Test(expected = NumberFormatException.class)
public void testParseLong2() {
byte[] bytes = "9223372036854775808".getBytes(); // Long.MAX_VALUE + 1
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.fail("NumberFormatException expected, got: " + result);
}
@Test(expected = NumberFormatException.class)
public void testParseLong3() {
byte[] bytes = "9223372036854775810".getBytes(); // Long.MAX_VALUE + 3
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.fail("NumberFormatException expected, got: " + result);
}
@Test(expected = NumberFormatException.class)
public void testParseLong4() {
BigInteger x = BigInteger.valueOf(5000000000L).shiftLeft(32);
byte[] bytes = String.valueOf(x).getBytes();
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.fail("NumberFormatException expected, got: " + result);
}
@Test
public void testParseLong5() {
String value = "9223372036854775806"; // Long.MAX_VALUE - 1
byte[] bytes = value.getBytes();
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.assertEquals(value, String.valueOf(result));
}
}

View File

@@ -0,0 +1,143 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.nio.charset.Charset;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import org.junit.Assert;
import org.junit.Test;
public class TestB2CConverter {
private static final byte[] UTF16_MESSAGE =
new byte[] {-2, -1, 0, 65, 0, 66, 0, 67};
private static final byte[] UTF8_INVALID = new byte[] {-8, -69, -73, -77};
private static final byte[] UTF8_PARTIAL = new byte[] {-50};
@Test
public void testSingleMessage() throws Exception {
testMessages(1);
}
@Test
public void testTwoMessage() throws Exception {
testMessages(2);
}
@Test
public void testManyMessage() throws Exception {
testMessages(10);
}
private void testMessages(int msgCount) throws Exception {
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_16);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk(32);
for (int i = 0; i < msgCount; i++) {
bc.append(UTF16_MESSAGE, 0, UTF16_MESSAGE.length);
conv.convert(bc, cc, true);
Assert.assertEquals("ABC", cc.toString());
bc.recycle();
cc.recycle();
conv.recycle();
}
System.out.println(cc);
}
@Test
public void testLeftoverSize() {
float maxLeftover = 0;
String charsetName = "UNSET";
for (Charset charset : Charset.availableCharsets().values()) {
float leftover;
if (charset.name().toLowerCase(Locale.ENGLISH).startsWith("x-")) {
// Non-standard charset that browsers won't be using
// Likely something used internally by the JRE
continue;
}
try {
leftover = charset.newEncoder().maxBytesPerChar();
} catch (UnsupportedOperationException uoe) {
// Skip it
continue;
}
if (leftover > maxLeftover) {
maxLeftover = leftover;
charsetName = charset.name();
}
}
Assert.assertTrue("Limit needs to be at least " + maxLeftover +
" (used in charset '" + charsetName + "')",
maxLeftover <= B2CConverter.LEFTOVER_SIZE);
}
@Test(expected=MalformedInputException.class)
public void testBug54602a() throws Exception {
// Check invalid input is rejected straight away
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk();
bc.append(UTF8_INVALID, 0, UTF8_INVALID.length);
cc.allocate(bc.getLength(), -1);
conv.convert(bc, cc, false);
}
@Test(expected=MalformedInputException.class)
public void testBug54602b() throws Exception {
// Check partial input is rejected
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk();
bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length);
cc.allocate(bc.getLength(), -1);
conv.convert(bc, cc, true);
}
@Test
public void testBug54602c() throws Exception {
// Check partial input is rejected once it is known to be all available
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk();
bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length);
cc.allocate(bc.getLength(), -1);
conv.convert(bc, cc, false);
Exception e = null;
try {
conv.convert(bc, cc, true);
} catch (MalformedInputException mie) {
e = mie;
}
Assert.assertNotNull(e);
}
}

View File

@@ -0,0 +1,176 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.apache.tomcat.util.buf.ByteChunk.ByteOutputChannel;
/**
* Test cases for {@link ByteChunk}.
*/
public class TestByteChunk {
@Test
public void testConvertToBytes() throws UnsupportedEncodingException {
String string = "HTTP/1.1 100 \r\n\r\n";
byte[] bytes = ByteChunk.convertToBytes(string);
byte[] expected = string.getBytes("ISO-8859-1");
Assert.assertTrue(Arrays.equals(bytes, expected));
}
/*
* Test for {@code findByte} vs. {@code indexOf} methods difference.
*
* <p>
* As discussed in the "Re: r944918" thread on dev@, {@code
* ByteChunk.indexOf()} works for 0-127 ASCII chars only, and cannot find
* any chars outside of the range. {@code ByteChunk.findByte()} works for
* any ISO-8859-1 chars.
*/
@Test
public void testFindByte() throws UnsupportedEncodingException {
// 0xa0 = 160 = &nbsp; character
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
final int len = bytes.length;
// indexOf() does not work outside of 0-127
Assert.assertEquals(5, ByteChunk.findByte(bytes, 0, len, (byte) '\u00a0'));
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 0, len, '\u00a0'));
Assert.assertEquals(0, ByteChunk.findByte(bytes, 0, len, (byte) 'H'));
Assert.assertEquals(0, ByteChunk.indexOf(bytes, 0, len, 'H'));
Assert.assertEquals(len - 1, ByteChunk.findByte(bytes, 0, len, (byte) 'd'));
Assert.assertEquals(len - 1, ByteChunk.indexOf(bytes, 0, len, 'd'));
Assert.assertEquals(-1, ByteChunk.findByte(bytes, 0, len, (byte) 'x'));
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 0, len, 'x'));
Assert.assertEquals(7, ByteChunk.findByte(bytes, 5, len, (byte) 'o'));
Assert.assertEquals(7, ByteChunk.indexOf(bytes, 5, len, 'o'));
Assert.assertEquals(-1, ByteChunk.findByte(bytes, 2, 5, (byte) 'w'));
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 5, 5, 'w'));
}
@Test
public void testIndexOf_Char() throws UnsupportedEncodingException {
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
final int len = bytes.length;
ByteChunk bc = new ByteChunk();
bc.setBytes(bytes, 0, len);
Assert.assertEquals(0, bc.indexOf('H', 0));
Assert.assertEquals(6, bc.indexOf('w', 0));
// Does not work outside of 0-127
Assert.assertEquals(-1, bc.indexOf('\u00a0', 0));
bc.setBytes(bytes, 6, 5);
Assert.assertEquals(1, bc.indexOf('o', 0));
bc.setBytes(bytes, 6, 2);
Assert.assertEquals(0, bc.indexOf('w', 0));
Assert.assertEquals(-1, bc.indexOf('d', 0));
}
@Test
public void testIndexOf_String() throws UnsupportedEncodingException {
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
final int len = bytes.length;
ByteChunk bc = new ByteChunk();
bc.setBytes(bytes, 0, len);
Assert.assertEquals(0, bc.indexOf("Hello", 0, "Hello".length(), 0));
Assert.assertEquals(2, bc.indexOf("ll", 0, 2, 0));
Assert.assertEquals(2, bc.indexOf("Hello", 2, 2, 0));
Assert.assertEquals(7, bc.indexOf("o", 0, 1, 5));
// Does not work outside of 0-127
Assert.assertEquals(-1, bc.indexOf("\u00a0", 0, 1, 0));
bc.setBytes(bytes, 6, 5);
Assert.assertEquals(1, bc.indexOf("o", 0, 1, 0));
bc.setBytes(bytes, 6, 2);
Assert.assertEquals(0, bc.indexOf("wo", 0, 1, 0));
Assert.assertEquals(-1, bc.indexOf("d", 0, 1, 0));
}
@Test
public void testFindBytes() throws UnsupportedEncodingException {
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
final int len = bytes.length;
Assert.assertEquals(0, ByteChunk.findBytes(bytes, 0, len, new byte[] { 'H' }));
Assert.assertEquals(5, ByteChunk.findBytes(bytes, 0, len, new byte[] {
(byte) '\u00a0', 'x' }));
Assert.assertEquals(5, ByteChunk.findBytes(bytes, 0, len - 4, new byte[] {
'x', (byte) '\u00a0' }));
Assert.assertEquals(len - 1, ByteChunk.findBytes(bytes, 2, len, new byte[] {
'x', 'd' }));
Assert.assertEquals(1, ByteChunk.findBytes(bytes, 0, len, new byte[] { 'o',
'e' }));
Assert.assertEquals(-1, ByteChunk.findBytes(bytes, 2, 5, new byte[] { 'w' }));
}
@Ignore // Requires a 6GB heap (on markt's desktop - YMMV)
@Test
public void testAppend() throws Exception {
ByteChunk bc = new ByteChunk();
bc.setByteOutputChannel(new Sink());
// Defaults to no limit
byte data[] = new byte[32 * 1024 * 1024];
for (int i = 0; i < 100; i++) {
bc.append(data, 0, data.length);
}
Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, bc.getBuffer().length);
}
public class Sink implements ByteOutputChannel {
@Override
public void realWriteBytes(byte[] cbuf, int off, int len) throws IOException {
// NO-OP
}
@Override
public void realWriteBytes(ByteBuffer from) throws IOException {
// NO-OP
}
}
}

View File

@@ -0,0 +1,96 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.apache.tomcat.util.buf.CharChunk.CharOutputChannel;
/**
* Test cases for {@link CharChunk}.
*/
public class TestCharChunk {
@Test
public void testEndsWith() {
CharChunk cc = new CharChunk();
Assert.assertFalse(cc.endsWith("test"));
cc.setChars("xxtestxx".toCharArray(), 2, 4);
Assert.assertTrue(cc.endsWith(""));
Assert.assertTrue(cc.endsWith("t"));
Assert.assertTrue(cc.endsWith("st"));
Assert.assertTrue(cc.endsWith("test"));
Assert.assertFalse(cc.endsWith("x"));
Assert.assertFalse(cc.endsWith("xxtest"));
}
@Test
public void testIndexOf_String() {
char[] chars = "Hello\u00a0world".toCharArray();
final int len = chars.length;
CharChunk cc = new CharChunk();
cc.setChars(chars, 0, len);
Assert.assertEquals(0, cc.indexOf("Hello", 0, "Hello".length(), 0));
Assert.assertEquals(2, cc.indexOf("ll", 0, 2, 0));
Assert.assertEquals(2, cc.indexOf("Hello", 2, 2, 0));
Assert.assertEquals(7, cc.indexOf("o", 0, 1, 5));
// Does work outside of 0-127 (unlike ByteChunk)
Assert.assertEquals(5, cc.indexOf("\u00a0", 0, 1, 0));
cc.setChars(chars, 6, 5);
Assert.assertEquals(1, cc.indexOf("o", 0, 1, 0));
cc.setChars(chars, 6, 2);
Assert.assertEquals(0, cc.indexOf("wo", 0, 1, 0));
Assert.assertEquals(-1, cc.indexOf("d", 0, 1, 0));
}
@Ignore // Requires an 11GB heap (on markt's desktop - YMMV)
@Test
public void testAppend() throws Exception {
CharChunk cc = new CharChunk();
cc.setCharOutputChannel(new Sink());
// Defaults to no limit
char data[] = new char[32 * 1024 * 1024];
for (int i = 0; i < 100; i++) {
cc.append(data, 0, data.length);
}
Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, cc.getBuffer().length);
}
public class Sink implements CharOutputChannel {
@Override
public void realWriteChars(char[] cbuf, int off, int len) throws IOException {
// NO-OP
}
}
}

View File

@@ -0,0 +1,80 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
public class TestCharsetCache {
@Test
public void testAllKnownCharsets() {
Set<String> known = new HashSet<>();
known.addAll(Arrays.asList(CharsetCache.LAZY_CHARSETS));
Set<String> initial = new HashSet<>();
initial.addAll(Arrays.asList(CharsetCache.INITIAL_CHARSETS));
List<String> cacheMisses = new ArrayList<>();
for (Charset charset: Charset.availableCharsets().values()) {
String name = charset.name().toLowerCase(Locale.ENGLISH);
// No need to test the charsets that are pre-loaded
if (initial.contains(name)) {
continue;
}
if (!known.contains(name)) {
cacheMisses.add(name);
}
for (String alias : charset.aliases()) {
alias = alias.toLowerCase(Locale.ENGLISH);
if (!known.contains(alias)) {
cacheMisses.add(alias);
}
}
}
if (cacheMisses.size() != 0) {
StringBuilder sb = new StringBuilder();
Collections.sort(cacheMisses);
for (String name : cacheMisses) {
if (sb.length() == 0) {
sb.append('"');
} else {
sb.append(", \"");
}
sb.append(name.toLowerCase(Locale.ENGLISH));
sb.append('"');
}
System.out.println(sb.toString());
}
Assert.assertTrue(cacheMisses.size() == 0);
}
}

View File

@@ -0,0 +1,141 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.junit.Test;
public class TestCharsetCachePerformance {
@Test
public void testNoCsCache() throws Exception {
doTest(new NoCsCache());
}
@Test
public void testFullCsCache() throws Exception {
doTest(new FullCsCache());
}
@Test
public void testLazyCsCache() throws Exception {
doTest(new LazyCsCache());
}
private void doTest(CsCache cache) throws Exception {
int threadCount = 10;
int iterations = 10000000;
String[] lookupNames = new String[] {
"ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5" };
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
threads[i] = new TestCsCacheThread(iterations, cache, lookupNames);
}
long startTime = System.nanoTime();
for (int i = 0; i < threadCount; i++) {
threads[i].start();
}
for (int i = 0; i < threadCount; i++) {
threads[i].join();
}
long endTime = System.nanoTime();
System.out.println(cache.getClass().getName() + ": " + (endTime - startTime) + "ns");
}
private static interface CsCache {
Charset getCharset(String charsetName);
}
private static class NoCsCache implements CsCache {
@Override
public Charset getCharset(String charsetName) {
return Charset.forName(charsetName);
}
}
private static class FullCsCache implements CsCache {
private static final Map<String,Charset> cache = new HashMap<>();
static {
for (Charset charset: Charset.availableCharsets().values()) {
cache.put(charset.name().toLowerCase(Locale.ENGLISH), charset);
for (String alias : charset.aliases()) {
cache.put(alias.toLowerCase(Locale.ENGLISH), charset);
}
}
}
@Override
public Charset getCharset(String charsetName) {
return cache.get(charsetName.toLowerCase(Locale.ENGLISH));
}
}
private static class LazyCsCache implements CsCache {
private CharsetCache cache = new CharsetCache();
@Override
public Charset getCharset(String charsetName) {
return cache.getCharset(charsetName);
}
}
private static class TestCsCacheThread extends Thread {
private final int iterations;
private final CsCache cache;
private final String[] lookupNames;
private final int lookupNamesCount;
public TestCsCacheThread(int iterations, CsCache cache, String[] lookupNames) {
this.iterations = iterations;
this.cache = cache;
this.lookupNames = lookupNames;
this.lookupNamesCount = lookupNames.length;
}
@Override
public void run() {
for (int i = 0; i < iterations; i++) {
cache.getCharset(lookupNames[i % lookupNamesCount]);
}
}
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.Test;
/**
* Test cases for {@link HexUtils}.
*/
public class TestHexUtils {
private static final String TEST01_STRING = "Hello World";
private static final byte[] TEST01_BYTES = TEST01_STRING.getBytes(StandardCharsets.UTF_8);
private static final String TEST02_STRING = "foo";
private static final byte[] TEST02_BYTES = TEST02_STRING.getBytes(StandardCharsets.UTF_8);
@Test
public void testGetDec() {
Assert.assertEquals(0, HexUtils.getDec('0'));
Assert.assertEquals(9, HexUtils.getDec('9'));
Assert.assertEquals(10, HexUtils.getDec('a'));
Assert.assertEquals(15, HexUtils.getDec('f'));
Assert.assertEquals(10, HexUtils.getDec('A'));
Assert.assertEquals(15, HexUtils.getDec('F'));
Assert.assertEquals(-1, HexUtils.getDec(0));
Assert.assertEquals(-1, HexUtils.getDec('Z'));
Assert.assertEquals(-1, HexUtils.getDec(255));
Assert.assertEquals(-1, HexUtils.getDec(-60));
}
@Test
public void testRoundTrip01() {
Assert.assertArrayEquals(TEST01_STRING, TEST01_BYTES,
HexUtils.fromHexString(HexUtils.toHexString(TEST01_BYTES)));
}
@Test
public void testRoundTrip02() {
Assert.assertArrayEquals(TEST02_STRING, TEST02_BYTES,
HexUtils.fromHexString(HexUtils.toHexString(TEST02_BYTES)));
}
@Test(expected=IllegalArgumentException.class)
public void testFromHex01() {
HexUtils.fromHexString("Not a hex string");
}
@Test(expected=IllegalArgumentException.class)
public void testFromHex02() {
// Odd number of hex characters
HexUtils.fromHexString("aaa");
}
}

View File

@@ -0,0 +1,69 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import org.junit.Test;
public class TestMessageBytes {
@Test
public void testToStringFromNull() {
MessageBytes mb = MessageBytes.newInstance();
mb.toString();
}
@Test
public void testToBytesFromNull() {
MessageBytes mb = MessageBytes.newInstance();
mb.toBytes();
}
@Test
public void testToCharsFromNull() {
MessageBytes mb = MessageBytes.newInstance();
mb.toChars();
}
@Test
public void testToStringAfterRecycle() {
MessageBytes mb = MessageBytes.newInstance();
mb.setString("foo");
mb.recycle();
mb.toString();
}
@Test
public void testToBytesAfterRecycle() {
MessageBytes mb = MessageBytes.newInstance();
mb.setString("foo");
mb.recycle();
mb.toBytes();
}
@Test
public void testToCharsAfterRecycle() {
MessageBytes mb = MessageBytes.newInstance();
mb.setString("foo");
mb.recycle();
mb.toChars();
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
/*
* None of these tests should throw a NPE.
*/
public class TestStringUtils {
@Test
public void testNullArray() {
Assert.assertEquals("", StringUtils.join((String[]) null));
}
@Test
public void testNullArrayCharStringBuilder() {
StringBuilder sb = new StringBuilder();
StringUtils.join((String[]) null, ',', sb);
Assert.assertEquals("", sb.toString());
}
@Test
public void testNullCollection() {
Assert.assertEquals("", StringUtils.join((Collection<String>) null));
}
@Test
public void testNullCollectionChar() {
Assert.assertEquals("", StringUtils.join(null, ','));
}
@Test
public void testNullIterableCharStringBuilder() {
StringBuilder sb = new StringBuilder();
StringUtils.join((Iterable<String>) null, ',', sb);
Assert.assertEquals("", sb.toString());
}
@Test
public void testNullArrayCharFunctionStringBuilder() {
StringBuilder sb = new StringBuilder();
StringUtils.join((String[]) null, ',', null, sb);
Assert.assertEquals("", sb.toString());
}
@Test
public void testNullIterableCharFunctionStringBuilder() {
StringBuilder sb = new StringBuilder();
StringUtils.join((Iterable<String>) null, ',', null, sb);
Assert.assertEquals("", sb.toString());
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.Test;
public class TestUDecoder {
@Test
public void testURLDecodeStringInvalid() {
// %n rather than %nn should throw an IAE according to the Javadoc
Exception exception = null;
try {
UDecoder.URLDecode("%5xxxxx");
} catch (Exception e) {
exception = e;
}
Assert.assertTrue(exception instanceof IllegalArgumentException);
// Edge case trying to trigger ArrayIndexOutOfBoundsException
exception = null;
try {
UDecoder.URLDecode("%5");
} catch (Exception e) {
exception = e;
}
Assert.assertTrue(exception instanceof IllegalArgumentException);
}
@Test
public void testURLDecodeStringValidIso88591Start() {
String result = UDecoder.URLDecode("%41xxxx", StandardCharsets.ISO_8859_1);
Assert.assertEquals("Axxxx", result);
}
@Test
public void testURLDecodeStringValidIso88591Middle() {
String result = UDecoder.URLDecode("xx%41xx", StandardCharsets.ISO_8859_1);
Assert.assertEquals("xxAxx", result);
}
@Test
public void testURLDecodeStringValidIso88591End() {
String result = UDecoder.URLDecode("xxxx%41", StandardCharsets.ISO_8859_1);
Assert.assertEquals("xxxxA", result);
}
@Test
public void testURLDecodeStringValidUtf8Start() {
String result = UDecoder.URLDecode("%c3%aaxxxx", StandardCharsets.UTF_8);
Assert.assertEquals("\u00eaxxxx", result);
}
@Test
public void testURLDecodeStringValidUtf8Middle() {
String result = UDecoder.URLDecode("xx%c3%aaxx", StandardCharsets.UTF_8);
Assert.assertEquals("xx\u00eaxx", result);
}
@Test
public void testURLDecodeStringValidUtf8End() {
String result = UDecoder.URLDecode("xxxx%c3%aa", StandardCharsets.UTF_8);
Assert.assertEquals("xxxx\u00ea", result);
}
@Test
public void testURLDecodeStringNonAsciiValidNone() {
String result = UDecoder.URLDecode("\u00eaxxxx", StandardCharsets.UTF_8);
Assert.assertEquals("\u00eaxxxx", result);
}
@Test
public void testURLDecodeStringNonAsciiValidUtf8() {
String result = UDecoder.URLDecode("\u00ea%c3%aa", StandardCharsets.UTF_8);
Assert.assertEquals("\u00ea\u00ea", result);
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.buf.UEncoder.SafeCharsSet;
/**
* Test cases for {@link UEncoder}.
*/
public class TestUEncoder {
@Test
public void testEncodeURLWithSlashInit() throws IOException {
UEncoder urlEncoder = new UEncoder(SafeCharsSet.WITH_SLASH);
String s = "a+b/c/d+e.class";
Assert.assertTrue(urlEncoder.encodeURL(s, 0, s.length()).equals(
"a%2bb/c/d%2be.class"));
Assert.assertTrue(urlEncoder.encodeURL(s, 2, s.length() - 2).equals(
"b/c/d%2be.cla"));
s = new String(new char[] { 0xD801, 0xDC01 });
Assert.assertTrue(urlEncoder.encodeURL(s, 0, s.length())
.equals("%f0%90%90%81"));
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
public class TestUriUtil24 extends TesterUriUtilBase {
public TestUriUtil24() {
super("$");
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
public class TestUriUtil26 extends TesterUriUtilBase {
public TestUriUtil26() {
super("&");
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
public class TestUriUtil2A extends TesterUriUtilBase {
public TestUriUtil2A() {
super("*");
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
public class TestUriUtil40 extends TesterUriUtilBase {
public TestUriUtil40() {
super("@");
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,136 @@
/*
* 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.
*/
package org.apache.tomcat.util.buf;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
public abstract class TesterUriUtilBase {
private final String separator;
protected TesterUriUtilBase(String separator) {
this.separator = separator;
TomcatURLStreamHandlerFactory.register();
System.setProperty("org.apache.tomcat.util.buf.UriUtil.WAR_SEPARATOR", separator);
}
@Test
public void testBuildJarUrl01() throws MalformedURLException {
File jarFile = new File("/patha/pathb!/pathc");
String result = UriUtil.buildJarUrl(jarFile).toString();
int index = result.indexOf("!/");
Assert.assertEquals(result, result.length() - 2, index);
}
@Test
public void testBuildJarUrl02() throws MalformedURLException {
File jarFile = new File("/patha/pathb*/pathc");
String result = UriUtil.buildJarUrl(jarFile).toString();
int index = result.indexOf("!/");
Assert.assertEquals(result, result.length() - 2, index);
index = result.indexOf("*/");
Assert.assertEquals(result, -1, index);
}
@Test
public void testBuildJarUrl03() throws MalformedURLException {
File jarFile = new File("/patha/pathb^/pathc");
String result = UriUtil.buildJarUrl(jarFile).toString();
int index = result.indexOf("!/");
Assert.assertEquals(result, result.length() - 2, index);
index = result.indexOf("^/");
Assert.assertEquals(result, -1, index);
}
@Test
public void testBuildJarUrl04() throws MalformedURLException {
File jarFile = new File("/patha/pathb" + separator + "/pathc");
String result = UriUtil.buildJarUrl(jarFile).toString();
int index = result.indexOf("!/");
Assert.assertEquals(result, result.length() - 2, index);
index = result.indexOf(separator + "/");
Assert.assertEquals(result, -1, index);
}
@Test
public void testWarToJar01() throws MalformedURLException {
doTestWarToJar("^");
}
@Test
public void testWarToJar02() throws MalformedURLException {
doTestWarToJar("*");
}
@Test
public void testWarToJar03() throws MalformedURLException {
doTestWarToJar(separator);
}
private void doTestWarToJar(String separator) throws MalformedURLException {
URL warUrl = new URL("war:file:/external/path" + separator + "/internal/path");
URL jarUrl = UriUtil.warToJar(warUrl);
Assert.assertEquals("jar:file:/external/path!/internal/path", jarUrl.toString());
}
// @Test /* Uncomment to test performance for different implementations. */
public void performanceTestBuildJarUrl() throws MalformedURLException {
File jarFile = new File("/patha/pathb^/pathc");
URL url = null;
int count = 1000000;
// Warm up
for (int i = 0; i < count / 10; i++) {
url = UriUtil.buildJarUrl(jarFile);
}
// Test
long start = System.nanoTime();
for (int i = 0; i < count / 10; i++) {
url = UriUtil.buildJarUrl(jarFile);
}
long duration = System.nanoTime() - start;
System.out.println("[" + count + "] iterations took [" +
duration + "] ns for [" + url + "]");
}
}

View File

@@ -0,0 +1,215 @@
/*
* 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.
*/
package org.apache.tomcat.util.collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
public class TestCaseInsensitiveKeyMap {
@Test
public void testPut() {
Object o1 = new Object();
Object o2 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Object o = map.put("A", o2);
Assert.assertEquals(o1, o);
Assert.assertEquals(o2, map.get("a"));
Assert.assertEquals(o2, map.get("A"));
}
@Test(expected=NullPointerException.class)
public void testPutNullKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put(null, o1);
}
@Test
public void testGet() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertEquals(o1, map.get("a"));
Assert.assertEquals(o1, map.get("A"));
}
@Test
public void testGetNullKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertNull(map.get(null));
}
@Test
public void testContainsKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertTrue(map.containsKey("a"));
Assert.assertTrue(map.containsKey("A"));
}
@Test
public void testContainsKeyNonString() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.containsKey(o1));
}
@Test
public void testContainsKeyNull() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.containsKey(null));
}
@Test
public void testContainsValue() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertTrue(map.containsValue(o1));
}
@Test
public void testRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.isEmpty());
map.remove("A");
Assert.assertTrue(map.isEmpty());
map.put("A", o1);
Assert.assertFalse(map.isEmpty());
map.remove("a");
Assert.assertTrue(map.isEmpty());
}
@Test
public void testClear() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
for (int i = 0; i < 10; i++) {
map.put(Integer.toString(i), o1);
}
Assert.assertEquals(10, map.size());
map.clear();
Assert.assertEquals(0, map.size());
}
@Test
public void testPutAll() {
Object o1 = new Object();
Object o2 = new Object();
Map<String,Object> source = new HashMap<>();
source.put("a", o1);
source.put("A", o2);
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.putAll(source);
Assert.assertEquals(1, map.size());
Assert.assertTrue(map.containsValue(o1) != map.containsValue(o2));
}
@Test
public void testKeySetContains() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Set<String> keys = map.keySet();
Assert.assertTrue(keys.contains("a"));
Assert.assertTrue(keys.contains("A"));
}
@Test
public void testKeySetRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Iterator<String> iter = map.keySet().iterator();
Assert.assertTrue(iter.hasNext());
iter.next();
iter.remove();
Assert.assertTrue(map.isEmpty());
}
@Test
public void testEntrySetRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Iterator<Entry<String,Object>> iter = map.entrySet().iterator();
Assert.assertTrue(iter.hasNext());
Entry<String,Object> entry = iter.next();
Assert.assertEquals("a", entry.getKey());
Assert.assertEquals(o1, entry.getValue());
iter.remove();
Assert.assertTrue(map.isEmpty());
}
}

View File

@@ -0,0 +1,117 @@
/*
* 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.
*/
package org.apache.tomcat.util.collections;
import org.junit.Assert;
import org.junit.Test;
public class TestSynchronizedQueue {
public void testPollEmpty() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Assert.assertNull(queue.poll());
}
@Test
public void testOfferPollOrder() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
Assert.assertNull(queue.poll());
}
@Test
public void testExpandOfferPollOrder() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 300; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 300; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
Assert.assertNull(queue.poll());
}
@Test
public void testExpandOfferPollOrder2() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 100; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 50; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
for (int i = 0; i < 200; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 250; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
Assert.assertNull(queue.poll());
}
}

View File

@@ -0,0 +1,119 @@
/*
* 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.
*/
package org.apache.tomcat.util.collections;
import org.junit.Assert;
import org.junit.Test;
public class TestSynchronizedStack {
@Test
public void testPopEmpty() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Assert.assertNull(stack.pop());
}
@Test
public void testPushPopOrder() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
@Test
public void testExpandPushPopOrder() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 300; i++) {
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
}
for (int i = 0; i < 300; i++) {
Assert.assertSame(stack.pop(), o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
}
Assert.assertNull(stack.pop());
}
@Test
public void testLimit() {
SynchronizedStack<Object> stack = new SynchronizedStack<>(2,2);
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
@Test
public void testLimitExpand() {
SynchronizedStack<Object> stack = new SynchronizedStack<>(1,3);
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
}

View File

@@ -0,0 +1,107 @@
/*
* 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.
*/
package org.apache.tomcat.util.collections;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.junit.Test;
public class TesterPerformanceSynchronizedQueue {
private static final int THREAD_COUNT = 4;
private static final int ITERATIONS = 1000000;
private static final SynchronizedQueue<Object> S_QUEUE =
new SynchronizedQueue<>();
private static final Queue<Object> QUEUE = new ConcurrentLinkedQueue<>();
@Test
public void testSynchronizedQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new StackThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("SynchronizedQueue: " + (end - start) + "ms");
}
public static class StackThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = S_QUEUE.poll();
if (obj == null) {
obj = new Object();
}
S_QUEUE.offer(obj);
}
super.run();
}
}
@Test
public void testConcurrentQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new QueueThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("ConcurrentLinkedQueue: " + (end - start) + "ms");
}
public static class QueueThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = QUEUE.poll();
if (obj == null) {
obj = new Object();
}
QUEUE.offer(obj);
}
super.run();
}
}
}

View File

@@ -0,0 +1,107 @@
/*
* 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.
*/
package org.apache.tomcat.util.collections;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.junit.Test;
public class TesterPerformanceSynchronizedStack {
private static final int THREAD_COUNT = 4;
private static final int ITERATIONS = 1000000;
private static final SynchronizedStack<Object> STACK =
new SynchronizedStack<>();
private static final Queue<Object> QUEUE = new ConcurrentLinkedQueue<>();
@Test
public void testSynchronizedStack() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new StackThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("SynchronizedStack: " + (end - start) + "ms");
}
public static class StackThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = STACK.pop();
if (obj == null) {
obj = new Object();
}
STACK.push(obj);
}
super.run();
}
}
@Test
public void testConcurrentQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new QueueThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("ConcurrentLinkedQueue: " + (end - start) + "ms");
}
public static class QueueThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = QUEUE.poll();
if (obj == null) {
obj = new Object();
}
QUEUE.offer(obj);
}
super.run();
}
}
}

View File

@@ -0,0 +1,125 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class TestLocalResolver {
private final Map<String, String> publicIds = new HashMap<>();
private final Map<String, String> systemIds = new HashMap<>();
private LocalResolver resolver = new LocalResolver(publicIds, systemIds, true);
private String WEB_22_LOCAL;
private String WEB_31_LOCAL;
private String WEBCOMMON_31_LOCAL;
@Before
public void init() {
WEB_22_LOCAL = urlFor("resources/web-app_2_2.dtd");
WEB_31_LOCAL = urlFor("resources/web-app_3_1.xsd");
WEBCOMMON_31_LOCAL = urlFor("resources/web-common_3_1.xsd");
publicIds.put(XmlIdentifiers.WEB_22_PUBLIC, WEB_22_LOCAL);
systemIds.put(XmlIdentifiers.WEB_31_XSD, WEB_31_LOCAL);
systemIds.put(WEBCOMMON_31_LOCAL, WEBCOMMON_31_LOCAL);
}
public String urlFor(String id) {
return ServletContext.class.getResource(id).toExternalForm();
}
@Test(expected = FileNotFoundException.class)
public void unknownNullId() throws IOException, SAXException {
Assert.assertNull(resolver.resolveEntity(null, null));
}
@Test(expected = FileNotFoundException.class)
public void unknownPublicId() throws IOException, SAXException {
Assert.assertNull(resolver.resolveEntity("unknown", null));
}
@Test(expected = FileNotFoundException.class)
public void unknownSystemId() throws IOException, SAXException {
InputSource source = resolver.resolveEntity(null, "unknown");
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals("unknown", source.getSystemId());
}
@Test(expected = FileNotFoundException.class)
public void unknownRelativeSystemId()
throws IOException, SAXException {
InputSource source = resolver.resolveEntity(
null, null, "http://example.com/home.html", "unknown");
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals("http://example.com/unknown", source.getSystemId());
}
@Test
public void publicIdIsResolved() throws IOException, SAXException {
InputSource source = resolver.resolveEntity(
XmlIdentifiers.WEB_22_PUBLIC, XmlIdentifiers.WEB_22_SYSTEM);
Assert.assertEquals(XmlIdentifiers.WEB_22_PUBLIC, source.getPublicId());
Assert.assertEquals(WEB_22_LOCAL, source.getSystemId());
}
@Test
public void systemIdIsIgnoredWhenPublicIdIsResolved()
throws IOException, SAXException {
InputSource source = resolver.resolveEntity(
XmlIdentifiers.WEB_22_PUBLIC, "unknown");
Assert.assertEquals(XmlIdentifiers.WEB_22_PUBLIC, source.getPublicId());
Assert.assertEquals(WEB_22_LOCAL, source.getSystemId());
}
@Test
public void systemIdIsResolved() throws IOException, SAXException {
InputSource source =
resolver.resolveEntity(null, XmlIdentifiers.WEB_31_XSD);
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals(WEB_31_LOCAL, source.getSystemId());
}
@Test
public void relativeSystemIdIsResolvedAgainstBaseURI()
throws IOException, SAXException {
InputSource source = resolver.resolveEntity(
null, null, WEB_31_LOCAL, "web-common_3_1.xsd");
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals(WEBCOMMON_31_LOCAL, source.getSystemId());
}
@Test
public void absoluteSystemIdOverridesBaseURI()
throws IOException, SAXException {
InputSource source = resolver.resolveEntity(null, null,
"http://example.com/home.html", XmlIdentifiers.WEB_31_XSD);
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals(WEB_31_LOCAL, source.getSystemId());
}
}

View File

@@ -0,0 +1,59 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.tld;
import java.io.File;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class TestImplicitTldParser {
private TldParser parser;
@Before
public void init() {
parser = new TldParser(true, true, new ImplicitTldRuleSet(), true);
}
@Test
public void testImplicitTldGood() throws Exception {
TaglibXml xml = parse("test/tld/implicit-good.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.1", xml.getJspVersion());
Assert.assertEquals("Ignored", xml.getShortName());
}
@Test(expected=SAXParseException.class)
public void testImplicitTldBad() throws Exception {
TaglibXml xml = parse("test/tld/implicit-bad.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.1", xml.getJspVersion());
Assert.assertEquals("Ignored", xml.getShortName());
}
private TaglibXml parse(String pathname) throws IOException, SAXException {
File file = new File(pathname);
TldResourcePath path = new TldResourcePath(file.toURI().toURL(), null);
return parser.parse(path);
}
}

View File

@@ -0,0 +1,173 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.tld;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.tagext.FunctionInfo;
import javax.servlet.jsp.tagext.TagAttributeInfo;
import javax.servlet.jsp.tagext.TagVariableInfo;
import javax.servlet.jsp.tagext.VariableInfo;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
public class TestTldParser {
private TldParser parser;
@Before
public void init() {
parser = new TldParser(true, true, new TldRuleSet(), true);
}
@Test
public void testTld() throws Exception {
TaglibXml xml = parse("test/tld/test.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.1", xml.getJspVersion());
Assert.assertEquals("test", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/TldTests", xml.getUri());
Assert.assertEquals(1, xml.getFunctions().size());
ValidatorXml validator = xml.getValidator();
Assert.assertEquals("com.example.Validator", validator.getValidatorClass());
Assert.assertEquals(1, validator.getInitParams().size());
Assert.assertEquals("value", validator.getInitParams().get("name"));
Assert.assertEquals(1, xml.getTags().size());
TagXml tag = xml.getTags().get(0);
Assert.assertEquals("org.apache.jasper.compiler.TestValidator$Echo", tag.getTagClass());
Assert.assertEquals("empty", tag.getBodyContent());
Assert.assertTrue(tag.hasDynamicAttributes());
Assert.assertEquals(1, tag.getVariables().size());
TagVariableInfo variableInfo = tag.getVariables().get(0);
Assert.assertEquals("var", variableInfo.getNameGiven());
Assert.assertEquals("java.lang.Object", variableInfo.getClassName());
Assert.assertTrue(variableInfo.getDeclare());
Assert.assertEquals(VariableInfo.AT_END, variableInfo.getScope());
Assert.assertEquals(4, tag.getAttributes().size());
TagAttributeInfo attributeInfo = tag.getAttributes().get(0);
Assert.assertEquals("Echo Tag", tag.getInfo());
Assert.assertEquals("Echo", tag.getDisplayName());
Assert.assertEquals("small", tag.getSmallIcon());
Assert.assertEquals("large", tag.getLargeIcon());
Assert.assertEquals("echo", attributeInfo.getName());
Assert.assertTrue(attributeInfo.isRequired());
Assert.assertTrue(attributeInfo.canBeRequestTime());
attributeInfo = tag.getAttributes().get(1);
Assert.assertEquals("fragment", attributeInfo.getName());
Assert.assertTrue(attributeInfo.isFragment());
Assert.assertTrue(attributeInfo.canBeRequestTime());
Assert.assertEquals("javax.servlet.jsp.tagext.JspFragment", attributeInfo.getTypeName());
attributeInfo = tag.getAttributes().get(2);
Assert.assertEquals("deferredValue", attributeInfo.getName());
Assert.assertEquals("javax.el.ValueExpression", attributeInfo.getTypeName());
Assert.assertEquals("java.util.Date", attributeInfo.getExpectedTypeName());
attributeInfo = tag.getAttributes().get(3);
Assert.assertEquals("deferredMethod", attributeInfo.getName());
Assert.assertEquals("javax.el.MethodExpression", attributeInfo.getTypeName());
Assert.assertEquals("java.util.Date getDate()", attributeInfo.getMethodSignature());
Assert.assertEquals(1, xml.getTagFiles().size());
TagFileXml tagFile = xml.getTagFiles().get(0);
Assert.assertEquals("Echo", tag.getDisplayName());
Assert.assertEquals("small", tag.getSmallIcon());
Assert.assertEquals("large", tag.getLargeIcon());
Assert.assertEquals("Echo2", tagFile.getName());
Assert.assertEquals("/echo.tag", tagFile.getPath());
Assert.assertEquals(1, xml.getFunctions().size());
FunctionInfo fn = xml.getFunctions().get(0);
Assert.assertEquals("trim", fn.getName());
Assert.assertEquals("org.apache.el.TesterFunctions", fn.getFunctionClass());
Assert.assertEquals("java.lang.String trim(java.lang.String)", fn.getFunctionSignature());
}
@Test
public void testParseTld21() throws Exception {
TaglibXml xml = parse("test/tld/tags21.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.1", xml.getJspVersion());
Assert.assertEquals("Tags21", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/tags21", xml.getUri());
verifyTags(xml.getTags());
}
@Test
public void testParseTld20() throws Exception {
TaglibXml xml = parse("test/tld/tags20.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.0", xml.getJspVersion());
Assert.assertEquals("Tags20", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/tags20", xml.getUri());
verifyTags(xml.getTags());
}
@Test
public void testParseTld12() throws Exception {
TaglibXml xml = parse("test/tld/tags12.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("1.2", xml.getJspVersion());
Assert.assertEquals("Tags12", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/tags12", xml.getUri());
verifyTags(xml.getTags());
}
@Test
public void testParseTld11() throws Exception {
TaglibXml xml = parse("test/tld/tags11.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("1.1", xml.getJspVersion());
Assert.assertEquals("Tags11", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/tags11", xml.getUri());
verifyTags(xml.getTags());
}
private void verifyTags(List<TagXml> tags) {
Assert.assertEquals(1, tags.size());
TagXml tag = tags.get(0);
Assert.assertEquals("Echo", tag.getName());
Assert.assertEquals("org.apache.jasper.compiler.TestValidator$Echo", tag.getTagClass());
Assert.assertEquals("empty", tag.getBodyContent());
}
@Test
public void testListener() throws Exception {
TaglibXml xml = parse("test/tld/listener.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
List<String> listeners = xml.getListeners();
Assert.assertEquals(1, listeners.size());
Assert.assertEquals("org.apache.catalina.core.TesterTldListener", listeners.get(0));
}
private TaglibXml parse(String pathname) throws IOException, SAXException {
File file = new File(pathname);
TldResourcePath path = new TldResourcePath(file.toURI().toURL(), null);
return parser.parse(path);
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.web;
import org.junit.Assert;
import org.junit.Test;
/**
* Test case for {@link FilterDef}.
*/
public class TestFilterDef {
@Test(expected = IllegalArgumentException.class)
public void testSetFilterNameNull() {
new FilterDef().setFilterName(null);
}
@Test(expected = IllegalArgumentException.class)
public void testSetFilterNameEmptyString() {
new FilterDef().setFilterName("");
}
@Test
public void testSetFilterName() {
FilterDef filterDef = new FilterDef();
filterDef.setFilterName("test");
Assert.assertEquals("'test' is expected as filter name",
"test", filterDef.getFilterName());
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.web;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.descriptor.JspConfigDescriptor;
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
import javax.servlet.descriptor.TaglibDescriptor;
import org.junit.Assert;
import org.junit.Test;
public class TestJspConfigDescriptorImpl {
@Test
public void testTaglibsAreIsolate() {
List<TaglibDescriptor> taglibs = new ArrayList<>();
taglibs.add(new TaglibDescriptorImpl("location", "uri"));
List<JspPropertyGroupDescriptor> propertyGroups = Collections.emptyList();
JspConfigDescriptor descriptor = new JspConfigDescriptorImpl(propertyGroups, taglibs);
descriptor.getTaglibs().clear();
Assert.assertEquals(taglibs, descriptor.getTaglibs());
}
@Test
public void testPropertyGroupsAreIsolate() {
List<TaglibDescriptor> taglibs = Collections.emptyList();
List<JspPropertyGroupDescriptor> propertyGroups = new ArrayList<>();
propertyGroups.add(new JspPropertyGroupDescriptorImpl(new JspPropertyGroup()));
JspConfigDescriptor descriptor = new JspConfigDescriptorImpl(propertyGroups, taglibs);
descriptor.getJspPropertyGroups().clear();
Assert.assertEquals(propertyGroups, descriptor.getJspPropertyGroups());
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.web;
import org.junit.Assert;
import org.junit.Test;
public class TestJspPropertyGroup {
private JspPropertyGroup group = new JspPropertyGroup();
@Test
public void testBug55262() {
group.addIncludePrelude("/prelude");
group.addIncludePrelude("/prelude");
group.addIncludeCoda("/coda");
group.addIncludeCoda("/coda");
Assert.assertEquals(2, group.getIncludePreludes().size());
Assert.assertEquals(2, group.getIncludeCodas().size());
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.web;
import org.junit.Assert;
import org.junit.Test;
public class TestJspPropertyGroupDescriptorImpl {
@Test
public void testPreludesAreIsolated() {
JspPropertyGroup jpg = new JspPropertyGroup();
jpg.addIncludePrelude("prelude");
JspPropertyGroupDescriptorImpl descriptor = new JspPropertyGroupDescriptorImpl(jpg);
descriptor.getIncludePreludes().clear();
Assert.assertEquals(1, descriptor.getIncludePreludes().size());
}
@Test
public void testCodasAreIsolated() {
JspPropertyGroup jpg = new JspPropertyGroup();
jpg.addIncludeCoda("coda");
JspPropertyGroupDescriptorImpl descriptor = new JspPropertyGroupDescriptorImpl(jpg);
descriptor.getIncludeCodas().clear();
Assert.assertEquals(1, descriptor.getIncludeCodas().size());
}
@Test
public void testUrlPatternsAreIsolated() {
JspPropertyGroup jpg = new JspPropertyGroup();
jpg.addUrlPatternDecoded("pattern");
JspPropertyGroupDescriptorImpl descriptor = new JspPropertyGroupDescriptorImpl(jpg);
descriptor.getUrlPatterns().clear();
Assert.assertEquals(1, descriptor.getUrlPatterns().size());
}
}

View File

@@ -0,0 +1,453 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.web;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.HttpConstraintElement;
import javax.servlet.HttpMethodConstraintElement;
import javax.servlet.ServletSecurityElement;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.ServletSecurity.EmptyRoleSemantic;
import org.junit.Assert;
import org.junit.Test;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
public class TestSecurityConstraint {
private static final String URL_PATTERN = "/test";
private static final String ROLE1 = "R1";
private static final Log DUMMY_LOG = LogFactory.getLog("DUMMY");
private static final SecurityConstraint GET_ONLY;
private static final SecurityConstraint POST_ONLY;
private static final SecurityConstraint GET_OMIT;
private static final SecurityConstraint POST_OMIT;
static {
// Configure the constraints to use in the tests
GET_ONLY = new SecurityConstraint();
GET_ONLY.addAuthRole(ROLE1);
SecurityCollection scGetOnly = new SecurityCollection();
scGetOnly.addMethod("GET");
scGetOnly.addPatternDecoded(URL_PATTERN);
scGetOnly.setName("GET-ONLY");
GET_ONLY.addCollection(scGetOnly);
POST_ONLY = new SecurityConstraint();
POST_ONLY.addAuthRole(ROLE1);
SecurityCollection scPostOnly = new SecurityCollection();
scPostOnly.addMethod("POST");
scPostOnly.addPatternDecoded(URL_PATTERN);
scPostOnly.setName("POST_ONLY");
POST_ONLY.addCollection(scPostOnly);
GET_OMIT = new SecurityConstraint();
GET_OMIT.addAuthRole(ROLE1);
SecurityCollection scGetOmit = new SecurityCollection();
scGetOmit.addOmittedMethod("GET");
scGetOmit.addPatternDecoded(URL_PATTERN);
scGetOmit.setName("GET_OMIT");
GET_OMIT.addCollection(scGetOmit);
POST_OMIT = new SecurityConstraint();
POST_OMIT.addAuthRole(ROLE1);
SecurityCollection scPostOmit = new SecurityCollection();
scPostOmit.addOmittedMethod("POST");
scPostOmit.addPatternDecoded(URL_PATTERN);
scPostOmit.setName("POST_OMIT");
POST_OMIT.addCollection(scPostOmit);
}
/**
* Uses the examples in SRV.13.4 as the basis for these tests
*/
@Test
public void testCreateConstraints() {
ServletSecurityElement element;
SecurityConstraint[] result;
Set<HttpMethodConstraintElement> hmces = new HashSet<>();
// Example 13-1
// @ServletSecurity
element = new ServletSecurityElement();
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(0, result.length);
// Example 13-2
// @ServletSecurity(
// @HttpConstraint(
// transportGuarantee = TransportGuarantee.CONFIDENTIAL))
element = new ServletSecurityElement(
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.CONFIDENTIAL));
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(1, result.length);
Assert.assertFalse(result[0].getAuthConstraint());
Assert.assertTrue(result[0].findCollections()[0].findPattern(URL_PATTERN));
Assert.assertEquals(0, result[0].findCollections()[0].findMethods().length);
Assert.assertEquals(ServletSecurity.TransportGuarantee.CONFIDENTIAL.name(),
result[0].getUserConstraint());
// Example 13-3
// @ServletSecurity(@HttpConstraint(EmptyRoleSemantic.DENY))
element = new ServletSecurityElement(
new HttpConstraintElement(EmptyRoleSemantic.DENY));
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(1, result.length);
Assert.assertTrue(result[0].getAuthConstraint());
Assert.assertTrue(result[0].findCollections()[0].findPattern(URL_PATTERN));
Assert.assertEquals(0, result[0].findCollections()[0].findMethods().length);
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[0].getUserConstraint());
// Example 13-4
// @ServletSecurity(@HttpConstraint(rolesAllowed = "R1"))
element = new ServletSecurityElement(new HttpConstraintElement(
ServletSecurity.TransportGuarantee.NONE, ROLE1));
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(1, result.length);
Assert.assertTrue(result[0].getAuthConstraint());
Assert.assertEquals(1, result[0].findAuthRoles().length);
Assert.assertTrue(result[0].findAuthRole(ROLE1));
Assert.assertTrue(result[0].findCollections()[0].findPattern(URL_PATTERN));
Assert.assertEquals(0, result[0].findCollections()[0].findMethods().length);
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[0].getUserConstraint());
// Example 13-5
// @ServletSecurity((httpMethodConstraints = {
// @HttpMethodConstraint(value = "GET", rolesAllowed = "R1"),
// @HttpMethodConstraint(value = "POST", rolesAllowed = "R1",
// transportGuarantee = TransportGuarantee.CONFIDENTIAL)
// })
hmces.clear();
hmces.add(new HttpMethodConstraintElement("GET",
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.NONE, ROLE1)));
hmces.add(new HttpMethodConstraintElement("POST",
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.CONFIDENTIAL,
ROLE1)));
element = new ServletSecurityElement(hmces);
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(2, result.length);
for (int i = 0; i < 2; i++) {
Assert.assertTrue(result[i].getAuthConstraint());
Assert.assertEquals(1, result[i].findAuthRoles().length);
Assert.assertTrue(result[i].findAuthRole(ROLE1));
Assert.assertTrue(result[i].findCollections()[0].findPattern(URL_PATTERN));
Assert.assertEquals(1, result[i].findCollections()[0].findMethods().length);
String method = result[i].findCollections()[0].findMethods()[0];
if ("GET".equals(method)) {
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[i].getUserConstraint());
} else if ("POST".equals(method)) {
Assert.assertEquals(ServletSecurity.TransportGuarantee.CONFIDENTIAL.name(),
result[i].getUserConstraint());
} else {
Assert.fail("Unexpected method :[" + method + "]");
}
}
// Example 13-6
// @ServletSecurity(value = @HttpConstraint(rolesAllowed = "R1"),
// httpMethodConstraints = @HttpMethodConstraint("GET"))
hmces.clear();
hmces.add(new HttpMethodConstraintElement("GET"));
element = new ServletSecurityElement(
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.NONE,
ROLE1),
hmces);
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(2, result.length);
for (int i = 0; i < 2; i++) {
Assert.assertTrue(result[i].findCollections()[0].findPattern(URL_PATTERN));
if (result[i].findCollections()[0].findMethods().length == 1) {
Assert.assertEquals("GET",
result[i].findCollections()[0].findMethods()[0]);
Assert.assertFalse(result[i].getAuthConstraint());
} else if (result[i].findCollections()[0].findOmittedMethods().length == 1) {
Assert.assertEquals("GET",
result[i].findCollections()[0].findOmittedMethods()[0]);
Assert.assertTrue(result[i].getAuthConstraint());
Assert.assertEquals(1, result[i].findAuthRoles().length);
Assert.assertEquals(ROLE1, result[i].findAuthRoles()[0]);
} else {
Assert.fail("Unexpected number of methods defined");
}
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[i].getUserConstraint());
}
// Example 13-7
// @ServletSecurity(value = @HttpConstraint(rolesAllowed = "R1"),
// httpMethodConstraints = @HttpMethodConstraint(value="TRACE",
// emptyRoleSemantic = EmptyRoleSemantic.DENY))
hmces.clear();
hmces.add(new HttpMethodConstraintElement("TRACE",
new HttpConstraintElement(EmptyRoleSemantic.DENY)));
element = new ServletSecurityElement(
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.NONE,
ROLE1),
hmces);
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(2, result.length);
for (int i = 0; i < 2; i++) {
Assert.assertTrue(result[i].findCollections()[0].findPattern(URL_PATTERN));
if (result[i].findCollections()[0].findMethods().length == 1) {
Assert.assertEquals("TRACE",
result[i].findCollections()[0].findMethods()[0]);
Assert.assertTrue(result[i].getAuthConstraint());
Assert.assertEquals(0, result[i].findAuthRoles().length);
} else if (result[i].findCollections()[0].findOmittedMethods().length == 1) {
Assert.assertEquals("TRACE",
result[i].findCollections()[0].findOmittedMethods()[0]);
Assert.assertTrue(result[i].getAuthConstraint());
Assert.assertEquals(1, result[i].findAuthRoles().length);
Assert.assertEquals(ROLE1, result[i].findAuthRoles()[0]);
} else {
Assert.fail("Unexpected number of methods defined");
}
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[i].getUserConstraint());
}
// Example 13-8 is the same as 13-4
// Example 13-9 is the same as 13-7
}
@Test
public void testFindUncoveredHttpMethods01() {
// No new constraints if denyUncoveredHttpMethods is false
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY}, false, DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods02() {
// No new constraints if denyUncoveredHttpMethods is false
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_OMIT}, false, DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods03() {
// No new constraints if denyUncoveredHttpMethods is false
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_ONLY}, false, DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods04() {
// No new constraints if denyUncoveredHttpMethods is false
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_OMIT}, false, DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods05() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY}, true, DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list GET as an omitted method
Assert.assertEquals(0, sc.findMethods().length);
Assert.assertEquals(1, sc.findOmittedMethods().length);
Assert.assertEquals("GET", sc.findOmittedMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods06() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_ONLY}, true, DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list POST as an omitted method
Assert.assertEquals(0, sc.findMethods().length);
Assert.assertEquals(1, sc.findOmittedMethods().length);
Assert.assertEquals("POST", sc.findOmittedMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods07() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_OMIT}, true, DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list GET as an method
Assert.assertEquals(0, sc.findOmittedMethods().length);
Assert.assertEquals(1, sc.findMethods().length);
Assert.assertEquals("GET", sc.findMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods08() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_OMIT}, true, DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list POST as an method
Assert.assertEquals(0, sc.findOmittedMethods().length);
Assert.assertEquals(1, sc.findMethods().length);
Assert.assertEquals("POST", sc.findMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods09() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY, GET_OMIT}, true,
DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods10() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_ONLY, POST_OMIT}, true,
DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods11() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY, POST_ONLY}, true,
DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list GET and POST as omitted methods
Assert.assertEquals(0, sc.findMethods().length);
Assert.assertEquals(2, sc.findOmittedMethods().length);
HashSet<String> omittedMethods = new HashSet<>();
for (String omittedMethod : sc.findOmittedMethods()) {
omittedMethods.add(omittedMethod);
}
Assert.assertTrue(omittedMethods.remove("GET"));
Assert.assertTrue(omittedMethods.remove("POST"));
}
@Test
public void testFindUncoveredHttpMethods12() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_OMIT, POST_OMIT}, true,
DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods13() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY, POST_OMIT}, true,
DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list POST as a method
Assert.assertEquals(1, sc.findMethods().length);
Assert.assertEquals(0, sc.findOmittedMethods().length);
Assert.assertEquals("POST", sc.findMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods14() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_OMIT, POST_ONLY}, true,
DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list GET as a method
Assert.assertEquals(1, sc.findMethods().length);
Assert.assertEquals(0, sc.findOmittedMethods().length);
Assert.assertEquals("GET", sc.findMethods()[0]);
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.web;
import org.junit.Assert;
import org.junit.Test;
/**
* Test case for {@link ServletDef}
*/
public class TestServletDef {
@Test(expected = IllegalArgumentException.class)
public void testSetServletNameNull() {
new ServletDef().setServletName(null);
}
@Test(expected = IllegalArgumentException.class)
public void testSetServletNameEmptyString() {
new ServletDef().setServletName("");
}
@Test
public void testSetServletName() {
ServletDef servletDef = new ServletDef();
servletDef.setServletName("test");
Assert.assertEquals("'test' is expected as servlet name",
"test", servletDef.getServletName());
}
}

View File

@@ -0,0 +1,155 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.digester.Digester;
public class TestWebRuleSet {
private Digester fragmentDigester = new Digester();
private WebRuleSet fragmentRuleSet = new WebRuleSet(true);
private Digester webDigester = new Digester();
private WebRuleSet webRuleSet = new WebRuleSet(false);
public TestWebRuleSet() {
fragmentDigester.addRuleSet(fragmentRuleSet);
webDigester.addRuleSet(webRuleSet);
}
@Test
public void testSingleNameInWebFragmentXml() throws Exception {
WebXml webXml = new WebXml();
parse(webXml, "web-fragment-1name.xml", true, true);
Assert.assertEquals("name1", webXml.getName());
}
@Test
public void testMultipleNameInWebFragmentXml() throws Exception {
parse(new WebXml(), "web-fragment-2name.xml", true, false);
}
@Test
public void testSingleOrderingInWebFragmentXml() throws Exception {
WebXml webXml = new WebXml();
parse(webXml, "web-fragment-1ordering.xml", true, true);
Assert.assertEquals(1, webXml.getBeforeOrdering().size());
Assert.assertTrue(webXml.getBeforeOrdering().contains("bar"));
}
@Test
public void testMultipleOrderingInWebFragmentXml() throws Exception {
parse(new WebXml(), "web-fragment-2ordering.xml", true, false);
}
@Test
public void testSingleOrderingInWebXml() throws Exception {
WebXml webXml = new WebXml();
parse(webXml, "web-1ordering.xml", false, true);
Assert.assertEquals(1, webXml.getAbsoluteOrdering().size());
Assert.assertTrue(webXml.getAbsoluteOrdering().contains("bar"));
}
@Test
public void testMultipleOrderingInWebXml() throws Exception {
parse(new WebXml(), "web-2ordering.xml", false, false);
}
@Test
public void testRecycle() throws Exception {
// Name
parse(new WebXml(), "web-fragment-2name.xml", true, false);
parse(new WebXml(), "web-fragment-1name.xml", true, true);
parse(new WebXml(), "web-fragment-2name.xml", true, false);
parse(new WebXml(), "web-fragment-1name.xml", true, true);
// Relative ordering
parse(new WebXml(), "web-fragment-2ordering.xml", true, false);
parse(new WebXml(), "web-fragment-1ordering.xml", true, true);
parse(new WebXml(), "web-fragment-2ordering.xml", true, false);
parse(new WebXml(), "web-fragment-1ordering.xml", true, true);
// Absolute ordering
parse(new WebXml(), "web-2ordering.xml", false, false);
parse(new WebXml(), "web-1ordering.xml", false, true);
parse(new WebXml(), "web-2ordering.xml", false, false);
parse(new WebXml(), "web-1ordering.xml", false, true);
}
@Test
public void testLifecycleMethodsDefinitions() throws Exception {
// post-construct and pre-destroy
parse(new WebXml(), "web-1lifecyclecallback.xml", false, true);
// conflicting post-construct definitions
parse(new WebXml(), "web-2lifecyclecallback.xml", false, false);
}
private synchronized void parse(WebXml webXml, String target,
boolean fragment, boolean expected) {
Digester d;
if (fragment) {
d = fragmentDigester;
fragmentRuleSet.recycle();
} else {
d = webDigester;
webRuleSet.recycle();
}
d.push(webXml);
File f = new File("test/org/apache/catalina/startup/" + target);
boolean result = true;
try (InputStream is = new FileInputStream(f);) {
d.parse(is);
} catch (Exception e) {
if (expected) {
// Didn't expect an exception
e.printStackTrace();
}
result = false;
}
if (expected) {
Assert.assertTrue(result);
} else {
Assert.assertFalse(result);
}
}
}

View File

@@ -0,0 +1,473 @@
/*
* 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.
*/
package org.apache.tomcat.util.descriptor.web;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.descriptor.DigesterFactory;
import org.apache.tomcat.util.descriptor.XmlErrorHandler;
import org.apache.tomcat.util.descriptor.XmlIdentifiers;
import org.apache.tomcat.util.digester.Digester;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Test case for {@link WebXml}.
*/
public class TestWebXml {
@Test
public void testParseVersion() {
WebXml webxml = new WebXml();
// Defaults
Assert.assertEquals(3, webxml.getMajorVersion());
Assert.assertEquals(1, webxml.getMinorVersion());
// Both get changed
webxml.setVersion("2.5");
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(5, webxml.getMinorVersion());
// unknown input should be ignored
webxml.setVersion("0.0");
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(5, webxml.getMinorVersion());
// null input should be ignored
webxml.setVersion(null);
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(5, webxml.getMinorVersion());
}
@Test
public void testParsePublicIdVersion22() {
WebXml webxml = new WebXml();
webxml.setPublicId(XmlIdentifiers.WEB_22_PUBLIC);
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(2, webxml.getMinorVersion());
Assert.assertEquals("2.2", webxml.getVersion());
}
@Test
public void testParsePublicIdVersion23() {
WebXml webxml = new WebXml();
webxml.setPublicId(XmlIdentifiers.WEB_23_PUBLIC);
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(3, webxml.getMinorVersion());
Assert.assertEquals("2.3", webxml.getVersion());
}
@Test
public void testParseVersion24() {
WebXml webxml = new WebXml();
webxml.setVersion("2.4");
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(4, webxml.getMinorVersion());
Assert.assertEquals("2.4", webxml.getVersion());
}
@Test
public void testParseVersion25() {
WebXml webxml = new WebXml();
webxml.setVersion("2.5");
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(5, webxml.getMinorVersion());
Assert.assertEquals("2.5", webxml.getVersion());
}
@Test
public void testParseVersion30() {
WebXml webxml = new WebXml();
webxml.setVersion("3.0");
Assert.assertEquals(3, webxml.getMajorVersion());
Assert.assertEquals(0, webxml.getMinorVersion());
Assert.assertEquals("3.0", webxml.getVersion());
}
@Test
public void testParseVersion31() {
WebXml webxml = new WebXml();
webxml.setVersion("3.1");
Assert.assertEquals(3, webxml.getMajorVersion());
Assert.assertEquals(1, webxml.getMinorVersion());
Assert.assertEquals("3.1", webxml.getVersion());
}
@Test
public void testValidateVersion22() throws IOException, SAXException {
doTestValidateVersion("2.2");
}
@Test
public void testValidateVersion23() throws IOException, SAXException {
doTestValidateVersion("2.3");
}
@Test
public void testValidateVersion24() throws IOException, SAXException {
doTestValidateVersion("2.4");
}
@Test
public void testValidateVersion25() throws IOException, SAXException {
doTestValidateVersion("2.5");
}
@Test
public void testValidateVersion30() throws IOException, SAXException {
doTestValidateVersion("3.0");
}
@Test
public void testValidateVersion31() throws IOException, SAXException {
doTestValidateVersion("3.1");
}
private void doTestValidateVersion(String version) throws IOException, SAXException {
WebXml webxml = new WebXml();
// Special cases
if ("2.2".equals(version)) {
webxml.setPublicId(XmlIdentifiers.WEB_22_PUBLIC);
} else if ("2.3".equals(version)) {
webxml.setPublicId(XmlIdentifiers.WEB_23_PUBLIC);
} else {
webxml.setVersion(version);
}
// Merged web.xml that is published as MERGED_WEB_XML context attribute
// in the simplest case consists of webapp's web.xml file
// plus the default conf/web.xml one.
Set<WebXml> defaults = new HashSet<>();
defaults.add(getDefaultWebXmlFragment());
webxml.merge(defaults);
Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(), true);
XmlErrorHandler handler = new XmlErrorHandler();
digester.setErrorHandler(handler);
InputSource is = new InputSource(new StringReader(webxml.toXml()));
WebXml webxmlResult = new WebXml();
digester.push(webxmlResult);
digester.parse(is);
Assert.assertEquals(0, handler.getErrors().size());
Assert.assertEquals(0, handler.getWarnings().size());
Assert.assertEquals(version, webxml.getVersion());
Assert.assertEquals(version, webxmlResult.getVersion());
}
// A simplified copy of ContextConfig.getDefaultWebXmlFragment().
// Assuming that global web.xml exists, host-specific web.xml does not exist.
private WebXml getDefaultWebXmlFragment() throws IOException, SAXException {
InputSource globalWebXml = new InputSource(new File("conf/web.xml")
.getAbsoluteFile().toURI().toString());
WebXml webXmlDefaultFragment = new WebXml();
webXmlDefaultFragment.setOverridable(true);
webXmlDefaultFragment.setDistributable(true);
webXmlDefaultFragment.setAlwaysAddWelcomeFiles(false);
Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(), true);
XmlErrorHandler handler = new XmlErrorHandler();
digester.setErrorHandler(handler);
digester.push(webXmlDefaultFragment);
digester.parse(globalWebXml);
Assert.assertEquals(0, handler.getErrors().size());
Assert.assertEquals(0, handler.getWarnings().size());
webXmlDefaultFragment.setReplaceWelcomeFiles(true);
// Assert that web.xml was parsed and is not empty. Default servlet is known to be there.
Assert.assertNotNull(webXmlDefaultFragment.getServlets().get("default"));
// Manually add some version specific features to ensure that these do
// not cause problems for the merged web.xml
// Filters were added in 2.3 so should be excluded in 2.2
FilterDef filterDef = new FilterDef();
filterDef.setFilterClass("org.apache.tomcat.DummyFilter");
filterDef.setFilterName("Dummy");
webXmlDefaultFragment.addFilter(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName("Dummy");
filterMap.addURLPatternDecoded("/*");
webXmlDefaultFragment.addFilterMapping(filterMap);
// Listeners were added in 2.3 so should be excluded in 2.2
webXmlDefaultFragment.addListener("org.apache.tomcat.DummyListener");
// resource-env-ref was added in 2.3 so should be excluded in 2.2
ContextResourceEnvRef resourceEnvRef = new ContextResourceEnvRef();
resourceEnvRef.setName("dummy");
resourceEnvRef.setType("dummy");
webXmlDefaultFragment.addResourceEnvRef(resourceEnvRef);
// ejb-local-ref was added in 2.3 so should be excluded in 2.2
ContextLocalEjb ejbLocalRef = new ContextLocalEjb();
ejbLocalRef.setName("dummy");
ejbLocalRef.setType("Session");
ejbLocalRef.setLocal("dummy");
ejbLocalRef.setHome("dummy");
webXmlDefaultFragment.addEjbLocalRef(ejbLocalRef);
// Servlet/run-as was added in 2.3 so should be excluded in 2.2
ServletDef servletDef = new ServletDef();
servletDef.setServletName("Dummy");
servletDef.setServletClass("org.apache.tomcat.DummyServlet");
servletDef.setRunAs("dummy");
webXmlDefaultFragment.addServlet(servletDef);
webXmlDefaultFragment.addServletMapping("/dummy", "Dummy");
// resource-ref/res-sharing-scope was added in 2.3 so should be excluded
// in 2.2
ContextResource contextResource = new ContextResource();
contextResource.setName("dummy");
contextResource.setType("dummy");
contextResource.setAuth("Container");
contextResource.setScope("Shareable");
webXmlDefaultFragment.addResourceRef(contextResource);
// security-constraint/display-name was added in 2.3 so should be
// excluded in 2.2
SecurityConstraint sc = new SecurityConstraint();
sc.setDisplayName("dummy");
SecurityCollection collection = new SecurityCollection();
collection.setName("dummy");
collection.addPatternDecoded("/*");
collection.addMethod("DELETE");
sc.addCollection(collection);
webXmlDefaultFragment.addSecurityConstraint(sc);
// service-ref was added in 2.4 so should be excluded in 2.3 and earlier
ContextService serviceRef = new ContextService();
serviceRef.setName("dummy");
serviceRef.setInterface("dummy");
webXmlDefaultFragment.addServiceRef(serviceRef);
// message-destination-ref was added in 2.4 so should be excluded in 2.3
// and earlier
MessageDestinationRef mdRef = new MessageDestinationRef();
mdRef.setName("dummy");
mdRef.setType("dummy");
mdRef.setUsage("Consumes");
webXmlDefaultFragment.addMessageDestinationRef(mdRef);
// message-destination was added in 2.4 so should be excluded in 2.3
// and earlier
MessageDestination md = new MessageDestination();
md.setName("dummy");
webXmlDefaultFragment.addMessageDestination(md);
// local-encoding-mapping-list was added in 2.4 so should be excluded in
// 2.3 and earlier
webXmlDefaultFragment.addLocaleEncodingMapping("en", "UTF-8");
// jsp-config was added in Servlet 2.4
webXmlDefaultFragment.addTaglib("dummy", "dummy");
// filter-mapping/dispatcher added in Servlet 2.4
filterMap.setDispatcher("REQUEST");
// listener-[description|display-name|icon] added in Servlet 2.4
// None of these are supported in WebXml
// filter-mapping/dispatcher/ASYNC added in Servlet 3.0
filterMap.setDispatcher("ASYNC");
// error-page with just location allowed in Servlet 3.0+
ErrorPage errorPage = new ErrorPage();
errorPage.setLocation("/dummy");
webXmlDefaultFragment.addErrorPage(errorPage);
// async-supported added to Servlet and Filter in 3.0
filterDef.setAsyncSupported("false");
servletDef.setAsyncSupported("false");
// session-cookie-config added in 3.0
SessionConfig sessionConfig = new SessionConfig();
sessionConfig.setCookieDomain("dummy");
webXmlDefaultFragment.setSessionConfig(sessionConfig);
// http-method-omission added in Servlet 3.0
// Let this trigger a validation error as dropping it silently could
// be a security concern
// multi-part-config added in Servlet 3.0
MultipartDef multiPart = new MultipartDef();
servletDef.setMultipartDef(multiPart);
// deny-uncovered-http-methods added in Servlet 3.1
webXmlDefaultFragment.setDenyUncoveredHttpMethods(true);
return webXmlDefaultFragment;
}
@Test
public void testLifecycleMethodsWebXml() {
WebXml webxml = new WebXml();
webxml.addPostConstructMethods("a", "a");
webxml.addPreDestroyMethods("b", "b");
WebXml fragment = new WebXml();
fragment.addPostConstructMethods("c", "c");
fragment.addPreDestroyMethods("d", "d");
Set<WebXml> fragments = new HashSet<>();
fragments.add(fragment);
webxml.merge(fragments);
Map<String, String> postConstructMethods = webxml.getPostConstructMethods();
Map<String, String> preDestroyMethods = webxml.getPreDestroyMethods();
Assert.assertEquals(1, postConstructMethods.size());
Assert.assertEquals(1, preDestroyMethods.size());
Assert.assertEquals("a", postConstructMethods.get("a"));
Assert.assertEquals("b", preDestroyMethods.get("b"));
}
@Test
public void testLifecycleMethodsWebFragments() {
WebXml webxml = new WebXml();
WebXml fragment1 = new WebXml();
fragment1.addPostConstructMethods("a", "a");
fragment1.addPreDestroyMethods("b", "b");
WebXml fragment2 = new WebXml();
fragment2.addPostConstructMethods("c", "c");
fragment2.addPreDestroyMethods("d", "d");
Set<WebXml> fragments = new HashSet<>();
fragments.add(fragment1);
fragments.add(fragment2);
webxml.merge(fragments);
Map<String, String> postConstructMethods = webxml.getPostConstructMethods();
Map<String, String> preDestroyMethods = webxml.getPreDestroyMethods();
Assert.assertEquals(2, postConstructMethods.size());
Assert.assertEquals(2, preDestroyMethods.size());
Assert.assertEquals("a", postConstructMethods.get("a"));
Assert.assertEquals("c", postConstructMethods.get("c"));
Assert.assertEquals("b", preDestroyMethods.get("b"));
Assert.assertEquals("d", preDestroyMethods.get("d"));
}
@Test
public void testLifecycleMethodsWebFragmentsWithConflicts() {
WebXml webxml = new WebXml();
WebXml fragment1 = new WebXml();
fragment1.addPostConstructMethods("a", "a");
fragment1.addPreDestroyMethods("b", "a");
WebXml fragment2 = new WebXml();
fragment2.addPostConstructMethods("a", "b");
Set<WebXml> fragments = new HashSet<>();
fragments.add(fragment1);
fragments.add(fragment2);
Assert.assertFalse(webxml.merge(fragments));
Assert.assertEquals(0, webxml.getPostConstructMethods().size());
WebXml fragment3 = new WebXml();
fragment3.addPreDestroyMethods("b", "b");
fragments.remove(fragment2);
fragments.add(fragment3);
Assert.assertFalse(webxml.merge(fragments));
Assert.assertEquals(0, webxml.getPreDestroyMethods().size());
}
@Test(expected=IllegalArgumentException.class)
public void testBug54387a() {
// Multiple servlets may not be mapped to the same url-pattern
WebXml webxml = new WebXml();
webxml.addServletMapping("/foo", "a");
webxml.addServletMapping("/foo", "b");
}
@Test(expected=IllegalArgumentException.class)
public void testBug54387b() {
// Multiple servlets may not be mapped to the same url-pattern
WebXml webxml = new WebXml();
WebXml f1 = new WebXml();
WebXml f2 = new WebXml();
HashSet<WebXml> fragments = new HashSet<>();
fragments.add(f1);
fragments.add(f2);
f1.addServletMapping("/foo", "a");
f2.addServletMapping("/foo", "b");
webxml.merge(fragments);
}
@Test
public void testBug54387c() {
// Multiple servlets may not be mapped to the same url-pattern but main
// web.xml takes priority
WebXml webxml = new WebXml();
WebXml f1 = new WebXml();
WebXml f2 = new WebXml();
HashSet<WebXml> fragments = new HashSet<>();
fragments.add(f1);
fragments.add(f2);
f1.addServletMapping("/foo", "a");
f2.addServletMapping("/foo", "b");
webxml.addServletMapping("/foo", "main");
webxml.merge(fragments);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,63 @@
/*
* 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.
*
*/
package org.apache.tomcat.util.file;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
public class TestConfigFileLoader {
@BeforeClass
public static void setup() {
TomcatURLStreamHandlerFactory.getInstance();
System.setProperty("catalina.base", "");
}
@Test
public void test01() throws IOException {
doTest("classpath:org/apache/catalina/mbeans-descriptors.xml");
}
@Test(expected=FileNotFoundException.class)
public void test02() throws IOException {
doTest("classpath:org/apache/catalina/foo");
}
@Test
public void test03() throws IOException {
doTest("test/webresources/dir1");
}
@Test(expected=FileNotFoundException.class)
public void test04() throws IOException {
doTest("test/webresources/unknown");
}
private void doTest(String path) throws IOException {
try (InputStream is = ConfigFileLoader.getInputStream(path)) {
Assert.assertNotNull(is);
}
}
}

View File

@@ -0,0 +1,96 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import java.io.IOException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
/**
* Base Test case for {@link LegacyCookieProcessor}. <b>Note</b> because of the
* use of <code>static final</code> constants in {@link LegacyCookieProcessor},
* each of these tests must be executed in a new JVM instance. The tests have
* been place in separate classes to facilitate this when running the unit tests
* via Ant.
*/
public abstract class CookiesBaseTest extends TomcatBaseTest {
/**
* Servlet for cookie naming test.
*/
public static class CookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String cookieName;
private final String cookieValue;
public CookieServlet(String cookieName, String cookieValue) {
this.cookieName = cookieName;
this.cookieValue = cookieValue;
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
try {
Cookie cookie = new Cookie(cookieName, cookieValue);
res.addCookie(cookie);
res.getWriter().write("Cookie name ok");
} catch (IllegalArgumentException iae) {
res.getWriter().write("Cookie name fail");
}
}
}
public static void addServlets(Tomcat tomcat) {
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.setCookieProcessor(new LegacyCookieProcessor());
Tomcat.addServlet(ctx, "invalid", new CookieServlet("na;me", "value"));
ctx.addServletMappingDecoded("/invalid", "invalid");
Tomcat.addServlet(ctx, "null", new CookieServlet(null, "value"));
ctx.addServletMappingDecoded("/null", "null");
Tomcat.addServlet(ctx, "blank", new CookieServlet("", "value"));
ctx.addServletMappingDecoded("/blank", "blank");
Tomcat.addServlet(ctx, "invalidFwd",
new CookieServlet("na/me", "value"));
ctx.addServletMappingDecoded("/invalidFwd", "invalidFwd");
Tomcat.addServlet(ctx, "invalidStrict",
new CookieServlet("$name", "value"));
ctx.addServletMappingDecoded("/invalidStrict", "invalidStrict");
Tomcat.addServlet(ctx, "valid", new CookieServlet("name", "value"));
ctx.addServletMappingDecoded("/valid", "valid");
Tomcat.addServlet(ctx, "switch", new CookieServlet("name", "val?ue"));
ctx.addServletMappingDecoded("/switch", "switch");
}
public abstract void testCookiesInstance() throws Exception;
}

View File

@@ -0,0 +1,84 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Test case for {@link LegacyCookieProcessor}. <b>Note</b> because of the use of <code>final
* static</code> constants in {@link LegacyCookieProcessor}, each of these tests must be
* executed in a new JVM instance. The tests have been place in separate classes
* to facilitate this when running the unit tests via Ant.
*/
public class TestBug49158 extends CookiesBaseTest {
public static final String path = "49158";
@Override
@Test
public void testCookiesInstance() throws Exception {
Tomcat tomcat = getTomcatInstance();
addServlets(tomcat);
tomcat.start();
Map<String,List<String>> headers = new HashMap<>();
ByteChunk res = new ByteChunk();
getUrl("http://localhost:" + getPort() + "/"+path, res, headers);
List<String> cookieHeaders = headers.get("Set-Cookie");
Assert.assertEquals("There should only be one Set-Cookie header in this test",
1, cookieHeaders.size());
}
public static void addServlets(Tomcat tomcat) {
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, path, new TestBug49158Servlet());
ctx.addServletMappingDecoded("/"+path, path);
}
public static class TestBug49158Servlet extends HttpServlet {
private static final long serialVersionUID = 2725990508758127399L;
@Override
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession();
session.invalidate();
session = req.getSession();
session.invalidate();
req.getSession();
}
}
}

View File

@@ -0,0 +1,344 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.startup.SimpleHttpClient;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestCookieParsing extends TomcatBaseTest {
private static final String[] COOKIES_WITH_EQUALS = new String[] {
"name=equals=middle", "name==equalsstart", "name=equalsend=" };
private static final String COOKIES_WITH_EQUALS_TRUNC = "name=equalsname=name=equalsend";
private static final String[] COOKIES_WITH_NAME_ONLY = new String[] {
"bob", "bob=" };
private static final String COOKIES_WITH_NAME_ONLY_CONCAT = "bob=bob=";
private static final String[] COOKIES_WITH_SEPS = new String[] {
"name=val/ue" };
private static final String COOKIES_WITH_SEPS_TRUNC = "name=val";
private static final String[] COOKIES_WITH_QUOTES = new String[] {
"name=\"val\\\"ue\"", "name=\"value\"" };
private static final String[] COOKIES_V0 = new String[] {
"$Version=0;name=\"val ue\"", "$Version=0;name=\"val\tue\""};
private static final String COOKIES_V0_CONCAT = "name=\"val ue\"name=\"val\tue\"";
private static final String[] COOKIES_V1 = new String[] {
"$Version=1;name=\"val ue\"", "$Version=1;name=\"val\tue\""};
private static final String COOKIES_V1_CONCAT = "name=\"val ue\"name=\"val\tue\"";
@Test
public void testLegacyWithEquals() throws Exception {
doTestLegacyEquals(true);
}
@Test
public void testLegacyWithoutEquals() throws Exception {
doTestLegacyEquals(false);
}
private void doTestLegacyEquals(boolean allowEquals) throws Exception {
LegacyCookieProcessor legacyCookieProcessor = new LegacyCookieProcessor();
legacyCookieProcessor.setAllowEqualsInValue(allowEquals);
// Need to allow name only cookies to handle equals at the start of
// the value
legacyCookieProcessor.setAllowNameOnly(true);
String expected;
if (allowEquals) {
expected = concat(COOKIES_WITH_EQUALS);
} else {
expected = COOKIES_WITH_EQUALS_TRUNC;
}
TestCookieParsingClient client = new TestCookieParsingClient(
legacyCookieProcessor, COOKIES_WITH_EQUALS, expected);
client.doRequest();
}
@Test
public void testRfc6265Equals() throws Exception {
// Always allows equals
TestCookieParsingClient client = new TestCookieParsingClient(
new Rfc6265CookieProcessor(), COOKIES_WITH_EQUALS, concat(COOKIES_WITH_EQUALS));
client.doRequest();
}
@Test
public void testLegacyWithNameOnly() throws Exception {
doTestLegacyNameOnly(true);
}
@Test
public void testLegacyWithoutNameOnly() throws Exception {
doTestLegacyNameOnly(false);
}
private void doTestLegacyNameOnly(boolean nameOnly) throws Exception {
LegacyCookieProcessor legacyCookieProcessor = new LegacyCookieProcessor();
legacyCookieProcessor.setAllowNameOnly(nameOnly);
String expected;
if (nameOnly) {
expected = COOKIES_WITH_NAME_ONLY_CONCAT;
} else {
expected = "";
}
TestCookieParsingClient client = new TestCookieParsingClient(
legacyCookieProcessor, COOKIES_WITH_NAME_ONLY, expected);
client.doRequest();
}
@Test
public void testRfc6265NameOnly() throws Exception {
// Always allows equals
TestCookieParsingClient client = new TestCookieParsingClient(
new Rfc6265CookieProcessor(), COOKIES_WITH_NAME_ONLY,
COOKIES_WITH_NAME_ONLY_CONCAT);
client.doRequest();
}
@Test
public void testRfc6265V0() throws Exception {
TestCookieParsingClient client = new TestCookieParsingClient(
new Rfc6265CookieProcessor(), COOKIES_V0, COOKIES_V0_CONCAT);
client.doRequest();
}
@Test
public void testRfc6265V1() throws Exception {
TestCookieParsingClient client = new TestCookieParsingClient(
new Rfc6265CookieProcessor(), COOKIES_V1, COOKIES_V1_CONCAT);
client.doRequest();
}
@Test
public void testLegacyWithSeps() throws Exception {
doTestLegacySeps(true, true);
}
@Test
public void testLegacyWithoutSeps() throws Exception {
doTestLegacySeps(false, true);
}
@Test
public void testLegacyWithFwdSlash() throws Exception {
doTestLegacySeps(true, false);
}
@Test
public void testLegacyWithoutFwdSlash() throws Exception {
doTestLegacySeps(false, false);
}
private void doTestLegacySeps(boolean seps, boolean fwdSlash) throws Exception {
LegacyCookieProcessor legacyCookieProcessor = new LegacyCookieProcessor();
legacyCookieProcessor.setAllowHttpSepsInV0(seps);
legacyCookieProcessor.setForwardSlashIsSeparator(fwdSlash);
String expected;
if (!seps && fwdSlash) {
expected = COOKIES_WITH_SEPS_TRUNC;
} else {
expected = concat(COOKIES_WITH_SEPS);
}
TestCookieParsingClient client = new TestCookieParsingClient(
legacyCookieProcessor, COOKIES_WITH_SEPS, expected);
client.doRequest();
}
@Test
public void testRfc6265Seps() throws Exception {
// Always allows equals
TestCookieParsingClient client = new TestCookieParsingClient(
new Rfc6265CookieProcessor(), COOKIES_WITH_SEPS, concat(COOKIES_WITH_SEPS));
client.doRequest();
}
@Test
public void testLegacyPreserveHeader() throws Exception {
LegacyCookieProcessor legacyCookieProcessor = new LegacyCookieProcessor();
String expected;
expected = concat(COOKIES_WITH_QUOTES);
TestCookieParsingClient client = new TestCookieParsingClient(
legacyCookieProcessor, true, COOKIES_WITH_QUOTES, expected);
client.doRequest();
}
@Test
public void testRfc6265PreserveHeader() throws Exception {
// Always allows equals
TestCookieParsingClient client = new TestCookieParsingClient(new Rfc6265CookieProcessor(),
true, COOKIES_WITH_QUOTES, concat(COOKIES_WITH_QUOTES));
client.doRequest();
}
private static String concat(String[] input) {
StringBuilder result = new StringBuilder();
for (String s : input) {
result.append(s);
}
return result.toString();
}
private class TestCookieParsingClient extends SimpleHttpClient {
private final CookieProcessor cookieProcessor;
private final String[] cookies;
private final String expected;
private final boolean echoHeader;
public TestCookieParsingClient(CookieProcessor cookieProcessor,
String[] cookies, String expected) {
this(cookieProcessor, false, cookies, expected);
}
public TestCookieParsingClient(CookieProcessor cookieProcessor,
boolean echoHeader, String[] cookies, String expected) {
this.cookieProcessor = cookieProcessor;
this.echoHeader = echoHeader;
this.cookies = cookies;
this.expected = expected;
}
private void doRequest() throws Exception {
Tomcat tomcat = getTomcatInstance();
Context root = tomcat.addContext("", TEMP_DIR);
root.setCookieProcessor(cookieProcessor);
if (echoHeader) {
Tomcat.addServlet(root, "Cookies", new EchoCookieHeader());
} else {
Tomcat.addServlet(root, "Cookies", new EchoCookies());
}
root.addServletMappingDecoded("/test", "Cookies");
tomcat.start();
// Open connection
setPort(tomcat.getConnector().getLocalPort());
connect();
StringBuilder request = new StringBuilder();
request.append("GET /test HTTP/1.0");
request.append(CRLF);
for (String cookie : cookies) {
request.append("Cookie: ");
request.append(cookie);
request.append(CRLF);
}
request.append(CRLF);
setRequest(new String[] {request.toString()});
processRequest(true); // blocks until response has been read
String response = getResponseBody();
// Close the connection
disconnect();
reset();
tomcat.stop();
Assert.assertEquals(expected, response);
}
@Override
public boolean isResponseBodyOK() {
return true;
}
}
private static class EchoCookies extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Cookie cookies[] = req.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
resp.getWriter().write(cookie.getName() + "=" +
cookie.getValue());
}
}
resp.flushBuffer();
}
}
private static class EchoCookieHeader extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.getCookies();
// Never do this in production code. It triggers an XSS.
Enumeration<String> cookieHeaders = req.getHeaders("Cookie");
while (cookieHeaders.hasMoreElements()) {
String cookieHeader = cookieHeaders.nextElement();
resp.getWriter().write(cookieHeader);
}
resp.flushBuffer();
}
}
}

View File

@@ -0,0 +1,407 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import javax.servlet.http.Cookie;
import org.junit.Assert;
import org.junit.Test;
public class TestCookieProcessorGeneration {
@Test
public void v0SimpleCookie() {
doTest(new Cookie("foo", "bar"), "foo=bar");
}
@Test
public void v0NullValue() {
doTest(new Cookie("foo", null), "foo=\"\"", "foo=");
}
@Test
public void v0QuotedValue() {
doTest(new Cookie("foo", "\"bar\""), "foo=\"bar\"");
}
@Test
public void v0ValueContainsSemicolon() {
doTest(new Cookie("foo", "a;b"), "foo=\"a;b\"; Version=1", null);
}
@Test
public void v0ValueContainsComma() {
doTest(new Cookie("foo", "a,b"), "foo=\"a,b\"; Version=1", null);
}
@Test
public void v0ValueContainsSpace() {
doTest(new Cookie("foo", "a b"), "foo=\"a b\"; Version=1", null);
}
@Test
public void v0ValueContainsEquals() {
Cookie cookie = new Cookie("foo", "a=b");
doTestDefaults(cookie, "foo=\"a=b\"; Version=1", "foo=a=b");
doTestAllowSeparators(cookie, "foo=a=b", "foo=a=b");
}
@Test
public void v0ValueContainsQuote() {
Cookie cookie = new Cookie("foo", "a\"b");
doTestDefaults(cookie,"foo=\"a\\\"b\"; Version=1", null);
doTestAllowSeparators(cookie,"foo=a\"b", null);
}
@Test
public void v0ValueContainsNonV0Separator() {
Cookie cookie = new Cookie("foo", "a()<>@:\\\"/[]?={}b");
doTestDefaults(cookie,"foo=\"a()<>@:\\\\\\\"/[]?={}b\"; Version=1", null);
doTestAllowSeparators(cookie,"foo=a()<>@:\\\"/[]?={}b", null);
}
@Test
public void v0ValueContainsBackslash() {
Cookie cookie = new Cookie("foo", "a\\b");
doTestDefaults(cookie, "foo=\"a\\\\b\"; Version=1", null);
doTestAllowSeparators(cookie, "foo=a\\b", null);
}
@Test
public void v0ValueContainsBackslashAtEnd() {
Cookie cookie = new Cookie("foo", "a\\");
doTestDefaults(cookie, "foo=\"a\\\\\"; Version=1", null);
doTestAllowSeparators(cookie, "foo=a\\", null);
}
@Test
public void v0ValueContainsBackslashAndQuote() {
Cookie cookie = new Cookie("foo", "a\"b\\c");
doTestDefaults(cookie, "foo=\"a\\\"b\\\\c\"; Version=1", null);
doTestAllowSeparators(cookie, "foo=a\"b\\c", null);
}
@Test
public void v1simpleCookie() {
Cookie cookie = new Cookie("foo", "bar");
cookie.setVersion(1);
doTest(cookie, "foo=bar; Version=1", "foo=bar");
}
@Test
public void v1NullValue() {
Cookie cookie = new Cookie("foo", null);
cookie.setVersion(1);
doTest(cookie, "foo=\"\"; Version=1", "foo=");
}
@Test
public void v1QuotedValue() {
Cookie cookie = new Cookie("foo", "\"bar\"");
cookie.setVersion(1);
doTest(cookie, "foo=\"bar\"; Version=1", "foo=\"bar\"");
}
@Test
public void v1ValueContainsSemicolon() {
Cookie cookie = new Cookie("foo", "a;b");
cookie.setVersion(1);
doTest(cookie, "foo=\"a;b\"; Version=1", null);
}
@Test
public void v1ValueContainsComma() {
Cookie cookie = new Cookie("foo", "a,b");
cookie.setVersion(1);
doTest(cookie, "foo=\"a,b\"; Version=1", null);
}
@Test
public void v1ValueContainsSpace() {
Cookie cookie = new Cookie("foo", "a b");
cookie.setVersion(1);
doTest(cookie, "foo=\"a b\"; Version=1", null);
}
@Test
public void v1ValueContainsEquals() {
Cookie cookie = new Cookie("foo", "a=b");
cookie.setVersion(1);
doTest(cookie, "foo=\"a=b\"; Version=1", "foo=a=b");
}
@Test
public void v1ValueContainsQuote() {
Cookie cookie = new Cookie("foo", "a\"b");
cookie.setVersion(1);
doTest(cookie, "foo=\"a\\\"b\"; Version=1", null);
}
@Test
public void v1ValueContainsNonV0Separator() {
Cookie cookie = new Cookie("foo", "a()<>@,;:\\\"/[]?={}b");
cookie.setVersion(1);
doTest(cookie, "foo=\"a()<>@,;:\\\\\\\"/[]?={}b\"; Version=1", null);
}
@Test
public void v1ValueContainsBackslash() {
Cookie cookie = new Cookie("foo", "a\\b");
cookie.setVersion(1);
doTest(cookie, "foo=\"a\\\\b\"; Version=1", null);
}
@Test
public void v1ValueContainsBackslashAndQuote() {
Cookie cookie = new Cookie("foo", "a\"b\\c");
cookie.setVersion(1);
doTest(cookie, "foo=\"a\\\"b\\\\c\"; Version=1", null);
}
@Test
public void v1ValueUTF8() {
String value = "\u2300";
Cookie cookie = new Cookie("foo", value);
cookie.setVersion(1);
doTest(cookie, (String) null, "foo=" + value);
}
@Test
public void v1TestMaxAgePositive() {
doV1TestMaxAge(100, "foo=bar; Version=1; Max-Age=100", "foo=bar; Max-Age=100");
}
@Test
public void v1TestMaxAgeZero() {
doV1TestMaxAge(0, "foo=bar; Version=1; Max-Age=0",
"foo=bar; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT");
}
@Test
public void v1TestMaxAgeNegative() {
doV1TestMaxAge(-100, "foo=bar; Version=1", "foo=bar");
}
@Test
public void v1TestDomainValid01() {
doV1TestDomain("example.com", "foo=bar; Version=1; Domain=example.com",
"foo=bar; Domain=example.com");
}
@Test
public void v1TestDomainValid02() {
doV1TestDomain("exa-mple.com", "foo=bar; Version=1; Domain=exa-mple.com",
"foo=bar; Domain=exa-mple.com");
}
@Test
public void v1TestDomainInvalid01() {
doV1TestDomain("example.com.", "foo=bar; Version=1; Domain=example.com.", null);
}
@Test
public void v1TestDomainInvalid02() {
doV1TestDomain("example.com-", "foo=bar; Version=1; Domain=example.com-", null);
}
@Test
public void v1TestDomainInvalid03() {
doV1TestDomain(".example.com.", "foo=bar; Version=1; Domain=.example.com.", null);
}
@Test
public void v1TestDomainInvalid04() {
doV1TestDomain("-example.com.", "foo=bar; Version=1; Domain=-example.com.", null);
}
@Test
public void v1TestDomainInvalid05() {
doV1TestDomain("example..com.", "foo=bar; Version=1; Domain=example..com.", null);
}
@Test
public void v1TestDomainInvalid06() {
doV1TestDomain("example-.com.", "foo=bar; Version=1; Domain=example-.com.", null);
}
@Test
public void v1TestDomainInvalid07() {
doV1TestDomain("exam$ple.com.", "foo=bar; Version=1; Domain=exam$ple.com.", null);
}
@Test
public void v1TestPathValid() {
doV1TestPath("/example", "foo=bar; Version=1; Path=/example",
"foo=bar; Path=/example");
}
@Test
public void v1TestPathInvalid01() {
doV1TestPath("exa\tmple", "foo=bar; Version=1; Path=\"exa\tmple\"", null);
}
@Test
public void testSameSiteCookies() {
LegacyCookieProcessor legacy = new LegacyCookieProcessor();
Rfc6265CookieProcessor rfc6265 = new Rfc6265CookieProcessor();
Cookie cookie = new Cookie("foo", "bar");
Assert.assertEquals("foo=bar", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar", rfc6265.generateHeader(cookie));
legacy.setSameSiteCookies("unset");
rfc6265.setSameSiteCookies("unset");
Assert.assertEquals("foo=bar", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar", rfc6265.generateHeader(cookie));
legacy.setSameSiteCookies("none");
rfc6265.setSameSiteCookies("none");
Assert.assertEquals("foo=bar; SameSite=None", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar; SameSite=None", rfc6265.generateHeader(cookie));
legacy.setSameSiteCookies("lax");
rfc6265.setSameSiteCookies("lax");
Assert.assertEquals("foo=bar; SameSite=Lax", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar; SameSite=Lax", rfc6265.generateHeader(cookie));
legacy.setSameSiteCookies("strict");
rfc6265.setSameSiteCookies("strict");
Assert.assertEquals("foo=bar; SameSite=Strict", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar; SameSite=Strict", rfc6265.generateHeader(cookie));
cookie.setSecure(true);
cookie.setHttpOnly(true);
legacy.setSameSiteCookies("unset");
rfc6265.setSameSiteCookies("unset");
Assert.assertEquals("foo=bar; Secure; HttpOnly", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar; Secure; HttpOnly", rfc6265.generateHeader(cookie));
legacy.setSameSiteCookies("none");
rfc6265.setSameSiteCookies("none");
Assert.assertEquals("foo=bar; Secure; HttpOnly; SameSite=None", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar; Secure; HttpOnly; SameSite=None", rfc6265.generateHeader(cookie));
legacy.setSameSiteCookies("lax");
rfc6265.setSameSiteCookies("lax");
Assert.assertEquals("foo=bar; Secure; HttpOnly; SameSite=Lax", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar; Secure; HttpOnly; SameSite=Lax", rfc6265.generateHeader(cookie));
legacy.setSameSiteCookies("strict");
rfc6265.setSameSiteCookies("strict");
Assert.assertEquals("foo=bar; Secure; HttpOnly; SameSite=Strict", legacy.generateHeader(cookie));
Assert.assertEquals("foo=bar; Secure; HttpOnly; SameSite=Strict", rfc6265.generateHeader(cookie));
}
private void doTest(Cookie cookie, String expected) {
doTest(cookie, expected, expected);
}
private void doTest(Cookie cookie,
String expectedLegacy, String expectedRfc6265) {
doTestDefaults(cookie, expectedLegacy, expectedRfc6265);
doTestAllowSeparators(cookie, expectedLegacy, expectedRfc6265);
}
private void doTestDefaults(Cookie cookie,
String expectedLegacy, String expectedRfc6265) {
CookieProcessor legacy = new LegacyCookieProcessor();
CookieProcessor rfc6265 = new Rfc6265CookieProcessor();
doTest(cookie, legacy, expectedLegacy, rfc6265, expectedRfc6265);
}
private void doTestAllowSeparators(Cookie cookie,
String expectedLegacy, String expectedRfc6265) {
LegacyCookieProcessor legacy = new LegacyCookieProcessor();
legacy.setAllowHttpSepsInV0(true);
legacy.setForwardSlashIsSeparator(true);
CookieProcessor rfc6265 = new Rfc6265CookieProcessor();
doTest(cookie, legacy, expectedLegacy, rfc6265, expectedRfc6265);
}
private void doTest(Cookie cookie,
CookieProcessor legacy, String expectedLegacy,
CookieProcessor rfc6265, String expectedRfc6265) {
doTest(cookie, legacy, expectedLegacy);
doTest(cookie, rfc6265, expectedRfc6265);
}
private void doTest(Cookie cookie, CookieProcessor cookieProcessor, String expected) {
if (expected == null) {
IllegalArgumentException e = null;
try {
cookieProcessor.generateHeader(cookie);
} catch (IllegalArgumentException iae) {
e = iae;
}
Assert.assertNotNull("Failed to throw IAE", e);
} else {
if (cookieProcessor instanceof Rfc6265CookieProcessor &&
cookie.getMaxAge() > 0) {
// Expires attribute will depend on time cookie is generated so
// use a modified test
Assert.assertTrue(cookieProcessor.generateHeader(cookie).startsWith(expected));
} else {
Assert.assertEquals(expected, cookieProcessor.generateHeader(cookie));
}
}
}
private void doV1TestMaxAge(int age, String expectedLegacy, String expectedRfc6265) {
LegacyCookieProcessor legacy = new LegacyCookieProcessor();
legacy.setAlwaysAddExpires(false);
Cookie cookie = new Cookie("foo", "bar");
cookie.setVersion(1);
cookie.setMaxAge(age);
doTest(cookie, legacy, expectedLegacy, new Rfc6265CookieProcessor(), expectedRfc6265);
}
private void doV1TestDomain(String domain, String expectedLegacy, String expectedRfc6265) {
LegacyCookieProcessor legacy = new LegacyCookieProcessor();
legacy.setAlwaysAddExpires(false);
Cookie cookie = new Cookie("foo", "bar");
cookie.setVersion(1);
cookie.setDomain(domain);
doTest(cookie, legacy, expectedLegacy, new Rfc6265CookieProcessor(), expectedRfc6265);
}
private void doV1TestPath(String path, String expectedLegacy, String expectedRfc6265) {
LegacyCookieProcessor legacy = new LegacyCookieProcessor();
legacy.setAlwaysAddExpires(false);
Cookie cookie = new Cookie("foo", "bar");
cookie.setVersion(1);
cookie.setPath(path);
doTest(cookie, legacy, expectedLegacy, new Rfc6265CookieProcessor(), expectedRfc6265);
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestCookieProcessorGenerationHttp extends TomcatBaseTest {
@Test
public void testUtf8CookieValue() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.setCookieProcessor(new Rfc6265CookieProcessor());
Tomcat.addServlet(ctx, "test", new CookieServlet("\u0120"));
ctx.addServletMappingDecoded("/test", "test");
tomcat.start();
Map<String,List<String>> headers = new HashMap<>();
ByteChunk res = new ByteChunk();
getUrl("http://localhost:" + getPort() + "/test", res, headers);
List<String> cookieHeaders = headers.get("Set-Cookie");
Assert.assertEquals("There should only be one Set-Cookie header in this test",
1, cookieHeaders.size());
// Client is assuming header is ISO-8859-1 encoding which it isn't. Turn
// the header value back into the received bytes (this isn't guaranteed
// to work with all values but it will for this test value)
byte[] headerBytes = cookieHeaders.get(0).getBytes(StandardCharsets.ISO_8859_1);
// Now convert those bytes to a String using UTF-8
String utf8Header = new String(headerBytes, StandardCharsets.UTF_8);
Assert.assertEquals("Test=\u0120", utf8Header);
}
private static class CookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String cookieValue;
public CookieServlet(String cookieValue) {
this.cookieValue = cookieValue;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Cookie cookie = new Cookie("Test", cookieValue);
resp.addCookie(cookie);
resp.setContentType("text/plain");
resp.getWriter().print("OK");
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,68 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Test case for {@link LegacyCookieProcessor}. <b>Note</b> because of the use
* of <code>final static</code> constants in the cookie processing code, each of
* these tests must be executed in a new JVM instance. The tests have been place
* in separate classes to facilitate this when running the unit tests via Ant.
*/
public class TestCookiesDefaultSysProps extends CookiesBaseTest {
@Override
@Test
public void testCookiesInstance() throws Exception {
Tomcat tomcat = getTomcatInstance();
addServlets(tomcat);
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/invalid");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/null");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/blank");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidFwd");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidStrict");
Assert.assertEquals("Cookie name ok", res.toString());
res = getUrl("http://localhost:" + getPort() + "/valid");
Assert.assertEquals("Cookie name ok", res.toString());
// Need to read response headers to test version switching
Map<String,List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/switch", res, headers);
List<String> cookieHeaders = headers.get("Set-Cookie");
for (String cookieHeader : cookieHeaders) {
Assert.assertEquals("name=\"val?ue\"; Version=1", cookieHeader);
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Test case for {@link LegacyCookieProcessor}. <b>Note</b> because of the use
* of <code>final static</code> constants in the cookie processing code, each of
* these tests must be executed in a new JVM instance. The tests have been place
* in separate classes to facilitate this when running the unit tests via Ant.
*/
public class TestCookiesNoFwdStrictSysProps extends CookiesBaseTest {
@Override
@Test
public void testCookiesInstance() throws Exception {
System.setProperty("org.apache.catalina.STRICT_SERVLET_COMPLIANCE",
"true");
System.setProperty("org.apache.tomcat.util.http.ServerCookie.FWD_SLASH_IS_SEPARATOR",
"false");
Tomcat tomcat = getTomcatInstance();
addServlets(tomcat);
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/invalid");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/null");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/blank");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidFwd");
Assert.assertEquals("Cookie name ok", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidStrict");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/valid");
Assert.assertEquals("Cookie name ok", res.toString());
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Test case for {@link LegacyCookieProcessor}. <b>Note</b> because of the use
* of <code>final static</code> constants in the cookie processing code, each of
* these tests must be executed in a new JVM instance. The tests have been place
* in separate classes to facilitate this when running the unit tests via Ant.
*/
public class TestCookiesNoStrictNamingSysProps extends CookiesBaseTest {
@Override
@Test
public void testCookiesInstance() throws Exception {
System.setProperty("org.apache.catalina.STRICT_SERVLET_COMPLIANCE",
"true");
System.setProperty("org.apache.tomcat.util.http.ServerCookie.STRICT_NAMING",
"false");
Tomcat tomcat = getTomcatInstance();
addServlets(tomcat);
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/invalid");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/null");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/blank");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidFwd");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidStrict");
Assert.assertEquals("Cookie name ok", res.toString());
res = getUrl("http://localhost:" + getPort() + "/valid");
Assert.assertEquals("Cookie name ok", res.toString());
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Test case for {@link LegacyCookieProcessor}. <b>Note</b> because of the use
* of <code>final static</code> constants in the cookie processing code, each of
* these tests must be executed in a new JVM instance. The tests have been place
* in separate classes to facilitate this when running the unit tests via Ant.
*/
public class TestCookiesStrictSysProps extends CookiesBaseTest {
@Override
@Test
public void testCookiesInstance() throws Exception {
System.setProperty("org.apache.catalina.STRICT_SERVLET_COMPLIANCE",
"true");
Tomcat tomcat = getTomcatInstance();
addServlets(tomcat);
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/invalid");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/null");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/blank");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidFwd");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/invalidStrict");
Assert.assertEquals("Cookie name fail", res.toString());
res = getUrl("http://localhost:" + getPort() + "/valid");
Assert.assertEquals("Cookie name ok", res.toString());
// Need to read response headers to test version switching
Map<String,List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/switch", res, headers);
List<String> cookieHeaders = headers.get("Set-Cookie");
for (String cookieHeader : cookieHeaders) {
Assert.assertEquals("name=\"val?ue\"; Version=1", cookieHeader);
}
}
}

View File

@@ -0,0 +1,83 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
@RunWith(Parameterized.class)
public class TestHeaderUtiltoPrintableString {
@Parameterized.Parameters(name = "{index}: expected[{1}]")
public static Collection<Object[]> parameters() {
List<Object[]> parameterSets = new ArrayList<>();
parameterSets.add(new String[] { "", "" });
parameterSets.add(new String[] { "abcd", "abcd" });
parameterSets.add(new String[] { "\u0000abcd", "0x00abcd" });
parameterSets.add(new String[] { "ab\u0000cd", "ab0x00cd" });
parameterSets.add(new String[] { "abcd\u0000", "abcd0x00" });
parameterSets.add(new String[] { "\tabcd", "0x09abcd" });
parameterSets.add(new String[] { "ab\tcd", "ab0x09cd" });
parameterSets.add(new String[] { "abcd\t", "abcd0x09" });
parameterSets.add(new String[] { " abcd", " abcd" });
parameterSets.add(new String[] { "ab cd", "ab cd" });
parameterSets.add(new String[] { "abcd ", "abcd " });
parameterSets.add(new String[] { "~abcd", "~abcd" });
parameterSets.add(new String[] { "ab~cd", "ab~cd" });
parameterSets.add(new String[] { "abcd~", "abcd~" });
parameterSets.add(new String[] { "\u007fabcd", "0x7fabcd" });
parameterSets.add(new String[] { "ab\u007fcd", "ab0x7fcd" });
parameterSets.add(new String[] { "abcd\u007f", "abcd0x7f" });
parameterSets.add(new String[] { "\u00a3abcd", "0xa3abcd" });
parameterSets.add(new String[] { "ab\u00a3cd", "ab0xa3cd" });
parameterSets.add(new String[] { "abcd\u00a3", "abcd0xa3" });
return parameterSets;
}
@Parameter(0)
public String input;
@Parameter(1)
public String expected;
@Test
public void doTest() {
byte[] bytes = input.getBytes(StandardCharsets.ISO_8859_1);
String result = HeaderUtil.toPrintableString(bytes, 0, bytes.length);
Assert.assertEquals(expected, result);
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.buf.MessageBytes;
public class TestLegacyCookieProcessor {
/*
* https://bz.apache.org/bugzilla/show_bug.cgi?id=59925
*/
@Test
public void testV0WithPath() {
LegacyCookieProcessor cp = new LegacyCookieProcessor();
cp.setAllowHttpSepsInV0(true);
cp.setForwardSlashIsSeparator(true);
MimeHeaders mimeHeaders = new MimeHeaders();
ServerCookies serverCookies = new ServerCookies(4);
MessageBytes cookieHeaderValue = mimeHeaders.addValue("Cookie");
byte[] bytes = "$Version=0;cname=cvalue;$Path=/example".getBytes(StandardCharsets.UTF_8);
cookieHeaderValue.setBytes(bytes, 0, bytes.length);
cp.parseCookieHeader(mimeHeaders, serverCookies);
Assert.assertEquals(1, serverCookies.getCookieCount());
for (int i = 0; i < 1; i++) {
ServerCookie actual = serverCookies.getCookie(i);
Assert.assertEquals(0, actual.getVersion());
Assert.assertEquals("cname", actual.getName().toString());
actual.getValue().getByteChunk().setCharset(StandardCharsets.UTF_8);
Assert.assertEquals("cvalue",
org.apache.tomcat.util.http.parser.Cookie.unescapeCookieValueRfc2109(
actual.getValue().toString()));
Assert.assertEquals("/example", actual.getPath().toString());
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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.
*/
package org.apache.tomcat.util.http;
import org.junit.Assert;
import org.junit.Test;
public class TestMimeHeaders {
public static final String HEADER_NAME_LC_STRING = "test";
public static final String HEADER_NAME_UC_STRING = "TEST";
public static final String HEADER_NAME_MIXED_STRING = "tEsT";
@Test
public void testSetValueStringIgnoresCase01() {
MimeHeaders mh = new MimeHeaders();
mh.setValue(HEADER_NAME_LC_STRING).setString(HEADER_NAME_LC_STRING);
mh.setValue(HEADER_NAME_UC_STRING).setString(HEADER_NAME_UC_STRING);
Assert.assertEquals(HEADER_NAME_UC_STRING, mh.getValue(HEADER_NAME_UC_STRING).toString());
Assert.assertEquals(HEADER_NAME_UC_STRING, mh.getValue(HEADER_NAME_LC_STRING).toString());
Assert.assertEquals(HEADER_NAME_UC_STRING, mh.getValue(HEADER_NAME_MIXED_STRING).toString());
}
@Test
public void testSetValueStringIgnoresCase02() {
MimeHeaders mh = new MimeHeaders();
mh.setValue(HEADER_NAME_UC_STRING).setString(HEADER_NAME_UC_STRING);
mh.setValue(HEADER_NAME_LC_STRING).setString(HEADER_NAME_LC_STRING);
Assert.assertEquals(HEADER_NAME_LC_STRING, mh.getValue(HEADER_NAME_LC_STRING).toString());
Assert.assertEquals(HEADER_NAME_LC_STRING, mh.getValue(HEADER_NAME_UC_STRING).toString());
Assert.assertEquals(HEADER_NAME_LC_STRING, mh.getValue(HEADER_NAME_MIXED_STRING).toString());
}
@Test
public void testSetValueStringIgnoresCase03() {
MimeHeaders mh = new MimeHeaders();
mh.setValue(HEADER_NAME_UC_STRING).setString(HEADER_NAME_UC_STRING);
mh.setValue(HEADER_NAME_MIXED_STRING).setString(HEADER_NAME_MIXED_STRING);
Assert.assertEquals(HEADER_NAME_MIXED_STRING, mh.getValue(HEADER_NAME_LC_STRING).toString());
Assert.assertEquals(HEADER_NAME_MIXED_STRING, mh.getValue(HEADER_NAME_UC_STRING).toString());
Assert.assertEquals(HEADER_NAME_MIXED_STRING, mh.getValue(HEADER_NAME_MIXED_STRING).toString());
}
}

Some files were not shown because too many files have changed in this diff Show More