1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.rest.client;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24
25 import java.io.IOException;
26
27 import javax.xml.bind.UnmarshalException;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.HBaseConfiguration;
32 import org.apache.hadoop.hbase.rest.Constants;
33 import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
34 import org.apache.hadoop.hbase.testclassification.SmallTests;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.apache.hadoop.util.StringUtils;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40
41
42
43 @Category(SmallTests.class)
44 public class TestXmlParsing {
45 private static final Log LOG = LogFactory.getLog(TestXmlParsing.class);
46
47 @Test
48 public void testParsingClusterVersion() throws Exception {
49 final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
50 + "<ClusterVersion Version=\"2.0.0\"/>";
51 Client client = mock(Client.class);
52 RemoteAdmin admin = new RemoteAdmin(client, HBaseConfiguration.create(), null);
53 Response resp = new Response(200, null, Bytes.toBytes(xml));
54
55 when(client.get("/version/cluster", Constants.MIMETYPE_XML)).thenReturn(resp);
56
57 StorageClusterVersionModel cv = admin.getClusterVersion();
58 assertEquals("2.0.0", cv.getVersion());
59 }
60
61 @Test
62 public void testFailOnExternalEntities() throws Exception {
63 final String externalEntitiesXml =
64 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
65 + " <!DOCTYPE foo [ <!ENTITY xxe SYSTEM \"/tmp/foo\"> ] >"
66 + " <ClusterVersion>&xee;</ClusterVersion>";
67 Client client = mock(Client.class);
68 RemoteAdmin admin = new RemoteAdmin(client, HBaseConfiguration.create(), null);
69 Response resp = new Response(200, null, Bytes.toBytes(externalEntitiesXml));
70
71 when(client.get("/version/cluster", Constants.MIMETYPE_XML)).thenReturn(resp);
72
73 try {
74 admin.getClusterVersion();
75 fail("Expected getClusterVersion() to throw an exception");
76 } catch (IOException e) {
77 assertEquals("Cause of exception ought to be a failure to parse the stream due to our " +
78 "invalid external entity. Make sure this isn't just a false positive due to " +
79 "implementation. see HBASE-19020.", UnmarshalException.class, e.getCause().getClass());
80 final String exceptionText = StringUtils.stringifyException(e);
81 final String expectedText = "\"xee\"";
82 LOG.debug("exception text: '" + exceptionText + "'", e);
83 assertTrue("Exception does not contain expected text", exceptionText.contains(expectedText));
84 }
85 }
86 }