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 org.apache.hadoop.hbase.HBaseConfiguration;
28 import org.apache.hadoop.hbase.rest.Constants;
29 import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
30 import org.apache.hadoop.hbase.testclassification.SmallTests;
31 import org.apache.hadoop.util.StringUtils;
32 import org.junit.Test;
33 import org.junit.experimental.categories.Category;
34
35
36
37
38 @Category(SmallTests.class)
39 public class TestXmlParsing {
40
41 @Test
42 public void testParsingClusterVersion() throws Exception {
43 final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
44 + "<ClusterVersion>2.0.0</ClusterVersion>";
45 Client client = mock(Client.class);
46 RemoteAdmin admin = new RemoteAdmin(client, HBaseConfiguration.create(), null);
47 Response resp = new Response(200, null, xml.getBytes());
48
49 when(client.get("/version/cluster", Constants.MIMETYPE_XML)).thenReturn(resp);
50
51 StorageClusterVersionModel cv = admin.getClusterVersion();
52 assertEquals("2.0.0", cv.getVersion());
53 }
54
55 @Test
56 public void testFailOnExternalEntities() throws Exception {
57 final String externalEntitiesXml =
58 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
59 + " <!DOCTYPE foo [ <!ENTITY xxe SYSTEM \"/tmp/foo\"> ] >"
60 + " <ClusterVersion>&xee;</ClusterVersion>";
61 Client client = mock(Client.class);
62 RemoteAdmin admin = new RemoteAdmin(client, HBaseConfiguration.create(), null);
63 Response resp = new Response(200, null, externalEntitiesXml.getBytes());
64
65 when(client.get("/version/cluster", Constants.MIMETYPE_XML)).thenReturn(resp);
66
67 try {
68 admin.getClusterVersion();
69 fail("Expected getClusterVersion() to throw an exception");
70 } catch (IOException e) {
71 final String exceptionText = StringUtils.stringifyException(e);
72 final String expectedText = "The entity \"xee\" was referenced, but not declared.";
73 assertTrue("Exception does not contain expected text", exceptionText.contains(expectedText));
74 }
75 }
76 }