001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.http;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertTrue;
024
025import javax.servlet.http.HttpServletRequest;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.MiscTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032import org.mockito.Mockito;
033
034@Category({ MiscTests.class, SmallTests.class })
035public class TestHtmlQuoting {
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestHtmlQuoting.class);
039
040  @Test
041  public void testNeedsQuoting() throws Exception {
042    assertTrue(HtmlQuoting.needsQuoting("abcde>"));
043    assertTrue(HtmlQuoting.needsQuoting("<abcde"));
044    assertTrue(HtmlQuoting.needsQuoting("abc'de"));
045    assertTrue(HtmlQuoting.needsQuoting("abcde\""));
046    assertTrue(HtmlQuoting.needsQuoting("&"));
047    assertFalse(HtmlQuoting.needsQuoting(""));
048    assertFalse(HtmlQuoting.needsQuoting("ab\ncdef"));
049    assertFalse(HtmlQuoting.needsQuoting(null));
050  }
051
052  @Test
053  public void testQuoting() throws Exception {
054    assertEquals("ab&lt;cd", HtmlQuoting.quoteHtmlChars("ab<cd"));
055    assertEquals("ab&gt;", HtmlQuoting.quoteHtmlChars("ab>"));
056    assertEquals("&amp;&amp;&amp;", HtmlQuoting.quoteHtmlChars("&&&"));
057    assertEquals(" &apos;\n", HtmlQuoting.quoteHtmlChars(" '\n"));
058    assertEquals("&quot;", HtmlQuoting.quoteHtmlChars("\""));
059    assertEquals(null, HtmlQuoting.quoteHtmlChars(null));
060  }
061
062  private void runRoundTrip(String str) throws Exception {
063    assertEquals(str, HtmlQuoting.unquoteHtmlChars(HtmlQuoting.quoteHtmlChars(str)));
064  }
065
066  @Test
067  public void testRoundtrip() throws Exception {
068    runRoundTrip("");
069    runRoundTrip("<>&'\"");
070    runRoundTrip("ab>cd<ef&ghi'\"");
071    runRoundTrip("A string\n with no quotable chars in it!");
072    runRoundTrip(null);
073    StringBuilder buffer = new StringBuilder();
074    for (char ch = 0; ch < 127; ++ch) {
075      buffer.append(ch);
076    }
077    runRoundTrip(buffer.toString());
078  }
079
080  @Test
081  public void testRequestQuoting() throws Exception {
082    HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
083    HttpServer.QuotingInputFilter.RequestQuoter quoter =
084      new HttpServer.QuotingInputFilter.RequestQuoter(mockReq);
085
086    Mockito.doReturn("a<b").when(mockReq).getParameter("x");
087    assertEquals("Test simple param quoting", "a&lt;b", quoter.getParameter("x"));
088
089    Mockito.doReturn(null).when(mockReq).getParameter("x");
090    assertEquals("Test that missing parameters dont cause NPE", null, quoter.getParameter("x"));
091
092    Mockito.doReturn(new String[] { "a<b", "b" }).when(mockReq).getParameterValues("x");
093    assertArrayEquals("Test escaping of an array", new String[] { "a&lt;b", "b" },
094      quoter.getParameterValues("x"));
095
096    Mockito.doReturn(null).when(mockReq).getParameterValues("x");
097    assertArrayEquals("Test that missing parameters dont cause NPE for array", null,
098      quoter.getParameterValues("x"));
099  }
100}