1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5042364
  27  * @summary Malformed MLet text file causes infinite loop in parser.
  28  * The MLetParser goes into an infinite loop when a tag is not
  29  * terminated with the corresponding '>' and an opening '<' for
  30  * the subsequent tag is encountered.
  31  * @author Luis-Miguel Alventosa
  32  * @modules java.management
  33  * @run clean ParserInfiniteLoopTest
  34  * @run build ParserInfiniteLoopTest
  35  * @run main/othervm/timeout=5 ParserInfiniteLoopTest mlet1.html
  36  * @run main/othervm/timeout=5 ParserInfiniteLoopTest mlet2.html
  37  * @run main/othervm/timeout=5 ParserInfiniteLoopTest mlet3.html
  38  */
  39 
  40 import java.io.File;
  41 import java.io.IOException;
  42 import javax.management.MBeanServer;
  43 import javax.management.MBeanServerFactory;
  44 import javax.management.ObjectName;
  45 import javax.management.ServiceNotFoundException;
  46 import javax.management.loading.MLet;
  47 
  48 public class ParserInfiniteLoopTest {
  49 
  50     public static void main(String[] args) throws Exception {
  51 
  52         boolean error = false;
  53 
  54         // Instantiate the MBean server
  55         //
  56         System.out.println("Create the MBean server");
  57         MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  58 
  59         // Instantiate an MLet
  60         //
  61         System.out.println("Create the MLet");
  62         MLet mlet = new MLet();
  63 
  64         // Register the MLet MBean with the MBeanServer
  65         //
  66         System.out.println("Register the MLet MBean");
  67         ObjectName mletObjectName = new ObjectName("Test:type=MLet");
  68         mbs.registerMBean(mlet, mletObjectName);
  69 
  70         // Call getMBeansFromURL
  71         //
  72         System.out.println("Call mlet.getMBeansFromURL(<url>)");
  73         String testSrc = System.getProperty("test.src");
  74         System.out.println("test.src = " + testSrc);
  75         String urlCodebase;
  76         if (testSrc.startsWith("/")) {
  77             urlCodebase =
  78                 "file:" + testSrc.replace(File.separatorChar, '/') + "/";
  79         } else {
  80             urlCodebase =
  81                 "file:/" + testSrc.replace(File.separatorChar, '/') + "/";
  82         }
  83         String mletFile = urlCodebase + args[0];
  84         System.out.println("MLet File = " + mletFile);
  85         try {
  86             mlet.getMBeansFromURL(mletFile);
  87             System.out.println(
  88                 "TEST FAILED: Expected ServiceNotFoundException not thrown");
  89             error = true;
  90         } catch (ServiceNotFoundException e) {
  91             if (e.getCause() == null) {
  92                 System.out.println("TEST FAILED: Got unexpected null cause " +
  93                     "in ServiceNotFoundException");
  94                 error = true;
  95             } else if (!(e.getCause() instanceof IOException)) {
  96                 System.out.println("TEST FAILED: Got unexpected non-null " +
  97                     "cause in ServiceNotFoundException");
  98                 error = true;
  99             } else {
 100                 System.out.println("TEST PASSED: Got expected non-null " +
 101                     "cause in ServiceNotFoundException");
 102                 error = false;
 103             }
 104             e.printStackTrace(System.out);
 105         }
 106 
 107         // Unregister the MLet MBean
 108         //
 109         System.out.println("Unregister the MLet MBean");
 110         mbs.unregisterMBean(mletObjectName);
 111 
 112         // Release MBean server
 113         //
 114         System.out.println("Release the MBean server");
 115         MBeanServerFactory.releaseMBeanServer(mbs);
 116 
 117         // End Test
 118         //
 119         System.out.println("Bye! Bye!");
 120         if (error) System.exit(1);
 121     }
 122 }