View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.http;
19  
20  import static org.junit.Assert.*;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  import org.mockito.Mockito;
28  
29  @Category(SmallTests.class)
30  public class TestHtmlQuoting {
31  
32    @Test public void testNeedsQuoting() throws Exception {
33      assertTrue(HtmlQuoting.needsQuoting("abcde>"));
34      assertTrue(HtmlQuoting.needsQuoting("<abcde"));
35      assertTrue(HtmlQuoting.needsQuoting("abc'de"));
36      assertTrue(HtmlQuoting.needsQuoting("abcde\""));
37      assertTrue(HtmlQuoting.needsQuoting("&"));
38      assertFalse(HtmlQuoting.needsQuoting(""));
39      assertFalse(HtmlQuoting.needsQuoting("ab\ncdef"));
40      assertFalse(HtmlQuoting.needsQuoting(null));
41    }
42  
43    @Test public void testQuoting() throws Exception {
44      assertEquals("ab&lt;cd", HtmlQuoting.quoteHtmlChars("ab<cd"));
45      assertEquals("ab&gt;", HtmlQuoting.quoteHtmlChars("ab>"));
46      assertEquals("&amp;&amp;&amp;", HtmlQuoting.quoteHtmlChars("&&&"));
47      assertEquals(" &apos;\n", HtmlQuoting.quoteHtmlChars(" '\n"));
48      assertEquals("&quot;", HtmlQuoting.quoteHtmlChars("\""));
49      assertEquals(null, HtmlQuoting.quoteHtmlChars(null));
50    }
51  
52    private void runRoundTrip(String str) throws Exception {
53      assertEquals(str, 
54                   HtmlQuoting.unquoteHtmlChars(HtmlQuoting.quoteHtmlChars(str)));
55    }
56    
57    @Test public void testRoundtrip() throws Exception {
58      runRoundTrip("");
59      runRoundTrip("<>&'\"");
60      runRoundTrip("ab>cd<ef&ghi'\"");
61      runRoundTrip("A string\n with no quotable chars in it!");
62      runRoundTrip(null);
63      StringBuilder buffer = new StringBuilder();
64      for(char ch=0; ch < 127; ++ch) {
65        buffer.append(ch);
66      }
67      runRoundTrip(buffer.toString());
68    }
69    
70  
71    @Test
72    public void testRequestQuoting() throws Exception {
73      HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
74      HttpServer.QuotingInputFilter.RequestQuoter quoter =
75        new HttpServer.QuotingInputFilter.RequestQuoter(mockReq);
76      
77      Mockito.doReturn("a<b").when(mockReq).getParameter("x");
78      assertEquals("Test simple param quoting",
79          "a&lt;b", quoter.getParameter("x"));
80      
81      Mockito.doReturn(null).when(mockReq).getParameter("x");
82      assertEquals("Test that missing parameters dont cause NPE",
83          null, quoter.getParameter("x"));
84  
85      Mockito.doReturn(new String[]{"a<b", "b"}).when(mockReq).getParameterValues("x");
86      assertArrayEquals("Test escaping of an array",
87          new String[]{"a&lt;b", "b"}, quoter.getParameterValues("x"));
88  
89      Mockito.doReturn(null).when(mockReq).getParameterValues("x");
90      assertArrayEquals("Test that missing parameters dont cause NPE for array",
91          null, quoter.getParameterValues("x"));
92    }
93  }