1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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<cd", HtmlQuoting.quoteHtmlChars("ab<cd"));
45 assertEquals("ab>", HtmlQuoting.quoteHtmlChars("ab>"));
46 assertEquals("&&&", HtmlQuoting.quoteHtmlChars("&&&"));
47 assertEquals(" '\n", HtmlQuoting.quoteHtmlChars(" '\n"));
48 assertEquals(""", 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<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<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 }