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.assertNull;
024import static org.junit.Assert.assertTrue;
025
026import javax.servlet.http.HttpServletRequest;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.testclassification.MiscTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.mockito.Mockito;
034
035@Category({ MiscTests.class, SmallTests.class })
036public class TestHtmlQuoting {
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestHtmlQuoting.class);
040
041  @Test
042  public void testNeedsQuoting() throws Exception {
043    assertTrue(HtmlQuoting.needsQuoting("abcde>"));
044    assertTrue(HtmlQuoting.needsQuoting("<abcde"));
045    assertTrue(HtmlQuoting.needsQuoting("abc'de"));
046    assertTrue(HtmlQuoting.needsQuoting("abcde\""));
047    assertTrue(HtmlQuoting.needsQuoting("&"));
048    assertFalse(HtmlQuoting.needsQuoting(""));
049    assertFalse(HtmlQuoting.needsQuoting("ab\ncdef"));
050    assertFalse(HtmlQuoting.needsQuoting(null));
051  }
052
053  @Test
054  public void testQuoting() throws Exception {
055    assertEquals("ab&lt;cd", HtmlQuoting.quoteHtmlChars("ab<cd"));
056    assertEquals("ab&gt;", HtmlQuoting.quoteHtmlChars("ab>"));
057    assertEquals("&amp;&amp;&amp;", HtmlQuoting.quoteHtmlChars("&&&"));
058    assertEquals(" &apos;\n", HtmlQuoting.quoteHtmlChars(" '\n"));
059    assertEquals("&quot;", HtmlQuoting.quoteHtmlChars("\""));
060    assertEquals(null, HtmlQuoting.quoteHtmlChars(null));
061  }
062
063  private void runRoundTrip(String str) throws Exception {
064    assertEquals(str, HtmlQuoting.unquoteHtmlChars(HtmlQuoting.quoteHtmlChars(str)));
065  }
066
067  @Test
068  public void testRoundtrip() throws Exception {
069    runRoundTrip("");
070    runRoundTrip("<>&'\"");
071    runRoundTrip("ab>cd<ef&ghi'\"");
072    runRoundTrip("A string\n with no quotable chars in it!");
073    runRoundTrip(null);
074    StringBuilder buffer = new StringBuilder();
075    for (char ch = 0; ch < 127; ++ch) {
076      buffer.append(ch);
077    }
078    runRoundTrip(buffer.toString());
079  }
080
081  @Test
082  public void testRequestQuoting() throws Exception {
083    HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
084    HttpServer.QuotingInputFilter.RequestQuoter quoter =
085      new HttpServer.QuotingInputFilter.RequestQuoter(mockReq);
086
087    Mockito.doReturn("a<b").when(mockReq).getParameter("x");
088    assertEquals("Test simple param quoting", "a&lt;b", quoter.getParameter("x"));
089
090    Mockito.doReturn(null).when(mockReq).getParameter("x");
091    assertEquals("Test that missing parameters dont cause NPE", null, quoter.getParameter("x"));
092
093    Mockito.doReturn(new String[] { "a<b", "b" }).when(mockReq).getParameterValues("x");
094    assertArrayEquals("Test escaping of an array", new String[] { "a&lt;b", "b" },
095      quoter.getParameterValues("x"));
096
097    Mockito.doReturn(null).when(mockReq).getParameterValues("x");
098    assertNull("Test that missing parameters dont cause NPE for array",
099      quoter.getParameterValues("x"));
100  }
101}