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 public void testNeedsQuoting() throws Exception {
041    assertTrue(HtmlQuoting.needsQuoting("abcde>"));
042    assertTrue(HtmlQuoting.needsQuoting("<abcde"));
043    assertTrue(HtmlQuoting.needsQuoting("abc'de"));
044    assertTrue(HtmlQuoting.needsQuoting("abcde\""));
045    assertTrue(HtmlQuoting.needsQuoting("&"));
046    assertFalse(HtmlQuoting.needsQuoting(""));
047    assertFalse(HtmlQuoting.needsQuoting("ab\ncdef"));
048    assertFalse(HtmlQuoting.needsQuoting(null));
049  }
050
051  @Test public void testQuoting() throws Exception {
052    assertEquals("ab&lt;cd", HtmlQuoting.quoteHtmlChars("ab<cd"));
053    assertEquals("ab&gt;", HtmlQuoting.quoteHtmlChars("ab>"));
054    assertEquals("&amp;&amp;&amp;", HtmlQuoting.quoteHtmlChars("&&&"));
055    assertEquals(" &apos;\n", HtmlQuoting.quoteHtmlChars(" '\n"));
056    assertEquals("&quot;", HtmlQuoting.quoteHtmlChars("\""));
057    assertEquals(null, HtmlQuoting.quoteHtmlChars(null));
058  }
059
060  private void runRoundTrip(String str) throws Exception {
061    assertEquals(str,
062                 HtmlQuoting.unquoteHtmlChars(HtmlQuoting.quoteHtmlChars(str)));
063  }
064
065  @Test public void testRoundtrip() throws Exception {
066    runRoundTrip("");
067    runRoundTrip("<>&'\"");
068    runRoundTrip("ab>cd<ef&ghi'\"");
069    runRoundTrip("A string\n with no quotable chars in it!");
070    runRoundTrip(null);
071    StringBuilder buffer = new StringBuilder();
072    for(char ch=0; ch < 127; ++ch) {
073      buffer.append(ch);
074    }
075    runRoundTrip(buffer.toString());
076  }
077
078  @Test
079  public void testRequestQuoting() throws Exception {
080    HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
081    HttpServer.QuotingInputFilter.RequestQuoter quoter =
082      new HttpServer.QuotingInputFilter.RequestQuoter(mockReq);
083
084    Mockito.doReturn("a<b").when(mockReq).getParameter("x");
085    assertEquals("Test simple param quoting",
086        "a&lt;b", quoter.getParameter("x"));
087
088    Mockito.doReturn(null).when(mockReq).getParameter("x");
089    assertEquals("Test that missing parameters dont cause NPE",
090        null, quoter.getParameter("x"));
091
092    Mockito.doReturn(new String[]{"a<b", "b"}).when(mockReq).getParameterValues("x");
093    assertArrayEquals("Test escaping of an array",
094        new String[]{"a&lt;b", "b"}, quoter.getParameterValues("x"));
095
096    Mockito.doReturn(null).when(mockReq).getParameterValues("x");
097    assertArrayEquals("Test that missing parameters dont cause NPE for array",
098        null, quoter.getParameterValues("x"));
099  }
100}