View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test class for {@link RemoteAdmin} to verify XML is parsed in a certain manner.
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  }