1   package org.apache.velocity.test;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.    
19   */
20  import org.apache.velocity.runtime.RuntimeConstants;
21  
22  
23  /**
24   * Test Strict escape mode
25   * property: RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE set to true
26   */
27  public class StrictEscapeTestCase extends BaseTestCase
28  {
29    public StrictEscapeTestCase(String name)
30    {
31        super(name);
32    }
33  
34    public void setUp() throws Exception
35    {
36        super.setUp();
37        engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE, Boolean.TRUE);
38        context.put("pow", "bang");
39        context.put("NULL", null);
40        context.put("animal", new Animal());
41        // DEBUG = true;
42    }
43    
44    public void testReferenceEscape()
45    {
46        engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);
47  
48        assertEvalException("\\\\$bogus");
49        assertEvalException("\\\\\\\\$bogus");
50      
51        assertEvalEquals("$bogus", "\\$bogus");
52        assertEvalEquals("$bogus.xyz", "\\$bogus.xyz");
53        assertEvalEquals("${bogus}", "\\${bogus}");
54        assertEvalEquals("${bogus.xyz}", "\\${bogus.xyz}");
55        assertEvalEquals("\\$bogus", "\\\\\\$bogus");
56        assertEvalEquals("\\xyz","#set($foo = \"xyz\")\\\\$foo");
57        assertEvalEquals("\\$foo","#set($foo = \"xyz\")\\\\\\$foo");
58        assertEvalEquals("$foo\\","#set($foo = \"xyz\")\\$foo\\");
59        assertEvalEquals("$pow", "#set($foo = \"\\$pow\")$foo");
60        assertEvalEquals("\\bang", "#set($foo = \"\\\\$pow\")$foo");
61        assertEvalEquals("\\$pow", "#set($foo = \"\\\\\\$pow\")$foo");
62        
63        assertEvalEquals("\\$%", "\\$%");
64        
65        // This should work but does not... may be related to VELOCITY-679
66        // This is broken from existing escape behavior
67        // assertEvalEquals("\\$bang", "\\$$pow");
68        
69        assertEvalEquals("$!foo", "#set($foo = $NULL)\\$!foo");
70        assertEvalEquals("$!animal.null", "\\$!animal.null");
71        assertEvalEquals("$!animal", "\\$!animal");
72    } 
73      
74    public void testMacroEscape()
75    {
76        engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);
77        assertEvalException("\\\\#bogus()");
78  
79        // define the foo macro
80        assertEvalEquals("", "#macro(foo)bar#end");
81        
82        assertEvalEquals("#foo()", "\\#foo()");
83        assertEvalEquals("\\bar", "\\\\#foo()");
84        assertEvalEquals("\\#foo()", "\\\\\\#foo()");
85              
86        assertEvalEquals("bar", "#set($abc = \"#foo()\")$abc");
87        assertEvalEquals("#foo()", "#set($abc = \"\\#foo()\")$abc");
88        assertEvalEquals("\\bar", "#set($abc = \"\\\\#foo()\")$abc");
89        
90        assertEvalEquals("#@foo()", "\\#@foo()");
91        assertEvalEquals("\\bar", "\\\\#@foo()#end");
92        assertEvalEquals("#@foo()#end", "\\#@foo()\\#end");
93        
94        assertEvalEquals("#end #foreach #define() #elseif", "\\#end \\#foreach \\#define() \\#elseif");
95        assertEvalEquals("#{end} #{foreach} #{define}() #{elseif}", "\\#{end} \\#{foreach} \\#{define}() \\#{elseif}");
96        assertEvalEquals("#macro(foo) #end", "\\#macro(foo) \\#end");
97              
98        assertEvalException("\\\\#end");
99        assertEvalException("\\\\#if()");
100       
101       // This should work but does not, probably related to VELOCITY-678
102       // this is broken from existing behavior
103       //assertEvalEquals("\\$bar", "\\$#foo()");
104   }
105 
106   /**
107    * Tests for non strict-mode
108    */
109   public void testStrictMode()
110   {
111       assertEvalEquals("#bogus()", "\\#bogus()");
112       assertEvalEquals("\\#bogus", "\\\\#bogus");
113       
114       assertEvalEquals("\\$bogus", "\\\\$bogus");
115       assertEvalEquals("\\\\$bogus", "\\\\\\\\$bogus");
116       assertEvalEquals("\\$bogus", "#set($foo = \"\\\\$bogus\")$foo");    
117   }
118 
119   /**
120    * Test object for escaping
121    */
122   public static class Animal
123   {      
124       public Object getNull()
125       {
126           return null;
127       }
128       
129       public String toString()
130       {
131           return null;
132       }
133   }
134 }