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