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,138 @@
/*
* 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.jasper;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class TestJspC {
private JspC jspc;
private File outputDir;
@Before
public void init() {
File tempDir = new File(System.getProperty("tomcat.test.temp",
"output/tmp"));
outputDir = new File(tempDir, "jspc");
jspc = new JspC();
}
@After
public void cleanup() throws IOException {
remove(outputDir);
}
@Test
public void precompileWebapp_2_2() throws IOException {
File appDir = new File("test/webapp-2.2");
File webappOut = new File(outputDir, appDir.getName());
precompile(appDir, webappOut);
verify(webappOut);
}
@Test
public void precompileWebapp_2_3() throws IOException {
File appDir = new File("test/webapp-2.3");
File webappOut = new File(outputDir, appDir.getName());
precompile(appDir, webappOut);
verify(webappOut);
}
@Test
public void precompileWebapp_2_4() throws IOException {
File appDir = new File("test/webapp-2.4");
File webappOut = new File(outputDir, appDir.getName());
precompile(appDir, webappOut);
verify(webappOut);
}
@Test
public void precompileWebapp_2_5() throws IOException {
File appDir = new File("test/webapp-2.5");
File webappOut = new File(outputDir, appDir.getName());
precompile(appDir, webappOut);
verify(webappOut);
}
@Test
public void precompileWebapp_3_0() throws IOException {
File appDir = new File("test/webapp-3.0");
File webappOut = new File(outputDir, appDir.getName());
precompile(appDir, webappOut);
verify(webappOut);
}
@Test
public void precompileWebapp_3_1() throws IOException {
File appDir = new File("test/webapp-3.1");
File webappOut = new File(outputDir, appDir.getName());
precompile(appDir, webappOut);
verify(webappOut);
}
private void verify(File webappOut) {
// for now, just check some expected files exist
Assert.assertTrue(new File(webappOut, "generated_web.xml").exists());
Assert.assertTrue(new File(webappOut,
"org/apache/jsp/el_002das_002dliteral_jsp.java").exists());
Assert.assertTrue(new File(webappOut,
"org/apache/jsp/tld_002dversions_jsp.java").exists());
}
private void precompile(File appDir, File webappOut) throws IOException {
remove(webappOut);
Assert.assertTrue("Failed to create [" + webappOut + "]", webappOut.mkdirs());
jspc.setUriroot(appDir.toString());
jspc.setOutputDir(webappOut.toString());
jspc.setValidateTld(false);
jspc.setWebXml(new File(webappOut, "generated_web.xml").toString());
jspc.execute();
}
private void remove(File base) throws IOException{
if (!base.exists()) {
return;
}
Files.walkFileTree(base.toPath(), new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir,
IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
}

View File

@@ -0,0 +1,74 @@
/*
* 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.jasper;
import java.io.File;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestJspCompilationContext extends TomcatBaseTest {
@Test
public void testTagFileInJar() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk body = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/jsp/tagFileInJar.jsp", body, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertTrue(body.toString().contains("00 - OK"));
}
/*
* Test case for https://bz.apache.org/bugzilla/show_bug.cgi?id=57626
*/
@Test
public void testModifiedTagFileInJar() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk body = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/jsp/tagFileInJar.jsp", body, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertTrue(body.toString().contains("00 - OK"));
File jsp = new File("test/webapp/jsp/tagFileInJar.jsp");
Assert.assertTrue("Failed to set last modified for [" + jsp + "]",
jsp.setLastModified(jsp.lastModified() + 10000));
// This test requires that modificationTestInterval is set to zero in
// web.xml. If not, a sleep longer that modificationTestInterval is
// required here.
rc = getUrl("http://localhost:" + getPort() +
"/test/jsp/tagFileInJar.jsp", body, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertTrue(body.toString().contains("00 - OK"));
}
}

View File

@@ -0,0 +1,220 @@
/*
* 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.jasper.compiler;
import org.apache.jasper.JasperException;
import org.xml.sax.Attributes;
class Dumper {
static class DumpVisitor extends Node.Visitor {
private int indent = 0;
private String getAttributes(Attributes attrs) {
if (attrs == null)
return "";
StringBuilder buf = new StringBuilder();
for (int i=0; i < attrs.getLength(); i++) {
buf.append(" " + attrs.getQName(i) + "=\""
+ attrs.getValue(i) + "\"");
}
return buf.toString();
}
private void printString(String str) {
printIndent();
System.out.print(str);
}
private void printString(String prefix, String str, String suffix) {
printIndent();
if (str != null) {
System.out.print(prefix + str + suffix);
} else {
System.out.print(prefix + suffix);
}
}
private void printAttributes(String prefix, Attributes attrs,
String suffix) {
printString(prefix, getAttributes(attrs), suffix);
}
private void dumpBody(Node n) throws JasperException {
Node.Nodes page = n.getBody();
if (page != null) {
// indent++;
page.visit(this);
// indent--;
}
}
@Override
public void visit(Node.PageDirective n) throws JasperException {
printAttributes("<%@ page", n.getAttributes(), "%>");
}
@Override
public void visit(Node.TaglibDirective n) throws JasperException {
printAttributes("<%@ taglib", n.getAttributes(), "%>");
}
@Override
public void visit(Node.IncludeDirective n) throws JasperException {
printAttributes("<%@ include", n.getAttributes(), "%>");
dumpBody(n);
}
@Override
public void visit(Node.Comment n) throws JasperException {
printString("<%--", n.getText(), "--%>");
}
@Override
public void visit(Node.Declaration n) throws JasperException {
printString("<%!", n.getText(), "%>");
}
@Override
public void visit(Node.Expression n) throws JasperException {
printString("<%=", n.getText(), "%>");
}
@Override
public void visit(Node.Scriptlet n) throws JasperException {
printString("<%", n.getText(), "%>");
}
@Override
public void visit(Node.IncludeAction n) throws JasperException {
printAttributes("<jsp:include", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:include>");
}
@Override
public void visit(Node.ForwardAction n) throws JasperException {
printAttributes("<jsp:forward", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:forward>");
}
@Override
public void visit(Node.GetProperty n) throws JasperException {
printAttributes("<jsp:getProperty", n.getAttributes(), "/>");
}
@Override
public void visit(Node.SetProperty n) throws JasperException {
printAttributes("<jsp:setProperty", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:setProperty>");
}
@Override
public void visit(Node.UseBean n) throws JasperException {
printAttributes("<jsp:useBean", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:useBean>");
}
@Override
public void visit(Node.PlugIn n) throws JasperException {
printAttributes("<jsp:plugin", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:plugin>");
}
@Override
public void visit(Node.ParamsAction n) throws JasperException {
printAttributes("<jsp:params", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:params>");
}
@Override
public void visit(Node.ParamAction n) throws JasperException {
printAttributes("<jsp:param", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:param>");
}
@Override
public void visit(Node.NamedAttribute n) throws JasperException {
printAttributes("<jsp:attribute", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:attribute>");
}
@Override
public void visit(Node.JspBody n) throws JasperException {
printAttributes("<jsp:body", n.getAttributes(), ">");
dumpBody(n);
printString("</jsp:body>");
}
@Override
public void visit(Node.ELExpression n) throws JasperException {
printString( "${" + n.getText() + "}" );
}
@Override
public void visit(Node.CustomTag n) throws JasperException {
printAttributes("<" + n.getQName(), n.getAttributes(), ">");
dumpBody(n);
printString("</" + n.getQName() + ">");
}
@Override
public void visit(Node.UninterpretedTag n) throws JasperException {
String tag = n.getQName();
printAttributes("<"+tag, n.getAttributes(), ">");
dumpBody(n);
printString("</" + tag + ">");
}
@Override
public void visit(Node.TemplateText n) throws JasperException {
printString(n.getText());
}
private void printIndent() {
for (int i=0; i < indent; i++) {
System.out.print(" ");
}
}
}
public static void dump(Node n) {
try {
n.accept(new DumpVisitor());
} catch (JasperException e) {
e.printStackTrace();
}
}
public static void dump(Node.Nodes page) {
try {
page.visit(new DumpVisitor());
} catch (JasperException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,179 @@
/*
* 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.jasper.compiler;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
import org.apache.el.ExpressionFactoryImpl;
import org.apache.el.TesterFunctions;
import org.apache.jasper.el.ELContextImpl;
/**
* Test the EL processing from JSP attributes. Similar tests may be found in
* {@link org.apache.el.TestELEvaluation} and {@link org.apache.el.TestELInJsp}.
*/
public class TestAttributeParser {
/**
* Test use of spaces in ternary expressions. This was primarily an EL
* parser bug.
*/
@Test
public void testBug42565() {
Assert.assertEquals("false", evalAttr("${false?true:false}", '\"'));
Assert.assertEquals("false", evalAttr("${false?true: false}", '\"'));
Assert.assertEquals("false", evalAttr("${false?true :false}", '\"'));
Assert.assertEquals("false", evalAttr("${false?true : false}", '\"'));
Assert.assertEquals("false", evalAttr("${false? true:false}", '\"'));
Assert.assertEquals("false", evalAttr("${false? true: false}", '\"'));
Assert.assertEquals("false", evalAttr("${false? true :false}", '\"'));
Assert.assertEquals("false", evalAttr("${false? true : false}", '\"'));
Assert.assertEquals("false", evalAttr("${false ?true:false}", '\"'));
Assert.assertEquals("false", evalAttr("${false ?true: false}", '\"'));
Assert.assertEquals("false", evalAttr("${false ?true :false}", '\"'));
Assert.assertEquals("false", evalAttr("${false ?true : false}", '\"'));
Assert.assertEquals("false", evalAttr("${false ? true:false}", '\"'));
Assert.assertEquals("false", evalAttr("${false ? true: false}", '\"'));
Assert.assertEquals("false", evalAttr("${false ? true :false}", '\"'));
Assert.assertEquals("false", evalAttr("${false ? true : false}", '\"'));
}
/**
* Test use nested ternary expressions. Full tests in
* {@link org.apache.el.TestELEvaluation}. This is just a smoke test to
* ensure JSP attribute processing doesn't cause any additional issues.
*/
@Test
public void testBug44994() {
Assert.assertEquals("none",
evalAttr("${0 lt 0 ? 1 lt 0 ? 'many': 'one': 'none'}", '\"'));
Assert.assertEquals("one",
evalAttr("${0 lt 1 ? 1 lt 1 ? 'many': 'one': 'none'}", '\"'));
Assert.assertEquals("many",
evalAttr("${0 lt 2 ? 1 lt 2 ? 'many': 'one': 'none'}", '\"'));
}
/**
* Test the quoting requirements of JSP attributes. This doesn't make use of
* EL. See {@link #testBug45451()} for a test that combines JSP attribute
* quoting and EL quoting.
*/
@Test
public void testBug45015() {
// Warning: Java String quoting vs. JSP attribute quoting
Assert.assertEquals("hello 'world'", evalAttr("hello 'world'", '\"'));
Assert.assertEquals("hello 'world", evalAttr("hello 'world", '\"'));
Assert.assertEquals("hello world'", evalAttr("hello world'", '\"'));
Assert.assertEquals("hello world'", evalAttr("hello world\\'", '\"'));
Assert.assertEquals("hello world\"", evalAttr("hello world\\\"", '\"'));
Assert.assertEquals("hello \"world\"", evalAttr("hello \"world\"", '\"'));
Assert.assertEquals("hello \"world", evalAttr("hello \"world", '\"'));
Assert.assertEquals("hello world\"", evalAttr("hello world\"", '\"'));
Assert.assertEquals("hello world'", evalAttr("hello world\\'", '\"'));
Assert.assertEquals("hello world\"", evalAttr("hello world\\\"", '\"'));
Assert.assertEquals("hello 'world'", evalAttr("hello 'world'", '\''));
Assert.assertEquals("hello 'world", evalAttr("hello 'world", '\''));
Assert.assertEquals("hello world'", evalAttr("hello world'", '\''));
Assert.assertEquals("hello world'", evalAttr("hello world\\'", '\''));
Assert.assertEquals("hello world\"", evalAttr("hello world\\\"", '\''));
Assert.assertEquals("hello \"world\"", evalAttr("hello \"world\"", '\''));
Assert.assertEquals("hello \"world", evalAttr("hello \"world", '\''));
Assert.assertEquals("hello world\"", evalAttr("hello world\"", '\''));
Assert.assertEquals("hello world'", evalAttr("hello world\\'", '\''));
Assert.assertEquals("hello world\"", evalAttr("hello world\\\"", '\''));
}
@Test
public void testBug45451() {
Assert.assertEquals("2", evalAttr("${1+1}", '\"'));
Assert.assertEquals("${1+1}", evalAttr("\\${1+1}", '\"'));
Assert.assertEquals("\\2", evalAttr("\\\\${1+1}", '\"'));
}
@Test
public void testBug49081() {
Assert.assertEquals("#2", evalAttr("#${1+1}", '\"'));
}
@Test
public void testLiteral() {
// Inspired by work on bug 45451, comments from kkolinko on the dev
// list and looking at the spec to find some edge cases
// '\' is only an escape character inside a StringLiteral
// Attribute escaping does not apply inside EL expressions
Assert.assertEquals("\\", evalAttr("${'\\\\'}", '\"'));
// Can use ''' inside '"' when quoting with '"' and vice versa without
// escaping
Assert.assertEquals("\\\"", evalAttr("${'\\\\\"'}", '\"'));
Assert.assertEquals("\"\\", evalAttr("${'\\\"\\\\'}", '\"'));
Assert.assertEquals("\\'", evalAttr("${'\\\\\\''}", '\"'));
Assert.assertEquals("'\\", evalAttr("${'\\'\\\\'}", '\"'));
// Quoting <% and %>
Assert.assertEquals("hello <% world", evalAttr("hello <\\% world", '\"'));
Assert.assertEquals("hello %> world", evalAttr("hello %> world", '\"'));
// Test that the end of literal in EL expression is recognized in
// parseEL(), be it quoted with single or double quotes. That is, that
// AttributeParser correctly switches between parseLiteral and parseEL
// methods.
//
// The test is based on the difference in how the '\' character is printed:
// when in parseLiteral \\${ will be printed as ${'\'}${, but if we are still
// inside of parseEL it will be printed as \${, thus preventing the EL
// expression that follows from being evaluated.
//
Assert.assertEquals("foo\\bar\\baz", evalAttr("${\'foo\'}\\\\${\'bar\'}\\\\${\'baz\'}", '\"'));
Assert.assertEquals("foo\\bar\\baz", evalAttr("${\'foo\'}\\\\${\"bar\"}\\\\${\'baz\'}", '\"'));
Assert.assertEquals("foo\\bar\\baz", evalAttr("${\"foo\"}\\\\${\'bar\'}\\\\${\"baz\"}", '\"'));
}
@Test
public void testScriptExpressionLiterals() {
Assert.assertEquals(" \"hello world\" ", parseScriptExpression(
" \"hello world\" ", (char) 0));
Assert.assertEquals(" \"hello \\\"world\" ", parseScriptExpression(
" \"hello \\\\\"world\" ", (char) 0));
}
private String evalAttr(String expression, char quote) {
ExpressionFactoryImpl exprFactory = new ExpressionFactoryImpl();
ELContextImpl ctx = new ELContextImpl(exprFactory);
ctx.setFunctionMapper(new TesterFunctions.FMapper());
ValueExpression ve = exprFactory.createValueExpression(ctx,
AttributeParser.getUnquoted(expression, quote, false, false,
false, false),
String.class);
return (String) ve.getValue(ctx);
}
private String parseScriptExpression(String expression, char quote) {
return AttributeParser.getUnquoted(expression, quote, false, false,
false, false);
}
}

View File

@@ -0,0 +1,208 @@
/*
* 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.jasper.compiler;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
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 TestCompiler extends TomcatBaseTest {
@Test
public void testBug49726a() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
Map<String,List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49726a.jsp",
res, headers);
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
// Check content type
String contentType = getSingleHeader("Content-Type", headers);
Assert.assertTrue(contentType.startsWith("text/html"));
}
@Test
public void testBug49726b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
Map<String,List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49726b.jsp",
res, headers);
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
// Check content type
String contentType = getSingleHeader("Content-Type", headers);
Assert.assertTrue(contentType.startsWith("text/plain"));
}
@Test
public void testBug53257a() throws Exception {
getTomcatInstanceTestWebapp(false, true);
// foo;bar.jsp
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug53257/foo%3bbar.jsp");
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
}
@Test
public void testBug53257b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug53257/foo&bar.jsp");
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
}
@Test
public void testBug53257c() throws Exception {
getTomcatInstanceTestWebapp(false, true);
// foo#bar.jsp
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug53257/foo%23bar.jsp");
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
}
@Test
public void testBug53257d() throws Exception {
getTomcatInstanceTestWebapp(false, true);
// foo%bar.jsp
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug53257/foo%25bar.jsp");
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
}
@Test
public void testBug53257e() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug53257/foo+bar.jsp");
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
}
@Test
public void testBug53257f() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug53257/foo%20bar.jsp");
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
}
@Test
public void testBug53257g() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug53257/foo%20bar/foobar.jsp");
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
}
@Test
public void testBug53257z() throws Exception {
getTomcatInstanceTestWebapp(false, true);
// Check that URL decoding is not done twice
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug53257/foo%2525bar.jsp", res, null);
Assert.assertEquals(404, rc);
}
@Test
public void testBug51584() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-fragments");
Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
skipTldsForResourceJars(ctx);
tomcat.start();
// No further tests required. The bug triggers an infinite loop on
// context start so the test will crash before it reaches this point if
// it fails
}
@Test
public void testBug55262() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug55262.jsp");
String result = res.toString();
Pattern prelude = Pattern.compile(
"(.*This is a prelude\\.){2}.*",
Pattern.MULTILINE | Pattern.DOTALL);
Pattern coda = Pattern.compile(
"(.*This is a coda\\.){2}.*",
Pattern.MULTILINE|Pattern.DOTALL);
Assert.assertTrue(prelude.matcher(result).matches());
Assert.assertTrue(coda.matcher(result).matches());
}
/** Assertion for text printed by tags:echo */
private static void assertEcho(String result, String expected) {
Assert.assertTrue(result, result.indexOf("<p>" + expected + "</p>") > 0);
}
}

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.jasper.compiler;
import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
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.jasper.JspCompilationContext;
import org.apache.jasper.compiler.ELInterpreterFactory.DefaultELInterpreter;
public class TestELInterpreterFactory extends TomcatBaseTest {
public static class SimpleELInterpreter implements ELInterpreter {
@Override
public String interpreterCall(JspCompilationContext context,
boolean isTagFile, String expression, Class<?> expectedType,
String fnmapvar) {
return expression;
}
}
@Test
public void testBug54239() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ServletContext context = ctx.getServletContext();
ELInterpreter interpreter =
ELInterpreterFactory.getELInterpreter(context);
Assert.assertNotNull(interpreter);
Assert.assertTrue(interpreter instanceof DefaultELInterpreter);
context.removeAttribute(ELInterpreter.class.getName());
context.setAttribute(ELInterpreter.class.getName(),
SimpleELInterpreter.class.getName());
interpreter = ELInterpreterFactory.getELInterpreter(context);
Assert.assertNotNull(interpreter);
Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
context.removeAttribute(ELInterpreter.class.getName());
SimpleELInterpreter simpleInterpreter = new SimpleELInterpreter();
context.setAttribute(ELInterpreter.class.getName(), simpleInterpreter);
interpreter = ELInterpreterFactory.getELInterpreter(context);
Assert.assertNotNull(interpreter);
Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
Assert.assertTrue(interpreter == simpleInterpreter);
context.removeAttribute(ELInterpreter.class.getName());
ctx.stop();
ctx.addApplicationListener(Bug54239Listener.class.getName());
ctx.start();
interpreter = ELInterpreterFactory.getELInterpreter(ctx.getServletContext());
Assert.assertNotNull(interpreter);
Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
}
public static class Bug54239Listener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().setInitParameter(ELInterpreter.class.getName(),
SimpleELInterpreter.class.getName());
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// NO-OP
}
}
}

View File

@@ -0,0 +1,324 @@
/*
* 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.jasper.compiler;
import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ELManager;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
import org.apache.jasper.JasperException;
import org.apache.jasper.compiler.ELNode.Nodes;
import org.apache.jasper.compiler.ELParser.TextBuilder;
/**
* You will need to keep your wits about you when working with this class. Keep
* in mind the following:
* <ul>
* <li>If in doubt, read the EL and JSP specifications. Twice.</li>
* <li>The escaping rules are complex and subtle. The explanation below (as well
* as the tests and the implementation) may have missed an edge case despite
* trying hard not to.
* <li>The strings passed to {@link #doTestParser(String,String)} are Java
* escaped in the source code and will be unescaped before being used.</li>
* <li>LiteralExpressions always occur outside of "${...}" and "#{...}". Literal
* expressions escape '$' and '#' with '\\'</li>
* <li>LiteralStrings always occur inside "${...}" or "#{...}". Literal strings
* escape '\'', '\"' and '\\' with '\\'. Escaping '\"' is optional if the
* literal string is delimited by '\''. Escaping '\'' is optional if the
* literal string is delimited by '\"'.</li>
* </ul>
*/
public class TestELParser {
@Test
public void testText() throws JasperException {
doTestParser("foo", "foo");
}
@Test
public void testLiteral() throws JasperException {
doTestParser("${'foo'}", "foo");
}
@Test
public void testVariable() throws JasperException {
doTestParser("${test}", null);
}
@Test
public void testFunction01() throws JasperException {
doTestParser("${do(x)}", null);
}
@Test
public void testFunction02() throws JasperException {
doTestParser("${do:it(x)}", null);
}
@Test
public void testFunction03() throws JasperException {
doTestParser("${do:it(x,y)}", null);
}
@Test
public void testFunction04() throws JasperException {
doTestParser("${do:it(x,y,z)}", null);
}
@Test
public void testFunction05() throws JasperException {
doTestParser("${do:it(x, '\\\\y',z)}", null);
}
@Test
public void testCompound01() throws JasperException {
doTestParser("1${'foo'}1", "1foo1");
}
@Test
public void testCompound02() throws JasperException {
doTestParser("1${test}1", null);
}
@Test
public void testCompound03() throws JasperException {
doTestParser("${foo}${bar}", null);
}
@Test
public void testTernary01() throws JasperException {
doTestParser("${true?true:false}", "true");
}
@Test
public void testTernary02() throws JasperException {
doTestParser("${a==1?true:false}", null);
}
@Test
public void testTernary03() throws JasperException {
doTestParser("${a eq1?true:false}", null);
}
@Test
public void testTernary04() throws JasperException {
doTestParser(" ${ a eq 1 ? true : false } ", null);
}
@Test
public void testTernary05() throws JasperException {
// Note this is invalid EL
doTestParser("${aeq1?true:false}", null);
}
@Test
public void testTernary06() throws JasperException {
doTestParser("${do:it(a eq1?true:false,y)}", null);
}
@Test
public void testTernary07() throws JasperException {
doTestParser(" ${ do:it( a eq 1 ? true : false, y ) } ", null);
}
@Test
public void testTernary08() throws JasperException {
doTestParser(" ${ do:it ( a eq 1 ? true : false, y ) } ", null);
}
@Test
public void testTernary09() throws JasperException {
doTestParser(" ${ do : it ( a eq 1 ? true : false, y ) } ", null);
}
@Test
public void testTernary10() throws JasperException {
doTestParser(" ${!empty my:link(foo)} ", null);
}
@Test
public void testTernary11() throws JasperException {
doTestParser("${true?'true':'false'}", "true");
}
@Test
public void testTernary12() throws JasperException {
doTestParser("${true?'tr\"ue':'false'}", "tr\"ue");
}
@Test
public void testTernary13() throws JasperException {
doTestParser("${true?'tr\\'ue':'false'}", "tr'ue");
}
@Test
public void testTernaryBug56031() throws JasperException {
doTestParser("${my:link(!empty registration ? registration : '/test/registration')}", null);
}
@Test
public void testQuotes01() throws JasperException {
doTestParser("'", "'");
}
@Test
public void testQuotes02() throws JasperException {
doTestParser("'${foo}'", null);
}
@Test
public void testQuotes03() throws JasperException {
doTestParser("'${'foo'}'", "'foo'");
}
@Test
public void testEscape01() throws JasperException {
doTestParser("${'\\\\'}", "\\");
}
@Test
public void testEscape02() throws JasperException {
doTestParser("\\\\x${'\\\\'}", "\\\\x\\");
}
@Test
public void testEscape03() throws JasperException {
doTestParser("\\\\", "\\\\");
}
@Test
public void testEscape04() throws JasperException {
// When parsed as EL in JSP the escaping of $ as \$ is optional
doTestParser("\\$", "\\$", "$");
}
@Test
public void testEscape05() throws JasperException {
// When parsed as EL in JSP the escaping of # as \# is optional
doTestParser("\\#", "\\#", "#");
}
@Test
public void testEscape07() throws JasperException {
doTestParser("${'\\\\$'}", "\\$");
}
@Test
public void testEscape08() throws JasperException {
doTestParser("${'\\\\#'}", "\\#");
}
@Test
public void testEscape09() throws JasperException {
doTestParser("\\${", "${");
}
@Test
public void testEscape10() throws JasperException {
doTestParser("\\#{", "#{");
}
@Test
public void testEscape11() throws JasperException {
// Bug 56612
doTestParser("${'\\'\\''}", "''");
}
private void doTestParser(String input, String expected) throws JasperException {
doTestParser(input, expected, input);
}
private void doTestParser(String input, String expectedResult, String expectedBuilderOutput) throws JasperException {
ELException elException = null;
String elResult = null;
// Don't try and evaluate expressions that depend on variables or functions
if (expectedResult != null) {
try {
ELManager manager = new ELManager();
ELContext context = manager.getELContext();
ExpressionFactory factory = ELManager.getExpressionFactory();
ValueExpression ve = factory.createValueExpression(context, input, String.class);
elResult = ve.getValue(context).toString();
Assert.assertEquals(expectedResult, elResult);
} catch (ELException ele) {
elException = ele;
}
}
Nodes nodes = null;
try {
nodes = ELParser.parse(input, false);
Assert.assertNull(elException);
} catch (IllegalArgumentException iae) {
Assert.assertNotNull(elResult, elException);
// Not strictly true but enables us to report both
iae.initCause(elException);
throw iae;
}
TextBuilder textBuilder = new TextBuilder(false);
nodes.visit(textBuilder);
Assert.assertEquals(expectedBuilderOutput, textBuilder.getText());
}
}

View File

@@ -0,0 +1,95 @@
/*
* 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.jasper.compiler;
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;
import org.junit.runners.Parameterized.Parameters;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
@RunWith(Parameterized.class)
public class TestEncodingDetector extends TomcatBaseTest {
@Parameters
public static Collection<Object[]> inputs() {
/// Note: These files are saved using the encoding indicated by the BOM
List<Object[]> result = new ArrayList<>();
result.add(new Object[] { "bom-none-prolog-none.jsp", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-none-prolog-none.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-none-prolog-utf16be.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-none-prolog-utf16le.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-none-prolog-utf8.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf8-prolog-none.jsp", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf8-prolog-none.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf8-prolog-utf16be.jspx", Integer.valueOf(500), null });
result.add(new Object[] { "bom-utf8-prolog-utf16le.jspx", Integer.valueOf(500), null });
result.add(new Object[] { "bom-utf8-prolog-utf8.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf16be-prolog-none.jsp", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf16be-prolog-none.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf16be-prolog-utf16be.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf16be-prolog-utf16le.jspx", Integer.valueOf(500), null });
result.add(new Object[] { "bom-utf16be-prolog-utf8.jspx", Integer.valueOf(500), null });
result.add(new Object[] { "bom-utf16le-prolog-none.jsp", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf16le-prolog-none.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf16le-prolog-utf16be.jspx", Integer.valueOf(500), null });
result.add(new Object[] { "bom-utf16le-prolog-utf16le.jspx", Integer.valueOf(200), Boolean.TRUE });
result.add(new Object[] { "bom-utf16le-prolog-utf8.jspx", Integer.valueOf(500), null });
result.add(new Object[] { "bug60769a.jspx", Integer.valueOf(500), null });
result.add(new Object[] { "bug60769b.jspx", Integer.valueOf(200), Boolean.TRUE });
return result;
}
@Parameter(0)
public String jspName;
@Parameter(1)
public int expectedResponseCode;
@Parameter(2)
public Boolean responseBodyOK;
@Test
public void testEncodedJsp() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk responseBody = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/jsp/encoding/" + jspName,
responseBody, null);
Assert.assertEquals(expectedResponseCode, rc);
if (expectedResponseCode == 200) {
// trim() to remove whitespace like new lines
String bodyText = responseBody.toString().trim();
if (responseBodyOK.booleanValue()) {
Assert.assertEquals("OK", bodyText);
} else {
Assert.assertNotEquals("OK", bodyText);
}
}
}
}

View File

@@ -0,0 +1,275 @@
/*
* 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.jasper.compiler;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.VariableInfo;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestGenerator extends TomcatBaseTest {
@Test
public void testBug45015a() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug45nnn/bug45015a.jsp");
String result = res.toString();
// Beware of the differences between escaping in JSP attributes and
// in Java Strings
assertEcho(result, "00-hello 'world'");
assertEcho(result, "01-hello 'world");
assertEcho(result, "02-hello world'");
assertEcho(result, "03-hello world'");
assertEcho(result, "04-hello world\"");
assertEcho(result, "05-hello \"world\"");
assertEcho(result, "06-hello \"world");
assertEcho(result, "07-hello world\"");
assertEcho(result, "08-hello world'");
assertEcho(result, "09-hello world\"");
}
@Test
public void testBug45015b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug45nnn/bug45015b.jsp", new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBug45015c() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug45nnn/bug45015c.jsp", new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBug48701Fail() throws Exception {
getTomcatInstanceTestWebapp(true, true);
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug48nnn/bug48701-fail.jsp", new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBug48701UseBean() throws Exception {
testBug48701("bug48nnn/bug48701-UseBean.jsp");
}
@Test
public void testBug48701VariableInfo() throws Exception {
testBug48701("bug48nnn/bug48701-VI.jsp");
}
@Test
public void testBug48701TagVariableInfoNameGiven() throws Exception {
testBug48701("bug48nnn/bug48701-TVI-NG.jsp");
}
@Test
public void testBug48701TagVariableInfoNameFromAttribute() throws Exception {
testBug48701("bug48nnn/bug48701-TVI-NFA.jsp");
}
private void testBug48701(String jsp) throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/" + jsp);
String result = res.toString();
assertEcho(result, "00-PASS");
}
public static class Bug48701 extends TagSupport {
private static final long serialVersionUID = 1L;
private String beanName = null;
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
@Override
public int doStartTag() throws JspException {
Bean bean = new Bean();
bean.setTime((new Date()).toString());
pageContext.setAttribute("now", bean);
return super.doStartTag();
}
}
public static class Bug48701TEI extends TagExtraInfo {
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("now", Bean.class.getCanonicalName(),
true, VariableInfo.AT_END)
};
}
}
public static class Bean {
private String time;
public void setTime(String time) {
this.time = time;
}
public String getTime() {
return time;
}
}
@Test
public void testBug49799() throws Exception {
String[] expected = { "<p style=\"color:red\">00-Red</p>",
"<p>01-Not Red</p>",
"<p style=\"color:red\">02-Red</p>",
"<p>03-Not Red</p>",
"<p style=\"color:red\">04-Red</p>",
"<p>05-Not Red</p>"};
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49799.jsp", res, null);
// Check request completed
String result = res.toString();
String[] lines = result.split("\n|\r|\r\n");
int i = 0;
for (String line : lines) {
if (line.length() > 0) {
Assert.assertEquals(expected[i], line);
i++;
}
}
}
/** Assertion for text printed by tags:echo */
private static void assertEcho(String result, String expected) {
Assert.assertTrue(result.indexOf("<p>" + expected + "</p>") > 0);
}
@Test
public void testBug56529() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug56529.jsp", bc, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String response = bc.toStringInternal();
Assert.assertTrue(response,
response.contains("[1:attribute1: '', attribute2: '']"));
Assert.assertTrue(response,
response.contains("[2:attribute1: '', attribute2: '']"));
}
public static class Bug56529 extends TagSupport {
private static final long serialVersionUID = 1L;
private String attribute1 = null;
private String attribute2 = null;
public void setAttribute1(String attribute1) {
this.attribute1 = attribute1;
}
public String getAttribute1() {
return attribute1;
}
public void setAttribute2(String attribute2) {
this.attribute2 = attribute2;
}
public String getAttribute2() {
return attribute2;
}
@Override
public int doEndTag() throws JspException {
try {
pageContext.getOut().print(
"attribute1: '" + attribute1 + "', " + "attribute2: '"
+ attribute2 + "'");
} catch (IOException e) {
throw new JspException(e);
}
return EVAL_PAGE;
}
}
@Test
public void testBug56581() throws LifecycleException {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
try {
getUrl("http://localhost:" + getPort()
+ "/test/bug5nnnn/bug56581.jsp", res, null);
Assert.fail("An IOException was expected.");
} catch (IOException expected) {
// ErrorReportValve in Tomcat 8.0.9+ flushes and aborts the
// connection when an unexpected error is encountered and response
// has already been committed. It results in an exception here:
// java.io.IOException: Premature EOF
}
String result = res.toString();
Assert.assertTrue(result.startsWith("0 Hello world!\n"));
Assert.assertTrue(result.endsWith("999 Hello world!\n"));
}
}

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.jasper.compiler;
import java.io.File;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestJspConfig extends TomcatBaseTest {
@Test
public void testServlet22NoEL() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-2.2");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/el-as-literal.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-${'hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>01-#{'hello world'}</p>") > 0);
}
@Test
public void testServlet23NoEL() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir =
new File("test/webapp-2.3");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/el-as-literal.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-${'hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>01-#{'hello world'}</p>") > 0);
}
@Test
public void testServlet24NoEL() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-2.4");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/el-as-literal.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>01-#{'hello world'}</p>") > 0);
}
@Test
public void testServlet25NoEL() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-2.5");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/el-as-literal.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
}
@Test
public void testServlet30NoEL() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-3.0");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/el-as-literal.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
}
@Test
public void testServlet31NoEL() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-3.1");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/el-as-literal.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
}
}

View File

@@ -0,0 +1,123 @@
/*
* 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.jasper.compiler;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class TestJspDocumentParser extends TomcatBaseTest {
@Test
public void testBug47977() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug47977.jspx", new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBug48827() throws Exception {
getTomcatInstanceTestWebapp(false, true);
Exception e = null;
try {
getUrl("http://localhost:" + getPort() + "/test/bug48nnn/bug48827.jspx");
} catch (IOException ioe) {
e = ioe;
}
// Should not fail
Assert.assertNull(e);
}
@Test
public void testBug54801() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54801a.jspx", bc, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
bc.recycle();
rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54801b.jspx", bc, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}
@Test
public void testBug54821() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54821a.jspx", bc, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
bc.recycle();
rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54821b.jspx", bc, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}
@Test
public void testSchemaValidation() throws Exception {
getTomcatInstanceTestWebapp(false, true);
String path = "http://localhost:" + getPort() + "/test/valid.jspx";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setFeature("http://apache.org/xml/features/validation/schema", true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void error(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
});
Document document = db.parse(path);
Assert.assertEquals("urn:valid", document.getDocumentElement().getNamespaceURI());
Assert.assertEquals("root", document.getDocumentElement().getLocalName());
}
}

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.jasper.compiler;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestJspReader extends TomcatBaseTest {
@Test
public void testBug53986() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug53986.jsp");
Assert.assertTrue(res.toString().contains("OK"));
}
}

View File

@@ -0,0 +1,73 @@
/*
* 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.jasper.compiler;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.apache.jasper.compiler.Node.PageDirective;
public class TestNode {
/*
* https://bz.apache.org/bugzilla/show_bug.cgi?id=57099
*/
@Test(expected=IllegalArgumentException.class)
public void testPageDirectiveImport01() {
doTestPageDirectiveImport("java.io.*;\r\n\timport java.net.*");
}
@Test
public void testPageDirectiveImport02() {
doTestPageDirectiveImport("a,b,c");
}
@Test
public void testPageDirectiveImport03() {
doTestPageDirectiveImport(" a , b , c ");
}
@Test
public void testPageDirectiveImport04() {
doTestPageDirectiveImport(" a\n , \r\nb , \nc\r ");
}
@Test
public void testPageDirectiveImport05() {
doTestPageDirectiveImport("java.util.List,java.util.ArrayList,java.util.Set");
}
@Test(expected=IllegalArgumentException.class)
public void testPageDirectiveImport06() {
doTestPageDirectiveImport("java.util.List;import java.util.ArrayList; import java.util.Set");
}
@Test
public void testPageDirectiveImport07() {
doTestPageDirectiveImport("java .\nutil.List,java.util.ArrayList,java.util.Set");
}
private void doTestPageDirectiveImport(String importDirective) {
PageDirective pd = new PageDirective(null, null, null);
pd.addImport(importDirective);
List<String> imports = pd.getImports();
Assert.assertEquals(3, imports.size());
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.jasper.compiler;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestNodeIntegration extends TomcatBaseTest {
@Test
public void testJspAttributeIsLiteral() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug55642a.jsp");
String result = res.toString();
Assert.assertTrue(
result.indexOf("/test/bug5nnnn/bug55642b.jsp?foo=bar&a=1&b=2") > 0);
}
}

View File

@@ -0,0 +1,291 @@
/*
* 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.jasper.compiler;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Tests are duplicated in {@link TestParserNoStrictWhitespace} with the strict
* whitespace parsing disabled.
*/
public class TestParser extends TomcatBaseTest {
@Test
public void testBug48627() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug48nnn/bug48627.jsp");
String result = res.toString();
// Beware of the differences between escaping in JSP attributes and
// in Java Strings
assertEcho(result, "00-\\");
assertEcho(result, "01-\\");
}
@Test
public void testBug48668a() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug48nnn/bug48668a.jsp");
String result = res.toString();
assertEcho(result, "00-Hello world</p>#{foo.bar}");
assertEcho(result, "01-Hello world</p>${foo.bar}");
assertEcho(result, "10-Hello ${'foo.bar}");
assertEcho(result, "11-Hello ${'foo.bar}");
assertEcho(result, "12-Hello #{'foo.bar}");
assertEcho(result, "13-Hello #{'foo.bar}");
assertEcho(result, "14-Hello ${'foo}");
assertEcho(result, "15-Hello ${'foo}");
assertEcho(result, "16-Hello #{'foo}");
assertEcho(result, "17-Hello #{'foo}");
assertEcho(result, "18-Hello ${'foo.bar}");
assertEcho(result, "19-Hello ${'foo.bar}");
assertEcho(result, "20-Hello #{'foo.bar}");
assertEcho(result, "21-Hello #{'foo.bar}");
assertEcho(result, "30-Hello ${'foo}");
assertEcho(result, "31-Hello ${'foo}");
assertEcho(result, "32-Hello #{'foo}");
assertEcho(result, "33-Hello #{'foo}");
assertEcho(result, "34-Hello ${'foo}");
assertEcho(result, "35-Hello ${'foo}");
assertEcho(result, "36-Hello #{'foo}");
assertEcho(result, "37-Hello #{'foo}");
assertEcho(result, "40-Hello ${'foo}");
assertEcho(result, "41-Hello ${'foo}");
assertEcho(result, "42-Hello #{'foo}");
assertEcho(result, "43-Hello #{'foo}");
assertEcho(result, "50-Hello ${'foo}");
assertEcho(result, "51-Hello ${'foo}");
assertEcho(result, "52-Hello #{'foo}");
assertEcho(result, "53-Hello #{'foo}");
}
@Test
public void testBug48668b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug48nnn/bug48668b.jsp");
String result = res.toString();
assertEcho(result, "00-Hello world</p>#{foo.bar}");
assertEcho(result, "01-Hello world</p>#{foo2");
}
@Test
public void testBug49297NoSpaceStrict() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297NoSpace.jsp", new ByteChunk(), null);
Assert.assertEquals(500, sc);
}
@Test
public void testBug49297DuplicateAttr() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297DuplicateAttr.jsp", new ByteChunk(), null);
Assert.assertEquals(500, sc);
}
@Test
public void testBug49297MultipleImport1() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297MultipleImport1.jsp", res, null);
Assert.assertEquals(200, sc);
assertEcho(res.toString(), "OK");
}
@Test
public void testBug49297MultipleImport2() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297MultipleImport2.jsp", res, null);
Assert.assertEquals(200, sc);
assertEcho(res.toString(), "OK");
}
@Test
public void testBug49297MultiplePageEncoding1() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297MultiplePageEncoding1.jsp", res, null);
Assert.assertEquals(500, sc);
}
@Test
public void testBug49297MultiplePageEncoding2() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297MultiplePageEncoding2.jsp", res, null);
Assert.assertEquals(500, sc);
}
@Test
public void testBug49297MultiplePageEncoding3() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297MultiplePageEncoding3.jsp", res, null);
Assert.assertEquals(500, sc);
}
@Test
public void testBug49297MultiplePageEncoding4() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297MultiplePageEncoding4.jsp", res, null);
Assert.assertEquals(500, sc);
}
@Test
public void testBug49297Tag() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297Tag.jsp", res, null);
Assert.assertEquals(200, sc);
assertEcho(res.toString(), "OK");
}
@Test
public void testBug52335() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug52335.jsp");
String result = res.toString();
// Beware of the differences between escaping in JSP attributes and
// in Java Strings
assertEcho(result, "00 - \\% \\\\% <%");
assertEcho(result, "01 - <b><%</b>");
assertEcho(result, "02 - <p>Foo</p><%");
}
@Test
public void testBug55198() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug55198.jsp");
String result = res.toString();
Assert.assertTrue(result,
result.contains("&quot;1foo1&lt;&amp;&gt;&quot;")
|| result.contains("&#034;1foo1&lt;&amp;&gt;&#034;"));
Assert.assertTrue(result,
result.contains("&quot;2bar2&lt;&amp;&gt;&quot;")
|| result.contains("&#034;2bar2&lt;&amp;&gt;&#034;"));
Assert.assertTrue(result,
result.contains("&quot;3a&amp;b3&quot;")
|| result.contains("&#034;3a&amp;b3&#034;"));
Assert.assertTrue(result,
result.contains("&quot;4&4&quot;")
|| result.contains("&#034;4&4&#034;"));
Assert.assertTrue(result,
result.contains("&quot;5&apos;5&quot;")
|| result.contains("&#034;5&apos;5&#034;"));
}
@Test
public void testBug56265() throws Exception {
getTomcatInstanceTestWebapp(true, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug56265.jsp");
String result = res.toString();
Assert.assertTrue(result,
result.contains("[1: [data-test]: [window.alert('Hello World <&>!')]]"));
Assert.assertTrue(result,
result.contains("[2: [data-test]: [window.alert('Hello World <&>!')]]"));
Assert.assertTrue(result,
result.contains("[3: [data-test]: [window.alert('Hello 'World <&>'!')]]"));
Assert.assertTrue(result,
result.contains("[4: [data-test]: [window.alert('Hello 'World <&>'!')]]"));
}
@Test
public void testBug56334And56561() throws Exception {
getTomcatInstanceTestWebapp(true, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug56334and56561.jspx");
String result = res.toString();
// NOTE: The expected values must themselves be \ escaped below
Assert.assertTrue(result, result.contains("01a\\?resize01a"));
Assert.assertTrue(result, result.contains("01b\\\\x\\?resize01b"));
Assert.assertTrue(result, result.contains("<set data-value=\"02a\\\\?resize02a\"/>"));
Assert.assertTrue(result, result.contains("<set data-value=\"02b\\\\\\\\x\\\\?resize02b\"/>"));
Assert.assertTrue(result, result.contains("<set data-value=\"03a\\?resize03a\"/>"));
Assert.assertTrue(result, result.contains("<set data-value=\"03b\\\\x\\?resize03b\"/>"));
Assert.assertTrue(result, result.contains("<04a\\?resize04a/>"));
Assert.assertTrue(result, result.contains("<04b\\\\x\\?resize04b/>"));
Assert.assertTrue(result, result.contains("<set data-value=\"05a$${&amp;\"/>"));
Assert.assertTrue(result, result.contains("<set data-value=\"05b$${&amp;2\"/>"));
Assert.assertTrue(result, result.contains("<set data-value=\"05c##{&gt;hello&lt;\"/>"));
Assert.assertTrue(result, result.contains("05x:<set data-value=\"\"/>"));
Assert.assertTrue(result, result.contains("<set xmlns:foo=\"urn:06a\\bar\\baz\"/>"));
Assert.assertTrue(result, result.contains("07a:<set data-value=\"\\?resize\"/>"));
Assert.assertTrue(result, result.contains("07b:<set data-content=\"\\?resize=.+\"/>"));
Assert.assertTrue(result, result.contains("07c:<set data-content=\"\\?resize=.+\"/>"));
Assert.assertTrue(result, result.contains("07d:<set data-content=\"false\"/>"));
Assert.assertTrue(result, result.contains("07e:<set data-content=\"false\"/>"));
Assert.assertTrue(result, result.contains("07f:<set data-content=\"\\\'something\'\"/>"));
Assert.assertTrue(result, result.contains("07g:<set data-content=\"\\\'something\'\"/>"));
}
/** Assertion for text printed by tags:echo */
private static void assertEcho(String result, String expected) {
Assert.assertTrue(result.indexOf("<p>" + expected + "</p>") > 0);
}
}

View File

@@ -0,0 +1,131 @@
/*
* 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.jasper.compiler;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Tests are duplicated in {@link TestParser} with the strict whitespace parsing
* enabled by default.
*/
public class TestParserNoStrictWhitespace extends TomcatBaseTest {
@Override
public void setUp() throws Exception {
System.setProperty(
"org.apache.jasper.compiler.Parser.STRICT_WHITESPACE",
"false");
super.setUp();
}
@Test
public void testBug48627() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug48nnn/bug48627.jsp");
String result = res.toString();
// Beware of the differences between escaping in JSP attributes and
// in Java Strings
assertEcho(result, "00-\\");
assertEcho(result, "01-\\");
}
@Test
public void testBug48668a() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug48nnn/bug48668a.jsp");
String result = res.toString();
assertEcho(result, "00-Hello world</p>#{foo.bar}");
assertEcho(result, "01-Hello world</p>${foo.bar}");
assertEcho(result, "10-Hello ${'foo.bar}");
assertEcho(result, "11-Hello ${'foo.bar}");
assertEcho(result, "12-Hello #{'foo.bar}");
assertEcho(result, "13-Hello #{'foo.bar}");
assertEcho(result, "14-Hello ${'foo}");
assertEcho(result, "15-Hello ${'foo}");
assertEcho(result, "16-Hello #{'foo}");
assertEcho(result, "17-Hello #{'foo}");
assertEcho(result, "18-Hello ${'foo.bar}");
assertEcho(result, "19-Hello ${'foo.bar}");
assertEcho(result, "20-Hello #{'foo.bar}");
assertEcho(result, "21-Hello #{'foo.bar}");
assertEcho(result, "30-Hello ${'foo}");
assertEcho(result, "31-Hello ${'foo}");
assertEcho(result, "32-Hello #{'foo}");
assertEcho(result, "33-Hello #{'foo}");
assertEcho(result, "34-Hello ${'foo}");
assertEcho(result, "35-Hello ${'foo}");
assertEcho(result, "36-Hello #{'foo}");
assertEcho(result, "37-Hello #{'foo}");
assertEcho(result, "40-Hello ${'foo}");
assertEcho(result, "41-Hello ${'foo}");
assertEcho(result, "42-Hello #{'foo}");
assertEcho(result, "43-Hello #{'foo}");
assertEcho(result, "50-Hello ${'foo}");
assertEcho(result, "51-Hello ${'foo}");
assertEcho(result, "52-Hello #{'foo}");
assertEcho(result, "53-Hello #{'foo}");
}
@Test
public void testBug48668b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug48nnn/bug48668b.jsp");
String result = res.toString();
assertEcho(result, "00-Hello world</p>#{foo.bar}");
assertEcho(result, "01-Hello world</p>#{foo2");
}
@Test
public void testBug49297NoSpaceNotStrict() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297NoSpace.jsp", res, null);
Assert.assertEquals(200, sc);
assertEcho(res.toString(), "Hello World");
}
@Test
public void testBug49297DuplicateAttr() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int sc = getUrl("http://localhost:" + getPort() +
"/test/bug49nnn/bug49297DuplicateAttr.jsp", new ByteChunk(), null);
Assert.assertEquals(500, sc);
}
/** Assertion for text printed by tags:echo */
private static void assertEcho(String result, String expected) {
Assert.assertTrue(result.indexOf("<p>" + expected + "</p>") > 0);
}
}

View File

@@ -0,0 +1,99 @@
/*
* 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.jasper.compiler;
import java.io.IOException;
import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.VariableInfo;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestScriptingVariabler extends TomcatBaseTest {
@Test
public void testBug42390() throws Exception {
getTomcatInstanceTestWebapp(false, true);
Exception e = null;
try {
getUrl("http://localhost:" + getPort() + "/test/bug42390.jsp");
} catch (IOException ioe) {
e = ioe;
}
// Should not fail
Assert.assertNull(e);
}
public static class Bug48616aTag extends TagSupport {
private static final long serialVersionUID = 1L;
}
public static class Bug48616bTag extends TagSupport {
private static final long serialVersionUID = 1L;
}
public static class Bug48616bTei extends TagExtraInfo {
/**
* Return information about the scripting variables to be created.
*/
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("Test", "java.lang.String", true,
VariableInfo.AT_END)
};
}
}
@Test
public void testBug48616() throws Exception {
getTomcatInstanceTestWebapp(false, true);
Exception e = null;
try {
getUrl("http://localhost:" + getPort() + "/test/bug48nnn/bug48616.jsp");
} catch (IOException ioe) {
e = ioe;
}
// Should not fail
Assert.assertNull(e);
}
@Test
public void testBug48616b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
Exception e = null;
try {
getUrl("http://localhost:" + getPort() + "/test/bug48nnn/bug48616b.jsp");
} catch (IOException ioe) {
e = ioe;
}
// Should not fail
Assert.assertNull(e);
}
}

View File

@@ -0,0 +1,43 @@
/*
* 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.jasper.compiler;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Test case for {@link TagLibraryInfoImpl}.
*/
public class TestTagLibraryInfoImpl extends TomcatBaseTest {
@Test
public void testRelativeTldLocation() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/jsp/test.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}
}

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.jasper.compiler;
import javax.servlet.ServletContext;
import javax.servlet.jsp.tagext.TagFileInfo;
import javax.servlet.jsp.tagext.TagInfo;
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;
/**
* Test case for {@link TagPluginManager}.
*/
public class TestTagPluginManager extends TomcatBaseTest {
private static TagInfo tagInfo = new TagInfo("ATag",
"org.apache.jasper.compiler.ATagSupport", "", "", null, null, null);
@Test
public void testBug54240() throws Exception {
Tomcat tomcat = getTomcatInstanceTestWebapp(false, true);
ServletContext context = ((Context) tomcat.getHost().findChildren()[0]).getServletContext();
TagPluginManager manager = new TagPluginManager(context);
Node.Nodes nodes = new Node.Nodes();
Node.CustomTag c = new Node.CustomTag("test:ATag", "test", "ATag",
"http://tomcat.apache.org/jasper", null, null, null, null, null,
new TagFileInfo("ATag", "http://tomcat.apache.org/jasper",
tagInfo));
c.setTagHandlerClass(TesterTag.class);
nodes.add(c);
manager.apply(nodes, null, null);
Node n = nodes.getNode(0);
Assert.assertNotNull(n);
Assert.assertTrue(n instanceof Node.CustomTag);
Node.CustomTag t = (Node.CustomTag)n;
Assert.assertNotNull(t.getAtSTag());
Node.Nodes sTag = c.getAtSTag();
Node scriptlet = sTag.getNode(0);
Assert.assertNotNull(scriptlet);
Assert.assertTrue(scriptlet instanceof Node.Scriptlet);
Node.Scriptlet s = (Node.Scriptlet)scriptlet;
Assert.assertEquals("//Just a comment", s.getText());
}
}

View File

@@ -0,0 +1,220 @@
/*
* 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.jasper.compiler;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestValidator extends TomcatBaseTest {
@Test
public void testBug47331() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug47331.jsp", new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testTldVersions22() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir =
new File("test/webapp-2.2");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/tld-versions.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>${'00-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>${'02-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>${'04-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>${'06-hello world'}</p>") > 0);
}
@Test
public void testTldVersions23() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir =
new File("test/webapp-2.3");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/tld-versions.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>${'00-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>${'02-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>${'04-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>${'06-hello world'}</p>") > 0);
}
@Test
public void testTldVersions24() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir =
new File("test/webapp-2.4");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/tld-versions.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>02-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>04-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>06-hello world</p>") > 0);
}
@Test
public void testTldVersions25() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir =
new File("test/webapp-2.5");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/tld-versions.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>02-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>04-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>06-hello world</p>") > 0);
}
@Test
public void testTldVersions30() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir =
new File("test/webapp-3.0");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/tld-versions.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>02-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>04-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>06-hello world</p>") > 0);
}
@Test
public void testTldVersions31() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir =
new File("test/webapp-3.1");
// app dir is relative to server home
tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/tld-versions.jsp");
String result = res.toString();
Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>02-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>04-hello world</p>") > 0);
Assert.assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
Assert.assertTrue(result.indexOf("<p>06-hello world</p>") > 0);
}
public static class Echo extends TagSupport {
private static final long serialVersionUID = 1L;
private String echo = null;
public void setEcho(String echo) {
this.echo = echo;
}
public String getEcho() {
return echo;
}
@Override
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("<p>" + echo + "</p>");
} catch (IOException e) {
pageContext.getServletContext().log("Tag (Echo21) failure", e);
}
return super.doStartTag();
}
}
}

View File

@@ -0,0 +1,28 @@
/*
* 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.jasper.compiler;
import javax.servlet.jsp.tagext.TagSupport;
/**
* A tag for test purpose
*/
public class TesterTag extends TagSupport {
private static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,31 @@
/*
* 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.jasper.compiler;
import org.apache.jasper.compiler.tagplugin.TagPlugin;
import org.apache.jasper.compiler.tagplugin.TagPluginContext;
/**
* Plug-in for {@link TesterTag}.
*/
public class TesterTagPlugin implements TagPlugin {
@Override
public void doTag(TagPluginContext ctxt) {
ctxt.generateJavaSource("//Just a comment");
}
}

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.jasper.compiler;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.security.Escape;
/**
* Performance tests for {@link Validator}.
*/
public class TesterValidator {
private static String[] bug53867TestData = new String[] {
"Hello World!",
"<meta http-equiv=\"Content-Language\">",
"This connection has limited network connectivity.",
"Please use this web page & to access file server resources." };
@Test
public void testBug53867() {
for (int i = 0; i < 10; i++) {
doTestBug53867();
}
}
private static void doTestBug53867() {
int count = 100000;
for (int j = 0; j < bug53867TestData.length; j++) {
Assert.assertEquals(doTestBug53867OldVersion(bug53867TestData[j]),
Escape.xml(bug53867TestData[j]));
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j < bug53867TestData.length; j++) {
doTestBug53867OldVersion(bug53867TestData[j]);
}
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j < bug53867TestData.length; j++) {
Escape.xml(bug53867TestData[j]);
}
}
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
for (int j = 0; j < bug53867TestData.length; j++) {
doTestBug53867OldVersion(bug53867TestData[j]);
}
}
System.out.println(
"Old escape:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
for (int j = 0; j < bug53867TestData.length; j++) {
Escape.xml(bug53867TestData[j]);
}
}
System.out.println(
"New escape:" + (System.currentTimeMillis() - start));
}
private static String doTestBug53867OldVersion(String s) {
if (s == null)
return null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '<') {
sb.append("&lt;");
} else if (c == '>') {
sb.append("&gt;");
} else if (c == '\'') {
sb.append("&#039;"); // &apos;
} else if (c == '&') {
sb.append("&amp;");
} else if (c == '"') {
sb.append("&#034;"); // &quot;
} else {
sb.append(c);
}
}
return sb.toString();
}
}

View File

@@ -0,0 +1,75 @@
/*
* 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.jasper.el;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.el.ELResolver;
import javax.servlet.jsp.el.ImplicitObjectELResolver;
import org.junit.Assert;
import org.junit.Test;
import org.apache.el.stream.StreamELResolverImpl;
public class TestJasperELResolver {
@Test
public void testConstructorNone() throws Exception {
doTestConstructor(0);
}
@Test
public void testConstructorOne() throws Exception {
doTestConstructor(1);
}
@Test
public void testConstructorFive() throws Exception {
doTestConstructor(5);
}
private void doTestConstructor(int count) throws Exception {
List<ELResolver> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
list.add(new ImplicitObjectELResolver());
}
JasperELResolver resolver =
new JasperELResolver(list, new StreamELResolverImpl());
Assert.assertEquals(Integer.valueOf(count),
getField("appResolversSize", resolver));
Assert.assertEquals(9 + count,
((ELResolver[])getField("resolvers", resolver)).length);
Assert.assertEquals(Integer.valueOf(9 + count),
Integer.valueOf(((AtomicInteger) getField("resolversSize", resolver)).get()));
}
private static final Object getField(String name, Object target)
throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
Field field = target.getClass().getDeclaredField(name);
field.setAccessible(true);
return field.get(target);
}
}

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.jasper.runtime;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestCustomHttpJspPage extends TomcatBaseTest {
/*
* Bug 58444
*/
@Test
public void testCustomBasePageWhenUsingTagFiles() throws Exception {
getTomcatInstanceTestWebapp(true, true);
ByteChunk out = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug58444.jsp", out, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String result = out.toString();
Assert.assertTrue(result, result.contains("00-PASS"));
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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.jasper.runtime;
import java.math.RoundingMode;
import java.util.Collections;
import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestJspContextWrapper extends TomcatBaseTest {
@Test
public void testELTagFilePageContext() throws Exception {
getTomcatInstanceTestWebapp(true, true);
ByteChunk out = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug58178.jsp", out, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String result = out.toString();
Assert.assertTrue(result, result.contains("PASS"));
}
@Test
public void testELTagFileImports() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk out = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug58178b.jsp", out, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String result = out.toString();
Assert.assertTrue(result, result.contains("00-" + DispatcherType.ASYNC));
// No obvious status fields for javax.servlet.http
// Could hack something with HttpUtils...
// No obvious status fields for javax.servlet.jsp
// Wild card (package) import
Assert.assertTrue(result, result.contains("01-" + RoundingMode.HALF_UP));
// Class import
Assert.assertTrue(result, result.contains("02-" + Collections.EMPTY_LIST.size()));
}
@Test
public void testELTagFileELContextListener() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk out = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug58178c.jsp", out, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String result = out.toString();
Assert.assertTrue(result, result.contains("JSP count: 1"));
Assert.assertTrue(result, result.contains("Tag count: 1"));
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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.jasper.runtime;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestJspWriterImpl extends TomcatBaseTest {
@Test
public void bug54241a() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54241a.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("01: null"));
Assert.assertTrue(body.contains("02: null"));
}
@Test
public void bug54241b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54241b.jsp", res, null);
Assert.assertEquals(res.toString(),
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
}

View File

@@ -0,0 +1,123 @@
/*
* 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.jasper.runtime;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
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.jasper.Constants;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestPageContextImpl extends TomcatBaseTest {
@Test
public void testDoForward() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug53545.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("OK"));
Assert.assertFalse(body.contains("FAIL"));
}
@Test
public void testDefaultBufferSize() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
// app dir is relative to server home
Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
// Add the Servlet
Tomcat.addServlet(ctx, "bug56010", new Bug56010());
ctx.addServletMappingDecoded("/bug56010", "bug56010");
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug56010");
String result = res.toString();
Assert.assertTrue(result.contains("OK"));
}
@Test
public void testIncludeThrowsIOException() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/jsp/pageContext1.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("OK"));
Assert.assertFalse(body.contains("FAILED"));
res = new ByteChunk();
rc = getUrl("http://localhost:" + getPort() + "/test/jsp/pageContext1.jsp?flush=true", res,
null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
body = res.toString();
Assert.assertTrue(body.contains("Flush"));
Assert.assertTrue(body.contains("OK"));
Assert.assertFalse(body.contains("FAILED"));
}
public static class Bug56010 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(
this, req, resp, null, false, JspWriter.DEFAULT_BUFFER, true);
JspWriter out = pageContext.getOut();
if (Constants.DEFAULT_BUFFER_SIZE == out.getBufferSize()) {
resp.getWriter().println("OK");
} else {
resp.getWriter().println("FAIL");
}
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* 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.jasper.runtime;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;
public abstract class TesterHttpJspBase extends GenericServlet implements HttpJspPage {
private static final long serialVersionUID = 1L;
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
_jspService((HttpServletRequest) req, (HttpServletResponse) res);
}
@Override
public abstract void _jspService(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
jspInit();
}
@Override
public void jspInit() {
// NO-OP by default
}
@Override
public void destroy() {
super.destroy();
jspDestroy();
}
@Override
public void jspDestroy() {
// NO-OP by default
}
}

View File

@@ -0,0 +1,153 @@
/*
* 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.jasper.servlet;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.descriptor.JspConfigDescriptor;
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
import org.junit.Assert;
import org.junit.Test;
public class TestJspCServletContext {
@Test
public void testWebapp() throws Exception {
File appDir = new File("test/webapp");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Assert.assertEquals(3, context.getEffectiveMajorVersion());
Assert.assertEquals(1, context.getEffectiveMinorVersion());
JspConfigDescriptor jspConfigDescriptor =
context.getJspConfigDescriptor();
Assert.assertTrue(jspConfigDescriptor.getTaglibs().isEmpty());
Collection<JspPropertyGroupDescriptor> propertyGroups =
jspConfigDescriptor.getJspPropertyGroups();
Assert.assertEquals(4, propertyGroups.size());
Iterator<JspPropertyGroupDescriptor> groupIterator =
propertyGroups.iterator();
JspPropertyGroupDescriptor groupDescriptor;
groupDescriptor = groupIterator.next();
Assert.assertEquals("text/plain",
groupDescriptor.getDefaultContentType());
Collection<String> urlPatterns =groupDescriptor.getUrlPatterns();
Assert.assertEquals(2, urlPatterns.size());
Iterator<String> iterator = urlPatterns.iterator();
Assert.assertEquals("/bug49nnn/bug49726a.jsp", iterator.next());
Assert.assertEquals("/bug49nnn/bug49726b.jsp", iterator.next());
groupDescriptor = groupIterator.next();
Assert.assertEquals(2, groupDescriptor.getIncludePreludes().size());
Assert.assertEquals(2, groupDescriptor.getIncludeCodas().size());
}
@Test
public void testWebapp_2_2() throws Exception {
File appDir = new File("test/webapp-2.2");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Assert.assertEquals(2, context.getEffectiveMajorVersion());
Assert.assertEquals(2, context.getEffectiveMinorVersion());
}
@Test
public void testWebapp_2_3() throws Exception {
File appDir = new File("test/webapp-2.3");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Assert.assertEquals(2, context.getEffectiveMajorVersion());
Assert.assertEquals(3, context.getEffectiveMinorVersion());
}
@Test
public void testWebapp_2_4() throws Exception {
File appDir = new File("test/webapp-2.4");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Assert.assertEquals(2, context.getEffectiveMajorVersion());
Assert.assertEquals(4, context.getEffectiveMinorVersion());
}
@Test
public void testWebapp_2_5() throws Exception {
File appDir = new File("test/webapp-2.5");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Assert.assertEquals(2, context.getEffectiveMajorVersion());
Assert.assertEquals(5, context.getEffectiveMinorVersion());
}
@Test
public void testWebapp_3_0() throws Exception {
File appDir = new File("test/webapp-3.0");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Assert.assertEquals(3, context.getEffectiveMajorVersion());
Assert.assertEquals(0, context.getEffectiveMinorVersion());
}
@Test
public void testWebapp_3_1() throws Exception {
File appDir = new File("test/webapp-3.1");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Assert.assertEquals(3, context.getEffectiveMajorVersion());
Assert.assertEquals(1, context.getEffectiveMinorVersion());
}
@Test
public void testWebresources() throws Exception {
File appDir = new File("test/webresources/dir1");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Assert.assertEquals(3, context.getEffectiveMajorVersion());
Assert.assertEquals(1, context.getEffectiveMinorVersion());
}
@Test
public void testResourceJARs() throws Exception {
File appDir = new File("test/webapp-fragments");
JspCServletContext context = new JspCServletContext(
null, appDir.toURI().toURL(), null, false, false);
Set<String> paths = context.getResourcePaths("/");
Assert.assertEquals(10, paths.size());
Assert.assertTrue(paths.contains("/WEB-INF/"));
Assert.assertTrue(paths.contains("/folder/"));
Assert.assertTrue(paths.contains("/'singlequote.jsp"));
Assert.assertTrue(paths.contains("/'singlequote2.jsp"));
Assert.assertTrue(paths.contains("/bug51396.jsp"));
Assert.assertTrue(paths.contains("/jndi.jsp"));
Assert.assertTrue(paths.contains("/resourceA.jsp"));
Assert.assertTrue(paths.contains("/resourceB.jsp"));
Assert.assertTrue(paths.contains("/resourceF.jsp"));
Assert.assertTrue(paths.contains("/warDirContext.jsp"));
paths = context.getResourcePaths("/folder/");
Assert.assertEquals(3, paths.size());
Assert.assertTrue(paths.contains("/folder/resourceC.jsp"));
Assert.assertTrue(paths.contains("/folder/resourceD.jsp"));
Assert.assertTrue(paths.contains("/folder/resourceE.jsp"));
}
}

View File

@@ -0,0 +1,108 @@
/*
* 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.jasper.servlet;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
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;
import org.apache.tomcat.util.descriptor.web.ErrorPage;
public class TestJspServlet extends TomcatBaseTest {
@Test
public void testBug56568a() throws Exception {
Tomcat tomcat = getTomcatInstance();
// Use the test web application so JSP support is available and the
// default JSP error page can be used.
File appDir = new File("test/webapp");
Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
// Create a servlet that always throws an exception for a PUT request
Tomcat.addServlet(context, "Bug56568Servlet", new Bug56568aServlet());
context.addServletMappingDecoded("/bug56568", "Bug56568Servlet");
// Configure a JSP page to handle the 500 error response
// The JSP page will see the same method as the original request (PUT)
// PUT requests are normally blocked for JSPs
ErrorPage ep = new ErrorPage();
ep.setErrorCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ep.setLocation("/WEB-INF/jsp/error.jsp");
context.addErrorPage(ep);
tomcat.start();
int rc = methodUrl("http://localhost:" + getPort() + "/test/bug56568",
new ByteChunk(), 5000, null, null, "PUT");
// Make sure we get the original 500 response and not a 405 response
// which would indicate that error.jsp is complaining about being called
// with the PUT method.
Assert.assertEquals(500, rc);
}
@Test
public void testBug56568b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int rc = methodUrl("http://localhost:" + getPort() + "/test/jsp/error.jsp",
new ByteChunk(), 500000, null, null, "PUT");
// Make sure we get a 200 response and not a 405 response
// which would indicate that error.jsp is complaining about being called
// with the PUT method.
Assert.assertEquals(200, rc);
}
@Test
public void testBug56568c() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int rc = methodUrl("http://localhost:" + getPort() + "/test/jsp/test.jsp",
new ByteChunk(), 500000, null, null, "PUT");
// Make sure we get a 405 response which indicates that test.jsp is
// complaining about being called with the PUT method.
Assert.assertEquals(405, rc);
}
private static class Bug56568aServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
throw new ServletException();
}
}
}

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.jasper.servlet;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.juli.logging.LogFactory;
import org.apache.tomcat.Jar;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.scan.JarFactory;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.easymock.EasyMock;
public class TestTldScanner extends TomcatBaseTest {
@Test
public void testWithWebapp() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-3.0");
Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
tomcat.start();
TldScanner scanner =
new TldScanner(context.getServletContext(), true, true, true);
scanner.scan();
Assert.assertEquals(5, scanner.getUriTldResourcePathMap().size());
Assert.assertEquals(1, scanner.getListeners().size());
}
@Test
public void testBug55807() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
((StandardJarScanner) context.getJarScanner()).setScanAllDirectories(true);
tomcat.start();
ByteChunk res = new ByteChunk();
Map<String,List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug55807.jsp",
res, headers);
// Check request completed
String result = res.toString();
assertEcho(result, "OK");
// Check the dependencies count
Assert.assertTrue(result.contains("<p>DependenciesCount: 1</p>"));
// Check the right timestamp was used in the dependency
File tld = new File("test/webapp/WEB-INF/classes/META-INF/bug55807.tld");
String expected = "<p>/WEB-INF/classes/META-INF/bug55807.tld : " +
tld.lastModified() + "</p>";
Assert.assertTrue(result.contains(expected));
// Check content type
String contentType = getSingleHeader("Content-Type", headers);
Assert.assertTrue(contentType.startsWith("text/html"));
}
/** Assertion for text printed by tags:echo */
private static void assertEcho(String result, String expected) {
Assert.assertTrue(result, result.indexOf("<p>" + expected + "</p>") > 0);
}
@Test
public void testBug57647() throws Exception {
TldScanner scanner = EasyMock.createMock(TldScanner.class);
Field f = TldScanner.class.getDeclaredField("log");
f.setAccessible(true);
f.set(scanner, LogFactory.getLog(TldScanner.class));
Constructor<TldScanner.TldScannerCallback> constructor =
TldScanner.TldScannerCallback.class.getDeclaredConstructor(TldScanner.class);
constructor.setAccessible(true);
TldScanner.TldScannerCallback callback = constructor.newInstance(scanner);
File webappDir = new File("webapps/examples");
Assert.assertFalse(callback.scanFoundNoTLDs());
scan(callback, webappDir, "WEB-INF/lib/taglibs-standard-spec-1.2.5.jar");
Assert.assertTrue(callback.scanFoundNoTLDs());
scan(callback, webappDir, "WEB-INF/lib/taglibs-standard-impl-1.2.5.jar");
Assert.assertTrue(callback.scanFoundNoTLDs());
}
private static void scan(TldScanner.TldScannerCallback callback, File webapp, String path)
throws Exception {
String fullPath = new File(webapp, path).toURI().toString();
URL jarUrl = new URL("jar:" + fullPath + "!/");
try (Jar jar = JarFactory.newInstance(jarUrl)) {
callback.scan(jar, path, true);
}
}
}

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.jasper.tagplugins.jstl.core;
import java.io.File;
import org.junit.Assert;
import org.junit.Before;
import org.apache.catalina.Context;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.catalina.webresources.StandardRoot;
public abstract class AbstractTestTag extends TomcatBaseTest {
@Override
@Before
public void setUp() throws Exception {
super.setUp();
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
ctx.setResources(new StandardRoot(ctx));
// Add the JSTL (we need the TLD)
File lib = new File("webapps/examples/WEB-INF/lib");
ctx.getResources().createWebResourceSet(
WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib",
lib.getAbsolutePath(), null, "/");
// Configure the use of the plug-in rather than the standard impl
File plugin = new File(
"java/org/apache/jasper/tagplugins/jstl/tagPlugins.xml");
Assert.assertTrue(plugin.isFile());
ctx.getResources().createWebResourceSet(
WebResourceRoot.ResourceSetType.POST, "/WEB-INF/tagPlugins.xml",
plugin.getAbsolutePath(), null, "/");
tomcat.start();
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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.jasper.tagplugins.jstl.core;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestForEach extends AbstractTestTag {
@Test
public void testBug54242() throws Exception {
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54242.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("OK - 1"));
Assert.assertTrue(body.contains("OK - 2"));
Assert.assertFalse(body.contains("FAIL"));
}
@Test
public void testBug54888() throws Exception {
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54888.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("OK - 1"));
Assert.assertTrue(body.contains("OK - 2"));
Assert.assertTrue(body.contains("OK - 3"));
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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.jasper.tagplugins.jstl.core;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestOut extends AbstractTestTag {
@Test
public void testBug54011() throws Exception {
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54011.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("OK - 1"));
Assert.assertTrue(body.contains("OK - 2"));
}
@Test
public void testBug54144() throws Exception {
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54144.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("OK - 1"));
Assert.assertTrue(body.contains("OK - 2"));
Assert.assertTrue(body.contains("OK - 3"));
Assert.assertTrue(body.contains("OK - 4"));
Assert.assertFalse(body.contains("FAIL"));
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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.jasper.tagplugins.jstl.core;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestSet extends AbstractTestTag {
@Test
public void testBug54011() throws Exception {
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54012.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("OK"));
}
@Test
public void testBug54338() throws Exception {
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug54338.jsp", res, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String body = res.toString();
Assert.assertTrue(body.contains("OK - 42"));
}
}

View File

@@ -0,0 +1,197 @@
/*
* 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.jasper.util;
import org.junit.Assert;
import org.junit.Test;
public class TestFastRemovalDequeue {
@Test
public void testSinglePushPop() throws Exception {
FastRemovalDequeue<Object> q = new FastRemovalDequeue<>(2);
Object o1 = new Object();
q.push(o1);
Object r = q.pop();
Assert.assertEquals(o1, r);
Assert.assertNull(q.first);
Assert.assertNull(q.last);
}
@Test
public void testDoublePushPop() throws Exception {
FastRemovalDequeue<Object> q = new FastRemovalDequeue<>(2);
Object o1 = new Object();
Object o2 = new Object();
q.push(o1);
q.push(o2);
Assert.assertEquals(o2, q.first.getContent());
Assert.assertEquals(o1, q.last.getContent());
Object r1 = q.pop();
Assert.assertEquals(o1, r1);
Assert.assertEquals(o2, q.first.getContent());
Assert.assertEquals(o2, q.last.getContent());
Object r2 = q.pop();
Assert.assertEquals(o2, r2);
Assert.assertNull(q.first);
Assert.assertNull(q.last);
}
@Test
public void testSingleUnpopPop() throws Exception {
FastRemovalDequeue<Object> q = new FastRemovalDequeue<>(2);
Object o1 = new Object();
q.unpop(o1);
Object r = q.pop();
Assert.assertEquals(o1, r);
Assert.assertNull(q.first);
Assert.assertNull(q.last);
}
@Test
public void testDoubleUnpopPop() throws Exception {
FastRemovalDequeue<Object> q = new FastRemovalDequeue<>(2);
Object o1 = new Object();
Object o2 = new Object();
q.unpop(o1);
q.unpop(o2);
Assert.assertEquals(o1, q.first.getContent());
Assert.assertEquals(o2, q.last.getContent());
Object r2 = q.pop();
Assert.assertEquals(o2, r2);
Assert.assertEquals(o1, q.first.getContent());
Assert.assertEquals(o1, q.last.getContent());
Object r1 = q.pop();
Assert.assertEquals(o1, r1);
Assert.assertNull(q.first);
Assert.assertNull(q.last);
}
@Test
public void testSinglePushUnpush() throws Exception {
FastRemovalDequeue<Object> q = new FastRemovalDequeue<>(2);
Object o1 = new Object();
q.push(o1);
Object r = q.unpush();
Assert.assertEquals(o1, r);
Assert.assertNull(q.first);
Assert.assertNull(q.last);
}
@Test
public void testDoublePushUnpush() throws Exception {
FastRemovalDequeue<Object> q = new FastRemovalDequeue<>(2);
Object o1 = new Object();
Object o2 = new Object();
q.push(o1);
q.push(o2);
Assert.assertEquals(o2, q.first.getContent());
Assert.assertEquals(o1, q.last.getContent());
Object r2 = q.unpush();
Assert.assertEquals(o2, r2);
Assert.assertEquals(o1, q.first.getContent());
Assert.assertEquals(o1, q.last.getContent());
Object r1 = q.unpush();
Assert.assertEquals(o1, r1);
Assert.assertNull(q.first);
Assert.assertNull(q.last);
}
@Test
public void testSinglePushRemove() throws Exception {
FastRemovalDequeue<Object> q = new FastRemovalDequeue<>(2);
Object o1 = new Object();
FastRemovalDequeue<Object>.Entry e1 = q.push(o1);
Assert.assertEquals(o1, e1.getContent());
q.remove(e1);
Assert.assertNull(q.first);
Assert.assertNull(q.last);
}
@Test
public void testDoublePushRemove() throws Exception {
FastRemovalDequeue<Object> q = new FastRemovalDequeue<>(2);
Object o1 = new Object();
Object o2 = new Object();
FastRemovalDequeue<Object>.Entry e1 = q.push(o1);
FastRemovalDequeue<Object>.Entry e2 = q.push(o2);
Assert.assertEquals(o1, e1.getContent());
Assert.assertEquals(o2, e2.getContent());
Assert.assertEquals(o2, q.first.getContent());
Assert.assertEquals(o1, q.last.getContent());
q.remove(e1);
Assert.assertEquals(o2, q.first.getContent());
Assert.assertEquals(o2, q.last.getContent());
q.remove(e2);
Assert.assertNull(q.first);
Assert.assertNull(q.last);
}
}