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  
18  package org.apache.hadoop.hbase.http.jmx;
19  
20  import java.net.URL;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.apache.hadoop.hbase.http.HttpServer;
28  import org.apache.hadoop.hbase.http.HttpServerFunctionalTest;
29  import org.junit.AfterClass;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(SmallTests.class)
35  public class TestJMXJsonServlet extends HttpServerFunctionalTest {
36    private   static final Log LOG = LogFactory.getLog(TestJMXJsonServlet.class);
37    private static HttpServer server;
38    private static URL baseUrl;
39  
40    @BeforeClass public static void setup() throws Exception {
41      server = createTestServer();
42      server.start();
43      baseUrl = getServerURL(server);
44    }
45    
46    @AfterClass public static void cleanup() throws Exception {
47      server.stop();
48    }
49    
50    public static void assertReFind(String re, String value) {
51      Pattern p = Pattern.compile(re);
52      Matcher m = p.matcher(value);
53      assertTrue("'"+p+"' does not match "+value, m.find());
54    }
55    
56    @Test public void testQuery() throws Exception {
57      String result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Runtime"));
58      LOG.info("/jmx?qry=java.lang:type=Runtime RESULT: "+result);
59      assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Runtime\"", result);
60      assertReFind("\"modelerType\"", result);
61      
62      result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory"));
63      LOG.info("/jmx?qry=java.lang:type=Memory RESULT: "+result);
64      assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
65      assertReFind("\"modelerType\"", result);
66      
67      result = readOutput(new URL(baseUrl, "/jmx"));
68      LOG.info("/jmx RESULT: "+result);
69      assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
70      
71      // test to get an attribute of a mbean
72      result = readOutput(new URL(baseUrl, 
73          "/jmx?get=java.lang:type=Memory::HeapMemoryUsage"));
74      LOG.info("/jmx RESULT: "+result);
75      assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
76      assertReFind("\"committed\"\\s*:", result);
77      
78      // negative test to get an attribute of a mbean
79      result = readOutput(new URL(baseUrl, 
80          "/jmx?get=java.lang:type=Memory::"));
81      LOG.info("/jmx RESULT: "+result);
82      assertReFind("\"ERROR\"", result);
83  
84      // test to get JSONP result
85      result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory&callback=mycallback1"));
86      LOG.info("/jmx?qry=java.lang:type=Memory&callback=mycallback RESULT: "+result);
87      assertReFind("^mycallback1\\(\\{", result);
88      assertReFind("\\}\\);$", result);
89  
90      // negative test to get an attribute of a mbean as JSONP
91      result = readOutput(new URL(baseUrl,
92          "/jmx?get=java.lang:type=Memory::&callback=mycallback2"));
93      LOG.info("/jmx RESULT: "+result);
94      assertReFind("^mycallback2\\(\\{", result);
95      assertReFind("\"ERROR\"", result);
96      assertReFind("\\}\\);$", result);
97  
98      // test to get an attribute of a mbean as JSONP
99      result = readOutput(new URL(baseUrl,
100         "/jmx?get=java.lang:type=Memory::HeapMemoryUsage&callback=mycallback3"));
101     LOG.info("/jmx RESULT: "+result);
102     assertReFind("^mycallback3\\(\\{", result);
103     assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
104     assertReFind("\"committed\"\\s*:", result);
105     assertReFind("\\}\\);$", result);
106 
107     // test to get XSS JSONP result
108     result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory&callback=<script>alert('hello')</script>"));
109     LOG.info("/jmx?qry=java.lang:type=Memory&callback=<script>alert('hello')</script> RESULT: "+result);
110     assertTrue(!result.contains("<script>"));
111 
112 
113   }
114 }